2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "alloc-util.h"
29 #include "bus-error.h"
30 #include "bus-unit-util.h"
32 #include "cgroup-show.h"
33 #include "cgroup-util.h"
35 #include "logs-show.h"
38 #include "parse-util.h"
39 #include "process-util.h"
40 #include "signal-util.h"
41 #include "spawn-polkit-agent.h"
43 #include "sysfs-show.h"
44 #include "terminal-util.h"
45 #include "unit-name.h"
46 #include "user-util.h"
50 static char **arg_property
= NULL
;
51 static bool arg_all
= false;
52 static bool arg_value
= false;
53 static bool arg_full
= false;
54 static bool arg_no_pager
= false;
55 static bool arg_legend
= true;
56 static const char *arg_kill_who
= NULL
;
57 static int arg_signal
= SIGTERM
;
58 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
59 static char *arg_host
= NULL
;
60 static bool arg_ask_password
= true;
61 static unsigned arg_lines
= 10;
62 static OutputMode arg_output
= OUTPUT_SHORT
;
64 static void polkit_agent_open_if_enabled(void) {
66 /* Open the polkit agent as a child process if necessary */
68 if (!arg_ask_password
)
71 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
77 static OutputFlags
get_output_flags(void) {
80 arg_all
* OUTPUT_SHOW_ALL
|
81 arg_full
* OUTPUT_FULL_WIDTH
|
82 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
83 colors_enabled() * OUTPUT_COLOR
;
86 static int get_session_path(sd_bus
*bus
, const char *session_id
, sd_bus_error
*error
, char **path
) {
87 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
91 r
= sd_bus_call_method(
93 "org.freedesktop.login1",
94 "/org/freedesktop/login1",
95 "org.freedesktop.login1.Manager",
102 r
= sd_bus_message_read(reply
, "o", &ans
);
114 static int list_sessions(int argc
, char *argv
[], void *userdata
) {
115 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
116 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
117 const char *id
, *user
, *seat
, *object
;
118 sd_bus
*bus
= userdata
;
126 pager_open(arg_no_pager
, false);
128 r
= sd_bus_call_method(
130 "org.freedesktop.login1",
131 "/org/freedesktop/login1",
132 "org.freedesktop.login1.Manager",
137 log_error("Failed to list sessions: %s", bus_error_message(&error
, r
));
141 r
= sd_bus_message_enter_container(reply
, 'a', "(susso)");
143 return bus_log_parse_error(r
);
146 printf("%10s %10s %-16s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT", "TTY");
148 while ((r
= sd_bus_message_read(reply
, "(susso)", &id
, &uid
, &user
, &seat
, &object
)) > 0) {
149 _cleanup_(sd_bus_error_free
) sd_bus_error error2
= SD_BUS_ERROR_NULL
;
150 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply2
= NULL
;
151 _cleanup_free_
char *path
= NULL
;
152 const char *tty
= NULL
;
154 r
= get_session_path(bus
, id
, &error2
, &path
);
156 log_warning("Failed to get session path: %s", bus_error_message(&error
, r
));
158 r
= sd_bus_get_property(
160 "org.freedesktop.login1",
162 "org.freedesktop.login1.Session",
168 log_warning("Failed to get TTY for session %s: %s",
169 id
, bus_error_message(&error2
, r
));
171 r
= sd_bus_message_read(reply2
, "s", &tty
);
173 return bus_log_parse_error(r
);
177 printf("%10s %10"PRIu32
" %-16s %-16s %-16s\n", id
, uid
, user
, seat
, strna(tty
));
181 return bus_log_parse_error(r
);
184 printf("\n%u sessions listed.\n", k
);
189 static int list_users(int argc
, char *argv
[], void *userdata
) {
190 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
191 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
192 const char *user
, *object
;
193 sd_bus
*bus
= userdata
;
201 pager_open(arg_no_pager
, false);
203 r
= sd_bus_call_method(
205 "org.freedesktop.login1",
206 "/org/freedesktop/login1",
207 "org.freedesktop.login1.Manager",
212 log_error("Failed to list users: %s", bus_error_message(&error
, r
));
216 r
= sd_bus_message_enter_container(reply
, 'a', "(uso)");
218 return bus_log_parse_error(r
);
221 printf("%10s %-16s\n", "UID", "USER");
223 while ((r
= sd_bus_message_read(reply
, "(uso)", &uid
, &user
, &object
)) > 0) {
224 printf("%10"PRIu32
" %-16s\n", uid
, user
);
228 return bus_log_parse_error(r
);
231 printf("\n%u users listed.\n", k
);
236 static int list_seats(int argc
, char *argv
[], void *userdata
) {
237 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
238 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
239 const char *seat
, *object
;
240 sd_bus
*bus
= userdata
;
246 pager_open(arg_no_pager
, false);
248 r
= sd_bus_call_method(
250 "org.freedesktop.login1",
251 "/org/freedesktop/login1",
252 "org.freedesktop.login1.Manager",
257 log_error("Failed to list seats: %s", bus_error_message(&error
, r
));
261 r
= sd_bus_message_enter_container(reply
, 'a', "(so)");
263 return bus_log_parse_error(r
);
266 printf("%-16s\n", "SEAT");
268 while ((r
= sd_bus_message_read(reply
, "(so)", &seat
, &object
)) > 0) {
269 printf("%-16s\n", seat
);
273 return bus_log_parse_error(r
);
276 printf("\n%u seats listed.\n", k
);
281 static int show_unit_cgroup(sd_bus
*bus
, const char *interface
, const char *unit
, pid_t leader
) {
282 _cleanup_free_
char *cgroup
= NULL
;
283 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
290 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &cgroup
);
303 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
306 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
309 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
311 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
314 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
316 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
321 typedef struct SessionStatusInfo
{
325 struct dual_timestamp timestamp
;
342 typedef struct UserStatusInfo
{
346 struct dual_timestamp timestamp
;
353 typedef struct SeatStatusInfo
{
355 char *active_session
;
359 static void session_status_info_clear(SessionStatusInfo
*info
) {
366 free(info
->remote_host
);
367 free(info
->remote_user
);
378 static void user_status_info_clear(UserStatusInfo
*info
) {
382 strv_free(info
->sessions
);
389 static void seat_status_info_clear(SeatStatusInfo
*info
) {
392 free(info
->active_session
);
393 strv_free(info
->sessions
);
398 static int prop_map_first_of_struct(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
399 const char *contents
;
402 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
406 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, contents
);
410 if (contents
[0] == 's' || contents
[0] == 'o') {
412 char **p
= (char **) userdata
;
414 r
= sd_bus_message_read_basic(m
, contents
[0], &s
);
418 r
= free_and_strdup(p
, s
);
422 r
= sd_bus_message_read_basic(m
, contents
[0], userdata
);
427 r
= sd_bus_message_skip(m
, contents
+1);
431 r
= sd_bus_message_exit_container(m
);
438 static int prop_map_sessions_strv(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
445 r
= sd_bus_message_enter_container(m
, 'a', "(so)");
449 while ((r
= sd_bus_message_read(m
, "(so)", &name
, NULL
)) > 0) {
450 r
= strv_extend(userdata
, name
);
457 return sd_bus_message_exit_container(m
);
460 static int print_session_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
462 static const struct bus_properties_map map
[] = {
463 { "Id", "s", NULL
, offsetof(SessionStatusInfo
, id
) },
464 { "Name", "s", NULL
, offsetof(SessionStatusInfo
, name
) },
465 { "TTY", "s", NULL
, offsetof(SessionStatusInfo
, tty
) },
466 { "Display", "s", NULL
, offsetof(SessionStatusInfo
, display
) },
467 { "RemoteHost", "s", NULL
, offsetof(SessionStatusInfo
, remote_host
) },
468 { "RemoteUser", "s", NULL
, offsetof(SessionStatusInfo
, remote_user
) },
469 { "Service", "s", NULL
, offsetof(SessionStatusInfo
, service
) },
470 { "Desktop", "s", NULL
, offsetof(SessionStatusInfo
, desktop
) },
471 { "Type", "s", NULL
, offsetof(SessionStatusInfo
, type
) },
472 { "Class", "s", NULL
, offsetof(SessionStatusInfo
, class) },
473 { "Scope", "s", NULL
, offsetof(SessionStatusInfo
, scope
) },
474 { "State", "s", NULL
, offsetof(SessionStatusInfo
, state
) },
475 { "VTNr", "u", NULL
, offsetof(SessionStatusInfo
, vtnr
) },
476 { "Leader", "u", NULL
, offsetof(SessionStatusInfo
, leader
) },
477 { "Remote", "b", NULL
, offsetof(SessionStatusInfo
, remote
) },
478 { "Timestamp", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.realtime
) },
479 { "TimestampMonotonic", "t", NULL
, offsetof(SessionStatusInfo
, timestamp
.monotonic
) },
480 { "User", "(uo)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, uid
) },
481 { "Seat", "(so)", prop_map_first_of_struct
, offsetof(SessionStatusInfo
, seat
) },
485 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
486 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
487 _cleanup_(session_status_info_clear
) SessionStatusInfo i
= {};
490 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, &i
);
492 return log_error_errno(r
, "Could not get properties: %m");
499 printf("%s - ", strna(i
.id
));
502 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
504 printf("%"PRIu32
"\n", i
.uid
);
506 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
507 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
510 printf("\t Since: %s; %s\n", s2
, s1
);
512 printf("\t Since: %s\n", s2
);
515 _cleanup_free_
char *t
= NULL
;
517 printf("\t Leader: %"PRIu32
, i
.leader
);
519 get_process_comm(i
.leader
, &t
);
526 if (!isempty(i
.seat
)) {
527 printf("\t Seat: %s", i
.seat
);
530 printf("; vc%u", i
.vtnr
);
536 printf("\t TTY: %s\n", i
.tty
);
538 printf("\t Display: %s\n", i
.display
);
540 if (i
.remote_host
&& i
.remote_user
)
541 printf("\t Remote: %s@%s\n", i
.remote_user
, i
.remote_host
);
542 else if (i
.remote_host
)
543 printf("\t Remote: %s\n", i
.remote_host
);
544 else if (i
.remote_user
)
545 printf("\t Remote: user %s\n", i
.remote_user
);
547 printf("\t Remote: Yes\n");
550 printf("\t Service: %s", i
.service
);
553 printf("; type %s", i
.type
);
556 printf("; class %s", i
.class);
560 printf("\t Type: %s", i
.type
);
563 printf("; class %s", i
.class);
567 printf("\t Class: %s\n", i
.class);
569 if (!isempty(i
.desktop
))
570 printf("\t Desktop: %s\n", i
.desktop
);
573 printf("\t State: %s\n", i
.state
);
576 printf("\t Unit: %s\n", i
.scope
);
577 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Scope", i
.scope
, i
.leader
);
579 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
581 show_journal_by_unit(
586 i
.timestamp
.monotonic
,
589 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
590 SD_JOURNAL_LOCAL_ONLY
,
599 static int print_user_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
601 static const struct bus_properties_map map
[] = {
602 { "Name", "s", NULL
, offsetof(UserStatusInfo
, name
) },
603 { "Linger", "b", NULL
, offsetof(UserStatusInfo
, linger
) },
604 { "Slice", "s", NULL
, offsetof(UserStatusInfo
, slice
) },
605 { "State", "s", NULL
, offsetof(UserStatusInfo
, state
) },
606 { "UID", "u", NULL
, offsetof(UserStatusInfo
, uid
) },
607 { "Timestamp", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.realtime
) },
608 { "TimestampMonotonic", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.monotonic
) },
609 { "Display", "(so)", prop_map_first_of_struct
, offsetof(UserStatusInfo
, display
) },
610 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(UserStatusInfo
, sessions
) },
614 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
615 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
616 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
619 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, &i
);
621 return log_error_errno(r
, "Could not get properties: %m");
629 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
631 printf("%"PRIu32
"\n", i
.uid
);
633 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
634 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
637 printf("\t Since: %s; %s\n", s2
, s1
);
639 printf("\t Since: %s\n", s2
);
641 if (!isempty(i
.state
))
642 printf("\t State: %s\n", i
.state
);
644 if (!strv_isempty(i
.sessions
)) {
646 printf("\tSessions:");
648 STRV_FOREACH(l
, i
.sessions
)
650 streq_ptr(*l
, i
.display
) ? "*" : "",
656 printf("\t Linger: %s\n", yes_no(i
.linger
));
659 printf("\t Unit: %s\n", i
.slice
);
660 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Slice", i
.slice
, 0);
662 show_journal_by_unit(
667 i
.timestamp
.monotonic
,
670 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
671 SD_JOURNAL_LOCAL_ONLY
,
679 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
681 static const struct bus_properties_map map
[] = {
682 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
683 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
684 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
688 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
691 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, &i
);
693 return log_error_errno(r
, "Could not get properties: %m");
700 printf("%s\n", strna(i
.id
));
702 if (!strv_isempty(i
.sessions
)) {
704 printf("\tSessions:");
706 STRV_FOREACH(l
, i
.sessions
) {
707 if (streq_ptr(*l
, i
.active_session
))
716 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
725 printf("\t Devices:\n");
727 show_sysfs(i
.id
, "\t\t ", c
);
733 #define property(name, fmt, ...) \
736 printf(fmt "\n", __VA_ARGS__); \
738 printf("%s=" fmt "\n", name, __VA_ARGS__); \
741 static int print_property(const char *name
, sd_bus_message
*m
, const char *contents
) {
748 if (arg_property
&& !strv_find(arg_property
, name
))
749 /* skip what we didn't read */
750 return sd_bus_message_skip(m
, contents
);
752 switch (contents
[0]) {
754 case SD_BUS_TYPE_STRUCT_BEGIN
:
756 if (contents
[1] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
759 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
761 return bus_log_parse_error(r
);
763 if (arg_all
|| !isempty(s
))
764 property(name
, "%s", s
);
768 } else if (contents
[1] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
771 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
773 return bus_log_parse_error(r
);
775 if (!uid_is_valid(uid
)) {
776 log_error("Invalid user ID: " UID_FMT
, uid
);
780 property(name
, UID_FMT
, uid
);
786 case SD_BUS_TYPE_ARRAY
:
788 if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
792 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
794 return bus_log_parse_error(r
);
799 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
800 printf("%s%s", space
? " " : "", s
);
804 if (space
|| !arg_value
)
808 return bus_log_parse_error(r
);
810 r
= sd_bus_message_exit_container(m
);
812 return bus_log_parse_error(r
);
820 r
= bus_print_property(name
, m
, arg_value
, arg_all
);
822 return bus_log_parse_error(r
);
825 r
= sd_bus_message_skip(m
, contents
);
827 return bus_log_parse_error(r
);
830 printf("%s=[unprintable]\n", name
);
836 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
837 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
838 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
845 r
= sd_bus_call_method(
847 "org.freedesktop.login1",
849 "org.freedesktop.DBus.Properties",
855 return log_error_errno(r
, "Failed to get properties: %s", bus_error_message(&error
, r
));
857 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "{sv}");
859 return bus_log_parse_error(r
);
866 while ((r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
867 const char *name
, *contents
;
869 r
= sd_bus_message_read(reply
, "s", &name
);
871 return bus_log_parse_error(r
);
873 r
= sd_bus_message_peek_type(reply
, NULL
, &contents
);
875 return bus_log_parse_error(r
);
877 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_VARIANT
, contents
);
879 return bus_log_parse_error(r
);
881 r
= print_property(name
, reply
, contents
);
885 r
= sd_bus_message_exit_container(reply
);
887 return bus_log_parse_error(r
);
889 r
= sd_bus_message_exit_container(reply
);
891 return bus_log_parse_error(r
);
894 return bus_log_parse_error(r
);
896 r
= sd_bus_message_exit_container(reply
);
898 return bus_log_parse_error(r
);
903 static int show_session(int argc
, char *argv
[], void *userdata
) {
904 bool properties
, new_line
= false;
905 sd_bus
*bus
= userdata
;
911 properties
= !strstr(argv
[0], "status");
913 pager_open(arg_no_pager
, false);
916 /* If not argument is specified inspect the manager
919 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
921 /* And in the pretty case, show data of the calling session */
922 return print_session_status_info(bus
, "/org/freedesktop/login1/session/self", &new_line
);
925 for (i
= 1; i
< argc
; i
++) {
926 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
927 _cleanup_free_
char *path
= NULL
;
929 r
= get_session_path(bus
, argv
[1], &error
, &path
);
931 log_error("Failed to get session path: %s", bus_error_message(&error
, r
));
936 r
= show_properties(bus
, path
, &new_line
);
938 r
= print_session_status_info(bus
, path
, &new_line
);
947 static int show_user(int argc
, char *argv
[], void *userdata
) {
948 bool properties
, new_line
= false;
949 sd_bus
*bus
= userdata
;
955 properties
= !strstr(argv
[0], "status");
957 pager_open(arg_no_pager
, false);
960 /* If not argument is specified inspect the manager
963 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
965 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
968 for (i
= 1; i
< argc
; i
++) {
969 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
970 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
971 const char *path
= NULL
;
974 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
976 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
978 r
= sd_bus_call_method(
980 "org.freedesktop.login1",
981 "/org/freedesktop/login1",
982 "org.freedesktop.login1.Manager",
985 "u", (uint32_t) uid
);
987 log_error("Failed to get user: %s", bus_error_message(&error
, r
));
991 r
= sd_bus_message_read(reply
, "o", &path
);
993 return bus_log_parse_error(r
);
996 r
= show_properties(bus
, path
, &new_line
);
998 r
= print_user_status_info(bus
, path
, &new_line
);
1007 static int show_seat(int argc
, char *argv
[], void *userdata
) {
1008 bool properties
, new_line
= false;
1009 sd_bus
*bus
= userdata
;
1015 properties
= !strstr(argv
[0], "status");
1017 pager_open(arg_no_pager
, false);
1020 /* If not argument is specified inspect the manager
1023 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
1025 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/self", &new_line
);
1028 for (i
= 1; i
< argc
; i
++) {
1029 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1030 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
1031 const char *path
= NULL
;
1033 r
= sd_bus_call_method(
1035 "org.freedesktop.login1",
1036 "/org/freedesktop/login1",
1037 "org.freedesktop.login1.Manager",
1042 log_error("Failed to get seat: %s", bus_error_message(&error
, r
));
1046 r
= sd_bus_message_read(reply
, "o", &path
);
1048 return bus_log_parse_error(r
);
1051 r
= show_properties(bus
, path
, &new_line
);
1053 r
= print_seat_status_info(bus
, path
, &new_line
);
1062 static int activate(int argc
, char *argv
[], void *userdata
) {
1063 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1064 sd_bus
*bus
= userdata
;
1065 char *short_argv
[3];
1071 polkit_agent_open_if_enabled();
1074 /* No argument? Let's convert this into the empty
1075 * session name, which the calls will then resolve to
1076 * the caller's session. */
1078 short_argv
[0] = argv
[0];
1079 short_argv
[1] = (char*) "";
1080 short_argv
[2] = NULL
;
1086 for (i
= 1; i
< argc
; i
++) {
1088 r
= sd_bus_call_method(
1090 "org.freedesktop.login1",
1091 "/org/freedesktop/login1",
1092 "org.freedesktop.login1.Manager",
1093 streq(argv
[0], "lock-session") ? "LockSession" :
1094 streq(argv
[0], "unlock-session") ? "UnlockSession" :
1095 streq(argv
[0], "terminate-session") ? "TerminateSession" :
1100 log_error("Failed to issue method call: %s", bus_error_message(&error
, -r
));
1108 static int kill_session(int argc
, char *argv
[], void *userdata
) {
1109 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1110 sd_bus
*bus
= userdata
;
1116 polkit_agent_open_if_enabled();
1119 arg_kill_who
= "all";
1121 for (i
= 1; i
< argc
; i
++) {
1123 r
= sd_bus_call_method(
1125 "org.freedesktop.login1",
1126 "/org/freedesktop/login1",
1127 "org.freedesktop.login1.Manager",
1130 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1132 log_error("Could not kill session: %s", bus_error_message(&error
, -r
));
1140 static int enable_linger(int argc
, char *argv
[], void *userdata
) {
1141 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1142 sd_bus
*bus
= userdata
;
1143 char* short_argv
[3];
1150 polkit_agent_open_if_enabled();
1152 b
= streq(argv
[0], "enable-linger");
1155 short_argv
[0] = argv
[0];
1156 short_argv
[1] = (char*) "";
1157 short_argv
[2] = NULL
;
1162 for (i
= 1; i
< argc
; i
++) {
1165 if (isempty(argv
[i
]))
1168 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1170 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1173 r
= sd_bus_call_method(
1175 "org.freedesktop.login1",
1176 "/org/freedesktop/login1",
1177 "org.freedesktop.login1.Manager",
1180 "ubb", (uint32_t) uid
, b
, true);
1182 log_error("Could not enable linger: %s", bus_error_message(&error
, -r
));
1190 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1191 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1192 sd_bus
*bus
= userdata
;
1198 polkit_agent_open_if_enabled();
1200 for (i
= 1; i
< argc
; i
++) {
1203 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1205 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1207 r
= sd_bus_call_method(
1209 "org.freedesktop.login1",
1210 "/org/freedesktop/login1",
1211 "org.freedesktop.login1.Manager",
1214 "u", (uint32_t) uid
);
1216 log_error("Could not terminate user: %s", bus_error_message(&error
, -r
));
1224 static int kill_user(int argc
, char *argv
[], void *userdata
) {
1225 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1226 sd_bus
*bus
= userdata
;
1232 polkit_agent_open_if_enabled();
1235 arg_kill_who
= "all";
1237 for (i
= 1; i
< argc
; i
++) {
1240 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1242 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1244 r
= sd_bus_call_method(
1246 "org.freedesktop.login1",
1247 "/org/freedesktop/login1",
1248 "org.freedesktop.login1.Manager",
1251 "ui", (uint32_t) uid
, arg_signal
);
1253 log_error("Could not kill user: %s", bus_error_message(&error
, -r
));
1261 static int attach(int argc
, char *argv
[], void *userdata
) {
1262 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1263 sd_bus
*bus
= userdata
;
1269 polkit_agent_open_if_enabled();
1271 for (i
= 2; i
< argc
; i
++) {
1273 r
= sd_bus_call_method(
1275 "org.freedesktop.login1",
1276 "/org/freedesktop/login1",
1277 "org.freedesktop.login1.Manager",
1280 "ssb", argv
[1], argv
[i
], true);
1283 log_error("Could not attach device: %s", bus_error_message(&error
, -r
));
1291 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1292 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1293 sd_bus
*bus
= userdata
;
1299 polkit_agent_open_if_enabled();
1301 r
= sd_bus_call_method(
1303 "org.freedesktop.login1",
1304 "/org/freedesktop/login1",
1305 "org.freedesktop.login1.Manager",
1310 log_error("Could not flush devices: %s", bus_error_message(&error
, -r
));
1315 static int lock_sessions(int argc
, char *argv
[], void *userdata
) {
1316 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1317 sd_bus
*bus
= userdata
;
1323 polkit_agent_open_if_enabled();
1325 r
= sd_bus_call_method(
1327 "org.freedesktop.login1",
1328 "/org/freedesktop/login1",
1329 "org.freedesktop.login1.Manager",
1330 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1334 log_error("Could not lock sessions: %s", bus_error_message(&error
, -r
));
1339 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1340 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1341 sd_bus
*bus
= userdata
;
1347 polkit_agent_open_if_enabled();
1349 for (i
= 1; i
< argc
; i
++) {
1351 r
= sd_bus_call_method(
1353 "org.freedesktop.login1",
1354 "/org/freedesktop/login1",
1355 "org.freedesktop.login1.Manager",
1360 log_error("Could not terminate seat: %s", bus_error_message(&error
, -r
));
1368 static int help(int argc
, char *argv
[], void *userdata
) {
1370 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1371 "Send control commands to or query the login manager.\n\n"
1372 " -h --help Show this help\n"
1373 " --version Show package version\n"
1374 " --no-pager Do not pipe output into a pager\n"
1375 " --no-legend Do not show the headers and footers\n"
1376 " --no-ask-password Don't prompt for password\n"
1377 " -H --host=[USER@]HOST Operate on remote host\n"
1378 " -M --machine=CONTAINER Operate on local container\n"
1379 " -p --property=NAME Show only properties by this name\n"
1380 " -a --all Show all properties, including empty ones\n"
1381 " --value When showing properties, only print the value\n"
1382 " -l --full Do not ellipsize output\n"
1383 " --kill-who=WHO Who to send signal to\n"
1384 " -s --signal=SIGNAL Which signal to send\n"
1385 " -n --lines=INTEGER Number of journal entries to show\n"
1386 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
1387 " verbose, export, json, json-pretty, json-sse, cat)\n\n"
1388 "Session Commands:\n"
1389 " list-sessions List sessions\n"
1390 " session-status [ID...] Show session status\n"
1391 " show-session [ID...] Show properties of sessions or the manager\n"
1392 " activate [ID] Activate a session\n"
1393 " lock-session [ID...] Screen lock one or more sessions\n"
1394 " unlock-session [ID...] Screen unlock one or more sessions\n"
1395 " lock-sessions Screen lock all current sessions\n"
1396 " unlock-sessions Screen unlock all current sessions\n"
1397 " terminate-session ID... Terminate one or more sessions\n"
1398 " kill-session ID... Send signal to processes of a session\n\n"
1400 " list-users List users\n"
1401 " user-status [USER...] Show user status\n"
1402 " show-user [USER...] Show properties of users or the manager\n"
1403 " enable-linger [USER...] Enable linger state of one or more users\n"
1404 " disable-linger [USER...] Disable linger state of one or more users\n"
1405 " terminate-user USER... Terminate all sessions of one or more users\n"
1406 " kill-user USER... Send signal to processes of a user\n\n"
1408 " list-seats List seats\n"
1409 " seat-status [NAME...] Show seat status\n"
1410 " show-seat [NAME...] Show properties of seats or the manager\n"
1411 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1412 " flush-devices Flush all device associations\n"
1413 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1414 , program_invocation_short_name
);
1419 static int parse_argv(int argc
, char *argv
[]) {
1422 ARG_VERSION
= 0x100,
1427 ARG_NO_ASK_PASSWORD
,
1430 static const struct option options
[] = {
1431 { "help", no_argument
, NULL
, 'h' },
1432 { "version", no_argument
, NULL
, ARG_VERSION
},
1433 { "property", required_argument
, NULL
, 'p' },
1434 { "all", no_argument
, NULL
, 'a' },
1435 { "value", no_argument
, NULL
, ARG_VALUE
},
1436 { "full", no_argument
, NULL
, 'l' },
1437 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1438 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1439 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1440 { "signal", required_argument
, NULL
, 's' },
1441 { "host", required_argument
, NULL
, 'H' },
1442 { "machine", required_argument
, NULL
, 'M' },
1443 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1444 { "lines", required_argument
, NULL
, 'n' },
1445 { "output", required_argument
, NULL
, 'o' },
1454 while ((c
= getopt_long(argc
, argv
, "hp:als:H:M:n:o:", options
, NULL
)) >= 0)
1459 help(0, NULL
, NULL
);
1466 r
= strv_extend(&arg_property
, optarg
);
1470 /* If the user asked for a particular
1471 * property, show it to him, even if it is
1490 if (safe_atou(optarg
, &arg_lines
) < 0) {
1491 log_error("Failed to parse lines '%s'", optarg
);
1497 arg_output
= output_mode_from_string(optarg
);
1498 if (arg_output
< 0) {
1499 log_error("Unknown output '%s'.", optarg
);
1505 arg_no_pager
= true;
1512 case ARG_NO_ASK_PASSWORD
:
1513 arg_ask_password
= false;
1517 arg_kill_who
= optarg
;
1521 arg_signal
= signal_from_string_try_harder(optarg
);
1522 if (arg_signal
< 0) {
1523 log_error("Failed to parse signal string %s.", optarg
);
1529 arg_transport
= BUS_TRANSPORT_REMOTE
;
1534 arg_transport
= BUS_TRANSPORT_MACHINE
;
1542 assert_not_reached("Unhandled option");
1548 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1550 static const Verb verbs
[] = {
1551 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1552 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1553 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1554 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1555 { "activate", VERB_ANY
, 2, 0, activate
},
1556 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1557 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1558 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1559 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1560 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1561 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1562 { "list-users", VERB_ANY
, 1, 0, list_users
},
1563 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1564 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1565 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1566 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1567 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1568 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1569 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1570 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1571 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1572 { "attach", 3, VERB_ANY
, 0, attach
},
1573 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1574 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1578 return dispatch_verb(argc
, argv
, verbs
, bus
);
1581 int main(int argc
, char *argv
[]) {
1585 setlocale(LC_ALL
, "");
1586 log_parse_environment();
1589 r
= parse_argv(argc
, argv
);
1593 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1595 log_error_errno(r
, "Failed to create bus connection: %m");
1599 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1601 r
= loginctl_main(argc
, argv
, bus
);
1604 sd_bus_flush_close_unref(bus
);
1607 polkit_agent_close();
1609 strv_free(arg_property
);
1611 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;