]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/emergency-action.c
basic: split out update_reboot_parameter_and_warn() into its own .c/.h files
[thirdparty/systemd.git] / src / core / emergency-action.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Lennart Poettering
6 Copyright 2012 Michael Olbrich
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/reboot.h>
23
24 #include "bus-error.h"
25 #include "bus-util.h"
26 #include "emergency-action.h"
27 #include "raw-reboot.h"
28 #include "reboot-util.h"
29 #include "special.h"
30 #include "string-table.h"
31 #include "terminal-util.h"
32
33 static void log_and_status(Manager *m, const char *message, const char *reason) {
34 log_warning("%s: %s", message, reason);
35 manager_status_printf(m, STATUS_TYPE_EMERGENCY,
36 ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL,
37 "%s: %s", message, reason);
38 }
39
40 int emergency_action(
41 Manager *m,
42 EmergencyAction action,
43 const char *reboot_arg,
44 const char *reason) {
45
46 assert(m);
47 assert(action >= 0);
48 assert(action < _EMERGENCY_ACTION_MAX);
49
50 if (action == EMERGENCY_ACTION_NONE)
51 return -ECANCELED;
52
53 if (!m->service_watchdogs) {
54 log_warning("Watchdog disabled! Not acting on: %s", reason);
55 return -ECANCELED;
56 }
57
58 if (!MANAGER_IS_SYSTEM(m)) {
59 /* Downgrade all options to simply exiting if we run
60 * in user mode */
61
62 log_warning("Exiting: %s", reason);
63 m->exit_code = MANAGER_EXIT;
64 return -ECANCELED;
65 }
66
67 switch (action) {
68
69 case EMERGENCY_ACTION_REBOOT:
70 log_and_status(m, "Rebooting", reason);
71
72 (void) update_reboot_parameter_and_warn(reboot_arg);
73 (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL);
74
75 break;
76
77 case EMERGENCY_ACTION_REBOOT_FORCE:
78 log_and_status(m, "Forcibly rebooting", reason);
79
80 (void) update_reboot_parameter_and_warn(reboot_arg);
81 m->exit_code = MANAGER_REBOOT;
82
83 break;
84
85 case EMERGENCY_ACTION_REBOOT_IMMEDIATE:
86 log_and_status(m, "Rebooting immediately", reason);
87
88 sync();
89
90 if (!isempty(reboot_arg)) {
91 log_info("Rebooting with argument '%s'.", reboot_arg);
92 (void) raw_reboot(LINUX_REBOOT_CMD_RESTART2, reboot_arg);
93 log_warning_errno(errno, "Failed to reboot with parameter, retrying without: %m");
94 }
95
96 log_info("Rebooting.");
97 (void) reboot(RB_AUTOBOOT);
98 break;
99
100 case EMERGENCY_ACTION_POWEROFF:
101 log_and_status(m, "Powering off", reason);
102 (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY, NULL);
103 break;
104
105 case EMERGENCY_ACTION_POWEROFF_FORCE:
106 log_and_status(m, "Forcibly powering off", reason);
107 m->exit_code = MANAGER_POWEROFF;
108 break;
109
110 case EMERGENCY_ACTION_POWEROFF_IMMEDIATE:
111 log_and_status(m, "Powering off immediately", reason);
112
113 sync();
114
115 log_info("Powering off.");
116 (void) reboot(RB_POWER_OFF);
117 break;
118
119 default:
120 assert_not_reached("Unknown emergency action");
121 }
122
123 return -ECANCELED;
124 }
125
126 static const char* const emergency_action_table[_EMERGENCY_ACTION_MAX] = {
127 [EMERGENCY_ACTION_NONE] = "none",
128 [EMERGENCY_ACTION_REBOOT] = "reboot",
129 [EMERGENCY_ACTION_REBOOT_FORCE] = "reboot-force",
130 [EMERGENCY_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate",
131 [EMERGENCY_ACTION_POWEROFF] = "poweroff",
132 [EMERGENCY_ACTION_POWEROFF_FORCE] = "poweroff-force",
133 [EMERGENCY_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate"
134 };
135 DEFINE_STRING_TABLE_LOOKUP(emergency_action, EmergencyAction);