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_value = false;
58 static bool arg_full = false;
59 static bool arg_no_pager = false;
60 static bool arg_legend = true;
61 static const char *arg_kill_who = NULL;
62 static int arg_signal = SIGTERM;
63 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
64 static char *arg_host = NULL;
65 static bool arg_ask_password = true;
66 static bool arg_ignore_inhibitors = false;
68 static unsigned arg_lines = 10;
69 static OutputMode arg_output = OUTPUT_SHORT;
78 ACTION_CANCEL_SHUTDOWN,
83 static void pager_open_if_enabled(void) {
91 static void polkit_agent_open_if_enabled(void) {
93 /* Open the polkit agent as a child process if necessary */
95 if (!arg_ask_password)
98 if (arg_transport != BUS_TRANSPORT_LOCAL)
104 #if 0 /// UNNEEDED by elogind
105 static OutputFlags get_output_flags(void) {
108 arg_all * OUTPUT_SHOW_ALL |
109 arg_full * OUTPUT_FULL_WIDTH |
110 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
111 colors_enabled() * OUTPUT_COLOR;
115 static int list_sessions(int argc, char *argv[], void *userdata) {
116 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
117 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
118 const char *id, *user, *seat, *object;
119 sd_bus *bus = userdata;
127 pager_open_if_enabled();
129 r = sd_bus_call_method(
131 "org.freedesktop.login1",
132 "/org/freedesktop/login1",
133 "org.freedesktop.login1.Manager",
138 log_error("Failed to list sessions: %s", bus_error_message(&error, r));
142 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
144 return bus_log_parse_error(r);
147 printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
149 while ((r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object)) > 0) {
150 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
154 return bus_log_parse_error(r);
157 printf("\n%u sessions listed.\n", k);
162 static int list_users(int argc, char *argv[], void *userdata) {
163 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
164 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
165 const char *user, *object;
166 sd_bus *bus = userdata;
174 pager_open_if_enabled();
176 r = sd_bus_call_method(
178 "org.freedesktop.login1",
179 "/org/freedesktop/login1",
180 "org.freedesktop.login1.Manager",
185 log_error("Failed to list users: %s", bus_error_message(&error, r));
189 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
191 return bus_log_parse_error(r);
194 printf("%10s %-16s\n", "UID", "USER");
196 while ((r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object)) > 0) {
197 printf("%10u %-16s\n", (unsigned) uid, user);
201 return bus_log_parse_error(r);
204 printf("\n%u users listed.\n", k);
209 static int list_seats(int argc, char *argv[], void *userdata) {
210 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
211 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
212 const char *seat, *object;
213 sd_bus *bus = userdata;
220 pager_open_if_enabled();
222 r = sd_bus_call_method(
224 "org.freedesktop.login1",
225 "/org/freedesktop/login1",
226 "org.freedesktop.login1.Manager",
231 log_error("Failed to list seats: %s", bus_error_message(&error, r));
235 r = sd_bus_message_enter_container(reply, 'a', "(so)");
237 return bus_log_parse_error(r);
240 printf("%-16s\n", "SEAT");
242 while ((r = sd_bus_message_read(reply, "(so)", &seat, &object)) > 0) {
243 printf("%-16s\n", seat);
247 return bus_log_parse_error(r);
250 printf("\n%u seats listed.\n", k);
255 #if 0 /// UNNEEDED by elogind
256 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
257 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
258 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
259 _cleanup_free_ char *path = NULL;
267 if (arg_transport != BUS_TRANSPORT_LOCAL)
270 path = unit_dbus_path_from_name(unit);
274 r = sd_bus_get_property(
276 "org.freedesktop.systemd1",
280 &error, &reply, "s");
284 r = sd_bus_message_read(reply, "s", &cgroup);
291 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
300 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, false, &leader, leader > 0, get_output_flags());
305 typedef struct SessionStatusInfo {
309 struct dual_timestamp timestamp;
326 typedef struct UserStatusInfo {
329 struct dual_timestamp timestamp;
336 typedef struct SeatStatusInfo {
338 char *active_session;
342 static void session_status_info_clear(SessionStatusInfo *info) {
349 free(info->remote_host);
350 free(info->remote_user);
361 static void user_status_info_clear(UserStatusInfo *info) {
365 strv_free(info->sessions);
372 static void seat_status_info_clear(SeatStatusInfo *info) {
375 free(info->active_session);
376 strv_free(info->sessions);
381 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
382 const char *contents;
385 r = sd_bus_message_peek_type(m, NULL, &contents);
389 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
393 if (contents[0] == 's' || contents[0] == 'o') {
395 char **p = (char **) userdata;
397 r = sd_bus_message_read_basic(m, contents[0], &s);
401 r = free_and_strdup(p, s);
405 r = sd_bus_message_read_basic(m, contents[0], userdata);
410 r = sd_bus_message_skip(m, contents+1);
414 r = sd_bus_message_exit_container(m);
421 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
428 r = sd_bus_message_enter_container(m, 'a', "(so)");
432 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
433 r = strv_extend(userdata, name);
440 return sd_bus_message_exit_container(m);
443 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
445 static const struct bus_properties_map map[] = {
446 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
447 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
448 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
449 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
450 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
451 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
452 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
453 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
454 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
455 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
456 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
457 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
458 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
459 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
460 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
461 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
462 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
463 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
464 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
468 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
469 char since2[FORMAT_TIMESTAMP_MAX], *s2;
470 _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
473 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
475 return log_error_errno(r, "Could not get properties: %m");
482 printf("%s - ", strna(i.id));
485 printf("%s (%u)\n", i.name, (unsigned) i.uid);
487 printf("%u\n", (unsigned) i.uid);
489 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
490 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
493 printf("\t Since: %s; %s\n", s2, s1);
495 printf("\t Since: %s\n", s2);
498 _cleanup_free_ char *t = NULL;
500 printf("\t Leader: %u", (unsigned) i.leader);
502 get_process_comm(i.leader, &t);
509 if (!isempty(i.seat)) {
510 printf("\t Seat: %s", i.seat);
513 printf("; vc%u", i.vtnr);
519 printf("\t TTY: %s\n", i.tty);
521 printf("\t Display: %s\n", i.display);
523 if (i.remote_host && i.remote_user)
524 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
525 else if (i.remote_host)
526 printf("\t Remote: %s\n", i.remote_host);
527 else if (i.remote_user)
528 printf("\t Remote: user %s\n", i.remote_user);
530 printf("\t Remote: Yes\n");
533 printf("\t Service: %s", i.service);
536 printf("; type %s", i.type);
539 printf("; class %s", i.class);
543 printf("\t Type: %s", i.type);
546 printf("; class %s", i.class);
550 printf("\t Class: %s\n", i.class);
552 if (!isempty(i.desktop))
553 printf("\t Desktop: %s\n", i.desktop);
556 printf("\t State: %s\n", i.state);
559 printf("\t Unit: %s\n", i.scope);
561 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
562 if (arg_transport == BUS_TRANSPORT_LOCAL) {
564 show_journal_by_unit(
569 i.timestamp.monotonic,
572 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
573 SD_JOURNAL_LOCAL_ONLY,
584 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
586 static const struct bus_properties_map map[] = {
587 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
588 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
589 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
590 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
591 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
592 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
593 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
594 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
598 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
599 char since2[FORMAT_TIMESTAMP_MAX], *s2;
600 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
603 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
605 return log_error_errno(r, "Could not get properties: %m");
613 printf("%s (%u)\n", i.name, (unsigned) i.uid);
615 printf("%u\n", (unsigned) i.uid);
617 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
618 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
621 printf("\t Since: %s; %s\n", s2, s1);
623 printf("\t Since: %s\n", s2);
625 if (!isempty(i.state))
626 printf("\t State: %s\n", i.state);
628 if (!strv_isempty(i.sessions)) {
630 printf("\tSessions:");
632 STRV_FOREACH(l, i.sessions) {
633 if (streq_ptr(*l, i.display))
643 printf("\t Unit: %s\n", i.slice);
645 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
646 show_journal_by_unit(
651 i.timestamp.monotonic,
654 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
655 SD_JOURNAL_LOCAL_ONLY,
665 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
667 static const struct bus_properties_map map[] = {
668 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
669 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
670 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
674 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
677 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
679 return log_error_errno(r, "Could not get properties: %m");
686 printf("%s\n", strna(i.id));
688 if (!strv_isempty(i.sessions)) {
690 printf("\tSessions:");
692 STRV_FOREACH(l, i.sessions) {
693 if (streq_ptr(*l, i.active_session))
702 if (arg_transport == BUS_TRANSPORT_LOCAL) {
711 printf("\t Devices:\n");
713 show_sysfs(i.id, "\t\t ", c);
719 #define property(name, fmt, ...) \
722 printf(fmt "\n", __VA_ARGS__); \
724 printf("%s=" fmt "\n", name, __VA_ARGS__); \
727 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
734 if (arg_property && !strv_find(arg_property, name))
735 /* skip what we didn't read */
736 return sd_bus_message_skip(m, contents);
738 switch (contents[0]) {
740 case SD_BUS_TYPE_STRUCT_BEGIN:
742 if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
745 r = sd_bus_message_read(m, "(so)", &s, NULL);
747 return bus_log_parse_error(r);
749 if (arg_all || !isempty(s))
750 property(name, "%s", s);
754 } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
757 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
759 return bus_log_parse_error(r);
761 if (!uid_is_valid(uid)) {
762 log_error("Invalid user ID: " UID_FMT, uid);
766 property(name, UID_FMT, uid);
772 case SD_BUS_TYPE_ARRAY:
774 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
778 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
780 return bus_log_parse_error(r);
785 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
786 printf("%s%s", space ? " " : "", s);
790 if (space || !arg_value)
794 return bus_log_parse_error(r);
796 r = sd_bus_message_exit_container(m);
798 return bus_log_parse_error(r);
806 r = bus_print_property(name, m, arg_value, arg_all);
808 return bus_log_parse_error(r);
811 r = sd_bus_message_skip(m, contents);
813 return bus_log_parse_error(r);
816 printf("%s=[unprintable]\n", name);
822 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
823 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
824 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
831 r = sd_bus_call_method(
833 "org.freedesktop.login1",
835 "org.freedesktop.DBus.Properties",
841 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
843 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
845 return bus_log_parse_error(r);
852 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
853 const char *name, *contents;
855 r = sd_bus_message_read(reply, "s", &name);
857 return bus_log_parse_error(r);
859 r = sd_bus_message_peek_type(reply, NULL, &contents);
861 return bus_log_parse_error(r);
863 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
865 return bus_log_parse_error(r);
867 r = print_property(name, reply, contents);
871 r = sd_bus_message_exit_container(reply);
873 return bus_log_parse_error(r);
875 r = sd_bus_message_exit_container(reply);
877 return bus_log_parse_error(r);
880 return bus_log_parse_error(r);
882 r = sd_bus_message_exit_container(reply);
884 return bus_log_parse_error(r);
889 static int show_session(int argc, char *argv[], void *userdata) {
890 bool properties, new_line = false;
891 sd_bus *bus = userdata;
897 properties = !strstr(argv[0], "status");
899 pager_open_if_enabled();
902 /* If not argument is specified inspect the manager
905 return show_properties(bus, "/org/freedesktop/login1", &new_line);
907 /* And in the pretty case, show data of the calling session */
908 return print_session_status_info(bus, "/org/freedesktop/login1/session/self", &new_line);
911 for (i = 1; i < argc; i++) {
912 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
913 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
914 const char *path = NULL;
916 r = sd_bus_call_method(
918 "org.freedesktop.login1",
919 "/org/freedesktop/login1",
920 "org.freedesktop.login1.Manager",
925 log_error("Failed to get session: %s", bus_error_message(&error, r));
929 r = sd_bus_message_read(reply, "o", &path);
931 return bus_log_parse_error(r);
934 r = show_properties(bus, path, &new_line);
936 r = print_session_status_info(bus, path, &new_line);
945 static int show_user(int argc, char *argv[], void *userdata) {
946 bool properties, new_line = false;
947 sd_bus *bus = userdata;
953 properties = !strstr(argv[0], "status");
955 pager_open_if_enabled();
958 /* If not argument is specified inspect the manager
961 return show_properties(bus, "/org/freedesktop/login1", &new_line);
963 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
966 for (i = 1; i < argc; i++) {
967 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
968 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
969 const char *path = NULL;
972 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
974 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
976 r = sd_bus_call_method(
978 "org.freedesktop.login1",
979 "/org/freedesktop/login1",
980 "org.freedesktop.login1.Manager",
983 "u", (uint32_t) uid);
985 log_error("Failed to get user: %s", bus_error_message(&error, r));
989 r = sd_bus_message_read(reply, "o", &path);
991 return bus_log_parse_error(r);
994 r = show_properties(bus, path, &new_line);
996 r = print_user_status_info(bus, path, &new_line);
1005 static int show_seat(int argc, char *argv[], void *userdata) {
1006 bool properties, new_line = false;
1007 sd_bus *bus = userdata;
1013 properties = !strstr(argv[0], "status");
1015 pager_open_if_enabled();
1018 /* If not argument is specified inspect the manager
1021 return show_properties(bus, "/org/freedesktop/login1", &new_line);
1023 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
1026 for (i = 1; i < argc; i++) {
1027 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1028 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1029 const char *path = NULL;
1031 r = sd_bus_call_method(
1033 "org.freedesktop.login1",
1034 "/org/freedesktop/login1",
1035 "org.freedesktop.login1.Manager",
1040 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1044 r = sd_bus_message_read(reply, "o", &path);
1046 return bus_log_parse_error(r);
1049 r = show_properties(bus, path, &new_line);
1051 r = print_seat_status_info(bus, path, &new_line);
1060 static int activate(int argc, char *argv[], void *userdata) {
1061 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1062 sd_bus *bus = userdata;
1063 char *short_argv[3];
1069 polkit_agent_open_if_enabled();
1072 /* No argument? Let's convert this into the empty
1073 * session name, which the calls will then resolve to
1074 * the caller's session. */
1076 short_argv[0] = argv[0];
1077 short_argv[1] = (char*) "";
1078 short_argv[2] = NULL;
1084 for (i = 1; i < argc; i++) {
1086 r = sd_bus_call_method(
1088 "org.freedesktop.login1",
1089 "/org/freedesktop/login1",
1090 "org.freedesktop.login1.Manager",
1091 streq(argv[0], "lock-session") ? "LockSession" :
1092 streq(argv[0], "unlock-session") ? "UnlockSession" :
1093 streq(argv[0], "terminate-session") ? "TerminateSession" :
1098 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1106 static int kill_session(int argc, char *argv[], void *userdata) {
1107 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1108 sd_bus *bus = userdata;
1114 polkit_agent_open_if_enabled();
1117 arg_kill_who = "all";
1119 for (i = 1; i < argc; i++) {
1121 r = sd_bus_call_method(
1123 "org.freedesktop.login1",
1124 "/org/freedesktop/login1",
1125 "org.freedesktop.login1.Manager",
1128 "ssi", argv[i], arg_kill_who, arg_signal);
1130 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1138 static int enable_linger(int argc, char *argv[], void *userdata) {
1139 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1140 sd_bus *bus = userdata;
1141 char* short_argv[3];
1148 polkit_agent_open_if_enabled();
1150 b = streq(argv[0], "enable-linger");
1153 short_argv[0] = argv[0];
1154 short_argv[1] = (char*) "";
1155 short_argv[2] = NULL;
1160 for (i = 1; i < argc; i++) {
1163 if (isempty(argv[i]))
1166 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1168 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1171 r = sd_bus_call_method(
1173 "org.freedesktop.login1",
1174 "/org/freedesktop/login1",
1175 "org.freedesktop.login1.Manager",
1178 "ubb", (uint32_t) uid, b, true);
1180 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1188 static int terminate_user(int argc, char *argv[], void *userdata) {
1189 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1190 sd_bus *bus = userdata;
1196 polkit_agent_open_if_enabled();
1198 for (i = 1; i < argc; i++) {
1201 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1203 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1205 r = sd_bus_call_method(
1207 "org.freedesktop.login1",
1208 "/org/freedesktop/login1",
1209 "org.freedesktop.login1.Manager",
1212 "u", (uint32_t) uid);
1214 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1222 static int kill_user(int argc, char *argv[], void *userdata) {
1223 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1224 sd_bus *bus = userdata;
1230 polkit_agent_open_if_enabled();
1233 arg_kill_who = "all";
1235 for (i = 1; i < argc; i++) {
1238 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1240 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1242 r = sd_bus_call_method(
1244 "org.freedesktop.login1",
1245 "/org/freedesktop/login1",
1246 "org.freedesktop.login1.Manager",
1249 "ui", (uint32_t) uid, arg_signal);
1251 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1259 static int attach(int argc, char *argv[], void *userdata) {
1260 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1261 sd_bus *bus = userdata;
1267 polkit_agent_open_if_enabled();
1269 for (i = 2; i < argc; i++) {
1271 r = sd_bus_call_method(
1273 "org.freedesktop.login1",
1274 "/org/freedesktop/login1",
1275 "org.freedesktop.login1.Manager",
1278 "ssb", argv[1], argv[i], true);
1281 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1289 static int flush_devices(int argc, char *argv[], void *userdata) {
1290 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1291 sd_bus *bus = userdata;
1297 polkit_agent_open_if_enabled();
1299 r = sd_bus_call_method(
1301 "org.freedesktop.login1",
1302 "/org/freedesktop/login1",
1303 "org.freedesktop.login1.Manager",
1308 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1313 static int lock_sessions(int argc, char *argv[], void *userdata) {
1314 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1315 sd_bus *bus = userdata;
1321 polkit_agent_open_if_enabled();
1323 r = sd_bus_call_method(
1325 "org.freedesktop.login1",
1326 "/org/freedesktop/login1",
1327 "org.freedesktop.login1.Manager",
1328 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1332 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1337 static int terminate_seat(int argc, char *argv[], void *userdata) {
1338 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1339 sd_bus *bus = userdata;
1345 polkit_agent_open_if_enabled();
1347 for (i = 1; i < argc; i++) {
1349 r = sd_bus_call_method(
1351 "org.freedesktop.login1",
1352 "/org/freedesktop/login1",
1353 "org.freedesktop.login1.Manager",
1358 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1366 /* Ask elogind, which might grant access to unprivileged users
1367 * through PolicyKit */
1368 static int reboot_with_logind(sd_bus *bus, enum action a) {
1369 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1372 static const char *table[_ACTION_MAX] = {
1373 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
1374 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
1375 [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
1381 polkit_agent_open_if_enabled();
1385 case ACTION_POWEROFF:
1386 method = "PowerOff";
1393 case ACTION_SUSPEND:
1397 case ACTION_HIBERNATE:
1398 method = "Hibernate";
1401 case ACTION_HYBRID_SLEEP:
1402 method = "HybridSleep";
1405 case ACTION_CANCEL_SHUTDOWN:
1406 method = "CancelScheduledShutdown";
1414 r = sd_bus_call_method(
1416 "org.freedesktop.login1",
1417 "/org/freedesktop/login1",
1418 "org.freedesktop.login1.Manager",
1427 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
1428 bus_error_message(&error, r));
1429 sd_bus_error_free(&error);
1434 r = sd_bus_call_method(
1436 "org.freedesktop.login1",
1437 "/org/freedesktop/login1",
1438 "org.freedesktop.login1.Manager",
1442 "b", arg_ask_password);
1444 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
1449 static const struct {
1450 HandleAction action;
1452 } action_table[_ACTION_MAX] = {
1453 [ACTION_POWEROFF] = { HANDLE_POWEROFF, "poweroff", },
1454 [ACTION_REBOOT] = { HANDLE_REBOOT, "reboot", },
1455 [ACTION_SUSPEND] = { HANDLE_SUSPEND, "suspend", },
1456 [ACTION_HIBERNATE] = { HANDLE_HIBERNATE, "hibernate", },
1457 [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
1460 static enum action verb_to_action(const char *verb) {
1463 for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
1464 if (streq_ptr(action_table[i].verb, verb))
1467 return _ACTION_INVALID;
1470 static int check_inhibitors(sd_bus *bus, enum action a) {
1471 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1472 _cleanup_strv_free_ char **sessions = NULL;
1473 const char *what, *who, *why, *mode;
1482 if (arg_ignore_inhibitors)
1491 r = sd_bus_call_method(
1493 "org.freedesktop.login1",
1494 "/org/freedesktop/login1",
1495 "org.freedesktop.login1.Manager",
1501 /* If logind is not around, then there are no inhibitors... */
1504 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
1506 return bus_log_parse_error(r);
1508 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
1509 _cleanup_free_ char *comm = NULL, *user = NULL;
1510 _cleanup_strv_free_ char **sv = NULL;
1512 if (!streq(mode, "block"))
1515 sv = strv_split(what, ":");
1519 if ((pid_t) pid < 0)
1520 return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
1522 if (!strv_contains(sv,
1523 a == ACTION_POWEROFF ||
1524 a == ACTION_REBOOT ? "shutdown" : "sleep"))
1527 get_process_comm(pid, &comm);
1528 user = uid_to_name(uid);
1530 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
1531 who, (pid_t) pid, strna(comm), strna(user), why);
1536 return bus_log_parse_error(r);
1538 r = sd_bus_message_exit_container(reply);
1540 return bus_log_parse_error(r);
1542 /* Check for current sessions */
1543 sd_get_sessions(&sessions);
1544 STRV_FOREACH(s, sessions) {
1545 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
1547 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
1550 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
1553 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
1556 sd_session_get_tty(*s, &tty);
1557 sd_session_get_seat(*s, &seat);
1558 sd_session_get_service(*s, &service);
1559 user = uid_to_name(uid);
1561 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
1568 log_error("Please retry operation after closing inhibitors and logging out other users.\n"
1569 "Alternatively, ignore inhibitors and users with 'loginctl %s -i'.",
1570 action_table[a].verb);
1575 static int start_special(int argc, char *argv[], void *userdata) {
1576 sd_bus *bus = userdata;
1582 a = verb_to_action(argv[0]);
1584 r = check_inhibitors(bus, a);
1588 /* Now power off actions in chroot environments */
1589 if ((a == ACTION_POWEROFF ||
1590 a == ACTION_REBOOT) &&
1591 (running_in_chroot() > 0) ) {
1592 log_info("Running in chroot, ignoring request.");
1596 /* Switch to cancel shutdown, if a shutdown action was requested,
1597 and the option to cancel it was set: */
1598 if ((a == ACTION_POWEROFF ||
1599 a == ACTION_REBOOT) &&
1600 (arg_action == ACTION_CANCEL_SHUTDOWN))
1601 return reboot_with_logind(bus, arg_action);
1603 /* Otherwise perform requested action */
1604 if (a == ACTION_POWEROFF ||
1605 a == ACTION_REBOOT ||
1606 a == ACTION_SUSPEND ||
1607 a == ACTION_HIBERNATE ||
1608 a == ACTION_HYBRID_SLEEP)
1609 return reboot_with_logind(bus, a);
1614 static int help(int argc, char *argv[], void *userdata) {
1616 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1617 "Send control commands to or query the login manager.\n\n"
1618 " -h --help Show this help\n"
1619 " --version Show package version\n"
1620 " --no-pager Do not pipe output into a pager\n"
1621 " --no-legend Do not show the headers and footers\n"
1622 " --no-ask-password Don't prompt for password\n"
1623 " -H --host=[USER@]HOST Operate on remote host\n"
1624 " -M --machine=CONTAINER Operate on local container\n"
1625 " -p --property=NAME Show only properties by this name\n"
1626 " -a --all Show all properties, including empty ones\n"
1627 " --value When showing properties, only print the value\n"
1628 " -l --full Do not ellipsize output\n"
1629 " --kill-who=WHO Who to send signal to\n"
1630 " -s --signal=SIGNAL Which signal to send\n"
1632 " -n --lines=INTEGER Number of journal entries to show\n"
1633 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1634 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1636 " -c Cancel a pending shutdown\n"
1637 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n"
1638 "Session Commands:\n"
1639 " list-sessions List sessions\n"
1640 " session-status [ID...] Show session status\n"
1641 " show-session [ID...] Show properties of sessions or the manager\n"
1642 " activate [ID] Activate a session\n"
1643 " lock-session [ID...] Screen lock one or more sessions\n"
1644 " unlock-session [ID...] Screen unlock one or more sessions\n"
1645 " lock-sessions Screen lock all current sessions\n"
1646 " unlock-sessions Screen unlock all current sessions\n"
1647 " terminate-session ID... Terminate one or more sessions\n"
1648 " kill-session ID... Send signal to processes of a session\n\n"
1650 " list-users List users\n"
1651 " user-status [USER...] Show user status\n"
1652 " show-user [USER...] Show properties of users or the manager\n"
1653 " enable-linger [USER...] Enable linger state of one or more users\n"
1654 " disable-linger [USER...] Disable linger state of one or more users\n"
1655 " terminate-user USER... Terminate all sessions of one or more users\n"
1656 " kill-user USER... Send signal to processes of a user\n\n"
1658 " list-seats List seats\n"
1659 " seat-status [NAME...] Show seat status\n"
1660 " show-seat [NAME...] Show properties of seats or the manager\n"
1661 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1662 " flush-devices Flush all device associations\n"
1663 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1664 "System Commands:\n"
1665 " poweroff Turn off the machine\n"
1666 " reboot Reboot the machine\n"
1667 " suspend Suspend the machine to memory\n"
1668 " hibernate Suspend the machine to disk\n"
1669 " hybrid-sleep Suspend the machine to memory and disk\n"
1670 , program_invocation_short_name);
1675 static int parse_argv(int argc, char *argv[]) {
1678 ARG_VERSION = 0x100,
1683 ARG_NO_ASK_PASSWORD,
1686 static const struct option options[] = {
1687 { "help", no_argument, NULL, 'h' },
1688 { "version", no_argument, NULL, ARG_VERSION },
1689 { "property", required_argument, NULL, 'p' },
1690 { "all", no_argument, NULL, 'a' },
1691 { "value", no_argument, NULL, ARG_VALUE },
1692 { "full", no_argument, NULL, 'l' },
1693 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1694 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1695 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1696 { "signal", required_argument, NULL, 's' },
1697 { "host", required_argument, NULL, 'H' },
1698 { "machine", required_argument, NULL, 'M' },
1699 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1701 { "lines", required_argument, NULL, 'n' },
1702 { "output", required_argument, NULL, 'o' },
1704 { "ignore-inhibitors", no_argument, NULL, 'i' },
1713 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1718 help(0, NULL, NULL);
1725 r = strv_extend(&arg_property, optarg);
1729 /* If the user asked for a particular
1730 * property, show it to him, even if it is
1749 if (safe_atou(optarg, &arg_lines) < 0) {
1750 log_error("Failed to parse lines '%s'", optarg);
1756 arg_output = output_mode_from_string(optarg);
1757 if (arg_output < 0) {
1758 log_error("Unknown output '%s'.", optarg);
1764 arg_no_pager = true;
1771 case ARG_NO_ASK_PASSWORD:
1772 arg_ask_password = false;
1776 arg_kill_who = optarg;
1780 arg_signal = signal_from_string_try_harder(optarg);
1781 if (arg_signal < 0) {
1782 log_error("Failed to parse signal string %s.", optarg);
1788 arg_transport = BUS_TRANSPORT_REMOTE;
1793 arg_transport = BUS_TRANSPORT_MACHINE;
1798 arg_action = ACTION_CANCEL_SHUTDOWN;
1802 arg_ignore_inhibitors = true;
1809 assert_not_reached("Unhandled option");
1815 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1817 static const Verb verbs[] = {
1818 { "help", VERB_ANY, VERB_ANY, 0, help },
1819 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1820 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1821 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1822 { "activate", VERB_ANY, 2, 0, activate },
1823 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1824 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1825 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1826 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1827 { "terminate-session", 2, VERB_ANY, 0, activate },
1828 { "kill-session", 2, VERB_ANY, 0, kill_session },
1829 { "list-users", VERB_ANY, 1, 0, list_users },
1830 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1831 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1832 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1833 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1834 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1835 { "kill-user", 2, VERB_ANY, 0, kill_user },
1836 { "list-seats", VERB_ANY, 1, 0, list_seats },
1837 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1838 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1839 { "attach", 3, VERB_ANY, 0, attach },
1840 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1841 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1842 { "poweroff", VERB_ANY, 1, 0, start_special },
1843 { "reboot", VERB_ANY, 1, 0, start_special },
1844 { "suspend", VERB_ANY, 1, 0, start_special },
1845 { "hibernate", VERB_ANY, 1, 0, start_special },
1846 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1847 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1851 return dispatch_verb(argc, argv, verbs, bus);
1854 int main(int argc, char *argv[]) {
1855 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1858 setlocale(LC_ALL, "");
1859 elogind_set_program_name(argv[0]);
1860 log_parse_environment();
1863 r = parse_argv(argc, argv);
1867 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1869 log_error_errno(r, "Failed to create bus connection: %m");
1873 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1875 r = loginctl_main(argc, argv, bus);
1879 polkit_agent_close();
1881 strv_free(arg_property);
1883 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;