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