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