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