2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-08-22 16:36:38 +02:00
|
|
|
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
|
|
|
|
#include "bus-error.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "bus-util.h"
|
2016-10-20 15:27:37 +02:00
|
|
|
#include "emergency-action.h"
|
2018-02-21 17:42:59 +01:00
|
|
|
#include "raw-reboot.h"
|
2018-02-21 17:54:35 +01:00
|
|
|
#include "reboot-util.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "special.h"
|
|
|
|
#include "string-table.h"
|
2015-04-10 23:15:59 +02:00
|
|
|
#include "terminal-util.h"
|
2018-10-16 16:34:45 +02:00
|
|
|
#include "virt.h"
|
2014-08-22 16:36:38 +02:00
|
|
|
|
2020-04-10 23:38:42 +02:00
|
|
|
static const char* const emergency_action_table[_EMERGENCY_ACTION_MAX] = {
|
|
|
|
[EMERGENCY_ACTION_NONE] = "none",
|
|
|
|
[EMERGENCY_ACTION_REBOOT] = "reboot",
|
|
|
|
[EMERGENCY_ACTION_REBOOT_FORCE] = "reboot-force",
|
|
|
|
[EMERGENCY_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate",
|
|
|
|
[EMERGENCY_ACTION_POWEROFF] = "poweroff",
|
|
|
|
[EMERGENCY_ACTION_POWEROFF_FORCE] = "poweroff-force",
|
|
|
|
[EMERGENCY_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate",
|
|
|
|
[EMERGENCY_ACTION_EXIT] = "exit",
|
|
|
|
[EMERGENCY_ACTION_EXIT_FORCE] = "exit-force",
|
|
|
|
};
|
|
|
|
|
2018-10-17 17:27:20 +02:00
|
|
|
static void log_and_status(Manager *m, bool warn, const char *message, const char *reason) {
|
|
|
|
log_full(warn ? LOG_WARNING : LOG_DEBUG, "%s: %s", message, reason);
|
|
|
|
if (warn)
|
|
|
|
manager_status_printf(m, STATUS_TYPE_EMERGENCY,
|
|
|
|
ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL,
|
|
|
|
"%s: %s", message, reason);
|
2014-10-28 04:14:23 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 13:20:54 +01:00
|
|
|
void emergency_action(
|
2014-08-22 16:36:38 +02:00
|
|
|
Manager *m,
|
2016-10-20 15:27:37 +02:00
|
|
|
EmergencyAction action,
|
2018-10-16 15:24:44 +02:00
|
|
|
EmergencyActionFlags options,
|
2016-10-20 15:27:37 +02:00
|
|
|
const char *reboot_arg,
|
2018-11-16 11:41:18 +01:00
|
|
|
int exit_status,
|
2016-10-20 15:27:37 +02:00
|
|
|
const char *reason) {
|
2014-08-22 16:36:38 +02:00
|
|
|
|
2020-04-10 23:38:42 +02:00
|
|
|
Unit *u;
|
|
|
|
|
2014-08-22 16:36:38 +02:00
|
|
|
assert(m);
|
|
|
|
assert(action >= 0);
|
2016-10-20 15:27:37 +02:00
|
|
|
assert(action < _EMERGENCY_ACTION_MAX);
|
2014-08-22 16:36:38 +02:00
|
|
|
|
2020-04-10 23:38:42 +02:00
|
|
|
/* Is the special shutdown target active or queued? If so, we are in shutdown state */
|
|
|
|
if (IN_SET(action, EMERGENCY_ACTION_REBOOT, EMERGENCY_ACTION_POWEROFF, EMERGENCY_ACTION_EXIT)) {
|
|
|
|
u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
|
|
|
|
if (u && unit_active_or_pending(u)) {
|
|
|
|
log_notice("Shutdown is already active. Skipping emergency action request %s.",
|
|
|
|
emergency_action_table[action]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 15:27:37 +02:00
|
|
|
if (action == EMERGENCY_ACTION_NONE)
|
2019-03-18 13:20:54 +01:00
|
|
|
return;
|
2014-08-22 16:36:38 +02:00
|
|
|
|
2018-10-16 15:24:44 +02:00
|
|
|
if (FLAGS_SET(options, EMERGENCY_ACTION_IS_WATCHDOG) && !m->service_watchdogs) {
|
2017-03-20 13:10:43 +01:00
|
|
|
log_warning("Watchdog disabled! Not acting on: %s", reason);
|
2019-03-18 13:20:54 +01:00
|
|
|
return;
|
2017-03-20 13:10:43 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 17:27:20 +02:00
|
|
|
bool warn = FLAGS_SET(options, EMERGENCY_ACTION_WARN);
|
|
|
|
|
2014-08-22 16:59:46 +02:00
|
|
|
switch (action) {
|
2014-08-22 16:36:38 +02:00
|
|
|
|
2016-10-20 15:27:37 +02:00
|
|
|
case EMERGENCY_ACTION_REBOOT:
|
2018-10-17 17:27:20 +02:00
|
|
|
log_and_status(m, warn, "Rebooting", reason);
|
2014-08-22 16:36:38 +02:00
|
|
|
|
2019-03-09 21:30:45 +01:00
|
|
|
(void) update_reboot_parameter_and_warn(reboot_arg, true);
|
2019-03-22 20:57:30 +01:00
|
|
|
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
|
2014-08-22 16:36:38 +02:00
|
|
|
break;
|
|
|
|
|
2016-10-20 15:27:37 +02:00
|
|
|
case EMERGENCY_ACTION_REBOOT_FORCE:
|
2018-10-17 17:27:20 +02:00
|
|
|
log_and_status(m, warn, "Forcibly rebooting", reason);
|
2014-10-28 04:14:23 +01:00
|
|
|
|
2019-03-09 21:30:45 +01:00
|
|
|
(void) update_reboot_parameter_and_warn(reboot_arg, true);
|
2018-10-09 15:42:19 +02:00
|
|
|
m->objective = MANAGER_REBOOT;
|
2016-04-07 16:53:37 +02:00
|
|
|
|
2014-08-22 16:36:38 +02:00
|
|
|
break;
|
|
|
|
|
2016-10-20 15:27:37 +02:00
|
|
|
case EMERGENCY_ACTION_REBOOT_IMMEDIATE:
|
2018-10-17 17:27:20 +02:00
|
|
|
log_and_status(m, warn, "Rebooting immediately", reason);
|
2014-08-22 16:36:38 +02:00
|
|
|
|
|
|
|
sync();
|
|
|
|
|
2016-04-07 16:53:37 +02:00
|
|
|
if (!isempty(reboot_arg)) {
|
2014-08-22 16:36:38 +02:00
|
|
|
log_info("Rebooting with argument '%s'.", reboot_arg);
|
2018-02-21 17:42:59 +01:00
|
|
|
(void) raw_reboot(LINUX_REBOOT_CMD_RESTART2, reboot_arg);
|
2016-04-07 16:53:37 +02:00
|
|
|
log_warning_errno(errno, "Failed to reboot with parameter, retrying without: %m");
|
2014-08-22 16:36:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log_info("Rebooting.");
|
2018-02-21 17:46:55 +01:00
|
|
|
(void) reboot(RB_AUTOBOOT);
|
2014-08-22 16:36:38 +02:00
|
|
|
break;
|
|
|
|
|
2018-10-16 13:28:39 +02:00
|
|
|
case EMERGENCY_ACTION_EXIT:
|
2018-11-16 11:41:18 +01:00
|
|
|
|
|
|
|
if (exit_status >= 0)
|
|
|
|
m->return_value = exit_status;
|
|
|
|
|
2018-10-16 16:34:45 +02:00
|
|
|
if (MANAGER_IS_USER(m) || detect_container() > 0) {
|
2018-10-17 17:27:20 +02:00
|
|
|
log_and_status(m, warn, "Exiting", reason);
|
2019-03-22 20:57:30 +01:00
|
|
|
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
|
2018-10-16 16:34:45 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-16 13:28:39 +02:00
|
|
|
|
2018-10-16 16:34:45 +02:00
|
|
|
log_notice("Doing \"poweroff\" action instead of an \"exit\" emergency action.");
|
|
|
|
_fallthrough_;
|
2018-10-16 13:28:39 +02:00
|
|
|
|
2016-10-20 15:27:37 +02:00
|
|
|
case EMERGENCY_ACTION_POWEROFF:
|
2018-10-17 17:27:20 +02:00
|
|
|
log_and_status(m, warn, "Powering off", reason);
|
2019-03-22 20:57:30 +01:00
|
|
|
(void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL, NULL);
|
2014-08-22 16:59:46 +02:00
|
|
|
break;
|
|
|
|
|
2018-10-16 13:28:39 +02:00
|
|
|
case EMERGENCY_ACTION_EXIT_FORCE:
|
2018-11-16 11:41:18 +01:00
|
|
|
|
|
|
|
if (exit_status >= 0)
|
|
|
|
m->return_value = exit_status;
|
|
|
|
|
2018-10-16 16:34:45 +02:00
|
|
|
if (MANAGER_IS_USER(m) || detect_container() > 0) {
|
2018-10-17 17:27:20 +02:00
|
|
|
log_and_status(m, warn, "Exiting immediately", reason);
|
2018-10-16 16:34:45 +02:00
|
|
|
m->objective = MANAGER_EXIT;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-16 13:28:39 +02:00
|
|
|
|
2018-10-16 16:34:45 +02:00
|
|
|
log_notice("Doing \"poweroff-force\" action instead of an \"exit-force\" emergency action.");
|
|
|
|
_fallthrough_;
|
2018-10-16 13:28:39 +02:00
|
|
|
|
2016-10-20 15:27:37 +02:00
|
|
|
case EMERGENCY_ACTION_POWEROFF_FORCE:
|
2018-10-17 17:27:20 +02:00
|
|
|
log_and_status(m, warn, "Forcibly powering off", reason);
|
2018-10-09 15:42:19 +02:00
|
|
|
m->objective = MANAGER_POWEROFF;
|
2014-08-22 16:59:46 +02:00
|
|
|
break;
|
|
|
|
|
2016-10-20 15:27:37 +02:00
|
|
|
case EMERGENCY_ACTION_POWEROFF_IMMEDIATE:
|
2018-10-17 17:27:20 +02:00
|
|
|
log_and_status(m, warn, "Powering off immediately", reason);
|
2014-08-22 16:59:46 +02:00
|
|
|
|
|
|
|
sync();
|
|
|
|
|
|
|
|
log_info("Powering off.");
|
2018-02-21 17:46:55 +01:00
|
|
|
(void) reboot(RB_POWER_OFF);
|
2014-08-22 16:59:46 +02:00
|
|
|
break;
|
|
|
|
|
2014-08-22 16:36:38 +02:00
|
|
|
default:
|
2016-10-20 15:27:37 +02:00
|
|
|
assert_not_reached("Unknown emergency action");
|
2014-08-22 16:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 15:27:37 +02:00
|
|
|
DEFINE_STRING_TABLE_LOOKUP(emergency_action, EmergencyAction);
|
2018-10-16 13:28:39 +02:00
|
|
|
|
|
|
|
int parse_emergency_action(
|
|
|
|
const char *value,
|
|
|
|
bool system,
|
|
|
|
EmergencyAction *ret) {
|
|
|
|
|
|
|
|
EmergencyAction x;
|
|
|
|
|
|
|
|
x = emergency_action_from_string(value);
|
|
|
|
if (x < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-10-16 16:34:45 +02:00
|
|
|
if (!system && x != EMERGENCY_ACTION_NONE && x < _EMERGENCY_ACTION_FIRST_USER_ACTION)
|
2018-10-16 13:28:39 +02:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
*ret = x;
|
|
|
|
return 0;
|
|
|
|
}
|