1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
11 #include "bus-error.h"
12 #include "bus-locator.h"
13 #include "bus-map-properties.h"
14 #include "bus-print-properties.h"
15 #include "bus-unit-procs.h"
16 #include "cgroup-show.h"
17 #include "cgroup-util.h"
18 #include "format-table.h"
20 #include "logs-show.h"
22 #include "main-func.h"
23 #include "memory-util.h"
25 #include "parse-argument.h"
26 #include "parse-util.h"
27 #include "pretty-print.h"
28 #include "process-util.h"
29 #include "rlimit-util.h"
31 #include "signal-util.h"
32 #include "spawn-polkit-agent.h"
33 #include "string-table.h"
35 #include "sysfs-show.h"
36 #include "terminal-util.h"
37 #include "unit-name.h"
38 #include "user-util.h"
41 static char **arg_property
= NULL
;
42 static BusPrintPropertyFlags arg_print_flags
= 0;
43 static bool arg_full
= false;
44 static PagerFlags arg_pager_flags
= 0;
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_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
56 static OutputFlags
get_output_flags(void) {
59 FLAGS_SET(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
) * OUTPUT_SHOW_ALL
|
60 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
61 colors_enabled() * OUTPUT_COLOR
;
64 static int get_session_path(sd_bus
*bus
, const char *session_id
, sd_bus_error
*error
, char **path
) {
65 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
69 r
= bus_call_method(bus
, bus_login_mgr
, "GetSession", error
, &reply
, "s", session_id
);
73 r
= sd_bus_message_read(reply
, "o", &ans
);
85 static int show_table(Table
*table
, const char *word
) {
91 if (table_get_rows(table
) > 1 || OUTPUT_MODE_IS_JSON(arg_output
)) {
92 r
= table_set_sort(table
, (size_t) 0);
94 return table_log_sort_error(r
);
96 table_set_header(table
, arg_legend
);
98 if (OUTPUT_MODE_IS_JSON(arg_output
))
99 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | JSON_FORMAT_COLOR_AUTO
);
101 r
= table_print(table
, NULL
);
103 return table_log_print_error(r
);
107 if (table_get_rows(table
) > 1)
108 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
110 printf("No %s.\n", word
);
116 static int list_sessions(int argc
, char *argv
[], void *userdata
) {
117 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
118 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
119 _cleanup_(table_unrefp
) Table
*table
= NULL
;
120 sd_bus
*bus
= userdata
;
126 pager_open(arg_pager_flags
);
128 r
= bus_call_method(bus
, bus_login_mgr
, "ListSessions", &error
, &reply
, NULL
);
130 return log_error_errno(r
, "Failed to list sessions: %s", bus_error_message(&error
, r
));
132 r
= sd_bus_message_enter_container(reply
, 'a', "(susso)");
134 return bus_log_parse_error(r
);
136 table
= table_new("session", "uid", "user", "seat", "tty");
140 /* Right-align the first two fields (since they are numeric) */
141 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
142 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100);
145 _cleanup_(sd_bus_error_free
) sd_bus_error error_tty
= SD_BUS_ERROR_NULL
;
146 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply_tty
= NULL
;
147 const char *id
, *user
, *seat
, *object
, *tty
= NULL
;
150 r
= sd_bus_message_read(reply
, "(susso)", &id
, &uid
, &user
, &seat
, &object
);
152 return bus_log_parse_error(r
);
156 r
= sd_bus_get_property(
158 "org.freedesktop.login1",
160 "org.freedesktop.login1.Session",
166 log_warning_errno(r
, "Failed to get TTY for session %s: %s", id
, bus_error_message(&error_tty
, r
));
168 r
= sd_bus_message_read(reply_tty
, "s", &tty
);
170 return bus_log_parse_error(r
);
173 r
= table_add_many(table
,
175 TABLE_UID
, (uid_t
) uid
,
178 TABLE_STRING
, strna(tty
));
180 return table_log_add_error(r
);
183 r
= sd_bus_message_exit_container(reply
);
185 return bus_log_parse_error(r
);
187 return show_table(table
, "sessions");
190 static int list_users(int argc
, char *argv
[], void *userdata
) {
191 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
192 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
193 _cleanup_(table_unrefp
) Table
*table
= NULL
;
194 sd_bus
*bus
= userdata
;
200 pager_open(arg_pager_flags
);
202 r
= bus_call_method(bus
, bus_login_mgr
, "ListUsers", &error
, &reply
, NULL
);
204 return log_error_errno(r
, "Failed to list users: %s", bus_error_message(&error
, r
));
206 r
= sd_bus_message_enter_container(reply
, 'a', "(uso)");
208 return bus_log_parse_error(r
);
210 table
= table_new("uid", "user");
214 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
220 r
= sd_bus_message_read(reply
, "(uso)", &uid
, &user
, NULL
);
222 return bus_log_parse_error(r
);
226 r
= table_add_many(table
,
227 TABLE_UID
, (uid_t
) uid
,
230 return table_log_add_error(r
);
233 r
= sd_bus_message_exit_container(reply
);
235 return bus_log_parse_error(r
);
237 return show_table(table
, "users");
240 static int list_seats(int argc
, char *argv
[], void *userdata
) {
241 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
242 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
243 _cleanup_(table_unrefp
) Table
*table
= NULL
;
244 sd_bus
*bus
= userdata
;
250 pager_open(arg_pager_flags
);
252 r
= bus_call_method(bus
, bus_login_mgr
, "ListSeats", &error
, &reply
, NULL
);
254 return log_error_errno(r
, "Failed to list seats: %s", bus_error_message(&error
, r
));
256 r
= sd_bus_message_enter_container(reply
, 'a', "(so)");
258 return bus_log_parse_error(r
);
260 table
= table_new("seat");
267 r
= sd_bus_message_read(reply
, "(so)", &seat
, NULL
);
269 return bus_log_parse_error(r
);
273 r
= table_add_cell(table
, NULL
, TABLE_STRING
, seat
);
275 return table_log_add_error(r
);
278 r
= sd_bus_message_exit_container(reply
);
280 return bus_log_parse_error(r
);
282 return show_table(table
, "seats");
285 static int show_unit_cgroup(sd_bus
*bus
, const char *interface
, const char *unit
, pid_t leader
) {
286 _cleanup_free_
char *cgroup
= NULL
;
287 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
294 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
307 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
310 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
313 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
315 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
318 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
320 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
325 typedef struct SessionStatusInfo
{
329 struct dual_timestamp timestamp
;
335 const char *remote_host
;
336 const char *remote_user
;
346 typedef struct UserStatusInfo
{
350 struct dual_timestamp timestamp
;
357 typedef struct SeatStatusInfo
{
359 const char *active_session
;
363 static void user_status_info_clear(UserStatusInfo
*info
) {
365 strv_free(info
->sessions
);
370 static void seat_status_info_clear(SeatStatusInfo
*info
) {
372 strv_free(info
->sessions
);
377 static int prop_map_first_of_struct(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
378 const char *contents
;
381 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
385 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, contents
);
389 r
= sd_bus_message_read_basic(m
, contents
[0], userdata
);
393 r
= sd_bus_message_skip(m
, contents
+1);
397 r
= sd_bus_message_exit_container(m
);
404 static int prop_map_sessions_strv(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
411 r
= sd_bus_message_enter_container(m
, 'a', "(so)");
415 while ((r
= sd_bus_message_read(m
, "(so)", &name
, NULL
)) > 0) {
416 r
= strv_extend(userdata
, name
);
423 return sd_bus_message_exit_container(m
);
426 static int print_session_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
428 static const struct bus_properties_map map
[] = {
429 { "Id", "s", NULL
, offsetof(SessionStatusInfo
, id
) },
430 { "Name", "s", NULL
, offsetof(SessionStatusInfo
, name
) },
431 { "TTY", "s", NULL
, offsetof(SessionStatusInfo
, tty
) },
432 { "Display", "s", NULL
, offsetof(SessionStatusInfo
, display
) },
433 { "RemoteHost", "s", NULL
, offsetof(SessionStatusInfo
, remote_host
) },
434 { "RemoteUser", "s", NULL
, offsetof(SessionStatusInfo
, remote_user
) },
435 { "Service", "s", NULL
, offsetof(SessionStatusInfo
, service
) },
436 { "Desktop", "s", NULL
, offsetof(SessionStatusInfo
, desktop
) },
437 { "Type", "s", NULL
, offsetof(SessionStatusInfo
, type
) },
438 { "Class", "s", NULL
, offsetof(SessionStatusInfo
, class) },
439 { "Scope", "s", NULL
, offsetof(SessionStatusInfo
, scope
) },
440 { "State", "s", NULL
, offsetof(SessionStatusInfo
, state
) },
441 { "VTNr", "u", NULL
, offsetof(SessionStatusInfo
, vtnr
) },
442 { "Leader", "u", NULL
, offsetof(SessionStatusInfo
, leader
) },
443 { "Remote", "b", NULL
, offsetof(SessionStatusInfo
, remote
) },
444 { "Timestamp", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.realtime
) },
445 { "TimestampMonotonic", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.monotonic
) },
446 { "User", "(uo)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, uid
) },
447 { "Seat", "(so)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, seat
) },
451 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
452 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
453 SessionStatusInfo i
= {};
456 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
458 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
465 printf("%s - ", strna(i
.id
));
468 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
470 printf("%"PRIu32
"\n", i
.uid
);
472 if (i
.timestamp
.realtime
> 0 && i
.timestamp
.realtime
< USEC_INFINITY
)
473 printf("\t Since: %s; %s\n",
474 FORMAT_TIMESTAMP(i
.timestamp
.realtime
),
475 FORMAT_TIMESTAMP_RELATIVE(i
.timestamp
.realtime
));
478 _cleanup_free_
char *t
= NULL
;
480 printf("\t Leader: %"PRIu32
, i
.leader
);
482 (void) get_process_comm(i
.leader
, &t
);
489 if (!isempty(i
.seat
)) {
490 printf("\t Seat: %s", i
.seat
);
493 printf("; vc%u", i
.vtnr
);
499 printf("\t TTY: %s\n", i
.tty
);
501 printf("\t Display: %s\n", i
.display
);
503 if (i
.remote_host
&& i
.remote_user
)
504 printf("\t Remote: %s@%s\n", i
.remote_user
, i
.remote_host
);
505 else if (i
.remote_host
)
506 printf("\t Remote: %s\n", i
.remote_host
);
507 else if (i
.remote_user
)
508 printf("\t Remote: user %s\n", i
.remote_user
);
510 printf("\t Remote: Yes\n");
513 printf("\t Service: %s", i
.service
);
516 printf("; type %s", i
.type
);
519 printf("; class %s", i
.class);
523 printf("\t Type: %s", i
.type
);
526 printf("; class %s", i
.class);
530 printf("\t Class: %s\n", i
.class);
532 if (!isempty(i
.desktop
))
533 printf("\t Desktop: %s\n", i
.desktop
);
536 printf("\t State: %s\n", i
.state
);
539 printf("\t Unit: %s\n", i
.scope
);
540 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Scope", i
.scope
, i
.leader
);
542 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
543 show_journal_by_unit(
549 i
.timestamp
.monotonic
,
552 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
553 SD_JOURNAL_LOCAL_ONLY
,
561 static int print_user_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
563 static const struct bus_properties_map map
[] = {
564 { "Name", "s", NULL
, offsetof(UserStatusInfo
, name
) },
565 { "Linger", "b", NULL
, offsetof(UserStatusInfo
, linger
) },
566 { "Slice", "s", NULL
, offsetof(UserStatusInfo
, slice
) },
567 { "State", "s", NULL
, offsetof(UserStatusInfo
, state
) },
568 { "UID", "u", NULL
, offsetof(UserStatusInfo
, uid
) },
569 { "Timestamp", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.realtime
) },
570 { "TimestampMonotonic", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.monotonic
) },
571 { "Display", "(so)", prop_map_first_of_struct
, offsetof(UserStatusInfo
, display
) },
572 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(UserStatusInfo
, sessions
) },
576 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
577 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
578 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
581 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
583 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
591 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
593 printf("%"PRIu32
"\n", i
.uid
);
595 if (i
.timestamp
.realtime
> 0 && i
.timestamp
.realtime
< USEC_INFINITY
)
596 printf("\t Since: %s; %s\n",
597 FORMAT_TIMESTAMP(i
.timestamp
.realtime
),
598 FORMAT_TIMESTAMP_RELATIVE(i
.timestamp
.realtime
));
600 if (!isempty(i
.state
))
601 printf("\t State: %s\n", i
.state
);
603 if (!strv_isempty(i
.sessions
)) {
605 printf("\tSessions:");
607 STRV_FOREACH(l
, i
.sessions
)
609 streq_ptr(*l
, i
.display
) ? "*" : "",
615 printf("\t Linger: %s\n", yes_no(i
.linger
));
618 printf("\t Unit: %s\n", i
.slice
);
619 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Slice", i
.slice
, 0);
621 show_journal_by_unit(
627 i
.timestamp
.monotonic
,
630 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
631 SD_JOURNAL_LOCAL_ONLY
,
639 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
641 static const struct bus_properties_map map
[] = {
642 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
643 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
644 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
648 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
649 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
650 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
653 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, 0, &error
, &m
, &i
);
655 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
662 printf("%s\n", strna(i
.id
));
664 if (!strv_isempty(i
.sessions
)) {
666 printf("\tSessions:");
668 STRV_FOREACH(l
, i
.sessions
) {
669 if (streq_ptr(*l
, i
.active_session
))
678 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
687 printf("\t Devices:\n");
689 show_sysfs(i
.id
, "\t\t ", c
, get_output_flags());
695 static int print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, BusPrintPropertyFlags flags
) {
697 const char *contents
;
703 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
709 case SD_BUS_TYPE_STRUCT
:
711 if (contents
[0] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
714 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
716 return bus_log_parse_error(r
);
718 bus_print_property_value(name
, expected_value
, flags
, s
);
722 } else if (contents
[0] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
725 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
727 return bus_log_parse_error(r
);
729 if (!uid_is_valid(uid
))
730 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
731 "Invalid user ID: " UID_FMT
,
734 bus_print_property_valuef(name
, expected_value
, flags
, UID_FMT
, uid
);
739 case SD_BUS_TYPE_ARRAY
:
741 if (contents
[0] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
745 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
747 return bus_log_parse_error(r
);
749 if (!FLAGS_SET(flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
))
752 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
753 printf("%s%s", space
? " " : "", s
);
757 if (space
|| !FLAGS_SET(flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
))
761 return bus_log_parse_error(r
);
763 r
= sd_bus_message_exit_container(m
);
765 return bus_log_parse_error(r
);
775 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
787 r
= bus_print_all_properties(
789 "org.freedesktop.login1",
796 return bus_log_parse_error(r
);
801 static int show_session(int argc
, char *argv
[], void *userdata
) {
802 bool properties
, new_line
= false;
803 sd_bus
*bus
= userdata
;
805 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
806 _cleanup_free_
char *path
= NULL
;
811 properties
= !strstr(argv
[0], "status");
813 pager_open(arg_pager_flags
);
816 /* If no argument is specified inspect the manager itself */
818 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
820 return print_session_status_info(bus
, "/org/freedesktop/login1/session/auto", &new_line
);
823 for (int i
= 1; i
< argc
; i
++) {
824 r
= get_session_path(bus
, argv
[i
], &error
, &path
);
826 return log_error_errno(r
, "Failed to get session path: %s", bus_error_message(&error
, r
));
829 r
= show_properties(bus
, path
, &new_line
);
831 r
= print_session_status_info(bus
, path
, &new_line
);
840 static int show_user(int argc
, char *argv
[], void *userdata
) {
841 bool properties
, new_line
= false;
842 sd_bus
*bus
= userdata
;
848 properties
= !strstr(argv
[0], "status");
850 pager_open(arg_pager_flags
);
853 /* If no argument is specified inspect the manager itself */
855 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
857 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
860 for (int i
= 1; i
< argc
; i
++) {
861 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
862 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
863 const char *path
= NULL
;
866 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
868 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
870 r
= sd_bus_call_method(
872 "org.freedesktop.login1",
873 "/org/freedesktop/login1",
874 "org.freedesktop.login1.Manager",
877 "u", (uint32_t) uid
);
879 return log_error_errno(r
, "Failed to get user: %s", bus_error_message(&error
, r
));
881 r
= sd_bus_message_read(reply
, "o", &path
);
883 return bus_log_parse_error(r
);
886 r
= show_properties(bus
, path
, &new_line
);
888 r
= print_user_status_info(bus
, path
, &new_line
);
897 static int show_seat(int argc
, char *argv
[], void *userdata
) {
898 bool properties
, new_line
= false;
899 sd_bus
*bus
= userdata
;
905 properties
= !strstr(argv
[0], "status");
907 pager_open(arg_pager_flags
);
910 /* If no argument is specified inspect the manager itself */
912 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
914 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/auto", &new_line
);
917 for (int i
= 1; i
< argc
; i
++) {
918 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
919 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
920 const char *path
= NULL
;
922 r
= bus_call_method(bus
, bus_login_mgr
, "GetSeat", &error
, &reply
, "s", argv
[i
]);
924 return log_error_errno(r
, "Failed to get seat: %s", bus_error_message(&error
, r
));
926 r
= sd_bus_message_read(reply
, "o", &path
);
928 return bus_log_parse_error(r
);
931 r
= show_properties(bus
, path
, &new_line
);
933 r
= print_seat_status_info(bus
, path
, &new_line
);
942 static int activate(int argc
, char *argv
[], void *userdata
) {
943 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
944 sd_bus
*bus
= userdata
;
950 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
953 r
= sd_bus_call_method(
955 "org.freedesktop.login1",
956 "/org/freedesktop/login1/session/auto",
957 "org.freedesktop.login1.Session",
958 streq(argv
[0], "lock-session") ? "Lock" :
959 streq(argv
[0], "unlock-session") ? "Unlock" :
960 streq(argv
[0], "terminate-session") ? "Terminate" :
964 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
969 for (int i
= 1; i
< argc
; i
++) {
974 streq(argv
[0], "lock-session") ? "LockSession" :
975 streq(argv
[0], "unlock-session") ? "UnlockSession" :
976 streq(argv
[0], "terminate-session") ? "TerminateSession" :
981 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
987 static int kill_session(int argc
, char *argv
[], void *userdata
) {
988 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
989 sd_bus
*bus
= userdata
;
995 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
998 arg_kill_who
= "all";
1000 for (int i
= 1; i
< argc
; i
++) {
1002 r
= bus_call_method(
1007 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1009 return log_error_errno(r
, "Could not kill session: %s", bus_error_message(&error
, r
));
1015 static int enable_linger(int argc
, char *argv
[], void *userdata
) {
1016 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1017 sd_bus
*bus
= userdata
;
1018 char* short_argv
[3];
1025 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1027 b
= streq(argv
[0], "enable-linger");
1030 /* No argument? Let's use an empty user name,
1031 * then logind will use our user. */
1033 short_argv
[0] = argv
[0];
1034 short_argv
[1] = (char*) "";
1035 short_argv
[2] = NULL
;
1040 for (int i
= 1; i
< argc
; i
++) {
1043 if (isempty(argv
[i
]))
1046 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1048 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1051 r
= bus_call_method(
1056 "ubb", (uint32_t) uid
, b
, true);
1058 return log_error_errno(r
, "Could not enable linger: %s", bus_error_message(&error
, r
));
1064 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1065 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1066 sd_bus
*bus
= userdata
;
1072 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1074 for (int i
= 1; i
< argc
; i
++) {
1077 if (isempty(argv
[i
]))
1080 const char *u
= argv
[i
];
1082 r
= get_user_creds(&u
, &uid
, NULL
, NULL
, NULL
, 0);
1084 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1087 r
= bus_call_method(bus
, bus_login_mgr
, "TerminateUser", &error
, NULL
, "u", (uint32_t) uid
);
1089 return log_error_errno(r
, "Could not terminate user: %s", bus_error_message(&error
, r
));
1095 static int kill_user(int argc
, char *argv
[], void *userdata
) {
1096 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1097 sd_bus
*bus
= userdata
;
1103 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1106 arg_kill_who
= "all";
1108 for (int i
= 1; i
< argc
; i
++) {
1111 if (isempty(argv
[i
]))
1114 const char *u
= argv
[i
];
1116 r
= get_user_creds(&u
, &uid
, NULL
, NULL
, NULL
, 0);
1118 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1121 r
= bus_call_method(
1126 "ui", (uint32_t) uid
, arg_signal
);
1128 return log_error_errno(r
, "Could not kill user: %s", bus_error_message(&error
, r
));
1134 static int attach(int argc
, char *argv
[], void *userdata
) {
1135 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1136 sd_bus
*bus
= userdata
;
1142 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1144 for (int i
= 2; i
< argc
; i
++) {
1146 r
= bus_call_method(
1151 "ssb", argv
[1], argv
[i
], true);
1153 return log_error_errno(r
, "Could not attach device: %s", bus_error_message(&error
, r
));
1159 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1160 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1161 sd_bus
*bus
= userdata
;
1167 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1169 r
= bus_call_method(bus
, bus_login_mgr
, "FlushDevices", &error
, NULL
, "b", true);
1171 return log_error_errno(r
, "Could not flush devices: %s", bus_error_message(&error
, r
));
1176 static int lock_sessions(int argc
, char *argv
[], void *userdata
) {
1177 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1178 sd_bus
*bus
= userdata
;
1184 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1186 r
= bus_call_method(
1189 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1193 return log_error_errno(r
, "Could not lock sessions: %s", bus_error_message(&error
, r
));
1198 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1199 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1200 sd_bus
*bus
= userdata
;
1206 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1208 for (int i
= 1; i
< argc
; i
++) {
1210 r
= bus_call_method(bus
, bus_login_mgr
, "TerminateSeat", &error
, NULL
, "s", argv
[i
]);
1212 return log_error_errno(r
, "Could not terminate seat: %s", bus_error_message(&error
, r
));
1218 static int help(int argc
, char *argv
[], void *userdata
) {
1219 _cleanup_free_
char *link
= NULL
;
1222 pager_open(arg_pager_flags
);
1224 r
= terminal_urlify_man("loginctl", "1", &link
);
1228 printf("%s [OPTIONS...] COMMAND ...\n\n"
1229 "%sSend control commands to or query the login manager.%s\n"
1230 "\nSession Commands:\n"
1231 " list-sessions List sessions\n"
1232 " session-status [ID...] Show session status\n"
1233 " show-session [ID...] Show properties of sessions or the manager\n"
1234 " activate [ID] Activate a session\n"
1235 " lock-session [ID...] Screen lock one or more sessions\n"
1236 " unlock-session [ID...] Screen unlock one or more sessions\n"
1237 " lock-sessions Screen lock all current sessions\n"
1238 " unlock-sessions Screen unlock all current sessions\n"
1239 " terminate-session ID... Terminate one or more sessions\n"
1240 " kill-session ID... Send signal to processes of a session\n"
1241 "\nUser Commands:\n"
1242 " list-users List users\n"
1243 " user-status [USER...] Show user status\n"
1244 " show-user [USER...] Show properties of users or the manager\n"
1245 " enable-linger [USER...] Enable linger state of one or more users\n"
1246 " disable-linger [USER...] Disable linger state of one or more users\n"
1247 " terminate-user USER... Terminate all sessions of one or more users\n"
1248 " kill-user USER... Send signal to processes of a user\n"
1249 "\nSeat Commands:\n"
1250 " list-seats List seats\n"
1251 " seat-status [NAME...] Show seat status\n"
1252 " show-seat [NAME...] Show properties of seats or the manager\n"
1253 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1254 " flush-devices Flush all device associations\n"
1255 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1257 " -h --help Show this help\n"
1258 " --version Show package version\n"
1259 " --no-pager Do not pipe output into a pager\n"
1260 " --no-legend Do not show the headers and footers\n"
1261 " --no-ask-password Don't prompt for password\n"
1262 " -H --host=[USER@]HOST Operate on remote host\n"
1263 " -M --machine=CONTAINER Operate on local container\n"
1264 " -p --property=NAME Show only properties by this name\n"
1265 " -P NAME Equivalent to --value --property=NAME\n"
1266 " -a --all Show all properties, including empty ones\n"
1267 " --value When showing properties, only print the value\n"
1268 " -l --full Do not ellipsize output\n"
1269 " --kill-who=WHO Who to send signal to\n"
1270 " -s --signal=SIGNAL Which signal to send\n"
1271 " -n --lines=INTEGER Number of journal entries to show\n"
1272 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1273 " short-iso, short-iso-precise, short-full,\n"
1274 " short-monotonic, short-unix, verbose, export,\n"
1275 " json, json-pretty, json-sse, json-seq, cat,\n"
1277 "\nSee the %s for details.\n",
1278 program_invocation_short_name
,
1286 static int parse_argv(int argc
, char *argv
[]) {
1288 ARG_VERSION
= 0x100,
1293 ARG_NO_ASK_PASSWORD
,
1296 static const struct option options
[] = {
1297 { "help", no_argument
, NULL
, 'h' },
1298 { "version", no_argument
, NULL
, ARG_VERSION
},
1299 { "property", required_argument
, NULL
, 'p' },
1300 { "all", no_argument
, NULL
, 'a' },
1301 { "value", no_argument
, NULL
, ARG_VALUE
},
1302 { "full", no_argument
, NULL
, 'l' },
1303 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1304 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1305 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1306 { "signal", required_argument
, NULL
, 's' },
1307 { "host", required_argument
, NULL
, 'H' },
1308 { "machine", required_argument
, NULL
, 'M' },
1309 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1310 { "lines", required_argument
, NULL
, 'n' },
1311 { "output", required_argument
, NULL
, 'o' },
1320 while ((c
= getopt_long(argc
, argv
, "hp:P:als:H:M:n:o:", options
, NULL
)) >= 0)
1325 return help(0, NULL
, NULL
);
1331 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
1335 r
= strv_extend(&arg_property
, optarg
);
1339 /* If the user asked for a particular
1340 * property, show it to them, even if it is
1342 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
1347 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_SHOW_EMPTY
, true);
1351 SET_FLAG(arg_print_flags
, BUS_PRINT_PROPERTY_ONLY_VALUE
, true);
1359 if (safe_atou(optarg
, &arg_lines
) < 0)
1360 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1361 "Failed to parse lines '%s'", optarg
);
1365 if (streq(optarg
, "help")) {
1366 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
1370 arg_output
= output_mode_from_string(optarg
);
1372 return log_error_errno(arg_output
, "Unknown output '%s'.", optarg
);
1374 if (OUTPUT_MODE_IS_JSON(arg_output
))
1380 arg_pager_flags
|= PAGER_DISABLE
;
1387 case ARG_NO_ASK_PASSWORD
:
1388 arg_ask_password
= false;
1392 arg_kill_who
= optarg
;
1396 r
= parse_signal_argument(optarg
, &arg_signal
);
1402 arg_transport
= BUS_TRANSPORT_REMOTE
;
1407 arg_transport
= BUS_TRANSPORT_MACHINE
;
1415 assert_not_reached();
1421 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1422 static const Verb verbs
[] = {
1423 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1424 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1425 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1426 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1427 { "activate", VERB_ANY
, 2, 0, activate
},
1428 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1429 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1430 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1431 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1432 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1433 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1434 { "list-users", VERB_ANY
, 1, 0, list_users
},
1435 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1436 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1437 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1438 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1439 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1440 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1441 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1442 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1443 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1444 { "attach", 3, VERB_ANY
, 0, attach
},
1445 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1446 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1450 return dispatch_verb(argc
, argv
, verbs
, bus
);
1453 static int run(int argc
, char *argv
[]) {
1454 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1457 setlocale(LC_ALL
, "");
1460 /* The journal merging logic potentially needs a lot of fds. */
1461 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1465 r
= parse_argv(argc
, argv
);
1469 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1471 return bus_log_connect_error(r
, arg_transport
);
1473 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1475 return loginctl_main(argc
, argv
, bus
);
1478 DEFINE_MAIN_FUNCTION(run
);