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
29 .\" $FreeBSD: src/lib/libc/sys/fcntl.2,v 1.16.2.13 2002/07/22 15:15:16 bde Exp $
42 .Fn fcntl "int fd" "int cmd" "..."
46 system call provides for control over descriptors.
49 is a descriptor to be operated on by
52 Depending on the value of
55 can take an additional third argument
57 .Bl -tag -width ".Dv F_DUP2FD_CLOEXEC"
59 Return a new descriptor as follows:
61 .Bl -bullet -compact -offset 4n
63 Lowest numbered available descriptor greater than or equal to
66 Same object references as the original descriptor.
68 New descriptor shares the same file offset if the object
71 Same access mode (read, write or read/write).
73 Same file status flags (i.e., both file descriptors
74 share the same file status flags).
76 The close-on-exec flag
78 associated with the new file descriptor is cleared, so the file descriptor is
83 .It Dv F_DUPFD_CLOEXEC
88 flag associated with the new file descriptor is set, so the file descriptor
93 It is functionally equivalent to
94 .Bd -literal -offset indent
97 .It Dv F_DUP2FD_CLOEXEC
102 flag associated with the new file descriptor is set.
108 constants are not portable, so they should not be used if
109 portability is needed.
115 Get the close-on-exec flag associated with the file descriptor
119 If the returned value ANDed with
122 the file will remain open across
124 otherwise the file will be closed upon execution of
129 Set the close-on-exec flag associated with
139 Get descriptor status flags, as described below
143 Set descriptor status flags to
146 Get the process ID or process group
151 signals; process groups are returned
156 Set the process or process group
162 process groups are specified by supplying
164 as negative, otherwise
166 is interpreted as a process ID.
168 Get the filesystem path associated with
172 bytes will be written to the buffer provided in
180 flags are as follows:
181 .Bl -tag -width O_NONBLOCKX
183 Non-blocking I/O; if no data is available to a
187 operation would block,
188 the read or write call returns -1 with the error
191 Force each write to append at the end of file;
197 Minimize or eliminate the cache effects of reading and writing.
199 will attempt to avoid caching the data you read or write.
201 avoid caching the data, it will minimize the impact the data has on the cache.
202 Use of this flag can drastically reduce performance if not used with care.
206 signal to be sent to the process group
207 when I/O is possible, e.g.,
208 upon availability of data to be read.
211 Several commands are available for doing advisory file locking;
212 they all operate on the following structure:
215 off_t l_start; /* starting offset */
216 off_t l_len; /* len = 0 means until end of file */
217 pid_t l_pid; /* lock owner */
218 short l_type; /* lock type: read/write, etc. */
219 short l_whence; /* type of l_start */
222 The commands available for advisory record locking are as follows:
223 .Bl -tag -width F_SETLKWX
225 Get the first lock that blocks the lock description pointed to by the
228 taken as a pointer to a
231 The information retrieved overwrites the information passed to
236 If no lock is found that would prevent this lock from being created,
237 the structure is left unchanged by this system call except for the
238 lock type which is set to
241 Set or clear a file segment lock according to the lock description
242 pointed to by the third argument,
244 taken as a pointer to a
248 is used to establish shared (or read) locks
250 or exclusive (or write) locks,
252 as well as remove either type of lock
254 If a shared or exclusive lock cannot be set,
256 returns immediately with
259 This command is the same as
261 except that if a shared or exclusive lock is blocked by other locks,
262 the process waits until the request can be satisfied.
263 If a signal that is to be caught is received while
265 is waiting for a region, the
267 will be interrupted if the signal handler has not specified the
273 When a shared lock has been set on a segment of a file,
274 other processes can set shared locks on that segment
276 A shared lock prevents any other process from setting an exclusive
277 lock on any portion of the protected area.
278 A request for a shared lock fails if the file descriptor was not
279 opened with read access.
281 An exclusive lock prevents any other process from setting a shared lock or
282 an exclusive lock on any portion of the protected area.
283 A request for an exclusive lock fails if the file was not
284 opened with write access.
293 to indicate that the relative offset,
295 bytes, will be measured from the start of the file,
296 current position, or end of the file, respectively.
299 is the number of consecutive bytes to be locked.
302 is negative, the result is undefined.
305 field is only used with
307 to return the process ID of the process holding a blocking lock.
310 request, the value of
315 Locks may start and extend beyond the current end of a file,
316 but may not start or extend before the beginning of the file.
317 A lock is set to extend to the largest possible value of the
318 file offset for that file if
325 point to the beginning of the file, and
327 is zero, the entire file is locked.
328 If an application wishes only to do entire file locking, the
330 system call is much more efficient.
332 There is at most one type of lock set for each byte in the file.
333 Before a successful return from an
337 request when the calling process has previously existing locks
338 on bytes in the region specified by the request,
339 the previous lock type for each byte in the specified
340 region is replaced by the new lock type.
341 As specified above under the descriptions
342 of shared locks and exclusive locks, an
346 request fails or blocks respectively when another process has existing
347 locks on bytes in the specified region and the type of any of those
348 locks conflicts with the type specified in the request.
350 This interface follows the completely stupid semantics of System V and
352 that require that all locks associated with a file for a given process are
355 file descriptor for that file is closed by that process.
356 This semantic means that applications must be aware of any files that
357 a subroutine library may access.
358 For example if an application for updating the password file locks the
359 password file database while making the update, and then calls
361 to retrieve a record,
362 the lock will be lost because
364 opens, reads, and closes the password database.
365 The database close will release all locks that the process has
366 associated with the database, even if the library routine never
367 requested a lock on the database.
368 Another minor semantic problem with this interface is that
369 locks are not inherited by a child process created using the
374 interface has much more rational last close semantics and
375 allows locks to be inherited by child processes.
378 system call is recommended for applications that want to ensure the integrity
379 of their locks when using library routines or wish to pass locks
387 locks are compatible.
388 Processes using different locking interfaces can cooperate
389 over the same file safely.
390 However, only one of such interfaces should be used within
392 If a file is locked by a process through
394 any record within the file will be seen as locked
395 from the viewpoint of another process using
404 if the process holding a blocking lock previously locked the
408 All locks associated with a file for a given process are
409 removed when the process terminates.
411 All locks obtained before a call to
413 remain in effect until the new program releases them.
414 If the new program does not know about the locks, they will not be
415 released until the program exits.
417 A potential for deadlock occurs if a process controlling a locked region
418 is put to sleep by attempting to lock the locked region of another process.
419 This implementation detects that sleeping until a locked region is unlocked
420 would cause a deadlock and fails with an
424 Upon successful completion, the value returned depends on
427 .Bl -tag -width F_GETOWNX -offset indent
429 A new file descriptor.
431 A file descriptor equal to
434 Value of flag (only the low-order bit is defined).
438 Value of file descriptor owner.
443 Otherwise, a value of -1 is returned and
445 is set to indicate the error.
449 system call will fail if:
462 and the segment of a file to be locked is already
463 exclusive-locked by another process;
464 or the type is an exclusive lock and some portion of the
465 segment of a file to be locked is already shared-locked or
466 exclusive-locked by another process.
471 is not a valid open file descriptor.
479 is not a valid file descriptor.
493 is not a valid file descriptor open for reading.
507 is not a valid file descriptor open for writing.
513 and a deadlock condition was detected.
519 and the system call was interrupted by a signal.
528 is negative or greater than the maximum allowable number
530 .Xr getdtablesize 2 ) .
539 and the data to which
547 and the maximum number of file descriptors permitted for the
548 process are already in use,
549 or no file descriptors greater than or equal to
559 and satisfying the lock or unlock request would result in the
560 number of locked regions in the system exceeding a system-imposed limit.
568 the process ID or process group given as an argument is in a
569 different session than the caller.
575 and the resulting path, inclusive of the terminator, would be greater than
584 the process ID given as argument is not in use.
589 refers to a descriptor open on a terminal device (as opposed to a
590 descriptor open on a socket), a
594 can fail for the same reasons as in
600 for the reasons as stated in
607 .Xr getdtablesize 2 ,
616 constant is non portable.
617 It is provided for compatibility with AIX and Solaris.
621 system call appeared in
625 .Dv F_DUPFD_CLOEXEC ,
629 constants first appeared in
634 constant was introduced in