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 July 7, 1998 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 kns 36.Op Fl t Ar seconds 37.Ar file 38.Ar command 39.Op Ar arguments 40.Sh DESCRIPTION 41The 42.Nm 43utility acquires an exclusive lock on a 44.Ar file , 45creating it if necessary, 46.Bf Em 47and removing the file on exit unless explicitly told not to. 48.Ef 49While holding the lock, it executes a 50.Ar command 51with optional 52.Ar arguments . 53After the 54.Ar command 55completes, 56.Nm 57releases the lock, and removes the 58.Ar file 59unless the 60.Fl k 61option is specified. 62.Bx Ns -style 63locking is used, as described in 64.Xr flock 2 ; 65the mere existence of the 66.Ar file 67is not considered to constitute a lock. 68.Pp 69If the 70.Nm 71utility is being used to facilitate concurrency between a number 72of processes, it is recommended that the 73.Fl k 74option be used. 75This will guarantee lock ordering, as well as implement 76a performance enhanced algorithm which minimizes CPU load associated 77with concurrent unlink, drop and re-acquire activity. 78It should be noted 79that if the 80.Fl k 81option is not used, then no guarantees around lock ordering can be made. 82.Pp 83The following options are supported: 84.Bl -tag -width ".Fl t Ar seconds" 85.It Fl k 86Causes the lock file to be kept (not removed) after the command 87completes. 88.It Fl s 89Causes 90.Nm 91to operate silently. 92Failure to acquire the lock is indicated only in the exit status. 93.It Fl n 94Causes 95.Nm 96to fail if the specified lock 97.Ar file 98does not exist. If 99.Fl n 100is not specified, 101.Nm 102will create 103.Ar file 104if necessary. 105.It Fl t Ar seconds 106Specifies a timeout for waiting for the lock. 107By default, 108.Nm 109waits indefinitely to acquire the lock. 110If a timeout is specified with this option, 111.Nm 112will wait at most the given number of 113.Ar seconds 114before giving up. 115A timeout of 0 may be given, in which case 116.Nm 117will fail unless it can acquire the lock immediately. 118When a lock times out, 119.Ar command 120is 121.Em not 122executed. 123.El 124.Pp 125In no event will 126.Nm 127break a lock that is held by another process. 128.Sh EXIT STATUS 129If 130.Nm 131successfully acquires the lock, it returns the exit status produced by 132.Ar command . 133Otherwise, it returns one of the exit codes defined in 134.Xr sysexits 3 , 135as follows: 136.Bl -tag -width ".Dv EX_CANTCREAT" 137.It Dv EX_TEMPFAIL 138The specified lock file was already locked by another process. 139.It Dv EX_CANTCREAT 140The 141.Nm 142utility 143was unable to create the lock file, e.g., because of insufficient access 144privileges. 145.It Dv EX_UNAVAILABLE 146The 147.Fl n 148option is specified and the specified lock file does not exist. 149.It Dv EX_USAGE 150There was an error on the 151.Nm 152command line. 153.It Dv EX_OSERR 154A system call (e.g., 155.Xr fork 2 ) 156failed unexpectedly. 157.It Dv EX_SOFTWARE 158The 159.Ar command 160did not exit normally, 161but may have been signaled or stopped. 162.El 163.Sh SEE ALSO 164.Xr flock 2 , 165.Xr lockf 3 , 166.Xr sysexits 3 167.Sh HISTORY 168A 169.Nm 170utility first appeared in 171.Fx 2.2 . 172.Sh AUTHORS 173.An John Polstra Aq Mt jdp@polstra.com 174