1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
14 #include "alloc-util.h"
15 #include "bus-error.h"
16 #include "bus-unit-util.h"
18 #include "cgroup-show.h"
19 #include "cgroup-util.h"
20 #include "format-table.h"
22 #include "logs-show.h"
25 #include "parse-util.h"
26 #include "process-util.h"
28 #include "signal-util.h"
29 #include "spawn-polkit-agent.h"
30 #include "string-table.h"
32 #include "sysfs-show.h"
33 #include "terminal-util.h"
34 #include "unit-name.h"
35 #include "user-util.h"
39 static char **arg_property
= NULL
;
40 static bool arg_all
= false;
41 static bool arg_value
= false;
42 static bool arg_full
= false;
43 static bool arg_no_pager
= false;
44 static bool arg_legend
= true;
45 static const char *arg_kill_who
= NULL
;
46 static int arg_signal
= SIGTERM
;
47 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
48 static char *arg_host
= NULL
;
49 static bool arg_ask_password
= true;
50 static unsigned arg_lines
= 10;
51 static OutputMode arg_output
= OUTPUT_SHORT
;
53 static OutputFlags
get_output_flags(void) {
56 arg_all
* OUTPUT_SHOW_ALL
|
57 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
58 colors_enabled() * OUTPUT_COLOR
;
61 static int get_session_path(sd_bus
*bus
, const char *session_id
, sd_bus_error
*error
, char **path
) {
62 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
66 r
= sd_bus_call_method(
68 "org.freedesktop.login1",
69 "/org/freedesktop/login1",
70 "org.freedesktop.login1.Manager",
77 r
= sd_bus_message_read(reply
, "o", &ans
);
89 static int show_table(Table
*table
, const char *word
) {
95 if (table_get_rows(table
) > 1) {
96 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
98 return log_error_errno(r
, "Failed to sort table: %m");
100 table_set_header(table
, arg_legend
);
102 r
= table_print(table
, NULL
);
104 return log_error_errno(r
, "Failed to show table: %m");
108 if (table_get_rows(table
) > 1)
109 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
111 printf("No %s.\n", word
);
117 static int list_sessions(int argc
, char *argv
[], void *userdata
) {
118 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
119 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
120 _cleanup_(table_unrefp
) Table
*table
= NULL
;
121 sd_bus
*bus
= userdata
;
127 (void) pager_open(arg_no_pager
, false);
129 r
= sd_bus_call_method(
131 "org.freedesktop.login1",
132 "/org/freedesktop/login1",
133 "org.freedesktop.login1.Manager",
138 return log_error_errno(r
, "Failed to list sessions: %s", bus_error_message(&error
, r
));
140 r
= sd_bus_message_enter_container(reply
, 'a', "(susso)");
142 return bus_log_parse_error(r
);
144 table
= table_new("SESSION", "UID", "USER", "SEAT", "TTY");
148 /* Right-align the first two fields (since they are numeric) */
149 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
150 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100);
153 _cleanup_(sd_bus_error_free
) sd_bus_error error_tty
= SD_BUS_ERROR_NULL
;
154 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply_tty
= NULL
;
155 const char *id
, *user
, *seat
, *object
, *tty
= NULL
;
158 r
= sd_bus_message_read(reply
, "(susso)", &id
, &uid
, &user
, &seat
, &object
);
160 return bus_log_parse_error(r
);
164 r
= sd_bus_get_property(
166 "org.freedesktop.login1",
168 "org.freedesktop.login1.Session",
174 log_warning_errno(r
, "Failed to get TTY for session %s: %s", id
, bus_error_message(&error_tty
, r
));
176 r
= sd_bus_message_read(reply_tty
, "s", &tty
);
178 return bus_log_parse_error(r
);
181 r
= table_add_many(table
,
186 TABLE_STRING
, strna(tty
));
188 return log_error_errno(r
, "Failed to add row to table: %m");
191 r
= sd_bus_message_exit_container(reply
);
193 return bus_log_parse_error(r
);
195 return show_table(table
, "sessions");
198 static int list_users(int argc
, char *argv
[], void *userdata
) {
199 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
200 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
201 _cleanup_(table_unrefp
) Table
*table
= NULL
;
202 sd_bus
*bus
= userdata
;
208 (void) pager_open(arg_no_pager
, false);
210 r
= sd_bus_call_method(
212 "org.freedesktop.login1",
213 "/org/freedesktop/login1",
214 "org.freedesktop.login1.Manager",
219 return log_error_errno(r
, "Failed to list users: %s", bus_error_message(&error
, r
));
221 r
= sd_bus_message_enter_container(reply
, 'a', "(uso)");
223 return bus_log_parse_error(r
);
225 table
= table_new("UID", "USER");
229 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
232 const char *user
, *object
;
235 r
= sd_bus_message_read(reply
, "(uso)", &uid
, &user
, &object
);
237 return bus_log_parse_error(r
);
241 r
= table_add_many(table
,
245 return log_error_errno(r
, "Failed to add row to table: %m");
248 r
= sd_bus_message_exit_container(reply
);
250 return bus_log_parse_error(r
);
252 return show_table(table
, "users");
255 static int list_seats(int argc
, char *argv
[], void *userdata
) {
256 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
257 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
258 _cleanup_(table_unrefp
) Table
*table
= NULL
;
259 sd_bus
*bus
= userdata
;
265 (void) pager_open(arg_no_pager
, false);
267 r
= sd_bus_call_method(
269 "org.freedesktop.login1",
270 "/org/freedesktop/login1",
271 "org.freedesktop.login1.Manager",
276 return log_error_errno(r
, "Failed to list seats: %s", bus_error_message(&error
, r
));
278 r
= sd_bus_message_enter_container(reply
, 'a', "(so)");
280 return bus_log_parse_error(r
);
282 table
= table_new("SEAT");
287 const char *seat
, *object
;
289 r
= sd_bus_message_read(reply
, "(so)", &seat
, &object
);
291 return bus_log_parse_error(r
);
295 r
= table_add_cell(table
, NULL
, TABLE_STRING
, seat
);
297 return log_error_errno(r
, "Failed to add row to table: %m");
300 r
= sd_bus_message_exit_container(reply
);
302 return bus_log_parse_error(r
);
304 return show_table(table
, "seats");
307 static int show_unit_cgroup(sd_bus
*bus
, const char *interface
, const char *unit
, pid_t leader
) {
308 _cleanup_free_
char *cgroup
= NULL
;
309 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
316 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
329 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
332 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
335 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
337 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
340 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
342 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
347 typedef struct SessionStatusInfo
{
351 struct dual_timestamp timestamp
;
357 const char *remote_host
;
358 const char *remote_user
;
368 typedef struct UserStatusInfo
{
372 struct dual_timestamp timestamp
;
379 typedef struct SeatStatusInfo
{
381 const char *active_session
;
385 static void user_status_info_clear(UserStatusInfo
*info
) {
387 strv_free(info
->sessions
);
392 static void seat_status_info_clear(SeatStatusInfo
*info
) {
394 strv_free(info
->sessions
);
399 static int prop_map_first_of_struct(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
400 const char *contents
;
403 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
407 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, contents
);
411 r
= sd_bus_message_read_basic(m
, contents
[0], userdata
);
415 r
= sd_bus_message_skip(m
, contents
+1);
419 r
= sd_bus_message_exit_container(m
);
426 static int prop_map_sessions_strv(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
433 r
= sd_bus_message_enter_container(m
, 'a', "(so)");
437 while ((r
= sd_bus_message_read(m
, "(so)", &name
, NULL
)) > 0) {
438 r
= strv_extend(userdata
, name
);
445 return sd_bus_message_exit_container(m
);
448 static int print_session_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
450 static const struct bus_properties_map map
[] = {
451 { "Id", "s", NULL
, offsetof(SessionStatusInfo
, id
) },
452 { "Name", "s", NULL
, offsetof(SessionStatusInfo
, name
) },
453 { "TTY", "s", NULL
, offsetof(SessionStatusInfo
, tty
) },
454 { "Display", "s", NULL
, offsetof(SessionStatusInfo
, display
) },
455 { "RemoteHost", "s", NULL
, offsetof(SessionStatusInfo
, remote_host
) },
456 { "RemoteUser", "s", NULL
, offsetof(SessionStatusInfo
, remote_user
) },
457 { "Service", "s", NULL
, offsetof(SessionStatusInfo
, service
) },
458 { "Desktop", "s", NULL
, offsetof(SessionStatusInfo
, desktop
) },
459 { "Type", "s", NULL
, offsetof(SessionStatusInfo
, type
) },
460 { "Class", "s", NULL
, offsetof(SessionStatusInfo
, class) },
461 { "Scope", "s", NULL
, offsetof(SessionStatusInfo
, scope
) },
462 { "State", "s", NULL
, offsetof(SessionStatusInfo
, state
) },
463 { "VTNr", "u", NULL
, offsetof(SessionStatusInfo
, vtnr
) },
464 { "Leader", "u", NULL
, offsetof(SessionStatusInfo
, leader
) },
465 { "Remote", "b", NULL
, offsetof(SessionStatusInfo
, remote
) },
466 { "Timestamp", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.realtime
) },
467 { "TimestampMonotonic", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.monotonic
) },
468 { "User", "(uo)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, uid
) },
469 { "Seat", "(so)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, seat
) },
473 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
474 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
475 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
476 char since2
[FORMAT_TIMESTAMP_MAX
];
478 SessionStatusInfo i
= {};
481 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
483 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
490 printf("%s - ", strna(i
.id
));
493 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
495 printf("%"PRIu32
"\n", 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: %"PRIu32
, 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 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Scope", i
.scope
, i
.leader
);
570 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
572 show_journal_by_unit(
577 i
.timestamp
.monotonic
,
580 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
581 SD_JOURNAL_LOCAL_ONLY
,
590 static int print_user_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
592 static const struct bus_properties_map map
[] = {
593 { "Name", "s", NULL
, offsetof(UserStatusInfo
, name
) },
594 { "Linger", "b", NULL
, offsetof(UserStatusInfo
, linger
) },
595 { "Slice", "s", NULL
, offsetof(UserStatusInfo
, slice
) },
596 { "State", "s", NULL
, offsetof(UserStatusInfo
, state
) },
597 { "UID", "u", NULL
, offsetof(UserStatusInfo
, uid
) },
598 { "Timestamp", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.realtime
) },
599 { "TimestampMonotonic", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.monotonic
) },
600 { "Display", "(so)", prop_map_first_of_struct
, offsetof(UserStatusInfo
, display
) },
601 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(UserStatusInfo
, sessions
) },
605 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
606 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
607 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
608 char since2
[FORMAT_TIMESTAMP_MAX
];
610 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
613 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
615 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
623 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
625 printf("%"PRIu32
"\n", i
.uid
);
627 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
628 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
631 printf("\t Since: %s; %s\n", s2
, s1
);
633 printf("\t Since: %s\n", s2
);
635 if (!isempty(i
.state
))
636 printf("\t State: %s\n", i
.state
);
638 if (!strv_isempty(i
.sessions
)) {
640 printf("\tSessions:");
642 STRV_FOREACH(l
, i
.sessions
)
644 streq_ptr(*l
, i
.display
) ? "*" : "",
650 printf("\t Linger: %s\n", yes_no(i
.linger
));
653 printf("\t Unit: %s\n", i
.slice
);
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
,
673 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
675 static const struct bus_properties_map map
[] = {
676 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
677 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
678 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
682 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
683 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
684 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
687 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, 0, &error
, &m
, &i
);
689 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
696 printf("%s\n", strna(i
.id
));
698 if (!strv_isempty(i
.sessions
)) {
700 printf("\tSessions:");
702 STRV_FOREACH(l
, i
.sessions
) {
703 if (streq_ptr(*l
, i
.active_session
))
712 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
721 printf("\t Devices:\n");
723 show_sysfs(i
.id
, "\t\t ", c
, get_output_flags());
729 #define property(name, fmt, ...) \
732 printf(fmt "\n", __VA_ARGS__); \
734 printf("%s=" fmt "\n", name, __VA_ARGS__); \
737 static int print_property(const char *name
, sd_bus_message
*m
, bool value
, bool all
) {
739 const char *contents
;
745 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
751 case SD_BUS_TYPE_STRUCT
:
753 if (contents
[0] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
756 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
758 return bus_log_parse_error(r
);
760 if (all
|| !isempty(s
))
761 property(name
, "%s", s
);
765 } else if (contents
[0] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
768 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
770 return bus_log_parse_error(r
);
772 if (!uid_is_valid(uid
)) {
773 log_error("Invalid user ID: " UID_FMT
, uid
);
777 property(name
, UID_FMT
, uid
);
782 case SD_BUS_TYPE_ARRAY
:
784 if (contents
[0] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
788 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
790 return bus_log_parse_error(r
);
795 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
796 printf("%s%s", space
? " " : "", s
);
804 return bus_log_parse_error(r
);
806 r
= sd_bus_message_exit_container(m
);
808 return bus_log_parse_error(r
);
818 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
830 r
= bus_print_all_properties(bus
, "org.freedesktop.login1", path
, print_property
, arg_property
, arg_value
, arg_all
, NULL
);
832 return bus_log_parse_error(r
);
837 static int show_session(int argc
, char *argv
[], void *userdata
) {
838 bool properties
, new_line
= false;
839 sd_bus
*bus
= userdata
;
841 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
842 _cleanup_free_
char *path
= NULL
;
847 properties
= !strstr(argv
[0], "status");
849 (void) pager_open(arg_no_pager
, false);
852 const char *session
, *p
= "/org/freedesktop/login1/session/self";
855 /* If no argument is specified inspect the manager itself */
856 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
858 /* And in the pretty case, show data of the calling session */
859 session
= getenv("XDG_SESSION_ID");
861 r
= get_session_path(bus
, session
, &error
, &path
);
863 log_error("Failed to get session path: %s", bus_error_message(&error
, r
));
869 return print_session_status_info(bus
, p
, &new_line
);
872 for (i
= 1; i
< argc
; i
++) {
873 r
= get_session_path(bus
, argv
[i
], &error
, &path
);
875 log_error("Failed to get session path: %s", bus_error_message(&error
, r
));
880 r
= show_properties(bus
, path
, &new_line
);
882 r
= print_session_status_info(bus
, path
, &new_line
);
891 static int show_user(int argc
, char *argv
[], void *userdata
) {
892 bool properties
, new_line
= false;
893 sd_bus
*bus
= userdata
;
899 properties
= !strstr(argv
[0], "status");
901 (void) pager_open(arg_no_pager
, false);
904 /* If not argument is specified inspect the manager
907 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
909 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
912 for (i
= 1; i
< argc
; i
++) {
913 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
914 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
915 const char *path
= NULL
;
918 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
920 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
922 r
= sd_bus_call_method(
924 "org.freedesktop.login1",
925 "/org/freedesktop/login1",
926 "org.freedesktop.login1.Manager",
929 "u", (uint32_t) uid
);
931 log_error("Failed to get user: %s", bus_error_message(&error
, r
));
935 r
= sd_bus_message_read(reply
, "o", &path
);
937 return bus_log_parse_error(r
);
940 r
= show_properties(bus
, path
, &new_line
);
942 r
= print_user_status_info(bus
, path
, &new_line
);
951 static int show_seat(int argc
, char *argv
[], void *userdata
) {
952 bool properties
, new_line
= false;
953 sd_bus
*bus
= userdata
;
959 properties
= !strstr(argv
[0], "status");
961 (void) pager_open(arg_no_pager
, false);
964 /* If not argument is specified inspect the manager
967 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
969 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/self", &new_line
);
972 for (i
= 1; i
< argc
; i
++) {
973 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
974 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
975 const char *path
= NULL
;
977 r
= sd_bus_call_method(
979 "org.freedesktop.login1",
980 "/org/freedesktop/login1",
981 "org.freedesktop.login1.Manager",
986 log_error("Failed to get seat: %s", bus_error_message(&error
, r
));
990 r
= sd_bus_message_read(reply
, "o", &path
);
992 return bus_log_parse_error(r
);
995 r
= show_properties(bus
, path
, &new_line
);
997 r
= print_seat_status_info(bus
, path
, &new_line
);
1006 static int activate(int argc
, char *argv
[], void *userdata
) {
1007 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1008 sd_bus
*bus
= userdata
;
1009 char *short_argv
[3];
1015 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1018 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1019 * session name, in which case logind will try to guess our session. */
1021 short_argv
[0] = argv
[0];
1022 short_argv
[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1023 short_argv
[2] = NULL
;
1029 for (i
= 1; i
< argc
; i
++) {
1031 r
= sd_bus_call_method(
1033 "org.freedesktop.login1",
1034 "/org/freedesktop/login1",
1035 "org.freedesktop.login1.Manager",
1036 streq(argv
[0], "lock-session") ? "LockSession" :
1037 streq(argv
[0], "unlock-session") ? "UnlockSession" :
1038 streq(argv
[0], "terminate-session") ? "TerminateSession" :
1043 log_error("Failed to issue method call: %s", bus_error_message(&error
, -r
));
1051 static int kill_session(int argc
, char *argv
[], void *userdata
) {
1052 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1053 sd_bus
*bus
= userdata
;
1059 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1062 arg_kill_who
= "all";
1064 for (i
= 1; i
< argc
; i
++) {
1066 r
= sd_bus_call_method(
1068 "org.freedesktop.login1",
1069 "/org/freedesktop/login1",
1070 "org.freedesktop.login1.Manager",
1073 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1075 log_error("Could not kill session: %s", bus_error_message(&error
, -r
));
1083 static int enable_linger(int argc
, char *argv
[], void *userdata
) {
1084 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1085 sd_bus
*bus
= userdata
;
1086 char* short_argv
[3];
1093 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1095 b
= streq(argv
[0], "enable-linger");
1098 /* No argument? Let's use an empty user name,
1099 * then logind will use our user. */
1101 short_argv
[0] = argv
[0];
1102 short_argv
[1] = (char*) "";
1103 short_argv
[2] = NULL
;
1108 for (i
= 1; i
< argc
; i
++) {
1111 if (isempty(argv
[i
]))
1114 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1116 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1119 r
= sd_bus_call_method(
1121 "org.freedesktop.login1",
1122 "/org/freedesktop/login1",
1123 "org.freedesktop.login1.Manager",
1126 "ubb", (uint32_t) uid
, b
, true);
1128 log_error("Could not enable linger: %s", bus_error_message(&error
, -r
));
1136 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1137 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1138 sd_bus
*bus
= userdata
;
1144 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1146 for (i
= 1; i
< argc
; i
++) {
1149 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1151 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1153 r
= sd_bus_call_method(
1155 "org.freedesktop.login1",
1156 "/org/freedesktop/login1",
1157 "org.freedesktop.login1.Manager",
1160 "u", (uint32_t) uid
);
1162 log_error("Could not terminate user: %s", bus_error_message(&error
, -r
));
1170 static int kill_user(int argc
, char *argv
[], void *userdata
) {
1171 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1172 sd_bus
*bus
= userdata
;
1178 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1181 arg_kill_who
= "all";
1183 for (i
= 1; i
< argc
; i
++) {
1186 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1188 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1190 r
= sd_bus_call_method(
1192 "org.freedesktop.login1",
1193 "/org/freedesktop/login1",
1194 "org.freedesktop.login1.Manager",
1197 "ui", (uint32_t) uid
, arg_signal
);
1199 log_error("Could not kill user: %s", bus_error_message(&error
, -r
));
1207 static int attach(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 for (i
= 2; i
< argc
; i
++) {
1219 r
= sd_bus_call_method(
1221 "org.freedesktop.login1",
1222 "/org/freedesktop/login1",
1223 "org.freedesktop.login1.Manager",
1226 "ssb", argv
[1], argv
[i
], true);
1229 log_error("Could not attach device: %s", bus_error_message(&error
, -r
));
1237 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1238 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1239 sd_bus
*bus
= userdata
;
1245 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1247 r
= sd_bus_call_method(
1249 "org.freedesktop.login1",
1250 "/org/freedesktop/login1",
1251 "org.freedesktop.login1.Manager",
1256 log_error("Could not flush devices: %s", bus_error_message(&error
, -r
));
1261 static int lock_sessions(int argc
, char *argv
[], void *userdata
) {
1262 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1263 sd_bus
*bus
= userdata
;
1269 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1271 r
= sd_bus_call_method(
1273 "org.freedesktop.login1",
1274 "/org/freedesktop/login1",
1275 "org.freedesktop.login1.Manager",
1276 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1280 log_error("Could not lock sessions: %s", bus_error_message(&error
, -r
));
1285 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1286 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1287 sd_bus
*bus
= userdata
;
1293 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1295 for (i
= 1; i
< argc
; i
++) {
1297 r
= sd_bus_call_method(
1299 "org.freedesktop.login1",
1300 "/org/freedesktop/login1",
1301 "org.freedesktop.login1.Manager",
1306 log_error("Could not terminate seat: %s", bus_error_message(&error
, -r
));
1314 static int help(int argc
, char *argv
[], void *userdata
) {
1316 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1317 "Send control commands to or query the login manager.\n\n"
1318 " -h --help Show this help\n"
1319 " --version Show package version\n"
1320 " --no-pager Do not pipe output into a pager\n"
1321 " --no-legend Do not show the headers and footers\n"
1322 " --no-ask-password Don't prompt for password\n"
1323 " -H --host=[USER@]HOST Operate on remote host\n"
1324 " -M --machine=CONTAINER Operate on local container\n"
1325 " -p --property=NAME Show only properties by this name\n"
1326 " -a --all Show all properties, including empty ones\n"
1327 " --value When showing properties, only print the value\n"
1328 " -l --full Do not ellipsize output\n"
1329 " --kill-who=WHO Who to send signal to\n"
1330 " -s --signal=SIGNAL Which signal to send\n"
1331 " -n --lines=INTEGER Number of journal entries to show\n"
1332 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1333 " short-iso, short-iso-precise, short-full,\n"
1334 " short-monotonic, short-unix, verbose, export,\n"
1335 " json, json-pretty, json-sse, cat)\n"
1336 "Session Commands:\n"
1337 " list-sessions List sessions\n"
1338 " session-status [ID...] Show session status\n"
1339 " show-session [ID...] Show properties of sessions or the manager\n"
1340 " activate [ID] Activate a session\n"
1341 " lock-session [ID...] Screen lock one or more sessions\n"
1342 " unlock-session [ID...] Screen unlock one or more sessions\n"
1343 " lock-sessions Screen lock all current sessions\n"
1344 " unlock-sessions Screen unlock all current sessions\n"
1345 " terminate-session ID... Terminate one or more sessions\n"
1346 " kill-session ID... Send signal to processes of a session\n\n"
1348 " list-users List users\n"
1349 " user-status [USER...] Show user status\n"
1350 " show-user [USER...] Show properties of users or the manager\n"
1351 " enable-linger [USER...] Enable linger state of one or more users\n"
1352 " disable-linger [USER...] Disable linger state of one or more users\n"
1353 " terminate-user USER... Terminate all sessions of one or more users\n"
1354 " kill-user USER... Send signal to processes of a user\n\n"
1356 " list-seats List seats\n"
1357 " seat-status [NAME...] Show seat status\n"
1358 " show-seat [NAME...] Show properties of seats or the manager\n"
1359 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1360 " flush-devices Flush all device associations\n"
1361 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1362 , program_invocation_short_name
);
1367 static int parse_argv(int argc
, char *argv
[]) {
1370 ARG_VERSION
= 0x100,
1375 ARG_NO_ASK_PASSWORD
,
1378 static const struct option options
[] = {
1379 { "help", no_argument
, NULL
, 'h' },
1380 { "version", no_argument
, NULL
, ARG_VERSION
},
1381 { "property", required_argument
, NULL
, 'p' },
1382 { "all", no_argument
, NULL
, 'a' },
1383 { "value", no_argument
, NULL
, ARG_VALUE
},
1384 { "full", no_argument
, NULL
, 'l' },
1385 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1386 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1387 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1388 { "signal", required_argument
, NULL
, 's' },
1389 { "host", required_argument
, NULL
, 'H' },
1390 { "machine", required_argument
, NULL
, 'M' },
1391 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1392 { "lines", required_argument
, NULL
, 'n' },
1393 { "output", required_argument
, NULL
, 'o' },
1402 while ((c
= getopt_long(argc
, argv
, "hp:als:H:M:n:o:", options
, NULL
)) >= 0)
1407 help(0, NULL
, NULL
);
1414 r
= strv_extend(&arg_property
, optarg
);
1418 /* If the user asked for a particular
1419 * property, show it to him, even if it is
1438 if (safe_atou(optarg
, &arg_lines
) < 0) {
1439 log_error("Failed to parse lines '%s'", optarg
);
1445 if (streq(optarg
, "help")) {
1446 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
1450 arg_output
= output_mode_from_string(optarg
);
1451 if (arg_output
< 0) {
1452 log_error("Unknown output '%s'.", optarg
);
1458 arg_no_pager
= true;
1465 case ARG_NO_ASK_PASSWORD
:
1466 arg_ask_password
= false;
1470 arg_kill_who
= optarg
;
1474 if (streq(optarg
, "help")) {
1475 DUMP_STRING_TABLE(signal
, int, _NSIG
);
1479 arg_signal
= signal_from_string(optarg
);
1480 if (arg_signal
< 0) {
1481 log_error("Failed to parse signal string %s.", optarg
);
1487 arg_transport
= BUS_TRANSPORT_REMOTE
;
1492 arg_transport
= BUS_TRANSPORT_MACHINE
;
1500 assert_not_reached("Unhandled option");
1506 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1508 static const Verb verbs
[] = {
1509 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1510 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1511 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1512 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1513 { "activate", VERB_ANY
, 2, 0, activate
},
1514 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1515 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1516 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1517 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1518 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1519 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1520 { "list-users", VERB_ANY
, 1, 0, list_users
},
1521 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1522 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1523 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1524 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1525 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1526 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1527 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1528 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1529 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1530 { "attach", 3, VERB_ANY
, 0, attach
},
1531 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1532 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1536 return dispatch_verb(argc
, argv
, verbs
, bus
);
1539 int main(int argc
, char *argv
[]) {
1543 setlocale(LC_ALL
, "");
1544 log_parse_environment();
1548 r
= parse_argv(argc
, argv
);
1552 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1554 log_error_errno(r
, "Failed to create bus connection: %m");
1558 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1560 r
= loginctl_main(argc
, argv
, bus
);
1563 /* make sure we terminate the bus connection first, and then close the
1564 * pager, see issue #3543 for the details. */
1565 sd_bus_flush_close_unref(bus
);
1567 polkit_agent_close();
1569 strv_free(arg_property
);
1571 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;