1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
14 #include "alloc-util.h"
15 #include "bus-error.h"
16 //#include "bus-unit-util.h"
18 //#include "cgroup-show.h"
19 #include "cgroup-util.h"
20 //#include "format-table.h"
22 //#include "logs-show.h"
25 #include "parse-util.h"
26 #include "process-util.h"
28 #include "signal-util.h"
29 #include "spawn-polkit-agent.h"
30 //#include "string-table.h"
32 #include "sysfs-show.h"
33 #include "terminal-util.h"
34 #include "unit-name.h"
35 #include "user-util.h"
39 /// Additional includes needed by elogind
40 #include "eloginctl.h"
42 static char **arg_property = NULL;
43 static bool arg_all = false;
44 static bool arg_value = false;
45 static bool arg_full = false;
46 static bool arg_no_pager = false;
47 static bool arg_legend = true;
48 static const char *arg_kill_who = NULL;
49 static int arg_signal = SIGTERM;
50 #if 0 /// UNNEEDED by elogind
51 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
52 static char *arg_host = NULL;
53 static bool arg_ask_password = true;
54 static unsigned arg_lines = 10;
55 static OutputMode arg_output = OUTPUT_SHORT;
57 /// Instead we need this:
58 extern BusTransport arg_transport;
59 static char *arg_host = NULL;
60 extern bool arg_ask_password;
61 extern bool arg_dry_run;
62 extern bool arg_quiet;
63 extern bool arg_no_wall;
64 extern usec_t arg_when;
65 extern bool arg_ignore_inhibitors;
66 extern elogind_action arg_action;
69 static OutputFlags get_output_flags(void) {
72 arg_all * OUTPUT_SHOW_ALL |
73 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
74 colors_enabled() * OUTPUT_COLOR;
77 static int get_session_path(sd_bus *bus, const char *session_id, sd_bus_error *error, char **path) {
78 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
82 r = sd_bus_call_method(
84 "org.freedesktop.login1",
85 "/org/freedesktop/login1",
86 "org.freedesktop.login1.Manager",
93 r = sd_bus_message_read(reply, "o", &ans);
105 static int show_table(Table *table, const char *word) {
111 if (table_get_rows(table) > 1) {
112 r = table_set_sort(table, (size_t) 0, (size_t) -1);
114 return log_error_errno(r, "Failed to sort table: %m");
116 table_set_header(table, arg_legend);
118 r = table_print(table, NULL);
120 return log_error_errno(r, "Failed to show table: %m");
124 if (table_get_rows(table) > 1)
125 printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
127 printf("No %s.\n", word);
133 static int list_sessions(int argc, char *argv[], void *userdata) {
134 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
135 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
136 _cleanup_(table_unrefp) Table *table = NULL;
137 sd_bus *bus = userdata;
143 (void) pager_open(arg_no_pager, false);
145 r = sd_bus_call_method(
147 "org.freedesktop.login1",
148 "/org/freedesktop/login1",
149 "org.freedesktop.login1.Manager",
154 return log_error_errno(r, "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);
160 table = table_new("SESSION", "UID", "USER", "SEAT", "TTY");
164 /* Right-align the first two fields (since they are numeric) */
165 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
166 (void) table_set_align_percent(table, TABLE_HEADER_CELL(1), 100);
169 _cleanup_(sd_bus_error_free) sd_bus_error error_tty = SD_BUS_ERROR_NULL;
170 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_tty = NULL;
171 const char *id, *user, *seat, *object, *tty = NULL;
174 r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object);
176 return bus_log_parse_error(r);
180 r = sd_bus_get_property(
182 "org.freedesktop.login1",
184 "org.freedesktop.login1.Session",
190 log_warning_errno(r, "Failed to get TTY for session %s: %s", id, bus_error_message(&error_tty, r));
192 r = sd_bus_message_read(reply_tty, "s", &tty);
194 return bus_log_parse_error(r);
197 r = table_add_many(table,
202 TABLE_STRING, strna(tty));
204 return log_error_errno(r, "Failed to add row to table: %m");
207 r = sd_bus_message_exit_container(reply);
209 return bus_log_parse_error(r);
211 return show_table(table, "sessions");
214 static int list_users(int argc, char *argv[], void *userdata) {
215 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
216 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
217 _cleanup_(table_unrefp) Table *table = NULL;
218 sd_bus *bus = userdata;
224 (void) pager_open(arg_no_pager, false);
226 r = sd_bus_call_method(
228 "org.freedesktop.login1",
229 "/org/freedesktop/login1",
230 "org.freedesktop.login1.Manager",
235 return log_error_errno(r, "Failed to list users: %s", bus_error_message(&error, r));
237 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
239 return bus_log_parse_error(r);
241 table = table_new("UID", "USER");
245 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
248 const char *user, *object;
251 r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object);
253 return bus_log_parse_error(r);
257 r = table_add_many(table,
261 return log_error_errno(r, "Failed to add row to table: %m");
264 r = sd_bus_message_exit_container(reply);
266 return bus_log_parse_error(r);
268 return show_table(table, "users");
271 static int list_seats(int argc, char *argv[], void *userdata) {
272 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
273 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
274 _cleanup_(table_unrefp) Table *table = NULL;
275 sd_bus *bus = userdata;
281 (void) pager_open(arg_no_pager, false);
283 r = sd_bus_call_method(
285 "org.freedesktop.login1",
286 "/org/freedesktop/login1",
287 "org.freedesktop.login1.Manager",
292 return log_error_errno(r, "Failed to list seats: %s", bus_error_message(&error, r));
294 r = sd_bus_message_enter_container(reply, 'a', "(so)");
296 return bus_log_parse_error(r);
298 table = table_new("SEAT");
303 const char *seat, *object;
305 r = sd_bus_message_read(reply, "(so)", &seat, &object);
307 return bus_log_parse_error(r);
311 r = table_add_cell(table, NULL, TABLE_STRING, seat);
313 return log_error_errno(r, "Failed to add row to table: %m");
316 r = sd_bus_message_exit_container(reply);
318 return bus_log_parse_error(r);
320 return show_table(table, "seats");
323 #if 0 /// UNNEEDED by elogind
324 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
325 _cleanup_free_ char *cgroup = NULL;
326 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
333 r = show_cgroup_get_unit_path_and_warn(bus, unit, &cgroup);
346 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
349 if (arg_transport == BUS_TRANSPORT_REMOTE)
352 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
354 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
357 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
359 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
365 typedef struct SessionStatusInfo {
369 struct dual_timestamp timestamp;
375 const char *remote_host;
376 const char *remote_user;
386 typedef struct UserStatusInfo {
390 struct dual_timestamp timestamp;
397 typedef struct SeatStatusInfo {
399 const char *active_session;
403 static void user_status_info_clear(UserStatusInfo *info) {
405 strv_free(info->sessions);
410 static void seat_status_info_clear(SeatStatusInfo *info) {
412 strv_free(info->sessions);
417 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
418 const char *contents;
421 r = sd_bus_message_peek_type(m, NULL, &contents);
425 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
429 r = sd_bus_message_read_basic(m, contents[0], userdata);
433 r = sd_bus_message_skip(m, contents+1);
437 r = sd_bus_message_exit_container(m);
444 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
451 r = sd_bus_message_enter_container(m, 'a', "(so)");
455 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
456 r = strv_extend(userdata, name);
463 return sd_bus_message_exit_container(m);
466 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
468 static const struct bus_properties_map map[] = {
469 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
470 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
471 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
472 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
473 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
474 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
475 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
476 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
477 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
478 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
479 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
480 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
481 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
482 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
483 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
484 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
485 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
486 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
487 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
491 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
492 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
493 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
494 char since2[FORMAT_TIMESTAMP_MAX];
496 SessionStatusInfo i = {};
499 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
501 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
508 printf("%s - ", strna(i.id));
511 printf("%s (%"PRIu32")\n", i.name, i.uid);
513 printf("%"PRIu32"\n", i.uid);
515 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
516 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
519 printf("\t Since: %s; %s\n", s2, s1);
521 printf("\t Since: %s\n", s2);
524 _cleanup_free_ char *t = NULL;
526 printf("\t Leader: %"PRIu32, i.leader);
528 get_process_comm(i.leader, &t);
535 if (!isempty(i.seat)) {
536 printf("\t Seat: %s", i.seat);
539 printf("; vc%u", i.vtnr);
545 printf("\t TTY: %s\n", i.tty);
547 printf("\t Display: %s\n", i.display);
549 if (i.remote_host && i.remote_user)
550 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
551 else if (i.remote_host)
552 printf("\t Remote: %s\n", i.remote_host);
553 else if (i.remote_user)
554 printf("\t Remote: user %s\n", i.remote_user);
556 printf("\t Remote: Yes\n");
559 printf("\t Service: %s", i.service);
562 printf("; type %s", i.type);
565 printf("; class %s", i.class);
569 printf("\t Type: %s", i.type);
572 printf("; class %s", i.class);
576 printf("\t Class: %s\n", i.class);
578 if (!isempty(i.desktop))
579 printf("\t Desktop: %s\n", i.desktop);
582 printf("\t State: %s\n", i.state);
585 printf("\t Unit: %s\n", i.scope);
586 #if 0 /// UNNEEDED by elogind
587 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
589 if (arg_transport == BUS_TRANSPORT_LOCAL) {
591 show_journal_by_unit(
596 i.timestamp.monotonic,
599 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
600 SD_JOURNAL_LOCAL_ONLY,
610 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
612 static const struct bus_properties_map map[] = {
613 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
614 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
615 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
616 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
617 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
618 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
619 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
620 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
621 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
625 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
626 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
627 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX];
628 char since2[FORMAT_TIMESTAMP_MAX];
630 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
633 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
635 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
643 printf("%s (%"PRIu32")\n", i.name, i.uid);
645 printf("%"PRIu32"\n", i.uid);
647 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
648 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
651 printf("\t Since: %s; %s\n", s2, s1);
653 printf("\t Since: %s\n", s2);
655 if (!isempty(i.state))
656 printf("\t State: %s\n", i.state);
658 if (!strv_isempty(i.sessions)) {
660 printf("\tSessions:");
662 STRV_FOREACH(l, i.sessions)
664 streq_ptr(*l, i.display) ? "*" : "",
670 printf("\t Linger: %s\n", yes_no(i.linger));
673 printf("\t Unit: %s\n", i.slice);
674 #if 0 /// UNNEEDED by elogind
675 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
677 show_journal_by_unit(
682 i.timestamp.monotonic,
685 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
686 SD_JOURNAL_LOCAL_ONLY,
695 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
697 static const struct bus_properties_map map[] = {
698 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
699 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
700 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
704 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
705 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
706 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
709 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, 0, &error, &m, &i);
711 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
718 printf("%s\n", strna(i.id));
720 if (!strv_isempty(i.sessions)) {
722 printf("\tSessions:");
724 STRV_FOREACH(l, i.sessions) {
725 if (streq_ptr(*l, i.active_session))
734 if (arg_transport == BUS_TRANSPORT_LOCAL) {
743 printf("\t Devices:\n");
745 show_sysfs(i.id, "\t\t ", c, get_output_flags());
751 #define property(name, fmt, ...) \
754 printf(fmt "\n", __VA_ARGS__); \
756 printf("%s=" fmt "\n", name, __VA_ARGS__); \
759 static int print_property(const char *name, sd_bus_message *m, bool value, bool all) {
761 const char *contents;
767 r = sd_bus_message_peek_type(m, &type, &contents);
773 case SD_BUS_TYPE_STRUCT:
775 if (contents[0] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
778 r = sd_bus_message_read(m, "(so)", &s, NULL);
780 return bus_log_parse_error(r);
782 if (all || !isempty(s))
783 property(name, "%s", s);
787 } else if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
790 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
792 return bus_log_parse_error(r);
794 if (!uid_is_valid(uid)) {
795 log_error("Invalid user ID: " UID_FMT, uid);
799 property(name, UID_FMT, uid);
804 case SD_BUS_TYPE_ARRAY:
806 if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
810 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
812 return bus_log_parse_error(r);
817 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
818 printf("%s%s", space ? " " : "", s);
826 return bus_log_parse_error(r);
828 r = sd_bus_message_exit_container(m);
830 return bus_log_parse_error(r);
840 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
852 r = bus_print_all_properties(bus, "org.freedesktop.login1", path, print_property, arg_property, arg_value, arg_all, NULL);
854 return bus_log_parse_error(r);
859 static int show_session(int argc, char *argv[], void *userdata) {
860 bool properties, new_line = false;
861 sd_bus *bus = userdata;
863 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
864 _cleanup_free_ char *path = NULL;
869 properties = !strstr(argv[0], "status");
871 (void) pager_open(arg_no_pager, false);
874 const char *session, *p = "/org/freedesktop/login1/session/self";
877 /* If no argument is specified inspect the manager itself */
878 return show_properties(bus, "/org/freedesktop/login1", &new_line);
880 /* And in the pretty case, show data of the calling session */
881 session = getenv("XDG_SESSION_ID");
883 r = get_session_path(bus, session, &error, &path);
885 log_error("Failed to get session path: %s", bus_error_message(&error, r));
891 return print_session_status_info(bus, p, &new_line);
894 for (i = 1; i < argc; i++) {
895 r = get_session_path(bus, argv[i], &error, &path);
897 log_error("Failed to get session path: %s", bus_error_message(&error, r));
902 r = show_properties(bus, path, &new_line);
904 r = print_session_status_info(bus, path, &new_line);
913 static int show_user(int argc, char *argv[], void *userdata) {
914 bool properties, new_line = false;
915 sd_bus *bus = userdata;
921 properties = !strstr(argv[0], "status");
923 (void) pager_open(arg_no_pager, false);
926 /* If not argument is specified inspect the manager
929 return show_properties(bus, "/org/freedesktop/login1", &new_line);
931 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
934 for (i = 1; i < argc; i++) {
935 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
936 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
937 const char *path = NULL;
940 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
942 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
944 r = sd_bus_call_method(
946 "org.freedesktop.login1",
947 "/org/freedesktop/login1",
948 "org.freedesktop.login1.Manager",
951 "u", (uint32_t) uid);
953 log_error("Failed to get user: %s", bus_error_message(&error, r));
957 r = sd_bus_message_read(reply, "o", &path);
959 return bus_log_parse_error(r);
962 r = show_properties(bus, path, &new_line);
964 r = print_user_status_info(bus, path, &new_line);
973 static int show_seat(int argc, char *argv[], void *userdata) {
974 bool properties, new_line = false;
975 sd_bus *bus = userdata;
981 properties = !strstr(argv[0], "status");
983 (void) pager_open(arg_no_pager, false);
986 /* If not argument is specified inspect the manager
989 return show_properties(bus, "/org/freedesktop/login1", &new_line);
991 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
994 for (i = 1; i < argc; i++) {
995 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
996 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
997 const char *path = NULL;
999 r = sd_bus_call_method(
1001 "org.freedesktop.login1",
1002 "/org/freedesktop/login1",
1003 "org.freedesktop.login1.Manager",
1008 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1012 r = sd_bus_message_read(reply, "o", &path);
1014 return bus_log_parse_error(r);
1017 r = show_properties(bus, path, &new_line);
1019 r = print_seat_status_info(bus, path, &new_line);
1028 static int activate(int argc, char *argv[], void *userdata) {
1029 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1030 sd_bus *bus = userdata;
1031 char *short_argv[3];
1037 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1040 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1041 * session name, in which case logind will try to guess our session. */
1043 short_argv[0] = argv[0];
1044 short_argv[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1045 short_argv[2] = NULL;
1051 for (i = 1; i < argc; i++) {
1053 r = sd_bus_call_method(
1055 "org.freedesktop.login1",
1056 "/org/freedesktop/login1",
1057 "org.freedesktop.login1.Manager",
1058 streq(argv[0], "lock-session") ? "LockSession" :
1059 streq(argv[0], "unlock-session") ? "UnlockSession" :
1060 streq(argv[0], "terminate-session") ? "TerminateSession" :
1065 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1073 static int kill_session(int argc, char *argv[], void *userdata) {
1074 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1075 sd_bus *bus = userdata;
1081 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1084 arg_kill_who = "all";
1086 for (i = 1; i < argc; i++) {
1088 r = sd_bus_call_method(
1090 "org.freedesktop.login1",
1091 "/org/freedesktop/login1",
1092 "org.freedesktop.login1.Manager",
1095 "ssi", argv[i], arg_kill_who, arg_signal);
1097 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1105 static int enable_linger(int argc, char *argv[], void *userdata) {
1106 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1107 sd_bus *bus = userdata;
1108 char* short_argv[3];
1115 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1117 b = streq(argv[0], "enable-linger");
1120 /* No argument? Let's use an empty user name,
1121 * then logind will use our user. */
1123 short_argv[0] = argv[0];
1124 short_argv[1] = (char*) "";
1125 short_argv[2] = NULL;
1130 for (i = 1; i < argc; i++) {
1133 if (isempty(argv[i]))
1136 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1138 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1141 r = sd_bus_call_method(
1143 "org.freedesktop.login1",
1144 "/org/freedesktop/login1",
1145 "org.freedesktop.login1.Manager",
1148 "ubb", (uint32_t) uid, b, true);
1150 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1158 static int terminate_user(int argc, char *argv[], void *userdata) {
1159 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1160 sd_bus *bus = userdata;
1166 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1168 for (i = 1; i < argc; i++) {
1171 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1173 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1175 r = sd_bus_call_method(
1177 "org.freedesktop.login1",
1178 "/org/freedesktop/login1",
1179 "org.freedesktop.login1.Manager",
1182 "u", (uint32_t) uid);
1184 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1192 static int kill_user(int argc, char *argv[], void *userdata) {
1193 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1194 sd_bus *bus = userdata;
1200 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1203 arg_kill_who = "all";
1205 for (i = 1; i < argc; 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]);
1212 r = sd_bus_call_method(
1214 "org.freedesktop.login1",
1215 "/org/freedesktop/login1",
1216 "org.freedesktop.login1.Manager",
1219 "ui", (uint32_t) uid, arg_signal);
1221 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1229 static int attach(int argc, char *argv[], void *userdata) {
1230 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1231 sd_bus *bus = userdata;
1237 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1239 for (i = 2; i < argc; i++) {
1241 r = sd_bus_call_method(
1243 "org.freedesktop.login1",
1244 "/org/freedesktop/login1",
1245 "org.freedesktop.login1.Manager",
1248 "ssb", argv[1], argv[i], true);
1251 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1259 static int flush_devices(int argc, char *argv[], void *userdata) {
1260 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1261 sd_bus *bus = userdata;
1267 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1269 r = sd_bus_call_method(
1271 "org.freedesktop.login1",
1272 "/org/freedesktop/login1",
1273 "org.freedesktop.login1.Manager",
1278 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1283 static int lock_sessions(int argc, char *argv[], void *userdata) {
1284 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1285 sd_bus *bus = userdata;
1291 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1293 r = sd_bus_call_method(
1295 "org.freedesktop.login1",
1296 "/org/freedesktop/login1",
1297 "org.freedesktop.login1.Manager",
1298 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1302 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1307 static int terminate_seat(int argc, char *argv[], void *userdata) {
1308 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1309 sd_bus *bus = userdata;
1315 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1317 for (i = 1; i < argc; i++) {
1319 r = sd_bus_call_method(
1321 "org.freedesktop.login1",
1322 "/org/freedesktop/login1",
1323 "org.freedesktop.login1.Manager",
1328 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1336 static int help(int argc, char *argv[], void *userdata) {
1338 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1339 "Send control commands to or query the login manager.\n\n"
1340 " -h --help Show this help\n"
1341 " --version Show package version\n"
1342 " --no-pager Do not pipe output into a pager\n"
1343 #if 1 /// elogind supports --no-wall and --dry-run
1344 " --no-wall Do not print any wall message\n"
1345 " --dry-run Only print what would be done\n"
1346 " -q --quiet Suppress output\n"
1348 " --no-legend Do not show the headers and footers\n"
1349 " --no-ask-password Don't prompt for password\n"
1350 " -H --host=[USER@]HOST Operate on remote host\n"
1351 " -M --machine=CONTAINER Operate on local container\n"
1352 " -p --property=NAME Show only properties by this name\n"
1353 " -a --all Show all properties, including empty ones\n"
1354 " --value When showing properties, only print the value\n"
1355 " -l --full Do not ellipsize output\n"
1356 " --kill-who=WHO Who to send signal to\n"
1357 " -s --signal=SIGNAL Which signal to send\n"
1358 #if 0 /// UNNEEDED by elogind
1359 " -n --lines=INTEGER Number of journal entries to show\n"
1360 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1361 " short-iso, short-iso-precise, short-full,\n"
1362 " short-monotonic, short-unix, verbose, export,\n"
1363 " json, json-pretty, json-sse, cat)\n"
1365 /// elogind can cancel shutdowns and allows to ignore inhibitors
1366 " -c Cancel a pending shutdown or reboot\n"
1367 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n\n"
1369 "Session Commands:\n"
1370 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1371 " list-sessions List sessions\n"
1373 " list[-sessions] List sessions (default command)\n"
1375 " session-status [ID...] Show session status\n"
1376 " show-session [ID...] Show properties of sessions or the manager\n"
1377 " activate [ID] Activate a session\n"
1378 " lock-session [ID...] Screen lock one or more sessions\n"
1379 " unlock-session [ID...] Screen unlock one or more sessions\n"
1380 " lock-sessions Screen lock all current sessions\n"
1381 " unlock-sessions Screen unlock all current sessions\n"
1382 " terminate-session ID... Terminate one or more sessions\n"
1383 " kill-session ID... Send signal to processes of a session\n\n"
1385 " list-users List users\n"
1386 " user-status [USER...] Show user status\n"
1387 " show-user [USER...] Show properties of users or the manager\n"
1388 " enable-linger [USER...] Enable linger state of one or more users\n"
1389 " disable-linger [USER...] Disable linger state of one or more users\n"
1390 " terminate-user USER... Terminate all sessions of one or more users\n"
1391 " kill-user USER... Send signal to processes of a user\n\n"
1393 " list-seats List seats\n"
1394 " seat-status [NAME...] Show seat status\n"
1395 " show-seat [NAME...] Show properties of seats or the manager\n"
1396 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1397 " flush-devices Flush all device associations\n"
1398 #if 0 /// elogind adds some system commands to loginctl
1399 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1401 " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
1402 "System Commands:\n"
1403 " poweroff [TIME] [WALL...] Turn off the machine\n"
1404 " reboot [TIME] [WALL...] Reboot the machine\n"
1405 " suspend Suspend the machine to memory\n"
1406 " hibernate Suspend the machine to disk\n"
1407 " hybrid-sleep Suspend the machine to memory and disk\n"
1409 , program_invocation_short_name);
1414 static int parse_argv(int argc, char *argv[]) {
1417 ARG_VERSION = 0x100,
1420 #if 1 /// elogind supports --no-wall and --dry-run
1426 ARG_NO_ASK_PASSWORD,
1429 static const struct option options[] = {
1430 { "help", no_argument, NULL, 'h' },
1431 { "version", no_argument, NULL, ARG_VERSION },
1432 { "property", required_argument, NULL, 'p' },
1433 { "all", no_argument, NULL, 'a' },
1434 { "value", no_argument, NULL, ARG_VALUE },
1435 { "full", no_argument, NULL, 'l' },
1436 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1437 #if 1 /// elogind supports --no-wall, --dry-run and --quiet
1438 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1439 { "dry-run", no_argument, NULL, ARG_DRY_RUN },
1440 { "quiet", no_argument, NULL, 'q' },
1442 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1443 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1444 { "signal", required_argument, NULL, 's' },
1445 { "host", required_argument, NULL, 'H' },
1446 { "machine", required_argument, NULL, 'M' },
1447 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1448 #if 0 /// UNNEEDED by elogind
1449 { "lines", required_argument, NULL, 'n' },
1450 { "output", required_argument, NULL, 'o' },
1452 /// elogind allows to ignore inhibitors for system commands.
1453 { "ignore-inhibitors", no_argument, NULL, 'i' },
1463 #if 0 /// elogind adds some system commands to loginctl
1464 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1466 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1472 help(0, NULL, NULL);
1479 r = strv_extend(&arg_property, optarg);
1483 /* If the user asked for a particular
1484 * property, show it to him, even if it is
1502 #if 0 /// UNNEEDED by elogind
1504 if (safe_atou(optarg, &arg_lines) < 0) {
1505 log_error("Failed to parse lines '%s'", optarg);
1511 if (streq(optarg, "help")) {
1512 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
1516 arg_output = output_mode_from_string(optarg);
1517 if (arg_output < 0) {
1518 log_error("Unknown output '%s'.", optarg);
1525 arg_no_pager = true;
1527 #if 1 /// elogind supports --no-wall, -dry-run and --quiet
1546 case ARG_NO_ASK_PASSWORD:
1547 arg_ask_password = false;
1551 arg_kill_who = optarg;
1555 if (streq(optarg, "help")) {
1556 DUMP_STRING_TABLE(signal, int, _NSIG);
1560 arg_signal = signal_from_string(optarg);
1561 if (arg_signal < 0) {
1562 log_error("Failed to parse signal string %s.", optarg);
1568 arg_transport = BUS_TRANSPORT_REMOTE;
1573 arg_transport = BUS_TRANSPORT_MACHINE;
1576 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1578 arg_action = ACTION_CANCEL_SHUTDOWN;
1582 arg_ignore_inhibitors = true;
1589 assert_not_reached("Unhandled option");
1595 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1597 static const Verb verbs[] = {
1598 { "help", VERB_ANY, VERB_ANY, 0, help },
1599 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1600 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1602 { "list", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1603 { "list-sessions", VERB_ANY, 1, 0, list_sessions },
1605 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1606 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1607 { "activate", VERB_ANY, 2, 0, activate },
1608 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1609 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1610 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1611 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1612 { "terminate-session", 2, VERB_ANY, 0, activate },
1613 { "kill-session", 2, VERB_ANY, 0, kill_session },
1614 { "list-users", VERB_ANY, 1, 0, list_users },
1615 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1616 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1617 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1618 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1619 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1620 { "kill-user", 2, VERB_ANY, 0, kill_user },
1621 { "list-seats", VERB_ANY, 1, 0, list_seats },
1622 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1623 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1624 { "attach", 3, VERB_ANY, 0, attach },
1625 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1626 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1627 #if 1 /// elogind adds some system commands to loginctl
1628 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
1629 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
1630 { "suspend", VERB_ANY, 1, 0, start_special },
1631 { "hibernate", VERB_ANY, 1, 0, start_special },
1632 { "hybrid-sleep", 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;