2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd 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 systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "alloc-util.h"
29 #include "bus-error.h"
31 //#include "cgroup-show.h"
32 #include "cgroup-util.h"
34 //#include "logs-show.h"
37 #include "parse-util.h"
38 #include "process-util.h"
39 #include "signal-util.h"
40 #include "spawn-polkit-agent.h"
42 #include "sysfs-show.h"
43 #include "terminal-util.h"
44 #include "unit-name.h"
45 #include "user-util.h"
49 /// Additional includes for elogind
50 #include "logind-action.h"
51 #include "musl_missing.h"
55 static char **arg_property = NULL;
56 static bool arg_all = false;
57 static bool arg_full = false;
58 static bool arg_no_pager = false;
59 static bool arg_legend = true;
60 static const char *arg_kill_who = NULL;
61 static int arg_signal = SIGTERM;
62 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
63 static char *arg_host = NULL;
64 static bool arg_ask_password = true;
65 static bool arg_ignore_inhibitors = false;
67 static unsigned arg_lines = 10;
68 static OutputMode arg_output = OUTPUT_SHORT;
77 ACTION_CANCEL_SHUTDOWN,
82 static void pager_open_if_enabled(void) {
90 static void polkit_agent_open_if_enabled(void) {
92 /* Open the polkit agent as a child process if necessary */
94 if (!arg_ask_password)
97 if (arg_transport != BUS_TRANSPORT_LOCAL)
103 #if 0 /// UNNEEDED by elogind
104 static OutputFlags get_output_flags(void) {
107 arg_all * OUTPUT_SHOW_ALL |
108 arg_full * OUTPUT_FULL_WIDTH |
109 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
110 colors_enabled() * OUTPUT_COLOR;
114 static int list_sessions(int argc, char *argv[], void *userdata) {
115 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
116 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
117 const char *id, *user, *seat, *object;
118 sd_bus *bus = userdata;
126 pager_open_if_enabled();
128 r = sd_bus_call_method(
130 "org.freedesktop.login1",
131 "/org/freedesktop/login1",
132 "org.freedesktop.login1.Manager",
137 log_error("Failed to list sessions: %s", bus_error_message(&error, r));
141 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
143 return bus_log_parse_error(r);
146 printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
148 while ((r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object)) > 0) {
149 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
153 return bus_log_parse_error(r);
156 printf("\n%u sessions listed.\n", k);
161 static int list_users(int argc, char *argv[], void *userdata) {
162 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
163 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
164 const char *user, *object;
165 sd_bus *bus = userdata;
173 pager_open_if_enabled();
175 r = sd_bus_call_method(
177 "org.freedesktop.login1",
178 "/org/freedesktop/login1",
179 "org.freedesktop.login1.Manager",
184 log_error("Failed to list users: %s", bus_error_message(&error, r));
188 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
190 return bus_log_parse_error(r);
193 printf("%10s %-16s\n", "UID", "USER");
195 while ((r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object)) > 0) {
196 printf("%10u %-16s\n", (unsigned) uid, user);
200 return bus_log_parse_error(r);
203 printf("\n%u users listed.\n", k);
208 static int list_seats(int argc, char *argv[], void *userdata) {
209 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
210 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
211 const char *seat, *object;
212 sd_bus *bus = userdata;
219 pager_open_if_enabled();
221 r = sd_bus_call_method(
223 "org.freedesktop.login1",
224 "/org/freedesktop/login1",
225 "org.freedesktop.login1.Manager",
230 log_error("Failed to list seats: %s", bus_error_message(&error, r));
234 r = sd_bus_message_enter_container(reply, 'a', "(so)");
236 return bus_log_parse_error(r);
239 printf("%-16s\n", "SEAT");
241 while ((r = sd_bus_message_read(reply, "(so)", &seat, &object)) > 0) {
242 printf("%-16s\n", seat);
246 return bus_log_parse_error(r);
249 printf("\n%u seats listed.\n", k);
254 #if 0 /// UNNEEDED by elogind
255 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
256 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
257 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
258 _cleanup_free_ char *path = NULL;
266 if (arg_transport != BUS_TRANSPORT_LOCAL)
269 path = unit_dbus_path_from_name(unit);
273 r = sd_bus_get_property(
275 "org.freedesktop.systemd1",
279 &error, &reply, "s");
283 r = sd_bus_message_read(reply, "s", &cgroup);
290 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
299 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, get_output_flags());
304 typedef struct SessionStatusInfo {
308 struct dual_timestamp timestamp;
325 typedef struct UserStatusInfo {
328 struct dual_timestamp timestamp;
335 typedef struct SeatStatusInfo {
337 char *active_session;
341 static void session_status_info_clear(SessionStatusInfo *info) {
348 free(info->remote_host);
349 free(info->remote_user);
360 static void user_status_info_clear(UserStatusInfo *info) {
364 strv_free(info->sessions);
371 static void seat_status_info_clear(SeatStatusInfo *info) {
374 free(info->active_session);
375 strv_free(info->sessions);
380 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
381 const char *contents;
384 r = sd_bus_message_peek_type(m, NULL, &contents);
388 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
392 if (contents[0] == 's' || contents[0] == 'o') {
394 char **p = (char **) userdata;
396 r = sd_bus_message_read_basic(m, contents[0], &s);
400 r = free_and_strdup(p, s);
404 r = sd_bus_message_read_basic(m, contents[0], userdata);
409 r = sd_bus_message_skip(m, contents+1);
413 r = sd_bus_message_exit_container(m);
420 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
427 r = sd_bus_message_enter_container(m, 'a', "(so)");
431 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
432 r = strv_extend(userdata, name);
439 return sd_bus_message_exit_container(m);
442 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
444 static const struct bus_properties_map map[] = {
445 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
446 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
447 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
448 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
449 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
450 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
451 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
452 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
453 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
454 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
455 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
456 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
457 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
458 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
459 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
460 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
461 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
462 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
463 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
467 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
468 char since2[FORMAT_TIMESTAMP_MAX], *s2;
469 _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
472 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
474 return log_error_errno(r, "Could not get properties: %m");
481 printf("%s - ", strna(i.id));
484 printf("%s (%u)\n", i.name, (unsigned) i.uid);
486 printf("%u\n", (unsigned) i.uid);
488 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
489 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
492 printf("\t Since: %s; %s\n", s2, s1);
494 printf("\t Since: %s\n", s2);
497 _cleanup_free_ char *t = NULL;
499 printf("\t Leader: %u", (unsigned) i.leader);
501 get_process_comm(i.leader, &t);
508 if (!isempty(i.seat)) {
509 printf("\t Seat: %s", i.seat);
512 printf("; vc%u", i.vtnr);
518 printf("\t TTY: %s\n", i.tty);
520 printf("\t Display: %s\n", i.display);
522 if (i.remote_host && i.remote_user)
523 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
524 else if (i.remote_host)
525 printf("\t Remote: %s\n", i.remote_host);
526 else if (i.remote_user)
527 printf("\t Remote: user %s\n", i.remote_user);
529 printf("\t Remote: Yes\n");
532 printf("\t Service: %s", i.service);
535 printf("; type %s", i.type);
538 printf("; class %s", i.class);
542 printf("\t Type: %s", i.type);
545 printf("; class %s", i.class);
549 printf("\t Class: %s\n", i.class);
551 if (!isempty(i.desktop))
552 printf("\t Desktop: %s\n", i.desktop);
555 printf("\t State: %s\n", i.state);
558 printf("\t Unit: %s\n", i.scope);
560 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
561 if (arg_transport == BUS_TRANSPORT_LOCAL) {
563 show_journal_by_unit(
568 i.timestamp.monotonic,
571 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
572 SD_JOURNAL_LOCAL_ONLY,
583 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
585 static const struct bus_properties_map map[] = {
586 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
587 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
588 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
589 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
590 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
591 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
592 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
593 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
597 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
598 char since2[FORMAT_TIMESTAMP_MAX], *s2;
599 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
602 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
604 return log_error_errno(r, "Could not get properties: %m");
612 printf("%s (%u)\n", i.name, (unsigned) i.uid);
614 printf("%u\n", (unsigned) i.uid);
616 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
617 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
620 printf("\t Since: %s; %s\n", s2, s1);
622 printf("\t Since: %s\n", s2);
624 if (!isempty(i.state))
625 printf("\t State: %s\n", i.state);
627 if (!strv_isempty(i.sessions)) {
629 printf("\tSessions:");
631 STRV_FOREACH(l, i.sessions) {
632 if (streq_ptr(*l, i.display))
642 printf("\t Unit: %s\n", i.slice);
644 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
645 show_journal_by_unit(
650 i.timestamp.monotonic,
653 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
654 SD_JOURNAL_LOCAL_ONLY,
664 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
666 static const struct bus_properties_map map[] = {
667 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
668 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
669 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
673 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
676 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
678 return log_error_errno(r, "Could not get properties: %m");
685 printf("%s\n", strna(i.id));
687 if (!strv_isempty(i.sessions)) {
689 printf("\tSessions:");
691 STRV_FOREACH(l, i.sessions) {
692 if (streq_ptr(*l, i.active_session))
701 if (arg_transport == BUS_TRANSPORT_LOCAL) {
710 printf("\t Devices:\n");
712 show_sysfs(i.id, "\t\t ", c);
718 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
725 if (arg_property && !strv_find(arg_property, name))
726 /* skip what we didn't read */
727 return sd_bus_message_skip(m, contents);
729 switch (contents[0]) {
731 case SD_BUS_TYPE_STRUCT_BEGIN:
733 if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
736 r = sd_bus_message_read(m, "(so)", &s, NULL);
738 return bus_log_parse_error(r);
740 if (arg_all || !isempty(s))
741 printf("%s=%s\n", name, s);
745 } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
748 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
750 return bus_log_parse_error(r);
752 if (!uid_is_valid(uid)) {
753 log_error("Invalid user ID: " UID_FMT, uid);
757 printf("%s=" UID_FMT "\n", name, uid);
764 case SD_BUS_TYPE_ARRAY:
766 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
770 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
772 return bus_log_parse_error(r);
776 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
777 printf("%s%s", space ? " " : "", s);
784 return bus_log_parse_error(r);
786 r = sd_bus_message_exit_container(m);
788 return bus_log_parse_error(r);
796 r = bus_print_property(name, m, arg_all);
798 return bus_log_parse_error(r);
801 r = sd_bus_message_skip(m, contents);
803 return bus_log_parse_error(r);
806 printf("%s=[unprintable]\n", name);
812 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
813 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
814 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
821 r = sd_bus_call_method(
823 "org.freedesktop.login1",
825 "org.freedesktop.DBus.Properties",
831 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
833 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
835 return bus_log_parse_error(r);
842 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
843 const char *name, *contents;
845 r = sd_bus_message_read(reply, "s", &name);
847 return bus_log_parse_error(r);
849 r = sd_bus_message_peek_type(reply, NULL, &contents);
851 return bus_log_parse_error(r);
853 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
855 return bus_log_parse_error(r);
857 r = print_property(name, reply, contents);
861 r = sd_bus_message_exit_container(reply);
863 return bus_log_parse_error(r);
865 r = sd_bus_message_exit_container(reply);
867 return bus_log_parse_error(r);
870 return bus_log_parse_error(r);
872 r = sd_bus_message_exit_container(reply);
874 return bus_log_parse_error(r);
879 static int show_session(int argc, char *argv[], void *userdata) {
880 bool properties, new_line = false;
881 sd_bus *bus = userdata;
887 properties = !strstr(argv[0], "status");
889 pager_open_if_enabled();
892 /* If not argument is specified inspect the manager
895 return show_properties(bus, "/org/freedesktop/login1", &new_line);
897 /* And in the pretty case, show data of the calling session */
898 return print_session_status_info(bus, "/org/freedesktop/login1/session/self", &new_line);
901 for (i = 1; i < argc; i++) {
902 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
903 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
904 const char *path = NULL;
906 r = sd_bus_call_method(
908 "org.freedesktop.login1",
909 "/org/freedesktop/login1",
910 "org.freedesktop.login1.Manager",
915 log_error("Failed to get session: %s", bus_error_message(&error, r));
919 r = sd_bus_message_read(reply, "o", &path);
921 return bus_log_parse_error(r);
924 r = show_properties(bus, path, &new_line);
926 r = print_session_status_info(bus, path, &new_line);
935 static int show_user(int argc, char *argv[], void *userdata) {
936 bool properties, new_line = false;
937 sd_bus *bus = userdata;
943 properties = !strstr(argv[0], "status");
945 pager_open_if_enabled();
948 /* If not argument is specified inspect the manager
951 return show_properties(bus, "/org/freedesktop/login1", &new_line);
953 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
956 for (i = 1; i < argc; i++) {
957 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
958 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
959 const char *path = NULL;
962 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
964 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
966 r = sd_bus_call_method(
968 "org.freedesktop.login1",
969 "/org/freedesktop/login1",
970 "org.freedesktop.login1.Manager",
973 "u", (uint32_t) uid);
975 log_error("Failed to get user: %s", bus_error_message(&error, r));
979 r = sd_bus_message_read(reply, "o", &path);
981 return bus_log_parse_error(r);
984 r = show_properties(bus, path, &new_line);
986 r = print_user_status_info(bus, path, &new_line);
995 static int show_seat(int argc, char *argv[], void *userdata) {
996 bool properties, new_line = false;
997 sd_bus *bus = userdata;
1003 properties = !strstr(argv[0], "status");
1005 pager_open_if_enabled();
1008 /* If not argument is specified inspect the manager
1011 return show_properties(bus, "/org/freedesktop/login1", &new_line);
1013 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
1016 for (i = 1; i < argc; i++) {
1017 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1018 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1019 const char *path = NULL;
1021 r = sd_bus_call_method(
1023 "org.freedesktop.login1",
1024 "/org/freedesktop/login1",
1025 "org.freedesktop.login1.Manager",
1030 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1034 r = sd_bus_message_read(reply, "o", &path);
1036 return bus_log_parse_error(r);
1039 r = show_properties(bus, path, &new_line);
1041 r = print_seat_status_info(bus, path, &new_line);
1050 static int activate(int argc, char *argv[], void *userdata) {
1051 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1052 sd_bus *bus = userdata;
1053 char *short_argv[3];
1059 polkit_agent_open_if_enabled();
1062 /* No argument? Let's convert this into the empty
1063 * session name, which the calls will then resolve to
1064 * the caller's session. */
1066 short_argv[0] = argv[0];
1067 short_argv[1] = (char*) "";
1068 short_argv[2] = NULL;
1074 for (i = 1; i < argc; i++) {
1076 r = sd_bus_call_method(
1078 "org.freedesktop.login1",
1079 "/org/freedesktop/login1",
1080 "org.freedesktop.login1.Manager",
1081 streq(argv[0], "lock-session") ? "LockSession" :
1082 streq(argv[0], "unlock-session") ? "UnlockSession" :
1083 streq(argv[0], "terminate-session") ? "TerminateSession" :
1088 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1096 static int kill_session(int argc, char *argv[], void *userdata) {
1097 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1098 sd_bus *bus = userdata;
1104 polkit_agent_open_if_enabled();
1107 arg_kill_who = "all";
1109 for (i = 1; i < argc; i++) {
1111 r = sd_bus_call_method(
1113 "org.freedesktop.login1",
1114 "/org/freedesktop/login1",
1115 "org.freedesktop.login1.Manager",
1118 "ssi", argv[i], arg_kill_who, arg_signal);
1120 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1128 static int enable_linger(int argc, char *argv[], void *userdata) {
1129 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1130 sd_bus *bus = userdata;
1131 char* short_argv[3];
1138 polkit_agent_open_if_enabled();
1140 b = streq(argv[0], "enable-linger");
1143 short_argv[0] = argv[0];
1144 short_argv[1] = (char*) "";
1145 short_argv[2] = NULL;
1150 for (i = 1; i < argc; i++) {
1153 if (isempty(argv[i]))
1156 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1158 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1161 r = sd_bus_call_method(
1163 "org.freedesktop.login1",
1164 "/org/freedesktop/login1",
1165 "org.freedesktop.login1.Manager",
1168 "ubb", (uint32_t) uid, b, true);
1170 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1178 static int terminate_user(int argc, char *argv[], void *userdata) {
1179 _cleanup_(sd_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++) {
1191 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1193 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1195 r = sd_bus_call_method(
1197 "org.freedesktop.login1",
1198 "/org/freedesktop/login1",
1199 "org.freedesktop.login1.Manager",
1202 "u", (uint32_t) uid);
1204 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1212 static int kill_user(int argc, char *argv[], void *userdata) {
1213 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1214 sd_bus *bus = userdata;
1220 polkit_agent_open_if_enabled();
1223 arg_kill_who = "all";
1225 for (i = 1; i < argc; i++) {
1228 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1230 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1232 r = sd_bus_call_method(
1234 "org.freedesktop.login1",
1235 "/org/freedesktop/login1",
1236 "org.freedesktop.login1.Manager",
1239 "ui", (uint32_t) uid, arg_signal);
1241 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1249 static int attach(int argc, char *argv[], void *userdata) {
1250 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1251 sd_bus *bus = userdata;
1257 polkit_agent_open_if_enabled();
1259 for (i = 2; i < argc; i++) {
1261 r = sd_bus_call_method(
1263 "org.freedesktop.login1",
1264 "/org/freedesktop/login1",
1265 "org.freedesktop.login1.Manager",
1268 "ssb", argv[1], argv[i], true);
1271 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1279 static int flush_devices(int argc, char *argv[], void *userdata) {
1280 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1281 sd_bus *bus = userdata;
1287 polkit_agent_open_if_enabled();
1289 r = sd_bus_call_method(
1291 "org.freedesktop.login1",
1292 "/org/freedesktop/login1",
1293 "org.freedesktop.login1.Manager",
1298 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1303 static int lock_sessions(int argc, char *argv[], void *userdata) {
1304 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1305 sd_bus *bus = userdata;
1311 polkit_agent_open_if_enabled();
1313 r = sd_bus_call_method(
1315 "org.freedesktop.login1",
1316 "/org/freedesktop/login1",
1317 "org.freedesktop.login1.Manager",
1318 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1322 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1327 static int terminate_seat(int argc, char *argv[], void *userdata) {
1328 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1329 sd_bus *bus = userdata;
1335 polkit_agent_open_if_enabled();
1337 for (i = 1; i < argc; i++) {
1339 r = sd_bus_call_method(
1341 "org.freedesktop.login1",
1342 "/org/freedesktop/login1",
1343 "org.freedesktop.login1.Manager",
1348 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1356 /* Ask elogind, which might grant access to unprivileged users
1357 * through PolicyKit */
1358 static int reboot_with_logind(sd_bus *bus, enum action a) {
1359 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1362 static const char *table[_ACTION_MAX] = {
1363 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
1364 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
1365 [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
1371 polkit_agent_open_if_enabled();
1375 case ACTION_POWEROFF:
1376 method = "PowerOff";
1383 case ACTION_SUSPEND:
1387 case ACTION_HIBERNATE:
1388 method = "Hibernate";
1391 case ACTION_HYBRID_SLEEP:
1392 method = "HybridSleep";
1395 case ACTION_CANCEL_SHUTDOWN:
1396 method = "CancelScheduledShutdown";
1404 r = sd_bus_call_method(
1406 "org.freedesktop.login1",
1407 "/org/freedesktop/login1",
1408 "org.freedesktop.login1.Manager",
1417 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
1418 bus_error_message(&error, r));
1419 sd_bus_error_free(&error);
1424 r = sd_bus_call_method(
1426 "org.freedesktop.login1",
1427 "/org/freedesktop/login1",
1428 "org.freedesktop.login1.Manager",
1432 "b", arg_ask_password);
1434 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
1439 static const struct {
1440 HandleAction action;
1442 } action_table[_ACTION_MAX] = {
1443 [ACTION_POWEROFF] = { HANDLE_POWEROFF, "poweroff", },
1444 [ACTION_REBOOT] = { HANDLE_REBOOT, "reboot", },
1445 [ACTION_SUSPEND] = { HANDLE_SUSPEND, "suspend", },
1446 [ACTION_HIBERNATE] = { HANDLE_HIBERNATE, "hibernate", },
1447 [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
1450 static enum action verb_to_action(const char *verb) {
1453 for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
1454 if (streq_ptr(action_table[i].verb, verb))
1457 return _ACTION_INVALID;
1460 static int check_inhibitors(sd_bus *bus, enum action a) {
1461 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1462 _cleanup_strv_free_ char **sessions = NULL;
1463 const char *what, *who, *why, *mode;
1472 if (arg_ignore_inhibitors)
1481 r = sd_bus_call_method(
1483 "org.freedesktop.login1",
1484 "/org/freedesktop/login1",
1485 "org.freedesktop.login1.Manager",
1491 /* If logind is not around, then there are no inhibitors... */
1494 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
1496 return bus_log_parse_error(r);
1498 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
1499 _cleanup_free_ char *comm = NULL, *user = NULL;
1500 _cleanup_strv_free_ char **sv = NULL;
1502 if (!streq(mode, "block"))
1505 sv = strv_split(what, ":");
1509 if ((pid_t) pid < 0)
1510 return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
1512 if (!strv_contains(sv,
1513 a == ACTION_POWEROFF ||
1514 a == ACTION_REBOOT ? "shutdown" : "sleep"))
1517 get_process_comm(pid, &comm);
1518 user = uid_to_name(uid);
1520 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
1521 who, (pid_t) pid, strna(comm), strna(user), why);
1526 return bus_log_parse_error(r);
1528 r = sd_bus_message_exit_container(reply);
1530 return bus_log_parse_error(r);
1532 /* Check for current sessions */
1533 sd_get_sessions(&sessions);
1534 STRV_FOREACH(s, sessions) {
1535 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
1537 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
1540 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
1543 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
1546 sd_session_get_tty(*s, &tty);
1547 sd_session_get_seat(*s, &seat);
1548 sd_session_get_service(*s, &service);
1549 user = uid_to_name(uid);
1551 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
1558 log_error("Please retry operation after closing inhibitors and logging out other users.\n"
1559 "Alternatively, ignore inhibitors and users with 'loginctl %s -i'.",
1560 action_table[a].verb);
1565 static int start_special(int argc, char *argv[], void *userdata) {
1566 sd_bus *bus = userdata;
1572 a = verb_to_action(argv[0]);
1574 r = check_inhibitors(bus, a);
1578 /* Now power off actions in chroot environments */
1579 if ((a == ACTION_POWEROFF ||
1580 a == ACTION_REBOOT) &&
1581 (running_in_chroot() > 0) ) {
1582 log_info("Running in chroot, ignoring request.");
1586 /* Switch to cancel shutdown, if a shutdown action was requested,
1587 and the option to cancel it was set: */
1588 if ((a == ACTION_POWEROFF ||
1589 a == ACTION_REBOOT) &&
1590 (arg_action == ACTION_CANCEL_SHUTDOWN))
1591 return reboot_with_logind(bus, arg_action);
1593 /* Otherwise perform requested action */
1594 if (a == ACTION_POWEROFF ||
1595 a == ACTION_REBOOT ||
1596 a == ACTION_SUSPEND ||
1597 a == ACTION_HIBERNATE ||
1598 a == ACTION_HYBRID_SLEEP)
1599 return reboot_with_logind(bus, a);
1604 static int help(int argc, char *argv[], void *userdata) {
1606 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1607 "Send control commands to or query the login manager.\n\n"
1608 " -h --help Show this help\n"
1609 " --version Show package version\n"
1610 " --no-pager Do not pipe output into a pager\n"
1611 " --no-legend Do not show the headers and footers\n"
1612 " --no-ask-password Don't prompt for password\n"
1613 " -H --host=[USER@]HOST Operate on remote host\n"
1614 " -M --machine=CONTAINER Operate on local container\n"
1615 " -p --property=NAME Show only properties by this name\n"
1616 " -a --all Show all properties, including empty ones\n"
1617 " -l --full Do not ellipsize output\n"
1618 " --kill-who=WHO Who to send signal to\n"
1619 " -s --signal=SIGNAL Which signal to send\n"
1621 " -n --lines=INTEGER Number of journal entries to show\n"
1622 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1623 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1625 " -c Cancel a pending shutdown\n"
1626 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n"
1627 "Session Commands:\n"
1628 " list-sessions List sessions\n"
1629 " session-status [ID...] Show session status\n"
1630 " show-session [ID...] Show properties of sessions or the manager\n"
1631 " activate [ID] Activate a session\n"
1632 " lock-session [ID...] Screen lock one or more sessions\n"
1633 " unlock-session [ID...] Screen unlock one or more sessions\n"
1634 " lock-sessions Screen lock all current sessions\n"
1635 " unlock-sessions Screen unlock all current sessions\n"
1636 " terminate-session ID... Terminate one or more sessions\n"
1637 " kill-session ID... Send signal to processes of a session\n\n"
1639 " list-users List users\n"
1640 " user-status [USER...] Show user status\n"
1641 " show-user [USER...] Show properties of users or the manager\n"
1642 " enable-linger [USER...] Enable linger state of one or more users\n"
1643 " disable-linger [USER...] Disable linger state of one or more users\n"
1644 " terminate-user USER... Terminate all sessions of one or more users\n"
1645 " kill-user USER... Send signal to processes of a user\n\n"
1647 " list-seats List seats\n"
1648 " seat-status [NAME...] Show seat status\n"
1649 " show-seat [NAME...] Show properties of seats or the manager\n"
1650 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1651 " flush-devices Flush all device associations\n"
1652 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1653 "System Commands:\n"
1654 " poweroff Turn off the machine\n"
1655 " reboot Reboot the machine\n"
1656 " suspend Suspend the machine to memory\n"
1657 " hibernate Suspend the machine to disk\n"
1658 " hybrid-sleep Suspend the machine to memory and disk\n"
1659 , program_invocation_short_name);
1664 static int parse_argv(int argc, char *argv[]) {
1667 ARG_VERSION = 0x100,
1671 ARG_NO_ASK_PASSWORD,
1674 static const struct option options[] = {
1675 { "help", no_argument, NULL, 'h' },
1676 { "version", no_argument, NULL, ARG_VERSION },
1677 { "property", required_argument, NULL, 'p' },
1678 { "all", no_argument, NULL, 'a' },
1679 { "full", no_argument, NULL, 'l' },
1680 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1681 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1682 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1683 { "signal", required_argument, NULL, 's' },
1684 { "host", required_argument, NULL, 'H' },
1685 { "machine", required_argument, NULL, 'M' },
1686 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1688 { "lines", required_argument, NULL, 'n' },
1689 { "output", required_argument, NULL, 'o' },
1691 { "ignore-inhibitors", no_argument, NULL, 'i' },
1700 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1705 help(0, NULL, NULL);
1712 r = strv_extend(&arg_property, optarg);
1716 /* If the user asked for a particular
1717 * property, show it to him, even if it is
1732 if (safe_atou(optarg, &arg_lines) < 0) {
1733 log_error("Failed to parse lines '%s'", optarg);
1739 arg_output = output_mode_from_string(optarg);
1740 if (arg_output < 0) {
1741 log_error("Unknown output '%s'.", optarg);
1747 arg_no_pager = true;
1754 case ARG_NO_ASK_PASSWORD:
1755 arg_ask_password = false;
1759 arg_kill_who = optarg;
1763 arg_signal = signal_from_string_try_harder(optarg);
1764 if (arg_signal < 0) {
1765 log_error("Failed to parse signal string %s.", optarg);
1771 arg_transport = BUS_TRANSPORT_REMOTE;
1776 arg_transport = BUS_TRANSPORT_MACHINE;
1781 arg_action = ACTION_CANCEL_SHUTDOWN;
1785 arg_ignore_inhibitors = true;
1792 assert_not_reached("Unhandled option");
1798 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1800 static const Verb verbs[] = {
1801 { "help", VERB_ANY, VERB_ANY, 0, help },
1802 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1803 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1804 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1805 { "activate", VERB_ANY, 2, 0, activate },
1806 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1807 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1808 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1809 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1810 { "terminate-session", 2, VERB_ANY, 0, activate },
1811 { "kill-session", 2, VERB_ANY, 0, kill_session },
1812 { "list-users", VERB_ANY, 1, 0, list_users },
1813 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1814 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1815 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1816 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1817 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1818 { "kill-user", 2, VERB_ANY, 0, kill_user },
1819 { "list-seats", VERB_ANY, 1, 0, list_seats },
1820 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1821 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1822 { "attach", 3, VERB_ANY, 0, attach },
1823 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1824 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1825 { "poweroff", VERB_ANY, 1, 0, start_special },
1826 { "reboot", VERB_ANY, 1, 0, start_special },
1827 { "suspend", VERB_ANY, 1, 0, start_special },
1828 { "hibernate", VERB_ANY, 1, 0, start_special },
1829 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1830 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1834 return dispatch_verb(argc, argv, verbs, bus);
1837 int main(int argc, char *argv[]) {
1838 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1841 setlocale(LC_ALL, "");
1842 elogind_set_program_name(argv[0]);
1843 log_parse_environment();
1846 r = parse_argv(argc, argv);
1850 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1852 log_error_errno(r, "Failed to create bus connection: %m");
1856 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1858 r = loginctl_main(argc, argv, bus);
1862 polkit_agent_close();
1864 strv_free(arg_property);
1866 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;