5 * Lock a file, run a program
7 * (c) 2003 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Toys utilties collection.
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
45 #include <mLib/mdwopt.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
49 /*----- Static variables --------------------------------------------------*/
53 /*----- Main code ---------------------------------------------------------*/
55 static void alrm(int s) { longjmp(jmp, 1); }
57 static void usage(FILE *fp)
60 "Usage: $ [-cfwx] [-p REALPROG] [-t TIMEOUT] FILE PROG ARGS...\n");
63 static void version(FILE *fp)
65 pquis(fp, "$ (version " VERSION ")\n");
68 static void help(FILE *fp)
74 Lock FILE and run PROG, passing ARGS. Options are:\n\
76 -h, --help Show this help message.\n\
77 -v, --version Show version string.\n\
78 -u, --usage Show terse usage summary.\n\
80 -c, --[no-]create Create FILE if it doesn't exist [default: on].\n\
81 -f, --[no-]fail Fail if the file is already locked [default: off].\n\
82 -w, --[no-]wait Wait for the lock to be available [default: off].\n\
83 -x, --[no-]exclusive Get an exclusive (writer) lock [default: on].\n\
84 -p, --program=REALPROG Run REALPROG instead of PROG.\n\
85 -t, --timeout=TIME Wait for TIME for lock to become available.\n\
89 int main(int argc, char *argv[])
94 void (*oalrm)(int) = 0;
110 unsigned f = f_create | f_excl;
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' },
128 int i = mdwopt(argc, argv, "-hvuw+f+c+x+p:t:", opts,
129 0, 0, OPTF_NEGATION);
145 case 'w' | OPTF_NEGATED:
151 case 'f' | OPTF_NEGATED:
157 case 'c' | OPTF_NEGATED:
163 case 'x' | OPTF_NEGATED:
168 t = strtol(optarg, &p, 0);
175 default: die(111, "unknown time unit `%c'", *p);
177 if (*p || t < 0 || errno)
178 die(111, "bad time value `%s'", optarg);
198 if (f & f_bogus || argc - optind < 1) {
207 ((f & f_create ? O_CREAT : 0) |
208 (f & f_excl ? O_RDWR : O_RDONLY)), 0666)) < 0)
209 die(111, "error opening `%s': %s", file, strerror(errno));
210 l.l_type = f & f_excl ? F_WRLCK : F_RDLCK;
211 l.l_whence = SEEK_SET;
220 oalrm = signal(SIGALRM, alrm);
223 if (fcntl(fd, f & f_wait ? F_SETLKW : F_SETLK, &l) >= 0)
226 signal(SIGALRM, oalrm);
235 ((errno != EAGAIN && errno != EWOULDBLOCK && errno != EACCES) ||
237 die(111, "error locking `%s': %s", file, strerror(errno));
241 if ((kid = fork()) < 0)
242 die(111, "error from fork: %s", strerror(errno));
246 die(111, "couldn't exec `%s': %s", prog, strerror(errno));
248 if (waitpid(kid, &st, 0) < 0)
249 die(EXIT_FAILURE, "error from wait: %s", strerror(errno));
251 l.l_whence = SEEK_SET;
254 fcntl(fd, F_SETLK, &l);
257 exit(WEXITSTATUS(st));
262 /*----- That's all, folks -------------------------------------------------*/