-
Notifications
You must be signed in to change notification settings - Fork 0
/
timebox.c
214 lines (166 loc) · 5.38 KB
/
timebox.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
/*
* timebox: run a program for at most M + N seconds.
*
* M = running period
* N = grace period (responding to SIGTERM)
*
* Copyright Alan Grow <[email protected]> 2017.
* This is free and unencumbered software released into the public domain.
*
*/
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void sig_catch(int sig, void (*f)());
void sig_uncatch(int sig);
void die(int code, const char *message);
void sysdie(const char *message);
int done_running = 0;
int done_gracing = 0;
void sigint() { done_gracing = done_running = 1; }
void sigchld() { done_gracing = 1; }
int main(int argc, char **argv)
{
// Process arguments.
if (argc < 4) die(1, "usage: timebox TIMEOUT GRACE program arg1 arg2 ...");
char *argv0 = *argv++; argc--;
double timeout_argument = atof(*argv++); argc--;
double grace_argument = atof(*argv++); argc--;
if (timeout_argument <= 0) die(1, "invalid TIMEOUT");
if (grace_argument < 0) die(1, "invalid GRACE");
// We work with timevals throughout, convert from doubles.
struct timeval timeout;
struct timeval grace;
timeout.tv_sec = (time_t)timeout_argument;
timeout.tv_usec = (suseconds_t)(1e6 * (timeout_argument - timeout.tv_sec));
grace.tv_sec = (time_t)grace_argument;
grace.tv_usec = (suseconds_t)(1e6 * (grace_argument - grace.tv_sec));
// Create the child-to-parent pipe.
int pipefds[2];
if (pipe(pipefds) < 0) sysdie("pipe error");
// SIGINT: initiate shutdown without any more delays.
// SIGCHLD: child has exited, no grace period delay needed.
sig_catch(SIGINT, sigint);
sig_catch(SIGCHLD, sigchld);
// Fork and run the child program.
pid_t pid;
switch ((pid = fork())) {
case -1:
sysdie("fork error"); break;
case 0: // Child, close pipe read side and run tail program.
sig_uncatch(SIGINT);
sig_uncatch(SIGCHLD);
if (close(pipefds[0]) < 0) sysdie("close error");
if (execvp(*argv, argv) < 0) sysdie("exec error");
// Never reached.
break;
default: // Parent, continue with this program.
break;
}
// Close pipe write side.
if (close(pipefds[1]) < 0) sysdie("close error");
// Calculate absolute deadlines for running and grace periods.
struct timeval started;
struct timeval deadline_running;
struct timeval deadline_gracing;
if (gettimeofday(&started, 0) < 0) sysdie("gettimeofday error");
timeradd(&started, &timeout, &deadline_running);
timeradd(&deadline_running, &grace, &deadline_gracing);
// Running period. Wait until one of these occurs:
// 1. The deadline has passed.
// 2. The deadline timeout elapses.
// 3. The child exits, triggering a readable zero byte at eof.
while (!done_running)
{
// Recalculate remaining time.
struct timeval now;
struct timeval remaining;
if (gettimeofday(&now, 0) < 0) sysdie("gettimeofday error");
timersub(&deadline_running, &now, &remaining);
// Done if deadline has passed (1).
if (remaining.tv_sec < 0) {
done_running = 1;
break;
}
// Wait for (2) or (3).
fd_set readable;
FD_ZERO(&readable);
FD_SET(pipefds[0], &readable);
int rc = select(pipefds[0]+1, &readable, 0, 0, &remaining);
// Done if timeout elapses (2).
if (rc == 0) done_running = 1;
if (done_running) break;
if (rc < 0 && (errno == EINTR || errno == EAGAIN)) continue;
if (rc < 0) sysdie("select error");
// The child-to-parent pipe is readable, so try to read a byte.
char c;
ssize_t bytes = read(pipefds[0], &c, 1);
// Prevent child keeping parent busy by writing to pipe.
if (bytes > 0) die(101, "child wrote to pipe");
// Done if we read zero bytes at eof (3).
if (!bytes) done_running = 1;
if (done_running) break;
if (bytes < 0) sysdie("read error");
}
// Grace period. Send TERM to child and wait for deadline to elapse.
kill(pid, SIGTERM);
kill(pid, SIGCONT);
while (!done_gracing)
{
// Recalculate remaining time.
struct timeval now;
struct timeval remaining;
if (gettimeofday(&now, 0) < 0) sysdie("gettimeofday error");
timersub(&deadline_gracing, &now, &remaining);
// Done if deadline has passed.
if (remaining.tv_sec < 0) {
done_gracing = 1;
break;
}
// Sleep until deadline.
int rc = select(0, 0, 0, 0, &remaining);
if (rc == 0) done_gracing = 1;
if (done_gracing) break;
if (rc < 0 && (errno == EINTR || errno == EAGAIN)) continue;
if (rc < 0) sysdie("select error");
}
// Finally, force kill child in case it's not responding.
kill(pid, SIGKILL);
// Collect child status and bubble up its exit code.
int status;
if (waitpid(pid, &status, 0) < 0) sysdie("waitpid error");
return WEXITSTATUS(status);
}
void sig_catch(int sig, void (*f)())
{
struct sigaction sa;
sa.sa_handler = f;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(sig,&sa,(struct sigaction *) 0);
}
void sig_uncatch(int sig)
{
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(sig,&sa,(struct sigaction *) 0);
}
void die(int code, const char *message)
{
fprintf(stderr, "%s\n", message);
_exit(100);
}
void sysdie(const char *message)
{
fprintf(stderr, "%s: %s\n", message, strerror(errno));
_exit(errno & 0x7f);
}