1 /* 2 * Copyright (C) 1997 John D. Polstra. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY JOHN D. POLSTRA AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL JOHN D. POLSTRA OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/types.h> 30 #include <sys/wait.h> 31 32 #include <err.h> 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <signal.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <sysexits.h> 39 #include <unistd.h> 40 41 static void cleanup(void); 42 static void killed(int sig); 43 static void timeout(int sig); 44 static void usage(void); 45 static int wait_for_lock(const char *name, int flags); 46 47 static const char *lockname; 48 static int lockfd = -1; 49 static int keep; 50 static volatile sig_atomic_t timed_out; 51 52 /* 53 * Execute an arbitrary command while holding a file lock. 54 */ 55 int 56 main(int argc, char **argv) 57 { 58 int ch, silent, status, waitsec; 59 pid_t child; 60 61 silent = keep = 0; 62 waitsec = -1; /* Infinite. */ 63 while ((ch = getopt(argc, argv, "skt:")) != -1) { 64 switch (ch) { 65 case 'k': 66 keep = 1; 67 break; 68 case 's': 69 silent = 1; 70 break; 71 case 't': 72 { 73 char *endptr; 74 waitsec = strtol(optarg, &endptr, 0); 75 if (*optarg == '\0' || *endptr != '\0' || waitsec < 0) 76 errx(EX_USAGE, 77 "invalid timeout \"%s\"", optarg); 78 } 79 break; 80 default: 81 usage(); 82 } 83 } 84 if (argc - optind < 2) 85 usage(); 86 lockname = argv[optind++]; 87 argc -= optind; 88 argv += optind; 89 if (waitsec > 0) { /* Set up a timeout. */ 90 struct sigaction act; 91 92 act.sa_handler = timeout; 93 sigemptyset(&act.sa_mask); 94 act.sa_flags = 0; /* Note that we do not set SA_RESTART. */ 95 sigaction(SIGALRM, &act, NULL); 96 alarm(waitsec); 97 } 98 lockfd = wait_for_lock(lockname, O_NONBLOCK); 99 while (lockfd == -1 && !timed_out && waitsec != 0) 100 lockfd = wait_for_lock(lockname, 0); 101 if (waitsec > 0) 102 alarm(0); 103 if (lockfd == -1) { /* We failed to acquire the lock. */ 104 if (silent) 105 exit(EX_TEMPFAIL); 106 errx(EX_TEMPFAIL, "%s: already locked", lockname); 107 } 108 /* At this point, we own the lock. */ 109 if (atexit(cleanup) == -1) 110 err(EX_OSERR, "atexit failed"); 111 if ((child = fork()) == -1) 112 err(EX_OSERR, "cannot fork"); 113 if (child == 0) { /* The child process. */ 114 close(lockfd); 115 execvp(argv[0], argv); 116 warn("%s", argv[0]); 117 _exit(1); 118 } 119 /* This is the parent process. */ 120 signal(SIGINT, SIG_IGN); 121 signal(SIGQUIT, SIG_IGN); 122 signal(SIGTERM, killed); 123 if (waitpid(child, &status, 0) == -1) 124 err(EX_OSERR, "waitpid failed"); 125 return (WIFEXITED(status) ? WEXITSTATUS(status) : 1); 126 } 127 128 /* 129 * Remove the lock file. 130 */ 131 static void 132 cleanup(void) 133 { 134 135 if (keep) 136 flock(lockfd, LOCK_UN); 137 else 138 unlink(lockname); 139 } 140 141 /* 142 * Signal handler for SIGTERM. Cleans up the lock file, then re-raises 143 * the signal. 144 */ 145 static void 146 killed(int sig) 147 { 148 149 cleanup(); 150 signal(sig, SIG_DFL); 151 if (kill(getpid(), sig) == -1) 152 err(EX_OSERR, "kill failed"); 153 } 154 155 /* 156 * Signal handler for SIGALRM. 157 */ 158 static void 159 timeout(int sig __unused) 160 { 161 162 timed_out = 1; 163 } 164 165 static void 166 usage(void) 167 { 168 169 fprintf(stderr, 170 "usage: lockf [-ks] [-t seconds] file command [arguments]\n"); 171 exit(EX_USAGE); 172 } 173 174 /* 175 * Wait until it might be possible to acquire a lock on the given file. 176 */ 177 static int 178 wait_for_lock(const char *name, int flags) 179 { 180 int fd; 181 182 if ((fd = open(name, O_CREAT|O_RDONLY|O_EXLOCK|flags, 0666)) == -1) { 183 if (errno == EINTR || errno == EAGAIN) 184 return (-1); 185 err(EX_CANTCREAT, "cannot open %s", name); 186 } 187 return (fd); 188 } 189