1.\" $OpenBSD: fcntl.2,v 1.28 2007/05/31 19:19:32 jmc Exp $ 2.\" $NetBSD: fcntl.2,v 1.6 1995/02/27 12:32:29 cgd Exp $ 3.\" 4.\" Copyright (c) 1983, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)fcntl.2 8.2 (Berkeley) 1/12/94 32.\" 33.Dd $Mdocdate: May 31 2007 $ 34.Dt FCNTL 2 35.Os 36.Sh NAME 37.Nm fcntl 38.Nd file control 39.Sh SYNOPSIS 40.Fd #include <fcntl.h> 41.Ft int 42.Fn fcntl "int fd" "int cmd" "..." 43.Sh DESCRIPTION 44The 45.Fn fcntl 46provides control over the properties of a file that is already open. 47The argument 48.Fa fd 49is a descriptor to be operated on by 50.Fa cmd 51as described below. 52The third parameter is called 53.Fa arg 54and is technically a pointer to 55.Fa void , 56but is interpreted as an 57.Li int 58by some commands, a pointer to a 59.Li struct flock 60by others (see below), and ignored by the rest. 61.Pp 62The commands are: 63.Bl -tag -width F_GETOWNX 64.It Dv F_DUPFD 65Return a new descriptor as follows: 66.Pp 67.Bl -bullet -compact 68.It 69Lowest numbered available descriptor greater than or equal to 70.Fa arg 71(interpreted as an 72.Li int ) . 73.It 74Same object references as the original descriptor. 75.It 76New descriptor shares the same file offset if the object 77was a file. 78.It 79Same access mode (read, write or read/write). 80.It 81Same file status flags (i.e., both file descriptors 82share the same file status flags). 83.It 84The close-on-exec flag associated with the new file descriptor 85is set to remain open across 86.Xr execv 3 87calls. 88.El 89.It Dv F_GETFD 90Get the close-on-exec flag associated with the file descriptor 91.Fa fd 92as 93.Dv FD_CLOEXEC . 94If the returned value ANDed with 95.Dv FD_CLOEXEC 96is 0, 97the file will remain open across 98.Fn exec , 99otherwise the file will be closed upon execution of 100.Fn exec 101.Fa ( arg 102is ignored). 103.It Dv F_SETFD 104Set the close-on-exec flag associated with 105.Fa fd 106to 107.Fa arg , 108where 109.Fa arg 110(interpreted as an 111.Li int ) 112is either 0 or 113.Dv FD_CLOEXEC , 114as described above. 115.It Dv F_GETFL 116Get file status flags associated with the file descriptor 117.Fa fd , 118as described below 119.Fa ( arg 120is ignored). 121.It Dv F_SETFL 122Set file status flags associated with the file descriptor 123.Fa fd 124to 125.Fa arg 126(interpreted as an 127.Li int ) . 128.It Dv F_GETOWN 129Get the process ID or process group 130currently receiving 131.Dv SIGIO 132and 133.Dv SIGURG 134signals; process groups are returned 135as negative values 136.Fa ( arg 137is ignored). 138.It Dv F_SETOWN 139Set the process or process group 140to receive 141.Dv SIGIO 142and 143.Dv SIGURG 144signals; 145process groups are specified by supplying 146.Fa arg 147(interpreted as an 148.Li int ) 149as negative, otherwise 150.Fa arg 151is taken as a process ID. 152.El 153.Pp 154The flags for the 155.Dv F_GETFL 156and 157.Dv F_SETFL 158commands are as follows: 159.Bl -tag -width O_NONBLOCKX 160.It Dv O_NONBLOCK 161Non-blocking 162.Tn I/O ; 163if no data is available to a 164.Xr read 2 165call, or if a 166.Xr write 2 167operation would block, 168the read or write call returns \-1 with the error 169.Er EAGAIN . 170.It Dv O_APPEND 171Force each write to append at the end of file; 172corresponds to the 173.Dv O_APPEND 174flag of 175.Xr open 2 . 176.It Dv O_ASYNC 177Enable the 178.Dv SIGIO 179signal to be sent to the process group 180when 181.Tn I/O 182is possible, e.g., 183upon availability of data to be read. 184.It Dv O_SYNC 185Cause writes to be synchronous. 186Data will be written to the physical device instead of 187just being stored in the buffer cache; corresponds to the 188.Dv O_SYNC 189flag of 190.Xr open 2 . 191.El 192.Pp 193Several commands are available for doing advisory file locking; 194they all operate on the following structure: 195.Bd -literal 196struct flock { 197 off_t l_start; /* starting offset */ 198 off_t l_len; /* len = 0 means until end of file */ 199 pid_t l_pid; /* lock owner */ 200 short l_type; /* lock type: read/write, etc. */ 201 short l_whence; /* type of l_start */ 202}; 203.Ed 204.Pp 205The commands available for advisory record locking are as follows: 206.Bl -tag -width F_SETLKWX 207.It Dv F_GETLK 208Get the first lock that blocks the lock description pointed to by the 209third argument, 210.Fa arg , 211taken as a pointer to a 212.Fa "struct flock" 213(see above). 214The information retrieved overwrites the information passed to 215.Fn fcntl 216in the 217.Fa flock 218structure. 219If no lock is found that would prevent this lock from being created, 220the structure is left unchanged by this function call except for the 221lock type which is set to 222.Dv F_UNLCK . 223.It Dv F_SETLK 224Set or clear a file segment lock according to the lock description 225pointed to by the third argument, 226.Fa arg , 227taken as a pointer to a 228.Fa "struct flock" 229(see above). 230.Dv F_SETLK 231is used to establish shared (or read) locks 232.Pq Dv F_RDLCK 233or exclusive (or write) locks 234.Pq Dv F_WRLCK , 235as well as remove either type of lock 236.Pq Dv F_UNLCK . 237If a shared or exclusive lock cannot be set, 238.Fn fcntl 239returns immediately with 240.Er EAGAIN . 241.It Dv F_SETLKW 242This command is the same as 243.Dv F_SETLK 244except that if a shared or exclusive lock is blocked by other locks, 245the process waits until the request can be satisfied. 246If a signal that is to be caught is received while 247.Fn fcntl 248is waiting for a region, the 249.Fn fcntl 250will be interrupted if the signal handler has not specified the 251.Dv SA_RESTART 252(see 253.Xr sigaction 2 ) . 254.El 255.Pp 256When a shared lock has been set on a segment of a file, 257other processes can set shared locks on that segment 258or a portion of it. 259A shared lock prevents any other process from setting an exclusive 260lock on any portion of the protected area. 261A request for a shared lock fails if the file descriptor was not 262opened with read access. 263.Pp 264An exclusive lock prevents any other process from setting a shared lock or 265an exclusive lock on any portion of the protected area. 266A request for an exclusive lock fails if the file was not 267opened with write access. 268.Pp 269The value of 270.Fa l_whence 271is 272.Dv SEEK_SET , 273.Dv SEEK_CUR , 274or 275.Dv SEEK_END 276to indicate that the relative offset, 277.Fa l_start 278bytes, will be measured from the start of the file, 279current position, or end of the file, respectively. 280The value of 281.Fa l_len 282is the number of consecutive bytes to be locked. 283If 284.Fa l_len 285is negative, the result is undefined. 286The 287.Fa l_pid 288field is only used with 289.Dv F_GETLK 290to return the process ID of the process holding a blocking lock. 291After a successful 292.Dv F_GETLK 293request, the value of 294.Fa l_whence 295is 296.Dv SEEK_SET . 297.Pp 298Locks may start and extend beyond the current end of a file, 299but may not start or extend before the beginning of the file. 300A lock is set to extend to the largest possible value of the 301file offset for that file if 302.Fa l_len 303is set to zero. 304If 305.Fa l_whence 306and 307.Fa l_start 308point to the beginning of the file, and 309.Fa l_len 310is zero, the entire file is locked. 311If an application wishes only to do entire file locking, the 312.Xr flock 2 313system call is much more efficient. 314.Pp 315There is at most one type of lock set for each byte in the file. 316Before a successful return from an 317.Dv F_SETLK 318or an 319.Dv F_SETLKW 320request when the calling process has previously existing locks 321on bytes in the region specified by the request, 322the previous lock type for each byte in the specified 323region is replaced by the new lock type. 324As specified above under the descriptions 325of shared locks and exclusive locks, an 326.Dv F_SETLK 327or an 328.Dv F_SETLKW 329request fails or blocks respectively when another process has existing 330locks on bytes in the specified region and the type of any of those 331locks conflicts with the type specified in the request. 332.Pp 333This interface follows the completely stupid semantics of System V and 334.St -p1003.1-88 335that require that all locks associated with a file for a given process are 336removed when 337.Em any 338file descriptor for that file is closed by that process. 339This semantic means that applications must be aware of any files that 340a subroutine library may access. 341For example if an application for updating the password file locks the 342password file database while making the update, and then calls 343.Xr getpwnam 3 344to retrieve a record, 345the lock will be lost because 346.Xr getpwnam 3 347opens, reads, and closes the password database. 348The database close will release all locks that the process has 349associated with the database, even if the library routine never 350requested a lock on the database. 351Another minor semantic problem with this interface is that 352locks are not inherited by a child process created using the 353.Xr fork 2 354function. 355The 356.Xr flock 2 357interface has much more rational last close semantics and 358allows locks to be inherited by child processes. 359.Xr flock 2 360is recommended for applications that want to ensure the integrity 361of their locks when using library routines or wish to pass locks 362to their children. 363Note that 364.Xr flock 2 365and 366.Fn fcntl 367locks may be safely used concurrently. 368.Pp 369All locks associated with a file for a given process are 370removed when the process terminates. 371.Pp 372A potential for deadlock occurs if a process controlling a locked region 373is put to sleep by attempting to lock the locked region of another process. 374This implementation detects that sleeping until a locked region is unlocked 375would cause a deadlock and fails with an 376.Er EDEADLK 377error. 378.Sh RETURN VALUES 379Upon successful completion, the value returned depends on 380.Fa cmd 381as follows: 382.Bl -tag -width F_GETOWNX -offset indent 383.It Dv F_DUPFD 384A new file descriptor. 385.It Dv F_GETFD 386Value of flag (only the low-order bit is defined). 387.It Dv F_GETFL 388Value of flags. 389.It Dv F_GETOWN 390Value of file descriptor owner. 391.It other 392Value other than \-1. 393.El 394.Pp 395Otherwise, a value of \-1 is returned and 396.Va errno 397is set to indicate the error. 398.Sh ERRORS 399.Fn fcntl 400will fail if: 401.Bl -tag -width Er 402.It Bq Er EAGAIN 403The argument 404.Fa cmd 405is 406.Dv F_SETLK , 407the type of lock 408.Pq Fa l_type 409is a shared lock 410.Pq Dv F_RDLCK 411or exclusive lock 412.Pq Dv F_WRLCK , 413and the segment of a file to be locked is already 414exclusive-locked by another process; 415or the type is an exclusive lock and some portion of the 416segment of a file to be locked is already shared-locked or 417exclusive-locked by another process. 418.It Bq Er EBADF 419.Fa fd 420is not a valid open file descriptor. 421.Pp 422The argument 423.Fa cmd 424is 425.Dv F_SETLK 426or 427.Dv F_SETLKW , 428the type of lock 429.Pq Fa l_type 430is a shared lock 431.Pq Dv F_RDLCK , 432and 433.Fa fd 434is not a valid file descriptor open for reading. 435.Pp 436The argument 437.Fa cmd 438is 439.Dv F_SETLK 440or 441.Dv F_SETLKW , 442the type of lock 443.Pq Fa l_type 444is an exclusive lock 445.Pq Dv F_WRLCK , 446and 447.Fa fd 448is not a valid file descriptor open for writing. 449.It Bq Er EDEADLK 450The argument 451.Fa cmd 452is 453.Dv F_SETLKW , 454and a deadlock condition was detected. 455.It Bq Er EINTR 456The argument 457.Fa cmd 458is 459.Dv F_SETLKW , 460and the function was interrupted by a signal. 461.It Bq Er EINVAL 462.Fa cmd 463is 464.Dv F_DUPFD 465and 466.Fa arg 467is negative or greater than the maximum allowable number 468(see 469.Xr getdtablesize 3 ) . 470.Pp 471The argument 472.Fa cmd 473is 474.Dv F_GETLK , 475.Dv F_SETLK , 476or 477.Dv F_SETLKW 478and the data to which 479.Fa arg 480points is not valid, or 481.Fa fd 482refers to a file that does not support locking. 483.It Bq Er EMFILE 484The argument 485.Fa cmd 486is 487.Dv F_DUPFD 488and the maximum number of open file descriptors permitted for the 489process are already in use, 490or no file descriptors greater than or equal to 491.Fa arg 492are available. 493.It Bq Er ENOLCK 494The argument 495.Fa cmd 496is 497.Dv F_SETLK 498or 499.Dv F_SETLKW , 500and satisfying the lock or unlock request would result in the 501number of locked regions in the system exceeding a system-imposed limit. 502.It Bq Er ESRCH 503.Fa cmd 504is 505.Dv F_SETOWN 506and the process ID given in 507.Fa arg 508is not in use. 509.El 510.Sh SEE ALSO 511.Xr close 2 , 512.Xr execve 2 , 513.Xr flock 2 , 514.Xr open 2 , 515.Xr sigaction 2 , 516.Xr getdtablesize 3 517.Sh HISTORY 518The 519.Fn fcntl 520function call appeared in 521.Bx 4.2 . 522