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