Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / lockf / lockf.c
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: 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.2 2003/06/17 04:29:28 dillon Exp $
27  */
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 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);
47
48 static const char *lockname;
49 static int lockfd;
50 static int keep;
51 static volatile sig_atomic_t timed_out;
52
53 /*
54  * Execute an arbitrary command while holding a file lock.
55  */
56 int
57 main(int argc, char **argv)
58 {
59     int ch;
60     int silent;
61     int status;
62     int waitsec;
63     pid_t child;
64
65     silent = 0;
66     keep = 0;
67     waitsec = -1;       /* Infinite. */
68     while ((ch = getopt(argc, argv, "skt:")) != -1) {
69         switch (ch) {
70
71         case 'k':
72             keep = 1;
73             break;
74
75         case 's':
76             silent = 1;
77             break;
78
79         case 't':
80             {
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);
85             }
86             break;
87
88         default:
89             usage();
90         }
91     }
92     if (argc - optind < 2)
93         usage();
94     lockname = argv[optind++];
95     argc -= optind;
96     argv += optind;
97
98     if (waitsec > 0) {          /* Set up a timeout. */
99         struct sigaction act;
100
101         act.sa_handler = timeout;
102         sigemptyset(&act.sa_mask);
103         act.sa_flags = 0;       /* Note that we do not set SA_RESTART. */
104
105         sigaction(SIGALRM, &act, NULL);
106         alarm(waitsec);
107     }
108
109     lockfd = acquire_lock(lockname);
110     while (lockfd == -1 && !timed_out && waitsec != 0) {
111         wait_for_lock(lockname);
112         lockfd = acquire_lock(lockname);
113     }
114
115     if (waitsec > 0)
116         alarm(0);
117
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);
122     }
123
124     /* At this point, we own the lock. */
125
126     if (atexit(cleanup) == -1)
127         err(EX_OSERR, "atexit failed");
128
129     if ((child = fork()) == -1)
130         err(EX_OSERR, "cannot fork");
131
132     if (child == 0) {   /* The child process. */
133         close(lockfd);
134         execvp(argv[0], argv);
135         perror(argv[0]);
136         _exit(1);
137     }
138
139     /* This is the parent process. */
140
141     signal(SIGINT, SIG_IGN);
142     signal(SIGQUIT, SIG_IGN);
143     signal(SIGTERM, killed);
144
145     if (waitpid(child, &status, 0) == -1)
146         err(EX_OSERR, "waitpid failed");
147
148     return WIFEXITED(status) ? WEXITSTATUS(status) : 1;
149 }
150
151 /*
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.
154  */
155 static int
156 acquire_lock(const char *name)
157 {
158     int fd;
159
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);
164     }
165     return fd;
166 }
167
168 /*
169  * Remove the lock file.
170  */
171 static void
172 cleanup(void)
173 {
174     if (keep)
175         flock(lockfd, LOCK_UN);
176     else
177         unlink(lockname);
178 }
179
180 /*
181  * Signal handler for SIGTERM.  Cleans up the lock file, then re-raises
182  * the signal.
183  */
184 static void
185 killed(int sig)
186 {
187     cleanup();
188     signal(sig, SIG_DFL);
189     if (kill(getpid(), sig) == -1)
190         err(EX_OSERR, "kill failed");
191 }
192
193 /*
194  * Signal handler for SIGALRM.
195  */
196 static void
197 timeout(int sig)
198 {
199     timed_out = 1;
200 }
201
202 static void
203 usage(void)
204 {
205     fprintf(stderr,
206       "usage: lockf [-ks] [-t seconds] file command [arguments]\n");
207     exit(EX_USAGE);
208 }
209
210 /*
211  * Wait until it might be possible to acquire a lock on the given file.
212  */
213 static void
214 wait_for_lock(const char *name)
215 {
216     int fd;
217
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);
222     }
223     close(fd);
224     return;
225 }