11de7b4b8SPedro F. Giffuni /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 31de7b4b8SPedro F. Giffuni * 4c8929a49SJohn Polstra * Copyright (C) 1997 John D. Polstra. All rights reserved. 5c8929a49SJohn Polstra * 6c8929a49SJohn Polstra * Redistribution and use in source and binary forms, with or without 7c8929a49SJohn Polstra * modification, are permitted provided that the following conditions 8c8929a49SJohn Polstra * are met: 9c8929a49SJohn Polstra * 1. Redistributions of source code must retain the above copyright 10c8929a49SJohn Polstra * notice, this list of conditions and the following disclaimer. 11c8929a49SJohn Polstra * 2. Redistributions in binary form must reproduce the above copyright 12c8929a49SJohn Polstra * notice, this list of conditions and the following disclaimer in the 13c8929a49SJohn Polstra * documentation and/or other materials provided with the distribution. 14c8929a49SJohn Polstra * 15c8929a49SJohn Polstra * THIS SOFTWARE IS PROVIDED BY JOHN D. POLSTRA AND CONTRIBUTORS ``AS IS'' AND 16c8929a49SJohn Polstra * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17c8929a49SJohn Polstra * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18c8929a49SJohn Polstra * ARE DISCLAIMED. IN NO EVENT SHALL JOHN D. POLSTRA OR CONTRIBUTORS BE LIABLE 19c8929a49SJohn Polstra * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20c8929a49SJohn Polstra * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21c8929a49SJohn Polstra * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22c8929a49SJohn Polstra * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23c8929a49SJohn Polstra * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24c8929a49SJohn Polstra * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25c8929a49SJohn Polstra * SUCH DAMAGE. 26c8929a49SJohn Polstra */ 27c8929a49SJohn Polstra 28e026a48cSDavid E. O'Brien #include <sys/cdefs.h> 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 41*3041e695SKyle Evans static int acquire_lock(const char *name, int flags, int silent); 42c8929a49SJohn Polstra static void cleanup(void); 43c8929a49SJohn Polstra static void killed(int sig); 44c8929a49SJohn Polstra static void timeout(int sig); 45a1b6427aSAlfonso Gregory static void usage(void) __dead2; 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 { 59b5be420aSEitan Adler int ch, flags, silent, status, waitsec; 60c8929a49SJohn Polstra pid_t child; 61c8929a49SJohn Polstra 6245edbdccSChristian S.J. Peron silent = keep = 0; 63437bab48SColin Percival flags = O_CREAT | O_RDONLY; 64c8929a49SJohn Polstra waitsec = -1; /* Infinite. */ 65437bab48SColin Percival while ((ch = getopt(argc, argv, "sknt:w")) != -1) { 66c8929a49SJohn Polstra switch (ch) { 672f278eacSJohn Polstra case 'k': 682f278eacSJohn Polstra keep = 1; 692f278eacSJohn Polstra break; 70b5be420aSEitan Adler case 'n': 71b5be420aSEitan Adler flags &= ~O_CREAT; 72b5be420aSEitan Adler break; 73c8929a49SJohn Polstra case 's': 74c8929a49SJohn Polstra silent = 1; 75c8929a49SJohn Polstra break; 76c8929a49SJohn Polstra case 't': 77c8929a49SJohn Polstra { 78c8929a49SJohn Polstra char *endptr; 79c8929a49SJohn Polstra waitsec = strtol(optarg, &endptr, 0); 80c8929a49SJohn Polstra if (*optarg == '\0' || *endptr != '\0' || waitsec < 0) 8145edbdccSChristian S.J. Peron errx(EX_USAGE, 8245edbdccSChristian S.J. Peron "invalid timeout \"%s\"", optarg); 83c8929a49SJohn Polstra } 84c8929a49SJohn Polstra break; 85437bab48SColin Percival case 'w': 86437bab48SColin Percival flags = (flags & ~O_RDONLY) | O_WRONLY; 87437bab48SColin Percival break; 88c8929a49SJohn Polstra default: 89c8929a49SJohn Polstra usage(); 90c8929a49SJohn Polstra } 91c8929a49SJohn Polstra } 92c8929a49SJohn Polstra if (argc - optind < 2) 93c8929a49SJohn Polstra usage(); 94c8929a49SJohn Polstra lockname = argv[optind++]; 95c8929a49SJohn Polstra argc -= optind; 96c8929a49SJohn Polstra argv += optind; 97c8929a49SJohn Polstra if (waitsec > 0) { /* Set up a timeout. */ 98c8929a49SJohn Polstra struct sigaction act; 99c8929a49SJohn Polstra 100c8929a49SJohn Polstra act.sa_handler = timeout; 101c8929a49SJohn Polstra sigemptyset(&act.sa_mask); 102c8929a49SJohn Polstra act.sa_flags = 0; /* Note that we do not set SA_RESTART. */ 103c8929a49SJohn Polstra sigaction(SIGALRM, &act, NULL); 104c8929a49SJohn Polstra alarm(waitsec); 105c8929a49SJohn Polstra } 1066a53f0a5SChristian S.J. Peron /* 1076a53f0a5SChristian S.J. Peron * If the "-k" option is not given, then we must not block when 1086a53f0a5SChristian S.J. Peron * acquiring the lock. If we did, then the lock holder would 1096a53f0a5SChristian S.J. Peron * unlink the file upon releasing the lock, and we would acquire 1106a53f0a5SChristian S.J. Peron * a lock on a file with no directory entry. Then another 1116a53f0a5SChristian S.J. Peron * process could come along and acquire the same lock. To avoid 1126a53f0a5SChristian S.J. Peron * this problem, we separate out the actions of waiting for the 1136a53f0a5SChristian S.J. Peron * lock to be available and of actually acquiring the lock. 1146a53f0a5SChristian S.J. Peron * 1156a53f0a5SChristian S.J. Peron * That approach produces behavior that is technically correct; 1166a53f0a5SChristian S.J. Peron * however, it causes some performance & ordering problems for 1176a53f0a5SChristian S.J. Peron * locks that have a lot of contention. First, it is unfair in 1186a53f0a5SChristian S.J. Peron * the sense that a released lock isn't necessarily granted to 1196a53f0a5SChristian S.J. Peron * the process that has been waiting the longest. A waiter may 1206a53f0a5SChristian S.J. Peron * be starved out indefinitely. Second, it creates a thundering 1216a53f0a5SChristian S.J. Peron * herd situation each time the lock is released. 1226a53f0a5SChristian S.J. Peron * 1236a53f0a5SChristian S.J. Peron * When the "-k" option is used, the unlink race no longer 1246a53f0a5SChristian S.J. Peron * exists. In that case we can block while acquiring the lock, 1256a53f0a5SChristian S.J. Peron * avoiding the separate step of waiting for the lock. This 1266a53f0a5SChristian S.J. Peron * yields fairness and improved performance. 1276a53f0a5SChristian S.J. Peron */ 128*3041e695SKyle Evans lockfd = acquire_lock(lockname, flags | O_NONBLOCK, silent); 1296a53f0a5SChristian S.J. Peron while (lockfd == -1 && !timed_out && waitsec != 0) { 1306a53f0a5SChristian S.J. Peron if (keep) 131*3041e695SKyle Evans lockfd = acquire_lock(lockname, flags, silent); 1326a53f0a5SChristian S.J. Peron else { 1336a53f0a5SChristian S.J. Peron wait_for_lock(lockname); 134*3041e695SKyle Evans lockfd = acquire_lock(lockname, flags | O_NONBLOCK, 135*3041e695SKyle Evans silent); 1366a53f0a5SChristian S.J. Peron } 1376a53f0a5SChristian S.J. Peron } 138c8929a49SJohn Polstra if (waitsec > 0) 139c8929a49SJohn Polstra alarm(0); 140c8929a49SJohn Polstra if (lockfd == -1) { /* We failed to acquire the lock. */ 141c8929a49SJohn Polstra if (silent) 142ae06cb4dSJohn Polstra exit(EX_TEMPFAIL); 143ae06cb4dSJohn Polstra errx(EX_TEMPFAIL, "%s: already locked", lockname); 144c8929a49SJohn Polstra } 145c8929a49SJohn Polstra /* At this point, we own the lock. */ 146c8929a49SJohn Polstra if (atexit(cleanup) == -1) 147ae06cb4dSJohn Polstra err(EX_OSERR, "atexit failed"); 148c8929a49SJohn Polstra if ((child = fork()) == -1) 149ae06cb4dSJohn Polstra err(EX_OSERR, "cannot fork"); 150c8929a49SJohn Polstra if (child == 0) { /* The child process. */ 151c8929a49SJohn Polstra close(lockfd); 152c8929a49SJohn Polstra execvp(argv[0], argv); 153208f2fd1STim J. Robbins warn("%s", argv[0]); 154c8929a49SJohn Polstra _exit(1); 155c8929a49SJohn Polstra } 156c8929a49SJohn Polstra /* This is the parent process. */ 157c8929a49SJohn Polstra signal(SIGINT, SIG_IGN); 158c8929a49SJohn Polstra signal(SIGQUIT, SIG_IGN); 159c8929a49SJohn Polstra signal(SIGTERM, killed); 160c8929a49SJohn Polstra if (waitpid(child, &status, 0) == -1) 161ae06cb4dSJohn Polstra err(EX_OSERR, "waitpid failed"); 1627f15c32dSDavid Malone return (WIFEXITED(status) ? WEXITSTATUS(status) : EX_SOFTWARE); 163c8929a49SJohn Polstra } 164c8929a49SJohn Polstra 165c8929a49SJohn Polstra /* 1666a53f0a5SChristian S.J. Peron * Try to acquire a lock on the given file, creating the file if 1676a53f0a5SChristian S.J. Peron * necessary. The flags argument is O_NONBLOCK or 0, depending on 1686a53f0a5SChristian S.J. Peron * whether we should wait for the lock. Returns an open file descriptor 1696a53f0a5SChristian S.J. Peron * on success, or -1 on failure. 1706a53f0a5SChristian S.J. Peron */ 1716a53f0a5SChristian S.J. Peron static int 172*3041e695SKyle Evans acquire_lock(const char *name, int flags, int silent) 1736a53f0a5SChristian S.J. Peron { 1746a53f0a5SChristian S.J. Peron int fd; 1756a53f0a5SChristian S.J. Peron 176437bab48SColin Percival if ((fd = open(name, O_EXLOCK|flags, 0666)) == -1) { 1776a53f0a5SChristian S.J. Peron if (errno == EAGAIN || errno == EINTR) 1786a53f0a5SChristian S.J. Peron return (-1); 179*3041e695SKyle Evans else if (errno == ENOENT && (flags & O_CREAT) == 0) { 180*3041e695SKyle Evans if (!silent) 181*3041e695SKyle Evans warn("%s", name); 182*3041e695SKyle Evans exit(EX_UNAVAILABLE); 183*3041e695SKyle Evans } 1846a53f0a5SChristian S.J. Peron err(EX_CANTCREAT, "cannot open %s", name); 1856a53f0a5SChristian S.J. Peron } 1866a53f0a5SChristian S.J. Peron return (fd); 1876a53f0a5SChristian S.J. Peron } 1886a53f0a5SChristian S.J. Peron 1896a53f0a5SChristian S.J. Peron /* 190c8929a49SJohn Polstra * Remove the lock file. 191c8929a49SJohn Polstra */ 192c8929a49SJohn Polstra static void 193c8929a49SJohn Polstra cleanup(void) 194c8929a49SJohn Polstra { 195cc2a9f52SChristian S.J. Peron 1962f278eacSJohn Polstra if (keep) 1972f278eacSJohn Polstra flock(lockfd, LOCK_UN); 1982f278eacSJohn Polstra else 199ae06cb4dSJohn Polstra unlink(lockname); 200c8929a49SJohn Polstra } 201c8929a49SJohn Polstra 202c8929a49SJohn Polstra /* 203c8929a49SJohn Polstra * Signal handler for SIGTERM. Cleans up the lock file, then re-raises 204c8929a49SJohn Polstra * the signal. 205c8929a49SJohn Polstra */ 206c8929a49SJohn Polstra static void 207c8929a49SJohn Polstra killed(int sig) 208c8929a49SJohn Polstra { 209cc2a9f52SChristian S.J. Peron 210c8929a49SJohn Polstra cleanup(); 211c8929a49SJohn Polstra signal(sig, SIG_DFL); 212c8929a49SJohn Polstra if (kill(getpid(), sig) == -1) 213ae06cb4dSJohn Polstra err(EX_OSERR, "kill failed"); 214c8929a49SJohn Polstra } 215c8929a49SJohn Polstra 216c8929a49SJohn Polstra /* 217c8929a49SJohn Polstra * Signal handler for SIGALRM. 218c8929a49SJohn Polstra */ 219c8929a49SJohn Polstra static void 2209ff5e898SDavid Malone timeout(int sig __unused) 221c8929a49SJohn Polstra { 222cc2a9f52SChristian S.J. Peron 223c8929a49SJohn Polstra timed_out = 1; 224c8929a49SJohn Polstra } 225c8929a49SJohn Polstra 226c8929a49SJohn Polstra static void 227c8929a49SJohn Polstra usage(void) 228c8929a49SJohn Polstra { 229cc2a9f52SChristian S.J. Peron 2303ec665d4SPhilippe Charnier fprintf(stderr, 231b5be420aSEitan Adler "usage: lockf [-kns] [-t seconds] file command [arguments]\n"); 2323ec665d4SPhilippe Charnier exit(EX_USAGE); 233c8929a49SJohn Polstra } 234c8929a49SJohn Polstra 235c8929a49SJohn Polstra /* 236c8929a49SJohn Polstra * Wait until it might be possible to acquire a lock on the given file. 2376a53f0a5SChristian S.J. Peron * If the file does not exist, return immediately without creating it. 238c8929a49SJohn Polstra */ 2396a53f0a5SChristian S.J. Peron static void 2406a53f0a5SChristian S.J. Peron wait_for_lock(const char *name) 241c8929a49SJohn Polstra { 242c8929a49SJohn Polstra int fd; 243c8929a49SJohn Polstra 2446a53f0a5SChristian S.J. Peron if ((fd = open(name, O_RDONLY|O_EXLOCK, 0666)) == -1) { 2456a53f0a5SChristian S.J. Peron if (errno == ENOENT || errno == EINTR) 2466a53f0a5SChristian S.J. Peron return; 247ae06cb4dSJohn Polstra err(EX_CANTCREAT, "cannot open %s", name); 248c8929a49SJohn Polstra } 2496a53f0a5SChristian S.J. Peron close(fd); 250c8929a49SJohn Polstra } 251