1FCNTL(2) Linux Programmer's Manual FCNTL(2)
2
3
4
6 fcntl - manipulate file descriptor
7
9 #include <unistd.h>
10 #include <fcntl.h>
11
12 int fcntl(int fd, int cmd, ... /* arg */ );
13
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 int, and we identify the argument using the name arg),
22 or void is specified if the argument is not required.
23
24 Duplicating a file descriptor
25 F_DUPFD (int)
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 (int; 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 (int)
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 Get the file access mode and the file status flags; arg is
64 ignored.
65
66 F_SETFL (int)
67 Set the file status flags to the value specified by arg. File
68 access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags
69 (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored.
70 On Linux this command can change only the O_APPEND, O_ASYNC,
71 O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
72
73 Advisory locking
74 F_GETLK, F_SETLK and F_SETLKW are used to acquire, release, and test
75 for the existence of record locks (also known as file-segment or file-
76 region locks). The third argument, lock, is a pointer to a structure
77 that has at least the following fields (in unspecified order).
78
79 struct flock {
80 ...
81 short l_type; /* Type of lock: F_RDLCK,
82 F_WRLCK, F_UNLCK */
83 short l_whence; /* How to interpret l_start:
84 SEEK_SET, SEEK_CUR, SEEK_END */
85 off_t l_start; /* Starting offset for lock */
86 off_t l_len; /* Number of bytes to lock */
87 pid_t l_pid; /* PID of process blocking our lock
88 (F_GETLK only) */
89 ...
90 };
91
92 The l_whence, l_start, and l_len fields of this structure specify the
93 range of bytes we wish to lock. Bytes past the end of the file may be
94 locked, but not bytes before the start of the file.
95
96 l_start is the starting offset for the lock, and is interpreted rela‐
97 tive to either: the start of the file (if l_whence is SEEK_SET); the
98 current file offset (if l_whence is SEEK_CUR); or the end of the file
99 (if l_whence is SEEK_END). In the final two cases, l_start can be a
100 negative number provided the offset does not lie before the start of
101 the file.
102
103 l_len specifies the number of bytes to be locked. If l_len is posi‐
104 tive, then the range to be locked covers bytes l_start up to and
105 including l_start+l_len-1. Specifying 0 for l_len has the special
106 meaning: lock all bytes starting at the location specified by l_whence
107 and l_start through to the end of file, no matter how large the file
108 grows.
109
110 POSIX.1-2001 allows (but does not require) an implementation to support
111 a negative l_len value; if l_len is negative, the interval described by
112 lock covers bytes l_start+l_len up to and including l_start-1. This is
113 supported by Linux since kernel versions 2.4.21 and 2.5.49.
114
115 The l_type field can be used to place a read (F_RDLCK) or a write
116 (F_WRLCK) lock on a file. Any number of processes may hold a read lock
117 (shared lock) on a file region, but only one process may hold a write
118 lock (exclusive lock). An exclusive lock excludes all other locks,
119 both shared and exclusive. A single process can hold only one type of
120 lock on a file region; if a new lock is applied to an already-locked
121 region, then the existing lock is converted to the new lock type.
122 (Such conversions may involve splitting, shrinking, or coalescing with
123 an existing lock if the byte range specified by the new lock does not
124 precisely coincide with the range of the existing lock.)
125
126 F_SETLK (struct flock *)
127 Acquire a lock (when l_type is F_RDLCK or F_WRLCK) or release a
128 lock (when l_type is F_UNLCK) on the bytes specified by the
129 l_whence, l_start, and l_len fields of lock. If a conflicting
130 lock is held by another process, this call returns -1 and sets
131 errno to EACCES or EAGAIN.
132
133 F_SETLKW (struct flock *)
134 As for F_SETLK, but if a conflicting lock is held on the file,
135 then wait for that lock to be released. If a signal is caught
136 while waiting, then the call is interrupted and (after the sig‐
137 nal handler has returned) returns immediately (with return value
138 -1 and errno set to EINTR; see signal(7)).
139
140 F_GETLK (struct flock *)
141 On input to this call, lock describes a lock we would like to
142 place on the file. If the lock could be placed, fcntl() does
143 not actually place it, but returns F_UNLCK in the l_type field
144 of lock and leaves the other fields of the structure unchanged.
145 If one or more incompatible locks would prevent this lock being
146 placed, then fcntl() returns details about one of these locks in
147 the l_type, l_whence, l_start, and l_len fields of lock and sets
148 l_pid to be the PID of the process holding that lock.
149
150 In order to place a read lock, fd must be open for reading. In order
151 to place a write lock, fd must be open for writing. To place both
152 types of lock, open a file read-write.
153
154 As well as being removed by an explicit F_UNLCK, record locks are auto‐
155 matically released when the process terminates or if it closes any file
156 descriptor referring to a file on which locks are held. This is bad:
157 it means that a process can lose the locks on a file like /etc/passwd
158 or /etc/mtab when for some reason a library function decides to open,
159 read and close it.
160
161 Record locks are not inherited by a child created via fork(2), but are
162 preserved across an execve(2).
163
164 Because of the buffering performed by the stdio(3) library, the use of
165 record locking with routines in that package should be avoided; use
166 read(2) and write(2) instead.
167
168 Mandatory locking
169 (Non-POSIX.) The above record locks may be either advisory or manda‐
170 tory, and are advisory by default.
171
172 Advisory locks are not enforced and are useful only between cooperating
173 processes.
174
175 Mandatory locks are enforced for all processes. If a process tries to
176 perform an incompatible access (e.g., read(2) or write(2)) on a file
177 region that has an incompatible mandatory lock, then the result depends
178 upon whether the O_NONBLOCK flag is enabled for its open file descrip‐
179 tion. If the O_NONBLOCK flag is not enabled, then system call is
180 blocked until the lock is removed or converted to a mode that is com‐
181 patible with the access. If the O_NONBLOCK flag is enabled, then the
182 system call fails with the error EAGAIN.
183
184 To make use of mandatory locks, mandatory locking must be enabled both
185 on the file system that contains the file to be locked, and on the file
186 itself. Mandatory locking is enabled on a file system using the "-o
187 mand" option to mount(8), or the MS_MANDLOCK flag for mount(2). Manda‐
188 tory locking is enabled on a file by disabling group execute permission
189 on the file and enabling the set-group-ID permission bit (see chmod(1)
190 and chmod(2)).
191
192 The Linux implementation of mandatory locking is unreliable. See BUGS
193 below.
194
195 Managing signals
196 F_GETOWN, F_SETOWN, F_GETOWN_EX, F_SETOWN_EX, F_GETSIG and F_SETSIG are
197 used to manage I/O availability signals:
198
199 F_GETOWN (void)
200 Return (as the function result) the process ID or process group
201 currently receiving SIGIO and SIGURG signals for events on file
202 descriptor fd. Process IDs are returned as positive values;
203 process group IDs are returned as negative values (but see BUGS
204 below). arg is ignored.
205
206 F_SETOWN (int)
207 Set the process ID or process group ID that will receive SIGIO
208 and SIGURG signals for events on file descriptor fd to the ID
209 given in arg. A process ID is specified as a positive value; a
210 process group ID is specified as a negative value. Most com‐
211 monly, the calling process specifies itself as the owner (that
212 is, arg is specified as getpid(2)).
213
214 If you set the O_ASYNC status flag on a file descriptor by using
215 the F_SETFL command of fcntl(), a SIGIO signal is sent whenever
216 input or output becomes possible on that file descriptor.
217 F_SETSIG can be used to obtain delivery of a signal other than
218 SIGIO. If this permission check fails, then the signal is
219 silently discarded.
220
221 Sending a signal to the owner process (group) specified by
222 F_SETOWN is subject to the same permissions checks as are
223 described for kill(2), where the sending process is the one that
224 employs F_SETOWN (but see BUGS below).
225
226 If the file descriptor fd refers to a socket, F_SETOWN also
227 selects the recipient of SIGURG signals that are delivered when
228 out-of-band data arrives on that socket. (SIGURG is sent in any
229 situation where select(2) would report the socket as having an
230 "exceptional condition".)
231
232 The following was true in 2.6.x kernels up to and including ker‐
233 nel 2.6.11:
234
235 If a nonzero value is given to F_SETSIG in a multi‐
236 threaded process running with a threading library that
237 supports thread groups (e.g., NPTL), then a positive
238 value given to F_SETOWN has a different meaning: instead
239 of being a process ID identifying a whole process, it is
240 a thread ID identifying a specific thread within a
241 process. Consequently, it may be necessary to pass
242 F_SETOWN the result of gettid(2) instead of getpid(2) to
243 get sensible results when F_SETSIG is used. (In current
244 Linux threading implementations, a main thread's thread
245 ID is the same as its process ID. This means that a sin‐
246 gle-threaded program can equally use gettid(2) or get‐
247 pid(2) in this scenario.) Note, however, that the state‐
248 ments in this paragraph do not apply to the SIGURG signal
249 generated for out-of-band data on a socket: this signal
250 is always sent to either a process or a process group,
251 depending on the value given to F_SETOWN.
252
253 The above behavior was accidentally dropped in Linux 2.6.12, and
254 won't be restored. From Linux 2.6.32 onward, use F_SETOWN_EX to
255 target SIGIO and SIGURG signals at a particular thread.
256
257 F_GETOWN_EX (struct f_owner_ex *) (since Linux 2.6.32)
258 Return the current file descriptor owner settings as defined by
259 a previous F_SETOWN_EX operation. The information is returned
260 in the structure pointed to by arg, which has the following
261 form:
262
263 struct f_owner_ex {
264 int type;
265 pid_t pid;
266 };
267
268 The type field will have one of the values F_OWNER_TID,
269 F_OWNER_PID, or F_OWNER_PGRP. The pid field is a positive inte‐
270 ger representing a thread ID, process ID, or process group ID.
271 See F_SETOWN_EX for more details.
272
273 F_SETOWN_EX (struct f_owner_ex *) (since Linux 2.6.32)
274 This operation performs a similar task to F_SETOWN. It allows
275 the caller to direct I/O availability signals to a specific
276 thread, process, or process group. The caller specifies the
277 target of signals via arg, which is a pointer to a f_owner_ex
278 structure. The type field has one of the following values,
279 which define how pid is interpreted:
280
281 F_OWNER_TID
282 Send the signal to the thread whose thread ID (the value
283 returned by a call to clone(2) or gettid(2)) is specified
284 in pid.
285
286 F_OWNER_PID
287 Send the signal to the process whose ID is specified in
288 pid.
289
290 F_OWNER_PGRP
291 Send the signal to the process group whose ID is speci‐
292 fied in pid. (Note that, unlike with F_SETOWN, a process
293 group ID is specified as a positive value here.)
294
295 F_GETSIG (void)
296 Return (as the function result) the signal sent when input or
297 output becomes possible. A value of zero means SIGIO is sent.
298 Any other value (including SIGIO) is the signal sent instead,
299 and in this case additional info is available to the signal han‐
300 dler if installed with SA_SIGINFO. arg is ignored.
301
302 F_SETSIG (int)
303 Set the signal sent when input or output becomes possible to the
304 value given in arg. A value of zero means to send the default
305 SIGIO signal. Any other value (including SIGIO) is the signal
306 to send instead, and in this case additional info is available
307 to the signal handler if installed with SA_SIGINFO.
308
309 By using F_SETSIG with a nonzero value, and setting SA_SIGINFO
310 for the signal handler (see sigaction(2)), extra information
311 about I/O events is passed to the handler in a siginfo_t struc‐
312 ture. If the si_code field indicates the source is SI_SIGIO,
313 the si_fd field gives the file descriptor associated with the
314 event. Otherwise, there is no indication which file descriptors
315 are pending, and you should use the usual mechanisms (select(2),
316 poll(2), read(2) with O_NONBLOCK set etc.) to determine which
317 file descriptors are available for I/O.
318
319 By selecting a real time signal (value >= SIGRTMIN), multiple
320 I/O events may be queued using the same signal numbers. (Queu‐
321 ing is dependent on available memory). Extra information is
322 available if SA_SIGINFO is set for the signal handler, as above.
323
324 Note that Linux imposes a limit on the number of real-time sig‐
325 nals that may be queued to a process (see getrlimit(2) and sig‐
326 nal(7)) and if this limit is reached, then the kernel reverts to
327 delivering SIGIO, and this signal is delivered to the entire
328 process rather than to a specific thread.
329
330 Using these mechanisms, a program can implement fully asynchronous I/O
331 without using select(2) or poll(2) most of the time.
332
333 The use of O_ASYNC, F_GETOWN, F_SETOWN is specific to BSD and Linux.
334 F_GETOWN_EX, F_SETOWN_EX, F_GETSIG, and F_SETSIG are Linux-specific.
335 POSIX has asynchronous I/O and the aio_sigevent structure to achieve
336 similar things; these are also available in Linux as part of the GNU C
337 Library (Glibc).
338
339 Leases
340 F_SETLEASE and F_GETLEASE (Linux 2.4 onward) are used (respectively) to
341 establish a new lease, and retrieve the current lease, on the open file
342 description referred to by the file descriptor fd. A file lease pro‐
343 vides a mechanism whereby the process holding the lease (the "lease
344 holder") is notified (via delivery of a signal) when a process (the
345 "lease breaker") tries to open(2) or truncate(2) the file referred to
346 by that file descriptor.
347
348 F_SETLEASE (int)
349 Set or remove a file lease according to which of the following
350 values is specified in the integer arg:
351
352 F_RDLCK
353 Take out a read lease. This will cause the calling
354 process to be notified when the file is opened for writ‐
355 ing or is truncated. A read lease can be placed only on
356 a file descriptor that is opened read-only.
357
358 F_WRLCK
359 Take out a write lease. This will cause the caller to be
360 notified when the file is opened for reading or writing
361 or is truncated. A write lease may be placed on a file
362 only if there are no other open file descriptors for the
363 file.
364
365 F_UNLCK
366 Remove our lease from the file.
367
368 Leases are associated with an open file description (see open(2)).
369 This means that duplicate file descriptors (created by, for example,
370 fork(2) or dup(2)) refer to the same lease, and this lease may be modi‐
371 fied or released using any of these descriptors. Furthermore, the
372 lease is released by either an explicit F_UNLCK operation on any of
373 these duplicate descriptors, or when all such descriptors have been
374 closed.
375
376 Leases may be taken out only on regular files. An unprivileged process
377 may take out a lease only on a file whose UID (owner) matches the file
378 system UID of the process. A process with the CAP_LEASE capability may
379 take out leases on arbitrary files.
380
381 F_GETLEASE (void)
382 Indicates what type of lease is associated with the file
383 descriptor fd by returning either F_RDLCK, F_WRLCK, or F_UNLCK,
384 indicating, respectively, a read lease , a write lease, or no
385 lease. arg is ignored.
386
387 When a process (the "lease breaker") performs an open(2) or truncate(2)
388 that conflicts with a lease established via F_SETLEASE, the system call
389 is blocked by the kernel and the kernel notifies the lease holder by
390 sending it a signal (SIGIO by default). The lease holder should
391 respond to receipt of this signal by doing whatever cleanup is required
392 in preparation for the file to be accessed by another process (e.g.,
393 flushing cached buffers) and then either remove or downgrade its lease.
394 A lease is removed by performing an F_SETLEASE command specifying arg
395 as F_UNLCK. If the lease holder currently holds a write lease on the
396 file, and the lease breaker is opening the file for reading, then it is
397 sufficient for the lease holder to downgrade the lease to a read lease.
398 This is done by performing an F_SETLEASE command specifying arg as
399 F_RDLCK.
400
401 If the lease holder fails to downgrade or remove the lease within the
402 number of seconds specified in /proc/sys/fs/lease-break-time then the
403 kernel forcibly removes or downgrades the lease holder's lease.
404
405 Once a lease break has been initiated, F_GETLEASE returns the target
406 lease type (either F_RDLCK or F_UNLCK, depending on what would be com‐
407 patible with the lease breaker) until the lease holder voluntarily
408 downgrades or removes the lease or the kernel forcibly does so after
409 the lease break timer expires.
410
411 Once the lease has been voluntarily or forcibly removed or downgraded,
412 and assuming the lease breaker has not unblocked its system call, the
413 kernel permits the lease breaker's system call to proceed.
414
415 If the lease breaker's blocked open(2) or truncate(2) is interrupted by
416 a signal handler, then the system call fails with the error EINTR, but
417 the other steps still occur as described above. If the lease breaker
418 is killed by a signal while blocked in open(2) or truncate(2), then the
419 other steps still occur as described above. If the lease breaker spec‐
420 ifies the O_NONBLOCK flag when calling open(2), then the call immedi‐
421 ately fails with the error EWOULDBLOCK, but the other steps still occur
422 as described above.
423
424 The default signal used to notify the lease holder is SIGIO, but this
425 can be changed using the F_SETSIG command to fcntl(). If a F_SETSIG
426 command is performed (even one specifying SIGIO), and the signal han‐
427 dler is established using SA_SIGINFO, then the handler will receive a
428 siginfo_t structure as its second argument, and the si_fd field of this
429 argument will hold the descriptor of the leased file that has been
430 accessed by another process. (This is useful if the caller holds
431 leases against multiple files).
432
433 File and directory change notification (dnotify)
434 F_NOTIFY (int)
435 (Linux 2.4 onward) Provide notification when the directory
436 referred to by fd or any of the files that it contains is
437 changed. The events to be notified are specified in arg, which
438 is a bit mask specified by ORing together zero or more of the
439 following bits:
440
441 DN_ACCESS A file was accessed (read, pread, readv)
442 DN_MODIFY A file was modified (write, pwrite, writev, trun‐
443 cate, ftruncate).
444 DN_CREATE A file was created (open, creat, mknod, mkdir, link,
445 symlink, rename).
446 DN_DELETE A file was unlinked (unlink, rename to another
447 directory, rmdir).
448 DN_RENAME A file was renamed within this directory (rename).
449 DN_ATTRIB The attributes of a file were changed (chown, chmod,
450 utime[s]).
451
452 (In order to obtain these definitions, the _GNU_SOURCE feature
453 test macro must be defined before including any header files.)
454
455 Directory notifications are normally "one-shot", and the appli‐
456 cation must reregister to receive further notifications. Alter‐
457 natively, if DN_MULTISHOT is included in arg, then notification
458 will remain in effect until explicitly removed.
459
460 A series of F_NOTIFY requests is cumulative, with the events in
461 arg being added to the set already monitored. To disable noti‐
462 fication of all events, make an F_NOTIFY call specifying arg as
463 0.
464
465 Notification occurs via delivery of a signal. The default sig‐
466 nal is SIGIO, but this can be changed using the F_SETSIG command
467 to fcntl(). In the latter case, the signal handler receives a
468 siginfo_t structure as its second argument (if the handler was
469 established using SA_SIGINFO) and the si_fd field of this struc‐
470 ture contains the file descriptor which generated the notifica‐
471 tion (useful when establishing notification on multiple directo‐
472 ries).
473
474 Especially when using DN_MULTISHOT, a real time signal should be
475 used for notification, so that multiple notifications can be
476 queued.
477
478 NOTE: New applications should use the inotify interface (avail‐
479 able since kernel 2.6.13), which provides a much superior inter‐
480 face for obtaining notifications of file system events. See
481 inotify(7).
482
483 Changing the capacity of a pipe
484 F_SETPIPE_SZ (int; since Linux 2.6.35)
485 Change the capacity of the pipe referred to by fd to be at least
486 arg bytes. An unprivileged process can adjust the pipe capacity
487 to any value between the system page size and the limit defined
488 in /proc/sys/fs/pipe-max-size (see proc(5)). Attempts to set
489 the pipe capacity below the page size are silently rounded up to
490 the page size. Attempts by an unprivileged process to set the
491 pipe capacity above the limit in /proc/sys/fs/pipe-max-size
492 yield the error EPERM; a privileged process (CAP_SYS_RESOURCE)
493 can override the limit. When allocating the buffer for the
494 pipe, the kernel may use a capacity larger than arg, if that is
495 convenient for the implementation. The F_GETPIPE_SZ operation
496 returns the actual size used. Attempting to set the pipe capac‐
497 ity smaller than the amount of buffer space currently used to
498 store data produces the error EBUSY.
499
500 F_GETPIPE_SZ (void; since Linux 2.6.35)
501 Return (as the function result) the capacity of the pipe
502 referred to by fd.
503
505 For a successful call, the return value depends on the operation:
506
507 F_DUPFD The new descriptor.
508
509 F_GETFD Value of file descriptor flags.
510
511 F_GETFL Value of file status flags.
512
513 F_GETLEASE
514 Type of lease held on file descriptor.
515
516 F_GETOWN Value of descriptor owner.
517
518 F_GETSIG Value of signal sent when read or write becomes possible, or
519 zero for traditional SIGIO behavior.
520
521 F_GETPIPE_SZ
522 The pipe capacity.
523
524 All other commands
525 Zero.
526
527 On error, -1 is returned, and errno is set appropriately.
528
530 EACCES or EAGAIN
531 Operation is prohibited by locks held by other processes.
532
533 EAGAIN The operation is prohibited because the file has been memory-
534 mapped by another process.
535
536 EBADF fd is not an open file descriptor, or the command was F_SETLK or
537 F_SETLKW and the file descriptor open mode doesn't match with
538 the type of lock requested.
539
540 EDEADLK
541 It was detected that the specified F_SETLKW command would cause
542 a deadlock.
543
544 EFAULT lock is outside your accessible address space.
545
546 EINTR For F_SETLKW, the command was interrupted by a signal; see sig‐
547 nal(7). For F_GETLK and F_SETLK, the command was interrupted by
548 a signal before the lock was checked or acquired. Most likely
549 when locking a remote file (e.g., locking over NFS), but can
550 sometimes happen locally.
551
552 EINVAL For F_DUPFD, arg is negative or is greater than the maximum
553 allowable value. For F_SETSIG, arg is not an allowable signal
554 number.
555
556 EMFILE For F_DUPFD, the process already has the maximum number of file
557 descriptors open.
558
559 ENOLCK Too many segment locks open, lock table is full, or a remote
560 locking protocol failed (e.g., locking over NFS).
561
562 EPERM Attempted to clear the O_APPEND flag on a file that has the
563 append-only attribute set.
564
566 SVr4, 4.3BSD, POSIX.1-2001. Only the operations F_DUPFD, F_GETFD,
567 F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK and F_SETLKW, are specified
568 in POSIX.1-2001.
569
570 F_GETOWN and F_SETOWN are specified in POSIX.1-2001. (To get their
571 definitions, define BSD_SOURCE, or _XOPEN_SOURCE with the value 500 or
572 greater, or define _POSIX_C_SOURCE with the value 200809L or greater.)
573
574 F_DUPFD_CLOEXEC is specified in POSIX.1-2008. (To get this definition,
575 define _POSIX_C_SOURCE with the value 200809L or greater, or
576 _XOPEN_SOURCE with the value 700 or greater.)
577
578 F_GETOWN_EX, F_SETOWN_EX, F_SETPIPE_SZ, F_GETPIPE_SZ, F_GETSIG, F_SET‐
579 SIG, F_NOTIFY, F_GETLEASE, and F_SETLEASE are Linux-specific. (Define
580 the _GNU_SOURCE macro to obtain these definitions.)
581
583 The original Linux fcntl() system call was not designed to handle large
584 file offsets (in the flock structure). Consequently, an fcntl64() sys‐
585 tem call was added in Linux 2.4. The newer system call employs a dif‐
586 ferent structure for file locking, flock64, and corresponding commands,
587 F_GETLK64, F_SETLK64, and F_SETLKW64. However, these details can be
588 ignored by applications using glibc, whose fcntl() wrapper function
589 transparently employs the more recent system call where it is avail‐
590 able.
591
592 The errors returned by dup2(2) are different from those returned by
593 F_DUPFD.
594
595 Since kernel 2.0, there is no interaction between the types of lock
596 placed by flock(2) and fcntl().
597
598 Several systems have more fields in struct flock such as, for example,
599 l_sysid. Clearly, l_pid alone is not going to be very useful if the
600 process holding the lock may live on a different machine.
601
603 A limitation of the Linux system call conventions on some architectures
604 (notably i386) means that if a (negative) process group ID to be
605 returned by F_GETOWN falls in the range -1 to -4095, then the return
606 value is wrongly interpreted by glibc as an error in the system call;
607 that is, the return value of fcntl() will be -1, and errno will contain
608 the (positive) process group ID. The Linux-specific F_GETOWN_EX opera‐
609 tion avoids this problem. Since glibc version 2.11, glibc makes the
610 kernel F_GETOWN problem invisible by implementing F_GETOWN using
611 F_GETOWN_EX.
612
613 In Linux 2.4 and earlier, there is bug that can occur when an unprivi‐
614 leged process uses F_SETOWN to specify the owner of a socket file
615 descriptor as a process (group) other than the caller. In this case,
616 fcntl() can return -1 with errno set to EPERM, even when the owner
617 process (group) is one that the caller has permission to send signals
618 to. Despite this error return, the file descriptor owner is set, and
619 signals will be sent to the owner.
620
621 The implementation of mandatory locking in all known versions of Linux
622 is subject to race conditions which render it unreliable: a write(2)
623 call that overlaps with a lock may modify data after the mandatory lock
624 is acquired; a read(2) call that overlaps with a lock may detect
625 changes to data that were made only after a write lock was acquired.
626 Similar races exist between mandatory locks and mmap(2). It is there‐
627 fore inadvisable to rely on mandatory locking.
628
630 dup2(2), flock(2), open(2), socket(2), lockf(3), capabilities(7), fea‐
631 ture_test_macros(7)
632
633 locks.txt, mandatory-locking.txt, and dnotify.txt in the Linux kernel
634 source directory Documentation/filesystems/ (on older kernels, these
635 files are directly under the Documentation/ directory, and mandatory-
636 locking.txt is called mandatory.txt)
637
639 This page is part of release 3.53 of the Linux man-pages project. A
640 description of the project, and information about reporting bugs, can
641 be found at http://www.kernel.org/doc/man-pages/.
642
643
644
645Linux 2012-04-15 FCNTL(2)