1 .\" Copyright (c) 1983, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94
41 .Fn fcntl "int fd" "int cmd" "..."
45 system call provides for control over descriptors.
48 is a descriptor to be operated on by
51 Depending on the value of
54 can take an additional third argument
56 Unless otherwise noted below for a specific operation,
60 .Bl -tag -width F_DUP2FD_CLOEXEC
62 Return a new descriptor as follows:
64 .Bl -bullet -compact -offset 4n
66 Lowest numbered available descriptor greater than or equal to
69 Same object references as the original descriptor.
71 New descriptor shares the same file offset if the object
74 Same access mode (read, write or read/write).
76 Same file status flags (i.e., both file descriptors
77 share the same file status flags).
79 The close-on-exec flag
81 associated with the new file descriptor is cleared, so the file descriptor is
86 .It Dv F_DUPFD_CLOEXEC
91 flag associated with the new file descriptor is set, so the file descriptor
96 It is functionally equivalent to
97 .Bd -literal -offset indent
100 .It Dv F_DUP2FD_CLOEXEC
105 flag associated with the new file descriptor is set.
111 constants are not portable, so they should not be used if
112 portability is needed.
118 Get the close-on-exec flag associated with the file descriptor
122 If the returned value ANDed with
125 the file will remain open across
127 otherwise the file will be closed upon execution of
132 Set the close-on-exec flag associated with
142 Get descriptor status flags, as described below
146 Set descriptor status flags to
149 Get the process ID or process group
154 signals; process groups are returned
159 Set the process or process group
165 process groups are specified by supplying
167 as negative, otherwise
169 is interpreted as a process ID.
171 Set or clear the read ahead amount for sequential access to the third
174 which is rounded up to the nearest block size.
177 turns off read ahead, a negative value restores the system default.
179 Equivalent to Darwin counterpart which sets read ahead amount of 128KB
180 when the third argument,
185 turns off read ahead.
187 Add seals to the file as described below, if the underlying filesystem supports
190 Get seals associated with the file, if the underlying filesystem supports seals.
191 .It Dv F_ISUNIONSTACK
192 Check if the vnode is part of a union stack (either the "union" flag from
195 This is a hack not intended to be used outside of libc.
198 .Vt struct kinfo_file
199 for the file referenced by the specified file descriptor.
202 argument should point to the storage for
203 .Vt struct kinfo_file .
206 member of the passed structure must be initialized with the sizeof of
207 .Vt struct kinfo_file ,
208 to allow for the interface versioning and evolution.
215 commands are as follows:
216 .Bl -tag -width O_NONBLOCKX
218 Non-blocking I/O; if no data is available to a
222 operation would block,
223 the read or write call returns -1 with the error
226 Force each write to append at the end of file;
232 Minimize or eliminate the cache effects of reading and writing.
234 will attempt to avoid caching the data you read or write.
236 avoid caching the data, it will minimize the impact the data has on the cache.
237 Use of this flag can drastically reduce performance if not used with care.
241 signal to be sent to the process group
242 when I/O is possible, e.g.,
243 upon availability of data to be read.
245 Enable synchronous writes.
251 is an historical synonym for
254 Enable synchronous data writes.
261 The seals that may be applied with
264 .Bl -tag -width F_SEAL_SHRINK
266 Prevent any further seals from being applied to the file.
268 Prevent the file from being shrunk with
271 Prevent the file from being enlarged with
277 Any writes in progress will finish before
280 If any writeable mappings exist, F_ADD_SEALS will fail and return
284 Seals are on a per-inode basis and require support by the underlying filesystem.
285 If the underlying filesystem does not support seals,
292 Several operations are available for doing advisory file locking;
293 they all operate on the following structure:
296 off_t l_start; /* starting offset */
297 off_t l_len; /* len = 0 means until end of file */
298 pid_t l_pid; /* lock owner */
299 short l_type; /* lock type: read/write, etc. */
300 short l_whence; /* type of l_start */
301 int l_sysid; /* remote system id or zero for local */
304 These advisory file locking operations take a pointer to
306 as the third argument
308 The commands available for advisory record locking are as follows:
309 .Bl -tag -width F_SETLKWX
311 Get the first lock that blocks the lock description pointed to by the
314 taken as a pointer to a
317 The information retrieved overwrites the information passed to
322 If no lock is found that would prevent this lock from being created,
323 the structure is left unchanged by this system call except for the
324 lock type which is set to
327 Set or clear a file segment lock according to the lock description
328 pointed to by the third argument,
330 taken as a pointer to a
334 is used to establish shared (or read) locks
336 or exclusive (or write) locks,
338 as well as remove either type of lock
340 If a shared or exclusive lock cannot be set,
342 returns immediately with
345 This command is the same as
347 except that if a shared or exclusive lock is blocked by other locks,
348 the process waits until the request can be satisfied.
349 If a signal that is to be caught is received while
351 is waiting for a region, the
353 will be interrupted if the signal handler has not specified the
359 When a shared lock has been set on a segment of a file,
360 other processes can set shared locks on that segment
362 A shared lock prevents any other process from setting an exclusive
363 lock on any portion of the protected area.
364 A request for a shared lock fails if the file descriptor was not
365 opened with read access.
367 An exclusive lock prevents any other process from setting a shared lock or
368 an exclusive lock on any portion of the protected area.
369 A request for an exclusive lock fails if the file was not
370 opened with write access.
379 to indicate that the relative offset,
381 bytes, will be measured from the start of the file,
382 current position, or end of the file, respectively.
385 is the number of consecutive bytes to be locked.
390 means end edge of the region.
395 fields are only used with
397 to return the process ID of the process holding a blocking lock and
398 the system ID of the system that owns that process.
399 Locks created by the local system will have a system ID of zero.
402 request, the value of
407 Locks may start and extend beyond the current end of a file,
408 but may not start or extend before the beginning of the file.
409 A lock is set to extend to the largest possible value of the
410 file offset for that file if
417 point to the beginning of the file, and
419 is zero, the entire file is locked.
420 If an application wishes only to do entire file locking, the
422 system call is much more efficient.
424 There is at most one type of lock set for each byte in the file.
425 Before a successful return from an
429 request when the calling process has previously existing locks
430 on bytes in the region specified by the request,
431 the previous lock type for each byte in the specified
432 region is replaced by the new lock type.
433 As specified above under the descriptions
434 of shared locks and exclusive locks, an
438 request fails or blocks respectively when another process has existing
439 locks on bytes in the specified region and the type of any of those
440 locks conflicts with the type specified in the request.
444 requests on local files is fair;
445 that is, while the thread is blocked,
446 subsequent requests conflicting with its requests will not be granted,
447 even if these requests do not conflict with existing locks.
449 This interface follows the completely stupid semantics of System V and
451 that require that all locks associated with a file for a given process are
454 file descriptor for that file is closed by that process.
455 This semantic means that applications must be aware of any files that
456 a subroutine library may access.
457 For example if an application for updating the password file locks the
458 password file database while making the update, and then calls
460 to retrieve a record,
461 the lock will be lost because
463 opens, reads, and closes the password database.
464 The database close will release all locks that the process has
465 associated with the database, even if the library routine never
466 requested a lock on the database.
467 Another minor semantic problem with this interface is that
468 locks are not inherited by a child process created using the
473 interface has much more rational last close semantics and
474 allows locks to be inherited by child processes.
477 system call is recommended for applications that want to ensure the integrity
478 of their locks when using library routines or wish to pass locks
486 locks are compatible.
487 Processes using different locking interfaces can cooperate
488 over the same file safely.
489 However, only one of such interfaces should be used within
491 If a file is locked by a process through
493 any record within the file will be seen as locked
494 from the viewpoint of another process using
503 if the process holding a blocking lock previously locked the
507 All locks associated with a file for a given process are
508 removed when the process terminates.
510 All locks obtained before a call to
512 remain in effect until the new program releases them.
513 If the new program does not know about the locks, they will not be
514 released until the program exits.
516 A potential for deadlock occurs if a process controlling a locked region
517 is put to sleep by attempting to lock the locked region of another process.
518 This implementation detects that sleeping until a locked region is unlocked
519 would cause a deadlock and fails with an
523 Upon successful completion, the value returned depends on
526 .Bl -tag -width F_GETOWNX -offset indent
528 A new file descriptor.
530 A file descriptor equal to
533 Value of flag (only the low-order bit is defined).
537 Value of file descriptor owner.
542 Otherwise, a value of -1 is returned and
544 is set to indicate the error.
548 system call will fail if:
561 and the segment of a file to be locked is already
562 exclusive-locked by another process;
563 or the type is an exclusive lock and some portion of the
564 segment of a file to be locked is already shared-locked or
565 exclusive-locked by another process.
570 is not a valid open file descriptor.
578 is not a valid file descriptor.
592 is not a valid file descriptor open for reading.
606 is not a valid file descriptor open for writing.
614 and writeable mappings of the file exist.
620 and a deadlock condition was detected.
626 and the system call was interrupted by a signal.
635 is negative or greater than the maximum allowable number
637 .Xr getdtablesize 2 ) .
646 and the data to which
656 and the underlying filesystem does not support sealing.
666 and the maximum number of file descriptors permitted for the
667 process are already in use,
668 or no file descriptors greater than or equal to
674 argument is not a valid file descriptor for the requested operation.
675 This may be the case if
677 is a device node, or a descriptor returned by
686 and satisfying the lock or unlock request would result in the
687 number of locked regions in the system exceeding a system-imposed limit.
698 refers to a file for which locking is not supported.
709 calculation overflowed.
717 the process ID or process group given as an argument is in a
718 different session than the caller.
727 seal has already been set.
735 the process ID given as argument is not in use.
740 refers to a descriptor open on a terminal device (as opposed to a
741 descriptor open on a socket), a
745 can fail for the same reasons as in
751 for the reasons as stated in
758 .Xr getdtablesize 2 ,
767 constant is non portable.
768 It is provided for compatibility with AIX and Solaris.
776 after any caught signal
777 and should continue waiting during thread suspension such as a stop signal.
778 However, in this implementation a call with
780 is restarted after catching a signal with a
782 handler or a thread suspension such as a stop signal.
786 system call appeared in
791 constant first appeared in