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