1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| | #define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/syscall.h> /* For SYS_xxx definitions */
#include <signal.h>
static void
handle_crash (int sig)
{
static const char msg[] = "Shepherd crashed!\n";
write (2, msg, sizeof msg);
#ifdef __sparc__
/* See 'raw_clone' in systemd. */
# error "SPARC uses a different 'clone' syscall convention"
#endif
pid_t pid = syscall (SYS_clone, SIGCHLD, NULL);
if (pid < 0)
abort ();
if (pid == 0)
{
/* Restore the default signal handler to get a core dump. */
signal (sig, SIG_DFL);
const struct rlimit infinity = { RLIM_INFINITY, RLIM_INFINITY };
setrlimit (RLIMIT_CORE, &infinity);
chdir ("/");
int pid = syscall (SYS_getpid);
kill (pid, sig);
/* As it turns out, 'kill' simply returns without doing anything, which
is consistent with the "Notes" section of kill(2). Thus, force a
crash. */
* (int *) 0 = 42;
_exit (254);
}
else
{
signal (sig, SIG_IGN);
int status;
waitpid (pid, &status, 0);
sync ();
_exit (255);
}
_exit (253);
}
static void initialize_crash_handler (void)
__attribute__ ((constructor));
static void
initialize_crash_handler (void)
{
signal (SIGSEGV, handle_crash);
signal (SIGABRT, handle_crash);
}
|