forked from kernelslacker/trinity
/
main.c
349 lines (288 loc) · 7.84 KB
/
main.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
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "child.h"
#include "debug.h"
#include "log.h"
#include "pids.h"
#include "random.h"
#include "shm.h"
#include "syscall.h"
#include "trinity.h"
/* exit() wrapper to clear the pid before exiting, so the
* watchdog doesn't spin forever on a dead pid.
*/
void exit_main_fail(void)
{
if (getpid() != shm->mainpid) {
show_backtrace();
BUG("wtf, exit_main_fail called from non main pid!\n");
}
shm->mainpid = 0;
exit(EXIT_FAILURE);
}
/* Generate children*/
static void fork_children(void)
{
while (shm->running_childs < max_children) {
int childno;
int pid = 0;
if (shm->spawn_no_more == TRUE)
return;
/* a new child means a new seed, or the new child
* will do the same syscalls as the one in the child it's replacing.
* (special case startup, or we reseed unnecessarily)
*/
if (shm->ready == TRUE)
reseed();
/* Find a space for it in the pid map */
childno = find_childno(EMPTY_PIDSLOT);
if (childno == CHILD_NOT_FOUND) {
outputerr("## Pid map was full!\n");
dump_childnos();
exit_main_fail();
}
fflush(stdout);
pid = fork();
if (pid == 0) {
/* Child process. */
struct childdata *child = shm->children[childno];
init_child(child, childno);
child_process();
debugf("child %d %d exiting.\n", childno, getpid());
shutdown_child_logging(this_child);
reap_child(child->pid);
_exit(EXIT_SUCCESS);
} else {
if (pid == -1) {
/* We failed, wait for a child to exit before retrying. */
if (shm->running_childs > 0)
return;
output(0, "couldn't create child! (%s)\n", strerror(errno));
panic(EXIT_FORK_FAILURE);
exit_main_fail();
}
}
shm->children[childno]->pid = pid;
shm->running_childs++;
debugf("Created child %d (pid:%d) [total:%d/%d]\n",
childno, pid, shm->running_childs, max_children);
if (shm->exit_reason != STILL_RUNNING)
return;
}
shm->ready = TRUE;
debugf("created enough children\n");
}
/*
* reap_child: Remove all references to a running child.
*
* This can get called from three possible places.
* 1. A child calls this itself just before it exits to clear out
* its child struct in the shm.
* 2. From the watchdog if it finds reference to a pid that no longer exists.
* 3. From the main pid if it gets a SIGBUS or SIGSTOP from the child.
*
* The reaper lock protects against these happening at the same time.
*/
void reap_child(pid_t childpid)
{
struct childdata *child;
int i;
lock(&shm->reaper_lock);
if (childpid == shm->last_reaped) {
debugf("already reaped %d!\n", childpid);
goto out;
}
i = find_childno(childpid);
if (i == CHILD_NOT_FOUND)
goto out;
debugf("Removing pid %d from pidmap.\n", childpid);
child = shm->children[i];
child->pid = EMPTY_PIDSLOT;
child->syscall.tv.tv_sec = 0;
shm->running_childs--;
shm->last_reaped = childpid;
out:
unlock(&shm->reaper_lock);
}
static void handle_childsig(int childpid, int childstatus, int stop)
{
int __sig;
if (stop == TRUE)
__sig = WSTOPSIG(childstatus);
else
__sig = WTERMSIG(childstatus);
switch (__sig) {
case SIGSTOP:
if (stop != TRUE)
return;
debugf("Sending PTRACE_DETACH (and then KILL)\n");
ptrace(PTRACE_DETACH, childpid, NULL, NULL);
kill(childpid, SIGKILL);
reap_child(childpid);
return;
case SIGALRM:
debugf("got a alarm signal from pid %d\n", childpid);
break;
case SIGFPE:
case SIGSEGV:
case SIGKILL:
case SIGPIPE:
case SIGABRT:
case SIGBUS:
if (stop == TRUE)
debugf("Child %d was stopped by %s\n", childpid, strsignal(WSTOPSIG(childstatus)));
else
debugf("got a signal from pid %d (%s)\n", childpid, strsignal(WTERMSIG(childstatus)));
reap_child(childpid);
return;
default:
if (__sig >= SIGRTMIN) {
debugf("Child %d got RT signal (%d). Ignoring.\n", childpid, __sig);
return;
}
if (stop == TRUE)
debugf("Child %d was stopped by unhandled signal (%s).\n", childpid, strsignal(WSTOPSIG(childstatus)));
else
debugf("** Child got an unhandled signal (%d)\n", WTERMSIG(childstatus));
return;
}
}
static void handle_child(pid_t childpid, int childstatus)
{
switch (childpid) {
case 0:
//debugf("Nothing changed. children:%d\n", shm->running_childs);
break;
case -1:
if (shm->exit_reason != STILL_RUNNING)
return;
if (errno == ECHILD) {
unsigned int i;
bool seen = FALSE;
debugf("All children exited!\n");
for_each_child(i) {
struct childdata *child;
child = shm->children[i];
if (child->pid != EMPTY_PIDSLOT) {
if (pid_alive(child->pid) == -1) {
debugf("Removing %d from pidmap\n", child->pid);
child->pid = EMPTY_PIDSLOT;
shm->running_childs--;
} else {
debugf("%d looks still alive! ignoring.\n", child->pid);
}
seen = TRUE;
}
}
if (seen == FALSE)
shm->running_childs = 0;
break;
}
output(0, "error! (%s)\n", strerror(errno));
break;
default:
debugf("Something happened to pid %d\n", childpid);
if (WIFEXITED(childstatus)) {
int childno;
childno = find_childno(childpid);
if (childno != CHILD_NOT_FOUND) {
debugf("Child %d exited after %ld operations.\n",
childpid, shm->children[childno]->syscall.op_nr);
reap_child(childpid);
}
break;
} else if (WIFSIGNALED(childstatus)) {
handle_childsig(childpid, childstatus, FALSE);
} else if (WIFSTOPPED(childstatus)) {
handle_childsig(childpid, childstatus, TRUE);
} else if (WIFCONTINUED(childstatus)) {
break;
} else {
output(0, "erk, wtf\n");
}
}
}
static void handle_children(void)
{
unsigned int i;
int childstatus;
pid_t pid;
if (shm->running_childs == 0)
return;
/* First, we wait for *any* child to wake us up. */
pid = waitpid(-1, &childstatus, WUNTRACED | WCONTINUED);
/* We were awoken, handle it. */
handle_child(pid, childstatus);
/* While we're awake, let's see if the other children need attention.
* We do this instead of just waitpid(-1) again so that there's no way
* for any one child to starve the others of attention.
*/
for_each_child(i) {
pid = shm->children[i]->pid;
if (pid == EMPTY_PIDSLOT)
continue;
if (pid_is_valid(pid) == FALSE) /* If we find something invalid, we just ignore */
continue; /* it and leave it to the watchdog to clean up. */
pid = waitpid(pid, &childstatus, WUNTRACED | WCONTINUED | WNOHANG);
if (pid != 0)
handle_child(pid, childstatus);
}
}
static const char *reasons[NUM_EXIT_REASONS] = {
"Still running.",
"No more syscalls enabled.",
"Completed maximum number of operations.",
"No file descriptors open.",
"Lost track of a child.",
"shm corruption - Found a pid out of range.",
"ctrl-c",
"kernel became tainted.",
"SHM was corrupted!",
"Child reparenting problem",
"No files in file list.",
"Main process disappeared.",
"UID changed.",
"Something happened during fd init.",
"fork() failure",
"some kind of locking catastrophe",
"error while opening logfiles",
};
const char * decode_exit(void)
{
return reasons[shm->exit_reason];
}
void main_loop(void)
{
while (shm->exit_reason == STILL_RUNNING) {
if (shm->running_childs < max_children)
fork_children();
handle_children();
}
/* if the pid map is corrupt, we can't trust that we'll
* ever successfully finish pidmap_empty, so skip it */
if ((shm->exit_reason == EXIT_LOST_CHILD) ||
(shm->exit_reason == EXIT_SHM_CORRUPTION))
goto dont_wait;
/* Wait until all children have exited. */
while (pidmap_empty() == FALSE)
handle_children();
dont_wait:
output(0, "Bailing main loop because %s.\n", decode_exit());
}
/*
* Something potentially bad happened. Alert all processes by setting appropriate shm vars.
* (not always 'bad', reaching max count for eg is one example).
*/
void panic(int reason)
{
shm->spawn_no_more = TRUE;
shm->exit_reason = reason;
}