2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "alloc-util.h"
29 #include "bus-error.h"
31 //#include "cgroup-show.h"
32 #include "cgroup-util.h"
34 //#include "logs-show.h"
37 #include "parse-util.h"
38 #include "process-util.h"
39 #include "signal-util.h"
40 //#include "spawn-polkit-agent.h"
42 #include "sysfs-show.h"
43 #include "terminal-util.h"
44 #include "unit-name.h"
45 #include "user-util.h"
49 /// Additional includes needed by elogind
50 #include "eloginctl.h"
52 static char **arg_property = NULL;
53 static bool arg_all = false;
54 static bool arg_value = false;
55 static bool arg_full = false;
56 static bool arg_no_pager = false;
57 static bool arg_legend = true;
58 static const char *arg_kill_who = NULL;
59 static int arg_signal = SIGTERM;
60 #if 0 /// UNNEEDED by elogind
61 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
62 static char *arg_host = NULL;
63 static bool arg_ask_password = true;
64 static unsigned arg_lines = 10;
65 static OutputMode arg_output = OUTPUT_SHORT;
67 /// Instead we need this:
68 extern BusTransport arg_transport;
69 static char *arg_host = NULL;
70 extern bool arg_ask_password;
71 extern bool arg_no_wall;
72 extern usec_t arg_when;
73 extern bool arg_ignore_inhibitors;
74 extern elogind_action arg_action;
77 #if 0 /// UNNEEDED by elogind
78 static void polkit_agent_open_if_enabled(void) {
80 /* Open the polkit agent as a child process if necessary */
82 if (!arg_ask_password)
85 if (arg_transport != BUS_TRANSPORT_LOCAL)
91 static OutputFlags get_output_flags(void) {
94 arg_all * OUTPUT_SHOW_ALL |
95 arg_full * OUTPUT_FULL_WIDTH |
96 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
97 colors_enabled() * OUTPUT_COLOR;
101 static int get_session_path(sd_bus *bus, const char *session_id, sd_bus_error *error, char **path) {
102 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
106 r = sd_bus_call_method(
108 "org.freedesktop.login1",
109 "/org/freedesktop/login1",
110 "org.freedesktop.login1.Manager",
117 r = sd_bus_message_read(reply, "o", &ans);
129 static int list_sessions(int argc, char *argv[], void *userdata) {
130 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
131 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
132 const char *id, *user, *seat, *object;
133 sd_bus *bus = userdata;
141 pager_open(arg_no_pager, false);
143 r = sd_bus_call_method(
145 "org.freedesktop.login1",
146 "/org/freedesktop/login1",
147 "org.freedesktop.login1.Manager",
152 log_error("Failed to list sessions: %s", bus_error_message(&error, r));
156 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
158 return bus_log_parse_error(r);
161 printf("%10s %10s %-16s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT", "TTY");
163 while ((r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object)) > 0) {
164 _cleanup_(sd_bus_error_free) sd_bus_error error2 = SD_BUS_ERROR_NULL;
165 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply2 = NULL;
166 _cleanup_free_ char *path = NULL;
167 const char *tty = NULL;
169 r = get_session_path(bus, id, &error2, &path);
171 log_warning("Failed to get session path: %s", bus_error_message(&error, r));
173 r = sd_bus_get_property(
175 "org.freedesktop.login1",
177 "org.freedesktop.login1.Session",
183 log_warning("Failed to get TTY for session %s: %s",
184 id, bus_error_message(&error2, r));
186 r = sd_bus_message_read(reply2, "s", &tty);
188 return bus_log_parse_error(r);
192 printf("%10s %10"PRIu32" %-16s %-16s %-16s\n", id, uid, user, seat, strna(tty));
196 return bus_log_parse_error(r);
199 printf("\n%u sessions listed.\n", k);
204 static int list_users(int argc, char *argv[], void *userdata) {
205 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
206 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
207 const char *user, *object;
208 sd_bus *bus = userdata;
216 pager_open(arg_no_pager, false);
218 r = sd_bus_call_method(
220 "org.freedesktop.login1",
221 "/org/freedesktop/login1",
222 "org.freedesktop.login1.Manager",
227 log_error("Failed to list users: %s", bus_error_message(&error, r));
231 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
233 return bus_log_parse_error(r);
236 printf("%10s %-16s\n", "UID", "USER");
238 while ((r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object)) > 0) {
239 printf("%10"PRIu32" %-16s\n", uid, user);
243 return bus_log_parse_error(r);
246 printf("\n%u users listed.\n", k);
251 static int list_seats(int argc, char *argv[], void *userdata) {
252 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
253 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
254 const char *seat, *object;
255 sd_bus *bus = userdata;
261 pager_open(arg_no_pager, false);
263 r = sd_bus_call_method(
265 "org.freedesktop.login1",
266 "/org/freedesktop/login1",
267 "org.freedesktop.login1.Manager",
272 log_error("Failed to list seats: %s", bus_error_message(&error, r));
276 r = sd_bus_message_enter_container(reply, 'a', "(so)");
278 return bus_log_parse_error(r);
281 printf("%-16s\n", "SEAT");
283 while ((r = sd_bus_message_read(reply, "(so)", &seat, &object)) > 0) {
284 printf("%-16s\n", seat);
288 return bus_log_parse_error(r);
291 printf("\n%u seats listed.\n", k);
296 #if 0 /// UNNEEDED by elogind
297 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
298 _cleanup_free_ char *cgroup = NULL;
299 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
306 r = show_cgroup_get_unit_path_and_warn(bus, unit, &cgroup);
319 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
322 if (arg_transport == BUS_TRANSPORT_REMOTE)
325 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
327 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
330 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
332 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
338 typedef struct SessionStatusInfo {
342 struct dual_timestamp timestamp;
359 typedef struct UserStatusInfo {
363 struct dual_timestamp timestamp;
370 typedef struct SeatStatusInfo {
372 char *active_session;
376 static void session_status_info_clear(SessionStatusInfo *info) {
383 free(info->remote_host);
384 free(info->remote_user);
395 static void user_status_info_clear(UserStatusInfo *info) {
399 strv_free(info->sessions);
406 static void seat_status_info_clear(SeatStatusInfo *info) {
409 free(info->active_session);
410 strv_free(info->sessions);
415 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
416 const char *contents;
419 r = sd_bus_message_peek_type(m, NULL, &contents);
423 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
427 if (contents[0] == 's' || contents[0] == 'o') {
429 char **p = (char **) userdata;
431 r = sd_bus_message_read_basic(m, contents[0], &s);
435 r = free_and_strdup(p, s);
439 r = sd_bus_message_read_basic(m, contents[0], userdata);
444 r = sd_bus_message_skip(m, contents+1);
448 r = sd_bus_message_exit_container(m);
455 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
462 r = sd_bus_message_enter_container(m, 'a', "(so)");
466 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
467 r = strv_extend(userdata, name);
474 return sd_bus_message_exit_container(m);
477 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
479 static const struct bus_properties_map map[] = {
480 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
481 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
482 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
483 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
484 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
485 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
486 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
487 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
488 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
489 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
490 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
491 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
492 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
493 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
494 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
495 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
496 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
497 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
498 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
502 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
503 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
504 char since2[FORMAT_TIMESTAMP_MAX], *s2;
505 _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
508 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &error, &i);
510 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
517 printf("%s - ", strna(i.id));
520 printf("%s (%"PRIu32")\n", i.name, i.uid);
522 printf("%"PRIu32"\n", i.uid);
524 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
525 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
528 printf("\t Since: %s; %s\n", s2, s1);
530 printf("\t Since: %s\n", s2);
533 _cleanup_free_ char *t = NULL;
535 printf("\t Leader: %"PRIu32, i.leader);
537 get_process_comm(i.leader, &t);
544 if (!isempty(i.seat)) {
545 printf("\t Seat: %s", i.seat);
548 printf("; vc%u", i.vtnr);
554 printf("\t TTY: %s\n", i.tty);
556 printf("\t Display: %s\n", i.display);
558 if (i.remote_host && i.remote_user)
559 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
560 else if (i.remote_host)
561 printf("\t Remote: %s\n", i.remote_host);
562 else if (i.remote_user)
563 printf("\t Remote: user %s\n", i.remote_user);
565 printf("\t Remote: Yes\n");
568 printf("\t Service: %s", i.service);
571 printf("; type %s", i.type);
574 printf("; class %s", i.class);
578 printf("\t Type: %s", i.type);
581 printf("; class %s", i.class);
585 printf("\t Class: %s\n", i.class);
587 if (!isempty(i.desktop))
588 printf("\t Desktop: %s\n", i.desktop);
591 printf("\t State: %s\n", i.state);
594 printf("\t Unit: %s\n", i.scope);
595 #if 0 /// UNNEEDED by elogind
596 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
598 if (arg_transport == BUS_TRANSPORT_LOCAL) {
600 show_journal_by_unit(
605 i.timestamp.monotonic,
608 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
609 SD_JOURNAL_LOCAL_ONLY,
619 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
621 static const struct bus_properties_map map[] = {
622 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
623 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
624 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
625 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
626 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
627 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
628 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
629 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
630 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
634 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
635 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
636 char since2[FORMAT_TIMESTAMP_MAX], *s2;
637 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
640 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &error, &i);
642 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
650 printf("%s (%"PRIu32")\n", i.name, i.uid);
652 printf("%"PRIu32"\n", i.uid);
654 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
655 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
658 printf("\t Since: %s; %s\n", s2, s1);
660 printf("\t Since: %s\n", s2);
662 if (!isempty(i.state))
663 printf("\t State: %s\n", i.state);
665 if (!strv_isempty(i.sessions)) {
667 printf("\tSessions:");
669 STRV_FOREACH(l, i.sessions)
671 streq_ptr(*l, i.display) ? "*" : "",
677 printf("\t Linger: %s\n", yes_no(i.linger));
680 printf("\t Unit: %s\n", i.slice);
681 #if 0 /// UNNEEDED by elogind
682 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
684 show_journal_by_unit(
689 i.timestamp.monotonic,
692 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
693 SD_JOURNAL_LOCAL_ONLY,
702 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
704 static const struct bus_properties_map map[] = {
705 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
706 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
707 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
711 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
712 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
715 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &error, &i);
717 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
724 printf("%s\n", strna(i.id));
726 if (!strv_isempty(i.sessions)) {
728 printf("\tSessions:");
730 STRV_FOREACH(l, i.sessions) {
731 if (streq_ptr(*l, i.active_session))
740 if (arg_transport == BUS_TRANSPORT_LOCAL) {
749 printf("\t Devices:\n");
751 show_sysfs(i.id, "\t\t ", c);
757 #define property(name, fmt, ...) \
760 printf(fmt "\n", __VA_ARGS__); \
762 printf("%s=" fmt "\n", name, __VA_ARGS__); \
765 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
772 if (arg_property && !strv_find(arg_property, name))
773 /* skip what we didn't read */
774 return sd_bus_message_skip(m, contents);
776 switch (contents[0]) {
778 case SD_BUS_TYPE_STRUCT_BEGIN:
780 if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
783 r = sd_bus_message_read(m, "(so)", &s, NULL);
785 return bus_log_parse_error(r);
787 if (arg_all || !isempty(s))
788 property(name, "%s", s);
792 } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
795 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
797 return bus_log_parse_error(r);
799 if (!uid_is_valid(uid)) {
800 log_error("Invalid user ID: " UID_FMT, uid);
804 property(name, UID_FMT, uid);
810 case SD_BUS_TYPE_ARRAY:
812 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
816 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
818 return bus_log_parse_error(r);
823 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
824 printf("%s%s", space ? " " : "", s);
828 if (space || !arg_value)
832 return bus_log_parse_error(r);
834 r = sd_bus_message_exit_container(m);
836 return bus_log_parse_error(r);
844 r = bus_print_property(name, m, arg_value, arg_all);
846 return bus_log_parse_error(r);
849 r = sd_bus_message_skip(m, contents);
851 return bus_log_parse_error(r);
854 printf("%s=[unprintable]\n", name);
860 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
861 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
862 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
869 r = sd_bus_call_method(
871 "org.freedesktop.login1",
873 "org.freedesktop.DBus.Properties",
879 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
881 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
883 return bus_log_parse_error(r);
890 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
891 const char *name, *contents;
893 r = sd_bus_message_read(reply, "s", &name);
895 return bus_log_parse_error(r);
897 r = sd_bus_message_peek_type(reply, NULL, &contents);
899 return bus_log_parse_error(r);
901 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
903 return bus_log_parse_error(r);
905 r = print_property(name, reply, contents);
909 r = sd_bus_message_exit_container(reply);
911 return bus_log_parse_error(r);
913 r = sd_bus_message_exit_container(reply);
915 return bus_log_parse_error(r);
918 return bus_log_parse_error(r);
920 r = sd_bus_message_exit_container(reply);
922 return bus_log_parse_error(r);
927 static int show_session(int argc, char *argv[], void *userdata) {
928 bool properties, new_line = false;
929 sd_bus *bus = userdata;
931 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
932 _cleanup_free_ char *path = NULL;
937 properties = !strstr(argv[0], "status");
939 pager_open(arg_no_pager, false);
942 const char *session, *p = "/org/freedesktop/login1/session/self";
945 /* If no argument is specified inspect the manager itself */
946 return show_properties(bus, "/org/freedesktop/login1", &new_line);
948 /* And in the pretty case, show data of the calling session */
949 session = getenv("XDG_SESSION_ID");
951 r = get_session_path(bus, session, &error, &path);
953 log_error("Failed to get session path: %s", bus_error_message(&error, r));
959 return print_session_status_info(bus, p, &new_line);
962 for (i = 1; i < argc; i++) {
963 r = get_session_path(bus, argv[i], &error, &path);
965 log_error("Failed to get session path: %s", bus_error_message(&error, r));
970 r = show_properties(bus, path, &new_line);
972 r = print_session_status_info(bus, path, &new_line);
981 static int show_user(int argc, char *argv[], void *userdata) {
982 bool properties, new_line = false;
983 sd_bus *bus = userdata;
989 properties = !strstr(argv[0], "status");
991 pager_open(arg_no_pager, false);
994 /* If not argument is specified inspect the manager
997 return show_properties(bus, "/org/freedesktop/login1", &new_line);
999 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
1002 for (i = 1; i < argc; i++) {
1003 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1004 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1005 const char *path = NULL;
1008 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1010 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1012 r = sd_bus_call_method(
1014 "org.freedesktop.login1",
1015 "/org/freedesktop/login1",
1016 "org.freedesktop.login1.Manager",
1019 "u", (uint32_t) uid);
1021 log_error("Failed to get user: %s", bus_error_message(&error, r));
1025 r = sd_bus_message_read(reply, "o", &path);
1027 return bus_log_parse_error(r);
1030 r = show_properties(bus, path, &new_line);
1032 r = print_user_status_info(bus, path, &new_line);
1041 static int show_seat(int argc, char *argv[], void *userdata) {
1042 bool properties, new_line = false;
1043 sd_bus *bus = userdata;
1049 properties = !strstr(argv[0], "status");
1051 pager_open(arg_no_pager, false);
1054 /* If not argument is specified inspect the manager
1057 return show_properties(bus, "/org/freedesktop/login1", &new_line);
1059 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
1062 for (i = 1; i < argc; i++) {
1063 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1064 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1065 const char *path = NULL;
1067 r = sd_bus_call_method(
1069 "org.freedesktop.login1",
1070 "/org/freedesktop/login1",
1071 "org.freedesktop.login1.Manager",
1076 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1080 r = sd_bus_message_read(reply, "o", &path);
1082 return bus_log_parse_error(r);
1085 r = show_properties(bus, path, &new_line);
1087 r = print_seat_status_info(bus, path, &new_line);
1096 static int activate(int argc, char *argv[], void *userdata) {
1097 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1098 sd_bus *bus = userdata;
1099 char *short_argv[3];
1105 polkit_agent_open_if_enabled();
1108 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1109 * session name, in which case logind will try to guess our session. */
1111 short_argv[0] = argv[0];
1112 short_argv[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1113 short_argv[2] = NULL;
1119 for (i = 1; i < argc; i++) {
1121 r = sd_bus_call_method(
1123 "org.freedesktop.login1",
1124 "/org/freedesktop/login1",
1125 "org.freedesktop.login1.Manager",
1126 streq(argv[0], "lock-session") ? "LockSession" :
1127 streq(argv[0], "unlock-session") ? "UnlockSession" :
1128 streq(argv[0], "terminate-session") ? "TerminateSession" :
1133 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1141 static int kill_session(int argc, char *argv[], void *userdata) {
1142 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1143 sd_bus *bus = userdata;
1149 polkit_agent_open_if_enabled();
1152 arg_kill_who = "all";
1154 for (i = 1; i < argc; i++) {
1156 r = sd_bus_call_method(
1158 "org.freedesktop.login1",
1159 "/org/freedesktop/login1",
1160 "org.freedesktop.login1.Manager",
1163 "ssi", argv[i], arg_kill_who, arg_signal);
1165 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1173 static int enable_linger(int argc, char *argv[], void *userdata) {
1174 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1175 sd_bus *bus = userdata;
1176 char* short_argv[3];
1183 polkit_agent_open_if_enabled();
1185 b = streq(argv[0], "enable-linger");
1188 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1189 * session name, in which case logind will try to guess our session. */
1191 short_argv[0] = argv[0];
1192 short_argv[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1193 short_argv[2] = NULL;
1198 for (i = 1; i < argc; i++) {
1201 if (isempty(argv[i]))
1204 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1206 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1209 r = sd_bus_call_method(
1211 "org.freedesktop.login1",
1212 "/org/freedesktop/login1",
1213 "org.freedesktop.login1.Manager",
1216 "ubb", (uint32_t) uid, b, true);
1218 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1226 static int terminate_user(int argc, char *argv[], void *userdata) {
1227 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1228 sd_bus *bus = userdata;
1234 polkit_agent_open_if_enabled();
1236 for (i = 1; i < argc; i++) {
1239 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1241 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1243 r = sd_bus_call_method(
1245 "org.freedesktop.login1",
1246 "/org/freedesktop/login1",
1247 "org.freedesktop.login1.Manager",
1250 "u", (uint32_t) uid);
1252 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1260 static int kill_user(int argc, char *argv[], void *userdata) {
1261 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1262 sd_bus *bus = userdata;
1268 polkit_agent_open_if_enabled();
1271 arg_kill_who = "all";
1273 for (i = 1; i < argc; i++) {
1276 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1278 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1280 r = sd_bus_call_method(
1282 "org.freedesktop.login1",
1283 "/org/freedesktop/login1",
1284 "org.freedesktop.login1.Manager",
1287 "ui", (uint32_t) uid, arg_signal);
1289 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1297 static int attach(int argc, char *argv[], void *userdata) {
1298 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1299 sd_bus *bus = userdata;
1305 polkit_agent_open_if_enabled();
1307 for (i = 2; i < argc; i++) {
1309 r = sd_bus_call_method(
1311 "org.freedesktop.login1",
1312 "/org/freedesktop/login1",
1313 "org.freedesktop.login1.Manager",
1316 "ssb", argv[1], argv[i], true);
1319 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1327 static int flush_devices(int argc, char *argv[], void *userdata) {
1328 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1329 sd_bus *bus = userdata;
1335 polkit_agent_open_if_enabled();
1337 r = sd_bus_call_method(
1339 "org.freedesktop.login1",
1340 "/org/freedesktop/login1",
1341 "org.freedesktop.login1.Manager",
1346 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1351 static int lock_sessions(int argc, char *argv[], void *userdata) {
1352 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1353 sd_bus *bus = userdata;
1359 polkit_agent_open_if_enabled();
1361 r = sd_bus_call_method(
1363 "org.freedesktop.login1",
1364 "/org/freedesktop/login1",
1365 "org.freedesktop.login1.Manager",
1366 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1370 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1375 static int terminate_seat(int argc, char *argv[], void *userdata) {
1376 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1377 sd_bus *bus = userdata;
1383 polkit_agent_open_if_enabled();
1385 for (i = 1; i < argc; i++) {
1387 r = sd_bus_call_method(
1389 "org.freedesktop.login1",
1390 "/org/freedesktop/login1",
1391 "org.freedesktop.login1.Manager",
1396 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1404 static int help(int argc, char *argv[], void *userdata) {
1406 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1407 "Send control commands to or query the login manager.\n\n"
1408 " -h --help Show this help\n"
1409 " --version Show package version\n"
1410 " --no-pager Do not pipe output into a pager\n"
1411 #if 1 /// elogind supports --no-wall
1412 " --no-wall Do not print any wall message\n"
1414 " --no-legend Do not show the headers and footers\n"
1415 " --no-ask-password Don't prompt for password\n"
1416 " -H --host=[USER@]HOST Operate on remote host\n"
1417 " -M --machine=CONTAINER Operate on local container\n"
1418 " -p --property=NAME Show only properties by this name\n"
1419 " -a --all Show all properties, including empty ones\n"
1420 " --value When showing properties, only print the value\n"
1421 " -l --full Do not ellipsize output\n"
1422 " --kill-who=WHO Who to send signal to\n"
1423 " -s --signal=SIGNAL Which signal to send\n"
1424 #if 0 /// UNNEEDED by elogind
1425 " -n --lines=INTEGER Number of journal entries to show\n"
1426 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1427 " short-iso, short-iso-precise, short-full,\n"
1428 " short-monotonic, short-unix, verbose, export,\n"
1429 " json, json-pretty, json-sse, cat)\n"
1431 /// elogind can cancel shutdowns and allows to ignore inhibitors
1432 " -c Cancel a pending shutdown or reboot\n"
1433 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n\n"
1435 "Session Commands:\n"
1436 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1437 " list-sessions List sessions\n"
1439 " list[-sessions] List sessions (default command)\n"
1441 " session-status [ID...] Show session status\n"
1442 " show-session [ID...] Show properties of sessions or the manager\n"
1443 " activate [ID] Activate a session\n"
1444 " lock-session [ID...] Screen lock one or more sessions\n"
1445 " unlock-session [ID...] Screen unlock one or more sessions\n"
1446 " lock-sessions Screen lock all current sessions\n"
1447 " unlock-sessions Screen unlock all current sessions\n"
1448 " terminate-session ID... Terminate one or more sessions\n"
1449 " kill-session ID... Send signal to processes of a session\n\n"
1451 " list-users List users\n"
1452 " user-status [USER...] Show user status\n"
1453 " show-user [USER...] Show properties of users or the manager\n"
1454 " enable-linger [USER...] Enable linger state of one or more users\n"
1455 " disable-linger [USER...] Disable linger state of one or more users\n"
1456 " terminate-user USER... Terminate all sessions of one or more users\n"
1457 " kill-user USER... Send signal to processes of a user\n\n"
1459 " list-seats List seats\n"
1460 " seat-status [NAME...] Show seat status\n"
1461 " show-seat [NAME...] Show properties of seats or the manager\n"
1462 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1463 " flush-devices Flush all device associations\n"
1464 " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
1465 #if 1 /// elogind adds some system commands to loginctl
1466 "System Commands:\n"
1467 " poweroff [TIME] [WALL...] Turn off the machine\n"
1468 " reboot [TIME] [WALL...] Reboot the machine\n"
1469 " suspend Suspend the machine to memory\n"
1470 " hibernate Suspend the machine to disk\n"
1471 " hybrid-sleep Suspend the machine to memory and disk\n"
1473 , program_invocation_short_name);
1478 static int parse_argv(int argc, char *argv[]) {
1481 ARG_VERSION = 0x100,
1484 #if 1 /// elogind supports --no-wall
1489 ARG_NO_ASK_PASSWORD,
1492 static const struct option options[] = {
1493 { "help", no_argument, NULL, 'h' },
1494 { "version", no_argument, NULL, ARG_VERSION },
1495 { "property", required_argument, NULL, 'p' },
1496 { "all", no_argument, NULL, 'a' },
1497 { "value", no_argument, NULL, ARG_VALUE },
1498 { "full", no_argument, NULL, 'l' },
1499 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1500 #if 1 /// elogind supports --no-wall
1501 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1503 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1504 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1505 { "signal", required_argument, NULL, 's' },
1506 { "host", required_argument, NULL, 'H' },
1507 { "machine", required_argument, NULL, 'M' },
1508 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1509 #if 0 /// UNNEEDED by elogind
1510 { "lines", required_argument, NULL, 'n' },
1511 { "output", required_argument, NULL, 'o' },
1513 /// elogind allows to ignore inhibitors for system commands.
1514 { "ignore-inhibitors", no_argument, NULL, 'i' },
1524 #if 0 /// elogind adds some system commands to loginctl
1525 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1527 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1533 help(0, NULL, NULL);
1540 r = strv_extend(&arg_property, optarg);
1544 /* If the user asked for a particular
1545 * property, show it to him, even if it is
1563 #if 0 /// UNNEEDED by elogind
1565 if (safe_atou(optarg, &arg_lines) < 0) {
1566 log_error("Failed to parse lines '%s'", optarg);
1572 arg_output = output_mode_from_string(optarg);
1573 if (arg_output < 0) {
1574 log_error("Unknown output '%s'.", optarg);
1581 arg_no_pager = true;
1583 #if 1 /// elogind supports --no-wall
1593 case ARG_NO_ASK_PASSWORD:
1594 arg_ask_password = false;
1598 arg_kill_who = optarg;
1602 arg_signal = signal_from_string_try_harder(optarg);
1603 if (arg_signal < 0) {
1604 log_error("Failed to parse signal string %s.", optarg);
1610 arg_transport = BUS_TRANSPORT_REMOTE;
1615 arg_transport = BUS_TRANSPORT_MACHINE;
1618 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1620 arg_action = ACTION_CANCEL_SHUTDOWN;
1624 arg_ignore_inhibitors = true;
1631 assert_not_reached("Unhandled option");
1637 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1639 static const Verb verbs[] = {
1640 { "help", VERB_ANY, VERB_ANY, 0, help },
1641 #if 0 /// elogind has "list" as a shorthand for "list-sessions"
1642 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1644 { "list", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1645 { "list-sessions", VERB_ANY, 1, 0, list_sessions },
1647 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1648 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1649 { "activate", VERB_ANY, 2, 0, activate },
1650 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1651 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1652 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1653 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1654 { "terminate-session", 2, VERB_ANY, 0, activate },
1655 { "kill-session", 2, VERB_ANY, 0, kill_session },
1656 { "list-users", VERB_ANY, 1, 0, list_users },
1657 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1658 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1659 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1660 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1661 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1662 { "kill-user", 2, VERB_ANY, 0, kill_user },
1663 { "list-seats", VERB_ANY, 1, 0, list_seats },
1664 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1665 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1666 { "attach", 3, VERB_ANY, 0, attach },
1667 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1668 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1669 #if 1 /// elogind adds some system commands to loginctl
1670 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
1671 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
1672 { "suspend", VERB_ANY, 1, 0, start_special },
1673 { "hibernate", VERB_ANY, 1, 0, start_special },
1674 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1675 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1680 #if 1 /// elogind can do shutdown and allows its cancellation
1681 if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
1682 return elogind_cancel_shutdown(bus);
1684 return dispatch_verb(argc, argv, verbs, bus);
1687 int main(int argc, char *argv[]) {
1691 setlocale(LC_ALL, "");
1692 elogind_set_program_name(argv[0]);
1693 log_parse_environment();
1696 r = parse_argv(argc, argv);
1700 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1702 log_error_errno(r, "Failed to create bus connection: %m");
1706 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1708 r = loginctl_main(argc, argv, bus);
1711 sd_bus_flush_close_unref(bus);
1714 #if 0 /// elogind does that in elogind_cleanup()
1715 polkit_agent_close();
1718 strv_free(arg_property);
1720 #if 1 /// elogind has some own cleanups to do
1723 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;