2 This file is part of elogind.
4 Copyright 2017 Sven Eden
6 elogind 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 elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
21 #include "bus-common-errors.h"
22 #include "bus-error.h"
24 #include "elogind-dbus.h"
26 #include "process-util.h"
27 #include "sd-messages.h"
29 #include "sleep-config.h"
30 #include "string-util.h"
32 #include "update-utmp.h"
33 #include "user-util.h"
36 static int bus_manager_log_shutdown(
39 HandleAction action) {
45 if (w != INHIBIT_SHUTDOWN)
50 p = "MESSAGE=System is powering down.";
51 q = "SHUTDOWN=power-off";
54 p = "MESSAGE=System is halting.";
58 p = "MESSAGE=System is rebooting.";
59 q = "SHUTDOWN=reboot";
62 p = "MESSAGE=System is rebooting with kexec.";
66 p = "MESSAGE=System is shutting down.";
70 if (isempty(m->wall_message))
73 p = strjoina(p, " (", m->wall_message, ").");
75 return log_struct(LOG_NOTICE,
76 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
82 /* elogind specific helper to make HALT and REBOOT possible. */
83 static int run_helper(const char *helper) {
86 return log_error_errno(errno, "Failed to fork: %m");
92 close_all_fds(NULL, 0);
94 execlp(helper, helper, NULL);
95 log_error_errno(errno, "Failed to execute %s: %m", helper);
99 return wait_for_terminate_and_warn(helper, pid, true);
102 /* elogind specific executor */
103 static int shutdown_or_sleep(Manager *m, HandleAction action) {
108 case HANDLE_POWEROFF:
109 return run_helper(HALT);
111 return run_helper(REBOOT);
113 return run_helper(HALT);
115 return run_helper(KEXEC);
117 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
118 case HANDLE_HIBERNATE:
119 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
120 case HANDLE_HYBRID_SLEEP:
121 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
127 static int execute_shutdown_or_sleep(
131 sd_bus_error *error) {
133 char** argv_utmp = NULL;
138 assert(w < _INHIBIT_WHAT_MAX);
140 bus_manager_log_shutdown(m, w, action);
142 if (IN_SET(action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
144 /* As we have no systemd update-utmp daemon running, we have to
145 * set the relevant utmp/wtmp entries ourselves.
148 if (strv_extend(&argv_utmp, "elogind") < 0)
151 if (HANDLE_REBOOT == action) {
152 if (strv_extend(&argv_utmp, "reboot") < 0)
155 if (strv_extend(&argv_utmp, "shutdown") < 0)
159 /* This comes from our patched update-utmp/update-utmp.c */
160 update_utmp(2, argv_utmp, m->bus);
161 strv_free(argv_utmp);
164 /* Now perform the requested action */
165 r = shutdown_or_sleep(m, action);
167 /* no more pending actions, whether this failed or not */
168 m->pending_action = HANDLE_IGNORE;
170 /* As elogind can not rely on a systemd manager to call all
171 * sleeping processes to wake up, we have to tell them all
173 if (w == INHIBIT_SLEEP) {
174 send_prepare_for(m, w, false);
182 /* Make sure the lid switch is ignored for a while */
183 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
188 int manager_dispatch_delayed(Manager *manager, bool timeout) {
190 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
191 Inhibitor *offending = NULL;
196 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
199 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
200 _cleanup_free_ char *comm = NULL, *u = NULL;
205 (void) get_process_comm(offending->pid, &comm);
206 u = uid_to_name(offending->uid);
208 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
209 offending->uid, strna(u),
210 offending->pid, strna(comm));
213 /* Actually do the operation */
214 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
216 log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
218 manager->pending_action = HANDLE_IGNORE;
219 manager->action_what = 0;
220 /* It is not a critical error for elogind if suspending fails */
226 static int delay_shutdown_or_sleep(
229 HandleAction action) {
236 assert(w < _INHIBIT_WHAT_MAX);
238 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
240 if (m->inhibit_timeout_source) {
241 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
243 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
245 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
247 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
249 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
250 timeout_val, 0, manager_inhibit_timeout_handler, m);
255 m->pending_action = action;
261 int bus_manager_shutdown_or_sleep_now_or_later(
265 sd_bus_error *error) {
272 assert(w <= _INHIBIT_WHAT_MAX);
274 /* Tell everybody to prepare for shutdown/sleep */
275 send_prepare_for(m, w, true);
278 m->inhibit_delay_max > 0 &&
279 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
281 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
282 handle_action_to_string(action),
283 delayed ? "" : "NOT ");
286 /* Shutdown is delayed, keep in mind what we
287 * want to do, and start a timeout */
288 r = delay_shutdown_or_sleep(m, w, action);
290 /* Shutdown is not delayed, execute it
292 r = execute_shutdown_or_sleep(m, w, action, error);
297 static int method_do_shutdown_or_sleep(
299 sd_bus_message *message,
300 HandleAction sleep_action,
303 const char *action_multiple_sessions,
304 const char *action_ignore_inhibit,
305 const char *sleep_verb,
306 sd_bus_error *error) {
313 assert(w <= _INHIBIT_WHAT_MAX);
315 r = sd_bus_message_read(message, "b", &interactive);
319 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
320 __FUNCTION__, action, sleep_verb,
321 interactive ? "" : "NOT ");
323 /* Don't allow multiple jobs being executed at the same time */
325 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
328 r = can_sleep(m, sleep_verb);
333 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
336 if (IN_SET(sleep_action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
337 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
338 action_ignore_inhibit, error);
339 log_debug_elogind("verify_shutdown_creds() returned %d", r);
344 r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
348 return sd_bus_reply_method_return(message, NULL);
351 int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
352 Manager *m = userdata;
354 log_debug_elogind("%s called", __FUNCTION__);
356 return method_do_shutdown_or_sleep(
360 "org.freedesktop.login1.power-off",
361 "org.freedesktop.login1.power-off-multiple-sessions",
362 "org.freedesktop.login1.power-off-ignore-inhibit",
367 int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
368 Manager *m = userdata;
370 log_debug_elogind("%s called", __FUNCTION__);
372 return method_do_shutdown_or_sleep(
376 "org.freedesktop.login1.reboot",
377 "org.freedesktop.login1.reboot-multiple-sessions",
378 "org.freedesktop.login1.reboot-ignore-inhibit",
383 int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
384 Manager *m = userdata;
386 log_debug_elogind("%s called", __FUNCTION__);
388 return method_do_shutdown_or_sleep(
392 "org.freedesktop.login1.suspend",
393 "org.freedesktop.login1.suspend-multiple-sessions",
394 "org.freedesktop.login1.suspend-ignore-inhibit",
399 int manager_scheduled_shutdown_handler(
404 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
405 Manager *m = userdata;
411 if (isempty(m->scheduled_shutdown_type))
414 if (streq(m->scheduled_shutdown_type, "halt"))
415 action = HANDLE_HALT;
416 else if (streq(m->scheduled_shutdown_type, "poweroff"))
417 action = HANDLE_POWEROFF;
419 action = HANDLE_REBOOT;
421 r = execute_shutdown_or_sleep(m, 0, action, &error);
423 return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type);
428 int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
429 Manager *m = userdata;
431 log_debug_elogind("%s called", __FUNCTION__);
433 return method_do_shutdown_or_sleep(
437 "org.freedesktop.login1.hibernate",
438 "org.freedesktop.login1.hibernate-multiple-sessions",
439 "org.freedesktop.login1.hibernate-ignore-inhibit",
444 int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
445 Manager *m = userdata;
447 log_debug_elogind("%s called", __FUNCTION__);
449 return method_do_shutdown_or_sleep(
453 "org.freedesktop.login1.hibernate",
454 "org.freedesktop.login1.hibernate-multiple-sessions",
455 "org.freedesktop.login1.hibernate-ignore-inhibit",