-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathforkserver.c
65 lines (57 loc) · 1.36 KB
/
forkserver.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
/*
* forkserver for AFL
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdint.h>
#include "afl/config.h"
#define PRINT_ERROR(string) (void)(write(2, string, strlen(string))+1)
int forkserver_initialized = 0;
void startForkServer() {
if (forkserver_initialized != 0)
return;
forkserver_initialized = 1;
uint8_t tmp[4];
int32_t child_pid = 0;
#ifdef DEBUG
PRINT_ERROR("starting forkserver\n");
#endif
if (write(FORKSRV_FD + 1, tmp, 4) != 4) {
PRINT_ERROR("Error writing fork server\n");
_exit(1);
}
while (1) {
uint32_t was_killed;
int32_t status;
if (read(FORKSRV_FD, &was_killed, 4) != 4) {
PRINT_ERROR("Error reading fork server\n");
_exit(1);
}
child_pid = fork();
if (child_pid < 0) {
PRINT_ERROR("Error fork\n");
_exit(1);
}
if (child_pid == 0) { // child
close(FORKSRV_FD);
close(FORKSRV_FD + 1);
return;
}
if (write(FORKSRV_FD + 1, &child_pid, 4) != 4) {
PRINT_ERROR("Error writing fork server (2)\n");
_exit(1);
}
if (waitpid(child_pid, &status, 0) < 0) {
PRINT_ERROR("Error waiting for child\n");
_exit(1);
}
if (write(FORKSRV_FD + 1, &status, 4) != 4) {
PRINT_ERROR("Error writing fork server (3)\n");
_exit(1);
}
}
}