2 * Each process that gets forked runs this code.
13 #include <sys/resource.h>
14 #include <sys/prctl.h>
23 #include "params.h" // for 'debug'
25 #include "trinity.h" // ARRAY_SIZE
27 static struct rlimit oldrlimit
;
29 static void disable_coredumps(void)
34 (void)signal(SIGSEGV
, SIG_DFL
);
38 getrlimit(RLIMIT_CORE
, &oldrlimit
);
41 limit
.rlim_max
= oldrlimit
.rlim_max
;
42 if (setrlimit(RLIMIT_CORE
, &limit
) != 0)
43 perror( "setrlimit(RLIMIT_CORE)" );
46 static void reenable_coredumps(void)
51 prctl(PR_SET_DUMPABLE
, TRUE
);
53 if (setrlimit(RLIMIT_CORE
, &oldrlimit
) != 0) {
54 outputerr("[%d] Error restoring rlimits to cur:%d max:%d (%s)\n",
56 (unsigned int) oldrlimit
.rlim_cur
,
57 (unsigned int) oldrlimit
.rlim_max
,
61 static void set_make_it_fail(void)
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
)
70 fd
= open("/proc/self/make-it-fail", O_WRONLY
);
74 if (write(fd
, buf
, 1) == -1) {
76 outputerr("writing to /proc/self/make-it-fail failed! (%s)\n", strerror(errno
));
78 shm
->do_make_it_fail
= TRUE
;
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)
90 asm volatile("":"+m" (x
));
92 asm volatile("":"+m" (x
));
95 void init_child(int childno
)
102 shm
->kill_count
[childno
] = 0;
106 if (sched_getaffinity(pid
, sizeof(set
), &set
) == 0) {
108 CPU_SET(childno
, &set
);
109 sched_setaffinity(pid
, sizeof(set
), &set
);
112 shm
->child_syscall_count
[childno
] = 0;
116 if (rand() % 100 < 50)
120 void check_parent_pid(void)
124 static unsigned int parent_check_time
= 10;
127 if (parent_check_time
!= 0)
130 parent_check_time
= 10;
132 if (getppid() == shm
->mainpid
)
137 //FIXME: Add locking so only one child does this output.
138 output(0, BUGTXT
"CHILD (pid:%d) GOT REPARENTED! "
139 "parent pid:%d. Watchdog pid:%d\n",
140 pid
, shm
->mainpid
, watchdog_pid
);
141 output(0, BUGTXT
"Last syscalls:\n");
143 for (i
= 0; i
< MAX_NR_CHILDREN
; i
++) {
144 // Skip over 'boring' entries.
145 if ((shm
->pids
[i
] == -1) &&
146 (shm
->previous_syscallno
[i
] == 0) &&
147 (shm
->child_syscall_count
[i
] == 0))
150 output(0, "[%d] pid:%d call:%s callno:%d\n",
152 print_syscall_name(shm
->previous_syscallno
[i
], shm
->do32bit
[i
]), // FIXME: need previous do32bit
153 shm
->child_syscall_count
[i
]);
155 shm
->exit_reason
= EXIT_REPARENT_PROBLEM
;
157 //TODO: Emergency logging.
163 int (*func
)(int childno
);
166 static const struct child_funcs child_functions
[] = {
167 { .type
= CHILD_RANDOM_SYSCALLS
, .name
= "rand_syscalls", .func
= child_random_syscalls
},
169 { .type
= CHILD_OPEN_ALL_FILES
, .name
= "read_all_files", .func
= child_read_all_files
},
173 int child_process(int childno
)
178 i
= rand() % ARRAY_SIZE(child_functions
);
181 output(0, "Chose %s.\n", child_functions
[i
].name
);
184 shm
->child_type
[childno
] = child_functions
[i
].type
;
185 ret
= child_functions
[i
].func(childno
);
187 reenable_coredumps();