]> git.saurik.com Git - apple/xnu.git/blob - bsd/man/man2/fcntl.2
xnu-1456.1.26.tar.gz
[apple/xnu.git] / bsd / man / man2 / fcntl.2
1 .\"
2 .\" Copyright (c) 2008 Apple Inc. All rights reserved.
3 .\"
4 .\" @APPLE_LICENSE_HEADER_START@
5 .\"
6 .\" This file contains Original Code and/or Modifications of Original Code
7 .\" as defined in and that are subject to the Apple Public Source License
8 .\" Version 2.0 (the 'License'). You may not use this file except in
9 .\" compliance with the License. Please obtain a copy of the License at
10 .\" http://www.opensource.apple.com/apsl/ and read it before using this
11 .\" file.
12 .\"
13 .\" The Original Code and all software distributed under the License are
14 .\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 .\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 .\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 .\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 .\" Please see the License for the specific language governing rights and
19 .\" limitations under the License.
20 .\"
21 .\" @APPLE_LICENSE_HEADER_END@
22 .\"
23 .\"
24 .\" $NetBSD: fcntl.2,v 1.6 1995/02/27 12:32:29 cgd Exp $
25 .\"
26 .\" Copyright (c) 1983, 1993
27 .\" The Regents of the University of California. All rights reserved.
28 .\"
29 .\" Redistribution and use in source and binary forms, with or without
30 .\" modification, are permitted provided that the following conditions
31 .\" are met:
32 .\" 1. Redistributions of source code must retain the above copyright
33 .\" notice, this list of conditions and the following disclaimer.
34 .\" 2. Redistributions in binary form must reproduce the above copyright
35 .\" notice, this list of conditions and the following disclaimer in the
36 .\" documentation and/or other materials provided with the distribution.
37 .\" 3. All advertising materials mentioning features or use of this software
38 .\" must display the following acknowledgement:
39 .\" This product includes software developed by the University of
40 .\" California, Berkeley and its contributors.
41 .\" 4. Neither the name of the University nor the names of its contributors
42 .\" may be used to endorse or promote products derived from this software
43 .\" without specific prior written permission.
44 .\"
45 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 .\" SUCH DAMAGE.
56 .\"
57 .\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94
58 .\"
59 .Dd October 2, 2008
60 .Dt FCNTL 2
61 .Os BSD 4.2
62 .Sh NAME
63 .Nm fcntl
64 .Nd file control
65 .Sh SYNOPSIS
66 .Fd #include <fcntl.h>
67 .Ft int
68 .Fo fcntl
69 .Fa "int fildes"
70 .Fa "int cmd"
71 .Fa "..."
72 .Fc
73 .Sh DESCRIPTION
74 .Fn Fcntl
75 provides for control over descriptors.
76 The argument
77 .Fa fildes
78 is a descriptor to be operated on by
79 .Fa cmd
80 as follows:
81 .Bl -tag -width F_WRITEBOOTSTRAPX
82 .It Dv F_DUPFD
83 Return a new descriptor as follows:
84 .Pp
85 .Bl -bullet -compact -offset 4n
86 .It
87 Lowest numbered available descriptor greater than or equal to
88 .Fa arg .
89 .It
90 Same object references as the original descriptor.
91 .It
92 New descriptor shares the same file offset if the object
93 was a file.
94 .It
95 Same access mode (read, write or read/write).
96 .It
97 Same file status flags (i.e., both file descriptors
98 share the same file status flags).
99 .It
100 The close-on-exec flag associated with the new file descriptor
101 is set to remain open across
102 .Xr execv 2
103 system calls.
104 .El
105 .It Dv F_GETFD
106 Get the close-on-exec flag associated with the file descriptor
107 .Fa fildes .
108 If the low-order bit of the returned value is 0,
109 the file will remain open across
110 .Fn exec ,
111 otherwise the file will be closed upon execution of
112 .Fn exec
113 .Fa ( arg
114 is ignored).
115 .It Dv F_SETFD
116 Set the close-on-exec flag associated with
117 .Fa fildes
118 to the low order bit of
119 .Fa arg
120 (0 or 1 as above).
121 .It Dv F_GETFL
122 Get descriptor status flags, as described below
123 .Fa ( arg
124 is ignored).
125 .It Dv F_SETFL
126 Set descriptor status flags to
127 .Fa arg .
128 .It Dv F_GETOWN
129 Get the process ID or process group
130 currently receiving
131 .Dv SIGIO
132 and
133 .Dv SIGURG
134 signals; process groups are returned
135 as negative values
136 .Fa ( arg
137 is ignored).
138 .It Dv F_SETOWN
139 Set the process or process group
140 to receive
141 .Dv SIGIO
142 and
143 .Dv SIGURG
144 signals;
145 process groups are specified by supplying
146 .Fa arg
147 as negative, otherwise
148 .Fa arg
149 is interpreted as a process ID.
150 .It Dv F_GETPATH
151 Get the path of the file descriptor
152 .Fa Fildes .
153 The argument must be a buffer of size
154 .Sy MAXPATHLEN
155 or greater.
156 .It Dv F_PREALLOCATE
157 Preallocate file storage space. Note: upon success,
158 the space that is allocated can be the same size or
159 larger than the space requested.
160 .It Dv F_SETSIZE
161 Truncate a file without zeroing space.
162 The calling process must have root privileges.
163 .It Dv F_RDADVISE
164 Issue an advisory read async with no copy to user.
165 .It Dv F_RDAHEAD
166 Turn read ahead off/on.
167 A zero value in
168 .Fa arg
169 disables read ahead.
170 A non-zero value in
171 .Fa arg
172 turns read ahead on.
173 .It Dv F_READBOOTSTRAP
174 Read bootstrap from disk.
175 .It Dv F_WRITEBOOTSTRAP
176 Write bootstrap on disk.
177 The calling process must have root privileges.
178 .It Dv F_NOCACHE
179 Turns data caching off/on. A non-zero value in
180 .Fa arg
181 turns data caching off.
182 A value of zero in
183 .Fa arg
184 turns data caching on.
185 .It Dv F_LOG2PHYS
186 Get disk device information.
187 Currently this only includes the
188 disk device address that corresponds
189 to the current file offset.
190 .It Dv F_FULLFSYNC
191 Does the same thing as
192 .Xr fsync 2
193 then asks the drive to
194 flush all buffered data to
195 the permanent storage device
196 .Fa ( arg
197 is ignored).
198 This is currently implemented on HFS, MS-DOS (FAT),
199 and Universal Disk Format (UDF) file systems.
200 The operation may take quite a while to complete.
201 Certain FireWire drives have also been known
202 to ignore the request to flush their buffered data.
203 .El
204 .Pp
205 The flags for the
206 .Dv F_GETFL
207 and
208 .Dv F_SETFL
209 commands are as follows:
210 .Bl -tag -width O_NONBLOCKX -offset indent
211 .It Dv O_NONBLOCK
212 Non-blocking I/O; if no data is available to a
213 .Xr read
214 call, or if a
215 .Xr write
216 operation would block,
217 the read or write call returns -1 with the error
218 .Er EAGAIN .
219 .It Dv O_APPEND
220 Force each write to append at the end of file;
221 corresponds to the
222 .Dv O_APPEND
223 flag of
224 .Xr open 2 .
225 .It Dv O_ASYNC
226 Enable the
227 .Dv SIGIO
228 signal to be sent to the process group
229 when I/O is possible, e.g.,
230 upon availability of data to be read.
231 .El
232 .Pp
233 Several commands are available for doing advisory file locking;
234 they all operate on the following structure:
235 .ne 7v
236 .Bd -literal
237 struct flock {
238 off_t l_start; /* starting offset */
239 off_t l_len; /* len = 0 means until end of file */
240 pid_t l_pid; /* lock owner */
241 short l_type; /* lock type: read/write, etc. */
242 short l_whence; /* type of l_start */
243 };
244 .Ed
245 .Pp
246 The commands available for advisory record locking are as follows:
247 .Bl -tag -width F_SETLKWX
248 .It Dv F_GETLK
249 Get the first lock that blocks the lock description pointed to by the
250 third argument,
251 .Fa arg ,
252 taken as a pointer to a
253 .Fa "struct flock"
254 (see above).
255 The information retrieved overwrites the information passed to
256 .Nm fcntl
257 in the
258 .Fa flock
259 structure.
260 If no lock is found that would prevent this lock from being created,
261 the structure is left unchanged by this function call except for the
262 lock type which is set to
263 .Dv F_UNLCK .
264 .It Dv F_SETLK
265 Set or clear a file segment lock according to the lock description
266 pointed to by the third argument,
267 .Fa arg ,
268 taken as a pointer to a
269 .Fa "struct flock"
270 (see above).
271 .Dv F_SETLK
272 is used to establish shared (or read) locks
273 .Dv (F_RDLCK)
274 or exclusive (or write) locks,
275 .Dv (F_WRLCK) ,
276 as well as remove either type of lock
277 .Dv (F_UNLCK) .
278 If a shared or exclusive lock cannot be set,
279 .Nm fcntl
280 returns immediately with
281 .Er EAGAIN .
282 .It Dv F_SETLKW
283 This command is the same as
284 .Dv F_SETLK
285 except that if a shared or exclusive lock is blocked by other locks,
286 the process waits until the request can be satisfied.
287 If a signal that is to be caught is received while
288 .Nm fcntl
289 is waiting for a region, the
290 .Nm fcntl
291 will be interrupted if the signal handler has not specified the
292 .Dv SA_RESTART
293 (see
294 .Xr sigaction 2 ) .
295 .El
296 .Pp
297 When a shared lock has been set on a segment of a file,
298 other processes can set shared locks on that segment
299 or a portion of it.
300 A shared lock prevents any other process from setting an exclusive
301 lock on any portion of the protected area.
302 A request for a shared lock fails if the file descriptor was not
303 opened with read access.
304 .Pp
305 An exclusive lock prevents any other process from setting a shared lock or
306 an exclusive lock on any portion of the protected area.
307 A request for an exclusive lock fails if the file was not
308 opened with write access.
309 .Pp
310 The value of
311 .Fa l_whence
312 is
313 .Dv SEEK_SET ,
314 .Dv SEEK_CUR ,
315 or
316 .Dv SEEK_END
317 to indicate that the relative offset,
318 .Fa l_start
319 bytes, will be measured from the start of the file,
320 current position, or end of the file, respectively.
321 The value of
322 .Fa l_len
323 is the number of consecutive bytes to be locked.
324 If
325 .Fa l_len
326 is negative, the result is undefined.
327 The
328 .Fa l_pid
329 field is only used with
330 .Dv F_GETLK
331 to return the process ID of the process holding a blocking lock.
332 After a successful
333 .Dv F_GETLK
334 request, the value of
335 .Fa l_whence
336 is
337 .Dv SEEK_SET .
338 .Pp
339 Locks may start and extend beyond the current end of a file,
340 but may not start or extend before the beginning of the file.
341 A lock is set to extend to the largest possible value of the
342 file offset for that file if
343 .Fa l_len
344 is set to zero. If
345 .Fa l_whence
346 and
347 .Fa l_start
348 point to the beginning of the file, and
349 .Fa l_len
350 is zero, the entire file is locked.
351 If an application wishes only to do entire file locking, the
352 .Xr flock 2
353 system call is much more efficient.
354 .Pp
355 There is at most one type of lock set for each byte in the file.
356 Before a successful return from an
357 .Dv F_SETLK
358 or an
359 .Dv F_SETLKW
360 request when the calling process has previously existing locks
361 on bytes in the region specified by the request,
362 the previous lock type for each byte in the specified
363 region is replaced by the new lock type.
364 As specified above under the descriptions
365 of shared locks and exclusive locks, an
366 .Dv F_SETLK
367 or an
368 .Dv F_SETLKW
369 request fails or blocks respectively when another process has existing
370 locks on bytes in the specified region and the type of any of those
371 locks conflicts with the type specified in the request.
372 .Pp
373 This interface follows the completely stupid semantics of System V and
374 .St -p1003.1-88
375 that require that all locks associated with a file for a given process are
376 removed when \fIany\fP file descriptor for that file is closed by that process.
377 This semantic means that applications must be aware of any files that
378 a subroutine library may access.
379 For example if an application for updating the password file locks the
380 password file database while making the update, and then calls
381 .Xr getpwname 3
382 to retrieve a record,
383 the lock will be lost because
384 .Xr getpwname 3
385 opens, reads, and closes the password database.
386 The database close will release all locks that the process has
387 associated with the database, even if the library routine never
388 requested a lock on the database.
389 Another minor semantic problem with this interface is that
390 locks are not inherited by a child process created using the
391 .Xr fork 2
392 function.
393 The
394 .Xr flock 2
395 interface has much more rational last close semantics and
396 allows locks to be inherited by child processes.
397 .Xr Flock 2
398 is recommended for applications that want to ensure the integrity
399 of their locks when using library routines or wish to pass locks
400 to their children.
401 Note that
402 .Xr flock 2
403 and
404 .Xr fcntl 2
405 locks may be safely used concurrently.
406 .Pp
407 All locks associated with a file for a given process are
408 removed when the process terminates.
409 .Pp
410 A potential for deadlock occurs if a process controlling a locked region
411 is put to sleep by attempting to lock the locked region of another process.
412 This implementation detects that sleeping until a locked region is unlocked
413 would cause a deadlock and fails with an
414 .Er EDEADLK
415 error.
416 .Pp
417 The
418 .Dv F_PREALLOCATE
419 command operates on the following structure:
420 .ne 7v
421 .Bd -literal
422 typedef struct fstore {
423 u_int32_t fst_flags; /* IN: flags word */
424 int fst_posmode; /* IN: indicates offset field */
425 off_t fst_offset; /* IN: start of the region */
426 off_t fst_length; /* IN: size of the region */
427 off_t fst_bytesalloc; /* OUT: number of bytes allocated */
428 } fstore_t;
429 .Ed
430 .Pp
431 The flags (fst_flags) for the
432 .Dv F_PREALLOCATE
433 command are as follows:
434 .Bl -tag -width F_ALLOCATECONTIGX -offset indent
435 .It Dv F_ALLOCATECONTIG
436 Allocate contiguous space.
437 .It Dv F_ALLOCATEALL
438 Allocate all requested space or no space at all.
439 .El
440 .Pp
441 The position modes (fst_posmode) for the
442 .Dv F_PREALLOCATE
443 command indicate how to use the offset field.
444 The modes are as follows:
445 .Bl -tag -width F_PEOFPOSMODEX -offset indent
446 .It Dv F_PEOFPOSMODE
447 Allocate from the physical end of file.
448 .It Dv F_VOLPOSMODE
449 Allocate from the volume offset.
450 .El
451 .Pp
452 The
453 .Dv F_RDADVISE
454 command operates on the following structure
455 which holds information passed from the
456 user to the system:
457 .ne 7v
458 .Bd -literal
459 struct radvisory {
460 off_t ra_offset; /* offset into the file */
461 int ra_count; /* size of the read */
462 };
463 .Ed
464 .Pp
465 The
466 .Dv F_READBOOTSTRAP and F_WRITEBOOTSTRAP
467 commands operate on the following structure.
468 .ne 7v
469 .Bd -literal
470 typedef struct fbootstraptransfer {
471 off_t fbt_offset; /* IN: offset to start read/write */
472 size_t fbt_length; /* IN: number of bytes to transfer */
473 void *fbt_buffer; /* IN: buffer to be read/written */
474 } fbootstraptransfer_t;
475 .Ed
476 .Pp
477 The
478 .Dv F_LOG2PHYS
479 command operates on the following structure.
480 .ne 7v
481 .Bd -literal
482 struct log2phys {
483 u_int32_t l2p_flags; /* unused so far */
484 off_t l2p_contigbytes; /* unused so far */
485 off_t l2p_devoffset; /* bytes into device */
486 };
487 .Ed
488 .Sh RETURN VALUES
489 Upon successful completion, the value returned depends on
490 .Fa cmd
491 as follows:
492 .Bl -tag -width F_GETOWNX -offset indent
493 .It Dv F_DUPFD
494 A new file descriptor.
495 .It Dv F_GETFD
496 Value of flag (only the low-order bit is defined).
497 .It Dv F_GETFL
498 Value of flags.
499 .It Dv F_GETOWN
500 Value of file descriptor owner.
501 .It other
502 Value other than -1.
503 .El
504 .Pp
505 Otherwise, a value of -1 is returned and
506 .Va errno
507 is set to indicate the error.
508 .Sh ERRORS
509 The
510 .Fn fcntl
511 system call will fail if:
512 .Bl -tag -width Er
513 .\" ==========
514 .It Bq Er EAGAIN
515 The argument
516 .Fa cmd
517 is
518 .Dv F_SETLK ,
519 the type of lock
520 .Fa (l_type)
521 is a shared lock
522 .Dv (F_RDLCK)
523 or exclusive lock
524 .Dv (F_WRLCK) ,
525 and the segment of a file to be locked is already
526 exclusive-locked by another process;
527 or the type is an exclusive lock and some portion of the
528 segment of a file to be locked is already shared-locked or
529 exclusive-locked by another process.
530 .It Bq Er EACCESS
531 The argument
532 .Fa cmd
533 is either
534 .Dv F_SETSIZE
535 or
536 .Dv F_WRITEBOOTSTRAP
537 and the calling process does not have root privileges.
538 .\" ==========
539 .It Bq Er EBADF
540 .Fa Fildes
541 is not a valid open file descriptor.
542 .Pp
543 The argument
544 .Fa cmd
545 is
546 .Dv F_SETLK
547 or
548 .Dv F_SETLKW ,
549 the type of lock
550 .Fa (l_type)
551 is a shared lock
552 .Dv (F_RDLCK) ,
553 and
554 .Fa fildes
555 is not a valid file descriptor open for reading.
556 .Pp
557 The argument
558 .Fa cmd
559 is
560 .Dv F_SETLK
561 or
562 .Dv F_SETLKW ,
563 the type of lock
564 .Fa (l_type)
565 is an exclusive lock
566 .Dv (F_WRLCK) ,
567 and
568 .Fa fildes
569 is not a valid file descriptor open for writing.
570 .Pp
571 The argument
572 .Fa cmd
573 is
574 .Dv F_PREALLOCATE
575 and the calling process does not have
576 file write permission.
577 .Pp
578 The argument
579 .Fa cmd
580 is
581 .Dv F_LOG2PHYS
582 and
583 .Fa fildes
584 is not a valid file descriptor open for reading.
585 .\" ==========
586 .It Bq Er EDEADLK
587 The argument
588 .Fa cmd
589 is
590 .Dv F_SETLKW ,
591 and a deadlock condition was detected.
592 .\" ==========
593 .It Bq Er EINTR
594 The argument
595 .Fa cmd
596 is
597 .Dv F_SETLKW ,
598 and the function was interrupted by a signal.
599 .\" ==========
600 .It Bq Er EINVAL
601 .Fa Cmd
602 is
603 .Dv F_DUPFD
604 and
605 .Fa arg
606 is negative or greater than the maximum allowable number
607 (see
608 .Xr getdtablesize 2 ) .
609 .Pp
610 The argument
611 .Fa cmd
612 is
613 .Dv F_GETLK ,
614 .Dv F_SETLK ,
615 or
616 .Dv F_SETLKW
617 and the data to which
618 .Fa arg
619 points is not valid, or
620 .Fa fildes
621 refers to a file that does not support locking.
622 .Pp
623 The argument
624 .Fa cmd
625 is
626 .Dv F_PREALLOCATE
627 and the
628 .Fa fst_posmode
629 is not a valid mode,
630 or when
631 .Dv F_PEOFPOSMODE
632 is set and
633 .Fa fst_offset
634 is a non-zero value,
635 or when
636 .Dv F_VOLPOSMODE
637 is set and
638 .Fa fst_offset
639 is a negative or zero value.
640 .Pp
641 The argument
642 .Fa cmd
643 is either
644 .Dv F_READBOOTSTRAP
645 or
646 .Dv F_WRITEBOOTSTRAP
647 and the operation was attempted on a non-HFS disk type.
648 .\" ==========
649 .It Bq Er EMFILE
650 .Fa Cmd
651 is
652 .Dv F_DUPFD
653 and the maximum allowed number of file descriptors are currently
654 open.
655 .\" ==========
656 .It Bq Er EMFILE
657 The argument
658 .Fa cmd
659 is
660 .Dv F_DUPED
661 and the maximum number of file descriptors permitted for the
662 process are already in use,
663 or no file descriptors greater than or equal to
664 .Fa arg
665 are available.
666 .\" ==========
667 .It Bq Er ENOLCK
668 The argument
669 .Fa cmd
670 is
671 .Dv F_SETLK
672 or
673 .Dv F_SETLKW ,
674 and satisfying the lock or unlock request would result in the
675 number of locked regions in the system exceeding a system-imposed limit.
676 .\" ==========
677 .It Bq Er EOVERFLOW
678 A return value would overflow its representation.
679 For example,
680 .Fa cmd
681 is F_GETLK, F_SETLK, or F_SETLKW
682 and the smallest (or, if l_len is non-zero, the largest) offset
683 of a byte in the requested segment
684 will not fit in an object of type off_t.
685 .\" ==========
686 .It Bq Er ESRCH
687 .Fa Cmd
688 is
689 .Dv F_SETOWN
690 and
691 the process ID given as argument is not in use.
692 .El
693 .Sh SEE ALSO
694 .Xr close 2 ,
695 .Xr execve 2 ,
696 .Xr flock 2 ,
697 .Xr getdtablesize 2 ,
698 .Xr open 2 ,
699 .Xr sigaction 3
700 .Sh HISTORY
701 The
702 .Fn fcntl
703 function call appeared in
704 .Bx 4.2 .