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 32*09a7fe0aSKyle Evans #include <assert.h> 33c8929a49SJohn Polstra #include <err.h> 34c8929a49SJohn Polstra #include <errno.h> 35c8929a49SJohn Polstra #include <fcntl.h> 36*09a7fe0aSKyle Evans #include <limits.h> 37c8929a49SJohn Polstra #include <signal.h> 38c8929a49SJohn Polstra #include <stdio.h> 39c8929a49SJohn Polstra #include <stdlib.h> 40*09a7fe0aSKyle Evans #include <string.h> 41ae06cb4dSJohn Polstra #include <sysexits.h> 42c8929a49SJohn Polstra #include <unistd.h> 43c8929a49SJohn Polstra 44*09a7fe0aSKyle Evans #define FDLOCK_PREFIX "/dev/fd/" 45*09a7fe0aSKyle Evans 46*09a7fe0aSKyle Evans union lock_subject { 47*09a7fe0aSKyle Evans long subj_fd; 48*09a7fe0aSKyle Evans const char *subj_name; 49*09a7fe0aSKyle Evans }; 50*09a7fe0aSKyle Evans 51*09a7fe0aSKyle Evans static int acquire_lock(union lock_subject *subj, int flags, int silent); 52c8929a49SJohn Polstra static void cleanup(void); 53c8929a49SJohn Polstra static void killed(int sig); 54c8929a49SJohn Polstra static void timeout(int sig); 55a1b6427aSAlfonso Gregory static void usage(void) __dead2; 566a53f0a5SChristian S.J. Peron static void wait_for_lock(const char *name); 57c8929a49SJohn Polstra 58c8929a49SJohn Polstra static const char *lockname; 59031469ebSChristian S.J. Peron static int lockfd = -1; 602f278eacSJohn Polstra static int keep; 61*09a7fe0aSKyle Evans static int fdlock; 62c8929a49SJohn Polstra static volatile sig_atomic_t timed_out; 63c8929a49SJohn Polstra 64c8929a49SJohn Polstra /* 65*09a7fe0aSKyle Evans * Check if fdlock is implied by the given `lockname`. We'll write the fd that 66*09a7fe0aSKyle Evans * is represented by it out to ofd, and the caller is expected to do any 67*09a7fe0aSKyle Evans * necessary validation on it. 68*09a7fe0aSKyle Evans */ 69*09a7fe0aSKyle Evans static int 70*09a7fe0aSKyle Evans fdlock_implied(const char *name, long *ofd) 71*09a7fe0aSKyle Evans { 72*09a7fe0aSKyle Evans char *endp; 73*09a7fe0aSKyle Evans long fd; 74*09a7fe0aSKyle Evans 75*09a7fe0aSKyle Evans if (strncmp(name, FDLOCK_PREFIX, sizeof(FDLOCK_PREFIX) - 1) != 0) 76*09a7fe0aSKyle Evans return (0); 77*09a7fe0aSKyle Evans 78*09a7fe0aSKyle Evans /* Skip past the prefix. */ 79*09a7fe0aSKyle Evans name += sizeof(FDLOCK_PREFIX) - 1; 80*09a7fe0aSKyle Evans errno = 0; 81*09a7fe0aSKyle Evans fd = strtol(name, &endp, 10); 82*09a7fe0aSKyle Evans if (errno != 0 || *endp != '\0') 83*09a7fe0aSKyle Evans return (0); 84*09a7fe0aSKyle Evans 85*09a7fe0aSKyle Evans *ofd = fd; 86*09a7fe0aSKyle Evans return (1); 87*09a7fe0aSKyle Evans } 88*09a7fe0aSKyle Evans 89*09a7fe0aSKyle Evans /* 90c8929a49SJohn Polstra * Execute an arbitrary command while holding a file lock. 91c8929a49SJohn Polstra */ 92c8929a49SJohn Polstra int 93c8929a49SJohn Polstra main(int argc, char **argv) 94c8929a49SJohn Polstra { 95b5be420aSEitan Adler int ch, flags, silent, status, waitsec; 96c8929a49SJohn Polstra pid_t child; 97*09a7fe0aSKyle Evans union lock_subject subj; 98c8929a49SJohn Polstra 9945edbdccSChristian S.J. Peron silent = keep = 0; 100437bab48SColin Percival flags = O_CREAT | O_RDONLY; 101c8929a49SJohn Polstra waitsec = -1; /* Infinite. */ 10235095fd2SKyle Evans while ((ch = getopt(argc, argv, "knst:w")) != -1) { 103c8929a49SJohn Polstra switch (ch) { 1042f278eacSJohn Polstra case 'k': 1052f278eacSJohn Polstra keep = 1; 1062f278eacSJohn Polstra break; 107b5be420aSEitan Adler case 'n': 108b5be420aSEitan Adler flags &= ~O_CREAT; 109b5be420aSEitan Adler break; 110c8929a49SJohn Polstra case 's': 111c8929a49SJohn Polstra silent = 1; 112c8929a49SJohn Polstra break; 113c8929a49SJohn Polstra case 't': 114c8929a49SJohn Polstra { 115c8929a49SJohn Polstra char *endptr; 116c8929a49SJohn Polstra waitsec = strtol(optarg, &endptr, 0); 117c8929a49SJohn Polstra if (*optarg == '\0' || *endptr != '\0' || waitsec < 0) 11845edbdccSChristian S.J. Peron errx(EX_USAGE, 11945edbdccSChristian S.J. Peron "invalid timeout \"%s\"", optarg); 120c8929a49SJohn Polstra } 121c8929a49SJohn Polstra break; 122437bab48SColin Percival case 'w': 123437bab48SColin Percival flags = (flags & ~O_RDONLY) | O_WRONLY; 124437bab48SColin Percival break; 125c8929a49SJohn Polstra default: 126c8929a49SJohn Polstra usage(); 127c8929a49SJohn Polstra } 128c8929a49SJohn Polstra } 129*09a7fe0aSKyle Evans 130c8929a49SJohn Polstra argc -= optind; 131c8929a49SJohn Polstra argv += optind; 132*09a7fe0aSKyle Evans 133*09a7fe0aSKyle Evans if (argc == 0) 134*09a7fe0aSKyle Evans usage(); 135*09a7fe0aSKyle Evans 136*09a7fe0aSKyle Evans lockname = argv[0]; 137*09a7fe0aSKyle Evans 138*09a7fe0aSKyle Evans argc--; 139*09a7fe0aSKyle Evans argv++; 140*09a7fe0aSKyle Evans 141*09a7fe0aSKyle Evans /* 142*09a7fe0aSKyle Evans * If there aren't any arguments left, then we must be in fdlock mode. 143*09a7fe0aSKyle Evans */ 144*09a7fe0aSKyle Evans if (argc == 0 && *lockname != '/') { 145*09a7fe0aSKyle Evans fdlock = 1; 146*09a7fe0aSKyle Evans subj.subj_fd = -1; 147*09a7fe0aSKyle Evans } else { 148*09a7fe0aSKyle Evans fdlock = fdlock_implied(lockname, &subj.subj_fd); 149*09a7fe0aSKyle Evans if (argc == 0 && !fdlock) { 150*09a7fe0aSKyle Evans fprintf(stderr, "Expected fd, got '%s'\n", lockname); 151*09a7fe0aSKyle Evans usage(); 152*09a7fe0aSKyle Evans } 153*09a7fe0aSKyle Evans } 154*09a7fe0aSKyle Evans 155*09a7fe0aSKyle Evans if (fdlock) { 156*09a7fe0aSKyle Evans if (subj.subj_fd < 0) { 157*09a7fe0aSKyle Evans char *endp; 158*09a7fe0aSKyle Evans 159*09a7fe0aSKyle Evans errno = 0; 160*09a7fe0aSKyle Evans subj.subj_fd = strtol(lockname, &endp, 10); 161*09a7fe0aSKyle Evans if (errno != 0 || *endp != '\0') { 162*09a7fe0aSKyle Evans fprintf(stderr, "Expected fd, got '%s'\n", 163*09a7fe0aSKyle Evans lockname); 164*09a7fe0aSKyle Evans usage(); 165*09a7fe0aSKyle Evans } 166*09a7fe0aSKyle Evans } 167*09a7fe0aSKyle Evans 168*09a7fe0aSKyle Evans if (subj.subj_fd < 0 || subj.subj_fd > INT_MAX) { 169*09a7fe0aSKyle Evans fprintf(stderr, "fd '%ld' out of range\n", 170*09a7fe0aSKyle Evans subj.subj_fd); 171*09a7fe0aSKyle Evans usage(); 172*09a7fe0aSKyle Evans } 173*09a7fe0aSKyle Evans } else { 174*09a7fe0aSKyle Evans subj.subj_name = lockname; 175*09a7fe0aSKyle Evans } 176*09a7fe0aSKyle Evans 177c8929a49SJohn Polstra if (waitsec > 0) { /* Set up a timeout. */ 178c8929a49SJohn Polstra struct sigaction act; 179c8929a49SJohn Polstra 180c8929a49SJohn Polstra act.sa_handler = timeout; 181c8929a49SJohn Polstra sigemptyset(&act.sa_mask); 182c8929a49SJohn Polstra act.sa_flags = 0; /* Note that we do not set SA_RESTART. */ 183c8929a49SJohn Polstra sigaction(SIGALRM, &act, NULL); 184c8929a49SJohn Polstra alarm(waitsec); 185c8929a49SJohn Polstra } 1866a53f0a5SChristian S.J. Peron /* 1876a53f0a5SChristian S.J. Peron * If the "-k" option is not given, then we must not block when 1886a53f0a5SChristian S.J. Peron * acquiring the lock. If we did, then the lock holder would 1896a53f0a5SChristian S.J. Peron * unlink the file upon releasing the lock, and we would acquire 1906a53f0a5SChristian S.J. Peron * a lock on a file with no directory entry. Then another 1916a53f0a5SChristian S.J. Peron * process could come along and acquire the same lock. To avoid 1926a53f0a5SChristian S.J. Peron * this problem, we separate out the actions of waiting for the 1936a53f0a5SChristian S.J. Peron * lock to be available and of actually acquiring the lock. 1946a53f0a5SChristian S.J. Peron * 1956a53f0a5SChristian S.J. Peron * That approach produces behavior that is technically correct; 1966a53f0a5SChristian S.J. Peron * however, it causes some performance & ordering problems for 1976a53f0a5SChristian S.J. Peron * locks that have a lot of contention. First, it is unfair in 1986a53f0a5SChristian S.J. Peron * the sense that a released lock isn't necessarily granted to 1996a53f0a5SChristian S.J. Peron * the process that has been waiting the longest. A waiter may 2006a53f0a5SChristian S.J. Peron * be starved out indefinitely. Second, it creates a thundering 2016a53f0a5SChristian S.J. Peron * herd situation each time the lock is released. 2026a53f0a5SChristian S.J. Peron * 2036a53f0a5SChristian S.J. Peron * When the "-k" option is used, the unlink race no longer 2046a53f0a5SChristian S.J. Peron * exists. In that case we can block while acquiring the lock, 2056a53f0a5SChristian S.J. Peron * avoiding the separate step of waiting for the lock. This 2066a53f0a5SChristian S.J. Peron * yields fairness and improved performance. 2076a53f0a5SChristian S.J. Peron */ 208*09a7fe0aSKyle Evans lockfd = acquire_lock(&subj, flags | O_NONBLOCK, silent); 2096a53f0a5SChristian S.J. Peron while (lockfd == -1 && !timed_out && waitsec != 0) { 210*09a7fe0aSKyle Evans if (keep || fdlock) 211*09a7fe0aSKyle Evans lockfd = acquire_lock(&subj, flags, silent); 2126a53f0a5SChristian S.J. Peron else { 2136a53f0a5SChristian S.J. Peron wait_for_lock(lockname); 214*09a7fe0aSKyle Evans lockfd = acquire_lock(&subj, flags | O_NONBLOCK, 2153041e695SKyle Evans silent); 2166a53f0a5SChristian S.J. Peron } 2176a53f0a5SChristian S.J. Peron } 218c8929a49SJohn Polstra if (waitsec > 0) 219c8929a49SJohn Polstra alarm(0); 220c8929a49SJohn Polstra if (lockfd == -1) { /* We failed to acquire the lock. */ 221c8929a49SJohn Polstra if (silent) 222ae06cb4dSJohn Polstra exit(EX_TEMPFAIL); 223ae06cb4dSJohn Polstra errx(EX_TEMPFAIL, "%s: already locked", lockname); 224c8929a49SJohn Polstra } 225*09a7fe0aSKyle Evans 226c8929a49SJohn Polstra /* At this point, we own the lock. */ 227*09a7fe0aSKyle Evans 228*09a7fe0aSKyle Evans /* Nothing else to do for FD lock, just exit */ 229*09a7fe0aSKyle Evans if (argc == 0) { 230*09a7fe0aSKyle Evans assert(fdlock); 231*09a7fe0aSKyle Evans return 0; 232*09a7fe0aSKyle Evans } 233*09a7fe0aSKyle Evans 234c8929a49SJohn Polstra if (atexit(cleanup) == -1) 235ae06cb4dSJohn Polstra err(EX_OSERR, "atexit failed"); 236c8929a49SJohn Polstra if ((child = fork()) == -1) 237ae06cb4dSJohn Polstra err(EX_OSERR, "cannot fork"); 238c8929a49SJohn Polstra if (child == 0) { /* The child process. */ 239c8929a49SJohn Polstra close(lockfd); 240c8929a49SJohn Polstra execvp(argv[0], argv); 241208f2fd1STim J. Robbins warn("%s", argv[0]); 242c8929a49SJohn Polstra _exit(1); 243c8929a49SJohn Polstra } 244c8929a49SJohn Polstra /* This is the parent process. */ 245c8929a49SJohn Polstra signal(SIGINT, SIG_IGN); 246c8929a49SJohn Polstra signal(SIGQUIT, SIG_IGN); 247c8929a49SJohn Polstra signal(SIGTERM, killed); 24818425c19SAlexander Melkov fclose(stdin); 24918425c19SAlexander Melkov fclose(stdout); 25018425c19SAlexander Melkov fclose(stderr); 251c8929a49SJohn Polstra if (waitpid(child, &status, 0) == -1) 25218425c19SAlexander Melkov exit(EX_OSERR); 2537f15c32dSDavid Malone return (WIFEXITED(status) ? WEXITSTATUS(status) : EX_SOFTWARE); 254c8929a49SJohn Polstra } 255c8929a49SJohn Polstra 256c8929a49SJohn Polstra /* 257*09a7fe0aSKyle Evans * Try to acquire a lock on the given file/fd, creating the file if 2586a53f0a5SChristian S.J. Peron * necessary. The flags argument is O_NONBLOCK or 0, depending on 2596a53f0a5SChristian S.J. Peron * whether we should wait for the lock. Returns an open file descriptor 2606a53f0a5SChristian S.J. Peron * on success, or -1 on failure. 2616a53f0a5SChristian S.J. Peron */ 2626a53f0a5SChristian S.J. Peron static int 263*09a7fe0aSKyle Evans acquire_lock(union lock_subject *subj, int flags, int silent) 2646a53f0a5SChristian S.J. Peron { 2656a53f0a5SChristian S.J. Peron int fd; 2666a53f0a5SChristian S.J. Peron 267*09a7fe0aSKyle Evans if (fdlock) { 268*09a7fe0aSKyle Evans assert(subj->subj_fd >= 0 && subj->subj_fd <= INT_MAX); 269*09a7fe0aSKyle Evans fd = (int)subj->subj_fd; 270*09a7fe0aSKyle Evans 271*09a7fe0aSKyle Evans if (flock(fd, LOCK_EX | LOCK_NB) == -1) { 272*09a7fe0aSKyle Evans if (errno == EAGAIN || errno == EINTR) 273*09a7fe0aSKyle Evans return (-1); 274*09a7fe0aSKyle Evans err(EX_CANTCREAT, "cannot lock fd %d", fd); 275*09a7fe0aSKyle Evans } 276*09a7fe0aSKyle Evans } else if ((fd = open(subj->subj_name, O_EXLOCK|flags, 0666)) == -1) { 2776a53f0a5SChristian S.J. Peron if (errno == EAGAIN || errno == EINTR) 2786a53f0a5SChristian S.J. Peron return (-1); 2793041e695SKyle Evans else if (errno == ENOENT && (flags & O_CREAT) == 0) { 2803041e695SKyle Evans if (!silent) 281*09a7fe0aSKyle Evans warn("%s", subj->subj_name); 2823041e695SKyle Evans exit(EX_UNAVAILABLE); 2833041e695SKyle Evans } 284*09a7fe0aSKyle Evans err(EX_CANTCREAT, "cannot open %s", subj->subj_name); 2856a53f0a5SChristian S.J. Peron } 2866a53f0a5SChristian S.J. Peron return (fd); 2876a53f0a5SChristian S.J. Peron } 2886a53f0a5SChristian S.J. Peron 2896a53f0a5SChristian S.J. Peron /* 290c8929a49SJohn Polstra * Remove the lock file. 291c8929a49SJohn Polstra */ 292c8929a49SJohn Polstra static void 293c8929a49SJohn Polstra cleanup(void) 294c8929a49SJohn Polstra { 295cc2a9f52SChristian S.J. Peron 296*09a7fe0aSKyle Evans if (keep || fdlock) 2972f278eacSJohn Polstra flock(lockfd, LOCK_UN); 2982f278eacSJohn Polstra else 299ae06cb4dSJohn Polstra unlink(lockname); 300c8929a49SJohn Polstra } 301c8929a49SJohn Polstra 302c8929a49SJohn Polstra /* 303c8929a49SJohn Polstra * Signal handler for SIGTERM. Cleans up the lock file, then re-raises 304c8929a49SJohn Polstra * the signal. 305c8929a49SJohn Polstra */ 306c8929a49SJohn Polstra static void 307c8929a49SJohn Polstra killed(int sig) 308c8929a49SJohn Polstra { 309cc2a9f52SChristian S.J. Peron 310c8929a49SJohn Polstra cleanup(); 311c8929a49SJohn Polstra signal(sig, SIG_DFL); 312c8929a49SJohn Polstra if (kill(getpid(), sig) == -1) 31318425c19SAlexander Melkov _Exit(EX_OSERR); 314c8929a49SJohn Polstra } 315c8929a49SJohn Polstra 316c8929a49SJohn Polstra /* 317c8929a49SJohn Polstra * Signal handler for SIGALRM. 318c8929a49SJohn Polstra */ 319c8929a49SJohn Polstra static void 3209ff5e898SDavid Malone timeout(int sig __unused) 321c8929a49SJohn Polstra { 322cc2a9f52SChristian S.J. Peron 323c8929a49SJohn Polstra timed_out = 1; 324c8929a49SJohn Polstra } 325c8929a49SJohn Polstra 326c8929a49SJohn Polstra static void 327c8929a49SJohn Polstra usage(void) 328c8929a49SJohn Polstra { 329cc2a9f52SChristian S.J. Peron 3303ec665d4SPhilippe Charnier fprintf(stderr, 331*09a7fe0aSKyle Evans "usage: lockf [-knsw] [-t seconds] file command [arguments]\n" 332*09a7fe0aSKyle Evans " lockf [-s] [-t seconds] fd\n"); 3333ec665d4SPhilippe Charnier exit(EX_USAGE); 334c8929a49SJohn Polstra } 335c8929a49SJohn Polstra 336c8929a49SJohn Polstra /* 337c8929a49SJohn Polstra * Wait until it might be possible to acquire a lock on the given file. 3386a53f0a5SChristian S.J. Peron * If the file does not exist, return immediately without creating it. 339c8929a49SJohn Polstra */ 3406a53f0a5SChristian S.J. Peron static void 3416a53f0a5SChristian S.J. Peron wait_for_lock(const char *name) 342c8929a49SJohn Polstra { 343c8929a49SJohn Polstra int fd; 344c8929a49SJohn Polstra 3456a53f0a5SChristian S.J. Peron if ((fd = open(name, O_RDONLY|O_EXLOCK, 0666)) == -1) { 3466a53f0a5SChristian S.J. Peron if (errno == ENOENT || errno == EINTR) 3476a53f0a5SChristian S.J. Peron return; 348ae06cb4dSJohn Polstra err(EX_CANTCREAT, "cannot open %s", name); 349c8929a49SJohn Polstra } 3506a53f0a5SChristian S.J. Peron close(fd); 351c8929a49SJohn Polstra } 352