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