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
19

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

EXAMPLES

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

APPLICATION USAGE

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

RATIONALE

189       None.
190

FUTURE DIRECTIONS

192       None.
193

SEE ALSO

195       alarm(),  chmod(),  close(), creat(), fcntl(), fopen(), mmap(), open(),
196       read(), write(), the Base Definitions volume  of  IEEE Std 1003.1-2001,
197       <unistd.h>
198
200       Portions  of  this text are reprinted and reproduced in electronic form
201       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
202       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
203       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
204       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
205       event of any discrepancy between this version and the original IEEE and
206       The  Open Group Standard, the original IEEE and The Open Group Standard
207       is the referee document. The original Standard can be  obtained  online
208       at http://www.opengroup.org/unix/online.html .
209
210
211
212IEEE/The Open Group                  2003                            LOCKF(3P)
Impressum