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 needed by elogind
50 #include "eloginctl.h"
52 static char **arg_property = NULL;
53 static bool arg_all = false;
54 static bool arg_value = false;
55 static bool arg_full = false;
56 static bool arg_no_pager = false;
57 static bool arg_legend = true;
58 static const char *arg_kill_who = NULL;
59 static int arg_signal = SIGTERM;
60 #if 0 /// UNNEEDED by elogind
61 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
62 static char *arg_host = NULL;
63 static bool arg_ask_password = true;
64 static unsigned arg_lines = 10;
65 static OutputMode arg_output = OUTPUT_SHORT;
67 /// Instead we need this:
68 extern BusTransport arg_transport;
69 static char *arg_host = NULL;
70 extern bool arg_ask_password;
71 extern bool arg_no_wall;
72 extern usec_t arg_when;
73 extern bool arg_ignore_inhibitors;
74 extern elogind_action arg_action;
77 #if 0 /// UNNEEDED by elogind
78 static void polkit_agent_open_if_enabled(void) {
80 /* Open the polkit agent as a child process if necessary */
82 if (!arg_ask_password)
85 if (arg_transport != BUS_TRANSPORT_LOCAL)
91 static OutputFlags get_output_flags(void) {
94 arg_all * OUTPUT_SHOW_ALL |
95 arg_full * OUTPUT_FULL_WIDTH |
96 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
97 colors_enabled() * OUTPUT_COLOR;
101 static int get_session_path(sd_bus *bus, const char *session_id, sd_bus_error *error, char **path) {
102 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
106 r = sd_bus_call_method(
108 "org.freedesktop.login1",
109 "/org/freedesktop/login1",
110 "org.freedesktop.login1.Manager",
117 r = sd_bus_message_read(reply, "o", &ans);
129 static int list_sessions(int argc, char *argv[], void *userdata) {
130 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
131 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
132 const char *id, *user, *seat, *object;
133 sd_bus *bus = userdata;
141 pager_open(arg_no_pager, false);
143 r = sd_bus_call_method(
145 "org.freedesktop.login1",
146 "/org/freedesktop/login1",
147 "org.freedesktop.login1.Manager",
152 log_error("Failed to list sessions: %s", bus_error_message(&error, r));
156 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
158 return bus_log_parse_error(r);
161 printf("%10s %10s %-16s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT", "TTY");
163 while ((r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object)) > 0) {
164 _cleanup_(sd_bus_error_free) sd_bus_error error2 = SD_BUS_ERROR_NULL;
165 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply2 = NULL;
166 _cleanup_free_ char *path = NULL;
167 const char *tty = NULL;
169 r = get_session_path(bus, id, &error2, &path);
171 log_warning("Failed to get session path: %s", bus_error_message(&error, r));
173 r = sd_bus_get_property(
175 "org.freedesktop.login1",
177 "org.freedesktop.login1.Session",
183 log_warning("Failed to get TTY for session %s: %s",
184 id, bus_error_message(&error2, r));
186 r = sd_bus_message_read(reply2, "s", &tty);
188 return bus_log_parse_error(r);
192 printf("%10s %10"PRIu32" %-16s %-16s %-16s\n", id, uid, user, seat, strna(tty));
196 return bus_log_parse_error(r);
199 printf("\n%u sessions listed.\n", k);
204 static int list_users(int argc, char *argv[], void *userdata) {
205 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
206 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
207 const char *user, *object;
208 sd_bus *bus = userdata;
216 pager_open(arg_no_pager, false);
218 r = sd_bus_call_method(
220 "org.freedesktop.login1",
221 "/org/freedesktop/login1",
222 "org.freedesktop.login1.Manager",
227 log_error("Failed to list users: %s", bus_error_message(&error, r));
231 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
233 return bus_log_parse_error(r);
236 printf("%10s %-16s\n", "UID", "USER");
238 while ((r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object)) > 0) {
239 printf("%10"PRIu32" %-16s\n", uid, user);
243 return bus_log_parse_error(r);
246 printf("\n%u users listed.\n", k);
251 static int list_seats(int argc, char *argv[], void *userdata) {
252 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
253 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
254 const char *seat, *object;
255 sd_bus *bus = userdata;
262 pager_open(arg_no_pager, false);
264 r = sd_bus_call_method(
266 "org.freedesktop.login1",
267 "/org/freedesktop/login1",
268 "org.freedesktop.login1.Manager",
273 log_error("Failed to list seats: %s", bus_error_message(&error, r));
277 r = sd_bus_message_enter_container(reply, 'a', "(so)");
279 return bus_log_parse_error(r);
282 printf("%-16s\n", "SEAT");
284 while ((r = sd_bus_message_read(reply, "(so)", &seat, &object)) > 0) {
285 printf("%-16s\n", seat);
289 return bus_log_parse_error(r);
292 printf("\n%u seats listed.\n", k);
297 #if 0 /// UNNEEDED by elogind
298 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
299 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
300 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
301 _cleanup_free_ char *path = NULL;
309 path = unit_dbus_path_from_name(unit);
313 r = sd_bus_get_property(
315 "org.freedesktop.systemd1",
323 return log_error_errno(r, "Failed to query ControlGroup: %s", bus_error_message(&error, r));
325 r = sd_bus_message_read(reply, "s", &cgroup);
327 return bus_log_parse_error(r);
338 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
341 if (arg_transport == BUS_TRANSPORT_REMOTE)
344 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
346 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
349 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
351 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
357 typedef struct SessionStatusInfo {
361 struct dual_timestamp timestamp;
378 typedef struct UserStatusInfo {
382 struct dual_timestamp timestamp;
389 typedef struct SeatStatusInfo {
391 char *active_session;
395 static void session_status_info_clear(SessionStatusInfo *info) {
402 free(info->remote_host);
403 free(info->remote_user);
414 static void user_status_info_clear(UserStatusInfo *info) {
418 strv_free(info->sessions);
425 static void seat_status_info_clear(SeatStatusInfo *info) {
428 free(info->active_session);
429 strv_free(info->sessions);
434 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
435 const char *contents;
438 r = sd_bus_message_peek_type(m, NULL, &contents);
442 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
446 if (contents[0] == 's' || contents[0] == 'o') {
448 char **p = (char **) userdata;
450 r = sd_bus_message_read_basic(m, contents[0], &s);
454 r = free_and_strdup(p, s);
458 r = sd_bus_message_read_basic(m, contents[0], userdata);
463 r = sd_bus_message_skip(m, contents+1);
467 r = sd_bus_message_exit_container(m);
474 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
481 r = sd_bus_message_enter_container(m, 'a', "(so)");
485 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
486 r = strv_extend(userdata, name);
493 return sd_bus_message_exit_container(m);
496 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
498 static const struct bus_properties_map map[] = {
499 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
500 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
501 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
502 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
503 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
504 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
505 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
506 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
507 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
508 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
509 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
510 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
511 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
512 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
513 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
514 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
515 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
516 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
517 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
521 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
522 char since2[FORMAT_TIMESTAMP_MAX], *s2;
523 _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
526 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
528 return log_error_errno(r, "Could not get properties: %m");
535 printf("%s - ", strna(i.id));
538 printf("%s (%"PRIu32")\n", i.name, i.uid);
540 printf("%"PRIu32"\n", i.uid);
542 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
543 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
546 printf("\t Since: %s; %s\n", s2, s1);
548 printf("\t Since: %s\n", s2);
551 _cleanup_free_ char *t = NULL;
553 printf("\t Leader: %"PRIu32, i.leader);
555 get_process_comm(i.leader, &t);
562 if (!isempty(i.seat)) {
563 printf("\t Seat: %s", i.seat);
566 printf("; vc%u", i.vtnr);
572 printf("\t TTY: %s\n", i.tty);
574 printf("\t Display: %s\n", i.display);
576 if (i.remote_host && i.remote_user)
577 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
578 else if (i.remote_host)
579 printf("\t Remote: %s\n", i.remote_host);
580 else if (i.remote_user)
581 printf("\t Remote: user %s\n", i.remote_user);
583 printf("\t Remote: Yes\n");
586 printf("\t Service: %s", i.service);
589 printf("; type %s", i.type);
592 printf("; class %s", i.class);
596 printf("\t Type: %s", i.type);
599 printf("; class %s", i.class);
603 printf("\t Class: %s\n", i.class);
605 if (!isempty(i.desktop))
606 printf("\t Desktop: %s\n", i.desktop);
609 printf("\t State: %s\n", i.state);
612 printf("\t Unit: %s\n", i.scope);
613 #if 0 /// UNNEEDED by elogind
614 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
616 if (arg_transport == BUS_TRANSPORT_LOCAL) {
618 show_journal_by_unit(
623 i.timestamp.monotonic,
626 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
627 SD_JOURNAL_LOCAL_ONLY,
637 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
639 static const struct bus_properties_map map[] = {
640 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
641 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
642 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
643 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
644 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
645 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
646 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
647 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
648 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
652 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
653 char since2[FORMAT_TIMESTAMP_MAX], *s2;
654 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
657 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
659 return log_error_errno(r, "Could not get properties: %m");
667 printf("%s (%"PRIu32")\n", i.name, i.uid);
669 printf("%"PRIu32"\n", i.uid);
671 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
672 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
675 printf("\t Since: %s; %s\n", s2, s1);
677 printf("\t Since: %s\n", s2);
679 if (!isempty(i.state))
680 printf("\t State: %s\n", i.state);
682 if (!strv_isempty(i.sessions)) {
684 printf("\tSessions:");
686 STRV_FOREACH(l, i.sessions)
688 streq_ptr(*l, i.display) ? "*" : "",
694 printf("\t Linger: %s\n", yes_no(i.linger));
697 printf("\t Unit: %s\n", i.slice);
698 #if 0 /// UNNEEDED by elogind
699 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
701 show_journal_by_unit(
706 i.timestamp.monotonic,
709 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
710 SD_JOURNAL_LOCAL_ONLY,
719 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
721 static const struct bus_properties_map map[] = {
722 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
723 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
724 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
728 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
731 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
733 return log_error_errno(r, "Could not get properties: %m");
740 printf("%s\n", strna(i.id));
742 if (!strv_isempty(i.sessions)) {
744 printf("\tSessions:");
746 STRV_FOREACH(l, i.sessions) {
747 if (streq_ptr(*l, i.active_session))
756 if (arg_transport == BUS_TRANSPORT_LOCAL) {
765 printf("\t Devices:\n");
767 show_sysfs(i.id, "\t\t ", c);
773 #define property(name, fmt, ...) \
776 printf(fmt "\n", __VA_ARGS__); \
778 printf("%s=" fmt "\n", name, __VA_ARGS__); \
781 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
788 if (arg_property && !strv_find(arg_property, name))
789 /* skip what we didn't read */
790 return sd_bus_message_skip(m, contents);
792 switch (contents[0]) {
794 case SD_BUS_TYPE_STRUCT_BEGIN:
796 if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
799 r = sd_bus_message_read(m, "(so)", &s, NULL);
801 return bus_log_parse_error(r);
803 if (arg_all || !isempty(s))
804 property(name, "%s", s);
808 } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
811 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
813 return bus_log_parse_error(r);
815 if (!uid_is_valid(uid)) {
816 log_error("Invalid user ID: " UID_FMT, uid);
820 property(name, UID_FMT, uid);
826 case SD_BUS_TYPE_ARRAY:
828 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
832 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
834 return bus_log_parse_error(r);
839 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
840 printf("%s%s", space ? " " : "", s);
844 if (space || !arg_value)
848 return bus_log_parse_error(r);
850 r = sd_bus_message_exit_container(m);
852 return bus_log_parse_error(r);
860 r = bus_print_property(name, m, arg_value, arg_all);
862 return bus_log_parse_error(r);
865 r = sd_bus_message_skip(m, contents);
867 return bus_log_parse_error(r);
870 printf("%s=[unprintable]\n", name);
876 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
877 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
878 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
885 r = sd_bus_call_method(
887 "org.freedesktop.login1",
889 "org.freedesktop.DBus.Properties",
895 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
897 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
899 return bus_log_parse_error(r);
906 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
907 const char *name, *contents;
909 r = sd_bus_message_read(reply, "s", &name);
911 return bus_log_parse_error(r);
913 r = sd_bus_message_peek_type(reply, NULL, &contents);
915 return bus_log_parse_error(r);
917 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
919 return bus_log_parse_error(r);
921 r = print_property(name, reply, contents);
925 r = sd_bus_message_exit_container(reply);
927 return bus_log_parse_error(r);
929 r = sd_bus_message_exit_container(reply);
931 return bus_log_parse_error(r);
934 return bus_log_parse_error(r);
936 r = sd_bus_message_exit_container(reply);
938 return bus_log_parse_error(r);
943 static int show_session(int argc, char *argv[], void *userdata) {
944 bool properties, new_line = false;
945 sd_bus *bus = userdata;
951 properties = !strstr(argv[0], "status");
953 pager_open(arg_no_pager, false);
956 /* If not argument is specified inspect the manager
959 return show_properties(bus, "/org/freedesktop/login1", &new_line);
961 /* And in the pretty case, show data of the calling session */
962 return print_session_status_info(bus, "/org/freedesktop/login1/session/self", &new_line);
965 for (i = 1; i < argc; i++) {
966 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
967 _cleanup_free_ char *path = NULL;
969 r = get_session_path(bus, argv[1], &error, &path);
971 log_error("Failed to get session path: %s", bus_error_message(&error, r));
976 r = show_properties(bus, path, &new_line);
978 r = print_session_status_info(bus, path, &new_line);
987 static int show_user(int argc, char *argv[], void *userdata) {
988 bool properties, new_line = false;
989 sd_bus *bus = userdata;
995 properties = !strstr(argv[0], "status");
997 pager_open(arg_no_pager, false);
1000 /* If not argument is specified inspect the manager
1003 return show_properties(bus, "/org/freedesktop/login1", &new_line);
1005 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
1008 for (i = 1; i < argc; i++) {
1009 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1010 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1011 const char *path = NULL;
1014 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1016 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1018 r = sd_bus_call_method(
1020 "org.freedesktop.login1",
1021 "/org/freedesktop/login1",
1022 "org.freedesktop.login1.Manager",
1025 "u", (uint32_t) uid);
1027 log_error("Failed to get user: %s", bus_error_message(&error, r));
1031 r = sd_bus_message_read(reply, "o", &path);
1033 return bus_log_parse_error(r);
1036 r = show_properties(bus, path, &new_line);
1038 r = print_user_status_info(bus, path, &new_line);
1047 static int show_seat(int argc, char *argv[], void *userdata) {
1048 bool properties, new_line = false;
1049 sd_bus *bus = userdata;
1055 properties = !strstr(argv[0], "status");
1057 pager_open(arg_no_pager, false);
1060 /* If not argument is specified inspect the manager
1063 return show_properties(bus, "/org/freedesktop/login1", &new_line);
1065 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
1068 for (i = 1; i < argc; i++) {
1069 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1070 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1071 const char *path = NULL;
1073 r = sd_bus_call_method(
1075 "org.freedesktop.login1",
1076 "/org/freedesktop/login1",
1077 "org.freedesktop.login1.Manager",
1082 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1086 r = sd_bus_message_read(reply, "o", &path);
1088 return bus_log_parse_error(r);
1091 r = show_properties(bus, path, &new_line);
1093 r = print_seat_status_info(bus, path, &new_line);
1102 static int activate(int argc, char *argv[], void *userdata) {
1103 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1104 sd_bus *bus = userdata;
1105 char *short_argv[3];
1111 polkit_agent_open_if_enabled();
1114 /* No argument? Let's convert this into the empty
1115 * session name, which the calls will then resolve to
1116 * the caller's session. */
1118 short_argv[0] = argv[0];
1119 short_argv[1] = (char*) "";
1120 short_argv[2] = NULL;
1126 for (i = 1; i < argc; i++) {
1128 r = sd_bus_call_method(
1130 "org.freedesktop.login1",
1131 "/org/freedesktop/login1",
1132 "org.freedesktop.login1.Manager",
1133 streq(argv[0], "lock-session") ? "LockSession" :
1134 streq(argv[0], "unlock-session") ? "UnlockSession" :
1135 streq(argv[0], "terminate-session") ? "TerminateSession" :
1140 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1148 static int kill_session(int argc, char *argv[], void *userdata) {
1149 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1150 sd_bus *bus = userdata;
1156 polkit_agent_open_if_enabled();
1159 arg_kill_who = "all";
1161 for (i = 1; i < argc; i++) {
1163 r = sd_bus_call_method(
1165 "org.freedesktop.login1",
1166 "/org/freedesktop/login1",
1167 "org.freedesktop.login1.Manager",
1170 "ssi", argv[i], arg_kill_who, arg_signal);
1172 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1180 static int enable_linger(int argc, char *argv[], void *userdata) {
1181 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1182 sd_bus *bus = userdata;
1183 char* short_argv[3];
1190 polkit_agent_open_if_enabled();
1192 b = streq(argv[0], "enable-linger");
1195 short_argv[0] = argv[0];
1196 short_argv[1] = (char*) "";
1197 short_argv[2] = NULL;
1202 for (i = 1; i < argc; i++) {
1205 if (isempty(argv[i]))
1208 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1210 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1213 r = sd_bus_call_method(
1215 "org.freedesktop.login1",
1216 "/org/freedesktop/login1",
1217 "org.freedesktop.login1.Manager",
1220 "ubb", (uint32_t) uid, b, true);
1222 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1230 static int terminate_user(int argc, char *argv[], void *userdata) {
1231 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1232 sd_bus *bus = userdata;
1238 polkit_agent_open_if_enabled();
1240 for (i = 1; i < argc; i++) {
1243 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1245 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1247 r = sd_bus_call_method(
1249 "org.freedesktop.login1",
1250 "/org/freedesktop/login1",
1251 "org.freedesktop.login1.Manager",
1254 "u", (uint32_t) uid);
1256 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1264 static int kill_user(int argc, char *argv[], void *userdata) {
1265 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1266 sd_bus *bus = userdata;
1272 polkit_agent_open_if_enabled();
1275 arg_kill_who = "all";
1277 for (i = 1; i < argc; i++) {
1280 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1282 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1284 r = sd_bus_call_method(
1286 "org.freedesktop.login1",
1287 "/org/freedesktop/login1",
1288 "org.freedesktop.login1.Manager",
1291 "ui", (uint32_t) uid, arg_signal);
1293 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1301 static int attach(int argc, char *argv[], void *userdata) {
1302 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1303 sd_bus *bus = userdata;
1309 polkit_agent_open_if_enabled();
1311 for (i = 2; i < argc; i++) {
1313 r = sd_bus_call_method(
1315 "org.freedesktop.login1",
1316 "/org/freedesktop/login1",
1317 "org.freedesktop.login1.Manager",
1320 "ssb", argv[1], argv[i], true);
1323 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1331 static int flush_devices(int argc, char *argv[], void *userdata) {
1332 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1333 sd_bus *bus = userdata;
1339 polkit_agent_open_if_enabled();
1341 r = sd_bus_call_method(
1343 "org.freedesktop.login1",
1344 "/org/freedesktop/login1",
1345 "org.freedesktop.login1.Manager",
1350 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1355 static int lock_sessions(int argc, char *argv[], void *userdata) {
1356 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1357 sd_bus *bus = userdata;
1363 polkit_agent_open_if_enabled();
1365 r = sd_bus_call_method(
1367 "org.freedesktop.login1",
1368 "/org/freedesktop/login1",
1369 "org.freedesktop.login1.Manager",
1370 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1374 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1379 static int terminate_seat(int argc, char *argv[], void *userdata) {
1380 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1381 sd_bus *bus = userdata;
1387 polkit_agent_open_if_enabled();
1389 for (i = 1; i < argc; i++) {
1391 r = sd_bus_call_method(
1393 "org.freedesktop.login1",
1394 "/org/freedesktop/login1",
1395 "org.freedesktop.login1.Manager",
1400 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1408 static int help(int argc, char *argv[], void *userdata) {
1410 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1411 "Send control commands to or query the login manager.\n\n"
1412 " -h --help Show this help\n"
1413 " --version Show package version\n"
1414 " --no-pager Do not pipe output into a pager\n"
1415 #if 1 /// elogind supports --no-wall
1416 " --no-wall Do not print any wall message\n"
1418 " --no-legend Do not show the headers and footers\n"
1419 " --no-ask-password Don't prompt for password\n"
1420 " -H --host=[USER@]HOST Operate on remote host\n"
1421 " -M --machine=CONTAINER Operate on local container\n"
1422 " -p --property=NAME Show only properties by this name\n"
1423 " -a --all Show all properties, including empty ones\n"
1424 " --value When showing properties, only print the value\n"
1425 " -l --full Do not ellipsize output\n"
1426 " --kill-who=WHO Who to send signal to\n"
1427 " -s --signal=SIGNAL Which signal to send\n"
1428 #if 0 /// UNNEEDED by elogind
1429 " -n --lines=INTEGER Number of journal entries to show\n"
1430 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1431 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1433 /// elogind can cancel shutdowns and allows to ignore inhibitors
1434 " -c Cancel a pending shutdown or reboot\n"
1435 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n\n"
1437 "Session Commands:\n"
1438 " list-sessions List sessions\n"
1439 " session-status [ID...] Show session status\n"
1440 " show-session [ID...] Show properties of sessions or the manager\n"
1441 " activate [ID] Activate a session\n"
1442 " lock-session [ID...] Screen lock one or more sessions\n"
1443 " unlock-session [ID...] Screen unlock one or more sessions\n"
1444 " lock-sessions Screen lock all current sessions\n"
1445 " unlock-sessions Screen unlock all current sessions\n"
1446 " terminate-session ID... Terminate one or more sessions\n"
1447 " kill-session ID... Send signal to processes of a session\n\n"
1449 " list-users List users\n"
1450 " user-status [USER...] Show user status\n"
1451 " show-user [USER...] Show properties of users or the manager\n"
1452 " enable-linger [USER...] Enable linger state of one or more users\n"
1453 " disable-linger [USER...] Disable linger state of one or more users\n"
1454 " terminate-user USER... Terminate all sessions of one or more users\n"
1455 " kill-user USER... Send signal to processes of a user\n\n"
1457 " list-seats List seats\n"
1458 " seat-status [NAME...] Show seat status\n"
1459 " show-seat [NAME...] Show properties of seats or the manager\n"
1460 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1461 " flush-devices Flush all device associations\n"
1462 " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
1463 #if 1 /// elogind adds some system commands to loginctl
1464 "System Commands:\n"
1465 " poweroff [TIME] [WALL...] Turn off the machine\n"
1466 " reboot [TIME] [WALL...] Reboot the machine\n"
1467 " suspend Suspend the machine to memory\n"
1468 " hibernate Suspend the machine to disk\n"
1469 " hybrid-sleep Suspend the machine to memory and disk\n"
1471 , program_invocation_short_name);
1476 static int parse_argv(int argc, char *argv[]) {
1479 ARG_VERSION = 0x100,
1482 #if 1 /// elogind supports --no-wall
1487 ARG_NO_ASK_PASSWORD,
1490 static const struct option options[] = {
1491 { "help", no_argument, NULL, 'h' },
1492 { "version", no_argument, NULL, ARG_VERSION },
1493 { "property", required_argument, NULL, 'p' },
1494 { "all", no_argument, NULL, 'a' },
1495 { "value", no_argument, NULL, ARG_VALUE },
1496 { "full", no_argument, NULL, 'l' },
1497 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1498 #if 1 /// elogind supports --no-wall
1499 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1501 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1502 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1503 { "signal", required_argument, NULL, 's' },
1504 { "host", required_argument, NULL, 'H' },
1505 { "machine", required_argument, NULL, 'M' },
1506 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1507 #if 0 /// UNNEEDED by elogind
1508 { "lines", required_argument, NULL, 'n' },
1509 { "output", required_argument, NULL, 'o' },
1511 /// elogind allows to ignore inhibitors for system commands.
1512 { "ignore-inhibitors", no_argument, NULL, 'i' },
1522 #if 0 /// elogind adds some system commands to loginctl
1523 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1525 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1531 help(0, NULL, NULL);
1538 r = strv_extend(&arg_property, optarg);
1542 /* If the user asked for a particular
1543 * property, show it to him, even if it is
1561 #if 0 /// UNNEEDED by elogind
1563 if (safe_atou(optarg, &arg_lines) < 0) {
1564 log_error("Failed to parse lines '%s'", optarg);
1570 arg_output = output_mode_from_string(optarg);
1571 if (arg_output < 0) {
1572 log_error("Unknown output '%s'.", optarg);
1579 arg_no_pager = true;
1581 #if 1 /// elogind supports --no-wall
1591 case ARG_NO_ASK_PASSWORD:
1592 arg_ask_password = false;
1596 arg_kill_who = optarg;
1600 arg_signal = signal_from_string_try_harder(optarg);
1601 if (arg_signal < 0) {
1602 log_error("Failed to parse signal string %s.", optarg);
1608 arg_transport = BUS_TRANSPORT_REMOTE;
1613 arg_transport = BUS_TRANSPORT_MACHINE;
1616 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1618 arg_action = ACTION_CANCEL_SHUTDOWN;
1622 arg_ignore_inhibitors = true;
1629 assert_not_reached("Unhandled option");
1635 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1637 static const Verb verbs[] = {
1638 { "help", VERB_ANY, VERB_ANY, 0, help },
1639 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1640 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1641 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1642 { "activate", VERB_ANY, 2, 0, activate },
1643 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1644 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1645 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1646 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1647 { "terminate-session", 2, VERB_ANY, 0, activate },
1648 { "kill-session", 2, VERB_ANY, 0, kill_session },
1649 { "list-users", VERB_ANY, 1, 0, list_users },
1650 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1651 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1652 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1653 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1654 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1655 { "kill-user", 2, VERB_ANY, 0, kill_user },
1656 { "list-seats", VERB_ANY, 1, 0, list_seats },
1657 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1658 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1659 { "attach", 3, VERB_ANY, 0, attach },
1660 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1661 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1662 #if 1 /// elogind adds some system commands to loginctl
1663 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
1664 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
1665 { "suspend", VERB_ANY, 1, 0, start_special },
1666 { "hibernate", VERB_ANY, 1, 0, start_special },
1667 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1668 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1673 #if 1 /// elogind can do shutdown and allows its cancellation
1674 if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
1675 return elogind_cancel_shutdown(bus);
1677 return dispatch_verb(argc, argv, verbs, bus);
1680 int main(int argc, char *argv[]) {
1684 setlocale(LC_ALL, "");
1685 elogind_set_program_name(argv[0]);
1686 log_parse_environment();
1689 r = parse_argv(argc, argv);
1693 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1695 log_error_errno(r, "Failed to create bus connection: %m");
1699 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1701 r = loginctl_main(argc, argv, bus);
1704 sd_bus_flush_close_unref(bus);
1707 #if 0 /// elogind does that in elogind_cleanup()
1708 polkit_agent_close();
1711 strv_free(arg_property);
1713 #if 1 /// elogind has some own cleanups to do
1716 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;