1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
7 Copyright 2012 Michael Olbrich
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.
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.
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/>.
23 #include <sys/reboot.h>
24 #include <linux/reboot.h>
26 #include "bus-error.h"
28 #include "failure-action.h"
30 #include "string-table.h"
31 #include "terminal-util.h"
33 static void log_and_status(Manager
*m
, const char *message
) {
34 log_warning("%s", message
);
35 manager_status_printf(m
, STATUS_TYPE_EMERGENCY
,
36 ANSI_HIGHLIGHT_RED
" !! " ANSI_NORMAL
,
43 const char *reboot_arg
) {
47 assert(action
< _FAILURE_ACTION_MAX
);
49 if (action
== FAILURE_ACTION_NONE
)
52 if (m
->running_as
== MANAGER_USER
) {
53 /* Downgrade all options to simply exiting if we run
56 log_warning("Exiting as result of failure.");
57 m
->exit_code
= MANAGER_EXIT
;
63 case FAILURE_ACTION_REBOOT
:
64 log_and_status(m
, "Rebooting as result of failure.");
66 update_reboot_param_file(reboot_arg
);
67 (void) manager_add_job_by_name_and_warn(m
, JOB_START
, SPECIAL_REBOOT_TARGET
, JOB_REPLACE
, NULL
);
71 case FAILURE_ACTION_REBOOT_FORCE
:
72 log_and_status(m
, "Forcibly rebooting as result of failure.");
74 update_reboot_param_file(reboot_arg
);
75 m
->exit_code
= MANAGER_REBOOT
;
78 case FAILURE_ACTION_REBOOT_IMMEDIATE
:
79 log_and_status(m
, "Rebooting immediately as result of failure.");
84 log_info("Rebooting with argument '%s'.", reboot_arg
);
85 syscall(SYS_reboot
, LINUX_REBOOT_MAGIC1
, LINUX_REBOOT_MAGIC2
, LINUX_REBOOT_CMD_RESTART2
, reboot_arg
);
88 log_info("Rebooting.");
92 case FAILURE_ACTION_POWEROFF
:
93 log_and_status(m
, "Powering off as result of failure.");
94 (void) manager_add_job_by_name_and_warn(m
, JOB_START
, SPECIAL_POWEROFF_TARGET
, JOB_REPLACE
, NULL
);
97 case FAILURE_ACTION_POWEROFF_FORCE
:
98 log_and_status(m
, "Forcibly powering off as result of failure.");
99 m
->exit_code
= MANAGER_POWEROFF
;
102 case FAILURE_ACTION_POWEROFF_IMMEDIATE
:
103 log_and_status(m
, "Powering off immediately as result of failure.");
107 log_info("Powering off.");
108 reboot(RB_POWER_OFF
);
112 assert_not_reached("Unknown failure action");
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",
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"
127 DEFINE_STRING_TABLE_LOOKUP(failure_action
, FailureAction
);