3 * Lock a file, run a program
5 * (c) 2003 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Toys utilties collection.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
43 #include <mLib/mdwopt.h>
44 #include <mLib/quis.h>
45 #include <mLib/report.h>
47 /*----- Static variables --------------------------------------------------*/
51 /*----- Main code ---------------------------------------------------------*/
53 static void alrm(int s
) { longjmp(jmp
, 1); }
55 static void usage(FILE *fp
)
58 "Usage: $ [-cfwx] [-p REALPROG] [-t TIMEOUT] FILE PROG ARGS...\n");
61 static void version(FILE *fp
)
62 { pquis(fp
, "$ (version " VERSION
")\n"); }
64 static void help(FILE *fp
)
70 Lock FILE and run PROG, passing ARGS. Options are:\n\
72 -h, --help Show this help message.\n\
73 -v, --version Show version string.\n\
74 -u, --usage Show terse usage summary.\n\
76 -c, --[no-]create Create FILE if it doesn't exist [default: on].\n\
77 -f, --[no-]fail Fail if the file is already locked [default: off].\n\
78 -w, --[no-]wait Wait for the lock to be available [default: off].\n\
79 -x, --[no-]exclusive Get an exclusive (writer) lock [default: on].\n\
80 -p, --program=REALPROG Run REALPROG instead of PROG.\n\
81 -t, --timeout=TIME Wait for TIME for lock to become available.\n\
85 int main(int argc
, char *argv
[])
90 void (*oalrm
)(int) = 0;
106 unsigned f
= f_create
| f_excl
;
111 static const struct option opts
[] = {
112 { "help", 0, 0, 'h' },
113 { "version", 0, 0, 'v' },
114 { "usage", 0, 0, 'u' },
115 { "wait", OPTF_NEGATE
, 0, 'w' },
116 { "fail", OPTF_NEGATE
, 0, 'f' },
117 { "create", OPTF_NEGATE
, 0, 'c' },
118 { "program", OPTF_ARGREQ
, 0, 'p' },
119 { "timeout", OPTF_ARGREQ
, 0, 't' },
120 { "exclusive", OPTF_NEGATE
, 0, 'x' },
124 int i
= mdwopt(argc
, argv
, "-hvuw+f+c+x+p:t:", opts
,
125 0, 0, OPTF_NEGATION
);
128 case 'h': help(stdout
); exit(0);
129 case 'v': version(stdout
); exit(0);
130 case 'u': usage(stdout
); exit(0);
131 case 'w': f
|= f_wait
; break;
132 case 'w' | OPTF_NEGATED
: f
&= ~f_wait
; break;
133 case 'f': f
|= f_fail
; break;
134 case 'f' | OPTF_NEGATED
: f
&= ~f_fail
; break;
135 case 'c': f
|= f_create
; break;
136 case 'c' | OPTF_NEGATED
: f
&= ~f_create
; break;
137 case 'x': f
|= f_excl
; break;
138 case 'x' | OPTF_NEGATED
: f
&= ~f_excl
; break;
141 t
= strtol(optarg
, &p
, 0);
148 default: die(111, "unknown time unit `%c'", *p
);
150 if (*p
|| t
< 0 || errno
) die(111, "bad time value `%s'", optarg
);
153 case 'p': prog
= optarg
; break;
155 if (file
) { optind
--; goto doneopts
; }
158 default: f
|= f_bogus
; break;
163 if (f
& f_bogus
|| argc
- optind
< 1) {
169 if (!prog
) prog
= av
[0];
171 ((f
& f_create ? O_CREAT
: 0) |
172 (f
& f_excl ? O_RDWR
: O_RDONLY
)), 0666)) < 0)
173 die(111, "error opening `%s': %s", file
, strerror(errno
));
174 l
.l_type
= f
& f_excl ? F_WRLCK
: F_RDLCK
;
175 l
.l_whence
= SEEK_SET
;
184 oalrm
= signal(SIGALRM
, alrm
);
185 if (t
>= 0) alarm(t
);
186 if (fcntl(fd
, f
& f_wait ? F_SETLKW
: F_SETLK
, &l
) >= 0) errno
= 0;
188 signal(SIGALRM
, oalrm
);
193 if (nt
> ot
) raise(SIGALRM
);
197 ((errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&& errno
!= EACCES
) ||
199 die(111, "error locking `%s': %s", file
, strerror(errno
));
202 if ((kid
= fork()) < 0) die(111, "error from fork: %s", strerror(errno
));
206 die(111, "couldn't exec `%s': %s", prog
, strerror(errno
));
208 if (waitpid(kid
, &rc
, 0) < 0)
209 die(EXIT_FAILURE
, "error from wait: %s", strerror(errno
));
211 l
.l_whence
= SEEK_SET
;
214 fcntl(fd
, F_SETLK
, &l
);
216 if (WIFEXITED(rc
)) exit(WEXITSTATUS(rc
));
217 else exit(128 + WTERMSIG(rc
));
220 /*----- That's all, folks -------------------------------------------------*/