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
39 .Fn fcntl "int fd" "int cmd" "..."
43 system call provides for control over descriptors.
46 is a descriptor to be operated on by
49 Depending on the value of
52 can take an additional third argument
54 Unless otherwise noted below for a specific operation,
58 .Bl -tag -width F_DUP2FD_CLOEXEC
60 Return a new descriptor as follows:
62 .Bl -bullet -compact -offset 4n
64 Lowest numbered available descriptor greater than or equal to
67 Same object references as the original descriptor.
69 New descriptor shares the same file offset if the object
72 Same access mode (read, write or read/write).
74 Same file status flags (i.e., both file descriptors
75 share the same file status flags).
77 The close-on-exec flag
79 associated with the new file descriptor is cleared, so the file descriptor is
84 .It Dv F_DUPFD_CLOEXEC
89 flag associated with the new file descriptor is set, so the file descriptor
94 It is functionally equivalent to
95 .Bd -literal -offset indent
98 .It Dv F_DUP2FD_CLOEXEC
103 flag associated with the new file descriptor is set.
109 constants are not portable, so they should not be used if
110 portability is needed.
116 Get the close-on-exec flag associated with the file descriptor
120 If the returned value ANDed with
123 the file will remain open across
125 otherwise the file will be closed upon execution of
130 Set the close-on-exec flag associated with
140 Get descriptor status flags, as described below
144 Set descriptor status flags to
147 Get the process ID or process group
152 signals; process groups are returned
157 Set the process or process group
163 process groups are specified by supplying
165 as negative, otherwise
167 is interpreted as a process ID.
169 Set or clear the read ahead amount for sequential access to the third
172 which is rounded up to the nearest block size.
175 turns off read ahead, a negative value restores the system default.
177 Equivalent to Darwin counterpart which sets read ahead amount of 128KB
178 when the third argument,
183 turns off read ahead.
185 Add seals to the file as described below, if the underlying filesystem supports
188 Get seals associated with the file, if the underlying filesystem supports seals.
189 .It Dv F_ISUNIONSTACK
190 Check if the vnode is part of a union stack (either the "union" flag from
193 This is a hack not intended to be used outside of libc.
196 .Vt struct kinfo_file
197 for the file referenced by the specified file descriptor.
200 argument should point to the storage for
201 .Vt struct kinfo_file .
204 member of the passed structure must be initialized with the sizeof of
205 .Vt struct kinfo_file ,
206 to allow for the interface versioning and evolution.
213 commands are as follows:
214 .Bl -tag -width O_NONBLOCKX
216 Non-blocking I/O; if no data is available to a
220 operation would block,
221 the read or write call returns -1 with the error
224 Force each write to append at the end of file;
230 Minimize or eliminate the cache effects of reading and writing.
232 will attempt to avoid caching the data you read or write.
234 avoid caching the data, it will minimize the impact the data has on the cache.
235 Use of this flag can drastically reduce performance if not used with care.
239 signal to be sent to the process group
240 when I/O is possible, e.g.,
241 upon availability of data to be read.
243 Enable synchronous writes.
249 is an historical synonym for
252 Enable synchronous data writes.
259 The seals that may be applied with
262 .Bl -tag -width F_SEAL_SHRINK
264 Prevent any further seals from being applied to the file.
266 Prevent the file from being shrunk with
269 Prevent the file from being enlarged with
275 Any writes in progress will finish before
278 If any writeable mappings exist, F_ADD_SEALS will fail and return
282 Seals are on a per-inode basis and require support by the underlying filesystem.
283 If the underlying filesystem does not support seals,
290 Several operations are available for doing advisory file locking;
291 they all operate on the following structure:
294 off_t l_start; /* starting offset */
295 off_t l_len; /* len = 0 means until end of file */
296 pid_t l_pid; /* lock owner */
297 short l_type; /* lock type: read/write, etc. */
298 short l_whence; /* type of l_start */
299 int l_sysid; /* remote system id or zero for local */
302 These advisory file locking operations take a pointer to
304 as the third argument
306 The commands available for advisory record locking are as follows:
307 .Bl -tag -width F_SETLKWX
309 Get the first lock that blocks the lock description pointed to by the
312 taken as a pointer to a
315 The information retrieved overwrites the information passed to
320 If no lock is found that would prevent this lock from being created,
321 the structure is left unchanged by this system call except for the
322 lock type which is set to
325 Set or clear a file segment lock according to the lock description
326 pointed to by the third argument,
328 taken as a pointer to a
332 is used to establish shared (or read) locks
334 or exclusive (or write) locks,
336 as well as remove either type of lock
338 If a shared or exclusive lock cannot be set,
340 returns immediately with
343 This command is the same as
345 except that if a shared or exclusive lock is blocked by other locks,
346 the process waits until the request can be satisfied.
347 If a signal that is to be caught is received while
349 is waiting for a region, the
351 will be interrupted if the signal handler has not specified the
357 When a shared lock has been set on a segment of a file,
358 other processes can set shared locks on that segment
360 A shared lock prevents any other process from setting an exclusive
361 lock on any portion of the protected area.
362 A request for a shared lock fails if the file descriptor was not
363 opened with read access.
365 An exclusive lock prevents any other process from setting a shared lock or
366 an exclusive lock on any portion of the protected area.
367 A request for an exclusive lock fails if the file was not
368 opened with write access.
377 to indicate that the relative offset,
379 bytes, will be measured from the start of the file,
380 current position, or end of the file, respectively.
383 is the number of consecutive bytes to be locked.
388 means end edge of the region.
393 fields are only used with
395 to return the process ID of the process holding a blocking lock and
396 the system ID of the system that owns that process.
397 Locks created by the local system will have a system ID of zero.
400 request, the value of
405 Locks may start and extend beyond the current end of a file,
406 but may not start or extend before the beginning of the file.
407 A lock is set to extend to the largest possible value of the
408 file offset for that file if
415 point to the beginning of the file, and
417 is zero, the entire file is locked.
418 If an application wishes only to do entire file locking, the
420 system call is much more efficient.
422 There is at most one type of lock set for each byte in the file.
423 Before a successful return from an
427 request when the calling process has previously existing locks
428 on bytes in the region specified by the request,
429 the previous lock type for each byte in the specified
430 region is replaced by the new lock type.
431 As specified above under the descriptions
432 of shared locks and exclusive locks, an
436 request fails or blocks respectively when another process has existing
437 locks on bytes in the specified region and the type of any of those
438 locks conflicts with the type specified in the request.
442 requests on local files is fair;
443 that is, while the thread is blocked,
444 subsequent requests conflicting with its requests will not be granted,
445 even if these requests do not conflict with existing locks.
447 This interface follows the completely stupid semantics of System V and
449 that require that all locks associated with a file for a given process are
452 file descriptor for that file is closed by that process.
453 This semantic means that applications must be aware of any files that
454 a subroutine library may access.
455 For example if an application for updating the password file locks the
456 password file database while making the update, and then calls
458 to retrieve a record,
459 the lock will be lost because
461 opens, reads, and closes the password database.
462 The database close will release all locks that the process has
463 associated with the database, even if the library routine never
464 requested a lock on the database.
465 Another minor semantic problem with this interface is that
466 locks are not inherited by a child process created using the
471 interface has much more rational last close semantics and
472 allows locks to be inherited by child processes.
475 system call is recommended for applications that want to ensure the integrity
476 of their locks when using library routines or wish to pass locks
484 locks are compatible.
485 Processes using different locking interfaces can cooperate
486 over the same file safely.
487 However, only one of such interfaces should be used within
489 If a file is locked by a process through
491 any record within the file will be seen as locked
492 from the viewpoint of another process using
501 if the process holding a blocking lock previously locked the
505 All locks associated with a file for a given process are
506 removed when the process terminates.
508 All locks obtained before a call to
510 remain in effect until the new program releases them.
511 If the new program does not know about the locks, they will not be
512 released until the program exits.
514 A potential for deadlock occurs if a process controlling a locked region
515 is put to sleep by attempting to lock the locked region of another process.
516 This implementation detects that sleeping until a locked region is unlocked
517 would cause a deadlock and fails with an
521 Upon successful completion, the value returned depends on
524 .Bl -tag -width F_GETOWNX -offset indent
526 A new file descriptor.
528 A file descriptor equal to
531 Value of flag (only the low-order bit is defined).
535 Value of file descriptor owner.
540 Otherwise, a value of -1 is returned and
542 is set to indicate the error.
546 system call will fail if:
559 and the segment of a file to be locked is already
560 exclusive-locked by another process;
561 or the type is an exclusive lock and some portion of the
562 segment of a file to be locked is already shared-locked or
563 exclusive-locked by another process.
568 is not a valid open file descriptor.
576 is not a valid file descriptor.
590 is not a valid file descriptor open for reading.
604 is not a valid file descriptor open for writing.
612 and writeable mappings of the file exist.
618 and a deadlock condition was detected.
624 and the system call was interrupted by a signal.
633 is negative or greater than the maximum allowable number
635 .Xr getdtablesize 2 ) .
644 and the data to which
654 and the underlying filesystem does not support sealing.
664 and the maximum number of file descriptors permitted for the
665 process are already in use,
666 or no file descriptors greater than or equal to
672 argument is not a valid file descriptor for the requested operation.
673 This may be the case if
675 is a device node, or a descriptor returned by
684 and satisfying the lock or unlock request would result in the
685 number of locked regions in the system exceeding a system-imposed limit.
696 refers to a file for which locking is not supported.
707 calculation overflowed.
715 the process ID or process group given as an argument is in a
716 different session than the caller.
725 seal has already been set.
733 the process ID given as argument is not in use.
738 refers to a descriptor open on a terminal device (as opposed to a
739 descriptor open on a socket), a
743 can fail for the same reasons as in
749 for the reasons as stated in
756 .Xr getdtablesize 2 ,
765 constant is non portable.
766 It is provided for compatibility with AIX and Solaris.
774 after any caught signal
775 and should continue waiting during thread suspension such as a stop signal.
776 However, in this implementation a call with
778 is restarted after catching a signal with a
780 handler or a thread suspension such as a stop signal.
784 system call appeared in
789 constant first appeared in