163769Sbostic.\" Copyright (c) 1983, 1993 263769Sbostic.\" The Regents of the University of California. All rights reserved. 320213Smckusick.\" 443568Strent.\" %sccs.include.redist.man% 520213Smckusick.\" 6*65702Smckusick.\" @(#)fcntl.2 8.2 (Berkeley) 01/12/94 738053Sbostic.\" 847208Scael.Dd 947208Scael.Dt FCNTL 2 1047208Scael.Os BSD 4.2 1147208Scael.Sh NAME 1247208Scael.Nm fcntl 1347208Scael.Nd file control 1447208Scael.Sh SYNOPSIS 1553065Sbostic.Fd #include <fcntl.h> 1647208Scael.Ft int 1747208Scael.Fn fcntl "int fd" "int cmd" "int arg" 1847208Scael.Sh DESCRIPTION 1947208Scael.Fn Fcntl 2020213Smckusickprovides for control over descriptors. 2120213SmckusickThe argument 2247208Scael.Fa fd 2320213Smckusickis a descriptor to be operated on by 2447208Scael.Fa cmd 2520213Smckusickas follows: 2647208Scael.Bl -tag -width F_GETOWNX 2747208Scael.It Dv F_DUPFD 2820213SmckusickReturn a new descriptor as follows: 2947208Scael.Pp 3047208Scael.Bl -bullet -compact -offset 4n 3147208Scael.It 3220213SmckusickLowest numbered available descriptor greater than or equal to 3347208Scael.Fa arg . 3447208Scael.It 3520213SmckusickSame object references as the original descriptor. 3647208Scael.It 3747208ScaelNew descriptor shares the same file offset if the object 3820213Smckusickwas a file. 3947208Scael.It 4020213SmckusickSame access mode (read, write or read/write). 4147208Scael.It 4220213SmckusickSame file status flags (i.e., both file descriptors 4320213Smckusickshare the same file status flags). 4447208Scael.It 4520213SmckusickThe close-on-exec flag associated with the new file descriptor 4620213Smckusickis set to remain open across 4747208Scael.Xr execv 2 4820213Smckusicksystem calls. 4947208Scael.El 5047208Scael.It Dv F_GETFD 5120213SmckusickGet the close-on-exec flag associated with the file descriptor 5247208Scael.Fa fd . 5347208ScaelIf the low-order bit of the returned value is 0, 5447208Scaelthe file will remain open across 5547208Scael.Fn exec , 5620213Smckusickotherwise the file will be closed upon execution of 5747208Scael.Fn exec 5847208Scael.Fa ( arg 5947208Scaelis ignored). 6047208Scael.It Dv F_SETFD 6120213SmckusickSet the close-on-exec flag associated with 6247208Scael.Fa fd 6320213Smckusickto the low order bit of 6447208Scael.Fa arg 6520213Smckusick(0 or 1 as above). 6647208Scael.It Dv F_GETFL 6747208ScaelGet descriptor status flags, as described below 6847208Scael.Fa ( arg 6947208Scaelis ignored). 7047208Scael.It Dv F_SETFL 7147208ScaelSet descriptor status flags to 7247208Scael.Fa arg . 7347208Scael.It Dv F_GETOWN 7420213SmckusickGet the process ID or process group 7547208Scaelcurrently receiving 7647208Scael.Dv SIGIO 7747208Scaeland 7847208Scael.Dv SIGURG 7920213Smckusicksignals; process groups are returned 8047208Scaelas negative values 8147208Scael.Fa ( arg 8247208Scaelis ignored). 8347208Scael.It Dv F_SETOWN 8420213SmckusickSet the process or process group 8547208Scaelto receive 8647208Scael.Dv SIGIO 8747208Scaeland 8847208Scael.Dv SIGURG 8947208Scaelsignals; 9020213Smckusickprocess groups are specified by supplying 9147208Scael.Fa arg 9220213Smckusickas negative, otherwise 9347208Scael.Fa arg 9420213Smckusickis interpreted as a process ID. 9547208Scael.El 9647208Scael.Pp 9747208ScaelThe flags for the 9847208Scael.Dv F_GETFL 9947208Scaeland 10047208Scael.Dv F_SETFL 10147208Scaelflags are as follows: 10263717Smckusick.Bl -tag -width O_NONBLOCKX 10353309Sbostic.It Dv O_NONBLOCK 10420213SmckusickNon-blocking I/O; if no data is available to a 10547208Scael.Xr read 10647208Scaelcall, or if a 10747208Scael.Xr write 10847208Scaeloperation would block, 10947208Scaelthe read or write call returns -1 with the error 11053310Sbostic.Er EAGAIN . 11147208Scael.It Dv O_APPEND 11220213SmckusickForce each write to append at the end of file; 11347208Scaelcorresponds to the 11447208Scael.Dv O_APPEND 11547208Scaelflag of 11647208Scael.Xr open 2 . 11747208Scael.It Dv O_ASYNC 11847208ScaelEnable the 11947208Scael.Dv SIGIO 12047208Scaelsignal to be sent to the process group 12128366Sannewhen I/O is possible, e.g., 12220213Smckusickupon availability of data to be read. 12347208Scael.El 12463717Smckusick.Pp 12563717SmckusickSeveral commands are available for doing advisory file locking; 12663717Smckusickthey all operate on the following structure: 127*65702Smckusick.ne 7v 12863717Smckusick.Bd -literal 12963717Smckusickstruct flock { 13063717Smckusick off_t l_start; /* starting offset */ 13163717Smckusick off_t l_len; /* len = 0 means until end of file */ 13263717Smckusick pid_t l_pid; /* lock owner */ 13363717Smckusick short l_type; /* lock type: read/write, etc. */ 13463717Smckusick short l_whence; /* type of l_start */ 13563717Smckusick}; 13663717Smckusick.Ed 13763717SmckusickThe commands available for advisory record locking are as follows: 13863717Smckusick.Bl -tag -width F_SETLKWX 13963717Smckusick.It Dv F_GETLK 14063717SmckusickGet the first lock that blocks the lock description pointed to by the 14163717Smckusickthird argument, 14263717Smckusick.Fa arg , 14363717Smckusicktaken as a pointer to a 14463717Smckusick.Fa "struct flock" 14563717Smckusick(see above). 14663717SmckusickThe information retrieved overwrites the information passed to 14763717Smckusick.Nm fcntl 14863717Smckusickin the 14963717Smckusick.Fa flock 15063717Smckusickstructure. 15163717SmckusickIf no lock is found that would prevent this lock from being created, 15263717Smckusickthe structure is left unchanged by this function call except for the 15363717Smckusicklock type which is set to 15463717Smckusick.Dv F_UNLCK . 15563717Smckusick.It Dv F_SETLK 15663717SmckusickSet or clear a file segment lock according to the lock description 15763717Smckusickpointed to by the third argument, 15863717Smckusick.Fa arg , 15963717Smckusicktaken as a pointer to a 16063717Smckusick.Fa "struct flock" 16163717Smckusick(see above). 16263717Smckusick.Dv F_SETLK 16363717Smckusickis used to establish shared (or read) locks 16463717Smckusick.Dv (F_RDLCK) 16563717Smckusickor exclusive (or write) locks, 16663717Smckusick.Dv (F_WRLCK) , 16763717Smckusickas well as remove either type of lock 16863717Smckusick.Dv (F_UNLCK) . 16963717SmckusickIf a shared or exclusive lock cannot be set, 17063717Smckusick.Nm fcntl 17163717Smckusickreturns immediately with 17263717Smckusick.Er EACCES . 17363717Smckusick.It Dv F_SETLKW 17463717SmckusickThis command is the same as 17563717Smckusick.Dv F_SETLK 17663717Smckusickexcept that if a shared or exclusive lock is blocked by other locks, 17763717Smckusickthe process waits until the request can be satisfied. 17863717SmckusickIf a signal that is to be caught is received while 17963717Smckusick.Nm fcntl 18063717Smckusickis waiting for a region, the 18163717Smckusick.Nm fcntl 18263717Smckusickwill be interrupted if the signal handler has not specified the 18363717Smckusick.Dv SA_RESTART 18463717Smckusick(see 18563717Smckusick.Xr sigaction 2 ) . 18663717Smckusick.El 18763717Smckusick.Pp 18863717SmckusickWhen a shared lock has been set on a segment of a file, 18963717Smckusickother processes can set shared locks on that segment 19063717Smckusickor a portion of it. 19163717SmckusickA shared lock prevents any other process from setting an exclusive 19263717Smckusicklock on any portion of the protected area. 19363717SmckusickA request for a shared lock fails if the file descriptor was not 19463717Smckusickopened with read access. 19563717Smckusick.Pp 19663717SmckusickAn exclusive lock prevents any other process from setting a shared lock or 19763717Smckusickan exclusive lock on any portion of the protected area. 19863717SmckusickA request for an exclusive lock fails if the file was not 19963717Smckusickopened with write access. 20063717Smckusick.Pp 20163717SmckusickThe value of 20263717Smckusick.Fa l_whence 20363717Smckusickis 20463717Smckusick.Dv SEEK_SET , 20563717Smckusick.Dv SEEK_CUR , 20663717Smckusickor 20763717Smckusick.Dv SEEK_END 20863717Smckusickto indicate that the relative offset, 20963717Smckusick.Fa l_start 21063717Smckusickbytes, will be measured from the start of the file, 21163717Smckusickcurrent position, or end of the file, respectively. 21263717SmckusickThe value of 21363717Smckusick.Fa l_len 21463717Smckusickis the number of consecutive bytes to be locked. 21563717SmckusickIf 21663717Smckusick.Fa l_len 21763717Smckusickis negative, the result is undefined. 21863717SmckusickThe 21963717Smckusick.Fa l_pid 22063717Smckusickfield is only used with 22163717Smckusick.Dv F_GETLK 22263717Smckusickto return the process ID of the process holding a blocking lock. 22363717SmckusickAfter a successful 22463717Smckusick.Dv F_GETLK 22563717Smckusickrequest, the value of 22663717Smckusick.Fa l_whence 22763717Smckusickis 22863717Smckusick.Dv SEEK_SET . 22963717Smckusick.Pp 23063717SmckusickLocks may start and extend beyond the current end of a file, 23163717Smckusickbut may not start or extend before the beginning of the file. 23263717SmckusickA lock is set to extend to the largest possible value of the 23363717Smckusickfile offset for that file if 23463717Smckusick.Fa l_len 23563717Smckusickis set to zero. If 23663717Smckusick.Fa l_whence 23763717Smckusickand 23863717Smckusick.Fa l_start 23963717Smckusickpoint to the beginning of the file, and 24063717Smckusick.Fa l_len 24163717Smckusickis zero, the entire file is locked. 24263717SmckusickIf an application wishes only to do entire file locking, the 24363717Smckusick.Xr flock 2 24463717Smckusicksystem call is much more efficient. 24563717Smckusick.Pp 24663717SmckusickThere is at most one type of lock set for each byte in the file. 24763717SmckusickBefore a successful return from an 24863717Smckusick.Dv F_SETLK 24963717Smckusickor an 25063717Smckusick.Dv F_SETLKW 25163717Smckusickrequest when the calling process has previously existing locks 25263717Smckusickon bytes in the region specified by the request, 25363717Smckusickthe previous lock type for each byte in the specified 25463717Smckusickregion is replaced by the new lock type. 25563717SmckusickAs specified above under the descriptions 25663717Smckusickof shared locks and exclusive locks, an 25763717Smckusick.Dv F_SETLK 25863717Smckusickor an 25963717Smckusick.Dv F_SETLKW 26063717Smckusickrequest fails or blocks respectively when another process has existing 26163717Smckusicklocks on bytes in the specified region and the type of any of those 26263717Smckusicklocks conflicts with the type specified in the request. 26363717Smckusick.Pp 26463717SmckusickThis interface follows the completely stupid semantics of System V and 26563717Smckusick.St -p1003.1-88 26663717Smckusickthat require that all locks associated with a file for a given process are 26763717Smckusickremoved when \fIany\fP file descriptor for that file is closed by that process. 26863717SmckusickThis semantic means that applications must be aware of any files that 26963717Smckusicka subroutine library may access. 27063717SmckusickFor example if an application for updating the password file locks the 27163717Smckusickpassword file database while making the update, and then calls 27263717Smckusick.Xr getpwname 3 27363717Smckusickto retrieve a record, 27463717Smckusickthe lock will be lost because 27563717Smckusick.Xr getpwname 3 27663717Smckusickopens, reads, and closes the password database. 27763717SmckusickThe database close will release all locks that the process has 27863717Smckusickassociated with the database, even if the library routine never 27963717Smckusickrequested a lock on the database. 28063717SmckusickAnother minor semantic problem with this interface is that 28163717Smckusicklocks are not inherited by a child process created using the 28263717Smckusick.Xr fork 2 28363717Smckusickfunction. 28463717SmckusickThe 28563717Smckusick.Xr flock 2 28663717Smckusickinterface has much more rational last close semantics and 28763717Smckusickallows locks to be inherited by child processes. 28863717Smckusick.Xr Flock 2 28963717Smckusickis recommended for applications that want to ensure the integrity 29063717Smckusickof their locks when using library routines or wish to pass locks 29163717Smckusickto their children. 29263717SmckusickNote that 29363717Smckusick.Xr flock 2 29463717Smckusickand 29563717Smckusick.Xr fcntl 2 29663717Smckusicklocks may be safely used concurrently. 29763717Smckusick.Pp 29863717SmckusickAll locks associated with a file for a given process are 29963717Smckusickremoved when the process terminates. 30063717Smckusick.Pp 30163717SmckusickA potential for deadlock occurs if a process controlling a locked region 30263717Smckusickis put to sleep by attempting to lock the locked region of another process. 30363717SmckusickThis implementation detects that sleeping until a locked region is unlocked 30463717Smckusickwould cause a deadlock and fails with an 30563717Smckusick.Er EDEADLK 30663717Smckusickerror. 30747208Scael.Sh RETURN VALUES 30820213SmckusickUpon successful completion, the value returned depends on 30947208Scael.Fa cmd 31020213Smckusickas follows: 31147208Scael.Bl -tag -width F_GETOWNX -offset indent 31247208Scael.It Dv F_DUPFD 31347208ScaelA new file descriptor. 31447208Scael.It Dv F_GETFD 31547208ScaelValue of flag (only the low-order bit is defined). 31647208Scael.It Dv F_GETFL 31747208ScaelValue of flags. 31847208Scael.It Dv F_GETOWN 31947208ScaelValue of file descriptor owner. 32047208Scael.It other 32147208ScaelValue other than -1. 32247208Scael.El 32347208Scael.Pp 32447208ScaelOtherwise, a value of -1 is returned and 32547208Scael.Va errno 32620213Smckusickis set to indicate the error. 32747208Scael.Sh ERRORS 32847208Scael.Fn Fcntl 32947208Scaelwill fail if: 33047208Scael.Bl -tag -width Er 33163717Smckusick.It Bq Er EACCES 33263717SmckusickThe argument 33363717Smckusick.Fa arg 33463717Smckusickis 33563717Smckusick.Dv F_SETLK , 33663717Smckusickthe type of lock 33763717Smckusick.Fa (l_type) 33863717Smckusickis a shared lock 33963717Smckusick.Dv (F_RDLCK) 34063717Smckusickor exclusive lock 34163717Smckusick.Dv (F_WRLCK) , 34263717Smckusickand the segment of a file to be locked is already 34363717Smckusickexclusive-locked by another process; 34463717Smckusickor the type is an exclusive lock and some portion of the 34563717Smckusicksegment of a file to be locked is already shared-locked or 34663717Smckusickexclusive-locked by another process. 34747208Scael.It Bq Er EBADF 34847208Scael.Fa Fildes 34920213Smckusickis not a valid open file descriptor. 35063717Smckusick.Pp 35163717SmckusickThe argument 35263717Smckusick.Fa cmd 35363717Smckusickis 35463717Smckusick.Dv F_SETLK 35563717Smckusickor 35663717Smckusick.Dv F_SETLKW , 35763717Smckusickthe type of lock 35863717Smckusick.Fa (l_type) 35963717Smckusickis a shared lock 36063717Smckusick.Dv (F_RDLCK) , 36163717Smckusickand 36263717Smckusick.Fa fildes 36363717Smckusickis not a valid file descriptor open for reading. 36463717Smckusick.Pp 36563717SmckusickThe argument 36663717Smckusick.Fa cmd 36763717Smckusickis 36863717Smckusick.Dv F_SETLK 36963717Smckusickor 37063717Smckusick.Dv F_SETLKW , 37163717Smckusickthe type of lock 37263717Smckusick.Fa (l_type) 37363717Smckusickis an exclusive lock 37463717Smckusick.Dv (F_WRLCK) , 37563717Smckusickand 37663717Smckusick.Fa fildes 37763717Smckusickis not a valid file descriptor open for writing. 37847208Scael.It Bq Er EMFILE 37947208Scael.Fa Cmd 38047208Scaelis 38147208Scael.Dv F_DUPFD 38247208Scaeland the maximum allowed number of file descriptors are currently 38320213Smckusickopen. 38463717Smckusick.It Bq Er EDEADLK 38563717SmckusickThe argument 38663717Smckusick.Fa cmd 38763717Smckusickis 38863717Smckusick.Dv F_SETLKW , 38963717Smckusickand a deadlock condition was detected. 39063717Smckusick.It Bq Er EINTR 39163717SmckusickThe argument 39263717Smckusick.Fa cmd 39363717Smckusickis 39463717Smckusick.Dv F_SETLKW , 39563717Smckusickand the function was interrupted by a signal. 39647208Scael.It Bq Er EINVAL 39747208Scael.Fa Cmd 39847208Scaelis 39947208Scael.Dv F_DUPFD 40047208Scaeland 40147208Scael.Fa arg 40228366Sanneis negative or greater than the maximum allowable number 40320213Smckusick(see 40447208Scael.Xr getdtablesize 2 ) . 40563717Smckusick.Pp 40663717SmckusickThe argument 40763717Smckusick.Fa cmd 40863717Smckusickis 40963717Smckusick.Dv F_GETLK , 41063717Smckusick.Dv F_SETLK , 41163717Smckusickor 41263717Smckusick.Dv F_SETLKW 41363717Smckusickand the data to which 41463717Smckusick.Fa arg 41563717Smckusickpoints is not valid, or 41663717Smckusick.Fa fildes 41763717Smckusickrefers to a file that does not support locking. 41863717Smckusick.It Bq Er EMFILE 41963717SmckusickThe argument 42063717Smckusick.Fa cmd 42163717Smckusickis 42263717Smckusick.Dv F_DUPED 42363717Smckusickand the maximum number of file descriptors permitted for the 42463717Smckusickprocess are already in use, 42563717Smckusickor no file descriptors greater than or equal to 42663717Smckusick.Fa arg 42763717Smckusickare available. 42863717Smckusick.It Bq Er ENOLCK 42963717SmckusickThe argument 43063717Smckusick.Fa cmd 43163717Smckusickis 43263717Smckusick.Dv F_SETLK 43363717Smckusickor 43463717Smckusick.Dv F_SETLKW , 43563717Smckusickand satisfying the lock or unlock request would result in the 43663717Smckusicknumber of locked regions in the system exceeding a system-imposed limit. 43747208Scael.It Bq Er ESRCH 43847208Scael.Fa Cmd 43947208Scaelis 44047208Scael.Dv F_SETOWN 44147208Scaeland 44228203Skarelsthe process ID given as argument is not in use. 44347208Scael.El 44447208Scael.Sh SEE ALSO 44547208Scael.Xr close 2 , 44647208Scael.Xr execve 2 , 44763717Smckusick.Xr flock 2 , 44847208Scael.Xr getdtablesize 2 , 44947208Scael.Xr open 2 , 45047208Scael.Xr sigvec 2 45147208Scael.Sh HISTORY 45247208ScaelThe 45347208Scael.Nm 45447208Scaelfunction call appeared in 45547208Scael.Bx 4.2 . 456