chiark / gitweb /
shared/cgroup-show: extract funtion to query unit cgroup path
[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         char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
503         char since2[FORMAT_TIMESTAMP_MAX], *s2;
504         _cleanup_(session_status_info_clear) SessionStatusInfo i = {};
505         int r;
506
507         r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
508         if (r < 0)
509                 return log_error_errno(r, "Could not get properties: %m");
510
511         if (*new_line)
512                 printf("\n");
513
514         *new_line = true;
515
516         printf("%s - ", strna(i.id));
517
518         if (i.name)
519                 printf("%s (%"PRIu32")\n", i.name, i.uid);
520         else
521                 printf("%"PRIu32"\n", i.uid);
522
523         s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
524         s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
525
526         if (s1)
527                 printf("\t   Since: %s; %s\n", s2, s1);
528         else if (s2)
529                 printf("\t   Since: %s\n", s2);
530
531         if (i.leader > 0) {
532                 _cleanup_free_ char *t = NULL;
533
534                 printf("\t  Leader: %"PRIu32, i.leader);
535
536                 get_process_comm(i.leader, &t);
537                 if (t)
538                         printf(" (%s)", t);
539
540                 printf("\n");
541         }
542
543         if (!isempty(i.seat)) {
544                 printf("\t    Seat: %s", i.seat);
545
546                 if (i.vtnr > 0)
547                         printf("; vc%u", i.vtnr);
548
549                 printf("\n");
550         }
551
552         if (i.tty)
553                 printf("\t     TTY: %s\n", i.tty);
554         else if (i.display)
555                 printf("\t Display: %s\n", i.display);
556
557         if (i.remote_host && i.remote_user)
558                 printf("\t  Remote: %s@%s\n", i.remote_user, i.remote_host);
559         else if (i.remote_host)
560                 printf("\t  Remote: %s\n", i.remote_host);
561         else if (i.remote_user)
562                 printf("\t  Remote: user %s\n", i.remote_user);
563         else if (i.remote)
564                 printf("\t  Remote: Yes\n");
565
566         if (i.service) {
567                 printf("\t Service: %s", i.service);
568
569                 if (i.type)
570                         printf("; type %s", i.type);
571
572                 if (i.class)
573                         printf("; class %s", i.class);
574
575                 printf("\n");
576         } else if (i.type) {
577                 printf("\t    Type: %s", i.type);
578
579                 if (i.class)
580                         printf("; class %s", i.class);
581
582                 printf("\n");
583         } else if (i.class)
584                 printf("\t   Class: %s\n", i.class);
585
586         if (!isempty(i.desktop))
587                 printf("\t Desktop: %s\n", i.desktop);
588
589         if (i.state)
590                 printf("\t   State: %s\n", i.state);
591
592         if (i.scope) {
593                 printf("\t    Unit: %s\n", i.scope);
594 #if 0 /// UNNEEDED by elogind
595                 show_unit_cgroup(bus, "org.freedesktop.systemd1.Scope", i.scope, i.leader);
596
597                 if (arg_transport == BUS_TRANSPORT_LOCAL) {
598
599                         show_journal_by_unit(
600                                         stdout,
601                                         i.scope,
602                                         arg_output,
603                                         0,
604                                         i.timestamp.monotonic,
605                                         arg_lines,
606                                         0,
607                                         get_output_flags() | OUTPUT_BEGIN_NEWLINE,
608                                         SD_JOURNAL_LOCAL_ONLY,
609                                         true,
610                                         NULL);
611                 }
612 #endif // 0
613         }
614
615         return 0;
616 }
617
618 static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line) {
619
620         static const struct bus_properties_map map[]  = {
621                 { "Name",               "s",     NULL,                     offsetof(UserStatusInfo, name)                },
622                 { "Linger",             "b",     NULL,                     offsetof(UserStatusInfo, linger)              },
623                 { "Slice",              "s",     NULL,                     offsetof(UserStatusInfo, slice)               },
624                 { "State",              "s",     NULL,                     offsetof(UserStatusInfo, state)               },
625                 { "UID",                "u",     NULL,                     offsetof(UserStatusInfo, uid)                 },
626                 { "Timestamp",          "t",     NULL,                     offsetof(UserStatusInfo, timestamp.realtime)  },
627                 { "TimestampMonotonic", "t",     NULL,                     offsetof(UserStatusInfo, timestamp.monotonic) },
628                 { "Display",            "(so)",  prop_map_first_of_struct, offsetof(UserStatusInfo, display)             },
629                 { "Sessions",           "a(so)", prop_map_sessions_strv,   offsetof(UserStatusInfo, sessions)            },
630                 {}
631         };
632
633         char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
634         char since2[FORMAT_TIMESTAMP_MAX], *s2;
635         _cleanup_(user_status_info_clear) UserStatusInfo i = {};
636         int r;
637
638         r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
639         if (r < 0)
640                 return log_error_errno(r, "Could not get properties: %m");
641
642         if (*new_line)
643                 printf("\n");
644
645         *new_line = true;
646
647         if (i.name)
648                 printf("%s (%"PRIu32")\n", i.name, i.uid);
649         else
650                 printf("%"PRIu32"\n", i.uid);
651
652         s1 = format_timestamp_relative(since1, sizeof(since1), i.timestamp.realtime);
653         s2 = format_timestamp(since2, sizeof(since2), i.timestamp.realtime);
654
655         if (s1)
656                 printf("\t   Since: %s; %s\n", s2, s1);
657         else if (s2)
658                 printf("\t   Since: %s\n", s2);
659
660         if (!isempty(i.state))
661                 printf("\t   State: %s\n", i.state);
662
663         if (!strv_isempty(i.sessions)) {
664                 char **l;
665                 printf("\tSessions:");
666
667                 STRV_FOREACH(l, i.sessions)
668                         printf(" %s%s",
669                                streq_ptr(*l, i.display) ? "*" : "",
670                                *l);
671
672                 printf("\n");
673         }
674
675         printf("\t  Linger: %s\n", yes_no(i.linger));
676
677         if (i.slice) {
678                 printf("\t    Unit: %s\n", i.slice);
679 #if 0 /// UNNEEDED by elogind
680                 show_unit_cgroup(bus, "org.freedesktop.systemd1.Slice", i.slice, 0);
681
682                 show_journal_by_unit(
683                                 stdout,
684                                 i.slice,
685                                 arg_output,
686                                 0,
687                                 i.timestamp.monotonic,
688                                 arg_lines,
689                                 0,
690                                 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
691                                 SD_JOURNAL_LOCAL_ONLY,
692                                 true,
693                                 NULL);
694 #endif // 0
695         }
696
697         return 0;
698 }
699
700 static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line) {
701
702         static const struct bus_properties_map map[]  = {
703                 { "Id",            "s",     NULL, offsetof(SeatStatusInfo, id) },
704                 { "ActiveSession", "(so)",  prop_map_first_of_struct, offsetof(SeatStatusInfo, active_session) },
705                 { "Sessions",      "a(so)", prop_map_sessions_strv, offsetof(SeatStatusInfo, sessions) },
706                 {}
707         };
708
709         _cleanup_(seat_status_info_clear) SeatStatusInfo i = {};
710         int r;
711
712         r = bus_map_all_properties(bus, "org.freedesktop.login1", path, map, &i);
713         if (r < 0)
714                 return log_error_errno(r, "Could not get properties: %m");
715
716         if (*new_line)
717                 printf("\n");
718
719         *new_line = true;
720
721         printf("%s\n", strna(i.id));
722
723         if (!strv_isempty(i.sessions)) {
724                 char **l;
725                 printf("\tSessions:");
726
727                 STRV_FOREACH(l, i.sessions) {
728                         if (streq_ptr(*l, i.active_session))
729                                 printf(" *%s", *l);
730                         else
731                                 printf(" %s", *l);
732                 }
733
734                 printf("\n");
735         }
736
737         if (arg_transport == BUS_TRANSPORT_LOCAL) {
738                 unsigned c;
739
740                 c = columns();
741                 if (c > 21)
742                         c -= 21;
743                 else
744                         c = 0;
745
746                 printf("\t Devices:\n");
747
748                 show_sysfs(i.id, "\t\t  ", c);
749         }
750
751         return 0;
752 }
753
754 #define property(name, fmt, ...)                                        \
755         do {                                                            \
756                 if (arg_value)                                          \
757                         printf(fmt "\n", __VA_ARGS__);                  \
758                 else                                                    \
759                         printf("%s=" fmt "\n", name, __VA_ARGS__);      \
760         } while(0)
761
762 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
763         int r;
764
765         assert(name);
766         assert(m);
767         assert(contents);
768
769         if (arg_property && !strv_find(arg_property, name))
770                 /* skip what we didn't read */
771                 return sd_bus_message_skip(m, contents);
772
773         switch (contents[0]) {
774
775         case SD_BUS_TYPE_STRUCT_BEGIN:
776
777                 if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
778                         const char *s;
779
780                         r = sd_bus_message_read(m, "(so)", &s, NULL);
781                         if (r < 0)
782                                 return bus_log_parse_error(r);
783
784                         if (arg_all || !isempty(s))
785                                 property(name, "%s", s);
786
787                         return 0;
788
789                 } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
790                         uint32_t uid;
791
792                         r = sd_bus_message_read(m, "(uo)", &uid, NULL);
793                         if (r < 0)
794                                 return bus_log_parse_error(r);
795
796                         if (!uid_is_valid(uid)) {
797                                 log_error("Invalid user ID: " UID_FMT, uid);
798                                 return -EINVAL;
799                         }
800
801                         property(name, UID_FMT, uid);
802                         return 0;
803                 }
804
805                 break;
806
807         case SD_BUS_TYPE_ARRAY:
808
809                 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
810                         const char *s;
811                         bool space = false;
812
813                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
814                         if (r < 0)
815                                 return bus_log_parse_error(r);
816
817                         if (!arg_value)
818                                 printf("%s=", name);
819
820                         while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
821                                 printf("%s%s", space ? " " : "", s);
822                                 space = true;
823                         }
824
825                         if (space || !arg_value)
826                                 printf("\n");
827
828                         if (r < 0)
829                                 return bus_log_parse_error(r);
830
831                         r = sd_bus_message_exit_container(m);
832                         if (r < 0)
833                                 return bus_log_parse_error(r);
834
835                         return 0;
836                 }
837
838                 break;
839         }
840
841         r = bus_print_property(name, m, arg_value, arg_all);
842         if (r < 0)
843                 return bus_log_parse_error(r);
844
845         if (r == 0) {
846                 r = sd_bus_message_skip(m, contents);
847                 if (r < 0)
848                         return bus_log_parse_error(r);
849
850                 if (arg_all)
851                         printf("%s=[unprintable]\n", name);
852         }
853
854         return 0;
855 }
856
857 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
858         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
859         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
860         int r;
861
862         assert(bus);
863         assert(path);
864         assert(new_line);
865
866         r = sd_bus_call_method(
867                         bus,
868                         "org.freedesktop.login1",
869                         path,
870                         "org.freedesktop.DBus.Properties",
871                         "GetAll",
872                         &error,
873                         &reply,
874                         "s", "");
875         if (r < 0)
876                 return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
877
878         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
879         if (r < 0)
880                 return bus_log_parse_error(r);
881
882         if (*new_line)
883                 printf("\n");
884
885         *new_line = true;
886
887         while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
888                 const char *name, *contents;
889
890                 r = sd_bus_message_read(reply, "s", &name);
891                 if (r < 0)
892                         return bus_log_parse_error(r);
893
894                 r = sd_bus_message_peek_type(reply, NULL, &contents);
895                 if (r < 0)
896                         return bus_log_parse_error(r);
897
898                 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
899                 if (r < 0)
900                         return bus_log_parse_error(r);
901
902                 r = print_property(name, reply, contents);
903                 if (r < 0)
904                         return r;
905
906                 r = sd_bus_message_exit_container(reply);
907                 if (r < 0)
908                         return bus_log_parse_error(r);
909
910                 r = sd_bus_message_exit_container(reply);
911                 if (r < 0)
912                         return bus_log_parse_error(r);
913         }
914         if (r < 0)
915                 return bus_log_parse_error(r);
916
917         r = sd_bus_message_exit_container(reply);
918         if (r < 0)
919                 return bus_log_parse_error(r);
920
921         return 0;
922 }
923
924 static int show_session(int argc, char *argv[], void *userdata) {
925         bool properties, new_line = false;
926         sd_bus *bus = userdata;
927         int r, i;
928
929         assert(bus);
930         assert(argv);
931
932         properties = !strstr(argv[0], "status");
933
934         pager_open(arg_no_pager, false);
935
936         if (argc <= 1) {
937                 /* If not argument is specified inspect the manager
938                  * itself */
939                 if (properties)
940                         return show_properties(bus, "/org/freedesktop/login1", &new_line);
941
942                 /* And in the pretty case, show data of the calling session */
943                 return print_session_status_info(bus, "/org/freedesktop/login1/session/self", &new_line);
944         }
945
946         for (i = 1; i < argc; i++) {
947                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
948                 _cleanup_free_ char *path = NULL;
949
950                 r = get_session_path(bus, argv[1], &error, &path);
951                 if (r < 0) {
952                         log_error("Failed to get session path: %s", bus_error_message(&error, r));
953                         return r;
954                 }
955
956                 if (properties)
957                         r = show_properties(bus, path, &new_line);
958                 else
959                         r = print_session_status_info(bus, path, &new_line);
960
961                 if (r < 0)
962                         return r;
963         }
964
965         return 0;
966 }
967
968 static int show_user(int argc, char *argv[], void *userdata) {
969         bool properties, new_line = false;
970         sd_bus *bus = userdata;
971         int r, i;
972
973         assert(bus);
974         assert(argv);
975
976         properties = !strstr(argv[0], "status");
977
978         pager_open(arg_no_pager, false);
979
980         if (argc <= 1) {
981                 /* If not argument is specified inspect the manager
982                  * itself */
983                 if (properties)
984                         return show_properties(bus, "/org/freedesktop/login1", &new_line);
985
986                 return print_user_status_info(bus, "/org/freedesktop/login1/user/self", &new_line);
987         }
988
989         for (i = 1; i < argc; i++) {
990                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
991                 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
992                 const char *path = NULL;
993                 uid_t uid;
994
995                 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
996                 if (r < 0)
997                         return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
998
999                 r = sd_bus_call_method(
1000                                 bus,
1001                                 "org.freedesktop.login1",
1002                                 "/org/freedesktop/login1",
1003                                 "org.freedesktop.login1.Manager",
1004                                 "GetUser",
1005                                 &error, &reply,
1006                                 "u", (uint32_t) uid);
1007                 if (r < 0) {
1008                         log_error("Failed to get user: %s", bus_error_message(&error, r));
1009                         return r;
1010                 }
1011
1012                 r = sd_bus_message_read(reply, "o", &path);
1013                 if (r < 0)
1014                         return bus_log_parse_error(r);
1015
1016                 if (properties)
1017                         r = show_properties(bus, path, &new_line);
1018                 else
1019                         r = print_user_status_info(bus, path, &new_line);
1020
1021                 if (r < 0)
1022                         return r;
1023         }
1024
1025         return 0;
1026 }
1027
1028 static int show_seat(int argc, char *argv[], void *userdata) {
1029         bool properties, new_line = false;
1030         sd_bus *bus = userdata;
1031         int r, i;
1032
1033         assert(bus);
1034         assert(argv);
1035
1036         properties = !strstr(argv[0], "status");
1037
1038         pager_open(arg_no_pager, false);
1039
1040         if (argc <= 1) {
1041                 /* If not argument is specified inspect the manager
1042                  * itself */
1043                 if (properties)
1044                         return show_properties(bus, "/org/freedesktop/login1", &new_line);
1045
1046                 return print_seat_status_info(bus, "/org/freedesktop/login1/seat/self", &new_line);
1047         }
1048
1049         for (i = 1; i < argc; i++) {
1050                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1051                 _cleanup_(sd_bus_message_unrefp) sd_bus_message * reply = NULL;
1052                 const char *path = NULL;
1053
1054                 r = sd_bus_call_method(
1055                                 bus,
1056                                 "org.freedesktop.login1",
1057                                 "/org/freedesktop/login1",
1058                                 "org.freedesktop.login1.Manager",
1059                                 "GetSeat",
1060                                 &error, &reply,
1061                                 "s", argv[i]);
1062                 if (r < 0) {
1063                         log_error("Failed to get seat: %s", bus_error_message(&error, r));
1064                         return r;
1065                 }
1066
1067                 r = sd_bus_message_read(reply, "o", &path);
1068                 if (r < 0)
1069                         return bus_log_parse_error(r);
1070
1071                 if (properties)
1072                         r = show_properties(bus, path, &new_line);
1073                 else
1074                         r = print_seat_status_info(bus, path, &new_line);
1075
1076                 if (r < 0)
1077                         return r;
1078         }
1079
1080         return 0;
1081 }
1082
1083 static int activate(int argc, char *argv[], void *userdata) {
1084         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1085         sd_bus *bus = userdata;
1086         char *short_argv[3];
1087         int r, i;
1088
1089         assert(bus);
1090         assert(argv);
1091
1092         polkit_agent_open_if_enabled();
1093
1094         if (argc < 2) {
1095                 /* No argument? Let's convert this into the empty
1096                  * session name, which the calls will then resolve to
1097                  * the caller's session. */
1098
1099                 short_argv[0] = argv[0];
1100                 short_argv[1] = (char*) "";
1101                 short_argv[2] = NULL;
1102
1103                 argv = short_argv;
1104                 argc = 2;
1105         }
1106
1107         for (i = 1; i < argc; i++) {
1108
1109                 r = sd_bus_call_method(
1110                                 bus,
1111                                 "org.freedesktop.login1",
1112                                 "/org/freedesktop/login1",
1113                                 "org.freedesktop.login1.Manager",
1114                                 streq(argv[0], "lock-session")      ? "LockSession" :
1115                                 streq(argv[0], "unlock-session")    ? "UnlockSession" :
1116                                 streq(argv[0], "terminate-session") ? "TerminateSession" :
1117                                                                       "ActivateSession",
1118                                 &error, NULL,
1119                                 "s", argv[i]);
1120                 if (r < 0) {
1121                         log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
1122                         return r;
1123                 }
1124         }
1125
1126         return 0;
1127 }
1128
1129 static int kill_session(int argc, char *argv[], void *userdata) {
1130         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1131         sd_bus *bus = userdata;
1132         int r, i;
1133
1134         assert(bus);
1135         assert(argv);
1136
1137         polkit_agent_open_if_enabled();
1138
1139         if (!arg_kill_who)
1140                 arg_kill_who = "all";
1141
1142         for (i = 1; i < argc; i++) {
1143
1144                 r = sd_bus_call_method(
1145                         bus,
1146                         "org.freedesktop.login1",
1147                         "/org/freedesktop/login1",
1148                         "org.freedesktop.login1.Manager",
1149                         "KillSession",
1150                         &error, NULL,
1151                         "ssi", argv[i], arg_kill_who, arg_signal);
1152                 if (r < 0) {
1153                         log_error("Could not kill session: %s", bus_error_message(&error, -r));
1154                         return r;
1155                 }
1156         }
1157
1158         return 0;
1159 }
1160
1161 static int enable_linger(int argc, char *argv[], void *userdata) {
1162         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1163         sd_bus *bus = userdata;
1164         char* short_argv[3];
1165         bool b;
1166         int r, i;
1167
1168         assert(bus);
1169         assert(argv);
1170
1171         polkit_agent_open_if_enabled();
1172
1173         b = streq(argv[0], "enable-linger");
1174
1175         if (argc < 2) {
1176                 short_argv[0] = argv[0];
1177                 short_argv[1] = (char*) "";
1178                 short_argv[2] = NULL;
1179                 argv = short_argv;
1180                 argc = 2;
1181         }
1182
1183         for (i = 1; i < argc; i++) {
1184                 uid_t uid;
1185
1186                 if (isempty(argv[i]))
1187                         uid = UID_INVALID;
1188                 else {
1189                         r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1190                         if (r < 0)
1191                                 return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1192                 }
1193
1194                 r = sd_bus_call_method(
1195                         bus,
1196                         "org.freedesktop.login1",
1197                         "/org/freedesktop/login1",
1198                         "org.freedesktop.login1.Manager",
1199                         "SetUserLinger",
1200                         &error, NULL,
1201                         "ubb", (uint32_t) uid, b, true);
1202                 if (r < 0) {
1203                         log_error("Could not enable linger: %s", bus_error_message(&error, -r));
1204                         return r;
1205                 }
1206         }
1207
1208         return 0;
1209 }
1210
1211 static int terminate_user(int argc, char *argv[], void *userdata) {
1212         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1213         sd_bus *bus = userdata;
1214         int r, i;
1215
1216         assert(bus);
1217         assert(argv);
1218
1219         polkit_agent_open_if_enabled();
1220
1221         for (i = 1; i < argc; i++) {
1222                 uid_t uid;
1223
1224                 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1225                 if (r < 0)
1226                         return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1227
1228                 r = sd_bus_call_method(
1229                         bus,
1230                         "org.freedesktop.login1",
1231                         "/org/freedesktop/login1",
1232                         "org.freedesktop.login1.Manager",
1233                         "TerminateUser",
1234                         &error, NULL,
1235                         "u", (uint32_t) uid);
1236                 if (r < 0) {
1237                         log_error("Could not terminate user: %s", bus_error_message(&error, -r));
1238                         return r;
1239                 }
1240         }
1241
1242         return 0;
1243 }
1244
1245 static int kill_user(int argc, char *argv[], void *userdata) {
1246         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1247         sd_bus *bus = userdata;
1248         int r, i;
1249
1250         assert(bus);
1251         assert(argv);
1252
1253         polkit_agent_open_if_enabled();
1254
1255         if (!arg_kill_who)
1256                 arg_kill_who = "all";
1257
1258         for (i = 1; i < argc; i++) {
1259                 uid_t uid;
1260
1261                 r = get_user_creds((const char**) (argv+i), &uid, NULL, NULL, NULL);
1262                 if (r < 0)
1263                         return log_error_errno(r, "Failed to look up user %s: %m", argv[i]);
1264
1265                 r = sd_bus_call_method(
1266                         bus,
1267                         "org.freedesktop.login1",
1268                         "/org/freedesktop/login1",
1269                         "org.freedesktop.login1.Manager",
1270                         "KillUser",
1271                         &error, NULL,
1272                         "ui", (uint32_t) uid, arg_signal);
1273                 if (r < 0) {
1274                         log_error("Could not kill user: %s", bus_error_message(&error, -r));
1275                         return r;
1276                 }
1277         }
1278
1279         return 0;
1280 }
1281
1282 static int attach(int argc, char *argv[], void *userdata) {
1283         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1284         sd_bus *bus = userdata;
1285         int r, i;
1286
1287         assert(bus);
1288         assert(argv);
1289
1290         polkit_agent_open_if_enabled();
1291
1292         for (i = 2; i < argc; i++) {
1293
1294                 r = sd_bus_call_method(
1295                         bus,
1296                         "org.freedesktop.login1",
1297                         "/org/freedesktop/login1",
1298                         "org.freedesktop.login1.Manager",
1299                         "AttachDevice",
1300                         &error, NULL,
1301                         "ssb", argv[1], argv[i], true);
1302
1303                 if (r < 0) {
1304                         log_error("Could not attach device: %s", bus_error_message(&error, -r));
1305                         return r;
1306                 }
1307         }
1308
1309         return 0;
1310 }
1311
1312 static int flush_devices(int argc, char *argv[], void *userdata) {
1313         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1314         sd_bus *bus = userdata;
1315         int r;
1316
1317         assert(bus);
1318         assert(argv);
1319
1320         polkit_agent_open_if_enabled();
1321
1322         r = sd_bus_call_method(
1323                         bus,
1324                         "org.freedesktop.login1",
1325                         "/org/freedesktop/login1",
1326                         "org.freedesktop.login1.Manager",
1327                         "FlushDevices",
1328                         &error, NULL,
1329                         "b", true);
1330         if (r < 0)
1331                 log_error("Could not flush devices: %s", bus_error_message(&error, -r));
1332
1333         return r;
1334 }
1335
1336 static int lock_sessions(int argc, char *argv[], void *userdata) {
1337         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1338         sd_bus *bus = userdata;
1339         int r;
1340
1341         assert(bus);
1342         assert(argv);
1343
1344         polkit_agent_open_if_enabled();
1345
1346         r = sd_bus_call_method(
1347                         bus,
1348                         "org.freedesktop.login1",
1349                         "/org/freedesktop/login1",
1350                         "org.freedesktop.login1.Manager",
1351                         streq(argv[0], "lock-sessions") ? "LockSessions" : "UnlockSessions",
1352                         &error, NULL,
1353                         NULL);
1354         if (r < 0)
1355                 log_error("Could not lock sessions: %s", bus_error_message(&error, -r));
1356
1357         return r;
1358 }
1359
1360 static int terminate_seat(int argc, char *argv[], void *userdata) {
1361         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1362         sd_bus *bus = userdata;
1363         int r, i;
1364
1365         assert(bus);
1366         assert(argv);
1367
1368         polkit_agent_open_if_enabled();
1369
1370         for (i = 1; i < argc; i++) {
1371
1372                 r = sd_bus_call_method(
1373                         bus,
1374                         "org.freedesktop.login1",
1375                         "/org/freedesktop/login1",
1376                         "org.freedesktop.login1.Manager",
1377                         "TerminateSeat",
1378                         &error, NULL,
1379                         "s", argv[i]);
1380                 if (r < 0) {
1381                         log_error("Could not terminate seat: %s", bus_error_message(&error, -r));
1382                         return r;
1383                 }
1384         }
1385
1386         return 0;
1387 }
1388
1389 static int help(int argc, char *argv[], void *userdata) {
1390
1391         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1392                "Send control commands to or query the login manager.\n\n"
1393                "  -h --help                Show this help\n"
1394                "     --version             Show package version\n"
1395                "     --no-pager            Do not pipe output into a pager\n"
1396 #if 1 /// elogind supports --no-wall
1397                "     --no-wall             Do not print any wall message\n"
1398 #endif // 1
1399                "     --no-legend           Do not show the headers and footers\n"
1400                "     --no-ask-password     Don't prompt for password\n"
1401                "  -H --host=[USER@]HOST    Operate on remote host\n"
1402                "  -M --machine=CONTAINER   Operate on local container\n"
1403                "  -p --property=NAME       Show only properties by this name\n"
1404                "  -a --all                 Show all properties, including empty ones\n"
1405                "     --value               When showing properties, only print the value\n"
1406                "  -l --full                Do not ellipsize output\n"
1407                "     --kill-who=WHO        Who to send signal to\n"
1408                "  -s --signal=SIGNAL       Which signal to send\n"
1409 #if 0 /// UNNEEDED by elogind
1410                "  -n --lines=INTEGER       Number of journal entries to show\n"
1411                "  -o --output=STRING       Change journal output mode (short, short-monotonic,\n"
1412                "                           verbose, export, json, json-pretty, json-sse, cat)\n\n"
1413 #else
1414                 /// elogind can cancel shutdowns and allows to ignore inhibitors
1415                "  -c                       Cancel a pending shutdown or reboot\n"
1416                "  -i --ignore-inhibitors   When shutting down or sleeping, ignore inhibitors\n\n"
1417 #endif // 0
1418                "Session Commands:\n"
1419                "  list-sessions            List sessions\n"
1420                "  session-status [ID...]   Show session status\n"
1421                "  show-session [ID...]     Show properties of sessions or the manager\n"
1422                "  activate [ID]            Activate a session\n"
1423                "  lock-session [ID...]     Screen lock one or more sessions\n"
1424                "  unlock-session [ID...]   Screen unlock one or more sessions\n"
1425                "  lock-sessions            Screen lock all current sessions\n"
1426                "  unlock-sessions          Screen unlock all current sessions\n"
1427                "  terminate-session ID...  Terminate one or more sessions\n"
1428                "  kill-session ID...       Send signal to processes of a session\n\n"
1429                "User Commands:\n"
1430                "  list-users               List users\n"
1431                "  user-status [USER...]    Show user status\n"
1432                "  show-user [USER...]      Show properties of users or the manager\n"
1433                "  enable-linger [USER...]  Enable linger state of one or more users\n"
1434                "  disable-linger [USER...] Disable linger state of one or more users\n"
1435                "  terminate-user USER...   Terminate all sessions of one or more users\n"
1436                "  kill-user USER...        Send signal to processes of a user\n\n"
1437                "Seat Commands:\n"
1438                "  list-seats               List seats\n"
1439                "  seat-status [NAME...]    Show seat status\n"
1440                "  show-seat [NAME...]      Show properties of seats or the manager\n"
1441                "  attach NAME DEVICE...    Attach one or more devices to a seat\n"
1442                "  flush-devices            Flush all device associations\n"
1443                "  terminate-seat NAME...   Terminate all sessions on one or more seats\n\n"
1444 #if 1 /// elogind adds some system commands to loginctl
1445                "System Commands:\n"
1446                "  poweroff [TIME] [WALL...] Turn off the machine\n"
1447                "  reboot   [TIME] [WALL...] Reboot the machine\n"
1448                "  suspend                   Suspend the machine to memory\n"
1449                "  hibernate                 Suspend the machine to disk\n"
1450                "  hybrid-sleep              Suspend the machine to memory and disk\n"
1451 #endif // 1
1452                , program_invocation_short_name);
1453
1454         return 0;
1455 }
1456
1457 static int parse_argv(int argc, char *argv[]) {
1458
1459         enum {
1460                 ARG_VERSION = 0x100,
1461                 ARG_VALUE,
1462                 ARG_NO_PAGER,
1463 #if 1 /// elogind supports --no-wall
1464                 ARG_NO_WALL,
1465 #endif // 1
1466                 ARG_NO_LEGEND,
1467                 ARG_KILL_WHO,
1468                 ARG_NO_ASK_PASSWORD,
1469         };
1470
1471         static const struct option options[] = {
1472                 { "help",            no_argument,       NULL, 'h'                 },
1473                 { "version",         no_argument,       NULL, ARG_VERSION         },
1474                 { "property",        required_argument, NULL, 'p'                 },
1475                 { "all",             no_argument,       NULL, 'a'                 },
1476                 { "value",           no_argument,       NULL, ARG_VALUE           },
1477                 { "full",            no_argument,       NULL, 'l'                 },
1478                 { "no-pager",        no_argument,       NULL, ARG_NO_PAGER        },
1479 #if 1 /// elogind supports --no-wall
1480                 { "no-wall",         no_argument,       NULL, ARG_NO_WALL         },
1481 #endif // 1
1482                 { "no-legend",       no_argument,       NULL, ARG_NO_LEGEND       },
1483                 { "kill-who",        required_argument, NULL, ARG_KILL_WHO        },
1484                 { "signal",          required_argument, NULL, 's'                 },
1485                 { "host",            required_argument, NULL, 'H'                 },
1486                 { "machine",         required_argument, NULL, 'M'                 },
1487                 { "no-ask-password", no_argument,       NULL, ARG_NO_ASK_PASSWORD },
1488 #if 0 /// UNNEEDED by elogind
1489                 { "lines",           required_argument, NULL, 'n'                 },
1490                 { "output",          required_argument, NULL, 'o'                 },
1491 #else
1492                 /// elogind allows to ignore inhibitors for system commands.
1493                 { "ignore-inhibitors", no_argument,     NULL, 'i'                 },
1494 #endif // 0
1495                 {}
1496         };
1497
1498         int c, r;
1499
1500         assert(argc >= 0);
1501         assert(argv);
1502
1503 #if 0 /// elogind adds some system commands to loginctl
1504         while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:", options, NULL)) >= 0)
1505 #else
1506         while ((c = getopt_long(argc, argv, "hp:als:H:M:n:o:ci", options, NULL)) >= 0)
1507 #endif // 0
1508
1509                 switch (c) {
1510
1511                 case 'h':
1512                         help(0, NULL, NULL);
1513                         return 0;
1514
1515                 case ARG_VERSION:
1516                         return version();
1517
1518                 case 'p': {
1519                         r = strv_extend(&arg_property, optarg);
1520                         if (r < 0)
1521                                 return log_oom();
1522
1523                         /* If the user asked for a particular
1524                          * property, show it to him, even if it is
1525                          * empty. */
1526                         arg_all = true;
1527                         break;
1528                 }
1529
1530                 case 'a':
1531                         arg_all = true;
1532                         break;
1533
1534                 case ARG_VALUE:
1535                         arg_value = true;
1536                         break;
1537
1538                 case 'l':
1539                         arg_full = true;
1540                         break;
1541
1542 #if 0 /// UNNEEDED by elogind
1543                 case 'n':
1544                         if (safe_atou(optarg, &arg_lines) < 0) {
1545                                 log_error("Failed to parse lines '%s'", optarg);
1546                                 return -EINVAL;
1547                         }
1548                         break;
1549
1550                 case 'o':
1551                         arg_output = output_mode_from_string(optarg);
1552                         if (arg_output < 0) {
1553                                 log_error("Unknown output '%s'.", optarg);
1554                                 return -EINVAL;
1555                         }
1556                         break;
1557 #endif // 0
1558
1559                 case ARG_NO_PAGER:
1560                         arg_no_pager = true;
1561                         break;
1562 #if 1 /// elogind supports --no-wall
1563                 case ARG_NO_WALL:
1564                         arg_no_wall = true;
1565                         break;
1566 #endif // 1
1567
1568                 case ARG_NO_LEGEND:
1569                         arg_legend = false;
1570                         break;
1571
1572                 case ARG_NO_ASK_PASSWORD:
1573                         arg_ask_password = false;
1574                         break;
1575
1576                 case ARG_KILL_WHO:
1577                         arg_kill_who = optarg;
1578                         break;
1579
1580                 case 's':
1581                         arg_signal = signal_from_string_try_harder(optarg);
1582                         if (arg_signal < 0) {
1583                                 log_error("Failed to parse signal string %s.", optarg);
1584                                 return -EINVAL;
1585                         }
1586                         break;
1587
1588                 case 'H':
1589                         arg_transport = BUS_TRANSPORT_REMOTE;
1590                         arg_host = optarg;
1591                         break;
1592
1593                 case 'M':
1594                         arg_transport = BUS_TRANSPORT_MACHINE;
1595                         arg_host = optarg;
1596                         break;
1597 #if 1 /// elogind can cancel shutdowns and allows to ignore inhibitors
1598                 case 'c':
1599                         arg_action = ACTION_CANCEL_SHUTDOWN;
1600                         break;
1601
1602                 case 'i':
1603                         arg_ignore_inhibitors = true;
1604                         break;
1605 #endif // 1
1606                 case '?':
1607                         return -EINVAL;
1608
1609                 default:
1610                         assert_not_reached("Unhandled option");
1611                 }
1612
1613         return 1;
1614 }
1615
1616 static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
1617
1618         static const Verb verbs[] = {
1619                 { "help",              VERB_ANY, VERB_ANY, 0,            help              },
1620                 { "list-sessions",     VERB_ANY, 1,        VERB_DEFAULT, list_sessions     },
1621                 { "session-status",    VERB_ANY, VERB_ANY, 0,            show_session      },
1622                 { "show-session",      VERB_ANY, VERB_ANY, 0,            show_session      },
1623                 { "activate",          VERB_ANY, 2,        0,            activate          },
1624                 { "lock-session",      VERB_ANY, VERB_ANY, 0,            activate          },
1625                 { "unlock-session",    VERB_ANY, VERB_ANY, 0,            activate          },
1626                 { "lock-sessions",     VERB_ANY, 1,        0,            lock_sessions     },
1627                 { "unlock-sessions",   VERB_ANY, 1,        0,            lock_sessions     },
1628                 { "terminate-session", 2,        VERB_ANY, 0,            activate          },
1629                 { "kill-session",      2,        VERB_ANY, 0,            kill_session      },
1630                 { "list-users",        VERB_ANY, 1,        0,            list_users        },
1631                 { "user-status",       VERB_ANY, VERB_ANY, 0,            show_user         },
1632                 { "show-user",         VERB_ANY, VERB_ANY, 0,            show_user         },
1633                 { "enable-linger",     VERB_ANY, VERB_ANY, 0,            enable_linger     },
1634                 { "disable-linger",    VERB_ANY, VERB_ANY, 0,            enable_linger     },
1635                 { "terminate-user",    2,        VERB_ANY, 0,            terminate_user    },
1636                 { "kill-user",         2,        VERB_ANY, 0,            kill_user         },
1637                 { "list-seats",        VERB_ANY, 1,        0,            list_seats        },
1638                 { "seat-status",       VERB_ANY, VERB_ANY, 0,            show_seat         },
1639                 { "show-seat",         VERB_ANY, VERB_ANY, 0,            show_seat         },
1640                 { "attach",            3,        VERB_ANY, 0,            attach            },
1641                 { "flush-devices",     VERB_ANY, 1,        0,            flush_devices     },
1642                 { "terminate-seat",    2,        VERB_ANY, 0,            terminate_seat    },
1643 #if 1 /// elogind adds some system commands to loginctl
1644                 { "poweroff",          VERB_ANY, VERB_ANY, 0,            start_special     },
1645                 { "reboot",            VERB_ANY, VERB_ANY, 0,            start_special     },
1646                 { "suspend",           VERB_ANY, 1,        0,            start_special     },
1647                 { "hibernate",         VERB_ANY, 1,        0,            start_special     },
1648                 { "hybrid-sleep",      VERB_ANY, 1,        0,            start_special     },
1649                 { "cancel-shutdown",   VERB_ANY, 1,        0,            start_special     },
1650 #endif // 1
1651                 {}
1652         };
1653
1654 #if 1 /// elogind can do shutdown and allows its cancellation
1655         if ((argc == optind) && (ACTION_CANCEL_SHUTDOWN == arg_action))
1656                 return elogind_cancel_shutdown(bus);
1657 #endif // 1
1658         return dispatch_verb(argc, argv, verbs, bus);
1659 }
1660
1661 int main(int argc, char *argv[]) {
1662         sd_bus *bus = NULL;
1663         int r;
1664
1665         setlocale(LC_ALL, "");
1666         elogind_set_program_name(argv[0]);
1667         log_parse_environment();
1668         log_open();
1669
1670         r = parse_argv(argc, argv);
1671         if (r <= 0)
1672                 goto finish;
1673
1674         r = bus_connect_transport(arg_transport, arg_host, false, &bus);
1675         if (r < 0) {
1676                 log_error_errno(r, "Failed to create bus connection: %m");
1677                 goto finish;
1678         }
1679
1680         sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
1681
1682         r = loginctl_main(argc, argv, bus);
1683
1684 finish:
1685         sd_bus_flush_close_unref(bus);
1686
1687         pager_close();
1688 #if 0 /// elogind does that in elogind_cleanup()
1689         polkit_agent_close();
1690 #endif // 0
1691
1692         strv_free(arg_property);
1693
1694 #if 1 /// elogind has some own cleanups to do
1695         elogind_cleanup();
1696 #endif // 1
1697         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1698 }