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. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94
33 .\" $FreeBSD: src/lib/libc/sys/fcntl.2,v 1.16.2.13 2002/07/22 15:15:16 bde Exp $
46 .Fn fcntl "int fd" "int cmd" "..."
49 provides for control over descriptors.
52 is a descriptor to be operated on by
54 as described below. Depending on the value of
57 can take an additional third argument
59 .Bl -tag -width F_GETOWNX
61 Return a new descriptor as follows:
63 .Bl -bullet -compact -offset 4n
65 Lowest numbered available descriptor greater than or equal to
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 set to remain open across
84 Get the close-on-exec flag associated with the file descriptor
88 If the returned value ANDed with
91 the file will remain open across
93 otherwise the file will be closed upon execution of
98 Set the close-on-exec flag associated with
108 Get descriptor status flags, as described below
112 Set descriptor status flags to
115 Get the process ID or process group
120 signals; process groups are returned
125 Set the process or process group
131 process groups are specified by supplying
133 as negative, otherwise
135 is interpreted as a process ID.
142 flags are as follows:
143 .Bl -tag -width O_NONBLOCKX
145 Non-blocking I/O; if no data is available to a
149 operation would block,
150 the read or write call returns -1 with the error
153 Force each write to append at the end of file;
159 Minimize or eliminate the cache effects of reading and writing. The system
160 will attempt to avoid caching the data you read or write. If it cannot
161 avoid caching the data, it will minimize the impact the data has on the cache.
162 Use of this flag can drastically reduce performance if not used with care.
166 signal to be sent to the process group
167 when I/O is possible, e.g.,
168 upon availability of data to be read.
171 Several commands are available for doing advisory file locking;
172 they all operate on the following structure:
175 off_t l_start; /* starting offset */
176 off_t l_len; /* len = 0 means until end of file */
177 pid_t l_pid; /* lock owner */
178 short l_type; /* lock type: read/write, etc. */
179 short l_whence; /* type of l_start */
182 The commands available for advisory record locking are as follows:
183 .Bl -tag -width F_SETLKWX
185 Get the first lock that blocks the lock description pointed to by the
188 taken as a pointer to a
191 The information retrieved overwrites the information passed to
196 If no lock is found that would prevent this lock from being created,
197 the structure is left unchanged by this function call except for the
198 lock type which is set to
201 Set or clear a file segment lock according to the lock description
202 pointed to by the third argument,
204 taken as a pointer to a
208 is used to establish shared (or read) locks
210 or exclusive (or write) locks,
212 as well as remove either type of lock
214 If a shared or exclusive lock cannot be set,
216 returns immediately with
219 This command is the same as
221 except that if a shared or exclusive lock is blocked by other locks,
222 the process waits until the request can be satisfied.
223 If a signal that is to be caught is received while
225 is waiting for a region, the
227 will be interrupted if the signal handler has not specified the
233 When a shared lock has been set on a segment of a file,
234 other processes can set shared locks on that segment
236 A shared lock prevents any other process from setting an exclusive
237 lock on any portion of the protected area.
238 A request for a shared lock fails if the file descriptor was not
239 opened with read access.
241 An exclusive lock prevents any other process from setting a shared lock or
242 an exclusive lock on any portion of the protected area.
243 A request for an exclusive lock fails if the file was not
244 opened with write access.
253 to indicate that the relative offset,
255 bytes, will be measured from the start of the file,
256 current position, or end of the file, respectively.
259 is the number of consecutive bytes to be locked.
262 is negative, the result is undefined.
265 field is only used with
267 to return the process ID of the process holding a blocking lock.
270 request, the value of
275 Locks may start and extend beyond the current end of a file,
276 but may not start or extend before the beginning of the file.
277 A lock is set to extend to the largest possible value of the
278 file offset for that file if
285 point to the beginning of the file, and
287 is zero, the entire file is locked.
288 If an application wishes only to do entire file locking, the
290 system call is much more efficient.
292 There is at most one type of lock set for each byte in the file.
293 Before a successful return from an
297 request when the calling process has previously existing locks
298 on bytes in the region specified by the request,
299 the previous lock type for each byte in the specified
300 region is replaced by the new lock type.
301 As specified above under the descriptions
302 of shared locks and exclusive locks, an
306 request fails or blocks respectively when another process has existing
307 locks on bytes in the specified region and the type of any of those
308 locks conflicts with the type specified in the request.
310 This interface follows the completely stupid semantics of System V and
312 that require that all locks associated with a file for a given process are
315 file descriptor for that file is closed by that process.
316 This semantic means that applications must be aware of any files that
317 a subroutine library may access.
318 For example if an application for updating the password file locks the
319 password file database while making the update, and then calls
321 to retrieve a record,
322 the lock will be lost because
324 opens, reads, and closes the password database.
325 The database close will release all locks that the process has
326 associated with the database, even if the library routine never
327 requested a lock on the database.
328 Another minor semantic problem with this interface is that
329 locks are not inherited by a child process created using the
334 interface has much more rational last close semantics and
335 allows locks to be inherited by child processes.
337 is recommended for applications that want to ensure the integrity
338 of their locks when using library routines or wish to pass locks
344 locks may be safely used concurrently but
348 if the process holding a blocking lock previously locked the
352 All locks associated with a file for a given process are
353 removed when the process terminates.
355 All locks obtained before a call to
357 remain in effect until the new program releases them.
358 If the new program does not know about the locks, they will not be
359 released until the program exits.
361 A potential for deadlock occurs if a process controlling a locked region
362 is put to sleep by attempting to lock the locked region of another process.
363 This implementation detects that sleeping until a locked region is unlocked
364 would cause a deadlock and fails with an
368 Upon successful completion, the value returned depends on
371 .Bl -tag -width F_GETOWNX -offset indent
373 A new file descriptor.
375 Value of flag (only the low-order bit is defined).
379 Value of file descriptor owner.
384 Otherwise, a value of -1 is returned and
386 is set to indicate the error.
402 and the segment of a file to be locked is already
403 exclusive-locked by another process;
404 or the type is an exclusive lock and some portion of the
405 segment of a file to be locked is already shared-locked or
406 exclusive-locked by another process.
409 is not a valid open file descriptor.
423 is not a valid file descriptor open for reading.
437 is not a valid file descriptor open for writing.
443 and a deadlock condition was detected.
449 and the function was interrupted by a signal.
456 is negative or greater than the maximum allowable number
458 .Xr getdtablesize 2 ) .
467 and the data to which
469 points is not valid, or
471 refers to a file that does not support locking.
477 and the maximum number of file descriptors permitted for the
478 process are already in use,
479 or no file descriptors greater than or equal to
489 and satisfying the lock or unlock request would result in the
490 number of locked regions in the system exceeding a system-imposed limit.
496 the process ID or process group given as an argument is in a
497 different session than the caller.
503 the process ID given as argument is not in use.
508 refers to a descriptor open on a terminal device (as opposed to a
509 descriptor open on a socket), a
513 can fail for the same reasons as in
519 for the reasons as stated in
525 .Xr getdtablesize 2 ,
533 function call appeared in