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