1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
13 #include "bus-unit-util.h"
15 #include "cgroup-show.h"
16 #include "cgroup-util.h"
17 #include "format-table.h"
19 #include "logs-show.h"
22 #include "parse-util.h"
23 #include "process-util.h"
25 #include "signal-util.h"
26 #include "spawn-polkit-agent.h"
27 #include "string-table.h"
29 #include "sysfs-show.h"
30 #include "terminal-util.h"
31 #include "unit-name.h"
32 #include "user-util.h"
36 static char **arg_property
= NULL
;
37 static bool arg_all
= false;
38 static bool arg_value
= false;
39 static bool arg_full
= false;
40 static bool arg_no_pager
= false;
41 static bool arg_legend
= true;
42 static const char *arg_kill_who
= NULL
;
43 static int arg_signal
= SIGTERM
;
44 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
45 static char *arg_host
= NULL
;
46 static bool arg_ask_password
= true;
47 static unsigned arg_lines
= 10;
48 static OutputMode arg_output
= OUTPUT_SHORT
;
50 static OutputFlags
get_output_flags(void) {
53 arg_all
* OUTPUT_SHOW_ALL
|
54 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
55 colors_enabled() * OUTPUT_COLOR
;
58 static int get_session_path(sd_bus
*bus
, const char *session_id
, sd_bus_error
*error
, char **path
) {
59 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
63 r
= sd_bus_call_method(
65 "org.freedesktop.login1",
66 "/org/freedesktop/login1",
67 "org.freedesktop.login1.Manager",
74 r
= sd_bus_message_read(reply
, "o", &ans
);
86 static int show_table(Table
*table
, const char *word
) {
92 if (table_get_rows(table
) > 1) {
93 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
95 return log_error_errno(r
, "Failed to sort table: %m");
97 table_set_header(table
, arg_legend
);
99 r
= table_print(table
, NULL
);
101 return log_error_errno(r
, "Failed to show table: %m");
105 if (table_get_rows(table
) > 1)
106 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
108 printf("No %s.\n", word
);
114 static int list_sessions(int argc
, char *argv
[], void *userdata
) {
115 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
116 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
117 _cleanup_(table_unrefp
) Table
*table
= NULL
;
118 sd_bus
*bus
= userdata
;
124 (void) 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 return log_error_errno(r
, "Failed to list sessions: %s", bus_error_message(&error
, r
));
137 r
= sd_bus_message_enter_container(reply
, 'a', "(susso)");
139 return bus_log_parse_error(r
);
141 table
= table_new("SESSION", "UID", "USER", "SEAT", "TTY");
145 /* Right-align the first two fields (since they are numeric) */
146 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
147 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100);
150 _cleanup_(sd_bus_error_free
) sd_bus_error error_tty
= SD_BUS_ERROR_NULL
;
151 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply_tty
= NULL
;
152 const char *id
, *user
, *seat
, *object
, *tty
= NULL
;
155 r
= sd_bus_message_read(reply
, "(susso)", &id
, &uid
, &user
, &seat
, &object
);
157 return bus_log_parse_error(r
);
161 r
= sd_bus_get_property(
163 "org.freedesktop.login1",
165 "org.freedesktop.login1.Session",
171 log_warning_errno(r
, "Failed to get TTY for session %s: %s", id
, bus_error_message(&error_tty
, r
));
173 r
= sd_bus_message_read(reply_tty
, "s", &tty
);
175 return bus_log_parse_error(r
);
178 r
= table_add_many(table
,
183 TABLE_STRING
, strna(tty
));
185 return log_error_errno(r
, "Failed to add row to table: %m");
188 r
= sd_bus_message_exit_container(reply
);
190 return bus_log_parse_error(r
);
192 return show_table(table
, "sessions");
195 static int list_users(int argc
, char *argv
[], void *userdata
) {
196 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
197 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
198 _cleanup_(table_unrefp
) Table
*table
= NULL
;
199 sd_bus
*bus
= userdata
;
205 (void) pager_open(arg_no_pager
, false);
207 r
= sd_bus_call_method(
209 "org.freedesktop.login1",
210 "/org/freedesktop/login1",
211 "org.freedesktop.login1.Manager",
216 return log_error_errno(r
, "Failed to list users: %s", bus_error_message(&error
, r
));
218 r
= sd_bus_message_enter_container(reply
, 'a', "(uso)");
220 return bus_log_parse_error(r
);
222 table
= table_new("UID", "USER");
226 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
232 r
= sd_bus_message_read(reply
, "(uso)", &uid
, &user
, NULL
);
234 return bus_log_parse_error(r
);
238 r
= table_add_many(table
,
242 return log_error_errno(r
, "Failed to add row to table: %m");
245 r
= sd_bus_message_exit_container(reply
);
247 return bus_log_parse_error(r
);
249 return show_table(table
, "users");
252 static int list_seats(int argc
, char *argv
[], void *userdata
) {
253 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
254 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
255 _cleanup_(table_unrefp
) Table
*table
= NULL
;
256 sd_bus
*bus
= userdata
;
262 (void) pager_open(arg_no_pager
, false);
264 r
= sd_bus_call_method(
266 "org.freedesktop.login1",
267 "/org/freedesktop/login1",
268 "org.freedesktop.login1.Manager",
273 return log_error_errno(r
, "Failed to list seats: %s", bus_error_message(&error
, r
));
275 r
= sd_bus_message_enter_container(reply
, 'a', "(so)");
277 return bus_log_parse_error(r
);
279 table
= table_new("SEAT");
286 r
= sd_bus_message_read(reply
, "(so)", &seat
, NULL
);
288 return bus_log_parse_error(r
);
292 r
= table_add_cell(table
, NULL
, TABLE_STRING
, seat
);
294 return log_error_errno(r
, "Failed to add row to table: %m");
297 r
= sd_bus_message_exit_container(reply
);
299 return bus_log_parse_error(r
);
301 return show_table(table
, "seats");
304 static int show_unit_cgroup(sd_bus
*bus
, const char *interface
, const char *unit
, pid_t leader
) {
305 _cleanup_free_
char *cgroup
= NULL
;
306 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
313 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
326 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
329 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
332 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
334 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
337 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
339 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
344 typedef struct SessionStatusInfo
{
348 struct dual_timestamp timestamp
;
354 const char *remote_host
;
355 const char *remote_user
;
365 typedef struct UserStatusInfo
{
369 struct dual_timestamp timestamp
;
376 typedef struct SeatStatusInfo
{
378 const char *active_session
;
382 static void user_status_info_clear(UserStatusInfo
*info
) {
384 strv_free(info
->sessions
);
389 static void seat_status_info_clear(SeatStatusInfo
*info
) {
391 strv_free(info
->sessions
);
396 static int prop_map_first_of_struct(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
397 const char *contents
;
400 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
404 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, contents
);
408 r
= sd_bus_message_read_basic(m
, contents
[0], userdata
);
412 r
= sd_bus_message_skip(m
, contents
+1);
416 r
= sd_bus_message_exit_container(m
);
423 static int prop_map_sessions_strv(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
430 r
= sd_bus_message_enter_container(m
, 'a', "(so)");
434 while ((r
= sd_bus_message_read(m
, "(so)", &name
, NULL
)) > 0) {
435 r
= strv_extend(userdata
, name
);
442 return sd_bus_message_exit_container(m
);
445 static int print_session_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
447 static const struct bus_properties_map map
[] = {
448 { "Id", "s", NULL
, offsetof(SessionStatusInfo
, id
) },
449 { "Name", "s", NULL
, offsetof(SessionStatusInfo
, name
) },
450 { "TTY", "s", NULL
, offsetof(SessionStatusInfo
, tty
) },
451 { "Display", "s", NULL
, offsetof(SessionStatusInfo
, display
) },
452 { "RemoteHost", "s", NULL
, offsetof(SessionStatusInfo
, remote_host
) },
453 { "RemoteUser", "s", NULL
, offsetof(SessionStatusInfo
, remote_user
) },
454 { "Service", "s", NULL
, offsetof(SessionStatusInfo
, service
) },
455 { "Desktop", "s", NULL
, offsetof(SessionStatusInfo
, desktop
) },
456 { "Type", "s", NULL
, offsetof(SessionStatusInfo
, type
) },
457 { "Class", "s", NULL
, offsetof(SessionStatusInfo
, class) },
458 { "Scope", "s", NULL
, offsetof(SessionStatusInfo
, scope
) },
459 { "State", "s", NULL
, offsetof(SessionStatusInfo
, state
) },
460 { "VTNr", "u", NULL
, offsetof(SessionStatusInfo
, vtnr
) },
461 { "Leader", "u", NULL
, offsetof(SessionStatusInfo
, leader
) },
462 { "Remote", "b", NULL
, offsetof(SessionStatusInfo
, remote
) },
463 { "Timestamp", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.realtime
) },
464 { "TimestampMonotonic", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.monotonic
) },
465 { "User", "(uo)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, uid
) },
466 { "Seat", "(so)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, seat
) },
470 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
471 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
472 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
473 char since2
[FORMAT_TIMESTAMP_MAX
];
475 SessionStatusInfo i
= {};
478 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
480 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
487 printf("%s - ", strna(i
.id
));
490 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
492 printf("%"PRIu32
"\n", i
.uid
);
494 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
495 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
498 printf("\t Since: %s; %s\n", s2
, s1
);
500 printf("\t Since: %s\n", s2
);
503 _cleanup_free_
char *t
= NULL
;
505 printf("\t Leader: %"PRIu32
, i
.leader
);
507 get_process_comm(i
.leader
, &t
);
514 if (!isempty(i
.seat
)) {
515 printf("\t Seat: %s", i
.seat
);
518 printf("; vc%u", i
.vtnr
);
524 printf("\t TTY: %s\n", i
.tty
);
526 printf("\t Display: %s\n", i
.display
);
528 if (i
.remote_host
&& i
.remote_user
)
529 printf("\t Remote: %s@%s\n", i
.remote_user
, i
.remote_host
);
530 else if (i
.remote_host
)
531 printf("\t Remote: %s\n", i
.remote_host
);
532 else if (i
.remote_user
)
533 printf("\t Remote: user %s\n", i
.remote_user
);
535 printf("\t Remote: Yes\n");
538 printf("\t Service: %s", i
.service
);
541 printf("; type %s", i
.type
);
544 printf("; class %s", i
.class);
548 printf("\t Type: %s", i
.type
);
551 printf("; class %s", i
.class);
555 printf("\t Class: %s\n", i
.class);
557 if (!isempty(i
.desktop
))
558 printf("\t Desktop: %s\n", i
.desktop
);
561 printf("\t State: %s\n", i
.state
);
564 printf("\t Unit: %s\n", i
.scope
);
565 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Scope", i
.scope
, i
.leader
);
567 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
569 show_journal_by_unit(
574 i
.timestamp
.monotonic
,
577 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
578 SD_JOURNAL_LOCAL_ONLY
,
587 static int print_user_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
589 static const struct bus_properties_map map
[] = {
590 { "Name", "s", NULL
, offsetof(UserStatusInfo
, name
) },
591 { "Linger", "b", NULL
, offsetof(UserStatusInfo
, linger
) },
592 { "Slice", "s", NULL
, offsetof(UserStatusInfo
, slice
) },
593 { "State", "s", NULL
, offsetof(UserStatusInfo
, state
) },
594 { "UID", "u", NULL
, offsetof(UserStatusInfo
, uid
) },
595 { "Timestamp", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.realtime
) },
596 { "TimestampMonotonic", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.monotonic
) },
597 { "Display", "(so)", prop_map_first_of_struct
, offsetof(UserStatusInfo
, display
) },
598 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(UserStatusInfo
, sessions
) },
602 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
603 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
604 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
605 char since2
[FORMAT_TIMESTAMP_MAX
];
607 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
610 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
612 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
620 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
622 printf("%"PRIu32
"\n", i
.uid
);
624 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
625 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
628 printf("\t Since: %s; %s\n", s2
, s1
);
630 printf("\t Since: %s\n", s2
);
632 if (!isempty(i
.state
))
633 printf("\t State: %s\n", i
.state
);
635 if (!strv_isempty(i
.sessions
)) {
637 printf("\tSessions:");
639 STRV_FOREACH(l
, i
.sessions
)
641 streq_ptr(*l
, i
.display
) ? "*" : "",
647 printf("\t Linger: %s\n", yes_no(i
.linger
));
650 printf("\t Unit: %s\n", i
.slice
);
651 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Slice", i
.slice
, 0);
653 show_journal_by_unit(
658 i
.timestamp
.monotonic
,
661 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
662 SD_JOURNAL_LOCAL_ONLY
,
670 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
672 static const struct bus_properties_map map
[] = {
673 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
674 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
675 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
679 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
680 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
681 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
684 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, 0, &error
, &m
, &i
);
686 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
693 printf("%s\n", strna(i
.id
));
695 if (!strv_isempty(i
.sessions
)) {
697 printf("\tSessions:");
699 STRV_FOREACH(l
, i
.sessions
) {
700 if (streq_ptr(*l
, i
.active_session
))
709 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
718 printf("\t Devices:\n");
720 show_sysfs(i
.id
, "\t\t ", c
, get_output_flags());
726 static int print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
728 const char *contents
;
734 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
740 case SD_BUS_TYPE_STRUCT
:
742 if (contents
[0] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
745 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
747 return bus_log_parse_error(r
);
749 if (all
|| !isempty(s
))
750 bus_print_property_value(name
, expected_value
, value
, "%s", s
);
754 } else if (contents
[0] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
757 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
759 return bus_log_parse_error(r
);
761 if (!uid_is_valid(uid
)) {
762 log_error("Invalid user ID: " UID_FMT
, uid
);
766 bus_print_property_value(name
, expected_value
, value
, UID_FMT
, uid
);
771 case SD_BUS_TYPE_ARRAY
:
773 if (contents
[0] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
777 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
779 return bus_log_parse_error(r
);
784 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
785 printf("%s%s", space
? " " : "", s
);
793 return bus_log_parse_error(r
);
795 r
= sd_bus_message_exit_container(m
);
797 return bus_log_parse_error(r
);
807 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
819 r
= bus_print_all_properties(bus
, "org.freedesktop.login1", path
, print_property
, arg_property
, arg_value
, arg_all
, NULL
);
821 return bus_log_parse_error(r
);
826 static int show_session(int argc
, char *argv
[], void *userdata
) {
827 bool properties
, new_line
= false;
828 sd_bus
*bus
= userdata
;
830 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
831 _cleanup_free_
char *path
= NULL
;
836 properties
= !strstr(argv
[0], "status");
838 (void) pager_open(arg_no_pager
, false);
841 const char *session
, *p
= "/org/freedesktop/login1/session/self";
844 /* If no argument is specified inspect the manager itself */
845 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
847 /* And in the pretty case, show data of the calling session */
848 session
= getenv("XDG_SESSION_ID");
850 r
= get_session_path(bus
, session
, &error
, &path
);
852 return log_error_errno(r
, "Failed to get session path: %s", bus_error_message(&error
, r
));
857 return print_session_status_info(bus
, p
, &new_line
);
860 for (i
= 1; i
< argc
; i
++) {
861 r
= get_session_path(bus
, argv
[i
], &error
, &path
);
863 return log_error_errno(r
, "Failed to get session path: %s", bus_error_message(&error
, r
));
866 r
= show_properties(bus
, path
, &new_line
);
868 r
= print_session_status_info(bus
, path
, &new_line
);
877 static int show_user(int argc
, char *argv
[], void *userdata
) {
878 bool properties
, new_line
= false;
879 sd_bus
*bus
= userdata
;
885 properties
= !strstr(argv
[0], "status");
887 (void) pager_open(arg_no_pager
, false);
890 /* If not argument is specified inspect the manager
893 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
895 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
898 for (i
= 1; i
< argc
; i
++) {
899 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
900 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
901 const char *path
= NULL
;
904 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
906 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
908 r
= sd_bus_call_method(
910 "org.freedesktop.login1",
911 "/org/freedesktop/login1",
912 "org.freedesktop.login1.Manager",
915 "u", (uint32_t) uid
);
917 return log_error_errno(r
, "Failed to get user: %s", bus_error_message(&error
, r
));
919 r
= sd_bus_message_read(reply
, "o", &path
);
921 return bus_log_parse_error(r
);
924 r
= show_properties(bus
, path
, &new_line
);
926 r
= print_user_status_info(bus
, path
, &new_line
);
935 static int show_seat(int argc
, char *argv
[], void *userdata
) {
936 bool properties
, new_line
= false;
937 sd_bus
*bus
= userdata
;
943 properties
= !strstr(argv
[0], "status");
945 (void) pager_open(arg_no_pager
, false);
948 /* If not argument is specified inspect the manager
951 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
953 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/self", &new_line
);
956 for (i
= 1; i
< argc
; i
++) {
957 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
958 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
959 const char *path
= NULL
;
961 r
= sd_bus_call_method(
963 "org.freedesktop.login1",
964 "/org/freedesktop/login1",
965 "org.freedesktop.login1.Manager",
970 return log_error_errno(r
, "Failed to get seat: %s", bus_error_message(&error
, r
));
972 r
= sd_bus_message_read(reply
, "o", &path
);
974 return bus_log_parse_error(r
);
977 r
= show_properties(bus
, path
, &new_line
);
979 r
= print_seat_status_info(bus
, path
, &new_line
);
988 static int activate(int argc
, char *argv
[], void *userdata
) {
989 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
990 sd_bus
*bus
= userdata
;
997 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1000 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1001 * session name, in which case logind will try to guess our session. */
1003 short_argv
[0] = argv
[0];
1004 short_argv
[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1005 short_argv
[2] = NULL
;
1011 for (i
= 1; i
< argc
; i
++) {
1013 r
= sd_bus_call_method(
1015 "org.freedesktop.login1",
1016 "/org/freedesktop/login1",
1017 "org.freedesktop.login1.Manager",
1018 streq(argv
[0], "lock-session") ? "LockSession" :
1019 streq(argv
[0], "unlock-session") ? "UnlockSession" :
1020 streq(argv
[0], "terminate-session") ? "TerminateSession" :
1025 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, -r
));
1031 static int kill_session(int argc
, char *argv
[], void *userdata
) {
1032 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1033 sd_bus
*bus
= userdata
;
1039 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1042 arg_kill_who
= "all";
1044 for (i
= 1; i
< argc
; i
++) {
1046 r
= sd_bus_call_method(
1048 "org.freedesktop.login1",
1049 "/org/freedesktop/login1",
1050 "org.freedesktop.login1.Manager",
1053 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1055 return log_error_errno(r
, "Could not kill session: %s", bus_error_message(&error
, -r
));
1061 static int enable_linger(int argc
, char *argv
[], void *userdata
) {
1062 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1063 sd_bus
*bus
= userdata
;
1064 char* short_argv
[3];
1071 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1073 b
= streq(argv
[0], "enable-linger");
1076 /* No argument? Let's use an empty user name,
1077 * then logind will use our user. */
1079 short_argv
[0] = argv
[0];
1080 short_argv
[1] = (char*) "";
1081 short_argv
[2] = NULL
;
1086 for (i
= 1; i
< argc
; i
++) {
1089 if (isempty(argv
[i
]))
1092 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1094 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1097 r
= sd_bus_call_method(
1099 "org.freedesktop.login1",
1100 "/org/freedesktop/login1",
1101 "org.freedesktop.login1.Manager",
1104 "ubb", (uint32_t) uid
, b
, true);
1106 return log_error_errno(r
, "Could not enable linger: %s", bus_error_message(&error
, -r
));
1112 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1113 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1114 sd_bus
*bus
= userdata
;
1120 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1122 for (i
= 1; i
< argc
; i
++) {
1125 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1127 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1129 r
= sd_bus_call_method(
1131 "org.freedesktop.login1",
1132 "/org/freedesktop/login1",
1133 "org.freedesktop.login1.Manager",
1136 "u", (uint32_t) uid
);
1138 return log_error_errno(r
, "Could not terminate user: %s", bus_error_message(&error
, -r
));
1144 static int kill_user(int argc
, char *argv
[], void *userdata
) {
1145 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1146 sd_bus
*bus
= userdata
;
1152 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1155 arg_kill_who
= "all";
1157 for (i
= 1; i
< argc
; i
++) {
1160 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1162 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1164 r
= sd_bus_call_method(
1166 "org.freedesktop.login1",
1167 "/org/freedesktop/login1",
1168 "org.freedesktop.login1.Manager",
1171 "ui", (uint32_t) uid
, arg_signal
);
1173 return log_error_errno(r
, "Could not kill user: %s", bus_error_message(&error
, -r
));
1179 static int attach(int argc
, char *argv
[], void *userdata
) {
1180 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1181 sd_bus
*bus
= userdata
;
1187 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1189 for (i
= 2; i
< argc
; i
++) {
1191 r
= sd_bus_call_method(
1193 "org.freedesktop.login1",
1194 "/org/freedesktop/login1",
1195 "org.freedesktop.login1.Manager",
1198 "ssb", argv
[1], argv
[i
], true);
1201 return log_error_errno(r
, "Could not attach device: %s", bus_error_message(&error
, -r
));
1207 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1208 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1209 sd_bus
*bus
= userdata
;
1215 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1217 r
= sd_bus_call_method(
1219 "org.freedesktop.login1",
1220 "/org/freedesktop/login1",
1221 "org.freedesktop.login1.Manager",
1226 return log_error_errno(r
, "Could not flush devices: %s", bus_error_message(&error
, -r
));
1231 static int lock_sessions(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(arg_transport
, arg_ask_password
);
1241 r
= sd_bus_call_method(
1243 "org.freedesktop.login1",
1244 "/org/freedesktop/login1",
1245 "org.freedesktop.login1.Manager",
1246 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1250 return log_error_errno(r
, "Could not lock sessions: %s", bus_error_message(&error
, -r
));
1255 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1256 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1257 sd_bus
*bus
= userdata
;
1263 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1265 for (i
= 1; i
< argc
; i
++) {
1267 r
= sd_bus_call_method(
1269 "org.freedesktop.login1",
1270 "/org/freedesktop/login1",
1271 "org.freedesktop.login1.Manager",
1276 return log_error_errno(r
, "Could not terminate seat: %s", bus_error_message(&error
, -r
));
1282 static int help(int argc
, char *argv
[], void *userdata
) {
1283 _cleanup_free_
char *link
= NULL
;
1286 (void) pager_open(arg_no_pager
, false);
1288 r
= terminal_urlify_man("loginctl", "1", &link
);
1292 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1293 "Send control commands to or query the login manager.\n\n"
1294 " -h --help Show this help\n"
1295 " --version Show package version\n"
1296 " --no-pager Do not pipe output into a pager\n"
1297 " --no-legend Do not show the headers and footers\n"
1298 " --no-ask-password Don't prompt for password\n"
1299 " -H --host=[USER@]HOST Operate on remote host\n"
1300 " -M --machine=CONTAINER Operate on local container\n"
1301 " -p --property=NAME Show only properties by this name\n"
1302 " -a --all Show all properties, including empty ones\n"
1303 " --value When showing properties, only print the value\n"
1304 " -l --full Do not ellipsize output\n"
1305 " --kill-who=WHO Who to send signal to\n"
1306 " -s --signal=SIGNAL Which signal to send\n"
1307 " -n --lines=INTEGER Number of journal entries to show\n"
1308 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1309 " short-iso, short-iso-precise, short-full,\n"
1310 " short-monotonic, short-unix, verbose, export,\n"
1311 " json, json-pretty, json-sse, json-seq, cat,\n"
1313 "Session Commands:\n"
1314 " list-sessions List sessions\n"
1315 " session-status [ID...] Show session status\n"
1316 " show-session [ID...] Show properties of sessions or the manager\n"
1317 " activate [ID] Activate a session\n"
1318 " lock-session [ID...] Screen lock one or more sessions\n"
1319 " unlock-session [ID...] Screen unlock one or more sessions\n"
1320 " lock-sessions Screen lock all current sessions\n"
1321 " unlock-sessions Screen unlock all current sessions\n"
1322 " terminate-session ID... Terminate one or more sessions\n"
1323 " kill-session ID... Send signal to processes of a session\n\n"
1325 " list-users List users\n"
1326 " user-status [USER...] Show user status\n"
1327 " show-user [USER...] Show properties of users or the manager\n"
1328 " enable-linger [USER...] Enable linger state of one or more users\n"
1329 " disable-linger [USER...] Disable linger state of one or more users\n"
1330 " terminate-user USER... Terminate all sessions of one or more users\n"
1331 " kill-user USER... Send signal to processes of a user\n\n"
1333 " list-seats List seats\n"
1334 " seat-status [NAME...] Show seat status\n"
1335 " show-seat [NAME...] Show properties of seats or the manager\n"
1336 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1337 " flush-devices Flush all device associations\n"
1338 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1339 "\nSee the %s for details.\n"
1340 , program_invocation_short_name
1347 static int parse_argv(int argc
, char *argv
[]) {
1350 ARG_VERSION
= 0x100,
1355 ARG_NO_ASK_PASSWORD
,
1358 static const struct option options
[] = {
1359 { "help", no_argument
, NULL
, 'h' },
1360 { "version", no_argument
, NULL
, ARG_VERSION
},
1361 { "property", required_argument
, NULL
, 'p' },
1362 { "all", no_argument
, NULL
, 'a' },
1363 { "value", no_argument
, NULL
, ARG_VALUE
},
1364 { "full", no_argument
, NULL
, 'l' },
1365 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1366 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1367 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1368 { "signal", required_argument
, NULL
, 's' },
1369 { "host", required_argument
, NULL
, 'H' },
1370 { "machine", required_argument
, NULL
, 'M' },
1371 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1372 { "lines", required_argument
, NULL
, 'n' },
1373 { "output", required_argument
, NULL
, 'o' },
1382 while ((c
= getopt_long(argc
, argv
, "hp:als:H:M:n:o:", options
, NULL
)) >= 0)
1387 return help(0, NULL
, NULL
);
1393 r
= strv_extend(&arg_property
, optarg
);
1397 /* If the user asked for a particular
1398 * property, show it to him, even if it is
1417 if (safe_atou(optarg
, &arg_lines
) < 0) {
1418 log_error("Failed to parse lines '%s'", optarg
);
1424 if (streq(optarg
, "help")) {
1425 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
1429 arg_output
= output_mode_from_string(optarg
);
1430 if (arg_output
< 0) {
1431 log_error("Unknown output '%s'.", optarg
);
1437 arg_no_pager
= true;
1444 case ARG_NO_ASK_PASSWORD
:
1445 arg_ask_password
= false;
1449 arg_kill_who
= optarg
;
1453 if (streq(optarg
, "help")) {
1454 DUMP_STRING_TABLE(signal
, int, _NSIG
);
1458 arg_signal
= signal_from_string(optarg
);
1459 if (arg_signal
< 0) {
1460 log_error("Failed to parse signal string %s.", optarg
);
1466 arg_transport
= BUS_TRANSPORT_REMOTE
;
1471 arg_transport
= BUS_TRANSPORT_MACHINE
;
1479 assert_not_reached("Unhandled option");
1485 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1487 static const Verb verbs
[] = {
1488 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1489 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1490 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1491 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1492 { "activate", VERB_ANY
, 2, 0, activate
},
1493 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1494 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1495 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1496 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1497 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1498 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1499 { "list-users", VERB_ANY
, 1, 0, list_users
},
1500 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1501 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1502 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1503 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1504 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1505 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1506 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1507 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1508 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1509 { "attach", 3, VERB_ANY
, 0, attach
},
1510 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1511 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1515 return dispatch_verb(argc
, argv
, verbs
, bus
);
1518 int main(int argc
, char *argv
[]) {
1522 setlocale(LC_ALL
, "");
1523 log_parse_environment();
1527 r
= parse_argv(argc
, argv
);
1531 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1533 log_error_errno(r
, "Failed to create bus connection: %m");
1537 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1539 r
= loginctl_main(argc
, argv
, bus
);
1542 /* make sure we terminate the bus connection first, and then close the
1543 * pager, see issue #3543 for the details. */
1544 sd_bus_flush_close_unref(bus
);
1546 polkit_agent_close();
1548 strv_free(arg_property
);
1550 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;