chiark / gitweb /
Allow up to 4096 simultaneous connections
[elogind.git] / src / core / failure-action.c
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
26 #include "bus-util.h"
27 #include "bus-error.h"
28 #include "special.h"
29 #include "failure-action.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_ON " !!  " ANSI_HIGHLIGHT_OFF,
35                               "%s", message);
36 }
37
38 int failure_action(
39                 Manager *m,
40                 FailureAction action,
41                 const char *reboot_arg) {
42
43         int r;
44
45         assert(m);
46         assert(action >= 0);
47         assert(action < _FAILURE_ACTION_MAX);
48
49         if (action == FAILURE_ACTION_NONE)
50                 return -ECANCELED;
51
52         if (m->running_as == SYSTEMD_USER) {
53                 /* Downgrade all options to simply exiting if we run
54                  * in user mode */
55
56                 log_warning("Exiting as result of failure.");
57                 m->exit_code = MANAGER_EXIT;
58                 return -ECANCELED;
59         }
60
61         switch (action) {
62
63         case FAILURE_ACTION_REBOOT: {
64                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
65
66                 log_and_status(m, "Rebooting as result of failure.");
67
68                 update_reboot_param_file(reboot_arg);
69                 r = manager_add_job_by_name(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
70                 if (r < 0)
71                         log_error("Failed to reboot: %s.", bus_error_message(&error, r));
72
73                 break;
74         }
75
76         case FAILURE_ACTION_REBOOT_FORCE:
77                 log_and_status(m, "Forcibly rebooting as result of failure.");
78
79                 update_reboot_param_file(reboot_arg);
80                 m->exit_code = MANAGER_REBOOT;
81                 break;
82
83         case FAILURE_ACTION_REBOOT_IMMEDIATE:
84                 log_and_status(m, "Rebooting immediately as result of failure.");
85
86                 sync();
87
88                 if (reboot_arg) {
89                         log_info("Rebooting with argument '%s'.", reboot_arg);
90                         syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reboot_arg);
91                 }
92
93                 log_info("Rebooting.");
94                 reboot(RB_AUTOBOOT);
95                 break;
96
97         case FAILURE_ACTION_POWEROFF: {
98                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
99
100                 log_and_status(m, "Powering off as result of failure.");
101
102                 r = manager_add_job_by_name(m, JOB_START, SPECIAL_POWEROFF_TARGET, JOB_REPLACE, true, &error, NULL);
103                 if (r < 0)
104                         log_error("Failed to poweroff: %s.", bus_error_message(&error, r));
105
106                 break;
107         }
108
109         case FAILURE_ACTION_POWEROFF_FORCE:
110                 log_and_status(m, "Forcibly powering off as result of failure.");
111                 m->exit_code = MANAGER_POWEROFF;
112                 break;
113
114         case FAILURE_ACTION_POWEROFF_IMMEDIATE:
115                 log_and_status(m, "Powering off immediately as result of failure.");
116
117                 sync();
118
119                 log_info("Powering off.");
120                 reboot(RB_POWER_OFF);
121                 break;
122
123         default:
124                 assert_not_reached("Unknown failure action");
125         }
126
127         return -ECANCELED;
128 }
129
130 static const char* const failure_action_table[_FAILURE_ACTION_MAX] = {
131         [FAILURE_ACTION_NONE] = "none",
132         [FAILURE_ACTION_REBOOT] = "reboot",
133         [FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
134         [FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate",
135         [FAILURE_ACTION_POWEROFF] = "poweroff",
136         [FAILURE_ACTION_POWEROFF_FORCE] = "poweroff-force",
137         [FAILURE_ACTION_POWEROFF_IMMEDIATE] = "poweroff-immediate"
138 };
139 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);