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