1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "bus-error.h"
38 #include "unit-name.h"
39 #include "sysfs-show.h"
40 // #include "logs-show.h"
41 #include "cgroup-show.h"
42 #include "cgroup-util.h"
43 #include "spawn-polkit-agent.h"
45 #include "process-util.h"
46 #include "terminal-util.h"
47 #include "signal-util.h"
48 #include "logind-action.h"
50 static char **arg_property = NULL;
51 static bool arg_all = false;
52 static bool arg_full = false;
53 static bool arg_no_pager = false;
54 static bool arg_legend = true;
55 static const char *arg_kill_who = NULL;
56 static int arg_signal = SIGTERM;
57 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
58 static char *arg_host = NULL;
59 static bool arg_ask_password = true;
60 static bool arg_ignore_inhibitors = false;
62 static unsigned arg_lines = 10;
63 static OutputMode arg_output = OUTPUT_SHORT;
72 ACTION_CANCEL_SHUTDOWN,
77 static void pager_open_if_enabled(void) {
85 static void polkit_agent_open_if_enabled(void) {
87 /* Open the polkit agent as a child process if necessary */
89 if (!arg_ask_password)
92 if (arg_transport != BUS_TRANSPORT_LOCAL)
98 /// UNNEEDED by elogind
100 static OutputFlags get_output_flags(void) {
103 arg_all * OUTPUT_SHOW_ALL |
104 arg_full * OUTPUT_FULL_WIDTH |
105 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
106 on_tty() * OUTPUT_COLOR;
110 static int list_sessions(int argc, char *argv[], void *userdata) {
111 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
112 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
113 const char *id, *user, *seat, *object;
114 sd_bus *bus = userdata;
122 pager_open_if_enabled();
124 r = sd_bus_call_method(
126 "org.freedesktop.login1",
127 "/org/freedesktop/login1",
128 "org.freedesktop.login1.Manager",
133 log_error("Failed to list sessions: %s", bus_error_message(&error, r));
137 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
139 return bus_log_parse_error(r);
142 printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
144 while ((r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object)) > 0) {
145 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
149 return bus_log_parse_error(r);
152 printf("\n%u sessions listed.\n", k);
157 static int list_users(int argc, char *argv[], void *userdata) {
158 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
159 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
160 const char *user, *object;
161 sd_bus *bus = userdata;
169 pager_open_if_enabled();
171 r = sd_bus_call_method(
173 "org.freedesktop.login1",
174 "/org/freedesktop/login1",
175 "org.freedesktop.login1.Manager",
180 log_error("Failed to list users: %s", bus_error_message(&error, r));
184 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
186 return bus_log_parse_error(r);
189 printf("%10s %-16s\n", "UID", "USER");
191 while ((r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object)) > 0) {
192 printf("%10u %-16s\n", (unsigned) uid, user);
196 return bus_log_parse_error(r);
199 printf("\n%u users listed.\n", k);
204 static int list_seats(int argc, char *argv[], void *userdata) {
205 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
206 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
207 const char *seat, *object;
208 sd_bus *bus = userdata;
215 pager_open_if_enabled();
217 r = sd_bus_call_method(
219 "org.freedesktop.login1",
220 "/org/freedesktop/login1",
221 "org.freedesktop.login1.Manager",
226 log_error("Failed to list seats: %s", bus_error_message(&error, r));
230 r = sd_bus_message_enter_container(reply, 'a', "(so)");
232 return bus_log_parse_error(r);
235 printf("%-16s\n", "SEAT");
237 while ((r = sd_bus_message_read(reply, "(so)", &seat, &object)) > 0) {
238 printf("%-16s\n", seat);
242 return bus_log_parse_error(r);
245 printf("\n%u seats listed.\n", k);
250 /// UNNEEDED by elogind
252 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
253 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
254 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
255 _cleanup_free_ char *path = NULL;
263 if (arg_transport != BUS_TRANSPORT_LOCAL)
266 path = unit_dbus_path_from_name(unit);
270 r = sd_bus_get_property(
272 "org.freedesktop.systemd1",
276 &error, &reply, "s");
280 r = sd_bus_message_read(reply, "s", &cgroup);
287 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
296 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, get_output_flags());
301 typedef struct SessionStatusInfo {
305 struct dual_timestamp timestamp;
322 typedef struct UserStatusInfo {
325 struct dual_timestamp timestamp;
332 typedef struct SeatStatusInfo {
334 char *active_session;
338 static void session_status_info_clear(SessionStatusInfo *info) {
345 free(info->remote_host);
346 free(info->remote_user);
357 static void user_status_info_clear(UserStatusInfo *info) {
361 strv_free(info->sessions);
368 static void seat_status_info_clear(SeatStatusInfo *info) {
371 free(info->active_session);
372 strv_free(info->sessions);
377 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
378 const char *contents;
381 r = sd_bus_message_peek_type(m, NULL, &contents);
385 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
389 if (contents[0] == 's' || contents[0] == 'o') {
391 char **p = (char **) userdata;
393 r = sd_bus_message_read_basic(m, contents[0], &s);
397 r = free_and_strdup(p, s);
401 r = sd_bus_message_read_basic(m, contents[0], userdata);
406 r = sd_bus_message_skip(m, contents+1);
410 r = sd_bus_message_exit_container(m);
417 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
424 r = sd_bus_message_enter_container(m, 'a', "(so)");
428 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
429 r = strv_extend(userdata, name);
436 return sd_bus_message_exit_container(m);
439 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
441 static const struct bus_properties_map map[] = {
442 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
443 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
444 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
445 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
446 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
447 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
448 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
449 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
450 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
451 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
452 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
453 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
454 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
455 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
456 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
457 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
458 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
459 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
460 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
464 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
465 char since2[FORMAT_TIMESTAMP_MAX], *s2;
466 _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
469 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
471 return log_error_errno(r, "Could not get properties: %m");
478 printf("%s - ", strna(i.id));
481 printf("%s (%u)\n", i.name, (unsigned) i.uid);
483 printf("%u\n", (unsigned) i.uid);
485 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
486 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
489 printf("\t Since: %s; %s\n", s2, s1);
491 printf("\t Since: %s\n", s2);
494 _cleanup_free_ char *t = NULL;
496 printf("\t Leader: %u", (unsigned) i.leader);
498 get_process_comm(i.leader, &t);
505 if (!isempty(i.seat)) {
506 printf("\t Seat: %s", i.seat);
509 printf("; vc%u", i.vtnr);
515 printf("\t TTY: %s\n", i.tty);
517 printf("\t Display: %s\n", i.display);
519 if (i.remote_host && i.remote_user)
520 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
521 else if (i.remote_host)
522 printf("\t Remote: %s\n", i.remote_host);
523 else if (i.remote_user)
524 printf("\t Remote: user %s\n", i.remote_user);
526 printf("\t Remote: Yes\n");
529 printf("\t Service: %s", i.service);
532 printf("; type %s", i.type);
535 printf("; class %s", i.class);
539 printf("\t Type: %s", i.type);
542 printf("; class %s", i.class);
546 printf("\t Class: %s\n", i.class);
548 if (!isempty(i.desktop))
549 printf("\t Desktop: %s\n", i.desktop);
552 printf("\t State: %s\n", i.state);
555 printf("\t Unit: %s\n", i.scope);
557 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
558 if (arg_transport == BUS_TRANSPORT_LOCAL) {
560 show_journal_by_unit(
565 i.timestamp.monotonic,
568 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
569 SD_JOURNAL_LOCAL_ONLY,
580 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
582 static const struct bus_properties_map map[] = {
583 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
584 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
585 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
586 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
587 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
588 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
589 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
590 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
594 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
595 char since2[FORMAT_TIMESTAMP_MAX], *s2;
596 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
599 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
601 return log_error_errno(r, "Could not get properties: %m");
609 printf("%s (%u)\n", i.name, (unsigned) i.uid);
611 printf("%u\n", (unsigned) i.uid);
613 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
614 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
617 printf("\t Since: %s; %s\n", s2, s1);
619 printf("\t Since: %s\n", s2);
621 if (!isempty(i.state))
622 printf("\t State: %s\n", i.state);
624 if (!strv_isempty(i.sessions)) {
626 printf("\tSessions:");
628 STRV_FOREACH(l, i.sessions) {
629 if (streq_ptr(*l, i.display))
639 printf("\t Unit: %s\n", i.slice);
641 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
642 show_journal_by_unit(
647 i.timestamp.monotonic,
650 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
651 SD_JOURNAL_LOCAL_ONLY,
661 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
663 static const struct bus_properties_map map[] = {
664 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
665 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
666 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
670 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
673 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
675 return log_error_errno(r, "Could not get properties: %m");
682 printf("%s\n", strna(i.id));
684 if (!strv_isempty(i.sessions)) {
686 printf("\tSessions:");
688 STRV_FOREACH(l, i.sessions) {
689 if (streq_ptr(*l, i.active_session))
698 if (arg_transport == BUS_TRANSPORT_LOCAL) {
707 printf("\t Devices:\n");
709 show_sysfs(i.id, "\t\t ", c);
715 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
723 r = bus_print_all_properties(bus, "org.freedesktop.login1", path, arg_property, arg_all);
725 log_error_errno(r, "Could not get properties: %m");
730 static int show_session(int argc, char *argv[], void *userdata) {
731 bool properties, new_line = false;
732 sd_bus *bus = userdata;
738 properties = !strstr(argv[0], "status");
740 pager_open_if_enabled();
743 /* If not argument is specified inspect the manager
746 return show_properties(bus, "/org/freedesktop/login1", &new_line);
748 /* And in the pretty case, show data of the calling session */
749 return print_session_status_info(bus, "/org/freedesktop/login1/session/self", &new_line);
752 for (i = 1; i < argc; i++) {
753 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
754 _cleanup_bus_message_unref_ sd_bus_message * reply = NULL;
755 const char *path = NULL;
757 r = sd_bus_call_method(
759 "org.freedesktop.login1",
760 "/org/freedesktop/login1",
761 "org.freedesktop.login1.Manager",
766 log_error("Failed to get session: %s", bus_error_message(&error, r));
770 r = sd_bus_message_read(reply, "o", &path);
772 return bus_log_parse_error(r);
775 r = show_properties(bus, path, &new_line);
777 r = print_session_status_info(bus, path, &new_line);
786 static int show_user(int argc, char *argv[], void *userdata) {
787 bool properties, new_line = false;
788 sd_bus *bus = userdata;
794 properties = !strstr(argv[0], "status");
796 pager_open_if_enabled();
799 /* If not argument is specified inspect the manager
802 return show_properties(bus, "/org/freedesktop/login1", &new_line);
804 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
807 for (i = 1; i < argc; i++) {
808 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
809 _cleanup_bus_message_unref_ sd_bus_message * reply = NULL;
810 const char *path = NULL;
813 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
815 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
817 r = sd_bus_call_method(
819 "org.freedesktop.login1",
820 "/org/freedesktop/login1",
821 "org.freedesktop.login1.Manager",
824 "u", (uint32_t) uid);
826 log_error("Failed to get user: %s", bus_error_message(&error, r));
830 r = sd_bus_message_read(reply, "o", &path);
832 return bus_log_parse_error(r);
835 r = show_properties(bus, path, &new_line);
837 r = print_user_status_info(bus, path, &new_line);
846 static int show_seat(int argc, char *argv[], void *userdata) {
847 bool properties, new_line = false;
848 sd_bus *bus = userdata;
854 properties = !strstr(argv[0], "status");
856 pager_open_if_enabled();
859 /* If not argument is specified inspect the manager
862 return show_properties(bus, "/org/freedesktop/login1", &new_line);
864 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
867 for (i = 1; i < argc; i++) {
868 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
869 _cleanup_bus_message_unref_ sd_bus_message * reply = NULL;
870 const char *path = NULL;
872 r = sd_bus_call_method(
874 "org.freedesktop.login1",
875 "/org/freedesktop/login1",
876 "org.freedesktop.login1.Manager",
881 log_error("Failed to get seat: %s", bus_error_message(&error, r));
885 r = sd_bus_message_read(reply, "o", &path);
887 return bus_log_parse_error(r);
890 r = show_properties(bus, path, &new_line);
892 r = print_seat_status_info(bus, path, &new_line);
901 static int activate(int argc, char *argv[], void *userdata) {
902 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
903 sd_bus *bus = userdata;
910 polkit_agent_open_if_enabled();
913 /* No argument? Let's convert this into the empty
914 * session name, which the calls will then resolve to
915 * the caller's session. */
917 short_argv[0] = argv[0];
918 short_argv[1] = (char*) "";
919 short_argv[2] = NULL;
925 for (i = 1; i < argc; i++) {
927 r = sd_bus_call_method(
929 "org.freedesktop.login1",
930 "/org/freedesktop/login1",
931 "org.freedesktop.login1.Manager",
932 streq(argv[0], "lock-session") ? "LockSession" :
933 streq(argv[0], "unlock-session") ? "UnlockSession" :
934 streq(argv[0], "terminate-session") ? "TerminateSession" :
939 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
947 static int kill_session(int argc, char *argv[], void *userdata) {
948 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
949 sd_bus *bus = userdata;
955 polkit_agent_open_if_enabled();
958 arg_kill_who = "all";
960 for (i = 1; i < argc; i++) {
962 r = sd_bus_call_method(
964 "org.freedesktop.login1",
965 "/org/freedesktop/login1",
966 "org.freedesktop.login1.Manager",
969 "ssi", argv[i], arg_kill_who, arg_signal);
971 log_error("Could not kill session: %s", bus_error_message(&error, -r));
979 static int enable_linger(int argc, char *argv[], void *userdata) {
980 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
981 sd_bus *bus = userdata;
989 polkit_agent_open_if_enabled();
991 b = streq(argv[0], "enable-linger");
994 short_argv[0] = argv[0];
995 short_argv[1] = (char*) "";
996 short_argv[2] = NULL;
1001 for (i = 1; i < argc; i++) {
1004 if (isempty(argv[i]))
1007 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1009 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1012 r = sd_bus_call_method(
1014 "org.freedesktop.login1",
1015 "/org/freedesktop/login1",
1016 "org.freedesktop.login1.Manager",
1019 "ubb", (uint32_t) uid, b, true);
1021 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1029 static int terminate_user(int argc, char *argv[], void *userdata) {
1030 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1031 sd_bus *bus = userdata;
1037 polkit_agent_open_if_enabled();
1039 for (i = 1; i < argc; i++) {
1042 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1044 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1046 r = sd_bus_call_method(
1048 "org.freedesktop.login1",
1049 "/org/freedesktop/login1",
1050 "org.freedesktop.login1.Manager",
1053 "u", (uint32_t) uid);
1055 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1063 static int kill_user(int argc, char *argv[], void *userdata) {
1064 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1065 sd_bus *bus = userdata;
1071 polkit_agent_open_if_enabled();
1074 arg_kill_who = "all";
1076 for (i = 1; i < argc; i++) {
1079 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1081 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1083 r = sd_bus_call_method(
1085 "org.freedesktop.login1",
1086 "/org/freedesktop/login1",
1087 "org.freedesktop.login1.Manager",
1090 "ui", (uint32_t) uid, arg_signal);
1092 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1100 static int attach(int argc, char *argv[], void *userdata) {
1101 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1102 sd_bus *bus = userdata;
1108 polkit_agent_open_if_enabled();
1110 for (i = 2; i < argc; i++) {
1112 r = sd_bus_call_method(
1114 "org.freedesktop.login1",
1115 "/org/freedesktop/login1",
1116 "org.freedesktop.login1.Manager",
1119 "ssb", argv[1], argv[i], true);
1122 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1130 static int flush_devices(int argc, char *argv[], void *userdata) {
1131 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1132 sd_bus *bus = userdata;
1138 polkit_agent_open_if_enabled();
1140 r = sd_bus_call_method(
1142 "org.freedesktop.login1",
1143 "/org/freedesktop/login1",
1144 "org.freedesktop.login1.Manager",
1149 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1154 static int lock_sessions(int argc, char *argv[], void *userdata) {
1155 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1156 sd_bus *bus = userdata;
1162 polkit_agent_open_if_enabled();
1164 r = sd_bus_call_method(
1166 "org.freedesktop.login1",
1167 "/org/freedesktop/login1",
1168 "org.freedesktop.login1.Manager",
1169 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1173 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1178 static int terminate_seat(int argc, char *argv[], void *userdata) {
1179 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1180 sd_bus *bus = userdata;
1186 polkit_agent_open_if_enabled();
1188 for (i = 1; i < argc; i++) {
1190 r = sd_bus_call_method(
1192 "org.freedesktop.login1",
1193 "/org/freedesktop/login1",
1194 "org.freedesktop.login1.Manager",
1199 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1207 /* Ask elogind, which might grant access to unprivileged users
1208 * through PolicyKit */
1209 static int reboot_with_logind(sd_bus *bus, enum action a) {
1210 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1213 static const char *table[_ACTION_MAX] = {
1214 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
1215 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
1216 [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
1222 polkit_agent_open_if_enabled();
1226 case ACTION_POWEROFF:
1227 method = "PowerOff";
1234 case ACTION_SUSPEND:
1238 case ACTION_HIBERNATE:
1239 method = "Hibernate";
1242 case ACTION_HYBRID_SLEEP:
1243 method = "HybridSleep";
1246 case ACTION_CANCEL_SHUTDOWN:
1247 method = "CancelScheduledShutdown";
1255 r = sd_bus_call_method(
1257 "org.freedesktop.login1",
1258 "/org/freedesktop/login1",
1259 "org.freedesktop.login1.Manager",
1268 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
1269 bus_error_message(&error, r));
1270 sd_bus_error_free(&error);
1275 r = sd_bus_call_method(
1277 "org.freedesktop.login1",
1278 "/org/freedesktop/login1",
1279 "org.freedesktop.login1.Manager",
1283 "b", arg_ask_password);
1285 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
1290 static const struct {
1291 HandleAction action;
1293 } action_table[_ACTION_MAX] = {
1294 [ACTION_POWEROFF] = { HANDLE_POWEROFF, "poweroff", },
1295 [ACTION_REBOOT] = { HANDLE_REBOOT, "reboot", },
1296 [ACTION_SUSPEND] = { HANDLE_SUSPEND, "suspend", },
1297 [ACTION_HIBERNATE] = { HANDLE_HIBERNATE, "hibernate", },
1298 [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
1301 static enum action verb_to_action(const char *verb) {
1304 for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
1305 if (streq_ptr(action_table[i].verb, verb))
1308 return _ACTION_INVALID;
1311 static int check_inhibitors(sd_bus *bus, enum action a) {
1312 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1313 _cleanup_strv_free_ char **sessions = NULL;
1314 const char *what, *who, *why, *mode;
1323 if (arg_ignore_inhibitors)
1332 r = sd_bus_call_method(
1334 "org.freedesktop.login1",
1335 "/org/freedesktop/login1",
1336 "org.freedesktop.login1.Manager",
1342 /* If logind is not around, then there are no inhibitors... */
1345 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
1347 return bus_log_parse_error(r);
1349 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
1350 _cleanup_free_ char *comm = NULL, *user = NULL;
1351 _cleanup_strv_free_ char **sv = NULL;
1353 if (!streq(mode, "block"))
1356 sv = strv_split(what, ":");
1360 if ((pid_t) pid < 0)
1361 return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
1363 if (!strv_contains(sv,
1364 a == ACTION_POWEROFF ||
1365 a == ACTION_REBOOT ? "shutdown" : "sleep"))
1368 get_process_comm(pid, &comm);
1369 user = uid_to_name(uid);
1371 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
1372 who, (pid_t) pid, strna(comm), strna(user), why);
1377 return bus_log_parse_error(r);
1379 r = sd_bus_message_exit_container(reply);
1381 return bus_log_parse_error(r);
1383 /* Check for current sessions */
1384 sd_get_sessions(&sessions);
1385 STRV_FOREACH(s, sessions) {
1386 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
1388 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
1391 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
1394 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
1397 sd_session_get_tty(*s, &tty);
1398 sd_session_get_seat(*s, &seat);
1399 sd_session_get_service(*s, &service);
1400 user = uid_to_name(uid);
1402 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
1409 log_error("Please retry operation after closing inhibitors and logging out other users.\n"
1410 "Alternatively, ignore inhibitors and users with 'loginctl %s -i'.",
1411 action_table[a].verb);
1416 static int start_special(int argc, char *argv[], void *userdata) {
1417 sd_bus *bus = userdata;
1423 a = verb_to_action(argv[0]);
1425 r = check_inhibitors(bus, a);
1429 /* Now power off actions in chroot environments */
1430 if ((a == ACTION_POWEROFF ||
1431 a == ACTION_REBOOT) &&
1432 (running_in_chroot() > 0) ) {
1433 log_info("Running in chroot, ignoring request.");
1437 /* Switch to cancel shutdown, if a shutdown action was requested,
1438 and the option to cancel it was set: */
1439 if ((a == ACTION_POWEROFF ||
1440 a == ACTION_REBOOT) &&
1441 (arg_action == ACTION_CANCEL_SHUTDOWN))
1442 return reboot_with_logind(bus, arg_action);
1444 /* Otherwise perform requested action */
1445 if (a == ACTION_POWEROFF ||
1446 a == ACTION_REBOOT ||
1447 a == ACTION_SUSPEND ||
1448 a == ACTION_HIBERNATE ||
1449 a == ACTION_HYBRID_SLEEP)
1450 return reboot_with_logind(bus, a);
1455 static int help(int argc, char *argv[], void *userdata) {
1457 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1458 "Send control commands to or query the login manager.\n\n"
1459 " -h --help Show this help\n"
1460 " --version Show package version\n"
1461 " --no-pager Do not pipe output into a pager\n"
1462 " --no-legend Do not show the headers and footers\n"
1463 " --no-ask-password Don't prompt for password\n"
1464 " -H --host=[USER@]HOST Operate on remote host\n"
1465 " -M --machine=CONTAINER Operate on local container\n"
1466 " -p --property=NAME Show only properties by this name\n"
1467 " -a --all Show all properties, including empty ones\n"
1468 " -l --full Do not ellipsize output\n"
1469 " --kill-who=WHO Who to send signal to\n"
1470 " -s --signal=SIGNAL Which signal to send\n"
1472 " -n --lines=INTEGER Number of journal entries to show\n"
1473 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1474 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1476 " -c Cancel a pending shutdown\n"
1477 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n"
1478 "Session Commands:\n"
1479 " list-sessions List sessions\n"
1480 " session-status [ID...] Show session status\n"
1481 " show-session [ID...] Show properties of sessions or the manager\n"
1482 " activate [ID] Activate a session\n"
1483 " lock-session [ID...] Screen lock one or more sessions\n"
1484 " unlock-session [ID...] Screen unlock one or more sessions\n"
1485 " lock-sessions Screen lock all current sessions\n"
1486 " unlock-sessions Screen unlock all current sessions\n"
1487 " terminate-session ID... Terminate one or more sessions\n"
1488 " kill-session ID... Send signal to processes of a session\n\n"
1490 " list-users List users\n"
1491 " user-status [USER...] Show user status\n"
1492 " show-user [USER...] Show properties of users or the manager\n"
1493 " enable-linger [USER...] Enable linger state of one or more users\n"
1494 " disable-linger [USER...] Disable linger state of one or more users\n"
1495 " terminate-user USER... Terminate all sessions of one or more users\n"
1496 " kill-user USER... Send signal to processes of a user\n\n"
1498 " list-seats List seats\n"
1499 " seat-status [NAME...] Show seat status\n"
1500 " show-seat [NAME...] Show properties of seats or the manager\n"
1501 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1502 " flush-devices Flush all device associations\n"
1503 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1504 "System Commands:\n"
1505 " poweroff Turn off the machine\n"
1506 " reboot Reboot the machine\n"
1507 " suspend Suspend the machine to memory\n"
1508 " hibernate Suspend the machine to disk\n"
1509 " hybrid-sleep Suspend the machine to memory and disk\n"
1510 , program_invocation_short_name);
1515 static int parse_argv(int argc, char *argv[]) {
1518 ARG_VERSION = 0x100,
1522 ARG_NO_ASK_PASSWORD,
1525 static const struct option options[] = {
1526 { "help", no_argument, NULL, 'h' },
1527 { "version", no_argument, NULL, ARG_VERSION },
1528 { "property", required_argument, NULL, 'p' },
1529 { "all", no_argument, NULL, 'a' },
1530 { "full", no_argument, NULL, 'l' },
1531 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1532 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1533 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1534 { "signal", required_argument, NULL, 's' },
1535 { "host", required_argument, NULL, 'H' },
1536 { "machine", required_argument, NULL, 'M' },
1537 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1539 { "lines", required_argument, NULL, 'n' },
1540 { "output", required_argument, NULL, 'o' },
1542 { "ignore-inhibitors", no_argument, NULL, 'i' },
1551 while ((c = getopt_long(argc, argv, "hp:als:H:M:ci", options, NULL)) >= 0)
1556 help(0, NULL, NULL);
1560 puts(PACKAGE_STRING);
1561 puts(SYSTEMD_FEATURES);
1565 r = strv_extend(&arg_property, optarg);
1569 /* If the user asked for a particular
1570 * property, show it to him, even if it is
1585 if (safe_atou(optarg, &arg_lines) < 0) {
1586 log_error("Failed to parse lines '%s'", optarg);
1592 arg_output = output_mode_from_string(optarg);
1593 if (arg_output < 0) {
1594 log_error("Unknown output '%s'.", optarg);
1600 arg_no_pager = true;
1607 case ARG_NO_ASK_PASSWORD:
1608 arg_ask_password = false;
1612 arg_kill_who = optarg;
1616 arg_signal = signal_from_string_try_harder(optarg);
1617 if (arg_signal < 0) {
1618 log_error("Failed to parse signal string %s.", optarg);
1624 arg_transport = BUS_TRANSPORT_REMOTE;
1629 arg_transport = BUS_TRANSPORT_MACHINE;
1634 arg_action = ACTION_CANCEL_SHUTDOWN;
1638 arg_ignore_inhibitors = true;
1645 assert_not_reached("Unhandled option");
1651 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1653 static const Verb verbs[] = {
1654 { "help", VERB_ANY, VERB_ANY, 0, help },
1655 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1656 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1657 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1658 { "activate", VERB_ANY, 2, 0, activate },
1659 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1660 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1661 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1662 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1663 { "terminate-session", 2, VERB_ANY, 0, activate },
1664 { "kill-session", 2, VERB_ANY, 0, kill_session },
1665 { "list-users", VERB_ANY, 1, 0, list_users },
1666 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1667 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1668 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1669 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1670 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1671 { "kill-user", 2, VERB_ANY, 0, kill_user },
1672 { "list-seats", VERB_ANY, 1, 0, list_seats },
1673 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1674 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1675 { "attach", 3, VERB_ANY, 0, attach },
1676 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1677 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1678 { "poweroff", VERB_ANY, 1, 0, start_special },
1679 { "reboot", VERB_ANY, 1, 0, start_special },
1680 { "suspend", VERB_ANY, 1, 0, start_special },
1681 { "hibernate", VERB_ANY, 1, 0, start_special },
1682 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1683 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1687 return dispatch_verb(argc, argv, verbs, bus);
1690 int main(int argc, char *argv[]) {
1691 _cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
1694 setlocale(LC_ALL, "");
1695 elogind_set_program_name(argv[0]);
1696 log_parse_environment();
1699 r = parse_argv(argc, argv);
1703 r = bus_open_transport(arg_transport, arg_host, false, &bus);
1705 log_error_errno(r, "Failed to create bus connection: %m");
1709 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1711 r = loginctl_main(argc, argv, bus);
1715 polkit_agent_close();
1717 strv_free(arg_property);
1719 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;