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"
33 #include "sysfs-show.h"
34 #include "terminal-util.h"
35 #include "unit-name.h"
36 #include "user-util.h"
40 /// Additional includes needed by elogind
41 #include "eloginctl.h"
43 static char **arg_property = NULL;
44 static bool arg_all = false;
45 static bool arg_value = false;
46 static bool arg_full = false;
47 static bool arg_no_pager = false;
48 static bool arg_legend = true;
49 static const char *arg_kill_who = NULL;
50 static int arg_signal = SIGTERM;
51 #if 0 /// UNNEEDED by elogind
52 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
53 static char *arg_host = NULL;
54 static bool arg_ask_password = true;
55 static unsigned arg_lines = 10;
56 static OutputMode arg_output = OUTPUT_SHORT;
58 /// Instead we need this:
59 extern BusTransport arg_transport;
60 static char *arg_host = NULL;
61 extern bool arg_ask_password;
62 extern bool arg_dry_run;
63 extern bool arg_quiet;
64 extern bool arg_no_wall;
65 extern usec_t arg_when;
66 extern bool arg_ignore_inhibitors;
67 extern elogind_action arg_action;
70 static OutputFlags get_output_flags(void) {
73 arg_all * OUTPUT_SHOW_ALL |
74 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
75 colors_enabled() * OUTPUT_COLOR;
78 static int get_session_path(sd_bus *bus, const char *session_id, sd_bus_error *error, char **path) {
79 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
83 r = sd_bus_call_method(
85 "org.freedesktop.login1",
86 "/org/freedesktop/login1",
87 "org.freedesktop.login1.Manager",
94 r = sd_bus_message_read(reply, "o", &ans);
106 static int show_table(Table *table, const char *word) {
112 if (table_get_rows(table) > 1) {
113 r = table_set_sort(table, (size_t) 0, (size_t) -1);
115 return log_error_errno(r, "Failed to sort table: %m");
117 table_set_header(table, arg_legend);
119 r = table_print(table, NULL);
121 return log_error_errno(r, "Failed to show table: %m");
125 if (table_get_rows(table) > 1)
126 printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
128 printf("No %s.\n", word);
134 static int list_sessions(int argc, char *argv[], void *userdata) {
135 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
136 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
137 _cleanup_(table_unrefp) Table *table = NULL;
138 sd_bus *bus = userdata;
144 (void) pager_open(arg_no_pager, false);
146 r = sd_bus_call_method(
148 "org.freedesktop.login1",
149 "/org/freedesktop/login1",
150 "org.freedesktop.login1.Manager",
155 return log_error_errno(r, "Failed to list sessions: %s", bus_error_message(&error, r));
157 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
159 return bus_log_parse_error(r);
161 table = table_new("SESSION", "UID", "USER", "SEAT", "TTY");
165 /* Right-align the first two fields (since they are numeric) */
166 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
167 (void) table_set_align_percent(table, TABLE_HEADER_CELL(1), 100);
170 _cleanup_(sd_bus_error_free) sd_bus_error error_tty = SD_BUS_ERROR_NULL;
171 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_tty = NULL;
172 const char *id, *user, *seat, *object, *tty = NULL;
175 r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object);
177 return bus_log_parse_error(r);
181 r = sd_bus_get_property(
183 "org.freedesktop.login1",
185 "org.freedesktop.login1.Session",
191 log_warning_errno(r, "Failed to get TTY for session %s: %s", id, bus_error_message(&error_tty, r));
193 r = sd_bus_message_read(reply_tty, "s", &tty);
195 return bus_log_parse_error(r);
198 r = table_add_many(table,
203 TABLE_STRING, strna(tty));
205 return log_error_errno(r, "Failed to add row to table: %m");
208 r = sd_bus_message_exit_container(reply);
210 return bus_log_parse_error(r);
212 return show_table(table, "sessions");
215 static int list_users(int argc, char *argv[], void *userdata) {
216 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
217 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
218 _cleanup_(table_unrefp) Table *table = NULL;
219 sd_bus *bus = userdata;
225 (void) pager_open(arg_no_pager, false);
227 r = sd_bus_call_method(
229 "org.freedesktop.login1",
230 "/org/freedesktop/login1",
231 "org.freedesktop.login1.Manager",
236 return log_error_errno(r, "Failed to list users: %s", bus_error_message(&error, r));
238 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
240 return bus_log_parse_error(r);
242 table = table_new("UID", "USER");
246 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
249 const char *user, *object;
252 r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object);
254 return bus_log_parse_error(r);
258 r = table_add_many(table,
262 return log_error_errno(r, "Failed to add row to table: %m");
265 r = sd_bus_message_exit_container(reply);
267 return bus_log_parse_error(r);
269 return show_table(table, "users");
272 static int list_seats(int argc, char *argv[], void *userdata) {
273 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
274 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
275 _cleanup_(table_unrefp) Table *table = NULL;
276 sd_bus *bus = userdata;
282 (void) pager_open(arg_no_pager, false);
284 r = sd_bus_call_method(
286 "org.freedesktop.login1",
287 "/org/freedesktop/login1",
288 "org.freedesktop.login1.Manager",
293 return log_error_errno(r, "Failed to list seats: %s", bus_error_message(&error, r));
295 r = sd_bus_message_enter_container(reply, 'a', "(so)");
297 return bus_log_parse_error(r);
299 table = table_new("SEAT");
304 const char *seat, *object;
306 r = sd_bus_message_read(reply, "(so)", &seat, &object);
308 return bus_log_parse_error(r);
312 r = table_add_cell(table, NULL, TABLE_STRING, seat);
314 return log_error_errno(r, "Failed to add row to table: %m");
317 r = sd_bus_message_exit_container(reply);
319 return bus_log_parse_error(r);
321 return show_table(table, "seats");
324 #if 0 /// UNNEEDED by elogind
325 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
326 _cleanup_free_ char *cgroup = NULL;
327 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
334 r = show_cgroup_get_unit_path_and_warn(bus, unit, &cgroup);
347 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
350 if (arg_transport == BUS_TRANSPORT_REMOTE)
353 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
355 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
358 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
360 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
366 typedef struct SessionStatusInfo {
370 struct dual_timestamp timestamp;
376 const char *remote_host;
377 const char *remote_user;
387 typedef struct UserStatusInfo {
391 struct dual_timestamp timestamp;
398 typedef struct SeatStatusInfo {
400 const char *active_session;
404 static void user_status_info_clear(UserStatusInfo *info) {
406 strv_free(info->sessions);
411 static void seat_status_info_clear(SeatStatusInfo *info) {
413 strv_free(info->sessions);
418 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
419 const char *contents;
422 r = sd_bus_message_peek_type(m, NULL, &contents);
426 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
430 r = sd_bus_message_read_basic(m, contents[0], userdata);
434 r = sd_bus_message_skip(m, contents+1);
438 r = sd_bus_message_exit_container(m);
445 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
452 r = sd_bus_message_enter_container(m, 'a', "(so)");
456 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
457 r = strv_extend(userdata, name);
464 return sd_bus_message_exit_container(m);
467 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
469 static const struct bus_properties_map map[] = {
470 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
471 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
472 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
473 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
474 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
475 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
476 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
477 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
478 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
479 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
480 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
481 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
482 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
483 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
484 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
485 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
486 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
487 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
488 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
492 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
493 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
494 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
495 char since2[FORMAT_TIMESTAMP_MAX], *s2;
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], *s1;
628 char since2[FORMAT_TIMESTAMP_MAX], *s2;
629 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
632 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
634 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
642 printf("%s (%"PRIu32")\n", i.name, i.uid);
644 printf("%"PRIu32"\n", i.uid);
646 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
647 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
650 printf("\t Since: %s; %s\n", s2, s1);
652 printf("\t Since: %s\n", s2);
654 if (!isempty(i.state))
655 printf("\t State: %s\n", i.state);
657 if (!strv_isempty(i.sessions)) {
659 printf("\tSessions:");
661 STRV_FOREACH(l, i.sessions)
663 streq_ptr(*l, i.display) ? "*" : "",
669 printf("\t Linger: %s\n", yes_no(i.linger));
672 printf("\t Unit: %s\n", i.slice);
673 #if 0 /// UNNEEDED by elogind
674 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
676 show_journal_by_unit(
681 i.timestamp.monotonic,
684 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
685 SD_JOURNAL_LOCAL_ONLY,
694 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
696 static const struct bus_properties_map map[] = {
697 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
698 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
699 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
703 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
704 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
705 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
708 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, 0, &error, &m, &i);
710 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
717 printf("%s\n", strna(i.id));
719 if (!strv_isempty(i.sessions)) {
721 printf("\tSessions:");
723 STRV_FOREACH(l, i.sessions) {
724 if (streq_ptr(*l, i.active_session))
733 if (arg_transport == BUS_TRANSPORT_LOCAL) {
742 printf("\t Devices:\n");
744 show_sysfs(i.id, "\t\t ", c, get_output_flags());
750 #define property(name, fmt, ...) \
753 printf(fmt "\n", __VA_ARGS__); \
755 printf("%s=" fmt "\n", name, __VA_ARGS__); \
758 static int print_property(const char *name, sd_bus_message *m, bool value, bool all) {
760 const char *contents;
766 r = sd_bus_message_peek_type(m, &type, &contents);
772 case SD_BUS_TYPE_STRUCT:
774 if (contents[0] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
777 r = sd_bus_message_read(m, "(so)", &s, NULL);
779 return bus_log_parse_error(r);
781 if (all || !isempty(s))
782 property(name, "%s", s);
786 } else if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
789 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
791 return bus_log_parse_error(r);
793 if (!uid_is_valid(uid)) {
794 log_error("Invalid user ID: " UID_FMT, uid);
798 property(name, UID_FMT, uid);
803 case SD_BUS_TYPE_ARRAY:
805 if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
809 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
811 return bus_log_parse_error(r);
816 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
817 printf("%s%s", space ? " " : "", s);
825 return bus_log_parse_error(r);
827 r = sd_bus_message_exit_container(m);
829 return bus_log_parse_error(r);
839 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
851 r = bus_print_all_properties(bus, "org.freedesktop.login1", path, print_property, arg_property, arg_value, arg_all, NULL);
853 return bus_log_parse_error(r);
858 static int show_session(int argc, char *argv[], void *userdata) {
859 bool properties, new_line = false;
860 sd_bus *bus = userdata;
862 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
863 _cleanup_free_ char *path = NULL;
868 properties = !strstr(argv[0], "status");
870 (void) pager_open(arg_no_pager, false);
873 const char *session, *p = "/org/freedesktop/login1/session/self";
876 /* If no argument is specified inspect the manager itself */
877 return show_properties(bus, "/org/freedesktop/login1", &new_line);
879 /* And in the pretty case, show data of the calling session */
880 session = getenv("XDG_SESSION_ID");
882 r = get_session_path(bus, session, &error, &path);
884 log_error("Failed to get session path: %s", bus_error_message(&error, r));
890 return print_session_status_info(bus, p, &new_line);
893 for (i = 1; i < argc; i++) {
894 r = get_session_path(bus, argv[i], &error, &path);
896 log_error("Failed to get session path: %s", bus_error_message(&error, r));
901 r = show_properties(bus, path, &new_line);
903 r = print_session_status_info(bus, path, &new_line);
912 static int show_user(int argc, char *argv[], void *userdata) {
913 bool properties, new_line = false;
914 sd_bus *bus = userdata;
920 properties = !strstr(argv[0], "status");
922 (void) pager_open(arg_no_pager, false);
925 /* If not argument is specified inspect the manager
928 return show_properties(bus, "/org/freedesktop/login1", &new_line);
930 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
933 for (i = 1; i < argc; i++) {
934 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
935 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
936 const char *path = NULL;
939 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
941 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
943 r = sd_bus_call_method(
945 "org.freedesktop.login1",
946 "/org/freedesktop/login1",
947 "org.freedesktop.login1.Manager",
950 "u", (uint32_t) uid);
952 log_error("Failed to get user: %s", bus_error_message(&error, r));
956 r = sd_bus_message_read(reply, "o", &path);
958 return bus_log_parse_error(r);
961 r = show_properties(bus, path, &new_line);
963 r = print_user_status_info(bus, path, &new_line);
972 static int show_seat(int argc, char *argv[], void *userdata) {
973 bool properties, new_line = false;
974 sd_bus *bus = userdata;
980 properties = !strstr(argv[0], "status");
982 (void) pager_open(arg_no_pager, false);
985 /* If not argument is specified inspect the manager
988 return show_properties(bus, "/org/freedesktop/login1", &new_line);
990 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
993 for (i = 1; i < argc; i++) {
994 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
995 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
996 const char *path = NULL;
998 r = sd_bus_call_method(
1000 "org.freedesktop.login1",
1001 "/org/freedesktop/login1",
1002 "org.freedesktop.login1.Manager",
1007 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1011 r = sd_bus_message_read(reply, "o", &path);
1013 return bus_log_parse_error(r);
1016 r = show_properties(bus, path, &new_line);
1018 r = print_seat_status_info(bus, path, &new_line);
1027 static int activate(int argc, char *argv[], void *userdata) {
1028 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1029 sd_bus *bus = userdata;
1030 char *short_argv[3];
1036 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1039 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1040 * session name, in which case logind will try to guess our session. */
1042 short_argv[0] = argv[0];
1043 short_argv[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1044 short_argv[2] = NULL;
1050 for (i = 1; i < argc; i++) {
1052 r = sd_bus_call_method(
1054 "org.freedesktop.login1",
1055 "/org/freedesktop/login1",
1056 "org.freedesktop.login1.Manager",
1057 streq(argv[0], "lock-session") ? "LockSession" :
1058 streq(argv[0], "unlock-session") ? "UnlockSession" :
1059 streq(argv[0], "terminate-session") ? "TerminateSession" :
1064 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1072 static int kill_session(int argc, char *argv[], void *userdata) {
1073 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1074 sd_bus *bus = userdata;
1080 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1083 arg_kill_who = "all";
1085 for (i = 1; i < argc; i++) {
1087 r = sd_bus_call_method(
1089 "org.freedesktop.login1",
1090 "/org/freedesktop/login1",
1091 "org.freedesktop.login1.Manager",
1094 "ssi", argv[i], arg_kill_who, arg_signal);
1096 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1104 static int enable_linger(int argc, char *argv[], void *userdata) {
1105 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1106 sd_bus *bus = userdata;
1107 char* short_argv[3];
1114 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1116 b = streq(argv[0], "enable-linger");
1119 /* No argument? Let's use an empty user name,
1120 * then logind will use our user. */
1122 short_argv[0] = argv[0];
1123 short_argv[1] = (char*) "";
1124 short_argv[2] = NULL;
1129 for (i = 1; i < argc; i++) {
1132 if (isempty(argv[i]))
1135 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1137 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1140 r = sd_bus_call_method(
1142 "org.freedesktop.login1",
1143 "/org/freedesktop/login1",
1144 "org.freedesktop.login1.Manager",
1147 "ubb", (uint32_t) uid, b, true);
1149 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1157 static int terminate_user(int argc, char *argv[], void *userdata) {
1158 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1159 sd_bus *bus = userdata;
1165 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1167 for (i = 1; i < argc; i++) {
1170 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1172 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1174 r = sd_bus_call_method(
1176 "org.freedesktop.login1",
1177 "/org/freedesktop/login1",
1178 "org.freedesktop.login1.Manager",
1181 "u", (uint32_t) uid);
1183 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1191 static int kill_user(int argc, char *argv[], void *userdata) {
1192 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1193 sd_bus *bus = userdata;
1199 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1202 arg_kill_who = "all";
1204 for (i = 1; i < argc; i++) {
1207 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1209 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1211 r = sd_bus_call_method(
1213 "org.freedesktop.login1",
1214 "/org/freedesktop/login1",
1215 "org.freedesktop.login1.Manager",
1218 "ui", (uint32_t) uid, arg_signal);
1220 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1228 static int attach(int argc, char *argv[], void *userdata) {
1229 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1230 sd_bus *bus = userdata;
1236 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1238 for (i = 2; i < argc; i++) {
1240 r = sd_bus_call_method(
1242 "org.freedesktop.login1",
1243 "/org/freedesktop/login1",
1244 "org.freedesktop.login1.Manager",
1247 "ssb", argv[1], argv[i], true);
1250 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1258 static int flush_devices(int argc, char *argv[], void *userdata) {
1259 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1260 sd_bus *bus = userdata;
1266 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1268 r = sd_bus_call_method(
1270 "org.freedesktop.login1",
1271 "/org/freedesktop/login1",
1272 "org.freedesktop.login1.Manager",
1277 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1282 static int lock_sessions(int argc, char *argv[], void *userdata) {
1283 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1284 sd_bus *bus = userdata;
1290 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1292 r = sd_bus_call_method(
1294 "org.freedesktop.login1",
1295 "/org/freedesktop/login1",
1296 "org.freedesktop.login1.Manager",
1297 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1301 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1306 static int terminate_seat(int argc, char *argv[], void *userdata) {
1307 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1308 sd_bus *bus = userdata;
1314 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1316 for (i = 1; i < argc; i++) {
1318 r = sd_bus_call_method(
1320 "org.freedesktop.login1",
1321 "/org/freedesktop/login1",
1322 "org.freedesktop.login1.Manager",
1327 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1335 static int help(int argc, char *argv[], void *userdata) {
1337 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1338 "Send control commands to or query the login manager.\n\n"
1339 " -h --help Show this help\n"
1340 " --version Show package version\n"
1341 " --no-pager Do not pipe output into a pager\n"
1342 #if 1 /// elogind supports --no-wall and --dry-run
1343 " --no-wall Do not print any wall message\n"
1344 " --dry-run Only print what would be done\n"
1345 " -q --quiet Suppress output\n"
1347 " --no-legend Do not show the headers and footers\n"
1348 " --no-ask-password Don't prompt for password\n"
1349 " -H --host=[USER@]HOST Operate on remote host\n"
1350 " -M --machine=CONTAINER Operate on local container\n"
1351 " -p --property=NAME Show only properties by this name\n"
1352 " -a --all Show all properties, including empty ones\n"
1353 " --value When showing properties, only print the value\n"
1354 " -l --full Do not ellipsize output\n"
1355 " --kill-who=WHO Who to send signal to\n"
1356 " -s --signal=SIGNAL Which signal to send\n"
1357 #if 0 /// UNNEEDED by elogind
1358 " -n --lines=INTEGER Number of journal entries to show\n"
1359 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1360 " short-iso, short-iso-precise, short-full,\n"
1361 " short-monotonic, short-unix, verbose, export,\n"
1362 " json, json-pretty, json-sse, cat)\n"
1364 /// elogind can cancel shutdowns and allows to ignore inhibitors
1365 " -c Cancel a pending shutdown or reboot\n"
1366 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n\n"
1368 "Session Commands:\n"
1369 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1370 " list-sessions List sessions\n"
1372 " list[-sessions] List sessions (default command)\n"
1374 " session-status [ID...] Show session status\n"
1375 " show-session [ID...] Show properties of sessions or the manager\n"
1376 " activate [ID] Activate a session\n"
1377 " lock-session [ID...] Screen lock one or more sessions\n"
1378 " unlock-session [ID...] Screen unlock one or more sessions\n"
1379 " lock-sessions Screen lock all current sessions\n"
1380 " unlock-sessions Screen unlock all current sessions\n"
1381 " terminate-session ID... Terminate one or more sessions\n"
1382 " kill-session ID... Send signal to processes of a session\n\n"
1384 " list-users List users\n"
1385 " user-status [USER...] Show user status\n"
1386 " show-user [USER...] Show properties of users or the manager\n"
1387 " enable-linger [USER...] Enable linger state of one or more users\n"
1388 " disable-linger [USER...] Disable linger state of one or more users\n"
1389 " terminate-user USER... Terminate all sessions of one or more users\n"
1390 " kill-user USER... Send signal to processes of a user\n\n"
1392 " list-seats List seats\n"
1393 " seat-status [NAME...] Show seat status\n"
1394 " show-seat [NAME...] Show properties of seats or the manager\n"
1395 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1396 " flush-devices Flush all device associations\n"
1397 #if 0 /// elogind adds some system commands to loginctl
1398 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1400 " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
1401 "System Commands:\n"
1402 " poweroff [TIME] [WALL...] Turn off the machine\n"
1403 " reboot [TIME] [WALL...] Reboot the machine\n"
1404 " suspend Suspend the machine to memory\n"
1405 " hibernate Suspend the machine to disk\n"
1406 " hybrid-sleep Suspend the machine to memory and disk\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 arg_output = output_mode_from_string(optarg);
1511 if (arg_output < 0) {
1512 log_error("Unknown output '%s'.", optarg);
1519 arg_no_pager = true;
1521 #if 1 /// elogind supports --no-wall, -dry-run and --quiet
1540 case ARG_NO_ASK_PASSWORD:
1541 arg_ask_password = false;
1545 arg_kill_who = optarg;
1549 arg_signal = signal_from_string_try_harder(optarg);
1550 if (arg_signal < 0) {
1551 log_error("Failed to parse signal string %s.", optarg);
1557 arg_transport = BUS_TRANSPORT_REMOTE;
1562 arg_transport = BUS_TRANSPORT_MACHINE;
1565 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1567 arg_action = ACTION_CANCEL_SHUTDOWN;
1571 arg_ignore_inhibitors = true;
1578 assert_not_reached("Unhandled option");
1584 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1586 static const Verb verbs[] = {
1587 { "help", VERB_ANY, VERB_ANY, 0, help },
1588 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1589 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1591 { "list", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1592 { "list-sessions", VERB_ANY, 1, 0, list_sessions },
1594 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1595 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1596 { "activate", VERB_ANY, 2, 0, activate },
1597 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1598 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1599 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1600 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1601 { "terminate-session", 2, VERB_ANY, 0, activate },
1602 { "kill-session", 2, VERB_ANY, 0, kill_session },
1603 { "list-users", VERB_ANY, 1, 0, list_users },
1604 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1605 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1606 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1607 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1608 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1609 { "kill-user", 2, VERB_ANY, 0, kill_user },
1610 { "list-seats", VERB_ANY, 1, 0, list_seats },
1611 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1612 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1613 { "attach", 3, VERB_ANY, 0, attach },
1614 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1615 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1616 #if 1 /// elogind adds some system commands to loginctl
1617 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
1618 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
1619 { "suspend", VERB_ANY, 1, 0, start_special },
1620 { "hibernate", VERB_ANY, 1, 0, start_special },
1621 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1622 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1627 #if 1 /// elogind can do shutdown and allows its cancellation
1628 if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
1629 return elogind_cancel_shutdown(bus);
1631 return dispatch_verb(argc, argv, verbs, bus);
1634 int main(int argc, char *argv[]) {
1638 setlocale(LC_ALL, "");
1639 elogind_set_program_name(argv[0]);
1640 log_parse_environment();
1644 r = parse_argv(argc, argv);
1648 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1650 log_error_errno(r, "Failed to create bus connection: %m");
1654 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1656 r = loginctl_main(argc, argv, bus);
1659 /* make sure we terminate the bus connection first, and then close the
1660 * pager, see issue #3543 for the details. */
1661 sd_bus_flush_close_unref(bus);
1663 #if 0 /// elogind does that in elogind_cleanup()
1664 polkit_agent_close();
1667 strv_free(arg_property);
1669 #if 1 /// elogind has some own cleanups to do
1672 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;