]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2928b0a8 LP |
2 | |
3 | #include <sys/reboot.h> | |
2928b0a8 | 4 | |
2928b0a8 | 5 | #include "bus-error.h" |
8b43440b | 6 | #include "bus-util.h" |
87a47f99 | 7 | #include "emergency-action.h" |
c52a937b | 8 | #include "raw-reboot.h" |
e3631d1c | 9 | #include "reboot-util.h" |
8b43440b LP |
10 | #include "special.h" |
11 | #include "string-table.h" | |
288a74cc | 12 | #include "terminal-util.h" |
a400bd8c | 13 | #include "virt.h" |
2928b0a8 | 14 | |
c7adcb1a ZJS |
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); | |
17 | if (warn) | |
18 | manager_status_printf(m, STATUS_TYPE_EMERGENCY, | |
19 | ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL, | |
20 | "%s: %s", message, reason); | |
ebc5788e ZJS |
21 | } |
22 | ||
87a47f99 | 23 | int emergency_action( |
2928b0a8 | 24 | Manager *m, |
87a47f99 | 25 | EmergencyAction action, |
1710d4be | 26 | EmergencyActionFlags options, |
87a47f99 LN |
27 | const char *reboot_arg, |
28 | const char *reason) { | |
2928b0a8 | 29 | |
2928b0a8 LP |
30 | assert(m); |
31 | assert(action >= 0); | |
87a47f99 | 32 | assert(action < _EMERGENCY_ACTION_MAX); |
2928b0a8 | 33 | |
87a47f99 | 34 | if (action == EMERGENCY_ACTION_NONE) |
f07756bf | 35 | return -ECANCELED; |
2928b0a8 | 36 | |
1710d4be | 37 | if (FLAGS_SET(options, EMERGENCY_ACTION_IS_WATCHDOG) && !m->service_watchdogs) { |
2a12e32e JK |
38 | log_warning("Watchdog disabled! Not acting on: %s", reason); |
39 | return -ECANCELED; | |
40 | } | |
41 | ||
c7adcb1a ZJS |
42 | bool warn = FLAGS_SET(options, EMERGENCY_ACTION_WARN); |
43 | ||
f07756bf | 44 | switch (action) { |
2928b0a8 | 45 | |
87a47f99 | 46 | case EMERGENCY_ACTION_REBOOT: |
c7adcb1a | 47 | log_and_status(m, warn, "Rebooting", reason); |
2928b0a8 | 48 | |
27c06cb5 LP |
49 | (void) update_reboot_parameter_and_warn(reboot_arg); |
50 | (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL); | |
2928b0a8 LP |
51 | |
52 | break; | |
2928b0a8 | 53 | |
87a47f99 | 54 | case EMERGENCY_ACTION_REBOOT_FORCE: |
c7adcb1a | 55 | log_and_status(m, warn, "Forcibly rebooting", reason); |
ebc5788e | 56 | |
27c06cb5 | 57 | (void) update_reboot_parameter_and_warn(reboot_arg); |
af41e508 | 58 | m->objective = MANAGER_REBOOT; |
27c06cb5 | 59 | |
2928b0a8 LP |
60 | break; |
61 | ||
87a47f99 | 62 | case EMERGENCY_ACTION_REBOOT_IMMEDIATE: |
c7adcb1a | 63 | log_and_status(m, warn, "Rebooting immediately", reason); |
2928b0a8 LP |
64 | |
65 | sync(); | |
66 | ||
27c06cb5 | 67 | if (!isempty(reboot_arg)) { |
2928b0a8 | 68 | log_info("Rebooting with argument '%s'.", reboot_arg); |
c52a937b | 69 | (void) raw_reboot(LINUX_REBOOT_CMD_RESTART2, reboot_arg); |
27c06cb5 | 70 | log_warning_errno(errno, "Failed to reboot with parameter, retrying without: %m"); |
2928b0a8 LP |
71 | } |
72 | ||
73 | log_info("Rebooting."); | |
118cf952 | 74 | (void) reboot(RB_AUTOBOOT); |
2928b0a8 LP |
75 | break; |
76 | ||
54fcb619 | 77 | case EMERGENCY_ACTION_EXIT: |
a400bd8c | 78 | if (MANAGER_IS_USER(m) || detect_container() > 0) { |
c7adcb1a | 79 | log_and_status(m, warn, "Exiting", reason); |
a400bd8c ZJS |
80 | (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL); |
81 | break; | |
82 | } | |
54fcb619 | 83 | |
a400bd8c ZJS |
84 | log_notice("Doing \"poweroff\" action instead of an \"exit\" emergency action."); |
85 | _fallthrough_; | |
54fcb619 | 86 | |
87a47f99 | 87 | case EMERGENCY_ACTION_POWEROFF: |
c7adcb1a | 88 | log_and_status(m, warn, "Powering off", reason); |
27c06cb5 | 89 | (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL); |
f07756bf | 90 | break; |
f07756bf | 91 | |
54fcb619 | 92 | case EMERGENCY_ACTION_EXIT_FORCE: |
a400bd8c | 93 | if (MANAGER_IS_USER(m) || detect_container() > 0) { |
c7adcb1a | 94 | log_and_status(m, warn, "Exiting immediately", reason); |
a400bd8c ZJS |
95 | m->objective = MANAGER_EXIT; |
96 | break; | |
97 | } | |
54fcb619 | 98 | |
a400bd8c ZJS |
99 | log_notice("Doing \"poweroff-force\" action instead of an \"exit-force\" emergency action."); |
100 | _fallthrough_; | |
54fcb619 | 101 | |
87a47f99 | 102 | case EMERGENCY_ACTION_POWEROFF_FORCE: |
c7adcb1a | 103 | log_and_status(m, warn, "Forcibly powering off", reason); |
af41e508 | 104 | m->objective = MANAGER_POWEROFF; |
f07756bf LP |
105 | break; |
106 | ||
87a47f99 | 107 | case EMERGENCY_ACTION_POWEROFF_IMMEDIATE: |
c7adcb1a | 108 | log_and_status(m, warn, "Powering off immediately", reason); |
f07756bf LP |
109 | |
110 | sync(); | |
111 | ||
112 | log_info("Powering off."); | |
118cf952 | 113 | (void) reboot(RB_POWER_OFF); |
f07756bf LP |
114 | break; |
115 | ||
2928b0a8 | 116 | default: |
87a47f99 | 117 | assert_not_reached("Unknown emergency action"); |
2928b0a8 LP |
118 | } |
119 | ||
120 | return -ECANCELED; | |
121 | } | |
122 | ||
87a47f99 LN |
123 | static const char* const emergency_action_table[_EMERGENCY_ACTION_MAX] = { |
124 | [EMERGENCY_ACTION_NONE] = "none", | |
125 | [EMERGENCY_ACTION_REBOOT] = "reboot", | |
126 | [EMERGENCY_ACTION_REBOOT_FORCE] = "reboot-force", | |
127 | [EMERGENCY_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate", | |
128 | [EMERGENCY_ACTION_POWEROFF] = "poweroff", | |
129 | [EMERGENCY_ACTION_POWEROFF_FORCE] = "poweroff-force", | |
54fcb619 ZJS |
130 | [EMERGENCY_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate", |
131 | [EMERGENCY_ACTION_EXIT] = "exit", | |
132 | [EMERGENCY_ACTION_EXIT_FORCE] = "exit-force", | |
2928b0a8 | 133 | }; |
87a47f99 | 134 | DEFINE_STRING_TABLE_LOOKUP(emergency_action, EmergencyAction); |
54fcb619 ZJS |
135 | |
136 | int parse_emergency_action( | |
137 | const char *value, | |
138 | bool system, | |
139 | EmergencyAction *ret) { | |
140 | ||
141 | EmergencyAction x; | |
142 | ||
143 | x = emergency_action_from_string(value); | |
144 | if (x < 0) | |
145 | return -EINVAL; | |
146 | ||
a400bd8c | 147 | if (!system && x != EMERGENCY_ACTION_NONE && x < _EMERGENCY_ACTION_FIRST_USER_ACTION) |
54fcb619 ZJS |
148 | return -EOPNOTSUPP; |
149 | ||
150 | *ret = x; | |
151 | return 0; | |
152 | } |