| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/reboot.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-messages.h" |
| 7 |
|
| 8 |
#include "constants.h" |
| 9 |
#include "crash-handler.h" |
| 10 |
#include "dlfcn-util.h" |
| 11 |
#include "exit-status.h" |
| 12 |
#include "format-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "main.h" |
| 15 |
#include "pidref.h" |
| 16 |
#include "process-util.h" |
| 17 |
#include "raw-clone.h" |
| 18 |
#include "rlimit-util.h" |
| 19 |
#include "signal-util.h" |
| 20 |
#include "string-table.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "terminal-util.h" |
| 23 |
#include "virt.h" |
| 24 |
|
| 25 |
_noreturn_ void freeze_or_exit_or_reboot(void) { |
| 26 |
|
| 27 |
/* If we are running in a container, let's prefer exiting, after all we can propagate an exit code to |
| 28 |
* the container manager, and thus inform it that something went wrong. */ |
| 29 |
if (detect_container() > 0) { |
| 30 |
log_struct(LOG_EMERG, |
| 31 |
LOG_MESSAGE("Exiting PID 1..."), |
| 32 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_EXIT_STR)); |
| 33 |
_exit(EXIT_EXCEPTION); |
| 34 |
} |
| 35 |
|
| 36 |
if (arg_crash_action == CRASH_POWEROFF) { |
| 37 |
log_notice("Shutting down..."); |
| 38 |
(void) reboot(RB_POWER_OFF); |
| 39 |
log_struct_errno(LOG_EMERG, errno, |
| 40 |
LOG_MESSAGE("Failed to power off: %m"), |
| 41 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_FAILED_STR)); |
| 42 |
} else if (arg_crash_action == CRASH_REBOOT) { |
| 43 |
log_notice("Rebooting in 10s..."); |
| 44 |
(void) sleep(10); |
| 45 |
|
| 46 |
log_notice("Rebooting now..."); |
| 47 |
(void) reboot(RB_AUTOBOOT); |
| 48 |
log_struct_errno(LOG_EMERG, errno, |
| 49 |
LOG_MESSAGE("Failed to reboot: %m"), |
| 50 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_FAILED_STR)); |
| 51 |
} |
| 52 |
|
| 53 |
log_struct(LOG_EMERG, |
| 54 |
LOG_MESSAGE("Freezing execution."), |
| 55 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_FREEZE_STR)); |
| 56 |
sync(); |
| 57 |
freeze(); |
| 58 |
} |
| 59 |
|
| 60 |
_noreturn_ static void crash(int sig, siginfo_t *siginfo, void *context) { |
| 61 |
static const struct sigaction sa_nocldwait = { |
| 62 |
.sa_handler = SIG_IGN, |
| 63 |
.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART, |
| 64 |
}; |
| 65 |
|
| 66 |
pid_t pid; |
| 67 |
int r; |
| 68 |
|
| 69 |
/* NB: 💣 💣 💣 This is a signal handler, most likely executed in a situation where we have corrupted |
| 70 |
* memory. Thus: please avoid any libc memory allocation here, or any functions that internally use |
| 71 |
* memory allocation, as we cannot rely on memory allocation still working at this point! (Note that |
| 72 |
* memory allocation is not async-signal-safe anyway — see signal-safety(7) for details —, and thus |
| 73 |
* is not permissible in signal handlers.) */ |
| 74 |
|
| 75 |
block_dlopen(); /* paranoia: never end up doing dlopen() as side-effect from some call anymore from |
| 76 |
* here */ |
| 77 |
|
| 78 |
if (getpid_cached() != 1) |
| 79 |
/* Pass this on immediately, if this is not PID 1 */ |
| 80 |
propagate_signal(sig, siginfo); |
| 81 |
else if (!arg_dump_core) |
| 82 |
log_struct(LOG_EMERG, |
| 83 |
LOG_MESSAGE("Caught <%s>, not dumping core.", signal_to_string(sig)), |
| 84 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_NO_COREDUMP_STR)); |
| 85 |
else { |
| 86 |
/* We want to wait for the core process, hence let's enable SIGCHLD */ |
| 87 |
(void) sigaction(SIGCHLD, &sigaction_nop_nocldstop, NULL); |
| 88 |
|
| 89 |
pid = raw_clone(SIGCHLD); |
| 90 |
if (pid < 0) |
| 91 |
log_struct_errno(LOG_EMERG, errno, |
| 92 |
LOG_MESSAGE("Caught <%s>, cannot fork for core dump: %m", signal_to_string(sig)), |
| 93 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_NO_FORK_STR)); |
| 94 |
else if (pid == 0) { |
| 95 |
/* Enable default signal handler for core dump */ |
| 96 |
|
| 97 |
(void) sigaction(sig, &sigaction_default, NULL); |
| 98 |
|
| 99 |
/* Don't limit the coredump size */ |
| 100 |
(void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)); |
| 101 |
|
| 102 |
/* Just to be sure... */ |
| 103 |
(void) chdir("/"); |
| 104 |
|
| 105 |
/* Raise the signal again */ |
| 106 |
propagate_signal(sig, siginfo); |
| 107 |
assert_not_reached(); |
| 108 |
_exit(EXIT_EXCEPTION); |
| 109 |
} else { |
| 110 |
siginfo_t status; |
| 111 |
|
| 112 |
if (siginfo) { |
| 113 |
if (!si_code_from_process(siginfo->si_code)) |
| 114 |
log_struct(LOG_EMERG, |
| 115 |
LOG_MESSAGE("Caught <%s>.", signal_to_string(sig)), |
| 116 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_UNKNOWN_SIGNAL_STR)); |
| 117 |
else if (siginfo->si_pid == 0) |
| 118 |
log_struct(LOG_EMERG, |
| 119 |
LOG_MESSAGE("Caught <%s>, from unknown sender process.", signal_to_string(sig)), |
| 120 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_UNKNOWN_SIGNAL_STR)); |
| 121 |
else if (siginfo->si_pid == 1) |
| 122 |
log_struct(LOG_EMERG, |
| 123 |
LOG_MESSAGE("Caught <%s>, from our own process.", signal_to_string(sig)), |
| 124 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_SYSTEMD_SIGNAL_STR)); |
| 125 |
else |
| 126 |
log_struct(LOG_EMERG, |
| 127 |
LOG_MESSAGE("Caught <%s> from PID "PID_FMT".", signal_to_string(sig), siginfo->si_pid), |
| 128 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_PROCESS_SIGNAL_STR)); |
| 129 |
} |
| 130 |
|
| 131 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pid); |
| 132 |
(void) pidref_set_pid(&pidref, pid); |
| 133 |
|
| 134 |
/* Order things nicely. */ |
| 135 |
r = pidref_wait_for_terminate(&pidref, &status); |
| 136 |
if (r < 0) |
| 137 |
log_struct_errno(LOG_EMERG, r, |
| 138 |
LOG_MESSAGE("Caught <%s>, waitpid() failed: %m", signal_to_string(sig)), |
| 139 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_WAITPID_FAILED_STR)); |
| 140 |
else if (status.si_code != CLD_DUMPED) { |
| 141 |
const char *s = status.si_code == CLD_EXITED ? |
| 142 |
exit_status_to_string(status.si_status, EXIT_STATUS_LIBC) : |
| 143 |
signal_to_string(status.si_status); |
| 144 |
|
| 145 |
log_struct(LOG_EMERG, |
| 146 |
LOG_MESSAGE("Caught <%s>, core dump failed (child "PID_FMT", code=%s, status=%i/%s).", |
| 147 |
signal_to_string(sig), |
| 148 |
pid, |
| 149 |
sigchld_code_to_string(status.si_code), |
| 150 |
status.si_status, |
| 151 |
strna(s)), |
| 152 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_COREDUMP_FAILED_STR)); |
| 153 |
} else |
| 154 |
log_struct(LOG_EMERG, |
| 155 |
LOG_MESSAGE("Caught <%s>, dumped core as pid "PID_FMT".", |
| 156 |
signal_to_string(sig), pid), |
| 157 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_COREDUMP_PID_STR)); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (arg_crash_chvt >= 0) |
| 162 |
(void) chvt(arg_crash_chvt); |
| 163 |
|
| 164 |
/* Let the kernel reap children for us */ |
| 165 |
(void) sigaction(SIGCHLD, &sa_nocldwait, NULL); |
| 166 |
|
| 167 |
if (arg_crash_shell) { |
| 168 |
log_notice("Executing crash shell..."); |
| 169 |
|
| 170 |
pid = raw_clone(SIGCHLD); |
| 171 |
if (pid < 0) |
| 172 |
log_struct_errno(LOG_EMERG, errno, |
| 173 |
LOG_MESSAGE("Failed to fork off crash shell: %m"), |
| 174 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_SHELL_FORK_FAILED_STR)); |
| 175 |
else if (pid == 0) { |
| 176 |
(void) setsid(); |
| 177 |
(void) terminal_vhangup("/dev/console"); |
| 178 |
(void) make_console_stdio(); |
| 179 |
(void) rlimit_nofile_safe(); |
| 180 |
(void) execle("/bin/sh", "/bin/sh", NULL, environ); |
| 181 |
|
| 182 |
log_struct_errno(LOG_EMERG, errno, |
| 183 |
LOG_MESSAGE("execle() failed: %m"), |
| 184 |
LOG_MESSAGE_ID(SD_MESSAGE_CRASH_EXECLE_FAILED_STR)); |
| 185 |
_exit(EXIT_EXCEPTION); |
| 186 |
} else { |
| 187 |
log_info("Spawned crash shell as PID "PID_FMT".", pid); |
| 188 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pid); |
| 189 |
(void) pidref_set_pid(&pidref, pid); |
| 190 |
(void) pidref_wait_for_terminate(&pidref, NULL); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
freeze_or_exit_or_reboot(); |
| 195 |
} |
| 196 |
|
| 197 |
void install_crash_handler(void) { |
| 198 |
static const struct sigaction sa = { |
| 199 |
.sa_sigaction = crash, |
| 200 |
.sa_flags = SA_NODEFER | SA_SIGINFO, /* So that we can raise the signal again from the signal handler */ |
| 201 |
}; |
| 202 |
int r; |
| 203 |
|
| 204 |
/* We ignore the return value here, since, we don't mind if we cannot set up a crash handler */ |
| 205 |
r = sigaction_many(&sa, SIGNALS_CRASH_HANDLER); |
| 206 |
if (r < 0) |
| 207 |
log_debug_errno(r, "I had trouble setting up the crash handler, ignoring: %m"); |
| 208 |
} |
| 209 |
|
| 210 |
static const char* const crash_action_table[_CRASH_ACTION_MAX] = { |
| 211 |
[CRASH_FREEZE] = "freeze", |
| 212 |
[CRASH_REBOOT] = "reboot", |
| 213 |
[CRASH_POWEROFF] = "poweroff", |
| 214 |
}; |
| 215 |
|
| 216 |
DEFINE_STRING_TABLE_LOOKUP(crash_action, CrashAction); |