1*0a6a1f1dSLionel Sambuc.\" $NetBSD: shlock.1,v 1.14 2014/03/18 18:20:45 riastradh Exp $ 2152a1565SThomas Cort.\" 3152a1565SThomas Cort.\" Copyright (c) 2006 The NetBSD Foundation, Inc. 4152a1565SThomas Cort.\" All rights reserved. 5152a1565SThomas Cort.\" 6152a1565SThomas Cort.\" This code is derived from software contributed to The NetBSD Foundation 7152a1565SThomas Cort.\" by Erik E. Fair. 8152a1565SThomas Cort.\" 9152a1565SThomas Cort.\" Redistribution and use in source and binary forms, with or without 10152a1565SThomas Cort.\" modification, are permitted provided that the following conditions 11152a1565SThomas Cort.\" are met: 12152a1565SThomas Cort.\" 1. Redistributions of source code must retain the above copyright 13152a1565SThomas Cort.\" notice, this list of conditions and the following disclaimer. 14152a1565SThomas Cort.\" 2. Redistributions in binary form must reproduce the above copyright 15152a1565SThomas Cort.\" notice, this list of conditions and the following disclaimer in the 16152a1565SThomas Cort.\" documentation and/or other materials provided with the distribution. 17152a1565SThomas Cort.\" 18152a1565SThomas Cort.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19152a1565SThomas Cort.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20152a1565SThomas Cort.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21152a1565SThomas Cort.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22152a1565SThomas Cort.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23152a1565SThomas Cort.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24152a1565SThomas Cort.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25152a1565SThomas Cort.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26152a1565SThomas Cort.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27152a1565SThomas Cort.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28152a1565SThomas Cort.\" POSSIBILITY OF SUCH DAMAGE. 29152a1565SThomas Cort.\" 3084d9c625SLionel Sambuc.Dd November 2, 2012 31152a1565SThomas Cort.Dt SHLOCK 1 32152a1565SThomas Cort.Os 33152a1565SThomas Cort.Sh NAME 34152a1565SThomas Cort.Nm shlock 35152a1565SThomas Cort.Nd create or verify a lock file for shell scripts 36152a1565SThomas Cort.Sh SYNOPSIS 37152a1565SThomas Cort.Nm 38152a1565SThomas Cort.Op Fl du 39152a1565SThomas Cort.Op Fl p Ar PID 40152a1565SThomas Cort.Fl f 41152a1565SThomas Cort.Ar lockfile 42152a1565SThomas Cort.Sh DESCRIPTION 43152a1565SThomas CortThe 44152a1565SThomas Cort.Nm 45152a1565SThomas Cortcommand can create or verify a lock file on behalf of a shell or 46152a1565SThomas Cortother script program. 47152a1565SThomas CortWhen it attempts to create a lock file, if one already exists, 48152a1565SThomas Cort.Nm 49152a1565SThomas Cortverifies that it is or is not valid. 50152a1565SThomas CortIf valid, 51152a1565SThomas Cort.Nm 52152a1565SThomas Cortwill exit with a non-zero exit code. 53152a1565SThomas CortIf invalid, 54152a1565SThomas Cort.Nm 55152a1565SThomas Cortwill remove the lock file, and 56152a1565SThomas Cortcreate a new one. 57152a1565SThomas Cort.Pp 58152a1565SThomas Cort.Nm 59152a1565SThomas Cortuses the 60152a1565SThomas Cort.Xr link 2 61152a1565SThomas Cortsystem call to make the final target lock file, which is an atomic 62152a1565SThomas Cortoperation (i.e. "dot locking", so named for this mechanism's original 63152a1565SThomas Cortuse for locking system mailboxes). 64152a1565SThomas CortIt puts the process ID ("PID") from the command line into the 65152a1565SThomas Cortrequested lock file. 66152a1565SThomas Cort.Pp 67152a1565SThomas Cort.Nm 68152a1565SThomas Cortverifies that an extant lock file is still valid by 69152a1565SThomas Cortusing 70152a1565SThomas Cort.Xr kill 2 71152a1565SThomas Cortwith a zero signal to check for the existence of the process that 72152a1565SThomas Cortholds the lock. 73152a1565SThomas Cort.Pp 74152a1565SThomas CortThe 75152a1565SThomas Cort.Fl d 76152a1565SThomas Cortoption causes 77152a1565SThomas Cort.Nm 78152a1565SThomas Cortto be verbose about what it is doing. 79152a1565SThomas Cort.Pp 80152a1565SThomas CortThe 81152a1565SThomas Cort.Fl f 82152a1565SThomas Cortargument with 83152a1565SThomas Cort.Ar lockfile 84152a1565SThomas Cortis always required. 85152a1565SThomas Cort.Pp 86152a1565SThomas CortThe 87152a1565SThomas Cort.Fl p 88152a1565SThomas Cortoption with 89152a1565SThomas Cort.Ar PID 90152a1565SThomas Cortis given when the program is to create a lock file; when absent, 91152a1565SThomas Cort.Nm 92152a1565SThomas Cortwill simply check for the validity of the lock file. 93152a1565SThomas Cort.Pp 94152a1565SThomas CortThe 95152a1565SThomas Cort.Fl u 96152a1565SThomas Cortoption causes 97152a1565SThomas Cort.Nm 98152a1565SThomas Cortto read and write the PID as a binary pid_t, instead of as ASCII, 99152a1565SThomas Cortto be compatible with the locks created by UUCP. 100152a1565SThomas Cort.Sh EXIT STATUS 101152a1565SThomas CortA zero exit code indicates a valid lock file. 102152a1565SThomas Cort.Sh EXAMPLES 103152a1565SThomas Cort.Ss BOURNE SHELL 104152a1565SThomas Cort.Bd -literal 105152a1565SThomas Cort#!/bin/sh 106152a1565SThomas Cortlckfile=/tmp/foo.lock 107152a1565SThomas Cortif shlock -f ${lckfile} -p $$ 108152a1565SThomas Cortthen 109152a1565SThomas Cort# do what required the lock 110152a1565SThomas Cort rm ${lckfile} 111152a1565SThomas Cortelse 112152a1565SThomas Cort echo Lock ${lckfile} already held by `cat ${lckfile}` 113152a1565SThomas Cortfi 114152a1565SThomas Cort.Ed 115152a1565SThomas Cort.Ss C SHELL 116152a1565SThomas Cort.Bd -literal 117152a1565SThomas Cort#!/bin/csh -f 118152a1565SThomas Cortset lckfile=/tmp/foo.lock 119152a1565SThomas Cortshlock -f ${lckfile} -p $$ 120152a1565SThomas Cortif ($status == 0) then 121152a1565SThomas Cort# do what required the lock 122152a1565SThomas Cort rm ${lckfile} 123152a1565SThomas Cortelse 124152a1565SThomas Cort echo Lock ${lckfile} already held by `cat ${lckfile}` 125152a1565SThomas Cortendif 126152a1565SThomas Cort.Ed 127152a1565SThomas Cort.Pp 128152a1565SThomas CortThe examples assume that the file system where the lock file is to 129152a1565SThomas Cortbe created is writable by the user, and has space available. 13084d9c625SLionel Sambuc.Sh SEE ALSO 13184d9c625SLionel Sambuc.Xr flock 1 132152a1565SThomas Cort.Sh HISTORY 133152a1565SThomas Cort.Nm 134152a1565SThomas Cortwas written for the first Network News Transfer Protocol (NNTP) 135152a1565SThomas Cortsoftware distribution, released in March 1986. 136152a1565SThomas CortThe algorithm was suggested by Peter Honeyman, 137152a1565SThomas Cortfrom work he did on HoneyDanBer UUCP. 138152a1565SThomas Cort.Sh AUTHORS 13984d9c625SLionel Sambuc.An Erik E. Fair Aq Mt fair@clock.org 140152a1565SThomas Cort.Sh BUGS 141152a1565SThomas CortDoes not work on NFS or other network file system on different 142152a1565SThomas Cortsystems because the disparate systems have disjoint PID spaces. 143152a1565SThomas Cort.Pp 144152a1565SThomas CortCannot handle the case where a lock file was not deleted, the 145152a1565SThomas Cortprocess that created it has exited, and the system has created a 146152a1565SThomas Cortnew process with the same PID as in the dead lock file. 147152a1565SThomas CortThe lock file will appear to be valid even though the process is 148152a1565SThomas Cortunrelated to the one that created the lock in the first place. 149152a1565SThomas CortAlways remove your lock files after you're done. 150