fcntl(2) System Calls Manual fcntl(2) fcntl - LIBRARY Standard C library (libc, -lc) #include int fcntl(int fd, int cmd, ... /* arg */ ); fcntl() fd. cmd. fcntl() . , cmd. cmd ( int, arg), void, . , , Linux. fcntl() cmd EINVAL, . F_DUPFD (int) fd, , arg. dup2(2) , . , . dup(2). F_DUPFD_CLOEXEC (int; Linux 2.6.24) F_DUPFD, --. fcntl() F_SETFD FD_CLOEXEC. , , O_CLOEXEC open(2). , . : FD_CLOEXEC, close-on-exec. FD_CLOEXEC 1, execve(2) ( execve(2) , ). FD_CLOEXEC 0, execve(2). F_GETFD (void) ( ) ; arg . F_SETFD (int) , arg. fcntl() F_SETFD close-on-exec fork(2) execve(2) , , . O_CLOEXEC open(2) . , open(2) , , fcntl(). ( dup(2), fcntl(F_DUPFD), fork(2) ..), . open(2). F_GETFL (void) ( ) ; arg . F_SETFL (int) , arg. (O_RDONLY, O_WRONLY, O_RDWR) (. ., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) arg . Linux O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME O_NONBLOCK. O_DSYNC O_SYNC; . (advisory) Linux (<<>>) UNIX, POSIX. Linux- . F_SETLK, F_SETLKW F_GETLK , ( , ). , lock, , , , ( ): struct flock { ... short l_type; /* : F_RDLCK, F_WRLCK, F_UNLCK */ short l_whence; /* l_start: SEEK_SET, SEEK_CUR, SEEK_END */ off_t l_start; /* */ off_t l_len; /* */ pid_t l_pid; /* PID , ( F_GETLK F_OFD_GETLK) */ ... }; l_whence, l_start l_len , . , . l_start -- , ( l_whence SEEK_SET); ( l_whence SEEK_CUR); ( l_whence SEEK_END). , l_start , , . l_len , . l_len , l_start l_start+l_len-1 . l_len 0, , l_whence l_start , . POSIX.1-2001 allows (but does not require) an implementation to support a negative l_len value; if l_len is negative, the interval described by lock covers bytes l_start+l_len up to and including l_start-1. This is supported since Linux 2.4.21 and Linux 2.5.49. l_type : (F_RDLCK) (F_WRLCK). ( ) , ( ). , . ; , . ( , , , , .) F_SETLK (struct flock *) ( l_type F_RDLCK F_WRLCK) ( l_type F_UNLCK) , l_whence, l_start l_len lock. , -1 errno EACCES EAGAIN (, , , POSIX ). F_SETLKW (struct flock *) F_SETLK, , . , ( ) ( -1 errno EINTR; . signal(7)). F_GETLK (struct flock *) , lock , . , fcntl() , F_UNLCK l_type lock . , fcntl() l_type, l_whence, l_start l_len lock. (), l_pid PID , . , l_pid -1. , . , , fd . , , fd . , . F_SETLKW, (deadlocks), , , . , , 100, 200. , F_SETLKW, . EDEADLK; , , , , . . , ; . F_UNLCK, , . , fork(2), execve(2). stdio(3), ; read(2) write(2). , , ( , ). : o , , , (), . : , , /etc/passwd /etc/mtab, - , . o . . . ( POSIX) , , . Linux 3.15 ( Austin Group POSIX.1). open(2). , , , ( , flock(2). ( ), fork(2) ( clone(2) CLONE_FILES), , . ( ), -- , -- , . , (. ., , fork(2), dup(2), fcntl() F_DUPFD . .), : , ( , , ). , , . , , open(2) . , fcntl(), lock, flock. , , l_pid 0. : F_OFD_SETLK (struct flock *) ( l_type F_RDLCK F_WRLCK) ( l_type F_UNLCK) , l_whence, l_start l_len lock. , -1 errno EAGAIN. F_OFD_SETLKW (struct flock *) F_OFD_SETLK, , . , ( ) ( -1 errno EINTR; . signal(7)). F_OFD_GETLK (struct flock *) lock , . , fcntl() , F_UNLCK l_type lock, . , , lock , F_GETLK. ( , ). (mandatory) Warning: the Linux implementation of mandatory locking is unreliable. See BUGS below. Because of these bugs, and the fact that the feature is believed to be little used, since Linux 4.5, mandatory locking has been made an optional feature, governed by a configuration option (CONFIG_MANDATORY_FILE_LOCKING). This feature is no longer supported at all in Linux 5.15 and above. , ( ) . . . (, read(2) write(2)) , , O_NONBLOCK . O_NONBLOCK , , . O_NONBLOCK , EAGAIN. , , , . <<-o mand>> mount(8) MS_MANDLOCK mount(2). set-group-ID (. chmod(1) chmod(2)). POSIX. , . , NFS, . - (. ., ), , . , , read(2) write(2) EIO. , . Linux 3.12, , , NFSv4 ( ). UNIX (SIGLOST). Linux - . / F_GETOWN, F_SETOWN, F_GETOWN_EX, F_SETOWN_EX, F_GETSIG F_SETSIG: F_GETOWN (void) Return (as the function result) the process ID or process group ID currently receiving SIGIO and SIGURG signals for events on file descriptor fd. Process IDs are returned as positive values; process group IDs are returned as negative values (but see BUGS below). arg is ignored. F_SETOWN (int) , SIGIO SIGURG fd. arg. , . , ( arg getpid(2)). , . O_ASYNC fcntl() F_SETFL. SIGIO , . , SIGIO, F_SETSIG fcntl(). - (), F_SETOWN -- , kill(2), , F_SETOWN ( ). , . : F_SETOWN fcntl(), . fd , F_SETOWN SIGURG, , . (SIGURG , select(2) , " ".) The following was true in Linux 2.6.x up to and including Linux 2.6.11: F_SETSIG , (, NPTL), , , F_SETOWN, : ID , , ID , . F_SETOWN gettid(2), getpid(2), F_SETSIG. ( Linux ID ID . , , gettid(2) getpid(2), .) , SIGURG, : , , F_SETOWN. Linux 2.6.12, . Linux 2.6.32 F_SETOWN_EX SIGIO SIGURG . F_GETOWN_EX (struct f_owner_ex *) ( Linux 2.6.32) , F_SETOWN_EX. , arg, : struct f_owner_ex { int type; pid_t pid; }; type : F_OWNER_TID, F_OWNER_PID F_OWNER_PGRP. pid , ID , ID ID . F_SETOWN_EX. F_SETOWN_EX (struct f_owner_ex *) ( Linux 2.6.32) , F_SETOWN. - , . arg, f_owner_ex. type , pid: F_OWNER_TID , ID (, clone(2) gettid(2)) pid. F_OWNER_PID , ID pid. F_OWNER_PGRP , ID pid. (, F_SETOWN, ID .) F_GETSIG (void) ( ) , , . 0 SIGIO. ( SIGIO) , , SA_SIGINFO. arg . F_SETSIG (int) , , , arg. 0 SIGIO. ( SIGIO) , , , SA_SIGINFO. F_SETSIG SA_SIGINFO (. sigaction(2)) / siginfo_t. si_code , -- SI_SIGIO, si_fd , . , , (select(2), poll(2), read(2) O_NONBLOCK ..), /. , , si_fd, , F_SETSIG. . (dup(2) ), , - , si_fd . ( >= SIGRTMIN) - ( ). , SA_SIGINFO. , Linux , (. getrlimit(2) signal(7)), , SIGIO, , . , - select(2) poll(2). The use of O_ASYNC is specific to BSD and Linux. The only use of F_GETOWN and F_SETOWN specified in POSIX.1 is in conjunction with the use of the SIGURG signal on sockets. (POSIX does not specify the SIGIO signal.) F_GETOWN_EX, F_SETOWN_EX, F_GETSIG, and F_SETSIG are Linux-specific. POSIX has asynchronous I/O and the aio_sigevent structure to achieve similar things; these are also available in Linux as part of the GNU C Library (glibc). F_SETLEASE F_GETLEASE ( Linux 2.4 ) , fd. , , (<<>>), ( ), (<< >>) open(2) truncate(2) , . F_SETLEASE (int) , , arg: F_RDLCK . , . , . F_WRLCK . , . , . F_UNLCK . (. open(2)). , (, , fork(2) dup(2)) , . , F_UNLCK , . . , UID () UID . CAP_LEASE . F_GETLEASE (void) fd; F_RDLCK, F_WRLCK F_UNLCK, , . arg . (<< >>) open(2) truncate(2), , F_SETLEASE, ( SIGIO). (, ) . F_SETLEASE arg, F_UNLCK. , , , . F_SETLEASE arg, F_RDLCK. , /proc/sys/fs/lease-break-time, . , , F_GETLEASE ( F_RDLCK F_UNLCK, ) , . , , , . , open(2) truncate(2), , EINTR, - . open(2) truncate(2), - . O_NONBLOCK open(2), EWOULDBLOCK, - . , SIGIO, , F_SETSIG fcntl(). F_SETSIG ( SIGIO), SA_SIGINFO, siginfo_t, si_fd , ( , ). (dnotify) F_NOTIFY (int) ( Linux 2.4) , fd , . , , arg, , (OR) : DN_ACCESS (read(2), pread(2), readv(2) ). DN_MODIFY (write(2), pwrite(2), writev(2), truncate(2), ftruncate(2) ). DN_CREATE (open(2), creat(2), mknod(2), mkdir(2), link(2), symlink(2), rename(2) ). DN_DELETE (unlink(2), rename(2) , rmdir(2)). DN_RENAME (rename(2)). DN_ATTRIB (chown(2), chmod(2), utime(2), utimensat(2) ). ( , _GNU_SOURCE .) , . , arg, DN_MULTISHOT, , . F_NOTIFY arg, . , F_NOTIFY, 0 arg. . SIGIO, F_SETSIG fcntl() (, SIGIO -- ; , ). , siginfo_t ( SA_SIGINFO), si_fd , ( ). , DN_MULTISHOT, , . NOTE: New applications should use the inotify interface (available since Linux 2.6.13), which provides a much superior interface for obtaining notifications of filesystem events. See inotify(7). F_SETPIPE_SZ (int; Linux 2.6.35) , fd; arg . , , /proc/sys/fs/pipe-max-size ( proc(5)). , . /proc/sys/fs/pipe-max-size EPERM; ( CAP_SYS_RESOURCE) . arg, ( ). ( ) . , EBUSY. , - , , , , . F_GETPIPE_SZ (void; Linux 2.6.35) ( ) , fd. (file sealing) . , , EPERM. , . , memfd_create(2). , memfd_create(2) ( MFD_ALLOW_SEALING). fcntl(), , EINVAL. (seals) -- inode. , inode, . , , . F_ADD_SEALS (int; Linux 3.17) arg inode, fd. . , . F_SEAL_SEAL ( ), EPERM. , F_SEAL_SEAL. fd . F_GET_SEALS (void; Linux 3.17) ( ) inode, fd. , 0. , -1 errno EINVAL. : F_SEAL_SEAL , fcntl() F_ADD_SEALS EPERM. , . F_SEAL_SEAL, . F_SEAL_SHRINK , . open(2) O_TRUNC, truncate(2) ftruncate(2). EPERM, . - . F_SEAL_GROW , . write(2) , truncate(2), ftruncate(2) fallocate(2). EPERM, . - . F_SEAL_WRITE , . , - . , , . write(2) fallocate(2) ( FALLOC_FL_PUNCH_HOLE). EPERM. , , mmap(2) EPERM. F_ADD_SEALS F_SEAL_WRITE EBUSY, . . , , - (io_submit(2)), . F_SEAL_FUTURE_WRITE ( Linux 5.1) The effect of this seal is similar to F_SEAL_WRITE, but the contents of the file can still be modified via shared writable mappings that were created prior to the seal being set. Any attempt to create a new writable mapping on the file via mmap(2) will fail with EPERM. Likewise, an attempt to write to the file via write(2) will fail with EPERM. Using this seal, one process can create a memory buffer that it can continue to modify while sharing that buffer on a "read-only" basis with other processes. / ( open(2)). << >> (write lifetime) , . , , , , - . , - , - , . fd: F_GET_RW_HINT (uint64_t *; Linux 4.13) /, , fd. F_SET_RW_HINT (uint64_t *; Linux 4.13) /, , fd. , . F_GET_FILE_RW_HINT (uint64_t *; Linux 4.13) /, , fd. F_SET_FILE_RW_HINT (uint64_t *; Linux 4.13) /, , fd. /, ( ), . Linux 4.13, /: RWH_WRITE_LIFE_NOT_SET . . RWH_WRITE_LIFE_NONE , , . RWH_WRITE_LIFE_SHORT , , , . RWH_WRITE_LIFE_MEDIUM , , , , , RWH_WRITE_LIFE_SHORT. RWH_WRITE_LIFE_LONG , , , , , RWH_WRITE_LIFE_MEDIUM. RWH_WRITE_LIFE_EXTREME , , , , , RWH_WRITE_LIFE_LONG. . : F_DUPFD . F_GETFD . F_GETFL . F_GETLEASE , . F_GETOWN , . F_GETSIG , SIGIO. F_GETPIPE_SZ F_SETPIPE_SZ . F_GET_SEALS , inode, fd. . -1, errno . EACCES EAGAIN , . EAGAIN , . EBADF fd . EBADF cmd F_SETLK F_SETLKW, . EBUSY cmd F_SETPIPE_SZ , arg, , . EBUSY cmd F_ADD_SEALS, arg F_SEAL_WRITE , , fd. EDEADLK , F_SETLKW (deadlock). EFAULT lock . EINTR cmd F_SETLKW F_OFD_SETLKW ; signal(7)). EINTR cmd F_GETLK, F_SETLK, F_OFD_GETLK F_OFD_SETLK . (, NFS), . EINVAL cmd . EINVAL cmd F_ADD_SEALS arg . EINVAL cmd F_ADD_SEALS F_GET_SEALS , inode, fd, . EINVAL cmd F_DUPFD arg ( RLIMIT_NOFILE getrlimit(2)). EINVAL cmd F_SETSIG arg . EINVAL cmd F_OFD_SETLK, F_OFD_SETLKW F_OFD_GETLK, l_pid . EMFILE cmd F_DUPFD . ENOLCK , (, NFS). ENOTDIR cmd F_NOTIFY, fd . EPERM cmd F_SETPIPE_SZ ; pipe(7). EPERM O_APPEND , . EPERM cmd F_ADD_SEALS, fd F_SEAL_SEAL. POSIX.1-2008. F_GETOWN_EX, F_SETOWN_EX, F_SETPIPE_SZ, F_GETPIPE_SZ, F_GETSIG, F_SETSIG, F_NOTIFY, F_GETLEASE F_SETLEASE Linux. _GNU_SOURCE. F_OFD_SETLK, F_OFD_SETLKW F_OFD_GETLK Linux ( _GNU_SOURCE), POSIX.1. F_ADD_SEALS F_GET_SEALS Linux. SVr4, 4.3BSD, POSIX.1-2001. Only the operations F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK, and F_SETLKW are specified in POSIX.1-2001. F_GETOWN F_SETOWN POSIX.1-2001. , _XOPEN_SOURCE 500 _POSIX_C_SOURCE 200809L . F_DUPFD_CLOEXEC POSIX.1-2008. , _POSIX_C_SOURCE 200809L , _XOPEN_SOURCE 700 . , dup2(2), , F_DUPFD. fcntl() Linux ( flock). , Linux 2.4 fcntl64(). -- flock64 -- F_GETLK64, F_SETLK64 F_SETLKW64. , , glibc, fcntl() , . (Record locks) Since Linux 2.0, there is no interaction between the types of lock placed by flock(2) and fcntl(). struct flock, , l_sysid ( , ). , l_pid , , , ; Linux, (, MIPS32), . fcntl() Linux ( flock). , Linux 2.4 fcntl64(). -- flock64 -- F_GETLK64, F_SETLK64 F_SETLKW64. , , glibc, fcntl() , . NFS Linux 3.12, NFSv4 ( 90 ), ( , NFSv4 (leasetime). Linux NFS /proc/fs/nfsd/nfsv4leasetime, . 90). , -. Linux 3.12, NFSv4 , -, , <<>>, , , . , pre-3.12, nfs.recover_lost_locks 1, - . - , 0 (). F_SETFL F_SETFL O_DSYNC O_SYNC. . F_GETOWN A limitation of the Linux system call conventions on some architectures (notably i386) means that if a (negative) process group ID to be returned by F_GETOWN falls in the range -1 to -4095, then the return value is wrongly interpreted by glibc as an error in the system call; that is, the return value of fcntl() will be -1, and errno will contain the (positive) process group ID. The Linux-specific F_GETOWN_EX operation avoids this problem. Since glibc 2.11, glibc makes the kernel F_GETOWN problem invisible by implementing F_GETOWN using F_GETOWN_EX. F_SETOWN Linux 2.4 , , F_SETOWN () . fcntl() -1 errno EPERM, () . , . (deadlock) F_SETLKW ( ), ( EDEADLK, ). , 10, , , , . , , clone(2) CLONE_FILES, , ( ) . (mandatory) Linux , : write(2), , ; read(2), , , . mmap(2). . . dup2(2), flock(2), open(2), socket(2), lockf(3), capabilities(7), feature_test_macros(7), lslocks(8) locks.txt, mandatory-locking.txt dnotify.txt Documentation/filesystems. Linux ( Documentation/, mandatory-locking.txt mandatory.txt). Azamat Hackimov , Dmitry Bolkhovskikh , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . fcntl(2)