| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "ansi-color.h" |
| 6 |
#include "emergency-action.h" |
| 7 |
#include "manager.h" |
| 8 |
#include "reboot-util.h" |
| 9 |
#include "special.h" |
| 10 |
#include "string-table.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "virt.h" |
| 13 |
|
| 14 |
static const char* const emergency_action_table[_EMERGENCY_ACTION_MAX] = { |
| 15 |
[EMERGENCY_ACTION_NONE] = "none", |
| 16 |
[EMERGENCY_ACTION_EXIT] = "exit", |
| 17 |
[EMERGENCY_ACTION_EXIT_FORCE] = "exit-force", |
| 18 |
[EMERGENCY_ACTION_REBOOT] = "reboot", |
| 19 |
[EMERGENCY_ACTION_REBOOT_FORCE] = "reboot-force", |
| 20 |
[EMERGENCY_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate", |
| 21 |
[EMERGENCY_ACTION_POWEROFF] = "poweroff", |
| 22 |
[EMERGENCY_ACTION_POWEROFF_FORCE] = "poweroff-force", |
| 23 |
[EMERGENCY_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate", |
| 24 |
[EMERGENCY_ACTION_SOFT_REBOOT] = "soft-reboot", |
| 25 |
[EMERGENCY_ACTION_SOFT_REBOOT_FORCE] = "soft-reboot-force", |
| 26 |
[EMERGENCY_ACTION_KEXEC] = "kexec", |
| 27 |
[EMERGENCY_ACTION_KEXEC_FORCE] = "kexec-force", |
| 28 |
[EMERGENCY_ACTION_HALT] = "halt", |
| 29 |
[EMERGENCY_ACTION_HALT_FORCE] = "halt-force", |
| 30 |
[EMERGENCY_ACTION_HALT_IMMEDIATE] = "halt-immediate", |
| 31 |
}; |
| 32 |
|
| 33 |
static void log_and_status( |
| 34 |
Manager *m, |
| 35 |
EmergencyAction action, |
| 36 |
EmergencyActionFlags flags, |
| 37 |
const char *message, |
| 38 |
const char *reason) { |
| 39 |
|
| 40 |
assert(m); |
| 41 |
assert(message); |
| 42 |
assert(reason); |
| 43 |
|
| 44 |
log_full(FLAGS_SET(flags, EMERGENCY_ACTION_WARN) ? LOG_WARNING : LOG_DEBUG, |
| 45 |
"%s: %s", message, reason); |
| 46 |
|
| 47 |
bool do_sleep = FLAGS_SET(flags, EMERGENCY_ACTION_WARN|EMERGENCY_ACTION_SLEEP_5S) && |
| 48 |
IN_SET(action, |
| 49 |
EMERGENCY_ACTION_EXIT_FORCE, |
| 50 |
EMERGENCY_ACTION_REBOOT_FORCE, EMERGENCY_ACTION_REBOOT_IMMEDIATE, |
| 51 |
EMERGENCY_ACTION_POWEROFF_FORCE, EMERGENCY_ACTION_POWEROFF_IMMEDIATE, |
| 52 |
EMERGENCY_ACTION_SOFT_REBOOT_FORCE, |
| 53 |
EMERGENCY_ACTION_KEXEC_FORCE); |
| 54 |
|
| 55 |
if (FLAGS_SET(flags, EMERGENCY_ACTION_WARN)) |
| 56 |
manager_status_printf( |
| 57 |
m, |
| 58 |
STATUS_TYPE_EMERGENCY, |
| 59 |
ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL, |
| 60 |
"%s: %s%s", message, reason, |
| 61 |
do_sleep ? ", proceeding in 5s" : ""); |
| 62 |
|
| 63 |
/* Optionally sleep for 5s so that the user can see this output, before we actually execute the |
| 64 |
* operation. Do this only if we immediately execute an operation, i.e. when there's no event loop to |
| 65 |
* feed anymore. */ |
| 66 |
if (do_sleep) |
| 67 |
(void) sleep(5); |
| 68 |
} |
| 69 |
|
| 70 |
void emergency_action( |
| 71 |
Manager *m, |
| 72 |
EmergencyAction action, |
| 73 |
EmergencyActionFlags flags, |
| 74 |
const char *reboot_arg, |
| 75 |
int exit_status, |
| 76 |
const char *reason) { |
| 77 |
|
| 78 |
Unit *u; |
| 79 |
|
| 80 |
assert(m); |
| 81 |
assert(action >= 0); |
| 82 |
assert(action < _EMERGENCY_ACTION_MAX); |
| 83 |
assert((flags & ~_EMERGENCY_ACTION_FLAGS_MAX) == 0); |
| 84 |
assert(reason); |
| 85 |
|
| 86 |
if (action == EMERGENCY_ACTION_NONE) |
| 87 |
return; |
| 88 |
|
| 89 |
/* Is the special shutdown target active or queued? If so, we are in shutdown state */ |
| 90 |
if (IN_SET(action, |
| 91 |
EMERGENCY_ACTION_REBOOT, |
| 92 |
EMERGENCY_ACTION_SOFT_REBOOT, |
| 93 |
EMERGENCY_ACTION_POWEROFF, |
| 94 |
EMERGENCY_ACTION_EXIT, |
| 95 |
EMERGENCY_ACTION_KEXEC, |
| 96 |
EMERGENCY_ACTION_HALT)) { |
| 97 |
u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET); |
| 98 |
if (u && unit_active_or_pending(u)) { |
| 99 |
log_notice("Shutdown is already active. Skipping emergency action request %s.", |
| 100 |
emergency_action_table[action]); |
| 101 |
return; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if (FLAGS_SET(flags, EMERGENCY_ACTION_IS_WATCHDOG) && !m->service_watchdogs) { |
| 106 |
log_warning("Watchdog disabled! Not acting on: %s", reason); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
switch (action) { |
| 111 |
|
| 112 |
case EMERGENCY_ACTION_REBOOT: |
| 113 |
log_and_status(m, action, flags, "Rebooting", reason); |
| 114 |
|
| 115 |
(void) update_reboot_parameter_and_warn(reboot_arg, true); |
| 116 |
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL); |
| 117 |
break; |
| 118 |
|
| 119 |
case EMERGENCY_ACTION_REBOOT_FORCE: |
| 120 |
log_and_status(m, action, flags, "Forcibly rebooting", reason); |
| 121 |
|
| 122 |
(void) update_reboot_parameter_and_warn(reboot_arg, true); |
| 123 |
m->objective = MANAGER_REBOOT; |
| 124 |
break; |
| 125 |
|
| 126 |
case EMERGENCY_ACTION_REBOOT_IMMEDIATE: |
| 127 |
log_and_status(m, action, flags, "Rebooting immediately", reason); |
| 128 |
|
| 129 |
sync(); |
| 130 |
|
| 131 |
if (!isempty(reboot_arg)) { |
| 132 |
log_info("Rebooting with argument '%s'.", reboot_arg); |
| 133 |
(void) raw_reboot(LINUX_REBOOT_CMD_RESTART2, reboot_arg); |
| 134 |
log_warning_errno(errno, "Failed to reboot with parameter, retrying without: %m"); |
| 135 |
} |
| 136 |
|
| 137 |
log_info("Rebooting."); |
| 138 |
(void) reboot(RB_AUTOBOOT); |
| 139 |
break; |
| 140 |
|
| 141 |
case EMERGENCY_ACTION_SOFT_REBOOT: |
| 142 |
log_and_status(m, action, flags, "Soft-rebooting", reason); |
| 143 |
|
| 144 |
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_SOFT_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL); |
| 145 |
break; |
| 146 |
|
| 147 |
case EMERGENCY_ACTION_SOFT_REBOOT_FORCE: |
| 148 |
log_and_status(m, action, flags, "Forcibly soft-rebooting", reason); |
| 149 |
|
| 150 |
m->objective = MANAGER_SOFT_REBOOT; |
| 151 |
break; |
| 152 |
|
| 153 |
case EMERGENCY_ACTION_EXIT: |
| 154 |
|
| 155 |
if (exit_status >= 0) |
| 156 |
m->return_value = exit_status; |
| 157 |
|
| 158 |
if (MANAGER_IS_USER(m) || detect_container() > 0) { |
| 159 |
log_and_status(m, action, flags, "Exiting", reason); |
| 160 |
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL); |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
log_notice("Doing \"poweroff\" action instead of an \"exit\" emergency action."); |
| 165 |
_fallthrough_; |
| 166 |
|
| 167 |
case EMERGENCY_ACTION_POWEROFF: |
| 168 |
log_and_status(m, action, flags, "Powering off", reason); |
| 169 |
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL); |
| 170 |
break; |
| 171 |
|
| 172 |
case EMERGENCY_ACTION_EXIT_FORCE: |
| 173 |
|
| 174 |
if (exit_status >= 0) |
| 175 |
m->return_value = exit_status; |
| 176 |
|
| 177 |
if (MANAGER_IS_USER(m) || detect_container() > 0) { |
| 178 |
log_and_status(m, action, flags, "Exiting immediately", reason); |
| 179 |
m->objective = MANAGER_EXIT; |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
log_notice("Doing \"poweroff-force\" action instead of an \"exit-force\" emergency action."); |
| 184 |
_fallthrough_; |
| 185 |
|
| 186 |
case EMERGENCY_ACTION_POWEROFF_FORCE: |
| 187 |
log_and_status(m, action, flags, "Forcibly powering off", reason); |
| 188 |
m->objective = MANAGER_POWEROFF; |
| 189 |
break; |
| 190 |
|
| 191 |
case EMERGENCY_ACTION_POWEROFF_IMMEDIATE: |
| 192 |
log_and_status(m, action, flags, "Powering off immediately", reason); |
| 193 |
|
| 194 |
sync(); |
| 195 |
|
| 196 |
log_info("Powering off."); |
| 197 |
(void) reboot(RB_POWER_OFF); |
| 198 |
break; |
| 199 |
|
| 200 |
case EMERGENCY_ACTION_KEXEC: |
| 201 |
log_and_status(m, action, flags, "Executing kexec", reason); |
| 202 |
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_KEXEC_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL); |
| 203 |
break; |
| 204 |
|
| 205 |
case EMERGENCY_ACTION_KEXEC_FORCE: |
| 206 |
log_and_status(m, action, flags, "Forcibly executing kexec", reason); |
| 207 |
m->objective = MANAGER_KEXEC; |
| 208 |
break; |
| 209 |
|
| 210 |
case EMERGENCY_ACTION_HALT: |
| 211 |
log_and_status(m, action, flags, "Halting", reason); |
| 212 |
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_HALT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL); |
| 213 |
break; |
| 214 |
|
| 215 |
case EMERGENCY_ACTION_HALT_FORCE: |
| 216 |
log_and_status(m, action, flags, "Forcibly halting", reason); |
| 217 |
m->objective = MANAGER_HALT; |
| 218 |
break; |
| 219 |
|
| 220 |
case EMERGENCY_ACTION_HALT_IMMEDIATE: |
| 221 |
log_and_status(m, action, flags, "Halting immediately", reason); |
| 222 |
|
| 223 |
sync(); |
| 224 |
|
| 225 |
log_info("Halting."); |
| 226 |
(void) reboot(RB_HALT_SYSTEM); |
| 227 |
break; |
| 228 |
|
| 229 |
default: |
| 230 |
assert_not_reached(); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
DEFINE_STRING_TABLE_LOOKUP(emergency_action, EmergencyAction); |
| 235 |
|
| 236 |
int parse_emergency_action( |
| 237 |
const char *value, |
| 238 |
RuntimeScope runtime_scope, |
| 239 |
EmergencyAction *ret) { |
| 240 |
|
| 241 |
EmergencyAction x; |
| 242 |
|
| 243 |
assert(ret); |
| 244 |
|
| 245 |
x = emergency_action_from_string(value); |
| 246 |
if (x < 0) |
| 247 |
return -EINVAL; |
| 248 |
|
| 249 |
if (runtime_scope != RUNTIME_SCOPE_SYSTEM && x > _EMERGENCY_ACTION_LAST_USER_ACTION) |
| 250 |
return -EOPNOTSUPP; |
| 251 |
|
| 252 |
*ret = x; |
| 253 |
return 0; |
| 254 |
} |