1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
13 //#include "bus-unit-util.h"
15 //#include "cgroup-show.h"
16 #include "cgroup-util.h"
17 #include "format-table.h"
19 //#include "logs-show.h"
22 #include "parse-util.h"
23 #include "process-util.h"
25 #include "signal-util.h"
26 #include "spawn-polkit-agent.h"
27 #include "string-table.h"
29 #include "sysfs-show.h"
30 #include "terminal-util.h"
31 #include "unit-name.h"
32 #include "user-util.h"
36 /// Additional includes needed by elogind
37 #include "eloginctl.h"
39 static char **arg_property = NULL;
40 static bool arg_all = false;
41 static bool arg_value = false;
42 static bool arg_full = false;
43 static bool arg_no_pager = false;
44 static bool arg_legend = true;
45 static const char *arg_kill_who = NULL;
46 static int arg_signal = SIGTERM;
47 #if 0 /// UNNEEDED by elogind
48 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
49 static char *arg_host = NULL;
50 static bool arg_ask_password = true;
51 static unsigned arg_lines = 10;
52 static OutputMode arg_output = OUTPUT_SHORT;
54 /// Instead we need this:
55 extern BusTransport arg_transport;
56 static char *arg_host = NULL;
57 extern bool arg_ask_password;
58 extern bool arg_dry_run;
59 extern bool arg_quiet;
60 extern bool arg_no_wall;
61 extern usec_t arg_when;
62 extern bool arg_ignore_inhibitors;
63 extern elogind_action arg_action;
66 static OutputFlags get_output_flags(void) {
69 arg_all * OUTPUT_SHOW_ALL |
70 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
71 colors_enabled() * OUTPUT_COLOR;
74 static int get_session_path(sd_bus *bus, const char *session_id, sd_bus_error *error, char **path) {
75 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
79 r = sd_bus_call_method(
81 "org.freedesktop.login1",
82 "/org/freedesktop/login1",
83 "org.freedesktop.login1.Manager",
90 r = sd_bus_message_read(reply, "o", &ans);
102 static int show_table(Table *table, const char *word) {
108 if (table_get_rows(table) > 1) {
109 r = table_set_sort(table, (size_t) 0, (size_t) -1);
111 return log_error_errno(r, "Failed to sort table: %m");
113 table_set_header(table, arg_legend);
115 r = table_print(table, NULL);
117 return log_error_errno(r, "Failed to show table: %m");
121 if (table_get_rows(table) > 1)
122 printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
124 printf("No %s.\n", word);
130 static int list_sessions(int argc, char *argv[], void *userdata) {
131 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
132 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
133 _cleanup_(table_unrefp) Table *table = NULL;
134 sd_bus *bus = userdata;
140 (void) pager_open(arg_no_pager, false);
142 r = sd_bus_call_method(
144 "org.freedesktop.login1",
145 "/org/freedesktop/login1",
146 "org.freedesktop.login1.Manager",
151 return log_error_errno(r, "Failed to list sessions: %s", bus_error_message(&error, r));
153 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
155 return bus_log_parse_error(r);
157 table = table_new("SESSION", "UID", "USER", "SEAT", "TTY");
161 /* Right-align the first two fields (since they are numeric) */
162 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
163 (void) table_set_align_percent(table, TABLE_HEADER_CELL(1), 100);
166 _cleanup_(sd_bus_error_free) sd_bus_error error_tty = SD_BUS_ERROR_NULL;
167 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_tty = NULL;
168 const char *id, *user, *seat, *object, *tty = NULL;
171 r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object);
173 return bus_log_parse_error(r);
177 r = sd_bus_get_property(
179 "org.freedesktop.login1",
181 "org.freedesktop.login1.Session",
187 log_warning_errno(r, "Failed to get TTY for session %s: %s", id, bus_error_message(&error_tty, r));
189 r = sd_bus_message_read(reply_tty, "s", &tty);
191 return bus_log_parse_error(r);
194 r = table_add_many(table,
199 TABLE_STRING, strna(tty));
201 return log_error_errno(r, "Failed to add row to table: %m");
204 r = sd_bus_message_exit_container(reply);
206 return bus_log_parse_error(r);
208 return show_table(table, "sessions");
211 static int list_users(int argc, char *argv[], void *userdata) {
212 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
213 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
214 _cleanup_(table_unrefp) Table *table = NULL;
215 sd_bus *bus = userdata;
221 (void) pager_open(arg_no_pager, false);
223 r = sd_bus_call_method(
225 "org.freedesktop.login1",
226 "/org/freedesktop/login1",
227 "org.freedesktop.login1.Manager",
232 return log_error_errno(r, "Failed to list users: %s", bus_error_message(&error, r));
234 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
236 return bus_log_parse_error(r);
238 table = table_new("UID", "USER");
242 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
248 r = sd_bus_message_read(reply, "(uso)", &uid, &user, NULL);
250 return bus_log_parse_error(r);
254 r = table_add_many(table,
258 return log_error_errno(r, "Failed to add row to table: %m");
261 r = sd_bus_message_exit_container(reply);
263 return bus_log_parse_error(r);
265 return show_table(table, "users");
268 static int list_seats(int argc, char *argv[], void *userdata) {
269 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
270 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
271 _cleanup_(table_unrefp) Table *table = NULL;
272 sd_bus *bus = userdata;
278 (void) pager_open(arg_no_pager, false);
280 r = sd_bus_call_method(
282 "org.freedesktop.login1",
283 "/org/freedesktop/login1",
284 "org.freedesktop.login1.Manager",
289 return log_error_errno(r, "Failed to list seats: %s", bus_error_message(&error, r));
291 r = sd_bus_message_enter_container(reply, 'a', "(so)");
293 return bus_log_parse_error(r);
295 table = table_new("SEAT");
302 r = sd_bus_message_read(reply, "(so)", &seat, NULL);
304 return bus_log_parse_error(r);
308 r = table_add_cell(table, NULL, TABLE_STRING, seat);
310 return log_error_errno(r, "Failed to add row to table: %m");
313 r = sd_bus_message_exit_container(reply);
315 return bus_log_parse_error(r);
317 return show_table(table, "seats");
320 #if 0 /// UNNEEDED by elogind
321 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
322 _cleanup_free_ char *cgroup = NULL;
323 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
330 r = show_cgroup_get_unit_path_and_warn(bus, unit, &cgroup);
343 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
346 if (arg_transport == BUS_TRANSPORT_REMOTE)
349 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
351 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
354 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
356 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
362 typedef struct SessionStatusInfo {
366 struct dual_timestamp timestamp;
372 const char *remote_host;
373 const char *remote_user;
383 typedef struct UserStatusInfo {
387 struct dual_timestamp timestamp;
394 typedef struct SeatStatusInfo {
396 const char *active_session;
400 static void user_status_info_clear(UserStatusInfo *info) {
402 strv_free(info->sessions);
407 static void seat_status_info_clear(SeatStatusInfo *info) {
409 strv_free(info->sessions);
414 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
415 const char *contents;
418 r = sd_bus_message_peek_type(m, NULL, &contents);
422 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
426 r = sd_bus_message_read_basic(m, contents[0], userdata);
430 r = sd_bus_message_skip(m, contents+1);
434 r = sd_bus_message_exit_container(m);
441 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
448 r = sd_bus_message_enter_container(m, 'a', "(so)");
452 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
453 r = strv_extend(userdata, name);
460 return sd_bus_message_exit_container(m);
463 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
465 static const struct bus_properties_map map[] = {
466 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
467 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
468 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
469 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
470 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
471 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
472 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
473 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
474 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
475 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
476 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
477 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
478 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
479 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
480 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
481 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
482 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
483 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
484 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
488 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
489 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
490 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
491 char since2[FORMAT_TIMESTAMP_MAX];
493 SessionStatusInfo i = {};
496 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
498 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
505 printf("%s - ", strna(i.id));
508 printf("%s (%"PRIu32")\n", i.name, i.uid);
510 printf("%"PRIu32"\n", i.uid);
512 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
513 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
516 printf("\t Since: %s; %s\n", s2, s1);
518 printf("\t Since: %s\n", s2);
521 _cleanup_free_ char *t = NULL;
523 printf("\t Leader: %"PRIu32, i.leader);
525 get_process_comm(i.leader, &t);
532 if (!isempty(i.seat)) {
533 printf("\t Seat: %s", i.seat);
536 printf("; vc%u", i.vtnr);
542 printf("\t TTY: %s\n", i.tty);
544 printf("\t Display: %s\n", i.display);
546 if (i.remote_host && i.remote_user)
547 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
548 else if (i.remote_host)
549 printf("\t Remote: %s\n", i.remote_host);
550 else if (i.remote_user)
551 printf("\t Remote: user %s\n", i.remote_user);
553 printf("\t Remote: Yes\n");
556 printf("\t Service: %s", i.service);
559 printf("; type %s", i.type);
562 printf("; class %s", i.class);
566 printf("\t Type: %s", i.type);
569 printf("; class %s", i.class);
573 printf("\t Class: %s\n", i.class);
575 if (!isempty(i.desktop))
576 printf("\t Desktop: %s\n", i.desktop);
579 printf("\t State: %s\n", i.state);
582 printf("\t Unit: %s\n", i.scope);
583 #if 0 /// UNNEEDED by elogind
584 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
586 if (arg_transport == BUS_TRANSPORT_LOCAL) {
588 show_journal_by_unit(
593 i.timestamp.monotonic,
596 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
597 SD_JOURNAL_LOCAL_ONLY,
607 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
609 static const struct bus_properties_map map[] = {
610 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
611 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
612 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
613 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
614 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
615 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
616 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
617 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
618 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
622 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
623 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
624 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
625 char since2[FORMAT_TIMESTAMP_MAX];
627 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
630 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
632 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
640 printf("%s (%"PRIu32")\n", i.name, i.uid);
642 printf("%"PRIu32"\n", i.uid);
644 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
645 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
648 printf("\t Since: %s; %s\n", s2, s1);
650 printf("\t Since: %s\n", s2);
652 if (!isempty(i.state))
653 printf("\t State: %s\n", i.state);
655 if (!strv_isempty(i.sessions)) {
657 printf("\tSessions:");
659 STRV_FOREACH(l, i.sessions)
661 streq_ptr(*l, i.display) ? "*" : "",
667 printf("\t Linger: %s\n", yes_no(i.linger));
670 printf("\t Unit: %s\n", i.slice);
671 #if 0 /// UNNEEDED by elogind
672 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
674 show_journal_by_unit(
679 i.timestamp.monotonic,
682 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
683 SD_JOURNAL_LOCAL_ONLY,
692 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
694 static const struct bus_properties_map map[] = {
695 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
696 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
697 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
701 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
702 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
703 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
706 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, 0, &error, &m, &i);
708 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
715 printf("%s\n", strna(i.id));
717 if (!strv_isempty(i.sessions)) {
719 printf("\tSessions:");
721 STRV_FOREACH(l, i.sessions) {
722 if (streq_ptr(*l, i.active_session))
731 if (arg_transport == BUS_TRANSPORT_LOCAL) {
740 printf("\t Devices:\n");
742 show_sysfs(i.id, "\t\t ", c, get_output_flags());
748 #define property(name, fmt, ...) \
751 printf(fmt "\n", __VA_ARGS__); \
753 printf("%s=" fmt "\n", name, __VA_ARGS__); \
756 static int print_property(const char *name, sd_bus_message *m, bool value, bool all) {
758 const char *contents;
764 r = sd_bus_message_peek_type(m, &type, &contents);
770 case SD_BUS_TYPE_STRUCT:
772 if (contents[0] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
775 r = sd_bus_message_read(m, "(so)", &s, NULL);
777 return bus_log_parse_error(r);
779 if (all || !isempty(s))
780 property(name, "%s", s);
784 } else if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
787 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
789 return bus_log_parse_error(r);
791 if (!uid_is_valid(uid)) {
792 log_error("Invalid user ID: " UID_FMT, uid);
796 property(name, UID_FMT, uid);
801 case SD_BUS_TYPE_ARRAY:
803 if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
807 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
809 return bus_log_parse_error(r);
814 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
815 printf("%s%s", space ? " " : "", s);
823 return bus_log_parse_error(r);
825 r = sd_bus_message_exit_container(m);
827 return bus_log_parse_error(r);
837 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
849 r = bus_print_all_properties(bus, "org.freedesktop.login1", path, print_property, arg_property, arg_value, arg_all, NULL);
851 return bus_log_parse_error(r);
856 static int show_session(int argc, char *argv[], void *userdata) {
857 bool properties, new_line = false;
858 sd_bus *bus = userdata;
860 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
861 _cleanup_free_ char *path = NULL;
866 properties = !strstr(argv[0], "status");
868 (void) pager_open(arg_no_pager, false);
871 const char *session, *p = "/org/freedesktop/login1/session/self";
874 /* If no argument is specified inspect the manager itself */
875 return show_properties(bus, "/org/freedesktop/login1", &new_line);
877 /* And in the pretty case, show data of the calling session */
878 session = getenv("XDG_SESSION_ID");
880 r = get_session_path(bus, session, &error, &path);
882 log_error("Failed to get session path: %s", bus_error_message(&error, r));
888 return print_session_status_info(bus, p, &new_line);
891 for (i = 1; i < argc; i++) {
892 r = get_session_path(bus, argv[i], &error, &path);
894 log_error("Failed to get session path: %s", bus_error_message(&error, r));
899 r = show_properties(bus, path, &new_line);
901 r = print_session_status_info(bus, path, &new_line);
910 static int show_user(int argc, char *argv[], void *userdata) {
911 bool properties, new_line = false;
912 sd_bus *bus = userdata;
918 properties = !strstr(argv[0], "status");
920 (void) pager_open(arg_no_pager, false);
923 /* If not argument is specified inspect the manager
926 return show_properties(bus, "/org/freedesktop/login1", &new_line);
928 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
931 for (i = 1; i < argc; i++) {
932 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
933 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
934 const char *path = NULL;
937 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
939 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
941 r = sd_bus_call_method(
943 "org.freedesktop.login1",
944 "/org/freedesktop/login1",
945 "org.freedesktop.login1.Manager",
948 "u", (uint32_t) uid);
950 log_error("Failed to get user: %s", bus_error_message(&error, r));
954 r = sd_bus_message_read(reply, "o", &path);
956 return bus_log_parse_error(r);
959 r = show_properties(bus, path, &new_line);
961 r = print_user_status_info(bus, path, &new_line);
970 static int show_seat(int argc, char *argv[], void *userdata) {
971 bool properties, new_line = false;
972 sd_bus *bus = userdata;
978 properties = !strstr(argv[0], "status");
980 (void) pager_open(arg_no_pager, false);
983 /* If not argument is specified inspect the manager
986 return show_properties(bus, "/org/freedesktop/login1", &new_line);
988 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
991 for (i = 1; i < argc; i++) {
992 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
993 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
994 const char *path = NULL;
996 r = sd_bus_call_method(
998 "org.freedesktop.login1",
999 "/org/freedesktop/login1",
1000 "org.freedesktop.login1.Manager",
1005 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1009 r = sd_bus_message_read(reply, "o", &path);
1011 return bus_log_parse_error(r);
1014 r = show_properties(bus, path, &new_line);
1016 r = print_seat_status_info(bus, path, &new_line);
1025 static int activate(int argc, char *argv[], void *userdata) {
1026 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1027 sd_bus *bus = userdata;
1028 char *short_argv[3];
1034 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1037 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1038 * session name, in which case logind will try to guess our session. */
1040 short_argv[0] = argv[0];
1041 short_argv[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1042 short_argv[2] = NULL;
1048 for (i = 1; i < argc; i++) {
1050 r = sd_bus_call_method(
1052 "org.freedesktop.login1",
1053 "/org/freedesktop/login1",
1054 "org.freedesktop.login1.Manager",
1055 streq(argv[0], "lock-session") ? "LockSession" :
1056 streq(argv[0], "unlock-session") ? "UnlockSession" :
1057 streq(argv[0], "terminate-session") ? "TerminateSession" :
1062 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1070 static int kill_session(int argc, char *argv[], void *userdata) {
1071 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1072 sd_bus *bus = userdata;
1078 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1081 arg_kill_who = "all";
1083 for (i = 1; i < argc; i++) {
1085 r = sd_bus_call_method(
1087 "org.freedesktop.login1",
1088 "/org/freedesktop/login1",
1089 "org.freedesktop.login1.Manager",
1092 "ssi", argv[i], arg_kill_who, arg_signal);
1094 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1102 static int enable_linger(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];
1112 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1114 b = streq(argv[0], "enable-linger");
1117 /* No argument? Let's use an empty user name,
1118 * then logind will use our user. */
1120 short_argv[0] = argv[0];
1121 short_argv[1] = (char*) "";
1122 short_argv[2] = NULL;
1127 for (i = 1; i < argc; i++) {
1130 if (isempty(argv[i]))
1133 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1135 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1138 r = sd_bus_call_method(
1140 "org.freedesktop.login1",
1141 "/org/freedesktop/login1",
1142 "org.freedesktop.login1.Manager",
1145 "ubb", (uint32_t) uid, b, true);
1147 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1155 static int terminate_user(int argc, char *argv[], void *userdata) {
1156 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1157 sd_bus *bus = userdata;
1163 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1165 for (i = 1; i < argc; i++) {
1168 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1170 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1172 r = sd_bus_call_method(
1174 "org.freedesktop.login1",
1175 "/org/freedesktop/login1",
1176 "org.freedesktop.login1.Manager",
1179 "u", (uint32_t) uid);
1181 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1189 static int kill_user(int argc, char *argv[], void *userdata) {
1190 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1191 sd_bus *bus = userdata;
1197 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1200 arg_kill_who = "all";
1202 for (i = 1; i < argc; i++) {
1205 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1207 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1209 r = sd_bus_call_method(
1211 "org.freedesktop.login1",
1212 "/org/freedesktop/login1",
1213 "org.freedesktop.login1.Manager",
1216 "ui", (uint32_t) uid, arg_signal);
1218 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1226 static int attach(int argc, char *argv[], void *userdata) {
1227 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1228 sd_bus *bus = userdata;
1234 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1236 for (i = 2; i < argc; i++) {
1238 r = sd_bus_call_method(
1240 "org.freedesktop.login1",
1241 "/org/freedesktop/login1",
1242 "org.freedesktop.login1.Manager",
1245 "ssb", argv[1], argv[i], true);
1248 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1256 static int flush_devices(int argc, char *argv[], void *userdata) {
1257 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1258 sd_bus *bus = userdata;
1264 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1266 r = sd_bus_call_method(
1268 "org.freedesktop.login1",
1269 "/org/freedesktop/login1",
1270 "org.freedesktop.login1.Manager",
1275 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1280 static int lock_sessions(int argc, char *argv[], void *userdata) {
1281 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1282 sd_bus *bus = userdata;
1288 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1290 r = sd_bus_call_method(
1292 "org.freedesktop.login1",
1293 "/org/freedesktop/login1",
1294 "org.freedesktop.login1.Manager",
1295 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1299 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1304 static int terminate_seat(int argc, char *argv[], void *userdata) {
1305 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1306 sd_bus *bus = userdata;
1312 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1314 for (i = 1; i < argc; i++) {
1316 r = sd_bus_call_method(
1318 "org.freedesktop.login1",
1319 "/org/freedesktop/login1",
1320 "org.freedesktop.login1.Manager",
1325 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1333 static int help(int argc, char *argv[], void *userdata) {
1335 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1336 "Send control commands to or query the login manager.\n\n"
1337 " -h --help Show this help\n"
1338 " --version Show package version\n"
1339 " --no-pager Do not pipe output into a pager\n"
1340 #if 1 /// elogind supports --no-wall and --dry-run
1341 " --no-wall Do not print any wall message\n"
1342 " --dry-run Only print what would be done\n"
1343 " -q --quiet Suppress output\n"
1345 " --no-legend Do not show the headers and footers\n"
1346 " --no-ask-password Don't prompt for password\n"
1347 " -H --host=[USER@]HOST Operate on remote host\n"
1348 " -M --machine=CONTAINER Operate on local container\n"
1349 " -p --property=NAME Show only properties by this name\n"
1350 " -a --all Show all properties, including empty ones\n"
1351 " --value When showing properties, only print the value\n"
1352 " -l --full Do not ellipsize output\n"
1353 " --kill-who=WHO Who to send signal to\n"
1354 " -s --signal=SIGNAL Which signal to send\n"
1355 #if 0 /// UNNEEDED by elogind
1356 " -n --lines=INTEGER Number of journal entries to show\n"
1357 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1358 " short-iso, short-iso-precise, short-full,\n"
1359 " short-monotonic, short-unix, verbose, export,\n"
1360 " json, json-pretty, json-sse, cat)\n"
1362 /// elogind can cancel shutdowns and allows to ignore inhibitors
1363 " -c Cancel a pending shutdown or reboot\n"
1364 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n\n"
1366 "Session Commands:\n"
1367 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1368 " list-sessions List sessions\n"
1370 " list[-sessions] List sessions (default command)\n"
1372 " session-status [ID...] Show session status\n"
1373 " show-session [ID...] Show properties of sessions or the manager\n"
1374 " activate [ID] Activate a session\n"
1375 " lock-session [ID...] Screen lock one or more sessions\n"
1376 " unlock-session [ID...] Screen unlock one or more sessions\n"
1377 " lock-sessions Screen lock all current sessions\n"
1378 " unlock-sessions Screen unlock all current sessions\n"
1379 " terminate-session ID... Terminate one or more sessions\n"
1380 " kill-session ID... Send signal to processes of a session\n\n"
1382 " list-users List users\n"
1383 " user-status [USER...] Show user status\n"
1384 " show-user [USER...] Show properties of users or the manager\n"
1385 " enable-linger [USER...] Enable linger state of one or more users\n"
1386 " disable-linger [USER...] Disable linger state of one or more users\n"
1387 " terminate-user USER... Terminate all sessions of one or more users\n"
1388 " kill-user USER... Send signal to processes of a user\n\n"
1390 " list-seats List seats\n"
1391 " seat-status [NAME...] Show seat status\n"
1392 " show-seat [NAME...] Show properties of seats or the manager\n"
1393 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1394 " flush-devices Flush all device associations\n"
1395 #if 0 /// elogind adds some system commands to loginctl
1396 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1398 " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
1399 "System Commands:\n"
1400 " poweroff [TIME] [WALL...] Turn off the machine\n"
1401 " reboot [TIME] [WALL...] Reboot the machine\n"
1402 " suspend Suspend the machine to memory\n"
1403 " hibernate Suspend the machine to disk\n"
1404 " hybrid-sleep Suspend the machine to memory and disk\n"
1405 " suspend-then-hibernate Suspend the system, wake after a period of\n"
1406 " time and put it into hibernate\n"
1408 , program_invocation_short_name);
1413 static int parse_argv(int argc, char *argv[]) {
1416 ARG_VERSION = 0x100,
1419 #if 1 /// elogind supports --no-wall and --dry-run
1425 ARG_NO_ASK_PASSWORD,
1428 static const struct option options[] = {
1429 { "help", no_argument, NULL, 'h' },
1430 { "version", no_argument, NULL, ARG_VERSION },
1431 { "property", required_argument, NULL, 'p' },
1432 { "all", no_argument, NULL, 'a' },
1433 { "value", no_argument, NULL, ARG_VALUE },
1434 { "full", no_argument, NULL, 'l' },
1435 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1436 #if 1 /// elogind supports --no-wall, --dry-run and --quiet
1437 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1438 { "dry-run", no_argument, NULL, ARG_DRY_RUN },
1439 { "quiet", no_argument, NULL, 'q' },
1441 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1442 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1443 { "signal", required_argument, NULL, 's' },
1444 { "host", required_argument, NULL, 'H' },
1445 { "machine", required_argument, NULL, 'M' },
1446 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1447 #if 0 /// UNNEEDED by elogind
1448 { "lines", required_argument, NULL, 'n' },
1449 { "output", required_argument, NULL, 'o' },
1451 /// elogind allows to ignore inhibitors for system commands.
1452 { "ignore-inhibitors", no_argument, NULL, 'i' },
1462 #if 0 /// elogind adds some system commands to loginctl
1463 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1465 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1471 help(0, NULL, NULL);
1478 r = strv_extend(&arg_property, optarg);
1482 /* If the user asked for a particular
1483 * property, show it to him, even if it is
1501 #if 0 /// UNNEEDED by elogind
1503 if (safe_atou(optarg, &arg_lines) < 0) {
1504 log_error("Failed to parse lines '%s'", optarg);
1510 if (streq(optarg, "help")) {
1511 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
1515 arg_output = output_mode_from_string(optarg);
1516 if (arg_output < 0) {
1517 log_error("Unknown output '%s'.", optarg);
1524 arg_no_pager = true;
1526 #if 1 /// elogind supports --no-wall, -dry-run and --quiet
1545 case ARG_NO_ASK_PASSWORD:
1546 arg_ask_password = false;
1550 arg_kill_who = optarg;
1554 if (streq(optarg, "help")) {
1555 DUMP_STRING_TABLE(signal, int, _NSIG);
1559 arg_signal = signal_from_string(optarg);
1560 if (arg_signal < 0) {
1561 log_error("Failed to parse signal string %s.", optarg);
1567 arg_transport = BUS_TRANSPORT_REMOTE;
1572 arg_transport = BUS_TRANSPORT_MACHINE;
1575 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1577 arg_action = ACTION_CANCEL_SHUTDOWN;
1581 arg_ignore_inhibitors = true;
1588 assert_not_reached("Unhandled option");
1594 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1596 static const Verb verbs[] = {
1597 { "help", VERB_ANY, VERB_ANY, 0, help },
1598 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1599 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1601 { "list", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1602 { "list-sessions", VERB_ANY, 1, 0, list_sessions },
1604 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1605 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1606 { "activate", VERB_ANY, 2, 0, activate },
1607 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1608 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1609 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1610 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1611 { "terminate-session", 2, VERB_ANY, 0, activate },
1612 { "kill-session", 2, VERB_ANY, 0, kill_session },
1613 { "list-users", VERB_ANY, 1, 0, list_users },
1614 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1615 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1616 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1617 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1618 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1619 { "kill-user", 2, VERB_ANY, 0, kill_user },
1620 { "list-seats", VERB_ANY, 1, 0, list_seats },
1621 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1622 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1623 { "attach", 3, VERB_ANY, 0, attach },
1624 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1625 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1626 #if 1 /// elogind adds some system commands to loginctl
1627 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
1628 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
1629 { "suspend", VERB_ANY, 1, 0, start_special },
1630 { "hibernate", VERB_ANY, 1, 0, start_special },
1631 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1632 { "suspend-then-hibernate", VERB_ANY, 1, 0, start_special },
1633 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1638 #if 1 /// elogind can do shutdown and allows its cancellation
1639 if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
1640 return elogind_cancel_shutdown(bus);
1642 return dispatch_verb(argc, argv, verbs, bus);
1645 int main(int argc, char *argv[]) {
1649 setlocale(LC_ALL, "");
1650 elogind_set_program_name(argv[0]);
1651 log_parse_environment();
1655 r = parse_argv(argc, argv);
1659 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1661 log_error_errno(r, "Failed to create bus connection: %m");
1665 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1667 r = loginctl_main(argc, argv, bus);
1670 /* make sure we terminate the bus connection first, and then close the
1671 * pager, see issue #3543 for the details. */
1672 sd_bus_flush_close_unref(bus);
1674 #if 0 /// elogind does that in elogind_cleanup()
1675 polkit_agent_close();
1678 strv_free(arg_property);
1680 #if 1 /// elogind has some own cleanups to do
1683 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;