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
|
/* execute a program with a timeout by alarm(2) */
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
static const char *argv0;
static char **prgargv;
static pid_t pid;
static int signo;
static unsigned int timeout;
static void timedout(int sig)
{
fprintf(stderr, "%s[%d]: %s[%d] timed out after %u sec\n",
argv0, getpid(), *prgargv, pid, timeout);
if (pid)
kill(-pid, signo);
}
static void interrupted(int sig)
{
alarm(0);
if (pid)
kill(-pid, sig);
}
static void usage()
{
fprintf(stderr, "%s <timeout-seconds> [-<signal>] program ...\n"
"Where <signal> is a signal number (see kill -l).\n"
"Some symbolic names recognized. KILL used by default\n",
argv0);
exit(1);
}
static struct {
const char *name;
int signo;
} known[] = {
{"HUP", SIGHUP},
{"INT", SIGINT},
{"QUIT", SIGQUIT},
{"ILL", SIGILL},
{"TRAP", SIGTRAP},
{"ABRT", SIGABRT},
{"BUS", SIGBUS},
{"FPE", SIGFPE},
{"KILL", SIGKILL},
{"USR1", SIGUSR1},
{"SEGV", SIGSEGV},
{"USR2", SIGUSR2},
{"PIPE", SIGPIPE},
{"ALRM", SIGALRM},
{"TERM", SIGTERM},
{"STKFLT", SIGSTKFLT},
{"CHLD", SIGCHLD},
{"CONT", SIGCONT},
{"STOP", SIGSTOP},
{"TSTP", SIGTSTP},
{"TTIN", SIGTTIN},
{"TTOU", SIGTTOU},
{"URG", SIGURG},
{"XCPU", SIGXCPU},
{"XFSZ", SIGXFSZ},
{"VTALRM", SIGVTALRM},
{"PROF", SIGPROF},
{"WINCH", SIGWINCH},
{"IO", SIGIO},
{"PWR", SIGPWR},
{"SYS", SIGSYS},
};
static int signo_arg(const char *arg)
{
if (*arg == '-') {
char *p;
int s = strtol(++arg, &p, 10);
if (!*p && p > arg && s > 0 && s < _NSIG) {
signo = s;
return 1;
}
if (!strncasecmp(arg, "SIG", 3))
arg += 3;
for (s = 0; s < sizeof(known)/sizeof(*known); ++s)
if (!strcasecmp(arg, known[s].name)) {
signo = known[s].signo;
return 1;
}
}
return 0;
}
int main(int argc, char** argv)
{
argv0 = strrchr(*argv, '/');
if (argv0)
++argv0;
else
argv0 = *argv;
signal(SIGALRM, timedout);
signal(SIGINT, interrupted);
signal(SIGHUP, interrupted);
++argv;
if (!*argv)
usage();
if (signo_arg(*argv))
++argv;
if (sscanf(*argv, "%u", &timeout) == 1)
++argv;
else
usage();
if (!signo && signo_arg(*argv))
++argv;
if (!signo)
signo = SIGKILL;
if (!*argv)
usage();
prgargv = argv;
alarm(timeout);
pid = fork();
if (!pid) {
signal(SIGALRM, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
signal(SIGINT, SIG_DFL);
signal(SIGHUP, SIG_DFL);
setpgid(0, 0);
execvp(*prgargv, prgargv);
fprintf(stderr, "%s: %s: %s\n",
argv0, *prgargv, strerror(errno));
_exit(2);
} else if (pid < 0) {
fprintf(stderr, "%s: %s\n", argv0, strerror(errno));
} else {
int status;
while (waitpid(pid, &status, 0) < 0 && EINTR == errno)
;
alarm(0);
if (WIFEXITED(status))
return WEXITSTATUS(status);
if (WIFSIGNALED(status)) {
/*
* Some signals are special, lets die with
* the same signal as child process
*/
if (WTERMSIG(status) == SIGHUP ||
WTERMSIG(status) == SIGINT ||
WTERMSIG(status) == SIGTERM ||
WTERMSIG(status) == SIGQUIT ||
WTERMSIG(status) == SIGKILL) {
signal(WTERMSIG(status), SIG_DFL);
raise(WTERMSIG(status));
}
fprintf(stderr, "%s: %s: %s\n",
argv0, *prgargv, strsignal(WTERMSIG(status)));
}
else
fprintf(stderr, "%s died\n", *prgargv);
}
return 2;
}
|