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

NAME

6       fcntl - manipulate file descriptor
7

SYNOPSIS

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

DESCRIPTION

15       fcntl() performs one of the operations described below on the open file
16       descriptor fd.  The operation is determined by cmd.
17
18       fcntl() can take an optional third argument.  Whether or not this argu‐
19       ment  is  required is determined by cmd.  The required argument type is
20       indicated in parentheses after  each  cmd  name  (in  most  cases,  the
21       required  type  is  long,  and  we identify the argument using the name
22       arg), or void is specified if the argument is not required.
23
24   Duplicating a file descriptor
25       F_DUPFD (long)
26              Find the lowest numbered available file descriptor greater  than
27              or  equal to arg and make it be a copy of fd.  This is different
28              from dup2(2), which uses exactly the descriptor specified.
29
30              On success, the new descriptor is returned.
31
32              See dup(2) for further details.
33
34       F_DUPFD_CLOEXEC (long; since Linux 2.6.24)
35              As for F_DUPFD, but additionally set the close-on-exec flag  for
36              the  duplicate  descriptor.  Specifying this flag permits a pro‐
37              gram to avoid an additional fcntl() F_SETFD operation to set the
38              FD_CLOEXEC flag.  For an explanation of why this flag is useful,
39              see the description of O_CLOEXEC in open(2).
40
41   File descriptor flags
42       The following commands manipulate the  flags  associated  with  a  file
43       descriptor.   Currently, only one such flag is defined: FD_CLOEXEC, the
44       close-on-exec flag.  If the FD_CLOEXEC bit is 0,  the  file  descriptor
45       will remain open across an execve(2), otherwise it will be closed.
46
47       F_GETFD (void)
48              Read the file descriptor flags; arg is ignored.
49
50       F_SETFD (long)
51              Set the file descriptor flags to the value specified by arg.
52
53   File status flags
54       Each  open  file  description has certain associated status flags, ini‐
55       tialized by open(2) and possibly modified by fcntl().  Duplicated  file
56       descriptors  (made with dup(2), fcntl(F_DUPFD), fork(2), etc.) refer to
57       the same open file description, and thus share  the  same  file  status
58       flags.
59
60       The file status flags and their semantics are described in open(2).
61
62       F_GETFL (void)
63              Read the file status flags; arg is ignored.
64
65       F_SETFL (long)
66              Set  the  file status flags to the value specified by arg.  File
67              access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags
68              (i.e.,  O_CREAT,  O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored.
69              On Linux this command can only  change  the  O_APPEND,  O_ASYNC,
70              O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
71
72   Advisory locking
73       F_GETLK,  F_SETLK  and  F_SETLKW are used to acquire, release, and test
74       for the existence of record locks (also known as file-segment or  file-
75       region  locks).   The third argument, lock, is a pointer to a structure
76       that has at least the following fields (in unspecified order).
77
78           struct flock {
79               ...
80               short l_type;    /* Type of lock: F_RDLCK,
81                                   F_WRLCK, F_UNLCK */
82               short l_whence;  /* How to interpret l_start:
83                                   SEEK_SET, SEEK_CUR, SEEK_END */
84               off_t l_start;   /* Starting offset for lock */
85               off_t l_len;     /* Number of bytes to lock */
86               pid_t l_pid;     /* PID of process blocking our lock
87                                   (F_GETLK only) */
88               ...
89           };
90
91       The l_whence, l_start, and l_len fields of this structure  specify  the
92       range  of bytes we wish to lock.  Bytes past the end of the file may be
93       locked, but not bytes before the start of the file.
94
95       l_start is the starting offset for the lock, and is  interpreted  rela‐
96       tive  to  either:  the start of the file (if l_whence is SEEK_SET); the
97       current file offset (if l_whence is SEEK_CUR); or the end of  the  file
98       (if  l_whence  is  SEEK_END).  In the final two cases, l_start can be a
99       negative number provided the offset does not lie before  the  start  of
100       the file.
101
102       l_len  specifies  the  number of bytes to be locked.  If l_len is posi‐
103       tive, then the range to be  locked  covers  bytes  l_start  up  to  and
104       including  l_start+l_len-1.   Specifying  0  for  l_len has the special
105       meaning: lock all bytes starting at the location specified by  l_whence
106       and  l_start  through  to the end of file, no matter how large the file
107       grows.
108
109       POSIX.1-2001 allows (but does not require) an implementation to support
110       a negative l_len value; if l_len is negative, the interval described by
111       lock covers bytes l_start+l_len up to and including l_start-1.  This is
112       supported by Linux since kernel versions 2.4.21 and 2.5.49.
113
114       The  l_type  field  can  be  used  to place a read (F_RDLCK) or a write
115       (F_WRLCK) lock on a file.  Any number of processes may hold a read lock
116       (shared  lock)  on a file region, but only one process may hold a write
117       lock (exclusive lock).  An exclusive lock  excludes  all  other  locks,
118       both  shared and exclusive.  A single process can hold only one type of
119       lock on a file region; if a new lock is applied  to  an  already-locked
120       region,  then  the  existing  lock  is  converted to the new lock type.
121       (Such conversions may involve splitting, shrinking, or coalescing  with
122       an  existing  lock if the byte range specified by the new lock does not
123       precisely coincide with the range of the existing lock.)
124
125       F_SETLK (struct flock *)
126              Acquire a lock (when l_type is F_RDLCK or F_WRLCK) or release  a
127              lock  (when  l_type  is  F_UNLCK)  on the bytes specified by the
128              l_whence, l_start, and l_len fields of lock.  If  a  conflicting
129              lock  is  held by another process, this call returns -1 and sets
130              errno to EACCES or EAGAIN.
131
132       F_SETLKW (struct flock *)
133              As for F_SETLK, but if a conflicting lock is held on  the  file,
134              then  wait  for that lock to be released.  If a signal is caught
135              while waiting, then the call is interrupted and (after the  sig‐
136              nal handler has returned) returns immediately (with return value
137              -1 and errno set to EINTR; see signal(7)).
138
139       F_GETLK (struct flock *)
140              On input to this call, lock describes a lock we  would  like  to
141              place  on  the  file.  If the lock could be placed, fcntl() does
142              not actually place it, but returns F_UNLCK in the  l_type  field
143              of  lock and leaves the other fields of the structure unchanged.
144              If one or more incompatible locks would prevent this lock  being
145              placed, then fcntl() returns details about one of these locks in
146              the l_type, l_whence, l_start, and l_len fields of lock and sets
147              l_pid to be the PID of the process holding that lock.
148
149       In  order  to place a read lock, fd must be open for reading.  In order
150       to place a write lock, fd must be open  for  writing.   To  place  both
151       types of lock, open a file read-write.
152
153       As well as being removed by an explicit F_UNLCK, record locks are auto‐
154       matically released when the process terminates or if it closes any file
155       descriptor  referring  to a file on which locks are held.  This is bad:
156       it means that a process can lose the locks on a file  like  /etc/passwd
157       or  /etc/mtab  when for some reason a library function decides to open,
158       read and close it.
159
160       Record locks are not inherited by a child created via fork(2), but  are
161       preserved across an execve(2).
162
163       Because  of the buffering performed by the stdio(3) library, the use of
164       record locking with routines in that package  should  be  avoided;  use
165       read(2) and write(2) instead.
166
167   Mandatory locking
168       (Non-POSIX.)   The  above record locks may be either advisory or manda‐
169       tory, and are advisory by default.
170
171       Advisory locks are not enforced and are useful only between cooperating
172       processes.
173
174       Mandatory  locks are enforced for all processes.  If a process tries to
175       perform an incompatible access (e.g., read(2) or write(2))  on  a  file
176       region that has an incompatible mandatory lock, then the result depends
177       upon whether the O_NONBLOCK flag is enabled for its open file  descrip‐
178       tion.   If  the  O_NONBLOCK  flag  is  not enabled, then system call is
179       blocked until the lock is removed or converted to a mode that  is  com‐
180       patible  with  the access.  If the O_NONBLOCK flag is enabled, then the
181       system call fails with the error EAGAIN.
182
183       To make use of mandatory locks, mandatory locking must be enabled  both
184       on the file system that contains the file to be locked, and on the file
185       itself.  Mandatory locking is enabled on a file system  using  the  "-o
186       mand" option to mount(8), or the MS_MANDLOCK flag for mount(2).  Manda‐
187       tory locking is enabled on a file by disabling group execute permission
188       on  the file and enabling the set-group-ID permission bit (see chmod(1)
189       and chmod(2)).
190
191       The Linux implementation of mandatory locking is unreliable.  See  BUGS
192       below.
193
194   Managing signals
195       F_GETOWN, F_SETOWN, F_GETOWN_EX, F_SETOWN_EX, F_GETSIG and F_SETSIG are
196       used to manage I/O availability signals:
197
198       F_GETOWN (void)
199              Return (as the function result) the process ID or process  group
200              currently  receiving SIGIO and SIGURG signals for events on file
201              descriptor fd.  Process IDs are  returned  as  positive  values;
202              process  group IDs are returned as negative values (but see BUGS
203              below).  arg is ignored.
204
205       F_SETOWN (long)
206              Set the process ID or process group ID that will  receive  SIGIO
207              and  SIGURG  signals  for events on file descriptor fd to the ID
208              given in arg.  A process ID is specified as a positive value;  a
209              process  group  ID  is specified as a negative value.  Most com‐
210              monly, the calling process specifies itself as the  owner  (that
211              is, arg is specified as getpid(2)).
212
213              If you set the O_ASYNC status flag on a file descriptor by using
214              the F_SETFL command of fcntl(), a SIGIO signal is sent  whenever
215              input  or  output  becomes  possible  on  that  file descriptor.
216              F_SETSIG can be used to obtain delivery of a signal  other  than
217              SIGIO.   If  this  permission  check  fails,  then the signal is
218              silently discarded.
219
220              Sending a signal to  the  owner  process  (group)  specified  by
221              F_SETOWN  is  subject  to  the  same  permissions  checks as are
222              described for kill(2), where the sending process is the one that
223              employs F_SETOWN (but see BUGS below).
224
225              If  the  file  descriptor  fd  refers to a socket, F_SETOWN also
226              selects the recipient of SIGURG signals that are delivered  when
227              out-of-band data arrives on that socket.  (SIGURG is sent in any
228              situation where select(2) would report the socket as  having  an
229              "exceptional condition".)
230
231              The following was true in 2.6.x kernels up to and including ker‐
232              nel 2.6.11:
233
234                     If a nonzero value is  given  to  F_SETSIG  in  a  multi‐
235                     threaded  process  running  with a threading library that
236                     supports thread groups  (e.g.,  NPTL),  then  a  positive
237                     value  given to F_SETOWN has a different meaning: instead
238                     of being a process ID identifying a whole process, it  is
239                     a  thread  ID  identifying  a  specific  thread  within a
240                     process.  Consequently,  it  may  be  necessary  to  pass
241                     F_SETOWN  the result of gettid(2) instead of getpid(2) to
242                     get sensible results when F_SETSIG is used.  (In  current
243                     Linux  threading  implementations, a main thread's thread
244                     ID is the same as its process ID.  This means that a sin‐
245                     gle-threaded  program  can  equally use gettid(2) or get‐
246                     pid(2) in this scenario.)  Note, however, that the state‐
247                     ments in this paragraph do not apply to the SIGURG signal
248                     generated for out-of-band data on a socket:  this  signal
249                     is  always  sent  to either a process or a process group,
250                     depending on the value given to F_SETOWN.
251
252              The above behavior was accidentally dropped in Linux 2.6.12, and
253              won't  be  restored.  From Linux 2.6.32 onwards, use F_SETOWN_EX
254              to target SIGIO and SIGURG signals at a particular thread.
255
256       F_GETOWN_EX (struct f_owner_ex *) (since Linux 2.6.32)
257              Return the current file descriptor owner settings as defined  by
258              a  previous  F_SETOWN_EX operation.  The information is returned
259              in the structure pointed to by  arg,  which  has  the  following
260              form:
261
262                  struct f_owner_ex {
263                      int   type;
264                      pid_t pid;
265                  };
266
267              The  type  field  will  have  one  of  the  values  F_OWNER_TID,
268              F_OWNER_PID, or F_OWNER_PGRP.  The pid field is a positive inte‐
269              ger  representing  a thread ID, process ID, or process group ID.
270              See F_SETOWN_EX for more details.
271
272       F_SETOWN_EX (struct f_owner_ex *) (since Linux 2.6.32)
273              This operation performs a similar task to F_SETOWN.   It  allows
274              the  caller  to  direct  I/O  availability signals to a specific
275              thread, process, or process group.   The  caller  specifies  the
276              target  of  signals  via arg, which is a pointer to a f_owner_ex
277              structure.  The type field has  one  of  the  following  values,
278              which define how pid is interpreted:
279
280              F_OWNER_TID
281                     Send  the signal to the thread whose thread ID (the value
282                     returned by a call to clone(2) or gettid(2)) is specified
283                     in pid.
284
285              F_OWNER_PID
286                     Send  the  signal to the process whose ID is specified in
287                     pid.
288
289              F_OWNER_PGRP
290                     Send the signal to the process group whose ID  is  speci‐
291                     fied in pid.  (Note that, unlike with F_SETOWN, a process
292                     group ID is specified as a positive value here.)
293
294       F_GETSIG (void)
295              Return (as the function result) the signal sent  when  input  or
296              output  becomes  possible.  A value of zero means SIGIO is sent.
297              Any other value (including SIGIO) is the  signal  sent  instead,
298              and in this case additional info is available to the signal han‐
299              dler if installed with SA_SIGINFO.  arg is ignored.
300
301       F_SETSIG (long)
302              Set the signal sent when input or output becomes possible to the
303              value  given  in arg.  A value of zero means to send the default
304              SIGIO signal.  Any other value (including SIGIO) is  the  signal
305              to  send  instead, and in this case additional info is available
306              to the signal handler if installed with SA_SIGINFO.
307
308              By using F_SETSIG with a nonzero value, and  setting  SA_SIGINFO
309              for  the  signal  handler  (see sigaction(2)), extra information
310              about I/O events is passed to the handler in a siginfo_t  struc‐
311              ture.   If  the  si_code field indicates the source is SI_SIGIO,
312              the si_fd field gives the file descriptor  associated  with  the
313              event.  Otherwise, there is no indication which file descriptors
314              are pending, and you should use the usual mechanisms (select(2),
315              poll(2),  read(2)  with  O_NONBLOCK set etc.) to determine which
316              file descriptors are available for I/O.
317
318              By selecting a real time signal (value  >=  SIGRTMIN),  multiple
319              I/O  events may be queued using the same signal numbers.  (Queu‐
320              ing is dependent on available  memory).   Extra  information  is
321              available if SA_SIGINFO is set for the signal handler, as above.
322
323              Note  that Linux imposes a limit on the number of real-time sig‐
324              nals that may be queued to a process (see getrlimit(2) and  sig‐
325              nal(7)) and if this limit is reached, then the kernel reverts to
326              delivering SIGIO, and this signal is  delivered  to  the  entire
327              process rather than to a specific thread.
328
329       Using  these mechanisms, a program can implement fully asynchronous I/O
330       without using select(2) or poll(2) most of the time.
331
332       The use of O_ASYNC, F_GETOWN, F_SETOWN is specific to  BSD  and  Linux.
333       F_GETOWN_EX,  F_SETOWN_EX,  F_GETSIG,  and F_SETSIG are Linux-specific.
334       POSIX has asynchronous I/O and the aio_sigevent  structure  to  achieve
335       similar  things; these are also available in Linux as part of the GNU C
336       Library (Glibc).
337
338   Leases
339       F_SETLEASE and F_GETLEASE (Linux 2.4 onwards) are  used  (respectively)
340       to  establish  a new lease, and retrieve the current lease, on the open
341       file description referred to by the file descriptor fd.  A  file  lease
342       provides  a mechanism whereby the process holding the lease (the "lease
343       holder") is notified (via delivery of a signal)  when  a  process  (the
344       "lease  breaker")  tries to open(2) or truncate(2) the file referred to
345       by that file descriptor.
346
347       F_SETLEASE (long)
348              Set or remove a file lease according to which of  the  following
349              values is specified in the integer arg:
350
351              F_RDLCK
352                     Take  out  a  read  lease.   This  will cause the calling
353                     process to be notified when the file is opened for  writ‐
354                     ing  or is truncated.  A read lease can only be placed on
355                     a file descriptor that is opened read-only.
356
357              F_WRLCK
358                     Take out a write lease.  This will cause the caller to be
359                     notified  when  the file is opened for reading or writing
360                     or is truncated.  A write lease may be placed on  a  file
361                     only  if there are no other open file descriptors for the
362                     file.
363
364              F_UNLCK
365                     Remove our lease from the file.
366
367       Leases are associated with an  open  file  description  (see  open(2)).
368       This  means  that  duplicate file descriptors (created by, for example,
369       fork(2) or dup(2)) refer to the same lease, and this lease may be modi‐
370       fied  or  released  using  any  of these descriptors.  Furthermore, the
371       lease is released by either an explicit F_UNLCK  operation  on  any  of
372       these  duplicate  descriptors,  or  when all such descriptors have been
373       closed.
374
375       Leases may only be taken out on regular files.  An unprivileged process
376       may  only take out a lease on a file whose UID (owner) matches the file
377       system UID of the process.  A process with the CAP_LEASE capability may
378       take out leases on arbitrary files.
379
380       F_GETLEASE (void)
381              Indicates  what  type  of  lease  is  associated  with  the file
382              descriptor fd by returning either F_RDLCK, F_WRLCK, or  F_UNLCK,
383              indicating,  respectively,  a  read lease , a write lease, or no
384              lease.  arg is ignored.
385
386       When a process (the "lease breaker") performs an open(2) or truncate(2)
387       that conflicts with a lease established via F_SETLEASE, the system call
388       is blocked by the kernel and the kernel notifies the  lease  holder  by
389       sending  it  a  signal  (SIGIO  by  default).   The lease holder should
390       respond to receipt of this signal by doing whatever cleanup is required
391       in  preparation  for  the file to be accessed by another process (e.g.,
392       flushing cached buffers) and then either remove or downgrade its lease.
393       A  lease  is removed by performing an F_SETLEASE command specifying arg
394       as F_UNLCK.  If the lease holder currently holds a write lease  on  the
395       file, and the lease breaker is opening the file for reading, then it is
396       sufficient for the lease holder to downgrade the lease to a read lease.
397       This  is  done  by  performing  an F_SETLEASE command specifying arg as
398       F_RDLCK.
399
400       If the lease holder fails to downgrade or remove the lease  within  the
401       number  of  seconds specified in /proc/sys/fs/lease-break-time then the
402       kernel forcibly removes or downgrades the lease holder's lease.
403
404       Once the lease has been voluntarily or forcibly removed or  downgraded,
405       and  assuming  the lease breaker has not unblocked its system call, the
406       kernel permits the lease breaker's system call to proceed.
407
408       If the lease breaker's blocked open(2) or truncate(2) is interrupted by
409       a  signal handler, then the system call fails with the error EINTR, but
410       the other steps still occur as described above.  If the  lease  breaker
411       is killed by a signal while blocked in open(2) or truncate(2), then the
412       other steps still occur as described above.  If the lease breaker spec‐
413       ifies  the  O_NONBLOCK flag when calling open(2), then the call immedi‐
414       ately fails with the error EWOULDBLOCK, but the other steps still occur
415       as described above.
416
417       The  default  signal used to notify the lease holder is SIGIO, but this
418       can be changed using the F_SETSIG command to fcntl().   If  a  F_SETSIG
419       command  is  performed (even one specifying SIGIO), and the signal han‐
420       dler is established using SA_SIGINFO, then the handler will  receive  a
421       siginfo_t structure as its second argument, and the si_fd field of this
422       argument will hold the descriptor of the  leased  file  that  has  been
423       accessed  by  another  process.   (This  is  useful if the caller holds
424       leases against multiple files).
425
426   File and directory change notification (dnotify)
427       F_NOTIFY (long)
428              (Linux 2.4 onwards)  Provide  notification  when  the  directory
429              referred  to  by  fd  or  any  of  the files that it contains is
430              changed.  The events to be notified are specified in arg,  which
431              is  a  bit  mask specified by ORing together zero or more of the
432              following bits:
433
434              DN_ACCESS   A file was accessed (read, pread, readv)
435              DN_MODIFY   A file was modified (write,  pwrite,  writev,  trun‐
436                          cate, ftruncate).
437              DN_CREATE   A file was created (open, creat, mknod, mkdir, link,
438                          symlink, rename).
439              DN_DELETE   A file  was  unlinked  (unlink,  rename  to  another
440                          directory, rmdir).
441              DN_RENAME   A file was renamed within this directory (rename).
442              DN_ATTRIB   The attributes of a file were changed (chown, chmod,
443                          utime[s]).
444
445              (In order to obtain these definitions, the  _GNU_SOURCE  feature
446              test macro must be defined.)
447
448              Directory  notifications are normally "one-shot", and the appli‐
449              cation must reregister to receive further notifications.  Alter‐
450              natively,  if DN_MULTISHOT is included in arg, then notification
451              will remain in effect until explicitly removed.
452
453              A series of F_NOTIFY requests is cumulative, with the events  in
454              arg  being added to the set already monitored.  To disable noti‐
455              fication of all events, make an F_NOTIFY call specifying arg  as
456              0.
457
458              Notification  occurs via delivery of a signal.  The default sig‐
459              nal is SIGIO, but this can be changed using the F_SETSIG command
460              to  fcntl().   In the latter case, the signal handler receives a
461              siginfo_t structure as its second argument (if the  handler  was
462              established using SA_SIGINFO) and the si_fd field of this struc‐
463              ture contains the file descriptor which generated the  notifica‐
464              tion (useful when establishing notification on multiple directo‐
465              ries).
466
467              Especially when using DN_MULTISHOT, a real time signal should be
468              used  for  notification,  so  that multiple notifications can be
469              queued.
470
471              NOTE: New applications should use the inotify interface  (avail‐
472              able since kernel 2.6.13), which provides a much superior inter‐
473              face for obtaining notifications of  file  system  events.   See
474              inotify(7).
475
476   Changing the capacity of a pipe
477       F_SETPIPE_SZ (long; since Linux 2.6.35)
478              Change the capacity of the pipe referred to by fd to be at least
479              arg bytes.  An unprivileged process can adjust the pipe capacity
480              to  any value between the system page size and the limit defined
481              in /proc/sys/fs/pipe-size-max (see proc(5)).   Attempts  to  set
482              the pipe capacity below the page size are silently rounded up to
483              the page size.  Attempts by an unprivileged process to  set  the
484              pipe  capacity  above  the  limit  in /proc/sys/fs/pipe-size-max
485              yield the error EPERM; a privileged  process  (CAP_SYS_RESOURCE)
486              can  override  the  limit.   When  allocating the buffer for the
487              pipe, the kernel may use a capacity larger than arg, if that  is
488              convenient  for  the implementation.  The F_GETPIPE_SZ operation
489              returns the actual size used.  Attempting to set the pipe capac‐
490              ity  smaller  than  the amount of buffer space currently used to
491              store data produces the error EBUSY.
492
493       F_GETPIPE_SZ (void; since Linux 2.6.35)
494              Return (as  the  function  result)  the  capacity  of  the  pipe
495              referred to by fd.
496

RETURN VALUE

498       For a successful call, the return value depends on the operation:
499
500       F_DUPFD  The new descriptor.
501
502       F_GETFD  Value of flags.
503
504       F_GETFL  Value of flags.
505
506       F_GETLEASE
507                Type of lease held on file descriptor.
508
509       F_GETOWN Value of descriptor owner.
510
511       F_GETSIG Value  of  signal sent when read or write becomes possible, or
512                zero for traditional SIGIO behavior.
513
514       F_GETPIPE_SZ
515                The pipe capacity.
516
517       All other commands
518                Zero.
519
520       On error, -1 is returned, and errno is set appropriately.
521

ERRORS

523       EACCES or EAGAIN
524              Operation is prohibited by locks held by other processes.
525
526       EAGAIN The operation is prohibited because the file  has  been  memory-
527              mapped by another process.
528
529       EBADF  fd is not an open file descriptor, or the command was F_SETLK or
530              F_SETLKW and the file descriptor open mode  doesn't  match  with
531              the type of lock requested.
532
533       EDEADLK
534              It  was detected that the specified F_SETLKW command would cause
535              a deadlock.
536
537       EFAULT lock is outside your accessible address space.
538
539       EINTR  For F_SETLKW, the command was interrupted by a signal; see  sig‐
540              nal(7).  For F_GETLK and F_SETLK, the command was interrupted by
541              a signal before the lock was checked or acquired.   Most  likely
542              when  locking  a  remote  file (e.g., locking over NFS), but can
543              sometimes happen locally.
544
545       EINVAL For F_DUPFD, arg is negative or  is  greater  than  the  maximum
546              allowable  value.   For F_SETSIG, arg is not an allowable signal
547              number.
548
549       EMFILE For F_DUPFD, the process already has the maximum number of  file
550              descriptors open.
551
552       ENOLCK Too  many  segment  locks  open, lock table is full, or a remote
553              locking protocol failed (e.g., locking over NFS).
554
555       EPERM  Attempted to clear the O_APPEND flag on  a  file  that  has  the
556              append-only attribute set.
557

CONFORMING TO

559       SVr4,  4.3BSD,  POSIX.1-2001.   Only  the  operations F_DUPFD, F_GETFD,
560       F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK,  F_SETLKW,  F_GETOWN,  and
561       F_SETOWN are specified in POSIX.1-2001.
562
563       F_DUPFD_CLOEXEC is specified in POSIX.1-2008.
564
565       F_GETOWN_EX,  F_SETOWN_EX, F_SETPIPE_SZ, F_GETPIPE_SZ, F_GETSIG, F_SET‐
566       SIG, F_NOTIFY, F_GETLEASE, and F_SETLEASE are Linux-specific.   (Define
567       the _GNU_SOURCE macro to obtain these definitions.)
568

NOTES

570       The  errors  returned  by  dup2(2) are different from those returned by
571       F_DUPFD.
572
573       Since kernel 2.0, there is no interaction between  the  types  of  lock
574       placed by flock(2) and fcntl().
575
576       Several  systems have more fields in struct flock such as, for example,
577       l_sysid.  Clearly, l_pid alone is not going to be very  useful  if  the
578       process holding the lock may live on a different machine.
579

BUGS

581       A limitation of the Linux system call conventions on some architectures
582       (notably i386) means that if  a  (negative)  process  group  ID  to  be
583       returned  by  F_GETOWN  falls in the range -1 to -4095, then the return
584       value is wrongly interpreted by glibc as an error in the  system  call;
585       that is, the return value of fcntl() will be -1, and errno will contain
586       the (positive) process group ID.  The Linux-specific F_GETOWN_EX opera‐
587       tion  avoids  this  problem.  Since glibc version 2.11, glibc makes the
588       kernel  F_GETOWN  problem  invisible  by  implementing  F_GETOWN  using
589       F_GETOWN_EX.
590
591       In  Linux 2.4 and earlier, there is bug that can occur when an unprivi‐
592       leged process uses F_SETOWN to specify  the  owner  of  a  socket  file
593       descriptor  as  a process (group) other than the caller.  In this case,
594       fcntl() can return -1 with errno set to  EPERM,  even  when  the  owner
595       process  (group)  is one that the caller has permission to send signals
596       to.  Despite this error return, the file descriptor owner is  set,  and
597       signals will be sent to the owner.
598
599       The  implementation of mandatory locking in all known versions of Linux
600       is subject to race conditions which render it  unreliable:  a  write(2)
601       call that overlaps with a lock may modify data after the mandatory lock
602       is acquired; a read(2) call  that  overlaps  with  a  lock  may  detect
603       changes  to  data  that were made only after a write lock was acquired.
604       Similar races exist between mandatory locks and mmap(2).  It is  there‐
605       fore inadvisable to rely on mandatory locking.
606

SEE ALSO

608       dup2(2),  flock(2), open(2), socket(2), lockf(3), capabilities(7), fea‐
609       ture_test_macros(7)
610
611       See also locks.txt, mandatory-locking.txt, and dnotify.txt in the  ker‐
612       nel  source  directory  Documentation/filesystems/.  (On older kernels,
613       these files are directly under the Documentation/ directory, and manda‐
614       tory-locking.txt is called mandatory.txt.)
615

COLOPHON

617       This  page  is  part of release 3.25 of the Linux man-pages project.  A
618       description of the project, and information about reporting  bugs,  can
619       be found at http://www.kernel.org/doc/man-pages/.
620
621
622
623Linux                             2010-06-19                          FCNTL(2)
Impressum