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"
15 static void log_and_status(Manager
*m
, bool warn
, const char *message
, const char *reason
) {
16 log_full(warn
? LOG_WARNING
: LOG_DEBUG
, "%s: %s", message
, reason
);
18 manager_status_printf(m
, STATUS_TYPE_EMERGENCY
,
19 ANSI_HIGHLIGHT_RED
" !! " ANSI_NORMAL
,
20 "%s: %s", message
, reason
);
25 EmergencyAction action
,
26 EmergencyActionFlags options
,
27 const char *reboot_arg
,
33 assert(action
< _EMERGENCY_ACTION_MAX
);
35 if (action
== EMERGENCY_ACTION_NONE
)
38 if (FLAGS_SET(options
, EMERGENCY_ACTION_IS_WATCHDOG
) && !m
->service_watchdogs
) {
39 log_warning("Watchdog disabled! Not acting on: %s", reason
);
43 bool warn
= FLAGS_SET(options
, EMERGENCY_ACTION_WARN
);
47 case EMERGENCY_ACTION_REBOOT
:
48 log_and_status(m
, warn
, "Rebooting", reason
);
50 (void) update_reboot_parameter_and_warn(reboot_arg
);
51 (void) manager_add_job_by_name_and_warn(m
, JOB_START
, SPECIAL_REBOOT_TARGET
, JOB_REPLACE_IRREVERSIBLY
, NULL
);
55 case EMERGENCY_ACTION_REBOOT_FORCE
:
56 log_and_status(m
, warn
, "Forcibly rebooting", reason
);
58 (void) update_reboot_parameter_and_warn(reboot_arg
);
59 m
->objective
= MANAGER_REBOOT
;
63 case EMERGENCY_ACTION_REBOOT_IMMEDIATE
:
64 log_and_status(m
, warn
, "Rebooting immediately", reason
);
68 if (!isempty(reboot_arg
)) {
69 log_info("Rebooting with argument '%s'.", reboot_arg
);
70 (void) raw_reboot(LINUX_REBOOT_CMD_RESTART2
, reboot_arg
);
71 log_warning_errno(errno
, "Failed to reboot with parameter, retrying without: %m");
74 log_info("Rebooting.");
75 (void) reboot(RB_AUTOBOOT
);
78 case EMERGENCY_ACTION_EXIT
:
81 m
->return_value
= exit_status
;
83 if (MANAGER_IS_USER(m
) || detect_container() > 0) {
84 log_and_status(m
, warn
, "Exiting", reason
);
85 (void) manager_add_job_by_name_and_warn(m
, JOB_START
, SPECIAL_EXIT_TARGET
, JOB_REPLACE_IRREVERSIBLY
, NULL
);
89 log_notice("Doing \"poweroff\" action instead of an \"exit\" emergency action.");
92 case EMERGENCY_ACTION_POWEROFF
:
93 log_and_status(m
, warn
, "Powering off", reason
);
94 (void) manager_add_job_by_name_and_warn(m
, JOB_START
, SPECIAL_POWEROFF_TARGET
, JOB_REPLACE_IRREVERSIBLY
, NULL
);
97 case EMERGENCY_ACTION_EXIT_FORCE
:
100 m
->return_value
= exit_status
;
102 if (MANAGER_IS_USER(m
) || detect_container() > 0) {
103 log_and_status(m
, warn
, "Exiting immediately", reason
);
104 m
->objective
= MANAGER_EXIT
;
108 log_notice("Doing \"poweroff-force\" action instead of an \"exit-force\" emergency action.");
111 case EMERGENCY_ACTION_POWEROFF_FORCE
:
112 log_and_status(m
, warn
, "Forcibly powering off", reason
);
113 m
->objective
= MANAGER_POWEROFF
;
116 case EMERGENCY_ACTION_POWEROFF_IMMEDIATE
:
117 log_and_status(m
, warn
, "Powering off immediately", reason
);
121 log_info("Powering off.");
122 (void) reboot(RB_POWER_OFF
);
126 assert_not_reached("Unknown emergency action");
132 static const char* const emergency_action_table
[_EMERGENCY_ACTION_MAX
] = {
133 [EMERGENCY_ACTION_NONE
] = "none",
134 [EMERGENCY_ACTION_REBOOT
] = "reboot",
135 [EMERGENCY_ACTION_REBOOT_FORCE
] = "reboot-force",
136 [EMERGENCY_ACTION_REBOOT_IMMEDIATE
] = "reboot-immediate",
137 [EMERGENCY_ACTION_POWEROFF
] = "poweroff",
138 [EMERGENCY_ACTION_POWEROFF_FORCE
] = "poweroff-force",
139 [EMERGENCY_ACTION_POWEROFF_IMMEDIATE
] = "poweroff-immediate",
140 [EMERGENCY_ACTION_EXIT
] = "exit",
141 [EMERGENCY_ACTION_EXIT_FORCE
] = "exit-force",
143 DEFINE_STRING_TABLE_LOOKUP(emergency_action
, EmergencyAction
);
145 int parse_emergency_action(
148 EmergencyAction
*ret
) {
152 x
= emergency_action_from_string(value
);
156 if (!system
&& x
!= EMERGENCY_ACTION_NONE
&& x
< _EMERGENCY_ACTION_FIRST_USER_ACTION
)