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