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