]>
Commit | Line | Data |
---|---|---|
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" |
2928b0a8 | 26 | #include "failure-action.h" |
8b43440b LP |
27 | #include "special.h" |
28 | #include "string-table.h" | |
288a74cc | 29 | #include "terminal-util.h" |
2928b0a8 | 30 | |
ebc5788e ZJS |
31 | static void log_and_status(Manager *m, const char *message) { |
32 | log_warning("%s", message); | |
33 | manager_status_printf(m, STATUS_TYPE_EMERGENCY, | |
1fc464f6 | 34 | ANSI_HIGHLIGHT_RED " !! " ANSI_NORMAL, |
ebc5788e ZJS |
35 | "%s", message); |
36 | } | |
37 | ||
2928b0a8 LP |
38 | int failure_action( |
39 | Manager *m, | |
40 | FailureAction action, | |
41 | const char *reboot_arg) { | |
42 | ||
2928b0a8 LP |
43 | assert(m); |
44 | assert(action >= 0); | |
45 | assert(action < _FAILURE_ACTION_MAX); | |
46 | ||
f07756bf LP |
47 | if (action == FAILURE_ACTION_NONE) |
48 | return -ECANCELED; | |
2928b0a8 | 49 | |
b2c23da8 | 50 | if (m->running_as == MANAGER_USER) { |
f07756bf LP |
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) { | |
2928b0a8 | 60 | |
53f18416 | 61 | case FAILURE_ACTION_REBOOT: |
ebc5788e | 62 | log_and_status(m, "Rebooting as result of failure."); |
2928b0a8 LP |
63 | |
64 | update_reboot_param_file(reboot_arg); | |
9c0d1c1c JH |
65 | (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_REBOOT_TARGET, |
66 | JOB_REPLACE_IRREVERSIBLY, NULL); | |
2928b0a8 LP |
67 | |
68 | break; | |
2928b0a8 LP |
69 | |
70 | case FAILURE_ACTION_REBOOT_FORCE: | |
ebc5788e ZJS |
71 | log_and_status(m, "Forcibly rebooting as result of failure."); |
72 | ||
2928b0a8 LP |
73 | update_reboot_param_file(reboot_arg); |
74 | m->exit_code = MANAGER_REBOOT; | |
75 | break; | |
76 | ||
77 | case FAILURE_ACTION_REBOOT_IMMEDIATE: | |
ebc5788e | 78 | log_and_status(m, "Rebooting immediately as result of failure."); |
2928b0a8 LP |
79 | |
80 | sync(); | |
81 | ||
82 | if (reboot_arg) { | |
83 | log_info("Rebooting with argument '%s'.", reboot_arg); | |
84 | syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reboot_arg); | |
85 | } | |
86 | ||
87 | log_info("Rebooting."); | |
88 | reboot(RB_AUTOBOOT); | |
89 | break; | |
90 | ||
53f18416 | 91 | case FAILURE_ACTION_POWEROFF: |
ebc5788e | 92 | log_and_status(m, "Powering off as result of failure."); |
9c0d1c1c JH |
93 | (void) manager_add_job_by_name_and_warn(m, JOB_START, SPECIAL_POWEROFF_TARGET, |
94 | JOB_REPLACE_IRREVERSIBLY, NULL); | |
f07756bf | 95 | break; |
f07756bf LP |
96 | |
97 | case FAILURE_ACTION_POWEROFF_FORCE: | |
ebc5788e | 98 | log_and_status(m, "Forcibly powering off as result of failure."); |
f07756bf LP |
99 | m->exit_code = MANAGER_POWEROFF; |
100 | break; | |
101 | ||
102 | case FAILURE_ACTION_POWEROFF_IMMEDIATE: | |
ebc5788e | 103 | log_and_status(m, "Powering off immediately as result of failure."); |
f07756bf LP |
104 | |
105 | sync(); | |
106 | ||
107 | log_info("Powering off."); | |
108 | reboot(RB_POWER_OFF); | |
109 | break; | |
110 | ||
2928b0a8 LP |
111 | default: |
112 | assert_not_reached("Unknown failure action"); | |
113 | } | |
114 | ||
115 | return -ECANCELED; | |
116 | } | |
117 | ||
118 | static const char* const failure_action_table[_FAILURE_ACTION_MAX] = { | |
119 | [FAILURE_ACTION_NONE] = "none", | |
120 | [FAILURE_ACTION_REBOOT] = "reboot", | |
121 | [FAILURE_ACTION_REBOOT_FORCE] = "reboot-force", | |
f07756bf LP |
122 | [FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate", |
123 | [FAILURE_ACTION_POWEROFF] = "poweroff", | |
124 | [FAILURE_ACTION_POWEROFF_FORCE] = "poweroff-force", | |
125 | [FAILURE_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate" | |
2928b0a8 LP |
126 | }; |
127 | DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction); |