flock, fcntl record locks

There were none: fcntl answered F_SETLK with EINVAL and flock was not a
system call. Alpine's apk, which runs unmodified on SchOS through
Alpine's own musl, locks its database before reading it and stopped
there.

kernel/fs/lock.c keeps one table of advisory locks keyed by (dev, inum):
flock() for the whole file, owned by the open file description;
F_SETLK/F_SETLKW/F_GETLK for byte ranges, owned by the process and
released when it closes any descriptor on the file or exits; and the
F_OFD_* forms, ranges owned by the description. flock and record locks
do not see each other, as on Linux. Waiters sleep until any release and
are interrupted by a signal.

cmd/locktest checks each between a parent and a child, including range
splitting, F_GETLK's report, and a blocked waiter being woken.

shell.nix gains pkg-config, meson, ninja, m4, bison, flex and xz, for
the X ports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1giNvrHTrPMPw7V6x1kQ4
This commit is contained in:
Nexoniarz 2026-10-24 12:22:36 +02:00
commit 6f3175d8a5
11 changed files with 562 additions and 2 deletions

View file

@ -110,6 +110,12 @@ struct file* filealloc(void);
int fdalloc(struct file*);
// sys_event.c: eventfd, timerfd, signalfd and epoll, as files
// kernel/fs/lock.c
int lock_flock(struct file *f, int op);
int lock_fcntl(struct file *f, int cmd, addr_t arg);
void lock_close_fd(struct file *f);
void lock_close_file(struct file *f);
void lock_exit(int pid);
struct evobj;
void ev_close(struct evobj *);
int ev_poll(struct file *, int events);

View file

@ -37,6 +37,7 @@
#define EPIPE 32 // Broken pipe
#define ERANGE 34 // Math result not representable
#define ENAMETOOLONG 36 // File name too long
#define ENOLCK 37 // No record locks available
#define ENOSYS 38 // Function not implemented
#define ENOTEMPTY 39 // Directory not empty
#define ELOOP 40 // Too many symbolic links encountered

View file

@ -39,5 +39,12 @@
#define F_GETFL 3
#define F_SETFL 4
#define F_DUPFD_CLOEXEC 1030 // Linux value
// Record locks; see kernel/fs/lock.c.
#define F_GETLK 5
#define F_SETLK 6
#define F_SETLKW 7
#define F_OFD_GETLK 36
#define F_OFD_SETLK 37
#define F_OFD_SETLKW 38
#define FD_CLOEXEC 1