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