xref: /freebsd/usr.bin/lockf/lockf.1 (revision 05248206f720394d95c2a7475429311df670a2e9)
1.\"
2.\" Copyright (C) 1998 John D. Polstra.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY JOHN D. POLSTRA AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL JOHN D. POLSTRA OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.Dd August 26, 2020
26.Dt LOCKF 1
27.Os
28.Sh NAME
29.Nm lockf
30.Nd execute a command while holding a file lock
31.Sh SYNOPSIS
32.Nm
33.Op Fl knsw
34.Op Fl t Ar seconds
35.Ar file
36.Ar command
37.Op Ar arguments
38.Sh DESCRIPTION
39The
40.Nm
41utility acquires an exclusive lock on a
42.Ar file ,
43creating it if necessary,
44.Bf Em
45and removing the file on exit unless explicitly told not to.
46.Ef
47While holding the lock, it executes a
48.Ar command
49with optional
50.Ar arguments .
51After the
52.Ar command
53completes,
54.Nm
55releases the lock, and removes the
56.Ar file
57unless the
58.Fl k
59option is specified.
60.Bx Ns -style
61locking is used, as described in
62.Xr flock 2 ;
63the mere existence of the
64.Ar file
65is not considered to constitute a lock.
66.Pp
67If the
68.Nm
69utility is being used to facilitate concurrency between a number
70of processes, it is recommended that the
71.Fl k
72option be used.
73This will guarantee lock ordering, as well as implement
74a performance enhanced algorithm which minimizes CPU load associated
75with concurrent unlink, drop and re-acquire activity.
76It should be noted
77that if the
78.Fl k
79option is not used, then no guarantees around lock ordering can be made.
80.Pp
81The following options are supported:
82.Bl -tag -width ".Fl t Ar seconds"
83.It Fl k
84Causes the lock file to be kept (not removed) after the command
85completes.
86.It Fl s
87Causes
88.Nm
89to operate silently.
90Failure to acquire the lock is indicated only in the exit status.
91.It Fl n
92Causes
93.Nm
94to fail if the specified lock
95.Ar file
96does not exist.
97If
98.Fl n
99is not specified,
100.Nm
101will create
102.Ar file
103if necessary.
104.It Fl t Ar seconds
105Specifies a timeout for waiting for the lock.
106By default,
107.Nm
108waits indefinitely to acquire the lock.
109If a timeout is specified with this option,
110.Nm
111will wait at most the given number of
112.Ar seconds
113before giving up.
114A timeout of 0 may be given, in which case
115.Nm
116will fail unless it can acquire the lock immediately.
117When a lock times out,
118.Ar command
119is
120.Em not
121executed.
122.It Fl w
123Causes
124.Nm
125to open
126.Ar file
127for writing rather than reading.
128This is necessary on filesystems (including NFSv4) where a file which
129has been opened read-only cannot be exclusively locked.
130.El
131.Pp
132In no event will
133.Nm
134break a lock that is held by another process.
135.Sh EXIT STATUS
136If
137.Nm
138successfully acquires the lock, it returns the exit status produced by
139.Ar command .
140Otherwise, it returns one of the exit codes defined in
141.Xr sysexits 3 ,
142as follows:
143.Bl -tag -width ".Dv EX_CANTCREAT"
144.It Dv EX_TEMPFAIL
145The specified lock file was already locked by another process.
146.It Dv EX_CANTCREAT
147The
148.Nm
149utility
150was unable to create the lock file, e.g., because of insufficient access
151privileges.
152.It Dv EX_UNAVAILABLE
153The
154.Fl n
155option is specified and the specified lock file does not exist.
156.It Dv EX_USAGE
157There was an error on the
158.Nm
159command line.
160.It Dv EX_OSERR
161A system call (e.g.,
162.Xr fork 2 )
163failed unexpectedly.
164.It Dv EX_SOFTWARE
165The
166.Ar command
167did not exit normally,
168but may have been signaled or stopped.
169.El
170.Sh EXAMPLES
171The first job takes a lock and sleeps for 5 seconds in the background.
172The second job tries to get the lock and timeouts after 1 second (PID numbers
173will differ):
174.Bd -literal -offset indent
175$ lockf mylock sleep 5 & lockf -t 1 mylock echo "Success"
176[1] 94410
177lockf: mylock: already locked
178.Ed
179.Pp
180The first job takes a lock and sleeps for 1 second in the background.
181The second job waits up to 5 seconds to take the lock and echoes the message on
182success (PID numbers will differ):
183.Bd -literal -offset indent
184$ lockf mylock sleep 1 & lockf -t 5 mylock echo "Success"
185[1] 19995
186Success
187[1]+  Done                    lockf mylock sleep 1
188.Ed
189.Sh SEE ALSO
190.Xr flock 2 ,
191.Xr lockf 3 ,
192.Xr sysexits 3
193.Sh HISTORY
194A
195.Nm
196utility first appeared in
197.Fx 2.2 .
198.Sh AUTHORS
199.An John Polstra Aq Mt jdp@polstra.com
200