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