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