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 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
503 char since2[FORMAT_TIMESTAMP_MAX], *s2;
504 _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
507 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
509 return log_error_errno(r, "Could not get properties: %m");
516 printf("%s - ", strna(i.id));
519 printf("%s (%"PRIu32")\n", i.name, i.uid);
521 printf("%"PRIu32"\n", i.uid);
523 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
524 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
527 printf("\t Since: %s; %s\n", s2, s1);
529 printf("\t Since: %s\n", s2);
532 _cleanup_free_ char *t = NULL;
534 printf("\t Leader: %"PRIu32, i.leader);
536 get_process_comm(i.leader, &t);
543 if (!isempty(i.seat)) {
544 printf("\t Seat: %s", i.seat);
547 printf("; vc%u", i.vtnr);
553 printf("\t TTY: %s\n", i.tty);
555 printf("\t Display: %s\n", i.display);
557 if (i.remote_host && i.remote_user)
558 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
559 else if (i.remote_host)
560 printf("\t Remote: %s\n", i.remote_host);
561 else if (i.remote_user)
562 printf("\t Remote: user %s\n", i.remote_user);
564 printf("\t Remote: Yes\n");
567 printf("\t Service: %s", i.service);
570 printf("; type %s", i.type);
573 printf("; class %s", i.class);
577 printf("\t Type: %s", i.type);
580 printf("; class %s", i.class);
584 printf("\t Class: %s\n", i.class);
586 if (!isempty(i.desktop))
587 printf("\t Desktop: %s\n", i.desktop);
590 printf("\t State: %s\n", i.state);
593 printf("\t Unit: %s\n", i.scope);
594 #if 0 /// UNNEEDED by elogind
595 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
597 if (arg_transport == BUS_TRANSPORT_LOCAL) {
599 show_journal_by_unit(
604 i.timestamp.monotonic,
607 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
608 SD_JOURNAL_LOCAL_ONLY,
618 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
620 static const struct bus_properties_map map[] = {
621 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
622 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
623 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
624 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
625 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
626 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
627 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
628 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
629 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
633 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
634 char since2[FORMAT_TIMESTAMP_MAX], *s2;
635 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
638 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
640 return log_error_errno(r, "Could not get properties: %m");
648 printf("%s (%"PRIu32")\n", i.name, i.uid);
650 printf("%"PRIu32"\n", i.uid);
652 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
653 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
656 printf("\t Since: %s; %s\n", s2, s1);
658 printf("\t Since: %s\n", s2);
660 if (!isempty(i.state))
661 printf("\t State: %s\n", i.state);
663 if (!strv_isempty(i.sessions)) {
665 printf("\tSessions:");
667 STRV_FOREACH(l, i.sessions)
669 streq_ptr(*l, i.display) ? "*" : "",
675 printf("\t Linger: %s\n", yes_no(i.linger));
678 printf("\t Unit: %s\n", i.slice);
679 #if 0 /// UNNEEDED by elogind
680 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
682 show_journal_by_unit(
687 i.timestamp.monotonic,
690 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
691 SD_JOURNAL_LOCAL_ONLY,
700 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
702 static const struct bus_properties_map map[] = {
703 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
704 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
705 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
709 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
712 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
714 return log_error_errno(r, "Could not get properties: %m");
721 printf("%s\n", strna(i.id));
723 if (!strv_isempty(i.sessions)) {
725 printf("\tSessions:");
727 STRV_FOREACH(l, i.sessions) {
728 if (streq_ptr(*l, i.active_session))
737 if (arg_transport == BUS_TRANSPORT_LOCAL) {
746 printf("\t Devices:\n");
748 show_sysfs(i.id, "\t\t ", c);
754 #define property(name, fmt, ...) \
757 printf(fmt "\n", __VA_ARGS__); \
759 printf("%s=" fmt "\n", name, __VA_ARGS__); \
762 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
769 if (arg_property && !strv_find(arg_property, name))
770 /* skip what we didn't read */
771 return sd_bus_message_skip(m, contents);
773 switch (contents[0]) {
775 case SD_BUS_TYPE_STRUCT_BEGIN:
777 if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
780 r = sd_bus_message_read(m, "(so)", &s, NULL);
782 return bus_log_parse_error(r);
784 if (arg_all || !isempty(s))
785 property(name, "%s", s);
789 } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
792 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
794 return bus_log_parse_error(r);
796 if (!uid_is_valid(uid)) {
797 log_error("Invalid user ID: " UID_FMT, uid);
801 property(name, UID_FMT, uid);
807 case SD_BUS_TYPE_ARRAY:
809 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
813 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
815 return bus_log_parse_error(r);
820 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
821 printf("%s%s", space ? " " : "", s);
825 if (space || !arg_value)
829 return bus_log_parse_error(r);
831 r = sd_bus_message_exit_container(m);
833 return bus_log_parse_error(r);
841 r = bus_print_property(name, m, arg_value, arg_all);
843 return bus_log_parse_error(r);
846 r = sd_bus_message_skip(m, contents);
848 return bus_log_parse_error(r);
851 printf("%s=[unprintable]\n", name);
857 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
858 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
859 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
866 r = sd_bus_call_method(
868 "org.freedesktop.login1",
870 "org.freedesktop.DBus.Properties",
876 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
878 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
880 return bus_log_parse_error(r);
887 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
888 const char *name, *contents;
890 r = sd_bus_message_read(reply, "s", &name);
892 return bus_log_parse_error(r);
894 r = sd_bus_message_peek_type(reply, NULL, &contents);
896 return bus_log_parse_error(r);
898 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
900 return bus_log_parse_error(r);
902 r = print_property(name, reply, contents);
906 r = sd_bus_message_exit_container(reply);
908 return bus_log_parse_error(r);
910 r = sd_bus_message_exit_container(reply);
912 return bus_log_parse_error(r);
915 return bus_log_parse_error(r);
917 r = sd_bus_message_exit_container(reply);
919 return bus_log_parse_error(r);
924 static int show_session(int argc, char *argv[], void *userdata) {
925 bool properties, new_line = false;
926 sd_bus *bus = userdata;
932 properties = !strstr(argv[0], "status");
934 pager_open(arg_no_pager, false);
937 /* If not argument is specified inspect the manager
940 return show_properties(bus, "/org/freedesktop/login1", &new_line);
942 /* And in the pretty case, show data of the calling session */
943 return print_session_status_info(bus, "/org/freedesktop/login1/session/self", &new_line);
946 for (i = 1; i < argc; i++) {
947 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
948 _cleanup_free_ char *path = NULL;
950 r = get_session_path(bus, argv[1], &error, &path);
952 log_error("Failed to get session path: %s", bus_error_message(&error, r));
957 r = show_properties(bus, path, &new_line);
959 r = print_session_status_info(bus, path, &new_line);
968 static int show_user(int argc, char *argv[], void *userdata) {
969 bool properties, new_line = false;
970 sd_bus *bus = userdata;
976 properties = !strstr(argv[0], "status");
978 pager_open(arg_no_pager, false);
981 /* If not argument is specified inspect the manager
984 return show_properties(bus, "/org/freedesktop/login1", &new_line);
986 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
989 for (i = 1; i < argc; i++) {
990 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
991 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
992 const char *path = NULL;
995 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
997 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
999 r = sd_bus_call_method(
1001 "org.freedesktop.login1",
1002 "/org/freedesktop/login1",
1003 "org.freedesktop.login1.Manager",
1006 "u", (uint32_t) uid);
1008 log_error("Failed to get user: %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_user_status_info(bus, path, &new_line);
1028 static int show_seat(int argc, char *argv[], void *userdata) {
1029 bool properties, new_line = false;
1030 sd_bus *bus = userdata;
1036 properties = !strstr(argv[0], "status");
1038 pager_open(arg_no_pager, false);
1041 /* If not argument is specified inspect the manager
1044 return show_properties(bus, "/org/freedesktop/login1", &new_line);
1046 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
1049 for (i = 1; i < argc; i++) {
1050 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1051 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1052 const char *path = NULL;
1054 r = sd_bus_call_method(
1056 "org.freedesktop.login1",
1057 "/org/freedesktop/login1",
1058 "org.freedesktop.login1.Manager",
1063 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1067 r = sd_bus_message_read(reply, "o", &path);
1069 return bus_log_parse_error(r);
1072 r = show_properties(bus, path, &new_line);
1074 r = print_seat_status_info(bus, path, &new_line);
1083 static int activate(int argc, char *argv[], void *userdata) {
1084 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1085 sd_bus *bus = userdata;
1086 char *short_argv[3];
1092 polkit_agent_open_if_enabled();
1095 /* No argument? Let's convert this into the empty
1096 * session name, which the calls will then resolve to
1097 * the caller's session. */
1099 short_argv[0] = argv[0];
1100 short_argv[1] = (char*) "";
1101 short_argv[2] = NULL;
1107 for (i = 1; i < argc; i++) {
1109 r = sd_bus_call_method(
1111 "org.freedesktop.login1",
1112 "/org/freedesktop/login1",
1113 "org.freedesktop.login1.Manager",
1114 streq(argv[0], "lock-session") ? "LockSession" :
1115 streq(argv[0], "unlock-session") ? "UnlockSession" :
1116 streq(argv[0], "terminate-session") ? "TerminateSession" :
1121 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1129 static int kill_session(int argc, char *argv[], void *userdata) {
1130 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1131 sd_bus *bus = userdata;
1137 polkit_agent_open_if_enabled();
1140 arg_kill_who = "all";
1142 for (i = 1; i < argc; i++) {
1144 r = sd_bus_call_method(
1146 "org.freedesktop.login1",
1147 "/org/freedesktop/login1",
1148 "org.freedesktop.login1.Manager",
1151 "ssi", argv[i], arg_kill_who, arg_signal);
1153 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1161 static int enable_linger(int argc, char *argv[], void *userdata) {
1162 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1163 sd_bus *bus = userdata;
1164 char* short_argv[3];
1171 polkit_agent_open_if_enabled();
1173 b = streq(argv[0], "enable-linger");
1176 short_argv[0] = argv[0];
1177 short_argv[1] = (char*) "";
1178 short_argv[2] = NULL;
1183 for (i = 1; i < argc; i++) {
1186 if (isempty(argv[i]))
1189 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1191 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1194 r = sd_bus_call_method(
1196 "org.freedesktop.login1",
1197 "/org/freedesktop/login1",
1198 "org.freedesktop.login1.Manager",
1201 "ubb", (uint32_t) uid, b, true);
1203 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1211 static int terminate_user(int argc, char *argv[], void *userdata) {
1212 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1213 sd_bus *bus = userdata;
1219 polkit_agent_open_if_enabled();
1221 for (i = 1; i < argc; i++) {
1224 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1226 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1228 r = sd_bus_call_method(
1230 "org.freedesktop.login1",
1231 "/org/freedesktop/login1",
1232 "org.freedesktop.login1.Manager",
1235 "u", (uint32_t) uid);
1237 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1245 static int kill_user(int argc, char *argv[], void *userdata) {
1246 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1247 sd_bus *bus = userdata;
1253 polkit_agent_open_if_enabled();
1256 arg_kill_who = "all";
1258 for (i = 1; i < argc; i++) {
1261 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1263 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1265 r = sd_bus_call_method(
1267 "org.freedesktop.login1",
1268 "/org/freedesktop/login1",
1269 "org.freedesktop.login1.Manager",
1272 "ui", (uint32_t) uid, arg_signal);
1274 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1282 static int attach(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();
1292 for (i = 2; i < argc; i++) {
1294 r = sd_bus_call_method(
1296 "org.freedesktop.login1",
1297 "/org/freedesktop/login1",
1298 "org.freedesktop.login1.Manager",
1301 "ssb", argv[1], argv[i], true);
1304 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1312 static int flush_devices(int argc, char *argv[], void *userdata) {
1313 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1314 sd_bus *bus = userdata;
1320 polkit_agent_open_if_enabled();
1322 r = sd_bus_call_method(
1324 "org.freedesktop.login1",
1325 "/org/freedesktop/login1",
1326 "org.freedesktop.login1.Manager",
1331 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1336 static int lock_sessions(int argc, char *argv[], void *userdata) {
1337 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1338 sd_bus *bus = userdata;
1344 polkit_agent_open_if_enabled();
1346 r = sd_bus_call_method(
1348 "org.freedesktop.login1",
1349 "/org/freedesktop/login1",
1350 "org.freedesktop.login1.Manager",
1351 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1355 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1360 static int terminate_seat(int argc, char *argv[], void *userdata) {
1361 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1362 sd_bus *bus = userdata;
1368 polkit_agent_open_if_enabled();
1370 for (i = 1; i < argc; i++) {
1372 r = sd_bus_call_method(
1374 "org.freedesktop.login1",
1375 "/org/freedesktop/login1",
1376 "org.freedesktop.login1.Manager",
1381 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1389 static int help(int argc, char *argv[], void *userdata) {
1391 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1392 "Send control commands to or query the login manager.\n\n"
1393 " -h --help Show this help\n"
1394 " --version Show package version\n"
1395 " --no-pager Do not pipe output into a pager\n"
1396 #if 1 /// elogind supports --no-wall
1397 " --no-wall Do not print any wall message\n"
1399 " --no-legend Do not show the headers and footers\n"
1400 " --no-ask-password Don't prompt for password\n"
1401 " -H --host=[USER@]HOST Operate on remote host\n"
1402 " -M --machine=CONTAINER Operate on local container\n"
1403 " -p --property=NAME Show only properties by this name\n"
1404 " -a --all Show all properties, including empty ones\n"
1405 " --value When showing properties, only print the value\n"
1406 " -l --full Do not ellipsize output\n"
1407 " --kill-who=WHO Who to send signal to\n"
1408 " -s --signal=SIGNAL Which signal to send\n"
1409 #if 0 /// UNNEEDED by elogind
1410 " -n --lines=INTEGER Number of journal entries to show\n"
1411 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1412 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1414 /// elogind can cancel shutdowns and allows to ignore inhibitors
1415 " -c Cancel a pending shutdown or reboot\n"
1416 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n\n"
1418 "Session Commands:\n"
1419 " list-sessions List sessions\n"
1420 " session-status [ID...] Show session status\n"
1421 " show-session [ID...] Show properties of sessions or the manager\n"
1422 " activate [ID] Activate a session\n"
1423 " lock-session [ID...] Screen lock one or more sessions\n"
1424 " unlock-session [ID...] Screen unlock one or more sessions\n"
1425 " lock-sessions Screen lock all current sessions\n"
1426 " unlock-sessions Screen unlock all current sessions\n"
1427 " terminate-session ID... Terminate one or more sessions\n"
1428 " kill-session ID... Send signal to processes of a session\n\n"
1430 " list-users List users\n"
1431 " user-status [USER...] Show user status\n"
1432 " show-user [USER...] Show properties of users or the manager\n"
1433 " enable-linger [USER...] Enable linger state of one or more users\n"
1434 " disable-linger [USER...] Disable linger state of one or more users\n"
1435 " terminate-user USER... Terminate all sessions of one or more users\n"
1436 " kill-user USER... Send signal to processes of a user\n\n"
1438 " list-seats List seats\n"
1439 " seat-status [NAME...] Show seat status\n"
1440 " show-seat [NAME...] Show properties of seats or the manager\n"
1441 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1442 " flush-devices Flush all device associations\n"
1443 " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
1444 #if 1 /// elogind adds some system commands to loginctl
1445 "System Commands:\n"
1446 " poweroff [TIME] [WALL...] Turn off the machine\n"
1447 " reboot [TIME] [WALL...] Reboot the machine\n"
1448 " suspend Suspend the machine to memory\n"
1449 " hibernate Suspend the machine to disk\n"
1450 " hybrid-sleep Suspend the machine to memory and disk\n"
1452 , program_invocation_short_name);
1457 static int parse_argv(int argc, char *argv[]) {
1460 ARG_VERSION = 0x100,
1463 #if 1 /// elogind supports --no-wall
1468 ARG_NO_ASK_PASSWORD,
1471 static const struct option options[] = {
1472 { "help", no_argument, NULL, 'h' },
1473 { "version", no_argument, NULL, ARG_VERSION },
1474 { "property", required_argument, NULL, 'p' },
1475 { "all", no_argument, NULL, 'a' },
1476 { "value", no_argument, NULL, ARG_VALUE },
1477 { "full", no_argument, NULL, 'l' },
1478 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1479 #if 1 /// elogind supports --no-wall
1480 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1482 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1483 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1484 { "signal", required_argument, NULL, 's' },
1485 { "host", required_argument, NULL, 'H' },
1486 { "machine", required_argument, NULL, 'M' },
1487 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1488 #if 0 /// UNNEEDED by elogind
1489 { "lines", required_argument, NULL, 'n' },
1490 { "output", required_argument, NULL, 'o' },
1492 /// elogind allows to ignore inhibitors for system commands.
1493 { "ignore-inhibitors", no_argument, NULL, 'i' },
1503 #if 0 /// elogind adds some system commands to loginctl
1504 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1506 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1512 help(0, NULL, NULL);
1519 r = strv_extend(&arg_property, optarg);
1523 /* If the user asked for a particular
1524 * property, show it to him, even if it is
1542 #if 0 /// UNNEEDED by elogind
1544 if (safe_atou(optarg, &arg_lines) < 0) {
1545 log_error("Failed to parse lines '%s'", optarg);
1551 arg_output = output_mode_from_string(optarg);
1552 if (arg_output < 0) {
1553 log_error("Unknown output '%s'.", optarg);
1560 arg_no_pager = true;
1562 #if 1 /// elogind supports --no-wall
1572 case ARG_NO_ASK_PASSWORD:
1573 arg_ask_password = false;
1577 arg_kill_who = optarg;
1581 arg_signal = signal_from_string_try_harder(optarg);
1582 if (arg_signal < 0) {
1583 log_error("Failed to parse signal string %s.", optarg);
1589 arg_transport = BUS_TRANSPORT_REMOTE;
1594 arg_transport = BUS_TRANSPORT_MACHINE;
1597 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1599 arg_action = ACTION_CANCEL_SHUTDOWN;
1603 arg_ignore_inhibitors = true;
1610 assert_not_reached("Unhandled option");
1616 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1618 static const Verb verbs[] = {
1619 { "help", VERB_ANY, VERB_ANY, 0, help },
1620 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1621 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1622 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1623 { "activate", VERB_ANY, 2, 0, activate },
1624 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1625 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1626 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1627 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1628 { "terminate-session", 2, VERB_ANY, 0, activate },
1629 { "kill-session", 2, VERB_ANY, 0, kill_session },
1630 { "list-users", VERB_ANY, 1, 0, list_users },
1631 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1632 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1633 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1634 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1635 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1636 { "kill-user", 2, VERB_ANY, 0, kill_user },
1637 { "list-seats", VERB_ANY, 1, 0, list_seats },
1638 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1639 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1640 { "attach", 3, VERB_ANY, 0, attach },
1641 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1642 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1643 #if 1 /// elogind adds some system commands to loginctl
1644 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
1645 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
1646 { "suspend", VERB_ANY, 1, 0, start_special },
1647 { "hibernate", VERB_ANY, 1, 0, start_special },
1648 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1649 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1654 #if 1 /// elogind can do shutdown and allows its cancellation
1655 if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
1656 return elogind_cancel_shutdown(bus);
1658 return dispatch_verb(argc, argv, verbs, bus);
1661 int main(int argc, char *argv[]) {
1665 setlocale(LC_ALL, "");
1666 elogind_set_program_name(argv[0]);
1667 log_parse_environment();
1670 r = parse_argv(argc, argv);
1674 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1676 log_error_errno(r, "Failed to create bus connection: %m");
1680 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1682 r = loginctl_main(argc, argv, bus);
1685 sd_bus_flush_close_unref(bus);
1688 #if 0 /// elogind does that in elogind_cleanup()
1689 polkit_agent_close();
1692 strv_free(arg_property);
1694 #if 1 /// elogind has some own cleanups to do
1697 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;