1LOCKF(3P)                  POSIX Programmer's Manual                 LOCKF(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       lockf — record locking on files
13

SYNOPSIS

15       #include <unistd.h>
16
17       int lockf(int fildes, int function, off_t size);
18

DESCRIPTION

20       The lockf() function shall lock sections of a file  with  advisory-mode
21       locks.  Calls  to lockf() from threads in other processes which attempt
22       to lock the locked file section shall either return an error  value  or
23       block  until  the section becomes unlocked. All the locks for a process
24       are removed when the process terminates. Record  locking  with  lockf()
25       shall  be  supported  for  regular files and may be supported for other
26       files.
27
28       The fildes argument is an open file descriptor.  To  establish  a  lock
29       with this function, the file descriptor shall be opened with write-only
30       permission (O_WRONLY) or with read/write permission (O_RDWR).
31
32       The function argument is a control value which specifies the action  to
33       be taken. The permissible values for function are defined in <unistd.h>
34       as follows:
35
36              ┌─────────┬──────────────────────────────────────────────┐
37Function Description                  
38              ├─────────┼──────────────────────────────────────────────┤
39              │F_ULOCK  │ Unlock locked sections.                      │
40              │F_LOCK   │ Lock a section for exclusive use.            │
41              │F_TLOCK  │ Test and lock a section for exclusive use.   │
42              │F_TEST   │ Test a section for locks by other processes. │
43              └─────────┴──────────────────────────────────────────────┘
44       F_TEST shall detect if a lock by another  process  is  present  on  the
45       specified section.
46
47       F_LOCK  and  F_TLOCK shall both lock a section of a file if the section
48       is available.
49
50       F_ULOCK shall remove locks from a section of the file.
51
52       The size argument is the number of contiguous bytes  to  be  locked  or
53       unlocked.   The  section to be locked or unlocked starts at the current
54       offset in the file and extends forward for a positive size or  backward
55       for  a  negative  size (the preceding bytes up to but not including the
56       current offset). If size is 0, the  section  from  the  current  offset
57       through the largest possible file offset shall be locked (that is, from
58       the current offset through the present or any future  end-of-file).  An
59       area  need  not be allocated to the file to be locked because locks may
60       exist past the end-of-file.
61
62       The sections locked with F_LOCK or F_TLOCK may, in whole  or  in  part,
63       contain  or  be  contained  by a previously locked section for the same
64       process. When this occurs, or if adjacent locked sections would  occur,
65       the  sections  shall  be  combined into a single locked section. If the
66       request would cause the number of  locks  to  exceed  a  system-imposed
67       limit, the request shall fail.
68
69       F_LOCK and F_TLOCK requests differ only by the action taken if the sec‐
70       tion is not available. F_LOCK shall block the calling thread until  the
71       section  is  available. F_TLOCK shall cause the function to fail if the
72       section is already locked by another process.
73
74       File locks shall be released on first close by the locking  process  of
75       any file descriptor for the file.
76
77       F_ULOCK  requests  may  release  (wholly or in part) one or more locked
78       sections controlled by the process. Locked sections shall  be  unlocked
79       starting  at  the current file offset through size bytes or to the end-
80       of-file if size is (off_t)0. When  all  of  a  locked  section  is  not
81       released (that is, when the beginning or end of the area to be unlocked
82       falls within a locked section), the remaining portions of that  section
83       shall  remain  locked by the process. Releasing the center portion of a
84       locked section shall cause the remaining locked beginning and end  por‐
85       tions  to  become  two  separate  locked sections. If the request would
86       cause the number of locks in the  system  to  exceed  a  system-imposed
87       limit, the request shall fail.
88
89       A potential for deadlock occurs if the threads of a process controlling
90       a locked section are blocked by accessing a locked section  of  another
91       process. If the system detects that deadlock would occur, lockf() shall
92       fail with an [EDEADLK] error.
93
94       The interaction between fcntl() and lockf() locks is unspecified.
95
96       Blocking on a section shall be interrupted by any signal.
97
98       An F_ULOCK request in which size is non-zero and the offset of the last
99       byte  of  the  requested  section is the maximum value for an object of
100       type off_t, when the process has an existing lock in which  size  is  0
101       and  which  includes  the  last byte of the requested section, shall be
102       treated as a request to unlock from the start of the requested  section
103       with  a size equal to 0. Otherwise, an F_ULOCK request shall attempt to
104       unlock only the requested section.
105
106       Attempting to lock a section of  a  file  that  is  associated  with  a
107       buffered stream produces unspecified results.
108

RETURN VALUE

110       Upon successful completion, lockf() shall return 0. Otherwise, it shall
111       return -1, set errno to indicate an error, and existing locks shall not
112       be changed.
113

ERRORS

115       The lockf() function shall fail if:
116
117       EBADF  The  fildes  argument  is  not  a valid open file descriptor; or
118              function is F_LOCK or F_TLOCK and fildes is  not  a  valid  file
119              descriptor open for writing.
120
121       EACCES or EAGAIN
122              The  function  argument  is F_TLOCK or F_TEST and the section is
123              already locked by another process.
124
125       EDEADLK
126              The function argument is F_LOCK and a deadlock is detected.
127
128       EINTR  A signal was caught during execution of the function.
129
130       EINVAL The function argument is not one of F_LOCK, F_TLOCK, F_TEST,  or
131              F_ULOCK; or size plus the current file offset is less than 0.
132
133       EOVERFLOW
134              The offset of the first, or if size is not 0 then the last, byte
135              in the requested section cannot be represented correctly  in  an
136              object of type off_t.
137
138       The lockf() function may fail if:
139
140       EAGAIN The  function  argument  is  F_LOCK  or  F_TLOCK and the file is
141              mapped with mmap().
142
143       EDEADLK or ENOLCK
144              The function argument is F_LOCK, F_TLOCK, or  F_ULOCK,  and  the
145              request  would  cause  the  number  of locks to exceed a system-
146              imposed limit.
147
148       EOPNOTSUPP or EINVAL
149              The implementation does not support the locking of files of  the
150              type indicated by the fildes argument.
151
152       The following sections are informative.
153

EXAMPLES

155   Locking a Portion of a File
156       In  the  following  example, a file named /home/cnd/mod1 is being modi‐
157       fied. Other processes that use locking are prevented from  changing  it
158       during  this  process.  Only  the first 10000 bytes are locked, and the
159       lock call fails if another process has any part  of  this  area  locked
160       already.
161
162
163           #include <fcntl.h>
164           #include <unistd.h>
165
166           int fildes;
167           int status;
168           ...
169           fildes = open("/home/cnd/mod1", O_RDWR);
170           status = lockf(fildes, F_TLOCK, (off_t)10000);
171

APPLICATION USAGE

173       Record-locking  should  not  be  used  in combination with the fopen(),
174       fread(), fwrite(), and other stdio functions. Instead, the more  primi‐
175       tive,  non-buffered  functions  (such  as open()) should be used. Unex‐
176       pected results may occur in processes that do  buffering  in  the  user
177       address  space.  The  process  may  later  read/write data which is/was
178       locked. The stdio functions are the most common  source  of  unexpected
179       buffering.
180
181       The  alarm()  function  may  be  used  to provide a timeout facility in
182       applications requiring it.
183

RATIONALE

185       None.
186

FUTURE DIRECTIONS

188       None.
189

SEE ALSO

191       alarm(), chmod(), close(), creat(), fcntl(), fopen(),  mmap(),  open(),
192       read(), write()
193
194       The Base Definitions volume of POSIX.1‐2017, <unistd.h>
195
197       Portions  of  this text are reprinted and reproduced in electronic form
198       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
199       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
200       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
201       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
202       event of any discrepancy between this version and the original IEEE and
203       The  Open Group Standard, the original IEEE and The Open Group Standard
204       is the referee document. The original Standard can be  obtained  online
205       at http://www.opengroup.org/unix/online.html .
206
207       Any  typographical  or  formatting  errors that appear in this page are
208       most likely to have been introduced during the conversion of the source
209       files  to  man page format. To report such errors, see https://www.ker
210       nel.org/doc/man-pages/reporting_bugs.html .
211
212
213
214IEEE/The Open Group                  2017                            LOCKF(3P)
Impressum