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"
25 #include "process-util.h"
26 #include "sd-messages.h"
27 #include "sleep-config.h"
28 #include "string-util.h"
30 #include "update-utmp.h"
31 #include "user-util.h"
34 int have_multiple_sessions(
43 /* Check for other users' sessions. Greeter sessions do not
44 * count, and non-login sessions do not count either. */
45 HASHMAP_FOREACH(session, m->sessions, i)
46 if (session->class == SESSION_USER &&
47 session->user->uid != uid)
53 static int bus_manager_log_shutdown(
56 HandleAction action) {
62 if (w != INHIBIT_SHUTDOWN)
67 p = "MESSAGE=System is powering down.";
68 q = "SHUTDOWN=power-off";
71 p = "MESSAGE=System is halting.";
75 p = "MESSAGE=System is rebooting.";
76 q = "SHUTDOWN=reboot";
79 p = "MESSAGE=System is rebooting with kexec.";
83 p = "MESSAGE=System is shutting down.";
87 if (isempty(m->wall_message))
90 p = strjoina(p, " (", m->wall_message, ").");
92 return log_struct(LOG_NOTICE,
93 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
99 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
101 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
102 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
103 [INHIBIT_SLEEP] = "PrepareForSleep"
106 int active = _active;
110 assert(w < _INHIBIT_WHAT_MAX);
111 assert(signal_name[w]);
113 return sd_bus_emit_signal(m->bus,
114 "/org/freedesktop/login1",
115 "org.freedesktop.login1.Manager",
121 static int execute_shutdown_or_sleep(
125 sd_bus_error *error) {
127 char** argv_utmp = NULL;
132 assert(w < _INHIBIT_WHAT_MAX);
134 bus_manager_log_shutdown(m, w, action);
136 if (IN_SET(action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
138 /* As we have no systemd update-utmp daemon running, we have to
139 * set the relevant utmp/wtmp entries ourselves.
142 if (strv_extend(&argv_utmp, "elogind") < 0)
145 if (HANDLE_REBOOT == action) {
146 if (strv_extend(&argv_utmp, "reboot") < 0)
149 if (strv_extend(&argv_utmp, "shutdown") < 0)
153 /* This comes from our patched update-utmp/update-utmp.c */
154 update_utmp(2, argv_utmp, m->bus);
155 strv_free(argv_utmp);
158 /* Now perform the requested action */
159 r = shutdown_or_sleep(m, action);
161 /* no more pending actions, whether this failed or not */
162 m->pending_action = HANDLE_IGNORE;
164 /* As elogind can not rely on a systemd manager to call all
165 * sleeping processes to wake up, we have to tell them all
167 if (w == INHIBIT_SLEEP) {
168 send_prepare_for(m, w, false);
176 /* Make sure the lid switch is ignored for a while */
177 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
182 int manager_dispatch_delayed(Manager *manager, bool timeout) {
184 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
185 Inhibitor *offending = NULL;
190 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
193 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
194 _cleanup_free_ char *comm = NULL, *u = NULL;
199 (void) get_process_comm(offending->pid, &comm);
200 u = uid_to_name(offending->uid);
202 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
203 offending->uid, strna(u),
204 offending->pid, strna(comm));
207 /* Actually do the operation */
208 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
210 log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
212 manager->pending_action = HANDLE_IGNORE;
213 manager->action_what = 0;
214 /* It is not a critical error for elogind if suspending fails */
220 static int manager_inhibit_timeout_handler(
225 Manager *manager = userdata;
229 assert(manager->inhibit_timeout_source == s);
231 r = manager_dispatch_delayed(manager, true);
232 return (r < 0) ? r : 0;
235 static int delay_shutdown_or_sleep(
238 HandleAction action) {
245 assert(w < _INHIBIT_WHAT_MAX);
247 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
249 if (m->inhibit_timeout_source) {
250 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
252 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
254 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
256 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
258 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
259 timeout_val, 0, manager_inhibit_timeout_handler, m);
264 m->pending_action = action;
270 int bus_manager_shutdown_or_sleep_now_or_later(
274 sd_bus_error *error) {
281 assert(w <= _INHIBIT_WHAT_MAX);
283 /* Tell everybody to prepare for shutdown/sleep */
284 send_prepare_for(m, w, true);
287 m->inhibit_delay_max > 0 &&
288 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
290 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
291 handle_action_to_string(action),
292 delayed ? "" : "NOT ");
295 /* Shutdown is delayed, keep in mind what we
296 * want to do, and start a timeout */
297 r = delay_shutdown_or_sleep(m, w, action);
299 /* Shutdown is not delayed, execute it
301 r = execute_shutdown_or_sleep(m, w, action, error);
306 int verify_shutdown_creds(
308 sd_bus_message *message,
312 const char *action_multiple_sessions,
313 const char *action_ignore_inhibit,
314 sd_bus_error *error) {
316 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
317 bool multiple_sessions, blocked;
324 assert(w <= _INHIBIT_WHAT_MAX);
326 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
330 r = sd_bus_creds_get_euid(creds, &uid);
334 r = have_multiple_sessions(m, uid);
338 multiple_sessions = r > 0;
339 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
341 if (multiple_sessions && action_multiple_sessions) {
342 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
346 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
349 if (blocked && action_ignore_inhibit) {
350 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
354 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
357 if (!multiple_sessions && !blocked && action) {
358 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
362 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
368 static int method_do_shutdown_or_sleep(
370 sd_bus_message *message,
371 HandleAction sleep_action,
374 const char *action_multiple_sessions,
375 const char *action_ignore_inhibit,
376 const char *sleep_verb,
377 sd_bus_error *error) {
384 assert(w <= _INHIBIT_WHAT_MAX);
386 r = sd_bus_message_read(message, "b", &interactive);
390 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
391 __FUNCTION__, action, sleep_verb,
392 interactive ? "" : "NOT ");
394 /* Don't allow multiple jobs being executed at the same time */
396 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
399 r = can_sleep(m, sleep_verb);
404 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
407 if (IN_SET(sleep_action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
408 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
409 action_ignore_inhibit, error);
410 log_debug_elogind("verify_shutdown_creds() returned %d", r);
415 r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
419 return sd_bus_reply_method_return(message, NULL);
422 int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
423 Manager *m = userdata;
425 log_debug_elogind("%s called", __FUNCTION__);
427 return method_do_shutdown_or_sleep(
431 "org.freedesktop.login1.power-off",
432 "org.freedesktop.login1.power-off-multiple-sessions",
433 "org.freedesktop.login1.power-off-ignore-inhibit",
438 int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
439 Manager *m = userdata;
441 log_debug_elogind("%s called", __FUNCTION__);
443 return method_do_shutdown_or_sleep(
447 "org.freedesktop.login1.reboot",
448 "org.freedesktop.login1.reboot-multiple-sessions",
449 "org.freedesktop.login1.reboot-ignore-inhibit",
454 int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
455 Manager *m = userdata;
457 log_debug_elogind("%s called", __FUNCTION__);
459 return method_do_shutdown_or_sleep(
463 "org.freedesktop.login1.suspend",
464 "org.freedesktop.login1.suspend-multiple-sessions",
465 "org.freedesktop.login1.suspend-ignore-inhibit",
470 int manager_scheduled_shutdown_handler(
475 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
476 Manager *m = userdata;
482 if (isempty(m->scheduled_shutdown_type))
485 if (streq(m->scheduled_shutdown_type, "halt"))
486 action = HANDLE_HALT;
487 else if (streq(m->scheduled_shutdown_type, "poweroff"))
488 action = HANDLE_POWEROFF;
490 action = HANDLE_REBOOT;
492 r = execute_shutdown_or_sleep(m, 0, action, &error);
494 return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type);
499 int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
500 Manager *m = userdata;
502 log_debug_elogind("%s called", __FUNCTION__);
504 return method_do_shutdown_or_sleep(
508 "org.freedesktop.login1.hibernate",
509 "org.freedesktop.login1.hibernate-multiple-sessions",
510 "org.freedesktop.login1.hibernate-ignore-inhibit",
515 int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
516 Manager *m = userdata;
518 log_debug_elogind("%s called", __FUNCTION__);
520 return method_do_shutdown_or_sleep(
524 "org.freedesktop.login1.hibernate",
525 "org.freedesktop.login1.hibernate-multiple-sessions",
526 "org.freedesktop.login1.hibernate-ignore-inhibit",