Fix typo.
[dragonfly.git] / usr.bin / lockf / lockf.c
blob67d2c4681ff69731e893834c62f83192c94d4645
1 /*
2 * Copyright (C) 1997 John D. Polstra. All rights reserved.
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.
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.
25 * $FreeBSD: src/usr.bin/lockf/lockf.c,v 1.8 1999/08/28 01:03:07 peter Exp $
26 * $DragonFly: src/usr.bin/lockf/lockf.c,v 1.3 2005/01/05 00:46:52 cpressey Exp $
29 #include <sys/types.h>
30 #include <sys/wait.h>
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>
41 static int acquire_lock(const char *name);
42 static void cleanup(void);
43 static void killed(int sig);
44 static void timeout(int sig);
45 static void usage(void);
46 static void wait_for_lock(const char *name);
48 static const char *lockname;
49 static int lockfd;
50 static int keep;
51 static volatile sig_atomic_t timed_out;
54 * Execute an arbitrary command while holding a file lock.
56 int
57 main(int argc, char **argv)
59 int ch;
60 int silent;
61 int status;
62 int waitsec;
63 pid_t child;
65 silent = 0;
66 keep = 0;
67 waitsec = -1; /* Infinite. */
68 while ((ch = getopt(argc, argv, "skt:")) != -1) {
69 switch (ch) {
71 case 'k':
72 keep = 1;
73 break;
75 case 's':
76 silent = 1;
77 break;
79 case 't':
81 char *endptr;
82 waitsec = strtol(optarg, &endptr, 0);
83 if (*optarg == '\0' || *endptr != '\0' || waitsec < 0)
84 errx(EX_USAGE, "invalid timeout \"%s\"", optarg);
86 break;
88 default:
89 usage();
92 if (argc - optind < 2)
93 usage();
94 lockname = argv[optind++];
95 argc -= optind;
96 argv += optind;
98 if (waitsec > 0) { /* Set up a timeout. */
99 struct sigaction act;
101 act.sa_handler = timeout;
102 sigemptyset(&act.sa_mask);
103 act.sa_flags = 0; /* Note that we do not set SA_RESTART. */
105 sigaction(SIGALRM, &act, NULL);
106 alarm(waitsec);
109 lockfd = acquire_lock(lockname);
110 while (lockfd == -1 && !timed_out && waitsec != 0) {
111 wait_for_lock(lockname);
112 lockfd = acquire_lock(lockname);
115 if (waitsec > 0)
116 alarm(0);
118 if (lockfd == -1) { /* We failed to acquire the lock. */
119 if (silent)
120 exit(EX_TEMPFAIL);
121 errx(EX_TEMPFAIL, "%s: already locked", lockname);
124 /* At this point, we own the lock. */
126 if (atexit(cleanup) == -1)
127 err(EX_OSERR, "atexit failed");
129 if ((child = fork()) == -1)
130 err(EX_OSERR, "cannot fork");
132 if (child == 0) { /* The child process. */
133 close(lockfd);
134 execvp(argv[0], argv);
135 perror(argv[0]);
136 _exit(1);
139 /* This is the parent process. */
141 signal(SIGINT, SIG_IGN);
142 signal(SIGQUIT, SIG_IGN);
143 signal(SIGTERM, killed);
145 if (waitpid(child, &status, 0) == -1)
146 err(EX_OSERR, "waitpid failed");
148 return WIFEXITED(status) ? WEXITSTATUS(status) : 1;
152 * Try to acquire a lock on the given file, but don't wait for it. Returns
153 * an open file descriptor on success, or -1 on failure.
155 static int
156 acquire_lock(const char *name)
158 int fd;
160 if ((fd = open(name, O_RDONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 0666)) == -1) {
161 if (errno == EAGAIN || errno == EINTR)
162 return -1;
163 err(EX_CANTCREAT, "cannot open %s", name);
165 return fd;
169 * Remove the lock file.
171 static void
172 cleanup(void)
174 if (keep)
175 flock(lockfd, LOCK_UN);
176 else
177 unlink(lockname);
181 * Signal handler for SIGTERM. Cleans up the lock file, then re-raises
182 * the signal.
184 static void
185 killed(int sig)
187 cleanup();
188 signal(sig, SIG_DFL);
189 if (kill(getpid(), sig) == -1)
190 err(EX_OSERR, "kill failed");
194 * Signal handler for SIGALRM.
196 static void
197 timeout(__unused int sig)
199 timed_out = 1;
202 static void
203 usage(void)
205 fprintf(stderr,
206 "usage: lockf [-ks] [-t seconds] file command [arguments]\n");
207 exit(EX_USAGE);
211 * Wait until it might be possible to acquire a lock on the given file.
213 static void
214 wait_for_lock(const char *name)
216 int fd;
218 if ((fd = open(name, O_RDONLY|O_EXLOCK)) == -1) {
219 if (errno == ENOENT || errno == EINTR)
220 return;
221 err(EX_CANTCREAT, "cannot open %s", name);
223 close(fd);
224 return;