]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/lockf/lockf.1
ping(8): Fix a mandoc related issue
[FreeBSD/FreeBSD.git] / usr.bin / lockf / lockf.1
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 .\" $FreeBSD$
26 .\"
27 .Dd August 26, 2020
28 .Dt LOCKF 1
29 .Os
30 .Sh NAME
31 .Nm lockf
32 .Nd execute a command while holding a file lock
33 .Sh SYNOPSIS
34 .Nm
35 .Op Fl knsw
36 .Op Fl t Ar seconds
37 .Ar file
38 .Ar command
39 .Op Ar arguments
40 .Sh DESCRIPTION
41 The
42 .Nm
43 utility acquires an exclusive lock on a
44 .Ar file ,
45 creating it if necessary,
46 .Bf Em
47 and removing the file on exit unless explicitly told not to.
48 .Ef
49 While holding the lock, it executes a
50 .Ar command
51 with optional
52 .Ar arguments .
53 After the
54 .Ar command
55 completes,
56 .Nm
57 releases the lock, and removes the
58 .Ar file
59 unless the
60 .Fl k
61 option is specified.
62 .Bx Ns -style
63 locking is used, as described in
64 .Xr flock 2 ;
65 the mere existence of the
66 .Ar file
67 is not considered to constitute a lock.
68 .Pp
69 If the
70 .Nm
71 utility is being used to facilitate concurrency between a number
72 of processes, it is recommended that the
73 .Fl k
74 option be used.
75 This will guarantee lock ordering, as well as implement
76 a performance enhanced algorithm which minimizes CPU load associated
77 with concurrent unlink, drop and re-acquire activity.
78 It should be noted
79 that if the
80 .Fl k
81 option is not used, then no guarantees around lock ordering can be made.
82 .Pp
83 The following options are supported:
84 .Bl -tag -width ".Fl t Ar seconds"
85 .It Fl k
86 Causes the lock file to be kept (not removed) after the command
87 completes.
88 .It Fl s
89 Causes
90 .Nm
91 to operate silently.
92 Failure to acquire the lock is indicated only in the exit status.
93 .It Fl n
94 Causes
95 .Nm
96 to fail if the specified lock
97 .Ar file
98 does not exist.
99 If
100 .Fl n
101 is not specified,
102 .Nm
103 will create
104 .Ar file
105 if necessary.
106 .It Fl t Ar seconds
107 Specifies a timeout for waiting for the lock.
108 By default,
109 .Nm
110 waits indefinitely to acquire the lock.
111 If a timeout is specified with this option,
112 .Nm
113 will wait at most the given number of
114 .Ar seconds
115 before giving up.
116 A timeout of 0 may be given, in which case
117 .Nm
118 will fail unless it can acquire the lock immediately.
119 When a lock times out,
120 .Ar command
121 is
122 .Em not
123 executed.
124 .It Fl w
125 Causes
126 .Nm
127 to open
128 .Ar file
129 for writing rather than reading.
130 This is necessary on filesystems (including NFSv4) where a file which
131 has been opened read-only cannot be exclusively locked.
132 .El
133 .Pp
134 In no event will
135 .Nm
136 break a lock that is held by another process.
137 .Sh EXIT STATUS
138 If
139 .Nm
140 successfully acquires the lock, it returns the exit status produced by
141 .Ar command .
142 Otherwise, it returns one of the exit codes defined in
143 .Xr sysexits 3 ,
144 as follows:
145 .Bl -tag -width ".Dv EX_CANTCREAT"
146 .It Dv EX_TEMPFAIL
147 The specified lock file was already locked by another process.
148 .It Dv EX_CANTCREAT
149 The
150 .Nm
151 utility
152 was unable to create the lock file, e.g., because of insufficient access
153 privileges.
154 .It Dv EX_UNAVAILABLE
155 The
156 .Fl n
157 option is specified and the specified lock file does not exist.
158 .It Dv EX_USAGE
159 There was an error on the
160 .Nm
161 command line.
162 .It Dv EX_OSERR
163 A system call (e.g.,
164 .Xr fork 2 )
165 failed unexpectedly.
166 .It Dv EX_SOFTWARE
167 The
168 .Ar command
169 did not exit normally,
170 but may have been signaled or stopped.
171 .El
172 .Sh EXAMPLES
173 The first job takes a lock and sleeps for 5 seconds in the background.
174 The second job tries to get the lock and timeouts after 1 second (PID numbers
175 will differ):
176 .Bd -literal -offset indent
177 $ lockf mylock sleep 5 & lockf -t 1 mylock echo "Success"
178 [1] 94410
179 lockf: mylock: already locked
180 .Ed
181 .Pp
182 The first job takes a lock and sleeps for 1 second in the background.
183 The second job waits up to 5 seconds to take the lock and echoes the message on
184 success (PID numbers will differ):
185 .Bd -literal -offset indent
186 $ lockf mylock sleep 1 & lockf -t 5 mylock echo "Success"
187 [1] 19995
188 Success
189 [1]+  Done                    lockf mylock sleep 1
190 .Ed
191 .Sh SEE ALSO
192 .Xr flock 2 ,
193 .Xr lockf 3 ,
194 .Xr sysexits 3
195 .Sh HISTORY
196 A
197 .Nm
198 utility first appeared in
199 .Fx 2.2 .
200 .Sh AUTHORS
201 .An John Polstra Aq Mt jdp@polstra.com