#define F_DUPFD 1 #define F_DUPFD_CLOEXEC 2 #define F_GETFD 3 #define F_SETFD 4 #define F_GETFL 5 #define F_SETFL 6 #define F_GETPIPE_SZ 7 #define F_SETPIPE_SZ 8 #define F_GETLK 9 #define F_SETLK 10 #define F_SETLKW 11 #define FD_CLOEXEC 0b01 #define FD_CLOFORK 0b10 #define F_RDLCK 1 #define F_UNLCK 2 #define F_WRLCK 3 struct flock { uint16_t l_type; uint16_t l_whence; uint64_t l_start; uint64_t l_len; uint32_t l_pid; } int fcntl(int fd, int cmd, void *arg);
This syscall is a multiplexed syscall that performs the operations described
below on the open file descriptor fd
. The operation is determined by
cmd
and may take an optional argument arg
.
The syscall’s return value will depend on the requested cmd
, and is
detailed along the operations below.
The valid operations are:
F_DUPFD
: Clones fd
into a the first available file descriptor
starting by arg
. Returns the resulting FD.
F_DUPFD_CLOEXEC
: The same as F_DUPFD
but sets the close on exec
flag for the cloned FD if succesful, in order to save a subsequent call.
F_GETFD
: The flags used for fd
will be returned. These can be
FD_CLOEXEC
, which if set, will signal that fd
will be closed
should an exec
call or similar happen, and FD_CLOFORK
, which
signals that fd
will not be cloned when calling clone
or similar.
F_SETFD
: The flags for fd
will be set with arg
using
the same values returned by F_GETFD
. The syscall will return 0
on sucess.
F_GETFL
: Returns as the function result the file access mode
and status flags.
F_SETFL
: What F_SETFD
is to F_GETFD
for
F_GETFL
.
F_GETPIPE_SZ
: If fd
points to a FIFO or pipe, return
its size.
F_SETPIPE_SZ
: If fd
points to a FIFO or pipe, the size will
be set to the value of arg
. If the operation would cause data loss, it
will fail.
F_GETLK
: If fd
points to a filesystem inode, the data passed
in arg
will be taken as a pointer to a flock
struct, and the
possibility to add the advisory lock described there will be checked.
POSIX advisory locks are advisory file description-bound marks that may be used as a filesystem semaphore. They require processes to check them, and are not automatic nor mandatory, thus, advisory.
If the lock could be placed, the passed struct’s l_type
will become
F_UNLCK
, else, the call will fail and the fields used to write the
information regarding one of the conflicting locks. As all IPC, this data may
not be true once processed, it is merely informative.
POSIX advisory locks can be used for implementing PID lock files, as to make sure only one instance of the same daemon is running.
F_SETLK
: The same as F_GETLK
, but actually sets the passed lock.
If it cannot be set, it will return failure with EAGAIN
.
F_SETLKW
: The same as F_SETLK
, but instead of being non-blocking,
this call will block until the passed lock can be set.
On failure, the syscall returns -1
. The returned errno
are:
EINVAL
: The passed cmd
is not implemented by the kernel.
EBADF
: The passed fd
is not open.