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 list_sessions(int argc, char *argv[], void *userdata) {
102 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
103 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
104 const char *id, *user, *seat, *object;
105 sd_bus *bus = userdata;
113 pager_open(arg_no_pager, false);
115 r = sd_bus_call_method(
117 "org.freedesktop.login1",
118 "/org/freedesktop/login1",
119 "org.freedesktop.login1.Manager",
124 log_error("Failed to list sessions: %s", bus_error_message(&error, r));
128 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
130 return bus_log_parse_error(r);
133 printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
135 while ((r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object)) > 0) {
136 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
140 return bus_log_parse_error(r);
143 printf("\n%u sessions listed.\n", k);
148 static int list_users(int argc, char *argv[], void *userdata) {
149 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
150 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
151 const char *user, *object;
152 sd_bus *bus = userdata;
160 pager_open(arg_no_pager, false);
162 r = sd_bus_call_method(
164 "org.freedesktop.login1",
165 "/org/freedesktop/login1",
166 "org.freedesktop.login1.Manager",
171 log_error("Failed to list users: %s", bus_error_message(&error, r));
175 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
177 return bus_log_parse_error(r);
180 printf("%10s %-16s\n", "UID", "USER");
182 while ((r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object)) > 0) {
183 printf("%10u %-16s\n", (unsigned) uid, user);
187 return bus_log_parse_error(r);
190 printf("\n%u users listed.\n", k);
195 static int list_seats(int argc, char *argv[], void *userdata) {
196 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
197 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
198 const char *seat, *object;
199 sd_bus *bus = userdata;
206 pager_open(arg_no_pager, false);
208 r = sd_bus_call_method(
210 "org.freedesktop.login1",
211 "/org/freedesktop/login1",
212 "org.freedesktop.login1.Manager",
217 log_error("Failed to list seats: %s", bus_error_message(&error, r));
221 r = sd_bus_message_enter_container(reply, 'a', "(so)");
223 return bus_log_parse_error(r);
226 printf("%-16s\n", "SEAT");
228 while ((r = sd_bus_message_read(reply, "(so)", &seat, &object)) > 0) {
229 printf("%-16s\n", seat);
233 return bus_log_parse_error(r);
236 printf("\n%u seats listed.\n", k);
241 #if 0 /// UNNEEDED by elogind
242 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
243 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
244 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
245 _cleanup_free_ char *path = NULL;
253 path = unit_dbus_path_from_name(unit);
257 r = sd_bus_get_property(
259 "org.freedesktop.systemd1",
267 return log_error_errno(r, "Failed to query ControlGroup: %s", bus_error_message(&error, r));
269 r = sd_bus_message_read(reply, "s", &cgroup);
271 return bus_log_parse_error(r);
282 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
285 if (arg_transport == BUS_TRANSPORT_REMOTE)
288 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
290 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
293 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
295 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
301 typedef struct SessionStatusInfo {
305 struct dual_timestamp timestamp;
322 typedef struct UserStatusInfo {
326 struct dual_timestamp timestamp;
333 typedef struct SeatStatusInfo {
335 char *active_session;
339 static void session_status_info_clear(SessionStatusInfo *info) {
346 free(info->remote_host);
347 free(info->remote_user);
358 static void user_status_info_clear(UserStatusInfo *info) {
362 strv_free(info->sessions);
369 static void seat_status_info_clear(SeatStatusInfo *info) {
372 free(info->active_session);
373 strv_free(info->sessions);
378 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
379 const char *contents;
382 r = sd_bus_message_peek_type(m, NULL, &contents);
386 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
390 if (contents[0] == 's' || contents[0] == 'o') {
392 char **p = (char **) userdata;
394 r = sd_bus_message_read_basic(m, contents[0], &s);
398 r = free_and_strdup(p, s);
402 r = sd_bus_message_read_basic(m, contents[0], userdata);
407 r = sd_bus_message_skip(m, contents+1);
411 r = sd_bus_message_exit_container(m);
418 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
425 r = sd_bus_message_enter_container(m, 'a', "(so)");
429 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
430 r = strv_extend(userdata, name);
437 return sd_bus_message_exit_container(m);
440 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
442 static const struct bus_properties_map map[] = {
443 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
444 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
445 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
446 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
447 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
448 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
449 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
450 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
451 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
452 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
453 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
454 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
455 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
456 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
457 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
458 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
459 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
460 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
461 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
465 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
466 char since2[FORMAT_TIMESTAMP_MAX], *s2;
467 _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
470 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
472 return log_error_errno(r, "Could not get properties: %m");
479 printf("%s - ", strna(i.id));
482 printf("%s (%u)\n", i.name, (unsigned) i.uid);
484 printf("%u\n", (unsigned) i.uid);
486 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
487 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
490 printf("\t Since: %s; %s\n", s2, s1);
492 printf("\t Since: %s\n", s2);
495 _cleanup_free_ char *t = NULL;
497 printf("\t Leader: %u", (unsigned) i.leader);
499 get_process_comm(i.leader, &t);
506 if (!isempty(i.seat)) {
507 printf("\t Seat: %s", i.seat);
510 printf("; vc%u", i.vtnr);
516 printf("\t TTY: %s\n", i.tty);
518 printf("\t Display: %s\n", i.display);
520 if (i.remote_host && i.remote_user)
521 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
522 else if (i.remote_host)
523 printf("\t Remote: %s\n", i.remote_host);
524 else if (i.remote_user)
525 printf("\t Remote: user %s\n", i.remote_user);
527 printf("\t Remote: Yes\n");
530 printf("\t Service: %s", i.service);
533 printf("; type %s", i.type);
536 printf("; class %s", i.class);
540 printf("\t Type: %s", i.type);
543 printf("; class %s", i.class);
547 printf("\t Class: %s\n", i.class);
549 if (!isempty(i.desktop))
550 printf("\t Desktop: %s\n", i.desktop);
553 printf("\t State: %s\n", i.state);
556 printf("\t Unit: %s\n", i.scope);
557 #if 0 /// UNNEEDED by elogind
558 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
560 if (arg_transport == BUS_TRANSPORT_LOCAL) {
562 show_journal_by_unit(
567 i.timestamp.monotonic,
570 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
571 SD_JOURNAL_LOCAL_ONLY,
581 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
583 static const struct bus_properties_map map[] = {
584 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
585 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
586 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
587 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
588 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
589 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
590 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
591 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
592 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
596 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
597 char since2[FORMAT_TIMESTAMP_MAX], *s2;
598 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
601 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
603 return log_error_errno(r, "Could not get properties: %m");
611 printf("%s (%u)\n", i.name, (unsigned) i.uid);
613 printf("%u\n", (unsigned) i.uid);
615 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
616 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
619 printf("\t Since: %s; %s\n", s2, s1);
621 printf("\t Since: %s\n", s2);
623 if (!isempty(i.state))
624 printf("\t State: %s\n", i.state);
626 if (!strv_isempty(i.sessions)) {
628 printf("\tSessions:");
630 STRV_FOREACH(l, i.sessions)
632 streq_ptr(*l, i.display) ? "*" : "",
638 printf("\t Linger: %s\n", yes_no(i.linger));
641 printf("\t Unit: %s\n", i.slice);
642 #if 0 /// UNNEEDED by elogind
643 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
645 show_journal_by_unit(
650 i.timestamp.monotonic,
653 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
654 SD_JOURNAL_LOCAL_ONLY,
663 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
665 static const struct bus_properties_map map[] = {
666 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
667 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
668 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
672 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
675 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
677 return log_error_errno(r, "Could not get properties: %m");
684 printf("%s\n", strna(i.id));
686 if (!strv_isempty(i.sessions)) {
688 printf("\tSessions:");
690 STRV_FOREACH(l, i.sessions) {
691 if (streq_ptr(*l, i.active_session))
700 if (arg_transport == BUS_TRANSPORT_LOCAL) {
709 printf("\t Devices:\n");
711 show_sysfs(i.id, "\t\t ", c);
717 #define property(name, fmt, ...) \
720 printf(fmt "\n", __VA_ARGS__); \
722 printf("%s=" fmt "\n", name, __VA_ARGS__); \
725 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
732 if (arg_property && !strv_find(arg_property, name))
733 /* skip what we didn't read */
734 return sd_bus_message_skip(m, contents);
736 switch (contents[0]) {
738 case SD_BUS_TYPE_STRUCT_BEGIN:
740 if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
743 r = sd_bus_message_read(m, "(so)", &s, NULL);
745 return bus_log_parse_error(r);
747 if (arg_all || !isempty(s))
748 property(name, "%s", s);
752 } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
755 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
757 return bus_log_parse_error(r);
759 if (!uid_is_valid(uid)) {
760 log_error("Invalid user ID: " UID_FMT, uid);
764 property(name, UID_FMT, uid);
770 case SD_BUS_TYPE_ARRAY:
772 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
776 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
778 return bus_log_parse_error(r);
783 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
784 printf("%s%s", space ? " " : "", s);
788 if (space || !arg_value)
792 return bus_log_parse_error(r);
794 r = sd_bus_message_exit_container(m);
796 return bus_log_parse_error(r);
804 r = bus_print_property(name, m, arg_value, arg_all);
806 return bus_log_parse_error(r);
809 r = sd_bus_message_skip(m, contents);
811 return bus_log_parse_error(r);
814 printf("%s=[unprintable]\n", name);
820 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
821 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
822 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
829 r = sd_bus_call_method(
831 "org.freedesktop.login1",
833 "org.freedesktop.DBus.Properties",
839 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
841 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
843 return bus_log_parse_error(r);
850 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
851 const char *name, *contents;
853 r = sd_bus_message_read(reply, "s", &name);
855 return bus_log_parse_error(r);
857 r = sd_bus_message_peek_type(reply, NULL, &contents);
859 return bus_log_parse_error(r);
861 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
863 return bus_log_parse_error(r);
865 r = print_property(name, reply, contents);
869 r = sd_bus_message_exit_container(reply);
871 return bus_log_parse_error(r);
873 r = sd_bus_message_exit_container(reply);
875 return bus_log_parse_error(r);
878 return bus_log_parse_error(r);
880 r = sd_bus_message_exit_container(reply);
882 return bus_log_parse_error(r);
887 static int show_session(int argc, char *argv[], void *userdata) {
888 bool properties, new_line = false;
889 sd_bus *bus = userdata;
895 properties = !strstr(argv[0], "status");
897 pager_open(arg_no_pager, false);
900 /* If not argument is specified inspect the manager
903 return show_properties(bus, "/org/freedesktop/login1", &new_line);
905 /* And in the pretty case, show data of the calling session */
906 return print_session_status_info(bus, "/org/freedesktop/login1/session/self", &new_line);
909 for (i = 1; i < argc; i++) {
910 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
911 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
912 const char *path = NULL;
914 r = sd_bus_call_method(
916 "org.freedesktop.login1",
917 "/org/freedesktop/login1",
918 "org.freedesktop.login1.Manager",
923 log_error("Failed to get session: %s", bus_error_message(&error, r));
927 r = sd_bus_message_read(reply, "o", &path);
929 return bus_log_parse_error(r);
932 r = show_properties(bus, path, &new_line);
934 r = print_session_status_info(bus, path, &new_line);
943 static int show_user(int argc, char *argv[], void *userdata) {
944 bool properties, new_line = false;
945 sd_bus *bus = userdata;
951 properties = !strstr(argv[0], "status");
953 pager_open(arg_no_pager, false);
956 /* If not argument is specified inspect the manager
959 return show_properties(bus, "/org/freedesktop/login1", &new_line);
961 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
964 for (i = 1; i < argc; i++) {
965 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
966 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
967 const char *path = NULL;
970 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
972 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
974 r = sd_bus_call_method(
976 "org.freedesktop.login1",
977 "/org/freedesktop/login1",
978 "org.freedesktop.login1.Manager",
981 "u", (uint32_t) uid);
983 log_error("Failed to get user: %s", bus_error_message(&error, r));
987 r = sd_bus_message_read(reply, "o", &path);
989 return bus_log_parse_error(r);
992 r = show_properties(bus, path, &new_line);
994 r = print_user_status_info(bus, path, &new_line);
1003 static int show_seat(int argc, char *argv[], void *userdata) {
1004 bool properties, new_line = false;
1005 sd_bus *bus = userdata;
1011 properties = !strstr(argv[0], "status");
1013 pager_open(arg_no_pager, false);
1016 /* If not argument is specified inspect the manager
1019 return show_properties(bus, "/org/freedesktop/login1", &new_line);
1021 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
1024 for (i = 1; i < argc; i++) {
1025 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1026 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1027 const char *path = NULL;
1029 r = sd_bus_call_method(
1031 "org.freedesktop.login1",
1032 "/org/freedesktop/login1",
1033 "org.freedesktop.login1.Manager",
1038 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1042 r = sd_bus_message_read(reply, "o", &path);
1044 return bus_log_parse_error(r);
1047 r = show_properties(bus, path, &new_line);
1049 r = print_seat_status_info(bus, path, &new_line);
1058 static int activate(int argc, char *argv[], void *userdata) {
1059 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1060 sd_bus *bus = userdata;
1061 char *short_argv[3];
1067 polkit_agent_open_if_enabled();
1070 /* No argument? Let's convert this into the empty
1071 * session name, which the calls will then resolve to
1072 * the caller's session. */
1074 short_argv[0] = argv[0];
1075 short_argv[1] = (char*) "";
1076 short_argv[2] = NULL;
1082 for (i = 1; i < argc; i++) {
1084 r = sd_bus_call_method(
1086 "org.freedesktop.login1",
1087 "/org/freedesktop/login1",
1088 "org.freedesktop.login1.Manager",
1089 streq(argv[0], "lock-session") ? "LockSession" :
1090 streq(argv[0], "unlock-session") ? "UnlockSession" :
1091 streq(argv[0], "terminate-session") ? "TerminateSession" :
1096 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1104 static int kill_session(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;
1112 polkit_agent_open_if_enabled();
1115 arg_kill_who = "all";
1117 for (i = 1; i < argc; i++) {
1119 r = sd_bus_call_method(
1121 "org.freedesktop.login1",
1122 "/org/freedesktop/login1",
1123 "org.freedesktop.login1.Manager",
1126 "ssi", argv[i], arg_kill_who, arg_signal);
1128 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1136 static int enable_linger(int argc, char *argv[], void *userdata) {
1137 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1138 sd_bus *bus = userdata;
1139 char* short_argv[3];
1146 polkit_agent_open_if_enabled();
1148 b = streq(argv[0], "enable-linger");
1151 short_argv[0] = argv[0];
1152 short_argv[1] = (char*) "";
1153 short_argv[2] = NULL;
1158 for (i = 1; i < argc; i++) {
1161 if (isempty(argv[i]))
1164 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1166 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1169 r = sd_bus_call_method(
1171 "org.freedesktop.login1",
1172 "/org/freedesktop/login1",
1173 "org.freedesktop.login1.Manager",
1176 "ubb", (uint32_t) uid, b, true);
1178 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1186 static int terminate_user(int argc, char *argv[], void *userdata) {
1187 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1188 sd_bus *bus = userdata;
1194 polkit_agent_open_if_enabled();
1196 for (i = 1; i < argc; i++) {
1199 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1201 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1203 r = sd_bus_call_method(
1205 "org.freedesktop.login1",
1206 "/org/freedesktop/login1",
1207 "org.freedesktop.login1.Manager",
1210 "u", (uint32_t) uid);
1212 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1220 static int kill_user(int argc, char *argv[], void *userdata) {
1221 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1222 sd_bus *bus = userdata;
1228 polkit_agent_open_if_enabled();
1231 arg_kill_who = "all";
1233 for (i = 1; i < argc; i++) {
1236 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1238 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1240 r = sd_bus_call_method(
1242 "org.freedesktop.login1",
1243 "/org/freedesktop/login1",
1244 "org.freedesktop.login1.Manager",
1247 "ui", (uint32_t) uid, arg_signal);
1249 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1257 static int attach(int argc, char *argv[], void *userdata) {
1258 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1259 sd_bus *bus = userdata;
1265 polkit_agent_open_if_enabled();
1267 for (i = 2; i < argc; i++) {
1269 r = sd_bus_call_method(
1271 "org.freedesktop.login1",
1272 "/org/freedesktop/login1",
1273 "org.freedesktop.login1.Manager",
1276 "ssb", argv[1], argv[i], true);
1279 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1287 static int flush_devices(int argc, char *argv[], void *userdata) {
1288 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1289 sd_bus *bus = userdata;
1295 polkit_agent_open_if_enabled();
1297 r = sd_bus_call_method(
1299 "org.freedesktop.login1",
1300 "/org/freedesktop/login1",
1301 "org.freedesktop.login1.Manager",
1306 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1311 static int lock_sessions(int argc, char *argv[], void *userdata) {
1312 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1313 sd_bus *bus = userdata;
1319 polkit_agent_open_if_enabled();
1321 r = sd_bus_call_method(
1323 "org.freedesktop.login1",
1324 "/org/freedesktop/login1",
1325 "org.freedesktop.login1.Manager",
1326 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1330 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1335 static int terminate_seat(int argc, char *argv[], void *userdata) {
1336 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1337 sd_bus *bus = userdata;
1343 polkit_agent_open_if_enabled();
1345 for (i = 1; i < argc; i++) {
1347 r = sd_bus_call_method(
1349 "org.freedesktop.login1",
1350 "/org/freedesktop/login1",
1351 "org.freedesktop.login1.Manager",
1356 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1364 static int help(int argc, char *argv[], void *userdata) {
1366 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1367 "Send control commands to or query the login manager.\n\n"
1368 " -h --help Show this help\n"
1369 " --version Show package version\n"
1370 " --no-pager Do not pipe output into a pager\n"
1371 #if 1 /// elogind supports --no-wall
1372 " --no-wall Do not print any wall message\n"
1374 " --no-legend Do not show the headers and footers\n"
1375 " --no-ask-password Don't prompt for password\n"
1376 " -H --host=[USER@]HOST Operate on remote host\n"
1377 " -M --machine=CONTAINER Operate on local container\n"
1378 " -p --property=NAME Show only properties by this name\n"
1379 " -a --all Show all properties, including empty ones\n"
1380 " --value When showing properties, only print the value\n"
1381 " -l --full Do not ellipsize output\n"
1382 " --kill-who=WHO Who to send signal to\n"
1383 " -s --signal=SIGNAL Which signal to send\n"
1384 #if 0 /// UNNEEDED by elogind
1385 " -n --lines=INTEGER Number of journal entries to show\n"
1386 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1387 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1389 /// elogind can cancel shutdowns and allows to ignore inhibitors
1390 " -c Cancel a pending shutdown or reboot\n"
1391 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n\n"
1393 "Session Commands:\n"
1394 " list-sessions List sessions\n"
1395 " session-status [ID...] Show session status\n"
1396 " show-session [ID...] Show properties of sessions or the manager\n"
1397 " activate [ID] Activate a session\n"
1398 " lock-session [ID...] Screen lock one or more sessions\n"
1399 " unlock-session [ID...] Screen unlock one or more sessions\n"
1400 " lock-sessions Screen lock all current sessions\n"
1401 " unlock-sessions Screen unlock all current sessions\n"
1402 " terminate-session ID... Terminate one or more sessions\n"
1403 " kill-session ID... Send signal to processes of a session\n\n"
1405 " list-users List users\n"
1406 " user-status [USER...] Show user status\n"
1407 " show-user [USER...] Show properties of users or the manager\n"
1408 " enable-linger [USER...] Enable linger state of one or more users\n"
1409 " disable-linger [USER...] Disable linger state of one or more users\n"
1410 " terminate-user USER... Terminate all sessions of one or more users\n"
1411 " kill-user USER... Send signal to processes of a user\n\n"
1413 " list-seats List seats\n"
1414 " seat-status [NAME...] Show seat status\n"
1415 " show-seat [NAME...] Show properties of seats or the manager\n"
1416 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1417 " flush-devices Flush all device associations\n"
1418 " terminate-seat NAME... Terminate all sessions on one or more seats\n\n"
1419 #if 1 /// elogind adds some system commands to loginctl
1420 "System Commands:\n"
1421 " poweroff [TIME] [WALL...] Turn off the machine\n"
1422 " reboot [TIME] [WALL...] Reboot the machine\n"
1423 " suspend Suspend the machine to memory\n"
1424 " hibernate Suspend the machine to disk\n"
1425 " hybrid-sleep Suspend the machine to memory and disk\n"
1427 , program_invocation_short_name);
1432 static int parse_argv(int argc, char *argv[]) {
1435 ARG_VERSION = 0x100,
1438 #if 1 /// elogind supports --no-wall
1443 ARG_NO_ASK_PASSWORD,
1446 static const struct option options[] = {
1447 { "help", no_argument, NULL, 'h' },
1448 { "version", no_argument, NULL, ARG_VERSION },
1449 { "property", required_argument, NULL, 'p' },
1450 { "all", no_argument, NULL, 'a' },
1451 { "value", no_argument, NULL, ARG_VALUE },
1452 { "full", no_argument, NULL, 'l' },
1453 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1454 #if 1 /// elogind supports --no-wall
1455 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1457 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1458 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1459 { "signal", required_argument, NULL, 's' },
1460 { "host", required_argument, NULL, 'H' },
1461 { "machine", required_argument, NULL, 'M' },
1462 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1463 #if 0 /// UNNEEDED by elogind
1464 { "lines", required_argument, NULL, 'n' },
1465 { "output", required_argument, NULL, 'o' },
1467 /// elogind allows to ignore inhibitors for system commands.
1468 { "ignore-inhibitors", no_argument, NULL, 'i' },
1478 #if 0 /// elogind adds some system commands to loginctl
1479 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1481 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1487 help(0, NULL, NULL);
1494 r = strv_extend(&arg_property, optarg);
1498 /* If the user asked for a particular
1499 * property, show it to him, even if it is
1517 #if 0 /// UNNEEDED by elogind
1519 if (safe_atou(optarg, &arg_lines) < 0) {
1520 log_error("Failed to parse lines '%s'", optarg);
1526 arg_output = output_mode_from_string(optarg);
1527 if (arg_output < 0) {
1528 log_error("Unknown output '%s'.", optarg);
1535 arg_no_pager = true;
1537 #if 1 /// elogind supports --no-wall
1547 case ARG_NO_ASK_PASSWORD:
1548 arg_ask_password = false;
1552 arg_kill_who = optarg;
1556 arg_signal = signal_from_string_try_harder(optarg);
1557 if (arg_signal < 0) {
1558 log_error("Failed to parse signal string %s.", optarg);
1564 arg_transport = BUS_TRANSPORT_REMOTE;
1569 arg_transport = BUS_TRANSPORT_MACHINE;
1572 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1574 arg_action = ACTION_CANCEL_SHUTDOWN;
1578 arg_ignore_inhibitors = true;
1585 assert_not_reached("Unhandled option");
1591 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1593 static const Verb verbs[] = {
1594 { "help", VERB_ANY, VERB_ANY, 0, help },
1595 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1596 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1597 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1598 { "activate", VERB_ANY, 2, 0, activate },
1599 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1600 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1601 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1602 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1603 { "terminate-session", 2, VERB_ANY, 0, activate },
1604 { "kill-session", 2, VERB_ANY, 0, kill_session },
1605 { "list-users", VERB_ANY, 1, 0, list_users },
1606 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1607 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1608 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1609 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1610 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1611 { "kill-user", 2, VERB_ANY, 0, kill_user },
1612 { "list-seats", VERB_ANY, 1, 0, list_seats },
1613 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1614 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1615 { "attach", 3, VERB_ANY, 0, attach },
1616 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1617 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1618 #if 1 /// elogind adds some system commands to loginctl
1619 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
1620 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
1621 { "suspend", VERB_ANY, 1, 0, start_special },
1622 { "hibernate", VERB_ANY, 1, 0, start_special },
1623 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1624 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1629 #if 1 /// elogind can do shutdown and allows its cancellation
1630 if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
1631 return elogind_cancel_shutdown(bus);
1633 return dispatch_verb(argc, argv, verbs, bus);
1636 int main(int argc, char *argv[]) {
1640 setlocale(LC_ALL, "");
1641 elogind_set_program_name(argv[0]);
1642 log_parse_environment();
1645 r = parse_argv(argc, argv);
1649 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1651 log_error_errno(r, "Failed to create bus connection: %m");
1655 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1657 r = loginctl_main(argc, argv, bus);
1660 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;