1 .\" $NetBSD: fcntl.2,v 1.6 1995/02/27 12:32:29 cgd 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. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94
43 .Fd #include <fcntl.h>
52 provides for control over descriptors.
55 is a descriptor to be operated on by
58 .Bl -tag -width F_WRITEBOOTSTRAPX
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 associated with the new file descriptor
78 is set to remain open across
83 Get the close-on-exec flag associated with the file descriptor
85 If the low-order bit of the returned value is 0,
86 the file will remain open across
88 otherwise the file will be closed upon execution of
93 Set the close-on-exec flag associated with
95 to the low order bit of
99 Get descriptor status flags, as described below
103 Set descriptor status flags to
106 Get the process ID or process group
111 signals; process groups are returned
116 Set the process or process group
122 process groups are specified by supplying
124 as negative, otherwise
126 is interpreted as a process ID.
128 Get the path of the file descriptor
130 The argument must be a buffer of size
134 Preallocate file storage space.
136 Truncate a file without zeroing space.
137 The calling process must have root privileges.
139 Issue an advisory read async with no copy to user.
141 Turn read ahead off/on.
148 .It Dv F_READBOOTSTRAP
149 Read bootstrap from disk.
150 .It Dv F_WRITEBOOTSTRAP
151 Write bootstrap on disk.
152 The calling process must have root privileges.
154 Turns data caching off/on. A non-zero value in
156 turns data caching off.
159 turns data caching on.
161 Get disk device information.
162 Currently this only includes the
163 disk device address that corresponds
164 to the current file offset.
166 Does the same thing as
168 then asks the drive to
169 flush all buffered data to
170 the permanent storage device
173 This is currently implemented on HFS, MS-DOS (FAT),
174 and Universal Disk Format (UDF) file systems.
175 The operation may take quite a while to complete.
176 Certain FireWire drives have also been known
177 to ignore the request to flush their buffered data.
184 commands are as follows:
185 .Bl -tag -width O_NONBLOCKX -offset indent
187 Non-blocking I/O; if no data is available to a
191 operation would block,
192 the read or write call returns -1 with the error
195 Force each write to append at the end of file;
203 signal to be sent to the process group
204 when I/O is possible, e.g.,
205 upon availability of data to be read.
208 Several commands are available for doing advisory file locking;
209 they all operate on the following structure:
213 off_t l_start; /* starting offset */
214 off_t l_len; /* len = 0 means until end of file */
215 pid_t l_pid; /* lock owner */
216 short l_type; /* lock type: read/write, etc. */
217 short l_whence; /* type of l_start */
221 The commands available for advisory record locking are as follows:
222 .Bl -tag -width F_SETLKWX
224 Get the first lock that blocks the lock description pointed to by the
227 taken as a pointer to a
230 The information retrieved overwrites the information passed to
235 If no lock is found that would prevent this lock from being created,
236 the structure is left unchanged by this function call except for the
237 lock type which is set to
240 Set or clear a file segment lock according to the lock description
241 pointed to by the third argument,
243 taken as a pointer to a
247 is used to establish shared (or read) locks
249 or exclusive (or write) locks,
251 as well as remove either type of lock
253 If a shared or exclusive lock cannot be set,
255 returns immediately with
258 This command is the same as
260 except that if a shared or exclusive lock is blocked by other locks,
261 the process waits until the request can be satisfied.
262 If a signal that is to be caught is received while
264 is waiting for a region, the
266 will be interrupted if the signal handler has not specified the
272 When a shared lock has been set on a segment of a file,
273 other processes can set shared locks on that segment
275 A shared lock prevents any other process from setting an exclusive
276 lock on any portion of the protected area.
277 A request for a shared lock fails if the file descriptor was not
278 opened with read access.
280 An exclusive lock prevents any other process from setting a shared lock or
281 an exclusive lock on any portion of the protected area.
282 A request for an exclusive lock fails if the file was not
283 opened with write access.
292 to indicate that the relative offset,
294 bytes, will be measured from the start of the file,
295 current position, or end of the file, respectively.
298 is the number of consecutive bytes to be locked.
301 is negative, the result is undefined.
304 field is only used with
306 to return the process ID of the process holding a blocking lock.
309 request, the value of
314 Locks may start and extend beyond the current end of a file,
315 but may not start or extend before the beginning of the file.
316 A lock is set to extend to the largest possible value of the
317 file offset for that file if
323 point to the beginning of the file, and
325 is zero, the entire file is locked.
326 If an application wishes only to do entire file locking, the
328 system call is much more efficient.
330 There is at most one type of lock set for each byte in the file.
331 Before a successful return from an
335 request when the calling process has previously existing locks
336 on bytes in the region specified by the request,
337 the previous lock type for each byte in the specified
338 region is replaced by the new lock type.
339 As specified above under the descriptions
340 of shared locks and exclusive locks, an
344 request fails or blocks respectively when another process has existing
345 locks on bytes in the specified region and the type of any of those
346 locks conflicts with the type specified in the request.
348 This interface follows the completely stupid semantics of System V and
350 that require that all locks associated with a file for a given process are
351 removed when \fIany\fP file descriptor for that file is closed by that process.
352 This semantic means that applications must be aware of any files that
353 a subroutine library may access.
354 For example if an application for updating the password file locks the
355 password file database while making the update, and then calls
357 to retrieve a record,
358 the lock will be lost because
360 opens, reads, and closes the password database.
361 The database close will release all locks that the process has
362 associated with the database, even if the library routine never
363 requested a lock on the database.
364 Another minor semantic problem with this interface is that
365 locks are not inherited by a child process created using the
370 interface has much more rational last close semantics and
371 allows locks to be inherited by child processes.
373 is recommended for applications that want to ensure the integrity
374 of their locks when using library routines or wish to pass locks
380 locks may be safely used concurrently.
382 All locks associated with a file for a given process are
383 removed when the process terminates.
385 A potential for deadlock occurs if a process controlling a locked region
386 is put to sleep by attempting to lock the locked region of another process.
387 This implementation detects that sleeping until a locked region is unlocked
388 would cause a deadlock and fails with an
394 command operates on the following structure:
397 typedef struct fstore {
398 u_int32_t fst_flags; /* IN: flags word */
399 int fst_posmode; /* IN: indicates offset field */
400 off_t fst_offset; /* IN: start of the region */
401 off_t fst_length; /* IN: size of the region */
402 off_t fst_bytesalloc; /* OUT: number of bytes allocated */
406 The flags (fst_flags) for the
408 command are as follows:
409 .Bl -tag -width F_ALLOCATECONTIGX -offset indent
410 .It Dv F_ALLOCATECONTIG
411 Allocate contiguous space.
413 Allocate all requested space or no space at all.
416 The position modes (fst_posmode) for the
418 command indicate how to use the offset field.
419 The modes are as follows:
420 .Bl -tag -width F_PEOFPOSMODEX -offset indent
422 Allocate from the physical end of file.
424 Allocate from the volume offset.
429 command operates on the following structure
430 which holds information passed from the
435 off_t ra_offset; /* offset into the file */
436 int ra_count; /* size of the read */
441 .Dv F_READBOOTSTRAP and F_WRITEBOOTSTRAP
442 commands operate on the following structure.
445 typedef struct fbootstraptransfer {
446 off_t fbt_offset; /* IN: offset to start read/write */
447 size_t fbt_length; /* IN: number of bytes to transfer */
448 void *fbt_buffer; /* IN: buffer to be read/written */
449 } fbootstraptransfer_t;
454 command operates on the following structure.
458 u_int32_t l2p_flags; /* unused so far */
459 off_t l2p_contigbytes; /* unused so far */
460 off_t l2p_devoffset; /* bytes into device */
464 Upon successful completion, the value returned depends on
467 .Bl -tag -width F_GETOWNX -offset indent
469 A new file descriptor.
471 Value of flag (only the low-order bit is defined).
475 Value of file descriptor owner.
480 Otherwise, a value of -1 is returned and
482 is set to indicate the error.
486 system call will fail if:
500 and the segment of a file to be locked is already
501 exclusive-locked by another process;
502 or the type is an exclusive lock and some portion of the
503 segment of a file to be locked is already shared-locked or
504 exclusive-locked by another process.
512 and the calling process does not have root privileges.
516 is not a valid open file descriptor.
530 is not a valid file descriptor open for reading.
544 is not a valid file descriptor open for writing.
550 and the calling process does not have
551 file write permission.
559 is not a valid file descriptor open for reading.
566 and a deadlock condition was detected.
573 and the function was interrupted by a signal.
581 is negative or greater than the maximum allowable number
583 .Xr getdtablesize 2 ) .
592 and the data to which
594 points is not valid, or
596 refers to a file that does not support locking.
614 is a negative or zero value.
622 and the operation was attempted on a non-HFS disk type.
628 and the maximum allowed number of file descriptors are currently
636 and the maximum number of file descriptors permitted for the
637 process are already in use,
638 or no file descriptors greater than or equal to
649 and satisfying the lock or unlock request would result in the
650 number of locked regions in the system exceeding a system-imposed limit.
653 A return value would overflow its representation.
656 is F_GETLK, F_SETLK, or F_SETLKW
657 and the smallest (or, if l_len is non-zero, the largest) offset
658 of a byte in the requested segment
659 will not fit in an object of type off_t.
666 the process ID given as argument is not in use.
672 .Xr getdtablesize 2 ,
678 function call appeared in