-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrunlock.c
174 lines (158 loc) · 4.56 KB
/
runlock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Copyright 2010 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define _GNU_SOURCE /* asprintf, basename */
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sysexits.h>
#include <syslog.h>
#include <unistd.h>
#include "subprocess.h"
#include "tempdir.h"
char* lock_filename = NULL;
volatile sig_atomic_t timeout_expired = 0;
static void usage(char* prog) {
fprintf(stderr,
"Usage: %s [options] command [arg [arg] ...]\n\n"
"This program prevents concurrent execution of a process\n"
"by holding an exclusive lock while the subprocess is running.\n"
"Subsequent attempts to run a process with the same lock,\n"
"while another process has the lock open, and is still\n"
"executing, will exit with a failure exit code.\n",
prog);
fprintf(stderr,
"\noptions:\n"
" -d send log messages to stderr as well as syslog.\n"
" -f lock_filename path to use as a lock file\n"
" -t timeout time in seconds to wait to acquire the lock\n"
" -h this help.\n");
}
static void alarm_handler(int signum);
static void alarm_handler(int signum) {
(void)signum; /* suppress unused variable warning */
timeout_expired = 1;
}
int main(int argc, char** argv) {
char* progname;
int arg;
char* command;
char** command_args;
int status = 0;
struct flock fl;
int fd;
char buf[BUFSIZ];
struct sigaction sa, old_sa;
int debug = 0;
int timeout = 5;
char* endptr;
memset(&fl, 0, sizeof(fl));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
progname = argv[0];
while ((arg = getopt(argc, argv, "+df:ht:")) > 0) {
switch (arg) {
case 'h':
usage(progname);
exit(EXIT_SUCCESS);
break;
case 'd':
debug = LOG_PERROR;
break;
case 'f':
if (asprintf(&lock_filename, "%s", optarg) == -1) {
perror("asprintf");
exit(EX_OSERR);
}
break;
case 't':
timeout = strtol(optarg, &endptr, 10);
if (*endptr || !optarg) {
fprintf(stderr, "invalid timeout specified: %s\n", optarg);
exit(EX_DATAERR);
}
break;
default:
break;
}
}
if (optind >= argc) {
usage(progname);
exit(EXIT_FAILURE);
} else {
command = strdup(argv[optind]);
command_args = &argv[optind];
}
openlog(progname, debug | LOG_ODELAY | LOG_PID | LOG_NOWAIT, LOG_CRON);
if (debug)
setlogmask(LOG_UPTO(LOG_DEBUG));
else
setlogmask(LOG_UPTO(LOG_INFO));
if (lock_filename == NULL) {
if (asprintf(&lock_filename, "%s/%s.pid", make_tempdir(),
basename(command)) == -1) {
perror("asprintf");
exit(EX_OSERR);
}
}
syslog(LOG_DEBUG, "lock filename is %s", lock_filename);
sa.sa_handler = alarm_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGALRM, &sa, &old_sa);
alarm(timeout);
if ((fd = open(lock_filename, O_CREAT | O_RDWR | O_TRUNC,
S_IRUSR | S_IWUSR)) < 0) {
perror(lock_filename);
exit(EX_NOINPUT);
} else {
if (fcntl(fd, F_SETLKW, &fl) < 0) {
switch (errno) {
case EINTR:
if (timeout_expired) {
syslog(LOG_INFO,
"waited %d seconds, already locked by another process",
timeout);
exit(EX_CANTCREAT);
}
break;
case EACCES:
case EAGAIN:
syslog(LOG_INFO, "already locked by another process");
exit(EX_CANTCREAT);
break;
default:
perror("fcntl");
exit(EXIT_FAILURE);
}
} else {
alarm(0);
sigaction(SIGALRM, &old_sa, NULL);
snprintf(buf, BUFSIZ, "%d\n", getpid());
if (write(fd, buf, strlen(buf)) == -1) {
perror("write");
}
fsync(fd);
syslog(LOG_DEBUG, "lock granted");
status = run_subprocess(command, command_args, NULL);
close(fd);
}
}
closelog();
return status;
}