/* We want to wait for the core process, hence let's enable SIGCHLD */
sigaction(SIGCHLD, &sa, NULL);
- pid = fork();
+ pid = raw_clone(SIGCHLD, NULL);
if (pid < 0)
log_emergency_errno(errno, "Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig));
chdir("/");
/* Raise the signal again */
- raise(sig);
+ pid = raw_getpid();
+ kill(pid, sig); /* raise() would kill the parent */
assert_not_reached("We shouldn't be here...");
_exit(1);
-
} else {
siginfo_t status;
int r;
if (r < 0)
log_emergency_errno(r, "Caught <%s>, waitpid() failed: %m", signal_to_string(sig));
else if (status.si_code != CLD_DUMPED)
- log_emergency("Caught <%s>, core dump failed.", signal_to_string(sig));
+ log_emergency("Caught <%s>, core dump failed (child "PID_FMT", code=%s, status=%i/%s).",
+ signal_to_string(sig),
+ pid, sigchld_code_to_string(status.si_code),
+ status.si_status,
+ strna(status.si_code == CLD_EXITED
+ ? exit_status_to_string(status.si_status, EXIT_STATUS_FULL)
+ : signal_to_string(status.si_status)));
else
log_emergency("Caught <%s>, dumped core as pid "PID_FMT".", signal_to_string(sig), pid);
}
/* Let the kernel reap children for us */
assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
- pid = fork();
+ pid = raw_clone(SIGCHLD, NULL);
if (pid < 0)
log_emergency_errno(errno, "Failed to fork off crash shell: %m");
else if (pid == 0) {
make_console_stdio();
- execl("/bin/sh", "/bin/sh", NULL);
+ execle("/bin/sh", "/bin/sh", NULL, environ);
- log_emergency_errno(errno, "execl() failed: %m");
+ log_emergency_errno(errno, "execle() failed: %m");
_exit(1);
- }
-
- log_info("Successfully spawned crash shell as pid "PID_FMT".", pid);
+ } else
+ log_info("Successfully spawned crash shell as PID "PID_FMT".", pid);
}
log_emergency("Freezing execution.");
#define CAP_AUDIT_READ 37
#endif
-static inline long raw_clone(unsigned long flags, void *child_stack) {
+static inline int raw_clone(unsigned long flags, void *child_stack) {
#if defined(__s390__) || defined(__CRIS__)
/* On s390 and cris the order of the first and second arguments
* of the raw clone() system call is reversed. */
- return syscall(__NR_clone, child_stack, flags);
+ return (int) syscall(__NR_clone, child_stack, flags);
#else
- return syscall(__NR_clone, flags, child_stack);
+ return (int) syscall(__NR_clone, flags, child_stack);
#endif
}
+
+static inline pid_t raw_getpid(void) {
+ return (pid_t) syscall(__NR_getpid);
+}
assert_se(parse_proc_cmdline(parse_item) >= 0);
}
+static void test_raw_clone(void) {
+ pid_t parent, pid, pid2;
+
+ parent = getpid();
+ log_info("before clone: getpid()→"PID_FMT, parent);
+ assert_se(raw_getpid() == parent);
+
+ pid = raw_clone(0, NULL);
+ assert(pid >= 0);
+
+ pid2 = raw_getpid();
+ log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
+ pid, getpid(), pid2);
+ if (pid == 0)
+ assert(pid2 != parent);
+ else
+ assert(pid2 == parent);
+}
+
int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
test_unquote_first_word();
test_unquote_many_words();
test_parse_proc_cmdline();
+ test_raw_clone();
return 0;
}