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 for elogind
50 #include "logind-action.h"
51 #include "musl_missing.h"
55 static char **arg_property = NULL;
56 static bool arg_all = false;
57 static bool arg_value = false;
58 static bool arg_full = false;
59 static bool arg_no_pager = false;
60 static bool arg_no_wall = false;
61 static bool arg_legend = true;
62 static const char *arg_kill_who = NULL;
63 static int arg_signal = SIGTERM;
64 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
65 static char *arg_host = NULL;
66 static usec_t arg_when = 0;
67 static char **arg_wall = NULL;
68 static bool arg_ask_password = true;
69 static bool arg_ignore_inhibitors = false;
70 #if 0 /// UNNEEDED by elogind
71 static unsigned arg_lines = 10;
72 static OutputMode arg_output = OUTPUT_SHORT;
83 ACTION_CANCEL_SHUTDOWN,
88 static void polkit_agent_open_if_enabled(void) {
90 /* Open the polkit agent as a child process if necessary */
92 if (!arg_ask_password)
95 if (arg_transport != BUS_TRANSPORT_LOCAL)
101 #if 0 /// UNNEEDED by elogind
102 static OutputFlags get_output_flags(void) {
105 arg_all * OUTPUT_SHOW_ALL |
106 arg_full * OUTPUT_FULL_WIDTH |
107 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
108 colors_enabled() * OUTPUT_COLOR;
112 static int list_sessions(int argc, char *argv[], void *userdata) {
113 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
114 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
115 const char *id, *user, *seat, *object;
116 sd_bus *bus = userdata;
124 pager_open(arg_no_pager, false);
126 r = sd_bus_call_method(
128 "org.freedesktop.login1",
129 "/org/freedesktop/login1",
130 "org.freedesktop.login1.Manager",
135 log_error("Failed to list sessions: %s", bus_error_message(&error, r));
139 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
141 return bus_log_parse_error(r);
144 printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
146 while ((r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object)) > 0) {
147 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
151 return bus_log_parse_error(r);
154 printf("\n%u sessions listed.\n", k);
159 static int list_users(int argc, char *argv[], void *userdata) {
160 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
161 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
162 const char *user, *object;
163 sd_bus *bus = userdata;
171 pager_open(arg_no_pager, false);
173 r = sd_bus_call_method(
175 "org.freedesktop.login1",
176 "/org/freedesktop/login1",
177 "org.freedesktop.login1.Manager",
182 log_error("Failed to list users: %s", bus_error_message(&error, r));
186 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
188 return bus_log_parse_error(r);
191 printf("%10s %-16s\n", "UID", "USER");
193 while ((r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object)) > 0) {
194 printf("%10u %-16s\n", (unsigned) uid, user);
198 return bus_log_parse_error(r);
201 printf("\n%u users listed.\n", k);
206 static int list_seats(int argc, char *argv[], void *userdata) {
207 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
208 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
209 const char *seat, *object;
210 sd_bus *bus = userdata;
217 pager_open(arg_no_pager, false);
219 r = sd_bus_call_method(
221 "org.freedesktop.login1",
222 "/org/freedesktop/login1",
223 "org.freedesktop.login1.Manager",
228 log_error("Failed to list seats: %s", bus_error_message(&error, r));
232 r = sd_bus_message_enter_container(reply, 'a', "(so)");
234 return bus_log_parse_error(r);
237 printf("%-16s\n", "SEAT");
239 while ((r = sd_bus_message_read(reply, "(so)", &seat, &object)) > 0) {
240 printf("%-16s\n", seat);
244 return bus_log_parse_error(r);
247 printf("\n%u seats listed.\n", k);
252 #if 0 /// UNNEEDED by elogind
253 static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit, pid_t leader) {
254 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
255 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
256 _cleanup_free_ char *path = NULL;
264 path = unit_dbus_path_from_name(unit);
268 r = sd_bus_get_property(
270 "org.freedesktop.systemd1",
278 return log_error_errno(r, "Failed to query ControlGroup: %s", bus_error_message(&error, r));
280 r = sd_bus_message_read(reply, "s", &cgroup);
282 return bus_log_parse_error(r);
293 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
296 if (arg_transport == BUS_TRANSPORT_REMOTE)
299 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
301 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
304 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
306 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
312 typedef struct SessionStatusInfo {
316 struct dual_timestamp timestamp;
333 typedef struct UserStatusInfo {
337 struct dual_timestamp timestamp;
344 typedef struct SeatStatusInfo {
346 char *active_session;
350 static void session_status_info_clear(SessionStatusInfo *info) {
357 free(info->remote_host);
358 free(info->remote_user);
369 static void user_status_info_clear(UserStatusInfo *info) {
373 strv_free(info->sessions);
380 static void seat_status_info_clear(SeatStatusInfo *info) {
383 free(info->active_session);
384 strv_free(info->sessions);
389 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
390 const char *contents;
393 r = sd_bus_message_peek_type(m, NULL, &contents);
397 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
401 if (contents[0] == 's' || contents[0] == 'o') {
403 char **p = (char **) userdata;
405 r = sd_bus_message_read_basic(m, contents[0], &s);
409 r = free_and_strdup(p, s);
413 r = sd_bus_message_read_basic(m, contents[0], userdata);
418 r = sd_bus_message_skip(m, contents+1);
422 r = sd_bus_message_exit_container(m);
429 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
436 r = sd_bus_message_enter_container(m, 'a', "(so)");
440 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
441 r = strv_extend(userdata, name);
448 return sd_bus_message_exit_container(m);
451 static int print_session_status_info(sd_bus *bus, const char *path, bool *new_line) {
453 static const struct bus_properties_map map[] = {
454 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
455 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
456 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
457 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
458 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
459 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
460 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
461 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
462 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
463 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
464 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
465 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
466 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
467 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
468 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
469 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
470 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
471 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
472 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
476 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
477 char since2[FORMAT_TIMESTAMP_MAX], *s2;
478 _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
481 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
483 return log_error_errno(r, "Could not get properties: %m");
490 printf("%s - ", strna(i.id));
493 printf("%s (%u)\n", i.name, (unsigned) i.uid);
495 printf("%u\n", (unsigned) i.uid);
497 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
498 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
501 printf("\t Since: %s; %s\n", s2, s1);
503 printf("\t Since: %s\n", s2);
506 _cleanup_free_ char *t = NULL;
508 printf("\t Leader: %u", (unsigned) i.leader);
510 get_process_comm(i.leader, &t);
517 if (!isempty(i.seat)) {
518 printf("\t Seat: %s", i.seat);
521 printf("; vc%u", i.vtnr);
527 printf("\t TTY: %s\n", i.tty);
529 printf("\t Display: %s\n", i.display);
531 if (i.remote_host && i.remote_user)
532 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
533 else if (i.remote_host)
534 printf("\t Remote: %s\n", i.remote_host);
535 else if (i.remote_user)
536 printf("\t Remote: user %s\n", i.remote_user);
538 printf("\t Remote: Yes\n");
541 printf("\t Service: %s", i.service);
544 printf("; type %s", i.type);
547 printf("; class %s", i.class);
551 printf("\t Type: %s", i.type);
554 printf("; class %s", i.class);
558 printf("\t Class: %s\n", i.class);
560 if (!isempty(i.desktop))
561 printf("\t Desktop: %s\n", i.desktop);
564 printf("\t State: %s\n", i.state);
567 printf("\t Unit: %s\n", i.scope);
568 #if 0 /// UNNEEDED by elogind
569 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
571 if (arg_transport == BUS_TRANSPORT_LOCAL) {
573 show_journal_by_unit(
578 i.timestamp.monotonic,
581 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
582 SD_JOURNAL_LOCAL_ONLY,
592 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
594 static const struct bus_properties_map map[] = {
595 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
596 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
597 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
598 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
599 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
600 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
601 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
602 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
603 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
607 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
608 char since2[FORMAT_TIMESTAMP_MAX], *s2;
609 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
612 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
614 return log_error_errno(r, "Could not get properties: %m");
622 printf("%s (%u)\n", i.name, (unsigned) i.uid);
624 printf("%u\n", (unsigned) i.uid);
626 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
627 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
630 printf("\t Since: %s; %s\n", s2, s1);
632 printf("\t Since: %s\n", s2);
634 if (!isempty(i.state))
635 printf("\t State: %s\n", i.state);
637 if (!strv_isempty(i.sessions)) {
639 printf("\tSessions:");
641 STRV_FOREACH(l, i.sessions)
643 streq_ptr(*l, i.display) ? "*" : "",
649 printf("\t Linger: %s\n", yes_no(i.linger));
652 printf("\t Unit: %s\n", i.slice);
653 #if 0 /// UNNEEDED by elogind
654 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
656 show_journal_by_unit(
661 i.timestamp.monotonic,
664 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
665 SD_JOURNAL_LOCAL_ONLY,
674 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
676 static const struct bus_properties_map map[] = {
677 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
678 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
679 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
683 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
686 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
688 return log_error_errno(r, "Could not get properties: %m");
695 printf("%s\n", strna(i.id));
697 if (!strv_isempty(i.sessions)) {
699 printf("\tSessions:");
701 STRV_FOREACH(l, i.sessions) {
702 if (streq_ptr(*l, i.active_session))
711 if (arg_transport == BUS_TRANSPORT_LOCAL) {
720 printf("\t Devices:\n");
722 show_sysfs(i.id, "\t\t ", c);
728 #define property(name, fmt, ...) \
731 printf(fmt "\n", __VA_ARGS__); \
733 printf("%s=" fmt "\n", name, __VA_ARGS__); \
736 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
743 if (arg_property && !strv_find(arg_property, name))
744 /* skip what we didn't read */
745 return sd_bus_message_skip(m, contents);
747 switch (contents[0]) {
749 case SD_BUS_TYPE_STRUCT_BEGIN:
751 if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
754 r = sd_bus_message_read(m, "(so)", &s, NULL);
756 return bus_log_parse_error(r);
758 if (arg_all || !isempty(s))
759 property(name, "%s", s);
763 } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
766 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
768 return bus_log_parse_error(r);
770 if (!uid_is_valid(uid)) {
771 log_error("Invalid user ID: " UID_FMT, uid);
775 property(name, UID_FMT, uid);
781 case SD_BUS_TYPE_ARRAY:
783 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
787 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
789 return bus_log_parse_error(r);
794 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
795 printf("%s%s", space ? " " : "", s);
799 if (space || !arg_value)
803 return bus_log_parse_error(r);
805 r = sd_bus_message_exit_container(m);
807 return bus_log_parse_error(r);
815 r = bus_print_property(name, m, arg_value, arg_all);
817 return bus_log_parse_error(r);
820 r = sd_bus_message_skip(m, contents);
822 return bus_log_parse_error(r);
825 printf("%s=[unprintable]\n", name);
831 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
832 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
833 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
840 r = sd_bus_call_method(
842 "org.freedesktop.login1",
844 "org.freedesktop.DBus.Properties",
850 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
852 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
854 return bus_log_parse_error(r);
861 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
862 const char *name, *contents;
864 r = sd_bus_message_read(reply, "s", &name);
866 return bus_log_parse_error(r);
868 r = sd_bus_message_peek_type(reply, NULL, &contents);
870 return bus_log_parse_error(r);
872 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
874 return bus_log_parse_error(r);
876 r = print_property(name, reply, contents);
880 r = sd_bus_message_exit_container(reply);
882 return bus_log_parse_error(r);
884 r = sd_bus_message_exit_container(reply);
886 return bus_log_parse_error(r);
889 return bus_log_parse_error(r);
891 r = sd_bus_message_exit_container(reply);
893 return bus_log_parse_error(r);
898 static int show_session(int argc, char *argv[], void *userdata) {
899 bool properties, new_line = false;
900 sd_bus *bus = userdata;
906 properties = !strstr(argv[0], "status");
908 pager_open(arg_no_pager, false);
911 /* If not argument is specified inspect the manager
914 return show_properties(bus, "/org/freedesktop/login1", &new_line);
916 /* And in the pretty case, show data of the calling session */
917 return print_session_status_info(bus, "/org/freedesktop/login1/session/self", &new_line);
920 for (i = 1; i < argc; i++) {
921 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
922 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
923 const char *path = NULL;
925 r = sd_bus_call_method(
927 "org.freedesktop.login1",
928 "/org/freedesktop/login1",
929 "org.freedesktop.login1.Manager",
934 log_error("Failed to get session: %s", bus_error_message(&error, r));
938 r = sd_bus_message_read(reply, "o", &path);
940 return bus_log_parse_error(r);
943 r = show_properties(bus, path, &new_line);
945 r = print_session_status_info(bus, path, &new_line);
954 static int show_user(int argc, char *argv[], void *userdata) {
955 bool properties, new_line = false;
956 sd_bus *bus = userdata;
962 properties = !strstr(argv[0], "status");
964 pager_open(arg_no_pager, false);
967 /* If not argument is specified inspect the manager
970 return show_properties(bus, "/org/freedesktop/login1", &new_line);
972 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
975 for (i = 1; i < argc; i++) {
976 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
977 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
978 const char *path = NULL;
981 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
983 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
985 r = sd_bus_call_method(
987 "org.freedesktop.login1",
988 "/org/freedesktop/login1",
989 "org.freedesktop.login1.Manager",
992 "u", (uint32_t) uid);
994 log_error("Failed to get user: %s", bus_error_message(&error, r));
998 r = sd_bus_message_read(reply, "o", &path);
1000 return bus_log_parse_error(r);
1003 r = show_properties(bus, path, &new_line);
1005 r = print_user_status_info(bus, path, &new_line);
1014 static int show_seat(int argc, char *argv[], void *userdata) {
1015 bool properties, new_line = false;
1016 sd_bus *bus = userdata;
1022 properties = !strstr(argv[0], "status");
1024 pager_open(arg_no_pager, false);
1027 /* If not argument is specified inspect the manager
1030 return show_properties(bus, "/org/freedesktop/login1", &new_line);
1032 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
1035 for (i = 1; i < argc; i++) {
1036 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1037 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1038 const char *path = NULL;
1040 r = sd_bus_call_method(
1042 "org.freedesktop.login1",
1043 "/org/freedesktop/login1",
1044 "org.freedesktop.login1.Manager",
1049 log_error("Failed to get seat: %s", bus_error_message(&error, r));
1053 r = sd_bus_message_read(reply, "o", &path);
1055 return bus_log_parse_error(r);
1058 r = show_properties(bus, path, &new_line);
1060 r = print_seat_status_info(bus, path, &new_line);
1069 static int activate(int argc, char *argv[], void *userdata) {
1070 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1071 sd_bus *bus = userdata;
1072 char *short_argv[3];
1078 polkit_agent_open_if_enabled();
1081 /* No argument? Let's convert this into the empty
1082 * session name, which the calls will then resolve to
1083 * the caller's session. */
1085 short_argv[0] = argv[0];
1086 short_argv[1] = (char*) "";
1087 short_argv[2] = NULL;
1093 for (i = 1; i < argc; i++) {
1095 r = sd_bus_call_method(
1097 "org.freedesktop.login1",
1098 "/org/freedesktop/login1",
1099 "org.freedesktop.login1.Manager",
1100 streq(argv[0], "lock-session") ? "LockSession" :
1101 streq(argv[0], "unlock-session") ? "UnlockSession" :
1102 streq(argv[0], "terminate-session") ? "TerminateSession" :
1107 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1115 static int kill_session(int argc, char *argv[], void *userdata) {
1116 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1117 sd_bus *bus = userdata;
1123 polkit_agent_open_if_enabled();
1126 arg_kill_who = "all";
1128 for (i = 1; i < argc; i++) {
1130 r = sd_bus_call_method(
1132 "org.freedesktop.login1",
1133 "/org/freedesktop/login1",
1134 "org.freedesktop.login1.Manager",
1137 "ssi", argv[i], arg_kill_who, arg_signal);
1139 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1147 static int enable_linger(int argc, char *argv[], void *userdata) {
1148 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1149 sd_bus *bus = userdata;
1150 char* short_argv[3];
1157 polkit_agent_open_if_enabled();
1159 b = streq(argv[0], "enable-linger");
1162 short_argv[0] = argv[0];
1163 short_argv[1] = (char*) "";
1164 short_argv[2] = NULL;
1169 for (i = 1; i < argc; i++) {
1172 if (isempty(argv[i]))
1175 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1177 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1180 r = sd_bus_call_method(
1182 "org.freedesktop.login1",
1183 "/org/freedesktop/login1",
1184 "org.freedesktop.login1.Manager",
1187 "ubb", (uint32_t) uid, b, true);
1189 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1197 static int terminate_user(int argc, char *argv[], void *userdata) {
1198 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1199 sd_bus *bus = userdata;
1205 polkit_agent_open_if_enabled();
1207 for (i = 1; i < argc; i++) {
1210 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1212 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1214 r = sd_bus_call_method(
1216 "org.freedesktop.login1",
1217 "/org/freedesktop/login1",
1218 "org.freedesktop.login1.Manager",
1221 "u", (uint32_t) uid);
1223 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1231 static int kill_user(int argc, char *argv[], void *userdata) {
1232 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1233 sd_bus *bus = userdata;
1239 polkit_agent_open_if_enabled();
1242 arg_kill_who = "all";
1244 for (i = 1; i < argc; i++) {
1247 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1249 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1251 r = sd_bus_call_method(
1253 "org.freedesktop.login1",
1254 "/org/freedesktop/login1",
1255 "org.freedesktop.login1.Manager",
1258 "ui", (uint32_t) uid, arg_signal);
1260 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1268 static int attach(int argc, char *argv[], void *userdata) {
1269 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1270 sd_bus *bus = userdata;
1276 polkit_agent_open_if_enabled();
1278 for (i = 2; i < argc; i++) {
1280 r = sd_bus_call_method(
1282 "org.freedesktop.login1",
1283 "/org/freedesktop/login1",
1284 "org.freedesktop.login1.Manager",
1287 "ssb", argv[1], argv[i], true);
1290 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1298 static int flush_devices(int argc, char *argv[], void *userdata) {
1299 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1300 sd_bus *bus = userdata;
1306 polkit_agent_open_if_enabled();
1308 r = sd_bus_call_method(
1310 "org.freedesktop.login1",
1311 "/org/freedesktop/login1",
1312 "org.freedesktop.login1.Manager",
1317 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1322 static int lock_sessions(int argc, char *argv[], void *userdata) {
1323 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1324 sd_bus *bus = userdata;
1330 polkit_agent_open_if_enabled();
1332 r = sd_bus_call_method(
1334 "org.freedesktop.login1",
1335 "/org/freedesktop/login1",
1336 "org.freedesktop.login1.Manager",
1337 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1341 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1346 static int terminate_seat(int argc, char *argv[], void *userdata) {
1347 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1348 sd_bus *bus = userdata;
1354 polkit_agent_open_if_enabled();
1356 for (i = 1; i < argc; i++) {
1358 r = sd_bus_call_method(
1360 "org.freedesktop.login1",
1361 "/org/freedesktop/login1",
1362 "org.freedesktop.login1.Manager",
1367 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1375 static int logind_set_wall_message(sd_bus* bus, const char* msg) {
1376 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1377 _cleanup_free_ char *m = NULL;
1380 if (strv_extend(&arg_wall, msg) < 0)
1383 m = strv_join(arg_wall, " ");
1387 r = sd_bus_call_method(
1389 "org.freedesktop.login1",
1390 "/org/freedesktop/login1",
1391 "org.freedesktop.login1.Manager",
1400 return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r));
1405 /* Ask elogind, which might grant access to unprivileged users
1406 * through PolicyKit */
1407 static int elogind_reboot(sd_bus *bus, enum action a) {
1408 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1411 static const char *table[_ACTION_MAX] = {
1412 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
1413 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
1414 [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
1420 polkit_agent_open_if_enabled();
1424 case ACTION_POWEROFF:
1425 method = "PowerOff";
1432 case ACTION_SUSPEND:
1436 case ACTION_HIBERNATE:
1437 method = "Hibernate";
1440 case ACTION_HYBRID_SLEEP:
1441 method = "HybridSleep";
1444 case ACTION_CANCEL_SHUTDOWN:
1445 method = "CancelScheduledShutdown";
1452 r = logind_set_wall_message(bus, table[a]);
1454 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
1455 bus_error_message(&error, r));
1456 sd_bus_error_free(&error);
1459 r = sd_bus_call_method(
1461 "org.freedesktop.login1",
1462 "/org/freedesktop/login1",
1463 "org.freedesktop.login1.Manager",
1467 "b", arg_ask_password);
1469 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
1474 static const struct {
1475 HandleAction action;
1477 } action_table[_ACTION_MAX] = {
1478 [ACTION_POWEROFF] = { HANDLE_POWEROFF, "poweroff", },
1479 [ACTION_REBOOT] = { HANDLE_REBOOT, "reboot", },
1480 [ACTION_SUSPEND] = { HANDLE_SUSPEND, "suspend", },
1481 [ACTION_HIBERNATE] = { HANDLE_HIBERNATE, "hibernate", },
1482 [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
1485 static enum action verb_to_action(const char *verb) {
1488 for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
1489 if (streq_ptr(action_table[i].verb, verb))
1492 return _ACTION_INVALID;
1495 static int parse_shutdown_time_spec(const char *t, usec_t *_u) {
1499 if (streq(t, "now"))
1501 else if (!strchr(t, ':')) {
1504 if (safe_atou64(t, &u) < 0)
1507 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
1516 hour = strtol(t, &e, 10);
1517 if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
1520 minute = strtol(e+1, &e, 10);
1521 if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
1524 n = now(CLOCK_REALTIME);
1525 s = (time_t) (n / USEC_PER_SEC);
1527 assert_se(localtime_r(&s, &tm));
1529 tm.tm_hour = (int) hour;
1530 tm.tm_min = (int) minute;
1533 assert_se(s = mktime(&tm));
1535 *_u = (usec_t) s * USEC_PER_SEC;
1538 *_u += USEC_PER_DAY;
1544 static int check_inhibitors(sd_bus* bus, enum action a) {
1545 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1546 _cleanup_strv_free_ char **sessions = NULL;
1547 const char *what, *who, *why, *mode;
1553 if (arg_ignore_inhibitors)
1565 r = sd_bus_call_method(
1567 "org.freedesktop.login1",
1568 "/org/freedesktop/login1",
1569 "org.freedesktop.login1.Manager",
1575 /* If logind is not around, then there are no inhibitors... */
1578 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
1580 return bus_log_parse_error(r);
1582 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
1583 _cleanup_free_ char *comm = NULL, *user = NULL;
1584 _cleanup_strv_free_ char **sv = NULL;
1586 if (!streq(mode, "block"))
1589 sv = strv_split(what, ":");
1593 if ((pid_t) pid < 0)
1594 return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
1596 if (!strv_contains(sv,
1601 ACTION_KEXEC) ? "shutdown" : "sleep"))
1604 get_process_comm(pid, &comm);
1605 user = uid_to_name(uid);
1607 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
1608 who, (pid_t) pid, strna(comm), strna(user), why);
1613 return bus_log_parse_error(r);
1615 r = sd_bus_message_exit_container(reply);
1617 return bus_log_parse_error(r);
1619 /* Check for current sessions */
1620 sd_get_sessions(&sessions);
1621 STRV_FOREACH(s, sessions) {
1622 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
1624 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
1627 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
1630 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
1633 sd_session_get_tty(*s, &tty);
1634 sd_session_get_seat(*s, &seat);
1635 sd_session_get_service(*s, &service);
1636 user = uid_to_name(uid);
1638 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
1645 log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
1646 action_table[a].verb);
1651 static int start_special(int argc, char *argv[], void *userdata) {
1652 sd_bus *bus = userdata;
1658 a = verb_to_action(argv[0]);
1660 r = check_inhibitors(bus, a);
1664 /* Now power off actions in chroot environments */
1665 if ((a == ACTION_POWEROFF ||
1666 a == ACTION_REBOOT) &&
1667 (running_in_chroot() > 0) ) {
1668 log_info("Running in chroot, ignoring request.");
1672 /* Switch to cancel shutdown, if a shutdown action was requested,
1673 and the option to cancel it was set: */
1674 if ((a == ACTION_POWEROFF ||
1675 a == ACTION_REBOOT) &&
1676 (arg_action == ACTION_CANCEL_SHUTDOWN))
1677 return elogind_reboot(bus, arg_action);
1679 /* Otherwise perform requested action */
1680 if (a == ACTION_POWEROFF ||
1681 a == ACTION_REBOOT ||
1682 a == ACTION_SUSPEND ||
1683 a == ACTION_HIBERNATE ||
1684 a == ACTION_HYBRID_SLEEP)
1685 return elogind_reboot(bus, a);
1690 static int help(int argc, char *argv[], void *userdata) {
1692 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1693 "Send control commands to or query the login manager.\n\n"
1694 " -h --help Show this help\n"
1695 " --version Show package version\n"
1696 " --no-pager Do not pipe output into a pager\n"
1697 " --no-wall Do not print any wall message\n"
1698 " --no-legend Do not show the headers and footers\n"
1699 " --no-ask-password Don't prompt for password\n"
1700 " -H --host=[USER@]HOST Operate on remote host\n"
1701 " -M --machine=CONTAINER Operate on local container\n"
1702 " -p --property=NAME Show only properties by this name\n"
1703 " -a --all Show all properties, including empty ones\n"
1704 " --value When showing properties, only print the value\n"
1705 " -l --full Do not ellipsize output\n"
1706 " --kill-who=WHO Who to send signal to\n"
1707 " -s --signal=SIGNAL Which signal to send\n"
1708 #if 0 /// UNNEEDED by elogind
1709 " -n --lines=INTEGER Number of journal entries to show\n"
1710 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1711 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1713 " -c Cancel a pending shutdown\n"
1714 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n"
1715 "Session Commands:\n"
1716 " list-sessions List sessions\n"
1717 " session-status [ID...] Show session status\n"
1718 " show-session [ID...] Show properties of sessions or the manager\n"
1719 " activate [ID] Activate a session\n"
1720 " lock-session [ID...] Screen lock one or more sessions\n"
1721 " unlock-session [ID...] Screen unlock one or more sessions\n"
1722 " lock-sessions Screen lock all current sessions\n"
1723 " unlock-sessions Screen unlock all current sessions\n"
1724 " terminate-session ID... Terminate one or more sessions\n"
1725 " kill-session ID... Send signal to processes of a session\n\n"
1727 " list-users List users\n"
1728 " user-status [USER...] Show user status\n"
1729 " show-user [USER...] Show properties of users or the manager\n"
1730 " enable-linger [USER...] Enable linger state of one or more users\n"
1731 " disable-linger [USER...] Disable linger state of one or more users\n"
1732 " terminate-user USER... Terminate all sessions of one or more users\n"
1733 " kill-user USER... Send signal to processes of a user\n\n"
1735 " list-seats List seats\n"
1736 " seat-status [NAME...] Show seat status\n"
1737 " show-seat [NAME...] Show properties of seats or the manager\n"
1738 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1739 " flush-devices Flush all device associations\n"
1740 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1741 "System Commands:\n"
1742 " poweroff Turn off the machine\n"
1743 " reboot Reboot the machine\n"
1744 " suspend Suspend the machine to memory\n"
1745 " hibernate Suspend the machine to disk\n"
1746 " hybrid-sleep Suspend the machine to memory and disk\n"
1747 , program_invocation_short_name);
1752 static int parse_argv(int argc, char *argv[]) {
1755 ARG_VERSION = 0x100,
1761 ARG_NO_ASK_PASSWORD,
1764 static const struct option options[] = {
1765 { "help", no_argument, NULL, 'h' },
1766 { "version", no_argument, NULL, ARG_VERSION },
1767 { "property", required_argument, NULL, 'p' },
1768 { "all", no_argument, NULL, 'a' },
1769 { "value", no_argument, NULL, ARG_VALUE },
1770 { "full", no_argument, NULL, 'l' },
1771 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1772 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1773 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1774 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1775 { "signal", required_argument, NULL, 's' },
1776 { "host", required_argument, NULL, 'H' },
1777 { "machine", required_argument, NULL, 'M' },
1778 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1779 #if 0 /// UNNEEDED by elogind
1780 { "lines", required_argument, NULL, 'n' },
1781 { "output", required_argument, NULL, 'o' },
1783 { "ignore-inhibitors", no_argument, NULL, 'i' },
1793 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1798 help(0, NULL, NULL);
1805 r = strv_extend(&arg_property, optarg);
1809 /* If the user asked for a particular
1810 * property, show it to him, even if it is
1828 #if 0 /// UNNEEDED by elogind
1830 if (safe_atou(optarg, &arg_lines) < 0) {
1831 log_error("Failed to parse lines '%s'", optarg);
1837 arg_output = output_mode_from_string(optarg);
1838 if (arg_output < 0) {
1839 log_error("Unknown output '%s'.", optarg);
1846 arg_no_pager = true;
1857 case ARG_NO_ASK_PASSWORD:
1858 arg_ask_password = false;
1862 arg_kill_who = optarg;
1866 arg_signal = signal_from_string_try_harder(optarg);
1867 if (arg_signal < 0) {
1868 log_error("Failed to parse signal string %s.", optarg);
1874 arg_transport = BUS_TRANSPORT_REMOTE;
1879 arg_transport = BUS_TRANSPORT_MACHINE;
1884 arg_action = ACTION_CANCEL_SHUTDOWN;
1888 arg_ignore_inhibitors = true;
1895 assert_not_reached("Unhandled option");
1898 if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) {
1899 r = parse_shutdown_time_spec(argv[optind], &arg_when);
1901 log_error("Failed to parse time specification: %s", argv[optind]);
1905 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
1907 if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN)
1908 /* No time argument for shutdown cancel */
1909 wall = argv + optind;
1910 else if (argc > optind + 1)
1911 /* We skip the time argument */
1912 wall = argv + optind + 1;
1915 arg_wall = strv_copy(wall);
1925 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1927 static const Verb verbs[] = {
1928 { "help", VERB_ANY, VERB_ANY, 0, help },
1929 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1930 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1931 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1932 { "activate", VERB_ANY, 2, 0, activate },
1933 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1934 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1935 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1936 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1937 { "terminate-session", 2, VERB_ANY, 0, activate },
1938 { "kill-session", 2, VERB_ANY, 0, kill_session },
1939 { "list-users", VERB_ANY, 1, 0, list_users },
1940 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1941 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1942 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1943 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1944 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1945 { "kill-user", 2, VERB_ANY, 0, kill_user },
1946 { "list-seats", VERB_ANY, 1, 0, list_seats },
1947 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1948 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1949 { "attach", 3, VERB_ANY, 0, attach },
1950 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1951 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1952 { "poweroff", VERB_ANY, 1, 0, start_special },
1953 { "reboot", VERB_ANY, 1, 0, start_special },
1954 { "suspend", VERB_ANY, 1, 0, start_special },
1955 { "hibernate", VERB_ANY, 1, 0, start_special },
1956 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
1957 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
1961 return dispatch_verb(argc, argv, verbs, bus);
1964 int main(int argc, char *argv[]) {
1965 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1968 setlocale(LC_ALL, "");
1969 elogind_set_program_name(argv[0]);
1970 log_parse_environment();
1973 r = parse_argv(argc, argv);
1977 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1979 log_error_errno(r, "Failed to create bus connection: %m");
1983 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1985 r = loginctl_main(argc, argv, bus);
1989 polkit_agent_close();
1991 strv_free(arg_property);
1992 strv_free(arg_wall);
1994 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;