chiark / gitweb /
loginctl: implement missing kill verb
[elogind.git] / src / loginctl.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <dbus/dbus.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <pwd.h>
28
29 #include "log.h"
30 #include "util.h"
31 #include "macro.h"
32 #include "pager.h"
33 #include "dbus-common.h"
34 #include "build.h"
35 #include "strv.h"
36 #include "cgroup-show.h"
37 #include "sysfs-show.h"
38
39 static char **arg_property = NULL;
40 static bool arg_all = false;
41 static bool arg_no_pager = false;
42 static const char *arg_kill_who = NULL;
43 static int arg_signal = SIGTERM;
44 static enum transport {
45         TRANSPORT_NORMAL,
46         TRANSPORT_SSH,
47         TRANSPORT_POLKIT
48 } arg_transport = TRANSPORT_NORMAL;
49 static const char *arg_host = NULL;
50
51 static bool on_tty(void) {
52         static int t = -1;
53
54         /* Note that this is invoked relatively early, before we start
55          * the pager. That means the value we return reflects whether
56          * we originally were started on a tty, not if we currently
57          * are. But this is intended, since we want colour and so on
58          * when run in our own pager. */
59
60         if (_unlikely_(t < 0))
61                 t = isatty(STDOUT_FILENO) > 0;
62
63         return t;
64 }
65
66 static void pager_open_if_enabled(void) {
67         on_tty();
68
69         if (!arg_no_pager)
70                 pager_open();
71 }
72
73 static int list_sessions(DBusConnection *bus, char **args, unsigned n) {
74         DBusMessage *m = NULL, *reply = NULL;
75         DBusError error;
76         int r;
77         DBusMessageIter iter, sub, sub2;
78         unsigned k = 0;
79
80         dbus_error_init(&error);
81
82         assert(bus);
83
84         pager_open_if_enabled();
85
86         m = dbus_message_new_method_call(
87                         "org.freedesktop.login1",
88                         "/org/freedesktop/login1",
89                         "org.freedesktop.login1.Manager",
90                         "ListSessions");
91         if (!m) {
92                 log_error("Could not allocate message.");
93                 return -ENOMEM;
94         }
95
96         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
97         if (!reply) {
98                 log_error("Failed to issue method call: %s", bus_error_message(&error));
99                 r = -EIO;
100                 goto finish;
101         }
102
103         if (!dbus_message_iter_init(reply, &iter) ||
104             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
105             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
106                 log_error("Failed to parse reply.");
107                 r = -EIO;
108                 goto finish;
109         }
110
111         dbus_message_iter_recurse(&iter, &sub);
112
113         if (on_tty())
114                 printf("%10s %10s %-16s %-16s\n", "SESSION", "UID", "USER", "SEAT");
115
116         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
117                 const char *id, *user, *seat, *object;
118                 uint32_t uid;
119
120                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
121                         log_error("Failed to parse reply.");
122                         r = -EIO;
123                         goto finish;
124                 }
125
126                 dbus_message_iter_recurse(&sub, &sub2);
127
128                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
129                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
130                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
131                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
132                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
133                         log_error("Failed to parse reply.");
134                         r = -EIO;
135                         goto finish;
136                 }
137
138                 printf("%10s %10u %-16s %-16s\n", id, (unsigned) uid, user, seat);
139
140                 k++;
141
142                 dbus_message_iter_next(&sub);
143         }
144
145         if (on_tty())
146                 printf("\n%u sessions listed.\n", k);
147
148         r = 0;
149
150 finish:
151         if (m)
152                 dbus_message_unref(m);
153
154         if (reply)
155                 dbus_message_unref(reply);
156
157         dbus_error_free(&error);
158
159         return r;
160 }
161
162 static int list_users(DBusConnection *bus, char **args, unsigned n) {
163         DBusMessage *m = NULL, *reply = NULL;
164         DBusError error;
165         int r;
166         DBusMessageIter iter, sub, sub2;
167         unsigned k = 0;
168
169         dbus_error_init(&error);
170
171         assert(bus);
172
173         pager_open_if_enabled();
174
175         m = dbus_message_new_method_call(
176                         "org.freedesktop.login1",
177                         "/org/freedesktop/login1",
178                         "org.freedesktop.login1.Manager",
179                         "ListUsers");
180         if (!m) {
181                 log_error("Could not allocate message.");
182                 return -ENOMEM;
183         }
184
185         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
186         if (!reply) {
187                 log_error("Failed to issue method call: %s", bus_error_message(&error));
188                 r = -EIO;
189                 goto finish;
190         }
191
192         if (!dbus_message_iter_init(reply, &iter) ||
193             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
194             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
195                 log_error("Failed to parse reply.");
196                 r = -EIO;
197                 goto finish;
198         }
199
200         dbus_message_iter_recurse(&iter, &sub);
201
202         if (on_tty())
203                 printf("%10s %-16s\n", "UID", "USER");
204
205         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
206                 const char *user, *object;
207                 uint32_t uid;
208
209                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
210                         log_error("Failed to parse reply.");
211                         r = -EIO;
212                         goto finish;
213                 }
214
215                 dbus_message_iter_recurse(&sub, &sub2);
216
217                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
218                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &user, true) < 0 ||
219                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
220                         log_error("Failed to parse reply.");
221                         r = -EIO;
222                         goto finish;
223                 }
224
225                 printf("%10u %-16s\n", (unsigned) uid, user);
226
227                 k++;
228
229                 dbus_message_iter_next(&sub);
230         }
231
232         if (on_tty())
233                 printf("\n%u users listed.\n", k);
234
235         r = 0;
236
237 finish:
238         if (m)
239                 dbus_message_unref(m);
240
241         if (reply)
242                 dbus_message_unref(reply);
243
244         dbus_error_free(&error);
245
246         return r;
247 }
248
249 static int list_seats(DBusConnection *bus, char **args, unsigned n) {
250         DBusMessage *m = NULL, *reply = NULL;
251         DBusError error;
252         int r;
253         DBusMessageIter iter, sub, sub2;
254         unsigned k = 0;
255
256         dbus_error_init(&error);
257
258         assert(bus);
259
260         pager_open_if_enabled();
261
262         m = dbus_message_new_method_call(
263                         "org.freedesktop.login1",
264                         "/org/freedesktop/login1",
265                         "org.freedesktop.login1.Manager",
266                         "ListSeats");
267         if (!m) {
268                 log_error("Could not allocate message.");
269                 return -ENOMEM;
270         }
271
272         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
273         if (!reply) {
274                 log_error("Failed to issue method call: %s", bus_error_message(&error));
275                 r = -EIO;
276                 goto finish;
277         }
278
279         if (!dbus_message_iter_init(reply, &iter) ||
280             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
281             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
282                 log_error("Failed to parse reply.");
283                 r = -EIO;
284                 goto finish;
285         }
286
287         dbus_message_iter_recurse(&iter, &sub);
288
289         if (on_tty())
290                 printf("%-16s\n", "SEAT");
291
292         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
293                 const char *seat, *object;
294
295                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
296                         log_error("Failed to parse reply.");
297                         r = -EIO;
298                         goto finish;
299                 }
300
301                 dbus_message_iter_recurse(&sub, &sub2);
302
303                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &seat, true) < 0 ||
304                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &object, false) < 0) {
305                         log_error("Failed to parse reply.");
306                         r = -EIO;
307                         goto finish;
308                 }
309
310                 printf("%-16s\n", seat);
311
312                 k++;
313
314                 dbus_message_iter_next(&sub);
315         }
316
317         if (on_tty())
318                 printf("\n%u seats listed.\n", k);
319
320         r = 0;
321
322 finish:
323         if (m)
324                 dbus_message_unref(m);
325
326         if (reply)
327                 dbus_message_unref(reply);
328
329         dbus_error_free(&error);
330
331         return r;
332 }
333
334 typedef struct SessionStatusInfo {
335         const char *id;
336         uid_t uid;
337         const char *name;
338         usec_t timestamp;
339         const char *control_group;
340         int vtnr;
341         const char *seat;
342         const char *tty;
343         const char *display;
344         bool remote;
345         const char *remote_host;
346         const char *remote_user;
347         const char *service;
348         pid_t leader;
349         const char *type;
350         bool active;
351 } SessionStatusInfo;
352
353 typedef struct UserStatusInfo {
354         uid_t uid;
355         const char *name;
356         usec_t timestamp;
357         const char *control_group;
358         const char *state;
359         char **sessions;
360         const char *display;
361 } UserStatusInfo;
362
363 typedef struct SeatStatusInfo {
364         const char *id;
365         const char *active_session;
366         char **sessions;
367 } SeatStatusInfo;
368
369 static void print_session_status_info(SessionStatusInfo *i) {
370         char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
371         char since2[FORMAT_TIMESTAMP_MAX], *s2;
372         assert(i);
373
374         printf("%s - ", strna(i->id));
375
376         if (i->name)
377                 printf("%s (%u)\n", i->name, (unsigned) i->uid);
378         else
379                 printf("%u\n", (unsigned) i->uid);
380
381         s1 = format_timestamp_pretty(since1, sizeof(since1), i->timestamp);
382         s2 = format_timestamp(since2, sizeof(since2), i->timestamp);
383
384         if (s1)
385                 printf("\t   Since: %s; %s\n", s2, s1);
386         else if (s2)
387                 printf("\t   Since: %s\n", s2);
388
389         if (i->leader > 0) {
390                 char *t = NULL;
391
392                 printf("\t  Leader: %u", (unsigned) i->leader);
393
394                 get_process_name(i->leader, &t);
395                 if (t) {
396                         printf(" (%s)", t);
397                         free(t);
398                 }
399
400                 printf("\n");
401         }
402
403         if (i->seat) {
404                 printf("\t    Seat: %s", i->seat);
405
406                 if (i->vtnr > 0)
407                         printf("; vc%i", i->vtnr);
408
409                 printf("\n");
410         }
411
412         if (i->tty)
413                 printf("\t     TTY: %s\n", i->tty);
414         else if (i->display)
415                 printf("\t Display: %s\n", i->display);
416
417         if (i->remote_host && i->remote_user)
418                 printf("\t  Remote: %s@%s\n", i->remote_user, i->remote_host);
419         else if (i->remote_host)
420                 printf("\t  Remote: %s\n", i->remote_host);
421         else if (i->remote_user)
422                 printf("\t  Remote: user %s\n", i->remote_user);
423         else if (i->remote)
424                 printf("\t  Remote: Yes\n");
425
426         if (i->service) {
427                 printf("\t Service: %s", i->service);
428
429                 if (i->type)
430                         printf("; type %s", i->type);
431
432                 printf("\n");
433         } else if (i->type)
434                 printf("\t    Type: %s\n", i->type);
435
436         printf("\t  Active: %s\n", yes_no(i->active));
437
438         if (i->control_group) {
439                 unsigned c;
440
441                 printf("\t  CGroup: %s\n", i->control_group);
442
443                 if (arg_transport != TRANSPORT_SSH) {
444                         c = columns();
445                         if (c > 18)
446                                 c -= 18;
447                         else
448                                 c = 0;
449
450                         show_cgroup_by_path(i->control_group, "\t\t  ", c);
451                 }
452         }
453 }
454
455 static void print_user_status_info(UserStatusInfo *i) {
456         char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
457         char since2[FORMAT_TIMESTAMP_MAX], *s2;
458         assert(i);
459
460         if (i->name)
461                 printf("%s (%u)\n", i->name, (unsigned) i->uid);
462         else
463                 printf("%u\n", (unsigned) i->uid);
464
465         s1 = format_timestamp_pretty(since1, sizeof(since1), i->timestamp);
466         s2 = format_timestamp(since2, sizeof(since2), i->timestamp);
467
468         if (s1)
469                 printf("\t   Since: %s; %s\n", s2, s1);
470         else if (s2)
471                 printf("\t   Since: %s\n", s2);
472
473         if (!isempty(i->state))
474                 printf("\t   State: %s\n", i->state);
475
476         if (!strv_isempty(i->sessions)) {
477                 char **l;
478                 printf("\tSessions:");
479
480                 STRV_FOREACH(l, i->sessions) {
481                         if (streq_ptr(*l, i->display))
482                                 printf(" *%s", *l);
483                         else
484                                 printf(" %s", *l);
485                 }
486
487                 printf("\n");
488         }
489
490         if (i->control_group) {
491                 unsigned c;
492
493                 printf("\t  CGroup: %s\n", i->control_group);
494
495                 if (arg_transport != TRANSPORT_SSH) {
496                         c = columns();
497                         if (c > 18)
498                                 c -= 18;
499                         else
500                                 c = 0;
501
502                         show_cgroup_by_path(i->control_group, "\t\t  ", c);
503                 }
504         }
505 }
506
507 static void print_seat_status_info(SeatStatusInfo *i) {
508         assert(i);
509
510         printf("%s\n", strna(i->id));
511
512         if (!strv_isempty(i->sessions)) {
513                 char **l;
514                 printf("\tSessions:");
515
516                 STRV_FOREACH(l, i->sessions) {
517                         if (streq_ptr(*l, i->active_session))
518                                 printf(" *%s", *l);
519                         else
520                                 printf(" %s", *l);
521                 }
522
523                 printf("\n");
524         }
525
526         if (arg_transport != TRANSPORT_SSH) {
527                 unsigned c;
528
529                 c = columns();
530                 if (c > 21)
531                         c -= 21;
532                 else
533                         c = 0;
534
535                 printf("\t Devices:\n");
536
537                 show_sysfs(i->id, "\t\t  ", c);
538         }
539 }
540
541 static int status_property_session(const char *name, DBusMessageIter *iter, SessionStatusInfo *i) {
542         assert(name);
543         assert(iter);
544         assert(i);
545
546         switch (dbus_message_iter_get_arg_type(iter)) {
547
548         case DBUS_TYPE_STRING: {
549                 const char *s;
550
551                 dbus_message_iter_get_basic(iter, &s);
552
553                 if (!isempty(s)) {
554                         if (streq(name, "Id"))
555                                 i->id = s;
556                         else if (streq(name, "Name"))
557                                 i->name = s;
558                         else if (streq(name, "ControlGroupPath"))
559                                 i->control_group = s;
560                         else if (streq(name, "TTY"))
561                                 i->tty = s;
562                         else if (streq(name, "Display"))
563                                 i->display = s;
564                         else if (streq(name, "RemoteHost"))
565                                 i->remote_host = s;
566                         else if (streq(name, "RemoteUser"))
567                                 i->remote_user = s;
568                         else if (streq(name, "Service"))
569                                 i->service = s;
570                         else if (streq(name, "Type"))
571                                 i->type = s;
572                 }
573                 break;
574         }
575
576         case DBUS_TYPE_UINT32: {
577                 uint32_t u;
578
579                 dbus_message_iter_get_basic(iter, &u);
580
581                 if (streq(name, "VTNr"))
582                         i->vtnr = (int) u;
583                 else if (streq(name, "Leader"))
584                         i->leader = (pid_t) u;
585
586                 break;
587         }
588
589         case DBUS_TYPE_BOOLEAN: {
590                 dbus_bool_t b;
591
592                 dbus_message_iter_get_basic(iter, &b);
593
594                 if (streq(name, "Remote"))
595                         i->remote = b;
596                 else if (streq(name, "Active"))
597                         i->active = b;
598
599                 break;
600         }
601
602         case DBUS_TYPE_UINT64: {
603                 uint64_t u;
604
605                 dbus_message_iter_get_basic(iter, &u);
606
607                 if (streq(name, "Timestamp"))
608                         i->timestamp = (usec_t) u;
609
610                 break;
611         }
612
613         case DBUS_TYPE_STRUCT: {
614                 DBusMessageIter sub;
615
616                 dbus_message_iter_recurse(iter, &sub);
617
618                 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "User")) {
619                         uint32_t u;
620
621                         dbus_message_iter_get_basic(&sub, &u);
622                         i->uid = (uid_t) u;
623
624                 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Seat")) {
625                         const char *s;
626
627                         dbus_message_iter_get_basic(&sub, &s);
628
629                         if (!isempty(s))
630                                 i->seat = s;
631                 }
632
633                 break;
634         }
635         }
636
637         return 0;
638 }
639
640 static int status_property_user(const char *name, DBusMessageIter *iter, UserStatusInfo *i) {
641         assert(name);
642         assert(iter);
643         assert(i);
644
645         switch (dbus_message_iter_get_arg_type(iter)) {
646
647         case DBUS_TYPE_STRING: {
648                 const char *s;
649
650                 dbus_message_iter_get_basic(iter, &s);
651
652                 if (!isempty(s)) {
653                         if (streq(name, "Name"))
654                                 i->name = s;
655                         else if (streq(name, "ControlGroupPath"))
656                                 i->control_group = s;
657                         else if (streq(name, "State"))
658                                 i->state = s;
659                 }
660                 break;
661         }
662
663         case DBUS_TYPE_UINT32: {
664                 uint32_t u;
665
666                 dbus_message_iter_get_basic(iter, &u);
667
668                 if (streq(name, "UID"))
669                         i->uid = (uid_t) u;
670
671                 break;
672         }
673
674         case DBUS_TYPE_UINT64: {
675                 uint64_t u;
676
677                 dbus_message_iter_get_basic(iter, &u);
678
679                 if (streq(name, "Timestamp"))
680                         i->timestamp = (usec_t) u;
681
682                 break;
683         }
684
685         case DBUS_TYPE_STRUCT: {
686                 DBusMessageIter sub;
687
688                 dbus_message_iter_recurse(iter, &sub);
689
690                 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Display")) {
691                         const char *s;
692
693                         dbus_message_iter_get_basic(&sub, &s);
694
695                         if (!isempty(s))
696                                 i->display = s;
697                 }
698
699                 break;
700         }
701
702         case DBUS_TYPE_ARRAY: {
703
704                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Sessions")) {
705                         DBusMessageIter sub, sub2;
706
707                         dbus_message_iter_recurse(iter, &sub);
708                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
709                                 const char *id;
710                                 const char *path;
711
712                                 dbus_message_iter_recurse(&sub, &sub2);
713
714                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) >= 0 &&
715                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &path, false) >= 0) {
716                                         char **l;
717
718                                         l = strv_append(i->sessions, id);
719                                         if (!l)
720                                                 return -ENOMEM;
721
722                                         strv_free(i->sessions);
723                                         i->sessions = l;
724                                 }
725
726                                 dbus_message_iter_next(&sub);
727                         }
728
729                         return 0;
730                 }
731         }
732         }
733
734         return 0;
735 }
736
737 static int status_property_seat(const char *name, DBusMessageIter *iter, SeatStatusInfo *i) {
738         assert(name);
739         assert(iter);
740         assert(i);
741
742         switch (dbus_message_iter_get_arg_type(iter)) {
743
744         case DBUS_TYPE_STRING: {
745                 const char *s;
746
747                 dbus_message_iter_get_basic(iter, &s);
748
749                 if (!isempty(s)) {
750                         if (streq(name, "Id"))
751                                 i->id = s;
752                 }
753                 break;
754         }
755
756         case DBUS_TYPE_STRUCT: {
757                 DBusMessageIter sub;
758
759                 dbus_message_iter_recurse(iter, &sub);
760
761                 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "ActiveSession")) {
762                         const char *s;
763
764                         dbus_message_iter_get_basic(&sub, &s);
765
766                         if (!isempty(s))
767                                 i->active_session = s;
768                 }
769
770                 break;
771         }
772
773         case DBUS_TYPE_ARRAY: {
774
775                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Sessions")) {
776                         DBusMessageIter sub, sub2;
777
778                         dbus_message_iter_recurse(iter, &sub);
779                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
780                                 const char *id;
781                                 const char *path;
782
783                                 dbus_message_iter_recurse(&sub, &sub2);
784
785                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) >= 0 &&
786                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &path, false) >= 0) {
787                                         char **l;
788
789                                         l = strv_append(i->sessions, id);
790                                         if (!l)
791                                                 return -ENOMEM;
792
793                                         strv_free(i->sessions);
794                                         i->sessions = l;
795                                 }
796
797                                 dbus_message_iter_next(&sub);
798                         }
799
800                         return 0;
801                 }
802         }
803         }
804
805         return 0;
806 }
807
808 static int print_property(const char *name, DBusMessageIter *iter) {
809         assert(name);
810         assert(iter);
811
812         if (arg_property && !strv_find(arg_property, name))
813                 return 0;
814
815         switch (dbus_message_iter_get_arg_type(iter)) {
816
817         case DBUS_TYPE_STRUCT: {
818                 DBusMessageIter sub;
819
820                 dbus_message_iter_recurse(iter, &sub);
821
822                 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING &&
823                     (streq(name, "Display") || streq(name, "ActiveSession"))) {
824                         const char *s;
825
826                         dbus_message_iter_get_basic(&sub, &s);
827
828                         if (arg_all || !isempty(s))
829                                 printf("%s=%s\n", name, s);
830                         return 0;
831                 }
832                 break;
833         }
834
835         case DBUS_TYPE_ARRAY:
836
837                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Sessions")) {
838                         DBusMessageIter sub, sub2;
839                         bool found = false;
840
841                         dbus_message_iter_recurse(iter, &sub);
842                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
843                                 const char *id;
844                                 const char *path;
845
846                                 dbus_message_iter_recurse(&sub, &sub2);
847
848                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) >= 0 &&
849                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &path, false) >= 0) {
850                                         if (found)
851                                                 printf(" %s", id);
852                                         else {
853                                                 printf("%s=%s", name, id);
854                                                 found = true;
855                                         }
856                                 }
857
858                                 dbus_message_iter_next(&sub);
859                         }
860
861                         if (!found && arg_all)
862                                 printf("%s=\n", name);
863                         else if (found)
864                                 printf("\n");
865
866                         return 0;
867                 }
868
869                 break;
870         }
871
872         if (generic_print_property(name, iter, arg_all) > 0)
873                 return 0;
874
875         if (arg_all)
876                 printf("%s=[unprintable]\n", name);
877
878         return 0;
879 }
880
881 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
882         DBusMessage *m = NULL, *reply = NULL;
883         const char *interface = "";
884         int r;
885         DBusError error;
886         DBusMessageIter iter, sub, sub2, sub3;
887         SessionStatusInfo session_info;
888         UserStatusInfo user_info;
889         SeatStatusInfo seat_info;
890
891         assert(bus);
892         assert(path);
893         assert(new_line);
894
895         zero(session_info);
896         zero(user_info);
897         zero(seat_info);
898
899         dbus_error_init(&error);
900
901         m = dbus_message_new_method_call(
902                         "org.freedesktop.login1",
903                         path,
904                         "org.freedesktop.DBus.Properties",
905                         "GetAll");
906         if (!m) {
907                 log_error("Could not allocate message.");
908                 r = -ENOMEM;
909                 goto finish;
910         }
911
912         if (!dbus_message_append_args(m,
913                                       DBUS_TYPE_STRING, &interface,
914                                       DBUS_TYPE_INVALID)) {
915                 log_error("Could not append arguments to message.");
916                 r = -ENOMEM;
917                 goto finish;
918         }
919
920         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
921         if (!reply) {
922                 log_error("Failed to issue method call: %s", bus_error_message(&error));
923                 r = -EIO;
924                 goto finish;
925         }
926
927         if (!dbus_message_iter_init(reply, &iter) ||
928             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
929             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY)  {
930                 log_error("Failed to parse reply.");
931                 r = -EIO;
932                 goto finish;
933         }
934
935         dbus_message_iter_recurse(&iter, &sub);
936
937         if (*new_line)
938                 printf("\n");
939
940         *new_line = true;
941
942         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
943                 const char *name;
944
945                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
946                         log_error("Failed to parse reply.");
947                         r = -EIO;
948                         goto finish;
949                 }
950
951                 dbus_message_iter_recurse(&sub, &sub2);
952
953                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
954                         log_error("Failed to parse reply.");
955                         r = -EIO;
956                         goto finish;
957                 }
958
959                 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT)  {
960                         log_error("Failed to parse reply.");
961                         r = -EIO;
962                         goto finish;
963                 }
964
965                 dbus_message_iter_recurse(&sub2, &sub3);
966
967                 if (show_properties)
968                         r = print_property(name, &sub3);
969                 else if (strstr(verb, "session"))
970                         r = status_property_session(name, &sub3, &session_info);
971                 else if (strstr(verb, "user"))
972                         r = status_property_user(name, &sub3, &user_info);
973                 else
974                         r = status_property_seat(name, &sub3, &seat_info);
975
976                 if (r < 0) {
977                         log_error("Failed to parse reply.");
978                         r = -EIO;
979                         goto finish;
980                 }
981
982                 dbus_message_iter_next(&sub);
983         }
984
985         if (!show_properties) {
986                 if (strstr(verb, "session"))
987                         print_session_status_info(&session_info);
988                 else if (strstr(verb, "user"))
989                         print_user_status_info(&user_info);
990                 else
991                         print_seat_status_info(&seat_info);
992         }
993
994         strv_free(seat_info.sessions);
995         strv_free(user_info.sessions);
996
997         r = 0;
998
999 finish:
1000         if (m)
1001                 dbus_message_unref(m);
1002
1003         if (reply)
1004                 dbus_message_unref(reply);
1005
1006         dbus_error_free(&error);
1007
1008         return r;
1009 }
1010
1011 static int show(DBusConnection *bus, char **args, unsigned n) {
1012         DBusMessage *m = NULL, *reply = NULL;
1013         int r, ret = 0;
1014         DBusError error;
1015         unsigned i;
1016         bool show_properties, new_line = false;
1017
1018         assert(bus);
1019         assert(args);
1020
1021         dbus_error_init(&error);
1022
1023         show_properties = !strstr(args[0], "status");
1024
1025         if (show_properties)
1026                 pager_open_if_enabled();
1027
1028         if (show_properties && n <= 1) {
1029                 /* If not argument is specified inspect the manager
1030                  * itself */
1031
1032                 ret = show_one(args[0], bus, "/org/freedesktop/login1", show_properties, &new_line);
1033                 goto finish;
1034         }
1035
1036         for (i = 1; i < n; i++) {
1037                 const char *path = NULL;
1038
1039                 if (strstr(args[0], "session")) {
1040
1041                         m = dbus_message_new_method_call(
1042                                         "org.freedesktop.login1",
1043                                         "/org/freedesktop/login1",
1044                                         "org.freedesktop.login1.Manager",
1045                                         "GetSession");
1046                         if (!m) {
1047                                 log_error("Could not allocate message.");
1048                                 ret = -ENOMEM;
1049                                 goto finish;
1050                         }
1051
1052                         if (!dbus_message_append_args(m,
1053                                                       DBUS_TYPE_STRING, &args[i],
1054                                                       DBUS_TYPE_INVALID)) {
1055                                 log_error("Could not append arguments to message.");
1056                                 ret = -ENOMEM;
1057                                 goto finish;
1058                         }
1059
1060                 } else if (strstr(args[0], "user")) {
1061                         uint32_t uid;
1062
1063                         if (safe_atou(args[i], &uid) < 0) {
1064                                 struct passwd *pw;
1065
1066                                 pw = getpwnam(args[i]);
1067                                 if (!pw) {
1068                                         log_error("User %s unknown.", args[i]);
1069                                         ret = -ENOENT;
1070                                         goto finish;
1071                                 }
1072
1073                                 uid = pw->pw_uid;
1074                         }
1075
1076                         m = dbus_message_new_method_call(
1077                                         "org.freedesktop.login1",
1078                                         "/org/freedesktop/login1",
1079                                         "org.freedesktop.login1.Manager",
1080                                         "GetUser");
1081                         if (!m) {
1082                                 log_error("Could not allocate message.");
1083                                 ret = -ENOMEM;
1084                                 goto finish;
1085                         }
1086
1087                         if (!dbus_message_append_args(m,
1088                                                       DBUS_TYPE_UINT32, &uid,
1089                                                       DBUS_TYPE_INVALID)) {
1090                                 log_error("Could not append arguments to message.");
1091                                 ret = -ENOMEM;
1092                                 goto finish;
1093                         }
1094                 } else {
1095
1096                         m = dbus_message_new_method_call(
1097                                         "org.freedesktop.login1",
1098                                         "/org/freedesktop/login1",
1099                                         "org.freedesktop.login1.Manager",
1100                                         "GetSeat");
1101                         if (!m) {
1102                                 log_error("Could not allocate message.");
1103                                 ret = -ENOMEM;
1104                                 goto finish;
1105                         }
1106
1107                         if (!dbus_message_append_args(m,
1108                                                       DBUS_TYPE_STRING, &args[i],
1109                                                       DBUS_TYPE_INVALID)) {
1110                                 log_error("Could not append arguments to message.");
1111                                 ret = -ENOMEM;
1112                                 goto finish;
1113                         }
1114                 }
1115
1116                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1117                 if (!reply) {
1118                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1119                         ret = -EIO;
1120                         goto finish;
1121                 }
1122
1123                 if (!dbus_message_get_args(reply, &error,
1124                                            DBUS_TYPE_OBJECT_PATH, &path,
1125                                            DBUS_TYPE_INVALID)) {
1126                         log_error("Failed to parse reply: %s", bus_error_message(&error));
1127                         ret = -EIO;
1128                         goto finish;
1129                 }
1130
1131                 r = show_one(args[0], bus, path, show_properties, &new_line);
1132                 if (r != 0)
1133                         ret = r;
1134
1135                 dbus_message_unref(m);
1136                 dbus_message_unref(reply);
1137                 m = reply = NULL;
1138         }
1139
1140 finish:
1141         if (m)
1142                 dbus_message_unref(m);
1143
1144         if (reply)
1145                 dbus_message_unref(reply);
1146
1147         dbus_error_free(&error);
1148
1149         return ret;
1150 }
1151
1152 static int activate(DBusConnection *bus, char **args, unsigned n) {
1153         DBusMessage *m = NULL, *reply = NULL;
1154         int ret = 0;
1155         DBusError error;
1156         unsigned i;
1157
1158         assert(bus);
1159         assert(args);
1160
1161         dbus_error_init(&error);
1162
1163         for (i = 1; i < n; i++) {
1164                 m = dbus_message_new_method_call(
1165                                 "org.freedesktop.login1",
1166                                 "/org/freedesktop/login1",
1167                                 "org.freedesktop.login1.Manager",
1168                                 streq(args[0], "lock-session")      ? "LockSession" :
1169                                 streq(args[0], "unlock-session")    ? "UnlockSession" :
1170                                 streq(args[0], "terminate-session") ? "TerminateSession" :
1171                                                                       "ActivateSession");
1172                 if (!m) {
1173                         log_error("Could not allocate message.");
1174                         ret = -ENOMEM;
1175                         goto finish;
1176                 }
1177
1178                 if (!dbus_message_append_args(m,
1179                                               DBUS_TYPE_STRING, &args[i],
1180                                               DBUS_TYPE_INVALID)) {
1181                         log_error("Could not append arguments to message.");
1182                         ret = -ENOMEM;
1183                         goto finish;
1184                 }
1185
1186                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1187                 if (!reply) {
1188                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1189                         ret = -EIO;
1190                         goto finish;
1191                 }
1192
1193                 dbus_message_unref(m);
1194                 dbus_message_unref(reply);
1195                 m = reply = NULL;
1196         }
1197
1198 finish:
1199         if (m)
1200                 dbus_message_unref(m);
1201
1202         if (reply)
1203                 dbus_message_unref(reply);
1204
1205         dbus_error_free(&error);
1206
1207         return ret;
1208 }
1209
1210 static int kill_session(DBusConnection *bus, char **args, unsigned n) {
1211         DBusMessage *m = NULL, *reply = NULL;
1212         int ret = 0;
1213         DBusError error;
1214         unsigned i;
1215
1216         assert(bus);
1217         assert(args);
1218
1219         dbus_error_init(&error);
1220
1221         if (!arg_kill_who)
1222                 arg_kill_who = "all";
1223
1224         for (i = 1; i < n; i++) {
1225                 m = dbus_message_new_method_call(
1226                                 "org.freedesktop.login1",
1227                                 "/org/freedesktop/login1",
1228                                 "org.freedesktop.login1.Manager",
1229                                 "KillSession");
1230                 if (!m) {
1231                         log_error("Could not allocate message.");
1232                         ret = -ENOMEM;
1233                         goto finish;
1234                 }
1235
1236                 if (!dbus_message_append_args(m,
1237                                               DBUS_TYPE_STRING, &args[i],
1238                                               DBUS_TYPE_STRING, &arg_kill_who,
1239                                               DBUS_TYPE_INT32, arg_signal,
1240                                               DBUS_TYPE_INVALID)) {
1241                         log_error("Could not append arguments to message.");
1242                         ret = -ENOMEM;
1243                         goto finish;
1244                 }
1245
1246                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1247                 if (!reply) {
1248                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1249                         ret = -EIO;
1250                         goto finish;
1251                 }
1252
1253                 dbus_message_unref(m);
1254                 dbus_message_unref(reply);
1255                 m = reply = NULL;
1256         }
1257
1258 finish:
1259         if (m)
1260                 dbus_message_unref(m);
1261
1262         if (reply)
1263                 dbus_message_unref(reply);
1264
1265         dbus_error_free(&error);
1266
1267         return ret;
1268 }
1269
1270 static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
1271         DBusMessage *m = NULL, *reply = NULL;
1272         int ret = 0;
1273         DBusError error;
1274         unsigned i;
1275         dbus_bool_t b, interactive = true;
1276
1277         assert(bus);
1278         assert(args);
1279
1280         dbus_error_init(&error);
1281
1282         b = streq(args[0], "enable-linger");
1283
1284         for (i = 1; i < n; i++) {
1285                 uint32_t uid;
1286
1287                 m = dbus_message_new_method_call(
1288                                 "org.freedesktop.login1",
1289                                 "/org/freedesktop/login1",
1290                                 "org.freedesktop.login1.Manager",
1291                                 "SetUserLinger");
1292                 if (!m) {
1293                         log_error("Could not allocate message.");
1294                         ret = -ENOMEM;
1295                         goto finish;
1296                 }
1297
1298                 if (safe_atou32(args[i], &uid) < 0) {
1299                         struct passwd *pw;
1300
1301                         errno = 0;
1302                         pw = getpwnam(args[i]);
1303                         if (!pw) {
1304                                 ret = errno ? -errno : -ENOENT;
1305                                 log_error("Failed to resolve user %s: %s", args[i], strerror(-ret));
1306                                 goto finish;
1307                         }
1308
1309                         uid = pw->pw_uid;
1310                 }
1311
1312                 if (!dbus_message_append_args(m,
1313                                               DBUS_TYPE_UINT32, &uid,
1314                                               DBUS_TYPE_BOOLEAN, &b,
1315                                               DBUS_TYPE_BOOLEAN, &interactive,
1316                                               DBUS_TYPE_INVALID)) {
1317                         log_error("Could not append arguments to message.");
1318                         ret = -ENOMEM;
1319                         goto finish;
1320                 }
1321
1322                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1323                 if (!reply) {
1324                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1325                         ret = -EIO;
1326                         goto finish;
1327                 }
1328
1329                 dbus_message_unref(m);
1330                 dbus_message_unref(reply);
1331                 m = reply = NULL;
1332         }
1333
1334 finish:
1335         if (m)
1336                 dbus_message_unref(m);
1337
1338         if (reply)
1339                 dbus_message_unref(reply);
1340
1341         dbus_error_free(&error);
1342
1343         return ret;
1344 }
1345
1346 static int terminate_user(DBusConnection *bus, char **args, unsigned n) {
1347         DBusMessage *m = NULL, *reply = NULL;
1348         int ret = 0;
1349         DBusError error;
1350         unsigned i;
1351
1352         assert(bus);
1353         assert(args);
1354
1355         dbus_error_init(&error);
1356
1357         for (i = 1; i < n; i++) {
1358                 uint32_t u;
1359
1360                 m = dbus_message_new_method_call(
1361                                 "org.freedesktop.login1",
1362                                 "/org/freedesktop/login1",
1363                                 "org.freedesktop.login1.Manager",
1364                                 "TerminateUser");
1365                 if (!m) {
1366                         log_error("Could not allocate message.");
1367                         ret = -ENOMEM;
1368                         goto finish;
1369                 }
1370
1371                 if (safe_atou32(args[i], &u) < 0) {
1372                         struct passwd *pw;
1373
1374                         errno = 0;
1375                         pw = getpwnam(args[i]);
1376                         if (!pw) {
1377                                 ret = errno ? -errno : -ENOENT;
1378                                 log_error("Failed to look up user %s: %s", args[i], strerror(-ret));
1379                                 goto finish;
1380                         }
1381
1382                         u = pw->pw_uid;
1383                 }
1384
1385                 if (!dbus_message_append_args(m,
1386                                               DBUS_TYPE_UINT32, &u,
1387                                               DBUS_TYPE_INVALID)) {
1388                         log_error("Could not append arguments to message.");
1389                         ret = -ENOMEM;
1390                         goto finish;
1391                 }
1392
1393                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1394                 if (!reply) {
1395                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1396                         ret = -EIO;
1397                         goto finish;
1398                 }
1399
1400                 dbus_message_unref(m);
1401                 dbus_message_unref(reply);
1402                 m = reply = NULL;
1403         }
1404
1405 finish:
1406         if (m)
1407                 dbus_message_unref(m);
1408
1409         if (reply)
1410                 dbus_message_unref(reply);
1411
1412         dbus_error_free(&error);
1413
1414         return ret;
1415 }
1416
1417 static int kill_user(DBusConnection *bus, char **args, unsigned n) {
1418         DBusMessage *m = NULL, *reply = NULL;
1419         int ret = 0;
1420         DBusError error;
1421         unsigned i;
1422
1423         assert(bus);
1424         assert(args);
1425
1426         dbus_error_init(&error);
1427
1428         if (!arg_kill_who)
1429                 arg_kill_who = "all";
1430
1431         for (i = 1; i < n; i++) {
1432                 uint32_t u;
1433
1434                 m = dbus_message_new_method_call(
1435                                 "org.freedesktop.login1",
1436                                 "/org/freedesktop/login1",
1437                                 "org.freedesktop.login1.Manager",
1438                                 "KillUser");
1439                 if (!m) {
1440                         log_error("Could not allocate message.");
1441                         ret = -ENOMEM;
1442                         goto finish;
1443                 }
1444
1445                 if (safe_atou32(args[i], &u) < 0) {
1446                         struct passwd *pw;
1447
1448                         errno = 0;
1449                         pw = getpwnam(args[i]);
1450                         if (!pw) {
1451                                 ret = errno ? -errno : -ENOENT;
1452                                 log_error("Failed to look up user %s: %s", args[i], strerror(-ret));
1453                                 goto finish;
1454                         }
1455
1456                         u = pw->pw_uid;
1457                 }
1458
1459                 if (!dbus_message_append_args(m,
1460                                               DBUS_TYPE_UINT32, &u,
1461                                               DBUS_TYPE_INT32, arg_signal,
1462                                               DBUS_TYPE_INVALID)) {
1463                         log_error("Could not append arguments to message.");
1464                         ret = -ENOMEM;
1465                         goto finish;
1466                 }
1467
1468                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1469                 if (!reply) {
1470                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1471                         ret = -EIO;
1472                         goto finish;
1473                 }
1474
1475                 dbus_message_unref(m);
1476                 dbus_message_unref(reply);
1477                 m = reply = NULL;
1478         }
1479
1480 finish:
1481         if (m)
1482                 dbus_message_unref(m);
1483
1484         if (reply)
1485                 dbus_message_unref(reply);
1486
1487         dbus_error_free(&error);
1488
1489         return ret;
1490 }
1491
1492 static int attach(DBusConnection *bus, char **args, unsigned n) {
1493         DBusMessage *m = NULL, *reply = NULL;
1494         int ret = 0;
1495         DBusError error;
1496         unsigned i;
1497         dbus_bool_t interactive = true;
1498
1499         assert(bus);
1500         assert(args);
1501
1502         dbus_error_init(&error);
1503
1504         for (i = 2; i < n; i++) {
1505                 m = dbus_message_new_method_call(
1506                                 "org.freedesktop.login1",
1507                                 "/org/freedesktop/login1",
1508                                 "org.freedesktop.login1.Manager",
1509                                 "AttachDevice");
1510                 if (!m) {
1511                         log_error("Could not allocate message.");
1512                         ret = -ENOMEM;
1513                         goto finish;
1514                 }
1515
1516                 if (!dbus_message_append_args(m,
1517                                               DBUS_TYPE_STRING, &args[1],
1518                                               DBUS_TYPE_STRING, &args[i],
1519                                               DBUS_TYPE_BOOLEAN, &interactive,
1520                                               DBUS_TYPE_INVALID)) {
1521                         log_error("Could not append arguments to message.");
1522                         ret = -ENOMEM;
1523                         goto finish;
1524                 }
1525
1526                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1527                 if (!reply) {
1528                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1529                         ret = -EIO;
1530                         goto finish;
1531                 }
1532
1533                 dbus_message_unref(m);
1534                 dbus_message_unref(reply);
1535                 m = reply = NULL;
1536         }
1537
1538 finish:
1539         if (m)
1540                 dbus_message_unref(m);
1541
1542         if (reply)
1543                 dbus_message_unref(reply);
1544
1545         dbus_error_free(&error);
1546
1547         return ret;
1548 }
1549
1550 static int flush_devices(DBusConnection *bus, char **args, unsigned n) {
1551         DBusMessage *m = NULL, *reply = NULL;
1552         int ret = 0;
1553         DBusError error;
1554         dbus_bool_t interactive = true;
1555
1556         assert(bus);
1557         assert(args);
1558
1559         dbus_error_init(&error);
1560
1561         m = dbus_message_new_method_call(
1562                         "org.freedesktop.login1",
1563                         "/org/freedesktop/login1",
1564                         "org.freedesktop.login1.Manager",
1565                         "FlushDevices");
1566         if (!m) {
1567                 log_error("Could not allocate message.");
1568                 ret = -ENOMEM;
1569                 goto finish;
1570         }
1571
1572         if (!dbus_message_append_args(m,
1573                                       DBUS_TYPE_BOOLEAN, &interactive,
1574                                       DBUS_TYPE_INVALID)) {
1575                 log_error("Could not append arguments to message.");
1576                 ret = -ENOMEM;
1577                 goto finish;
1578         }
1579
1580         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1581         if (!reply) {
1582                 log_error("Failed to issue method call: %s", bus_error_message(&error));
1583                 ret = -EIO;
1584                 goto finish;
1585         }
1586
1587 finish:
1588         if (m)
1589                 dbus_message_unref(m);
1590
1591         if (reply)
1592                 dbus_message_unref(reply);
1593
1594         dbus_error_free(&error);
1595
1596         return ret;
1597 }
1598
1599 static int terminate_seat(DBusConnection *bus, char **args, unsigned n) {
1600         DBusMessage *m = NULL, *reply = NULL;
1601         int ret = 0;
1602         DBusError error;
1603         unsigned i;
1604
1605         assert(bus);
1606         assert(args);
1607
1608         dbus_error_init(&error);
1609
1610         for (i = 1; i < n; i++) {
1611                 m = dbus_message_new_method_call(
1612                                 "org.freedesktop.login1",
1613                                 "/org/freedesktop/login1",
1614                                 "org.freedesktop.login1.Manager",
1615                                 "TerminateSeat");
1616                 if (!m) {
1617                         log_error("Could not allocate message.");
1618                         ret = -ENOMEM;
1619                         goto finish;
1620                 }
1621
1622                 if (!dbus_message_append_args(m,
1623                                               DBUS_TYPE_STRING, &args[i],
1624                                               DBUS_TYPE_INVALID)) {
1625                         log_error("Could not append arguments to message.");
1626                         ret = -ENOMEM;
1627                         goto finish;
1628                 }
1629
1630                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1631                 if (!reply) {
1632                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1633                         ret = -EIO;
1634                         goto finish;
1635                 }
1636
1637                 dbus_message_unref(m);
1638                 dbus_message_unref(reply);
1639                 m = reply = NULL;
1640         }
1641
1642 finish:
1643         if (m)
1644                 dbus_message_unref(m);
1645
1646         if (reply)
1647                 dbus_message_unref(reply);
1648
1649         dbus_error_free(&error);
1650
1651         return ret;
1652 }
1653
1654 static int help(void) {
1655
1656         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1657                "Send control commands to or query the login manager.\n\n"
1658                "  -h --help           Show this help\n"
1659                "     --version        Show package version\n"
1660                "  -p --property=NAME  Show only properties by this name\n"
1661                "  -a --all            Show all properties, including empty ones\n"
1662                "     --kill-who=WHO   Who to send signal to\n"
1663                "  -s --signal=SIGNAL  Which signal to send\n"
1664                "  -H --host=[USER@]HOST\n"
1665                "                      Show information for remote host\n"
1666                "  -P --privileged     Acquire privileges before execution\n"
1667                "     --no-pager       Do not pipe output into a pager\n\n"
1668                "Commands:\n"
1669                "  list-sessions                   List sessions\n"
1670                "  session-status [ID...]          Show session status\n"
1671                "  show-session [ID...]            Show properties of one or more sessions\n"
1672                "  activate [ID]                   Activate a session\n"
1673                "  lock-session [ID...]            Screen lock one or more sessions\n"
1674                "  unlock-session [ID...]          Screen unlock one or more sessions\n"
1675                "  terminate-session [ID...]       Terminate one or more sessions\n"
1676                "  kill-session [ID...]            Send signal to processes of a session\n"
1677                "  list-users                      List users\n"
1678                "  user-status [USER...]           Show user status\n"
1679                "  show-user [USER...]             Show properties of one or more users\n"
1680                "  enable-linger [USER...]         Enable linger state of one or more users\n"
1681                "  disable-linger [USER...]        Disable linger state of one or more users\n"
1682                "  terminate-user [USER...]        Terminate all sessions of one or more users\n"
1683                "  kill-user [USER...]             Send signal to processes of a user\n"
1684                "  list-seats                      List seats\n"
1685                "  seat-status [NAME...]           Show seat status\n"
1686                "  show-seat [NAME...]             Show properties of one or more seats\n"
1687                "  attach [NAME] [DEVICE...]       Attach one or more devices to a seat\n"
1688                "  flush-devices                   Flush all device associations\n"
1689                "  terminate-seat [NAME...]        Terminate all sessions on one or more seats\n",
1690                program_invocation_short_name);
1691
1692         return 0;
1693 }
1694
1695 static int parse_argv(int argc, char *argv[]) {
1696
1697         enum {
1698                 ARG_VERSION = 0x100,
1699                 ARG_NO_PAGER,
1700                 ARG_KILL_WHO
1701         };
1702
1703         static const struct option options[] = {
1704                 { "help",      no_argument,       NULL, 'h'           },
1705                 { "version",   no_argument,       NULL, ARG_VERSION   },
1706                 { "property",  required_argument, NULL, 'p'           },
1707                 { "all",       no_argument,       NULL, 'a'           },
1708                 { "no-pager",  no_argument,       NULL, ARG_NO_PAGER  },
1709                 { "kill-who",  required_argument, NULL, ARG_KILL_WHO  },
1710                 { "signal",    required_argument, NULL, 's'           },
1711                 { "host",      required_argument, NULL, 'H'           },
1712                 { "privileged",no_argument,       NULL, 'P'           },
1713                 { NULL,        0,                 NULL, 0             }
1714         };
1715
1716         int c;
1717
1718         assert(argc >= 0);
1719         assert(argv);
1720
1721         while ((c = getopt_long(argc, argv, "hp:as:H:P", options, NULL)) >= 0) {
1722
1723                 switch (c) {
1724
1725                 case 'h':
1726                         help();
1727                         return 0;
1728
1729                 case ARG_VERSION:
1730                         puts(PACKAGE_STRING);
1731                         puts(DISTRIBUTION);
1732                         puts(SYSTEMD_FEATURES);
1733                         return 0;
1734
1735                 case 'p': {
1736                         char **l;
1737
1738                         l = strv_append(arg_property, optarg);
1739                         if (!l)
1740                                 return -ENOMEM;
1741
1742                         strv_free(arg_property);
1743                         arg_property = l;
1744
1745                         /* If the user asked for a particular
1746                          * property, show it to him, even if it is
1747                          * empty. */
1748                         arg_all = true;
1749                         break;
1750                 }
1751
1752                 case 'a':
1753                         arg_all = true;
1754                         break;
1755
1756                 case ARG_NO_PAGER:
1757                         arg_no_pager = true;
1758                         break;
1759
1760                 case ARG_KILL_WHO:
1761                         arg_kill_who = optarg;
1762                         break;
1763
1764                 case 's':
1765                         arg_signal = signal_from_string_try_harder(optarg);
1766                         if (arg_signal < 0) {
1767                                 log_error("Failed to parse signal string %s.", optarg);
1768                                 return -EINVAL;
1769                         }
1770                         break;
1771
1772                 case 'P':
1773                         arg_transport = TRANSPORT_POLKIT;
1774                         break;
1775
1776                 case 'H':
1777                         arg_transport = TRANSPORT_SSH;
1778                         arg_host = optarg;
1779                         break;
1780
1781                 case '?':
1782                         return -EINVAL;
1783
1784                 default:
1785                         log_error("Unknown option code %c", c);
1786                         return -EINVAL;
1787                 }
1788         }
1789
1790         return 1;
1791 }
1792
1793 static int loginctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
1794
1795         static const struct {
1796                 const char* verb;
1797                 const enum {
1798                         MORE,
1799                         LESS,
1800                         EQUAL
1801                 } argc_cmp;
1802                 const int argc;
1803                 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
1804         } verbs[] = {
1805                 { "list-sessions",         LESS,   1, list_sessions    },
1806                 { "session-status",        MORE,   2, show             },
1807                 { "show-session",          MORE,   1, show             },
1808                 { "activate",              EQUAL,  2, activate         },
1809                 { "lock-session",          MORE,   2, activate         },
1810                 { "unlock-session",        MORE,   2, activate         },
1811                 { "terminate-session",     MORE,   2, activate         },
1812                 { "kill-session",          MORE,   2, kill_session     },
1813                 { "list-users",            EQUAL,  1, list_users       },
1814                 { "user-status",           MORE,   2, show             },
1815                 { "show-user",             MORE,   1, show             },
1816                 { "enable-linger",         MORE,   2, enable_linger    },
1817                 { "disable-linger",        MORE,   2, enable_linger    },
1818                 { "terminate-user",        MORE,   2, terminate_user   },
1819                 { "kill-user",             MORE,   2, kill_user        },
1820                 { "list-seats",            EQUAL,  1, list_seats       },
1821                 { "seat-status",           MORE,   2, show             },
1822                 { "show-seat",             MORE,   1, show             },
1823                 { "attach",                MORE,   3, attach           },
1824                 { "flush-devices",         EQUAL,  1, flush_devices    },
1825                 { "terminate-seat",        MORE,   2, terminate_seat   },
1826         };
1827
1828         int left;
1829         unsigned i;
1830
1831         assert(argc >= 0);
1832         assert(argv);
1833         assert(error);
1834
1835         left = argc - optind;
1836
1837         if (left <= 0)
1838                 /* Special rule: no arguments means "list-sessions" */
1839                 i = 0;
1840         else {
1841                 if (streq(argv[optind], "help")) {
1842                         help();
1843                         return 0;
1844                 }
1845
1846                 for (i = 0; i < ELEMENTSOF(verbs); i++)
1847                         if (streq(argv[optind], verbs[i].verb))
1848                                 break;
1849
1850                 if (i >= ELEMENTSOF(verbs)) {
1851                         log_error("Unknown operation %s", argv[optind]);
1852                         return -EINVAL;
1853                 }
1854         }
1855
1856         switch (verbs[i].argc_cmp) {
1857
1858         case EQUAL:
1859                 if (left != verbs[i].argc) {
1860                         log_error("Invalid number of arguments.");
1861                         return -EINVAL;
1862                 }
1863
1864                 break;
1865
1866         case MORE:
1867                 if (left < verbs[i].argc) {
1868                         log_error("Too few arguments.");
1869                         return -EINVAL;
1870                 }
1871
1872                 break;
1873
1874         case LESS:
1875                 if (left > verbs[i].argc) {
1876                         log_error("Too many arguments.");
1877                         return -EINVAL;
1878                 }
1879
1880                 break;
1881
1882         default:
1883                 assert_not_reached("Unknown comparison operator.");
1884         }
1885
1886         if (!bus) {
1887                 log_error("Failed to get D-Bus connection: %s", error->message);
1888                 return -EIO;
1889         }
1890
1891         return verbs[i].dispatch(bus, argv + optind, left);
1892 }
1893
1894 int main(int argc, char*argv[]) {
1895         int r, retval = EXIT_FAILURE;
1896         DBusConnection *bus = NULL;
1897         DBusError error;
1898
1899         dbus_error_init(&error);
1900
1901         log_parse_environment();
1902         log_open();
1903
1904         r = parse_argv(argc, argv);
1905         if (r < 0)
1906                 goto finish;
1907         else if (r == 0) {
1908                 retval = EXIT_SUCCESS;
1909                 goto finish;
1910         }
1911
1912         if (arg_transport == TRANSPORT_NORMAL)
1913                 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
1914         else if (arg_transport == TRANSPORT_POLKIT)
1915                 bus_connect_system_polkit(&bus, &error);
1916         else if (arg_transport == TRANSPORT_SSH)
1917                 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
1918         else
1919                 assert_not_reached("Uh, invalid transport...");
1920
1921         r = loginctl_main(bus, argc, argv, &error);
1922         retval = r < 0 ? EXIT_FAILURE : r;
1923
1924 finish:
1925         if (bus) {
1926                 dbus_connection_flush(bus);
1927                 dbus_connection_close(bus);
1928                 dbus_connection_unref(bus);
1929         }
1930
1931         dbus_error_free(&error);
1932         dbus_shutdown();
1933
1934         strv_free(arg_property);
1935
1936         pager_close();
1937
1938         return retval;
1939 }