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