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) {
87 r = safe_fork_full(helper, NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_WAIT, &pid);
90 return log_error_errno(errno, "Failed to fork run %s: %m", helper);
95 execlp(helper, helper, NULL);
96 log_error_errno(errno, "Failed to execute %s: %m", helper);
103 /* elogind specific executor */
104 static int shutdown_or_sleep(Manager *m, HandleAction action) {
109 case HANDLE_POWEROFF:
110 return run_helper(POWEROFF);
112 return run_helper(REBOOT);
114 return run_helper(HALT);
116 return run_helper(KEXEC);
118 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
119 case HANDLE_HIBERNATE:
120 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
121 case HANDLE_HYBRID_SLEEP:
122 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
128 int execute_shutdown_or_sleep(
132 sd_bus_error *error) {
134 char** argv_utmp = NULL;
139 assert(w < _INHIBIT_WHAT_MAX);
141 bus_manager_log_shutdown(m, w, action);
143 if (IN_SET(action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
145 /* As we have no systemd update-utmp daemon running, we have to
146 * set the relevant utmp/wtmp entries ourselves.
149 if (strv_extend(&argv_utmp, "elogind") < 0)
152 if (HANDLE_REBOOT == action) {
153 if (strv_extend(&argv_utmp, "reboot") < 0)
156 if (strv_extend(&argv_utmp, "shutdown") < 0)
160 /* This comes from our patched update-utmp/update-utmp.c */
161 update_utmp(2, argv_utmp, m->bus);
162 strv_free(argv_utmp);
165 /* Now perform the requested action */
166 r = shutdown_or_sleep(m, action);
168 /* no more pending actions, whether this failed or not */
169 m->pending_action = HANDLE_IGNORE;
171 /* As elogind can not rely on a systemd manager to call all
172 * sleeping processes to wake up, we have to tell them all
174 if (w == INHIBIT_SLEEP) {
175 (void) send_prepare_for(m, w, false);
183 /* Make sure the lid switch is ignored for a while */
184 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
189 static int method_do_shutdown_or_sleep(
191 sd_bus_message *message,
192 HandleAction sleep_action,
195 const char *action_multiple_sessions,
196 const char *action_ignore_inhibit,
197 const char *sleep_verb,
198 sd_bus_error *error) {
205 assert(w <= _INHIBIT_WHAT_MAX);
207 r = sd_bus_message_read(message, "b", &interactive);
211 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
212 __FUNCTION__, action, sleep_verb,
213 interactive ? "" : "NOT ");
215 /* Don't allow multiple jobs being executed at the same time */
217 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
220 r = can_sleep(m, sleep_verb);
225 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
228 if (IN_SET(sleep_action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
229 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
230 action_ignore_inhibit, error);
231 log_debug_elogind("verify_shutdown_creds() returned %d", r);
236 r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
240 return sd_bus_reply_method_return(message, NULL);
243 int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
244 Manager *m = userdata;
246 log_debug_elogind("%s called", __FUNCTION__);
248 return method_do_shutdown_or_sleep(
252 "org.freedesktop.login1.power-off",
253 "org.freedesktop.login1.power-off-multiple-sessions",
254 "org.freedesktop.login1.power-off-ignore-inhibit",
259 int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
260 Manager *m = userdata;
262 log_debug_elogind("%s called", __FUNCTION__);
264 return method_do_shutdown_or_sleep(
268 "org.freedesktop.login1.reboot",
269 "org.freedesktop.login1.reboot-multiple-sessions",
270 "org.freedesktop.login1.reboot-ignore-inhibit",
275 int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
276 Manager *m = userdata;
278 log_debug_elogind("%s called", __FUNCTION__);
280 return method_do_shutdown_or_sleep(
284 "org.freedesktop.login1.halt",
285 "org.freedesktop.login1.halt-multiple-sessions",
286 "org.freedesktop.login1.halt-ignore-inhibit",
291 int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
292 Manager *m = userdata;
294 log_debug_elogind("%s called", __FUNCTION__);
296 return method_do_shutdown_or_sleep(
300 "org.freedesktop.login1.suspend",
301 "org.freedesktop.login1.suspend-multiple-sessions",
302 "org.freedesktop.login1.suspend-ignore-inhibit",
307 int manager_scheduled_shutdown_handler(
312 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
313 Manager *m = userdata;
319 if (isempty(m->scheduled_shutdown_type))
322 if (streq(m->scheduled_shutdown_type, "halt"))
323 action = HANDLE_HALT;
324 else if (streq(m->scheduled_shutdown_type, "poweroff"))
325 action = HANDLE_POWEROFF;
327 action = HANDLE_REBOOT;
329 /* Don't allow multiple jobs being executed at the same time */
330 if (m->action_what) {
332 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress",
333 m->scheduled_shutdown_type);
337 if (m->shutdown_dry_run) {
338 /* We do not process delay inhibitors here. Otherwise, we
339 * would have to be considered "in progress" (like the check
340 * above) for some seconds after our admin has seen the final
343 bus_manager_log_shutdown(m, INHIBIT_SHUTDOWN, action);
344 log_info("Running in dry run, suppressing action.");
345 reset_scheduled_shutdown(m);
350 r = execute_shutdown_or_sleep(m, 0, action, &error);
352 log_error_errno(r, "Scheduled shutdown to %s failed: %m",
353 m->scheduled_shutdown_type);
360 reset_scheduled_shutdown(m);
364 int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
365 Manager *m = userdata;
367 log_debug_elogind("%s called", __FUNCTION__);
369 return method_do_shutdown_or_sleep(
373 "org.freedesktop.login1.hibernate",
374 "org.freedesktop.login1.hibernate-multiple-sessions",
375 "org.freedesktop.login1.hibernate-ignore-inhibit",
380 int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
381 Manager *m = userdata;
383 log_debug_elogind("%s called", __FUNCTION__);
385 return method_do_shutdown_or_sleep(
389 "org.freedesktop.login1.hibernate",
390 "org.freedesktop.login1.hibernate-multiple-sessions",
391 "org.freedesktop.login1.hibernate-ignore-inhibit",