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