1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
16 #include "alloc-util.h"
17 #include "bus-error.h"
18 //#include "bus-unit-util.h"
20 //#include "cgroup-show.h"
21 #include "cgroup-util.h"
22 //#include "format-table.h"
24 //#include "logs-show.h"
27 #include "parse-util.h"
28 #include "process-util.h"
30 #include "signal-util.h"
31 #include "spawn-polkit-agent.h"
32 //#include "string-table.h"
34 #include "sysfs-show.h"
35 #include "terminal-util.h"
36 #include "unit-name.h"
37 #include "user-util.h"
41 /// Additional includes needed by elogind
42 #include "eloginctl.h"
44 static char **arg_property = NULL;
45 static bool arg_all = false;
46 static bool arg_value = false;
47 static bool arg_full = false;
48 static bool arg_no_pager = false;
49 static bool arg_legend = true;
50 static const char *arg_kill_who = NULL;
51 static int arg_signal = SIGTERM;
52 #if 0 /// UNNEEDED by elogind
53 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
54 static char *arg_host = NULL;
55 static bool arg_ask_password = true;
56 static unsigned arg_lines = 10;
57 static OutputMode arg_output = OUTPUT_SHORT;
59 /// Instead we need this:
60 extern BusTransport arg_transport;
61 static char *arg_host = NULL;
62 extern bool arg_ask_password;
63 extern bool arg_dry_run;
64 extern bool arg_quiet;
65 extern bool arg_no_wall;
66 extern usec_t arg_when;
67 extern bool arg_ignore_inhibitors;
68 extern elogind_action arg_action;
71 static OutputFlags get_output_flags(void) {
74 arg_all * OUTPUT_SHOW_ALL |
75 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
76 colors_enabled() * OUTPUT_COLOR;
79 static int get_session_path(sd_bus *bus, const char *session_id, sd_bus_error *error, char **path) {
80 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
84 r = sd_bus_call_method(
86 "org.freedesktop.login1",
87 "/org/freedesktop/login1",
88 "org.freedesktop.login1.Manager",
95 r = sd_bus_message_read(reply, "o", &ans);
107 static int show_table(Table *table, const char *word) {
113 if (table_get_rows(table) > 1) {
114 r = table_set_sort(table, (size_t) 0, (size_t) -1);
116 return log_error_errno(r, "Failed to sort table: %m");
118 table_set_header(table, arg_legend);
120 r = table_print(table, NULL);
122 return log_error_errno(r, "Failed to show table: %m");
126 if (table_get_rows(table) > 1)
127 printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
129 printf("No %s.\n", word);
135 static int list_sessions(int argc, char *argv[], void *userdata) {
136 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
137 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
138 _cleanup_(table_unrefp) Table *table = NULL;
139 sd_bus *bus = userdata;
145 (void) pager_open(arg_no_pager, false);
147 r = sd_bus_call_method(
149 "org.freedesktop.login1",
150 "/org/freedesktop/login1",
151 "org.freedesktop.login1.Manager",
156 return log_error_errno(r, "Failed to list sessions: %s", bus_error_message(&error, r));
158 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
160 return bus_log_parse_error(r);
162 table = table_new("SESSION", "UID", "USER", "SEAT", "TTY");
166 /* Right-align the first two fields (since they are numeric) */
167 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
168 (void) table_set_align_percent(table, TABLE_HEADER_CELL(1), 100);
171 _cleanup_(sd_bus_error_free) sd_bus_error error_tty = SD_BUS_ERROR_NULL;
172 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_tty = NULL;
173 const char *id, *user, *seat, *object, *tty = NULL;
176 r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object);
178 return bus_log_parse_error(r);
182 r = sd_bus_get_property(
184 "org.freedesktop.login1",
186 "org.freedesktop.login1.Session",
192 log_warning_errno(r, "Failed to get TTY for session %s: %s", id, bus_error_message(&error_tty, r));
194 r = sd_bus_message_read(reply_tty, "s", &tty);
196 return bus_log_parse_error(r);
199 r = table_add_many(table,
204 TABLE_STRING, strna(tty));
206 return log_error_errno(r, "Failed to add row to table: %m");
209 r = sd_bus_message_exit_container(reply);
211 return bus_log_parse_error(r);
213 return show_table(table, "sessions");
216 static int list_users(int argc, char *argv[], void *userdata) {
217 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
218 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
219 _cleanup_(table_unrefp) Table *table = NULL;
220 sd_bus *bus = userdata;
226 (void) pager_open(arg_no_pager, false);
228 r = sd_bus_call_method(
230 "org.freedesktop.login1",
231 "/org/freedesktop/login1",
232 "org.freedesktop.login1.Manager",
237 return log_error_errno(r, "Failed to list users: %s", bus_error_message(&error, r));
239 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
241 return bus_log_parse_error(r);
243 table = table_new("UID", "USER");
247 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
250 const char *user, *object;
253 r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object);
255 return bus_log_parse_error(r);
259 r = table_add_many(table,
263 return log_error_errno(r, "Failed to add row to table: %m");
266 r = sd_bus_message_exit_container(reply);
268 return bus_log_parse_error(r);
270 return show_table(table, "users");
273 static int list_seats(int argc, char *argv[], void *userdata) {
274 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
275 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
276 _cleanup_(table_unrefp) Table *table = NULL;
277 sd_bus *bus = userdata;
283 (void) pager_open(arg_no_pager, false);
285 r = sd_bus_call_method(
287 "org.freedesktop.login1",
288 "/org/freedesktop/login1",
289 "org.freedesktop.login1.Manager",
294 return log_error_errno(r, "Failed to list seats: %s", bus_error_message(&error, r));
296 r = sd_bus_message_enter_container(reply, 'a', "(so)");
298 return bus_log_parse_error(r);
300 table = table_new("SEAT");
305 const char *seat, *object;
307 r = sd_bus_message_read(reply, "(so)", &seat, &object);
309 return bus_log_parse_error(r);
313 r = table_add_cell(table, NULL, TABLE_STRING, seat);
315 return log_error_errno(r, "Failed to add row to table: %m");
318 r = sd_bus_message_exit_container(reply);
320 return bus_log_parse_error(r);
322 return show_table(table, "seats");
325 #if 0 /// UNNEEDED by elogind
326 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
327 _cleanup_free_ char *cgroup = NULL;
328 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
335 r = show_cgroup_get_unit_path_and_warn(bus, unit, &cgroup);
348 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
351 if (arg_transport == BUS_TRANSPORT_REMOTE)
354 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
356 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
359 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
361 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
367 typedef struct SessionStatusInfo {
371 struct dual_timestamp timestamp;
377 const char *remote_host;
378 const char *remote_user;
388 typedef struct UserStatusInfo {
392 struct dual_timestamp timestamp;
399 typedef struct SeatStatusInfo {
401 const char *active_session;
405 static void user_status_info_clear(UserStatusInfo *info) {
407 strv_free(info->sessions);
412 static void seat_status_info_clear(SeatStatusInfo *info) {
414 strv_free(info->sessions);
419 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
420 const char *contents;
423 r = sd_bus_message_peek_type(m, NULL, &contents);
427 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
431 r = sd_bus_message_read_basic(m, contents[0], userdata);
435 r = sd_bus_message_skip(m, contents+1);
439 r = sd_bus_message_exit_container(m);
446 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
453 r = sd_bus_message_enter_container(m, 'a', "(so)");
457 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
458 r = strv_extend(userdata, name);
465 return sd_bus_message_exit_container(m);
468 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
470 static const struct bus_properties_map map[] = {
471 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
472 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
473 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
474 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
475 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
476 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
477 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
478 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
479 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
480 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
481 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
482 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
483 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
484 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
485 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
486 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
487 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
488 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
489 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
493 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
494 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
495 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
496 char since2[FORMAT_TIMESTAMP_MAX];
498 SessionStatusInfo i = {};
501 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
503 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
510 printf("%s - ", strna(i.id));
513 printf("%s (%"PRIu32")\n", i.name, i.uid);
515 printf("%"PRIu32"\n", i.uid);
517 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
518 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
521 printf("\t Since: %s; %s\n", s2, s1);
523 printf("\t Since: %s\n", s2);
526 _cleanup_free_ char *t = NULL;
528 printf("\t Leader: %"PRIu32, i.leader);
530 get_process_comm(i.leader, &t);
537 if (!isempty(i.seat)) {
538 printf("\t Seat: %s", i.seat);
541 printf("; vc%u", i.vtnr);
547 printf("\t TTY: %s\n", i.tty);
549 printf("\t Display: %s\n", i.display);
551 if (i.remote_host && i.remote_user)
552 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
553 else if (i.remote_host)
554 printf("\t Remote: %s\n", i.remote_host);
555 else if (i.remote_user)
556 printf("\t Remote: user %s\n", i.remote_user);
558 printf("\t Remote: Yes\n");
561 printf("\t Service: %s", i.service);
564 printf("; type %s", i.type);
567 printf("; class %s", i.class);
571 printf("\t Type: %s", i.type);
574 printf("; class %s", i.class);
578 printf("\t Class: %s\n", i.class);
580 if (!isempty(i.desktop))
581 printf("\t Desktop: %s\n", i.desktop);
584 printf("\t State: %s\n", i.state);
587 printf("\t Unit: %s\n", i.scope);
588 #if 0 /// UNNEEDED by elogind
589 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
591 if (arg_transport == BUS_TRANSPORT_LOCAL) {
593 show_journal_by_unit(
598 i.timestamp.monotonic,
601 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
602 SD_JOURNAL_LOCAL_ONLY,
612 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
614 static const struct bus_properties_map map[] = {
615 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
616 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
617 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
618 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
619 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
620 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
621 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
622 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
623 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
627 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
628 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
629 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
630 char since2[FORMAT_TIMESTAMP_MAX];
632 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
635 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
637 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
645 printf("%s (%"PRIu32")\n", i.name, i.uid);
647 printf("%"PRIu32"\n", i.uid);
649 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
650 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
653 printf("\t Since: %s; %s\n", s2, s1);
655 printf("\t Since: %s\n", s2);
657 if (!isempty(i.state))
658 printf("\t State: %s\n", i.state);
660 if (!strv_isempty(i.sessions)) {
662 printf("\tSessions:");
664 STRV_FOREACH(l, i.sessions)
666 streq_ptr(*l, i.display) ? "*" : "",
672 printf("\t Linger: %s\n", yes_no(i.linger));
675 printf("\t Unit: %s\n", i.slice);
676 #if 0 /// UNNEEDED by elogind
677 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
679 show_journal_by_unit(
684 i.timestamp.monotonic,
687 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
688 SD_JOURNAL_LOCAL_ONLY,
697 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
699 static const struct bus_properties_map map[] = {
700 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
701 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
702 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
706 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
707 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
708 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
711 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, 0, &error, &m, &i);
713 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
720 printf("%s\n", strna(i.id));
722 if (!strv_isempty(i.sessions)) {
724 printf("\tSessions:");
726 STRV_FOREACH(l, i.sessions) {
727 if (streq_ptr(*l, i.active_session))
736 if (arg_transport == BUS_TRANSPORT_LOCAL) {
745 printf("\t Devices:\n");
747 show_sysfs(i.id, "\t\t ", c, get_output_flags());
753 #define property(name, fmt, ...) \
756 printf(fmt "\n", __VA_ARGS__); \
758 printf("%s=" fmt "\n", name, __VA_ARGS__); \
761 static int print_property(const char *name, sd_bus_message *m, bool value, bool all) {
763 const char *contents;
769 r = sd_bus_message_peek_type(m, &type, &contents);
775 case SD_BUS_TYPE_STRUCT:
777 if (contents[0] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
780 r = sd_bus_message_read(m, "(so)", &s, NULL);
782 return bus_log_parse_error(r);
784 if (all || !isempty(s))
785 property(name, "%s", s);
789 } else if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
792 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
794 return bus_log_parse_error(r);
796 if (!uid_is_valid(uid)) {
797 log_error("Invalid user ID: " UID_FMT, uid);
801 property(name, UID_FMT, uid);
806 case SD_BUS_TYPE_ARRAY:
808 if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
812 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
814 return bus_log_parse_error(r);
819 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
820 printf("%s%s", space ? " " : "", s);
828 return bus_log_parse_error(r);
830 r = sd_bus_message_exit_container(m);
832 return bus_log_parse_error(r);
842 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
854 r = bus_print_all_properties(bus, "org.freedesktop.login1", path, print_property, arg_property, arg_value, arg_all, NULL);
856 return bus_log_parse_error(r);
861 static int show_session(int argc, char *argv[], void *userdata) {
862 bool properties, new_line = false;
863 sd_bus *bus = userdata;
865 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
866 _cleanup_free_ char *path = NULL;
871 properties = !strstr(argv[0], "status");
873 (void) pager_open(arg_no_pager, false);
876 const char *session, *p = "/org/freedesktop/login1/session/self";
879 /* If no argument is specified inspect the manager itself */
880 return show_properties(bus, "/org/freedesktop/login1", &new_line);
882 /* And in the pretty case, show data of the calling session */
883 session = getenv("XDG_SESSION_ID");
885 r = get_session_path(bus, session, &error, &path);
887 log_error("Failed to get session path: %s", bus_error_message(&error, r));
893 return print_session_status_info(bus, p, &new_line);
896 for (i = 1; i < argc; i++) {
897 r = get_session_path(bus, argv[i], &error, &path);
899 log_error("Failed to get session path: %s", bus_error_message(&error, r));
904 r = show_properties(bus, path, &new_line);
906 r = print_session_status_info(bus, path, &new_line);
915 static int show_user(int argc, char *argv[], void *userdata) {
916 bool properties, new_line = false;
917 sd_bus *bus = userdata;
923 properties = !strstr(argv[0], "status");
925 (void) pager_open(arg_no_pager, false);
928 /* If not argument is specified inspect the manager
931 return show_properties(bus, "/org/freedesktop/login1", &new_line);
933 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
936 for (i = 1; i < argc; i++) {
937 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
938 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
939 const char *path = NULL;
942 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
944 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
946 r = sd_bus_call_method(
948 "org.freedesktop.login1",
949 "/org/freedesktop/login1",
950 "org.freedesktop.login1.Manager",
953 "u", (uint32_t) uid);
955 log_error("Failed to get user: %s", bus_error_message(&error, r));
959 r = sd_bus_message_read(reply, "o", &path);
961 return bus_log_parse_error(r);
964 r = show_properties(bus, path, &new_line);
966 r = print_user_status_info(bus, path, &new_line);
975 static int show_seat(int argc, char *argv[], void *userdata) {
976 bool properties, new_line = false;
977 sd_bus *bus = userdata;
983 properties = !strstr(argv[0], "status");
985 (void) pager_open(arg_no_pager, false);
988 /* If not argument is specified inspect the manager
991 return show_properties(bus, "/org/freedesktop/login1", &new_line);
993 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
996 for (i = 1; i < argc; i++) {
997 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
998 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
999 const char *path = NULL;
1001 r = sd_bus_call_method(
1003 "org.freedesktop.login1",
1004 "/org/freedesktop/login1",
1005 "org.freedesktop.login1.Manager",
1010 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1014 r = sd_bus_message_read(reply, "o", &path);
1016 return bus_log_parse_error(r);
1019 r = show_properties(bus, path, &new_line);
1021 r = print_seat_status_info(bus, path, &new_line);
1030 static int activate(int argc, char *argv[], void *userdata) {
1031 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1032 sd_bus *bus = userdata;
1033 char *short_argv[3];
1039 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1042 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1043 * session name, in which case logind will try to guess our session. */
1045 short_argv[0] = argv[0];
1046 short_argv[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1047 short_argv[2] = NULL;
1053 for (i = 1; i < argc; i++) {
1055 r = sd_bus_call_method(
1057 "org.freedesktop.login1",
1058 "/org/freedesktop/login1",
1059 "org.freedesktop.login1.Manager",
1060 streq(argv[0], "lock-session") ? "LockSession" :
1061 streq(argv[0], "unlock-session") ? "UnlockSession" :
1062 streq(argv[0], "terminate-session") ? "TerminateSession" :
1067 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1075 static int kill_session(int argc, char *argv[], void *userdata) {
1076 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1077 sd_bus *bus = userdata;
1083 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1086 arg_kill_who = "all";
1088 for (i = 1; i < argc; i++) {
1090 r = sd_bus_call_method(
1092 "org.freedesktop.login1",
1093 "/org/freedesktop/login1",
1094 "org.freedesktop.login1.Manager",
1097 "ssi", argv[i], arg_kill_who, arg_signal);
1099 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1107 static int enable_linger(int argc, char *argv[], void *userdata) {
1108 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1109 sd_bus *bus = userdata;
1110 char* short_argv[3];
1117 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1119 b = streq(argv[0], "enable-linger");
1122 /* No argument? Let's use an empty user name,
1123 * then logind will use our user. */
1125 short_argv[0] = argv[0];
1126 short_argv[1] = (char*) "";
1127 short_argv[2] = NULL;
1132 for (i = 1; i < argc; i++) {
1135 if (isempty(argv[i]))
1138 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1140 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1143 r = sd_bus_call_method(
1145 "org.freedesktop.login1",
1146 "/org/freedesktop/login1",
1147 "org.freedesktop.login1.Manager",
1150 "ubb", (uint32_t) uid, b, true);
1152 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1160 static int terminate_user(int argc, char *argv[], void *userdata) {
1161 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1162 sd_bus *bus = userdata;
1168 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1170 for (i = 1; i < argc; i++) {
1173 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1175 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1177 r = sd_bus_call_method(
1179 "org.freedesktop.login1",
1180 "/org/freedesktop/login1",
1181 "org.freedesktop.login1.Manager",
1184 "u", (uint32_t) uid);
1186 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1194 static int kill_user(int argc, char *argv[], void *userdata) {
1195 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1196 sd_bus *bus = userdata;
1202 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1205 arg_kill_who = "all";
1207 for (i = 1; i < argc; i++) {
1210 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1212 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1214 r = sd_bus_call_method(
1216 "org.freedesktop.login1",
1217 "/org/freedesktop/login1",
1218 "org.freedesktop.login1.Manager",
1221 "ui", (uint32_t) uid, arg_signal);
1223 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1231 static int attach(int argc, char *argv[], void *userdata) {
1232 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1233 sd_bus *bus = userdata;
1239 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1241 for (i = 2; i < argc; i++) {
1243 r = sd_bus_call_method(
1245 "org.freedesktop.login1",
1246 "/org/freedesktop/login1",
1247 "org.freedesktop.login1.Manager",
1250 "ssb", argv[1], argv[i], true);
1253 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1261 static int flush_devices(int argc, char *argv[], void *userdata) {
1262 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1263 sd_bus *bus = userdata;
1269 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1271 r = sd_bus_call_method(
1273 "org.freedesktop.login1",
1274 "/org/freedesktop/login1",
1275 "org.freedesktop.login1.Manager",
1280 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1285 static int lock_sessions(int argc, char *argv[], void *userdata) {
1286 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1287 sd_bus *bus = userdata;
1293 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1295 r = sd_bus_call_method(
1297 "org.freedesktop.login1",
1298 "/org/freedesktop/login1",
1299 "org.freedesktop.login1.Manager",
1300 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1304 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1309 static int terminate_seat(int argc, char *argv[], void *userdata) {
1310 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1311 sd_bus *bus = userdata;
1317 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1319 for (i = 1; i < argc; i++) {
1321 r = sd_bus_call_method(
1323 "org.freedesktop.login1",
1324 "/org/freedesktop/login1",
1325 "org.freedesktop.login1.Manager",
1330 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1338 static int help(int argc, char *argv[], void *userdata) {
1340 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1341 "Send control commands to or query the login manager.\n\n"
1342 " -h --help Show this help\n"
1343 " --version Show package version\n"
1344 " --no-pager Do not pipe output into a pager\n"
1345 #if 1 /// elogind supports --no-wall and --dry-run
1346 " --no-wall Do not print any wall message\n"
1347 " --dry-run Only print what would be done\n"
1348 " -q --quiet Suppress output\n"
1350 " --no-legend Do not show the headers and footers\n"
1351 " --no-ask-password Don't prompt for password\n"
1352 " -H --host=[USER@]HOST Operate on remote host\n"
1353 " -M --machine=CONTAINER Operate on local container\n"
1354 " -p --property=NAME Show only properties by this name\n"
1355 " -a --all Show all properties, including empty ones\n"
1356 " --value When showing properties, only print the value\n"
1357 " -l --full Do not ellipsize output\n"
1358 " --kill-who=WHO Who to send signal to\n"
1359 " -s --signal=SIGNAL Which signal to send\n"
1360 #if 0 /// UNNEEDED by elogind
1361 " -n --lines=INTEGER Number of journal entries to show\n"
1362 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1363 " short-iso, short-iso-precise, short-full,\n"
1364 " short-monotonic, short-unix, verbose, export,\n"
1365 " json, json-pretty, json-sse, cat)\n"
1367 /// elogind can cancel shutdowns and allows to ignore inhibitors
1368 " -c Cancel a pending shutdown or reboot\n"
1369 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n\n"
1371 "Session Commands:\n"
1372 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1373 " list-sessions List sessions\n"
1375 " list[-sessions] List sessions (default command)\n"
1377 " session-status [ID...] Show session status\n"
1378 " show-session [ID...] Show properties of sessions or the manager\n"
1379 " activate [ID] Activate a session\n"
1380 " lock-session [ID...] Screen lock one or more sessions\n"
1381 " unlock-session [ID...] Screen unlock one or more sessions\n"
1382 " lock-sessions Screen lock all current sessions\n"
1383 " unlock-sessions Screen unlock all current sessions\n"
1384 " terminate-session ID... Terminate one or more sessions\n"
1385 " kill-session ID... Send signal to processes of a session\n\n"
1387 " list-users List users\n"
1388 " user-status [USER...] Show user status\n"
1389 " show-user [USER...] Show properties of users or the manager\n"
1390 " enable-linger [USER...] Enable linger state of one or more users\n"
1391 " disable-linger [USER...] Disable linger state of one or more users\n"
1392 " terminate-user USER... Terminate all sessions of one or more users\n"
1393 " kill-user USER... Send signal to processes of a user\n\n"
1395 " list-seats List seats\n"
1396 " seat-status [NAME...] Show seat status\n"
1397 " show-seat [NAME...] Show properties of seats or the manager\n"
1398 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1399 " flush-devices Flush all device associations\n"
1400 #if 0 /// elogind adds some system commands to loginctl
1401 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1403 " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
1404 "System Commands:\n"
1405 " poweroff [TIME] [WALL...] Turn off the machine\n"
1406 " reboot [TIME] [WALL...] Reboot the machine\n"
1407 " suspend Suspend the machine to memory\n"
1408 " hibernate Suspend the machine to disk\n"
1409 " hybrid-sleep Suspend the machine to memory and disk\n"
1411 , program_invocation_short_name);
1416 static int parse_argv(int argc, char *argv[]) {
1419 ARG_VERSION = 0x100,
1422 #if 1 /// elogind supports --no-wall and --dry-run
1428 ARG_NO_ASK_PASSWORD,
1431 static const struct option options[] = {
1432 { "help", no_argument, NULL, 'h' },
1433 { "version", no_argument, NULL, ARG_VERSION },
1434 { "property", required_argument, NULL, 'p' },
1435 { "all", no_argument, NULL, 'a' },
1436 { "value", no_argument, NULL, ARG_VALUE },
1437 { "full", no_argument, NULL, 'l' },
1438 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1439 #if 1 /// elogind supports --no-wall, --dry-run and --quiet
1440 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1441 { "dry-run", no_argument, NULL, ARG_DRY_RUN },
1442 { "quiet", no_argument, NULL, 'q' },
1444 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1445 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1446 { "signal", required_argument, NULL, 's' },
1447 { "host", required_argument, NULL, 'H' },
1448 { "machine", required_argument, NULL, 'M' },
1449 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1450 #if 0 /// UNNEEDED by elogind
1451 { "lines", required_argument, NULL, 'n' },
1452 { "output", required_argument, NULL, 'o' },
1454 /// elogind allows to ignore inhibitors for system commands.
1455 { "ignore-inhibitors", no_argument, NULL, 'i' },
1465 #if 0 /// elogind adds some system commands to loginctl
1466 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1468 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1474 help(0, NULL, NULL);
1481 r = strv_extend(&arg_property, optarg);
1485 /* If the user asked for a particular
1486 * property, show it to him, even if it is
1504 #if 0 /// UNNEEDED by elogind
1506 if (safe_atou(optarg, &arg_lines) < 0) {
1507 log_error("Failed to parse lines '%s'", optarg);
1513 if (streq(optarg, "help")) {
1514 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
1518 arg_output = output_mode_from_string(optarg);
1519 if (arg_output < 0) {
1520 log_error("Unknown output '%s'.", optarg);
1527 arg_no_pager = true;
1529 #if 1 /// elogind supports --no-wall, -dry-run and --quiet
1548 case ARG_NO_ASK_PASSWORD:
1549 arg_ask_password = false;
1553 arg_kill_who = optarg;
1557 if (streq(optarg, "help")) {
1558 DUMP_STRING_TABLE(signal, int, _NSIG);
1562 arg_signal = signal_from_string(optarg);
1563 if (arg_signal < 0) {
1564 log_error("Failed to parse signal string %s.", optarg);
1570 arg_transport = BUS_TRANSPORT_REMOTE;
1575 arg_transport = BUS_TRANSPORT_MACHINE;
1578 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1580 arg_action = ACTION_CANCEL_SHUTDOWN;
1584 arg_ignore_inhibitors = true;
1591 assert_not_reached("Unhandled option");
1597 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1599 static const Verb verbs[] = {
1600 { "help", VERB_ANY, VERB_ANY, 0, help },
1601 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1602 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1604 { "list", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1605 { "list-sessions", VERB_ANY, 1, 0, list_sessions },
1607 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1608 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1609 { "activate", VERB_ANY, 2, 0, activate },
1610 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1611 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1612 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1613 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1614 { "terminate-session", 2, VERB_ANY, 0, activate },
1615 { "kill-session", 2, VERB_ANY, 0, kill_session },
1616 { "list-users", VERB_ANY, 1, 0, list_users },
1617 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1618 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1619 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1620 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1621 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1622 { "kill-user", 2, VERB_ANY, 0, kill_user },
1623 { "list-seats", VERB_ANY, 1, 0, list_seats },
1624 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1625 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1626 { "attach", 3, VERB_ANY, 0, attach },
1627 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1628 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1629 #if 1 /// elogind adds some system commands to loginctl
1630 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
1631 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
1632 { "suspend", VERB_ANY, 1, 0, start_special },
1633 { "hibernate", VERB_ANY, 1, 0, start_special },
1634 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1635 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1640 #if 1 /// elogind can do shutdown and allows its cancellation
1641 if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
1642 return elogind_cancel_shutdown(bus);
1644 return dispatch_verb(argc, argv, verbs, bus);
1647 int main(int argc, char *argv[]) {
1651 setlocale(LC_ALL, "");
1652 elogind_set_program_name(argv[0]);
1653 log_parse_environment();
1657 r = parse_argv(argc, argv);
1661 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1663 log_error_errno(r, "Failed to create bus connection: %m");
1667 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1669 r = loginctl_main(argc, argv, bus);
1672 /* make sure we terminate the bus connection first, and then close the
1673 * pager, see issue #3543 for the details. */
1674 sd_bus_flush_close_unref(bus);
1676 #if 0 /// elogind does that in elogind_cleanup()
1677 polkit_agent_close();
1680 strv_free(arg_property);
1682 #if 1 /// elogind has some own cleanups to do
1685 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;