]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/failure-action.c
Merge pull request #2594 from keszybz/spelling
[thirdparty/systemd.git] / src / core / failure-action.c
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>
23
24 #include "bus-error.h"
25 #include "bus-util.h"
26 #include "failure-action.h"
27 #include "special.h"
28 #include "string-table.h"
29 #include "terminal-util.h"
30
31 static void log_and_status(Manager *m, const char *message) {
32 log_warning("%s", message);
33 manager_status_printf(m, STATUS_TYPE_EMERGENCY,
34 ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL,
35 "%s", message);
36 }
37
38 int failure_action(
39 Manager *m,
40 FailureAction action,
41 const char *reboot_arg) {
42
43 assert(m);
44 assert(action >= 0);
45 assert(action < _FAILURE_ACTION_MAX);
46
47 if (action == FAILURE_ACTION_NONE)
48 return -ECANCELED;
49
50 if (m->running_as == MANAGER_USER) {
51 /* Downgrade all options to simply exiting if we run
52 * in user mode */
53
54 log_warning("Exiting as result of failure.");
55 m->exit_code = MANAGER_EXIT;
56 return -ECANCELED;
57 }
58
59 switch (action) {
60
61 case FAILURE_ACTION_REBOOT:
62 log_and_status(m, "Rebooting as result of failure.");
63
64 update_reboot_param_file(reboot_arg);
65 (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, NULL);
66
67 break;
68
69 case FAILURE_ACTION_REBOOT_FORCE:
70 log_and_status(m, "Forcibly rebooting as result of failure.");
71
72 update_reboot_param_file(reboot_arg);
73 m->exit_code = MANAGER_REBOOT;
74 break;
75
76 case FAILURE_ACTION_REBOOT_IMMEDIATE:
77 log_and_status(m, "Rebooting immediately as result of failure.");
78
79 sync();
80
81 if (reboot_arg) {
82 log_info("Rebooting with argument '%s'.", reboot_arg);
83 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reboot_arg);
84 }
85
86 log_info("Rebooting.");
87 reboot(RB_AUTOBOOT);
88 break;
89
90 case FAILURE_ACTION_POWEROFF:
91 log_and_status(m, "Powering off as result of failure.");
92 (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE, NULL);
93 break;
94
95 case FAILURE_ACTION_POWEROFF_FORCE:
96 log_and_status(m, "Forcibly powering off as result of failure.");
97 m->exit_code = MANAGER_POWEROFF;
98 break;
99
100 case FAILURE_ACTION_POWEROFF_IMMEDIATE:
101 log_and_status(m, "Powering off immediately as result of failure.");
102
103 sync();
104
105 log_info("Powering off.");
106 reboot(RB_POWER_OFF);
107 break;
108
109 default:
110 assert_not_reached("Unknown failure action");
111 }
112
113 return -ECANCELED;
114 }
115
116 static const char* const failure_action_table[_FAILURE_ACTION_MAX] = {
117 [FAILURE_ACTION_NONE] = "none",
118 [FAILURE_ACTION_REBOOT] = "reboot",
119 [FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
120 [FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate",
121 [FAILURE_ACTION_POWEROFF] = "poweroff",
122 [FAILURE_ACTION_POWEROFF_FORCE] = "poweroff-force",
123 [FAILURE_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate"
124 };
125 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);