5 * with-lock-ex -<mode> <lockfile> <command> <args>...
6 * with-lock-ex -l <lockfile>
10 * f fail if the lock cannot be acquired
11 * q silently do nothing if the lock cannot be acquired
12 * l show who is waiting (print "none" or "read <pid>"
13 * or "write <pid>"; lockfile opened for reading;
14 * no command may be specified)
16 * with-lock-ex will open and lock the lockfile for writing and
17 * then feed the remainder of its arguments to exec(2); when
18 * that process terminates the fd will be closed and the file
19 * unlocked automatically by the kernel.
21 * If invoked as with-lock, behaves like with-lock-ex -f (for backward
22 * compatibility with an earlier version).
24 * This file written by me, Ian Jackson, in 1993, 1994, 1995, 1996,
27 * Copyright 1993-2016 Ian Jackson in some jurisdictions
28 * Copyright 2017 Ian Jackson in all jurisdictions
29 * Copyright 2017 Genome Research Ltd
33 * Permission is hereby granted, free of charge, to any person obtaining a
34 * copy of this software and associated documentation files (the "Software"),
35 * to deal in the Software without restriction, including without limitation
36 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
37 * and/or sell copies of the Software, and to permit persons to whom the
38 * Software is furnished to do so, subject to the following conditions:
40 * The above copyright notice and this permission notice shall be included in
41 * all copies or substantial portions of the Software.
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
46 * SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR
47 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
48 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49 * DEALINGS IN THE SOFTWARE.
60 static const char *cmd;
62 static void fail(const char *why) __attribute__((noreturn));
64 static void fail(const char *why) {
65 fprintf(stderr,"with-lock-ex %s: %s: %s\n",cmd,why,strerror(errno));
69 static void badusage(void) __attribute__((noreturn));
71 static void badusage(void) {
72 fputs("usage: with-lock-ex -w|-q|-f [-t <secs>] <lockfile> <command> <args>...\n"
73 " with-lock-ex -l <lockfile>\n"
74 " with-lock-ex <lockfile> <command> <args>...\n",
81 int main(int argc, char **argv) {
83 struct stat stab, fstab;
88 while ((c= getopt(argc,argv,"+wfqlt:")) != -1) {
94 if (mode != 'x') badusage();
102 argv += optind-1; argc -= optind-1;
103 if (argc < 2) badusage();
106 um= umask(0777); if (um==-1) fail("find umask");
107 if (umask(um)==-1) fail("reset umask");
111 int openmode = mode=='l' ? O_RDONLY : O_RDWR|O_CREAT;
113 fd= open(argv[1],openmode,0666&~(um|((um&0222)<<1)));
114 if (fd<0) fail(argv[1]);
118 fl.l_whence= SEEK_SET;
120 fl.l_len= mode=='l' ? 0 : 1;
122 mode=='l' ? F_GETLK :
123 mode=='w' ? F_SETLKW :
127 (errno == EAGAIN || errno == EWOULDBLOCK || errno == EBUSY))
129 if (errno != EINTR) fail("could not acquire lock");
134 fl.l_type == F_WRLCK ? "write" :
135 fl.l_type == F_RDLCK ? "read" : "unknown",
136 (unsigned long)fl.l_pid);
140 if (ferror(stdout)) fail("print to stdout\n");
144 if (fstat(fd, &fstab)) fail("could not fstat lock fd");
145 if (stat(argv[1], &stab)) {
146 if (errno != ENOENT) fail("could not stat lockfile");
148 if (stab.st_dev == fstab.st_dev &&
149 stab.st_ino == fstab.st_ino) break;
154 cloexec= fcntl(fd, F_GETFD); if (cloexec==-1) fail("fcntl F_GETFD");
156 if (fcntl(fd, F_SETFD, cloexec)==-1) fail("fcntl F_SETFD");
159 fail("unable to execute command");