1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
13 #include "bus-unit-procs.h"
15 #include "cgroup-show.h"
16 #include "cgroup-util.h"
17 #include "format-table.h"
19 #include "logs-show.h"
21 #include "main-func.h"
22 #include "memory-util.h"
24 #include "parse-util.h"
25 #include "pretty-print.h"
26 #include "process-util.h"
27 #include "rlimit-util.h"
29 #include "signal-util.h"
30 #include "spawn-polkit-agent.h"
31 #include "string-table.h"
33 #include "sysfs-show.h"
34 #include "terminal-util.h"
35 #include "unit-name.h"
36 #include "user-util.h"
39 static char **arg_property
= NULL
;
40 static bool arg_all
= false;
41 static bool arg_value
= false;
42 static bool arg_full
= false;
43 static PagerFlags arg_pager_flags
= 0;
44 static bool arg_legend
= true;
45 static const char *arg_kill_who
= NULL
;
46 static int arg_signal
= SIGTERM
;
47 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
48 static char *arg_host
= NULL
;
49 static bool arg_ask_password
= true;
50 static unsigned arg_lines
= 10;
51 static OutputMode arg_output
= OUTPUT_SHORT
;
53 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
55 static OutputFlags
get_output_flags(void) {
58 arg_all
* OUTPUT_SHOW_ALL
|
59 (arg_full
|| !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
60 colors_enabled() * OUTPUT_COLOR
;
63 static int get_session_path(sd_bus
*bus
, const char *session_id
, sd_bus_error
*error
, char **path
) {
64 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
68 r
= sd_bus_call_method(
70 "org.freedesktop.login1",
71 "/org/freedesktop/login1",
72 "org.freedesktop.login1.Manager",
79 r
= sd_bus_message_read(reply
, "o", &ans
);
91 static int show_table(Table
*table
, const char *word
) {
97 if (table_get_rows(table
) > 1) {
98 r
= table_set_sort(table
, (size_t) 0, (size_t) -1);
100 return log_error_errno(r
, "Failed to sort table: %m");
102 table_set_header(table
, arg_legend
);
104 if (OUTPUT_MODE_IS_JSON(arg_output
))
105 r
= table_print_json(table
, NULL
, output_mode_to_json_format_flags(arg_output
) | JSON_FORMAT_COLOR_AUTO
);
107 r
= table_print(table
, NULL
);
109 return log_error_errno(r
, "Failed to show table: %m");
113 if (table_get_rows(table
) > 1)
114 printf("\n%zu %s listed.\n", table_get_rows(table
) - 1, word
);
116 printf("No %s.\n", word
);
122 static int list_sessions(int argc
, char *argv
[], void *userdata
) {
123 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
124 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
125 _cleanup_(table_unrefp
) Table
*table
= NULL
;
126 sd_bus
*bus
= userdata
;
132 (void) pager_open(arg_pager_flags
);
134 r
= sd_bus_call_method(
136 "org.freedesktop.login1",
137 "/org/freedesktop/login1",
138 "org.freedesktop.login1.Manager",
143 return log_error_errno(r
, "Failed to list sessions: %s", bus_error_message(&error
, r
));
145 r
= sd_bus_message_enter_container(reply
, 'a', "(susso)");
147 return bus_log_parse_error(r
);
149 table
= table_new("session", "uid", "user", "seat", "tty");
153 /* Right-align the first two fields (since they are numeric) */
154 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
155 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(1), 100);
158 _cleanup_(sd_bus_error_free
) sd_bus_error error_tty
= SD_BUS_ERROR_NULL
;
159 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply_tty
= NULL
;
160 const char *id
, *user
, *seat
, *object
, *tty
= NULL
;
163 r
= sd_bus_message_read(reply
, "(susso)", &id
, &uid
, &user
, &seat
, &object
);
165 return bus_log_parse_error(r
);
169 r
= sd_bus_get_property(
171 "org.freedesktop.login1",
173 "org.freedesktop.login1.Session",
179 log_warning_errno(r
, "Failed to get TTY for session %s: %s", id
, bus_error_message(&error_tty
, r
));
181 r
= sd_bus_message_read(reply_tty
, "s", &tty
);
183 return bus_log_parse_error(r
);
186 r
= table_add_many(table
,
191 TABLE_STRING
, strna(tty
));
193 return log_error_errno(r
, "Failed to add row to table: %m");
196 r
= sd_bus_message_exit_container(reply
);
198 return bus_log_parse_error(r
);
200 return show_table(table
, "sessions");
203 static int list_users(int argc
, char *argv
[], void *userdata
) {
204 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
205 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
206 _cleanup_(table_unrefp
) Table
*table
= NULL
;
207 sd_bus
*bus
= userdata
;
213 (void) pager_open(arg_pager_flags
);
215 r
= sd_bus_call_method(
217 "org.freedesktop.login1",
218 "/org/freedesktop/login1",
219 "org.freedesktop.login1.Manager",
224 return log_error_errno(r
, "Failed to list users: %s", bus_error_message(&error
, r
));
226 r
= sd_bus_message_enter_container(reply
, 'a', "(uso)");
228 return bus_log_parse_error(r
);
230 table
= table_new("uid", "user");
234 (void) table_set_align_percent(table
, TABLE_HEADER_CELL(0), 100);
240 r
= sd_bus_message_read(reply
, "(uso)", &uid
, &user
, NULL
);
242 return bus_log_parse_error(r
);
246 r
= table_add_many(table
,
250 return log_error_errno(r
, "Failed to add row to table: %m");
253 r
= sd_bus_message_exit_container(reply
);
255 return bus_log_parse_error(r
);
257 return show_table(table
, "users");
260 static int list_seats(int argc
, char *argv
[], void *userdata
) {
261 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
262 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
263 _cleanup_(table_unrefp
) Table
*table
= NULL
;
264 sd_bus
*bus
= userdata
;
270 (void) pager_open(arg_pager_flags
);
272 r
= sd_bus_call_method(
274 "org.freedesktop.login1",
275 "/org/freedesktop/login1",
276 "org.freedesktop.login1.Manager",
281 return log_error_errno(r
, "Failed to list seats: %s", bus_error_message(&error
, r
));
283 r
= sd_bus_message_enter_container(reply
, 'a', "(so)");
285 return bus_log_parse_error(r
);
287 table
= table_new("seat");
294 r
= sd_bus_message_read(reply
, "(so)", &seat
, NULL
);
296 return bus_log_parse_error(r
);
300 r
= table_add_cell(table
, NULL
, TABLE_STRING
, seat
);
302 return log_error_errno(r
, "Failed to add row to table: %m");
305 r
= sd_bus_message_exit_container(reply
);
307 return bus_log_parse_error(r
);
309 return show_table(table
, "seats");
312 static int show_unit_cgroup(sd_bus
*bus
, const char *interface
, const char *unit
, pid_t leader
) {
313 _cleanup_free_
char *cgroup
= NULL
;
314 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
321 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
334 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
337 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
340 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
342 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
345 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
347 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
352 typedef struct SessionStatusInfo
{
356 struct dual_timestamp timestamp
;
362 const char *remote_host
;
363 const char *remote_user
;
373 typedef struct UserStatusInfo
{
377 struct dual_timestamp timestamp
;
384 typedef struct SeatStatusInfo
{
386 const char *active_session
;
390 static void user_status_info_clear(UserStatusInfo
*info
) {
392 strv_free(info
->sessions
);
397 static void seat_status_info_clear(SeatStatusInfo
*info
) {
399 strv_free(info
->sessions
);
404 static int prop_map_first_of_struct(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
405 const char *contents
;
408 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
412 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, contents
);
416 r
= sd_bus_message_read_basic(m
, contents
[0], userdata
);
420 r
= sd_bus_message_skip(m
, contents
+1);
424 r
= sd_bus_message_exit_container(m
);
431 static int prop_map_sessions_strv(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
438 r
= sd_bus_message_enter_container(m
, 'a', "(so)");
442 while ((r
= sd_bus_message_read(m
, "(so)", &name
, NULL
)) > 0) {
443 r
= strv_extend(userdata
, name
);
450 return sd_bus_message_exit_container(m
);
453 static int print_session_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
455 static const struct bus_properties_map map
[] = {
456 { "Id", "s", NULL
, offsetof(SessionStatusInfo
, id
) },
457 { "Name", "s", NULL
, offsetof(SessionStatusInfo
, name
) },
458 { "TTY", "s", NULL
, offsetof(SessionStatusInfo
, tty
) },
459 { "Display", "s", NULL
, offsetof(SessionStatusInfo
, display
) },
460 { "RemoteHost", "s", NULL
, offsetof(SessionStatusInfo
, remote_host
) },
461 { "RemoteUser", "s", NULL
, offsetof(SessionStatusInfo
, remote_user
) },
462 { "Service", "s", NULL
, offsetof(SessionStatusInfo
, service
) },
463 { "Desktop", "s", NULL
, offsetof(SessionStatusInfo
, desktop
) },
464 { "Type", "s", NULL
, offsetof(SessionStatusInfo
, type
) },
465 { "Class", "s", NULL
, offsetof(SessionStatusInfo
, class) },
466 { "Scope", "s", NULL
, offsetof(SessionStatusInfo
, scope
) },
467 { "State", "s", NULL
, offsetof(SessionStatusInfo
, state
) },
468 { "VTNr", "u", NULL
, offsetof(SessionStatusInfo
, vtnr
) },
469 { "Leader", "u", NULL
, offsetof(SessionStatusInfo
, leader
) },
470 { "Remote", "b", NULL
, offsetof(SessionStatusInfo
, remote
) },
471 { "Timestamp", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.realtime
) },
472 { "TimestampMonotonic", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.monotonic
) },
473 { "User", "(uo)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, uid
) },
474 { "Seat", "(so)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, seat
) },
478 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
479 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
480 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
481 char since2
[FORMAT_TIMESTAMP_MAX
];
483 SessionStatusInfo i
= {};
486 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
488 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
495 printf("%s - ", strna(i
.id
));
498 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
500 printf("%"PRIu32
"\n", i
.uid
);
502 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
503 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
506 printf("\t Since: %s; %s\n", s2
, s1
);
508 printf("\t Since: %s\n", s2
);
511 _cleanup_free_
char *t
= NULL
;
513 printf("\t Leader: %"PRIu32
, i
.leader
);
515 get_process_comm(i
.leader
, &t
);
522 if (!isempty(i
.seat
)) {
523 printf("\t Seat: %s", i
.seat
);
526 printf("; vc%u", i
.vtnr
);
532 printf("\t TTY: %s\n", i
.tty
);
534 printf("\t Display: %s\n", i
.display
);
536 if (i
.remote_host
&& i
.remote_user
)
537 printf("\t Remote: %s@%s\n", i
.remote_user
, i
.remote_host
);
538 else if (i
.remote_host
)
539 printf("\t Remote: %s\n", i
.remote_host
);
540 else if (i
.remote_user
)
541 printf("\t Remote: user %s\n", i
.remote_user
);
543 printf("\t Remote: Yes\n");
546 printf("\t Service: %s", i
.service
);
549 printf("; type %s", i
.type
);
552 printf("; class %s", i
.class);
556 printf("\t Type: %s", i
.type
);
559 printf("; class %s", i
.class);
563 printf("\t Class: %s\n", i
.class);
565 if (!isempty(i
.desktop
))
566 printf("\t Desktop: %s\n", i
.desktop
);
569 printf("\t State: %s\n", i
.state
);
572 printf("\t Unit: %s\n", i
.scope
);
573 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Scope", i
.scope
, i
.leader
);
575 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
577 show_journal_by_unit(
582 i
.timestamp
.monotonic
,
585 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
586 SD_JOURNAL_LOCAL_ONLY
,
595 static int print_user_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
597 static const struct bus_properties_map map
[] = {
598 { "Name", "s", NULL
, offsetof(UserStatusInfo
, name
) },
599 { "Linger", "b", NULL
, offsetof(UserStatusInfo
, linger
) },
600 { "Slice", "s", NULL
, offsetof(UserStatusInfo
, slice
) },
601 { "State", "s", NULL
, offsetof(UserStatusInfo
, state
) },
602 { "UID", "u", NULL
, offsetof(UserStatusInfo
, uid
) },
603 { "Timestamp", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.realtime
) },
604 { "TimestampMonotonic", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.monotonic
) },
605 { "Display", "(so)", prop_map_first_of_struct
, offsetof(UserStatusInfo
, display
) },
606 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(UserStatusInfo
, sessions
) },
610 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
611 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
612 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
613 char since2
[FORMAT_TIMESTAMP_MAX
];
615 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
618 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, BUS_MAP_BOOLEAN_AS_BOOL
, &error
, &m
, &i
);
620 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
628 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
630 printf("%"PRIu32
"\n", i
.uid
);
632 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
633 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
636 printf("\t Since: %s; %s\n", s2
, s1
);
638 printf("\t Since: %s\n", s2
);
640 if (!isempty(i
.state
))
641 printf("\t State: %s\n", i
.state
);
643 if (!strv_isempty(i
.sessions
)) {
645 printf("\tSessions:");
647 STRV_FOREACH(l
, i
.sessions
)
649 streq_ptr(*l
, i
.display
) ? "*" : "",
655 printf("\t Linger: %s\n", yes_no(i
.linger
));
658 printf("\t Unit: %s\n", i
.slice
);
659 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Slice", i
.slice
, 0);
661 show_journal_by_unit(
666 i
.timestamp
.monotonic
,
669 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
670 SD_JOURNAL_LOCAL_ONLY
,
678 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
680 static const struct bus_properties_map map
[] = {
681 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
682 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
683 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
687 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
688 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
689 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
692 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, 0, &error
, &m
, &i
);
694 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
701 printf("%s\n", strna(i
.id
));
703 if (!strv_isempty(i
.sessions
)) {
705 printf("\tSessions:");
707 STRV_FOREACH(l
, i
.sessions
) {
708 if (streq_ptr(*l
, i
.active_session
))
717 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
726 printf("\t Devices:\n");
728 show_sysfs(i
.id
, "\t\t ", c
, get_output_flags());
734 static int print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
736 const char *contents
;
742 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
748 case SD_BUS_TYPE_STRUCT
:
750 if (contents
[0] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
753 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
755 return bus_log_parse_error(r
);
757 if (all
|| !isempty(s
))
758 bus_print_property_value(name
, expected_value
, value
, s
);
762 } else if (contents
[0] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
765 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
767 return bus_log_parse_error(r
);
769 if (!uid_is_valid(uid
))
770 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
771 "Invalid user ID: " UID_FMT
,
774 bus_print_property_valuef(name
, expected_value
, value
, UID_FMT
, uid
);
779 case SD_BUS_TYPE_ARRAY
:
781 if (contents
[0] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
785 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
787 return bus_log_parse_error(r
);
792 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
793 printf("%s%s", space
? " " : "", s
);
801 return bus_log_parse_error(r
);
803 r
= sd_bus_message_exit_container(m
);
805 return bus_log_parse_error(r
);
815 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
827 r
= bus_print_all_properties(bus
, "org.freedesktop.login1", path
, print_property
, arg_property
, arg_value
, arg_all
, NULL
);
829 return bus_log_parse_error(r
);
834 static int show_session(int argc
, char *argv
[], void *userdata
) {
835 bool properties
, new_line
= false;
836 sd_bus
*bus
= userdata
;
838 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
839 _cleanup_free_
char *path
= NULL
;
844 properties
= !strstr(argv
[0], "status");
846 (void) pager_open(arg_pager_flags
);
849 const char *session
, *p
= "/org/freedesktop/login1/session/self";
852 /* If no argument is specified inspect the manager itself */
853 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
855 /* And in the pretty case, show data of the calling session */
856 session
= getenv("XDG_SESSION_ID");
858 r
= get_session_path(bus
, session
, &error
, &path
);
860 return log_error_errno(r
, "Failed to get session path: %s", bus_error_message(&error
, r
));
865 return print_session_status_info(bus
, p
, &new_line
);
868 for (i
= 1; i
< argc
; i
++) {
869 r
= get_session_path(bus
, argv
[i
], &error
, &path
);
871 return log_error_errno(r
, "Failed to get session path: %s", bus_error_message(&error
, r
));
874 r
= show_properties(bus
, path
, &new_line
);
876 r
= print_session_status_info(bus
, path
, &new_line
);
885 static int show_user(int argc
, char *argv
[], void *userdata
) {
886 bool properties
, new_line
= false;
887 sd_bus
*bus
= userdata
;
893 properties
= !strstr(argv
[0], "status");
895 (void) pager_open(arg_pager_flags
);
898 /* If not argument is specified inspect the manager
901 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
903 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
906 for (i
= 1; i
< argc
; i
++) {
907 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
908 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
909 const char *path
= NULL
;
912 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
914 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
916 r
= sd_bus_call_method(
918 "org.freedesktop.login1",
919 "/org/freedesktop/login1",
920 "org.freedesktop.login1.Manager",
923 "u", (uint32_t) uid
);
925 return log_error_errno(r
, "Failed to get user: %s", bus_error_message(&error
, r
));
927 r
= sd_bus_message_read(reply
, "o", &path
);
929 return bus_log_parse_error(r
);
932 r
= show_properties(bus
, path
, &new_line
);
934 r
= print_user_status_info(bus
, path
, &new_line
);
943 static int show_seat(int argc
, char *argv
[], void *userdata
) {
944 bool properties
, new_line
= false;
945 sd_bus
*bus
= userdata
;
951 properties
= !strstr(argv
[0], "status");
953 (void) pager_open(arg_pager_flags
);
956 /* If not argument is specified inspect the manager
959 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
961 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/self", &new_line
);
964 for (i
= 1; i
< argc
; i
++) {
965 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
966 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
967 const char *path
= NULL
;
969 r
= sd_bus_call_method(
971 "org.freedesktop.login1",
972 "/org/freedesktop/login1",
973 "org.freedesktop.login1.Manager",
978 return log_error_errno(r
, "Failed to get seat: %s", bus_error_message(&error
, r
));
980 r
= sd_bus_message_read(reply
, "o", &path
);
982 return bus_log_parse_error(r
);
985 r
= show_properties(bus
, path
, &new_line
);
987 r
= print_seat_status_info(bus
, path
, &new_line
);
996 static int activate(int argc
, char *argv
[], void *userdata
) {
997 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
998 sd_bus
*bus
= userdata
;
1005 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1008 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1009 * session name, in which case logind will try to guess our session. */
1011 short_argv
[0] = argv
[0];
1012 short_argv
[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1013 short_argv
[2] = NULL
;
1019 for (i
= 1; i
< argc
; i
++) {
1021 r
= sd_bus_call_method(
1023 "org.freedesktop.login1",
1024 "/org/freedesktop/login1",
1025 "org.freedesktop.login1.Manager",
1026 streq(argv
[0], "lock-session") ? "LockSession" :
1027 streq(argv
[0], "unlock-session") ? "UnlockSession" :
1028 streq(argv
[0], "terminate-session") ? "TerminateSession" :
1033 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, -r
));
1039 static int kill_session(int argc
, char *argv
[], void *userdata
) {
1040 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1041 sd_bus
*bus
= userdata
;
1047 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1050 arg_kill_who
= "all";
1052 for (i
= 1; i
< argc
; i
++) {
1054 r
= sd_bus_call_method(
1056 "org.freedesktop.login1",
1057 "/org/freedesktop/login1",
1058 "org.freedesktop.login1.Manager",
1061 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1063 return log_error_errno(r
, "Could not kill session: %s", bus_error_message(&error
, -r
));
1069 static int enable_linger(int argc
, char *argv
[], void *userdata
) {
1070 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1071 sd_bus
*bus
= userdata
;
1072 char* short_argv
[3];
1079 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1081 b
= streq(argv
[0], "enable-linger");
1084 /* No argument? Let's use an empty user name,
1085 * then logind will use our user. */
1087 short_argv
[0] = argv
[0];
1088 short_argv
[1] = (char*) "";
1089 short_argv
[2] = NULL
;
1094 for (i
= 1; i
< argc
; i
++) {
1097 if (isempty(argv
[i
]))
1100 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1102 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1105 r
= sd_bus_call_method(
1107 "org.freedesktop.login1",
1108 "/org/freedesktop/login1",
1109 "org.freedesktop.login1.Manager",
1112 "ubb", (uint32_t) uid
, b
, true);
1114 return log_error_errno(r
, "Could not enable linger: %s", bus_error_message(&error
, -r
));
1120 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1121 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1122 sd_bus
*bus
= userdata
;
1128 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1130 for (i
= 1; i
< argc
; i
++) {
1133 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1135 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1137 r
= sd_bus_call_method(
1139 "org.freedesktop.login1",
1140 "/org/freedesktop/login1",
1141 "org.freedesktop.login1.Manager",
1144 "u", (uint32_t) uid
);
1146 return log_error_errno(r
, "Could not terminate user: %s", bus_error_message(&error
, -r
));
1152 static int kill_user(int argc
, char *argv
[], void *userdata
) {
1153 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1154 sd_bus
*bus
= userdata
;
1160 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1163 arg_kill_who
= "all";
1165 for (i
= 1; i
< argc
; i
++) {
1168 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
, 0);
1170 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1172 r
= sd_bus_call_method(
1174 "org.freedesktop.login1",
1175 "/org/freedesktop/login1",
1176 "org.freedesktop.login1.Manager",
1179 "ui", (uint32_t) uid
, arg_signal
);
1181 return log_error_errno(r
, "Could not kill user: %s", bus_error_message(&error
, -r
));
1187 static int attach(int argc
, char *argv
[], void *userdata
) {
1188 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1189 sd_bus
*bus
= userdata
;
1195 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1197 for (i
= 2; i
< argc
; i
++) {
1199 r
= sd_bus_call_method(
1201 "org.freedesktop.login1",
1202 "/org/freedesktop/login1",
1203 "org.freedesktop.login1.Manager",
1206 "ssb", argv
[1], argv
[i
], true);
1209 return log_error_errno(r
, "Could not attach device: %s", bus_error_message(&error
, -r
));
1215 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1216 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1217 sd_bus
*bus
= userdata
;
1223 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1225 r
= sd_bus_call_method(
1227 "org.freedesktop.login1",
1228 "/org/freedesktop/login1",
1229 "org.freedesktop.login1.Manager",
1234 return log_error_errno(r
, "Could not flush devices: %s", bus_error_message(&error
, -r
));
1239 static int lock_sessions(int argc
, char *argv
[], void *userdata
) {
1240 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1241 sd_bus
*bus
= userdata
;
1247 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1249 r
= sd_bus_call_method(
1251 "org.freedesktop.login1",
1252 "/org/freedesktop/login1",
1253 "org.freedesktop.login1.Manager",
1254 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1258 return log_error_errno(r
, "Could not lock sessions: %s", bus_error_message(&error
, -r
));
1263 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1264 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1265 sd_bus
*bus
= userdata
;
1271 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1273 for (i
= 1; i
< argc
; i
++) {
1275 r
= sd_bus_call_method(
1277 "org.freedesktop.login1",
1278 "/org/freedesktop/login1",
1279 "org.freedesktop.login1.Manager",
1284 return log_error_errno(r
, "Could not terminate seat: %s", bus_error_message(&error
, -r
));
1290 static int help(int argc
, char *argv
[], void *userdata
) {
1291 _cleanup_free_
char *link
= NULL
;
1294 (void) pager_open(arg_pager_flags
);
1296 r
= terminal_urlify_man("loginctl", "1", &link
);
1300 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1301 "Send control commands to or query the login manager.\n\n"
1302 " -h --help Show this help\n"
1303 " --version Show package version\n"
1304 " --no-pager Do not pipe output into a pager\n"
1305 " --no-legend Do not show the headers and footers\n"
1306 " --no-ask-password Don't prompt for password\n"
1307 " -H --host=[USER@]HOST Operate on remote host\n"
1308 " -M --machine=CONTAINER Operate on local container\n"
1309 " -p --property=NAME Show only properties by this name\n"
1310 " -a --all Show all properties, including empty ones\n"
1311 " --value When showing properties, only print the value\n"
1312 " -l --full Do not ellipsize output\n"
1313 " --kill-who=WHO Who to send signal to\n"
1314 " -s --signal=SIGNAL Which signal to send\n"
1315 " -n --lines=INTEGER Number of journal entries to show\n"
1316 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1317 " short-iso, short-iso-precise, short-full,\n"
1318 " short-monotonic, short-unix, verbose, export,\n"
1319 " json, json-pretty, json-sse, json-seq, cat,\n"
1321 "Session Commands:\n"
1322 " list-sessions List sessions\n"
1323 " session-status [ID...] Show session status\n"
1324 " show-session [ID...] Show properties of sessions or the manager\n"
1325 " activate [ID] Activate a session\n"
1326 " lock-session [ID...] Screen lock one or more sessions\n"
1327 " unlock-session [ID...] Screen unlock one or more sessions\n"
1328 " lock-sessions Screen lock all current sessions\n"
1329 " unlock-sessions Screen unlock all current sessions\n"
1330 " terminate-session ID... Terminate one or more sessions\n"
1331 " kill-session ID... Send signal to processes of a session\n\n"
1333 " list-users List users\n"
1334 " user-status [USER...] Show user status\n"
1335 " show-user [USER...] Show properties of users or the manager\n"
1336 " enable-linger [USER...] Enable linger state of one or more users\n"
1337 " disable-linger [USER...] Disable linger state of one or more users\n"
1338 " terminate-user USER... Terminate all sessions of one or more users\n"
1339 " kill-user USER... Send signal to processes of a user\n\n"
1341 " list-seats List seats\n"
1342 " seat-status [NAME...] Show seat status\n"
1343 " show-seat [NAME...] Show properties of seats or the manager\n"
1344 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1345 " flush-devices Flush all device associations\n"
1346 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1347 "\nSee the %s for details.\n"
1348 , program_invocation_short_name
1355 static int parse_argv(int argc
, char *argv
[]) {
1358 ARG_VERSION
= 0x100,
1363 ARG_NO_ASK_PASSWORD
,
1366 static const struct option options
[] = {
1367 { "help", no_argument
, NULL
, 'h' },
1368 { "version", no_argument
, NULL
, ARG_VERSION
},
1369 { "property", required_argument
, NULL
, 'p' },
1370 { "all", no_argument
, NULL
, 'a' },
1371 { "value", no_argument
, NULL
, ARG_VALUE
},
1372 { "full", no_argument
, NULL
, 'l' },
1373 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1374 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1375 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1376 { "signal", required_argument
, NULL
, 's' },
1377 { "host", required_argument
, NULL
, 'H' },
1378 { "machine", required_argument
, NULL
, 'M' },
1379 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1380 { "lines", required_argument
, NULL
, 'n' },
1381 { "output", required_argument
, NULL
, 'o' },
1390 while ((c
= getopt_long(argc
, argv
, "hp:als:H:M:n:o:", options
, NULL
)) >= 0)
1395 return help(0, NULL
, NULL
);
1401 r
= strv_extend(&arg_property
, optarg
);
1405 /* If the user asked for a particular
1406 * property, show it to him, even if it is
1425 if (safe_atou(optarg
, &arg_lines
) < 0)
1426 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1427 "Failed to parse lines '%s'", optarg
);
1431 if (streq(optarg
, "help")) {
1432 DUMP_STRING_TABLE(output_mode
, OutputMode
, _OUTPUT_MODE_MAX
);
1436 arg_output
= output_mode_from_string(optarg
);
1438 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1439 "Unknown output '%s'.", optarg
);
1441 if (OUTPUT_MODE_IS_JSON(arg_output
))
1447 arg_pager_flags
|= PAGER_DISABLE
;
1454 case ARG_NO_ASK_PASSWORD
:
1455 arg_ask_password
= false;
1459 arg_kill_who
= optarg
;
1463 if (streq(optarg
, "help")) {
1464 DUMP_STRING_TABLE(signal
, int, _NSIG
);
1468 arg_signal
= signal_from_string(optarg
);
1470 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1471 "Failed to parse signal string %s.", optarg
);
1475 arg_transport
= BUS_TRANSPORT_REMOTE
;
1480 arg_transport
= BUS_TRANSPORT_MACHINE
;
1488 assert_not_reached("Unhandled option");
1494 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1496 static const Verb verbs
[] = {
1497 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1498 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1499 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1500 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1501 { "activate", VERB_ANY
, 2, 0, activate
},
1502 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1503 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1504 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1505 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1506 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1507 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1508 { "list-users", VERB_ANY
, 1, 0, list_users
},
1509 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1510 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1511 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1512 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1513 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1514 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1515 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1516 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1517 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1518 { "attach", 3, VERB_ANY
, 0, attach
},
1519 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1520 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1524 return dispatch_verb(argc
, argv
, verbs
, bus
);
1527 static int run(int argc
, char *argv
[]) {
1528 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1531 setlocale(LC_ALL
, "");
1532 log_parse_environment();
1535 /* The journal merging logic potentially needs a lot of fds. */
1536 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE
);
1540 r
= parse_argv(argc
, argv
);
1544 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1546 return log_error_errno(r
, "Failed to create bus connection: %m");
1548 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1550 return loginctl_main(argc
, argv
, bus
);
1553 DEFINE_MAIN_FUNCTION(run
);