1c8929a49SJohn Polstra /* 2c8929a49SJohn Polstra * Copyright (C) 1997 John D. Polstra. All rights reserved. 3c8929a49SJohn Polstra * 4c8929a49SJohn Polstra * Redistribution and use in source and binary forms, with or without 5c8929a49SJohn Polstra * modification, are permitted provided that the following conditions 6c8929a49SJohn Polstra * are met: 7c8929a49SJohn Polstra * 1. Redistributions of source code must retain the above copyright 8c8929a49SJohn Polstra * notice, this list of conditions and the following disclaimer. 9c8929a49SJohn Polstra * 2. Redistributions in binary form must reproduce the above copyright 10c8929a49SJohn Polstra * notice, this list of conditions and the following disclaimer in the 11c8929a49SJohn Polstra * documentation and/or other materials provided with the distribution. 12c8929a49SJohn Polstra * 13c8929a49SJohn Polstra * THIS SOFTWARE IS PROVIDED BY JOHN D. POLSTRA AND CONTRIBUTORS ``AS IS'' AND 14c8929a49SJohn Polstra * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15c8929a49SJohn Polstra * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16c8929a49SJohn Polstra * ARE DISCLAIMED. IN NO EVENT SHALL JOHN D. POLSTRA OR CONTRIBUTORS BE LIABLE 17c8929a49SJohn Polstra * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18c8929a49SJohn Polstra * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19c8929a49SJohn Polstra * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20c8929a49SJohn Polstra * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21c8929a49SJohn Polstra * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22c8929a49SJohn Polstra * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23c8929a49SJohn Polstra * SUCH DAMAGE. 24c8929a49SJohn Polstra */ 25c8929a49SJohn Polstra 26e026a48cSDavid E. O'Brien #include <sys/cdefs.h> 27e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$"); 28e026a48cSDavid E. O'Brien 29c8929a49SJohn Polstra #include <sys/types.h> 30c8929a49SJohn Polstra #include <sys/wait.h> 31c8929a49SJohn Polstra 32c8929a49SJohn Polstra #include <err.h> 33c8929a49SJohn Polstra #include <errno.h> 34c8929a49SJohn Polstra #include <fcntl.h> 35c8929a49SJohn Polstra #include <signal.h> 36c8929a49SJohn Polstra #include <stdio.h> 37c8929a49SJohn Polstra #include <stdlib.h> 38ae06cb4dSJohn Polstra #include <sysexits.h> 39c8929a49SJohn Polstra #include <unistd.h> 40c8929a49SJohn Polstra 416a53f0a5SChristian S.J. Peron static int acquire_lock(const char *name, int flags); 42c8929a49SJohn Polstra static void cleanup(void); 43c8929a49SJohn Polstra static void killed(int sig); 44c8929a49SJohn Polstra static void timeout(int sig); 45c8929a49SJohn Polstra static void usage(void); 466a53f0a5SChristian S.J. Peron static void wait_for_lock(const char *name); 47c8929a49SJohn Polstra 48c8929a49SJohn Polstra static const char *lockname; 49031469ebSChristian S.J. Peron static int lockfd = -1; 502f278eacSJohn Polstra static int keep; 51c8929a49SJohn Polstra static volatile sig_atomic_t timed_out; 52c8929a49SJohn Polstra 53c8929a49SJohn Polstra /* 54c8929a49SJohn Polstra * Execute an arbitrary command while holding a file lock. 55c8929a49SJohn Polstra */ 56c8929a49SJohn Polstra int 57c8929a49SJohn Polstra main(int argc, char **argv) 58c8929a49SJohn Polstra { 5945edbdccSChristian S.J. Peron int ch, silent, status, waitsec; 60c8929a49SJohn Polstra pid_t child; 61c8929a49SJohn Polstra 6245edbdccSChristian S.J. Peron silent = keep = 0; 63c8929a49SJohn Polstra waitsec = -1; /* Infinite. */ 642f278eacSJohn Polstra while ((ch = getopt(argc, argv, "skt:")) != -1) { 65c8929a49SJohn Polstra switch (ch) { 662f278eacSJohn Polstra case 'k': 672f278eacSJohn Polstra keep = 1; 682f278eacSJohn Polstra break; 69c8929a49SJohn Polstra case 's': 70c8929a49SJohn Polstra silent = 1; 71c8929a49SJohn Polstra break; 72c8929a49SJohn Polstra case 't': 73c8929a49SJohn Polstra { 74c8929a49SJohn Polstra char *endptr; 75c8929a49SJohn Polstra waitsec = strtol(optarg, &endptr, 0); 76c8929a49SJohn Polstra if (*optarg == '\0' || *endptr != '\0' || waitsec < 0) 7745edbdccSChristian S.J. Peron errx(EX_USAGE, 7845edbdccSChristian S.J. Peron "invalid timeout \"%s\"", optarg); 79c8929a49SJohn Polstra } 80c8929a49SJohn Polstra break; 81c8929a49SJohn Polstra default: 82c8929a49SJohn Polstra usage(); 83c8929a49SJohn Polstra } 84c8929a49SJohn Polstra } 85c8929a49SJohn Polstra if (argc - optind < 2) 86c8929a49SJohn Polstra usage(); 87c8929a49SJohn Polstra lockname = argv[optind++]; 88c8929a49SJohn Polstra argc -= optind; 89c8929a49SJohn Polstra argv += optind; 90c8929a49SJohn Polstra if (waitsec > 0) { /* Set up a timeout. */ 91c8929a49SJohn Polstra struct sigaction act; 92c8929a49SJohn Polstra 93c8929a49SJohn Polstra act.sa_handler = timeout; 94c8929a49SJohn Polstra sigemptyset(&act.sa_mask); 95c8929a49SJohn Polstra act.sa_flags = 0; /* Note that we do not set SA_RESTART. */ 96c8929a49SJohn Polstra sigaction(SIGALRM, &act, NULL); 97c8929a49SJohn Polstra alarm(waitsec); 98c8929a49SJohn Polstra } 996a53f0a5SChristian S.J. Peron /* 1006a53f0a5SChristian S.J. Peron * If the "-k" option is not given, then we must not block when 1016a53f0a5SChristian S.J. Peron * acquiring the lock. If we did, then the lock holder would 1026a53f0a5SChristian S.J. Peron * unlink the file upon releasing the lock, and we would acquire 1036a53f0a5SChristian S.J. Peron * a lock on a file with no directory entry. Then another 1046a53f0a5SChristian S.J. Peron * process could come along and acquire the same lock. To avoid 1056a53f0a5SChristian S.J. Peron * this problem, we separate out the actions of waiting for the 1066a53f0a5SChristian S.J. Peron * lock to be available and of actually acquiring the lock. 1076a53f0a5SChristian S.J. Peron * 1086a53f0a5SChristian S.J. Peron * That approach produces behavior that is technically correct; 1096a53f0a5SChristian S.J. Peron * however, it causes some performance & ordering problems for 1106a53f0a5SChristian S.J. Peron * locks that have a lot of contention. First, it is unfair in 1116a53f0a5SChristian S.J. Peron * the sense that a released lock isn't necessarily granted to 1126a53f0a5SChristian S.J. Peron * the process that has been waiting the longest. A waiter may 1136a53f0a5SChristian S.J. Peron * be starved out indefinitely. Second, it creates a thundering 1146a53f0a5SChristian S.J. Peron * herd situation each time the lock is released. 1156a53f0a5SChristian S.J. Peron * 1166a53f0a5SChristian S.J. Peron * When the "-k" option is used, the unlink race no longer 1176a53f0a5SChristian S.J. Peron * exists. In that case we can block while acquiring the lock, 1186a53f0a5SChristian S.J. Peron * avoiding the separate step of waiting for the lock. This 1196a53f0a5SChristian S.J. Peron * yields fairness and improved performance. 1206a53f0a5SChristian S.J. Peron */ 1216a53f0a5SChristian S.J. Peron lockfd = acquire_lock(lockname, O_NONBLOCK); 1226a53f0a5SChristian S.J. Peron while (lockfd == -1 && !timed_out && waitsec != 0) { 1236a53f0a5SChristian S.J. Peron if (keep) 1246a53f0a5SChristian S.J. Peron lockfd = acquire_lock(lockname, 0); 1256a53f0a5SChristian S.J. Peron else { 1266a53f0a5SChristian S.J. Peron wait_for_lock(lockname); 1276a53f0a5SChristian S.J. Peron lockfd = acquire_lock(lockname, O_NONBLOCK); 1286a53f0a5SChristian S.J. Peron } 1296a53f0a5SChristian S.J. Peron } 130c8929a49SJohn Polstra if (waitsec > 0) 131c8929a49SJohn Polstra alarm(0); 132c8929a49SJohn Polstra if (lockfd == -1) { /* We failed to acquire the lock. */ 133c8929a49SJohn Polstra if (silent) 134ae06cb4dSJohn Polstra exit(EX_TEMPFAIL); 135ae06cb4dSJohn Polstra errx(EX_TEMPFAIL, "%s: already locked", lockname); 136c8929a49SJohn Polstra } 137c8929a49SJohn Polstra /* At this point, we own the lock. */ 138c8929a49SJohn Polstra if (atexit(cleanup) == -1) 139ae06cb4dSJohn Polstra err(EX_OSERR, "atexit failed"); 140c8929a49SJohn Polstra if ((child = fork()) == -1) 141ae06cb4dSJohn Polstra err(EX_OSERR, "cannot fork"); 142c8929a49SJohn Polstra if (child == 0) { /* The child process. */ 143c8929a49SJohn Polstra close(lockfd); 144c8929a49SJohn Polstra execvp(argv[0], argv); 145208f2fd1STim J. Robbins warn("%s", argv[0]); 146c8929a49SJohn Polstra _exit(1); 147c8929a49SJohn Polstra } 148c8929a49SJohn Polstra /* This is the parent process. */ 149c8929a49SJohn Polstra signal(SIGINT, SIG_IGN); 150c8929a49SJohn Polstra signal(SIGQUIT, SIG_IGN); 151c8929a49SJohn Polstra signal(SIGTERM, killed); 152c8929a49SJohn Polstra if (waitpid(child, &status, 0) == -1) 153ae06cb4dSJohn Polstra err(EX_OSERR, "waitpid failed"); 1547f15c32dSDavid Malone return (WIFEXITED(status) ? WEXITSTATUS(status) : EX_SOFTWARE); 155c8929a49SJohn Polstra } 156c8929a49SJohn Polstra 157c8929a49SJohn Polstra /* 1586a53f0a5SChristian S.J. Peron * Try to acquire a lock on the given file, creating the file if 1596a53f0a5SChristian S.J. Peron * necessary. The flags argument is O_NONBLOCK or 0, depending on 1606a53f0a5SChristian S.J. Peron * whether we should wait for the lock. Returns an open file descriptor 1616a53f0a5SChristian S.J. Peron * on success, or -1 on failure. 1626a53f0a5SChristian S.J. Peron */ 1636a53f0a5SChristian S.J. Peron static int 1646a53f0a5SChristian S.J. Peron acquire_lock(const char *name, int flags) 1656a53f0a5SChristian S.J. Peron { 1666a53f0a5SChristian S.J. Peron int fd; 1676a53f0a5SChristian S.J. Peron 1686a53f0a5SChristian S.J. Peron if ((fd = open(name, O_RDONLY|O_CREAT|O_EXLOCK|flags, 0666)) == -1) { 1696a53f0a5SChristian S.J. Peron if (errno == EAGAIN || errno == EINTR) 1706a53f0a5SChristian S.J. Peron return (-1); 1716a53f0a5SChristian S.J. Peron err(EX_CANTCREAT, "cannot open %s", name); 1726a53f0a5SChristian S.J. Peron } 1736a53f0a5SChristian S.J. Peron return (fd); 1746a53f0a5SChristian S.J. Peron } 1756a53f0a5SChristian S.J. Peron 1766a53f0a5SChristian S.J. Peron /* 177c8929a49SJohn Polstra * Remove the lock file. 178c8929a49SJohn Polstra */ 179c8929a49SJohn Polstra static void 180c8929a49SJohn Polstra cleanup(void) 181c8929a49SJohn Polstra { 182cc2a9f52SChristian S.J. Peron 1832f278eacSJohn Polstra if (keep) 1842f278eacSJohn Polstra flock(lockfd, LOCK_UN); 1852f278eacSJohn Polstra else 186ae06cb4dSJohn Polstra unlink(lockname); 187c8929a49SJohn Polstra } 188c8929a49SJohn Polstra 189c8929a49SJohn Polstra /* 190c8929a49SJohn Polstra * Signal handler for SIGTERM. Cleans up the lock file, then re-raises 191c8929a49SJohn Polstra * the signal. 192c8929a49SJohn Polstra */ 193c8929a49SJohn Polstra static void 194c8929a49SJohn Polstra killed(int sig) 195c8929a49SJohn Polstra { 196cc2a9f52SChristian S.J. Peron 197c8929a49SJohn Polstra cleanup(); 198c8929a49SJohn Polstra signal(sig, SIG_DFL); 199c8929a49SJohn Polstra if (kill(getpid(), sig) == -1) 200ae06cb4dSJohn Polstra err(EX_OSERR, "kill failed"); 201c8929a49SJohn Polstra } 202c8929a49SJohn Polstra 203c8929a49SJohn Polstra /* 204c8929a49SJohn Polstra * Signal handler for SIGALRM. 205c8929a49SJohn Polstra */ 206c8929a49SJohn Polstra static void 2079ff5e898SDavid Malone timeout(int sig __unused) 208c8929a49SJohn Polstra { 209cc2a9f52SChristian S.J. Peron 210c8929a49SJohn Polstra timed_out = 1; 211c8929a49SJohn Polstra } 212c8929a49SJohn Polstra 213c8929a49SJohn Polstra static void 214c8929a49SJohn Polstra usage(void) 215c8929a49SJohn Polstra { 216cc2a9f52SChristian S.J. Peron 2173ec665d4SPhilippe Charnier fprintf(stderr, 2182f278eacSJohn Polstra "usage: lockf [-ks] [-t seconds] file command [arguments]\n"); 2193ec665d4SPhilippe Charnier exit(EX_USAGE); 220c8929a49SJohn Polstra } 221c8929a49SJohn Polstra 222c8929a49SJohn Polstra /* 223c8929a49SJohn Polstra * Wait until it might be possible to acquire a lock on the given file. 2246a53f0a5SChristian S.J. Peron * If the file does not exist, return immediately without creating it. 225c8929a49SJohn Polstra */ 2266a53f0a5SChristian S.J. Peron static void 2276a53f0a5SChristian S.J. Peron wait_for_lock(const char *name) 228c8929a49SJohn Polstra { 229c8929a49SJohn Polstra int fd; 230c8929a49SJohn Polstra 2316a53f0a5SChristian S.J. Peron if ((fd = open(name, O_RDONLY|O_EXLOCK, 0666)) == -1) { 2326a53f0a5SChristian S.J. Peron if (errno == ENOENT || errno == EINTR) 2336a53f0a5SChristian S.J. Peron return; 234ae06cb4dSJohn Polstra err(EX_CANTCREAT, "cannot open %s", name); 235c8929a49SJohn Polstra } 2366a53f0a5SChristian S.J. Peron close(fd); 237c8929a49SJohn Polstra } 238