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"
24 #include "rlimit-util.h"
26 #include "signal-util.h"
27 #include "spawn-polkit-agent.h"
28 #include "string-table.h"
30 #include "sysfs-show.h"
31 #include "terminal-util.h"
32 #include "unit-name.h"
33 #include "user-util.h"
37 static char **arg_property
= NULL
;
38 static bool arg_all
= false;
39 static bool arg_value
= false;
40 static bool arg_full
= false;
41 static bool arg_no_pager
= false;
42 static bool arg_legend
= true;
43 static const char *arg_kill_who
= NULL
;
44 static int arg_signal
= SIGTERM
;
45 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
46 static char *arg_host
= NULL
;
47 static bool arg_ask_password
= true;
48 static unsigned arg_lines
= 10;
49 static OutputMode arg_output
= OUTPUT_SHORT
;
51 static OutputFlags
get_output_flags(void) {
54 arg_all
* OUTPUT_SHOW_ALL
|
55 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
56 colors_enabled() * OUTPUT_COLOR
;
59 static int get_session_path(sd_bus
*bus
, const char *session_id
, sd_bus_error
*error
, char **path
) {
60 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
64 r
= sd_bus_call_method(
66 "org.freedesktop.login1",
67 "/org/freedesktop/login1",
68 "org.freedesktop.login1.Manager",
75 r
= sd_bus_message_read(reply
, "o", &ans
);
87 static int show_table(Table
*table
, const char *word
) {
93 if (table_get_rows(table
) > 1) {
94 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
96 return log_error_errno(r
, "Failed to sort table: %m");
98 table_set_header(table
, arg_legend
);
100 r
= table_print(table
, NULL
);
102 return log_error_errno(r
, "Failed to show table: %m");
106 if (table_get_rows(table
) > 1)
107 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
109 printf("No %s.\n", word
);
115 static int list_sessions(int argc
, char *argv
[], void *userdata
) {
116 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
117 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
118 _cleanup_(table_unrefp
) Table
*table
= NULL
;
119 sd_bus
*bus
= userdata
;
125 (void) pager_open(arg_no_pager
, false);
127 r
= sd_bus_call_method(
129 "org.freedesktop.login1",
130 "/org/freedesktop/login1",
131 "org.freedesktop.login1.Manager",
136 return log_error_errno(r
, "Failed to list sessions: %s", bus_error_message(&error
, r
));
138 r
= sd_bus_message_enter_container(reply
, 'a', "(susso)");
140 return bus_log_parse_error(r
);
142 table
= table_new("SESSION", "UID", "USER", "SEAT", "TTY");
146 /* Right-align the first two fields (since they are numeric) */
147 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
148 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100);
151 _cleanup_(sd_bus_error_free
) sd_bus_error error_tty
= SD_BUS_ERROR_NULL
;
152 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply_tty
= NULL
;
153 const char *id
, *user
, *seat
, *object
, *tty
= NULL
;
156 r
= sd_bus_message_read(reply
, "(susso)", &id
, &uid
, &user
, &seat
, &object
);
158 return bus_log_parse_error(r
);
162 r
= sd_bus_get_property(
164 "org.freedesktop.login1",
166 "org.freedesktop.login1.Session",
172 log_warning_errno(r
, "Failed to get TTY for session %s: %s", id
, bus_error_message(&error_tty
, r
));
174 r
= sd_bus_message_read(reply_tty
, "s", &tty
);
176 return bus_log_parse_error(r
);
179 r
= table_add_many(table
,
184 TABLE_STRING
, strna(tty
));
186 return log_error_errno(r
, "Failed to add row to table: %m");
189 r
= sd_bus_message_exit_container(reply
);
191 return bus_log_parse_error(r
);
193 return show_table(table
, "sessions");
196 static int list_users(int argc
, char *argv
[], void *userdata
) {
197 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
198 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
199 _cleanup_(table_unrefp
) Table
*table
= NULL
;
200 sd_bus
*bus
= userdata
;
206 (void) pager_open(arg_no_pager
, false);
208 r
= sd_bus_call_method(
210 "org.freedesktop.login1",
211 "/org/freedesktop/login1",
212 "org.freedesktop.login1.Manager",
217 return log_error_errno(r
, "Failed to list users: %s", bus_error_message(&error
, r
));
219 r
= sd_bus_message_enter_container(reply
, 'a', "(uso)");
221 return bus_log_parse_error(r
);
223 table
= table_new("UID", "USER");
227 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
233 r
= sd_bus_message_read(reply
, "(uso)", &uid
, &user
, NULL
);
235 return bus_log_parse_error(r
);
239 r
= table_add_many(table
,
243 return log_error_errno(r
, "Failed to add row to table: %m");
246 r
= sd_bus_message_exit_container(reply
);
248 return bus_log_parse_error(r
);
250 return show_table(table
, "users");
253 static int list_seats(int argc
, char *argv
[], void *userdata
) {
254 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
255 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
256 _cleanup_(table_unrefp
) Table
*table
= NULL
;
257 sd_bus
*bus
= userdata
;
263 (void) pager_open(arg_no_pager
, false);
265 r
= sd_bus_call_method(
267 "org.freedesktop.login1",
268 "/org/freedesktop/login1",
269 "org.freedesktop.login1.Manager",
274 return log_error_errno(r
, "Failed to list seats: %s", bus_error_message(&error
, r
));
276 r
= sd_bus_message_enter_container(reply
, 'a', "(so)");
278 return bus_log_parse_error(r
);
280 table
= table_new("SEAT");
287 r
= sd_bus_message_read(reply
, "(so)", &seat
, NULL
);
289 return bus_log_parse_error(r
);
293 r
= table_add_cell(table
, NULL
, TABLE_STRING
, seat
);
295 return log_error_errno(r
, "Failed to add row to table: %m");
298 r
= sd_bus_message_exit_container(reply
);
300 return bus_log_parse_error(r
);
302 return show_table(table
, "seats");
305 static int show_unit_cgroup(sd_bus
*bus
, const char *interface
, const char *unit
, pid_t leader
) {
306 _cleanup_free_
char *cgroup
= NULL
;
307 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
314 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
327 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
330 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
333 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
335 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
338 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
340 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
345 typedef struct SessionStatusInfo
{
349 struct dual_timestamp timestamp
;
355 const char *remote_host
;
356 const char *remote_user
;
366 typedef struct UserStatusInfo
{
370 struct dual_timestamp timestamp
;
377 typedef struct SeatStatusInfo
{
379 const char *active_session
;
383 static void user_status_info_clear(UserStatusInfo
*info
) {
385 strv_free(info
->sessions
);
390 static void seat_status_info_clear(SeatStatusInfo
*info
) {
392 strv_free(info
->sessions
);
397 static int prop_map_first_of_struct(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
398 const char *contents
;
401 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
405 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, contents
);
409 r
= sd_bus_message_read_basic(m
, contents
[0], userdata
);
413 r
= sd_bus_message_skip(m
, contents
+1);
417 r
= sd_bus_message_exit_container(m
);
424 static int prop_map_sessions_strv(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
431 r
= sd_bus_message_enter_container(m
, 'a', "(so)");
435 while ((r
= sd_bus_message_read(m
, "(so)", &name
, NULL
)) > 0) {
436 r
= strv_extend(userdata
, name
);
443 return sd_bus_message_exit_container(m
);
446 static int print_session_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
448 static const struct bus_properties_map map
[] = {
449 { "Id", "s", NULL
, offsetof(SessionStatusInfo
, id
) },
450 { "Name", "s", NULL
, offsetof(SessionStatusInfo
, name
) },
451 { "TTY", "s", NULL
, offsetof(SessionStatusInfo
, tty
) },
452 { "Display", "s", NULL
, offsetof(SessionStatusInfo
, display
) },
453 { "RemoteHost", "s", NULL
, offsetof(SessionStatusInfo
, remote_host
) },
454 { "RemoteUser", "s", NULL
, offsetof(SessionStatusInfo
, remote_user
) },
455 { "Service", "s", NULL
, offsetof(SessionStatusInfo
, service
) },
456 { "Desktop", "s", NULL
, offsetof(SessionStatusInfo
, desktop
) },
457 { "Type", "s", NULL
, offsetof(SessionStatusInfo
, type
) },
458 { "Class", "s", NULL
, offsetof(SessionStatusInfo
, class) },
459 { "Scope", "s", NULL
, offsetof(SessionStatusInfo
, scope
) },
460 { "State", "s", NULL
, offsetof(SessionStatusInfo
, state
) },
461 { "VTNr", "u", NULL
, offsetof(SessionStatusInfo
, vtnr
) },
462 { "Leader", "u", NULL
, offsetof(SessionStatusInfo
, leader
) },
463 { "Remote", "b", NULL
, offsetof(SessionStatusInfo
, remote
) },
464 { "Timestamp", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.realtime
) },
465 { "TimestampMonotonic", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.monotonic
) },
466 { "User", "(uo)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, uid
) },
467 { "Seat", "(so)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, seat
) },
471 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
472 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
473 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
474 char since2
[FORMAT_TIMESTAMP_MAX
];
476 SessionStatusInfo i
= {};
479 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
481 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
488 printf("%s - ", strna(i
.id
));
491 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
493 printf("%"PRIu32
"\n", i
.uid
);
495 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
496 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
499 printf("\t Since: %s; %s\n", s2
, s1
);
501 printf("\t Since: %s\n", s2
);
504 _cleanup_free_
char *t
= NULL
;
506 printf("\t Leader: %"PRIu32
, i
.leader
);
508 get_process_comm(i
.leader
, &t
);
515 if (!isempty(i
.seat
)) {
516 printf("\t Seat: %s", i
.seat
);
519 printf("; vc%u", i
.vtnr
);
525 printf("\t TTY: %s\n", i
.tty
);
527 printf("\t Display: %s\n", i
.display
);
529 if (i
.remote_host
&& i
.remote_user
)
530 printf("\t Remote: %s@%s\n", i
.remote_user
, i
.remote_host
);
531 else if (i
.remote_host
)
532 printf("\t Remote: %s\n", i
.remote_host
);
533 else if (i
.remote_user
)
534 printf("\t Remote: user %s\n", i
.remote_user
);
536 printf("\t Remote: Yes\n");
539 printf("\t Service: %s", i
.service
);
542 printf("; type %s", i
.type
);
545 printf("; class %s", i
.class);
549 printf("\t Type: %s", i
.type
);
552 printf("; class %s", i
.class);
556 printf("\t Class: %s\n", i
.class);
558 if (!isempty(i
.desktop
))
559 printf("\t Desktop: %s\n", i
.desktop
);
562 printf("\t State: %s\n", i
.state
);
565 printf("\t Unit: %s\n", i
.scope
);
566 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Scope", i
.scope
, i
.leader
);
568 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
570 show_journal_by_unit(
575 i
.timestamp
.monotonic
,
578 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
579 SD_JOURNAL_LOCAL_ONLY
,
588 static int print_user_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
590 static const struct bus_properties_map map
[] = {
591 { "Name", "s", NULL
, offsetof(UserStatusInfo
, name
) },
592 { "Linger", "b", NULL
, offsetof(UserStatusInfo
, linger
) },
593 { "Slice", "s", NULL
, offsetof(UserStatusInfo
, slice
) },
594 { "State", "s", NULL
, offsetof(UserStatusInfo
, state
) },
595 { "UID", "u", NULL
, offsetof(UserStatusInfo
, uid
) },
596 { "Timestamp", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.realtime
) },
597 { "TimestampMonotonic", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.monotonic
) },
598 { "Display", "(so)", prop_map_first_of_struct
, offsetof(UserStatusInfo
, display
) },
599 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(UserStatusInfo
, sessions
) },
603 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
604 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
605 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
606 char since2
[FORMAT_TIMESTAMP_MAX
];
608 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
611 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
613 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
621 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
623 printf("%"PRIu32
"\n", i
.uid
);
625 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
626 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
629 printf("\t Since: %s; %s\n", s2
, s1
);
631 printf("\t Since: %s\n", s2
);
633 if (!isempty(i
.state
))
634 printf("\t State: %s\n", i
.state
);
636 if (!strv_isempty(i
.sessions
)) {
638 printf("\tSessions:");
640 STRV_FOREACH(l
, i
.sessions
)
642 streq_ptr(*l
, i
.display
) ? "*" : "",
648 printf("\t Linger: %s\n", yes_no(i
.linger
));
651 printf("\t Unit: %s\n", i
.slice
);
652 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Slice", i
.slice
, 0);
654 show_journal_by_unit(
659 i
.timestamp
.monotonic
,
662 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
663 SD_JOURNAL_LOCAL_ONLY
,
671 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
673 static const struct bus_properties_map map
[] = {
674 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
675 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
676 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
680 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
681 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
682 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
685 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, 0, &error
, &m
, &i
);
687 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
694 printf("%s\n", strna(i
.id
));
696 if (!strv_isempty(i
.sessions
)) {
698 printf("\tSessions:");
700 STRV_FOREACH(l
, i
.sessions
) {
701 if (streq_ptr(*l
, i
.active_session
))
710 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
719 printf("\t Devices:\n");
721 show_sysfs(i
.id
, "\t\t ", c
, get_output_flags());
727 static int print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
729 const char *contents
;
735 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
741 case SD_BUS_TYPE_STRUCT
:
743 if (contents
[0] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
746 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
748 return bus_log_parse_error(r
);
750 if (all
|| !isempty(s
))
751 bus_print_property_value(name
, expected_value
, value
, "%s", s
);
755 } else if (contents
[0] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
758 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
760 return bus_log_parse_error(r
);
762 if (!uid_is_valid(uid
)) {
763 log_error("Invalid user ID: " UID_FMT
, uid
);
767 bus_print_property_value(name
, expected_value
, value
, UID_FMT
, uid
);
772 case SD_BUS_TYPE_ARRAY
:
774 if (contents
[0] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
778 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
780 return bus_log_parse_error(r
);
785 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
786 printf("%s%s", space
? " " : "", s
);
794 return bus_log_parse_error(r
);
796 r
= sd_bus_message_exit_container(m
);
798 return bus_log_parse_error(r
);
808 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
820 r
= bus_print_all_properties(bus
, "org.freedesktop.login1", path
, print_property
, arg_property
, arg_value
, arg_all
, NULL
);
822 return bus_log_parse_error(r
);
827 static int show_session(int argc
, char *argv
[], void *userdata
) {
828 bool properties
, new_line
= false;
829 sd_bus
*bus
= userdata
;
831 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
832 _cleanup_free_
char *path
= NULL
;
837 properties
= !strstr(argv
[0], "status");
839 (void) pager_open(arg_no_pager
, false);
842 const char *session
, *p
= "/org/freedesktop/login1/session/self";
845 /* If no argument is specified inspect the manager itself */
846 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
848 /* And in the pretty case, show data of the calling session */
849 session
= getenv("XDG_SESSION_ID");
851 r
= get_session_path(bus
, session
, &error
, &path
);
853 return log_error_errno(r
, "Failed to get session path: %s", bus_error_message(&error
, r
));
858 return print_session_status_info(bus
, p
, &new_line
);
861 for (i
= 1; i
< argc
; i
++) {
862 r
= get_session_path(bus
, argv
[i
], &error
, &path
);
864 return log_error_errno(r
, "Failed to get session path: %s", bus_error_message(&error
, r
));
867 r
= show_properties(bus
, path
, &new_line
);
869 r
= print_session_status_info(bus
, path
, &new_line
);
878 static int show_user(int argc
, char *argv
[], void *userdata
) {
879 bool properties
, new_line
= false;
880 sd_bus
*bus
= userdata
;
886 properties
= !strstr(argv
[0], "status");
888 (void) pager_open(arg_no_pager
, false);
891 /* If not argument is specified inspect the manager
894 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
896 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
899 for (i
= 1; i
< argc
; i
++) {
900 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
901 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
902 const char *path
= NULL
;
905 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
907 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
909 r
= sd_bus_call_method(
911 "org.freedesktop.login1",
912 "/org/freedesktop/login1",
913 "org.freedesktop.login1.Manager",
916 "u", (uint32_t) uid
);
918 return log_error_errno(r
, "Failed to get user: %s", bus_error_message(&error
, r
));
920 r
= sd_bus_message_read(reply
, "o", &path
);
922 return bus_log_parse_error(r
);
925 r
= show_properties(bus
, path
, &new_line
);
927 r
= print_user_status_info(bus
, path
, &new_line
);
936 static int show_seat(int argc
, char *argv
[], void *userdata
) {
937 bool properties
, new_line
= false;
938 sd_bus
*bus
= userdata
;
944 properties
= !strstr(argv
[0], "status");
946 (void) pager_open(arg_no_pager
, false);
949 /* If not argument is specified inspect the manager
952 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
954 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/self", &new_line
);
957 for (i
= 1; i
< argc
; i
++) {
958 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
959 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
960 const char *path
= NULL
;
962 r
= sd_bus_call_method(
964 "org.freedesktop.login1",
965 "/org/freedesktop/login1",
966 "org.freedesktop.login1.Manager",
971 return log_error_errno(r
, "Failed to get seat: %s", bus_error_message(&error
, r
));
973 r
= sd_bus_message_read(reply
, "o", &path
);
975 return bus_log_parse_error(r
);
978 r
= show_properties(bus
, path
, &new_line
);
980 r
= print_seat_status_info(bus
, path
, &new_line
);
989 static int activate(int argc
, char *argv
[], void *userdata
) {
990 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
991 sd_bus
*bus
= userdata
;
998 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1001 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1002 * session name, in which case logind will try to guess our session. */
1004 short_argv
[0] = argv
[0];
1005 short_argv
[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1006 short_argv
[2] = NULL
;
1012 for (i
= 1; i
< argc
; i
++) {
1014 r
= sd_bus_call_method(
1016 "org.freedesktop.login1",
1017 "/org/freedesktop/login1",
1018 "org.freedesktop.login1.Manager",
1019 streq(argv
[0], "lock-session") ? "LockSession" :
1020 streq(argv
[0], "unlock-session") ? "UnlockSession" :
1021 streq(argv
[0], "terminate-session") ? "TerminateSession" :
1026 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, -r
));
1032 static int kill_session(int argc
, char *argv
[], void *userdata
) {
1033 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1034 sd_bus
*bus
= userdata
;
1040 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1043 arg_kill_who
= "all";
1045 for (i
= 1; i
< argc
; i
++) {
1047 r
= sd_bus_call_method(
1049 "org.freedesktop.login1",
1050 "/org/freedesktop/login1",
1051 "org.freedesktop.login1.Manager",
1054 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1056 return log_error_errno(r
, "Could not kill session: %s", bus_error_message(&error
, -r
));
1062 static int enable_linger(int argc
, char *argv
[], void *userdata
) {
1063 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1064 sd_bus
*bus
= userdata
;
1065 char* short_argv
[3];
1072 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1074 b
= streq(argv
[0], "enable-linger");
1077 /* No argument? Let's use an empty user name,
1078 * then logind will use our user. */
1080 short_argv
[0] = argv
[0];
1081 short_argv
[1] = (char*) "";
1082 short_argv
[2] = NULL
;
1087 for (i
= 1; i
< argc
; i
++) {
1090 if (isempty(argv
[i
]))
1093 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1095 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1098 r
= sd_bus_call_method(
1100 "org.freedesktop.login1",
1101 "/org/freedesktop/login1",
1102 "org.freedesktop.login1.Manager",
1105 "ubb", (uint32_t) uid
, b
, true);
1107 return log_error_errno(r
, "Could not enable linger: %s", bus_error_message(&error
, -r
));
1113 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1114 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1115 sd_bus
*bus
= userdata
;
1121 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1123 for (i
= 1; i
< argc
; i
++) {
1126 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1128 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1130 r
= sd_bus_call_method(
1132 "org.freedesktop.login1",
1133 "/org/freedesktop/login1",
1134 "org.freedesktop.login1.Manager",
1137 "u", (uint32_t) uid
);
1139 return log_error_errno(r
, "Could not terminate user: %s", bus_error_message(&error
, -r
));
1145 static int kill_user(int argc
, char *argv
[], void *userdata
) {
1146 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1147 sd_bus
*bus
= userdata
;
1153 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1156 arg_kill_who
= "all";
1158 for (i
= 1; i
< argc
; i
++) {
1161 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1163 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1165 r
= sd_bus_call_method(
1167 "org.freedesktop.login1",
1168 "/org/freedesktop/login1",
1169 "org.freedesktop.login1.Manager",
1172 "ui", (uint32_t) uid
, arg_signal
);
1174 return log_error_errno(r
, "Could not kill user: %s", bus_error_message(&error
, -r
));
1180 static int attach(int argc
, char *argv
[], void *userdata
) {
1181 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1182 sd_bus
*bus
= userdata
;
1188 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1190 for (i
= 2; i
< argc
; i
++) {
1192 r
= sd_bus_call_method(
1194 "org.freedesktop.login1",
1195 "/org/freedesktop/login1",
1196 "org.freedesktop.login1.Manager",
1199 "ssb", argv
[1], argv
[i
], true);
1202 return log_error_errno(r
, "Could not attach device: %s", bus_error_message(&error
, -r
));
1208 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1209 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1210 sd_bus
*bus
= userdata
;
1216 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1218 r
= sd_bus_call_method(
1220 "org.freedesktop.login1",
1221 "/org/freedesktop/login1",
1222 "org.freedesktop.login1.Manager",
1227 return log_error_errno(r
, "Could not flush devices: %s", bus_error_message(&error
, -r
));
1232 static int lock_sessions(int argc
, char *argv
[], void *userdata
) {
1233 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1234 sd_bus
*bus
= userdata
;
1240 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1242 r
= sd_bus_call_method(
1244 "org.freedesktop.login1",
1245 "/org/freedesktop/login1",
1246 "org.freedesktop.login1.Manager",
1247 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1251 return log_error_errno(r
, "Could not lock sessions: %s", bus_error_message(&error
, -r
));
1256 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1257 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1258 sd_bus
*bus
= userdata
;
1264 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1266 for (i
= 1; i
< argc
; i
++) {
1268 r
= sd_bus_call_method(
1270 "org.freedesktop.login1",
1271 "/org/freedesktop/login1",
1272 "org.freedesktop.login1.Manager",
1277 return log_error_errno(r
, "Could not terminate seat: %s", bus_error_message(&error
, -r
));
1283 static int help(int argc
, char *argv
[], void *userdata
) {
1284 _cleanup_free_
char *link
= NULL
;
1287 (void) pager_open(arg_no_pager
, false);
1289 r
= terminal_urlify_man("loginctl", "1", &link
);
1293 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1294 "Send control commands to or query the login manager.\n\n"
1295 " -h --help Show this help\n"
1296 " --version Show package version\n"
1297 " --no-pager Do not pipe output into a pager\n"
1298 " --no-legend Do not show the headers and footers\n"
1299 " --no-ask-password Don't prompt for password\n"
1300 " -H --host=[USER@]HOST Operate on remote host\n"
1301 " -M --machine=CONTAINER Operate on local container\n"
1302 " -p --property=NAME Show only properties by this name\n"
1303 " -a --all Show all properties, including empty ones\n"
1304 " --value When showing properties, only print the value\n"
1305 " -l --full Do not ellipsize output\n"
1306 " --kill-who=WHO Who to send signal to\n"
1307 " -s --signal=SIGNAL Which signal to send\n"
1308 " -n --lines=INTEGER Number of journal entries to show\n"
1309 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1310 " short-iso, short-iso-precise, short-full,\n"
1311 " short-monotonic, short-unix, verbose, export,\n"
1312 " json, json-pretty, json-sse, json-seq, cat,\n"
1314 "Session Commands:\n"
1315 " list-sessions List sessions\n"
1316 " session-status [ID...] Show session status\n"
1317 " show-session [ID...] Show properties of sessions or the manager\n"
1318 " activate [ID] Activate a session\n"
1319 " lock-session [ID...] Screen lock one or more sessions\n"
1320 " unlock-session [ID...] Screen unlock one or more sessions\n"
1321 " lock-sessions Screen lock all current sessions\n"
1322 " unlock-sessions Screen unlock all current sessions\n"
1323 " terminate-session ID... Terminate one or more sessions\n"
1324 " kill-session ID... Send signal to processes of a session\n\n"
1326 " list-users List users\n"
1327 " user-status [USER...] Show user status\n"
1328 " show-user [USER...] Show properties of users or the manager\n"
1329 " enable-linger [USER...] Enable linger state of one or more users\n"
1330 " disable-linger [USER...] Disable linger state of one or more users\n"
1331 " terminate-user USER... Terminate all sessions of one or more users\n"
1332 " kill-user USER... Send signal to processes of a user\n\n"
1334 " list-seats List seats\n"
1335 " seat-status [NAME...] Show seat status\n"
1336 " show-seat [NAME...] Show properties of seats or the manager\n"
1337 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1338 " flush-devices Flush all device associations\n"
1339 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1340 "\nSee the %s for details.\n"
1341 , program_invocation_short_name
1348 static int parse_argv(int argc
, char *argv
[]) {
1351 ARG_VERSION
= 0x100,
1356 ARG_NO_ASK_PASSWORD
,
1359 static const struct option options
[] = {
1360 { "help", no_argument
, NULL
, 'h' },
1361 { "version", no_argument
, NULL
, ARG_VERSION
},
1362 { "property", required_argument
, NULL
, 'p' },
1363 { "all", no_argument
, NULL
, 'a' },
1364 { "value", no_argument
, NULL
, ARG_VALUE
},
1365 { "full", no_argument
, NULL
, 'l' },
1366 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1367 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1368 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1369 { "signal", required_argument
, NULL
, 's' },
1370 { "host", required_argument
, NULL
, 'H' },
1371 { "machine", required_argument
, NULL
, 'M' },
1372 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1373 { "lines", required_argument
, NULL
, 'n' },
1374 { "output", required_argument
, NULL
, 'o' },
1383 while ((c
= getopt_long(argc
, argv
, "hp:als:H:M:n:o:", options
, NULL
)) >= 0)
1388 return help(0, NULL
, NULL
);
1394 r
= strv_extend(&arg_property
, optarg
);
1398 /* If the user asked for a particular
1399 * property, show it to him, even if it is
1418 if (safe_atou(optarg
, &arg_lines
) < 0) {
1419 log_error("Failed to parse lines '%s'", optarg
);
1425 if (streq(optarg
, "help")) {
1426 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
1430 arg_output
= output_mode_from_string(optarg
);
1431 if (arg_output
< 0) {
1432 log_error("Unknown output '%s'.", optarg
);
1438 arg_no_pager
= true;
1445 case ARG_NO_ASK_PASSWORD
:
1446 arg_ask_password
= false;
1450 arg_kill_who
= optarg
;
1454 if (streq(optarg
, "help")) {
1455 DUMP_STRING_TABLE(signal
, int, _NSIG
);
1459 arg_signal
= signal_from_string(optarg
);
1460 if (arg_signal
< 0) {
1461 log_error("Failed to parse signal string %s.", optarg
);
1467 arg_transport
= BUS_TRANSPORT_REMOTE
;
1472 arg_transport
= BUS_TRANSPORT_MACHINE
;
1480 assert_not_reached("Unhandled option");
1486 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1488 static const Verb verbs
[] = {
1489 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1490 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1491 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1492 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1493 { "activate", VERB_ANY
, 2, 0, activate
},
1494 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1495 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1496 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1497 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1498 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1499 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1500 { "list-users", VERB_ANY
, 1, 0, list_users
},
1501 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1502 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1503 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1504 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1505 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1506 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1507 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1508 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1509 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1510 { "attach", 3, VERB_ANY
, 0, attach
},
1511 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1512 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1516 return dispatch_verb(argc
, argv
, verbs
, bus
);
1519 int main(int argc
, char *argv
[]) {
1523 setlocale(LC_ALL
, "");
1524 log_parse_environment();
1527 /* The journal merging logic potentially needs a lot of fds. */
1528 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1532 r
= parse_argv(argc
, argv
);
1536 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1538 log_error_errno(r
, "Failed to create bus connection: %m");
1542 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1544 r
= loginctl_main(argc
, argv
, bus
);
1547 /* make sure we terminate the bus connection first, and then close the
1548 * pager, see issue #3543 for the details. */
1549 sd_bus_flush_close_unref(bus
);
1551 polkit_agent_close();
1553 strv_free(arg_property
);
1555 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;