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 int have_multiple_sessions(
45 /* Check for other users' sessions. Greeter sessions do not
46 * count, and non-login sessions do not count either. */
47 HASHMAP_FOREACH(session, m->sessions, i)
48 if (session->class == SESSION_USER &&
49 session->user->uid != uid)
55 static int bus_manager_log_shutdown(
58 HandleAction action) {
64 if (w != INHIBIT_SHUTDOWN)
69 p = "MESSAGE=System is powering down.";
70 q = "SHUTDOWN=power-off";
73 p = "MESSAGE=System is halting.";
77 p = "MESSAGE=System is rebooting.";
78 q = "SHUTDOWN=reboot";
81 p = "MESSAGE=System is rebooting with kexec.";
85 p = "MESSAGE=System is shutting down.";
89 if (isempty(m->wall_message))
92 p = strjoina(p, " (", m->wall_message, ").");
94 return log_struct(LOG_NOTICE,
95 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
101 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
103 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
104 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
105 [INHIBIT_SLEEP] = "PrepareForSleep"
108 int active = _active;
112 assert(w < _INHIBIT_WHAT_MAX);
113 assert(signal_name[w]);
115 return sd_bus_emit_signal(m->bus,
116 "/org/freedesktop/login1",
117 "org.freedesktop.login1.Manager",
123 /* elogind specific helper to make HALT and REBOOT possible. */
124 static int run_helper(const char *helper) {
127 return log_error_errno(errno, "Failed to fork: %m");
133 close_all_fds(NULL, 0);
135 execlp(helper, helper, NULL);
136 log_error_errno(errno, "Failed to execute %s: %m", helper);
140 return wait_for_terminate_and_warn(helper, pid, true);
143 /* elogind specific executor */
144 static int shutdown_or_sleep(Manager *m, HandleAction action) {
149 case HANDLE_POWEROFF:
150 return run_helper(HALT);
152 return run_helper(REBOOT);
154 return run_helper(HALT);
156 return run_helper(KEXEC);
158 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
159 case HANDLE_HIBERNATE:
160 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
161 case HANDLE_HYBRID_SLEEP:
162 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
168 static int execute_shutdown_or_sleep(
172 sd_bus_error *error) {
174 char** argv_utmp = NULL;
179 assert(w < _INHIBIT_WHAT_MAX);
181 bus_manager_log_shutdown(m, w, action);
183 if (IN_SET(action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
185 /* As we have no systemd update-utmp daemon running, we have to
186 * set the relevant utmp/wtmp entries ourselves.
189 if (strv_extend(&argv_utmp, "elogind") < 0)
192 if (HANDLE_REBOOT == action) {
193 if (strv_extend(&argv_utmp, "reboot") < 0)
196 if (strv_extend(&argv_utmp, "shutdown") < 0)
200 /* This comes from our patched update-utmp/update-utmp.c */
201 update_utmp(2, argv_utmp, m->bus);
202 strv_free(argv_utmp);
205 /* Now perform the requested action */
206 r = shutdown_or_sleep(m, action);
208 /* no more pending actions, whether this failed or not */
209 m->pending_action = HANDLE_IGNORE;
211 /* As elogind can not rely on a systemd manager to call all
212 * sleeping processes to wake up, we have to tell them all
214 if (w == INHIBIT_SLEEP) {
215 send_prepare_for(m, w, false);
223 /* Make sure the lid switch is ignored for a while */
224 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
229 int manager_dispatch_delayed(Manager *manager, bool timeout) {
231 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
232 Inhibitor *offending = NULL;
237 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
240 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
241 _cleanup_free_ char *comm = NULL, *u = NULL;
246 (void) get_process_comm(offending->pid, &comm);
247 u = uid_to_name(offending->uid);
249 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
250 offending->uid, strna(u),
251 offending->pid, strna(comm));
254 /* Actually do the operation */
255 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
257 log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
259 manager->pending_action = HANDLE_IGNORE;
260 manager->action_what = 0;
261 /* It is not a critical error for elogind if suspending fails */
267 static int manager_inhibit_timeout_handler(
272 Manager *manager = userdata;
276 assert(manager->inhibit_timeout_source == s);
278 r = manager_dispatch_delayed(manager, true);
279 return (r < 0) ? r : 0;
282 static int delay_shutdown_or_sleep(
285 HandleAction action) {
292 assert(w < _INHIBIT_WHAT_MAX);
294 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
296 if (m->inhibit_timeout_source) {
297 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
299 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
301 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
303 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
305 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
306 timeout_val, 0, manager_inhibit_timeout_handler, m);
311 m->pending_action = action;
317 int bus_manager_shutdown_or_sleep_now_or_later(
321 sd_bus_error *error) {
328 assert(w <= _INHIBIT_WHAT_MAX);
330 /* Tell everybody to prepare for shutdown/sleep */
331 send_prepare_for(m, w, true);
334 m->inhibit_delay_max > 0 &&
335 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
337 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
338 handle_action_to_string(action),
339 delayed ? "" : "NOT ");
342 /* Shutdown is delayed, keep in mind what we
343 * want to do, and start a timeout */
344 r = delay_shutdown_or_sleep(m, w, action);
346 /* Shutdown is not delayed, execute it
348 r = execute_shutdown_or_sleep(m, w, action, error);
353 int verify_shutdown_creds(
355 sd_bus_message *message,
359 const char *action_multiple_sessions,
360 const char *action_ignore_inhibit,
361 sd_bus_error *error) {
363 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
364 bool multiple_sessions, blocked;
371 assert(w <= _INHIBIT_WHAT_MAX);
373 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
377 r = sd_bus_creds_get_euid(creds, &uid);
381 r = have_multiple_sessions(m, uid);
385 multiple_sessions = r > 0;
386 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
388 if (multiple_sessions && action_multiple_sessions) {
389 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
393 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
396 if (blocked && action_ignore_inhibit) {
397 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
401 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
404 if (!multiple_sessions && !blocked && action) {
405 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
409 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
415 static int method_do_shutdown_or_sleep(
417 sd_bus_message *message,
418 HandleAction sleep_action,
421 const char *action_multiple_sessions,
422 const char *action_ignore_inhibit,
423 const char *sleep_verb,
424 sd_bus_error *error) {
431 assert(w <= _INHIBIT_WHAT_MAX);
433 r = sd_bus_message_read(message, "b", &interactive);
437 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
438 __FUNCTION__, action, sleep_verb,
439 interactive ? "" : "NOT ");
441 /* Don't allow multiple jobs being executed at the same time */
443 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
446 r = can_sleep(m, sleep_verb);
451 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
454 if (IN_SET(sleep_action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
455 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
456 action_ignore_inhibit, error);
457 log_debug_elogind("verify_shutdown_creds() returned %d", r);
462 r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
466 return sd_bus_reply_method_return(message, NULL);
469 int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
470 Manager *m = userdata;
472 log_debug_elogind("%s called", __FUNCTION__);
474 return method_do_shutdown_or_sleep(
478 "org.freedesktop.login1.power-off",
479 "org.freedesktop.login1.power-off-multiple-sessions",
480 "org.freedesktop.login1.power-off-ignore-inhibit",
485 int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
486 Manager *m = userdata;
488 log_debug_elogind("%s called", __FUNCTION__);
490 return method_do_shutdown_or_sleep(
494 "org.freedesktop.login1.reboot",
495 "org.freedesktop.login1.reboot-multiple-sessions",
496 "org.freedesktop.login1.reboot-ignore-inhibit",
501 int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
502 Manager *m = userdata;
504 log_debug_elogind("%s called", __FUNCTION__);
506 return method_do_shutdown_or_sleep(
510 "org.freedesktop.login1.suspend",
511 "org.freedesktop.login1.suspend-multiple-sessions",
512 "org.freedesktop.login1.suspend-ignore-inhibit",
517 int manager_scheduled_shutdown_handler(
522 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
523 Manager *m = userdata;
529 if (isempty(m->scheduled_shutdown_type))
532 if (streq(m->scheduled_shutdown_type, "halt"))
533 action = HANDLE_HALT;
534 else if (streq(m->scheduled_shutdown_type, "poweroff"))
535 action = HANDLE_POWEROFF;
537 action = HANDLE_REBOOT;
539 r = execute_shutdown_or_sleep(m, 0, action, &error);
541 return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type);
546 int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
547 Manager *m = userdata;
549 log_debug_elogind("%s called", __FUNCTION__);
551 return method_do_shutdown_or_sleep(
555 "org.freedesktop.login1.hibernate",
556 "org.freedesktop.login1.hibernate-multiple-sessions",
557 "org.freedesktop.login1.hibernate-ignore-inhibit",
562 int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
563 Manager *m = userdata;
565 log_debug_elogind("%s called", __FUNCTION__);
567 return method_do_shutdown_or_sleep(
571 "org.freedesktop.login1.hibernate",
572 "org.freedesktop.login1.hibernate-multiple-sessions",
573 "org.freedesktop.login1.hibernate-ignore-inhibit",