1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <sys/reboot.h>
7 #include "emergency-action.h"
8 #include "raw-reboot.h"
9 #include "reboot-util.h"
11 #include "string-table.h"
12 #include "terminal-util.h"
14 static void log_and_status(Manager
*m
, const char *message
, const char *reason
) {
15 log_warning("%s: %s", message
, reason
);
16 manager_status_printf(m
, STATUS_TYPE_EMERGENCY
,
17 ANSI_HIGHLIGHT_RED
" !! " ANSI_NORMAL
,
18 "%s: %s", message
, reason
);
23 EmergencyAction action
,
24 const char *reboot_arg
,
29 assert(action
< _EMERGENCY_ACTION_MAX
);
31 if (action
== EMERGENCY_ACTION_NONE
)
34 if (!m
->service_watchdogs
) {
35 log_warning("Watchdog disabled! Not acting on: %s", reason
);
39 if (!MANAGER_IS_SYSTEM(m
)) {
40 /* Downgrade all options to simply exiting if we run
43 log_warning("Exiting: %s", reason
);
44 m
->objective
= MANAGER_EXIT
;
50 case EMERGENCY_ACTION_REBOOT
:
51 log_and_status(m
, "Rebooting", reason
);
53 (void) update_reboot_parameter_and_warn(reboot_arg
);
54 (void) manager_add_job_by_name_and_warn(m
, JOB_START
, SPECIAL_REBOOT_TARGET
, JOB_REPLACE_IRREVERSIBLY
, NULL
);
58 case EMERGENCY_ACTION_REBOOT_FORCE
:
59 log_and_status(m
, "Forcibly rebooting", reason
);
61 (void) update_reboot_parameter_and_warn(reboot_arg
);
62 m
->objective
= MANAGER_REBOOT
;
66 case EMERGENCY_ACTION_REBOOT_IMMEDIATE
:
67 log_and_status(m
, "Rebooting immediately", reason
);
71 if (!isempty(reboot_arg
)) {
72 log_info("Rebooting with argument '%s'.", reboot_arg
);
73 (void) raw_reboot(LINUX_REBOOT_CMD_RESTART2
, reboot_arg
);
74 log_warning_errno(errno
, "Failed to reboot with parameter, retrying without: %m");
77 log_info("Rebooting.");
78 (void) reboot(RB_AUTOBOOT
);
81 case EMERGENCY_ACTION_POWEROFF
:
82 log_and_status(m
, "Powering off", reason
);
83 (void) manager_add_job_by_name_and_warn(m
, JOB_START
, SPECIAL_POWEROFF_TARGET
, JOB_REPLACE_IRREVERSIBLY
, NULL
);
86 case EMERGENCY_ACTION_POWEROFF_FORCE
:
87 log_and_status(m
, "Forcibly powering off", reason
);
88 m
->objective
= MANAGER_POWEROFF
;
91 case EMERGENCY_ACTION_POWEROFF_IMMEDIATE
:
92 log_and_status(m
, "Powering off immediately", reason
);
96 log_info("Powering off.");
97 (void) reboot(RB_POWER_OFF
);
101 assert_not_reached("Unknown emergency action");
107 static const char* const emergency_action_table
[_EMERGENCY_ACTION_MAX
] = {
108 [EMERGENCY_ACTION_NONE
] = "none",
109 [EMERGENCY_ACTION_REBOOT
] = "reboot",
110 [EMERGENCY_ACTION_REBOOT_FORCE
] = "reboot-force",
111 [EMERGENCY_ACTION_REBOOT_IMMEDIATE
] = "reboot-immediate",
112 [EMERGENCY_ACTION_POWEROFF
] = "poweroff",
113 [EMERGENCY_ACTION_POWEROFF_FORCE
] = "poweroff-force",
114 [EMERGENCY_ACTION_POWEROFF_IMMEDIATE
] = "poweroff-immediate"
116 DEFINE_STRING_TABLE_LOOKUP(emergency_action
, EmergencyAction
);