1 /* $NetBSD: shlock.c,v 1.9 2006/10/07 21:13:00 elad 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.9 2006/10/07 21:13:00 elad Exp $");
70 #include <sys/types.h>
72 #include <fcntl.h> /* Needed on hpux */
93 const char USAGE
[] = "%s: USAGE: %s [-du] [-p PID] -f file\n";
94 const char E_unlk
[] = "%s: unlink(%s): %s\n";
95 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 char *xtmpfile(char *, pid_t
, int);
108 int cklock(char *, int);
109 int mklock(char *, pid_t
, int);
110 void bad_usage(void);
111 int main(int, char **);
114 ** Create a temporary file, all ready to lock with.
115 ** The file arg is so we get the filename right, if he
116 ** gave us a full path, instead of using the current directory
117 ** which might not be in the same filesystem.
120 xtmpfile(char *file
, __pid_t pid
, int uucpstyle
)
124 char *cp
, buf
[BUFSIZ
];
125 static char tempname
[BUFSIZ
];
127 sprintf(buf
, "shlock%ld", (u_long
)getpid());
128 if ((cp
= strrchr(strcpy(tempname
, file
), '/')) != (char *)NULL
) {
130 (void) strcat(tempname
, buf
);
132 (void) strcpy(tempname
, buf
);
133 dprintf("%s: temporary filename: %s\n", Pname
, tempname
);
135 sprintf(buf
, "%ld\n", (u_long
)pid
);
138 if ((fd
= open(tempname
, O_RDWR
|O_CREAT
|O_EXCL
, 0644)) < 0) {
141 dprintf("%s: file %s exists already.\n",
143 if (unlink(tempname
) < 0) {
144 fprintf(stderr
, E_unlk
,
145 Pname
, tempname
, strerror(errno
));
146 return((char *)NULL
);
153 fprintf(stderr
, E_open
,
154 Pname
, tempname
, strerror(errno
));
155 return((char *)NULL
);
160 ** Write the PID into the temporary file before attempting to link
161 ** to the actual lock file. That way we have a valid lock the instant
162 ** the link succeeds.
165 (write(fd
, &pid
, sizeof(pid
)) != sizeof(pid
)) :
166 (write(fd
, buf
, len
) < 0))
168 fprintf(stderr
, "%s: write(%s,%ld): %s\n",
169 Pname
, tempname
, (u_long
)pid
, strerror(errno
));
171 if (unlink(tempname
) < 0) {
172 fprintf(stderr
, E_unlk
,
173 Pname
, tempname
, strerror(errno
));
175 return((char *)NULL
);
182 ** Does the PID exist?
183 ** Send null signal to find out.
186 p_exists(__pid_t pid
)
188 dprintf("%s: process %ld is ", Pname
, (u_long
)pid
);
190 dprintf("invalid\n");
193 if (kill(pid
, 0) < 0) {
197 return(FALSE
); /* pid does not exist */
200 return(TRUE
); /* pid exists */
202 dprintf("state unknown: %s\n", strerror(errno
));
203 return(TRUE
); /* be conservative */
207 return(TRUE
); /* pid exists */
211 ** Check the validity of an existing lock file.
213 ** Read the PID out of the lock
214 ** Send a null signal to determine whether that PID still exists
215 ** Existence (or not) determines the validity of the lock.
217 ** Two bigs wins to this algorithm:
219 ** o Locks do not survive crashes of either the system or the
220 ** application by any appreciable period of time.
222 ** o No clean up to do if the system or application crashes.
226 cklock(char *file
, int uucpstyle
)
228 int fd
= open(file
, O_RDONLY
);
233 dprintf("%s: checking extant lock <%s>\n", Pname
, file
);
236 fprintf(stderr
, E_open
, Pname
, file
, strerror(errno
));
237 return(TRUE
); /* might or might not; conservatism */
241 ((len
= read(fd
, &pid
, sizeof(pid
))) != sizeof(pid
)) :
242 ((len
= read(fd
, buf
, sizeof(buf
))) <= 0))
245 dprintf("%s: lock file format error\n", Pname
);
250 return(p_exists(uucpstyle
? pid
: atoi(buf
)));
254 mklock(char *file
, __pid_t pid
, int uucpstyle
)
259 dprintf("%s: trying lock <%s> for process %ld\n", Pname
, file
,
261 if ((tmp
= xtmpfile(file
, pid
, uucpstyle
)) == (char *)NULL
)
265 if (link(tmp
, file
) < 0) {
268 dprintf("%s: lock <%s> already exists\n", Pname
, file
);
269 if (cklock(file
, uucpstyle
)) {
270 dprintf("%s: extant lock is valid\n", Pname
);
273 dprintf("%s: lock is invalid, removing\n",
275 if (unlink(file
) < 0) {
276 fprintf(stderr
, E_unlk
,
277 Pname
, file
, strerror(errno
));
282 ** I hereby profane the god of structured programming,
287 fprintf(stderr
, "%s: link(%s, %s): %s\n",
288 Pname
, tmp
, file
, strerror(errno
));
292 dprintf("%s: got lock <%s>\n", Pname
, file
);
295 if (unlink(tmp
) < 0) {
296 fprintf(stderr
, E_unlk
, Pname
, tmp
, strerror(errno
));
304 fprintf(stderr
, USAGE
, Pname
, Pname
);
309 main(int ac
, char **av
)
312 char *file
= (char *)NULL
;
314 int uucpstyle
= FALSE
; /* indicating UUCP style locks */
315 int only_check
= TRUE
; /* don't make a lock */
317 Pname
= ((Pname
= strrchr(av
[0], '/')) ? Pname
+ 1 : av
[0]);
319 for(x
= 1; x
< ac
; x
++) {
320 if (av
[x
][0] == '-') {
329 if (strlen(av
[x
]) > 2) {
330 pid
= atoi(&av
[x
][2]);
337 only_check
= FALSE
; /* wants one */
340 if (strlen(av
[x
]) > 2) {
355 if (file
== (char *)NULL
|| (!only_check
&& pid
<= 0)) {
360 exit(cklock(file
, uucpstyle
) ? LOCK_GOOD
: LOCK_BAD
);
363 exit(mklock(file
, pid
, uucpstyle
) ? LOCK_SET
: LOCK_FAIL
);