2 * Each process that gets forked runs this code.
13 #include <sys/resource.h>
14 #include <sys/prctl.h>
20 #include "params.h" // for 'debug'
27 #include "trinity.h" // ARRAY_SIZE
28 #include "utils.h" // zmalloc
30 static struct rlimit oldrlimit
;
32 static void disable_coredumps(void)
37 (void)signal(SIGSEGV
, SIG_DFL
);
41 getrlimit(RLIMIT_CORE
, &oldrlimit
);
44 limit
.rlim_max
= oldrlimit
.rlim_max
;
45 if (setrlimit(RLIMIT_CORE
, &limit
) != 0)
46 perror( "setrlimit(RLIMIT_CORE)" );
49 static void reenable_coredumps(void)
54 prctl(PR_SET_DUMPABLE
, TRUE
);
56 if (setrlimit(RLIMIT_CORE
, &oldrlimit
) != 0) {
57 outputerr("[%d] Error restoring rlimits to cur:%d max:%d (%s)\n",
59 (unsigned int) oldrlimit
.rlim_cur
,
60 (unsigned int) oldrlimit
.rlim_max
,
64 static void set_make_it_fail(void)
67 const char *buf
= "1";
69 /* If we failed last time, don't bother trying in future. */
70 if (shm
->do_make_it_fail
== TRUE
)
73 fd
= open("/proc/self/make-it-fail", O_WRONLY
);
77 if (write(fd
, buf
, 1) == -1) {
79 outputerr("writing to /proc/self/make-it-fail failed! (%s)\n", strerror(errno
));
81 shm
->do_make_it_fail
= TRUE
;
87 * We call this occasionally to set some FPU state, in the hopes that we
88 * might tickle some weird FPU/scheduler related bugs
90 static void use_fpu(void)
93 asm volatile("":"+m" (x
));
95 asm volatile("":"+m" (x
));
100 void init_child(int childno
)
103 pid_t pid
= getpid();
105 this_child
= childno
;
109 shm
->kill_count
[childno
] = 0;
113 if (sched_getaffinity(pid
, sizeof(set
), &set
) == 0) {
115 CPU_SET(childno
, &set
);
116 sched_setaffinity(pid
, sizeof(set
), &set
);
119 shm
->child_syscall_count
[childno
] = 0;
123 if (rand() % 100 < 50)
126 shm
->num_mappings
[childno
] = 0;
127 shm
->mappings
[childno
] = zmalloc(sizeof(struct map
));
128 INIT_LIST_HEAD(&shm
->mappings
[childno
]->list
);
131 void check_parent_pid(void)
135 static unsigned int parent_check_time
= 10;
138 if (parent_check_time
!= 0)
141 parent_check_time
= 10;
143 if (getppid() == shm
->mainpid
)
148 //FIXME: Add locking so only one child does this output.
149 output(0, BUGTXT
"CHILD (pid:%d) GOT REPARENTED! "
150 "parent pid:%d. Watchdog pid:%d\n",
151 pid
, shm
->mainpid
, watchdog_pid
);
152 output(0, BUGTXT
"Last syscalls:\n");
154 for (i
= 0; i
< MAX_NR_CHILDREN
; i
++) {
155 // Skip over 'boring' entries.
156 if ((shm
->pids
[i
] == -1) &&
157 (shm
->previous_syscallno
[i
] == 0) &&
158 (shm
->child_syscall_count
[i
] == 0))
161 output(0, "[%d] pid:%d call:%s callno:%d\n",
163 print_syscall_name(shm
->previous_syscallno
[i
], shm
->do32bit
[i
]), // FIXME: need previous do32bit
164 shm
->child_syscall_count
[i
]);
166 shm
->exit_reason
= EXIT_REPARENT_PROBLEM
;
168 //TODO: Emergency logging.
174 int (*func
)(int childno
);
177 static const struct child_funcs child_functions
[] = {
178 { .type
= CHILD_RANDOM_SYSCALLS
, .name
= "rand_syscalls", .func
= child_random_syscalls
},
180 { .type
= CHILD_OPEN_ALL_FILES
, .name
= "read_all_files", .func
= child_read_all_files
},
184 int child_process(int childno
)
189 i
= rand() % ARRAY_SIZE(child_functions
);
192 output(0, "Chose %s.\n", child_functions
[i
].name
);
195 shm
->child_type
[childno
] = child_functions
[i
].type
;
196 ret
= child_functions
[i
].func(childno
);
198 reenable_coredumps();