1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
16 #include "alloc-util.h"
17 #include "bus-error.h"
18 #include "bus-unit-util.h"
20 #include "cgroup-show.h"
21 #include "cgroup-util.h"
22 #include "format-table.h"
24 #include "logs-show.h"
27 #include "parse-util.h"
28 #include "process-util.h"
30 #include "signal-util.h"
31 #include "spawn-polkit-agent.h"
33 #include "sysfs-show.h"
34 #include "terminal-util.h"
35 #include "unit-name.h"
36 #include "user-util.h"
40 static char **arg_property
= NULL
;
41 static bool arg_all
= false;
42 static bool arg_value
= false;
43 static bool arg_full
= false;
44 static bool arg_no_pager
= false;
45 static bool arg_legend
= true;
46 static const char *arg_kill_who
= NULL
;
47 static int arg_signal
= SIGTERM
;
48 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
49 static char *arg_host
= NULL
;
50 static bool arg_ask_password
= true;
51 static unsigned arg_lines
= 10;
52 static OutputMode arg_output
= OUTPUT_SHORT
;
54 static OutputFlags
get_output_flags(void) {
57 arg_all
* OUTPUT_SHOW_ALL
|
58 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
59 colors_enabled() * OUTPUT_COLOR
;
62 static int get_session_path(sd_bus
*bus
, const char *session_id
, sd_bus_error
*error
, char **path
) {
63 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
67 r
= sd_bus_call_method(
69 "org.freedesktop.login1",
70 "/org/freedesktop/login1",
71 "org.freedesktop.login1.Manager",
78 r
= sd_bus_message_read(reply
, "o", &ans
);
90 static int show_table(Table
*table
, const char *word
) {
96 if (table_get_rows(table
) > 1) {
97 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
99 return log_error_errno(r
, "Failed to sort table: %m");
101 table_set_header(table
, arg_legend
);
103 r
= table_print(table
, NULL
);
105 return log_error_errno(r
, "Failed to show table: %m");
109 if (table_get_rows(table
) > 1)
110 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
112 printf("No %s.\n", word
);
118 static int list_sessions(int argc
, char *argv
[], void *userdata
) {
119 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
120 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
121 _cleanup_(table_unrefp
) Table
*table
= NULL
;
122 sd_bus
*bus
= userdata
;
128 (void) pager_open(arg_no_pager
, false);
130 r
= sd_bus_call_method(
132 "org.freedesktop.login1",
133 "/org/freedesktop/login1",
134 "org.freedesktop.login1.Manager",
139 return log_error_errno(r
, "Failed to list sessions: %s", bus_error_message(&error
, r
));
141 r
= sd_bus_message_enter_container(reply
, 'a', "(susso)");
143 return bus_log_parse_error(r
);
145 table
= table_new("SESSION", "UID", "USER", "SEAT", "TTY");
149 /* Right-align the first two fields (since they are numeric) */
150 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
151 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100);
154 _cleanup_(sd_bus_error_free
) sd_bus_error error_tty
= SD_BUS_ERROR_NULL
;
155 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply_tty
= NULL
;
156 const char *id
, *user
, *seat
, *object
, *tty
= NULL
;
159 r
= sd_bus_message_read(reply
, "(susso)", &id
, &uid
, &user
, &seat
, &object
);
161 return bus_log_parse_error(r
);
165 r
= sd_bus_get_property(
167 "org.freedesktop.login1",
169 "org.freedesktop.login1.Session",
175 log_warning_errno(r
, "Failed to get TTY for session %s: %s", id
, bus_error_message(&error_tty
, r
));
177 r
= sd_bus_message_read(reply_tty
, "s", &tty
);
179 return bus_log_parse_error(r
);
182 r
= table_add_many(table
,
187 TABLE_STRING
, strna(tty
));
189 return log_error_errno(r
, "Failed to add row to table: %m");
192 r
= sd_bus_message_exit_container(reply
);
194 return bus_log_parse_error(r
);
196 return show_table(table
, "sessions");
199 static int list_users(int argc
, char *argv
[], void *userdata
) {
200 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
201 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
202 _cleanup_(table_unrefp
) Table
*table
= NULL
;
203 sd_bus
*bus
= userdata
;
209 (void) pager_open(arg_no_pager
, false);
211 r
= sd_bus_call_method(
213 "org.freedesktop.login1",
214 "/org/freedesktop/login1",
215 "org.freedesktop.login1.Manager",
220 return log_error_errno(r
, "Failed to list users: %s", bus_error_message(&error
, r
));
222 r
= sd_bus_message_enter_container(reply
, 'a', "(uso)");
224 return bus_log_parse_error(r
);
226 table
= table_new("UID", "USER");
230 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
233 const char *user
, *object
;
236 r
= sd_bus_message_read(reply
, "(uso)", &uid
, &user
, &object
);
238 return bus_log_parse_error(r
);
242 r
= table_add_many(table
,
246 return log_error_errno(r
, "Failed to add row to table: %m");
249 r
= sd_bus_message_exit_container(reply
);
251 return bus_log_parse_error(r
);
253 return show_table(table
, "users");
256 static int list_seats(int argc
, char *argv
[], void *userdata
) {
257 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
258 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
259 _cleanup_(table_unrefp
) Table
*table
= NULL
;
260 sd_bus
*bus
= userdata
;
266 (void) pager_open(arg_no_pager
, false);
268 r
= sd_bus_call_method(
270 "org.freedesktop.login1",
271 "/org/freedesktop/login1",
272 "org.freedesktop.login1.Manager",
277 return log_error_errno(r
, "Failed to list seats: %s", bus_error_message(&error
, r
));
279 r
= sd_bus_message_enter_container(reply
, 'a', "(so)");
281 return bus_log_parse_error(r
);
283 table
= table_new("SEAT");
288 const char *seat
, *object
;
290 r
= sd_bus_message_read(reply
, "(so)", &seat
, &object
);
292 return bus_log_parse_error(r
);
296 r
= table_add_cell(table
, NULL
, TABLE_STRING
, seat
);
298 return log_error_errno(r
, "Failed to add row to table: %m");
301 r
= sd_bus_message_exit_container(reply
);
303 return bus_log_parse_error(r
);
305 return show_table(table
, "seats");
308 static int show_unit_cgroup(sd_bus
*bus
, const char *interface
, const char *unit
, pid_t leader
) {
309 _cleanup_free_
char *cgroup
= NULL
;
310 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
317 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
330 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
333 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
336 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
338 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
341 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
343 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
348 typedef struct SessionStatusInfo
{
352 struct dual_timestamp timestamp
;
358 const char *remote_host
;
359 const char *remote_user
;
369 typedef struct UserStatusInfo
{
373 struct dual_timestamp timestamp
;
380 typedef struct SeatStatusInfo
{
382 const char *active_session
;
386 static void user_status_info_clear(UserStatusInfo
*info
) {
388 strv_free(info
->sessions
);
393 static void seat_status_info_clear(SeatStatusInfo
*info
) {
395 strv_free(info
->sessions
);
400 static int prop_map_first_of_struct(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
401 const char *contents
;
404 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
408 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, contents
);
412 r
= sd_bus_message_read_basic(m
, contents
[0], userdata
);
416 r
= sd_bus_message_skip(m
, contents
+1);
420 r
= sd_bus_message_exit_container(m
);
427 static int prop_map_sessions_strv(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
434 r
= sd_bus_message_enter_container(m
, 'a', "(so)");
438 while ((r
= sd_bus_message_read(m
, "(so)", &name
, NULL
)) > 0) {
439 r
= strv_extend(userdata
, name
);
446 return sd_bus_message_exit_container(m
);
449 static int print_session_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
451 static const struct bus_properties_map map
[] = {
452 { "Id", "s", NULL
, offsetof(SessionStatusInfo
, id
) },
453 { "Name", "s", NULL
, offsetof(SessionStatusInfo
, name
) },
454 { "TTY", "s", NULL
, offsetof(SessionStatusInfo
, tty
) },
455 { "Display", "s", NULL
, offsetof(SessionStatusInfo
, display
) },
456 { "RemoteHost", "s", NULL
, offsetof(SessionStatusInfo
, remote_host
) },
457 { "RemoteUser", "s", NULL
, offsetof(SessionStatusInfo
, remote_user
) },
458 { "Service", "s", NULL
, offsetof(SessionStatusInfo
, service
) },
459 { "Desktop", "s", NULL
, offsetof(SessionStatusInfo
, desktop
) },
460 { "Type", "s", NULL
, offsetof(SessionStatusInfo
, type
) },
461 { "Class", "s", NULL
, offsetof(SessionStatusInfo
, class) },
462 { "Scope", "s", NULL
, offsetof(SessionStatusInfo
, scope
) },
463 { "State", "s", NULL
, offsetof(SessionStatusInfo
, state
) },
464 { "VTNr", "u", NULL
, offsetof(SessionStatusInfo
, vtnr
) },
465 { "Leader", "u", NULL
, offsetof(SessionStatusInfo
, leader
) },
466 { "Remote", "b", NULL
, offsetof(SessionStatusInfo
, remote
) },
467 { "Timestamp", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.realtime
) },
468 { "TimestampMonotonic", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.monotonic
) },
469 { "User", "(uo)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, uid
) },
470 { "Seat", "(so)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, seat
) },
474 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
475 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
476 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
477 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
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
], *s1
;
608 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
609 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
612 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
614 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
622 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
624 printf("%"PRIu32
"\n", i
.uid
);
626 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
627 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
630 printf("\t Since: %s; %s\n", s2
, s1
);
632 printf("\t Since: %s\n", s2
);
634 if (!isempty(i
.state
))
635 printf("\t State: %s\n", i
.state
);
637 if (!strv_isempty(i
.sessions
)) {
639 printf("\tSessions:");
641 STRV_FOREACH(l
, i
.sessions
)
643 streq_ptr(*l
, i
.display
) ? "*" : "",
649 printf("\t Linger: %s\n", yes_no(i
.linger
));
652 printf("\t Unit: %s\n", i
.slice
);
653 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Slice", i
.slice
, 0);
655 show_journal_by_unit(
660 i
.timestamp
.monotonic
,
663 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
664 SD_JOURNAL_LOCAL_ONLY
,
672 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
674 static const struct bus_properties_map map
[] = {
675 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
676 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
677 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
681 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
682 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
683 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
686 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, 0, &error
, &m
, &i
);
688 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
695 printf("%s\n", strna(i
.id
));
697 if (!strv_isempty(i
.sessions
)) {
699 printf("\tSessions:");
701 STRV_FOREACH(l
, i
.sessions
) {
702 if (streq_ptr(*l
, i
.active_session
))
711 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
720 printf("\t Devices:\n");
722 show_sysfs(i
.id
, "\t\t ", c
, get_output_flags());
728 #define property(name, fmt, ...) \
731 printf(fmt "\n", __VA_ARGS__); \
733 printf("%s=" fmt "\n", name, __VA_ARGS__); \
736 static int print_property(const char *name
, sd_bus_message
*m
, bool value
, bool all
) {
738 const char *contents
;
744 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
750 case SD_BUS_TYPE_STRUCT
:
752 if (contents
[0] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
755 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
757 return bus_log_parse_error(r
);
759 if (all
|| !isempty(s
))
760 property(name
, "%s", s
);
764 } else if (contents
[0] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
767 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
769 return bus_log_parse_error(r
);
771 if (!uid_is_valid(uid
)) {
772 log_error("Invalid user ID: " UID_FMT
, uid
);
776 property(name
, UID_FMT
, uid
);
781 case SD_BUS_TYPE_ARRAY
:
783 if (contents
[0] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
787 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
789 return bus_log_parse_error(r
);
794 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
795 printf("%s%s", space
? " " : "", s
);
803 return bus_log_parse_error(r
);
805 r
= sd_bus_message_exit_container(m
);
807 return bus_log_parse_error(r
);
817 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
829 r
= bus_print_all_properties(bus
, "org.freedesktop.login1", path
, print_property
, arg_property
, arg_value
, arg_all
, NULL
);
831 return bus_log_parse_error(r
);
836 static int show_session(int argc
, char *argv
[], void *userdata
) {
837 bool properties
, new_line
= false;
838 sd_bus
*bus
= userdata
;
840 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
841 _cleanup_free_
char *path
= NULL
;
846 properties
= !strstr(argv
[0], "status");
848 (void) pager_open(arg_no_pager
, false);
851 const char *session
, *p
= "/org/freedesktop/login1/session/self";
854 /* If no argument is specified inspect the manager itself */
855 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
857 /* And in the pretty case, show data of the calling session */
858 session
= getenv("XDG_SESSION_ID");
860 r
= get_session_path(bus
, session
, &error
, &path
);
862 log_error("Failed to get session path: %s", bus_error_message(&error
, r
));
868 return print_session_status_info(bus
, p
, &new_line
);
871 for (i
= 1; i
< argc
; i
++) {
872 r
= get_session_path(bus
, argv
[i
], &error
, &path
);
874 log_error("Failed to get session path: %s", bus_error_message(&error
, r
));
879 r
= show_properties(bus
, path
, &new_line
);
881 r
= print_session_status_info(bus
, path
, &new_line
);
890 static int show_user(int argc
, char *argv
[], void *userdata
) {
891 bool properties
, new_line
= false;
892 sd_bus
*bus
= userdata
;
898 properties
= !strstr(argv
[0], "status");
900 (void) pager_open(arg_no_pager
, false);
903 /* If not argument is specified inspect the manager
906 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
908 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
911 for (i
= 1; i
< argc
; i
++) {
912 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
913 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
914 const char *path
= NULL
;
917 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
919 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
921 r
= sd_bus_call_method(
923 "org.freedesktop.login1",
924 "/org/freedesktop/login1",
925 "org.freedesktop.login1.Manager",
928 "u", (uint32_t) uid
);
930 log_error("Failed to get user: %s", bus_error_message(&error
, r
));
934 r
= sd_bus_message_read(reply
, "o", &path
);
936 return bus_log_parse_error(r
);
939 r
= show_properties(bus
, path
, &new_line
);
941 r
= print_user_status_info(bus
, path
, &new_line
);
950 static int show_seat(int argc
, char *argv
[], void *userdata
) {
951 bool properties
, new_line
= false;
952 sd_bus
*bus
= userdata
;
958 properties
= !strstr(argv
[0], "status");
960 (void) pager_open(arg_no_pager
, false);
963 /* If not argument is specified inspect the manager
966 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
968 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/self", &new_line
);
971 for (i
= 1; i
< argc
; i
++) {
972 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
973 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
974 const char *path
= NULL
;
976 r
= sd_bus_call_method(
978 "org.freedesktop.login1",
979 "/org/freedesktop/login1",
980 "org.freedesktop.login1.Manager",
985 log_error("Failed to get seat: %s", bus_error_message(&error
, r
));
989 r
= sd_bus_message_read(reply
, "o", &path
);
991 return bus_log_parse_error(r
);
994 r
= show_properties(bus
, path
, &new_line
);
996 r
= print_seat_status_info(bus
, path
, &new_line
);
1005 static int activate(int argc
, char *argv
[], void *userdata
) {
1006 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1007 sd_bus
*bus
= userdata
;
1008 char *short_argv
[3];
1014 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1017 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1018 * session name, in which case logind will try to guess our session. */
1020 short_argv
[0] = argv
[0];
1021 short_argv
[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1022 short_argv
[2] = NULL
;
1028 for (i
= 1; i
< argc
; i
++) {
1030 r
= sd_bus_call_method(
1032 "org.freedesktop.login1",
1033 "/org/freedesktop/login1",
1034 "org.freedesktop.login1.Manager",
1035 streq(argv
[0], "lock-session") ? "LockSession" :
1036 streq(argv
[0], "unlock-session") ? "UnlockSession" :
1037 streq(argv
[0], "terminate-session") ? "TerminateSession" :
1042 log_error("Failed to issue method call: %s", bus_error_message(&error
, -r
));
1050 static int kill_session(int argc
, char *argv
[], void *userdata
) {
1051 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1052 sd_bus
*bus
= userdata
;
1058 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1061 arg_kill_who
= "all";
1063 for (i
= 1; i
< argc
; i
++) {
1065 r
= sd_bus_call_method(
1067 "org.freedesktop.login1",
1068 "/org/freedesktop/login1",
1069 "org.freedesktop.login1.Manager",
1072 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1074 log_error("Could not kill session: %s", bus_error_message(&error
, -r
));
1082 static int enable_linger(int argc
, char *argv
[], void *userdata
) {
1083 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1084 sd_bus
*bus
= userdata
;
1085 char* short_argv
[3];
1092 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1094 b
= streq(argv
[0], "enable-linger");
1097 /* No argument? Let's use an empty user name,
1098 * then logind will use our user. */
1100 short_argv
[0] = argv
[0];
1101 short_argv
[1] = (char*) "";
1102 short_argv
[2] = NULL
;
1107 for (i
= 1; i
< argc
; i
++) {
1110 if (isempty(argv
[i
]))
1113 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1115 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1118 r
= sd_bus_call_method(
1120 "org.freedesktop.login1",
1121 "/org/freedesktop/login1",
1122 "org.freedesktop.login1.Manager",
1125 "ubb", (uint32_t) uid
, b
, true);
1127 log_error("Could not enable linger: %s", bus_error_message(&error
, -r
));
1135 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1136 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1137 sd_bus
*bus
= userdata
;
1143 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1145 for (i
= 1; i
< argc
; i
++) {
1148 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1150 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1152 r
= sd_bus_call_method(
1154 "org.freedesktop.login1",
1155 "/org/freedesktop/login1",
1156 "org.freedesktop.login1.Manager",
1159 "u", (uint32_t) uid
);
1161 log_error("Could not terminate user: %s", bus_error_message(&error
, -r
));
1169 static int kill_user(int argc
, char *argv
[], void *userdata
) {
1170 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1171 sd_bus
*bus
= userdata
;
1177 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1180 arg_kill_who
= "all";
1182 for (i
= 1; i
< argc
; i
++) {
1185 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1187 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1189 r
= sd_bus_call_method(
1191 "org.freedesktop.login1",
1192 "/org/freedesktop/login1",
1193 "org.freedesktop.login1.Manager",
1196 "ui", (uint32_t) uid
, arg_signal
);
1198 log_error("Could not kill user: %s", bus_error_message(&error
, -r
));
1206 static int attach(int argc
, char *argv
[], void *userdata
) {
1207 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1208 sd_bus
*bus
= userdata
;
1214 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1216 for (i
= 2; i
< argc
; i
++) {
1218 r
= sd_bus_call_method(
1220 "org.freedesktop.login1",
1221 "/org/freedesktop/login1",
1222 "org.freedesktop.login1.Manager",
1225 "ssb", argv
[1], argv
[i
], true);
1228 log_error("Could not attach device: %s", bus_error_message(&error
, -r
));
1236 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1237 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1238 sd_bus
*bus
= userdata
;
1244 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1246 r
= sd_bus_call_method(
1248 "org.freedesktop.login1",
1249 "/org/freedesktop/login1",
1250 "org.freedesktop.login1.Manager",
1255 log_error("Could not flush devices: %s", bus_error_message(&error
, -r
));
1260 static int lock_sessions(int argc
, char *argv
[], void *userdata
) {
1261 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1262 sd_bus
*bus
= userdata
;
1268 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1270 r
= sd_bus_call_method(
1272 "org.freedesktop.login1",
1273 "/org/freedesktop/login1",
1274 "org.freedesktop.login1.Manager",
1275 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1279 log_error("Could not lock sessions: %s", bus_error_message(&error
, -r
));
1284 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1285 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1286 sd_bus
*bus
= userdata
;
1292 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1294 for (i
= 1; i
< argc
; i
++) {
1296 r
= sd_bus_call_method(
1298 "org.freedesktop.login1",
1299 "/org/freedesktop/login1",
1300 "org.freedesktop.login1.Manager",
1305 log_error("Could not terminate seat: %s", bus_error_message(&error
, -r
));
1313 static int help(int argc
, char *argv
[], void *userdata
) {
1315 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1316 "Send control commands to or query the login manager.\n\n"
1317 " -h --help Show this help\n"
1318 " --version Show package version\n"
1319 " --no-pager Do not pipe output into a pager\n"
1320 " --no-legend Do not show the headers and footers\n"
1321 " --no-ask-password Don't prompt for password\n"
1322 " -H --host=[USER@]HOST Operate on remote host\n"
1323 " -M --machine=CONTAINER Operate on local container\n"
1324 " -p --property=NAME Show only properties by this name\n"
1325 " -a --all Show all properties, including empty ones\n"
1326 " --value When showing properties, only print the value\n"
1327 " -l --full Do not ellipsize output\n"
1328 " --kill-who=WHO Who to send signal to\n"
1329 " -s --signal=SIGNAL Which signal to send\n"
1330 " -n --lines=INTEGER Number of journal entries to show\n"
1331 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1332 " short-iso, short-iso-precise, short-full,\n"
1333 " short-monotonic, short-unix, verbose, export,\n"
1334 " json, json-pretty, json-sse, cat)\n"
1335 "Session Commands:\n"
1336 " list-sessions List sessions\n"
1337 " session-status [ID...] Show session status\n"
1338 " show-session [ID...] Show properties of sessions or the manager\n"
1339 " activate [ID] Activate a session\n"
1340 " lock-session [ID...] Screen lock one or more sessions\n"
1341 " unlock-session [ID...] Screen unlock one or more sessions\n"
1342 " lock-sessions Screen lock all current sessions\n"
1343 " unlock-sessions Screen unlock all current sessions\n"
1344 " terminate-session ID... Terminate one or more sessions\n"
1345 " kill-session ID... Send signal to processes of a session\n\n"
1347 " list-users List users\n"
1348 " user-status [USER...] Show user status\n"
1349 " show-user [USER...] Show properties of users or the manager\n"
1350 " enable-linger [USER...] Enable linger state of one or more users\n"
1351 " disable-linger [USER...] Disable linger state of one or more users\n"
1352 " terminate-user USER... Terminate all sessions of one or more users\n"
1353 " kill-user USER... Send signal to processes of a user\n\n"
1355 " list-seats List seats\n"
1356 " seat-status [NAME...] Show seat status\n"
1357 " show-seat [NAME...] Show properties of seats or the manager\n"
1358 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1359 " flush-devices Flush all device associations\n"
1360 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1361 , program_invocation_short_name
);
1366 static int parse_argv(int argc
, char *argv
[]) {
1369 ARG_VERSION
= 0x100,
1374 ARG_NO_ASK_PASSWORD
,
1377 static const struct option options
[] = {
1378 { "help", no_argument
, NULL
, 'h' },
1379 { "version", no_argument
, NULL
, ARG_VERSION
},
1380 { "property", required_argument
, NULL
, 'p' },
1381 { "all", no_argument
, NULL
, 'a' },
1382 { "value", no_argument
, NULL
, ARG_VALUE
},
1383 { "full", no_argument
, NULL
, 'l' },
1384 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1385 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1386 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1387 { "signal", required_argument
, NULL
, 's' },
1388 { "host", required_argument
, NULL
, 'H' },
1389 { "machine", required_argument
, NULL
, 'M' },
1390 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1391 { "lines", required_argument
, NULL
, 'n' },
1392 { "output", required_argument
, NULL
, 'o' },
1401 while ((c
= getopt_long(argc
, argv
, "hp:als:H:M:n:o:", options
, NULL
)) >= 0)
1406 help(0, NULL
, NULL
);
1413 r
= strv_extend(&arg_property
, optarg
);
1417 /* If the user asked for a particular
1418 * property, show it to him, even if it is
1437 if (safe_atou(optarg
, &arg_lines
) < 0) {
1438 log_error("Failed to parse lines '%s'", optarg
);
1444 arg_output
= output_mode_from_string(optarg
);
1445 if (arg_output
< 0) {
1446 log_error("Unknown output '%s'.", optarg
);
1452 arg_no_pager
= true;
1459 case ARG_NO_ASK_PASSWORD
:
1460 arg_ask_password
= false;
1464 arg_kill_who
= optarg
;
1468 arg_signal
= signal_from_string_try_harder(optarg
);
1469 if (arg_signal
< 0) {
1470 log_error("Failed to parse signal string %s.", optarg
);
1476 arg_transport
= BUS_TRANSPORT_REMOTE
;
1481 arg_transport
= BUS_TRANSPORT_MACHINE
;
1489 assert_not_reached("Unhandled option");
1495 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1497 static const Verb verbs
[] = {
1498 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1499 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1500 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1501 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1502 { "activate", VERB_ANY
, 2, 0, activate
},
1503 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1504 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1505 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1506 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1507 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1508 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1509 { "list-users", VERB_ANY
, 1, 0, list_users
},
1510 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1511 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1512 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1513 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1514 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1515 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1516 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1517 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1518 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1519 { "attach", 3, VERB_ANY
, 0, attach
},
1520 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1521 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1525 return dispatch_verb(argc
, argv
, verbs
, bus
);
1528 int main(int argc
, char *argv
[]) {
1532 setlocale(LC_ALL
, "");
1533 log_parse_environment();
1537 r
= parse_argv(argc
, argv
);
1541 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1543 log_error_errno(r
, "Failed to create bus connection: %m");
1547 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1549 r
= loginctl_main(argc
, argv
, bus
);
1552 /* make sure we terminate the bus connection first, and then close the
1553 * pager, see issue #3543 for the details. */
1554 sd_bus_flush_close_unref(bus
);
1556 polkit_agent_close();
1558 strv_free(arg_property
);
1560 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;