1 /* $NetBSD: shlock.c,v 1.9 2006/10/07 21:13:00 elad Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Erik E. Fair. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 ** Program to produce reliable locks for shell scripts. 41 ** Algorithm suggested by Peter Honeyman, January 1984, 42 ** in connection with HoneyDanBer UUCP. 43 ** 44 ** I tried extending this to handle shared locks in November 1987, 45 ** and ran into to some fundamental problems: 46 ** 47 ** Neither 4.3 BSD nor System V have an open(2) with locking, 48 ** so that you can open a file and have it locked as soon as 49 ** it's real; you have to make two system calls, and there's 50 ** a race... 51 ** 52 ** When removing dead process id's from a list in a file, 53 ** you need to truncate the file (you don't want to create a 54 ** new one; see above); unfortunately for the portability of 55 ** this program, only 4.3 BSD has ftruncate(2). 56 ** 57 ** Erik E. Fair <fair@ucbarpa.berkeley.edu>, November 8, 1987 58 ** 59 ** Extensions for UUCP style locks (i.e. pid is an int in the file, 60 ** rather than an ASCII string). Also fix long standing bug with 61 ** full file systems and temporary files. 62 ** 63 ** Erik E. Fair <fair@apple.com>, November 12, 1989 64 ** 65 ** ANSIfy the code somewhat to make gcc -Wall happy with the code. 66 ** Submit to NetBSD 67 ** 68 ** Erik E. Fair <fair@clock.org>, May 20, 1997 69 */ 70 71 #include <sys/cdefs.h> 72 73 #ifndef lint 74 __RCSID("$NetBSD: shlock.c,v 1.9 2006/10/07 21:13:00 elad Exp $"); 75 #endif 76 77 #include <sys/types.h> 78 #include <sys/file.h> 79 #include <fcntl.h> /* Needed on hpux */ 80 #include <stdio.h> 81 #include <signal.h> 82 #include <errno.h> 83 #include <string.h> 84 #include <unistd.h> 85 #include <stdlib.h> 86 87 #define LOCK_SET 0 88 #define LOCK_FAIL 1 89 90 #define LOCK_GOOD 0 91 #define LOCK_BAD 1 92 93 #define FAIL (-1) 94 95 #define TRUE 1 96 #define FALSE 0 97 98 int Debug = FALSE; 99 char *Pname; 100 const char USAGE[] = "%s: USAGE: %s [-du] [-p PID] -f file\n"; 101 const char E_unlk[] = "%s: unlink(%s): %s\n"; 102 const char E_open[] = "%s: open(%s): %s\n"; 103 104 #define dprintf if (Debug) printf 105 106 /* 107 ** Prototypes to make the ANSI compilers happy 108 ** Didn't lint used to do type and argument checking? 109 ** (and wasn't that sufficient?) 110 */ 111 112 /* the following is in case you need to make the prototypes go away. */ 113 char *xtmpfile(char *, pid_t, int); 114 int p_exists(pid_t); 115 int cklock(char *, int); 116 int mklock(char *, pid_t, int); 117 void bad_usage(void); 118 int main(int, char **); 119 120 /* 121 ** Create a temporary file, all ready to lock with. 122 ** The file arg is so we get the filename right, if he 123 ** gave us a full path, instead of using the current directory 124 ** which might not be in the same filesystem. 125 */ 126 char * 127 xtmpfile(char *file, __pid_t pid, int uucpstyle) 128 { 129 int fd; 130 int len; 131 char *cp, buf[BUFSIZ]; 132 static char tempname[BUFSIZ]; 133 134 sprintf(buf, "shlock%ld", (u_long)getpid()); 135 if ((cp = strrchr(strcpy(tempname, file), '/')) != (char *)NULL) { 136 *++cp = '\0'; 137 (void) strcat(tempname, buf); 138 } else 139 (void) strcpy(tempname, buf); 140 dprintf("%s: temporary filename: %s\n", Pname, tempname); 141 142 sprintf(buf, "%ld\n", (u_long)pid); 143 len = strlen(buf); 144 openloop: 145 if ((fd = open(tempname, O_RDWR|O_CREAT|O_EXCL, 0644)) < 0) { 146 switch(errno) { 147 case EEXIST: 148 dprintf("%s: file %s exists already.\n", 149 Pname, tempname); 150 if (unlink(tempname) < 0) { 151 fprintf(stderr, E_unlk, 152 Pname, tempname, strerror(errno)); 153 return((char *)NULL); 154 } 155 /* 156 ** Further profanity 157 */ 158 goto openloop; 159 default: 160 fprintf(stderr, E_open, 161 Pname, tempname, strerror(errno)); 162 return((char *)NULL); 163 } 164 } 165 166 /* 167 ** Write the PID into the temporary file before attempting to link 168 ** to the actual lock file. That way we have a valid lock the instant 169 ** the link succeeds. 170 */ 171 if (uucpstyle ? 172 (write(fd, &pid, sizeof(pid)) != sizeof(pid)) : 173 (write(fd, buf, len) < 0)) 174 { 175 fprintf(stderr, "%s: write(%s,%ld): %s\n", 176 Pname, tempname, (u_long)pid, strerror(errno)); 177 (void) close(fd); 178 if (unlink(tempname) < 0) { 179 fprintf(stderr, E_unlk, 180 Pname, tempname, strerror(errno)); 181 } 182 return((char *)NULL); 183 } 184 (void) close(fd); 185 return(tempname); 186 } 187 188 /* 189 ** Does the PID exist? 190 ** Send null signal to find out. 191 */ 192 int 193 p_exists(__pid_t pid) 194 { 195 dprintf("%s: process %ld is ", Pname, (u_long)pid); 196 if (pid <= 0) { 197 dprintf("invalid\n"); 198 return(FALSE); 199 } 200 if (kill(pid, 0) < 0) { 201 switch(errno) { 202 case ESRCH: 203 dprintf("dead\n"); 204 return(FALSE); /* pid does not exist */ 205 case EPERM: 206 dprintf("alive\n"); 207 return(TRUE); /* pid exists */ 208 default: 209 dprintf("state unknown: %s\n", strerror(errno)); 210 return(TRUE); /* be conservative */ 211 } 212 } 213 dprintf("alive\n"); 214 return(TRUE); /* pid exists */ 215 } 216 217 /* 218 ** Check the validity of an existing lock file. 219 ** 220 ** Read the PID out of the lock 221 ** Send a null signal to determine whether that PID still exists 222 ** Existence (or not) determines the validity of the lock. 223 ** 224 ** Two bigs wins to this algorithm: 225 ** 226 ** o Locks do not survive crashes of either the system or the 227 ** application by any appreciable period of time. 228 ** 229 ** o No clean up to do if the system or application crashes. 230 ** 231 */ 232 int 233 cklock(char *file, int uucpstyle) 234 { 235 int fd = open(file, O_RDONLY); 236 ssize_t len; 237 pid_t pid; 238 char buf[BUFSIZ]; 239 240 dprintf("%s: checking extant lock <%s>\n", Pname, file); 241 if (fd < 0) { 242 if (errno != ENOENT) 243 fprintf(stderr, E_open, Pname, file, strerror(errno)); 244 return(TRUE); /* might or might not; conservatism */ 245 } 246 247 if (uucpstyle ? 248 ((len = read(fd, &pid, sizeof(pid))) != sizeof(pid)) : 249 ((len = read(fd, buf, sizeof(buf))) <= 0)) 250 { 251 close(fd); 252 dprintf("%s: lock file format error\n", Pname); 253 return(FALSE); 254 } 255 close(fd); 256 buf[len + 1] = '\0'; 257 return(p_exists(uucpstyle ? pid : atoi(buf))); 258 } 259 260 int 261 mklock(char *file, __pid_t pid, int uucpstyle) 262 { 263 char *tmp; 264 int retcode = FALSE; 265 266 dprintf("%s: trying lock <%s> for process %ld\n", Pname, file, 267 (u_long)pid); 268 if ((tmp = xtmpfile(file, pid, uucpstyle)) == (char *)NULL) 269 return(FALSE); 270 271 linkloop: 272 if (link(tmp, file) < 0) { 273 switch(errno) { 274 case EEXIST: 275 dprintf("%s: lock <%s> already exists\n", Pname, file); 276 if (cklock(file, uucpstyle)) { 277 dprintf("%s: extant lock is valid\n", Pname); 278 break; 279 } else { 280 dprintf("%s: lock is invalid, removing\n", 281 Pname); 282 if (unlink(file) < 0) { 283 fprintf(stderr, E_unlk, 284 Pname, file, strerror(errno)); 285 break; 286 } 287 } 288 /* 289 ** I hereby profane the god of structured programming, 290 ** Edsgar Dijkstra 291 */ 292 goto linkloop; 293 default: 294 fprintf(stderr, "%s: link(%s, %s): %s\n", 295 Pname, tmp, file, strerror(errno)); 296 break; 297 } 298 } else { 299 dprintf("%s: got lock <%s>\n", Pname, file); 300 retcode = TRUE; 301 } 302 if (unlink(tmp) < 0) { 303 fprintf(stderr, E_unlk, Pname, tmp, strerror(errno)); 304 } 305 return(retcode); 306 } 307 308 void 309 bad_usage(void) 310 { 311 fprintf(stderr, USAGE, Pname, Pname); 312 exit(LOCK_FAIL); 313 } 314 315 int 316 main(int ac, char **av) 317 { 318 int x; 319 char *file = (char *)NULL; 320 pid_t pid = 0; 321 int uucpstyle = FALSE; /* indicating UUCP style locks */ 322 int only_check = TRUE; /* don't make a lock */ 323 324 Pname = ((Pname = strrchr(av[0], '/')) ? Pname + 1 : av[0]); 325 326 for(x = 1; x < ac; x++) { 327 if (av[x][0] == '-') { 328 switch(av[x][1]) { 329 case 'u': 330 uucpstyle = TRUE; 331 break; 332 case 'd': 333 Debug = TRUE; 334 break; 335 case 'p': 336 if (strlen(av[x]) > 2) { 337 pid = atoi(&av[x][2]); 338 } else { 339 if (++x >= ac) { 340 bad_usage(); 341 } 342 pid = atoi(av[x]); 343 } 344 only_check = FALSE; /* wants one */ 345 break; 346 case 'f': 347 if (strlen(av[x]) > 2) { 348 file = &av[x][2]; 349 } else { 350 if (++x >= ac) { 351 bad_usage(); 352 } 353 file = av[x]; 354 } 355 break; 356 default: 357 bad_usage(); 358 } 359 } 360 } 361 362 if (file == (char *)NULL || (!only_check && pid <= 0)) { 363 bad_usage(); 364 } 365 366 if (only_check) { 367 exit(cklock(file, uucpstyle) ? LOCK_GOOD : LOCK_BAD); 368 } 369 370 exit(mklock(file, pid, uucpstyle) ? LOCK_SET : LOCK_FAIL); 371 } 372