chiark / gitweb /
core: add support for a configurable system-wide start-up timeout
[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 #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
43         switch (action) {
44
45         case FAILURE_ACTION_NONE:
46                 break;
47
48         case FAILURE_ACTION_REBOOT: {
49                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
50
51                 log_warning("Rebooting as result of failure.");
52
53                 update_reboot_param_file(reboot_arg);
54                 r = manager_add_job_by_name(m, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
55                 if (r < 0)
56                         log_error("Failed to reboot: %s.", bus_error_message(&error, r));
57
58                 break;
59         }
60
61         case FAILURE_ACTION_REBOOT_FORCE:
62                 log_warning("Forcibly rebooting as result of failure.");
63                 update_reboot_param_file(reboot_arg);
64                 m->exit_code = MANAGER_REBOOT;
65                 break;
66
67         case FAILURE_ACTION_REBOOT_IMMEDIATE:
68                 log_warning("Rebooting immediately as result of failure.");
69
70                 sync();
71
72                 if (reboot_arg) {
73                         log_info("Rebooting with argument '%s'.", reboot_arg);
74                         syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reboot_arg);
75                 }
76
77                 log_info("Rebooting.");
78                 reboot(RB_AUTOBOOT);
79                 break;
80
81         default:
82                 assert_not_reached("Unknown failure action");
83         }
84
85         return -ECANCELED;
86 }
87
88 static const char* const failure_action_table[_FAILURE_ACTION_MAX] = {
89         [FAILURE_ACTION_NONE] = "none",
90         [FAILURE_ACTION_REBOOT] = "reboot",
91         [FAILURE_ACTION_REBOOT_FORCE] = "reboot-force",
92         [FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate"
93 };
94 DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction);