2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "bus-error.h"
25 #include "conf-parser.h"
26 #include "formats-util.h"
27 #include "logind-action.h"
28 #include "process-util.h"
29 //#include "sleep-config.h"
30 //#include "special.h"
31 #include "string-table.h"
32 #include "terminal-util.h"
33 #include "user-util.h"
35 // Additional includes needed by elogind
38 #include "sd-messages.h"
42 int manager_handle_action(
44 InhibitWhat inhibit_key,
46 bool ignore_inhibited,
49 static const char * const message_table[_HANDLE_ACTION_MAX] = {
50 [HANDLE_POWEROFF] = "Powering Off...",
51 [HANDLE_REBOOT] = "Rebooting...",
52 [HANDLE_HALT] = "Halting...",
53 [HANDLE_KEXEC] = "Rebooting via kexec...",
54 [HANDLE_SUSPEND] = "Suspending...",
55 [HANDLE_HIBERNATE] = "Hibernating...",
56 [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending..."
59 #if 0 /// elogind does this itself. No target table required
60 static const char * const target_table[_HANDLE_ACTION_MAX] = {
61 [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
62 [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
63 [HANDLE_HALT] = SPECIAL_HALT_TARGET,
64 [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
65 [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
66 [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
67 [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET
71 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
72 InhibitWhat inhibit_operation;
73 Inhibitor *offending = NULL;
79 /* If the key handling is turned off, don't do anything */
80 if (handle == HANDLE_IGNORE) {
81 log_debug("Refusing operation, as it is turned off.");
85 if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
86 /* If the last system suspend or startup is too close,
87 * let's not suspend for now, to give USB docking
88 * stations some time to settle so that we can
89 * properly watch its displays. */
90 if (m->lid_switch_ignore_event_source) {
91 log_debug("Ignoring lid switch request, system startup or resume too close.");
96 /* If the key handling is inhibited, don't do anything */
97 if (inhibit_key > 0) {
98 if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
99 log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
104 /* Locking is handled differently from the rest. */
105 if (handle == HANDLE_LOCK) {
110 log_info("Locking sessions...");
111 session_send_lock_all(m, true);
115 if (handle == HANDLE_SUSPEND)
116 supported = can_sleep(m, "suspend") > 0;
117 else if (handle == HANDLE_HIBERNATE)
118 supported = can_sleep(m, "hibernate") > 0;
119 else if (handle == HANDLE_HYBRID_SLEEP)
120 supported = can_sleep(m, "hybrid-sleep") > 0;
121 else if (handle == HANDLE_KEXEC)
122 supported = access(KEXEC, X_OK) >= 0;
127 log_warning("Requested operation not supported, ignoring.");
131 if (m->action_what) {
132 log_debug("Action already in progress, ignoring.");
136 inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE || handle == HANDLE_HYBRID_SLEEP ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
138 /* If the actual operation is inhibited, warn and fail */
139 if (!ignore_inhibited &&
140 manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
141 _cleanup_free_ char *comm = NULL, *u = NULL;
143 get_process_comm(offending->pid, &comm);
144 u = uid_to_name(offending->uid);
146 /* If this is just a recheck of the lid switch then don't warn about anything */
148 log_debug("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
149 inhibit_what_to_string(inhibit_operation),
150 offending->uid, strna(u),
151 offending->pid, strna(comm));
155 log_error("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
156 inhibit_what_to_string(inhibit_operation),
157 offending->uid, strna(u),
158 offending->pid, strna(comm));
163 log_info("%s", message_table[handle]);
165 #if 0 /// elogind uses its own variant, which can use the handle directly.
166 r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error);
168 r = bus_manager_shutdown_or_sleep_now_or_later(m, handle, inhibit_operation, &error);
171 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
178 static int run_helper(const char *helper) {
181 return log_error_errno(errno, "Failed to fork: %m");
187 close_all_fds(NULL, 0);
189 execlp(helper, helper, NULL);
190 log_error_errno(errno, "Failed to execute %s: %m", helper);
194 return wait_for_terminate_and_warn(helper, pid, true);
197 int shutdown_or_sleep(Manager *m, HandleAction action) {
202 case HANDLE_POWEROFF:
203 return run_helper(HALT);
205 return run_helper(REBOOT);
207 return run_helper(HALT);
209 return run_helper(KEXEC);
211 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
212 case HANDLE_HIBERNATE:
213 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
214 case HANDLE_HYBRID_SLEEP:
215 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
221 static const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
222 [HANDLE_IGNORE] = "ignore",
223 [HANDLE_POWEROFF] = "poweroff",
224 [HANDLE_REBOOT] = "reboot",
225 [HANDLE_HALT] = "halt",
226 [HANDLE_KEXEC] = "kexec",
227 [HANDLE_SUSPEND] = "suspend",
228 [HANDLE_HIBERNATE] = "hibernate",
229 [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
230 [HANDLE_LOCK] = "lock"
233 DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
234 DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");