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 * $FreeBSD$ 26 */ 27 28 #include <sys/types.h> 29 #include <sys/wait.h> 30 31 #include <err.h> 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <signal.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <sysexits.h> 38 #include <unistd.h> 39 40 static int acquire_lock(const char *name); 41 static void cleanup(void); 42 static void killed(int sig); 43 static void timeout(int sig); 44 static void usage(void); 45 static void wait_for_lock(const char *name); 46 47 static const char *lockname; 48 static int lockfd; 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; 59 int silent; 60 int status; 61 int waitsec; 62 pid_t child; 63 64 silent = 0; 65 keep = 0; 66 waitsec = -1; /* Infinite. */ 67 while ((ch = getopt(argc, argv, "skt:")) != -1) { 68 switch (ch) { 69 70 case 'k': 71 keep = 1; 72 break; 73 74 case 's': 75 silent = 1; 76 break; 77 78 case 't': 79 { 80 char *endptr; 81 waitsec = strtol(optarg, &endptr, 0); 82 if (*optarg == '\0' || *endptr != '\0' || waitsec < 0) 83 errx(EX_USAGE, "invalid timeout \"%s\"", optarg); 84 } 85 break; 86 87 default: 88 usage(); 89 } 90 } 91 if (argc - optind < 2) 92 usage(); 93 lockname = argv[optind++]; 94 argc -= optind; 95 argv += optind; 96 97 if (waitsec > 0) { /* Set up a timeout. */ 98 struct sigaction act; 99 100 act.sa_handler = timeout; 101 sigemptyset(&act.sa_mask); 102 act.sa_flags = 0; /* Note that we do not set SA_RESTART. */ 103 104 sigaction(SIGALRM, &act, NULL); 105 alarm(waitsec); 106 } 107 108 lockfd = acquire_lock(lockname); 109 while (lockfd == -1 && !timed_out && waitsec != 0) { 110 wait_for_lock(lockname); 111 lockfd = acquire_lock(lockname); 112 } 113 114 if (waitsec > 0) 115 alarm(0); 116 117 if (lockfd == -1) { /* We failed to acquire the lock. */ 118 if (silent) 119 exit(EX_TEMPFAIL); 120 errx(EX_TEMPFAIL, "%s: already locked", lockname); 121 } 122 123 /* At this point, we own the lock. */ 124 125 if (atexit(cleanup) == -1) 126 err(EX_OSERR, "atexit failed"); 127 128 if ((child = fork()) == -1) 129 err(EX_OSERR, "cannot fork"); 130 131 if (child == 0) { /* The child process. */ 132 close(lockfd); 133 execvp(argv[0], argv); 134 perror(argv[0]); 135 _exit(1); 136 } 137 138 /* This is the parent process. */ 139 140 signal(SIGINT, SIG_IGN); 141 signal(SIGQUIT, SIG_IGN); 142 signal(SIGTERM, killed); 143 144 if (waitpid(child, &status, 0) == -1) 145 err(EX_OSERR, "waitpid failed"); 146 147 return WIFEXITED(status) ? WEXITSTATUS(status) : 1; 148 } 149 150 /* 151 * Try to acquire a lock on the given file, but don't wait for it. Returns 152 * an open file descriptor on success, or -1 on failure. 153 */ 154 static int 155 acquire_lock(const char *name) 156 { 157 int fd; 158 159 if ((fd = open(name, O_RDONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 0666)) == -1) { 160 if (errno == EAGAIN || errno == EINTR) 161 return -1; 162 err(EX_CANTCREAT, "cannot open %s", name); 163 } 164 return fd; 165 } 166 167 /* 168 * Remove the lock file. 169 */ 170 static void 171 cleanup(void) 172 { 173 if (keep) 174 flock(lockfd, LOCK_UN); 175 else 176 unlink(lockname); 177 } 178 179 /* 180 * Signal handler for SIGTERM. Cleans up the lock file, then re-raises 181 * the signal. 182 */ 183 static void 184 killed(int sig) 185 { 186 cleanup(); 187 signal(sig, SIG_DFL); 188 if (kill(getpid(), sig) == -1) 189 err(EX_OSERR, "kill failed"); 190 } 191 192 /* 193 * Signal handler for SIGALRM. 194 */ 195 static void 196 timeout(int sig) 197 { 198 timed_out = 1; 199 } 200 201 static void 202 usage(void) 203 { 204 fprintf(stderr, 205 "usage: lockf [-ks] [-t seconds] file command [arguments]\n"); 206 exit(EX_USAGE); 207 } 208 209 /* 210 * Wait until it might be possible to acquire a lock on the given file. 211 */ 212 static void 213 wait_for_lock(const char *name) 214 { 215 int fd; 216 217 if ((fd = open(name, O_RDONLY|O_EXLOCK)) == -1) { 218 if (errno == ENOENT || errno == EINTR) 219 return; 220 err(EX_CANTCREAT, "cannot open %s", name); 221 } 222 close(fd); 223 return; 224 } 225