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 static int elogind_cancel_shutdown(sd_bus *bus) {
1406 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1409 r = sd_bus_call_method(
1411 "org.freedesktop.login1",
1412 "/org/freedesktop/login1",
1413 "org.freedesktop.login1.Manager",
1414 "CancelScheduledShutdown",
1418 return log_warning_errno(r, "Failed to talk to elogind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r));
1423 static int elogind_schedule_shutdown(sd_bus *bus, enum action a) {
1424 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1425 const char *method = NULL;
1431 polkit_agent_open_if_enabled();
1435 case ACTION_POWEROFF:
1436 method = "poweroff";
1447 r = logind_set_wall_message(bus, NULL);
1450 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
1451 bus_error_message(&error, r));
1452 sd_bus_error_free(&error);
1455 r = sd_bus_call_method(
1457 "org.freedesktop.login1",
1458 "/org/freedesktop/login1",
1459 "org.freedesktop.login1.Manager",
1468 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
1473 static int elogind_reboot(sd_bus *bus, enum action a) {
1474 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1475 const char *method = NULL;
1477 static const char *table[_ACTION_MAX] = {
1478 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
1479 [ACTION_POWEROFF] = "The system is going down for power-off NOW!"
1485 polkit_agent_open_if_enabled();
1489 case ACTION_POWEROFF:
1490 method = "PowerOff";
1497 case ACTION_SUSPEND:
1501 case ACTION_HIBERNATE:
1502 method = "Hibernate";
1505 case ACTION_HYBRID_SLEEP:
1506 method = "HybridSleep";
1513 r = logind_set_wall_message(bus, table[a]);
1516 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
1517 bus_error_message(&error, r));
1518 sd_bus_error_free(&error);
1521 /* Now call elogind itself to request the operation */
1522 r = sd_bus_call_method(
1524 "org.freedesktop.login1",
1525 "/org/freedesktop/login1",
1526 "org.freedesktop.login1.Manager",
1534 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
1539 static const struct {
1540 HandleAction action;
1542 } action_table[_ACTION_MAX] = {
1543 [ACTION_POWEROFF] = { HANDLE_POWEROFF, "poweroff", },
1544 [ACTION_REBOOT] = { HANDLE_REBOOT, "reboot", },
1545 [ACTION_SUSPEND] = { HANDLE_SUSPEND, "suspend", },
1546 [ACTION_HIBERNATE] = { HANDLE_HIBERNATE, "hibernate", },
1547 [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
1550 static enum action verb_to_action(const char *verb) {
1553 for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
1554 if (streq_ptr(action_table[i].verb, verb))
1557 return _ACTION_INVALID;
1560 static int parse_shutdown_time_spec(const char *t, usec_t *_u) {
1564 if (streq(t, "now"))
1566 else if (!strchr(t, ':')) {
1569 if (safe_atou64(t, &u) < 0)
1572 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
1581 hour = strtol(t, &e, 10);
1582 if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
1585 minute = strtol(e+1, &e, 10);
1586 if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
1589 n = now(CLOCK_REALTIME);
1590 s = (time_t) (n / USEC_PER_SEC);
1592 assert_se(localtime_r(&s, &tm));
1594 tm.tm_hour = (int) hour;
1595 tm.tm_min = (int) minute;
1598 assert_se(s = mktime(&tm));
1600 *_u = (usec_t) s * USEC_PER_SEC;
1603 *_u += USEC_PER_DAY;
1609 static int check_inhibitors(sd_bus* bus, enum action a) {
1610 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1611 _cleanup_strv_free_ char **sessions = NULL;
1612 const char *what, *who, *why, *mode;
1618 if (arg_ignore_inhibitors)
1630 r = sd_bus_call_method(
1632 "org.freedesktop.login1",
1633 "/org/freedesktop/login1",
1634 "org.freedesktop.login1.Manager",
1640 /* If logind is not around, then there are no inhibitors... */
1643 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
1645 return bus_log_parse_error(r);
1647 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
1648 _cleanup_free_ char *comm = NULL, *user = NULL;
1649 _cleanup_strv_free_ char **sv = NULL;
1651 if (!streq(mode, "block"))
1654 sv = strv_split(what, ":");
1658 if ((pid_t) pid < 0)
1659 return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
1661 if (!strv_contains(sv,
1666 ACTION_KEXEC) ? "shutdown" : "sleep"))
1669 get_process_comm(pid, &comm);
1670 user = uid_to_name(uid);
1672 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
1673 who, (pid_t) pid, strna(comm), strna(user), why);
1678 return bus_log_parse_error(r);
1680 r = sd_bus_message_exit_container(reply);
1682 return bus_log_parse_error(r);
1684 /* Check for current sessions */
1685 sd_get_sessions(&sessions);
1686 STRV_FOREACH(s, sessions) {
1687 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
1689 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
1692 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
1695 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
1698 sd_session_get_tty(*s, &tty);
1699 sd_session_get_seat(*s, &seat);
1700 sd_session_get_service(*s, &service);
1701 user = uid_to_name(uid);
1703 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
1710 log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'loginctl -i %s'.",
1711 action_table[a].verb);
1716 static int start_special(int argc, char *argv[], void *userdata) {
1717 sd_bus *bus = userdata;
1724 a = verb_to_action(argv[0]);
1726 /* Switch to cancel shutdown, if a shutdown action was requested,
1727 and the option to cancel it was set: */
1728 if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
1729 && (arg_action == ACTION_CANCEL_SHUTDOWN) )
1730 return elogind_cancel_shutdown(bus);
1732 r = check_inhibitors(bus, a);
1736 /* No power off actions in chroot environments */
1737 if ((a == ACTION_POWEROFF ||
1738 a == ACTION_REBOOT) &&
1739 (running_in_chroot() > 0) ) {
1740 log_info("Running in chroot, ignoring request.");
1744 /* Check time arguments */
1745 if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
1747 && (arg_action != ACTION_CANCEL_SHUTDOWN) ) {
1748 r = parse_shutdown_time_spec(argv[1], &arg_when);
1750 log_error("Failed to parse time specification: %s", argv[optind]);
1754 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
1756 /* The optional user wall message must be set */
1757 if ((argc > 1) && (arg_action == ACTION_CANCEL_SHUTDOWN) )
1758 /* No time argument for shutdown cancel */
1761 /* We skip the time argument */
1765 arg_wall = strv_copy(wall);
1770 /* Perform requested action */
1776 ACTION_HYBRID_SLEEP)) {
1778 return elogind_schedule_shutdown(bus, a);
1780 return elogind_reboot(bus, a);
1786 static int help(int argc, char *argv[], void *userdata) {
1788 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1789 "Send control commands to or query the login manager.\n\n"
1790 " -h --help Show this help\n"
1791 " --version Show package version\n"
1792 " --no-pager Do not pipe output into a pager\n"
1793 " --no-wall Do not print any wall message\n"
1794 " --no-legend Do not show the headers and footers\n"
1795 " --no-ask-password Don't prompt for password\n"
1796 " -H --host=[USER@]HOST Operate on remote host\n"
1797 " -M --machine=CONTAINER Operate on local container\n"
1798 " -p --property=NAME Show only properties by this name\n"
1799 " -a --all Show all properties, including empty ones\n"
1800 " --value When showing properties, only print the value\n"
1801 " -l --full Do not ellipsize output\n"
1802 " --kill-who=WHO Who to send signal to\n"
1803 " -s --signal=SIGNAL Which signal to send\n"
1804 #if 0 /// UNNEEDED by elogind
1805 " -n --lines=INTEGER Number of journal entries to show\n"
1806 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1807 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1809 " -c Cancel a pending shutdown or reboot\n"
1810 " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n"
1811 "Session Commands:\n"
1812 " list-sessions List sessions\n"
1813 " session-status [ID...] Show session status\n"
1814 " show-session [ID...] Show properties of sessions or the manager\n"
1815 " activate [ID] Activate a session\n"
1816 " lock-session [ID...] Screen lock one or more sessions\n"
1817 " unlock-session [ID...] Screen unlock one or more sessions\n"
1818 " lock-sessions Screen lock all current sessions\n"
1819 " unlock-sessions Screen unlock all current sessions\n"
1820 " terminate-session ID... Terminate one or more sessions\n"
1821 " kill-session ID... Send signal to processes of a session\n\n"
1823 " list-users List users\n"
1824 " user-status [USER...] Show user status\n"
1825 " show-user [USER...] Show properties of users or the manager\n"
1826 " enable-linger [USER...] Enable linger state of one or more users\n"
1827 " disable-linger [USER...] Disable linger state of one or more users\n"
1828 " terminate-user USER... Terminate all sessions of one or more users\n"
1829 " kill-user USER... Send signal to processes of a user\n\n"
1831 " list-seats List seats\n"
1832 " seat-status [NAME...] Show seat status\n"
1833 " show-seat [NAME...] Show properties of seats or the manager\n"
1834 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1835 " flush-devices Flush all device associations\n"
1836 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1837 "System Commands:\n"
1838 " poweroff [TIME] [WALL...] Turn off the machine\n"
1839 " reboot [TIME] [WALL...] Reboot the machine\n"
1840 " suspend Suspend the machine to memory\n"
1841 " hibernate Suspend the machine to disk\n"
1842 " hybrid-sleep Suspend the machine to memory and disk\n"
1843 , program_invocation_short_name);
1848 static int parse_argv(int argc, char *argv[]) {
1851 ARG_VERSION = 0x100,
1857 ARG_NO_ASK_PASSWORD,
1860 static const struct option options[] = {
1861 { "help", no_argument, NULL, 'h' },
1862 { "version", no_argument, NULL, ARG_VERSION },
1863 { "property", required_argument, NULL, 'p' },
1864 { "all", no_argument, NULL, 'a' },
1865 { "value", no_argument, NULL, ARG_VALUE },
1866 { "full", no_argument, NULL, 'l' },
1867 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1868 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1869 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1870 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1871 { "signal", required_argument, NULL, 's' },
1872 { "host", required_argument, NULL, 'H' },
1873 { "machine", required_argument, NULL, 'M' },
1874 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1875 #if 0 /// UNNEEDED by elogind
1876 { "lines", required_argument, NULL, 'n' },
1877 { "output", required_argument, NULL, 'o' },
1879 { "ignore-inhibitors", no_argument, NULL, 'i' },
1888 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0) {
1893 help(0, NULL, NULL);
1900 r = strv_extend(&arg_property, optarg);
1904 /* If the user asked for a particular
1905 * property, show it to him, even if it is
1923 #if 0 /// UNNEEDED by elogind
1925 if (safe_atou(optarg, &arg_lines) < 0) {
1926 log_error("Failed to parse lines '%s'", optarg);
1932 arg_output = output_mode_from_string(optarg);
1933 if (arg_output < 0) {
1934 log_error("Unknown output '%s'.", optarg);
1941 arg_no_pager = true;
1952 case ARG_NO_ASK_PASSWORD:
1953 arg_ask_password = false;
1957 arg_kill_who = optarg;
1961 arg_signal = signal_from_string_try_harder(optarg);
1962 if (arg_signal < 0) {
1963 log_error("Failed to parse signal string %s.", optarg);
1969 arg_transport = BUS_TRANSPORT_REMOTE;
1974 arg_transport = BUS_TRANSPORT_MACHINE;
1979 arg_action = ACTION_CANCEL_SHUTDOWN;
1983 arg_ignore_inhibitors = true;
1990 assert_not_reached("Unhandled option");
1997 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1999 static const Verb verbs[] = {
2000 { "help", VERB_ANY, VERB_ANY, 0, help },
2001 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
2002 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
2003 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
2004 { "activate", VERB_ANY, 2, 0, activate },
2005 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
2006 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
2007 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
2008 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
2009 { "terminate-session", 2, VERB_ANY, 0, activate },
2010 { "kill-session", 2, VERB_ANY, 0, kill_session },
2011 { "list-users", VERB_ANY, 1, 0, list_users },
2012 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
2013 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
2014 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
2015 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
2016 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
2017 { "kill-user", 2, VERB_ANY, 0, kill_user },
2018 { "list-seats", VERB_ANY, 1, 0, list_seats },
2019 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
2020 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
2021 { "attach", 3, VERB_ANY, 0, attach },
2022 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
2023 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
2024 { "poweroff", VERB_ANY, VERB_ANY, 0, start_special },
2025 { "reboot", VERB_ANY, VERB_ANY, 0, start_special },
2026 { "suspend", VERB_ANY, 1, 0, start_special },
2027 { "hibernate", VERB_ANY, 1, 0, start_special },
2028 { "hybrid-sleep", VERB_ANY, 1, 0, start_special },
2029 { "cancel-shutdown", VERB_ANY, 1, 0, start_special },
2033 if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
2034 return elogind_cancel_shutdown(bus);
2036 return dispatch_verb(argc, argv, verbs, bus);
2039 int main(int argc, char *argv[]) {
2043 setlocale(LC_ALL, "");
2044 elogind_set_program_name(argv[0]);
2045 log_parse_environment();
2048 r = parse_argv(argc, argv);
2052 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
2054 log_error_errno(r, "Failed to create bus connection: %m");
2058 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
2059 r = loginctl_main(argc, argv, bus);
2062 sd_bus_flush_close_unref(bus);
2065 polkit_agent_close();
2067 strv_free(arg_property);
2068 strv_free(arg_wall);
2070 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;