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