chiark / gitweb /
systemctl: implement auto-pager a la git
[elogind.git] / src / systemctl.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 <sys/reboot.h>
23 #include <stdio.h>
24 #include <getopt.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/ioctl.h>
29 #include <termios.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <stddef.h>
35 #include <sys/prctl.h>
36
37 #include <dbus/dbus.h>
38
39 #include "log.h"
40 #include "util.h"
41 #include "macro.h"
42 #include "set.h"
43 #include "utmp-wtmp.h"
44 #include "special.h"
45 #include "initreq.h"
46 #include "strv.h"
47 #include "dbus-common.h"
48 #include "cgroup-show.h"
49 #include "cgroup-util.h"
50 #include "list.h"
51 #include "path-lookup.h"
52 #include "conf-parser.h"
53 #include "sd-daemon.h"
54 #include "shutdownd.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
57 #include "build.h"
58 #include "unit-name.h"
59
60 static const char *arg_type = NULL;
61 static char **arg_property = NULL;
62 static bool arg_all = false;
63 static bool arg_fail = false;
64 static bool arg_user = false;
65 static bool arg_global = false;
66 static bool arg_immediate = false;
67 static bool arg_no_block = false;
68 static bool arg_no_pager = false;
69 static bool arg_no_wtmp = false;
70 static bool arg_no_sync = false;
71 static bool arg_no_wall = false;
72 static bool arg_no_reload = false;
73 static bool arg_dry = false;
74 static bool arg_quiet = false;
75 static bool arg_full = false;
76 static bool arg_force = false;
77 static bool arg_defaults = false;
78 static bool arg_ask_password = false;
79 static char **arg_wall = NULL;
80 static const char *arg_kill_who = NULL;
81 static const char *arg_kill_mode = NULL;
82 static int arg_signal = SIGTERM;
83 static usec_t arg_when = 0;
84 static enum action {
85         ACTION_INVALID,
86         ACTION_SYSTEMCTL,
87         ACTION_HALT,
88         ACTION_POWEROFF,
89         ACTION_REBOOT,
90         ACTION_KEXEC,
91         ACTION_EXIT,
92         ACTION_RUNLEVEL2,
93         ACTION_RUNLEVEL3,
94         ACTION_RUNLEVEL4,
95         ACTION_RUNLEVEL5,
96         ACTION_RESCUE,
97         ACTION_EMERGENCY,
98         ACTION_DEFAULT,
99         ACTION_RELOAD,
100         ACTION_REEXEC,
101         ACTION_RUNLEVEL,
102         ACTION_CANCEL_SHUTDOWN,
103         _ACTION_MAX
104 } arg_action = ACTION_SYSTEMCTL;
105 static enum dot {
106         DOT_ALL,
107         DOT_ORDER,
108         DOT_REQUIRE
109 } arg_dot = DOT_ALL;
110
111 static bool private_bus = false;
112
113 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
114
115 static bool on_tty(void) {
116         static int t = -1;
117
118         if (_unlikely_(t < 0))
119                 t = isatty(STDOUT_FILENO) > 0;
120
121         return t;
122 }
123
124 static void spawn_ask_password_agent(void) {
125         pid_t parent, child;
126
127         /* We check STDIN here, not STDOUT, since this is about input,
128          * not output */
129         if (!isatty(STDIN_FILENO))
130                 return;
131
132         if (!arg_ask_password)
133                 return;
134
135         parent = getpid();
136
137         /* Spawns a temporary TTY agent, making sure it goes away when
138          * we go away */
139
140         if ((child = fork()) < 0)
141                 return;
142
143         if (child == 0) {
144                 /* In the child */
145
146                 const char * const args[] = {
147                         SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
148                         "--watch",
149                         NULL
150                 };
151
152                 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
153                         _exit(EXIT_FAILURE);
154
155                 /* Check whether our parent died before we were able
156                  * to set the death signal */
157                 if (getppid() != parent)
158                         _exit(EXIT_SUCCESS);
159
160                 execv(args[0], (char **) args);
161                 _exit(EXIT_FAILURE);
162         }
163 }
164
165 static const char *ansi_highlight(bool b) {
166
167         if (!on_tty())
168                 return "";
169
170         return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
171 }
172
173 static const char *ansi_highlight_green(bool b) {
174
175         if (!on_tty())
176                 return "";
177
178         return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
179 }
180
181 static bool error_is_no_service(const DBusError *error) {
182         assert(error);
183
184         if (!dbus_error_is_set(error))
185                 return false;
186
187         if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
188                 return true;
189
190         if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
191                 return true;
192
193         return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
194 }
195
196 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
197         assert(error);
198
199         if (!dbus_error_is_set(error))
200                 return r;
201
202         if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
203             dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
204             dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
205             dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
206                 return EXIT_NOPERMISSION;
207
208         if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
209                 return EXIT_NOTINSTALLED;
210
211         if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
212             dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
213                 return EXIT_NOTIMPLEMENTED;
214
215         if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
216                 return EXIT_NOTCONFIGURED;
217
218         if (r != 0)
219                 return r;
220
221         return EXIT_FAILURE;
222 }
223
224 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
225
226         assert(iter);
227         assert(data);
228
229         if (dbus_message_iter_get_arg_type(iter) != type)
230                 return -EIO;
231
232         dbus_message_iter_get_basic(iter, data);
233
234         if (!dbus_message_iter_next(iter) != !next)
235                 return -EIO;
236
237         return 0;
238 }
239
240 static void warn_wall(enum action action) {
241         static const char *table[_ACTION_MAX] = {
242                 [ACTION_HALT]      = "The system is going down for system halt NOW!",
243                 [ACTION_REBOOT]    = "The system is going down for reboot NOW!",
244                 [ACTION_POWEROFF]  = "The system is going down for power-off NOW!",
245                 [ACTION_KEXEC]     = "The system is going down for kexec reboot NOW!",
246                 [ACTION_RESCUE]    = "The system is going down to rescue mode NOW!",
247                 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
248         };
249
250         if (arg_no_wall)
251                 return;
252
253         if (arg_wall) {
254                 char *p;
255
256                 if (!(p = strv_join(arg_wall, " "))) {
257                         log_error("Failed to join strings.");
258                         return;
259                 }
260
261                 if (*p) {
262                         utmp_wall(p, NULL);
263                         free(p);
264                         return;
265                 }
266
267                 free(p);
268         }
269
270         if (!table[action])
271                 return;
272
273         utmp_wall(table[action], NULL);
274 }
275
276 struct unit_info {
277         const char *id;
278         const char *description;
279         const char *load_state;
280         const char *active_state;
281         const char *sub_state;
282         const char *following;
283         const char *unit_path;
284         uint32_t job_id;
285         const char *job_type;
286         const char *job_path;
287 };
288
289 static int compare_unit_info(const void *a, const void *b) {
290         const char *d1, *d2;
291         const struct unit_info *u = a, *v = b;
292
293         d1 = strrchr(u->id, '.');
294         d2 = strrchr(v->id, '.');
295
296         if (d1 && d2) {
297                 int r;
298
299                 if ((r = strcasecmp(d1, d2)) != 0)
300                         return r;
301         }
302
303         return strcasecmp(u->id, v->id);
304 }
305
306 static bool output_show_job(const struct unit_info *u) {
307         const char *dot;
308
309         return (!arg_type || ((dot = strrchr(u->id, '.')) &&
310                               streq(dot+1, arg_type))) &&
311                 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
312 }
313
314 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
315         unsigned active_len, sub_len, job_len, n_shown = 0;
316         const struct unit_info *u;
317
318         active_len = sizeof("ACTIVE")-1;
319         sub_len = sizeof("SUB")-1;
320         job_len = sizeof("JOB")-1;
321
322         for (u = unit_infos; u < unit_infos + c; u++) {
323                 if (!output_show_job(u))
324                         continue;
325
326                 active_len = MAX(active_len, strlen(u->active_state));
327                 sub_len = MAX(sub_len, strlen(u->sub_state));
328                 if (u->job_id != 0)
329                         job_len = MAX(job_len, strlen(u->job_type));
330         }
331
332         if (on_tty()) {
333                 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
334                        active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
335                 if (columns() >= 80+12 || arg_full)
336                         printf(" %s\n", "DESCRIPTION");
337                 else
338                         printf("\n");
339         }
340
341         for (u = unit_infos; u < unit_infos + c; u++) {
342                 char *e;
343                 int a = 0, b = 0;
344                 const char *on_loaded, *off_loaded;
345                 const char *on_active, *off_active;
346
347                 if (!output_show_job(u))
348                         continue;
349
350                 n_shown++;
351
352                 if (!streq(u->load_state, "loaded") &&
353                     !streq(u->load_state, "banned")) {
354                         on_loaded = ansi_highlight(true);
355                         off_loaded = ansi_highlight(false);
356                 } else
357                         on_loaded = off_loaded = "";
358
359                 if (streq(u->active_state, "failed")) {
360                         on_active = ansi_highlight(true);
361                         off_active = ansi_highlight(false);
362                 } else
363                         on_active = off_active = "";
364
365                 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
366
367                 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
368                        e ? e : u->id,
369                        on_loaded, u->load_state, off_loaded,
370                        on_active, active_len, u->active_state,
371                        sub_len, u->sub_state, off_active,
372                        &a);
373
374                 free(e);
375
376                 a -= strlen(on_loaded) + strlen(off_loaded);
377                 a -= strlen(on_active) + strlen(off_active);
378
379                 if (u->job_id != 0)
380                         printf(" %-*s", job_len, u->job_type);
381                 else
382                         b = 1 + job_len;
383
384                 if (a + b + 1 < columns()) {
385                         if (u->job_id == 0)
386                                 printf(" %-*s", job_len, "");
387
388                         if (arg_full)
389                                 printf(" %s", u->description);
390                         else
391                                 printf(" %.*s", columns() - a - b - 1, u->description);
392                 }
393
394                 fputs("\n", stdout);
395         }
396
397         if (on_tty()) {
398                 printf("\nLOAD   = Reflects whether the unit definition was properly loaded.\n"
399                        "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
400                        "SUB    = The low-level unit activation state, values depend on unit type.\n"
401                        "JOB    = Pending job for the unit.\n");
402
403                 if (arg_all)
404                         printf("\n%u units listed.\n", n_shown);
405                 else
406                         printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
407         }
408 }
409
410 static int list_units(DBusConnection *bus, char **args, unsigned n) {
411         DBusMessage *m = NULL, *reply = NULL;
412         DBusError error;
413         int r;
414         DBusMessageIter iter, sub, sub2;
415         unsigned c = 0, n_units = 0;
416         struct unit_info *unit_infos = NULL;
417
418         dbus_error_init(&error);
419
420         assert(bus);
421
422         if (!(m = dbus_message_new_method_call(
423                               "org.freedesktop.systemd1",
424                               "/org/freedesktop/systemd1",
425                               "org.freedesktop.systemd1.Manager",
426                               "ListUnits"))) {
427                 log_error("Could not allocate message.");
428                 return -ENOMEM;
429         }
430
431         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
432                 log_error("Failed to issue method call: %s", bus_error_message(&error));
433                 r = -EIO;
434                 goto finish;
435         }
436
437         if (!dbus_message_iter_init(reply, &iter) ||
438             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
439             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
440                 log_error("Failed to parse reply.");
441                 r = -EIO;
442                 goto finish;
443         }
444
445         dbus_message_iter_recurse(&iter, &sub);
446
447         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
448                 struct unit_info *u;
449
450                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
451                         log_error("Failed to parse reply.");
452                         r = -EIO;
453                         goto finish;
454                 }
455
456                 if (c >= n_units) {
457                         struct unit_info *w;
458
459                         n_units = MAX(2*c, 16);
460                         w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
461
462                         if (!w) {
463                                 log_error("Failed to allocate unit array.");
464                                 r = -ENOMEM;
465                                 goto finish;
466                         }
467
468                         unit_infos = w;
469                 }
470
471                 u = unit_infos+c;
472
473                 dbus_message_iter_recurse(&sub, &sub2);
474
475                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
476                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
477                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
478                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
479                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
480                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
481                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
482                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
483                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
484                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
485                         log_error("Failed to parse reply.");
486                         r = -EIO;
487                         goto finish;
488                 }
489
490                 dbus_message_iter_next(&sub);
491                 c++;
492         }
493
494         qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
495         output_units_list(unit_infos, c);
496
497         r = 0;
498
499 finish:
500         if (m)
501                 dbus_message_unref(m);
502
503         if (reply)
504                 dbus_message_unref(reply);
505
506         free(unit_infos);
507
508         dbus_error_free(&error);
509
510         return r;
511 }
512
513 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
514         static const char * const colors[] = {
515                 "Requires",              "[color=\"black\"]",
516                 "RequiresOverridable",   "[color=\"black\"]",
517                 "Requisite",             "[color=\"darkblue\"]",
518                 "RequisiteOverridable",  "[color=\"darkblue\"]",
519                 "Wants",                 "[color=\"darkgrey\"]",
520                 "Conflicts",             "[color=\"red\"]",
521                 "ConflictedBy",          "[color=\"red\"]",
522                 "After",                 "[color=\"green\"]"
523         };
524
525         const char *c = NULL;
526         unsigned i;
527
528         assert(name);
529         assert(prop);
530         assert(iter);
531
532         for (i = 0; i < ELEMENTSOF(colors); i += 2)
533                 if (streq(colors[i], prop)) {
534                         c = colors[i+1];
535                         break;
536                 }
537
538         if (!c)
539                 return 0;
540
541         if (arg_dot != DOT_ALL)
542                 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
543                         return 0;
544
545         switch (dbus_message_iter_get_arg_type(iter)) {
546
547         case DBUS_TYPE_ARRAY:
548
549                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
550                         DBusMessageIter sub;
551
552                         dbus_message_iter_recurse(iter, &sub);
553
554                         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
555                                 const char *s;
556
557                                 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
558                                 dbus_message_iter_get_basic(&sub, &s);
559                                 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
560
561                                 dbus_message_iter_next(&sub);
562                         }
563
564                         return 0;
565                 }
566         }
567
568         return 0;
569 }
570
571 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
572         DBusMessage *m = NULL, *reply = NULL;
573         const char *interface = "org.freedesktop.systemd1.Unit";
574         int r;
575         DBusError error;
576         DBusMessageIter iter, sub, sub2, sub3;
577
578         assert(bus);
579         assert(path);
580
581         dbus_error_init(&error);
582
583         if (!(m = dbus_message_new_method_call(
584                               "org.freedesktop.systemd1",
585                               path,
586                               "org.freedesktop.DBus.Properties",
587                               "GetAll"))) {
588                 log_error("Could not allocate message.");
589                 r = -ENOMEM;
590                 goto finish;
591         }
592
593         if (!dbus_message_append_args(m,
594                                       DBUS_TYPE_STRING, &interface,
595                                       DBUS_TYPE_INVALID)) {
596                 log_error("Could not append arguments to message.");
597                 r = -ENOMEM;
598                 goto finish;
599         }
600
601         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
602                 log_error("Failed to issue method call: %s", bus_error_message(&error));
603                 r = -EIO;
604                 goto finish;
605         }
606
607         if (!dbus_message_iter_init(reply, &iter) ||
608             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
609             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY)  {
610                 log_error("Failed to parse reply.");
611                 r = -EIO;
612                 goto finish;
613         }
614
615         dbus_message_iter_recurse(&iter, &sub);
616
617         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
618                 const char *prop;
619
620                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
621                         log_error("Failed to parse reply.");
622                         r = -EIO;
623                         goto finish;
624                 }
625
626                 dbus_message_iter_recurse(&sub, &sub2);
627
628                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
629                         log_error("Failed to parse reply.");
630                         r = -EIO;
631                         goto finish;
632                 }
633
634                 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT)  {
635                         log_error("Failed to parse reply.");
636                         r = -EIO;
637                         goto finish;
638                 }
639
640                 dbus_message_iter_recurse(&sub2, &sub3);
641
642                 if (dot_one_property(name, prop, &sub3)) {
643                         log_error("Failed to parse reply.");
644                         r = -EIO;
645                         goto finish;
646                 }
647
648                 dbus_message_iter_next(&sub);
649         }
650
651         r = 0;
652
653 finish:
654         if (m)
655                 dbus_message_unref(m);
656
657         if (reply)
658                 dbus_message_unref(reply);
659
660         dbus_error_free(&error);
661
662         return r;
663 }
664
665 static int dot(DBusConnection *bus, char **args, unsigned n) {
666         DBusMessage *m = NULL, *reply = NULL;
667         DBusError error;
668         int r;
669         DBusMessageIter iter, sub, sub2;
670
671         dbus_error_init(&error);
672
673         assert(bus);
674
675         if (!(m = dbus_message_new_method_call(
676                               "org.freedesktop.systemd1",
677                               "/org/freedesktop/systemd1",
678                               "org.freedesktop.systemd1.Manager",
679                               "ListUnits"))) {
680                 log_error("Could not allocate message.");
681                 return -ENOMEM;
682         }
683
684         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
685                 log_error("Failed to issue method call: %s", bus_error_message(&error));
686                 r = -EIO;
687                 goto finish;
688         }
689
690         if (!dbus_message_iter_init(reply, &iter) ||
691             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
692             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
693                 log_error("Failed to parse reply.");
694                 r = -EIO;
695                 goto finish;
696         }
697
698         printf("digraph systemd {\n");
699
700         dbus_message_iter_recurse(&iter, &sub);
701         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
702                 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
703
704                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
705                         log_error("Failed to parse reply.");
706                         r = -EIO;
707                         goto finish;
708                 }
709
710                 dbus_message_iter_recurse(&sub, &sub2);
711
712                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
713                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
714                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
715                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
716                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
717                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
718                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
719                         log_error("Failed to parse reply.");
720                         r = -EIO;
721                         goto finish;
722                 }
723
724                 if ((r = dot_one(bus, id, unit_path)) < 0)
725                         goto finish;
726
727                 /* printf("\t\"%s\";\n", id); */
728                 dbus_message_iter_next(&sub);
729         }
730
731         printf("}\n");
732
733         log_info("   Color legend: black     = Requires\n"
734                  "                 dark blue = Requisite\n"
735                  "                 dark grey = Wants\n"
736                  "                 red       = Conflicts\n"
737                  "                 green     = After\n");
738
739         if (isatty(fileno(stdout)))
740                 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
741                            "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
742
743         r = 0;
744
745 finish:
746         if (m)
747                 dbus_message_unref(m);
748
749         if (reply)
750                 dbus_message_unref(reply);
751
752         dbus_error_free(&error);
753
754         return r;
755 }
756
757 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
758         DBusMessage *m = NULL, *reply = NULL;
759         DBusError error;
760         int r;
761         DBusMessageIter iter, sub, sub2;
762         unsigned k = 0;
763
764         dbus_error_init(&error);
765
766         assert(bus);
767
768         if (!(m = dbus_message_new_method_call(
769                               "org.freedesktop.systemd1",
770                               "/org/freedesktop/systemd1",
771                               "org.freedesktop.systemd1.Manager",
772                               "ListJobs"))) {
773                 log_error("Could not allocate message.");
774                 return -ENOMEM;
775         }
776
777         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
778                 log_error("Failed to issue method call: %s", bus_error_message(&error));
779                 r = -EIO;
780                 goto finish;
781         }
782
783         if (!dbus_message_iter_init(reply, &iter) ||
784             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
785             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
786                 log_error("Failed to parse reply.");
787                 r = -EIO;
788                 goto finish;
789         }
790
791         dbus_message_iter_recurse(&iter, &sub);
792
793         if (isatty(STDOUT_FILENO))
794                 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
795
796         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
797                 const char *name, *type, *state, *job_path, *unit_path;
798                 uint32_t id;
799                 char *e;
800
801                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
802                         log_error("Failed to parse reply.");
803                         r = -EIO;
804                         goto finish;
805                 }
806
807                 dbus_message_iter_recurse(&sub, &sub2);
808
809                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
810                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
811                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
812                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
813                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
814                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
815                         log_error("Failed to parse reply.");
816                         r = -EIO;
817                         goto finish;
818                 }
819
820                 e = arg_full ? NULL : ellipsize(name, 25, 33);
821                 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
822                 free(e);
823
824                 k++;
825
826                 dbus_message_iter_next(&sub);
827         }
828
829         if (isatty(STDOUT_FILENO))
830                 printf("\n%u jobs listed.\n", k);
831
832         r = 0;
833
834 finish:
835         if (m)
836                 dbus_message_unref(m);
837
838         if (reply)
839                 dbus_message_unref(reply);
840
841         dbus_error_free(&error);
842
843         return r;
844 }
845
846 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
847         DBusMessage *m = NULL, *reply = NULL;
848         DBusError error;
849         int r;
850         unsigned i;
851
852         dbus_error_init(&error);
853
854         assert(bus);
855         assert(args);
856
857         for (i = 1; i < n; i++) {
858
859                 if (!(m = dbus_message_new_method_call(
860                                       "org.freedesktop.systemd1",
861                                       "/org/freedesktop/systemd1",
862                                       "org.freedesktop.systemd1.Manager",
863                                       "LoadUnit"))) {
864                         log_error("Could not allocate message.");
865                         r = -ENOMEM;
866                         goto finish;
867                 }
868
869                 if (!dbus_message_append_args(m,
870                                               DBUS_TYPE_STRING, &args[i],
871                                               DBUS_TYPE_INVALID)) {
872                         log_error("Could not append arguments to message.");
873                         r = -ENOMEM;
874                         goto finish;
875                 }
876
877                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
878                         log_error("Failed to issue method call: %s", bus_error_message(&error));
879                         r = -EIO;
880                         goto finish;
881                 }
882
883                 dbus_message_unref(m);
884                 dbus_message_unref(reply);
885
886                 m = reply = NULL;
887         }
888
889         r = 0;
890
891 finish:
892         if (m)
893                 dbus_message_unref(m);
894
895         if (reply)
896                 dbus_message_unref(reply);
897
898         dbus_error_free(&error);
899
900         return r;
901 }
902
903 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
904         DBusMessage *m = NULL, *reply = NULL;
905         DBusError error;
906         int r;
907         unsigned i;
908
909         dbus_error_init(&error);
910
911         assert(bus);
912         assert(args);
913
914         if (n <= 1)
915                 return daemon_reload(bus, args, n);
916
917         for (i = 1; i < n; i++) {
918                 unsigned id;
919                 const char *path;
920
921                 if (!(m = dbus_message_new_method_call(
922                                       "org.freedesktop.systemd1",
923                                       "/org/freedesktop/systemd1",
924                                       "org.freedesktop.systemd1.Manager",
925                                       "GetJob"))) {
926                         log_error("Could not allocate message.");
927                         r = -ENOMEM;
928                         goto finish;
929                 }
930
931                 if ((r = safe_atou(args[i], &id)) < 0) {
932                         log_error("Failed to parse job id: %s", strerror(-r));
933                         goto finish;
934                 }
935
936                 assert_cc(sizeof(uint32_t) == sizeof(id));
937                 if (!dbus_message_append_args(m,
938                                               DBUS_TYPE_UINT32, &id,
939                                               DBUS_TYPE_INVALID)) {
940                         log_error("Could not append arguments to message.");
941                         r = -ENOMEM;
942                         goto finish;
943                 }
944
945                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
946                         log_error("Failed to issue method call: %s", bus_error_message(&error));
947                         r = -EIO;
948                         goto finish;
949                 }
950
951                 if (!dbus_message_get_args(reply, &error,
952                                            DBUS_TYPE_OBJECT_PATH, &path,
953                                            DBUS_TYPE_INVALID)) {
954                         log_error("Failed to parse reply: %s", bus_error_message(&error));
955                         r = -EIO;
956                         goto finish;
957                 }
958
959                 dbus_message_unref(m);
960                 if (!(m = dbus_message_new_method_call(
961                                       "org.freedesktop.systemd1",
962                                       path,
963                                       "org.freedesktop.systemd1.Job",
964                                       "Cancel"))) {
965                         log_error("Could not allocate message.");
966                         r = -ENOMEM;
967                         goto finish;
968                 }
969
970                 dbus_message_unref(reply);
971                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
972                         log_error("Failed to issue method call: %s", bus_error_message(&error));
973                         r = -EIO;
974                         goto finish;
975                 }
976
977                 dbus_message_unref(m);
978                 dbus_message_unref(reply);
979                 m = reply = NULL;
980         }
981
982         r = 0;
983
984 finish:
985         if (m)
986                 dbus_message_unref(m);
987
988         if (reply)
989                 dbus_message_unref(reply);
990
991         dbus_error_free(&error);
992
993         return r;
994 }
995
996 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
997         DBusMessage *m = NULL, *reply = NULL;
998         dbus_bool_t b = FALSE;
999         DBusMessageIter iter, sub;
1000         const char
1001                 *interface = "org.freedesktop.systemd1.Unit",
1002                 *property = "NeedDaemonReload",
1003                 *path;
1004
1005         /* We ignore all errors here, since this is used to show a warning only */
1006
1007         if (!(m = dbus_message_new_method_call(
1008                               "org.freedesktop.systemd1",
1009                               "/org/freedesktop/systemd1",
1010                               "org.freedesktop.systemd1.Manager",
1011                               "GetUnit")))
1012                 goto finish;
1013
1014         if (!dbus_message_append_args(m,
1015                                       DBUS_TYPE_STRING, &unit,
1016                                       DBUS_TYPE_INVALID))
1017                 goto finish;
1018
1019         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1020                 goto finish;
1021
1022         if (!dbus_message_get_args(reply, NULL,
1023                                    DBUS_TYPE_OBJECT_PATH, &path,
1024                                    DBUS_TYPE_INVALID))
1025                 goto finish;
1026
1027         dbus_message_unref(m);
1028         if (!(m = dbus_message_new_method_call(
1029                               "org.freedesktop.systemd1",
1030                               path,
1031                               "org.freedesktop.DBus.Properties",
1032                               "Get")))
1033                 goto finish;
1034
1035         if (!dbus_message_append_args(m,
1036                                       DBUS_TYPE_STRING, &interface,
1037                                       DBUS_TYPE_STRING, &property,
1038                                       DBUS_TYPE_INVALID)) {
1039                 goto finish;
1040         }
1041
1042         dbus_message_unref(reply);
1043         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1044                 goto finish;
1045
1046         if (!dbus_message_iter_init(reply, &iter) ||
1047             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1048                 goto finish;
1049
1050         dbus_message_iter_recurse(&iter, &sub);
1051
1052         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1053                 goto finish;
1054
1055         dbus_message_iter_get_basic(&sub, &b);
1056
1057 finish:
1058         if (m)
1059                 dbus_message_unref(m);
1060
1061         if (reply)
1062                 dbus_message_unref(reply);
1063
1064         return b;
1065 }
1066
1067 typedef struct WaitData {
1068         Set *set;
1069         bool failed;
1070 } WaitData;
1071
1072 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1073         DBusError error;
1074         WaitData *d = data;
1075
1076         assert(connection);
1077         assert(message);
1078         assert(d);
1079
1080         dbus_error_init(&error);
1081
1082         log_debug("Got D-Bus request: %s.%s() on %s",
1083                   dbus_message_get_interface(message),
1084                   dbus_message_get_member(message),
1085                   dbus_message_get_path(message));
1086
1087         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1088                 log_error("Warning! D-Bus connection terminated.");
1089                 dbus_connection_close(connection);
1090
1091         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1092                 uint32_t id;
1093                 const char *path;
1094                 dbus_bool_t success = true;
1095
1096                 if (!dbus_message_get_args(message, &error,
1097                                            DBUS_TYPE_UINT32, &id,
1098                                            DBUS_TYPE_OBJECT_PATH, &path,
1099                                            DBUS_TYPE_BOOLEAN, &success,
1100                                            DBUS_TYPE_INVALID))
1101                         log_error("Failed to parse message: %s", bus_error_message(&error));
1102                 else {
1103                         char *p;
1104
1105                         if ((p = set_remove(d->set, (char*) path)))
1106                                 free(p);
1107
1108                         if (!success)
1109                                 d->failed = true;
1110                 }
1111         }
1112
1113         dbus_error_free(&error);
1114         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1115 }
1116
1117 static int enable_wait_for_jobs(DBusConnection *bus) {
1118         DBusError error;
1119
1120         assert(bus);
1121
1122         if (private_bus)
1123                 return 0;
1124
1125         dbus_error_init(&error);
1126         dbus_bus_add_match(bus,
1127                            "type='signal',"
1128                            "sender='org.freedesktop.systemd1',"
1129                            "interface='org.freedesktop.systemd1.Manager',"
1130                            "member='JobRemoved',"
1131                            "path='/org/freedesktop/systemd1'",
1132                            &error);
1133
1134         if (dbus_error_is_set(&error)) {
1135                 log_error("Failed to add match: %s", bus_error_message(&error));
1136                 dbus_error_free(&error);
1137                 return -EIO;
1138         }
1139
1140         /* This is slightly dirty, since we don't undo the match registrations. */
1141         return 0;
1142 }
1143
1144 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1145         int r;
1146         WaitData d;
1147
1148         assert(bus);
1149         assert(s);
1150
1151         zero(d);
1152         d.set = s;
1153         d.failed = false;
1154
1155         if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1156                 log_error("Failed to add filter.");
1157                 r = -ENOMEM;
1158                 goto finish;
1159         }
1160
1161         while (!set_isempty(s) &&
1162                dbus_connection_read_write_dispatch(bus, -1))
1163                 ;
1164
1165         if (!arg_quiet && d.failed)
1166                 log_error("Job failed. See system logs and 'systemctl status' for details.");
1167
1168         r = d.failed ? -EIO : 0;
1169
1170 finish:
1171         /* This is slightly dirty, since we don't undo the filter registration. */
1172
1173         return r;
1174 }
1175
1176 static int start_unit_one(
1177                 DBusConnection *bus,
1178                 const char *method,
1179                 const char *name,
1180                 const char *mode,
1181                 DBusError *error,
1182                 Set *s) {
1183
1184         DBusMessage *m = NULL, *reply = NULL;
1185         const char *path;
1186         int r;
1187
1188         assert(bus);
1189         assert(method);
1190         assert(name);
1191         assert(mode);
1192         assert(error);
1193         assert(arg_no_block || s);
1194
1195         if (!(m = dbus_message_new_method_call(
1196                               "org.freedesktop.systemd1",
1197                               "/org/freedesktop/systemd1",
1198                               "org.freedesktop.systemd1.Manager",
1199                               method))) {
1200                 log_error("Could not allocate message.");
1201                 r = -ENOMEM;
1202                 goto finish;
1203         }
1204
1205         if (!dbus_message_append_args(m,
1206                                       DBUS_TYPE_STRING, &name,
1207                                       DBUS_TYPE_STRING, &mode,
1208                                       DBUS_TYPE_INVALID)) {
1209                 log_error("Could not append arguments to message.");
1210                 r = -ENOMEM;
1211                 goto finish;
1212         }
1213
1214         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1215
1216                 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1217                         /* There's always a fallback possible for
1218                          * legacy actions. */
1219                         r = -EADDRNOTAVAIL;
1220                         goto finish;
1221                 }
1222
1223                 log_error("Failed to issue method call: %s", bus_error_message(error));
1224                 r = -EIO;
1225                 goto finish;
1226         }
1227
1228         if (!dbus_message_get_args(reply, error,
1229                                    DBUS_TYPE_OBJECT_PATH, &path,
1230                                    DBUS_TYPE_INVALID)) {
1231                 log_error("Failed to parse reply: %s", bus_error_message(error));
1232                 r = -EIO;
1233                 goto finish;
1234         }
1235
1236         if (need_daemon_reload(bus, name))
1237                 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1238                             arg_user ? "--user" : "--system");
1239
1240         if (!arg_no_block) {
1241                 char *p;
1242
1243                 if (!(p = strdup(path))) {
1244                         log_error("Failed to duplicate path.");
1245                         r = -ENOMEM;
1246                         goto finish;
1247                 }
1248
1249                 if ((r = set_put(s, p)) < 0) {
1250                         free(p);
1251                         log_error("Failed to add path to set.");
1252                         goto finish;
1253                 }
1254         }
1255
1256         r = 0;
1257
1258 finish:
1259         if (m)
1260                 dbus_message_unref(m);
1261
1262         if (reply)
1263                 dbus_message_unref(reply);
1264
1265         return r;
1266 }
1267
1268 static enum action verb_to_action(const char *verb) {
1269         if (streq(verb, "halt"))
1270                 return ACTION_HALT;
1271         else if (streq(verb, "poweroff"))
1272                 return ACTION_POWEROFF;
1273         else if (streq(verb, "reboot"))
1274                 return ACTION_REBOOT;
1275         else if (streq(verb, "kexec"))
1276                 return ACTION_KEXEC;
1277         else if (streq(verb, "rescue"))
1278                 return ACTION_RESCUE;
1279         else if (streq(verb, "emergency"))
1280                 return ACTION_EMERGENCY;
1281         else if (streq(verb, "default"))
1282                 return ACTION_DEFAULT;
1283         else if (streq(verb, "exit"))
1284                 return ACTION_EXIT;
1285         else
1286                 return ACTION_INVALID;
1287 }
1288
1289 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1290
1291         static const char * const table[_ACTION_MAX] = {
1292                 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1293                 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1294                 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1295                 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1296                 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1297                 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1298                 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1299                 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1300                 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1301                 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1302                 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1303                 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1304         };
1305
1306         int r, ret = 0;
1307         unsigned i;
1308         const char *method, *mode, *one_name;
1309         Set *s = NULL;
1310         DBusError error;
1311
1312         dbus_error_init(&error);
1313
1314         assert(bus);
1315
1316         spawn_ask_password_agent();
1317
1318         if (arg_action == ACTION_SYSTEMCTL) {
1319                 method =
1320                         streq(args[0], "stop")                  ? "StopUnit" :
1321                         streq(args[0], "reload")                ? "ReloadUnit" :
1322                         streq(args[0], "restart")               ? "RestartUnit" :
1323                         streq(args[0], "try-restart")           ||
1324                         streq(args[0], "condrestart")           ? "TryRestartUnit" :
1325                         streq(args[0], "reload-or-restart")     ? "ReloadOrRestartUnit" :
1326                         streq(args[0], "reload-or-try-restart") ||
1327                         streq(args[0], "force-reload")          ? "ReloadOrTryRestartUnit" :
1328                                                                   "StartUnit";
1329
1330                 mode =
1331                         (streq(args[0], "isolate") ||
1332                          streq(args[0], "rescue")  ||
1333                          streq(args[0], "emergency")) ? "isolate" :
1334                                              arg_fail ? "fail" :
1335                                                         "replace";
1336
1337                 one_name = table[verb_to_action(args[0])];
1338
1339         } else {
1340                 assert(arg_action < ELEMENTSOF(table));
1341                 assert(table[arg_action]);
1342
1343                 method = "StartUnit";
1344
1345                 mode = (arg_action == ACTION_EMERGENCY ||
1346                         arg_action == ACTION_RESCUE ||
1347                         arg_action == ACTION_RUNLEVEL2 ||
1348                         arg_action == ACTION_RUNLEVEL3 ||
1349                         arg_action == ACTION_RUNLEVEL4 ||
1350                         arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1351
1352                 one_name = table[arg_action];
1353         }
1354
1355         if (!arg_no_block) {
1356                 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1357                         log_error("Could not watch jobs: %s", strerror(-ret));
1358                         goto finish;
1359                 }
1360
1361                 if (!(s = set_new(string_hash_func, string_compare_func))) {
1362                         log_error("Failed to allocate set.");
1363                         ret = -ENOMEM;
1364                         goto finish;
1365                 }
1366         }
1367
1368         if (one_name) {
1369                 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1370                         goto finish;
1371         } else {
1372                 for (i = 1; i < n; i++)
1373                         if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1374                                 ret = translate_bus_error_to_exit_status(r, &error);
1375                                 dbus_error_free(&error);
1376                         }
1377         }
1378
1379         if (!arg_no_block)
1380                 if ((r = wait_for_jobs(bus, s)) < 0) {
1381                         ret = r;
1382                         goto finish;
1383                 }
1384
1385 finish:
1386         if (s)
1387                 set_free_free(s);
1388
1389         dbus_error_free(&error);
1390
1391         return ret;
1392 }
1393
1394 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1395         int r;
1396
1397         assert(bus);
1398         assert(args);
1399
1400         if (arg_force &&
1401             (streq(args[0], "halt") ||
1402              streq(args[0], "poweroff") ||
1403              streq(args[0], "reboot") ||
1404              streq(args[0], "kexec") ||
1405              streq(args[0], "exit")))
1406                 return daemon_reload(bus, args, n);
1407
1408         r = start_unit(bus, args, n);
1409
1410         if (r >= 0)
1411                 warn_wall(verb_to_action(args[0]));
1412
1413         return r;
1414 }
1415
1416 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1417         DBusMessage *m = NULL, *reply = NULL;
1418         const char
1419                 *interface = "org.freedesktop.systemd1.Unit",
1420                 *property = "ActiveState";
1421         int r = 3; /* According to LSB: "program is not running" */
1422         DBusError error;
1423         unsigned i;
1424
1425         assert(bus);
1426         assert(args);
1427
1428         dbus_error_init(&error);
1429
1430         for (i = 1; i < n; i++) {
1431                 const char *path = NULL;
1432                 const char *state;
1433                 DBusMessageIter iter, sub;
1434
1435                 if (!(m = dbus_message_new_method_call(
1436                                       "org.freedesktop.systemd1",
1437                                       "/org/freedesktop/systemd1",
1438                                       "org.freedesktop.systemd1.Manager",
1439                                       "GetUnit"))) {
1440                         log_error("Could not allocate message.");
1441                         r = -ENOMEM;
1442                         goto finish;
1443                 }
1444
1445                 if (!dbus_message_append_args(m,
1446                                               DBUS_TYPE_STRING, &args[i],
1447                                               DBUS_TYPE_INVALID)) {
1448                         log_error("Could not append arguments to message.");
1449                         r = -ENOMEM;
1450                         goto finish;
1451                 }
1452
1453                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1454
1455                         /* Hmm, cannot figure out anything about this unit... */
1456                         if (!arg_quiet)
1457                                 puts("unknown");
1458
1459                         dbus_error_free(&error);
1460                         dbus_message_unref(m);
1461                         continue;
1462                 }
1463
1464                 if (!dbus_message_get_args(reply, &error,
1465                                            DBUS_TYPE_OBJECT_PATH, &path,
1466                                            DBUS_TYPE_INVALID)) {
1467                         log_error("Failed to parse reply: %s", bus_error_message(&error));
1468                         r = -EIO;
1469                         goto finish;
1470                 }
1471
1472                 dbus_message_unref(m);
1473                 if (!(m = dbus_message_new_method_call(
1474                                       "org.freedesktop.systemd1",
1475                                       path,
1476                                       "org.freedesktop.DBus.Properties",
1477                                       "Get"))) {
1478                         log_error("Could not allocate message.");
1479                         r = -ENOMEM;
1480                         goto finish;
1481                 }
1482
1483                 if (!dbus_message_append_args(m,
1484                                               DBUS_TYPE_STRING, &interface,
1485                                               DBUS_TYPE_STRING, &property,
1486                                               DBUS_TYPE_INVALID)) {
1487                         log_error("Could not append arguments to message.");
1488                         r = -ENOMEM;
1489                         goto finish;
1490                 }
1491
1492                 dbus_message_unref(reply);
1493                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1494                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1495                         r = -EIO;
1496                         goto finish;
1497                 }
1498
1499                 if (!dbus_message_iter_init(reply, &iter) ||
1500                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
1501                         log_error("Failed to parse reply.");
1502                         r = -EIO;
1503                         goto finish;
1504                 }
1505
1506                 dbus_message_iter_recurse(&iter, &sub);
1507
1508                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
1509                         log_error("Failed to parse reply.");
1510                         r = -EIO;
1511                         goto finish;
1512                 }
1513
1514                 dbus_message_iter_get_basic(&sub, &state);
1515
1516                 if (!arg_quiet)
1517                         puts(state);
1518
1519                 if (streq(state, "active") || streq(state, "reloading"))
1520                         r = 0;
1521
1522                 dbus_message_unref(m);
1523                 dbus_message_unref(reply);
1524                 m = reply = NULL;
1525         }
1526
1527 finish:
1528         if (m)
1529                 dbus_message_unref(m);
1530
1531         if (reply)
1532                 dbus_message_unref(reply);
1533
1534         dbus_error_free(&error);
1535
1536         return r;
1537 }
1538
1539 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1540         DBusMessage *m = NULL, *reply = NULL;
1541         int r = 0;
1542         DBusError error;
1543         unsigned i;
1544
1545         assert(bus);
1546         assert(args);
1547
1548         dbus_error_init(&error);
1549
1550         if (!arg_kill_who)
1551                 arg_kill_who = "all";
1552
1553         if (!arg_kill_mode)
1554                 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1555
1556         for (i = 1; i < n; i++) {
1557
1558                 if (!(m = dbus_message_new_method_call(
1559                                       "org.freedesktop.systemd1",
1560                                       "/org/freedesktop/systemd1",
1561                                       "org.freedesktop.systemd1.Manager",
1562                                       "KillUnit"))) {
1563                         log_error("Could not allocate message.");
1564                         r = -ENOMEM;
1565                         goto finish;
1566                 }
1567
1568                 if (!dbus_message_append_args(m,
1569                                               DBUS_TYPE_STRING, &args[i],
1570                                               DBUS_TYPE_STRING, &arg_kill_who,
1571                                               DBUS_TYPE_STRING, &arg_kill_mode,
1572                                               DBUS_TYPE_INT32, &arg_signal,
1573                                               DBUS_TYPE_INVALID)) {
1574                         log_error("Could not append arguments to message.");
1575                         r = -ENOMEM;
1576                         goto finish;
1577                 }
1578
1579                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1580                         log_error("Failed to issue method call: %s", bus_error_message(&error));
1581                         dbus_error_free(&error);
1582                         r = -EIO;
1583                 }
1584
1585                 dbus_message_unref(m);
1586
1587                 if (reply)
1588                         dbus_message_unref(reply);
1589                 m = reply = NULL;
1590         }
1591
1592 finish:
1593         if (m)
1594                 dbus_message_unref(m);
1595
1596         if (reply)
1597                 dbus_message_unref(reply);
1598
1599         dbus_error_free(&error);
1600
1601         return r;
1602 }
1603
1604 typedef struct ExecStatusInfo {
1605         char *path;
1606         char **argv;
1607
1608         bool ignore;
1609
1610         usec_t start_timestamp;
1611         usec_t exit_timestamp;
1612         pid_t pid;
1613         int code;
1614         int status;
1615
1616         LIST_FIELDS(struct ExecStatusInfo, exec);
1617 } ExecStatusInfo;
1618
1619 static void exec_status_info_free(ExecStatusInfo *i) {
1620         assert(i);
1621
1622         free(i->path);
1623         strv_free(i->argv);
1624         free(i);
1625 }
1626
1627 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1628         uint64_t start_timestamp, exit_timestamp;
1629         DBusMessageIter sub2, sub3;
1630         const char*path;
1631         unsigned n;
1632         uint32_t pid;
1633         int32_t code, status;
1634         dbus_bool_t ignore;
1635
1636         assert(i);
1637         assert(i);
1638
1639         if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1640                 return -EIO;
1641
1642         dbus_message_iter_recurse(sub, &sub2);
1643
1644         if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1645                 return -EIO;
1646
1647         if (!(i->path = strdup(path)))
1648                 return -ENOMEM;
1649
1650         if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1651             dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1652                 return -EIO;
1653
1654         n = 0;
1655         dbus_message_iter_recurse(&sub2, &sub3);
1656         while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1657                 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1658                 dbus_message_iter_next(&sub3);
1659                 n++;
1660         }
1661
1662
1663         if (!(i->argv = new0(char*, n+1)))
1664                 return -ENOMEM;
1665
1666         n = 0;
1667         dbus_message_iter_recurse(&sub2, &sub3);
1668         while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1669                 const char *s;
1670
1671                 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1672                 dbus_message_iter_get_basic(&sub3, &s);
1673                 dbus_message_iter_next(&sub3);
1674
1675                 if (!(i->argv[n++] = strdup(s)))
1676                         return -ENOMEM;
1677         }
1678
1679         if (!dbus_message_iter_next(&sub2) ||
1680             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1681             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1682             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1683             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1684             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1685             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1686                 return -EIO;
1687
1688         i->ignore = ignore;
1689         i->start_timestamp = (usec_t) start_timestamp;
1690         i->exit_timestamp = (usec_t) exit_timestamp;
1691         i->pid = (pid_t) pid;
1692         i->code = code;
1693         i->status = status;
1694
1695         return 0;
1696 }
1697
1698 typedef struct UnitStatusInfo {
1699         const char *id;
1700         const char *load_state;
1701         const char *active_state;
1702         const char *sub_state;
1703
1704         const char *description;
1705         const char *following;
1706
1707         const char *path;
1708         const char *default_control_group;
1709
1710         usec_t inactive_exit_timestamp;
1711         usec_t active_enter_timestamp;
1712         usec_t active_exit_timestamp;
1713         usec_t inactive_enter_timestamp;
1714
1715         bool need_daemon_reload;
1716
1717         /* Service */
1718         pid_t main_pid;
1719         pid_t control_pid;
1720         const char *status_text;
1721         bool running:1;
1722 #ifdef HAVE_SYSV_COMPAT
1723         bool is_sysv:1;
1724 #endif
1725
1726         usec_t start_timestamp;
1727         usec_t exit_timestamp;
1728
1729         int exit_code, exit_status;
1730
1731         /* Socket */
1732         unsigned n_accepted;
1733         unsigned n_connections;
1734         bool accept;
1735
1736         /* Device */
1737         const char *sysfs_path;
1738
1739         /* Mount, Automount */
1740         const char *where;
1741
1742         /* Swap */
1743         const char *what;
1744
1745         LIST_HEAD(ExecStatusInfo, exec);
1746 } UnitStatusInfo;
1747
1748 static void print_status_info(UnitStatusInfo *i) {
1749         ExecStatusInfo *p;
1750         const char *on, *off, *ss;
1751         usec_t timestamp;
1752         char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1753         char since2[FORMAT_TIMESTAMP_MAX], *s2;
1754
1755         assert(i);
1756
1757         /* This shows pretty information about a unit. See
1758          * print_property() for a low-level property printer */
1759
1760         printf("%s", strna(i->id));
1761
1762         if (i->description && !streq_ptr(i->id, i->description))
1763                 printf(" - %s", i->description);
1764
1765         printf("\n");
1766
1767         if (i->following)
1768                 printf("\t  Follow: unit currently follows state of %s\n", i->following);
1769
1770         if (streq_ptr(i->load_state, "failed") ||
1771             streq_ptr(i->load_state, "banned")) {
1772                 on = ansi_highlight(true);
1773                 off = ansi_highlight(false);
1774         } else
1775                 on = off = "";
1776
1777         if (i->path)
1778                 printf("\t  Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1779         else
1780                 printf("\t  Loaded: %s%s%s\n", on, strna(i->load_state), off);
1781
1782         ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1783
1784         if (streq_ptr(i->active_state, "failed")) {
1785                 on = ansi_highlight(true);
1786                 off = ansi_highlight(false);
1787         } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1788                 on = ansi_highlight_green(true);
1789                 off = ansi_highlight_green(false);
1790         } else
1791                 on = off = "";
1792
1793         if (ss)
1794                 printf("\t  Active: %s%s (%s)%s",
1795                        on,
1796                        strna(i->active_state),
1797                        ss,
1798                        off);
1799         else
1800                 printf("\t  Active: %s%s%s",
1801                        on,
1802                        strna(i->active_state),
1803                        off);
1804
1805         timestamp = (streq_ptr(i->active_state, "active")      ||
1806                      streq_ptr(i->active_state, "reloading"))   ? i->active_enter_timestamp :
1807                     (streq_ptr(i->active_state, "inactive")    ||
1808                      streq_ptr(i->active_state, "failed"))      ? i->inactive_enter_timestamp :
1809                     streq_ptr(i->active_state, "activating")    ? i->inactive_exit_timestamp :
1810                                                                   i->active_exit_timestamp;
1811
1812         s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1813         s2 = format_timestamp(since2, sizeof(since2), timestamp);
1814
1815         if (s1)
1816                 printf(" since %s; %s\n", s2, s1);
1817         else if (s2)
1818                 printf(" since %s\n", s2);
1819         else
1820                 printf("\n");
1821
1822         if (i->sysfs_path)
1823                 printf("\t  Device: %s\n", i->sysfs_path);
1824         if (i->where)
1825                 printf("\t   Where: %s\n", i->where);
1826         if (i->what)
1827                 printf("\t    What: %s\n", i->what);
1828
1829         if (i->accept)
1830                 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1831
1832         LIST_FOREACH(exec, p, i->exec) {
1833                 char *t;
1834
1835                 /* Only show exited processes here */
1836                 if (p->code == 0)
1837                         continue;
1838
1839                 t = strv_join(p->argv, " ");
1840                 printf("\t Process: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1841                 free(t);
1842
1843                 if (p->code == CLD_EXITED) {
1844                         const char *c;
1845
1846                         printf("status=%i", p->status);
1847
1848 #ifdef HAVE_SYSV_COMPAT
1849                         if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1850 #else
1851                         if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1852 #endif
1853                                 printf("/%s", c);
1854
1855                 } else
1856                         printf("signal=%s", signal_to_string(p->status));
1857                 printf(")\n");
1858
1859                 if (i->main_pid == p->pid &&
1860                     i->start_timestamp == p->start_timestamp &&
1861                     i->exit_timestamp == p->start_timestamp)
1862                         /* Let's not show this twice */
1863                         i->main_pid = 0;
1864
1865                 if (p->pid == i->control_pid)
1866                         i->control_pid = 0;
1867         }
1868
1869         if (i->main_pid > 0 || i->control_pid > 0) {
1870                 printf("\t");
1871
1872                 if (i->main_pid > 0) {
1873                         printf("Main PID: %u", (unsigned) i->main_pid);
1874
1875                         if (i->running) {
1876                                 char *t = NULL;
1877                                 get_process_name(i->main_pid, &t);
1878                                 if (t) {
1879                                         printf(" (%s)", t);
1880                                         free(t);
1881                                 }
1882                         } else if (i->exit_code > 0) {
1883                                 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1884
1885                                 if (i->exit_code == CLD_EXITED) {
1886                                         const char *c;
1887
1888                                         printf("status=%i", i->exit_status);
1889
1890 #ifdef HAVE_SYSV_COMPAT
1891                                         if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1892 #else
1893                                         if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
1894 #endif
1895                                                 printf("/%s", c);
1896
1897                                 } else
1898                                         printf("signal=%s", signal_to_string(i->exit_status));
1899                                 printf(")");
1900                         }
1901                 }
1902
1903                 if (i->main_pid > 0 && i->control_pid > 0)
1904                         printf(";");
1905
1906                 if (i->control_pid > 0) {
1907                         char *t = NULL;
1908
1909                         printf(" Control: %u", (unsigned) i->control_pid);
1910
1911                         get_process_name(i->control_pid, &t);
1912                         if (t) {
1913                                 printf(" (%s)", t);
1914                                 free(t);
1915                         }
1916                 }
1917
1918                 printf("\n");
1919         }
1920
1921         if (i->status_text)
1922                 printf("\t  Status: \"%s\"\n", i->status_text);
1923
1924         if (i->default_control_group) {
1925                 unsigned c;
1926
1927                 printf("\t  CGroup: %s\n", i->default_control_group);
1928
1929                 if ((c = columns()) > 18)
1930                         c -= 18;
1931                 else
1932                         c = 0;
1933
1934                 show_cgroup_by_path(i->default_control_group, "\t\t  ", c);
1935         }
1936
1937         if (i->need_daemon_reload)
1938                 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1939                        ansi_highlight(true),
1940                        ansi_highlight(false),
1941                        arg_user ? "--user" : "--system");
1942 }
1943
1944 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1945
1946         switch (dbus_message_iter_get_arg_type(iter)) {
1947
1948         case DBUS_TYPE_STRING: {
1949                 const char *s;
1950
1951                 dbus_message_iter_get_basic(iter, &s);
1952
1953                 if (s[0]) {
1954                         if (streq(name, "Id"))
1955                                 i->id = s;
1956                         else if (streq(name, "LoadState"))
1957                                 i->load_state = s;
1958                         else if (streq(name, "ActiveState"))
1959                                 i->active_state = s;
1960                         else if (streq(name, "SubState"))
1961                                 i->sub_state = s;
1962                         else if (streq(name, "Description"))
1963                                 i->description = s;
1964                         else if (streq(name, "FragmentPath"))
1965                                 i->path = s;
1966 #ifdef HAVE_SYSV_COMPAT
1967                         else if (streq(name, "SysVPath")) {
1968                                 i->is_sysv = true;
1969                                 i->path = s;
1970                         }
1971 #endif
1972                         else if (streq(name, "DefaultControlGroup"))
1973                                 i->default_control_group = s;
1974                         else if (streq(name, "StatusText"))
1975                                 i->status_text = s;
1976                         else if (streq(name, "SysFSPath"))
1977                                 i->sysfs_path = s;
1978                         else if (streq(name, "Where"))
1979                                 i->where = s;
1980                         else if (streq(name, "What"))
1981                                 i->what = s;
1982                         else if (streq(name, "Following"))
1983                                 i->following = s;
1984                 }
1985
1986                 break;
1987         }
1988
1989         case DBUS_TYPE_BOOLEAN: {
1990                 dbus_bool_t b;
1991
1992                 dbus_message_iter_get_basic(iter, &b);
1993
1994                 if (streq(name, "Accept"))
1995                         i->accept = b;
1996                 else if (streq(name, "NeedDaemonReload"))
1997                         i->need_daemon_reload = b;
1998
1999                 break;
2000         }
2001
2002         case DBUS_TYPE_UINT32: {
2003                 uint32_t u;
2004
2005                 dbus_message_iter_get_basic(iter, &u);
2006
2007                 if (streq(name, "MainPID")) {
2008                         if (u > 0) {
2009                                 i->main_pid = (pid_t) u;
2010                                 i->running = true;
2011                         }
2012                 } else if (streq(name, "ControlPID"))
2013                         i->control_pid = (pid_t) u;
2014                 else if (streq(name, "ExecMainPID")) {
2015                         if (u > 0)
2016                                 i->main_pid = (pid_t) u;
2017                 } else if (streq(name, "NAccepted"))
2018                         i->n_accepted = u;
2019                 else if (streq(name, "NConnections"))
2020                         i->n_connections = u;
2021
2022                 break;
2023         }
2024
2025         case DBUS_TYPE_INT32: {
2026                 int32_t j;
2027
2028                 dbus_message_iter_get_basic(iter, &j);
2029
2030                 if (streq(name, "ExecMainCode"))
2031                         i->exit_code = (int) j;
2032                 else if (streq(name, "ExecMainStatus"))
2033                         i->exit_status = (int) j;
2034
2035                 break;
2036         }
2037
2038         case DBUS_TYPE_UINT64: {
2039                 uint64_t u;
2040
2041                 dbus_message_iter_get_basic(iter, &u);
2042
2043                 if (streq(name, "ExecMainStartTimestamp"))
2044                         i->start_timestamp = (usec_t) u;
2045                 else if (streq(name, "ExecMainExitTimestamp"))
2046                         i->exit_timestamp = (usec_t) u;
2047                 else if (streq(name, "ActiveEnterTimestamp"))
2048                         i->active_enter_timestamp = (usec_t) u;
2049                 else if (streq(name, "InactiveEnterTimestamp"))
2050                         i->inactive_enter_timestamp = (usec_t) u;
2051                 else if (streq(name, "InactiveExitTimestamp"))
2052                         i->inactive_exit_timestamp = (usec_t) u;
2053                 else if (streq(name, "ActiveExitTimestamp"))
2054                         i->active_exit_timestamp = (usec_t) u;
2055
2056                 break;
2057         }
2058
2059         case DBUS_TYPE_ARRAY: {
2060
2061                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2062                     startswith(name, "Exec")) {
2063                         DBusMessageIter sub;
2064
2065                         dbus_message_iter_recurse(iter, &sub);
2066                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2067                                 ExecStatusInfo *info;
2068                                 int r;
2069
2070                                 if (!(info = new0(ExecStatusInfo, 1)))
2071                                         return -ENOMEM;
2072
2073                                 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2074                                         free(info);
2075                                         return r;
2076                                 }
2077
2078                                 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2079
2080                                 dbus_message_iter_next(&sub);
2081                         }
2082                 }
2083
2084                 break;
2085         }
2086         }
2087
2088         return 0;
2089 }
2090
2091 static int print_property(const char *name, DBusMessageIter *iter) {
2092         assert(name);
2093         assert(iter);
2094
2095         /* This is a low-level property printer, see
2096          * print_status_info() for the nicer output */
2097
2098         if (arg_property && !strv_find(arg_property, name))
2099                 return 0;
2100
2101         switch (dbus_message_iter_get_arg_type(iter)) {
2102
2103         case DBUS_TYPE_STRING: {
2104                 const char *s;
2105                 dbus_message_iter_get_basic(iter, &s);
2106
2107                 if (arg_all || s[0])
2108                         printf("%s=%s\n", name, s);
2109
2110                 return 0;
2111         }
2112
2113         case DBUS_TYPE_BOOLEAN: {
2114                 dbus_bool_t b;
2115                 dbus_message_iter_get_basic(iter, &b);
2116                 printf("%s=%s\n", name, yes_no(b));
2117
2118                 return 0;
2119         }
2120
2121         case DBUS_TYPE_UINT64: {
2122                 uint64_t u;
2123                 dbus_message_iter_get_basic(iter, &u);
2124
2125                 /* Yes, heuristics! But we can change this check
2126                  * should it turn out to not be sufficient */
2127
2128                 if (strstr(name, "Timestamp")) {
2129                         char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2130
2131                         if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2132                                 printf("%s=%s\n", name, strempty(t));
2133                 } else if (strstr(name, "USec")) {
2134                         char timespan[FORMAT_TIMESPAN_MAX];
2135
2136                         printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2137                 } else
2138                         printf("%s=%llu\n", name, (unsigned long long) u);
2139
2140                 return 0;
2141         }
2142
2143         case DBUS_TYPE_UINT32: {
2144                 uint32_t u;
2145                 dbus_message_iter_get_basic(iter, &u);
2146
2147                 if (strstr(name, "UMask") || strstr(name, "Mode"))
2148                         printf("%s=%04o\n", name, u);
2149                 else
2150                         printf("%s=%u\n", name, (unsigned) u);
2151
2152                 return 0;
2153         }
2154
2155         case DBUS_TYPE_INT32: {
2156                 int32_t i;
2157                 dbus_message_iter_get_basic(iter, &i);
2158
2159                 printf("%s=%i\n", name, (int) i);
2160                 return 0;
2161         }
2162
2163         case DBUS_TYPE_DOUBLE: {
2164                 double d;
2165                 dbus_message_iter_get_basic(iter, &d);
2166
2167                 printf("%s=%g\n", name, d);
2168                 return 0;
2169         }
2170
2171         case DBUS_TYPE_STRUCT: {
2172                 DBusMessageIter sub;
2173                 dbus_message_iter_recurse(iter, &sub);
2174
2175                 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2176                         uint32_t u;
2177
2178                         dbus_message_iter_get_basic(&sub, &u);
2179
2180                         if (u)
2181                                 printf("%s=%u\n", name, (unsigned) u);
2182                         else if (arg_all)
2183                                 printf("%s=\n", name);
2184
2185                         return 0;
2186                 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2187                         const char *s;
2188
2189                         dbus_message_iter_get_basic(&sub, &s);
2190
2191                         if (arg_all || s[0])
2192                                 printf("%s=%s\n", name, s);
2193
2194                         return 0;
2195                 }
2196
2197                 break;
2198         }
2199
2200         case DBUS_TYPE_ARRAY:
2201
2202                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2203                         DBusMessageIter sub;
2204                         bool space = false;
2205
2206                         dbus_message_iter_recurse(iter, &sub);
2207                         if (arg_all ||
2208                             dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2209                                 printf("%s=", name);
2210
2211                                 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2212                                         const char *s;
2213
2214                                         assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2215                                         dbus_message_iter_get_basic(&sub, &s);
2216                                         printf("%s%s", space ? " " : "", s);
2217
2218                                         space = true;
2219                                         dbus_message_iter_next(&sub);
2220                                 }
2221
2222                                 puts("");
2223                         }
2224
2225                         return 0;
2226
2227                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2228                         DBusMessageIter sub;
2229
2230                         dbus_message_iter_recurse(iter, &sub);
2231                         if (arg_all ||
2232                             dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2233                                 printf("%s=", name);
2234
2235                                 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2236                                         uint8_t u;
2237
2238                                         assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2239                                         dbus_message_iter_get_basic(&sub, &u);
2240                                         printf("%02x", u);
2241
2242                                         dbus_message_iter_next(&sub);
2243                                 }
2244
2245                                 puts("");
2246                         }
2247
2248                         return 0;
2249
2250                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2251                         DBusMessageIter sub, sub2;
2252
2253                         dbus_message_iter_recurse(iter, &sub);
2254                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2255                                 const char *type, *path;
2256
2257                                 dbus_message_iter_recurse(&sub, &sub2);
2258
2259                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2260                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2261                                         printf("%s=%s\n", type, path);
2262
2263                                 dbus_message_iter_next(&sub);
2264                         }
2265
2266                         return 0;
2267
2268                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2269                         DBusMessageIter sub, sub2;
2270
2271                         dbus_message_iter_recurse(iter, &sub);
2272                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2273                                 const char *base;
2274                                 uint64_t value, next_elapse;
2275
2276                                 dbus_message_iter_recurse(&sub, &sub2);
2277
2278                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2279                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2280                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2281                                         char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2282
2283                                         printf("%s={ value=%s ; next_elapse=%s }\n",
2284                                                base,
2285                                                format_timespan(timespan1, sizeof(timespan1), value),
2286                                                format_timespan(timespan2, sizeof(timespan2), next_elapse));
2287                                 }
2288
2289                                 dbus_message_iter_next(&sub);
2290                         }
2291
2292                         return 0;
2293
2294                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2295                         DBusMessageIter sub;
2296
2297                         dbus_message_iter_recurse(iter, &sub);
2298                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2299                                 ExecStatusInfo info;
2300
2301                                 zero(info);
2302                                 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2303                                         char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2304                                         char *t;
2305
2306                                         t = strv_join(info.argv, " ");
2307
2308                                         printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2309                                                name,
2310                                                strna(info.path),
2311                                                strna(t),
2312                                                yes_no(info.ignore),
2313                                                strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2314                                                strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2315                                                (unsigned) info. pid,
2316                                                sigchld_code_to_string(info.code),
2317                                                info.status,
2318                                                info.code == CLD_EXITED ? "" : "/",
2319                                                strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2320
2321                                         free(t);
2322                                 }
2323
2324                                 free(info.path);
2325                                 strv_free(info.argv);
2326
2327                                 dbus_message_iter_next(&sub);
2328                         }
2329
2330                         return 0;
2331                 }
2332
2333                 break;
2334         }
2335
2336         if (arg_all)
2337                 printf("%s=[unprintable]\n", name);
2338
2339         return 0;
2340 }
2341
2342 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2343         DBusMessage *m = NULL, *reply = NULL;
2344         const char *interface = "";
2345         int r;
2346         DBusError error;
2347         DBusMessageIter iter, sub, sub2, sub3;
2348         UnitStatusInfo info;
2349         ExecStatusInfo *p;
2350
2351         assert(bus);
2352         assert(path);
2353         assert(new_line);
2354
2355         zero(info);
2356         dbus_error_init(&error);
2357
2358         if (!(m = dbus_message_new_method_call(
2359                               "org.freedesktop.systemd1",
2360                               path,
2361                               "org.freedesktop.DBus.Properties",
2362                               "GetAll"))) {
2363                 log_error("Could not allocate message.");
2364                 r = -ENOMEM;
2365                 goto finish;
2366         }
2367
2368         if (!dbus_message_append_args(m,
2369                                       DBUS_TYPE_STRING, &interface,
2370                                       DBUS_TYPE_INVALID)) {
2371                 log_error("Could not append arguments to message.");
2372                 r = -ENOMEM;
2373                 goto finish;
2374         }
2375
2376         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2377                 log_error("Failed to issue method call: %s", bus_error_message(&error));
2378                 r = -EIO;
2379                 goto finish;
2380         }
2381
2382         if (!dbus_message_iter_init(reply, &iter) ||
2383             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2384             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY)  {
2385                 log_error("Failed to parse reply.");
2386                 r = -EIO;
2387                 goto finish;
2388         }
2389
2390         dbus_message_iter_recurse(&iter, &sub);
2391
2392         if (*new_line)
2393                 printf("\n");
2394
2395         *new_line = true;
2396
2397         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2398                 const char *name;
2399
2400                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2401                         log_error("Failed to parse reply.");
2402                         r = -EIO;
2403                         goto finish;
2404                 }
2405
2406                 dbus_message_iter_recurse(&sub, &sub2);
2407
2408                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2409                         log_error("Failed to parse reply.");
2410                         r = -EIO;
2411                         goto finish;
2412                 }
2413
2414                 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT)  {
2415                         log_error("Failed to parse reply.");
2416                         r = -EIO;
2417                         goto finish;
2418                 }
2419
2420                 dbus_message_iter_recurse(&sub2, &sub3);
2421
2422                 if (show_properties)
2423                         r = print_property(name, &sub3);
2424                 else
2425                         r = status_property(name, &sub3, &info);
2426
2427                 if (r < 0) {
2428                         log_error("Failed to parse reply.");
2429                         r = -EIO;
2430                         goto finish;
2431                 }
2432
2433                 dbus_message_iter_next(&sub);
2434         }
2435
2436         r = 0;
2437
2438         if (!show_properties)
2439                 print_status_info(&info);
2440
2441         if (!streq_ptr(info.active_state, "active") &&
2442             !streq_ptr(info.active_state, "reloading") &&
2443             streq(verb, "status"))
2444                 /* According to LSB: "program not running" */
2445                 r = 3;
2446
2447         while ((p = info.exec)) {
2448                 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2449                 exec_status_info_free(p);
2450         }
2451
2452 finish:
2453         if (m)
2454                 dbus_message_unref(m);
2455
2456         if (reply)
2457                 dbus_message_unref(reply);
2458
2459         dbus_error_free(&error);
2460
2461         return r;
2462 }
2463
2464 static int show(DBusConnection *bus, char **args, unsigned n) {
2465         DBusMessage *m = NULL, *reply = NULL;
2466         int r, ret = 0;
2467         DBusError error;
2468         unsigned i;
2469         bool show_properties, new_line = false;
2470
2471         assert(bus);
2472         assert(args);
2473
2474         dbus_error_init(&error);
2475
2476         show_properties = !streq(args[0], "status");
2477
2478         if (show_properties && n <= 1) {
2479                 /* If not argument is specified inspect the manager
2480                  * itself */
2481
2482                 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2483                 goto finish;
2484         }
2485
2486         for (i = 1; i < n; i++) {
2487                 const char *path = NULL;
2488                 uint32_t id;
2489
2490                 if (safe_atou32(args[i], &id) < 0) {
2491
2492                         /* Interpret as unit name */
2493
2494                         if (!(m = dbus_message_new_method_call(
2495                                               "org.freedesktop.systemd1",
2496                                               "/org/freedesktop/systemd1",
2497                                               "org.freedesktop.systemd1.Manager",
2498                                               "LoadUnit"))) {
2499                                 log_error("Could not allocate message.");
2500                                 ret = -ENOMEM;
2501                                 goto finish;
2502                         }
2503
2504                         if (!dbus_message_append_args(m,
2505                                                       DBUS_TYPE_STRING, &args[i],
2506                                                       DBUS_TYPE_INVALID)) {
2507                                 log_error("Could not append arguments to message.");
2508                                 ret = -ENOMEM;
2509                                 goto finish;
2510                         }
2511
2512                         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2513
2514                                 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2515                                         log_error("Failed to issue method call: %s", bus_error_message(&error));
2516                                         ret = -EIO;
2517                                         goto finish;
2518                                 }
2519
2520                                 dbus_error_free(&error);
2521
2522                                 dbus_message_unref(m);
2523                                 if (!(m = dbus_message_new_method_call(
2524                                                       "org.freedesktop.systemd1",
2525                                                       "/org/freedesktop/systemd1",
2526                                                       "org.freedesktop.systemd1.Manager",
2527                                                       "GetUnit"))) {
2528                                         log_error("Could not allocate message.");
2529                                         ret = -ENOMEM;
2530                                         goto finish;
2531                                 }
2532
2533                                 if (!dbus_message_append_args(m,
2534                                                               DBUS_TYPE_STRING, &args[i],
2535                                                               DBUS_TYPE_INVALID)) {
2536                                         log_error("Could not append arguments to message.");
2537                                         ret = -ENOMEM;
2538                                         goto finish;
2539                                 }
2540
2541                                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2542                                         log_error("Failed to issue method call: %s", bus_error_message(&error));
2543
2544                                         if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2545                                                 ret = 4; /* According to LSB: "program or service status is unknown" */
2546                                         else
2547                                                 ret = -EIO;
2548                                         goto finish;
2549                                 }
2550                         }
2551
2552                 } else if (show_properties) {
2553
2554                         /* Interpret as job id */
2555
2556                         if (!(m = dbus_message_new_method_call(
2557                                               "org.freedesktop.systemd1",
2558                                               "/org/freedesktop/systemd1",
2559                                               "org.freedesktop.systemd1.Manager",
2560                                               "GetJob"))) {
2561                                 log_error("Could not allocate message.");
2562                                 ret = -ENOMEM;
2563                                 goto finish;
2564                         }
2565
2566                         if (!dbus_message_append_args(m,
2567                                                       DBUS_TYPE_UINT32, &id,
2568                                                       DBUS_TYPE_INVALID)) {
2569                                 log_error("Could not append arguments to message.");
2570                                 ret = -ENOMEM;
2571                                 goto finish;
2572                         }
2573
2574                         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2575                                 log_error("Failed to issue method call: %s", bus_error_message(&error));
2576                                 ret = -EIO;
2577                                 goto finish;
2578                         }
2579                 } else {
2580
2581                         /* Interpret as PID */
2582
2583                         if (!(m = dbus_message_new_method_call(
2584                                               "org.freedesktop.systemd1",
2585                                               "/org/freedesktop/systemd1",
2586                                               "org.freedesktop.systemd1.Manager",
2587                                               "GetUnitByPID"))) {
2588                                 log_error("Could not allocate message.");
2589                                 ret = -ENOMEM;
2590                                 goto finish;
2591                         }
2592
2593                         if (!dbus_message_append_args(m,
2594                                                       DBUS_TYPE_UINT32, &id,
2595                                                       DBUS_TYPE_INVALID)) {
2596                                 log_error("Could not append arguments to message.");
2597                                 ret = -ENOMEM;
2598                                 goto finish;
2599                         }
2600
2601                         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2602                                 log_error("Failed to issue method call: %s", bus_error_message(&error));
2603                                 ret = -EIO;
2604                                 goto finish;
2605                         }
2606                 }
2607
2608                 if (!dbus_message_get_args(reply, &error,
2609                                            DBUS_TYPE_OBJECT_PATH, &path,
2610                                            DBUS_TYPE_INVALID)) {
2611                         log_error("Failed to parse reply: %s", bus_error_message(&error));
2612                         ret = -EIO;
2613                         goto finish;
2614                 }
2615
2616                 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2617                         ret = r;
2618
2619                 dbus_message_unref(m);
2620                 dbus_message_unref(reply);
2621                 m = reply = NULL;
2622         }
2623
2624 finish:
2625         if (m)
2626                 dbus_message_unref(m);
2627
2628         if (reply)
2629                 dbus_message_unref(reply);
2630
2631         dbus_error_free(&error);
2632
2633         return ret;
2634 }
2635
2636 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2637         DBusError error;
2638         DBusMessage *m = NULL, *reply = NULL;
2639
2640         assert(connection);
2641         assert(message);
2642
2643         dbus_error_init(&error);
2644
2645         log_debug("Got D-Bus request: %s.%s() on %s",
2646                   dbus_message_get_interface(message),
2647                   dbus_message_get_member(message),
2648                   dbus_message_get_path(message));
2649
2650         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2651                 log_error("Warning! D-Bus connection terminated.");
2652                 dbus_connection_close(connection);
2653
2654         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2655                    dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2656                 const char *id, *path;
2657
2658                 if (!dbus_message_get_args(message, &error,
2659                                            DBUS_TYPE_STRING, &id,
2660                                            DBUS_TYPE_OBJECT_PATH, &path,
2661                                            DBUS_TYPE_INVALID))
2662                         log_error("Failed to parse message: %s", bus_error_message(&error));
2663                 else if (streq(dbus_message_get_member(message), "UnitNew"))
2664                         printf("Unit %s added.\n", id);
2665                 else
2666                         printf("Unit %s removed.\n", id);
2667
2668         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2669                    dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2670                 uint32_t id;
2671                 const char *path;
2672
2673                 if (!dbus_message_get_args(message, &error,
2674                                            DBUS_TYPE_UINT32, &id,
2675                                            DBUS_TYPE_OBJECT_PATH, &path,
2676                                            DBUS_TYPE_INVALID))
2677                         log_error("Failed to parse message: %s", bus_error_message(&error));
2678                 else if (streq(dbus_message_get_member(message), "JobNew"))
2679                         printf("Job %u added.\n", id);
2680                 else
2681                         printf("Job %u removed.\n", id);
2682
2683
2684         } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2685
2686                 const char *path, *interface, *property = "Id";
2687                 DBusMessageIter iter, sub;
2688
2689                 path = dbus_message_get_path(message);
2690
2691                 if (!dbus_message_get_args(message, &error,
2692                                           DBUS_TYPE_STRING, &interface,
2693                                           DBUS_TYPE_INVALID)) {
2694                         log_error("Failed to parse message: %s", bus_error_message(&error));
2695                         goto finish;
2696                 }
2697
2698                 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2699                     !streq(interface, "org.freedesktop.systemd1.Unit"))
2700                         goto finish;
2701
2702                 if (!(m = dbus_message_new_method_call(
2703                               "org.freedesktop.systemd1",
2704                               path,
2705                               "org.freedesktop.DBus.Properties",
2706                               "Get"))) {
2707                         log_error("Could not allocate message.");
2708                         goto oom;
2709                 }
2710
2711                 if (!dbus_message_append_args(m,
2712                                               DBUS_TYPE_STRING, &interface,
2713                                               DBUS_TYPE_STRING, &property,
2714                                               DBUS_TYPE_INVALID)) {
2715                         log_error("Could not append arguments to message.");
2716                         goto finish;
2717                 }
2718
2719                 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2720                         log_error("Failed to issue method call: %s", bus_error_message(&error));
2721                         goto finish;
2722                 }
2723
2724                 if (!dbus_message_iter_init(reply, &iter) ||
2725                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
2726                         log_error("Failed to parse reply.");
2727                         goto finish;
2728                 }
2729
2730                 dbus_message_iter_recurse(&iter, &sub);
2731
2732                 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2733                         const char *id;
2734
2735                         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
2736                                 log_error("Failed to parse reply.");
2737                                 goto finish;
2738                         }
2739
2740                         dbus_message_iter_get_basic(&sub, &id);
2741                         printf("Unit %s changed.\n", id);
2742                 } else {
2743                         uint32_t id;
2744
2745                         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32)  {
2746                                 log_error("Failed to parse reply.");
2747                                 goto finish;
2748                         }
2749
2750                         dbus_message_iter_get_basic(&sub, &id);
2751                         printf("Job %u changed.\n", id);
2752                 }
2753         }
2754
2755 finish:
2756         if (m)
2757                 dbus_message_unref(m);
2758
2759         if (reply)
2760                 dbus_message_unref(reply);
2761
2762         dbus_error_free(&error);
2763         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2764
2765 oom:
2766         if (m)
2767                 dbus_message_unref(m);
2768
2769         if (reply)
2770                 dbus_message_unref(reply);
2771
2772         dbus_error_free(&error);
2773         return DBUS_HANDLER_RESULT_NEED_MEMORY;
2774 }
2775
2776 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2777         DBusMessage *m = NULL, *reply = NULL;
2778         DBusError error;
2779         int r;
2780
2781         dbus_error_init(&error);
2782
2783         if (!private_bus) {
2784                 dbus_bus_add_match(bus,
2785                                    "type='signal',"
2786                                    "sender='org.freedesktop.systemd1',"
2787                                    "interface='org.freedesktop.systemd1.Manager',"
2788                                    "path='/org/freedesktop/systemd1'",
2789                                    &error);
2790
2791                 if (dbus_error_is_set(&error)) {
2792                         log_error("Failed to add match: %s", bus_error_message(&error));
2793                         r = -EIO;
2794                         goto finish;
2795                 }
2796
2797                 dbus_bus_add_match(bus,
2798                                    "type='signal',"
2799                                    "sender='org.freedesktop.systemd1',"
2800                                    "interface='org.freedesktop.DBus.Properties',"
2801                                    "member='PropertiesChanged'",
2802                                    &error);
2803
2804                 if (dbus_error_is_set(&error)) {
2805                         log_error("Failed to add match: %s", bus_error_message(&error));
2806                         r = -EIO;
2807                         goto finish;
2808                 }
2809         }
2810
2811         if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2812                 log_error("Failed to add filter.");
2813                 r = -ENOMEM;
2814                 goto finish;
2815         }
2816
2817         if (!(m = dbus_message_new_method_call(
2818                               "org.freedesktop.systemd1",
2819                               "/org/freedesktop/systemd1",
2820                               "org.freedesktop.systemd1.Manager",
2821                               "Subscribe"))) {
2822                 log_error("Could not allocate message.");
2823                 r = -ENOMEM;
2824                 goto finish;
2825         }
2826
2827         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2828                 log_error("Failed to issue method call: %s", bus_error_message(&error));
2829                 r = -EIO;
2830                 goto finish;
2831         }
2832
2833         while (dbus_connection_read_write_dispatch(bus, -1))
2834                 ;
2835
2836         r = 0;
2837
2838 finish:
2839
2840         /* This is slightly dirty, since we don't undo the filter or the matches. */
2841
2842         if (m)
2843                 dbus_message_unref(m);
2844
2845         if (reply)
2846                 dbus_message_unref(reply);
2847
2848         dbus_error_free(&error);
2849
2850         return r;
2851 }
2852
2853 static int dump(DBusConnection *bus, char **args, unsigned n) {
2854         DBusMessage *m = NULL, *reply = NULL;
2855         DBusError error;
2856         int r;
2857         const char *text;
2858
2859         dbus_error_init(&error);
2860
2861         if (!(m = dbus_message_new_method_call(
2862                               "org.freedesktop.systemd1",
2863                               "/org/freedesktop/systemd1",
2864                               "org.freedesktop.systemd1.Manager",
2865                               "Dump"))) {
2866                 log_error("Could not allocate message.");
2867                 return -ENOMEM;
2868         }
2869
2870         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2871                 log_error("Failed to issue method call: %s", bus_error_message(&error));
2872                 r = -EIO;
2873                 goto finish;
2874         }
2875
2876         if (!dbus_message_get_args(reply, &error,
2877                                    DBUS_TYPE_STRING, &text,
2878                                    DBUS_TYPE_INVALID)) {
2879                 log_error("Failed to parse reply: %s", bus_error_message(&error));
2880                 r = -EIO;
2881                 goto finish;
2882         }
2883
2884         fputs(text, stdout);
2885
2886         r = 0;
2887
2888 finish:
2889         if (m)
2890                 dbus_message_unref(m);
2891
2892         if (reply)
2893                 dbus_message_unref(reply);
2894
2895         dbus_error_free(&error);
2896
2897         return r;
2898 }
2899
2900 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2901         DBusMessage *m = NULL, *reply = NULL;
2902         DBusError error;
2903         int r;
2904         const char *name = "", *path, *id;
2905         dbus_bool_t cleanup = FALSE;
2906         DBusMessageIter iter, sub;
2907         const char
2908                 *interface = "org.freedesktop.systemd1.Unit",
2909                 *property = "Id";
2910
2911         dbus_error_init(&error);
2912
2913         if (!(m = dbus_message_new_method_call(
2914                               "org.freedesktop.systemd1",
2915                               "/org/freedesktop/systemd1",
2916                               "org.freedesktop.systemd1.Manager",
2917                               "CreateSnapshot"))) {
2918                 log_error("Could not allocate message.");
2919                 return -ENOMEM;
2920         }
2921
2922         if (n > 1)
2923                 name = args[1];
2924
2925         if (!dbus_message_append_args(m,
2926                                       DBUS_TYPE_STRING, &name,
2927                                       DBUS_TYPE_BOOLEAN, &cleanup,
2928                                       DBUS_TYPE_INVALID)) {
2929                 log_error("Could not append arguments to message.");
2930                 r = -ENOMEM;
2931                 goto finish;
2932         }
2933
2934         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2935                 log_error("Failed to issue method call: %s", bus_error_message(&error));
2936                 r = -EIO;
2937                 goto finish;
2938         }
2939
2940         if (!dbus_message_get_args(reply, &error,
2941                                    DBUS_TYPE_OBJECT_PATH, &path,
2942                                    DBUS_TYPE_INVALID)) {
2943                 log_error("Failed to parse reply: %s", bus_error_message(&error));
2944                 r = -EIO;
2945                 goto finish;
2946         }
2947
2948         dbus_message_unref(m);
2949         if (!(m = dbus_message_new_method_call(
2950                               "org.freedesktop.systemd1",
2951                               path,
2952                               "org.freedesktop.DBus.Properties",
2953                               "Get"))) {
2954                 log_error("Could not allocate message.");
2955                 return -ENOMEM;
2956         }
2957
2958         if (!dbus_message_append_args(m,
2959                                       DBUS_TYPE_STRING, &interface,
2960                                       DBUS_TYPE_STRING, &property,
2961                                       DBUS_TYPE_INVALID)) {
2962                 log_error("Could not append arguments to message.");
2963                 r = -ENOMEM;
2964                 goto finish;
2965         }
2966
2967         dbus_message_unref(reply);
2968         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2969                 log_error("Failed to issue method call: %s", bus_error_message(&error));
2970                 r = -EIO;
2971                 goto finish;
2972         }
2973
2974         if (!dbus_message_iter_init(reply, &iter) ||
2975             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
2976                 log_error("Failed to parse reply.");
2977                 r = -EIO;
2978                 goto finish;
2979         }
2980
2981         dbus_message_iter_recurse(&iter, &sub);
2982
2983         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
2984                 log_error("Failed to parse reply.");
2985                 r = -EIO;
2986                 goto finish;
2987         }
2988
2989         dbus_message_iter_get_basic(&sub, &id);
2990
2991         if (!arg_quiet)
2992                 puts(id);
2993         r = 0;
2994
2995 finish:
2996         if (m)
2997                 dbus_message_unref(m);
2998
2999         if (reply)
3000                 dbus_message_unref(reply);
3001
3002         dbus_error_free(&error);
3003
3004         return r;
3005 }
3006
3007 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
3008         DBusMessage *m = NULL, *reply = NULL;
3009         int r;
3010         DBusError error;
3011         unsigned i;
3012
3013         assert(bus);
3014         assert(args);
3015
3016         dbus_error_init(&error);
3017
3018         for (i = 1; i < n; i++) {
3019                 const char *path = NULL;
3020
3021                 if (!(m = dbus_message_new_method_call(
3022                                       "org.freedesktop.systemd1",
3023                                       "/org/freedesktop/systemd1",
3024                                       "org.freedesktop.systemd1.Manager",
3025                                       "GetUnit"))) {
3026                         log_error("Could not allocate message.");
3027                         r = -ENOMEM;
3028                         goto finish;
3029                 }
3030
3031                 if (!dbus_message_append_args(m,
3032                                               DBUS_TYPE_STRING, &args[i],
3033                                               DBUS_TYPE_INVALID)) {
3034                         log_error("Could not append arguments to message.");
3035                         r = -ENOMEM;
3036                         goto finish;
3037                 }
3038
3039                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3040                         log_error("Failed to issue method call: %s", bus_error_message(&error));
3041                         r = -EIO;
3042                         goto finish;
3043                 }
3044
3045                 if (!dbus_message_get_args(reply, &error,
3046                                            DBUS_TYPE_OBJECT_PATH, &path,
3047                                            DBUS_TYPE_INVALID)) {
3048                         log_error("Failed to parse reply: %s", bus_error_message(&error));
3049                         r = -EIO;
3050                         goto finish;
3051                 }
3052
3053                 dbus_message_unref(m);
3054                 if (!(m = dbus_message_new_method_call(
3055                                       "org.freedesktop.systemd1",
3056                                       path,
3057                                       "org.freedesktop.systemd1.Snapshot",
3058                                       "Remove"))) {
3059                         log_error("Could not allocate message.");
3060                         r = -ENOMEM;
3061                         goto finish;
3062                 }
3063
3064                 dbus_message_unref(reply);
3065                 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3066                         log_error("Failed to issue method call: %s", bus_error_message(&error));
3067                         r = -EIO;
3068                         goto finish;
3069                 }
3070
3071                 dbus_message_unref(m);
3072                 dbus_message_unref(reply);
3073                 m = reply = NULL;
3074         }
3075
3076         r = 0;
3077
3078 finish:
3079         if (m)
3080                 dbus_message_unref(m);
3081
3082         if (reply)
3083                 dbus_message_unref(reply);
3084
3085         dbus_error_free(&error);
3086
3087         return r;
3088 }
3089
3090 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
3091         DBusMessage *m = NULL, *reply = NULL;
3092         DBusError error;
3093         int r;
3094         const char *method;
3095
3096         dbus_error_init(&error);
3097
3098         if (arg_action == ACTION_RELOAD)
3099                 method = "Reload";
3100         else if (arg_action == ACTION_REEXEC)
3101                 method = "Reexecute";
3102         else {
3103                 assert(arg_action == ACTION_SYSTEMCTL);
3104
3105                 method =
3106                         streq(args[0], "clear-jobs")    ||
3107                         streq(args[0], "cancel")        ? "ClearJobs" :
3108                         streq(args[0], "daemon-reexec") ? "Reexecute" :
3109                         streq(args[0], "reset-failed")  ? "ResetFailed" :
3110                         streq(args[0], "halt")          ? "Halt" :
3111                         streq(args[0], "poweroff")      ? "PowerOff" :
3112                         streq(args[0], "reboot")        ? "Reboot" :
3113                         streq(args[0], "kexec")         ? "KExec" :
3114                         streq(args[0], "exit")          ? "Exit" :
3115                                     /* "daemon-reload" */ "Reload";
3116         }
3117
3118         if (!(m = dbus_message_new_method_call(
3119                               "org.freedesktop.systemd1",
3120                               "/org/freedesktop/systemd1",
3121                               "org.freedesktop.systemd1.Manager",
3122                               method))) {
3123                 log_error("Could not allocate message.");
3124                 return -ENOMEM;
3125         }
3126
3127         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3128
3129                 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3130                         /* There's always a fallback possible for
3131                          * legacy actions. */
3132                         r = -EADDRNOTAVAIL;
3133                         goto finish;
3134                 }
3135
3136                 log_error("Failed to issue method call: %s", bus_error_message(&error));
3137                 r = -EIO;
3138                 goto finish;
3139         }
3140
3141         r = 0;
3142
3143 finish:
3144         if (m)
3145                 dbus_message_unref(m);
3146