we don't care about child_process's return value.
[trinity.git] / child.c
blob0b5134c2e943ca64d5df77b51f7921fd24d68740
1 /*
2 * Each process that gets forked runs this code.
3 */
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sched.h>
12 #include <sys/time.h>
13 #include <sys/resource.h>
14 #include <sys/prctl.h>
16 #include "arch.h"
17 #include "child.h"
18 #include "list.h"
19 #include "log.h"
20 #include "maps.h"
21 #include "params.h" // for 'debug'
22 #include "pids.h"
23 #include "random.h"
24 #include "shm.h"
25 #include "signals.h"
26 #include "syscall.h"
27 #include "tables.h"
28 #include "trinity.h" // ARRAY_SIZE
29 #include "utils.h" // zmalloc
31 static void disable_coredumps(void)
33 struct rlimit limit = { .rlim_cur = 0, .rlim_max = 0 };
35 if (debug == TRUE) {
36 (void)signal(SIGABRT, SIG_DFL);
37 (void)signal(SIGSEGV, SIG_DFL);
38 return;
41 if (setrlimit(RLIMIT_CORE, &limit) != 0)
42 perror( "setrlimit(RLIMIT_CORE)" );
44 prctl(PR_SET_DUMPABLE, FALSE);
47 static void enable_coredumps(void)
49 struct rlimit limit = {
50 .rlim_cur = RLIM_INFINITY,
51 .rlim_max = RLIM_INFINITY
54 if (debug == TRUE)
55 return;
57 prctl(PR_SET_DUMPABLE, TRUE);
59 (void) setrlimit(RLIMIT_CORE, &limit);
61 static void set_make_it_fail(void)
63 int fd;
64 const char *buf = "1";
66 /* If we failed last time, don't bother trying in future. */
67 if (shm->do_make_it_fail == TRUE)
68 return;
70 fd = open("/proc/self/make-it-fail", O_WRONLY);
71 if (fd == -1)
72 return;
74 if (write(fd, buf, 1) == -1) {
75 if (errno != EPERM)
76 outputerr("writing to /proc/self/make-it-fail failed! (%s)\n", strerror(errno));
77 else
78 shm->do_make_it_fail = TRUE;
80 close(fd);
84 * We call this occasionally to set some FPU state, in the hopes that we
85 * might tickle some weird FPU/scheduler related bugs
87 static void use_fpu(void)
89 double x = 0;
90 asm volatile("":"+m" (x));
91 x += 1;
92 asm volatile("":"+m" (x));
95 int this_child = 0;
97 static void setup_page_maps(void)
99 unsigned long *page;
100 unsigned int i;
102 page = (void *) page_maps;
104 for (i = 0; i < page_size / sizeof(unsigned long); i++) {
105 struct map *map;
107 map = get_map();
108 page[i] = (unsigned long) map->ptr;
112 static void oom_score_adj(int adj)
114 FILE *fp;
116 fp = fopen("/proc/self/oom_score_adj", "w");
117 if (!fp)
118 return;
120 fprintf(fp, "%d", adj);
121 fclose(fp);
124 void init_child(int childno)
126 cpu_set_t set;
127 pid_t pid = getpid();
128 char childname[17];
130 this_child = childno;
132 set_seed(childno);
134 shm->kill_count[childno] = 0;
136 shm->num_mappings[childno] = 0;
137 shm->mappings[childno] = zmalloc(sizeof(struct map));
138 INIT_LIST_HEAD(&shm->mappings[childno]->list);
140 setup_page_maps();
142 if (sched_getaffinity(pid, sizeof(set), &set) == 0) {
143 CPU_ZERO(&set);
144 CPU_SET(childno, &set);
145 sched_setaffinity(pid, sizeof(set), &set);
148 shm->child_syscall_count[childno] = 0;
150 memset(childname, 0, sizeof(childname));
151 sprintf(childname, "trinity-c%d", childno);
152 prctl(PR_SET_NAME, (unsigned long) &childname);
154 oom_score_adj(500);
156 /* Wait for parent to set our pidslot */
157 while (shm->pids[childno] != getpid()) {
158 int ret = 0;
160 /* Make sure parent is actually alive to wait for us. */
161 ret = pid_alive(shm->mainpid);
162 if (ret != 0) {
163 shm->exit_reason = EXIT_SHM_CORRUPTION;
164 outputerr(BUGTXT "parent (%d) went away!\n", shm->mainpid);
165 sleep(20000);
169 /* Wait for all the children to start up. */
170 while (shm->ready == FALSE)
171 sleep(1);
173 set_make_it_fail();
175 if (rand() % 100 < 50)
176 use_fpu();
178 mask_signals_child();
180 disable_coredumps();
183 void check_parent_pid(void)
185 pid_t pid;
186 unsigned int i;
187 static unsigned int parent_check_time = 10;
189 parent_check_time--;
190 if (parent_check_time != 0)
191 return;
193 parent_check_time = 10;
195 if (getppid() == shm->mainpid)
196 return;
198 pid = getpid();
200 //FIXME: Add locking so only one child does this output.
201 output(0, BUGTXT "CHILD (pid:%d) GOT REPARENTED! "
202 "parent pid:%d. Watchdog pid:%d\n",
203 pid, shm->mainpid, watchdog_pid);
204 output(0, BUGTXT "Last syscalls:\n");
206 for_each_pidslot(i) {
207 // Skip over 'boring' entries.
208 if ((shm->pids[i] == EMPTY_PIDSLOT) &&
209 (shm->previous[i].nr == 0) &&
210 (shm->child_syscall_count[i] == 0))
211 continue;
213 output(0, "[%d] pid:%d call:%s callno:%d\n",
214 i, shm->pids[i],
215 print_syscall_name(shm->previous[i].nr, shm->previous[i].do32bit),
216 shm->child_syscall_count[i]);
218 shm->exit_reason = EXIT_REPARENT_PROBLEM;
219 exit(EXIT_FAILURE);
220 //TODO: Emergency logging.
223 struct child_funcs {
224 int type;
225 const char *name;
226 int (*func)(int childno);
227 unsigned char likelyhood;
230 static const struct child_funcs child_ops[] = {
231 { .type = CHILD_RANDOM_SYSCALLS, .name = "rand_syscalls", .func = child_random_syscalls, 100 },
232 // { .type = CHILD_OPEN_ALL_FILES, .name = "read_all_files", .func = child_read_all_files },
236 // FIXME: when we have different child ops, we're going to need to redo the progress detector.
237 static unsigned int handle_sigreturn(int childno)
239 static unsigned int count = 0;
240 static unsigned int last = -1;
242 output(2, "<timed out>\n"); /* Flush out the previous syscall output. */
244 /* Check if we're making any progress at all. */
245 if (shm->child_syscall_count[childno] == last) {
246 count++;
247 //output(1, "no progress for %d tries.\n", count);
248 } else {
249 count = 0;
250 last = shm->child_syscall_count[childno];
252 if (count == 3) {
253 output(1, "no progress for 3 tries, exiting child.\n");
254 return 0;
257 if (shm->kill_count[childno] > 0) {
258 output(1, "[%d] Missed a kill signal, exiting\n", getpid());
259 return 0;
262 if (sigwas != SIGALRM)
263 output(1, "[%d] Back from signal handler! (sig was %s)\n", getpid(), strsignal(sigwas));
265 return 1;
268 void child_process(int childno)
270 int ret;
271 unsigned int i;
272 const char *lastop = NULL;
274 ret = sigsetjmp(ret_jump, 1);
275 if (ret != 0) {
276 if (handle_sigreturn(childno) == 0)
277 return; // Exit the child, things are getting too weird.
280 while (shm->exit_reason == STILL_RUNNING) {
282 check_parent_pid();
284 while (shm->regenerating == TRUE)
285 sleep(1);
287 /* If the parent reseeded, we should reflect the latest seed too. */
288 if (shm->seed != shm->seeds[childno])
289 set_seed(childno);
291 /* Choose operations for this iteration. */
292 i = rand() % ARRAY_SIZE(child_ops);
294 if (rand() % 100 <= child_ops[i].likelyhood) {
295 if (lastop != child_ops[i].name) {
296 output(0, "Chose %s.\n", child_ops[i].name);
297 lastop = child_ops[i].name;
300 shm->child_type[childno] = child_ops[i].type;
301 ret = child_ops[i].func(childno); // Do we care about the return code ? Right now, no.
305 enable_coredumps();