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