1 .\" $NetBSD: fcntl.2,v 1.40 2012/01/25 00:28:35 christos Exp $
3 .\" Copyright (c) 1983, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94
37 .Nd file descriptor control
43 .Fn fcntl "int fd" "int cmd" "..."
46 provides for control over descriptors.
49 is a descriptor to be operated on by
52 The third parameter is called
54 and is technically a pointer to void, but it is
55 interpreted as an int by some commands and ignored by others.
58 .Bl -tag -width F_DUPFD_CLOEXEC
60 Return a new descriptor as follows:
62 .Bl -bullet -compact -offset 4n
64 Lowest numbered available descriptor greater than or equal to
66 which is interpreted as an int.
68 Same object references as the original descriptor.
70 New descriptor shares the same file offset if the object
73 Same access mode (read, write or read/write).
75 Same file status flags (i.e., both file descriptors
76 share the same file status flags).
78 The close-on-exec flag associated with the new file descriptor
79 is cleared to remain open across
83 .It Dv F_DUPFD_CLOEXEC
86 but sets the close-on-exec property on the file descriptor created.
88 Get the close-on-exec flag associated with the file descriptor
92 If the returned value ANDed with
95 the file will remain open across
97 otherwise the file will be closed upon execution of
102 Set the close-on-exec flag associated with
112 Get descriptor status flags, as described below
116 Set descriptor status flags to
118 which is interpreted as an int.
120 Get the process ID or process group
125 signals; process groups are returned
130 Set the process or process group
136 process groups are specified by supplying
138 as negative, otherwise
140 is interpreted as a process ID.
143 is interpreted as an int.
145 Close all file descriptors greater than or equal to
148 Return the maximum file descriptor number currently open by the process.
149 .It Dv F_GETNOSIGPIPE
152 flag is set in the file descriptor.
153 .It Dv F_SETNOSIGPIPE
156 in the file descriptor.
159 The set of valid flags for the
163 flags are as follows:
173 These flags are described in
176 Several commands are available for doing advisory file locking;
177 they all operate on the following structure:
180 off_t l_start; /* starting offset */
181 off_t l_len; /* len = 0 means until end of file */
182 pid_t l_pid; /* lock owner */
183 short l_type; /* lock type: read/write, etc. */
184 short l_whence; /* type of l_start */
188 The commands available for advisory record locking are as follows:
189 .Bl -tag -width F_SETLKWX
191 Get the first lock that blocks the lock description pointed to by the
194 taken as a pointer to a
197 The information retrieved overwrites the information passed to
202 If no lock is found that would prevent this lock from being created,
203 the structure is left unchanged by this function call except for the
209 Set or clear a file segment lock according to the lock description
210 pointed to by the third argument,
212 taken as a pointer to a
215 As specified by the value of
218 is used to establish shared (or read) locks
220 or exclusive (or write) locks,
222 as well as remove either type of lock
224 If a shared or exclusive lock cannot be set,
226 returns immediately with
229 This command is the same as
231 except that if a shared or exclusive lock is blocked by other locks,
232 the process waits until the request can be satisfied.
233 If a signal that is to be caught is received while
235 is waiting for a region, the
237 will be interrupted if the signal handler has not specified the
243 When a shared lock has been set on a segment of a file,
244 other processes can set shared locks on that segment
246 A shared lock prevents any other process from setting an exclusive
247 lock on any portion of the protected area.
248 A request for a shared lock fails if the file descriptor was not
249 opened with read access.
251 An exclusive lock prevents any other process from setting a shared lock or
252 an exclusive lock on any portion of the protected area.
253 A request for an exclusive lock fails if the file was not
254 opened with write access.
263 to indicate that the relative offset,
265 bytes, will be measured from the start of the file,
266 current position, or end of the file, respectively.
269 is the number of consecutive bytes to be locked.
272 is negative, the result is undefined.
275 field is only used with
277 to return the process ID of the process holding a blocking lock.
280 request, the value of
285 Locks may start and extend beyond the current end of a file,
286 but may not start or extend before the beginning of the file.
287 A lock is set to extend to the largest possible value of the
288 file offset for that file if
295 point to the beginning of the file, and
297 is zero, the entire file is locked.
298 If an application wishes only to do entire file locking, the
300 system call is much more efficient.
302 There is at most one type of lock set for each byte in the file.
303 Before a successful return from an
307 request when the calling process has previously existing locks
308 on bytes in the region specified by the request,
309 the previous lock type for each byte in the specified
310 region is replaced by the new lock type.
311 As specified above under the descriptions
312 of shared locks and exclusive locks, an
316 request fails or blocks respectively when another process has existing
317 locks on bytes in the specified region and the type of any of those
318 locks conflicts with the type specified in the request.
320 Upon successful completion, the value returned depends on
323 .Bl -tag -width F_GETOWNX -offset indent
325 A new file descriptor.
327 Value of flag (only the low-order bit is defined).
331 Value of file descriptor owner.
333 Value of the highest file descriptor open by the process.
335 Value other than \-1.
338 Otherwise, a value of \-1 is returned and
340 is set to indicate the error.
342 This interface follows the completely stupid semantics of
346 that require that all locks associated with a file for a given process are
347 removed when \fIany\fP file descriptor for that file is closed by that process.
348 This semantic means that applications must be aware of any files that
349 a subroutine library may access.
350 For example if an application for updating the password file locks the
351 password file database while making the update, and then calls
353 to retrieve a record,
354 the lock will be lost because
356 opens, reads, and closes the password database.
357 The database close will release all locks that the process has
358 associated with the database, even if the library routine never
359 requested a lock on the database.
361 Another minor semantic problem with this interface is that
362 locks are not inherited by a child process created using the
367 interface has much more rational last close semantics and
368 allows locks to be inherited by child processes.
371 is recommended for applications that want to ensure the integrity
372 of their locks when using library routines or wish to pass locks
378 locks may be safely used concurrently.
380 All locks associated with a file for a given process are
381 removed when the process terminates.
383 A potential for deadlock occurs if a process controlling a locked region
384 is put to sleep by attempting to lock the locked region of another process.
385 This implementation detects that sleeping until a locked region is unlocked
386 would cause a deadlock and fails with an
404 and the segment of a file to be locked is already
405 exclusive-locked by another process;
406 or the type is an exclusive lock and some portion of the
407 segment of a file to be locked is already shared-locked or
408 exclusive-locked by another process.
411 is not a valid open file descriptor.
425 is not a valid file descriptor open for reading.
439 is not a valid file descriptor open for writing.
445 and a deadlock condition was detected.
451 and the function was interrupted by a signal.
463 is negative or greater than the maximum allowable number
465 .Xr getdtablesize 3 ) .
474 and the data to which
476 points is not valid, or
478 refers to a file that does not support locking.
484 and the maximum number of file descriptors permitted for the
485 process are already in use,
486 or no file descriptors greater than or equal to
493 and system-wide the maximum allowed number of file descriptors are
502 and satisfying the lock or unlock request would result in the
503 number of locked regions in the system exceeding a system-imposed limit.
509 the process ID given as argument is not in use.
526 function call appeared in