2 * Each process that gets forked runs this code.
13 #include <sys/resource.h>
14 #include <sys/prctl.h>
21 #include "params.h" // for 'debug'
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 };
36 (void)signal(SIGABRT
, SIG_DFL
);
37 (void)signal(SIGSEGV
, SIG_DFL
);
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
57 prctl(PR_SET_DUMPABLE
, TRUE
);
59 (void) setrlimit(RLIMIT_CORE
, &limit
);
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
));
97 static void setup_page_maps(void)
102 page
= (void *) page_maps
;
104 for (i
= 0; i
< page_size
/ sizeof(unsigned long); i
++) {
108 page
[i
] = (unsigned long) map
->ptr
;
112 static void oom_score_adj(int adj
)
116 fp
= fopen("/proc/self/oom_score_adj", "w");
120 fprintf(fp
, "%d", adj
);
124 void init_child(int childno
)
127 pid_t pid
= getpid();
130 this_child
= 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
);
142 if (sched_getaffinity(pid
, sizeof(set
), &set
) == 0) {
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
);
156 /* Wait for parent to set our pidslot */
157 while (shm
->pids
[childno
] != getpid()) {
160 /* Make sure parent is actually alive to wait for us. */
161 ret
= pid_alive(shm
->mainpid
);
163 shm
->exit_reason
= EXIT_SHM_CORRUPTION
;
164 outputerr(BUGTXT
"parent (%d) went away!\n", shm
->mainpid
);
169 /* Wait for all the children to start up. */
170 while (shm
->ready
== FALSE
)
175 if (rand() % 100 < 50)
178 mask_signals_child();
183 void check_parent_pid(void)
187 static unsigned int parent_check_time
= 10;
190 if (parent_check_time
!= 0)
193 parent_check_time
= 10;
195 if (getppid() == shm
->mainpid
)
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))
213 output(0, "[%d] pid:%d call:%s callno:%d\n",
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
;
220 //TODO: Emergency logging.
225 int (*func
)(int childno
);
226 unsigned char likelyhood
;
229 static const struct child_funcs child_ops
[] = {
230 { .name
= "rand_syscalls", .func
= child_random_syscalls
, .likelyhood
= 100 },
234 // FIXME: when we have different child ops, we're going to need to redo the progress detector.
235 static unsigned int handle_sigreturn(int childno
)
237 static unsigned int count
= 0;
238 static unsigned int last
= -1;
240 output(2, "<timed out>\n"); /* Flush out the previous syscall output. */
242 /* Check if we're making any progress at all. */
243 if (shm
->child_syscall_count
[childno
] == last
) {
245 //output(1, "no progress for %d tries.\n", count);
248 last
= shm
->child_syscall_count
[childno
];
251 output(1, "no progress for 3 tries, exiting child.\n");
255 if (shm
->kill_count
[childno
] > 0) {
256 output(1, "[%d] Missed a kill signal, exiting\n", getpid());
260 if (sigwas
!= SIGALRM
)
261 output(1, "[%d] Back from signal handler! (sig was %s)\n", getpid(), strsignal(sigwas
));
266 void child_process(int childno
)
270 const char *lastop
= NULL
;
272 ret
= sigsetjmp(ret_jump
, 1);
274 if (handle_sigreturn(childno
) == 0)
275 return; // Exit the child, things are getting too weird.
278 while (shm
->exit_reason
== STILL_RUNNING
) {
282 while (shm
->regenerating
== TRUE
)
285 /* If the parent reseeded, we should reflect the latest seed too. */
286 if (shm
->seed
!= shm
->seeds
[childno
])
289 /* Choose operations for this iteration. */
290 i
= rand() % ARRAY_SIZE(child_ops
);
292 if (rand() % 100 <= child_ops
[i
].likelyhood
) {
293 if (lastop
!= child_ops
[i
].name
) {
294 output(0, "Chose %s.\n", child_ops
[i
].name
);
295 lastop
= child_ops
[i
].name
;
298 ret
= child_ops
[i
].func(childno
); // Do we care about the return code ? Right now, no.