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