Commit | Line | Data |
---|---|---|
f342fce2 | 1 | /* -*-c-*- |
2 | * | |
f342fce2 | 3 | * Lock a file, run a program |
4 | * | |
5 | * (c) 2003 Mark Wooding | |
6 | */ | |
7 | ||
841e5aca | 8 | /*----- Licensing notice --------------------------------------------------* |
f342fce2 | 9 | * |
10 | * This file is part of the Toys utilties collection. | |
11 | * | |
12 | * Toys is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
841e5aca | 16 | * |
f342fce2 | 17 | * Toys is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
841e5aca | 21 | * |
f342fce2 | 22 | * You should have received a copy of the GNU General Public License |
23 | * along with Toys; if not, write to the Free Software Foundation, | |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | */ | |
26 | ||
f342fce2 | 27 | /*----- Header files ------------------------------------------------------*/ |
28 | ||
29 | #include <errno.h> | |
30 | #include <setjmp.h> | |
31 | #include <signal.h> | |
32 | #include <stdio.h> | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
ed04d555 | 35 | #include <time.h> |
f342fce2 | 36 | |
37 | #include <sys/types.h> | |
38 | #include <sys/time.h> | |
39 | #include <unistd.h> | |
40 | #include <fcntl.h> | |
41 | #include <sys/wait.h> | |
ed1b4ef8 | 42 | #include <sys/stat.h> |
f342fce2 | 43 | |
44 | #include <mLib/mdwopt.h> | |
45 | #include <mLib/quis.h> | |
46 | #include <mLib/report.h> | |
47 | ||
48 | /*----- Static variables --------------------------------------------------*/ | |
49 | ||
50 | static jmp_buf jmp; | |
b7ec4e91 | 51 | static int err; |
f342fce2 | 52 | |
53 | /*----- Main code ---------------------------------------------------------*/ | |
54 | ||
55 | static void alrm(int s) { longjmp(jmp, 1); } | |
56 | ||
57 | static void usage(FILE *fp) | |
58 | { | |
59 | pquis(fp, | |
60 | "Usage: $ [-cfwx] [-p REALPROG] [-t TIMEOUT] FILE PROG ARGS...\n"); | |
61 | } | |
62 | ||
63 | static void version(FILE *fp) | |
c9a19878 | 64 | { pquis(fp, "$ (version " VERSION ")\n"); } |
f342fce2 | 65 | |
66 | static void help(FILE *fp) | |
67 | { | |
68 | version(fp); | |
69 | putchar('\n'); | |
70 | usage(fp); | |
71 | pquis(fp, "\n\ | |
72 | Lock FILE and run PROG, passing ARGS. Options are:\n\ | |
73 | \n\ | |
74 | -h, --help Show this help message.\n\ | |
75 | -v, --version Show version string.\n\ | |
76 | -u, --usage Show terse usage summary.\n\ | |
77 | \n\ | |
78 | -c, --[no-]create Create FILE if it doesn't exist [default: on].\n\ | |
79 | -f, --[no-]fail Fail if the file is already locked [default: off].\n\ | |
80 | -w, --[no-]wait Wait for the lock to be available [default: off].\n\ | |
81 | -x, --[no-]exclusive Get an exclusive (writer) lock [default: on].\n\ | |
82 | -p, --program=REALPROG Run REALPROG instead of PROG.\n\ | |
83 | -t, --timeout=TIME Wait for TIME for lock to become available.\n\ | |
84 | "); | |
85 | } | |
86 | ||
87 | int main(int argc, char *argv[]) | |
88 | { | |
89 | const char *file = 0; | |
90 | const char *prog = 0; | |
91 | char *const *av; | |
92 | void (*oalrm)(int) = 0; | |
b7ec4e91 | 93 | int fd = -1; |
f342fce2 | 94 | struct flock l; |
95 | char *p; | |
96 | int t = -1; | |
b7ec4e91 | 97 | int oflag; |
1ad88a2f | 98 | unsigned int ot = 0; |
ed1b4ef8 | 99 | struct stat st, nst; |
f342fce2 | 100 | time_t nt; |
101 | pid_t kid; | |
df6e37e2 | 102 | int rc; |
f342fce2 | 103 | |
104 | #define f_bogus 1u | |
105 | #define f_wait 2u | |
106 | #define f_fail 4u | |
107 | #define f_create 8u | |
108 | #define f_excl 16u | |
109 | ||
110 | unsigned f = f_create | f_excl; | |
111 | ||
112 | ego(argv[0]); | |
113 | ||
114 | for (;;) { | |
115 | static const struct option opts[] = { | |
116 | { "help", 0, 0, 'h' }, | |
117 | { "version", 0, 0, 'v' }, | |
118 | { "usage", 0, 0, 'u' }, | |
119 | { "wait", OPTF_NEGATE, 0, 'w' }, | |
120 | { "fail", OPTF_NEGATE, 0, 'f' }, | |
121 | { "create", OPTF_NEGATE, 0, 'c' }, | |
122 | { "program", OPTF_ARGREQ, 0, 'p' }, | |
123 | { "timeout", OPTF_ARGREQ, 0, 't' }, | |
124 | { "exclusive", OPTF_NEGATE, 0, 'x' }, | |
125 | { 0, 0, 0, 0 } | |
126 | }; | |
127 | ||
128 | int i = mdwopt(argc, argv, "-hvuw+f+c+x+p:t:", opts, | |
129 | 0, 0, OPTF_NEGATION); | |
c9a19878 | 130 | if (i < 0) break; |
f342fce2 | 131 | switch (i) { |
c9a19878 MW |
132 | case 'h': help(stdout); exit(0); |
133 | case 'v': version(stdout); exit(0); | |
134 | case 'u': usage(stdout); exit(0); | |
135 | case 'w': f |= f_wait; break; | |
136 | case 'w' | OPTF_NEGATED: f &= ~f_wait; break; | |
137 | case 'f': f |= f_fail; break; | |
138 | case 'f' | OPTF_NEGATED: f &= ~f_fail; break; | |
139 | case 'c': f |= f_create; break; | |
140 | case 'c' | OPTF_NEGATED: f &= ~f_create; break; | |
141 | case 'x': f |= f_excl; break; | |
142 | case 'x' | OPTF_NEGATED: f &= ~f_excl; break; | |
f342fce2 | 143 | case 't': |
144 | errno = 0; | |
145 | t = strtol(optarg, &p, 0); | |
146 | switch (*p) { | |
147 | case 'd': t *= 24; | |
148 | case 'h': t *= 60; | |
149 | case 'm': t *= 60; | |
150 | case 's': p++; | |
151 | case 0: break; | |
152 | default: die(111, "unknown time unit `%c'", *p); | |
153 | } | |
c9a19878 | 154 | if (*p || t < 0 || errno) die(111, "bad time value `%s'", optarg); |
f342fce2 | 155 | f |= f_wait; |
156 | break; | |
c9a19878 | 157 | case 'p': prog = optarg; break; |
f342fce2 | 158 | case 0: |
c9a19878 | 159 | if (file) { optind--; goto doneopts; } |
f342fce2 | 160 | file = optarg; |
161 | break; | |
c9a19878 | 162 | default: f |= f_bogus; break; |
f342fce2 | 163 | } |
164 | } | |
165 | ||
166 | doneopts: | |
167 | if (f & f_bogus || argc - optind < 1) { | |
168 | usage(stderr); | |
169 | exit(EXIT_FAILURE); | |
170 | } | |
171 | ||
172 | av = &argv[optind]; | |
c9a19878 | 173 | if (!prog) prog = av[0]; |
b7ec4e91 MW |
174 | oflag = f & f_excl ? O_RDWR : O_RDONLY; |
175 | if (f & f_create) oflag |= O_CREAT; | |
f342fce2 | 176 | l.l_type = f & f_excl ? F_WRLCK : F_RDLCK; |
177 | l.l_whence = SEEK_SET; | |
178 | l.l_start = 0; | |
179 | l.l_len = 0; | |
180 | nt = time(0); | |
181 | if (setjmp(jmp)) { | |
b7ec4e91 | 182 | err = EAGAIN; |
f342fce2 | 183 | nt = t; |
b7ec4e91 | 184 | goto done; |
f342fce2 | 185 | } |
b7ec4e91 MW |
186 | ot = alarm(0); |
187 | oalrm = signal(SIGALRM, alrm); | |
188 | if (t >= 0) alarm(t); | |
ed1b4ef8 | 189 | again: |
b7ec4e91 MW |
190 | if ((fd = open(file, oflag, 0666)) < 0) |
191 | die(111, "error opening `%s': %s", file, strerror(errno)); | |
ed1b4ef8 MW |
192 | if (fstat(fd, &st)) |
193 | die(111, "error from fstat on `%s': %s", file, strerror(errno)); | |
b7ec4e91 | 194 | err = fcntl(fd, f & f_wait ? F_SETLKW : F_SETLK, &l) >= 0 ? 0 : errno; |
61dc021c MW |
195 | /* It's tempting to `optimize' this code by opening a new file descriptor |
196 | * here so as to elide the additional call to fstat(2) above. But this | |
197 | * doesn't work: if we successfully acquire the lock, we then have two file | |
198 | * descriptors open on the lock file, so we have to close one -- but, under | |
199 | * the daft fcntl(2) rules, even closing `nfd' will release the lock | |
200 | * immediately. | |
201 | */ | |
ed1b4ef8 MW |
202 | if (stat(file, &nst)) { |
203 | if (errno == ENOENT) { close(fd); goto again; } | |
204 | else die(111, "error from stat on `%s': %s", file, strerror(errno)); | |
205 | } | |
206 | if (st.st_dev != nst.st_dev || st.st_ino != nst.st_ino) | |
207 | { close(fd); goto again; } | |
b7ec4e91 | 208 | done: |
f342fce2 | 209 | signal(SIGALRM, oalrm); |
76e174aa MW |
210 | if (!ot) |
211 | alarm(0); | |
212 | else { | |
f342fce2 | 213 | nt = time(0) - nt; |
c9a19878 MW |
214 | if (nt > ot) raise(SIGALRM); |
215 | else alarm(ot - nt); | |
f342fce2 | 216 | } |
b7ec4e91 MW |
217 | switch (err) { |
218 | case 0: break; | |
219 | case EAGAIN: | |
220 | #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN | |
221 | case EWOULDBLOCK: | |
222 | #endif | |
223 | case EACCES: | |
224 | if (!(f & f_fail)) exit(0); | |
225 | default: | |
226 | die(111, "error locking `%s': %s", file, strerror(errno)); | |
227 | } | |
c9a19878 | 228 | if ((kid = fork()) < 0) die(111, "error from fork: %s", strerror(errno)); |
f342fce2 | 229 | if (!kid) { |
230 | close(fd); | |
231 | execvp(prog, av); | |
232 | die(111, "couldn't exec `%s': %s", prog, strerror(errno)); | |
233 | } | |
df6e37e2 | 234 | if (waitpid(kid, &rc, 0) < 0) |
f342fce2 | 235 | die(EXIT_FAILURE, "error from wait: %s", strerror(errno)); |
236 | l.l_type = F_UNLCK; | |
237 | l.l_whence = SEEK_SET; | |
238 | l.l_start = 0; | |
239 | l.l_len = 0; | |
240 | fcntl(fd, F_SETLK, &l); | |
b7ec4e91 | 241 | if (fd >= 0) close(fd); |
df6e37e2 | 242 | if (WIFEXITED(rc)) exit(WEXITSTATUS(rc)); |
0f4f1fb6 | 243 | else exit(128 + WTERMSIG(rc)); |
f342fce2 | 244 | } |
245 | ||
246 | /*----- That's all, folks -------------------------------------------------*/ |