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 (IN_SET(contents
[0], 's', '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 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
486 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
487 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
488 _cleanup_(session_status_info_clear
) SessionStatusInfo i
= {};
491 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, &error
, &i
);
493 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
500 printf("%s - ", strna(i
.id
));
503 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
505 printf("%"PRIu32
"\n", i
.uid
);
507 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
508 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
511 printf("\t Since: %s; %s\n", s2
, s1
);
513 printf("\t Since: %s\n", s2
);
516 _cleanup_free_
char *t
= NULL
;
518 printf("\t Leader: %"PRIu32
, i
.leader
);
520 get_process_comm(i
.leader
, &t
);
527 if (!isempty(i
.seat
)) {
528 printf("\t Seat: %s", i
.seat
);
531 printf("; vc%u", i
.vtnr
);
537 printf("\t TTY: %s\n", i
.tty
);
539 printf("\t Display: %s\n", i
.display
);
541 if (i
.remote_host
&& i
.remote_user
)
542 printf("\t Remote: %s@%s\n", i
.remote_user
, i
.remote_host
);
543 else if (i
.remote_host
)
544 printf("\t Remote: %s\n", i
.remote_host
);
545 else if (i
.remote_user
)
546 printf("\t Remote: user %s\n", i
.remote_user
);
548 printf("\t Remote: Yes\n");
551 printf("\t Service: %s", i
.service
);
554 printf("; type %s", i
.type
);
557 printf("; class %s", i
.class);
561 printf("\t Type: %s", i
.type
);
564 printf("; class %s", i
.class);
568 printf("\t Class: %s\n", i
.class);
570 if (!isempty(i
.desktop
))
571 printf("\t Desktop: %s\n", i
.desktop
);
574 printf("\t State: %s\n", i
.state
);
577 printf("\t Unit: %s\n", i
.scope
);
578 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Scope", i
.scope
, i
.leader
);
580 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
582 show_journal_by_unit(
587 i
.timestamp
.monotonic
,
590 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
591 SD_JOURNAL_LOCAL_ONLY
,
600 static int print_user_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
602 static const struct bus_properties_map map
[] = {
603 { "Name", "s", NULL
, offsetof(UserStatusInfo
, name
) },
604 { "Linger", "b", NULL
, offsetof(UserStatusInfo
, linger
) },
605 { "Slice", "s", NULL
, offsetof(UserStatusInfo
, slice
) },
606 { "State", "s", NULL
, offsetof(UserStatusInfo
, state
) },
607 { "UID", "u", NULL
, offsetof(UserStatusInfo
, uid
) },
608 { "Timestamp", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.realtime
) },
609 { "TimestampMonotonic", "t", NULL
, offsetof(UserStatusInfo
, timestamp
.monotonic
) },
610 { "Display", "(so)", prop_map_first_of_struct
, offsetof(UserStatusInfo
, display
) },
611 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(UserStatusInfo
, sessions
) },
615 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
616 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
617 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
618 _cleanup_(user_status_info_clear
) UserStatusInfo i
= {};
621 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, &error
, &i
);
623 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
631 printf("%s (%"PRIu32
")\n", i
.name
, i
.uid
);
633 printf("%"PRIu32
"\n", i
.uid
);
635 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
.timestamp
.realtime
);
636 s2
= format_timestamp(since2
, sizeof(since2
), i
.timestamp
.realtime
);
639 printf("\t Since: %s; %s\n", s2
, s1
);
641 printf("\t Since: %s\n", s2
);
643 if (!isempty(i
.state
))
644 printf("\t State: %s\n", i
.state
);
646 if (!strv_isempty(i
.sessions
)) {
648 printf("\tSessions:");
650 STRV_FOREACH(l
, i
.sessions
)
652 streq_ptr(*l
, i
.display
) ? "*" : "",
658 printf("\t Linger: %s\n", yes_no(i
.linger
));
661 printf("\t Unit: %s\n", i
.slice
);
662 show_unit_cgroup(bus
, "org.freedesktop.systemd1.Slice", i
.slice
, 0);
664 show_journal_by_unit(
669 i
.timestamp
.monotonic
,
672 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
673 SD_JOURNAL_LOCAL_ONLY
,
681 static int print_seat_status_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
683 static const struct bus_properties_map map
[] = {
684 { "Id", "s", NULL
, offsetof(SeatStatusInfo
, id
) },
685 { "ActiveSession", "(so)", prop_map_first_of_struct
, offsetof(SeatStatusInfo
, active_session
) },
686 { "Sessions", "a(so)", prop_map_sessions_strv
, offsetof(SeatStatusInfo
, sessions
) },
690 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
691 _cleanup_(seat_status_info_clear
) SeatStatusInfo i
= {};
694 r
= bus_map_all_properties(bus
, "org.freedesktop.login1", path
, map
, &error
, &i
);
696 return log_error_errno(r
, "Could not get properties: %s", bus_error_message(&error
, r
));
703 printf("%s\n", strna(i
.id
));
705 if (!strv_isempty(i
.sessions
)) {
707 printf("\tSessions:");
709 STRV_FOREACH(l
, i
.sessions
) {
710 if (streq_ptr(*l
, i
.active_session
))
719 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
728 printf("\t Devices:\n");
730 show_sysfs(i
.id
, "\t\t ", c
);
736 #define property(name, fmt, ...) \
739 printf(fmt "\n", __VA_ARGS__); \
741 printf("%s=" fmt "\n", name, __VA_ARGS__); \
744 static int print_property(const char *name
, sd_bus_message
*m
, const char *contents
) {
751 if (arg_property
&& !strv_find(arg_property
, name
))
752 /* skip what we didn't read */
753 return sd_bus_message_skip(m
, contents
);
755 switch (contents
[0]) {
757 case SD_BUS_TYPE_STRUCT_BEGIN
:
759 if (contents
[1] == SD_BUS_TYPE_STRING
&& STR_IN_SET(name
, "Display", "Seat", "ActiveSession")) {
762 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
764 return bus_log_parse_error(r
);
766 if (arg_all
|| !isempty(s
))
767 property(name
, "%s", s
);
771 } else if (contents
[1] == SD_BUS_TYPE_UINT32
&& streq(name
, "User")) {
774 r
= sd_bus_message_read(m
, "(uo)", &uid
, NULL
);
776 return bus_log_parse_error(r
);
778 if (!uid_is_valid(uid
)) {
779 log_error("Invalid user ID: " UID_FMT
, uid
);
783 property(name
, UID_FMT
, uid
);
789 case SD_BUS_TYPE_ARRAY
:
791 if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Sessions")) {
795 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(so)");
797 return bus_log_parse_error(r
);
802 while ((r
= sd_bus_message_read(m
, "(so)", &s
, NULL
)) > 0) {
803 printf("%s%s", space
? " " : "", s
);
807 if (space
|| !arg_value
)
811 return bus_log_parse_error(r
);
813 r
= sd_bus_message_exit_container(m
);
815 return bus_log_parse_error(r
);
823 r
= bus_print_property(name
, m
, arg_value
, arg_all
);
825 return bus_log_parse_error(r
);
828 r
= sd_bus_message_skip(m
, contents
);
830 return bus_log_parse_error(r
);
833 printf("%s=[unprintable]\n", name
);
839 static int show_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
840 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
841 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
848 r
= sd_bus_call_method(
850 "org.freedesktop.login1",
852 "org.freedesktop.DBus.Properties",
858 return log_error_errno(r
, "Failed to get properties: %s", bus_error_message(&error
, r
));
860 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "{sv}");
862 return bus_log_parse_error(r
);
869 while ((r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
870 const char *name
, *contents
;
872 r
= sd_bus_message_read(reply
, "s", &name
);
874 return bus_log_parse_error(r
);
876 r
= sd_bus_message_peek_type(reply
, NULL
, &contents
);
878 return bus_log_parse_error(r
);
880 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_VARIANT
, contents
);
882 return bus_log_parse_error(r
);
884 r
= print_property(name
, reply
, contents
);
888 r
= sd_bus_message_exit_container(reply
);
890 return bus_log_parse_error(r
);
892 r
= sd_bus_message_exit_container(reply
);
894 return bus_log_parse_error(r
);
897 return bus_log_parse_error(r
);
899 r
= sd_bus_message_exit_container(reply
);
901 return bus_log_parse_error(r
);
906 static int show_session(int argc
, char *argv
[], void *userdata
) {
907 bool properties
, new_line
= false;
908 sd_bus
*bus
= userdata
;
910 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
911 _cleanup_free_
char *path
= NULL
;
916 properties
= !strstr(argv
[0], "status");
918 pager_open(arg_no_pager
, false);
921 const char *session
, *p
= "/org/freedesktop/login1/session/self";
924 /* If no argument is specified inspect the manager itself */
925 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
927 /* And in the pretty case, show data of the calling session */
928 session
= getenv("XDG_SESSION_ID");
930 r
= get_session_path(bus
, session
, &error
, &path
);
932 log_error("Failed to get session path: %s", bus_error_message(&error
, r
));
938 return print_session_status_info(bus
, p
, &new_line
);
941 for (i
= 1; i
< argc
; i
++) {
942 r
= get_session_path(bus
, argv
[i
], &error
, &path
);
944 log_error("Failed to get session path: %s", bus_error_message(&error
, r
));
949 r
= show_properties(bus
, path
, &new_line
);
951 r
= print_session_status_info(bus
, path
, &new_line
);
960 static int show_user(int argc
, char *argv
[], void *userdata
) {
961 bool properties
, new_line
= false;
962 sd_bus
*bus
= userdata
;
968 properties
= !strstr(argv
[0], "status");
970 pager_open(arg_no_pager
, false);
973 /* If not argument is specified inspect the manager
976 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
978 return print_user_status_info(bus
, "/org/freedesktop/login1/user/self", &new_line
);
981 for (i
= 1; i
< argc
; i
++) {
982 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
983 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
984 const char *path
= NULL
;
987 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
989 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
991 r
= sd_bus_call_method(
993 "org.freedesktop.login1",
994 "/org/freedesktop/login1",
995 "org.freedesktop.login1.Manager",
998 "u", (uint32_t) uid
);
1000 log_error("Failed to get user: %s", bus_error_message(&error
, r
));
1004 r
= sd_bus_message_read(reply
, "o", &path
);
1006 return bus_log_parse_error(r
);
1009 r
= show_properties(bus
, path
, &new_line
);
1011 r
= print_user_status_info(bus
, path
, &new_line
);
1020 static int show_seat(int argc
, char *argv
[], void *userdata
) {
1021 bool properties
, new_line
= false;
1022 sd_bus
*bus
= userdata
;
1028 properties
= !strstr(argv
[0], "status");
1030 pager_open(arg_no_pager
, false);
1033 /* If not argument is specified inspect the manager
1036 return show_properties(bus
, "/org/freedesktop/login1", &new_line
);
1038 return print_seat_status_info(bus
, "/org/freedesktop/login1/seat/self", &new_line
);
1041 for (i
= 1; i
< argc
; i
++) {
1042 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1043 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* reply
= NULL
;
1044 const char *path
= NULL
;
1046 r
= sd_bus_call_method(
1048 "org.freedesktop.login1",
1049 "/org/freedesktop/login1",
1050 "org.freedesktop.login1.Manager",
1055 log_error("Failed to get seat: %s", bus_error_message(&error
, r
));
1059 r
= sd_bus_message_read(reply
, "o", &path
);
1061 return bus_log_parse_error(r
);
1064 r
= show_properties(bus
, path
, &new_line
);
1066 r
= print_seat_status_info(bus
, path
, &new_line
);
1075 static int activate(int argc
, char *argv
[], void *userdata
) {
1076 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1077 sd_bus
*bus
= userdata
;
1078 char *short_argv
[3];
1084 polkit_agent_open_if_enabled();
1087 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1088 * session name, in which case logind will try to guess our session. */
1090 short_argv
[0] = argv
[0];
1091 short_argv
[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1092 short_argv
[2] = NULL
;
1098 for (i
= 1; i
< argc
; i
++) {
1100 r
= sd_bus_call_method(
1102 "org.freedesktop.login1",
1103 "/org/freedesktop/login1",
1104 "org.freedesktop.login1.Manager",
1105 streq(argv
[0], "lock-session") ? "LockSession" :
1106 streq(argv
[0], "unlock-session") ? "UnlockSession" :
1107 streq(argv
[0], "terminate-session") ? "TerminateSession" :
1112 log_error("Failed to issue method call: %s", bus_error_message(&error
, -r
));
1120 static int kill_session(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();
1131 arg_kill_who
= "all";
1133 for (i
= 1; i
< argc
; i
++) {
1135 r
= sd_bus_call_method(
1137 "org.freedesktop.login1",
1138 "/org/freedesktop/login1",
1139 "org.freedesktop.login1.Manager",
1142 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1144 log_error("Could not kill session: %s", bus_error_message(&error
, -r
));
1152 static int enable_linger(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
;
1155 char* short_argv
[3];
1162 polkit_agent_open_if_enabled();
1164 b
= streq(argv
[0], "enable-linger");
1167 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1168 * session name, in which case logind will try to guess our session. */
1170 short_argv
[0] = argv
[0];
1171 short_argv
[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1172 short_argv
[2] = NULL
;
1177 for (i
= 1; i
< argc
; i
++) {
1180 if (isempty(argv
[i
]))
1183 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1185 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1188 r
= sd_bus_call_method(
1190 "org.freedesktop.login1",
1191 "/org/freedesktop/login1",
1192 "org.freedesktop.login1.Manager",
1195 "ubb", (uint32_t) uid
, b
, true);
1197 log_error("Could not enable linger: %s", bus_error_message(&error
, -r
));
1205 static int terminate_user(int argc
, char *argv
[], void *userdata
) {
1206 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1207 sd_bus
*bus
= userdata
;
1213 polkit_agent_open_if_enabled();
1215 for (i
= 1; i
< argc
; i
++) {
1218 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1220 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1222 r
= sd_bus_call_method(
1224 "org.freedesktop.login1",
1225 "/org/freedesktop/login1",
1226 "org.freedesktop.login1.Manager",
1229 "u", (uint32_t) uid
);
1231 log_error("Could not terminate user: %s", bus_error_message(&error
, -r
));
1239 static int kill_user(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();
1250 arg_kill_who
= "all";
1252 for (i
= 1; i
< argc
; i
++) {
1255 r
= get_user_creds((const char**) (argv
+i
), &uid
, NULL
, NULL
, NULL
);
1257 return log_error_errno(r
, "Failed to look up user %s: %m", argv
[i
]);
1259 r
= sd_bus_call_method(
1261 "org.freedesktop.login1",
1262 "/org/freedesktop/login1",
1263 "org.freedesktop.login1.Manager",
1266 "ui", (uint32_t) uid
, arg_signal
);
1268 log_error("Could not kill user: %s", bus_error_message(&error
, -r
));
1276 static int attach(int argc
, char *argv
[], void *userdata
) {
1277 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1278 sd_bus
*bus
= userdata
;
1284 polkit_agent_open_if_enabled();
1286 for (i
= 2; i
< argc
; i
++) {
1288 r
= sd_bus_call_method(
1290 "org.freedesktop.login1",
1291 "/org/freedesktop/login1",
1292 "org.freedesktop.login1.Manager",
1295 "ssb", argv
[1], argv
[i
], true);
1298 log_error("Could not attach device: %s", bus_error_message(&error
, -r
));
1306 static int flush_devices(int argc
, char *argv
[], void *userdata
) {
1307 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1308 sd_bus
*bus
= userdata
;
1314 polkit_agent_open_if_enabled();
1316 r
= sd_bus_call_method(
1318 "org.freedesktop.login1",
1319 "/org/freedesktop/login1",
1320 "org.freedesktop.login1.Manager",
1325 log_error("Could not flush devices: %s", bus_error_message(&error
, -r
));
1330 static int lock_sessions(int argc
, char *argv
[], void *userdata
) {
1331 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1332 sd_bus
*bus
= userdata
;
1338 polkit_agent_open_if_enabled();
1340 r
= sd_bus_call_method(
1342 "org.freedesktop.login1",
1343 "/org/freedesktop/login1",
1344 "org.freedesktop.login1.Manager",
1345 streq(argv
[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1349 log_error("Could not lock sessions: %s", bus_error_message(&error
, -r
));
1354 static int terminate_seat(int argc
, char *argv
[], void *userdata
) {
1355 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1356 sd_bus
*bus
= userdata
;
1362 polkit_agent_open_if_enabled();
1364 for (i
= 1; i
< argc
; i
++) {
1366 r
= sd_bus_call_method(
1368 "org.freedesktop.login1",
1369 "/org/freedesktop/login1",
1370 "org.freedesktop.login1.Manager",
1375 log_error("Could not terminate seat: %s", bus_error_message(&error
, -r
));
1383 static int help(int argc
, char *argv
[], void *userdata
) {
1385 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1386 "Send control commands to or query the login manager.\n\n"
1387 " -h --help Show this help\n"
1388 " --version Show package version\n"
1389 " --no-pager Do not pipe output into a pager\n"
1390 " --no-legend Do not show the headers and footers\n"
1391 " --no-ask-password Don't prompt for password\n"
1392 " -H --host=[USER@]HOST Operate on remote host\n"
1393 " -M --machine=CONTAINER Operate on local container\n"
1394 " -p --property=NAME Show only properties by this name\n"
1395 " -a --all Show all properties, including empty ones\n"
1396 " --value When showing properties, only print the value\n"
1397 " -l --full Do not ellipsize output\n"
1398 " --kill-who=WHO Who to send signal to\n"
1399 " -s --signal=SIGNAL Which signal to send\n"
1400 " -n --lines=INTEGER Number of journal entries to show\n"
1401 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1402 " short-iso, short-iso-precise, short-full,\n"
1403 " short-monotonic, short-unix, verbose, export,\n"
1404 " json, json-pretty, json-sse, cat)\n"
1405 "Session Commands:\n"
1406 " list-sessions List sessions\n"
1407 " session-status [ID...] Show session status\n"
1408 " show-session [ID...] Show properties of sessions or the manager\n"
1409 " activate [ID] Activate a session\n"
1410 " lock-session [ID...] Screen lock one or more sessions\n"
1411 " unlock-session [ID...] Screen unlock one or more sessions\n"
1412 " lock-sessions Screen lock all current sessions\n"
1413 " unlock-sessions Screen unlock all current sessions\n"
1414 " terminate-session ID... Terminate one or more sessions\n"
1415 " kill-session ID... Send signal to processes of a session\n\n"
1417 " list-users List users\n"
1418 " user-status [USER...] Show user status\n"
1419 " show-user [USER...] Show properties of users or the manager\n"
1420 " enable-linger [USER...] Enable linger state of one or more users\n"
1421 " disable-linger [USER...] Disable linger state of one or more users\n"
1422 " terminate-user USER... Terminate all sessions of one or more users\n"
1423 " kill-user USER... Send signal to processes of a user\n\n"
1425 " list-seats List seats\n"
1426 " seat-status [NAME...] Show seat status\n"
1427 " show-seat [NAME...] Show properties of seats or the manager\n"
1428 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1429 " flush-devices Flush all device associations\n"
1430 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1431 , program_invocation_short_name
);
1436 static int parse_argv(int argc
, char *argv
[]) {
1439 ARG_VERSION
= 0x100,
1444 ARG_NO_ASK_PASSWORD
,
1447 static const struct option options
[] = {
1448 { "help", no_argument
, NULL
, 'h' },
1449 { "version", no_argument
, NULL
, ARG_VERSION
},
1450 { "property", required_argument
, NULL
, 'p' },
1451 { "all", no_argument
, NULL
, 'a' },
1452 { "value", no_argument
, NULL
, ARG_VALUE
},
1453 { "full", no_argument
, NULL
, 'l' },
1454 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1455 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1456 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
1457 { "signal", required_argument
, NULL
, 's' },
1458 { "host", required_argument
, NULL
, 'H' },
1459 { "machine", required_argument
, NULL
, 'M' },
1460 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1461 { "lines", required_argument
, NULL
, 'n' },
1462 { "output", required_argument
, NULL
, 'o' },
1471 while ((c
= getopt_long(argc
, argv
, "hp:als:H:M:n:o:", options
, NULL
)) >= 0)
1476 help(0, NULL
, NULL
);
1483 r
= strv_extend(&arg_property
, optarg
);
1487 /* If the user asked for a particular
1488 * property, show it to him, even if it is
1507 if (safe_atou(optarg
, &arg_lines
) < 0) {
1508 log_error("Failed to parse lines '%s'", optarg
);
1514 arg_output
= output_mode_from_string(optarg
);
1515 if (arg_output
< 0) {
1516 log_error("Unknown output '%s'.", optarg
);
1522 arg_no_pager
= true;
1529 case ARG_NO_ASK_PASSWORD
:
1530 arg_ask_password
= false;
1534 arg_kill_who
= optarg
;
1538 arg_signal
= signal_from_string_try_harder(optarg
);
1539 if (arg_signal
< 0) {
1540 log_error("Failed to parse signal string %s.", optarg
);
1546 arg_transport
= BUS_TRANSPORT_REMOTE
;
1551 arg_transport
= BUS_TRANSPORT_MACHINE
;
1559 assert_not_reached("Unhandled option");
1565 static int loginctl_main(int argc
, char *argv
[], sd_bus
*bus
) {
1567 static const Verb verbs
[] = {
1568 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1569 { "list-sessions", VERB_ANY
, 1, VERB_DEFAULT
, list_sessions
},
1570 { "session-status", VERB_ANY
, VERB_ANY
, 0, show_session
},
1571 { "show-session", VERB_ANY
, VERB_ANY
, 0, show_session
},
1572 { "activate", VERB_ANY
, 2, 0, activate
},
1573 { "lock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1574 { "unlock-session", VERB_ANY
, VERB_ANY
, 0, activate
},
1575 { "lock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1576 { "unlock-sessions", VERB_ANY
, 1, 0, lock_sessions
},
1577 { "terminate-session", 2, VERB_ANY
, 0, activate
},
1578 { "kill-session", 2, VERB_ANY
, 0, kill_session
},
1579 { "list-users", VERB_ANY
, 1, 0, list_users
},
1580 { "user-status", VERB_ANY
, VERB_ANY
, 0, show_user
},
1581 { "show-user", VERB_ANY
, VERB_ANY
, 0, show_user
},
1582 { "enable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1583 { "disable-linger", VERB_ANY
, VERB_ANY
, 0, enable_linger
},
1584 { "terminate-user", 2, VERB_ANY
, 0, terminate_user
},
1585 { "kill-user", 2, VERB_ANY
, 0, kill_user
},
1586 { "list-seats", VERB_ANY
, 1, 0, list_seats
},
1587 { "seat-status", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1588 { "show-seat", VERB_ANY
, VERB_ANY
, 0, show_seat
},
1589 { "attach", 3, VERB_ANY
, 0, attach
},
1590 { "flush-devices", VERB_ANY
, 1, 0, flush_devices
},
1591 { "terminate-seat", 2, VERB_ANY
, 0, terminate_seat
},
1595 return dispatch_verb(argc
, argv
, verbs
, bus
);
1598 int main(int argc
, char *argv
[]) {
1602 setlocale(LC_ALL
, "");
1603 log_parse_environment();
1606 r
= parse_argv(argc
, argv
);
1610 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
1612 log_error_errno(r
, "Failed to create bus connection: %m");
1616 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1618 r
= loginctl_main(argc
, argv
, bus
);
1621 sd_bus_flush_close_unref(bus
);
1624 polkit_agent_close();
1626 strv_free(arg_property
);
1628 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;