-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchdog.c
278 lines (221 loc) · 6.4 KB
/
watchdog.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
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include "trinity.h" // ignore_tainted
#include "shm.h"
#include "files.h"
#include "syscall.h"
#include "pids.h"
#include "params.h" // quiet_level
#include "log.h"
#include "child.h"
static void watchdog(void);
void init_watchdog(void)
{
static const struct timespec ts = { .tv_nsec = 100000000 }; /* 100ms */
pid_t pid;
fflush(stdout);
pid = fork();
if (pid == 0)
watchdog(); // Never returns.
while (shm->watchdog_pid == 0)
nanosleep(&ts, NULL);
output(0, "[%d] Started watchdog process, PID is %d\n", getpid(), shm->watchdog_pid);
}
static int check_shm_sanity(void)
{
unsigned int i;
pid_t pid;
if (shm->running_childs == 0)
return SHM_OK;
for_each_pidslot(i) {
pid = shm->pids[i];
if (pid == EMPTY_PIDSLOT)
continue;
if (pid_is_valid(pid) == FALSE) {
shm->exit_reason = EXIT_PID_OUT_OF_RANGE;
return SHM_CORRUPT;
}
}
// FIXME: The '500000' is magic, and should be dynamically calculated.
// On startup, we should figure out how many getpid()'s per second we can do,
// and use that.
if (shm->total_syscalls_done - shm->previous_count > 500000) {
output(0, "[watchdog] Execcount increased dramatically! (old:%ld new:%ld):\n",
shm->previous_count, shm->total_syscalls_done);
shm->exit_reason = EXIT_SHM_CORRUPTION;
}
shm->previous_count = shm->total_syscalls_done;
return SHM_OK;
}
static unsigned int reap_dead_kids()
{
unsigned int i;
unsigned int alive = 0;
unsigned int reaped = 0;
for_each_pidslot(i) {
pid_t pid;
int ret;
pid = shm->pids[i];
if (pid == EMPTY_PIDSLOT)
continue;
ret = kill(pid, 0);
/* If it disappeared, reap it. */
if (ret == -1) {
if (errno == ESRCH) {
output(0, "[watchdog] pid %d has disappeared (oom-killed maybe?). Reaping.\n", pid);
reap_child(pid);
reaped++;
} else {
output(0, "[watchdog] problem running getpgid on pid %d (%d:%s)\n", pid, errno, strerror(errno));
}
} else {
alive++;
}
if (shm->running_childs == 0)
return 0;
}
if (reaped != 0)
output(0, "[watchdog] Reaped %d dead children\n", reaped);
return alive;
}
static void check_children(void)
{
struct timeval tv;
time_t diff;
time_t old, now;
pid_t pid;
unsigned int i;
gettimeofday(&tv, NULL);
now = tv.tv_sec;
for_each_pidslot(i) {
pid = shm->pids[i];
if (pid == EMPTY_PIDSLOT)
continue;
old = shm->tv[i].tv_sec;
if (old == 0)
continue;
/* if we wrapped, just reset it, we'll pick it up next time around. */
if (old > (now + 3)) {
printf("[watchdog] child %d wrapped! old=%ld now=%ld\n", i, old, now);
shm->tv[i].tv_sec = now;
continue;
}
diff = now - old;
/* if we're way off, we're comparing garbage. Reset it. */
if (diff > 1000) {
output(0, "[watchdog] huge delta! pid slot %d [%d]: old:%ld now:%ld diff:%d. Setting to now.\n", i, pid, old, now, diff);
shm->tv[i].tv_sec = now;
continue;
}
/* After 30 seconds of no progress, send a kill signal. */
if (diff == 30) {
output(0, "[watchdog] pid %d hasn't made progress in 30 seconds! (last:%ld now:%ld diff:%d). "
"Stuck in syscall %d:%s%s. Sending SIGKILL.\n",
pid, old, now, diff,
shm->syscallno[i],
print_syscall_name(shm->syscallno[i], shm->do32bit[i]),
shm->do32bit[i] ? " (32bit)" : "");
kill(pid, SIGKILL);
break;
}
/* If it's still around after 60 seconds, we have bigger problems.
* Find out what's going on. */
if (diff > 60) {
output(0, "[watchdog] pid %d hasn't made progress in 60 seconds! (last:%ld now:%ld diff:%d)\n",
pid, old, now, diff);
shm->tv[i].tv_sec = now;
}
}
}
static void watchdog(void)
{
static const char watchdogname[17]="trinity-watchdog";
static unsigned long lastcount = 0;
shm->watchdog_pid = getpid();
printf("[%d] Watchdog is alive\n", shm->watchdog_pid);
prctl(PR_SET_NAME, (unsigned long) &watchdogname);
(void)signal(SIGSEGV, SIG_DFL);
while (shm->exit_reason == STILL_RUNNING) {
if (shm->regenerating == FALSE) {
if (check_shm_sanity() == SHM_CORRUPT)
goto corrupt;
reap_dead_kids();
check_children();
if (syscalls_todo && (shm->total_syscalls_done >= syscalls_todo)) {
output(0, "[watchdog] Reached limit %d. Telling children to exit.\n", syscalls_todo);
shm->exit_reason = EXIT_REACHED_COUNT;
}
// Periodic log syncing. FIXME: This is kinda ugly, and mostly unnecessary.
if (shm->total_syscalls_done % 1000 == 0)
synclogs();
if ((quiet_level > 1) && (shm->total_syscalls_done > 1)) {
if (shm->total_syscalls_done - lastcount > 10000) {
printf("[watchdog] %ld iterations. [F:%ld S:%ld]\n",
shm->total_syscalls_done, shm->failures, shm->successes);
lastcount = shm->total_syscalls_done;
}
}
}
/* Only check taint if it was zero on startup */
if (ignore_tainted == FALSE) {
if (check_tainted() != 0) {
output(0, "[watchdog] kernel became tainted! Last seed was %u\n", shm->seed);
shm->exit_reason = EXIT_KERNEL_TAINTED;
}
}
if (shm->need_reseed == FALSE) {
shm->reseed_counter++;
/* If we haven't reseeded in five minutes, trigger one. */
if (shm->reseed_counter == 300) {
output(0, "[watchdog] Triggering periodic reseed.\n");
shm->need_reseed = TRUE;
shm->reseed_counter = 0;
}
}
sleep(1);
}
corrupt:
/* We don't want to ever exit before main is waiting for us. */
while (shm->regenerating == TRUE)
sleep(1);
/* Wait for all the children to exit. */
while (shm->running_childs > 0) {
unsigned int i;
unsigned int alive;
/* Make sure there's no dead kids lying around.
* We need to do this in case the oom killer has been killing them,
* otherwise we end up stuck here with no child processes.
*/
alive = reap_dead_kids();
if (alive == 0)
goto out;
/* Ok, some kids are still alive. 'help' them along with a SIGKILL */
for_each_pidslot(i) {
pid_t pid;
pid = shm->pids[i];
if (pid == EMPTY_PIDSLOT)
continue;
kill(pid, SIGKILL);
}
/* wait a second to give kids a chance to exit. */
sleep(1);
if (check_shm_sanity()) {
// FIXME: If we get here, we over-wrote the real exit_reason.
// We should have saved that, and handled appropriately.
goto out;
}
}
out:
output(0, "[%d] Watchdog exiting\n", getpid());
_exit(EXIT_SUCCESS);
}