]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/loginctl.c
Merge pull request #8817 from yuwata/cleanup-nsflags
[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], *s1;
478 char since2[FORMAT_TIMESTAMP_MAX], *s2;
479 SessionStatusInfo i = {};
480 int r;
481
482 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
483 if (r < 0)
484 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
485
486 if (*new_line)
487 printf("\n");
488
489 *new_line = true;
490
491 printf("%s - ", strna(i.id));
492
493 if (i.name)
494 printf("%s (%"PRIu32")\n", i.name, i.uid);
495 else
496 printf("%"PRIu32"\n", i.uid);
497
498 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
499 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
500
501 if (s1)
502 printf("\t Since: %s; %s\n", s2, s1);
503 else if (s2)
504 printf("\t Since: %s\n", s2);
505
506 if (i.leader > 0) {
507 _cleanup_free_ char *t = NULL;
508
509 printf("\t Leader: %"PRIu32, i.leader);
510
511 get_process_comm(i.leader, &t);
512 if (t)
513 printf(" (%s)", t);
514
515 printf("\n");
516 }
517
518 if (!isempty(i.seat)) {
519 printf("\t Seat: %s", i.seat);
520
521 if (i.vtnr > 0)
522 printf("; vc%u", i.vtnr);
523
524 printf("\n");
525 }
526
527 if (i.tty)
528 printf("\t TTY: %s\n", i.tty);
529 else if (i.display)
530 printf("\t Display: %s\n", i.display);
531
532 if (i.remote_host && i.remote_user)
533 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
534 else if (i.remote_host)
535 printf("\t Remote: %s\n", i.remote_host);
536 else if (i.remote_user)
537 printf("\t Remote: user %s\n", i.remote_user);
538 else if (i.remote)
539 printf("\t Remote: Yes\n");
540
541 if (i.service) {
542 printf("\t Service: %s", i.service);
543
544 if (i.type)
545 printf("; type %s", i.type);
546
547 if (i.class)
548 printf("; class %s", i.class);
549
550 printf("\n");
551 } else if (i.type) {
552 printf("\t Type: %s", i.type);
553
554 if (i.class)
555 printf("; class %s", i.class);
556
557 printf("\n");
558 } else if (i.class)
559 printf("\t Class: %s\n", i.class);
560
561 if (!isempty(i.desktop))
562 printf("\t Desktop: %s\n", i.desktop);
563
564 if (i.state)
565 printf("\t State: %s\n", i.state);
566
567 if (i.scope) {
568 printf("\t Unit: %s\n", i.scope);
569 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
570
571 if (arg_transport == BUS_TRANSPORT_LOCAL) {
572
573 show_journal_by_unit(
574 stdout,
575 i.scope,
576 arg_output,
577 0,
578 i.timestamp.monotonic,
579 arg_lines,
580 0,
581 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
582 SD_JOURNAL_LOCAL_ONLY,
583 true,
584 NULL);
585 }
586 }
587
588 return 0;
589 }
590
591 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
592
593 static const struct bus_properties_map map[] = {
594 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
595 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
596 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
597 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
598 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
599 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
600 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
601 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
602 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
603 {}
604 };
605
606 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
607 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
608 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
609 char since2[FORMAT_TIMESTAMP_MAX], *s2;
610 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
611 int r;
612
613 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
614 if (r < 0)
615 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
616
617 if (*new_line)
618 printf("\n");
619
620 *new_line = true;
621
622 if (i.name)
623 printf("%s (%"PRIu32")\n", i.name, i.uid);
624 else
625 printf("%"PRIu32"\n", i.uid);
626
627 s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
628 s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
629
630 if (s1)
631 printf("\t Since: %s; %s\n", s2, s1);
632 else if (s2)
633 printf("\t Since: %s\n", s2);
634
635 if (!isempty(i.state))
636 printf("\t State: %s\n", i.state);
637
638 if (!strv_isempty(i.sessions)) {
639 char **l;
640 printf("\tSessions:");
641
642 STRV_FOREACH(l, i.sessions)
643 printf(" %s%s",
644 streq_ptr(*l, i.display) ? "*" : "",
645 *l);
646
647 printf("\n");
648 }
649
650 printf("\t Linger: %s\n", yes_no(i.linger));
651
652 if (i.slice) {
653 printf("\t Unit: %s\n", i.slice);
654 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
655
656 show_journal_by_unit(
657 stdout,
658 i.slice,
659 arg_output,
660 0,
661 i.timestamp.monotonic,
662 arg_lines,
663 0,
664 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
665 SD_JOURNAL_LOCAL_ONLY,
666 true,
667 NULL);
668 }
669
670 return 0;
671 }
672
673 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
674
675 static const struct bus_properties_map map[] = {
676 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
677 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
678 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
679 {}
680 };
681
682 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
683 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
684 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
685 int r;
686
687 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, 0, &error, &m, &i);
688 if (r < 0)
689 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
690
691 if (*new_line)
692 printf("\n");
693
694 *new_line = true;
695
696 printf("%s\n", strna(i.id));
697
698 if (!strv_isempty(i.sessions)) {
699 char **l;
700 printf("\tSessions:");
701
702 STRV_FOREACH(l, i.sessions) {
703 if (streq_ptr(*l, i.active_session))
704 printf(" *%s", *l);
705 else
706 printf(" %s", *l);
707 }
708
709 printf("\n");
710 }
711
712 if (arg_transport == BUS_TRANSPORT_LOCAL) {
713 unsigned c;
714
715 c = columns();
716 if (c > 21)
717 c -= 21;
718 else
719 c = 0;
720
721 printf("\t Devices:\n");
722
723 show_sysfs(i.id, "\t\t ", c, get_output_flags());
724 }
725
726 return 0;
727 }
728
729 #define property(name, fmt, ...) \
730 do { \
731 if (value) \
732 printf(fmt "\n", __VA_ARGS__); \
733 else \
734 printf("%s=" fmt "\n", name, __VA_ARGS__); \
735 } while (0)
736
737 static int print_property(const char *name, sd_bus_message *m, bool value, bool all) {
738 char type;
739 const char *contents;
740 int r;
741
742 assert(name);
743 assert(m);
744
745 r = sd_bus_message_peek_type(m, &type, &contents);
746 if (r < 0)
747 return r;
748
749 switch (type) {
750
751 case SD_BUS_TYPE_STRUCT:
752
753 if (contents[0] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
754 const char *s;
755
756 r = sd_bus_message_read(m, "(so)", &s, NULL);
757 if (r < 0)
758 return bus_log_parse_error(r);
759
760 if (all || !isempty(s))
761 property(name, "%s", s);
762
763 return 1;
764
765 } else if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
766 uint32_t uid;
767
768 r = sd_bus_message_read(m, "(uo)", &uid, NULL);
769 if (r < 0)
770 return bus_log_parse_error(r);
771
772 if (!uid_is_valid(uid)) {
773 log_error("Invalid user ID: " UID_FMT, uid);
774 return -EINVAL;
775 }
776
777 property(name, UID_FMT, uid);
778 return 1;
779 }
780 break;
781
782 case SD_BUS_TYPE_ARRAY:
783
784 if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
785 const char *s;
786 bool space = false;
787
788 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
789 if (r < 0)
790 return bus_log_parse_error(r);
791
792 if (!value)
793 printf("%s=", name);
794
795 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
796 printf("%s%s", space ? " " : "", s);
797 space = true;
798 }
799
800 if (space || !value)
801 printf("\n");
802
803 if (r < 0)
804 return bus_log_parse_error(r);
805
806 r = sd_bus_message_exit_container(m);
807 if (r < 0)
808 return bus_log_parse_error(r);
809
810 return 1;
811 }
812 break;
813 }
814
815 return 0;
816 }
817
818 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
819 int r;
820
821 assert(bus);
822 assert(path);
823 assert(new_line);
824
825 if (*new_line)
826 printf("\n");
827
828 *new_line = true;
829
830 r = bus_print_all_properties(bus, "org.freedesktop.login1", path, print_property, arg_property, arg_value, arg_all, NULL);
831 if (r < 0)
832 return bus_log_parse_error(r);
833
834 return 0;
835 }
836
837 static int show_session(int argc, char *argv[], void *userdata) {
838 bool properties, new_line = false;
839 sd_bus *bus = userdata;
840 int r, i;
841 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
842 _cleanup_free_ char *path = NULL;
843
844 assert(bus);
845 assert(argv);
846
847 properties = !strstr(argv[0], "status");
848
849 (void) pager_open(arg_no_pager, false);
850
851 if (argc <= 1) {
852 const char *session, *p = "/org/freedesktop/login1/session/self";
853
854 if (properties)
855 /* If no argument is specified inspect the manager itself */
856 return show_properties(bus, "/org/freedesktop/login1", &new_line);
857
858 /* And in the pretty case, show data of the calling session */
859 session = getenv("XDG_SESSION_ID");
860 if (session) {
861 r = get_session_path(bus, session, &error, &path);
862 if (r < 0) {
863 log_error("Failed to get session path: %s", bus_error_message(&error, r));
864 return r;
865 }
866 p = path;
867 }
868
869 return print_session_status_info(bus, p, &new_line);
870 }
871
872 for (i = 1; i < argc; i++) {
873 r = get_session_path(bus, argv[i], &error, &path);
874 if (r < 0) {
875 log_error("Failed to get session path: %s", bus_error_message(&error, r));
876 return r;
877 }
878
879 if (properties)
880 r = show_properties(bus, path, &new_line);
881 else
882 r = print_session_status_info(bus, path, &new_line);
883
884 if (r < 0)
885 return r;
886 }
887
888 return 0;
889 }
890
891 static int show_user(int argc, char *argv[], void *userdata) {
892 bool properties, new_line = false;
893 sd_bus *bus = userdata;
894 int r, i;
895
896 assert(bus);
897 assert(argv);
898
899 properties = !strstr(argv[0], "status");
900
901 (void) pager_open(arg_no_pager, false);
902
903 if (argc <= 1) {
904 /* If not argument is specified inspect the manager
905 * itself */
906 if (properties)
907 return show_properties(bus, "/org/freedesktop/login1", &new_line);
908
909 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
910 }
911
912 for (i = 1; i < argc; i++) {
913 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
914 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
915 const char *path = NULL;
916 uid_t uid;
917
918 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
919 if (r < 0)
920 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
921
922 r = sd_bus_call_method(
923 bus,
924 "org.freedesktop.login1",
925 "/org/freedesktop/login1",
926 "org.freedesktop.login1.Manager",
927 "GetUser",
928 &error, &reply,
929 "u", (uint32_t) uid);
930 if (r < 0) {
931 log_error("Failed to get user: %s", bus_error_message(&error, r));
932 return r;
933 }
934
935 r = sd_bus_message_read(reply, "o", &path);
936 if (r < 0)
937 return bus_log_parse_error(r);
938
939 if (properties)
940 r = show_properties(bus, path, &new_line);
941 else
942 r = print_user_status_info(bus, path, &new_line);
943
944 if (r < 0)
945 return r;
946 }
947
948 return 0;
949 }
950
951 static int show_seat(int argc, char *argv[], void *userdata) {
952 bool properties, new_line = false;
953 sd_bus *bus = userdata;
954 int r, i;
955
956 assert(bus);
957 assert(argv);
958
959 properties = !strstr(argv[0], "status");
960
961 (void) pager_open(arg_no_pager, false);
962
963 if (argc <= 1) {
964 /* If not argument is specified inspect the manager
965 * itself */
966 if (properties)
967 return show_properties(bus, "/org/freedesktop/login1", &new_line);
968
969 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
970 }
971
972 for (i = 1; i < argc; i++) {
973 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
974 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
975 const char *path = NULL;
976
977 r = sd_bus_call_method(
978 bus,
979 "org.freedesktop.login1",
980 "/org/freedesktop/login1",
981 "org.freedesktop.login1.Manager",
982 "GetSeat",
983 &error, &reply,
984 "s", argv[i]);
985 if (r < 0) {
986 log_error("Failed to get seat: %s", bus_error_message(&error, r));
987 return r;
988 }
989
990 r = sd_bus_message_read(reply, "o", &path);
991 if (r < 0)
992 return bus_log_parse_error(r);
993
994 if (properties)
995 r = show_properties(bus, path, &new_line);
996 else
997 r = print_seat_status_info(bus, path, &new_line);
998
999 if (r < 0)
1000 return r;
1001 }
1002
1003 return 0;
1004 }
1005
1006 static int activate(int argc, char *argv[], void *userdata) {
1007 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1008 sd_bus *bus = userdata;
1009 char *short_argv[3];
1010 int r, i;
1011
1012 assert(bus);
1013 assert(argv);
1014
1015 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1016
1017 if (argc < 2) {
1018 /* No argument? Let's either use $XDG_SESSION_ID (if specified), or an empty
1019 * session name, in which case logind will try to guess our session. */
1020
1021 short_argv[0] = argv[0];
1022 short_argv[1] = getenv("XDG_SESSION_ID") ?: (char*) "";
1023 short_argv[2] = NULL;
1024
1025 argv = short_argv;
1026 argc = 2;
1027 }
1028
1029 for (i = 1; i < argc; i++) {
1030
1031 r = sd_bus_call_method(
1032 bus,
1033 "org.freedesktop.login1",
1034 "/org/freedesktop/login1",
1035 "org.freedesktop.login1.Manager",
1036 streq(argv[0], "lock-session") ? "LockSession" :
1037 streq(argv[0], "unlock-session") ? "UnlockSession" :
1038 streq(argv[0], "terminate-session") ? "TerminateSession" :
1039 "ActivateSession",
1040 &error, NULL,
1041 "s", argv[i]);
1042 if (r < 0) {
1043 log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1044 return r;
1045 }
1046 }
1047
1048 return 0;
1049 }
1050
1051 static int kill_session(int argc, char *argv[], void *userdata) {
1052 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1053 sd_bus *bus = userdata;
1054 int r, i;
1055
1056 assert(bus);
1057 assert(argv);
1058
1059 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1060
1061 if (!arg_kill_who)
1062 arg_kill_who = "all";
1063
1064 for (i = 1; i < argc; i++) {
1065
1066 r = sd_bus_call_method(
1067 bus,
1068 "org.freedesktop.login1",
1069 "/org/freedesktop/login1",
1070 "org.freedesktop.login1.Manager",
1071 "KillSession",
1072 &error, NULL,
1073 "ssi", argv[i], arg_kill_who, arg_signal);
1074 if (r < 0) {
1075 log_error("Could not kill session: %s", bus_error_message(&error, -r));
1076 return r;
1077 }
1078 }
1079
1080 return 0;
1081 }
1082
1083 static int enable_linger(int argc, char *argv[], void *userdata) {
1084 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1085 sd_bus *bus = userdata;
1086 char* short_argv[3];
1087 bool b;
1088 int r, i;
1089
1090 assert(bus);
1091 assert(argv);
1092
1093 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1094
1095 b = streq(argv[0], "enable-linger");
1096
1097 if (argc < 2) {
1098 /* No argument? Let's use an empty user name,
1099 * then logind will use our user. */
1100
1101 short_argv[0] = argv[0];
1102 short_argv[1] = (char*) "";
1103 short_argv[2] = NULL;
1104 argv = short_argv;
1105 argc = 2;
1106 }
1107
1108 for (i = 1; i < argc; i++) {
1109 uid_t uid;
1110
1111 if (isempty(argv[i]))
1112 uid = UID_INVALID;
1113 else {
1114 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1115 if (r < 0)
1116 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1117 }
1118
1119 r = sd_bus_call_method(
1120 bus,
1121 "org.freedesktop.login1",
1122 "/org/freedesktop/login1",
1123 "org.freedesktop.login1.Manager",
1124 "SetUserLinger",
1125 &error, NULL,
1126 "ubb", (uint32_t) uid, b, true);
1127 if (r < 0) {
1128 log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1129 return r;
1130 }
1131 }
1132
1133 return 0;
1134 }
1135
1136 static int terminate_user(int argc, char *argv[], void *userdata) {
1137 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1138 sd_bus *bus = userdata;
1139 int r, i;
1140
1141 assert(bus);
1142 assert(argv);
1143
1144 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1145
1146 for (i = 1; i < argc; i++) {
1147 uid_t uid;
1148
1149 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1150 if (r < 0)
1151 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1152
1153 r = sd_bus_call_method(
1154 bus,
1155 "org.freedesktop.login1",
1156 "/org/freedesktop/login1",
1157 "org.freedesktop.login1.Manager",
1158 "TerminateUser",
1159 &error, NULL,
1160 "u", (uint32_t) uid);
1161 if (r < 0) {
1162 log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1163 return r;
1164 }
1165 }
1166
1167 return 0;
1168 }
1169
1170 static int kill_user(int argc, char *argv[], void *userdata) {
1171 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1172 sd_bus *bus = userdata;
1173 int r, i;
1174
1175 assert(bus);
1176 assert(argv);
1177
1178 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1179
1180 if (!arg_kill_who)
1181 arg_kill_who = "all";
1182
1183 for (i = 1; i < argc; i++) {
1184 uid_t uid;
1185
1186 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1187 if (r < 0)
1188 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1189
1190 r = sd_bus_call_method(
1191 bus,
1192 "org.freedesktop.login1",
1193 "/org/freedesktop/login1",
1194 "org.freedesktop.login1.Manager",
1195 "KillUser",
1196 &error, NULL,
1197 "ui", (uint32_t) uid, arg_signal);
1198 if (r < 0) {
1199 log_error("Could not kill user: %s", bus_error_message(&error, -r));
1200 return r;
1201 }
1202 }
1203
1204 return 0;
1205 }
1206
1207 static int attach(int argc, char *argv[], void *userdata) {
1208 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1209 sd_bus *bus = userdata;
1210 int r, i;
1211
1212 assert(bus);
1213 assert(argv);
1214
1215 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1216
1217 for (i = 2; i < argc; i++) {
1218
1219 r = sd_bus_call_method(
1220 bus,
1221 "org.freedesktop.login1",
1222 "/org/freedesktop/login1",
1223 "org.freedesktop.login1.Manager",
1224 "AttachDevice",
1225 &error, NULL,
1226 "ssb", argv[1], argv[i], true);
1227
1228 if (r < 0) {
1229 log_error("Could not attach device: %s", bus_error_message(&error, -r));
1230 return r;
1231 }
1232 }
1233
1234 return 0;
1235 }
1236
1237 static int flush_devices(int argc, char *argv[], void *userdata) {
1238 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1239 sd_bus *bus = userdata;
1240 int r;
1241
1242 assert(bus);
1243 assert(argv);
1244
1245 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1246
1247 r = sd_bus_call_method(
1248 bus,
1249 "org.freedesktop.login1",
1250 "/org/freedesktop/login1",
1251 "org.freedesktop.login1.Manager",
1252 "FlushDevices",
1253 &error, NULL,
1254 "b", true);
1255 if (r < 0)
1256 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1257
1258 return r;
1259 }
1260
1261 static int lock_sessions(int argc, char *argv[], void *userdata) {
1262 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1263 sd_bus *bus = userdata;
1264 int r;
1265
1266 assert(bus);
1267 assert(argv);
1268
1269 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1270
1271 r = sd_bus_call_method(
1272 bus,
1273 "org.freedesktop.login1",
1274 "/org/freedesktop/login1",
1275 "org.freedesktop.login1.Manager",
1276 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1277 &error, NULL,
1278 NULL);
1279 if (r < 0)
1280 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1281
1282 return r;
1283 }
1284
1285 static int terminate_seat(int argc, char *argv[], void *userdata) {
1286 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1287 sd_bus *bus = userdata;
1288 int r, i;
1289
1290 assert(bus);
1291 assert(argv);
1292
1293 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1294
1295 for (i = 1; i < argc; i++) {
1296
1297 r = sd_bus_call_method(
1298 bus,
1299 "org.freedesktop.login1",
1300 "/org/freedesktop/login1",
1301 "org.freedesktop.login1.Manager",
1302 "TerminateSeat",
1303 &error, NULL,
1304 "s", argv[i]);
1305 if (r < 0) {
1306 log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1307 return r;
1308 }
1309 }
1310
1311 return 0;
1312 }
1313
1314 static int help(int argc, char *argv[], void *userdata) {
1315
1316 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1317 "Send control commands to or query the login manager.\n\n"
1318 " -h --help Show this help\n"
1319 " --version Show package version\n"
1320 " --no-pager Do not pipe output into a pager\n"
1321 " --no-legend Do not show the headers and footers\n"
1322 " --no-ask-password Don't prompt for password\n"
1323 " -H --host=[USER@]HOST Operate on remote host\n"
1324 " -M --machine=CONTAINER Operate on local container\n"
1325 " -p --property=NAME Show only properties by this name\n"
1326 " -a --all Show all properties, including empty ones\n"
1327 " --value When showing properties, only print the value\n"
1328 " -l --full Do not ellipsize output\n"
1329 " --kill-who=WHO Who to send signal to\n"
1330 " -s --signal=SIGNAL Which signal to send\n"
1331 " -n --lines=INTEGER Number of journal entries to show\n"
1332 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1333 " short-iso, short-iso-precise, short-full,\n"
1334 " short-monotonic, short-unix, verbose, export,\n"
1335 " json, json-pretty, json-sse, cat)\n"
1336 "Session Commands:\n"
1337 " list-sessions List sessions\n"
1338 " session-status [ID...] Show session status\n"
1339 " show-session [ID...] Show properties of sessions or the manager\n"
1340 " activate [ID] Activate a session\n"
1341 " lock-session [ID...] Screen lock one or more sessions\n"
1342 " unlock-session [ID...] Screen unlock one or more sessions\n"
1343 " lock-sessions Screen lock all current sessions\n"
1344 " unlock-sessions Screen unlock all current sessions\n"
1345 " terminate-session ID... Terminate one or more sessions\n"
1346 " kill-session ID... Send signal to processes of a session\n\n"
1347 "User Commands:\n"
1348 " list-users List users\n"
1349 " user-status [USER...] Show user status\n"
1350 " show-user [USER...] Show properties of users or the manager\n"
1351 " enable-linger [USER...] Enable linger state of one or more users\n"
1352 " disable-linger [USER...] Disable linger state of one or more users\n"
1353 " terminate-user USER... Terminate all sessions of one or more users\n"
1354 " kill-user USER... Send signal to processes of a user\n\n"
1355 "Seat Commands:\n"
1356 " list-seats List seats\n"
1357 " seat-status [NAME...] Show seat status\n"
1358 " show-seat [NAME...] Show properties of seats or the manager\n"
1359 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1360 " flush-devices Flush all device associations\n"
1361 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
1362 , program_invocation_short_name);
1363
1364 return 0;
1365 }
1366
1367 static int parse_argv(int argc, char *argv[]) {
1368
1369 enum {
1370 ARG_VERSION = 0x100,
1371 ARG_VALUE,
1372 ARG_NO_PAGER,
1373 ARG_NO_LEGEND,
1374 ARG_KILL_WHO,
1375 ARG_NO_ASK_PASSWORD,
1376 };
1377
1378 static const struct option options[] = {
1379 { "help", no_argument, NULL, 'h' },
1380 { "version", no_argument, NULL, ARG_VERSION },
1381 { "property", required_argument, NULL, 'p' },
1382 { "all", no_argument, NULL, 'a' },
1383 { "value", no_argument, NULL, ARG_VALUE },
1384 { "full", no_argument, NULL, 'l' },
1385 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
1386 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1387 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1388 { "signal", required_argument, NULL, 's' },
1389 { "host", required_argument, NULL, 'H' },
1390 { "machine", required_argument, NULL, 'M' },
1391 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
1392 { "lines", required_argument, NULL, 'n' },
1393 { "output", required_argument, NULL, 'o' },
1394 {}
1395 };
1396
1397 int c, r;
1398
1399 assert(argc >= 0);
1400 assert(argv);
1401
1402 while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1403
1404 switch (c) {
1405
1406 case 'h':
1407 help(0, NULL, NULL);
1408 return 0;
1409
1410 case ARG_VERSION:
1411 return version();
1412
1413 case 'p': {
1414 r = strv_extend(&arg_property, optarg);
1415 if (r < 0)
1416 return log_oom();
1417
1418 /* If the user asked for a particular
1419 * property, show it to him, even if it is
1420 * empty. */
1421 arg_all = true;
1422 break;
1423 }
1424
1425 case 'a':
1426 arg_all = true;
1427 break;
1428
1429 case ARG_VALUE:
1430 arg_value = true;
1431 break;
1432
1433 case 'l':
1434 arg_full = true;
1435 break;
1436
1437 case 'n':
1438 if (safe_atou(optarg, &arg_lines) < 0) {
1439 log_error("Failed to parse lines '%s'", optarg);
1440 return -EINVAL;
1441 }
1442 break;
1443
1444 case 'o':
1445 if (streq(optarg, "help")) {
1446 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
1447 return 0;
1448 }
1449
1450 arg_output = output_mode_from_string(optarg);
1451 if (arg_output < 0) {
1452 log_error("Unknown output '%s'.", optarg);
1453 return -EINVAL;
1454 }
1455 break;
1456
1457 case ARG_NO_PAGER:
1458 arg_no_pager = true;
1459 break;
1460
1461 case ARG_NO_LEGEND:
1462 arg_legend = false;
1463 break;
1464
1465 case ARG_NO_ASK_PASSWORD:
1466 arg_ask_password = false;
1467 break;
1468
1469 case ARG_KILL_WHO:
1470 arg_kill_who = optarg;
1471 break;
1472
1473 case 's':
1474 if (streq(optarg, "help")) {
1475 DUMP_STRING_TABLE(signal, int, _NSIG);
1476 return 0;
1477 }
1478
1479 arg_signal = signal_from_string(optarg);
1480 if (arg_signal < 0) {
1481 log_error("Failed to parse signal string %s.", optarg);
1482 return -EINVAL;
1483 }
1484 break;
1485
1486 case 'H':
1487 arg_transport = BUS_TRANSPORT_REMOTE;
1488 arg_host = optarg;
1489 break;
1490
1491 case 'M':
1492 arg_transport = BUS_TRANSPORT_MACHINE;
1493 arg_host = optarg;
1494 break;
1495
1496 case '?':
1497 return -EINVAL;
1498
1499 default:
1500 assert_not_reached("Unhandled option");
1501 }
1502
1503 return 1;
1504 }
1505
1506 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1507
1508 static const Verb verbs[] = {
1509 { "help", VERB_ANY, VERB_ANY, 0, help },
1510 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
1511 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
1512 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
1513 { "activate", VERB_ANY, 2, 0, activate },
1514 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1515 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
1516 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1517 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1518 { "terminate-session", 2, VERB_ANY, 0, activate },
1519 { "kill-session", 2, VERB_ANY, 0, kill_session },
1520 { "list-users", VERB_ANY, 1, 0, list_users },
1521 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
1522 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
1523 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1524 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1525 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1526 { "kill-user", 2, VERB_ANY, 0, kill_user },
1527 { "list-seats", VERB_ANY, 1, 0, list_seats },
1528 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1529 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
1530 { "attach", 3, VERB_ANY, 0, attach },
1531 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1532 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1533 {}
1534 };
1535
1536 return dispatch_verb(argc, argv, verbs, bus);
1537 }
1538
1539 int main(int argc, char *argv[]) {
1540 sd_bus *bus = NULL;
1541 int r;
1542
1543 setlocale(LC_ALL, "");
1544 log_parse_environment();
1545 log_open();
1546 sigbus_install();
1547
1548 r = parse_argv(argc, argv);
1549 if (r <= 0)
1550 goto finish;
1551
1552 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1553 if (r < 0) {
1554 log_error_errno(r, "Failed to create bus connection: %m");
1555 goto finish;
1556 }
1557
1558 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1559
1560 r = loginctl_main(argc, argv, bus);
1561
1562 finish:
1563 /* make sure we terminate the bus connection first, and then close the
1564 * pager, see issue #3543 for the details. */
1565 sd_bus_flush_close_unref(bus);
1566 pager_close();
1567 polkit_agent_close();
1568
1569 strv_free(arg_property);
1570
1571 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1572 }