]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/fcntl.2
Update to bmake-20200704
[FreeBSD/FreeBSD.git] / lib / libc / sys / fcntl.2
1 .\" Copyright (c) 1983, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
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.
15 .\"
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
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)fcntl.2     8.2 (Berkeley) 1/12/94
29 .\" $FreeBSD$
30 .\"
31 .Dd January 17, 2020
32 .Dt FCNTL 2
33 .Os
34 .Sh NAME
35 .Nm fcntl
36 .Nd file control
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In fcntl.h
41 .Ft int
42 .Fn fcntl "int fd" "int cmd" "..."
43 .Sh DESCRIPTION
44 The
45 .Fn fcntl
46 system call provides for control over descriptors.
47 The argument
48 .Fa fd
49 is a descriptor to be operated on by
50 .Fa cmd
51 as described below.
52 Depending on the value of
53 .Fa cmd ,
54 .Fn fcntl
55 can take an additional third argument
56 .Fa "int arg" .
57 .Bl -tag -width F_DUP2FD_CLOEXEC
58 .It Dv F_DUPFD
59 Return a new descriptor as follows:
60 .Pp
61 .Bl -bullet -compact -offset 4n
62 .It
63 Lowest numbered available descriptor greater than or equal to
64 .Fa arg .
65 .It
66 Same object references as the original descriptor.
67 .It
68 New descriptor shares the same file offset if the object
69 was a file.
70 .It
71 Same access mode (read, write or read/write).
72 .It
73 Same file status flags (i.e., both file descriptors
74 share the same file status flags).
75 .It
76 The close-on-exec flag
77 .Dv FD_CLOEXEC
78 associated with the new file descriptor is cleared, so the file descriptor is
79 to remain open across
80 .Xr execve 2
81 system calls.
82 .El
83 .It Dv F_DUPFD_CLOEXEC
84 Like
85 .Dv F_DUPFD ,
86 but the
87 .Dv FD_CLOEXEC
88 flag associated with the new file descriptor is set, so the file descriptor
89 is closed when
90 .Xr execve 2
91 system call executes.
92 .It Dv F_DUP2FD
93 It is functionally equivalent to
94 .Bd -literal -offset indent
95 dup2(fd, arg)
96 .Ed
97 .It Dv F_DUP2FD_CLOEXEC
98 Like
99 .Dv F_DUP2FD ,
100 but the
101 .Dv FD_CLOEXEC
102 flag associated with the new file descriptor is set.
103 .Pp
104 The
105 .Dv F_DUP2FD
106 and
107 .Dv F_DUP2FD_CLOEXEC
108 constants are not portable, so they should not be used if
109 portability is needed.
110 Use
111 .Fn dup2
112 instead of
113 .Dv F_DUP2FD .
114 .It Dv F_GETFD
115 Get the close-on-exec flag associated with the file descriptor
116 .Fa fd
117 as
118 .Dv FD_CLOEXEC .
119 If the returned value ANDed with
120 .Dv FD_CLOEXEC
121 is 0,
122 the file will remain open across
123 .Fn exec ,
124 otherwise the file will be closed upon execution of
125 .Fn exec
126 .Fa ( arg
127 is ignored).
128 .It Dv F_SETFD
129 Set the close-on-exec flag associated with
130 .Fa fd
131 to
132 .Fa arg ,
133 where
134 .Fa arg
135 is either 0 or
136 .Dv FD_CLOEXEC ,
137 as described above.
138 .It Dv F_GETFL
139 Get descriptor status flags, as described below
140 .Fa ( arg
141 is ignored).
142 .It Dv F_SETFL
143 Set descriptor status flags to
144 .Fa arg .
145 .It Dv F_GETOWN
146 Get the process ID or process group
147 currently receiving
148 .Dv SIGIO
149 and
150 .Dv SIGURG
151 signals; process groups are returned
152 as negative values
153 .Fa ( arg
154 is ignored).
155 .It Dv F_SETOWN
156 Set the process or process group
157 to receive
158 .Dv SIGIO
159 and
160 .Dv SIGURG
161 signals;
162 process groups are specified by supplying
163 .Fa arg
164 as negative, otherwise
165 .Fa arg
166 is interpreted as a process ID.
167 .It Dv F_READAHEAD
168 Set or clear the read ahead amount for sequential access to the third
169 argument,
170 .Fa arg ,
171 which is rounded up to the nearest block size.
172 A zero value in
173 .Fa arg
174 turns off read ahead, a negative value restores the system default.
175 .It Dv F_RDAHEAD
176 Equivalent to Darwin counterpart which sets read ahead amount of 128KB
177 when the third argument,
178 .Fa arg
179 is non-zero.
180 A zero value in
181 .Fa arg
182 turns off read ahead.
183 .It Dv F_ADD_SEALS
184 Add seals to the file as described below, if the underlying filesystem supports
185 seals.
186 .It Dv F_GET_SEALS
187 Get seals associated with the file, if the underlying filesystem supports seals.
188 .It Dv F_ISUNIONSTACK
189 Check if the vnode is part of a union stack (either the "union" flag from
190 .Xr mount 2
191 or unionfs).
192 This is a hack not intended to be used outside of libc.
193 .El
194 .Pp
195 The flags for the
196 .Dv F_GETFL
197 and
198 .Dv F_SETFL
199 flags are as follows:
200 .Bl -tag -width O_NONBLOCKX
201 .It Dv O_NONBLOCK
202 Non-blocking I/O; if no data is available to a
203 .Xr read 2
204 system call, or if a
205 .Xr write 2
206 operation would block,
207 the read or write call returns -1 with the error
208 .Er EAGAIN .
209 .It Dv O_APPEND
210 Force each write to append at the end of file;
211 corresponds to the
212 .Dv O_APPEND
213 flag of
214 .Xr open 2 .
215 .It Dv O_DIRECT
216 Minimize or eliminate the cache effects of reading and writing.
217 The system
218 will attempt to avoid caching the data you read or write.
219 If it cannot
220 avoid caching the data, it will minimize the impact the data has on the cache.
221 Use of this flag can drastically reduce performance if not used with care.
222 .It Dv O_ASYNC
223 Enable the
224 .Dv SIGIO
225 signal to be sent to the process group
226 when I/O is possible, e.g.,
227 upon availability of data to be read.
228 .El
229 .Pp
230 The seals that may be applied with
231 .Dv F_ADD_SEALS
232 are as follows:
233 .Bl -tag -width F_SEAL_SHRINK
234 .It Dv F_SEAL_SEAL
235 Prevent any further seals from being applied to the file.
236 .It Dv F_SEAL_SHRINK
237 Prevent the file from being shrunk with
238 .Xr ftruncate 2 .
239 .It Dv F_SEAL_GROW
240 Prevent the file from being enlarged with
241 .Xr ftruncate 2 .
242 .It Dv F_SEAL_WRITE
243 Prevent any further
244 .Xr write 2
245 calls to the file.
246 Any writes in progress will finish before
247 .Fn fcntl
248 returns.
249 If any writeable mappings exist, F_ADD_SEALS will fail and return
250 .Dv EBUSY .
251 .El
252 .Pp
253 Seals are on a per-inode basis and require support by the underlying filesystem.
254 If the underlying filesystem does not support seals,
255 .Dv F_ADD_SEALS
256 and
257 .Dv F_GET_SEALS
258 will fail and return
259 .Dv EINVAL .
260 .Pp
261 Several commands are available for doing advisory file locking;
262 they all operate on the following structure:
263 .Bd -literal
264 struct flock {
265         off_t   l_start;        /* starting offset */
266         off_t   l_len;          /* len = 0 means until end of file */
267         pid_t   l_pid;          /* lock owner */
268         short   l_type;         /* lock type: read/write, etc. */
269         short   l_whence;       /* type of l_start */
270         int     l_sysid;        /* remote system id or zero for local */
271 };
272 .Ed
273 The commands available for advisory record locking are as follows:
274 .Bl -tag -width F_SETLKWX
275 .It Dv F_GETLK
276 Get the first lock that blocks the lock description pointed to by the
277 third argument,
278 .Fa arg ,
279 taken as a pointer to a
280 .Fa "struct flock"
281 (see above).
282 The information retrieved overwrites the information passed to
283 .Fn fcntl
284 in the
285 .Fa flock
286 structure.
287 If no lock is found that would prevent this lock from being created,
288 the structure is left unchanged by this system call except for the
289 lock type which is set to
290 .Dv F_UNLCK .
291 .It Dv F_SETLK
292 Set or clear a file segment lock according to the lock description
293 pointed to by the third argument,
294 .Fa arg ,
295 taken as a pointer to a
296 .Fa "struct flock"
297 (see above).
298 .Dv F_SETLK
299 is used to establish shared (or read) locks
300 .Pq Dv F_RDLCK
301 or exclusive (or write) locks,
302 .Pq Dv F_WRLCK ,
303 as well as remove either type of lock
304 .Pq Dv F_UNLCK .
305 If a shared or exclusive lock cannot be set,
306 .Fn fcntl
307 returns immediately with
308 .Er EAGAIN .
309 .It Dv F_SETLKW
310 This command is the same as
311 .Dv F_SETLK
312 except that if a shared or exclusive lock is blocked by other locks,
313 the process waits until the request can be satisfied.
314 If a signal that is to be caught is received while
315 .Fn fcntl
316 is waiting for a region, the
317 .Fn fcntl
318 will be interrupted if the signal handler has not specified the
319 .Dv SA_RESTART
320 (see
321 .Xr sigaction 2 ) .
322 .El
323 .Pp
324 When a shared lock has been set on a segment of a file,
325 other processes can set shared locks on that segment
326 or a portion of it.
327 A shared lock prevents any other process from setting an exclusive
328 lock on any portion of the protected area.
329 A request for a shared lock fails if the file descriptor was not
330 opened with read access.
331 .Pp
332 An exclusive lock prevents any other process from setting a shared lock or
333 an exclusive lock on any portion of the protected area.
334 A request for an exclusive lock fails if the file was not
335 opened with write access.
336 .Pp
337 The value of
338 .Fa l_whence
339 is
340 .Dv SEEK_SET ,
341 .Dv SEEK_CUR ,
342 or
343 .Dv SEEK_END
344 to indicate that the relative offset,
345 .Fa l_start
346 bytes, will be measured from the start of the file,
347 current position, or end of the file, respectively.
348 The value of
349 .Fa l_len
350 is the number of consecutive bytes to be locked.
351 If
352 .Fa l_len
353 is negative,
354 .Fa l_start
355 means end edge of the region.
356 The
357 .Fa l_pid
358 and
359 .Fa l_sysid
360 fields are only used with
361 .Dv F_GETLK
362 to return the process ID of the process holding a blocking lock and
363 the system ID of the system that owns that process.
364 Locks created by the local system will have a system ID of zero.
365 After a successful
366 .Dv F_GETLK
367 request, the value of
368 .Fa l_whence
369 is
370 .Dv SEEK_SET .
371 .Pp
372 Locks may start and extend beyond the current end of a file,
373 but may not start or extend before the beginning of the file.
374 A lock is set to extend to the largest possible value of the
375 file offset for that file if
376 .Fa l_len
377 is set to zero.
378 If
379 .Fa l_whence
380 and
381 .Fa l_start
382 point to the beginning of the file, and
383 .Fa l_len
384 is zero, the entire file is locked.
385 If an application wishes only to do entire file locking, the
386 .Xr flock 2
387 system call is much more efficient.
388 .Pp
389 There is at most one type of lock set for each byte in the file.
390 Before a successful return from an
391 .Dv F_SETLK
392 or an
393 .Dv F_SETLKW
394 request when the calling process has previously existing locks
395 on bytes in the region specified by the request,
396 the previous lock type for each byte in the specified
397 region is replaced by the new lock type.
398 As specified above under the descriptions
399 of shared locks and exclusive locks, an
400 .Dv F_SETLK
401 or an
402 .Dv F_SETLKW
403 request fails or blocks respectively when another process has existing
404 locks on bytes in the specified region and the type of any of those
405 locks conflicts with the type specified in the request.
406 .Pp
407 The queuing for
408 .Dv F_SETLKW
409 requests on local files is fair;
410 that is, while the thread is blocked,
411 subsequent requests conflicting with its requests will not be granted,
412 even if these requests do not conflict with existing locks.
413 .Pp
414 This interface follows the completely stupid semantics of System V and
415 .St -p1003.1-88
416 that require that all locks associated with a file for a given process are
417 removed when
418 .Em any
419 file descriptor for that file is closed by that process.
420 This semantic means that applications must be aware of any files that
421 a subroutine library may access.
422 For example if an application for updating the password file locks the
423 password file database while making the update, and then calls
424 .Xr getpwnam 3
425 to retrieve a record,
426 the lock will be lost because
427 .Xr getpwnam 3
428 opens, reads, and closes the password database.
429 The database close will release all locks that the process has
430 associated with the database, even if the library routine never
431 requested a lock on the database.
432 Another minor semantic problem with this interface is that
433 locks are not inherited by a child process created using the
434 .Xr fork 2
435 system call.
436 The
437 .Xr flock 2
438 interface has much more rational last close semantics and
439 allows locks to be inherited by child processes.
440 The
441 .Xr flock 2
442 system call is recommended for applications that want to ensure the integrity
443 of their locks when using library routines or wish to pass locks
444 to their children.
445 .Pp
446 The
447 .Fn fcntl ,
448 .Xr flock 2 ,
449 and
450 .Xr lockf 3
451 locks are compatible.
452 Processes using different locking interfaces can cooperate
453 over the same file safely.
454 However, only one of such interfaces should be used within
455 the same process.
456 If a file is locked by a process through
457 .Xr flock 2 ,
458 any record within the file will be seen as locked
459 from the viewpoint of another process using
460 .Fn fcntl
461 or
462 .Xr lockf 3 ,
463 and vice versa.
464 Note that
465 .Fn fcntl F_GETLK
466 returns \-1 in
467 .Fa l_pid
468 if the process holding a blocking lock previously locked the
469 file descriptor by
470 .Xr flock 2 .
471 .Pp
472 All locks associated with a file for a given process are
473 removed when the process terminates.
474 .Pp
475 All locks obtained before a call to
476 .Xr execve 2
477 remain in effect until the new program releases them.
478 If the new program does not know about the locks, they will not be
479 released until the program exits.
480 .Pp
481 A potential for deadlock occurs if a process controlling a locked region
482 is put to sleep by attempting to lock the locked region of another process.
483 This implementation detects that sleeping until a locked region is unlocked
484 would cause a deadlock and fails with an
485 .Er EDEADLK
486 error.
487 .Sh RETURN VALUES
488 Upon successful completion, the value returned depends on
489 .Fa cmd
490 as follows:
491 .Bl -tag -width F_GETOWNX -offset indent
492 .It Dv F_DUPFD
493 A new file descriptor.
494 .It Dv F_DUP2FD
495 A file descriptor equal to
496 .Fa arg .
497 .It Dv F_GETFD
498 Value of flag (only the low-order bit is defined).
499 .It Dv F_GETFL
500 Value of flags.
501 .It Dv F_GETOWN
502 Value of file descriptor owner.
503 .It other
504 Value other than -1.
505 .El
506 .Pp
507 Otherwise, a value of -1 is returned and
508 .Va errno
509 is set to indicate the error.
510 .Sh ERRORS
511 The
512 .Fn fcntl
513 system call will fail if:
514 .Bl -tag -width Er
515 .It Bq Er EAGAIN
516 The argument
517 .Fa cmd
518 is
519 .Dv F_SETLK ,
520 the type of lock
521 .Pq Fa l_type
522 is a shared lock
523 .Pq Dv F_RDLCK
524 or exclusive lock
525 .Pq Dv F_WRLCK ,
526 and the segment of a file to be locked is already
527 exclusive-locked by another process;
528 or the type is an exclusive lock and some portion of the
529 segment of a file to be locked is already shared-locked or
530 exclusive-locked by another process.
531 .It Bq Er EBADF
532 The
533 .Fa fd
534 argument
535 is not a valid open file descriptor.
536 .Pp
537 The argument
538 .Fa cmd
539 is
540 .Dv F_DUP2FD ,
541 and
542 .Fa arg
543 is not a valid file descriptor.
544 .Pp
545 The argument
546 .Fa cmd
547 is
548 .Dv F_SETLK
549 or
550 .Dv F_SETLKW ,
551 the type of lock
552 .Pq Fa l_type
553 is a shared lock
554 .Pq Dv F_RDLCK ,
555 and
556 .Fa fd
557 is not a valid file descriptor open for reading.
558 .Pp
559 The argument
560 .Fa cmd
561 is
562 .Dv F_SETLK
563 or
564 .Dv F_SETLKW ,
565 the type of lock
566 .Pq Fa l_type
567 is an exclusive lock
568 .Pq Dv F_WRLCK ,
569 and
570 .Fa fd
571 is not a valid file descriptor open for writing.
572 .It Bq Er EBUSY
573 The argument
574 .Fa cmd
575 is
576 .Dv F_ADD_SEALS ,
577 attempting to set
578 .Dv F_SEAL_WRITE ,
579 and writeable mappings of the file exist.
580 .It Bq Er EDEADLK
581 The argument
582 .Fa cmd
583 is
584 .Dv F_SETLKW ,
585 and a deadlock condition was detected.
586 .It Bq Er EINTR
587 The argument
588 .Fa cmd
589 is
590 .Dv F_SETLKW ,
591 and the system call was interrupted by a signal.
592 .It Bq Er EINVAL
593 The
594 .Fa cmd
595 argument
596 is
597 .Dv F_DUPFD
598 and
599 .Fa arg
600 is negative or greater than the maximum allowable number
601 (see
602 .Xr getdtablesize 2 ) .
603 .Pp
604 The argument
605 .Fa cmd
606 is
607 .Dv F_GETLK ,
608 .Dv F_SETLK
609 or
610 .Dv F_SETLKW
611 and the data to which
612 .Fa arg
613 points is not valid.
614 .Pp
615 The argument
616 .Fa cmd
617 is
618 .Dv F_ADD_SEALS
619 or
620 .Dv F_GET_SEALS ,
621 and the underlying filesystem does not support sealing.
622 .Pp
623 The argument
624 .Fa cmd
625 is invalid.
626 .It Bq Er EMFILE
627 The argument
628 .Fa cmd
629 is
630 .Dv F_DUPFD
631 and the maximum number of file descriptors permitted for the
632 process are already in use,
633 or no file descriptors greater than or equal to
634 .Fa arg
635 are available.
636 .It Bq Er ENOTTY
637 The
638 .Fa fd
639 argument is not a valid file descriptor for the requested operation.
640 This may be the case if
641 .Fa fd
642 is a device node, or a descriptor returned by
643 .Xr kqueue 2 .
644 .It Bq Er ENOLCK
645 The argument
646 .Fa cmd
647 is
648 .Dv F_SETLK
649 or
650 .Dv F_SETLKW ,
651 and satisfying the lock or unlock request would result in the
652 number of locked regions in the system exceeding a system-imposed limit.
653 .It Bq Er EOPNOTSUPP
654 The argument
655 .Fa cmd
656 is
657 .Dv F_GETLK ,
658 .Dv F_SETLK
659 or
660 .Dv F_SETLKW
661 and
662 .Fa fd
663 refers to a file for which locking is not supported.
664 .It Bq Er EOVERFLOW
665 The argument
666 .Fa cmd
667 is
668 .Dv F_GETLK ,
669 .Dv F_SETLK
670 or
671 .Dv F_SETLKW
672 and an
673 .Fa off_t
674 calculation overflowed.
675 .It Bq Er EPERM
676 The
677 .Fa cmd
678 argument
679 is
680 .Dv F_SETOWN
681 and
682 the process ID or process group given as an argument is in a
683 different session than the caller.
684 .Pp
685 The
686 .Fa cmd
687 argument
688 is
689 .Dv F_ADD_SEALS
690 and the
691 .Dv F_SEAL_SEAL
692 seal has already been set.
693 .It Bq Er ESRCH
694 The
695 .Fa cmd
696 argument
697 is
698 .Dv F_SETOWN
699 and
700 the process ID given as argument is not in use.
701 .El
702 .Pp
703 In addition, if
704 .Fa fd
705 refers to a descriptor open on a terminal device (as opposed to a
706 descriptor open on a socket), a
707 .Fa cmd
708 of
709 .Dv F_SETOWN
710 can fail for the same reasons as in
711 .Xr tcsetpgrp 3 ,
712 and a
713 .Fa cmd
714 of
715 .Dv F_GETOWN
716 for the reasons as stated in
717 .Xr tcgetpgrp 3 .
718 .Sh SEE ALSO
719 .Xr close 2 ,
720 .Xr dup2 2 ,
721 .Xr execve 2 ,
722 .Xr flock 2 ,
723 .Xr getdtablesize 2 ,
724 .Xr open 2 ,
725 .Xr sigaction 2 ,
726 .Xr lockf 3 ,
727 .Xr tcgetpgrp 3 ,
728 .Xr tcsetpgrp 3
729 .Sh STANDARDS
730 The
731 .Dv F_DUP2FD
732 constant is non portable.
733 It is provided for compatibility with AIX and Solaris.
734 .Pp
735 Per
736 .St -susv4 ,
737 a call with
738 .Dv F_SETLKW
739 should fail with
740 .Bq Er EINTR
741 after any caught signal
742 and should continue waiting during thread suspension such as a stop signal.
743 However, in this implementation a call with
744 .Dv F_SETLKW
745 is restarted after catching a signal with a
746 .Dv SA_RESTART
747 handler or a thread suspension such as a stop signal.
748 .Sh HISTORY
749 The
750 .Fn fcntl
751 system call appeared in
752 .Bx 4.2 .
753 .Pp
754 The
755 .Dv F_DUP2FD
756 constant first appeared in
757 .Fx 7.1 .