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 bool arg_all
= false;
43 static bool arg_value
= false;
44 static bool arg_full
= false;
45 static PagerFlags arg_pager_flags
= 0;
46 static bool arg_legend
= true;
47 static const char *arg_kill_who
= NULL
;
48 static int arg_signal
= SIGTERM
;
49 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
50 static char *arg_host
= NULL
;
51 static bool arg_ask_password
= true;
52 static unsigned arg_lines
= 10;
53 static OutputMode arg_output
= OUTPUT_SHORT
;
55 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
57 static OutputFlags
get_output_flags(void) {
60 arg_all
* OUTPUT_SHOW_ALL
|
61 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
62 colors_enabled() * OUTPUT_COLOR
;
65 static int get_session_path(sd_bus
*bus
, const char *session_id
, sd_bus_error
*error
, char **path
) {
66 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
70 r
= bus_call_method(bus
, bus_login_mgr
, "GetSession", error
, &reply
, "s", session_id
);
74 r
= sd_bus_message_read(reply
, "o", &ans
);
86 static int show_table(Table
*table
, const char *word
) {
92 if (table_get_rows(table
) > 1 || OUTPUT_MODE_IS_JSON(arg_output
)) {
93 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
95 return table_log_sort_error(r
);
97 table_set_header(table
, arg_legend
);
99 if (OUTPUT_MODE_IS_JSON(arg_output
))
100 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | JSON_FORMAT_COLOR_AUTO
);
102 r
= table_print(table
, NULL
);
104 return table_log_print_error(r
);
108 if (table_get_rows(table
) > 1)
109 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
111 printf("No %s.\n", word
);
117 static int list_sessions(int argc
, char *argv
[], void *userdata
) {
118 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
119 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
120 _cleanup_(table_unrefp
) Table
*table
= NULL
;
121 sd_bus
*bus
= userdata
;
127 (void) pager_open(arg_pager_flags
);
129 r
= bus_call_method(bus
, bus_login_mgr
, "ListSessions", &error
, &reply
, NULL
);
131 return log_error_errno(r
, "Failed to list sessions: %s", bus_error_message(&error
, r
));
133 r
= sd_bus_message_enter_container(reply
, 'a', "(susso)");
135 return bus_log_parse_error(r
);
137 table
= table_new("session", "uid", "user", "seat", "tty");
141 /* Right-align the first two fields (since they are numeric) */
142 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
143 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100);
146 _cleanup_(sd_bus_error_free
) sd_bus_error error_tty
= SD_BUS_ERROR_NULL
;
147 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply_tty
= NULL
;
148 const char *id
, *user
, *seat
, *object
, *tty
= NULL
;
151 r
= sd_bus_message_read(reply
, "(susso)", &id
, &uid
, &user
, &seat
, &object
);
153 return bus_log_parse_error(r
);
157 r
= sd_bus_get_property(
159 "org.freedesktop.login1",
161 "org.freedesktop.login1.Session",
167 log_warning_errno(r
, "Failed to get TTY for session %s: %s", id
, bus_error_message(&error_tty
, r
));
169 r
= sd_bus_message_read(reply_tty
, "s", &tty
);
171 return bus_log_parse_error(r
);
174 r
= table_add_many(table
,
176 TABLE_UID
, (uid_t
) uid
,
179 TABLE_STRING
, strna(tty
));
181 return table_log_add_error(r
);
184 r
= sd_bus_message_exit_container(reply
);
186 return bus_log_parse_error(r
);
188 return show_table(table
, "sessions");
191 static int list_users(int argc
, char *argv
[], void *userdata
) {
192 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
193 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
194 _cleanup_(table_unrefp
) Table
*table
= NULL
;
195 sd_bus
*bus
= userdata
;
201 (void) pager_open(arg_pager_flags
);
203 r
= bus_call_method(bus
, bus_login_mgr
, "ListUsers", &error
, &reply
, NULL
);
205 return log_error_errno(r
, "Failed to list users: %s", bus_error_message(&error
, r
));
207 r
= sd_bus_message_enter_container(reply
, 'a', "(uso)");
209 return bus_log_parse_error(r
);
211 table
= table_new("uid", "user");
215 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
221 r
= sd_bus_message_read(reply
, "(uso)", &uid
, &user
, NULL
);
223 return bus_log_parse_error(r
);
227 r
= table_add_many(table
,
228 TABLE_UID
, (uid_t
) uid
,
231 return table_log_add_error(r
);
234 r
= sd_bus_message_exit_container(reply
);
236 return bus_log_parse_error(r
);
238 return show_table(table
, "users");
241 static int list_seats(int argc
, char *argv
[], void *userdata
) {
242 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
243 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
244 _cleanup_(table_unrefp
) Table
*table
= NULL
;
245 sd_bus
*bus
= userdata
;
251 (void) pager_open(arg_pager_flags
);
253 r
= bus_call_method(bus
, bus_login_mgr
, "ListSeats", &error
, &reply
, NULL
);
255 return log_error_errno(r
, "Failed to list seats: %s", bus_error_message(&error
, r
));
257 r
= sd_bus_message_enter_container(reply
, 'a', "(so)");
259 return bus_log_parse_error(r
);
261 table
= table_new("seat");
268 r
= sd_bus_message_read(reply
, "(so)", &seat
, NULL
);
270 return bus_log_parse_error(r
);
274 r
= table_add_cell(table
, NULL
, TABLE_STRING
, seat
);
276 return table_log_add_error(r
);
279 r
= sd_bus_message_exit_container(reply
);
281 return bus_log_parse_error(r
);
283 return show_table(table
, "seats");
286 static int show_unit_cgroup(sd_bus
*bus
, const char *interface
, const char *unit
, pid_t leader
) {
287 _cleanup_free_
char *cgroup
= NULL
;
288 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
295 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
308 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
311 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
314 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
316 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
319 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
321 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
326 typedef struct SessionStatusInfo
{
330 struct dual_timestamp timestamp
;
336 const char *remote_host
;
337 const char *remote_user
;
347 typedef struct UserStatusInfo
{
351 struct dual_timestamp timestamp
;
358 typedef struct SeatStatusInfo
{
360 const char *active_session
;
364 static void user_status_info_clear(UserStatusInfo
*info
) {
366 strv_free(info
->sessions
);
371 static void seat_status_info_clear(SeatStatusInfo
*info
) {
373 strv_free(info
->sessions
);
378 static int prop_map_first_of_struct(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
379 const char *contents
;
382 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
386 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, contents
);
390 r
= sd_bus_message_read_basic(m
, contents
[0], userdata
);
394 r
= sd_bus_message_skip(m
, contents
+1);
398 r
= sd_bus_message_exit_container(m
);
405 static int prop_map_sessions_strv(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
412 r
= sd_bus_message_enter_container(m
, 'a', "(so)");
416 while ((r
= sd_bus_message_read(m
, "(so)", &name
, NULL
)) > 0) {
417 r
= strv_extend(userdata
, name
);
424 return sd_bus_message_exit_container(m
);
427 static int print_session_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
429 static const struct bus_properties_map map
[] = {
430 { "Id", "s", NULL
, offsetof(SessionStatusInfo
, id
) },
431 { "Name", "s", NULL
, offsetof(SessionStatusInfo
, name
) },
432 { "TTY", "s", NULL
, offsetof(SessionStatusInfo
, tty
) },
433 { "Display", "s", NULL
, offsetof(SessionStatusInfo
, display
) },
434 { "RemoteHost", "s", NULL
, offsetof(SessionStatusInfo
, remote_host
) },
435 { "RemoteUser", "s", NULL
, offsetof(SessionStatusInfo
, remote_user
) },
436 { "Service", "s", NULL
, offsetof(SessionStatusInfo
, service
) },
437 { "Desktop", "s", NULL
, offsetof(SessionStatusInfo
, desktop
) },
438 { "Type", "s", NULL
, offsetof(SessionStatusInfo
, type
) },
439 { "Class", "s", NULL
, offsetof(SessionStatusInfo
, class) },
440 { "Scope", "s", NULL
, offsetof(SessionStatusInfo
, scope
) },
441 { "State", "s", NULL
, offsetof(SessionStatusInfo
, state
) },
442 { "VTNr", "u", NULL
, offsetof(SessionStatusInfo
, vtnr
) },
443 { "Leader", "u", NULL
, offsetof(SessionStatusInfo
, leader
) },
444 { "Remote", "b", NULL
, offsetof(SessionStatusInfo
, remote
) },
445 { "Timestamp", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.realtime
) },
446 { "TimestampMonotonic", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.monotonic
) },
447 { "User", "(uo)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, uid
) },
448 { "Seat", "(so)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, seat
) },
452 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
453 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
454 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
455 char since2
[FORMAT_TIMESTAMP_MAX
];
457 SessionStatusInfo i
= {};
460 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
462 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
469 printf("%s - ", strna(i
.id
));
472 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
474 printf("%"PRIu32
"\n", i
.uid
);
476 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
477 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
480 printf("\t Since: %s; %s\n", s2
, s1
);
482 printf("\t Since: %s\n", s2
);
485 _cleanup_free_
char *t
= NULL
;
487 printf("\t Leader: %"PRIu32
, i
.leader
);
489 (void) get_process_comm(i
.leader
, &t
);
496 if (!isempty(i
.seat
)) {
497 printf("\t Seat: %s", i
.seat
);
500 printf("; vc%u", i
.vtnr
);
506 printf("\t TTY: %s\n", i
.tty
);
508 printf("\t Display: %s\n", i
.display
);
510 if (i
.remote_host
&& i
.remote_user
)
511 printf("\t Remote: %s@%s\n", i
.remote_user
, i
.remote_host
);
512 else if (i
.remote_host
)
513 printf("\t Remote: %s\n", i
.remote_host
);
514 else if (i
.remote_user
)
515 printf("\t Remote: user %s\n", i
.remote_user
);
517 printf("\t Remote: Yes\n");
520 printf("\t Service: %s", i
.service
);
523 printf("; type %s", i
.type
);
526 printf("; class %s", i
.class);
530 printf("\t Type: %s", i
.type
);
533 printf("; class %s", i
.class);
537 printf("\t Class: %s\n", i
.class);
539 if (!isempty(i
.desktop
))
540 printf("\t Desktop: %s\n", i
.desktop
);
543 printf("\t State: %s\n", i
.state
);
546 printf("\t Unit: %s\n", i
.scope
);
547 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Scope", i
.scope
, i
.leader
);
549 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
550 show_journal_by_unit(
556 i
.timestamp
.monotonic
,
559 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
560 SD_JOURNAL_LOCAL_ONLY
,
568 static int print_user_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
570 static const struct bus_properties_map map
[] = {
571 { "Name", "s", NULL
, offsetof(UserStatusInfo
, name
) },
572 { "Linger", "b", NULL
, offsetof(UserStatusInfo
, linger
) },
573 { "Slice", "s", NULL
, offsetof(UserStatusInfo
, slice
) },
574 { "State", "s", NULL
, offsetof(UserStatusInfo
, state
) },
575 { "UID", "u", NULL
, offsetof(UserStatusInfo
, uid
) },
576 { "Timestamp", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.realtime
) },
577 { "TimestampMonotonic", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.monotonic
) },
578 { "Display", "(so)", prop_map_first_of_struct
, offsetof(UserStatusInfo
, display
) },
579 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(UserStatusInfo
, sessions
) },
583 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
584 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
585 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
586 char since2
[FORMAT_TIMESTAMP_MAX
];
588 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
591 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
593 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
601 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
603 printf("%"PRIu32
"\n", i
.uid
);
605 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
606 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
609 printf("\t Since: %s; %s\n", s2
, s1
);
611 printf("\t Since: %s\n", s2
);
613 if (!isempty(i
.state
))
614 printf("\t State: %s\n", i
.state
);
616 if (!strv_isempty(i
.sessions
)) {
618 printf("\tSessions:");
620 STRV_FOREACH(l
, i
.sessions
)
622 streq_ptr(*l
, i
.display
) ? "*" : "",
628 printf("\t Linger: %s\n", yes_no(i
.linger
));
631 printf("\t Unit: %s\n", i
.slice
);
632 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Slice", i
.slice
, 0);
634 show_journal_by_unit(
640 i
.timestamp
.monotonic
,
643 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
644 SD_JOURNAL_LOCAL_ONLY
,
652 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
654 static const struct bus_properties_map map
[] = {
655 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
656 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
657 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
661 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
662 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
663 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
666 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, 0, &error
, &m
, &i
);
668 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
675 printf("%s\n", strna(i
.id
));
677 if (!strv_isempty(i
.sessions
)) {
679 printf("\tSessions:");
681 STRV_FOREACH(l
, i
.sessions
) {
682 if (streq_ptr(*l
, i
.active_session
))
691 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
700 printf("\t Devices:\n");
702 show_sysfs(i
.id
, "\t\t ", c
, get_output_flags());
708 static int print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
710 const char *contents
;
716 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
722 case SD_BUS_TYPE_STRUCT
:
724 if (contents
[0] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
727 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
729 return bus_log_parse_error(r
);
731 if (all
|| !isempty(s
))
732 bus_print_property_value(name
, expected_value
, value
, s
);
736 } else if (contents
[0] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
739 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
741 return bus_log_parse_error(r
);
743 if (!uid_is_valid(uid
))
744 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
745 "Invalid user ID: " UID_FMT
,
748 bus_print_property_valuef(name
, expected_value
, value
, UID_FMT
, uid
);
753 case SD_BUS_TYPE_ARRAY
:
755 if (contents
[0] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
759 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
761 return bus_log_parse_error(r
);
766 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
767 printf("%s%s", space
? " " : "", s
);
775 return bus_log_parse_error(r
);
777 r
= sd_bus_message_exit_container(m
);
779 return bus_log_parse_error(r
);
789 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
801 r
= bus_print_all_properties(
803 "org.freedesktop.login1",
811 return bus_log_parse_error(r
);
816 static int show_session(int argc
, char *argv
[], void *userdata
) {
817 bool properties
, new_line
= false;
818 sd_bus
*bus
= userdata
;
820 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
821 _cleanup_free_
char *path
= NULL
;
826 properties
= !strstr(argv
[0], "status");
828 (void) pager_open(arg_pager_flags
);
831 /* If no argument is specified inspect the manager itself */
833 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
835 return print_session_status_info(bus
, "/org/freedesktop/login1/session/auto", &new_line
);
838 for (int i
= 1; i
< argc
; i
++) {
839 r
= get_session_path(bus
, argv
[i
], &error
, &path
);
841 return log_error_errno(r
, "Failed to get session path: %s", bus_error_message(&error
, r
));
844 r
= show_properties(bus
, path
, &new_line
);
846 r
= print_session_status_info(bus
, path
, &new_line
);
855 static int show_user(int argc
, char *argv
[], void *userdata
) {
856 bool properties
, new_line
= false;
857 sd_bus
*bus
= userdata
;
863 properties
= !strstr(argv
[0], "status");
865 (void) pager_open(arg_pager_flags
);
868 /* If no argument is specified inspect the manager itself */
870 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
872 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
875 for (int i
= 1; i
< argc
; i
++) {
876 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
877 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
878 const char *path
= NULL
;
881 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
883 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
885 r
= sd_bus_call_method(
887 "org.freedesktop.login1",
888 "/org/freedesktop/login1",
889 "org.freedesktop.login1.Manager",
892 "u", (uint32_t) uid
);
894 return log_error_errno(r
, "Failed to get user: %s", bus_error_message(&error
, r
));
896 r
= sd_bus_message_read(reply
, "o", &path
);
898 return bus_log_parse_error(r
);
901 r
= show_properties(bus
, path
, &new_line
);
903 r
= print_user_status_info(bus
, path
, &new_line
);
912 static int show_seat(int argc
, char *argv
[], void *userdata
) {
913 bool properties
, new_line
= false;
914 sd_bus
*bus
= userdata
;
920 properties
= !strstr(argv
[0], "status");
922 (void) pager_open(arg_pager_flags
);
925 /* If no argument is specified inspect the manager itself */
927 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
929 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/auto", &new_line
);
932 for (int i
= 1; i
< argc
; i
++) {
933 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
934 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
935 const char *path
= NULL
;
937 r
= bus_call_method(bus
, bus_login_mgr
, "GetSeat", &error
, &reply
, "s", argv
[i
]);
939 return log_error_errno(r
, "Failed to get seat: %s", bus_error_message(&error
, r
));
941 r
= sd_bus_message_read(reply
, "o", &path
);
943 return bus_log_parse_error(r
);
946 r
= show_properties(bus
, path
, &new_line
);
948 r
= print_seat_status_info(bus
, path
, &new_line
);
957 static int activate(int argc
, char *argv
[], void *userdata
) {
958 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
959 sd_bus
*bus
= userdata
;
965 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
968 r
= sd_bus_call_method(
970 "org.freedesktop.login1",
971 "/org/freedesktop/login1/session/auto",
972 "org.freedesktop.login1.Session",
973 streq(argv
[0], "lock-session") ? "Lock" :
974 streq(argv
[0], "unlock-session") ? "Unlock" :
975 streq(argv
[0], "terminate-session") ? "Terminate" :
979 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
984 for (int i
= 1; i
< argc
; i
++) {
989 streq(argv
[0], "lock-session") ? "LockSession" :
990 streq(argv
[0], "unlock-session") ? "UnlockSession" :
991 streq(argv
[0], "terminate-session") ? "TerminateSession" :
996 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
1002 static int kill_session(int argc
, char *argv
[], void *userdata
) {
1003 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1004 sd_bus
*bus
= userdata
;
1010 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1013 arg_kill_who
= "all";
1015 for (int i
= 1; i
< argc
; i
++) {
1017 r
= bus_call_method(
1022 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1024 return log_error_errno(r
, "Could not kill session: %s", bus_error_message(&error
, r
));
1030 static int enable_linger(int argc
, char *argv
[], void *userdata
) {
1031 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1032 sd_bus
*bus
= userdata
;
1033 char* short_argv
[3];
1040 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1042 b
= streq(argv
[0], "enable-linger");
1045 /* No argument? Let's use an empty user name,
1046 * then logind will use our user. */
1048 short_argv
[0] = argv
[0];
1049 short_argv
[1] = (char*) "";
1050 short_argv
[2] = NULL
;
1055 for (int i
= 1; i
< argc
; i
++) {
1058 if (isempty(argv
[i
]))
1061 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1063 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1066 r
= bus_call_method(
1071 "ubb", (uint32_t) uid
, b
, true);
1073 return log_error_errno(r
, "Could not enable linger: %s", bus_error_message(&error
, r
));
1079 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1080 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1081 sd_bus
*bus
= userdata
;
1087 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1089 for (int i
= 1; i
< argc
; i
++) {
1092 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1094 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1096 r
= bus_call_method(bus
, bus_login_mgr
, "TerminateUser", &error
, NULL
, "u", (uint32_t) uid
);
1098 return log_error_errno(r
, "Could not terminate user: %s", bus_error_message(&error
, r
));
1104 static int kill_user(int argc
, char *argv
[], void *userdata
) {
1105 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1106 sd_bus
*bus
= userdata
;
1112 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1115 arg_kill_who
= "all";
1117 for (int i
= 1; i
< argc
; i
++) {
1120 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1122 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1124 r
= bus_call_method(
1129 "ui", (uint32_t) uid
, arg_signal
);
1131 return log_error_errno(r
, "Could not kill user: %s", bus_error_message(&error
, r
));
1137 static int attach(int argc
, char *argv
[], void *userdata
) {
1138 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1139 sd_bus
*bus
= userdata
;
1145 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1147 for (int i
= 2; i
< argc
; i
++) {
1149 r
= bus_call_method(
1154 "ssb", argv
[1], argv
[i
], true);
1156 return log_error_errno(r
, "Could not attach device: %s", bus_error_message(&error
, r
));
1162 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1163 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1164 sd_bus
*bus
= userdata
;
1170 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1172 r
= bus_call_method(bus
, bus_login_mgr
, "FlushDevices", &error
, NULL
, "b", true);
1174 return log_error_errno(r
, "Could not flush devices: %s", bus_error_message(&error
, r
));
1179 static int lock_sessions(int argc
, char *argv
[], void *userdata
) {
1180 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1181 sd_bus
*bus
= userdata
;
1187 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1189 r
= bus_call_method(
1192 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1196 return log_error_errno(r
, "Could not lock sessions: %s", bus_error_message(&error
, r
));
1201 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1202 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1203 sd_bus
*bus
= userdata
;
1209 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1211 for (int i
= 1; i
< argc
; i
++) {
1213 r
= bus_call_method(bus
, bus_login_mgr
, "TerminateSeat", &error
, NULL
, "s", argv
[i
]);
1215 return log_error_errno(r
, "Could not terminate seat: %s", bus_error_message(&error
, r
));
1221 static int help(int argc
, char *argv
[], void *userdata
) {
1222 _cleanup_free_
char *link
= NULL
;
1225 (void) pager_open(arg_pager_flags
);
1227 r
= terminal_urlify_man("loginctl", "1", &link
);
1231 printf("%s [OPTIONS...] COMMAND ...\n\n"
1232 "%sSend control commands to or query the login manager.%s\n"
1233 "\nSession Commands:\n"
1234 " list-sessions List sessions\n"
1235 " session-status [ID...] Show session status\n"
1236 " show-session [ID...] Show properties of sessions or the manager\n"
1237 " activate [ID] Activate a session\n"
1238 " lock-session [ID...] Screen lock one or more sessions\n"
1239 " unlock-session [ID...] Screen unlock one or more sessions\n"
1240 " lock-sessions Screen lock all current sessions\n"
1241 " unlock-sessions Screen unlock all current sessions\n"
1242 " terminate-session ID... Terminate one or more sessions\n"
1243 " kill-session ID... Send signal to processes of a session\n"
1244 "\nUser Commands:\n"
1245 " list-users List users\n"
1246 " user-status [USER...] Show user status\n"
1247 " show-user [USER...] Show properties of users or the manager\n"
1248 " enable-linger [USER...] Enable linger state of one or more users\n"
1249 " disable-linger [USER...] Disable linger state of one or more users\n"
1250 " terminate-user USER... Terminate all sessions of one or more users\n"
1251 " kill-user USER... Send signal to processes of a user\n"
1252 "\nSeat Commands:\n"
1253 " list-seats List seats\n"
1254 " seat-status [NAME...] Show seat status\n"
1255 " show-seat [NAME...] Show properties of seats or the manager\n"
1256 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1257 " flush-devices Flush all device associations\n"
1258 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1260 " -h --help Show this help\n"
1261 " --version Show package version\n"
1262 " --no-pager Do not pipe output into a pager\n"
1263 " --no-legend Do not show the headers and footers\n"
1264 " --no-ask-password Don't prompt for password\n"
1265 " -H --host=[USER@]HOST Operate on remote host\n"
1266 " -M --machine=CONTAINER Operate on local container\n"
1267 " -p --property=NAME Show only properties by this name\n"
1268 " -P NAME Equivalent to --value --property=NAME\n"
1269 " -a --all Show all properties, including empty ones\n"
1270 " --value When showing properties, only print the value\n"
1271 " -l --full Do not ellipsize output\n"
1272 " --kill-who=WHO Who to send signal to\n"
1273 " -s --signal=SIGNAL Which signal to send\n"
1274 " -n --lines=INTEGER Number of journal entries to show\n"
1275 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1276 " short-iso, short-iso-precise, short-full,\n"
1277 " short-monotonic, short-unix, verbose, export,\n"
1278 " json, json-pretty, json-sse, json-seq, cat,\n"
1280 "\nSee the %s for details.\n",
1281 program_invocation_short_name
,
1289 static int parse_argv(int argc
, char *argv
[]) {
1291 ARG_VERSION
= 0x100,
1296 ARG_NO_ASK_PASSWORD
,
1299 static const struct option options
[] = {
1300 { "help", no_argument
, NULL
, 'h' },
1301 { "version", no_argument
, NULL
, ARG_VERSION
},
1302 { "property", required_argument
, NULL
, 'p' },
1303 { "all", no_argument
, NULL
, 'a' },
1304 { "value", no_argument
, NULL
, ARG_VALUE
},
1305 { "full", no_argument
, NULL
, 'l' },
1306 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1307 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1308 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1309 { "signal", required_argument
, NULL
, 's' },
1310 { "host", required_argument
, NULL
, 'H' },
1311 { "machine", required_argument
, NULL
, 'M' },
1312 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1313 { "lines", required_argument
, NULL
, 'n' },
1314 { "output", required_argument
, NULL
, 'o' },
1323 while ((c
= getopt_long(argc
, argv
, "hp:P:als:H:M:n:o:", options
, NULL
)) >= 0)
1328 return help(0, NULL
, NULL
);
1338 r
= strv_extend(&arg_property
, optarg
);
1342 /* If the user asked for a particular
1343 * property, show it to them, even if it is
1362 if (safe_atou(optarg
, &arg_lines
) < 0)
1363 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1364 "Failed to parse lines '%s'", optarg
);
1368 if (streq(optarg
, "help")) {
1369 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
1373 arg_output
= output_mode_from_string(optarg
);
1375 return log_error_errno(arg_output
, "Unknown output '%s'.", optarg
);
1377 if (OUTPUT_MODE_IS_JSON(arg_output
))
1383 arg_pager_flags
|= PAGER_DISABLE
;
1390 case ARG_NO_ASK_PASSWORD
:
1391 arg_ask_password
= false;
1395 arg_kill_who
= optarg
;
1399 r
= parse_signal_argument(optarg
, &arg_signal
);
1405 arg_transport
= BUS_TRANSPORT_REMOTE
;
1410 arg_transport
= BUS_TRANSPORT_MACHINE
;
1418 assert_not_reached("Unhandled option");
1424 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1425 static const Verb verbs
[] = {
1426 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1427 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1428 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1429 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1430 { "activate", VERB_ANY
, 2, 0, activate
},
1431 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1432 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1433 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1434 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1435 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1436 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1437 { "list-users", VERB_ANY
, 1, 0, list_users
},
1438 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1439 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1440 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1441 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1442 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1443 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1444 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1445 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1446 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1447 { "attach", 3, VERB_ANY
, 0, attach
},
1448 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1449 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1453 return dispatch_verb(argc
, argv
, verbs
, bus
);
1456 static int run(int argc
, char *argv
[]) {
1457 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1460 setlocale(LC_ALL
, "");
1463 /* The journal merging logic potentially needs a lot of fds. */
1464 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1468 r
= parse_argv(argc
, argv
);
1472 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1474 return bus_log_connect_error(r
);
1476 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1478 return loginctl_main(argc
, argv
, bus
);
1481 DEFINE_MAIN_FUNCTION(run
);