Debian Codegraph @

src/login/loginctl.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <locale.h>
4 #include <unistd.h>
5
6 #include "sd-bus.h"
7 #include "sd-journal.h"
8
9 #include "alloc-util.h"
10 #include "ansi-color.h"
11 #include "build.h"
12 #include "bus-error.h"
13 #include "bus-locator.h"
14 #include "bus-map-properties.h"
15 #include "bus-print-properties.h"
16 #include "bus-unit-procs.h"
17 #include "bus-util.h"
18 #include "cgroup-show.h"
19 #include "cgroup-util.h"
20 #include "format-table.h"
21 #include "format-util.h"
22 #include "help-util.h"
23 #include "log.h"
24 #include "logs-show.h"
25 #include "main-func.h"
26 #include "options.h"
27 #include "pager.h"
28 #include "parse-argument.h"
29 #include "parse-util.h"
30 #include "polkit-agent.h"
31 #include "process-util.h"
32 #include "runtime-scope.h"
33 #include "string-table.h"
34 #include "string-util.h"
35 #include "strv.h"
36 #include "sysfs-show.h"
37 #include "terminal-util.h"
38 #include "time-util.h"
39 #include "user-util.h"
40 #include "verbs.h"
41
42 static char **arg_property = NULL;
43 static BusPrintPropertyFlags arg_print_flags = 0;
44 static bool arg_full = false;
45 static PagerFlags arg_pager_flags = 0;
46 static bool arg_legend = true;
47 static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
48 static const char *arg_kill_whom = NULL;
49 static int arg_signal = SIGTERM;
50 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
51 static const char *arg_host = NULL;
52 static bool arg_ask_password = true;
53 static unsigned arg_lines = 10;
54 static OutputMode arg_output = OUTPUT_SHORT;
55
56 STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
57
58 typedef struct SessionStatusInfo {
59 const char *id;
60 uid_t uid;
61 const char *name;
62 dual_timestamp timestamp;
63 unsigned vtnr;
64 const char *seat;
65 const char *tty;
66 const char *display;
67 bool remote;
68 const char *remote_host;
69 const char *remote_user;
70 const char *service;
71 pid_t leader;
72 const char *type;
73 const char *class;
74 const char *state;
75 const char *scope;
76 const char *desktop;
77 bool idle_hint;
78 dual_timestamp idle_hint_timestamp;
79 } SessionStatusInfo;
80
81 typedef struct UserStatusInfo {
82 uid_t uid;
83 bool linger;
84 const char *name;
85 dual_timestamp timestamp;
86 const char *state;
87 char **sessions;
88 const char *display;
89 const char *slice;
90 } UserStatusInfo;
91
92 typedef struct SeatStatusInfo {
93 const char *id;
94 const char *active_session;
95 char **sessions;
96 } SeatStatusInfo;
97
98 static void user_status_info_done(UserStatusInfo *info) {
99 assert(info);
100
101 strv_free(info->sessions);
102 }
103
104 static void seat_status_info_done(SeatStatusInfo *info) {
105 assert(info);
106
107 strv_free(info->sessions);
108 }
109
110 static OutputFlags get_output_flags(void) {
111 return
112 FLAGS_SET(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) * OUTPUT_SHOW_ALL |
113 (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
114 colors_enabled() * OUTPUT_COLOR;
115 }
116
117 static int list_table_print(Table *table, const char *type) {
118 int r;
119
120 assert(table);
121 assert(type);
122
123 r = table_set_sort(table, (size_t) 0);
124 if (r < 0)
125 return table_log_sort_error(r);
126
127 r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
128 if (r < 0)
129 return r;
130
131 if (arg_legend) {
132 if (table_isempty(table))
133 printf("No %s.\n", type);
134 else
135 printf("\n%zu %s listed.\n", table_get_rows(table) - 1, type);
136 }
137
138 return 0;
139 }
140
141 static int list_sessions_table_add(Table *table, sd_bus_message *reply) {
142 int r;
143
144 assert(table);
145 assert(reply);
146
147 r = sd_bus_message_enter_container(reply, 'a', "(sussussbto)");
148 if (r < 0)
149 return bus_log_parse_error(r);
150
151 for (;;) {
152 const char *session_id, *user, *seat, *class, *tty;
153 uint32_t uid, leader_pid;
154 int idle;
155 uint64_t idle_timestamp_monotonic;
156
157 r = sd_bus_message_read(reply, "(sussussbto)",
158 &session_id,
159 &uid,
160 &user,
161 &seat,
162 &leader_pid,
163 &class,
164 &tty,
165 &idle,
166 &idle_timestamp_monotonic,
167 /* object= */ NULL);
168 if (r < 0)
169 return bus_log_parse_error(r);
170 if (r == 0)
171 break;
172
173 r = table_add_many(table,
174 TABLE_STRING, session_id,
175 TABLE_UID, (uid_t) uid,
176 TABLE_STRING, user,
177 TABLE_STRING, empty_to_null(seat),
178 TABLE_PID, (pid_t) leader_pid,
179 TABLE_STRING, class,
180 TABLE_STRING, empty_to_null(tty),
181 TABLE_BOOLEAN, idle);
182 if (r < 0)
183 return table_log_add_error(r);
184
185 if (idle)
186 r = table_add_cell(table, NULL, TABLE_TIMESTAMP_RELATIVE_MONOTONIC, &idle_timestamp_monotonic);
187 else
188 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
189 if (r < 0)
190 return table_log_add_error(r);
191 }
192
193 r = sd_bus_message_exit_container(reply);
194 if (r < 0)
195 return bus_log_parse_error(r);
196
197 return 0;
198 }
199
200 static int list_sessions_table_add_fallback(Table *table, sd_bus_message *reply, sd_bus *bus) {
201
202 static const struct bus_properties_map map[] = {
203 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
204 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
205 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
206 { "IdleHint", "b", NULL, offsetof(SessionStatusInfo, idle_hint) },
207 { "IdleSinceHintMonotonic", "t", NULL, offsetof(SessionStatusInfo, idle_hint_timestamp.monotonic) },
208 {},
209 };
210
211 int r;
212
213 assert(table);
214 assert(reply);
215 assert(bus);
216
217 r = sd_bus_message_enter_container(reply, 'a', "(susso)");
218 if (r < 0)
219 return bus_log_parse_error(r);
220
221 for (;;) {
222 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
223 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
224 const char *id, *user, *seat, *object;
225 uint32_t uid;
226 SessionStatusInfo i = {};
227
228 r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object);
229 if (r < 0)
230 return bus_log_parse_error(r);
231 if (r == 0)
232 break;
233
234 r = bus_map_all_properties(bus, "org.freedesktop.login1", object, map, BUS_MAP_BOOLEAN_AS_BOOL, &e, &m, &i);
235 if (r < 0) {
236 log_full_errno(sd_bus_error_has_name(&e, SD_BUS_ERROR_UNKNOWN_OBJECT) ? LOG_DEBUG : LOG_WARNING,
237 r,
238 "Failed to get properties of session %s, ignoring: %s",
239 id, bus_error_message(&e, r));
240 continue;
241 }
242
243 r = table_add_many(table,
244 TABLE_STRING, id,
245 TABLE_UID, (uid_t) uid,
246 TABLE_STRING, user,
247 TABLE_STRING, empty_to_null(seat),
248 TABLE_PID, i.leader,
249 TABLE_STRING, i.class,
250 TABLE_STRING, empty_to_null(i.tty),
251 TABLE_BOOLEAN, i.idle_hint);
252 if (r < 0)
253 return table_log_add_error(r);
254
255 if (i.idle_hint)
256 r = table_add_cell(table, NULL, TABLE_TIMESTAMP_RELATIVE_MONOTONIC, &i.idle_hint_timestamp.monotonic);
257 else
258 r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
259 if (r < 0)
260 return table_log_add_error(r);
261 }
262
263 r = sd_bus_message_exit_container(reply);
264 if (r < 0)
265 return bus_log_parse_error(r);
266
267 return 0;
268 }
269
270 VERB_GROUP("Session Commands");
271
272 VERB_DEFAULT_NOARG(verb_list_sessions, "list-sessions", "List sessions");
273 static int verb_list_sessions(int argc, char *argv[], uintptr_t _data, void *userdata) {
274 sd_bus *bus = ASSERT_PTR(userdata);
275 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
276 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
277 _cleanup_(table_unrefp) Table *table = NULL;
278 bool use_ex = true;
279 int r;
280
281 assert(argv);
282
283 r = bus_call_method(bus, bus_login_mgr, "ListSessionsEx", &error, &reply, NULL);
284 if (r < 0) {
285 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) {
286 sd_bus_error_free(&error);
287
288 use_ex = false;
289 r = bus_call_method(bus, bus_login_mgr, "ListSessions", &error, &reply, NULL);
290 }
291 if (r < 0)
292 return log_error_errno(r, "Failed to list sessions: %s", bus_error_message(&error, r));
293 }
294
295 table = table_new("session", "uid", "user", "seat", "leader", "class", "tty", "idle", "since");
296 if (!table)
297 return log_oom();
298
299 /* Right-align the first two fields (since they are numeric) */
300 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
301 (void) table_set_align_percent(table, TABLE_HEADER_CELL(1), 100);
302
303 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
304
305 if (use_ex)
306 r = list_sessions_table_add(table, reply);
307 else
308 r = list_sessions_table_add_fallback(table, reply, bus);
309 if (r < 0)
310 return r;
311
312 return list_table_print(table, "sessions");
313 }
314
315 static int show_unit_cgroup(
316 sd_bus *bus,
317 const char *unit,
318 pid_t leader,
319 const char *prefix) {
320
321 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
322 _cleanup_free_ char *cgroup = NULL;
323 int r;
324
325 assert(bus);
326 assert(unit);
327 assert(prefix);
328
329 r = show_cgroup_get_unit_path_and_warn(bus, unit, &cgroup);
330 if (r < 0)
331 return r;
332
333 if (isempty(cgroup))
334 return 0;
335
336 unsigned c = MAX(LESS_BY(columns(), 18U), 10U);
337 r = unit_show_processes(bus, unit, cgroup, prefix, c, get_output_flags(), &error);
338 if (r == -EBADR) {
339 if (arg_transport == BUS_TRANSPORT_REMOTE)
340 return 0;
341
342 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
343
344 if (cg_is_empty(cgroup) != 0 && leader <= 0)
345 return 0;
346
347 show_cgroup_and_extra(cgroup, prefix, c, &leader, leader > 0, get_output_flags());
348 } else if (r < 0)
349 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
350
351 return 0;
352 }
353
354 static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
355 const char *contents;
356 int r;
357
358 assert(bus);
359 assert(m);
360
361 r = sd_bus_message_peek_type(m, NULL, &contents);
362 if (r < 0)
363 return r;
364
365 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, contents);
366 if (r < 0)
367 return r;
368
369 r = sd_bus_message_read_basic(m, contents[0], userdata);
370 if (r < 0)
371 return r;
372
373 r = sd_bus_message_skip(m, contents+1);
374 if (r < 0)
375 return r;
376
377 r = sd_bus_message_exit_container(m);
378 if (r < 0)
379 return r;
380
381 return 0;
382 }
383
384 static int prop_map_sessions_strv(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
385 const char *name;
386 int r;
387
388 assert(bus);
389 assert(m);
390
391 r = sd_bus_message_enter_container(m, 'a', "(so)");
392 if (r < 0)
393 return r;
394
395 while ((r = sd_bus_message_read(m, "(so)", &name, NULL)) > 0) {
396 r = strv_extend(userdata, name);
397 if (r < 0)
398 return r;
399 }
400 if (r < 0)
401 return r;
402
403 return sd_bus_message_exit_container(m);
404 }
405
406 static int mark_session(char **sessions, const char *target_session) {
407 assert(sessions);
408 assert(target_session);
409
410 STRV_FOREACH(i, sessions)
411 if (streq(*i, target_session)) {
412 _cleanup_free_ char *marked = NULL;
413
414 marked = strjoin("*", target_session);
415 if (!marked)
416 return log_oom();
417
418 return free_and_replace(*i, marked);
419 }
420
421 return 0;
422 }
423
424 static int print_session_status_info(sd_bus *bus, const char *path) {
425
426 static const struct bus_properties_map map[] = {
427 { "Id", "s", NULL, offsetof(SessionStatusInfo, id) },
428 { "Name", "s", NULL, offsetof(SessionStatusInfo, name) },
429 { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) },
430 { "Display", "s", NULL, offsetof(SessionStatusInfo, display) },
431 { "RemoteHost", "s", NULL, offsetof(SessionStatusInfo, remote_host) },
432 { "RemoteUser", "s", NULL, offsetof(SessionStatusInfo, remote_user) },
433 { "Service", "s", NULL, offsetof(SessionStatusInfo, service) },
434 { "Desktop", "s", NULL, offsetof(SessionStatusInfo, desktop) },
435 { "Type", "s", NULL, offsetof(SessionStatusInfo, type) },
436 { "Class", "s", NULL, offsetof(SessionStatusInfo, class) },
437 { "Scope", "s", NULL, offsetof(SessionStatusInfo, scope) },
438 { "State", "s", NULL, offsetof(SessionStatusInfo, state) },
439 { "VTNr", "u", NULL, offsetof(SessionStatusInfo, vtnr) },
440 { "Leader", "u", NULL, offsetof(SessionStatusInfo, leader) },
441 { "Remote", "b", NULL, offsetof(SessionStatusInfo, remote) },
442 { "Timestamp", "t", NULL, offsetof(SessionStatusInfo, timestamp.realtime) },
443 { "TimestampMonotonic", "t", NULL, offsetof(SessionStatusInfo, timestamp.monotonic) },
444 { "IdleHint", "b", NULL, offsetof(SessionStatusInfo, idle_hint) },
445 { "IdleSinceHint", "t", NULL, offsetof(SessionStatusInfo, idle_hint_timestamp.realtime) },
446 { "IdleSinceHintMonotonic", "t", NULL, offsetof(SessionStatusInfo, idle_hint_timestamp.monotonic) },
447 { "User", "(uo)", prop_map_first_of_struct, offsetof(SessionStatusInfo, uid) },
448 { "Seat", "(so)", prop_map_first_of_struct, offsetof(SessionStatusInfo, seat) },
449 {}
450 };
451
452 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
453 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
454 _cleanup_(table_unrefp) Table *table = NULL;
455 SessionStatusInfo i = {};
456 int r;
457
458 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
459 if (r < 0)
460 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
461
462 table = table_new_vertical();
463 if (!table)
464 return log_oom();
465
466 table_set_ersatz_string(table, TABLE_ERSATZ_NA);
467
468 if (dual_timestamp_is_set(&i.timestamp)) {
469 r = table_add_cell(table, NULL, TABLE_FIELD, "Since");
470 if (r < 0)
471 return table_log_add_error(r);
472
473 r = table_add_cell_stringf(table, NULL, "%s; %s",
474 FORMAT_TIMESTAMP(i.timestamp.realtime),
475 FORMAT_TIMESTAMP_RELATIVE_MONOTONIC(i.timestamp.monotonic));
476 if (r < 0)
477 return table_log_add_error(r);
478 }
479
480 r = table_add_many(table,
481 TABLE_FIELD, "State",
482 TABLE_STRING, i.state);
483 if (r < 0)
484 return table_log_add_error(r);
485
486 if (i.leader > 0) {
487 _cleanup_free_ char *name = NULL;
488
489 (void) pid_get_comm(i.leader, &name);
490
491 r = table_add_cell(table, NULL, TABLE_FIELD, "Leader");
492 if (r < 0)
493 return table_log_add_error(r);
494
495 r = table_add_cell_stringf(table, NULL, PID_FMT "%s%s%s",
496 i.leader,
497 !isempty(name) ? " (" : "",
498 strempty(name),
499 !isempty(name) ? ")" : "");
500 if (r < 0)
501 return table_log_add_error(r);
502 }
503
504 if (!isempty(i.seat)) {
505 r = table_add_cell(table, NULL, TABLE_FIELD, "Seat");
506 if (r < 0)
507 return table_log_add_error(r);
508
509 if (i.vtnr > 0)
510 r = table_add_cell_stringf(table, NULL, "%s; vc%u", i.seat, i.vtnr);
511 else
512 r = table_add_cell(table, NULL, TABLE_STRING, i.seat);
513 if (r < 0)
514 return table_log_add_error(r);
515 }
516
517 if (!isempty(i.tty))
518 r = table_add_many(table,
519 TABLE_FIELD, "TTY",
520 TABLE_STRING, i.tty);
521 else if (!isempty(i.display))
522 r = table_add_many(table,
523 TABLE_FIELD, "Display",
524 TABLE_STRING, i.display);
525 else
526 r = 0;
527 if (r < 0)
528 return table_log_add_error(r);
529
530 r = table_add_cell(table, NULL, TABLE_FIELD, "Remote");
531 if (r < 0)
532 return table_log_add_error(r);
533
534 if (i.remote_host && i.remote_user)
535 r = table_add_cell_stringf(table, NULL, "%s@%s", i.remote_user, i.remote_host);
536 else if (i.remote_host)
537 r = table_add_cell(table, NULL, TABLE_STRING, i.remote_host);
538 else if (i.remote_user)
539 r = table_add_cell_stringf(table, NULL, "user %s", i.remote_user);
540 else
541 r = table_add_cell(table, NULL, TABLE_BOOLEAN, &i.remote);
542 if (r < 0)
543 return table_log_add_error(r);
544
545 if (i.service) {
546 r = table_add_many(table,
547 TABLE_FIELD, "Service",
548 TABLE_STRING, i.service);
549 if (r < 0)
550 return table_log_add_error(r);
551 }
552
553 if (i.type) {
554 r = table_add_many(table,
555 TABLE_FIELD, "Type",
556 TABLE_STRING, i.type);
557 if (r < 0)
558 return table_log_add_error(r);
559 }
560
561 if (i.class) {
562 r = table_add_many(table,
563 TABLE_FIELD, "Class",
564 TABLE_STRING, i.class);
565 if (r < 0)
566 return table_log_add_error(r);
567 }
568
569 if (!isempty(i.desktop)) {
570 r = table_add_many(table,
571 TABLE_FIELD, "Desktop",
572 TABLE_STRING, i.desktop);
573 if (r < 0)
574 return table_log_add_error(r);
575 }
576
577 r = table_add_cell(table, NULL, TABLE_FIELD, "Idle");
578 if (r < 0)
579 return table_log_add_error(r);
580
581 if (i.idle_hint && dual_timestamp_is_set(&i.idle_hint_timestamp))
582 r = table_add_cell_stringf(table, NULL, "%s since %s (%s)",
583 yes_no(i.idle_hint),
584 FORMAT_TIMESTAMP(i.idle_hint_timestamp.realtime),
585 FORMAT_TIMESTAMP_RELATIVE_MONOTONIC(i.idle_hint_timestamp.monotonic));
586 else
587 r = table_add_cell(table, NULL, TABLE_BOOLEAN, &i.idle_hint);
588 if (r < 0)
589 return table_log_add_error(r);
590
591 if (i.scope) {
592 r = table_add_many(table,
593 TABLE_FIELD, "Unit",
594 TABLE_SET_MINIMUM_WIDTH, STRLEN("Display"), /* For alignment with show_unit_cgroup */
595 TABLE_STRING, i.scope);
596 if (r < 0)
597 return table_log_add_error(r);
598 }
599
600 /* We don't use the table to show the header, in order to make the width of the column stable. */
601 printf("%s%s - %s (" UID_FMT ")%s\n", ansi_highlight(), i.id, i.name, i.uid, ansi_normal());
602
603 r = table_print_or_warn(table);
604 if (r < 0)
605 return r;
606
607 if (i.scope) {
608 show_unit_cgroup(bus, i.scope, i.leader, /* prefix= */ strrepa(" ", STRLEN("Display: ")));
609
610 if (arg_transport == BUS_TRANSPORT_LOCAL)
611 show_journal_by_unit(
612 stdout,
613 i.scope,
614 /* namespace= */ NULL,
615 arg_output,
616 /* n_columns= */ 0,
617 i.timestamp.monotonic,
618 arg_lines,
619 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
620 SD_JOURNAL_LOCAL_ONLY,
621 /* system_unit= */ true,
622 /* ellipsized= */ NULL);
623 }
624
625 return 0;
626 }
627
628 static int print_user_status_info(sd_bus *bus, const char *path) {
629
630 static const struct bus_properties_map map[] = {
631 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
632 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
633 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
634 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
635 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
636 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
637 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
638 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
639 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
640 {}
641 };
642
643 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
644 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
645 _cleanup_(user_status_info_done) UserStatusInfo i = {};
646 _cleanup_(table_unrefp) Table *table = NULL;
647 int r;
648
649 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
650 if (r < 0)
651 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
652
653 table = table_new_vertical();
654 if (!table)
655 return log_oom();
656
657 table_set_ersatz_string(table, TABLE_ERSATZ_NA);
658
659 if (dual_timestamp_is_set(&i.timestamp)) {
660 r = table_add_cell(table, NULL, TABLE_FIELD, "Since");
661 if (r < 0)
662 return table_log_add_error(r);
663
664 r = table_add_cell_stringf(table, NULL, "%s; %s",
665 FORMAT_TIMESTAMP(i.timestamp.realtime),
666 FORMAT_TIMESTAMP_RELATIVE_MONOTONIC(i.timestamp.monotonic));
667 if (r < 0)
668 return table_log_add_error(r);
669 }
670
671 r = table_add_many(table,
672 TABLE_FIELD, "State",
673 TABLE_STRING, i.state);
674 if (r < 0)
675 return table_log_add_error(r);
676
677 if (!strv_isempty(i.sessions)) {
678 _cleanup_strv_free_ char **sessions = TAKE_PTR(i.sessions);
679
680 r = mark_session(sessions, i.display);
681 if (r < 0)
682 return r;
683
684 r = table_add_many(table,
685 TABLE_FIELD, "Sessions",
686 TABLE_STRV_WRAPPED, sessions);
687 if (r < 0)
688 return table_log_add_error(r);
689 }
690
691 r = table_add_many(table,
692 TABLE_FIELD, "Linger",
693 TABLE_BOOLEAN, i.linger);
694 if (r < 0)
695 return table_log_add_error(r);
696
697 if (i.slice) {
698 r = table_add_many(table,
699 TABLE_FIELD, "Unit",
700 TABLE_SET_MINIMUM_WIDTH, STRLEN("Sessions"), /* For alignment with show_unit_cgroup */
701 TABLE_STRING, i.slice);
702 if (r < 0)
703 return table_log_add_error(r);
704 }
705
706 printf("%s%s (" UID_FMT ")%s\n", ansi_highlight(), i.name, i.uid, ansi_normal());
707
708 r = table_print_or_warn(table);
709 if (r < 0)
710 return r;
711
712 if (i.slice) {
713 show_unit_cgroup(bus, i.slice, /* leader= */ 0, /* prefix= */ strrepa(" ", STRLEN("Sessions: ")));
714
715 if (arg_transport == BUS_TRANSPORT_LOCAL)
716 show_journal_by_unit(
717 stdout,
718 i.slice,
719 /* namespace= */ NULL,
720 arg_output,
721 /* n_columns= */ 0,
722 i.timestamp.monotonic,
723 arg_lines,
724 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
725 SD_JOURNAL_LOCAL_ONLY,
726 /* system_unit= */ true,
727 /* ellipsized= */ NULL);
728 }
729
730 return 0;
731 }
732
733 static int print_seat_status_info(sd_bus *bus, const char *path) {
734
735 static const struct bus_properties_map map[] = {
736 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
737 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
738 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
739 {}
740 };
741
742 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
743 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
744 _cleanup_(seat_status_info_done) SeatStatusInfo i = {};
745 _cleanup_(table_unrefp) Table *table = NULL;
746 int r;
747
748 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, 0, &error, &m, &i);
749 if (r < 0)
750 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
751
752 table = table_new_vertical();
753 if (!table)
754 return log_oom();
755
756 table_set_ersatz_string(table, TABLE_ERSATZ_NA);
757
758 if (!strv_isempty(i.sessions)) {
759 _cleanup_strv_free_ char **sessions = TAKE_PTR(i.sessions);
760
761 r = mark_session(sessions, i.active_session);
762 if (r < 0)
763 return r;
764
765 r = table_add_many(table,
766 TABLE_FIELD, "Sessions",
767 TABLE_STRV_WRAPPED, sessions);
768 if (r < 0)
769 return table_log_add_error(r);
770 }
771
772 if (arg_transport == BUS_TRANSPORT_LOCAL) {
773 r = table_add_many(table,
774 TABLE_FIELD, "Devices",
775 TABLE_SET_MINIMUM_WIDTH, STRLEN("Sessions"), /* For alignment with show_sysfs */
776 TABLE_EMPTY);
777 if (r < 0)
778 return table_log_add_error(r);
779 }
780
781 printf("%s%s%s\n", ansi_highlight(), i.id, ansi_normal());
782
783 r = table_print_or_warn(table);
784 if (r < 0)
785 return r;
786
787 if (arg_transport == BUS_TRANSPORT_LOCAL) {
788 unsigned c = MAX(LESS_BY(columns(), 21U), 10U);
789 show_sysfs(i.id, strrepa(" ", STRLEN("Sessions:")), c, get_output_flags());
790 }
791
792 return 0;
793 }
794
795 static int print_property(const char *name, const char *expected_value, sd_bus_message *m, BusPrintPropertyFlags flags) {
796 char type;
797 const char *contents;
798 int r;
799
800 assert(name);
801 assert(m);
802
803 r = sd_bus_message_peek_type(m, &type, &contents);
804 if (r < 0)
805 return r;
806
807 switch (type) {
808
809 case SD_BUS_TYPE_STRUCT:
810
811 if (contents[0] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
812 const char *s;
813
814 r = sd_bus_message_read(m, "(so)", &s, NULL);
815 if (r < 0)
816 return bus_log_parse_error(r);
817
818 bus_print_property_value(name, expected_value, flags, s);
819
820 return 1;
821
822 } else if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
823 uint32_t uid;
824
825 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
826 if (r < 0)
827 return bus_log_parse_error(r);
828
829 if (!uid_is_valid(uid))
830 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
831 "Invalid user ID: " UID_FMT,
832 uid);
833
834 bus_print_property_valuef(name, expected_value, flags, UID_FMT, uid);
835 return 1;
836 }
837 break;
838
839 case SD_BUS_TYPE_ARRAY:
840
841 if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
842 const char *s;
843 bool space = false;
844
845 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
846 if (r < 0)
847 return bus_log_parse_error(r);
848
849 if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
850 printf("%s=", name);
851
852 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
853 printf("%s%s", space ? " " : "", s);
854 space = true;
855 }
856
857 if (space || !FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
858 printf("\n");
859
860 if (r < 0)
861 return bus_log_parse_error(r);
862
863 r = sd_bus_message_exit_container(m);
864 if (r < 0)
865 return bus_log_parse_error(r);
866
867 return 1;
868 }
869 break;
870 }
871
872 return 0;
873 }
874
875 static int show_properties(sd_bus *bus, const char *path) {
876 int r;
877
878 assert(bus);
879 assert(path);
880
881 r = bus_print_all_properties(
882 bus,
883 "org.freedesktop.login1",
884 path,
885 print_property,
886 arg_property,
887 arg_print_flags,
888 NULL);
889 if (r < 0)
890 return bus_log_parse_error(r);
891
892 return 0;
893 }
894
895 static int get_bus_path_by_id(
896 sd_bus *bus,
897 const char *type,
898 const char *method,
899 const char *id,
900 char **ret) {
901
902 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
903 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
904 const char *path;
905 int r;
906
907 assert(bus);
908 assert(type);
909 assert(STR_IN_SET(type, "session", "seat"));
910 assert(method);
911 assert(id);
912 assert(ret);
913
914 r = bus_call_method(bus, bus_login_mgr, method, &error, &reply, "s", id);
915 if (r < 0)
916 return log_error_errno(r, "Failed to get path for %s '%s': %s", type, id, bus_error_message(&error, r));
917
918 r = sd_bus_message_read(reply, "o", &path);
919 if (r < 0)
920 return bus_log_parse_error(r);
921
922 return strdup_to(ret, path);
923 }
924
925 VERB(verb_show_session, "session-status", "[ID…]", VERB_ANY, VERB_ANY, 0,
926 "Show session status");
927 VERB(verb_show_session, "show-session", "[ID…]", VERB_ANY, VERB_ANY, 0,
928 "Show properties of sessions or the manager");
929 static int verb_show_session(int argc, char *argv[], uintptr_t _data, void *userdata) {
930 sd_bus *bus = ASSERT_PTR(userdata);
931 bool properties;
932 int r;
933
934 assert(argv);
935
936 properties = !strstr(argv[0], "status");
937
938 pager_open(arg_pager_flags);
939
940 if (argc <= 1) {
941 _cleanup_free_ char *path = NULL;
942
943 /* If no argument is specified inspect the manager itself */
944 if (properties)
945 return show_properties(bus, "/org/freedesktop/login1");
946
947 r = get_bus_path_by_id(bus, "session", "GetSession", "auto", &path);
948 if (r < 0)
949 return r;
950
951 return print_session_status_info(bus, path);
952 }
953
954 for (int i = 1, first = true; i < argc; i++, first = false) {
955 _cleanup_free_ char *path = NULL;
956
957 r = get_bus_path_by_id(bus, "session", "GetSession", argv[i], &path);
958 if (r < 0)
959 return r;
960
961 if (!first)
962 putchar('\n');
963
964 if (properties)
965 r = show_properties(bus, path);
966 else
967 r = print_session_status_info(bus, path);
968 if (r < 0)
969 return r;
970 }
971
972 return 0;
973 }
974
975 VERB(verb_activate, "activate", "[ID]", VERB_ANY, 2, 0,
976 "Activate a session");
977 VERB(verb_activate, "lock-session", "[ID…]", VERB_ANY, VERB_ANY, 0,
978 "Screen lock one or more sessions");
979 VERB(verb_activate, "unlock-session", "[ID…]", VERB_ANY, VERB_ANY, 0,
980 "Screen unlock one or more sessions");
981 static int verb_activate(int argc, char *argv[], uintptr_t _data, void *userdata) {
982 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
983 sd_bus *bus = ASSERT_PTR(userdata);
984 int r;
985
986 assert(argv);
987
988 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
989
990 if (argc < 2) {
991 r = sd_bus_call_method(
992 bus,
993 "org.freedesktop.login1",
994 "/org/freedesktop/login1/session/auto",
995 "org.freedesktop.login1.Session",
996 streq(argv[0], "lock-session") ? "Lock" :
997 streq(argv[0], "unlock-session") ? "Unlock" :
998 streq(argv[0], "terminate-session") ? "Terminate" :
999 "Activate",
1000 &error, NULL, NULL);
1001 if (r < 0)
1002 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
1003
1004 return 0;
1005 }
1006
1007 for (int i = 1; i < argc; i++) {
1008 r = bus_call_method(
1009 bus,
1010 bus_login_mgr,
1011 streq(argv[0], "lock-session") ? "LockSession" :
1012 streq(argv[0], "unlock-session") ? "UnlockSession" :
1013 streq(argv[0], "terminate-session") ? "TerminateSession" :
1014 "ActivateSession",
1015 &error, NULL,
1016 "s", argv[i]);
1017 if (r < 0)
1018 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
1019 }
1020
1021 return 0;
1022 }
1023
1024 VERB_NOARG(verb_lock_sessions, "lock-sessions", "Screen lock all current sessions");
1025 VERB_NOARG(verb_lock_sessions, "unlock-sessions", "Screen unlock all current sessions");
1026 static int verb_lock_sessions(int argc, char *argv[], uintptr_t _data, void *userdata) {
1027 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1028 sd_bus *bus = ASSERT_PTR(userdata);
1029 int r;
1030
1031 assert(argv);
1032
1033 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1034
1035 r = bus_call_method(
1036 bus,
1037 bus_login_mgr,
1038 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1039 &error, NULL,
1040 NULL);
1041 if (r < 0)
1042 return log_error_errno(r, "Could not lock sessions: %s", bus_error_message(&error, r));
1043
1044 return 0;
1045 }
1046
1047 /* The implementation is above, but we put this here to preserve the logical order in --help. */
1048 VERB(verb_activate, "terminate-session", "ID…", 2, VERB_ANY, 0,
1049 "Terminate one or more sessions");
1050
1051 VERB(verb_kill_session, "kill-session", "ID…", 2, VERB_ANY, 0,
1052 "Send signal to processes of a session");
1053 static int verb_kill_session(int argc, char *argv[], uintptr_t _data, void *userdata) {
1054 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1055 sd_bus *bus = ASSERT_PTR(userdata);
1056 int r;
1057
1058 assert(argv);
1059
1060 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1061
1062 if (!arg_kill_whom)
1063 arg_kill_whom = "all";
1064
1065 for (int i = 1; i < argc; i++) {
1066 r = bus_call_method(
1067 bus,
1068 bus_login_mgr,
1069 "KillSession",
1070 &error, NULL,
1071 "ssi", argv[i], arg_kill_whom, arg_signal);
1072 if (r < 0)
1073 return log_error_errno(r, "Could not kill session: %s", bus_error_message(&error, r));
1074 }
1075
1076 return 0;
1077 }
1078
1079 VERB_GROUP("User Commands");
1080
1081 VERB_NOARG(verb_list_users, "list-users", "List users");
1082 static int verb_list_users(int argc, char *argv[], uintptr_t _data, void *userdata) {
1083
1084 static const struct bus_properties_map property_map[] = {
1085 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
1086 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
1087 {},
1088 };
1089
1090 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1091 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1092 _cleanup_(table_unrefp) Table *table = NULL;
1093 sd_bus *bus = ASSERT_PTR(userdata);
1094 int r;
1095
1096 assert(argv);
1097
1098 r = bus_call_method(bus, bus_login_mgr, "ListUsers", &error, &reply, NULL);
1099 if (r < 0)
1100 return log_error_errno(r, "Failed to list users: %s", bus_error_message(&error, r));
1101
1102 r = sd_bus_message_enter_container(reply, 'a', "(uso)");
1103 if (r < 0)
1104 return bus_log_parse_error(r);
1105
1106 table = table_new("uid", "user", "linger", "state");
1107 if (!table)
1108 return log_oom();
1109
1110 (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
1111 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
1112
1113 for (;;) {
1114 _cleanup_(sd_bus_error_free) sd_bus_error error_property = SD_BUS_ERROR_NULL;
1115 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_property = NULL;
1116 _cleanup_(user_status_info_done) UserStatusInfo info = {};
1117 const char *user, *object;
1118 uint32_t uid;
1119
1120 r = sd_bus_message_read(reply, "(uso)", &uid, &user, &object);
1121 if (r < 0)
1122 return bus_log_parse_error(r);
1123 if (r == 0)
1124 break;
1125
1126 r = bus_map_all_properties(bus,
1127 "org.freedesktop.login1",
1128 object,
1129 property_map,
1130 BUS_MAP_BOOLEAN_AS_BOOL,
1131 &error_property,
1132 &reply_property,
1133 &info);
1134 if (r < 0) {
1135 log_full_errno(sd_bus_error_has_name(&error_property, SD_BUS_ERROR_UNKNOWN_OBJECT) ? LOG_DEBUG : LOG_WARNING,
1136 r,
1137 "Failed to get properties of user %s, ignoring: %s",
1138 user, bus_error_message(&error_property, r));
1139 continue;
1140 }
1141
1142 r = table_add_many(table,
1143 TABLE_UID, (uid_t) uid,
1144 TABLE_STRING, user,
1145 TABLE_BOOLEAN, info.linger,
1146 TABLE_STRING, info.state);
1147 if (r < 0)
1148 return table_log_add_error(r);
1149 }
1150
1151 r = sd_bus_message_exit_container(reply);
1152 if (r < 0)
1153 return bus_log_parse_error(r);
1154
1155 return list_table_print(table, "users");
1156 }
1157
1158 VERB(verb_show_user, "user-status", "[USER…]", VERB_ANY, VERB_ANY, 0,
1159 "Show user status");
1160 VERB(verb_show_user, "show-user", "[USER…]", VERB_ANY, VERB_ANY, 0,
1161 "Show properties of users or the manager");
1162 static int verb_show_user(int argc, char *argv[], uintptr_t _data, void *userdata) {
1163 sd_bus *bus = ASSERT_PTR(userdata);
1164 bool properties;
1165 int r;
1166
1167 assert(argv);
1168
1169 properties = !strstr(argv[0], "status");
1170
1171 pager_open(arg_pager_flags);
1172
1173 if (argc <= 1) {
1174 /* If no argument is specified inspect the manager itself */
1175 if (properties)
1176 return show_properties(bus, "/org/freedesktop/login1");
1177
1178 return print_user_status_info(bus, "/org/freedesktop/login1/user/self");
1179 }
1180
1181 for (int i = 1, first = true; i < argc; i++, first = false) {
1182 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1183 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1184 const char *path;
1185 uid_t uid;
1186
1187 r = get_user_creds(argv[i], /* flags= */ 0, NULL, &uid, NULL, NULL, NULL);
1188 if (r < 0)
1189 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1190
1191 r = bus_call_method(bus, bus_login_mgr, "GetUser", &error, &reply, "u", (uint32_t) uid);
1192 if (r < 0)
1193 return log_error_errno(r, "Failed to get user: %s", bus_error_message(&error, r));
1194
1195 r = sd_bus_message_read(reply, "o", &path);
1196 if (r < 0)
1197 return bus_log_parse_error(r);
1198
1199 if (!first)
1200 putchar('\n');
1201
1202 if (properties)
1203 r = show_properties(bus, path);
1204 else
1205 r = print_user_status_info(bus, path);
1206 if (r < 0)
1207 return r;
1208 }
1209
1210 return 0;
1211 }
1212
1213 VERB(verb_enable_linger, "enable-linger", "[USER…]", VERB_ANY, VERB_ANY, 0,
1214 "Enable linger state of one or more users");
1215 VERB(verb_enable_linger, "disable-linger", "[USER…]", VERB_ANY, VERB_ANY, 0,
1216 "Disable linger state of one or more users");
1217 static int verb_enable_linger(int argc, char *argv[], uintptr_t _data, void *userdata) {
1218 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1219 sd_bus *bus = ASSERT_PTR(userdata);
1220 char* short_argv[3];
1221 bool b;
1222 int r;
1223
1224 assert(argv);
1225
1226 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1227
1228 b = streq(argv[0], "enable-linger");
1229
1230 if (argc < 2) {
1231 /* No argument? Let's use an empty user name,
1232 * then logind will use our user. */
1233
1234 short_argv[0] = argv[0];
1235 short_argv[1] = (char*) "";
1236 short_argv[2] = NULL;
1237 argv = short_argv;
1238 argc = 2;
1239 }
1240
1241 for (int i = 1; i < argc; i++) {
1242 uid_t uid;
1243
1244 if (isempty(argv[i]))
1245 uid = UID_INVALID;
1246 else {
1247 r = get_user_creds(argv[i], /* flags= */ 0, NULL, &uid, NULL, NULL, NULL);
1248 if (r < 0)
1249 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1250 }
1251
1252 r = bus_call_method(
1253 bus,
1254 bus_login_mgr,
1255 "SetUserLinger",
1256 &error, NULL,
1257 "ubb", (uint32_t) uid, b, true);
1258 if (r < 0)
1259 return log_error_errno(r, "Could not enable linger: %s", bus_error_message(&error, r));
1260 }
1261
1262 return 0;
1263 }
1264
1265 VERB(verb_terminate_user, "terminate-user", "USER…", 2, VERB_ANY, 0,
1266 "Terminate all sessions of one or more users");
1267 static int verb_terminate_user(int argc, char *argv[], uintptr_t _data, void *userdata) {
1268 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1269 sd_bus *bus = ASSERT_PTR(userdata);
1270 int r;
1271
1272 assert(argv);
1273
1274 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1275
1276 for (int i = 1; i < argc; i++) {
1277 uid_t uid;
1278
1279 if (isempty(argv[i]))
1280 uid = getuid();
1281 else {
1282 r = get_user_creds(argv[i], /* flags= */ 0, NULL, &uid, NULL, NULL, NULL);
1283 if (r < 0)
1284 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1285 }
1286
1287 r = bus_call_method(bus, bus_login_mgr, "TerminateUser", &error, NULL, "u", (uint32_t) uid);
1288 if (r < 0)
1289 return log_error_errno(r, "Could not terminate user: %s", bus_error_message(&error, r));
1290 }
1291
1292 return 0;
1293 }
1294
1295 VERB(verb_kill_user, "kill-user", "USER…", 2, VERB_ANY, 0,
1296 "Send signal to processes of a user");
1297 static int verb_kill_user(int argc, char *argv[], uintptr_t _data, void *userdata) {
1298 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1299 sd_bus *bus = ASSERT_PTR(userdata);
1300 int r;
1301
1302 assert(argv);
1303
1304 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1305
1306 if (!arg_kill_whom)
1307 arg_kill_whom = "all";
1308
1309 for (int i = 1; i < argc; i++) {
1310 uid_t uid;
1311
1312 if (isempty(argv[i]))
1313 uid = getuid();
1314 else {
1315 r = get_user_creds(argv[i], /* flags= */ 0, NULL, &uid, NULL, NULL, NULL);
1316 if (r < 0)
1317 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1318 }
1319
1320 r = bus_call_method(
1321 bus,
1322 bus_login_mgr,
1323 "KillUser",
1324 &error, NULL,
1325 "ui", (uint32_t) uid, arg_signal);
1326 if (r < 0)
1327 return log_error_errno(r, "Could not kill user: %s", bus_error_message(&error, r));
1328 }
1329
1330 return 0;
1331 }
1332
1333 VERB_GROUP("Seat Commands");
1334
1335 VERB_NOARG(verb_list_seats, "list-seats", "List seats");
1336 static int verb_list_seats(int argc, char *argv[], uintptr_t _data, void *userdata) {
1337 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1338 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1339 _cleanup_(table_unrefp) Table *table = NULL;
1340 sd_bus *bus = ASSERT_PTR(userdata);
1341 int r;
1342
1343 assert(argv);
1344
1345 r = bus_call_method(bus, bus_login_mgr, "ListSeats", &error, &reply, NULL);
1346 if (r < 0)
1347 return log_error_errno(r, "Failed to list seats: %s", bus_error_message(&error, r));
1348
1349 r = sd_bus_message_enter_container(reply, 'a', "(so)");
1350 if (r < 0)
1351 return bus_log_parse_error(r);
1352
1353 table = table_new("seat");
1354 if (!table)
1355 return log_oom();
1356
1357 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
1358
1359 for (;;) {
1360 const char *seat;
1361
1362 r = sd_bus_message_read(reply, "(so)", &seat, NULL);
1363 if (r < 0)
1364 return bus_log_parse_error(r);
1365 if (r == 0)
1366 break;
1367
1368 r = table_add_cell(table, NULL, TABLE_STRING, seat);
1369 if (r < 0)
1370 return table_log_add_error(r);
1371 }
1372
1373 r = sd_bus_message_exit_container(reply);
1374 if (r < 0)
1375 return bus_log_parse_error(r);
1376
1377 return list_table_print(table, "seats");
1378 }
1379
1380 VERB(verb_show_seat, "seat-status", "[NAME…]", VERB_ANY, VERB_ANY, 0,
1381 "Show seat status");
1382 VERB(verb_show_seat, "show-seat", "[NAME…]", VERB_ANY, VERB_ANY, 0,
1383 "Show properties of seats or the manager");
1384 static int verb_show_seat(int argc, char *argv[], uintptr_t _data, void *userdata) {
1385 sd_bus *bus = ASSERT_PTR(userdata);
1386 bool properties;
1387 int r;
1388
1389 assert(argv);
1390
1391 properties = !strstr(argv[0], "status");
1392
1393 pager_open(arg_pager_flags);
1394
1395 if (argc <= 1) {
1396 _cleanup_free_ char *path = NULL;
1397
1398 /* If no argument is specified inspect the manager itself */
1399 if (properties)
1400 return show_properties(bus, "/org/freedesktop/login1");
1401
1402 r = get_bus_path_by_id(bus, "seat", "GetSeat", "auto", &path);
1403 if (r < 0)
1404 return r;
1405
1406 return print_seat_status_info(bus, path);
1407 }
1408
1409 for (int i = 1, first = true; i < argc; i++, first = false) {
1410 _cleanup_free_ char *path = NULL;
1411
1412 r = get_bus_path_by_id(bus, "seat", "GetSeat", argv[i], &path);
1413 if (r < 0)
1414 return r;
1415
1416 if (!first)
1417 putchar('\n');
1418
1419 if (properties)
1420 r = show_properties(bus, path);
1421 else
1422 r = print_seat_status_info(bus, path);
1423 if (r < 0)
1424 return r;
1425 }
1426
1427 return 0;
1428 }
1429
1430 VERB(verb_attach, "attach", "NAME DEVICE…", 3, VERB_ANY, 0,
1431 "Attach one or more devices to a seat");
1432 static int verb_attach(int argc, char *argv[], uintptr_t _data, void *userdata) {
1433 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1434 sd_bus *bus = ASSERT_PTR(userdata);
1435 int r;
1436
1437 assert(argv);
1438
1439 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1440
1441 for (int i = 2; i < argc; i++) {
1442
1443 r = bus_call_method(
1444 bus,
1445 bus_login_mgr,
1446 "AttachDevice",
1447 &error, NULL,
1448 "ssb", argv[1], argv[i], true);
1449 if (r < 0)
1450 return log_error_errno(r, "Could not attach device: %s", bus_error_message(&error, r));
1451 }
1452
1453 return 0;
1454 }
1455
1456 VERB_NOARG(verb_flush_devices, "flush-devices", "Flush all device associations");
1457 static int verb_flush_devices(int argc, char *argv[], uintptr_t _data, void *userdata) {
1458 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1459 sd_bus *bus = ASSERT_PTR(userdata);
1460 int r;
1461
1462 assert(argv);
1463
1464 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1465
1466 r = bus_call_method(bus, bus_login_mgr, "FlushDevices", &error, NULL, "b", true);
1467 if (r < 0)
1468 return log_error_errno(r, "Could not flush devices: %s", bus_error_message(&error, r));
1469
1470 return 0;
1471 }
1472
1473 VERB(verb_terminate_seat, "terminate-seat", "NAME…", 2, VERB_ANY, 0,
1474 "Terminate all sessions on one or more seats");
1475 static int verb_terminate_seat(int argc, char *argv[], uintptr_t _data, void *userdata) {
1476 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1477 sd_bus *bus = ASSERT_PTR(userdata);
1478 int r;
1479
1480 assert(argv);
1481
1482 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1483
1484 for (int i = 1; i < argc; i++) {
1485
1486 r = bus_call_method(bus, bus_login_mgr, "TerminateSeat", &error, NULL, "s", argv[i]);
1487 if (r < 0)
1488 return log_error_errno(r, "Could not terminate seat: %s", bus_error_message(&error, r));
1489 }
1490
1491 return 0;
1492 }
1493
1494 static int help(void) {
1495 static const char *const groups[] = {
1496 "Session Commands",
1497 "User Commands",
1498 "Seat Commands",
1499 };
1500
1501 Table *vtables[ELEMENTSOF(groups)] = {};
1502 CLEANUP_ELEMENTS(vtables, table_unref_array_clear);
1503 _cleanup_(table_unrefp) Table *options = NULL;
1504 int r;
1505
1506 pager_open(arg_pager_flags);
1507
1508 for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
1509 r = verbs_get_help_table_group(groups[i], &vtables[i]);
1510 if (r < 0)
1511 return r;
1512 }
1513
1514 r = option_parser_get_help_table(&options);
1515 if (r < 0)
1516 return r;
1517
1518 assert_cc(ELEMENTSOF(vtables) == 3);
1519 (void) table_sync_column_widths(0, vtables[0], vtables[1], vtables[2], options);
1520
1521 help_cmdline("[OPTIONS…] COMMAND …");
1522 help_abstract("Send control commands to or query the login manager.");
1523
1524 for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
1525 help_section(groups[i]);
1526 r = table_print_or_warn(vtables[i]);
1527 if (r < 0)
1528 return r;
1529 }
1530
1531 help_section("Options");
1532 r = table_print_or_warn(options);
1533 if (r < 0)
1534 return r;
1535
1536 help_man_page_reference("loginctl", "1");
1537 return 0;
1538 }
1539
1540 VERB_COMMON_HELP_HIDDEN(help);
1541
1542 static int parse_argv(int argc, char *argv[], char ***remaining_args) {
1543 int r;
1544
1545 assert(argc >= 0);
1546 assert(argv);
1547 assert(remaining_args);
1548
1549 OptionParser opts = { argc, argv };
1550
1551 FOREACH_OPTION_OR_RETURN(c, &opts)
1552 switch (c) {
1553
1554 OPTION_COMMON_HELP:
1555 return help();
1556
1557 OPTION_COMMON_VERSION:
1558 return version();
1559
1560 OPTION_COMMON_HOST:
1561 arg_transport = BUS_TRANSPORT_REMOTE;
1562 arg_host = opts.arg;
1563 break;
1564
1565 OPTION_COMMON_MACHINE:
1566 r = parse_machine_argument(opts.arg, &arg_host, &arg_transport);
1567 if (r < 0)
1568 return r;
1569 break;
1570
1571 OPTION('p', "property", "NAME", "Show only properties by this name"): {}
1572 OPTION_SHORT('P', "NAME", "Equivalent to --value --property=NAME"):
1573 r = strv_extend(&arg_property, opts.arg);
1574 if (r < 0)
1575 return log_oom();
1576
1577 /* If the user asked for a particular property, show it to them, even if it is
1578 * empty. */
1579 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
1580
1581 if (opts.opt->short_code == 'P')
1582 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
1583
1584 break;
1585
1586 OPTION('a', "all", NULL, "Show all properties, including empty ones"):
1587 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
1588 break;
1589
1590 OPTION_LONG("value", NULL, "When showing properties, only print the value"):
1591 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
1592 break;
1593
1594 OPTION('l', "full", NULL, "Do not ellipsize output"):
1595 arg_full = true;
1596 break;
1597
1598 OPTION_LONG("kill-whom", "WHOM", "Whom to send signal to"):
1599 arg_kill_whom = opts.arg;
1600 break;
1601
1602 OPTION('s', "signal", "SIGNAL", "Which signal to send"):
1603 r = parse_signal_argument(opts.arg, &arg_signal);
1604 if (r <= 0)
1605 return r;
1606 break;
1607
1608 OPTION('n', "lines", "INTEGER", "Number of journal entries to show"):
1609 if (safe_atou(opts.arg, &arg_lines) < 0)
1610 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1611 "Failed to parse lines '%s'", opts.arg);
1612 break;
1613
1614 OPTION_COMMON_JSON:
1615 r = parse_json_argument(opts.arg, &arg_json_format_flags);
1616 if (r <= 0)
1617 return r;
1618
1619 if (sd_json_format_enabled(arg_json_format_flags))
1620 arg_legend = false;
1621
1622 break;
1623
1624 OPTION_COMMON_LOWERCASE_J:
1625 arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO;
1626 arg_legend = false;
1627 break;
1628
1629 OPTION('o', "output", "MODE",
1630 "Change journal output mode (short, short-precise, short-iso, "
1631 "short-iso-precise, short-full, short-monotonic, short-unix, short-delta, "
1632 "json, json-pretty, json-sse, json-seq, cat, verbose, export, with-unit)"):
1633 if (streq(opts.arg, "help"))
1634 return DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
1635
1636 arg_output = output_mode_from_string(opts.arg);
1637 if (arg_output < 0)
1638 return log_error_errno(arg_output, "Unknown output '%s'.", opts.arg);
1639
1640 break;
1641
1642 OPTION_COMMON_NO_PAGER:
1643 arg_pager_flags |= PAGER_DISABLE;
1644 break;
1645
1646 OPTION_COMMON_NO_LEGEND:
1647 arg_legend = false;
1648 break;
1649
1650 OPTION_COMMON_NO_ASK_PASSWORD:
1651 arg_ask_password = false;
1652 break;
1653 }
1654
1655 *remaining_args = option_parser_get_args(&opts);
1656 return 1;
1657 }
1658
1659 static int run(int argc, char *argv[]) {
1660 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1661 char **args = NULL;
1662 int r;
1663
1664 setlocale(LC_ALL, "");
1665 log_setup();
1666
1667 r = parse_argv(argc, argv, &args);
1668 if (r <= 0)
1669 return r;
1670
1671 journal_browse_prepare();
1672
1673 r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
1674 if (r < 0)
1675 return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
1676
1677 (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1678
1679 return dispatch_verb(args, bus);
1680 }
1681
1682 DEFINE_MAIN_FUNCTION(run);