1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <dbus/dbus.h>
34 #include "dbus-common.h"
37 #include "cgroup-show.h"
38 #include "sysfs-show.h"
39 #include "spawn-polkit-agent.h"
41 static char **arg_property = NULL;
42 static bool arg_all = false;
43 static bool arg_full = false;
44 static bool arg_no_pager = false;
45 static const char *arg_kill_who = NULL;
46 static int arg_signal = SIGTERM;
47 static enum transport {
51 } arg_transport = TRANSPORT_NORMAL;
52 static bool arg_ask_password = true;
53 static const char *arg_host = NULL;
55 static void pager_open_if_enabled(void) {
57 /* Cache result before we open the pager */
64 static void polkit_agent_open_if_enabled(void) {
66 /* Open the polkit agent as a child process if necessary */
68 if (!arg_ask_password)
74 static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
75 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
77 DBusMessageIter iter, sub, sub2;
80 pager_open_if_enabled();
82 r = bus_method_call_with_reply (
84 "org.freedesktop.login1",
85 "/org/freedesktop/login1",
86 "org.freedesktop.login1.Manager",
94 if (!dbus_message_iter_init(reply, &iter) ||
95 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
96 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
97 log_error("Failed to parse reply.");
101 dbus_message_iter_recurse(&iter, &sub);
104 printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
106 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
107 const char *id, *user, *seat, *object;
110 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
111 log_error("Failed to parse reply.");
115 dbus_message_iter_recurse(&sub, &sub2);
117 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
118 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
119 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
120 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
121 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
122 log_error("Failed to parse reply.");
126 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
130 dbus_message_iter_next(&sub);
134 printf("\n%u sessions listed.\n", k);
139 static int list_users(DBusConnection *bus, char **args, unsigned n) {
140 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
142 DBusMessageIter iter, sub, sub2;
145 pager_open_if_enabled();
147 r = bus_method_call_with_reply (
149 "org.freedesktop.login1",
150 "/org/freedesktop/login1",
151 "org.freedesktop.login1.Manager",
159 if (!dbus_message_iter_init(reply, &iter) ||
160 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
161 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
162 log_error("Failed to parse reply.");
166 dbus_message_iter_recurse(&iter, &sub);
169 printf("%10s %-16s\n", "UID", "USER");
171 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
172 const char *user, *object;
175 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
176 log_error("Failed to parse reply.");
180 dbus_message_iter_recurse(&sub, &sub2);
182 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
183 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
184 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
185 log_error("Failed to parse reply.");
189 printf("%10u %-16s\n", (unsigned) uid, user);
193 dbus_message_iter_next(&sub);
197 printf("\n%u users listed.\n", k);
202 static int list_seats(DBusConnection *bus, char **args, unsigned n) {
203 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
205 DBusMessageIter iter, sub, sub2;
208 pager_open_if_enabled();
210 r = bus_method_call_with_reply (
212 "org.freedesktop.login1",
213 "/org/freedesktop/login1",
214 "org.freedesktop.login1.Manager",
222 if (!dbus_message_iter_init(reply, &iter) ||
223 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
224 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
225 log_error("Failed to parse reply.");
229 dbus_message_iter_recurse(&iter, &sub);
232 printf("%-16s\n", "SEAT");
234 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
235 const char *seat, *object;
237 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
238 log_error("Failed to parse reply.");
242 dbus_message_iter_recurse(&sub, &sub2);
244 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
245 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
246 log_error("Failed to parse reply.");
250 printf("%-16s\n", seat);
254 dbus_message_iter_next(&sub);
258 printf("\n%u seats listed.\n", k);
263 typedef struct SessionStatusInfo {
268 const char *default_control_group;
274 const char *remote_host;
275 const char *remote_user;
283 typedef struct UserStatusInfo {
287 const char *default_control_group;
293 typedef struct SeatStatusInfo {
295 const char *active_session;
299 static void print_session_status_info(SessionStatusInfo *i) {
300 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
301 char since2[FORMAT_TIMESTAMP_MAX], *s2;
304 printf("%s - ", strna(i->id));
307 printf("%s (%u)\n", i->name, (unsigned) i->uid);
309 printf("%u\n", (unsigned) i->uid);
311 s1 = format_timestamp_relative(since1, sizeof(since1), i->timestamp);
312 s2 = format_timestamp(since2, sizeof(since2), i->timestamp);
315 printf("\t Since: %s; %s\n", s2, s1);
317 printf("\t Since: %s\n", s2);
322 printf("\t Leader: %u", (unsigned) i->leader);
324 get_process_comm(i->leader, &t);
334 printf("\t Seat: %s", i->seat);
337 printf("; vc%i", i->vtnr);
343 printf("\t TTY: %s\n", i->tty);
345 printf("\t Display: %s\n", i->display);
347 if (i->remote_host && i->remote_user)
348 printf("\t Remote: %s@%s\n", i->remote_user, i->remote_host);
349 else if (i->remote_host)
350 printf("\t Remote: %s\n", i->remote_host);
351 else if (i->remote_user)
352 printf("\t Remote: user %s\n", i->remote_user);
354 printf("\t Remote: Yes\n");
357 printf("\t Service: %s", i->service);
360 printf("; type %s", i->type);
363 printf("; class %s", i->class);
366 } else if (i->type) {
367 printf("\t Type: %s\n", i->type);
370 printf("; class %s", i->class);
372 printf("\t Class: %s\n", i->class);
375 printf("\t State: %s\n", i->state);
377 if (i->default_control_group) {
380 arg_all * OUTPUT_SHOW_ALL |
381 arg_full * OUTPUT_FULL_WIDTH;
383 printf("\t CGroup: %s\n", i->default_control_group);
385 if (arg_transport != TRANSPORT_SSH) {
392 show_cgroup_and_extra_by_spec(i->default_control_group,
393 "\t\t ", c, false, &i->leader,
394 i->leader > 0 ? 1 : 0,
400 static void print_user_status_info(UserStatusInfo *i) {
401 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
402 char since2[FORMAT_TIMESTAMP_MAX], *s2;
406 printf("%s (%u)\n", i->name, (unsigned) i->uid);
408 printf("%u\n", (unsigned) i->uid);
410 s1 = format_timestamp_relative(since1, sizeof(since1), i->timestamp);
411 s2 = format_timestamp(since2, sizeof(since2), i->timestamp);
414 printf("\t Since: %s; %s\n", s2, s1);
416 printf("\t Since: %s\n", s2);
418 if (!isempty(i->state))
419 printf("\t State: %s\n", i->state);
421 if (!strv_isempty(i->sessions)) {
423 printf("\tSessions:");
425 STRV_FOREACH(l, i->sessions) {
426 if (streq_ptr(*l, i->display))
435 if (i->default_control_group) {
438 arg_all * OUTPUT_SHOW_ALL |
439 arg_full * OUTPUT_FULL_WIDTH;
441 printf("\t CGroup: %s\n", i->default_control_group);
443 if (arg_transport != TRANSPORT_SSH) {
450 show_cgroup_by_path(i->default_control_group, "\t\t ",
451 c, false, output_flags);
456 static void print_seat_status_info(SeatStatusInfo *i) {
459 printf("%s\n", strna(i->id));
461 if (!strv_isempty(i->sessions)) {
463 printf("\tSessions:");
465 STRV_FOREACH(l, i->sessions) {
466 if (streq_ptr(*l, i->active_session))
475 if (arg_transport != TRANSPORT_SSH) {
484 printf("\t Devices:\n");
486 show_sysfs(i->id, "\t\t ", c);
490 static int status_property_session(const char *name, DBusMessageIter *iter, SessionStatusInfo *i) {
495 switch (dbus_message_iter_get_arg_type(iter)) {
497 case DBUS_TYPE_STRING: {
500 dbus_message_iter_get_basic(iter, &s);
503 if (streq(name, "Id"))
505 else if (streq(name, "Name"))
507 else if (streq(name, "DefaultControlGroup"))
508 i->default_control_group = s;
509 else if (streq(name, "TTY"))
511 else if (streq(name, "Display"))
513 else if (streq(name, "RemoteHost"))
515 else if (streq(name, "RemoteUser"))
517 else if (streq(name, "Service"))
519 else if (streq(name, "Type"))
521 else if (streq(name, "Class"))
523 else if (streq(name, "State"))
529 case DBUS_TYPE_UINT32: {
532 dbus_message_iter_get_basic(iter, &u);
534 if (streq(name, "VTNr"))
536 else if (streq(name, "Leader"))
537 i->leader = (pid_t) u;
542 case DBUS_TYPE_BOOLEAN: {
545 dbus_message_iter_get_basic(iter, &b);
547 if (streq(name, "Remote"))
553 case DBUS_TYPE_UINT64: {
556 dbus_message_iter_get_basic(iter, &u);
558 if (streq(name, "Timestamp"))
559 i->timestamp = (usec_t) u;
564 case DBUS_TYPE_STRUCT: {
567 dbus_message_iter_recurse(iter, &sub);
569 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "User")) {
572 dbus_message_iter_get_basic(&sub, &u);
575 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Seat")) {
578 dbus_message_iter_get_basic(&sub, &s);
591 static int status_property_user(const char *name, DBusMessageIter *iter, UserStatusInfo *i) {
596 switch (dbus_message_iter_get_arg_type(iter)) {
598 case DBUS_TYPE_STRING: {
601 dbus_message_iter_get_basic(iter, &s);
604 if (streq(name, "Name"))
606 else if (streq(name, "DefaultControlGroup"))
607 i->default_control_group = s;
608 else if (streq(name, "State"))
614 case DBUS_TYPE_UINT32: {
617 dbus_message_iter_get_basic(iter, &u);
619 if (streq(name, "UID"))
625 case DBUS_TYPE_UINT64: {
628 dbus_message_iter_get_basic(iter, &u);
630 if (streq(name, "Timestamp"))
631 i->timestamp = (usec_t) u;
636 case DBUS_TYPE_STRUCT: {
639 dbus_message_iter_recurse(iter, &sub);
641 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Display")) {
644 dbus_message_iter_get_basic(&sub, &s);
653 case DBUS_TYPE_ARRAY: {
655 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Sessions")) {
656 DBusMessageIter sub, sub2;
658 dbus_message_iter_recurse(iter, &sub);
659 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
663 dbus_message_iter_recurse(&sub, &sub2);
665 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) >= 0 &&
666 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &path, false) >= 0) {
669 l = strv_append(i->sessions, id);
673 strv_free(i->sessions);
677 dbus_message_iter_next(&sub);
688 static int status_property_seat(const char *name, DBusMessageIter *iter, SeatStatusInfo *i) {
693 switch (dbus_message_iter_get_arg_type(iter)) {
695 case DBUS_TYPE_STRING: {
698 dbus_message_iter_get_basic(iter, &s);
701 if (streq(name, "Id"))
707 case DBUS_TYPE_STRUCT: {
710 dbus_message_iter_recurse(iter, &sub);
712 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "ActiveSession")) {
715 dbus_message_iter_get_basic(&sub, &s);
718 i->active_session = s;
724 case DBUS_TYPE_ARRAY: {
726 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Sessions")) {
727 DBusMessageIter sub, sub2;
729 dbus_message_iter_recurse(iter, &sub);
730 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
734 dbus_message_iter_recurse(&sub, &sub2);
736 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) >= 0 &&
737 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &path, false) >= 0) {
740 l = strv_append(i->sessions, id);
744 strv_free(i->sessions);
748 dbus_message_iter_next(&sub);
759 static int print_property(const char *name, DBusMessageIter *iter) {
763 if (arg_property && !strv_find(arg_property, name))
766 switch (dbus_message_iter_get_arg_type(iter)) {
768 case DBUS_TYPE_STRUCT: {
771 dbus_message_iter_recurse(iter, &sub);
773 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING &&
774 (streq(name, "Display") || streq(name, "ActiveSession"))) {
777 dbus_message_iter_get_basic(&sub, &s);
779 if (arg_all || !isempty(s))
780 printf("%s=%s\n", name, s);
786 case DBUS_TYPE_ARRAY:
788 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Sessions")) {
789 DBusMessageIter sub, sub2;
792 dbus_message_iter_recurse(iter, &sub);
793 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
797 dbus_message_iter_recurse(&sub, &sub2);
799 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) >= 0 &&
800 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &path, false) >= 0) {
804 printf("%s=%s", name, id);
809 dbus_message_iter_next(&sub);
812 if (!found && arg_all)
813 printf("%s=\n", name);
823 if (generic_print_property(name, iter, arg_all) > 0)
827 printf("%s=[unprintable]\n", name);
832 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
833 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
834 const char *interface = "";
836 DBusMessageIter iter, sub, sub2, sub3;
837 SessionStatusInfo session_info;
838 UserStatusInfo user_info;
839 SeatStatusInfo seat_info;
848 r = bus_method_call_with_reply(
850 "org.freedesktop.login1",
852 "org.freedesktop.DBus.Properties",
856 DBUS_TYPE_STRING, &interface,
861 if (!dbus_message_iter_init(reply, &iter) ||
862 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
863 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
864 log_error("Failed to parse reply.");
869 dbus_message_iter_recurse(&iter, &sub);
876 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
879 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
880 log_error("Failed to parse reply.");
885 dbus_message_iter_recurse(&sub, &sub2);
887 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
888 log_error("Failed to parse reply.");
893 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
894 log_error("Failed to parse reply.");
899 dbus_message_iter_recurse(&sub2, &sub3);
902 r = print_property(name, &sub3);
903 else if (strstr(verb, "session"))
904 r = status_property_session(name, &sub3, &session_info);
905 else if (strstr(verb, "user"))
906 r = status_property_user(name, &sub3, &user_info);
908 r = status_property_seat(name, &sub3, &seat_info);
911 log_error("Failed to parse reply.");
915 dbus_message_iter_next(&sub);
918 if (!show_properties) {
919 if (strstr(verb, "session"))
920 print_session_status_info(&session_info);
921 else if (strstr(verb, "user"))
922 print_user_status_info(&user_info);
924 print_seat_status_info(&seat_info);
930 strv_free(seat_info.sessions);
931 strv_free(user_info.sessions);
936 static int show(DBusConnection *bus, char **args, unsigned n) {
937 _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
941 bool show_properties, new_line = false;
946 dbus_error_init(&error);
948 show_properties = !strstr(args[0], "status");
950 pager_open_if_enabled();
952 if (show_properties && n <= 1) {
953 /* If not argument is specified inspect the manager
956 ret = show_one(args[0], bus, "/org/freedesktop/login1", show_properties, &new_line);
960 for (i = 1; i < n; i++) {
961 const char *path = NULL;
963 if (strstr(args[0], "session")) {
965 ret = bus_method_call_with_reply (
967 "org.freedesktop.login1",
968 "/org/freedesktop/login1",
969 "org.freedesktop.login1.Manager",
973 DBUS_TYPE_STRING, &args[i],
976 } else if (strstr(args[0], "user")) {
980 ret = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
982 log_error("User %s unknown.", args[i]);
987 ret = bus_method_call_with_reply (
989 "org.freedesktop.login1",
990 "/org/freedesktop/login1",
991 "org.freedesktop.login1.Manager",
995 DBUS_TYPE_UINT32, &u,
999 ret = bus_method_call_with_reply (
1001 "org.freedesktop.login1",
1002 "/org/freedesktop/login1",
1003 "org.freedesktop.login1.Manager",
1007 DBUS_TYPE_STRING, &args[i],
1013 if (!dbus_message_get_args(reply, &error,
1014 DBUS_TYPE_OBJECT_PATH, &path,
1015 DBUS_TYPE_INVALID)) {
1016 log_error("Failed to parse reply: %s", bus_error_message(&error));
1021 r = show_one(args[0], bus, path, show_properties, &new_line);
1027 dbus_error_free(&error);
1032 static int activate(DBusConnection *bus, char **args, unsigned n) {
1038 for (i = 1; i < n; i++) {
1040 ret = bus_method_call_with_reply (
1042 "org.freedesktop.login1",
1043 "/org/freedesktop/login1",
1044 "org.freedesktop.login1.Manager",
1045 streq(args[0], "lock-session") ? "LockSession" :
1046 streq(args[0], "unlock-session") ? "UnlockSession" :
1047 streq(args[0], "terminate-session") ? "TerminateSession" :
1051 DBUS_TYPE_STRING, &args[i],
1061 static int kill_session(DBusConnection *bus, char **args, unsigned n) {
1067 arg_kill_who = "all";
1069 for (i = 1; i < n; i++) {
1072 r = bus_method_call_with_reply (
1074 "org.freedesktop.login1",
1075 "/org/freedesktop/login1",
1076 "org.freedesktop.login1.Manager",
1080 DBUS_TYPE_STRING, &args[i],
1081 DBUS_TYPE_STRING, &arg_kill_who,
1082 DBUS_TYPE_INT32, &arg_signal,
1091 static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
1093 dbus_bool_t b, interactive = true;
1097 polkit_agent_open_if_enabled();
1099 b = streq(args[0], "enable-linger");
1101 for (i = 1; i < n; i++) {
1106 r = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
1108 log_error("Failed to resolve user %s: %s", args[i], strerror(-r));
1113 r = bus_method_call_with_reply (
1115 "org.freedesktop.login1",
1116 "/org/freedesktop/login1",
1117 "org.freedesktop.login1.Manager",
1121 DBUS_TYPE_UINT32, &u,
1122 DBUS_TYPE_BOOLEAN, &b,
1123 DBUS_TYPE_BOOLEAN, &interactive,
1132 static int terminate_user(DBusConnection *bus, char **args, unsigned n) {
1137 for (i = 1; i < n; i++) {
1142 r = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
1144 log_error("Failed to look up user %s: %s", args[i], strerror(-r));
1149 r = bus_method_call_with_reply (
1151 "org.freedesktop.login1",
1152 "/org/freedesktop/login1",
1153 "org.freedesktop.login1.Manager",
1157 DBUS_TYPE_UINT32, &u,
1166 static int kill_user(DBusConnection *bus, char **args, unsigned n) {
1172 arg_kill_who = "all";
1174 for (i = 1; i < n; i++) {
1179 r = get_user_creds((const char**) (args+i), &uid, NULL, NULL, NULL);
1181 log_error("Failed to look up user %s: %s", args[i], strerror(-r));
1186 r = bus_method_call_with_reply (
1188 "org.freedesktop.login1",
1189 "/org/freedesktop/login1",
1190 "org.freedesktop.login1.Manager",
1194 DBUS_TYPE_UINT32, &u,
1195 DBUS_TYPE_INT32, &arg_signal,
1204 static int attach(DBusConnection *bus, char **args, unsigned n) {
1206 dbus_bool_t interactive = true;
1210 polkit_agent_open_if_enabled();
1212 for (i = 2; i < n; i++) {
1215 r = bus_method_call_with_reply (
1217 "org.freedesktop.login1",
1218 "/org/freedesktop/login1",
1219 "org.freedesktop.login1.Manager",
1223 DBUS_TYPE_STRING, &args[1],
1224 DBUS_TYPE_STRING, &args[i],
1225 DBUS_TYPE_BOOLEAN, &interactive,
1234 static int flush_devices(DBusConnection *bus, char **args, unsigned n) {
1235 dbus_bool_t interactive = true;
1239 polkit_agent_open_if_enabled();
1241 return bus_method_call_with_reply (
1243 "org.freedesktop.login1",
1244 "/org/freedesktop/login1",
1245 "org.freedesktop.login1.Manager",
1249 DBUS_TYPE_BOOLEAN, &interactive,
1253 static int lock_sessions(DBusConnection *bus, char **args, unsigned n) {
1256 polkit_agent_open_if_enabled();
1258 return bus_method_call_with_reply (
1260 "org.freedesktop.login1",
1261 "/org/freedesktop/login1",
1262 "org.freedesktop.login1.Manager",
1263 streq(args[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1269 static int terminate_seat(DBusConnection *bus, char **args, unsigned n) {
1274 for (i = 1; i < n; i++) {
1277 r = bus_method_call_with_reply (
1279 "org.freedesktop.login1",
1280 "/org/freedesktop/login1",
1281 "org.freedesktop.login1.Manager",
1285 DBUS_TYPE_STRING, &args[i],
1294 static int help(void) {
1296 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1297 "Send control commands to or query the login manager.\n\n"
1298 " -h --help Show this help\n"
1299 " --version Show package version\n"
1300 " -p --property=NAME Show only properties by this name\n"
1301 " -a --all Show all properties, including empty ones\n"
1302 " --kill-who=WHO Who to send signal to\n"
1303 " --full Do not ellipsize output\n"
1304 " -s --signal=SIGNAL Which signal to send\n"
1305 " --no-ask-password Don't prompt for password\n"
1306 " -H --host=[USER@]HOST Show information for remote host\n"
1307 " -P --privileged Acquire privileges before execution\n"
1308 " --no-pager Do not pipe output into a pager\n\n"
1310 " list-sessions List sessions\n"
1311 " session-status [ID...] Show session status\n"
1312 " show-session [ID...] Show properties of one or more sessions\n"
1313 " activate [ID] Activate a session\n"
1314 " lock-session [ID...] Screen lock one or more sessions\n"
1315 " unlock-session [ID...] Screen unlock one or more sessions\n"
1316 " lock-sessions Screen lock all current sessions\n"
1317 " unlock-sessions Screen unlock all current sessions\n"
1318 " terminate-session [ID...] Terminate one or more sessions\n"
1319 " kill-session [ID...] Send signal to processes of a session\n"
1320 " list-users List users\n"
1321 " user-status [USER...] Show user status\n"
1322 " show-user [USER...] Show properties of one or more users\n"
1323 " enable-linger [USER...] Enable linger state of one or more users\n"
1324 " disable-linger [USER...] Disable linger state of one or more users\n"
1325 " terminate-user [USER...] Terminate all sessions of one or more users\n"
1326 " kill-user [USER...] Send signal to processes of a user\n"
1327 " list-seats List seats\n"
1328 " seat-status [NAME...] Show seat status\n"
1329 " show-seat [NAME...] Show properties of one or more seats\n"
1330 " attach [NAME] [DEVICE...] Attach one or more devices to a seat\n"
1331 " flush-devices Flush all device associations\n"
1332 " terminate-seat [NAME...] Terminate all sessions on one or more seats\n",
1333 program_invocation_short_name);
1338 static int parse_argv(int argc, char *argv[]) {
1341 ARG_VERSION = 0x100,
1344 ARG_NO_ASK_PASSWORD,
1348 static const struct option options[] = {
1349 { "help", no_argument, NULL, 'h' },
1350 { "version", no_argument, NULL, ARG_VERSION },
1351 { "property", required_argument, NULL, 'p' },
1352 { "all", no_argument, NULL, 'a' },
1353 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1354 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1355 { "signal", required_argument, NULL, 's' },
1356 { "host", required_argument, NULL, 'H' },
1357 { "privileged", no_argument, NULL, 'P' },
1358 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1359 { "full", no_argument, NULL, ARG_FULL },
1360 { NULL, 0, NULL, 0 }
1368 while ((c = getopt_long(argc, argv, "hp:as:H:P", options, NULL)) >= 0) {
1377 puts(PACKAGE_STRING);
1378 puts(SYSTEMD_FEATURES);
1384 l = strv_append(arg_property, optarg);
1388 strv_free(arg_property);
1391 /* If the user asked for a particular
1392 * property, show it to him, even if it is
1403 arg_no_pager = true;
1406 case ARG_NO_ASK_PASSWORD:
1407 arg_ask_password = false;
1411 arg_kill_who = optarg;
1415 arg_signal = signal_from_string_try_harder(optarg);
1416 if (arg_signal < 0) {
1417 log_error("Failed to parse signal string %s.", optarg);
1423 arg_transport = TRANSPORT_POLKIT;
1427 arg_transport = TRANSPORT_SSH;
1439 log_error("Unknown option code %c", c);
1447 static int loginctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
1449 static const struct {
1457 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
1459 { "list-sessions", LESS, 1, list_sessions },
1460 { "session-status", MORE, 2, show },
1461 { "show-session", MORE, 1, show },
1462 { "activate", EQUAL, 2, activate },
1463 { "lock-session", MORE, 2, activate },
1464 { "unlock-session", MORE, 2, activate },
1465 { "lock-sessions", EQUAL, 1, lock_sessions },
1466 { "unlock-sessions", EQUAL, 1, lock_sessions },
1467 { "terminate-session", MORE, 2, activate },
1468 { "kill-session", MORE, 2, kill_session },
1469 { "list-users", EQUAL, 1, list_users },
1470 { "user-status", MORE, 2, show },
1471 { "show-user", MORE, 1, show },
1472 { "enable-linger", MORE, 2, enable_linger },
1473 { "disable-linger", MORE, 2, enable_linger },
1474 { "terminate-user", MORE, 2, terminate_user },
1475 { "kill-user", MORE, 2, kill_user },
1476 { "list-seats", EQUAL, 1, list_seats },
1477 { "seat-status", MORE, 2, show },
1478 { "show-seat", MORE, 1, show },
1479 { "attach", MORE, 3, attach },
1480 { "flush-devices", EQUAL, 1, flush_devices },
1481 { "terminate-seat", MORE, 2, terminate_seat },
1491 left = argc - optind;
1494 /* Special rule: no arguments means "list-sessions" */
1497 if (streq(argv[optind], "help")) {
1502 for (i = 0; i < ELEMENTSOF(verbs); i++)
1503 if (streq(argv[optind], verbs[i].verb))
1506 if (i >= ELEMENTSOF(verbs)) {
1507 log_error("Unknown operation %s", argv[optind]);
1512 switch (verbs[i].argc_cmp) {
1515 if (left != verbs[i].argc) {
1516 log_error("Invalid number of arguments.");
1523 if (left < verbs[i].argc) {
1524 log_error("Too few arguments.");
1531 if (left > verbs[i].argc) {
1532 log_error("Too many arguments.");
1539 assert_not_reached("Unknown comparison operator.");
1543 log_error("Failed to get D-Bus connection: %s", error->message);
1547 return verbs[i].dispatch(bus, argv + optind, left);
1550 int main(int argc, char*argv[]) {
1551 int r, retval = EXIT_FAILURE;
1552 DBusConnection *bus = NULL;
1555 dbus_error_init(&error);
1557 setlocale(LC_ALL, "");
1558 log_parse_environment();
1561 r = parse_argv(argc, argv);
1565 retval = EXIT_SUCCESS;
1569 if (arg_transport == TRANSPORT_NORMAL)
1570 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
1571 else if (arg_transport == TRANSPORT_POLKIT)
1572 bus_connect_system_polkit(&bus, &error);
1573 else if (arg_transport == TRANSPORT_SSH)
1574 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
1576 assert_not_reached("Uh, invalid transport...");
1578 r = loginctl_main(bus, argc, argv, &error);
1579 retval = r < 0 ? EXIT_FAILURE : r;
1583 dbus_connection_flush(bus);
1584 dbus_connection_close(bus);
1585 dbus_connection_unref(bus);
1588 dbus_error_free(&error);
1591 strv_free(arg_property);