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-error.h"
23 #include "eloginctl.h"
24 #include "logind-action.h"
25 #include "parse-util.h"
26 #include "process-util.h"
28 #include "sd-messages.h"
29 #include "spawn-polkit-agent.h"
30 #include "string-util.h"
32 #include "terminal-util.h"
33 #include "user-util.h"
37 elogind_action arg_action = _ACTION_INVALID;
38 bool arg_ask_password = true;
39 bool arg_ignore_inhibitors = false;
40 bool arg_no_wall = false;
41 BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
42 char** arg_wall = NULL;
48 } action_table[_ACTION_MAX] = {
49 [ACTION_POWEROFF] = { HANDLE_POWEROFF, "poweroff", },
50 [ACTION_REBOOT] = { HANDLE_REBOOT, "reboot", },
51 [ACTION_SUSPEND] = { HANDLE_SUSPEND, "suspend", },
52 [ACTION_HIBERNATE] = { HANDLE_HIBERNATE, "hibernate", },
53 [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
56 static int elogind_set_wall_message(sd_bus* bus, const char* msg);
58 static enum elogind_action verb_to_action(const char *verb) {
59 enum elogind_action i;
61 for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
62 if (streq_ptr(action_table[i].verb, verb))
65 return _ACTION_INVALID;
69 * systemctl/systemctl.c:3292:logind_check_inhibitors()
71 static int check_inhibitors(sd_bus* bus, enum elogind_action a) {
72 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
73 _cleanup_strv_free_ char **sessions = NULL;
74 const char *what, *who, *why, *mode;
80 if (arg_ignore_inhibitors)
92 r = sd_bus_call_method(
94 "org.freedesktop.login1",
95 "/org/freedesktop/login1",
96 "org.freedesktop.login1.Manager",
102 /* If logind is not around, then there are no inhibitors... */
105 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
107 return bus_log_parse_error(r);
109 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
110 _cleanup_free_ char *comm = NULL, *user = NULL;
111 _cleanup_strv_free_ char **sv = NULL;
113 if (!streq(mode, "block"))
116 sv = strv_split(what, ":");
121 return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
123 if (!strv_contains(sv,
128 ACTION_KEXEC) ? "shutdown" : "sleep"))
131 get_process_comm(pid, &comm);
132 user = uid_to_name(uid);
134 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
135 who, (pid_t) pid, strna(comm), strna(user), why);
140 return bus_log_parse_error(r);
142 r = sd_bus_message_exit_container(reply);
144 return bus_log_parse_error(r);
146 /* Check for current sessions */
147 sd_get_sessions(&sessions);
148 STRV_FOREACH(s, sessions) {
149 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
151 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
154 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
157 if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "tty"))
160 sd_session_get_tty(*s, &tty);
161 sd_session_get_seat(*s, &seat);
162 sd_session_get_service(*s, &service);
163 user = uid_to_name(uid);
165 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
172 log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'loginctl %s -i'.",
173 action_table[a].verb);
179 * systemctl/systemctl.c:8410:logind_cancel_shutdown()
181 int elogind_cancel_shutdown(sd_bus *bus) {
182 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
185 (void) elogind_set_wall_message(bus, NULL);
187 r = sd_bus_call_method(
189 "org.freedesktop.login1",
190 "/org/freedesktop/login1",
191 "org.freedesktop.login1.Manager",
192 "CancelScheduledShutdown",
196 return log_warning_errno(r,
197 "Failed to talk to elogind, shutdown hasn't been cancelled: %s",
198 bus_error_message(&error, r));
203 /* Only a little helper for cleaning up elogind specific extra stuff. */
204 void elogind_cleanup(void) {
205 polkit_agent_close();
209 /* Littel debug log helper, helps debugging systemctl comands we mimic. */
210 static void elogind_log_special(enum elogind_action a) {
211 #ifdef ENABLE_DEBUG_ELOGIND
215 LOG_MESSAGE("Halt action called."),
216 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
219 case ACTION_POWEROFF:
221 LOG_MESSAGE("Poweroff action called."),
222 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
227 LOG_MESSAGE("Reboot action called."),
228 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
233 LOG_MESSAGE("KExec action called."),
234 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
239 LOG_MESSAGE("Suspend action called."),
240 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
243 case ACTION_HIBERNATE:
245 LOG_MESSAGE("Hibernate action called."),
246 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
249 case ACTION_HYBRID_SLEEP:
251 LOG_MESSAGE("Hybrid-Sleep action called."),
252 "MESSAGE_ID=" SD_MESSAGE_SLEEP_START_STR,
255 case ACTION_CANCEL_SHUTDOWN:
257 LOG_MESSAGE("Cancel Shutdown called."),
258 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
264 #endif // ENABLE_DEBUG_ELOGIND
268 * systemctl/systemctl.c:3229:logind_reboot()
270 static int elogind_reboot(sd_bus *bus, enum elogind_action a) {
271 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
272 const char *method = NULL, *description = NULL;
274 static const char *table[_ACTION_MAX] = {
275 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
276 [ACTION_POWEROFF] = "The system is going down for power-off NOW!"
286 description = "reboot system";
289 case ACTION_POWEROFF:
291 description = "power off system";
296 description = "suspend system";
299 case ACTION_HIBERNATE:
300 method = "Hibernate";
301 description = "hibernate system";
304 case ACTION_HYBRID_SLEEP:
305 method = "HybridSleep";
306 description = "put system into hybrid sleep";
313 polkit_agent_open_if_enabled();
315 if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT) )
316 (void) elogind_set_wall_message(bus, table[a]);
318 /* Now call elogind itself to request the operation */
319 r = sd_bus_call_method(
321 "org.freedesktop.login1",
322 "/org/freedesktop/login1",
323 "org.freedesktop.login1.Manager",
327 "b", arg_ask_password);
330 return log_error_errno(r, "Failed to %s via elogind: %s",
332 bus_error_message(&error, r));
338 * systemctl/systemctl.c:8281:logind_schedule_shutdown()
340 static int elogind_schedule_shutdown(sd_bus *bus, enum elogind_action a) {
341 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
342 char date[FORMAT_TIMESTAMP_MAX];
353 case ACTION_POWEROFF:
365 (void) elogind_set_wall_message(bus, NULL);
367 r = sd_bus_call_method(
369 "org.freedesktop.login1",
370 "/org/freedesktop/login1",
371 "org.freedesktop.login1.Manager",
380 return log_warning_errno(r,
381 "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s",
382 bus_error_message(&error, r));
384 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
385 format_timestamp(date, sizeof(date), arg_when));
391 * systemctl/systemctl.c:3193:logind_set_wall_message()
392 * (Tweaked to allow an extra message to be appended.)
394 static int elogind_set_wall_message(sd_bus* bus, const char* msg) {
395 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
396 _cleanup_free_ char *m = NULL;
399 if (strv_extend(&arg_wall, msg) < 0)
402 m = strv_join(arg_wall, " ");
406 r = sd_bus_call_method(
408 "org.freedesktop.login1",
409 "/org/freedesktop/login1",
410 "org.freedesktop.login1.Manager",
419 return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r));
425 * systemctl/systemctl.c:7743:parse_shutdown_time_spec()
427 static int parse_shutdown_time_spec(const char *t, usec_t *_u) {
433 else if (!strchr(t, ':')) {
436 if (safe_atou64(t, &u) < 0)
439 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
448 hour = strtol(t, &e, 10);
449 if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
452 minute = strtol(e+1, &e, 10);
453 if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
456 n = now(CLOCK_REALTIME);
457 s = (time_t) (n / USEC_PER_SEC);
459 assert_se(localtime_r(&s, &tm));
461 tm.tm_hour = (int) hour;
462 tm.tm_min = (int) minute;
465 assert_se(s = mktime(&tm));
467 *_u = (usec_t) s * USEC_PER_SEC;
477 * systemctl/systemctl.c:270:polkit_agent_open_if_enabled()
479 void polkit_agent_open_if_enabled(void) {
481 /* Open the polkit agent as a child process if necessary */
483 if (!arg_ask_password)
486 if (arg_transport != BUS_TRANSPORT_LOCAL)
493 * systemctl/systemctl.c:3482:start_special()
494 * However, this elogind variant is very different from the original.
496 int start_special(int argc, char *argv[], void *userdata) {
497 sd_bus *bus = userdata;
498 enum elogind_action a;
504 a = verb_to_action(argv[0]);
506 elogind_log_special(a);
508 /* For poweroff and reboot, some extra checks are performed: */
509 if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT) ) {
511 /* No power off actions in chroot environments */
512 if ( running_in_chroot() > 0 ) {
513 log_info("Running in chroot, ignoring request.");
517 /* Check time argument */
518 if ( (argc > 1) && (ACTION_CANCEL_SHUTDOWN != arg_action)) {
519 r = parse_shutdown_time_spec(argv[1], &arg_when);
521 log_error("Failed to parse time specification: %s", argv[optind]);
526 /* The optional user wall message must be set */
528 && ( (arg_action == ACTION_CANCEL_SHUTDOWN)
529 || (0 == arg_when) ) )
530 /* No time argument for shutdown cancel, or no
531 * time argument given. */
534 /* We skip the time argument */
538 arg_wall = strv_copy(wall);
544 /* Switch to cancel shutdown, if a shutdown action was requested,
545 and the option to cancel it was set: */
546 if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
547 && (arg_action == ACTION_CANCEL_SHUTDOWN) )
548 return elogind_cancel_shutdown(bus);
550 r = check_inhibitors(bus, a);
554 /* Perform requested action */
560 ACTION_HYBRID_SLEEP)) {
562 return elogind_schedule_shutdown(bus, a);
564 return elogind_reboot(bus, a);