]>
Commit | Line | Data |
---|---|---|
2928b0a8 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 Lennart Poettering | |
7 | Copyright 2012 Michael Olbrich | |
8 | ||
9 | systemd is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU Lesser General Public License as published by | |
11 | the Free Software Foundation; either version 2.1 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | systemd is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | Lesser General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU Lesser General Public License | |
20 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
21 | ***/ | |
22 | ||
23 | #include <sys/reboot.h> | |
24 | #include <linux/reboot.h> | |
25 | #include <sys/syscall.h> | |
26 | ||
27 | #include "bus-util.h" | |
28 | #include "bus-error.h" | |
29 | #include "special.h" | |
30 | #include "failure-action.h" | |
31 | ||
32 | int failure_action( | |
33 | Manager *m, | |
34 | FailureAction action, | |
35 | const char *reboot_arg) { | |
36 | ||
37 | int r; | |
38 | ||
39 | assert(m); | |
40 | assert(action >= 0); | |
41 | assert(action < _FAILURE_ACTION_MAX); | |
42 | ||
f07756bf LP |
43 | if (action == FAILURE_ACTION_NONE) |
44 | return -ECANCELED; | |
2928b0a8 | 45 | |
f07756bf LP |
46 | if (m->running_as == SYSTEMD_USER) { |
47 | /* Downgrade all options to simply exiting if we run | |
48 | * in user mode */ | |
49 | ||
50 | log_warning("Exiting as result of failure."); | |
51 | m->exit_code = MANAGER_EXIT; | |
52 | return -ECANCELED; | |
53 | } | |
54 | ||
55 | switch (action) { | |
2928b0a8 LP |
56 | |
57 | case FAILURE_ACTION_REBOOT: { | |
58 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; | |
59 | ||
60 | log_warning("Rebooting as result of failure."); | |
61 | ||
62 | update_reboot_param_file(reboot_arg); | |
63 | r = manager_add_job_by_name(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL); | |
64 | if (r < 0) | |
65 | log_error("Failed to reboot: %s.", bus_error_message(&error, r)); | |
66 | ||
67 | break; | |
68 | } | |
69 | ||
70 | case FAILURE_ACTION_REBOOT_FORCE: | |
71 | log_warning("Forcibly rebooting as result of failure."); | |
72 | update_reboot_param_file(reboot_arg); | |
73 | m->exit_code = MANAGER_REBOOT; | |
74 | break; | |
75 | ||
76 | case FAILURE_ACTION_REBOOT_IMMEDIATE: | |
77 | log_warning("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 | ||
f07756bf LP |
90 | case FAILURE_ACTION_POWEROFF: { |
91 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; | |
92 | ||
93 | log_warning("Powering off as result of failure."); | |
94 | ||
95 | r = manager_add_job_by_name(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE, true, &error, NULL); | |
96 | if (r < 0) | |
97 | log_error("Failed to poweroff: %s.", bus_error_message(&error, r)); | |
98 | ||
99 | break; | |
100 | } | |
101 | ||
102 | case FAILURE_ACTION_POWEROFF_FORCE: | |
103 | log_warning("Forcibly powering off as result of failure."); | |
104 | m->exit_code = MANAGER_POWEROFF; | |
105 | break; | |
106 | ||
107 | case FAILURE_ACTION_POWEROFF_IMMEDIATE: | |
108 | log_warning("Powering off immediately as result of failure."); | |
109 | ||
110 | sync(); | |
111 | ||
112 | log_info("Powering off."); | |
113 | reboot(RB_POWER_OFF); | |
114 | break; | |
115 | ||
2928b0a8 LP |
116 | default: |
117 | assert_not_reached("Unknown failure action"); | |
118 | } | |
119 | ||
120 | return -ECANCELED; | |
121 | } | |
122 | ||
123 | static const char* const failure_action_table[_FAILURE_ACTION_MAX] = { | |
124 | [FAILURE_ACTION_NONE] = "none", | |
125 | [FAILURE_ACTION_REBOOT] = "reboot", | |
126 | [FAILURE_ACTION_REBOOT_FORCE] = "reboot-force", | |
f07756bf LP |
127 | [FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate", |
128 | [FAILURE_ACTION_POWEROFF] = "poweroff", | |
129 | [FAILURE_ACTION_POWEROFF_FORCE] = "poweroff-force", | |
130 | [FAILURE_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate" | |
2928b0a8 LP |
131 | }; |
132 | DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction); |