1FCNTL(2)                   Linux Programmer's Manual                  FCNTL(2)
2
3
4

NAME

6       fcntl - manipulate file descriptor
7

SYNOPSIS

9       #include <fcntl.h>
10
11       int fcntl(int fd, int cmd, ... /* arg */ );
12

DESCRIPTION

14       fcntl() performs one of the operations described below on the open file
15       descriptor fd.  The operation is determined by cmd.
16
17       fcntl() can take an optional third argument.  Whether or not this argu‐
18       ment  is  required is determined by cmd.  The required argument type is
19       indicated in parentheses after each cmd name (in most  cases,  the  re‐
20       quired  type  is int, and we identify the argument using the name arg),
21       or void is specified if the argument is not required.
22
23       Certain of the operations below are supported only since  a  particular
24       Linux  kernel  version.   The  preferred method of checking whether the
25       host kernel supports a particular operation is to invoke  fcntl()  with
26       the  desired  cmd value and then test whether the call failed with EIN‐
27       VAL, indicating that the kernel does not recognize this value.
28
29   Duplicating a file descriptor
30       F_DUPFD (int)
31              Duplicate the  file  descriptor  fd  using  the  lowest-numbered
32              available file descriptor greater than or equal to arg.  This is
33              different from dup2(2), which uses exactly the  file  descriptor
34              specified.
35
36              On success, the new file descriptor is returned.
37
38              See dup(2) for further details.
39
40       F_DUPFD_CLOEXEC (int; since Linux 2.6.24)
41              As  for F_DUPFD, but additionally set the close-on-exec flag for
42              the duplicate file descriptor.  Specifying this flag  permits  a
43              program  to avoid an additional fcntl() F_SETFD operation to set
44              the FD_CLOEXEC flag.  For an explanation of  why  this  flag  is
45              useful, see the description of O_CLOEXEC in open(2).
46
47   File descriptor flags
48       The  following commands manipulate the flags associated with a file de‐
49       scriptor.  Currently, only one such flag is  defined:  FD_CLOEXEC,  the
50       close-on-exec  flag.  If the FD_CLOEXEC bit is set, the file descriptor
51       will automatically be closed during a successful  execve(2).   (If  the
52       execve(2)  fails, the file descriptor is left open.)  If the FD_CLOEXEC
53       bit is not set, the file descriptor will  remain  open  across  an  ex‐
54       ecve(2).
55
56       F_GETFD (void)
57              Return  (as  the function result) the file descriptor flags; arg
58              is ignored.
59
60       F_SETFD (int)
61              Set the file descriptor flags to the value specified by arg.
62
63       In multithreaded programs, using fcntl() F_SETFD to set  the  close-on-
64       exec  flag  at  the same time as another thread performs a fork(2) plus
65       execve(2) is vulnerable to a race condition  that  may  unintentionally
66       leak  the file descriptor to the program executed in the child process.
67       See the discussion of the O_CLOEXEC flag in open(2) for details  and  a
68       remedy to the problem.
69
70   File status flags
71       Each  open  file  description has certain associated status flags, ini‐
72       tialized by open(2) and possibly modified by fcntl().  Duplicated  file
73       descriptors  (made with dup(2), fcntl(F_DUPFD), fork(2), etc.) refer to
74       the same open file description, and thus share  the  same  file  status
75       flags.
76
77       The file status flags and their semantics are described in open(2).
78
79       F_GETFL (void)
80              Return  (as  the  function  result) the file access mode and the
81              file status flags; arg is ignored.
82
83       F_SETFL (int)
84              Set the file status flags to the value specified by  arg.   File
85              access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags
86              (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg  are  ignored.
87              On  Linux,  this  command can change only the O_APPEND, O_ASYNC,
88              O_DIRECT, O_NOATIME, and O_NONBLOCK flags.  It is  not  possible
89              to change the O_DSYNC and O_SYNC flags; see BUGS, below.
90
91   Advisory record locking
92       Linux  implements traditional ("process-associated") UNIX record locks,
93       as standardized by POSIX.  For a Linux-specific alternative with better
94       semantics, see the discussion of open file description locks below.
95
96       F_SETLK,  F_SETLKW,  and F_GETLK are used to acquire, release, and test
97       for the existence of record locks (also known as byte-range,  file-seg‐
98       ment, or file-region locks).  The third argument, lock, is a pointer to
99       a structure that has at least the following fields (in unspecified  or‐
100       der).
101
102           struct flock {
103               ...
104               short l_type;    /* Type of lock: F_RDLCK,
105                                   F_WRLCK, F_UNLCK */
106               short l_whence;  /* How to interpret l_start:
107                                   SEEK_SET, SEEK_CUR, SEEK_END */
108               off_t l_start;   /* Starting offset for lock */
109               off_t l_len;     /* Number of bytes to lock */
110               pid_t l_pid;     /* PID of process blocking our lock
111                                   (set by F_GETLK and F_OFD_GETLK) */
112               ...
113           };
114
115       The  l_whence,  l_start, and l_len fields of this structure specify the
116       range of bytes we wish to lock.  Bytes past the end of the file may  be
117       locked, but not bytes before the start of the file.
118
119       l_start  is  the starting offset for the lock, and is interpreted rela‐
120       tive to either: the start of the file (if l_whence  is  SEEK_SET);  the
121       current  file  offset (if l_whence is SEEK_CUR); or the end of the file
122       (if l_whence is SEEK_END).  In the final two cases, l_start  can  be  a
123       negative  number  provided  the offset does not lie before the start of
124       the file.
125
126       l_len specifies the number of bytes to be locked.  If  l_len  is  posi‐
127       tive,  then  the  range to be locked covers bytes l_start up to and in‐
128       cluding l_start+l_len-1.  Specifying 0 for l_len has the special  mean‐
129       ing:  lock all bytes starting at the location specified by l_whence and
130       l_start through to the end of file, no matter how large the file grows.
131
132       POSIX.1-2001 allows (but does not require) an implementation to support
133       a negative l_len value; if l_len is negative, the interval described by
134       lock covers bytes l_start+l_len up to and including l_start-1.  This is
135       supported by Linux since kernel versions 2.4.21 and 2.5.49.
136
137       The  l_type  field  can  be  used  to place a read (F_RDLCK) or a write
138       (F_WRLCK) lock on a file.  Any number of processes may hold a read lock
139       (shared  lock)  on a file region, but only one process may hold a write
140       lock (exclusive lock).  An exclusive lock  excludes  all  other  locks,
141       both  shared and exclusive.  A single process can hold only one type of
142       lock on a file region; if a new lock is applied  to  an  already-locked
143       region,  then  the  existing  lock  is  converted to the new lock type.
144       (Such conversions may involve splitting, shrinking, or coalescing  with
145       an  existing  lock if the byte range specified by the new lock does not
146       precisely coincide with the range of the existing lock.)
147
148       F_SETLK (struct flock *)
149              Acquire a lock (when l_type is F_RDLCK or F_WRLCK) or release  a
150              lock  (when  l_type  is  F_UNLCK)  on the bytes specified by the
151              l_whence, l_start, and l_len fields of lock.  If  a  conflicting
152              lock  is  held by another process, this call returns -1 and sets
153              errno to EACCES or EAGAIN.  (The error  returned  in  this  case
154              differs across implementations, so POSIX requires a portable ap‐
155              plication to check for both errors.)
156
157       F_SETLKW (struct flock *)
158              As for F_SETLK, but if a conflicting lock is held on  the  file,
159              then  wait  for that lock to be released.  If a signal is caught
160              while waiting, then the call is interrupted and (after the  sig‐
161              nal handler has returned) returns immediately (with return value
162              -1 and errno set to EINTR; see signal(7)).
163
164       F_GETLK (struct flock *)
165              On input to this call, lock describes a lock we  would  like  to
166              place  on  the  file.  If the lock could be placed, fcntl() does
167              not actually place it, but returns F_UNLCK in the  l_type  field
168              of lock and leaves the other fields of the structure unchanged.
169
170              If  one or more incompatible locks would prevent this lock being
171              placed, then fcntl() returns details about one of those locks in
172              the l_type, l_whence, l_start, and l_len fields of lock.  If the
173              conflicting lock is a  traditional  (process-associated)  record
174              lock,  then  the  l_pid  field  is set to the PID of the process
175              holding that lock.  If the conflicting lock is an open file  de‐
176              scription lock, then l_pid is set to -1.  Note that the returned
177              information may already be out of date by the  time  the  caller
178              inspects it.
179
180       In  order  to place a read lock, fd must be open for reading.  In order
181       to place a write lock, fd must be open  for  writing.   To  place  both
182       types of lock, open a file read-write.
183
184       When placing locks with F_SETLKW, the kernel detects deadlocks, whereby
185       two or more processes have their  lock  requests  mutually  blocked  by
186       locks  held  by  the  other  processes.  For example, suppose process A
187       holds a write lock on byte 100 of a file, and process B holds  a  write
188       lock  on  byte 200.  If each process then attempts to lock the byte al‐
189       ready locked by the other process using F_SETLKW, then,  without  dead‐
190       lock detection, both processes would remain blocked indefinitely.  When
191       the kernel detects such deadlocks, it causes one of the  blocking  lock
192       requests  to  immediately  fail  with the error EDEADLK; an application
193       that encounters such an error should release some of its locks to allow
194       other  applications  to proceed before attempting regain the locks that
195       it requires.  Circular deadlocks involving more than two processes  are
196       also  detected.   Note, however, that there are limitations to the ker‐
197       nel's deadlock-detection algorithm; see BUGS.
198
199       As well as being removed by an explicit F_UNLCK, record locks are auto‐
200       matically released when the process terminates.
201
202       Record  locks are not inherited by a child created via fork(2), but are
203       preserved across an execve(2).
204
205       Because of the buffering performed by the stdio(3) library, the use  of
206       record  locking  with  routines  in that package should be avoided; use
207       read(2) and write(2) instead.
208
209       The record locks described above are associated with the  process  (un‐
210       like  the  open file description locks described below).  This has some
211       unfortunate consequences:
212
213       *  If a process closes any file descriptor referring to  a  file,  then
214          all  of the process's locks on that file are released, regardless of
215          the file descriptor(s) on which the locks were  obtained.   This  is
216          bad:  it  means  that a process can lose its locks on a file such as
217          /etc/passwd or /etc/mtab when for some reason a library function de‐
218          cides to open, read, and close the same file.
219
220       *  The  threads  in  a  process  share locks.  In other words, a multi‐
221          threaded program can't use record locking  to  ensure  that  threads
222          don't simultaneously access the same region of a file.
223
224       Open file description locks solve both of these problems.
225
226   Open file description locks (non-POSIX)
227       Open  file description locks are advisory byte-range locks whose opera‐
228       tion is in most respects identical to the traditional record locks  de‐
229       scribed  above.   This lock type is Linux-specific, and available since
230       Linux 3.15.  (There is a proposal with the Austin Group to include this
231       lock type in the next revision of POSIX.1.)  For an explanation of open
232       file descriptions, see open(2).
233
234       The principal difference between the two lock  types  is  that  whereas
235       traditional  record  locks are associated with a process, open file de‐
236       scription locks are associated with the open file description on  which
237       they  are  acquired,  much  like  locks acquired with flock(2).  Conse‐
238       quently (and unlike traditional advisory record locks), open  file  de‐
239       scription  locks  are  inherited  across  fork(2)  (and  clone(2)  with
240       CLONE_FILES), and are only automatically released on the last close  of
241       the  open  file  description, instead of being released on any close of
242       the file.
243
244       Conflicting lock combinations (i.e., a read lock and a  write  lock  or
245       two  write  locks)  where one lock is an open file description lock and
246       the other is a traditional record lock conflict even when they are  ac‐
247       quired by the same process on the same file descriptor.
248
249       Open  file  description locks placed via the same open file description
250       (i.e., via the same file descriptor, or via a duplicate of the file de‐
251       scriptor  created  by  fork(2), dup(2), fcntl() F_DUPFD, and so on) are
252       always compatible: if a new lock is placed on an already locked region,
253       then  the  existing lock is converted to the new lock type.  (Such con‐
254       versions may result in splitting, shrinking, or coalescing with an  ex‐
255       isting lock as discussed above.)
256
257       On  the  other hand, open file description locks may conflict with each
258       other when they are acquired  via  different  open  file  descriptions.
259       Thus, the threads in a multithreaded program can use open file descrip‐
260       tion locks to synchronize access to a file region by having each thread
261       perform  its own open(2) on the file and applying locks via the result‐
262       ing file descriptor.
263
264       As with traditional advisory locks,  the  third  argument  to  fcntl(),
265       lock, is a pointer to an flock structure.  By contrast with traditional
266       record locks, the l_pid field of that structure must  be  set  to  zero
267       when using the commands described below.
268
269       The commands for working with open file description locks are analogous
270       to those used with traditional locks:
271
272       F_OFD_SETLK (struct flock *)
273              Acquire an open file description lock (when l_type is F_RDLCK or
274              F_WRLCK)  or  release an open file description lock (when l_type
275              is F_UNLCK) on the bytes specified by the l_whence, l_start, and
276              l_len  fields of lock.  If a conflicting lock is held by another
277              process, this call returns -1 and sets errno to EAGAIN.
278
279       F_OFD_SETLKW (struct flock *)
280              As for F_OFD_SETLK, but if a conflicting lock  is  held  on  the
281              file,  then  wait  for that lock to be released.  If a signal is
282              caught while waiting, then the call is  interrupted  and  (after
283              the  signal  handler has returned) returns immediately (with re‐
284              turn value -1 and errno set to EINTR; see signal(7)).
285
286       F_OFD_GETLK (struct flock *)
287              On input to this call, lock describes an open  file  description
288              lock  we  would like to place on the file.  If the lock could be
289              placed, fcntl() does not actually place it, but returns  F_UNLCK
290              in  the  l_type field of lock and leaves the other fields of the
291              structure unchanged.  If one or more  incompatible  locks  would
292              prevent  this lock being placed, then details about one of these
293              locks are returned via lock, as described above for F_GETLK.
294
295       In the current implementation, no deadlock detection is  performed  for
296       open  file  description locks.  (This contrasts with process-associated
297       record locks, for which the kernel does perform deadlock detection.)
298
299   Mandatory locking
300       Warning: the Linux implementation of mandatory locking  is  unreliable.
301       See  BUGS  below.  Because of these bugs, and the fact that the feature
302       is believed to be little used, since Linux 4.5, mandatory  locking  has
303       been made an optional feature, governed by a configuration option (CON‐
304       FIG_MANDATORY_FILE_LOCKING).  This is an initial step  toward  removing
305       this feature completely.
306
307       By  default,  both  traditional  (process-associated) and open file de‐
308       scription record locks are advisory.  Advisory locks are  not  enforced
309       and are useful only between cooperating processes.
310
311       Both  lock  types  can also be mandatory.  Mandatory locks are enforced
312       for all processes.  If a process tries to perform an  incompatible  ac‐
313       cess (e.g., read(2) or write(2)) on a file region that has an incompat‐
314       ible mandatory lock, then the result depends upon  whether  the  O_NON‐
315       BLOCK flag is enabled for its open file description.  If the O_NONBLOCK
316       flag is not enabled, then the system call is blocked until the lock  is
317       removed  or converted to a mode that is compatible with the access.  If
318       the O_NONBLOCK flag is enabled, then the system call fails with the er‐
319       ror EAGAIN.
320
321       To  make use of mandatory locks, mandatory locking must be enabled both
322       on the filesystem that contains the file to be locked, and on the  file
323       itself.   Mandatory  locking  is  enabled on a filesystem using the "-o
324       mand" option to mount(8), or the MS_MANDLOCK flag for mount(2).  Manda‐
325       tory locking is enabled on a file by disabling group execute permission
326       on the file and enabling the set-group-ID permission bit (see  chmod(1)
327       and chmod(2)).
328
329       Mandatory  locking  is not specified by POSIX.  Some other systems also
330       support mandatory locking, although the details of  how  to  enable  it
331       vary across systems.
332
333   Lost locks
334       When an advisory lock is obtained on a networked filesystem such as NFS
335       it is possible that the lock might get lost.  This may  happen  due  to
336       administrative  action  on  the  server,  or due to a network partition
337       (i.e., loss of network connectivity with the server) which  lasts  long
338       enough  for the server to assume that the client is no longer function‐
339       ing.
340
341       When the filesystem determines  that  a  lock  has  been  lost,  future
342       read(2)  or  write(2) requests may fail with the error EIO.  This error
343       will persist until the lock  is  removed  or  the  file  descriptor  is
344       closed.   Since  Linux 3.12, this happens at least for NFSv4 (including
345       all minor versions).
346
347       Some versions of UNIX send a signal  (SIGLOST)  in  this  circumstance.
348       Linux  does  not define this signal, and does not provide any asynchro‐
349       nous notification of lost locks.
350
351   Managing signals
352       F_GETOWN, F_SETOWN, F_GETOWN_EX, F_SETOWN_EX,  F_GETSIG,  and  F_SETSIG
353       are used to manage I/O availability signals:
354
355       F_GETOWN (void)
356              Return  (as the function result) the process ID or process group
357              ID currently receiving SIGIO and SIGURG signals  for  events  on
358              file  descriptor  fd.  Process IDs are returned as positive val‐
359              ues; process group IDs are returned as negative values (but  see
360              BUGS below).  arg is ignored.
361
362       F_SETOWN (int)
363              Set  the  process ID or process group ID that will receive SIGIO
364              and SIGURG signals for events on the file  descriptor  fd.   The
365              target  process  or  process  group  ID  is specified in arg.  A
366              process ID is specified as a positive value; a process group  ID
367              is  specified  as  a negative value.  Most commonly, the calling
368              process specifies itself as the owner (that is, arg is specified
369              as getpid(2)).
370
371              As  well as setting the file descriptor owner, one must also en‐
372              able generation of signals on the file descriptor.  This is done
373              by  using  the  fcntl()  F_SETFL command to set the O_ASYNC file
374              status flag on the file descriptor.  Subsequently, a SIGIO  sig‐
375              nal  is  sent  whenever  input or output becomes possible on the
376              file descriptor.  The fcntl() F_SETSIG command can  be  used  to
377              obtain delivery of a signal other than SIGIO.
378
379              Sending a signal to the owner process (group) specified by F_SE‐
380              TOWN is subject to the same permissions checks as are  described
381              for  kill(2),  where the sending process is the one that employs
382              F_SETOWN (but see BUGS below).  If this permission check  fails,
383              then the signal is silently discarded.  Note: The F_SETOWN oper‐
384              ation records the caller's credentials at the time  of  the  fc‐
385              ntl()  call, and it is these saved credentials that are used for
386              the permission checks.
387
388              If the file descriptor fd refers to a socket, F_SETOWN also  se‐
389              lects  the  recipient  of SIGURG signals that are delivered when
390              out-of-band data arrives on that socket.  (SIGURG is sent in any
391              situation  where  select(2) would report the socket as having an
392              "exceptional condition".)
393
394              The following was true in 2.6.x kernels up to and including ker‐
395              nel 2.6.11:
396
397                     If  a  nonzero  value  is  given  to F_SETSIG in a multi‐
398                     threaded process running with a  threading  library  that
399                     supports  thread  groups  (e.g.,  NPTL),  then a positive
400                     value given to F_SETOWN has a different meaning:  instead
401                     of  being a process ID identifying a whole process, it is
402                     a thread  ID  identifying  a  specific  thread  within  a
403                     process.  Consequently, it may be necessary to pass F_SE‐
404                     TOWN the result of gettid(2) instead of getpid(2) to  get
405                     sensible  results  when  F_SETSIG  is  used.  (In current
406                     Linux threading implementations, a main  thread's  thread
407                     ID is the same as its process ID.  This means that a sin‐
408                     gle-threaded program can equally use  gettid(2)  or  get‐
409                     pid(2) in this scenario.)  Note, however, that the state‐
410                     ments in this paragraph do not apply to the SIGURG signal
411                     generated  for  out-of-band data on a socket: this signal
412                     is always sent to either a process or  a  process  group,
413                     depending on the value given to F_SETOWN.
414
415              The above behavior was accidentally dropped in Linux 2.6.12, and
416              won't be restored.  From Linux 2.6.32 onward, use F_SETOWN_EX to
417              target SIGIO and SIGURG signals at a particular thread.
418
419       F_GETOWN_EX (struct f_owner_ex *) (since Linux 2.6.32)
420              Return  the current file descriptor owner settings as defined by
421              a previous F_SETOWN_EX operation.  The information  is  returned
422              in  the  structure  pointed  to  by arg, which has the following
423              form:
424
425                  struct f_owner_ex {
426                      int   type;
427                      pid_t pid;
428                  };
429
430              The  type  field  will  have  one  of  the  values  F_OWNER_TID,
431              F_OWNER_PID, or F_OWNER_PGRP.  The pid field is a positive inte‐
432              ger representing a thread ID, process ID, or process  group  ID.
433              See F_SETOWN_EX for more details.
434
435       F_SETOWN_EX (struct f_owner_ex *) (since Linux 2.6.32)
436              This  operation  performs a similar task to F_SETOWN.  It allows
437              the caller to direct I/O  availability  signals  to  a  specific
438              thread,  process,  or  process  group.  The caller specifies the
439              target of signals via arg, which is a pointer  to  a  f_owner_ex
440              structure.   The  type  field  has  one of the following values,
441              which define how pid is interpreted:
442
443              F_OWNER_TID
444                     Send the signal to the thread whose thread ID (the  value
445                     returned by a call to clone(2) or gettid(2)) is specified
446                     in pid.
447
448              F_OWNER_PID
449                     Send the signal to the process whose ID is  specified  in
450                     pid.
451
452              F_OWNER_PGRP
453                     Send  the  signal to the process group whose ID is speci‐
454                     fied in pid.  (Note that, unlike with F_SETOWN, a process
455                     group ID is specified as a positive value here.)
456
457       F_GETSIG (void)
458              Return  (as  the  function result) the signal sent when input or
459              output becomes possible.  A value of zero means SIGIO  is  sent.
460              Any  other  value  (including SIGIO) is the signal sent instead,
461              and in this case additional info is available to the signal han‐
462              dler if installed with SA_SIGINFO.  arg is ignored.
463
464       F_SETSIG (int)
465              Set the signal sent when input or output becomes possible to the
466              value given in arg.  A value of zero means to send  the  default
467              SIGIO  signal.   Any other value (including SIGIO) is the signal
468              to send instead, and in this case additional info  is  available
469              to the signal handler if installed with SA_SIGINFO.
470
471              By  using  F_SETSIG with a nonzero value, and setting SA_SIGINFO
472              for the signal handler  (see  sigaction(2)),  extra  information
473              about  I/O events is passed to the handler in a siginfo_t struc‐
474              ture.  If the si_code field indicates the  source  is  SI_SIGIO,
475              the  si_fd  field  gives the file descriptor associated with the
476              event.  Otherwise, there is no indication which file descriptors
477              are pending, and you should use the usual mechanisms (select(2),
478              poll(2), read(2) with O_NONBLOCK set etc.)  to  determine  which
479              file descriptors are available for I/O.
480
481              Note  that the file descriptor provided in si_fd is the one that
482              was specified during the F_SETSIG operation.  This can  lead  to
483              an  unusual  corner  case.  If the file descriptor is duplicated
484              (dup(2) or similar), and the original file descriptor is closed,
485              then  I/O  events  will  continue to be generated, but the si_fd
486              field will contain the number of the now closed file descriptor.
487
488              By selecting a real time signal (value  >=  SIGRTMIN),  multiple
489              I/O  events may be queued using the same signal numbers.  (Queu‐
490              ing is dependent on available  memory.)   Extra  information  is
491              available if SA_SIGINFO is set for the signal handler, as above.
492
493              Note  that Linux imposes a limit on the number of real-time sig‐
494              nals that may be queued to a process (see getrlimit(2) and  sig‐
495              nal(7)) and if this limit is reached, then the kernel reverts to
496              delivering SIGIO, and this signal is  delivered  to  the  entire
497              process rather than to a specific thread.
498
499       Using  these mechanisms, a program can implement fully asynchronous I/O
500       without using select(2) or poll(2) most of the time.
501
502       The use of O_ASYNC is specific to BSD  and  Linux.   The  only  use  of
503       F_GETOWN  and  F_SETOWN specified in POSIX.1 is in conjunction with the
504       use of the SIGURG signal on sockets.  (POSIX does not specify the SIGIO
505       signal.)   F_GETOWN_EX,  F_SETOWN_EX, F_GETSIG, and F_SETSIG are Linux-
506       specific.  POSIX has asynchronous I/O and the aio_sigevent structure to
507       achieve  similar  things;  these are also available in Linux as part of
508       the GNU C Library (Glibc).
509
510   Leases
511       F_SETLEASE and F_GETLEASE (Linux 2.4 onward) are used  to  establish  a
512       new lease, and retrieve the current lease, on the open file description
513       referred to by the file descriptor fd.  A file lease provides a  mecha‐
514       nism  whereby the process holding the lease (the "lease holder") is no‐
515       tified (via delivery of a signal) when a process (the "lease  breaker")
516       tries  to  open(2) or truncate(2) the file referred to by that file de‐
517       scriptor.
518
519       F_SETLEASE (int)
520              Set or remove a file lease according to which of  the  following
521              values is specified in the integer arg:
522
523              F_RDLCK
524                     Take  out  a  read  lease.   This  will cause the calling
525                     process to be notified when the file is opened for  writ‐
526                     ing  or is truncated.  A read lease can be placed only on
527                     a file descriptor that is opened read-only.
528
529              F_WRLCK
530                     Take out a write lease.  This will cause the caller to be
531                     notified  when  the file is opened for reading or writing
532                     or is truncated.  A write lease may be placed on  a  file
533                     only  if there are no other open file descriptors for the
534                     file.
535
536              F_UNLCK
537                     Remove our lease from the file.
538
539       Leases are associated with an  open  file  description  (see  open(2)).
540       This  means  that  duplicate file descriptors (created by, for example,
541       fork(2) or dup(2)) refer to the same lease, and this lease may be modi‐
542       fied  or  released  using  any  of these descriptors.  Furthermore, the
543       lease is released by either an explicit F_UNLCK  operation  on  any  of
544       these  duplicate  file  descriptors,  or when all such file descriptors
545       have been closed.
546
547       Leases may be taken out only on regular files.  An unprivileged process
548       may  take  out  a  lease  only  on a file whose UID (owner) matches the
549       filesystem UID of the process.  A process with the CAP_LEASE capability
550       may take out leases on arbitrary files.
551
552       F_GETLEASE (void)
553              Indicates  what  type  of  lease is associated with the file de‐
554              scriptor fd by returning either F_RDLCK,  F_WRLCK,  or  F_UNLCK,
555              indicating,  respectively,  a  read lease , a write lease, or no
556              lease.  arg is ignored.
557
558       When a process (the "lease breaker") performs an open(2) or truncate(2)
559       that conflicts with a lease established via F_SETLEASE, the system call
560       is blocked by the kernel and the kernel notifies the  lease  holder  by
561       sending  it  a  signal (SIGIO by default).  The lease holder should re‐
562       spond to receipt of this signal by doing whatever cleanup  is  required
563       in  preparation  for  the file to be accessed by another process (e.g.,
564       flushing cached buffers) and then either remove or downgrade its lease.
565       A  lease  is removed by performing an F_SETLEASE command specifying arg
566       as F_UNLCK.  If the lease holder currently holds a write lease  on  the
567       file, and the lease breaker is opening the file for reading, then it is
568       sufficient for the lease holder to downgrade the lease to a read lease.
569       This  is  done  by  performing  an F_SETLEASE command specifying arg as
570       F_RDLCK.
571
572       If the lease holder fails to downgrade or remove the lease  within  the
573       number  of seconds specified in /proc/sys/fs/lease-break-time, then the
574       kernel forcibly removes or downgrades the lease holder's lease.
575
576       Once a lease break has been initiated, F_GETLEASE  returns  the  target
577       lease  type (either F_RDLCK or F_UNLCK, depending on what would be com‐
578       patible with the lease breaker)  until  the  lease  holder  voluntarily
579       downgrades  or  removes  the lease or the kernel forcibly does so after
580       the lease break timer expires.
581
582       Once the lease has been voluntarily or forcibly removed or  downgraded,
583       and  assuming  the lease breaker has not unblocked its system call, the
584       kernel permits the lease breaker's system call to proceed.
585
586       If the lease breaker's blocked open(2) or truncate(2) is interrupted by
587       a  signal handler, then the system call fails with the error EINTR, but
588       the other steps still occur as described above.  If the  lease  breaker
589       is killed by a signal while blocked in open(2) or truncate(2), then the
590       other steps still occur as described above.  If the lease breaker spec‐
591       ifies  the  O_NONBLOCK flag when calling open(2), then the call immedi‐
592       ately fails with the error EWOULDBLOCK, but the other steps still occur
593       as described above.
594
595       The  default  signal used to notify the lease holder is SIGIO, but this
596       can be changed using the F_SETSIG command to fcntl().   If  a  F_SETSIG
597       command  is  performed (even one specifying SIGIO), and the signal han‐
598       dler is established using SA_SIGINFO, then the handler will  receive  a
599       siginfo_t structure as its second argument, and the si_fd field of this
600       argument will hold the file descriptor of the leased file that has been
601       accessed  by  another  process.   (This  is  useful if the caller holds
602       leases against multiple files.)
603
604   File and directory change notification (dnotify)
605       F_NOTIFY (int)
606              (Linux 2.4 onward) Provide notification when the  directory  re‐
607              ferred to by fd or any of the files that it contains is changed.
608              The events to be notified are specified in arg, which is  a  bit
609              mask  specified  by ORing together zero or more of the following
610              bits:
611
612              DN_ACCESS
613                     A file was accessed  (read(2),  pread(2),  readv(2),  and
614                     similar)
615              DN_MODIFY
616                     A  file  was  modified  (write(2),  pwrite(2), writev(2),
617                     truncate(2), ftruncate(2), and similar).
618              DN_CREATE
619                     A  file  was  created   (open(2),   creat(2),   mknod(2),
620                     mkdir(2), link(2), symlink(2), rename(2) into this direc‐
621                     tory).
622              DN_DELETE
623                     A file was unlinked (unlink(2), rename(2) to another  di‐
624                     rectory, rmdir(2)).
625              DN_RENAME
626                     A file was renamed within this directory (rename(2)).
627              DN_ATTRIB
628                     The   attributes   of  a  file  were  changed  (chown(2),
629                     chmod(2), utime(2), utimensat(2), and similar).
630
631              (In order to obtain these definitions, the  _GNU_SOURCE  feature
632              test macro must be defined before including any header files.)
633
634              Directory  notifications are normally "one-shot", and the appli‐
635              cation must reregister to receive further notifications.  Alter‐
636              natively,  if DN_MULTISHOT is included in arg, then notification
637              will remain in effect until explicitly removed.
638
639              A series of F_NOTIFY requests is cumulative, with the events  in
640              arg  being added to the set already monitored.  To disable noti‐
641              fication of all events, make an F_NOTIFY call specifying arg  as
642              0.
643
644              Notification  occurs via delivery of a signal.  The default sig‐
645              nal is SIGIO, but this can be changed using the F_SETSIG command
646              to  fcntl().  (Note that SIGIO is one of the nonqueuing standard
647              signals; switching to the use of a real-time signal  means  that
648              multiple  notifications  can  be queued to the process.)  In the
649              latter case, the signal handler receives a  siginfo_t  structure
650              as  its  second  argument  (if the handler was established using
651              SA_SIGINFO) and the si_fd field of this structure  contains  the
652              file  descriptor  which  generated the notification (useful when
653              establishing notification on multiple directories).
654
655              Especially when using DN_MULTISHOT, a real time signal should be
656              used  for  notification,  so  that multiple notifications can be
657              queued.
658
659              NOTE: New applications should use the inotify interface  (avail‐
660              able since kernel 2.6.13), which provides a much superior inter‐
661              face for obtaining notifications of filesystem events.  See ino‐
662              tify(7).
663
664   Changing the capacity of a pipe
665       F_SETPIPE_SZ (int; since Linux 2.6.35)
666              Change the capacity of the pipe referred to by fd to be at least
667              arg bytes.  An unprivileged process can adjust the pipe capacity
668              to  any value between the system page size and the limit defined
669              in /proc/sys/fs/pipe-max-size (see proc(5)).   Attempts  to  set
670              the pipe capacity below the page size are silently rounded up to
671              the page size.  Attempts by an unprivileged process to  set  the
672              pipe  capacity  above  the  limit  in /proc/sys/fs/pipe-max-size
673              yield the error EPERM; a privileged  process  (CAP_SYS_RESOURCE)
674              can override the limit.
675
676              When  allocating  the  buffer for the pipe, the kernel may use a
677              capacity larger than arg, if that is convenient for  the  imple‐
678              mentation.   (In  the  current implementation, the allocation is
679              the next higher power-of-two page-size multiple of the requested
680              size.)   The  actual capacity (in bytes) that is set is returned
681              as the function result.
682
683              Attempting to set the pipe capacity smaller than the  amount  of
684              buffer  space  currently  used  to store data produces the error
685              EBUSY.
686
687              Note that because of the way the pages of the  pipe  buffer  are
688              employed  when  data is written to the pipe, the number of bytes
689              that can be written may be less than the nominal size, depending
690              on the size of the writes.
691
692       F_GETPIPE_SZ (void; since Linux 2.6.35)
693              Return  (as  the  function  result) the capacity of the pipe re‐
694              ferred to by fd.
695
696   File Sealing
697       File seals limit the set of allowed operations on a  given  file.   For
698       each seal that is set on a file, a specific set of operations will fail
699       with EPERM on this file from now on.  The file is said  to  be  sealed.
700       The default set of seals depends on the type of the underlying file and
701       filesystem.  For an overview of file sealing, a discussion of its  pur‐
702       pose, and some code examples, see memfd_create(2).
703
704       Currently, file seals can be applied only to a file descriptor returned
705       by memfd_create(2) (if the MFD_ALLOW_SEALING was employed).   On  other
706       filesystems,  all  fcntl() operations that operate on seals will return
707       EINVAL.
708
709       Seals are a property of an inode.  Thus, all open file descriptors  re‐
710       ferring  to  the  same inode share the same set of seals.  Furthermore,
711       seals can never be removed, only added.
712
713       F_ADD_SEALS (int; since Linux 3.17)
714              Add the seals given in the bit-mask argument arg to the  set  of
715              seals of the inode referred to by the file descriptor fd.  Seals
716              cannot be removed again.  Once this call succeeds, the seals are
717              enforced by the kernel immediately.  If the current set of seals
718              includes F_SEAL_SEAL (see below), then this  call  will  be  re‐
719              jected  with  EPERM.  Adding a seal that is already set is a no-
720              op, in case F_SEAL_SEAL is not set already.  In order to place a
721              seal, the file descriptor fd must be writable.
722
723       F_GET_SEALS (void; since Linux 3.17)
724              Return  (as the function result) the current set of seals of the
725              inode referred to by fd.  If no seals are set,  0  is  returned.
726              If  the  file does not support sealing, -1 is returned and errno
727              is set to EINVAL.
728
729       The following seals are available:
730
731       F_SEAL_SEAL
732              If  this  seal  is  set,  any  further  call  to  fcntl()   with
733              F_ADD_SEALS  fails  with  the error EPERM.  Therefore, this seal
734              prevents any modifications to the set of seals itself.   If  the
735              initial  set  of seals of a file includes F_SEAL_SEAL, then this
736              effectively causes the set of seals to be constant and locked.
737
738       F_SEAL_SHRINK
739              If this seal is set, the file in question cannot be  reduced  in
740              size.   This  affects  open(2)  with the O_TRUNC flag as well as
741              truncate(2) and ftruncate(2).  Those calls fail  with  EPERM  if
742              you  try  to  shrink  the file in question.  Increasing the file
743              size is still possible.
744
745       F_SEAL_GROW
746              If this seal is set, the size of the file in question cannot  be
747              increased.   This  affects  write(2) beyond the end of the file,
748              truncate(2), ftruncate(2), and fallocate(2).  These  calls  fail
749              with  EPERM  if  you use them to increase the file size.  If you
750              keep the size or shrink it, those calls still work as expected.
751
752       F_SEAL_WRITE
753              If this seal is set, you cannot modify the contents of the file.
754              Note  that  shrinking  or  growing the size of the file is still
755              possible and allowed.  Thus, this seal is normally used in  com‐
756              bination  with  one  of  the  other  seals.   This  seal affects
757              write(2) and fallocate(2) (only in  combination  with  the  FAL‐
758              LOC_FL_PUNCH_HOLE  flag).   Those  calls fail with EPERM if this
759              seal is set.  Furthermore, trying to create new shared, writable
760              memory-mappings via mmap(2) will also fail with EPERM.
761
762              Using  the  F_ADD_SEALS  operation  to set the F_SEAL_WRITE seal
763              fails with EBUSY if any writable, shared mapping  exists.   Such
764              mappings  must  be  unmapped before you can add this seal.  Fur‐
765              thermore, if there are any asynchronous I/O operations  (io_sub‐
766              mit(2)) pending on the file, all outstanding writes will be dis‐
767              carded.
768
769       F_SEAL_FUTURE_WRITE (since Linux 5.1)
770              The effect of this seal is similar to F_SEAL_WRITE, but the con‐
771              tents of the file can still be modified via shared writable map‐
772              pings that were created prior to the seal being  set.   Any  at‐
773              tempt  to  create a new writable mapping on the file via mmap(2)
774              will fail with EPERM.  Likewise, an attempt to write to the file
775              via write(2) will fail with EPERM.
776
777              Using  this seal, one process can create a memory buffer that it
778              can continue to modify while sharing that  buffer  on  a  "read-
779              only" basis with other processes.
780
781   File read/write hints
782       Write  lifetime  hints can be used to inform the kernel about the rela‐
783       tive expected lifetime of writes on a given inode or via  a  particular
784       open  file  description.   (See open(2) for an explanation of open file
785       descriptions.)  In this context, the term "write  lifetime"  means  the
786       expected  time the data will live on media, before being overwritten or
787       erased.
788
789       An application may use the different hint  values  specified  below  to
790       separate writes into different write classes, so that multiple users or
791       applications running on a single storage back-end can  aggregate  their
792       I/O  patterns in a consistent manner.  However, there are no functional
793       semantics implied by these flags, and different I/O classes can use the
794       write  lifetime  hints in arbitrary ways, so long as the hints are used
795       consistently.
796
797       The following operations can be applied to the file descriptor, fd:
798
799       F_GET_RW_HINT (uint64_t *; since Linux 4.13)
800              Returns the value of the read/write hint associated with the un‐
801              derlying inode referred to by fd.
802
803       F_SET_RW_HINT (uint64_t *; since Linux 4.13)
804              Sets  the  read/write  hint value associated with the underlying
805              inode referred to by fd.  This hint persists until either it  is
806              explicitly modified or the underlying filesystem is unmounted.
807
808       F_GET_FILE_RW_HINT (uint64_t *; since Linux 4.13)
809              Returns  the  value  of  the read/write hint associated with the
810              open file description referred to by fd.
811
812       F_SET_FILE_RW_HINT (uint64_t *; since Linux 4.13)
813              Sets the read/write hint value associated with the open file de‐
814              scription referred to by fd.
815
816       If  an  open  file description has not been assigned a read/write hint,
817       then it shall use the value assigned to the inode, if any.
818
819       The following read/write hints are valid since Linux 4.13:
820
821       RWH_WRITE_LIFE_NOT_SET
822              No specific hint has been set.  This is the default value.
823
824       RWH_WRITE_LIFE_NONE
825              No specific write lifetime is associated with this file  or  in‐
826              ode.
827
828       RWH_WRITE_LIFE_SHORT
829              Data  written to this inode or via this open file description is
830              expected to have a short lifetime.
831
832       RWH_WRITE_LIFE_MEDIUM
833              Data written to this inode or via this open file description  is
834              expected  to  have  a  lifetime  longer  than  data written with
835              RWH_WRITE_LIFE_SHORT.
836
837       RWH_WRITE_LIFE_LONG
838              Data written to this inode or via this open file description  is
839              expected  to  have  a  lifetime  longer  than  data written with
840              RWH_WRITE_LIFE_MEDIUM.
841
842       RWH_WRITE_LIFE_EXTREME
843              Data written to this inode or via this open file description  is
844              expected  to  have  a  lifetime  longer  than  data written with
845              RWH_WRITE_LIFE_LONG.
846
847       All the write-specific hints are relative to each other, and  no  indi‐
848       vidual absolute meaning should be attributed to them.
849

RETURN VALUE

851       For a successful call, the return value depends on the operation:
852
853       F_DUPFD
854              The new file descriptor.
855
856       F_GETFD
857              Value of file descriptor flags.
858
859       F_GETFL
860              Value of file status flags.
861
862       F_GETLEASE
863              Type of lease held on file descriptor.
864
865       F_GETOWN
866              Value of file descriptor owner.
867
868       F_GETSIG
869              Value  of  signal  sent  when read or write becomes possible, or
870              zero for traditional SIGIO behavior.
871
872       F_GETPIPE_SZ, F_SETPIPE_SZ
873              The pipe capacity.
874
875       F_GET_SEALS
876              A bit mask identifying the seals that have been set for the  in‐
877              ode referred to by fd.
878
879       All other commands
880              Zero.
881
882       On error, -1 is returned, and errno is set to indicate the error.
883

ERRORS

885       EACCES or EAGAIN
886              Operation is prohibited by locks held by other processes.
887
888       EAGAIN The  operation  is  prohibited because the file has been memory-
889              mapped by another process.
890
891       EBADF  fd is not an open file descriptor
892
893       EBADF  cmd is F_SETLK or F_SETLKW and the  file  descriptor  open  mode
894              doesn't match with the type of lock requested.
895
896       EBUSY  cmd  is  F_SETPIPE_SZ and the new pipe capacity specified in arg
897              is smaller than the amount of buffer  space  currently  used  to
898              store data in the pipe.
899
900       EBUSY  cmd  is F_ADD_SEALS, arg includes F_SEAL_WRITE, and there exists
901              a writable, shared mapping on the file referred to by fd.
902
903       EDEADLK
904              It was detected that the specified F_SETLKW command would  cause
905              a deadlock.
906
907       EFAULT lock is outside your accessible address space.
908
909       EINTR  cmd  is  F_SETLKW  or  F_OFD_SETLKW and the operation was inter‐
910              rupted by a signal; see signal(7).
911
912       EINTR  cmd is F_GETLK, F_SETLK, F_OFD_GETLK, or  F_OFD_SETLK,  and  the
913              operation  was  interrupted  by  a  signal  before  the lock was
914              checked or acquired.  Most likely when  locking  a  remote  file
915              (e.g., locking over NFS), but can sometimes happen locally.
916
917       EINVAL The value specified in cmd is not recognized by this kernel.
918
919       EINVAL cmd is F_ADD_SEALS and arg includes an unrecognized sealing bit.
920
921       EINVAL cmd  is F_ADD_SEALS or F_GET_SEALS and the filesystem containing
922              the inode referred to by fd does not support sealing.
923
924       EINVAL cmd is F_DUPFD and arg is negative or is greater than the  maxi‐
925              mum  allowable  value  (see  the  discussion of RLIMIT_NOFILE in
926              getrlimit(2)).
927
928       EINVAL cmd is F_SETSIG and arg is not an allowable signal number.
929
930       EINVAL cmd is F_OFD_SETLK, F_OFD_SETLKW, or F_OFD_GETLK, and l_pid  was
931              not specified as zero.
932
933       EMFILE cmd  is  F_DUPFD and the per-process limit on the number of open
934              file descriptors has been reached.
935
936       ENOLCK Too many segment locks open, lock table is  full,  or  a  remote
937              locking protocol failed (e.g., locking over NFS).
938
939       ENOTDIR
940              F_NOTIFY was specified in cmd, but fd does not refer to a direc‐
941              tory.
942
943       EPERM  cmd is F_SETPIPE_SZ and the soft or hard  user  pipe  limit  has
944              been reached; see pipe(7).
945
946       EPERM  Attempted  to clear the O_APPEND flag on a file that has the ap‐
947              pend-only attribute set.
948
949       EPERM  cmd was F_ADD_SEALS, but fd was not open for writing or the cur‐
950              rent set of seals on the file already includes F_SEAL_SEAL.
951

CONFORMING TO

953       SVr4,  4.3BSD,  POSIX.1-2001.   Only  the  operations F_DUPFD, F_GETFD,
954       F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK, and F_SETLKW are specified
955       in POSIX.1-2001.
956
957       F_GETOWN  and  F_SETOWN  are  specified in POSIX.1-2001.  (To get their
958       definitions, define either _XOPEN_SOURCE with the value 500 or greater,
959       or _POSIX_C_SOURCE with the value 200809L or greater.)
960
961       F_DUPFD_CLOEXEC is specified in POSIX.1-2008.  (To get this definition,
962       define  _POSIX_C_SOURCE  with  the  value  200809L   or   greater,   or
963       _XOPEN_SOURCE with the value 700 or greater.)
964
965       F_GETOWN_EX,  F_SETOWN_EX, F_SETPIPE_SZ, F_GETPIPE_SZ, F_GETSIG, F_SET‐
966       SIG, F_NOTIFY, F_GETLEASE, and F_SETLEASE are Linux-specific.   (Define
967       the _GNU_SOURCE macro to obtain these definitions.)
968
969       F_OFD_SETLK,  F_OFD_SETLKW, and F_OFD_GETLK are Linux-specific (and one
970       must define _GNU_SOURCE to obtain their definitions), but work is being
971       done to have them included in the next version of POSIX.1.
972
973       F_ADD_SEALS and F_GET_SEALS are Linux-specific.
974

NOTES

976       The  errors  returned  by  dup2(2) are different from those returned by
977       F_DUPFD.
978
979   File locking
980       The original Linux fcntl() system call was not designed to handle large
981       file offsets (in the flock structure).  Consequently, an fcntl64() sys‐
982       tem call was added in Linux 2.4.  The newer system call employs a  dif‐
983       ferent structure for file locking, flock64, and corresponding commands,
984       F_GETLK64, F_SETLK64, and F_SETLKW64.  However, these  details  can  be
985       ignored  by  applications  using  glibc, whose fcntl() wrapper function
986       transparently employs the more recent system call where  it  is  avail‐
987       able.
988
989   Record locks
990       Since  kernel  2.0,  there  is no interaction between the types of lock
991       placed by flock(2) and fcntl().
992
993       Several systems have more fields in struct flock such as, for  example,
994       l_sysid  (to  identify  the  machine where the lock is held).  Clearly,
995       l_pid alone is not going to be very useful if the process  holding  the
996       lock  may  live on a different machine; on Linux, while present on some
997       architectures (such as MIPS32), this field is not used.
998
999       The original Linux fcntl() system call was not designed to handle large
1000       file offsets (in the flock structure).  Consequently, an fcntl64() sys‐
1001       tem call was added in Linux 2.4.  The newer system call employs a  dif‐
1002       ferent structure for file locking, flock64, and corresponding commands,
1003       F_GETLK64, F_SETLK64, and F_SETLKW64.  However, these  details  can  be
1004       ignored  by  applications  using  glibc, whose fcntl() wrapper function
1005       transparently employs the more recent system call where  it  is  avail‐
1006       able.
1007
1008   Record locking and NFS
1009       Before Linux 3.12, if an NFSv4 client loses contact with the server for
1010       a period of time (defined as more than 90 seconds  with  no  communica‐
1011       tion),  it might lose and regain a lock without ever being aware of the
1012       fact.  (The period of time after which contact is assumed lost is known
1013       as  the NFSv4 leasetime.  On a Linux NFS server, this can be determined
1014       by looking at /proc/fs/nfsd/nfsv4leasetime, which expresses the  period
1015       in seconds.  The default value for this file is 90.)  This scenario po‐
1016       tentially risks data corruption, since another process might acquire  a
1017       lock in the intervening period and perform file I/O.
1018
1019       Since Linux 3.12, if an NFSv4 client loses contact with the server, any
1020       I/O to the file by a process which "thinks" it holds a lock  will  fail
1021       until  that  process  closes and reopens the file.  A kernel parameter,
1022       nfs.recover_lost_locks, can be set to 1 to obtain the  pre-3.12  behav‐
1023       ior, whereby the client will attempt to recover lost locks when contact
1024       is reestablished with the server.  Because of  the  attendant  risk  of
1025       data corruption, this parameter defaults to 0 (disabled).
1026

BUGS

1028   F_SETFL
1029       It  is  not  possible to use F_SETFL to change the state of the O_DSYNC
1030       and O_SYNC flags.  Attempts to change the  state  of  these  flags  are
1031       silently ignored.
1032
1033   F_GETOWN
1034       A limitation of the Linux system call conventions on some architectures
1035       (notably i386) means that if a (negative) process group ID  to  be  re‐
1036       turned  by  F_GETOWN  falls  in  the range -1 to -4095, then the return
1037       value is wrongly interpreted by glibc as an error in the  system  call;
1038       that is, the return value of fcntl() will be -1, and errno will contain
1039       the (positive) process group ID.  The Linux-specific F_GETOWN_EX opera‐
1040       tion  avoids  this  problem.  Since glibc version 2.11, glibc makes the
1041       kernel  F_GETOWN  problem  invisible  by  implementing  F_GETOWN  using
1042       F_GETOWN_EX.
1043
1044   F_SETOWN
1045       In  Linux 2.4 and earlier, there is bug that can occur when an unprivi‐
1046       leged process uses F_SETOWN to specify the owner of a socket  file  de‐
1047       scriptor as a process (group) other than the caller.  In this case, fc‐
1048       ntl() can return -1 with errno  set  to  EPERM,  even  when  the  owner
1049       process  (group)  is one that the caller has permission to send signals
1050       to.  Despite this error return, the file descriptor owner is  set,  and
1051       signals will be sent to the owner.
1052
1053   Deadlock detection
1054       The  deadlock-detection  algorithm  employed by the kernel when dealing
1055       with F_SETLKW requests can yield both false negatives (failures to  de‐
1056       tect  deadlocks,  leaving a set of deadlocked processes blocked indefi‐
1057       nitely) and false positives (EDEADLK errors when there is no deadlock).
1058       For  example, the kernel limits the lock depth of its dependency search
1059       to 10 steps, meaning that circular deadlock  chains  that  exceed  that
1060       size  will  not be detected.  In addition, the kernel may falsely indi‐
1061       cate a deadlock when two or more processes created using  the  clone(2)
1062       CLONE_FILES flag place locks that appear (to the kernel) to conflict.
1063
1064   Mandatory locking
1065       The Linux implementation of mandatory locking is subject to race condi‐
1066       tions which render it unreliable: a write(2) call that overlaps with  a
1067       lock  may  modify  data after the mandatory lock is acquired; a read(2)
1068       call that overlaps with a lock may detect changes  to  data  that  were
1069       made only after a write lock was acquired.  Similar races exist between
1070       mandatory locks and mmap(2).  It is therefore inadvisable  to  rely  on
1071       mandatory locking.
1072

SEE ALSO

1074       dup2(2),  flock(2), open(2), socket(2), lockf(3), capabilities(7), fea‐
1075       ture_test_macros(7), lslocks(8)
1076
1077       locks.txt, mandatory-locking.txt, and dnotify.txt in the  Linux  kernel
1078       source  directory  Documentation/filesystems/  (on older kernels, these
1079       files are directly  under  the  Documentation/  directory,  and  manda‐
1080       tory-locking.txt is called mandatory.txt)
1081

COLOPHON

1083       This  page  is  part of release 5.13 of the Linux man-pages project.  A
1084       description of the project, information about reporting bugs,  and  the
1085       latest     version     of     this    page,    can    be    found    at
1086       https://www.kernel.org/doc/man-pages/.
1087
1088
1089
1090Linux                             2021-03-22                          FCNTL(2)
Impressum