1 /* $NetBSD: shlock.c,v 1.13 2015/04/10 09:34:43 tron Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 ** Program to produce reliable locks for shell scripts.
34 ** Algorithm suggested by Peter Honeyman, January 1984,
35 ** in connection with HoneyDanBer UUCP.
37 ** I tried extending this to handle shared locks in November 1987,
38 ** and ran into to some fundamental problems:
40 ** Neither 4.3 BSD nor System V have an open(2) with locking,
41 ** so that you can open a file and have it locked as soon as
42 ** it's real; you have to make two system calls, and there's
45 ** When removing dead process id's from a list in a file,
46 ** you need to truncate the file (you don't want to create a
47 ** new one; see above); unfortunately for the portability of
48 ** this program, only 4.3 BSD has ftruncate(2).
50 ** Erik E. Fair <fair@ucbarpa.berkeley.edu>, November 8, 1987
52 ** Extensions for UUCP style locks (i.e. pid is an int in the file,
53 ** rather than an ASCII string). Also fix long standing bug with
54 ** full file systems and temporary files.
56 ** Erik E. Fair <fair@apple.com>, November 12, 1989
58 ** ANSIfy the code somewhat to make gcc -Wall happy with the code.
61 ** Erik E. Fair <fair@clock.org>, May 20, 1997
64 #include <sys/cdefs.h>
67 __RCSID("$NetBSD: shlock.c,v 1.13 2015/04/10 09:34:43 tron Exp $");
70 #include <sys/types.h>
72 #include <fcntl.h> /* Needed on hpux */
91 static int Debug
= FALSE
;
93 static const char USAGE
[] = "%s: USAGE: %s [-du] [-p PID] -f file\n";
94 static const char E_unlk
[] = "%s: unlink(%s): %s\n";
95 static const char E_open
[] = "%s: open(%s): %s\n";
97 #define dprintf if (Debug) printf
100 ** Prototypes to make the ANSI compilers happy
101 ** Didn't lint used to do type and argument checking?
102 ** (and wasn't that sufficient?)
105 /* the following is in case you need to make the prototypes go away. */
106 static char *xtmpfile(char *, pid_t
, int);
107 static int p_exists(pid_t
);
108 static int cklock(char *, int);
109 static int mklock(char *, pid_t
, int);
110 __dead
static void bad_usage(void);
113 ** Create a temporary file, all ready to lock with.
114 ** The file arg is so we get the filename right, if he
115 ** gave us a full path, instead of using the current directory
116 ** which might not be in the same filesystem.
119 xtmpfile(char *file
, pid_t pid
, int uucpstyle
)
123 char *cp
, buf
[BUFSIZ
];
124 static char tempname
[BUFSIZ
];
126 sprintf(buf
, "shlock%ld", (long)getpid());
127 if ((cp
= strrchr(strcpy(tempname
, file
), '/')) != NULL
) {
129 (void) strcat(tempname
, buf
);
131 (void) strcpy(tempname
, buf
);
132 dprintf("%s: temporary filename: %s\n", Pname
, tempname
);
134 sprintf(buf
, "%ld\n", (long)pid
);
137 if ((fd
= open(tempname
, O_RDWR
|O_CREAT
|O_EXCL
, 0644)) < 0) {
140 dprintf("%s: file %s exists already.\n",
142 if (unlink(tempname
) < 0) {
143 fprintf(stderr
, E_unlk
,
144 Pname
, tempname
, strerror(errno
));
152 fprintf(stderr
, E_open
,
153 Pname
, tempname
, strerror(errno
));
159 ** Write the PID into the temporary file before attempting to link
160 ** to the actual lock file. That way we have a valid lock the instant
161 ** the link succeeds.
164 (write(fd
, &pid
, sizeof(pid
)) != sizeof(pid
)) :
165 (write(fd
, buf
, len
) < 0))
167 fprintf(stderr
, "%s: write(%s,%ld): %s\n",
168 Pname
, tempname
, (long)pid
, strerror(errno
));
170 if (unlink(tempname
) < 0) {
171 fprintf(stderr
, E_unlk
,
172 Pname
, tempname
, strerror(errno
));
181 ** Does the PID exist?
182 ** Send null signal to find out.
187 dprintf("%s: process %ld is ", Pname
, (long)pid
);
189 dprintf("invalid\n");
192 if (kill(pid
, 0) < 0) {
196 return(FALSE
); /* pid does not exist */
199 return(TRUE
); /* pid exists */
201 dprintf("state unknown: %s\n", strerror(errno
));
202 return(TRUE
); /* be conservative */
206 return(TRUE
); /* pid exists */
210 ** Check the validity of an existing lock file.
212 ** Read the PID out of the lock
213 ** Send a null signal to determine whether that PID still exists
214 ** Existence (or not) determines the validity of the lock.
216 ** Two bigs wins to this algorithm:
218 ** o Locks do not survive crashes of either the system or the
219 ** application by any appreciable period of time.
221 ** o No clean up to do if the system or application crashes.
225 cklock(char *file
, int uucpstyle
)
227 int fd
= open(file
, O_RDONLY
);
232 dprintf("%s: checking extant lock <%s>\n", Pname
, file
);
235 fprintf(stderr
, E_open
, Pname
, file
, strerror(errno
));
236 return(TRUE
); /* might or might not; conservatism */
240 ((len
= read(fd
, &pid
, sizeof(pid
))) != sizeof(pid
)) :
241 ((len
= read(fd
, buf
, sizeof(buf
))) <= 0))
244 dprintf("%s: lock file format error\n", Pname
);
249 return(p_exists(uucpstyle
? pid
: atoi(buf
)));
253 mklock(char *file
, pid_t pid
, int uucpstyle
)
258 dprintf("%s: trying lock <%s> for process %ld\n", Pname
, file
,
260 if ((tmp
= xtmpfile(file
, pid
, uucpstyle
)) == NULL
)
264 if (link(tmp
, file
) < 0) {
267 dprintf("%s: lock <%s> already exists\n", Pname
, file
);
268 if (cklock(file
, uucpstyle
)) {
269 dprintf("%s: extant lock is valid\n", Pname
);
272 dprintf("%s: lock is invalid, removing\n",
274 if (unlink(file
) < 0) {
275 fprintf(stderr
, E_unlk
,
276 Pname
, file
, strerror(errno
));
281 ** I hereby profane the god of structured programming,
286 fprintf(stderr
, "%s: link(%s, %s): %s\n",
287 Pname
, tmp
, file
, strerror(errno
));
291 dprintf("%s: got lock <%s>\n", Pname
, file
);
294 if (unlink(tmp
) < 0) {
295 fprintf(stderr
, E_unlk
, Pname
, tmp
, strerror(errno
));
303 fprintf(stderr
, USAGE
, Pname
, Pname
);
308 main(int ac
, char **av
)
313 int uucpstyle
= FALSE
; /* indicating UUCP style locks */
314 int only_check
= TRUE
; /* don't make a lock */
316 Pname
= ((Pname
= strrchr(av
[0], '/')) ? Pname
+ 1 : av
[0]);
318 for(x
= 1; x
< ac
; x
++) {
319 if (av
[x
][0] == '-') {
328 if (strlen(av
[x
]) > 2) {
329 pid
= atoi(&av
[x
][2]);
336 only_check
= FALSE
; /* wants one */
339 if (strlen(av
[x
]) > 2) {
354 if (file
== NULL
|| (!only_check
&& pid
<= 0)) {
359 exit(cklock(file
, uucpstyle
) ? LOCK_GOOD
: LOCK_BAD
);
362 exit(mklock(file
, pid
, uucpstyle
) ? LOCK_SET
: LOCK_FAIL
);