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;
68 static int check_inhibitors(sd_bus* bus, enum elogind_action a) {
69 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
70 _cleanup_strv_free_ char **sessions = NULL;
71 const char *what, *who, *why, *mode;
77 if (arg_ignore_inhibitors)
89 r = sd_bus_call_method(
91 "org.freedesktop.login1",
92 "/org/freedesktop/login1",
93 "org.freedesktop.login1.Manager",
99 /* If logind is not around, then there are no inhibitors... */
102 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
104 return bus_log_parse_error(r);
106 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
107 _cleanup_free_ char *comm = NULL, *user = NULL;
108 _cleanup_strv_free_ char **sv = NULL;
110 if (!streq(mode, "block"))
113 sv = strv_split(what, ":");
118 return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
120 if (!strv_contains(sv,
125 ACTION_KEXEC) ? "shutdown" : "sleep"))
128 get_process_comm(pid, &comm);
129 user = uid_to_name(uid);
131 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
132 who, (pid_t) pid, strna(comm), strna(user), why);
137 return bus_log_parse_error(r);
139 r = sd_bus_message_exit_container(reply);
141 return bus_log_parse_error(r);
143 /* Check for current sessions */
144 sd_get_sessions(&sessions);
145 STRV_FOREACH(s, sessions) {
146 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
148 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
151 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
154 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
157 sd_session_get_tty(*s, &tty);
158 sd_session_get_seat(*s, &seat);
159 sd_session_get_service(*s, &service);
160 user = uid_to_name(uid);
162 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
169 log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'loginctl -i %s'.",
170 action_table[a].verb);
175 int elogind_cancel_shutdown(sd_bus *bus) {
176 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
179 r = elogind_set_wall_message(bus, NULL);
182 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
183 bus_error_message(&error, r));
184 sd_bus_error_free(&error);
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, "Failed to talk to elogind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r));
201 void elogind_cleanup(void) {
202 polkit_agent_close();
206 static void elogind_log_special(enum elogind_action a) {
207 #ifdef ENABLE_DEBUG_ELOGIND
211 LOG_MESSAGE("Halt action called."),
212 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
215 case ACTION_POWEROFF:
217 LOG_MESSAGE("Poweroff action called."),
218 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
223 LOG_MESSAGE("Reboot action called."),
224 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
229 LOG_MESSAGE("KExec action called."),
230 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
235 LOG_MESSAGE("Suspend action called."),
236 LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
239 case ACTION_HIBERNATE:
241 LOG_MESSAGE("Hibernate action called."),
242 LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
244 case ACTION_HYBRID_SLEEP:
246 LOG_MESSAGE("Hybrid-Sleep action called."),
247 LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
249 case ACTION_CANCEL_SHUTDOWN:
251 LOG_MESSAGE("Cancel Shutdown called."),
252 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
258 #endif // ENABLE_DEBUG_ELOGIND
261 static int elogind_reboot(sd_bus *bus, enum elogind_action a) {
262 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
263 const char *method = NULL;
265 static const char *table[_ACTION_MAX] = {
266 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
267 [ACTION_POWEROFF] = "The system is going down for power-off NOW!"
275 case ACTION_POWEROFF:
287 case ACTION_HIBERNATE:
288 method = "Hibernate";
291 case ACTION_HYBRID_SLEEP:
292 method = "HybridSleep";
299 polkit_agent_open_if_enabled();
300 r = elogind_set_wall_message(bus, table[a]);
303 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
304 bus_error_message(&error, r));
305 sd_bus_error_free(&error);
308 /* Now call elogind itself to request the operation */
309 r = sd_bus_call_method(
311 "org.freedesktop.login1",
312 "/org/freedesktop/login1",
313 "org.freedesktop.login1.Manager",
317 "b", arg_ask_password);
320 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
325 static int elogind_schedule_shutdown(sd_bus *bus, enum elogind_action a) {
326 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
327 const char *method = NULL;
335 case ACTION_POWEROFF:
347 r = elogind_set_wall_message(bus, NULL);
350 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
351 bus_error_message(&error, r));
352 sd_bus_error_free(&error);
355 r = sd_bus_call_method(
357 "org.freedesktop.login1",
358 "/org/freedesktop/login1",
359 "org.freedesktop.login1.Manager",
368 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
373 static int elogind_set_wall_message(sd_bus* bus, const char* msg) {
374 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
375 _cleanup_free_ char *m = NULL;
378 if (strv_extend(&arg_wall, msg) < 0)
381 m = strv_join(arg_wall, " ");
385 r = sd_bus_call_method(
387 "org.freedesktop.login1",
388 "/org/freedesktop/login1",
389 "org.freedesktop.login1.Manager",
398 return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r));
403 static int parse_shutdown_time_spec(const char *t, usec_t *_u) {
409 else if (!strchr(t, ':')) {
412 if (safe_atou64(t, &u) < 0)
415 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
424 hour = strtol(t, &e, 10);
425 if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
428 minute = strtol(e+1, &e, 10);
429 if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
432 n = now(CLOCK_REALTIME);
433 s = (time_t) (n / USEC_PER_SEC);
435 assert_se(localtime_r(&s, &tm));
437 tm.tm_hour = (int) hour;
438 tm.tm_min = (int) minute;
441 assert_se(s = mktime(&tm));
443 *_u = (usec_t) s * USEC_PER_SEC;
452 void polkit_agent_open_if_enabled(void) {
454 /* Open the polkit agent as a child process if necessary */
456 if (!arg_ask_password)
459 if (arg_transport != BUS_TRANSPORT_LOCAL)
465 int start_special(int argc, char *argv[], void *userdata) {
466 sd_bus *bus = userdata;
467 enum elogind_action a;
473 a = verb_to_action(argv[0]);
475 elogind_log_special(a);
477 /* No power off actions in chroot environments */
478 if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
479 && (running_in_chroot() > 0) ) {
480 log_info("Running in chroot, ignoring request.");
484 /* Check time arguments */
485 if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
487 && (arg_action != ACTION_CANCEL_SHUTDOWN) ) {
488 r = parse_shutdown_time_spec(argv[1], &arg_when);
490 log_error("Failed to parse time specification: %s", argv[optind]);
494 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
496 /* The optional user wall message must be set */
497 if ((argc > 1) && (arg_action == ACTION_CANCEL_SHUTDOWN) )
498 /* No time argument for shutdown cancel */
501 /* We skip the time argument */
505 arg_wall = strv_copy(wall);
510 /* Switch to cancel shutdown, if a shutdown action was requested,
511 and the option to cancel it was set: */
512 if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
513 && (arg_action == ACTION_CANCEL_SHUTDOWN) )
514 return elogind_cancel_shutdown(bus);
516 r = check_inhibitors(bus, a);
520 /* Perform requested action */
526 ACTION_HYBRID_SLEEP)) {
528 return elogind_schedule_shutdown(bus, a);
530 return elogind_reboot(bus, a);