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