File: crash-handler.c

package info (click to toggle)
shepherd 1.0.9-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,828 kB
  • sloc: lisp: 8,779; sh: 3,586; makefile: 290; ansic: 50
file content (93 lines) | stat: -rw-r--r-- 2,562 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* crash-handler.c -- PID 1 helper to dump core upon a crash on GNU/Linux.
   Copyright © 2019 Ludovic Courtès <ludo@gnu.org>

   This file is part of the GNU Shepherd.

   The GNU Shepherd is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or (at
   your option) any later version.

   The GNU Shepherd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Shepherd.  If not, see <https://www.gnu.org/licenses/>.  */

#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>

/* Arrange to dump core.  */
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

  /* Start a child process that will be able to dump core.  */
  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
    {
      /* Wait for the child process and terminate.  */
      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)
{
  /* Register raw signal handlers.  This cannot be done in Scheme because the
     handler can only call async-signal-safe functions.  */
  signal (SIGSEGV, handle_crash);
  signal (SIGABRT, handle_crash);
}