-
Notifications
You must be signed in to change notification settings - Fork 1
/
hatimerun.c
409 lines (345 loc) · 9.39 KB
/
hatimerun.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*********************************************************************
*** hatimerun.c
*** $Id$
********************************************************************/
/*
HA-Tools (c) 2001,2003,2005-2009 by Markus Winand <mws@fatalmind.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include "hatimerun.h"
#include "signumber.h"
#include "error.h"
/* default signal to be sent if -k was not specified */
#define _FAIL_SIGNAL SIGKILL
/* defines the maximum pair number of -k -t pairs */
#define MAXARGSIZE 16
extern char* optarg;
extern int optind;
int debug = 1;
char *cmdname = NULL;
int async = 0;
/* _idx variables are the indexes used during parsing, */
/* as well as during alarming. */
int term_signal[MAXARGSIZE];
int timeout[MAXARGSIZE];
int FAIL_EXITCODE[MAXARGSIZE];
volatile int timeouthappend_idx = 0;
void hatimerun_sighand(int a) {
timeouthappend_idx++;
}
void initialize_arrays() {
int i;
for (i=0; i < MAXARGSIZE; ++i) {
term_signal[i] = _FAIL_SIGNAL;;
timeout[i] = 0;
FAIL_EXITCODE[i] = _FAIL_EXITCODE;
}
}
int mk_child(char *argv[], int wpip)
{
pid_t childpid;
int childcode = 0;
childpid = fork();
switch (childpid) {
case -1: /* fail*/
error("fork() failed: %s\n", strerror(errno));
break;
case 0: /* fork() succeeded*/
/* kiddie*/
/* set into own new process group*/
setpgid(0,0);
execvp(argv[0], argv);
if (async) {
/* let the (waiting) parent know */
char buf = 'X';
write(wpip, &buf, 1);
close(wpip);
}
/* exec failed*/
error("exec(%s) failed: %s\n", argv[0], strerror(errno));
/* never returns*/
break;
default: /*parent*/
/*
*
* Set the signalhandlers for parent to ignore signals
*
*/
{ /* new block allows local variables ;) */
int ret;
int childstatus;
struct sigaction ign,alrt;
int timeoutsprocessed_idx = timeouthappend_idx;
ign.sa_handler = SIG_IGN;
sigemptyset(&ign.sa_mask);
ign.sa_flags = 0;
sigaction(SIGTERM, &ign, NULL);
sigaction(SIGHUP, &ign, NULL);
sigaction(SIGINT, &ign, NULL);
sigaction(SIGPIPE, &ign, NULL);
sigaction(SIGQUIT, &ign, NULL);
sigaction(SIGUSR1, &ign, NULL);
sigaction(SIGUSR2, &ign, NULL);
alrt.sa_handler = hatimerun_sighand;
sigemptyset(&alrt.sa_mask);
alrt.sa_flags = 0;
sigaction(SIGALRM, &alrt,NULL);
alarm(timeout[timeouthappend_idx]);
do {
ret = waitpid(childpid, &childstatus, 0);
if (ret == -1 && errno == EINTR && timeouthappend_idx > timeoutsprocessed_idx) {
/* kill it*/
kill(-childpid, term_signal[timeoutsprocessed_idx]); /* happend -1 since it is not yet processed */
childcode = FAIL_EXITCODE[timeoutsprocessed_idx];
if (timeout[timeouthappend_idx] > 0) {
alarm(timeout[timeouthappend_idx]);
}
timeoutsprocessed_idx = timeouthappend_idx;
}
} while (ret == -1 && errno == EINTR );
if ((timeouthappend_idx == 0) && WIFEXITED(childstatus)) {
childcode = WEXITSTATUS(childstatus);
} /* else? already set above */
} /* block for local vairables; */
break;
};
return (childcode);
}
/*
*
* Signal Handler
*
*/
void nop(int i) {
return;
}
void usage() {
fprintf(stderr, "usage: %s [-a] [-e exitcode] [-k signame] "
"-t secs command [args]\n", cmdname);
exit (FAIL_EXITCODE[0]);
}
void longusage() {
fprintf(stderr,
"usage: %s [-a] [-e exitcode] [-k signame] -t [[hh:]mm:]secs command [args]\n"
" %s [-l|-h|-?]\n"
"Options:\n"
" -a Async mode. Starts %s in the background\n"
" -e exitcode Changes the exitcode returned by %s on fail\n", cmdname, cmdname, cmdname, cmdname);
fprintf(stderr,
" -k signame Specifies the signal witch will be sent to the process group\n"
" if a timeout occures\n"
" -t time Specifies the timeout.\n"
" You can specify the number of seconds or hours/minutes/seconds\n"
" notation seperated by colon (e.g. 1:30:0 means 1 hour, 30 minutes)\n"
" -l Print list of available signals on this platform and exit.\n");
fprintf(stderr,
"Version:\n"
" V%s\n"
" Copyright (c) 2001,2003,2005-2009 by Markus Winand <mws@fatalmind.com>\n"
" This program is distributed in the hope that it will be useful,\n"
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
" GNU General Public License for more details.\n\n", VERSION);
exit (FAIL_EXITCODE[0]) ;
}
int parse_timespec(char* arg) {
int h=0, m=0, s=0, rv = 0;
char dummy;
if (sscanf(optarg, "%d%c", &s, &dummy) != 1) {
/* not a pure numeric input, maybe colon seperated? */
if (sscanf(optarg, "%d:%d%c", &m, &s, &dummy) != 2) {
if (sscanf(optarg, "%d:%d:%d%c", &h, &m, &s, &dummy) != 3) {
error("illegal option \"-t %s\"\n", optarg);
}
}
}
if (s < 0 || m < 0 || h < 0) {
error("illegal option \"-t %s\"\n", optarg);
exit (FAIL_EXITCODE[0]);
}
rv = (h*60 + m) * 60 + s;
if (rv <= 0) {
error("illegal option \"-t %s\"\n", optarg);
exit (FAIL_EXITCODE[0]);
}
return rv;
}
/*
*
* Command line parsing
*
*/
char **cmd_line(int argc, char *argv[]) {
char **childargv = NULL;
int childargc = argc - 1;
int hlp;
char dummy;
int ch;
int timeout_idx = 0;
int term_signal_idx = 0;
int FAIL_EXITCODE_idx = 0;
#ifdef HAVE_SETENV
char* env_POSIXLY_CORRECT;
env_POSIXLY_CORRECT = getenv("POSIXLY_CORRECT");
setenv("POSIXLY_CORRECT", "1", 0);
#endif
while ((ch = getopt(argc, argv, "ae:k:t:lh?")) != -1) {
switch(ch) {
case 'a':
async = 1;
break;
case 'e': /* exitcode */
if (FAIL_EXITCODE_idx >= MAXARGSIZE) {
error("limit of -e occurances reached, set MAXARGSIZE higher and recompile (current limit: %d)", MAXARGSIZE);
}
if (sscanf(optarg, "%d%c", &hlp, &dummy) != 1) {
error("illegal option \"-e %s\"\n", optarg);
}
FAIL_EXITCODE[FAIL_EXITCODE_idx++] = hlp;
break;
case 'k': /* signame */
if (term_signal_idx >= MAXARGSIZE) {
error("limit of -k occurances reached, set MAXARGSIZE higher and recompile (current limit: %d)", MAXARGSIZE);
}
if (sscanf(optarg,"%d%c", &hlp, &dummy) == 1) {
term_signal[term_signal_idx++] = hlp;
} else {
/* check for signal name*/
hlp = signumber(optarg);
if (hlp == 0) {
error("unknown signal \"%s\"\n",optarg);
}
term_signal[term_signal_idx++] = hlp;
}
break;
case 't':
if (timeout_idx >= MAXARGSIZE) {
error("limit of -t occurances reached, set MAXARGSIZE higher and recompile (current limit: %d)", MAXARGSIZE);
}
hlp = parse_timespec(optarg);
timeout[timeout_idx++] = hlp;
break;
case 'l':
siglist();
break;
case '?':
case 'h':
longusage();
break;
}
}
#ifdef HAVE_SETENV
if (env_POSIXLY_CORRECT == NULL) {
unsetenv("POSIXLY_CORRECT");
}
#endif
childargc = argc - optind;
childargv = argv + optind;
if (timeout_idx == 0 || childargc < 1) {
usage();
}
/* null terminated argv for execvp */
childargv[childargc] = NULL;
return childargv;
}
/*
*
* Main
*
*/
int main(int argc, char *argv[]) {
char **childargv = NULL;
int child_return = 99;
struct sigaction act;
pid_t async_fork = 0;
int pip[2] = {0, 0}; /* used for error checking in async mode */
initialize_arrays();
cmdname = argv[0];
if (argc < 2) {
usage();
}
/*
*
* Command line parsing
*
*/
childargv = cmd_line(argc, argv);
/*
*
* fork() for async mode
*
*/
if (async) {
/* use a pipe to communicate successfuly exec via close on exec flag */
int flags;
if (pipe(pip) != 0) {
error("pipe() failed: %s\n", strerror(errno));
}
/* close on exec for the write end */
flags = fcntl(pip[0], F_GETFD);
flags |= FD_CLOEXEC;
fcntl(pip[0], F_SETFD, flags);
flags = fcntl(pip[1], F_GETFD);
flags |= FD_CLOEXEC;
fcntl(pip[1], F_SETFD, flags);
async_fork = fork();
if (async_fork == -1) {
/* fail*/
error("fork() failed: %s\n", strerror(errno));
}
if (async_fork > 0) {
/* daddie waits for pipe */
char buffer = ' ';
close(pip[1]); /* all write-end's must be close in order to get EOF on read */
if (read(pip[0], &buffer, 1) > 0) {
/* if there was something to read */
/* an error occured during execvp in the child */
exit(FAIL_EXITCODE[0]);
}
/* and dies */
exit(0);
}
/* async child, closes readpipe */
close(pip[0]);
}
/*
*
* Set a nop signalhandler for SIGCHLD, needed to interrupt the sleep
*
*/
act.sa_handler = nop;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &act, NULL) != 0) {
error("sigaction() failed: %s\n", strerror(errno));
}
/*
*
* do the work
*
*/
child_return = mk_child(childargv, pip[1]);
exit (child_return);
}