2 .\" This file and its contents are supplied under the terms of the
3 .\" Common Development and Distribution License ("CDDL"), version 1.0.
4 .\" You may only use this file in accordance with the terms of version
7 .\" A full copy of the text of the CDDL should have accompanied this
8 .\" source. A copy of the CDDL is also available via the Internet at
9 .\" http://www.illumos.org/license/CDDL.
12 .\" Copyright 1989 AT&T
13 .\" Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved
14 .\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
15 .\" Copyright 2015 Joyent, Inc.
17 .TH FLOCK 3C "Feb 16, 2015"
19 flock \- OFD(open file description)-style file locking
25 \fBint\fR \fBflock\fR(\fBint\fR \fIfildes\fR, \fBint\fR \fIoperation\fR);
30 The \fBflock()\fR function allows advisory locks to be applied to and removed
31 from a file. Calls to \fBflock()\fR from callers that attempt to lock
32 the locked file section via a different open file handle will either return an
33 error value or be put to sleep until the resource becomes unlocked.
34 See \fBfcntl\fR(2) for more information about record locking. Locks created or
35 removed via this function will apply to the entire file, including any future
36 growth in the file's length.
39 The \fIfildes\fR argument is an open file descriptor. A lock can be established
40 without regard for the mode with which the file was opened.
43 The \fIoperation\fR argument is a control value that specifies the action to be
44 taken. The permissible values for \fIoperation\fR are defined in
45 <\fBsys/file.h\fR> as follows:
49 #define LOCK_SH 1 /* shared file lock */
50 #define LOCK_EX 2 /* exclusive file lock */
51 #define LOCK_NB 4 /* do not block when attempting to create lock */
52 #define LOCK_UN 8 /* remove existing file lock */
58 To create a lock, either \fBLOCK_SH\fR or \fBLOCK_EX\fR should be specified,
59 optionally bitwise-ored with \fBLOCK_NB\fR. To remove a lock, \fBLOCK_UN\fR
60 should be specified. All other values of \fIoperation\fR are reserved for
61 future extensions and will result in an error if not implemented.
64 This function creates, upgrades, downgrades, or removes either shared or
65 exclusive OFD-style locks. Locks created by this function are owned by open
66 files, not file descriptors. That is, file descriptors duplicated through
67 \fBdup\fR(2), \fBfork\fR(2), or \fBfcntl\fR(2) do not result in multiple
68 instances of a lock, but rather multiple references to the same lock. If a
69 process holding a lock on a file forks and the child explicitly unlocks the
70 file, the parent will lose its lock. See \fBfcntl\fR(2) for more information
71 about file locking and the interaction between locks created by this function
72 and those created by other mechanisms. These locks are currently not supported
73 over remote file systems (e.g. \fBnfs\fR(4)) which use the Network Lock
77 Sleeping on a resource is interrupted with any signal. The \fBalarm\fR(2)
78 function may be used to provide a timeout facility in applications that require
82 Upon successful completion, \fB0\fR is returned. Otherwise, \fB\(mi1\fR is
83 returned and \fBerrno\fR is set to indicate the error.
86 The \fBflock()\fR function will fail if:
93 The \fIfildes\fR argument is not a valid open file descriptor; or
94 \fIoperation\fR contains \fBLOCK_SH\fR and \fIfiledes\fR is not open for
95 reading; or \fIoperation\fR contains \fBLOCK_EX\fR and \fIfiledes\fR is not
102 \fB\fBEWOULDBLOCK\fR\fR
105 The \fIoperation\fR argument contains \fBLOCK_NB\fR and a conflicting lock
115 A signal was caught during execution of the function.
124 The \fIoperation\fR argument does not contain one of \fBLOCK_SH\fR,
125 \fBLOCK_EX\fR, or \fBLOCK_UN\fR; or the \fIoperation\fR argument contains
126 \fBLOCK_UN\fR and \fBLOCK_NB\fR; or the \fIoperation\fR argument contains
127 any bits other than those set by \fBLOCK_SH\fR, \fBLOCK_EX\fR, \fBLOCK_NB\fR,
133 The \fBflock()\fR function may fail if:
140 The \fIoperation\fR argument contains \fBLOCK_SH\fR or \fBLOCK_EX\fR and the
141 file is mapped with \fBmmap\fR(2).
150 The number of locked file regions in the system would exceed a system-imposed
160 The locking of files of the type indicated by the \fIfildes\fR argument is not
166 File-locking should not be used in combination with the \fBfopen\fR(3C),
167 \fBfread\fR(3C), \fBfwrite\fR(3C) and other \fBstdio\fR functions. Instead,
168 the more primitive, non-buffered functions (such as \fBopen\fR(2)) should be
169 used. Unexpected results may occur in processes that do buffering in the user
170 address space. The process may later read/write data which is/was locked. The
171 \fBstdio\fR functions are the most common source of unexpected buffering.
174 The \fBalarm\fR(2) function may be used to provide a timeout facility in
175 applications requiring it.
178 Locks created by this facility conflict with those created by the
179 \fBlockf\fR(3C) and \fBfcntl\fR(2) facilities. This facility creates and
180 removed OFD-style locks; see \fBfcntl\fR(2) for information about the
181 interaction between OFD-style and POSIX-style file locks.
184 See \fBattributes\fR(5) for descriptions of the following attributes:
192 ATTRIBUTE TYPE ATTRIBUTE VALUE
194 Interface Stability Standard
201 \fBIntro\fR(2), \fBalarm\fR(2), \fBchmod\fR(2), \fBclose\fR(2), \fBcreat\fR(2),
202 \fBfcntl\fR(2), \fBmmap\fR(2), \fBopen\fR(2), \fBread\fR(2), \fBwrite\fR(2),
203 \fBattributes\fR(5), \fBstandards\fR(5)