]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/loginctl.c
strv: make iterator in STRV_FOREACH() declaread in the loop
[thirdparty/systemd.git] / src / login / loginctl.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
86beb213 25#include "parse-argument.h"
6bedfcbb 26#include "parse-util.h"
294bf0c3 27#include "pretty-print.h"
3f6fd1ba 28#include "process-util.h"
1abaf488 29#include "rlimit-util.h"
9e29521e 30#include "sigbus.h"
3f6fd1ba
LP
31#include "signal-util.h"
32#include "spawn-polkit-agent.h"
5c828e66 33#include "string-table.h"
a4c279f8 34#include "strv.h"
a4c279f8 35#include "sysfs-show.h"
288a74cc 36#include "terminal-util.h"
3f6fd1ba 37#include "unit-name.h"
b1d4f8e1 38#include "user-util.h"
3f6fd1ba 39#include "verbs.h"
abca4822 40
a4c279f8 41static char **arg_property = NULL;
255b1fc8 42static BusPrintPropertyFlags arg_print_flags = 0;
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
255b1fc8 59 FLAGS_SET(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY) * 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)) {
ef1e0b9a 92 r = table_set_sort(table, (size_t) 0);
99f1229d 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
384c2c32 126 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
384c2c32 200 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
384c2c32 250 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;
f37f8a61 453 SessionStatusInfo i = {};
f8f14b36
SP
454 int r;
455
a7e4861c 456 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
f647962d 457 if (r < 0)
f9e0eefc 458 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
495cb9bb
DH
459
460 if (*new_line)
461 printf("\n");
462
463 *new_line = true;
a4c279f8 464
f8f14b36 465 printf("%s - ", strna(i.id));
a4c279f8 466
f8f14b36 467 if (i.name)
e8f215d3 468 printf("%s (%"PRIu32")\n", i.name, i.uid);
a4c279f8 469 else
e8f215d3 470 printf("%"PRIu32"\n", i.uid);
a4c279f8 471
0da36375 472 if (timestamp_is_set(i.timestamp.realtime))
f843f85d
YW
473 printf("\t Since: %s; %s\n",
474 FORMAT_TIMESTAMP(i.timestamp.realtime),
475 FORMAT_TIMESTAMP_RELATIVE(i.timestamp.realtime));
a4c279f8 476
f8f14b36 477 if (i.leader > 0) {
9444b1f2 478 _cleanup_free_ char *t = NULL;
a4c279f8 479
e8f215d3 480 printf("\t Leader: %"PRIu32, i.leader);
a4c279f8 481
74d6421d 482 (void) get_process_comm(i.leader, &t);
9444b1f2 483 if (t)
a4c279f8 484 printf(" (%s)", t);
a4c279f8
LP
485
486 printf("\n");
487 }
488
016284c3 489 if (!isempty(i.seat)) {
f8f14b36 490 printf("\t Seat: %s", i.seat);
a4c279f8 491
f8f14b36 492 if (i.vtnr > 0)
c4ef0548 493 printf("; vc%u", i.vtnr);
a4c279f8
LP
494
495 printf("\n");
496 }
497
f8f14b36
SP
498 if (i.tty)
499 printf("\t TTY: %s\n", i.tty);
500 else if (i.display)
501 printf("\t Display: %s\n", i.display);
502
503 if (i.remote_host && i.remote_user)
504 printf("\t Remote: %s@%s\n", i.remote_user, i.remote_host);
505 else if (i.remote_host)
506 printf("\t Remote: %s\n", i.remote_host);
507 else if (i.remote_user)
508 printf("\t Remote: user %s\n", i.remote_user);
509 else if (i.remote)
a4c279f8
LP
510 printf("\t Remote: Yes\n");
511
f8f14b36
SP
512 if (i.service) {
513 printf("\t Service: %s", i.service);
a4c279f8 514
f8f14b36
SP
515 if (i.type)
516 printf("; type %s", i.type);
a4c279f8 517
f8f14b36
SP
518 if (i.class)
519 printf("; class %s", i.class);
55efac6c 520
a4c279f8 521 printf("\n");
f8f14b36 522 } else if (i.type) {
91d53e2b 523 printf("\t Type: %s", i.type);
a4c279f8 524
f8f14b36
SP
525 if (i.class)
526 printf("; class %s", i.class);
91d53e2b
MM
527
528 printf("\n");
f8f14b36
SP
529 } else if (i.class)
530 printf("\t Class: %s\n", i.class);
55efac6c 531
a4cd87e9
LP
532 if (!isempty(i.desktop))
533 printf("\t Desktop: %s\n", i.desktop);
534
f8f14b36
SP
535 if (i.state)
536 printf("\t State: %s\n", i.state);
a4c279f8 537
f8f14b36
SP
538 if (i.scope) {
539 printf("\t Unit: %s\n", i.scope);
540 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
3c756001 541
d7a0f1f4 542 if (arg_transport == BUS_TRANSPORT_LOCAL)
3c756001
LP
543 show_journal_by_unit(
544 stdout,
545 i.scope,
d93dda3a 546 NULL,
3c756001
LP
547 arg_output,
548 0,
549 i.timestamp.monotonic,
550 arg_lines,
551 0,
552 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
553 SD_JOURNAL_LOCAL_ONLY,
554 true,
555 NULL);
a4c279f8 556 }
f8f14b36
SP
557
558 return 0;
a4c279f8
LP
559}
560
495cb9bb 561static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
f8f14b36
SP
562
563 static const struct bus_properties_map map[] = {
3c756001 564 { "Name", "s", NULL, offsetof(UserStatusInfo, name) },
26e00f0e 565 { "Linger", "b", NULL, offsetof(UserStatusInfo, linger) },
3c756001
LP
566 { "Slice", "s", NULL, offsetof(UserStatusInfo, slice) },
567 { "State", "s", NULL, offsetof(UserStatusInfo, state) },
568 { "UID", "u", NULL, offsetof(UserStatusInfo, uid) },
569 { "Timestamp", "t", NULL, offsetof(UserStatusInfo, timestamp.realtime) },
570 { "TimestampMonotonic", "t", NULL, offsetof(UserStatusInfo, timestamp.monotonic) },
571 { "Display", "(so)", prop_map_first_of_struct, offsetof(UserStatusInfo, display) },
572 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(UserStatusInfo, sessions) },
f8f14b36
SP
573 {}
574 };
575
f9e0eefc 576 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 577 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
e7e55dbd 578 _cleanup_(user_status_info_clear) UserStatusInfo i = {};
f8f14b36 579 int r;
a4c279f8 580
a7e4861c 581 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, BUS_MAP_BOOLEAN_AS_BOOL, &error, &m, &i);
e7e55dbd 582 if (r < 0)
f9e0eefc 583 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
495cb9bb
DH
584
585 if (*new_line)
586 printf("\n");
587
588 *new_line = true;
f8f14b36
SP
589
590 if (i.name)
e8f215d3 591 printf("%s (%"PRIu32")\n", i.name, i.uid);
a4c279f8 592 else
e8f215d3 593 printf("%"PRIu32"\n", i.uid);
a4c279f8 594
0da36375 595 if (timestamp_is_set(i.timestamp.realtime))
f843f85d
YW
596 printf("\t Since: %s; %s\n",
597 FORMAT_TIMESTAMP(i.timestamp.realtime),
598 FORMAT_TIMESTAMP_RELATIVE(i.timestamp.realtime));
a4c279f8 599
f8f14b36
SP
600 if (!isempty(i.state))
601 printf("\t State: %s\n", i.state);
a4c279f8 602
f8f14b36 603 if (!strv_isempty(i.sessions)) {
a4c279f8
LP
604 printf("\tSessions:");
605
26e00f0e
ZJS
606 STRV_FOREACH(l, i.sessions)
607 printf(" %s%s",
608 streq_ptr(*l, i.display) ? "*" : "",
609 *l);
a4c279f8
LP
610
611 printf("\n");
612 }
613
26e00f0e
ZJS
614 printf("\t Linger: %s\n", yes_no(i.linger));
615
f8f14b36
SP
616 if (i.slice) {
617 printf("\t Unit: %s\n", i.slice);
618 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
3c756001
LP
619
620 show_journal_by_unit(
621 stdout,
622 i.slice,
d93dda3a 623 NULL,
3c756001
LP
624 arg_output,
625 0,
626 i.timestamp.monotonic,
627 arg_lines,
628 0,
629 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
630 SD_JOURNAL_LOCAL_ONLY,
631 true,
632 NULL);
a4c279f8 633 }
f8f14b36 634
e7e55dbd 635 return 0;
a4c279f8
LP
636}
637
495cb9bb 638static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
a4c279f8 639
f8f14b36
SP
640 static const struct bus_properties_map map[] = {
641 { "Id", "s", NULL, offsetof(SeatStatusInfo, id) },
46e65dcc
LP
642 { "ActiveSession", "(so)", prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
643 { "Sessions", "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
f8f14b36
SP
644 {}
645 };
a4c279f8 646
f9e0eefc 647 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f37f8a61 648 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
e7e55dbd 649 _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
f8f14b36
SP
650 int r;
651
a7e4861c 652 r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, 0, &error, &m, &i);
e7e55dbd 653 if (r < 0)
f9e0eefc 654 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
495cb9bb
DH
655
656 if (*new_line)
657 printf("\n");
658
659 *new_line = true;
f8f14b36
SP
660
661 printf("%s\n", strna(i.id));
662
663 if (!strv_isempty(i.sessions)) {
a4c279f8
LP
664 printf("\tSessions:");
665
f8f14b36
SP
666 STRV_FOREACH(l, i.sessions) {
667 if (streq_ptr(*l, i.active_session))
a4c279f8
LP
668 printf(" *%s", *l);
669 else
670 printf(" %s", *l);
671 }
672
673 printf("\n");
674 }
675
f8f14b36 676 if (arg_transport == BUS_TRANSPORT_LOCAL) {
a4c279f8
LP
677 unsigned c;
678
679 c = columns();
88e3dc90
LP
680 if (c > 21)
681 c -= 21;
a4c279f8
LP
682 else
683 c = 0;
684
685 printf("\t Devices:\n");
686
3850319b 687 show_sysfs(i.id, "\t\t ", c, get_output_flags());
a4c279f8 688 }
a4c279f8 689
e7e55dbd 690 return 0;
a4c279f8
LP
691}
692
255b1fc8 693static int print_property(const char *name, const char *expected_value, sd_bus_message *m, BusPrintPropertyFlags flags) {
07636114
YW
694 char type;
695 const char *contents;
2a998c74
LN
696 int r;
697
698 assert(name);
699 assert(m);
2a998c74 700
07636114
YW
701 r = sd_bus_message_peek_type(m, &type, &contents);
702 if (r < 0)
703 return r;
2a998c74 704
07636114 705 switch (type) {
2a998c74 706
07636114 707 case SD_BUS_TYPE_STRUCT:
2a998c74 708
07636114 709 if (contents[0] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
2a998c74
LN
710 const char *s;
711
712 r = sd_bus_message_read(m, "(so)", &s, NULL);
713 if (r < 0)
714 return bus_log_parse_error(r);
715
255b1fc8 716 bus_print_property_value(name, expected_value, flags, s);
2a998c74 717
07636114 718 return 1;
2a998c74 719
07636114 720 } else if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
2a998c74
LN
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
baaa35ad
ZJS
727 if (!uid_is_valid(uid))
728 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
729 "Invalid user ID: " UID_FMT,
730 uid);
2a998c74 731
255b1fc8 732 bus_print_property_valuef(name, expected_value, flags, UID_FMT, uid);
07636114 733 return 1;
2a998c74 734 }
2a998c74
LN
735 break;
736
737 case SD_BUS_TYPE_ARRAY:
738
07636114 739 if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
2a998c74
LN
740 const char *s;
741 bool space = false;
742
743 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
744 if (r < 0)
745 return bus_log_parse_error(r);
746
255b1fc8 747 if (!FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
f4046fe0 748 printf("%s=", name);
2a998c74
LN
749
750 while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
751 printf("%s%s", space ? " " : "", s);
752 space = true;
753 }
754
255b1fc8 755 if (space || !FLAGS_SET(flags, BUS_PRINT_PROPERTY_ONLY_VALUE))
f4046fe0 756 printf("\n");
2a998c74
LN
757
758 if (r < 0)
759 return bus_log_parse_error(r);
760
761 r = sd_bus_message_exit_container(m);
762 if (r < 0)
763 return bus_log_parse_error(r);
764
07636114 765 return 1;
2a998c74 766 }
2a998c74
LN
767 break;
768 }
769
2a998c74
LN
770 return 0;
771}
772
97aa7b47
DH
773static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
774 int r;
775
2a998c74
LN
776 assert(bus);
777 assert(path);
778 assert(new_line);
779
97aa7b47
DH
780 if (*new_line)
781 printf("\n");
782
783 *new_line = true;
784
8183ebcd
ZJS
785 r = bus_print_all_properties(
786 bus,
787 "org.freedesktop.login1",
788 path,
789 print_property,
790 arg_property,
255b1fc8 791 arg_print_flags,
8183ebcd 792 NULL);
2a998c74
LN
793 if (r < 0)
794 return bus_log_parse_error(r);
795
796 return 0;
97aa7b47
DH
797}
798
f7621db0 799static int show_session(int argc, char *argv[], void *userdata) {
97aa7b47 800 bool properties, new_line = false;
f7621db0 801 sd_bus *bus = userdata;
f7091f45 802 int r;
9cf8e208
ZJS
803 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
804 _cleanup_free_ char *path = NULL;
a4c279f8 805
f8f14b36 806 assert(bus);
f7621db0 807 assert(argv);
a4c279f8 808
f7621db0 809 properties = !strstr(argv[0], "status");
a4c279f8 810
384c2c32 811 pager_open(arg_pager_flags);
a4c279f8 812
86e1f46f 813 if (argc <= 1) {
544c4e1e 814 /* If no argument is specified inspect the manager itself */
86e1f46f
LP
815 if (properties)
816 return show_properties(bus, "/org/freedesktop/login1", &new_line);
817
544c4e1e 818 return print_session_status_info(bus, "/org/freedesktop/login1/session/auto", &new_line);
a4c279f8
LP
819 }
820
f7091f45 821 for (int i = 1; i < argc; i++) {
b0d08b05 822 r = get_session_path(bus, argv[i], &error, &path);
4ae25393
YW
823 if (r < 0)
824 return log_error_errno(r, "Failed to get session path: %s", bus_error_message(&error, r));
a4c279f8 825
97aa7b47
DH
826 if (properties)
827 r = show_properties(bus, path, &new_line);
f8f14b36 828 else
495cb9bb
DH
829 r = print_session_status_info(bus, path, &new_line);
830
831 if (r < 0)
f8f14b36 832 return r;
a4c279f8
LP
833 }
834
a4c279f8
LP
835 return 0;
836}
837
f7621db0 838static int show_user(int argc, char *argv[], void *userdata) {
97aa7b47 839 bool properties, new_line = false;
f7621db0 840 sd_bus *bus = userdata;
f7091f45 841 int r;
a4c279f8 842
f8f14b36 843 assert(bus);
f7621db0 844 assert(argv);
a4c279f8 845
f7621db0 846 properties = !strstr(argv[0], "status");
a4c279f8 847
384c2c32 848 pager_open(arg_pager_flags);
a4c279f8 849
86e1f46f 850 if (argc <= 1) {
544c4e1e 851 /* If no argument is specified inspect the manager itself */
86e1f46f
LP
852 if (properties)
853 return show_properties(bus, "/org/freedesktop/login1", &new_line);
854
855 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
f8f14b36 856 }
a4c279f8 857
f7091f45 858 for (int i = 1; i < argc; i++) {
4afd3348
LP
859 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
860 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
f8f14b36
SP
861 const char *path = NULL;
862 uid_t uid;
a4c279f8 863
fafff8f1 864 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL, 0);
f647962d 865 if (r < 0)
f7621db0 866 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
a4c279f8 867
f8f14b36
SP
868 r = sd_bus_call_method(
869 bus,
870 "org.freedesktop.login1",
871 "/org/freedesktop/login1",
872 "org.freedesktop.login1.Manager",
873 "GetUser",
874 &error, &reply,
875 "u", (uint32_t) uid);
4ae25393
YW
876 if (r < 0)
877 return log_error_errno(r, "Failed to get user: %s", bus_error_message(&error, r));
a4c279f8 878
f8f14b36
SP
879 r = sd_bus_message_read(reply, "o", &path);
880 if (r < 0)
5b30bef8 881 return bus_log_parse_error(r);
a4c279f8 882
97aa7b47
DH
883 if (properties)
884 r = show_properties(bus, path, &new_line);
9444b1f2 885 else
495cb9bb
DH
886 r = print_user_status_info(bus, path, &new_line);
887
888 if (r < 0)
f8f14b36 889 return r;
a4c279f8
LP
890 }
891
f8f14b36 892 return 0;
a4c279f8
LP
893}
894
f7621db0 895static int show_seat(int argc, char *argv[], void *userdata) {
97aa7b47 896 bool properties, new_line = false;
f7621db0 897 sd_bus *bus = userdata;
f7091f45 898 int r;
a4c279f8
LP
899
900 assert(bus);
f7621db0 901 assert(argv);
a4c279f8 902
f7621db0 903 properties = !strstr(argv[0], "status");
a4c279f8 904
384c2c32 905 pager_open(arg_pager_flags);
a4c279f8 906
86e1f46f 907 if (argc <= 1) {
544c4e1e 908 /* If no argument is specified inspect the manager itself */
86e1f46f
LP
909 if (properties)
910 return show_properties(bus, "/org/freedesktop/login1", &new_line);
911
544c4e1e 912 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/auto", &new_line);
a4c279f8
LP
913 }
914
f7091f45 915 for (int i = 1; i < argc; i++) {
4afd3348
LP
916 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
917 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
a4c279f8
LP
918 const char *path = NULL;
919
5d990cc5 920 r = bus_call_method(bus, bus_login_mgr, "GetSeat", &error, &reply, "s", argv[i]);
4ae25393
YW
921 if (r < 0)
922 return log_error_errno(r, "Failed to get seat: %s", bus_error_message(&error, r));
9444b1f2 923
f8f14b36
SP
924 r = sd_bus_message_read(reply, "o", &path);
925 if (r < 0)
5b30bef8 926 return bus_log_parse_error(r);
a4c279f8 927
97aa7b47
DH
928 if (properties)
929 r = show_properties(bus, path, &new_line);
f8f14b36 930 else
495cb9bb
DH
931 r = print_seat_status_info(bus, path, &new_line);
932
933 if (r < 0)
f8f14b36 934 return r;
a4c279f8
LP
935 }
936
f8f14b36 937 return 0;
a4c279f8
LP
938}
939
f7621db0 940static int activate(int argc, char *argv[], void *userdata) {
4afd3348 941 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 942 sd_bus *bus = userdata;
f7091f45 943 int r;
24310c11 944
f7621db0
LP
945 assert(bus);
946 assert(argv);
24310c11 947
8a4b13c5 948 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
079dac08 949
906b76b2 950 if (argc < 2) {
dc084399
LP
951 r = sd_bus_call_method(
952 bus,
953 "org.freedesktop.login1",
954 "/org/freedesktop/login1/session/auto",
955 "org.freedesktop.login1.Session",
956 streq(argv[0], "lock-session") ? "Lock" :
957 streq(argv[0], "unlock-session") ? "Unlock" :
958 streq(argv[0], "terminate-session") ? "Terminate" :
959 "Activate",
960 &error, NULL, NULL);
961 if (r < 0)
962 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
2fbcde74 963
dc084399 964 return 0;
906b76b2
LP
965 }
966
f7091f45 967 for (int i = 1; i < argc; i++) {
f8440af5 968
5d990cc5 969 r = bus_call_method(
2a3613b1 970 bus,
5d990cc5 971 bus_login_mgr,
f7621db0
LP
972 streq(argv[0], "lock-session") ? "LockSession" :
973 streq(argv[0], "unlock-session") ? "UnlockSession" :
974 streq(argv[0], "terminate-session") ? "TerminateSession" :
2a3613b1 975 "ActivateSession",
f8f14b36 976 &error, NULL,
f7621db0 977 "s", argv[i]);
4ae25393 978 if (r < 0)
544c4e1e 979 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
24310c11
LP
980 }
981
f8f14b36 982 return 0;
a4c279f8
LP
983}
984
f7621db0 985static int kill_session(int argc, char *argv[], void *userdata) {
4afd3348 986 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 987 sd_bus *bus = userdata;
f7091f45 988 int r;
de07ab16 989
f7621db0
LP
990 assert(bus);
991 assert(argv);
de07ab16 992
8a4b13c5 993 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
079dac08 994
de07ab16
LP
995 if (!arg_kill_who)
996 arg_kill_who = "all";
997
f7091f45 998 for (int i = 1; i < argc; i++) {
4654e558 999
5d990cc5
VC
1000 r = bus_call_method(
1001 bus,
1002 bus_login_mgr,
1003 "KillSession",
1004 &error, NULL,
1005 "ssi", argv[i], arg_kill_who, arg_signal);
4ae25393 1006 if (r < 0)
2a03b9ed 1007 return log_error_errno(r, "Could not kill session: %s", bus_error_message(&error, r));
de07ab16
LP
1008 }
1009
4654e558 1010 return 0;
a4c279f8
LP
1011}
1012
f7621db0 1013static int enable_linger(int argc, char *argv[], void *userdata) {
4afd3348 1014 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 1015 sd_bus *bus = userdata;
2fbcde74 1016 char* short_argv[3];
f8f14b36 1017 bool b;
f7091f45 1018 int r;
88e3dc90 1019
f7621db0
LP
1020 assert(bus);
1021 assert(argv);
88e3dc90 1022
8a4b13c5 1023 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
6bb92a16 1024
f7621db0 1025 b = streq(argv[0], "enable-linger");
88e3dc90 1026
906b76b2 1027 if (argc < 2) {
545f779f
AJ
1028 /* No argument? Let's use an empty user name,
1029 * then logind will use our user. */
bdb07fa5 1030
2fbcde74 1031 short_argv[0] = argv[0];
545f779f 1032 short_argv[1] = (char*) "";
2fbcde74
LP
1033 short_argv[2] = NULL;
1034 argv = short_argv;
906b76b2
LP
1035 argc = 2;
1036 }
1037
f7091f45 1038 for (int i = 1; i < argc; i++) {
ddd88763 1039 uid_t uid;
88e3dc90 1040
906b76b2
LP
1041 if (isempty(argv[i]))
1042 uid = UID_INVALID;
1043 else {
fafff8f1 1044 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL, 0);
906b76b2
LP
1045 if (r < 0)
1046 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1047 }
88e3dc90 1048
5d990cc5
VC
1049 r = bus_call_method(
1050 bus,
1051 bus_login_mgr,
1052 "SetUserLinger",
1053 &error, NULL,
1054 "ubb", (uint32_t) uid, b, true);
4ae25393 1055 if (r < 0)
2a03b9ed 1056 return log_error_errno(r, "Could not enable linger: %s", bus_error_message(&error, r));
88e3dc90
LP
1057 }
1058
4654e558 1059 return 0;
88e3dc90
LP
1060}
1061
f7621db0 1062static int terminate_user(int argc, char *argv[], void *userdata) {
4afd3348 1063 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 1064 sd_bus *bus = userdata;
f7091f45 1065 int r;
88e3dc90 1066
f7621db0
LP
1067 assert(bus);
1068 assert(argv);
88e3dc90 1069
8a4b13c5 1070 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
079dac08 1071
f7091f45 1072 for (int i = 1; i < argc; i++) {
ddd88763 1073 uid_t uid;
88e3dc90 1074
68892f94
LP
1075 if (isempty(argv[i]))
1076 uid = getuid();
1077 else {
1078 const char *u = argv[i];
1079
1080 r = get_user_creds(&u, &uid, NULL, NULL, NULL, 0);
1081 if (r < 0)
1082 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1083 }
88e3dc90 1084
5d990cc5 1085 r = bus_call_method(bus, bus_login_mgr, "TerminateUser", &error, NULL, "u", (uint32_t) uid);
4ae25393 1086 if (r < 0)
2a03b9ed 1087 return log_error_errno(r, "Could not terminate user: %s", bus_error_message(&error, r));
88e3dc90
LP
1088 }
1089
4654e558 1090 return 0;
a4c279f8
LP
1091}
1092
f7621db0 1093static int kill_user(int argc, char *argv[], void *userdata) {
4afd3348 1094 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 1095 sd_bus *bus = userdata;
f7091f45 1096 int r;
de07ab16 1097
f7621db0
LP
1098 assert(bus);
1099 assert(argv);
de07ab16 1100
8a4b13c5 1101 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
079dac08 1102
de07ab16
LP
1103 if (!arg_kill_who)
1104 arg_kill_who = "all";
1105
f7091f45 1106 for (int i = 1; i < argc; i++) {
ddd88763 1107 uid_t uid;
de07ab16 1108
68892f94
LP
1109 if (isempty(argv[i]))
1110 uid = getuid();
1111 else {
1112 const char *u = argv[i];
1113
1114 r = get_user_creds(&u, &uid, NULL, NULL, NULL, 0);
1115 if (r < 0)
1116 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1117 }
de07ab16 1118
5d990cc5 1119 r = bus_call_method(
4654e558 1120 bus,
5d990cc5 1121 bus_login_mgr,
4654e558 1122 "KillUser",
f8f14b36
SP
1123 &error, NULL,
1124 "ui", (uint32_t) uid, arg_signal);
4ae25393 1125 if (r < 0)
2a03b9ed 1126 return log_error_errno(r, "Could not kill user: %s", bus_error_message(&error, r));
de07ab16
LP
1127 }
1128
4654e558 1129 return 0;
de07ab16
LP
1130}
1131
f7621db0 1132static int attach(int argc, char *argv[], void *userdata) {
4afd3348 1133 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 1134 sd_bus *bus = userdata;
f7091f45 1135 int r;
88e3dc90 1136
f7621db0
LP
1137 assert(bus);
1138 assert(argv);
88e3dc90 1139
8a4b13c5 1140 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
6bb92a16 1141
f7091f45 1142 for (int i = 2; i < argc; i++) {
4654e558 1143
5d990cc5 1144 r = bus_call_method(
4654e558 1145 bus,
5d990cc5 1146 bus_login_mgr,
4654e558 1147 "AttachDevice",
f8f14b36 1148 &error, NULL,
f7621db0 1149 "ssb", argv[1], argv[i], true);
4ae25393 1150 if (r < 0)
2a03b9ed 1151 return log_error_errno(r, "Could not attach device: %s", bus_error_message(&error, r));
88e3dc90
LP
1152 }
1153
4654e558 1154 return 0;
a4c279f8
LP
1155}
1156
f7621db0 1157static int flush_devices(int argc, char *argv[], void *userdata) {
4afd3348 1158 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 1159 sd_bus *bus = userdata;
f8f14b36 1160 int r;
88e3dc90 1161
f7621db0
LP
1162 assert(bus);
1163 assert(argv);
88e3dc90 1164
8a4b13c5 1165 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
6bb92a16 1166
5d990cc5 1167 r = bus_call_method(bus, bus_login_mgr, "FlushDevices", &error, NULL, "b", true);
f8f14b36 1168 if (r < 0)
2a03b9ed 1169 return log_error_errno(r, "Could not flush devices: %s", bus_error_message(&error, r));
f8f14b36 1170
4ae25393 1171 return 0;
a4c279f8
LP
1172}
1173
f7621db0 1174static int lock_sessions(int argc, char *argv[], void *userdata) {
4afd3348 1175 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 1176 sd_bus *bus = userdata;
f8f14b36 1177 int r;
b6160029 1178
f7621db0
LP
1179 assert(bus);
1180 assert(argv);
7212a8a9 1181
8a4b13c5 1182 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
079dac08 1183
5d990cc5 1184 r = bus_call_method(
2a3613b1 1185 bus,
5d990cc5 1186 bus_login_mgr,
f7621db0 1187 streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
f8f14b36
SP
1188 &error, NULL,
1189 NULL);
1190 if (r < 0)
2a03b9ed 1191 return log_error_errno(r, "Could not lock sessions: %s", bus_error_message(&error, r));
f8f14b36 1192
4ae25393 1193 return 0;
7212a8a9
LP
1194}
1195
f7621db0 1196static int terminate_seat(int argc, char *argv[], void *userdata) {
4afd3348 1197 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
f7621db0 1198 sd_bus *bus = userdata;
f7091f45 1199 int r;
88e3dc90 1200
f7621db0
LP
1201 assert(bus);
1202 assert(argv);
88e3dc90 1203
8a4b13c5 1204 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
079dac08 1205
f7091f45 1206 for (int i = 1; i < argc; i++) {
4654e558 1207
5d990cc5 1208 r = bus_call_method(bus, bus_login_mgr, "TerminateSeat", &error, NULL, "s", argv[i]);
4ae25393 1209 if (r < 0)
2a03b9ed 1210 return log_error_errno(r, "Could not terminate seat: %s", bus_error_message(&error, r));
88e3dc90
LP
1211 }
1212
4654e558 1213 return 0;
a4c279f8
LP
1214}
1215
f7621db0 1216static int help(int argc, char *argv[], void *userdata) {
37ec0fdd
LP
1217 _cleanup_free_ char *link = NULL;
1218 int r;
1219
384c2c32 1220 pager_open(arg_pager_flags);
37ec0fdd
LP
1221
1222 r = terminal_urlify_man("loginctl", "1", &link);
1223 if (r < 0)
1224 return log_oom();
079dac08 1225
353b2baa
LP
1226 printf("%s [OPTIONS...] COMMAND ...\n\n"
1227 "%sSend control commands to or query the login manager.%s\n"
e1fac8a6 1228 "\nSession Commands:\n"
4f8f66cb 1229 " list-sessions List sessions\n"
86e1f46f 1230 " session-status [ID...] Show session status\n"
4f8f66cb 1231 " show-session [ID...] Show properties of sessions or the manager\n"
906b76b2
LP
1232 " activate [ID] Activate a session\n"
1233 " lock-session [ID...] Screen lock one or more sessions\n"
1234 " unlock-session [ID...] Screen unlock one or more sessions\n"
4f8f66cb
ZJS
1235 " lock-sessions Screen lock all current sessions\n"
1236 " unlock-sessions Screen unlock all current sessions\n"
1237 " terminate-session ID... Terminate one or more sessions\n"
353b2baa
LP
1238 " kill-session ID... Send signal to processes of a session\n"
1239 "\nUser Commands:\n"
4f8f66cb 1240 " list-users List users\n"
86e1f46f 1241 " user-status [USER...] Show user status\n"
4f8f66cb 1242 " show-user [USER...] Show properties of users or the manager\n"
906b76b2
LP
1243 " enable-linger [USER...] Enable linger state of one or more users\n"
1244 " disable-linger [USER...] Disable linger state of one or more users\n"
4f8f66cb 1245 " terminate-user USER... Terminate all sessions of one or more users\n"
353b2baa
LP
1246 " kill-user USER... Send signal to processes of a user\n"
1247 "\nSeat Commands:\n"
4f8f66cb 1248 " list-seats List seats\n"
86e1f46f
LP
1249 " seat-status [NAME...] Show seat status\n"
1250 " show-seat [NAME...] Show properties of seats or the manager\n"
4f8f66cb
ZJS
1251 " attach NAME DEVICE... Attach one or more devices to a seat\n"
1252 " flush-devices Flush all device associations\n"
601185b4 1253 " terminate-seat NAME... Terminate all sessions on one or more seats\n"
353b2baa 1254 "\nOptions:\n"
e1fac8a6
ZJS
1255 " -h --help Show this help\n"
1256 " --version Show package version\n"
1257 " --no-pager Do not pipe output into a pager\n"
1258 " --no-legend Do not show the headers and footers\n"
1259 " --no-ask-password Don't prompt for password\n"
1260 " -H --host=[USER@]HOST Operate on remote host\n"
1261 " -M --machine=CONTAINER Operate on local container\n"
1262 " -p --property=NAME Show only properties by this name\n"
60b254ca 1263 " -P NAME Equivalent to --value --property=NAME\n"
e1fac8a6
ZJS
1264 " -a --all Show all properties, including empty ones\n"
1265 " --value When showing properties, only print the value\n"
1266 " -l --full Do not ellipsize output\n"
1267 " --kill-who=WHO Who to send signal to\n"
1268 " -s --signal=SIGNAL Which signal to send\n"
1269 " -n --lines=INTEGER Number of journal entries to show\n"
1270 " -o --output=STRING Change journal output mode (short, short-precise,\n"
1271 " short-iso, short-iso-precise, short-full,\n"
1272 " short-monotonic, short-unix, verbose, export,\n"
1273 " json, json-pretty, json-sse, json-seq, cat,\n"
1274 " with-unit)\n"
bc556335
DDM
1275 "\nSee the %s for details.\n",
1276 program_invocation_short_name,
1277 ansi_highlight(),
1278 ansi_normal(),
1279 link);
f7621db0
LP
1280
1281 return 0;
abca4822
LP
1282}
1283
1284static int parse_argv(int argc, char *argv[]) {
abca4822
LP
1285 enum {
1286 ARG_VERSION = 0x100,
f4046fe0 1287 ARG_VALUE,
a4c279f8 1288 ARG_NO_PAGER,
841aa8c0 1289 ARG_NO_LEGEND,
6bb92a16 1290 ARG_KILL_WHO,
9bdbc2e2 1291 ARG_NO_ASK_PASSWORD,
abca4822
LP
1292 };
1293
1294 static const struct option options[] = {
6d0274f1
LP
1295 { "help", no_argument, NULL, 'h' },
1296 { "version", no_argument, NULL, ARG_VERSION },
1297 { "property", required_argument, NULL, 'p' },
1298 { "all", no_argument, NULL, 'a' },
f4046fe0 1299 { "value", no_argument, NULL, ARG_VALUE },
422fa650 1300 { "full", no_argument, NULL, 'l' },
6d0274f1 1301 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
841aa8c0 1302 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
6d0274f1
LP
1303 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
1304 { "signal", required_argument, NULL, 's' },
1305 { "host", required_argument, NULL, 'H' },
f8f14b36 1306 { "machine", required_argument, NULL, 'M' },
6d0274f1 1307 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
3c756001
LP
1308 { "lines", required_argument, NULL, 'n' },
1309 { "output", required_argument, NULL, 'o' },
eb9da376 1310 {}
abca4822
LP
1311 };
1312
1c3051eb 1313 int c, r;
abca4822
LP
1314
1315 assert(argc >= 0);
1316 assert(argv);
1317
60b254ca 1318 while ((c = getopt_long(argc, argv, "hp:P:als:H:M:n:o:", options, NULL)) >= 0)
abca4822
LP
1319
1320 switch (c) {
1321
1322 case 'h':
37ec0fdd 1323 return help(0, NULL, NULL);
abca4822
LP
1324
1325 case ARG_VERSION:
3f6fd1ba 1326 return version();
abca4822 1327
60b254ca 1328 case 'P':
255b1fc8 1329 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
60b254ca
RP
1330 _fallthrough_;
1331
a4c279f8 1332 case 'p': {
1c3051eb
DH
1333 r = strv_extend(&arg_property, optarg);
1334 if (r < 0)
1335 return log_oom();
a4c279f8
LP
1336
1337 /* If the user asked for a particular
e8607daf 1338 * property, show it to them, even if it is
a4c279f8 1339 * empty. */
255b1fc8 1340 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
a4c279f8
LP
1341 break;
1342 }
1343
1344 case 'a':
255b1fc8 1345 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_SHOW_EMPTY, true);
a4c279f8
LP
1346 break;
1347
f4046fe0 1348 case ARG_VALUE:
255b1fc8 1349 SET_FLAG(arg_print_flags, BUS_PRINT_PROPERTY_ONLY_VALUE, true);
f4046fe0
ZJS
1350 break;
1351
422fa650
ZJS
1352 case 'l':
1353 arg_full = true;
1354 break;
1355
3c756001 1356 case 'n':
baaa35ad
ZJS
1357 if (safe_atou(optarg, &arg_lines) < 0)
1358 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1359 "Failed to parse lines '%s'", optarg);
3c756001
LP
1360 break;
1361
1362 case 'o':
5c828e66
LP
1363 if (streq(optarg, "help")) {
1364 DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
1365 return 0;
1366 }
1367
3c756001 1368 arg_output = output_mode_from_string(optarg);
baaa35ad 1369 if (arg_output < 0)
7211c853 1370 return log_error_errno(arg_output, "Unknown output '%s'.", optarg);
e3483674
LP
1371
1372 if (OUTPUT_MODE_IS_JSON(arg_output))
1373 arg_legend = false;
1374
3c756001
LP
1375 break;
1376
abca4822 1377 case ARG_NO_PAGER:
0221d68a 1378 arg_pager_flags |= PAGER_DISABLE;
abca4822
LP
1379 break;
1380
841aa8c0
ZJS
1381 case ARG_NO_LEGEND:
1382 arg_legend = false;
1383 break;
1384
6bb92a16
LP
1385 case ARG_NO_ASK_PASSWORD:
1386 arg_ask_password = false;
5d5e98eb 1387 break;
6bb92a16 1388
a4c279f8
LP
1389 case ARG_KILL_WHO:
1390 arg_kill_who = optarg;
1391 break;
1392
1393 case 's':
86beb213
ZJS
1394 r = parse_signal_argument(optarg, &arg_signal);
1395 if (r <= 0)
1396 return r;
a4c279f8
LP
1397 break;
1398
f8f14b36
SP
1399 case 'H':
1400 arg_transport = BUS_TRANSPORT_REMOTE;
1401 arg_host = optarg;
abca4822
LP
1402 break;
1403
f8f14b36 1404 case 'M':
de33fc62 1405 arg_transport = BUS_TRANSPORT_MACHINE;
f8f14b36 1406 arg_host = optarg;
abca4822
LP
1407 break;
1408
1409 case '?':
1410 return -EINVAL;
1411
1412 default:
04499a70 1413 assert_not_reached();
abca4822 1414 }
abca4822
LP
1415
1416 return 1;
1417}
1418
f7621db0 1419static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
f7621db0
LP
1420 static const Verb verbs[] = {
1421 { "help", VERB_ANY, VERB_ANY, 0, help },
1422 { "list-sessions", VERB_ANY, 1, VERB_DEFAULT, list_sessions },
86e1f46f 1423 { "session-status", VERB_ANY, VERB_ANY, 0, show_session },
f7621db0 1424 { "show-session", VERB_ANY, VERB_ANY, 0, show_session },
906b76b2
LP
1425 { "activate", VERB_ANY, 2, 0, activate },
1426 { "lock-session", VERB_ANY, VERB_ANY, 0, activate },
1427 { "unlock-session", VERB_ANY, VERB_ANY, 0, activate },
f7621db0
LP
1428 { "lock-sessions", VERB_ANY, 1, 0, lock_sessions },
1429 { "unlock-sessions", VERB_ANY, 1, 0, lock_sessions },
1430 { "terminate-session", 2, VERB_ANY, 0, activate },
1431 { "kill-session", 2, VERB_ANY, 0, kill_session },
1432 { "list-users", VERB_ANY, 1, 0, list_users },
86e1f46f 1433 { "user-status", VERB_ANY, VERB_ANY, 0, show_user },
f7621db0 1434 { "show-user", VERB_ANY, VERB_ANY, 0, show_user },
906b76b2
LP
1435 { "enable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
1436 { "disable-linger", VERB_ANY, VERB_ANY, 0, enable_linger },
f7621db0
LP
1437 { "terminate-user", 2, VERB_ANY, 0, terminate_user },
1438 { "kill-user", 2, VERB_ANY, 0, kill_user },
1439 { "list-seats", VERB_ANY, 1, 0, list_seats },
86e1f46f
LP
1440 { "seat-status", VERB_ANY, VERB_ANY, 0, show_seat },
1441 { "show-seat", VERB_ANY, VERB_ANY, 0, show_seat },
f7621db0
LP
1442 { "attach", 3, VERB_ANY, 0, attach },
1443 { "flush-devices", VERB_ANY, 1, 0, flush_devices },
1444 { "terminate-seat", 2, VERB_ANY, 0, terminate_seat },
1445 {}
abca4822
LP
1446 };
1447
f7621db0 1448 return dispatch_verb(argc, argv, verbs, bus);
abca4822
LP
1449}
1450
eae5c847
YW
1451static int run(int argc, char *argv[]) {
1452 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
f8f14b36 1453 int r;
abca4822 1454
a9cdc94f 1455 setlocale(LC_ALL, "");
d2acb93d 1456 log_setup();
1abaf488
LP
1457
1458 /* The journal merging logic potentially needs a lot of fds. */
1459 (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
1460
9e29521e 1461 sigbus_install();
abca4822
LP
1462
1463 r = parse_argv(argc, argv);
f8f14b36 1464 if (r <= 0)
eae5c847 1465 return r;
f8f14b36 1466
266f3e26 1467 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
eae5c847 1468 if (r < 0)
10a7340a 1469 return bus_log_connect_error(r, arg_transport);
abca4822 1470
eae5c847 1471 (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
a4c279f8 1472
eae5c847 1473 return loginctl_main(argc, argv, bus);
abca4822 1474}
eae5c847
YW
1475
1476DEFINE_MAIN_FUNCTION(run);