1*8269e767SBrooks Davis.\" Copyright (c) 1983, 1993 2*8269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 3*8269e767SBrooks Davis.\" 4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 6*8269e767SBrooks Davis.\" are met: 7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 8*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 10*8269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 11*8269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 13*8269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 14*8269e767SBrooks Davis.\" without specific prior written permission. 15*8269e767SBrooks Davis.\" 16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*8269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8269e767SBrooks Davis.\" SUCH DAMAGE. 27*8269e767SBrooks Davis.\" 28*8269e767SBrooks Davis.Dd December 7, 2021 29*8269e767SBrooks Davis.Dt FCNTL 2 30*8269e767SBrooks Davis.Os 31*8269e767SBrooks Davis.Sh NAME 32*8269e767SBrooks Davis.Nm fcntl 33*8269e767SBrooks Davis.Nd file control 34*8269e767SBrooks Davis.Sh LIBRARY 35*8269e767SBrooks Davis.Lb libc 36*8269e767SBrooks Davis.Sh SYNOPSIS 37*8269e767SBrooks Davis.In fcntl.h 38*8269e767SBrooks Davis.Ft int 39*8269e767SBrooks Davis.Fn fcntl "int fd" "int cmd" "..." 40*8269e767SBrooks Davis.Sh DESCRIPTION 41*8269e767SBrooks DavisThe 42*8269e767SBrooks Davis.Fn fcntl 43*8269e767SBrooks Davissystem call provides for control over descriptors. 44*8269e767SBrooks DavisThe argument 45*8269e767SBrooks Davis.Fa fd 46*8269e767SBrooks Davisis a descriptor to be operated on by 47*8269e767SBrooks Davis.Fa cmd 48*8269e767SBrooks Davisas described below. 49*8269e767SBrooks DavisDepending on the value of 50*8269e767SBrooks Davis.Fa cmd , 51*8269e767SBrooks Davis.Fn fcntl 52*8269e767SBrooks Daviscan take an additional third argument 53*8269e767SBrooks Davis.Fa arg . 54*8269e767SBrooks DavisUnless otherwise noted below for a specific operation, 55*8269e767SBrooks Davis.Fa arg 56*8269e767SBrooks Davishas type 57*8269e767SBrooks Davis.Vt int . 58*8269e767SBrooks Davis.Bl -tag -width F_DUP2FD_CLOEXEC 59*8269e767SBrooks Davis.It Dv F_DUPFD 60*8269e767SBrooks DavisReturn a new descriptor as follows: 61*8269e767SBrooks Davis.Pp 62*8269e767SBrooks Davis.Bl -bullet -compact -offset 4n 63*8269e767SBrooks Davis.It 64*8269e767SBrooks DavisLowest numbered available descriptor greater than or equal to 65*8269e767SBrooks Davis.Fa arg . 66*8269e767SBrooks Davis.It 67*8269e767SBrooks DavisSame object references as the original descriptor. 68*8269e767SBrooks Davis.It 69*8269e767SBrooks DavisNew descriptor shares the same file offset if the object 70*8269e767SBrooks Daviswas a file. 71*8269e767SBrooks Davis.It 72*8269e767SBrooks DavisSame access mode (read, write or read/write). 73*8269e767SBrooks Davis.It 74*8269e767SBrooks DavisSame file status flags (i.e., both file descriptors 75*8269e767SBrooks Davisshare the same file status flags). 76*8269e767SBrooks Davis.It 77*8269e767SBrooks DavisThe close-on-exec flag 78*8269e767SBrooks Davis.Dv FD_CLOEXEC 79*8269e767SBrooks Davisassociated with the new file descriptor is cleared, so the file descriptor is 80*8269e767SBrooks Davisto remain open across 81*8269e767SBrooks Davis.Xr execve 2 82*8269e767SBrooks Davissystem calls. 83*8269e767SBrooks Davis.El 84*8269e767SBrooks Davis.It Dv F_DUPFD_CLOEXEC 85*8269e767SBrooks DavisLike 86*8269e767SBrooks Davis.Dv F_DUPFD , 87*8269e767SBrooks Davisbut the 88*8269e767SBrooks Davis.Dv FD_CLOEXEC 89*8269e767SBrooks Davisflag associated with the new file descriptor is set, so the file descriptor 90*8269e767SBrooks Davisis closed when 91*8269e767SBrooks Davis.Xr execve 2 92*8269e767SBrooks Davissystem call executes. 93*8269e767SBrooks Davis.It Dv F_DUP2FD 94*8269e767SBrooks DavisIt is functionally equivalent to 95*8269e767SBrooks Davis.Bd -literal -offset indent 96*8269e767SBrooks Davisdup2(fd, arg) 97*8269e767SBrooks Davis.Ed 98*8269e767SBrooks Davis.It Dv F_DUP2FD_CLOEXEC 99*8269e767SBrooks DavisLike 100*8269e767SBrooks Davis.Dv F_DUP2FD , 101*8269e767SBrooks Davisbut the 102*8269e767SBrooks Davis.Dv FD_CLOEXEC 103*8269e767SBrooks Davisflag associated with the new file descriptor is set. 104*8269e767SBrooks Davis.Pp 105*8269e767SBrooks DavisThe 106*8269e767SBrooks Davis.Dv F_DUP2FD 107*8269e767SBrooks Davisand 108*8269e767SBrooks Davis.Dv F_DUP2FD_CLOEXEC 109*8269e767SBrooks Davisconstants are not portable, so they should not be used if 110*8269e767SBrooks Davisportability is needed. 111*8269e767SBrooks DavisUse 112*8269e767SBrooks Davis.Fn dup2 113*8269e767SBrooks Davisinstead of 114*8269e767SBrooks Davis.Dv F_DUP2FD . 115*8269e767SBrooks Davis.It Dv F_GETFD 116*8269e767SBrooks DavisGet the close-on-exec flag associated with the file descriptor 117*8269e767SBrooks Davis.Fa fd 118*8269e767SBrooks Davisas 119*8269e767SBrooks Davis.Dv FD_CLOEXEC . 120*8269e767SBrooks DavisIf the returned value ANDed with 121*8269e767SBrooks Davis.Dv FD_CLOEXEC 122*8269e767SBrooks Davisis 0, 123*8269e767SBrooks Davisthe file will remain open across 124*8269e767SBrooks Davis.Fn exec , 125*8269e767SBrooks Davisotherwise the file will be closed upon execution of 126*8269e767SBrooks Davis.Fn exec 127*8269e767SBrooks Davis.Fa ( arg 128*8269e767SBrooks Davisis ignored). 129*8269e767SBrooks Davis.It Dv F_SETFD 130*8269e767SBrooks DavisSet the close-on-exec flag associated with 131*8269e767SBrooks Davis.Fa fd 132*8269e767SBrooks Davisto 133*8269e767SBrooks Davis.Fa arg , 134*8269e767SBrooks Daviswhere 135*8269e767SBrooks Davis.Fa arg 136*8269e767SBrooks Davisis either 0 or 137*8269e767SBrooks Davis.Dv FD_CLOEXEC , 138*8269e767SBrooks Davisas described above. 139*8269e767SBrooks Davis.It Dv F_GETFL 140*8269e767SBrooks DavisGet descriptor status flags, as described below 141*8269e767SBrooks Davis.Fa ( arg 142*8269e767SBrooks Davisis ignored). 143*8269e767SBrooks Davis.It Dv F_SETFL 144*8269e767SBrooks DavisSet descriptor status flags to 145*8269e767SBrooks Davis.Fa arg . 146*8269e767SBrooks Davis.It Dv F_GETOWN 147*8269e767SBrooks DavisGet the process ID or process group 148*8269e767SBrooks Daviscurrently receiving 149*8269e767SBrooks Davis.Dv SIGIO 150*8269e767SBrooks Davisand 151*8269e767SBrooks Davis.Dv SIGURG 152*8269e767SBrooks Davissignals; process groups are returned 153*8269e767SBrooks Davisas negative values 154*8269e767SBrooks Davis.Fa ( arg 155*8269e767SBrooks Davisis ignored). 156*8269e767SBrooks Davis.It Dv F_SETOWN 157*8269e767SBrooks DavisSet the process or process group 158*8269e767SBrooks Davisto receive 159*8269e767SBrooks Davis.Dv SIGIO 160*8269e767SBrooks Davisand 161*8269e767SBrooks Davis.Dv SIGURG 162*8269e767SBrooks Davissignals; 163*8269e767SBrooks Davisprocess groups are specified by supplying 164*8269e767SBrooks Davis.Fa arg 165*8269e767SBrooks Davisas negative, otherwise 166*8269e767SBrooks Davis.Fa arg 167*8269e767SBrooks Davisis interpreted as a process ID. 168*8269e767SBrooks Davis.It Dv F_READAHEAD 169*8269e767SBrooks DavisSet or clear the read ahead amount for sequential access to the third 170*8269e767SBrooks Davisargument, 171*8269e767SBrooks Davis.Fa arg , 172*8269e767SBrooks Daviswhich is rounded up to the nearest block size. 173*8269e767SBrooks DavisA zero value in 174*8269e767SBrooks Davis.Fa arg 175*8269e767SBrooks Davisturns off read ahead, a negative value restores the system default. 176*8269e767SBrooks Davis.It Dv F_RDAHEAD 177*8269e767SBrooks DavisEquivalent to Darwin counterpart which sets read ahead amount of 128KB 178*8269e767SBrooks Daviswhen the third argument, 179*8269e767SBrooks Davis.Fa arg 180*8269e767SBrooks Davisis non-zero. 181*8269e767SBrooks DavisA zero value in 182*8269e767SBrooks Davis.Fa arg 183*8269e767SBrooks Davisturns off read ahead. 184*8269e767SBrooks Davis.It Dv F_ADD_SEALS 185*8269e767SBrooks DavisAdd seals to the file as described below, if the underlying filesystem supports 186*8269e767SBrooks Davisseals. 187*8269e767SBrooks Davis.It Dv F_GET_SEALS 188*8269e767SBrooks DavisGet seals associated with the file, if the underlying filesystem supports seals. 189*8269e767SBrooks Davis.It Dv F_ISUNIONSTACK 190*8269e767SBrooks DavisCheck if the vnode is part of a union stack (either the "union" flag from 191*8269e767SBrooks Davis.Xr mount 2 192*8269e767SBrooks Davisor unionfs). 193*8269e767SBrooks DavisThis is a hack not intended to be used outside of libc. 194*8269e767SBrooks Davis.It Dv F_KINFO 195*8269e767SBrooks DavisFills a 196*8269e767SBrooks Davis.Vt struct kinfo_file 197*8269e767SBrooks Davisfor the file referenced by the specified file descriptor. 198*8269e767SBrooks DavisThe 199*8269e767SBrooks Davis.Fa arg 200*8269e767SBrooks Davisargument should point to the storage for 201*8269e767SBrooks Davis.Vt struct kinfo_file . 202*8269e767SBrooks DavisThe 203*8269e767SBrooks Davis.Va kf_structsize 204*8269e767SBrooks Davismember of the passed structure must be initialized with the sizeof of 205*8269e767SBrooks Davis.Vt struct kinfo_file , 206*8269e767SBrooks Davisto allow for the interface versioning and evolution. 207*8269e767SBrooks Davis.El 208*8269e767SBrooks Davis.Pp 209*8269e767SBrooks DavisThe flags for the 210*8269e767SBrooks Davis.Dv F_GETFL 211*8269e767SBrooks Davisand 212*8269e767SBrooks Davis.Dv F_SETFL 213*8269e767SBrooks Daviscommands are as follows: 214*8269e767SBrooks Davis.Bl -tag -width O_NONBLOCKX 215*8269e767SBrooks Davis.It Dv O_NONBLOCK 216*8269e767SBrooks DavisNon-blocking I/O; if no data is available to a 217*8269e767SBrooks Davis.Xr read 2 218*8269e767SBrooks Davissystem call, or if a 219*8269e767SBrooks Davis.Xr write 2 220*8269e767SBrooks Davisoperation would block, 221*8269e767SBrooks Davisthe read or write call returns -1 with the error 222*8269e767SBrooks Davis.Er EAGAIN . 223*8269e767SBrooks Davis.It Dv O_APPEND 224*8269e767SBrooks DavisForce each write to append at the end of file; 225*8269e767SBrooks Daviscorresponds to the 226*8269e767SBrooks Davis.Dv O_APPEND 227*8269e767SBrooks Davisflag of 228*8269e767SBrooks Davis.Xr open 2 . 229*8269e767SBrooks Davis.It Dv O_DIRECT 230*8269e767SBrooks DavisMinimize or eliminate the cache effects of reading and writing. 231*8269e767SBrooks DavisThe system 232*8269e767SBrooks Daviswill attempt to avoid caching the data you read or write. 233*8269e767SBrooks DavisIf it cannot 234*8269e767SBrooks Davisavoid caching the data, it will minimize the impact the data has on the cache. 235*8269e767SBrooks DavisUse of this flag can drastically reduce performance if not used with care. 236*8269e767SBrooks Davis.It Dv O_ASYNC 237*8269e767SBrooks DavisEnable the 238*8269e767SBrooks Davis.Dv SIGIO 239*8269e767SBrooks Davissignal to be sent to the process group 240*8269e767SBrooks Daviswhen I/O is possible, e.g., 241*8269e767SBrooks Davisupon availability of data to be read. 242*8269e767SBrooks Davis.It Dv O_SYNC 243*8269e767SBrooks DavisEnable synchronous writes. 244*8269e767SBrooks DavisCorresponds to the 245*8269e767SBrooks Davis.Dv O_SYNC 246*8269e767SBrooks Davisflag of 247*8269e767SBrooks Davis.Xr open 2 . 248*8269e767SBrooks Davis.Dv O_FSYNC 249*8269e767SBrooks Davisis an historical synonym for 250*8269e767SBrooks Davis.Dv O_SYNC . 251*8269e767SBrooks Davis.It Dv O_DSYNC 252*8269e767SBrooks DavisEnable synchronous data writes. 253*8269e767SBrooks DavisCorresponds to the 254*8269e767SBrooks Davis.Dv O_DSYNC 255*8269e767SBrooks Davisflag of 256*8269e767SBrooks Davis.Xr open 2 . 257*8269e767SBrooks Davis.El 258*8269e767SBrooks Davis.Pp 259*8269e767SBrooks DavisThe seals that may be applied with 260*8269e767SBrooks Davis.Dv F_ADD_SEALS 261*8269e767SBrooks Davisare as follows: 262*8269e767SBrooks Davis.Bl -tag -width F_SEAL_SHRINK 263*8269e767SBrooks Davis.It Dv F_SEAL_SEAL 264*8269e767SBrooks DavisPrevent any further seals from being applied to the file. 265*8269e767SBrooks Davis.It Dv F_SEAL_SHRINK 266*8269e767SBrooks DavisPrevent the file from being shrunk with 267*8269e767SBrooks Davis.Xr ftruncate 2 . 268*8269e767SBrooks Davis.It Dv F_SEAL_GROW 269*8269e767SBrooks DavisPrevent the file from being enlarged with 270*8269e767SBrooks Davis.Xr ftruncate 2 . 271*8269e767SBrooks Davis.It Dv F_SEAL_WRITE 272*8269e767SBrooks DavisPrevent any further 273*8269e767SBrooks Davis.Xr write 2 274*8269e767SBrooks Daviscalls to the file. 275*8269e767SBrooks DavisAny writes in progress will finish before 276*8269e767SBrooks Davis.Fn fcntl 277*8269e767SBrooks Davisreturns. 278*8269e767SBrooks DavisIf any writeable mappings exist, F_ADD_SEALS will fail and return 279*8269e767SBrooks Davis.Dv EBUSY . 280*8269e767SBrooks Davis.El 281*8269e767SBrooks Davis.Pp 282*8269e767SBrooks DavisSeals are on a per-inode basis and require support by the underlying filesystem. 283*8269e767SBrooks DavisIf the underlying filesystem does not support seals, 284*8269e767SBrooks Davis.Dv F_ADD_SEALS 285*8269e767SBrooks Davisand 286*8269e767SBrooks Davis.Dv F_GET_SEALS 287*8269e767SBrooks Daviswill fail and return 288*8269e767SBrooks Davis.Dv EINVAL . 289*8269e767SBrooks Davis.Pp 290*8269e767SBrooks DavisSeveral operations are available for doing advisory file locking; 291*8269e767SBrooks Davisthey all operate on the following structure: 292*8269e767SBrooks Davis.Bd -literal 293*8269e767SBrooks Davisstruct flock { 294*8269e767SBrooks Davis off_t l_start; /* starting offset */ 295*8269e767SBrooks Davis off_t l_len; /* len = 0 means until end of file */ 296*8269e767SBrooks Davis pid_t l_pid; /* lock owner */ 297*8269e767SBrooks Davis short l_type; /* lock type: read/write, etc. */ 298*8269e767SBrooks Davis short l_whence; /* type of l_start */ 299*8269e767SBrooks Davis int l_sysid; /* remote system id or zero for local */ 300*8269e767SBrooks Davis}; 301*8269e767SBrooks Davis.Ed 302*8269e767SBrooks DavisThese advisory file locking operations take a pointer to 303*8269e767SBrooks Davis.Vt struct flock 304*8269e767SBrooks Davisas the third argument 305*8269e767SBrooks Davis.Fa arg . 306*8269e767SBrooks DavisThe commands available for advisory record locking are as follows: 307*8269e767SBrooks Davis.Bl -tag -width F_SETLKWX 308*8269e767SBrooks Davis.It Dv F_GETLK 309*8269e767SBrooks DavisGet the first lock that blocks the lock description pointed to by the 310*8269e767SBrooks Davisthird argument, 311*8269e767SBrooks Davis.Fa arg , 312*8269e767SBrooks Davistaken as a pointer to a 313*8269e767SBrooks Davis.Fa "struct flock" 314*8269e767SBrooks Davis(see above). 315*8269e767SBrooks DavisThe information retrieved overwrites the information passed to 316*8269e767SBrooks Davis.Fn fcntl 317*8269e767SBrooks Davisin the 318*8269e767SBrooks Davis.Fa flock 319*8269e767SBrooks Davisstructure. 320*8269e767SBrooks DavisIf no lock is found that would prevent this lock from being created, 321*8269e767SBrooks Davisthe structure is left unchanged by this system call except for the 322*8269e767SBrooks Davislock type which is set to 323*8269e767SBrooks Davis.Dv F_UNLCK . 324*8269e767SBrooks Davis.It Dv F_SETLK 325*8269e767SBrooks DavisSet or clear a file segment lock according to the lock description 326*8269e767SBrooks Davispointed to by the third argument, 327*8269e767SBrooks Davis.Fa arg , 328*8269e767SBrooks Davistaken as a pointer to a 329*8269e767SBrooks Davis.Fa "struct flock" 330*8269e767SBrooks Davis(see above). 331*8269e767SBrooks Davis.Dv F_SETLK 332*8269e767SBrooks Davisis used to establish shared (or read) locks 333*8269e767SBrooks Davis.Pq Dv F_RDLCK 334*8269e767SBrooks Davisor exclusive (or write) locks, 335*8269e767SBrooks Davis.Pq Dv F_WRLCK , 336*8269e767SBrooks Davisas well as remove either type of lock 337*8269e767SBrooks Davis.Pq Dv F_UNLCK . 338*8269e767SBrooks DavisIf a shared or exclusive lock cannot be set, 339*8269e767SBrooks Davis.Fn fcntl 340*8269e767SBrooks Davisreturns immediately with 341*8269e767SBrooks Davis.Er EAGAIN . 342*8269e767SBrooks Davis.It Dv F_SETLKW 343*8269e767SBrooks DavisThis command is the same as 344*8269e767SBrooks Davis.Dv F_SETLK 345*8269e767SBrooks Davisexcept that if a shared or exclusive lock is blocked by other locks, 346*8269e767SBrooks Davisthe process waits until the request can be satisfied. 347*8269e767SBrooks DavisIf a signal that is to be caught is received while 348*8269e767SBrooks Davis.Fn fcntl 349*8269e767SBrooks Davisis waiting for a region, the 350*8269e767SBrooks Davis.Fn fcntl 351*8269e767SBrooks Daviswill be interrupted if the signal handler has not specified the 352*8269e767SBrooks Davis.Dv SA_RESTART 353*8269e767SBrooks Davis(see 354*8269e767SBrooks Davis.Xr sigaction 2 ) . 355*8269e767SBrooks Davis.El 356*8269e767SBrooks Davis.Pp 357*8269e767SBrooks DavisWhen a shared lock has been set on a segment of a file, 358*8269e767SBrooks Davisother processes can set shared locks on that segment 359*8269e767SBrooks Davisor a portion of it. 360*8269e767SBrooks DavisA shared lock prevents any other process from setting an exclusive 361*8269e767SBrooks Davislock on any portion of the protected area. 362*8269e767SBrooks DavisA request for a shared lock fails if the file descriptor was not 363*8269e767SBrooks Davisopened with read access. 364*8269e767SBrooks Davis.Pp 365*8269e767SBrooks DavisAn exclusive lock prevents any other process from setting a shared lock or 366*8269e767SBrooks Davisan exclusive lock on any portion of the protected area. 367*8269e767SBrooks DavisA request for an exclusive lock fails if the file was not 368*8269e767SBrooks Davisopened with write access. 369*8269e767SBrooks Davis.Pp 370*8269e767SBrooks DavisThe value of 371*8269e767SBrooks Davis.Fa l_whence 372*8269e767SBrooks Davisis 373*8269e767SBrooks Davis.Dv SEEK_SET , 374*8269e767SBrooks Davis.Dv SEEK_CUR , 375*8269e767SBrooks Davisor 376*8269e767SBrooks Davis.Dv SEEK_END 377*8269e767SBrooks Davisto indicate that the relative offset, 378*8269e767SBrooks Davis.Fa l_start 379*8269e767SBrooks Davisbytes, will be measured from the start of the file, 380*8269e767SBrooks Daviscurrent position, or end of the file, respectively. 381*8269e767SBrooks DavisThe value of 382*8269e767SBrooks Davis.Fa l_len 383*8269e767SBrooks Davisis the number of consecutive bytes to be locked. 384*8269e767SBrooks DavisIf 385*8269e767SBrooks Davis.Fa l_len 386*8269e767SBrooks Davisis negative, 387*8269e767SBrooks Davis.Fa l_start 388*8269e767SBrooks Davismeans end edge of the region. 389*8269e767SBrooks DavisThe 390*8269e767SBrooks Davis.Fa l_pid 391*8269e767SBrooks Davisand 392*8269e767SBrooks Davis.Fa l_sysid 393*8269e767SBrooks Davisfields are only used with 394*8269e767SBrooks Davis.Dv F_GETLK 395*8269e767SBrooks Davisto return the process ID of the process holding a blocking lock and 396*8269e767SBrooks Davisthe system ID of the system that owns that process. 397*8269e767SBrooks DavisLocks created by the local system will have a system ID of zero. 398*8269e767SBrooks DavisAfter a successful 399*8269e767SBrooks Davis.Dv F_GETLK 400*8269e767SBrooks Davisrequest, the value of 401*8269e767SBrooks Davis.Fa l_whence 402*8269e767SBrooks Davisis 403*8269e767SBrooks Davis.Dv SEEK_SET . 404*8269e767SBrooks Davis.Pp 405*8269e767SBrooks DavisLocks may start and extend beyond the current end of a file, 406*8269e767SBrooks Davisbut may not start or extend before the beginning of the file. 407*8269e767SBrooks DavisA lock is set to extend to the largest possible value of the 408*8269e767SBrooks Davisfile offset for that file if 409*8269e767SBrooks Davis.Fa l_len 410*8269e767SBrooks Davisis set to zero. 411*8269e767SBrooks DavisIf 412*8269e767SBrooks Davis.Fa l_whence 413*8269e767SBrooks Davisand 414*8269e767SBrooks Davis.Fa l_start 415*8269e767SBrooks Davispoint to the beginning of the file, and 416*8269e767SBrooks Davis.Fa l_len 417*8269e767SBrooks Davisis zero, the entire file is locked. 418*8269e767SBrooks DavisIf an application wishes only to do entire file locking, the 419*8269e767SBrooks Davis.Xr flock 2 420*8269e767SBrooks Davissystem call is much more efficient. 421*8269e767SBrooks Davis.Pp 422*8269e767SBrooks DavisThere is at most one type of lock set for each byte in the file. 423*8269e767SBrooks DavisBefore a successful return from an 424*8269e767SBrooks Davis.Dv F_SETLK 425*8269e767SBrooks Davisor an 426*8269e767SBrooks Davis.Dv F_SETLKW 427*8269e767SBrooks Davisrequest when the calling process has previously existing locks 428*8269e767SBrooks Davison bytes in the region specified by the request, 429*8269e767SBrooks Davisthe previous lock type for each byte in the specified 430*8269e767SBrooks Davisregion is replaced by the new lock type. 431*8269e767SBrooks DavisAs specified above under the descriptions 432*8269e767SBrooks Davisof shared locks and exclusive locks, an 433*8269e767SBrooks Davis.Dv F_SETLK 434*8269e767SBrooks Davisor an 435*8269e767SBrooks Davis.Dv F_SETLKW 436*8269e767SBrooks Davisrequest fails or blocks respectively when another process has existing 437*8269e767SBrooks Davislocks on bytes in the specified region and the type of any of those 438*8269e767SBrooks Davislocks conflicts with the type specified in the request. 439*8269e767SBrooks Davis.Pp 440*8269e767SBrooks DavisThe queuing for 441*8269e767SBrooks Davis.Dv F_SETLKW 442*8269e767SBrooks Davisrequests on local files is fair; 443*8269e767SBrooks Davisthat is, while the thread is blocked, 444*8269e767SBrooks Davissubsequent requests conflicting with its requests will not be granted, 445*8269e767SBrooks Daviseven if these requests do not conflict with existing locks. 446*8269e767SBrooks Davis.Pp 447*8269e767SBrooks DavisThis interface follows the completely stupid semantics of System V and 448*8269e767SBrooks Davis.St -p1003.1-88 449*8269e767SBrooks Davisthat require that all locks associated with a file for a given process are 450*8269e767SBrooks Davisremoved when 451*8269e767SBrooks Davis.Em any 452*8269e767SBrooks Davisfile descriptor for that file is closed by that process. 453*8269e767SBrooks DavisThis semantic means that applications must be aware of any files that 454*8269e767SBrooks Davisa subroutine library may access. 455*8269e767SBrooks DavisFor example if an application for updating the password file locks the 456*8269e767SBrooks Davispassword file database while making the update, and then calls 457*8269e767SBrooks Davis.Xr getpwnam 3 458*8269e767SBrooks Davisto retrieve a record, 459*8269e767SBrooks Davisthe lock will be lost because 460*8269e767SBrooks Davis.Xr getpwnam 3 461*8269e767SBrooks Davisopens, reads, and closes the password database. 462*8269e767SBrooks DavisThe database close will release all locks that the process has 463*8269e767SBrooks Davisassociated with the database, even if the library routine never 464*8269e767SBrooks Davisrequested a lock on the database. 465*8269e767SBrooks DavisAnother minor semantic problem with this interface is that 466*8269e767SBrooks Davislocks are not inherited by a child process created using the 467*8269e767SBrooks Davis.Xr fork 2 468*8269e767SBrooks Davissystem call. 469*8269e767SBrooks DavisThe 470*8269e767SBrooks Davis.Xr flock 2 471*8269e767SBrooks Davisinterface has much more rational last close semantics and 472*8269e767SBrooks Davisallows locks to be inherited by child processes. 473*8269e767SBrooks DavisThe 474*8269e767SBrooks Davis.Xr flock 2 475*8269e767SBrooks Davissystem call is recommended for applications that want to ensure the integrity 476*8269e767SBrooks Davisof their locks when using library routines or wish to pass locks 477*8269e767SBrooks Davisto their children. 478*8269e767SBrooks Davis.Pp 479*8269e767SBrooks DavisThe 480*8269e767SBrooks Davis.Fn fcntl , 481*8269e767SBrooks Davis.Xr flock 2 , 482*8269e767SBrooks Davisand 483*8269e767SBrooks Davis.Xr lockf 3 484*8269e767SBrooks Davislocks are compatible. 485*8269e767SBrooks DavisProcesses using different locking interfaces can cooperate 486*8269e767SBrooks Davisover the same file safely. 487*8269e767SBrooks DavisHowever, only one of such interfaces should be used within 488*8269e767SBrooks Davisthe same process. 489*8269e767SBrooks DavisIf a file is locked by a process through 490*8269e767SBrooks Davis.Xr flock 2 , 491*8269e767SBrooks Davisany record within the file will be seen as locked 492*8269e767SBrooks Davisfrom the viewpoint of another process using 493*8269e767SBrooks Davis.Fn fcntl 494*8269e767SBrooks Davisor 495*8269e767SBrooks Davis.Xr lockf 3 , 496*8269e767SBrooks Davisand vice versa. 497*8269e767SBrooks DavisNote that 498*8269e767SBrooks Davis.Fn fcntl F_GETLK 499*8269e767SBrooks Davisreturns \-1 in 500*8269e767SBrooks Davis.Fa l_pid 501*8269e767SBrooks Davisif the process holding a blocking lock previously locked the 502*8269e767SBrooks Davisfile descriptor by 503*8269e767SBrooks Davis.Xr flock 2 . 504*8269e767SBrooks Davis.Pp 505*8269e767SBrooks DavisAll locks associated with a file for a given process are 506*8269e767SBrooks Davisremoved when the process terminates. 507*8269e767SBrooks Davis.Pp 508*8269e767SBrooks DavisAll locks obtained before a call to 509*8269e767SBrooks Davis.Xr execve 2 510*8269e767SBrooks Davisremain in effect until the new program releases them. 511*8269e767SBrooks DavisIf the new program does not know about the locks, they will not be 512*8269e767SBrooks Davisreleased until the program exits. 513*8269e767SBrooks Davis.Pp 514*8269e767SBrooks DavisA potential for deadlock occurs if a process controlling a locked region 515*8269e767SBrooks Davisis put to sleep by attempting to lock the locked region of another process. 516*8269e767SBrooks DavisThis implementation detects that sleeping until a locked region is unlocked 517*8269e767SBrooks Daviswould cause a deadlock and fails with an 518*8269e767SBrooks Davis.Er EDEADLK 519*8269e767SBrooks Daviserror. 520*8269e767SBrooks Davis.Sh RETURN VALUES 521*8269e767SBrooks DavisUpon successful completion, the value returned depends on 522*8269e767SBrooks Davis.Fa cmd 523*8269e767SBrooks Davisas follows: 524*8269e767SBrooks Davis.Bl -tag -width F_GETOWNX -offset indent 525*8269e767SBrooks Davis.It Dv F_DUPFD 526*8269e767SBrooks DavisA new file descriptor. 527*8269e767SBrooks Davis.It Dv F_DUP2FD 528*8269e767SBrooks DavisA file descriptor equal to 529*8269e767SBrooks Davis.Fa arg . 530*8269e767SBrooks Davis.It Dv F_GETFD 531*8269e767SBrooks DavisValue of flag (only the low-order bit is defined). 532*8269e767SBrooks Davis.It Dv F_GETFL 533*8269e767SBrooks DavisValue of flags. 534*8269e767SBrooks Davis.It Dv F_GETOWN 535*8269e767SBrooks DavisValue of file descriptor owner. 536*8269e767SBrooks Davis.It other 537*8269e767SBrooks DavisValue other than -1. 538*8269e767SBrooks Davis.El 539*8269e767SBrooks Davis.Pp 540*8269e767SBrooks DavisOtherwise, a value of -1 is returned and 541*8269e767SBrooks Davis.Va errno 542*8269e767SBrooks Davisis set to indicate the error. 543*8269e767SBrooks Davis.Sh ERRORS 544*8269e767SBrooks DavisThe 545*8269e767SBrooks Davis.Fn fcntl 546*8269e767SBrooks Davissystem call will fail if: 547*8269e767SBrooks Davis.Bl -tag -width Er 548*8269e767SBrooks Davis.It Bq Er EAGAIN 549*8269e767SBrooks DavisThe argument 550*8269e767SBrooks Davis.Fa cmd 551*8269e767SBrooks Davisis 552*8269e767SBrooks Davis.Dv F_SETLK , 553*8269e767SBrooks Davisthe type of lock 554*8269e767SBrooks Davis.Pq Fa l_type 555*8269e767SBrooks Davisis a shared lock 556*8269e767SBrooks Davis.Pq Dv F_RDLCK 557*8269e767SBrooks Davisor exclusive lock 558*8269e767SBrooks Davis.Pq Dv F_WRLCK , 559*8269e767SBrooks Davisand the segment of a file to be locked is already 560*8269e767SBrooks Davisexclusive-locked by another process; 561*8269e767SBrooks Davisor the type is an exclusive lock and some portion of the 562*8269e767SBrooks Davissegment of a file to be locked is already shared-locked or 563*8269e767SBrooks Davisexclusive-locked by another process. 564*8269e767SBrooks Davis.It Bq Er EBADF 565*8269e767SBrooks DavisThe 566*8269e767SBrooks Davis.Fa fd 567*8269e767SBrooks Davisargument 568*8269e767SBrooks Davisis not a valid open file descriptor. 569*8269e767SBrooks Davis.Pp 570*8269e767SBrooks DavisThe argument 571*8269e767SBrooks Davis.Fa cmd 572*8269e767SBrooks Davisis 573*8269e767SBrooks Davis.Dv F_DUP2FD , 574*8269e767SBrooks Davisand 575*8269e767SBrooks Davis.Fa arg 576*8269e767SBrooks Davisis not a valid file descriptor. 577*8269e767SBrooks Davis.Pp 578*8269e767SBrooks DavisThe argument 579*8269e767SBrooks Davis.Fa cmd 580*8269e767SBrooks Davisis 581*8269e767SBrooks Davis.Dv F_SETLK 582*8269e767SBrooks Davisor 583*8269e767SBrooks Davis.Dv F_SETLKW , 584*8269e767SBrooks Davisthe type of lock 585*8269e767SBrooks Davis.Pq Fa l_type 586*8269e767SBrooks Davisis a shared lock 587*8269e767SBrooks Davis.Pq Dv F_RDLCK , 588*8269e767SBrooks Davisand 589*8269e767SBrooks Davis.Fa fd 590*8269e767SBrooks Davisis not a valid file descriptor open for reading. 591*8269e767SBrooks Davis.Pp 592*8269e767SBrooks DavisThe argument 593*8269e767SBrooks Davis.Fa cmd 594*8269e767SBrooks Davisis 595*8269e767SBrooks Davis.Dv F_SETLK 596*8269e767SBrooks Davisor 597*8269e767SBrooks Davis.Dv F_SETLKW , 598*8269e767SBrooks Davisthe type of lock 599*8269e767SBrooks Davis.Pq Fa l_type 600*8269e767SBrooks Davisis an exclusive lock 601*8269e767SBrooks Davis.Pq Dv F_WRLCK , 602*8269e767SBrooks Davisand 603*8269e767SBrooks Davis.Fa fd 604*8269e767SBrooks Davisis not a valid file descriptor open for writing. 605*8269e767SBrooks Davis.It Bq Er EBUSY 606*8269e767SBrooks DavisThe argument 607*8269e767SBrooks Davis.Fa cmd 608*8269e767SBrooks Davisis 609*8269e767SBrooks Davis.Dv F_ADD_SEALS , 610*8269e767SBrooks Davisattempting to set 611*8269e767SBrooks Davis.Dv F_SEAL_WRITE , 612*8269e767SBrooks Davisand writeable mappings of the file exist. 613*8269e767SBrooks Davis.It Bq Er EDEADLK 614*8269e767SBrooks DavisThe argument 615*8269e767SBrooks Davis.Fa cmd 616*8269e767SBrooks Davisis 617*8269e767SBrooks Davis.Dv F_SETLKW , 618*8269e767SBrooks Davisand a deadlock condition was detected. 619*8269e767SBrooks Davis.It Bq Er EINTR 620*8269e767SBrooks DavisThe argument 621*8269e767SBrooks Davis.Fa cmd 622*8269e767SBrooks Davisis 623*8269e767SBrooks Davis.Dv F_SETLKW , 624*8269e767SBrooks Davisand the system call was interrupted by a signal. 625*8269e767SBrooks Davis.It Bq Er EINVAL 626*8269e767SBrooks DavisThe 627*8269e767SBrooks Davis.Fa cmd 628*8269e767SBrooks Davisargument 629*8269e767SBrooks Davisis 630*8269e767SBrooks Davis.Dv F_DUPFD 631*8269e767SBrooks Davisand 632*8269e767SBrooks Davis.Fa arg 633*8269e767SBrooks Davisis negative or greater than the maximum allowable number 634*8269e767SBrooks Davis(see 635*8269e767SBrooks Davis.Xr getdtablesize 2 ) . 636*8269e767SBrooks Davis.Pp 637*8269e767SBrooks DavisThe argument 638*8269e767SBrooks Davis.Fa cmd 639*8269e767SBrooks Davisis 640*8269e767SBrooks Davis.Dv F_GETLK , 641*8269e767SBrooks Davis.Dv F_SETLK 642*8269e767SBrooks Davisor 643*8269e767SBrooks Davis.Dv F_SETLKW 644*8269e767SBrooks Davisand the data to which 645*8269e767SBrooks Davis.Fa arg 646*8269e767SBrooks Davispoints is not valid. 647*8269e767SBrooks Davis.Pp 648*8269e767SBrooks DavisThe argument 649*8269e767SBrooks Davis.Fa cmd 650*8269e767SBrooks Davisis 651*8269e767SBrooks Davis.Dv F_ADD_SEALS 652*8269e767SBrooks Davisor 653*8269e767SBrooks Davis.Dv F_GET_SEALS , 654*8269e767SBrooks Davisand the underlying filesystem does not support sealing. 655*8269e767SBrooks Davis.Pp 656*8269e767SBrooks DavisThe argument 657*8269e767SBrooks Davis.Fa cmd 658*8269e767SBrooks Davisis invalid. 659*8269e767SBrooks Davis.It Bq Er EMFILE 660*8269e767SBrooks DavisThe argument 661*8269e767SBrooks Davis.Fa cmd 662*8269e767SBrooks Davisis 663*8269e767SBrooks Davis.Dv F_DUPFD 664*8269e767SBrooks Davisand the maximum number of file descriptors permitted for the 665*8269e767SBrooks Davisprocess are already in use, 666*8269e767SBrooks Davisor no file descriptors greater than or equal to 667*8269e767SBrooks Davis.Fa arg 668*8269e767SBrooks Davisare available. 669*8269e767SBrooks Davis.It Bq Er ENOTTY 670*8269e767SBrooks DavisThe 671*8269e767SBrooks Davis.Fa fd 672*8269e767SBrooks Davisargument is not a valid file descriptor for the requested operation. 673*8269e767SBrooks DavisThis may be the case if 674*8269e767SBrooks Davis.Fa fd 675*8269e767SBrooks Davisis a device node, or a descriptor returned by 676*8269e767SBrooks Davis.Xr kqueue 2 . 677*8269e767SBrooks Davis.It Bq Er ENOLCK 678*8269e767SBrooks DavisThe argument 679*8269e767SBrooks Davis.Fa cmd 680*8269e767SBrooks Davisis 681*8269e767SBrooks Davis.Dv F_SETLK 682*8269e767SBrooks Davisor 683*8269e767SBrooks Davis.Dv F_SETLKW , 684*8269e767SBrooks Davisand satisfying the lock or unlock request would result in the 685*8269e767SBrooks Davisnumber of locked regions in the system exceeding a system-imposed limit. 686*8269e767SBrooks Davis.It Bq Er EOPNOTSUPP 687*8269e767SBrooks DavisThe argument 688*8269e767SBrooks Davis.Fa cmd 689*8269e767SBrooks Davisis 690*8269e767SBrooks Davis.Dv F_GETLK , 691*8269e767SBrooks Davis.Dv F_SETLK 692*8269e767SBrooks Davisor 693*8269e767SBrooks Davis.Dv F_SETLKW 694*8269e767SBrooks Davisand 695*8269e767SBrooks Davis.Fa fd 696*8269e767SBrooks Davisrefers to a file for which locking is not supported. 697*8269e767SBrooks Davis.It Bq Er EOVERFLOW 698*8269e767SBrooks DavisThe argument 699*8269e767SBrooks Davis.Fa cmd 700*8269e767SBrooks Davisis 701*8269e767SBrooks Davis.Dv F_GETLK , 702*8269e767SBrooks Davis.Dv F_SETLK 703*8269e767SBrooks Davisor 704*8269e767SBrooks Davis.Dv F_SETLKW 705*8269e767SBrooks Davisand an 706*8269e767SBrooks Davis.Fa off_t 707*8269e767SBrooks Daviscalculation overflowed. 708*8269e767SBrooks Davis.It Bq Er EPERM 709*8269e767SBrooks DavisThe 710*8269e767SBrooks Davis.Fa cmd 711*8269e767SBrooks Davisargument 712*8269e767SBrooks Davisis 713*8269e767SBrooks Davis.Dv F_SETOWN 714*8269e767SBrooks Davisand 715*8269e767SBrooks Davisthe process ID or process group given as an argument is in a 716*8269e767SBrooks Davisdifferent session than the caller. 717*8269e767SBrooks Davis.Pp 718*8269e767SBrooks DavisThe 719*8269e767SBrooks Davis.Fa cmd 720*8269e767SBrooks Davisargument 721*8269e767SBrooks Davisis 722*8269e767SBrooks Davis.Dv F_ADD_SEALS 723*8269e767SBrooks Davisand the 724*8269e767SBrooks Davis.Dv F_SEAL_SEAL 725*8269e767SBrooks Davisseal has already been set. 726*8269e767SBrooks Davis.It Bq Er ESRCH 727*8269e767SBrooks DavisThe 728*8269e767SBrooks Davis.Fa cmd 729*8269e767SBrooks Davisargument 730*8269e767SBrooks Davisis 731*8269e767SBrooks Davis.Dv F_SETOWN 732*8269e767SBrooks Davisand 733*8269e767SBrooks Davisthe process ID given as argument is not in use. 734*8269e767SBrooks Davis.El 735*8269e767SBrooks Davis.Pp 736*8269e767SBrooks DavisIn addition, if 737*8269e767SBrooks Davis.Fa fd 738*8269e767SBrooks Davisrefers to a descriptor open on a terminal device (as opposed to a 739*8269e767SBrooks Davisdescriptor open on a socket), a 740*8269e767SBrooks Davis.Fa cmd 741*8269e767SBrooks Davisof 742*8269e767SBrooks Davis.Dv F_SETOWN 743*8269e767SBrooks Daviscan fail for the same reasons as in 744*8269e767SBrooks Davis.Xr tcsetpgrp 3 , 745*8269e767SBrooks Davisand a 746*8269e767SBrooks Davis.Fa cmd 747*8269e767SBrooks Davisof 748*8269e767SBrooks Davis.Dv F_GETOWN 749*8269e767SBrooks Davisfor the reasons as stated in 750*8269e767SBrooks Davis.Xr tcgetpgrp 3 . 751*8269e767SBrooks Davis.Sh SEE ALSO 752*8269e767SBrooks Davis.Xr close 2 , 753*8269e767SBrooks Davis.Xr dup2 2 , 754*8269e767SBrooks Davis.Xr execve 2 , 755*8269e767SBrooks Davis.Xr flock 2 , 756*8269e767SBrooks Davis.Xr getdtablesize 2 , 757*8269e767SBrooks Davis.Xr open 2 , 758*8269e767SBrooks Davis.Xr sigaction 2 , 759*8269e767SBrooks Davis.Xr lockf 3 , 760*8269e767SBrooks Davis.Xr tcgetpgrp 3 , 761*8269e767SBrooks Davis.Xr tcsetpgrp 3 762*8269e767SBrooks Davis.Sh STANDARDS 763*8269e767SBrooks DavisThe 764*8269e767SBrooks Davis.Dv F_DUP2FD 765*8269e767SBrooks Davisconstant is non portable. 766*8269e767SBrooks DavisIt is provided for compatibility with AIX and Solaris. 767*8269e767SBrooks Davis.Pp 768*8269e767SBrooks DavisPer 769*8269e767SBrooks Davis.St -susv4 , 770*8269e767SBrooks Davisa call with 771*8269e767SBrooks Davis.Dv F_SETLKW 772*8269e767SBrooks Davisshould fail with 773*8269e767SBrooks Davis.Bq Er EINTR 774*8269e767SBrooks Davisafter any caught signal 775*8269e767SBrooks Davisand should continue waiting during thread suspension such as a stop signal. 776*8269e767SBrooks DavisHowever, in this implementation a call with 777*8269e767SBrooks Davis.Dv F_SETLKW 778*8269e767SBrooks Davisis restarted after catching a signal with a 779*8269e767SBrooks Davis.Dv SA_RESTART 780*8269e767SBrooks Davishandler or a thread suspension such as a stop signal. 781*8269e767SBrooks Davis.Sh HISTORY 782*8269e767SBrooks DavisThe 783*8269e767SBrooks Davis.Fn fcntl 784*8269e767SBrooks Davissystem call appeared in 785*8269e767SBrooks Davis.Bx 4.2 . 786*8269e767SBrooks Davis.Pp 787*8269e767SBrooks DavisThe 788*8269e767SBrooks Davis.Dv F_DUP2FD 789*8269e767SBrooks Davisconstant first appeared in 790*8269e767SBrooks Davis.Fx 7.1 . 791