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