chiark / gitweb /
0aeae91b35dd7b69f6c98b35cb43fa6d77b190bb
[elogind.git] / src / systemctl / systemctl.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/reboot.h>
23 #include <stdio.h>
24 #include <getopt.h>
25 #include <locale.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/ioctl.h>
30 #include <termios.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <stddef.h>
36 #include <sys/prctl.h>
37 #include <dbus/dbus.h>
38
39 #include <systemd/sd-daemon.h>
40 #include <systemd/sd-shutdown.h>
41 #include <systemd/sd-login.h>
42
43 #include "log.h"
44 #include "util.h"
45 #include "macro.h"
46 #include "set.h"
47 #include "utmp-wtmp.h"
48 #include "special.h"
49 #include "initreq.h"
50 #include "path-util.h"
51 #include "strv.h"
52 #include "dbus-common.h"
53 #include "cgroup-show.h"
54 #include "cgroup-util.h"
55 #include "list.h"
56 #include "path-lookup.h"
57 #include "conf-parser.h"
58 #include "exit-status.h"
59 #include "bus-errors.h"
60 #include "build.h"
61 #include "unit-name.h"
62 #include "pager.h"
63 #include "spawn-ask-password-agent.h"
64 #include "spawn-polkit-agent.h"
65 #include "install.h"
66 #include "logs-show.h"
67 #include "path-util.h"
68 #include "socket-util.h"
69 #include "fileio.h"
70
71 static char **arg_types = NULL;
72 static char **arg_states = NULL;
73 static char **arg_properties = NULL;
74 static bool arg_all = false;
75 static enum dependency {
76         DEPENDENCY_FORWARD,
77         DEPENDENCY_REVERSE,
78         DEPENDENCY_AFTER,
79         DEPENDENCY_BEFORE,
80 } arg_dependency = DEPENDENCY_FORWARD;
81 static const char *arg_job_mode = "replace";
82 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
83 static bool arg_no_block = false;
84 static bool arg_no_legend = false;
85 static bool arg_no_pager = false;
86 static bool arg_no_wtmp = false;
87 static bool arg_no_wall = false;
88 static bool arg_no_reload = false;
89 static bool arg_show_types = false;
90 static bool arg_ignore_inhibitors = false;
91 static bool arg_dry = false;
92 static bool arg_quiet = false;
93 static bool arg_full = false;
94 static int arg_force = 0;
95 static bool arg_ask_password = true;
96 static bool arg_runtime = false;
97 static char **arg_wall = NULL;
98 static const char *arg_kill_who = NULL;
99 static int arg_signal = SIGTERM;
100 static const char *arg_root = NULL;
101 static usec_t arg_when = 0;
102 static enum action {
103         ACTION_INVALID,
104         ACTION_SYSTEMCTL,
105         ACTION_HALT,
106         ACTION_POWEROFF,
107         ACTION_REBOOT,
108         ACTION_KEXEC,
109         ACTION_EXIT,
110         ACTION_SUSPEND,
111         ACTION_HIBERNATE,
112         ACTION_HYBRID_SLEEP,
113         ACTION_RUNLEVEL2,
114         ACTION_RUNLEVEL3,
115         ACTION_RUNLEVEL4,
116         ACTION_RUNLEVEL5,
117         ACTION_RESCUE,
118         ACTION_EMERGENCY,
119         ACTION_DEFAULT,
120         ACTION_RELOAD,
121         ACTION_REEXEC,
122         ACTION_RUNLEVEL,
123         ACTION_CANCEL_SHUTDOWN,
124         _ACTION_MAX
125 } arg_action = ACTION_SYSTEMCTL;
126 static enum transport {
127         TRANSPORT_NORMAL,
128         TRANSPORT_SSH,
129         TRANSPORT_POLKIT
130 } arg_transport = TRANSPORT_NORMAL;
131 static char *arg_host = NULL;
132 static char *arg_user = NULL;
133 static unsigned arg_lines = 10;
134 static OutputMode arg_output = OUTPUT_SHORT;
135 static bool arg_plain = false;
136
137 static bool private_bus = false;
138
139 static int daemon_reload(DBusConnection *bus, char **args);
140 static void halt_now(enum action a);
141
142 static void pager_open_if_enabled(void) {
143
144         if (arg_no_pager)
145                 return;
146
147         pager_open(false);
148 }
149
150 static void ask_password_agent_open_if_enabled(void) {
151
152         /* Open the password agent as a child process if necessary */
153
154         if (!arg_ask_password)
155                 return;
156
157         if (arg_scope != UNIT_FILE_SYSTEM)
158                 return;
159
160         ask_password_agent_open();
161 }
162
163 #ifdef HAVE_LOGIND
164 static void polkit_agent_open_if_enabled(void) {
165
166         /* Open the polkit agent as a child process if necessary */
167
168         if (!arg_ask_password)
169                 return;
170
171         if (arg_scope != UNIT_FILE_SYSTEM)
172                 return;
173
174         polkit_agent_open();
175 }
176 #endif
177
178 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
179         assert(error);
180
181         if (!dbus_error_is_set(error))
182                 return r;
183
184         if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
185             dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
186             dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
187             dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
188                 return EXIT_NOPERMISSION;
189
190         if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
191                 return EXIT_NOTINSTALLED;
192
193         if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
194             dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
195                 return EXIT_NOTIMPLEMENTED;
196
197         if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
198                 return EXIT_NOTCONFIGURED;
199
200         if (r != 0)
201                 return r;
202
203         return EXIT_FAILURE;
204 }
205
206 static void warn_wall(enum action a) {
207         static const char *table[_ACTION_MAX] = {
208                 [ACTION_HALT]            = "The system is going down for system halt NOW!",
209                 [ACTION_REBOOT]          = "The system is going down for reboot NOW!",
210                 [ACTION_POWEROFF]        = "The system is going down for power-off NOW!",
211                 [ACTION_KEXEC]           = "The system is going down for kexec reboot NOW!",
212                 [ACTION_RESCUE]          = "The system is going down to rescue mode NOW!",
213                 [ACTION_EMERGENCY]       = "The system is going down to emergency mode NOW!",
214                 [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
215         };
216
217         if (arg_no_wall)
218                 return;
219
220         if (arg_wall) {
221                 _cleanup_free_ char *p;
222
223                 p = strv_join(arg_wall, " ");
224                 if (!p) {
225                         log_oom();
226                         return;
227                 }
228
229                 if (*p) {
230                         utmp_wall(p, NULL);
231                         return;
232                 }
233         }
234
235         if (!table[a])
236                 return;
237
238         utmp_wall(table[a], NULL);
239 }
240
241 static bool avoid_bus(void) {
242
243         if (running_in_chroot() > 0)
244                 return true;
245
246         if (sd_booted() <= 0)
247                 return true;
248
249         if (!isempty(arg_root))
250                 return true;
251
252         if (arg_scope == UNIT_FILE_GLOBAL)
253                 return true;
254
255         return false;
256 }
257
258 static int compare_unit_info(const void *a, const void *b) {
259         const char *d1, *d2;
260         const struct unit_info *u = a, *v = b;
261
262         d1 = strrchr(u->id, '.');
263         d2 = strrchr(v->id, '.');
264
265         if (d1 && d2) {
266                 int r;
267
268                 r = strcasecmp(d1, d2);
269                 if (r != 0)
270                         return r;
271         }
272
273         return strcasecmp(u->id, v->id);
274 }
275
276 static bool output_show_unit(const struct unit_info *u) {
277         const char *dot;
278
279         if (!strv_isempty(arg_states))
280                 return strv_contains(arg_states, u->load_state) || strv_contains(arg_states, u->sub_state) || strv_contains(arg_states, u->active_state);
281
282         return (!arg_types || ((dot = strrchr(u->id, '.')) &&
283                                strv_find(arg_types, dot+1))) &&
284                 (arg_all || !(streq(u->active_state, "inactive")
285                               || u->following[0]) || u->job_id > 0);
286 }
287
288 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
289         unsigned id_len, max_id_len, active_len, sub_len, job_len, desc_len, n_shown = 0;
290         const struct unit_info *u;
291         int job_count = 0;
292
293         max_id_len = sizeof("UNIT")-1;
294         active_len = sizeof("ACTIVE")-1;
295         sub_len = sizeof("SUB")-1;
296         job_len = sizeof("JOB")-1;
297         desc_len = 0;
298
299         for (u = unit_infos; u < unit_infos + c; u++) {
300                 if (!output_show_unit(u))
301                         continue;
302
303                 max_id_len = MAX(max_id_len, strlen(u->id));
304                 active_len = MAX(active_len, strlen(u->active_state));
305                 sub_len = MAX(sub_len, strlen(u->sub_state));
306                 if (u->job_id != 0) {
307                         job_len = MAX(job_len, strlen(u->job_type));
308                         job_count++;
309                 }
310         }
311
312         if (!arg_full) {
313                 unsigned basic_len;
314                 id_len = MIN(max_id_len, 25u);
315                 basic_len = 5 + id_len + 5 + active_len + sub_len;
316                 if (job_count)
317                         basic_len += job_len + 1;
318                 if (basic_len < (unsigned) columns()) {
319                         unsigned extra_len, incr;
320                         extra_len = columns() - basic_len;
321                         /* Either UNIT already got 25, or is fully satisfied.
322                          * Grant up to 25 to DESC now. */
323                         incr = MIN(extra_len, 25u);
324                         desc_len += incr;
325                         extra_len -= incr;
326                         /* split the remaining space between UNIT and DESC,
327                          * but do not give UNIT more than it needs. */
328                         if (extra_len > 0) {
329                                 incr = MIN(extra_len / 2, max_id_len - id_len);
330                                 id_len += incr;
331                                 desc_len += extra_len - incr;
332                         }
333                 }
334         } else
335                 id_len = max_id_len;
336
337         for (u = unit_infos; u < unit_infos + c; u++) {
338                 _cleanup_free_ char *e = NULL;
339                 const char *on_loaded, *off_loaded, *on = "";
340                 const char *on_active, *off_active, *off = "";
341
342                 if (!output_show_unit(u))
343                         continue;
344
345                 if (!n_shown && !arg_no_legend) {
346                         printf("%-*s %-6s %-*s %-*s ", id_len, "UNIT", "LOAD",
347                                active_len, "ACTIVE", sub_len, "SUB");
348                         if (job_count)
349                                 printf("%-*s ", job_len, "JOB");
350                         if (!arg_full && arg_no_pager)
351                                 printf("%.*s\n", desc_len, "DESCRIPTION");
352                         else
353                                 printf("%s\n", "DESCRIPTION");
354                 }
355
356                 n_shown++;
357
358                 if (streq(u->load_state, "error") ||
359                     streq(u->load_state, "not-found")) {
360                         on_loaded = on = ansi_highlight_red();
361                         off_loaded = off = ansi_highlight_off();
362                 } else
363                         on_loaded = off_loaded = "";
364
365                 if (streq(u->active_state, "failed")) {
366                         on_active = on = ansi_highlight_red();
367                         off_active = off = ansi_highlight_off();
368                 } else
369                         on_active = off_active = "";
370
371                 e = arg_full ? NULL : ellipsize(u->id, id_len, 33);
372
373                 printf("%s%-*s%s %s%-6s%s %s%-*s %-*s%s %-*s",
374                        on, id_len, e ? e : u->id, off,
375                        on_loaded, u->load_state, off_loaded,
376                        on_active, active_len, u->active_state,
377                        sub_len, u->sub_state, off_active,
378                        job_count ? job_len + 1 : 0, u->job_id ? u->job_type : "");
379                 if (!arg_full && arg_no_pager)
380                         printf("%.*s\n", desc_len, u->description);
381                 else
382                         printf("%s\n", u->description);
383         }
384
385         if (!arg_no_legend) {
386                 const char *on, *off;
387
388                 if (n_shown) {
389                         printf("\nLOAD   = Reflects whether the unit definition was properly loaded.\n"
390                                "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
391                                "SUB    = The low-level unit activation state, values depend on unit type.\n");
392                         if (job_count)
393                                 printf("JOB    = Pending job for the unit.\n");
394                         puts("");
395                         on = ansi_highlight();
396                         off = ansi_highlight_off();
397                 } else {
398                         on = ansi_highlight_red();
399                         off = ansi_highlight_off();
400                 }
401
402                 if (arg_all)
403                         printf("%s%u loaded units listed.%s\n"
404                                "To show all installed unit files use 'systemctl list-unit-files'.\n",
405                                on, n_shown, off);
406                 else
407                         printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
408                                "To show all installed unit files use 'systemctl list-unit-files'.\n",
409                                on, n_shown, off);
410         }
411 }
412
413 static int get_unit_list(
414                 DBusConnection *bus,
415                 DBusMessage **reply,
416                 struct unit_info **unit_infos,
417                 unsigned *c) {
418
419         DBusMessageIter iter, sub;
420         size_t size = 0;
421         int r;
422
423         assert(bus);
424         assert(unit_infos);
425         assert(c);
426
427         r = bus_method_call_with_reply(
428                         bus,
429                         "org.freedesktop.systemd1",
430                         "/org/freedesktop/systemd1",
431                         "org.freedesktop.systemd1.Manager",
432                         "ListUnits",
433                         reply,
434                         NULL,
435                         DBUS_TYPE_INVALID);
436         if (r < 0)
437                 return r;
438
439         if (!dbus_message_iter_init(*reply, &iter) ||
440             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
441             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
442                 log_error("Failed to parse reply.");
443                 return -EIO;
444         }
445
446         dbus_message_iter_recurse(&iter, &sub);
447
448         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
449                 if (!GREEDY_REALLOC(*unit_infos, size, *c + 1))
450                         return log_oom();
451
452                 bus_parse_unit_info(&sub, *unit_infos + *c);
453                 (*c)++;
454
455                 dbus_message_iter_next(&sub);
456         }
457
458         return 0;
459 }
460
461 static int list_units(DBusConnection *bus, char **args) {
462         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
463         _cleanup_free_ struct unit_info *unit_infos = NULL;
464         unsigned c = 0;
465         int r;
466
467         pager_open_if_enabled();
468
469         r = get_unit_list(bus, &reply, &unit_infos, &c);
470         if (r < 0)
471                 return r;
472
473         qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
474
475         output_units_list(unit_infos, c);
476
477         return 0;
478 }
479
480 static int get_triggered_units(
481                 DBusConnection *bus,
482                 const char* unit_path,
483                 char*** triggered) {
484
485         const char *interface = "org.freedesktop.systemd1.Unit",
486                    *triggers_property = "Triggers";
487         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
488         DBusMessageIter iter, sub;
489         int r;
490
491         r = bus_method_call_with_reply(bus,
492                                        "org.freedesktop.systemd1",
493                                        unit_path,
494                                        "org.freedesktop.DBus.Properties",
495                                        "Get",
496                                        &reply,
497                                        NULL,
498                                        DBUS_TYPE_STRING, &interface,
499                                        DBUS_TYPE_STRING, &triggers_property,
500                                        DBUS_TYPE_INVALID);
501         if (r < 0)
502                 return r;
503
504         if (!dbus_message_iter_init(reply, &iter) ||
505             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
506                 log_error("Failed to parse reply.");
507                 return -EBADMSG;
508         }
509
510         dbus_message_iter_recurse(&iter, &sub);
511         dbus_message_iter_recurse(&sub, &iter);
512         sub = iter;
513
514         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
515                 const char *unit;
516
517                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
518                         log_error("Failed to parse reply.");
519                         return -EBADMSG;
520                 }
521
522                 dbus_message_iter_get_basic(&sub, &unit);
523                 r = strv_extend(triggered, unit);
524                 if (r < 0)
525                         return r;
526
527                 dbus_message_iter_next(&sub);
528         }
529
530         return 0;
531 }
532
533 static int get_listening(DBusConnection *bus, const char* unit_path,
534                          char*** listen, unsigned *c)
535 {
536         const char *interface = "org.freedesktop.systemd1.Socket",
537                    *listen_property = "Listen";
538         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
539         DBusMessageIter iter, sub;
540         int r;
541
542         r = bus_method_call_with_reply(bus,
543                                        "org.freedesktop.systemd1",
544                                        unit_path,
545                                        "org.freedesktop.DBus.Properties",
546                                        "Get",
547                                        &reply,
548                                        NULL,
549                                        DBUS_TYPE_STRING, &interface,
550                                        DBUS_TYPE_STRING, &listen_property,
551                                        DBUS_TYPE_INVALID);
552         if (r < 0)
553                 return r;
554
555         if (!dbus_message_iter_init(reply, &iter) ||
556             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
557                 log_error("Failed to parse reply.");
558                 return -EBADMSG;
559         }
560
561         dbus_message_iter_recurse(&iter, &sub);
562         dbus_message_iter_recurse(&sub, &iter);
563         sub = iter;
564
565         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
566                 DBusMessageIter sub2;
567                 const char *type, *path;
568
569                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
570                         log_error("Failed to parse reply.");
571                         return -EBADMSG;
572                 }
573
574                 dbus_message_iter_recurse(&sub, &sub2);
575
576                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
577                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0) {
578                         r = strv_extend(listen, type);
579                         if (r < 0)
580                                 return r;
581
582                         r = strv_extend(listen, path);
583                         if (r < 0)
584                                 return r;
585
586                         (*c) ++;
587                 }
588
589                 dbus_message_iter_next(&sub);
590         }
591
592         return 0;
593 }
594
595 struct socket_info {
596         const char* id;
597
598         char* type;
599         char* path;
600
601         /* Note: triggered is a list here, although it almost certainly
602          * will always be one unit. Nevertheless, dbus API allows for multiple
603          * values, so let's follow that.*/
604         char** triggered;
605
606         /* The strv above is shared. free is set only in the first one. */
607         bool own_triggered;
608 };
609
610 static int socket_info_compare(struct socket_info *a, struct socket_info *b) {
611         int o = strcmp(a->path, b->path);
612         if (o == 0)
613                 o = strcmp(a->type, b->type);
614         return o;
615 }
616
617 static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
618         struct socket_info *s;
619         unsigned pathlen = sizeof("LISTEN") - 1,
620                 typelen = (sizeof("TYPE") - 1) * arg_show_types,
621                 socklen = sizeof("UNIT") - 1,
622                 servlen = sizeof("ACTIVATES") - 1;
623         const char *on, *off;
624
625         for (s = socket_infos; s < socket_infos + cs; s++) {
626                 char **a;
627                 unsigned tmp = 0;
628
629                 socklen = MAX(socklen, strlen(s->id));
630                 if (arg_show_types)
631                         typelen = MAX(typelen, strlen(s->type));
632                 pathlen = MAX(pathlen, strlen(s->path));
633
634                 STRV_FOREACH(a, s->triggered)
635                         tmp += strlen(*a) + 2*(a != s->triggered);
636                 servlen = MAX(servlen, tmp);
637         }
638
639         if (cs) {
640                 if (!arg_no_legend)
641                         printf("%-*s %-*.*s%-*s %s\n",
642                                pathlen, "LISTEN",
643                                typelen + arg_show_types, typelen + arg_show_types, "TYPE ",
644                                socklen, "UNIT",
645                                "ACTIVATES");
646
647                 for (s = socket_infos; s < socket_infos + cs; s++) {
648                         char **a;
649
650                         if (arg_show_types)
651                                 printf("%-*s %-*s %-*s",
652                                        pathlen, s->path, typelen, s->type, socklen, s->id);
653                         else
654                                 printf("%-*s %-*s",
655                                        pathlen, s->path, socklen, s->id);
656                         STRV_FOREACH(a, s->triggered)
657                                 printf("%s %s",
658                                        a == s->triggered ? "" : ",", *a);
659                         printf("\n");
660                 }
661
662                 on = ansi_highlight();
663                 off = ansi_highlight_off();
664                 if (!arg_no_legend)
665                         printf("\n");
666         } else {
667                 on = ansi_highlight_red();
668                 off = ansi_highlight_off();
669         }
670
671         if (!arg_no_legend) {
672                 printf("%s%u sockets listed.%s\n", on, cs, off);
673                 if (!arg_all)
674                         printf("Pass --all to see loaded but inactive sockets, too.\n");
675         }
676
677         return 0;
678 }
679
680 static int list_sockets(DBusConnection *bus, char **args) {
681         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
682         _cleanup_free_ struct unit_info *unit_infos = NULL;
683         struct socket_info *socket_infos = NULL;
684         const struct unit_info *u;
685         struct socket_info *s;
686         unsigned cu = 0, cs = 0;
687         size_t size = 0;
688         int r;
689
690         pager_open_if_enabled();
691
692         r = get_unit_list(bus, &reply, &unit_infos, &cu);
693         if (r < 0)
694                 return r;
695
696         for (u = unit_infos; u < unit_infos + cu; u++) {
697                 const char *dot;
698                 _cleanup_strv_free_ char **listen = NULL, **triggered = NULL;
699                 unsigned c = 0, i;
700
701                 if (!output_show_unit(u))
702                         continue;
703
704                 if ((dot = strrchr(u->id, '.')) && !streq(dot+1, "socket"))
705                         continue;
706
707                 r = get_triggered_units(bus, u->unit_path, &triggered);
708                 if (r < 0)
709                         goto cleanup;
710
711                 r = get_listening(bus, u->unit_path, &listen, &c);
712                 if (r < 0)
713                         goto cleanup;
714
715                 if (!GREEDY_REALLOC(socket_infos, size, cs + c)) {
716                         r = log_oom();
717                         goto cleanup;
718                 }
719
720                 for (i = 0; i < c; i++)
721                         socket_infos[cs + i] = (struct socket_info) {
722                                 .id = u->id,
723                                 .type = listen[i*2],
724                                 .path = listen[i*2 + 1],
725                                 .triggered = triggered,
726                                 .own_triggered = i==0,
727                         };
728
729                 /* from this point on we will cleanup those socket_infos */
730                 cs += c;
731                 free(listen);
732                 listen = triggered = NULL; /* avoid cleanup */
733         }
734
735         qsort(socket_infos, cs, sizeof(struct socket_info),
736               (__compar_fn_t) socket_info_compare);
737
738         output_sockets_list(socket_infos, cs);
739
740  cleanup:
741         assert(cs == 0 || socket_infos);
742         for (s = socket_infos; s < socket_infos + cs; s++) {
743                 free(s->type);
744                 free(s->path);
745                 if (s->own_triggered)
746                         strv_free(s->triggered);
747         }
748         free(socket_infos);
749
750         return 0;
751 }
752
753 static int compare_unit_file_list(const void *a, const void *b) {
754         const char *d1, *d2;
755         const UnitFileList *u = a, *v = b;
756
757         d1 = strrchr(u->path, '.');
758         d2 = strrchr(v->path, '.');
759
760         if (d1 && d2) {
761                 int r;
762
763                 r = strcasecmp(d1, d2);
764                 if (r != 0)
765                         return r;
766         }
767
768         return strcasecmp(path_get_file_name(u->path), path_get_file_name(v->path));
769 }
770
771 static bool output_show_unit_file(const UnitFileList *u) {
772         const char *dot;
773
774         return !arg_types || ((dot = strrchr(u->path, '.')) && strv_find(arg_types, dot+1));
775 }
776
777 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
778         unsigned max_id_len, id_cols, state_cols, n_shown = 0;
779         const UnitFileList *u;
780
781         max_id_len = sizeof("UNIT FILE")-1;
782         state_cols = sizeof("STATE")-1;
783         for (u = units; u < units + c; u++) {
784                 if (!output_show_unit_file(u))
785                         continue;
786
787                 max_id_len = MAX(max_id_len, strlen(path_get_file_name(u->path)));
788                 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
789         }
790
791         if (!arg_full) {
792                 unsigned basic_cols;
793                 id_cols = MIN(max_id_len, 25u);
794                 basic_cols = 1 + id_cols + state_cols;
795                 if (basic_cols < (unsigned) columns())
796                         id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
797         } else
798                 id_cols = max_id_len;
799
800         if (!arg_no_legend)
801                 printf("%-*s %-*s\n", id_cols, "UNIT FILE", state_cols, "STATE");
802
803         for (u = units; u < units + c; u++) {
804                 _cleanup_free_ char *e = NULL;
805                 const char *on, *off;
806                 const char *id;
807
808                 if (!output_show_unit_file(u))
809                         continue;
810
811                 n_shown++;
812
813                 if (u->state == UNIT_FILE_MASKED ||
814                     u->state == UNIT_FILE_MASKED_RUNTIME ||
815                     u->state == UNIT_FILE_DISABLED ||
816                     u->state == UNIT_FILE_INVALID) {
817                         on  = ansi_highlight_red();
818                         off = ansi_highlight_off();
819                 } else if (u->state == UNIT_FILE_ENABLED) {
820                         on  = ansi_highlight_green();
821                         off = ansi_highlight_off();
822                 } else
823                         on = off = "";
824
825                 id = path_get_file_name(u->path);
826
827                 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
828
829                 printf("%-*s %s%-*s%s\n",
830                        id_cols, e ? e : id,
831                        on, state_cols, unit_file_state_to_string(u->state), off);
832         }
833
834         if (!arg_no_legend)
835                 printf("\n%u unit files listed.\n", n_shown);
836 }
837
838 static int list_unit_files(DBusConnection *bus, char **args) {
839         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
840         _cleanup_free_ UnitFileList *units = NULL;
841         DBusMessageIter iter, sub, sub2;
842         unsigned c = 0, n_units = 0;
843         int r;
844
845         pager_open_if_enabled();
846
847         if (avoid_bus()) {
848                 Hashmap *h;
849                 UnitFileList *u;
850                 Iterator i;
851
852                 h = hashmap_new(string_hash_func, string_compare_func);
853                 if (!h)
854                         return log_oom();
855
856                 r = unit_file_get_list(arg_scope, arg_root, h);
857                 if (r < 0) {
858                         unit_file_list_free(h);
859                         log_error("Failed to get unit file list: %s", strerror(-r));
860                         return r;
861                 }
862
863                 n_units = hashmap_size(h);
864                 units = new(UnitFileList, n_units);
865                 if (!units) {
866                         unit_file_list_free(h);
867                         return log_oom();
868                 }
869
870                 HASHMAP_FOREACH(u, h, i) {
871                         memcpy(units + c++, u, sizeof(UnitFileList));
872                         free(u);
873                 }
874
875                 hashmap_free(h);
876         } else {
877                 r = bus_method_call_with_reply(
878                                 bus,
879                                 "org.freedesktop.systemd1",
880                                 "/org/freedesktop/systemd1",
881                                 "org.freedesktop.systemd1.Manager",
882                                 "ListUnitFiles",
883                                 &reply,
884                                 NULL,
885                                 DBUS_TYPE_INVALID);
886                 if (r < 0)
887                         return r;
888
889                 if (!dbus_message_iter_init(reply, &iter) ||
890                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
891                     dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
892                         log_error("Failed to parse reply.");
893                         return -EIO;
894                 }
895
896                 dbus_message_iter_recurse(&iter, &sub);
897
898                 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
899                         UnitFileList *u;
900                         const char *state;
901
902                         assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT);
903
904                         if (c >= n_units) {
905                                 UnitFileList *w;
906
907                                 n_units = MAX(2*c, 16u);
908                                 w = realloc(units, sizeof(struct UnitFileList) * n_units);
909                                 if (!w)
910                                         return log_oom();
911
912                                 units = w;
913                         }
914
915                         u = units + c;
916
917                         dbus_message_iter_recurse(&sub, &sub2);
918
919                         if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
920                             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
921                                 log_error("Failed to parse reply.");
922                                 return -EIO;
923                         }
924
925                         u->state = unit_file_state_from_string(state);
926
927                         dbus_message_iter_next(&sub);
928                         c++;
929                 }
930         }
931
932         if (c > 0) {
933                 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
934                 output_unit_file_list(units, c);
935         }
936
937         return 0;
938 }
939
940 static int list_dependencies_print(const char *name, int level, unsigned int branches, bool last) {
941         int i;
942         _cleanup_free_ char *n = NULL;
943         size_t len = 0;
944         size_t max_len = MAX(columns(),20u);
945
946         if (!arg_plain) {
947                 for (i = level - 1; i >= 0; i--) {
948                         len += 2;
949                         if(len > max_len - 3 && !arg_full) {
950                                 printf("%s...\n",max_len % 2 ? "" : " ");
951                                 return 0;
952                         }
953                         printf("%s", draw_special_char(branches & (1 << i) ? DRAW_TREE_VERT : DRAW_TREE_SPACE));
954                 }
955                 len += 2;
956                 if(len > max_len - 3 && !arg_full) {
957                         printf("%s...\n",max_len % 2 ? "" : " ");
958                         return 0;
959                 }
960                 printf("%s", draw_special_char(last ? DRAW_TREE_RIGHT : DRAW_TREE_BRANCH));
961         }
962
963         if(arg_full){
964                 printf("%s\n", name);
965                 return 0;
966         }
967
968         n = ellipsize(name, max_len-len, 100);
969         if(!n)
970                 return log_oom();
971
972         printf("%s\n", n);
973         return 0;
974 }
975
976 static int list_dependencies_get_dependencies(DBusConnection *bus, const char *name, char ***deps) {
977         static const char *dependencies[] = {
978                 [DEPENDENCY_FORWARD] = "Requires\0"
979                                        "RequiresOverridable\0"
980                                        "Requisite\0"
981                                        "RequisiteOverridable\0"
982                                        "Wants\0",
983                 [DEPENDENCY_REVERSE] = "RequiredBy\0"
984                                        "RequiredByOverridable\0"
985                                        "WantedBy\0"
986                                        "PartOf\0",
987                 [DEPENDENCY_AFTER]   = "After\0",
988                 [DEPENDENCY_BEFORE]  = "Before\0",
989         };
990
991         _cleanup_free_ char *path;
992         const char *interface = "org.freedesktop.systemd1.Unit";
993
994         _cleanup_dbus_message_unref_  DBusMessage *reply = NULL;
995         DBusMessageIter iter, sub, sub2, sub3;
996
997         int r = 0;
998         char **ret = NULL;
999
1000         assert(bus);
1001         assert(name);
1002         assert(deps);
1003
1004         path = unit_dbus_path_from_name(name);
1005         if (path == NULL) {
1006                 r = -EINVAL;
1007                 goto finish;
1008         }
1009
1010         r = bus_method_call_with_reply(
1011                 bus,
1012                 "org.freedesktop.systemd1",
1013                 path,
1014                 "org.freedesktop.DBus.Properties",
1015                 "GetAll",
1016                 &reply,
1017                 NULL,
1018                 DBUS_TYPE_STRING, &interface,
1019                 DBUS_TYPE_INVALID);
1020         if (r < 0)
1021                 goto finish;
1022
1023         if (!dbus_message_iter_init(reply, &iter) ||
1024                 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1025                 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1026                 log_error("Failed to parse reply.");
1027                 r = -EIO;
1028                 goto finish;
1029         }
1030
1031         dbus_message_iter_recurse(&iter, &sub);
1032
1033         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1034                 const char *prop;
1035
1036                 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_DICT_ENTRY);
1037                 dbus_message_iter_recurse(&sub, &sub2);
1038
1039                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
1040                         log_error("Failed to parse reply.");
1041                         r = -EIO;
1042                         goto finish;
1043                 }
1044
1045                 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1046                         log_error("Failed to parse reply.");
1047                         r = -EIO;
1048                         goto finish;
1049                 }
1050
1051                 dbus_message_iter_recurse(&sub2, &sub3);
1052                 dbus_message_iter_next(&sub);
1053
1054                 assert(arg_dependency < ELEMENTSOF(dependencies));
1055                 if (!nulstr_contains(dependencies[arg_dependency], prop))
1056                         continue;
1057
1058                 if (dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_ARRAY) {
1059                         if (dbus_message_iter_get_element_type(&sub3) == DBUS_TYPE_STRING) {
1060                                 DBusMessageIter sub4;
1061                                 dbus_message_iter_recurse(&sub3, &sub4);
1062
1063                                 while (dbus_message_iter_get_arg_type(&sub4) != DBUS_TYPE_INVALID) {
1064                                         const char *s;
1065
1066                                         assert(dbus_message_iter_get_arg_type(&sub4) == DBUS_TYPE_STRING);
1067                                         dbus_message_iter_get_basic(&sub4, &s);
1068
1069                                         r = strv_extend(&ret, s);
1070                                         if (r < 0) {
1071                                                 log_oom();
1072                                                 goto finish;
1073                                         }
1074
1075                                         dbus_message_iter_next(&sub4);
1076                                 }
1077                         }
1078                 }
1079         }
1080 finish:
1081         if (r < 0)
1082                 strv_free(ret);
1083         else
1084                 *deps = ret;
1085         return r;
1086 }
1087
1088 static int list_dependencies_compare(const void *_a, const void *_b) {
1089         const char **a = (const char**) _a, **b = (const char**) _b;
1090         if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET)
1091                 return 1;
1092         if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET)
1093                 return -1;
1094         return strcasecmp(*a, *b);
1095 }
1096
1097 static int list_dependencies_one(DBusConnection *bus, const char *name, int level, char ***units, unsigned int branches) {
1098         _cleanup_strv_free_ char **deps = NULL, **u;
1099         char **c;
1100         int r = 0;
1101
1102         u = strv_append(*units, name);
1103         if (!u)
1104                 return log_oom();
1105
1106         r = list_dependencies_get_dependencies(bus, name, &deps);
1107         if (r < 0)
1108                 return r;
1109
1110         qsort(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
1111
1112         STRV_FOREACH(c, deps) {
1113                 if (strv_contains(u, *c)) {
1114                         if (!arg_plain) {
1115                                 r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1);
1116                                 if (r < 0)
1117                                         return r;
1118                         }
1119                         continue;
1120                 }
1121
1122                 r = list_dependencies_print(*c, level, branches, c[1] == NULL);
1123                 if (r < 0)
1124                         return r;
1125
1126                 if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) {
1127                        r = list_dependencies_one(bus, *c, level + 1, &u, (branches << 1) | (c[1] == NULL ? 0 : 1));
1128                        if(r < 0)
1129                                return r;
1130                 }
1131         }
1132         if (arg_plain) {
1133                 strv_free(*units);
1134                 *units = u;
1135                 u = NULL;
1136         }
1137         return 0;
1138 }
1139
1140 static int list_dependencies(DBusConnection *bus, char **args) {
1141         _cleanup_free_ char *unit = NULL;
1142         _cleanup_strv_free_ char **units = NULL;
1143         const char *u;
1144
1145         assert(bus);
1146
1147         if (args[1]) {
1148                 unit = unit_name_mangle(args[1]);
1149                 if (!unit)
1150                         return log_oom();
1151                 u = unit;
1152         } else
1153                 u = SPECIAL_DEFAULT_TARGET;
1154
1155         pager_open_if_enabled();
1156
1157         puts(u);
1158
1159         return list_dependencies_one(bus, u, 0, &units, 0);
1160 }
1161
1162 static int get_default(DBusConnection *bus, char **args) {
1163         char *path = NULL;
1164         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
1165         int r;
1166         _cleanup_dbus_error_free_ DBusError error;
1167
1168         dbus_error_init(&error);
1169
1170         if (!bus || avoid_bus()) {
1171                 r = unit_file_get_default(arg_scope, arg_root, &path);
1172
1173                 if (r < 0) {
1174                         log_error("Operation failed: %s", strerror(-r));
1175                         goto finish;
1176                 }
1177
1178                 r = 0;
1179         } else {
1180                 r = bus_method_call_with_reply(
1181                         bus,
1182                         "org.freedesktop.systemd1",
1183                         "/org/freedesktop/systemd1",
1184                         "org.freedesktop.systemd1.Manager",
1185                         "GetDefaultTarget",
1186                         &reply,
1187                         NULL,
1188                         DBUS_TYPE_INVALID);
1189
1190                 if (r < 0) {
1191                         log_error("Operation failed: %s", strerror(-r));
1192                         goto finish;
1193                 }
1194
1195                 if (!dbus_message_get_args(reply, &error,
1196                                    DBUS_TYPE_STRING, &path,
1197                                    DBUS_TYPE_INVALID)) {
1198                         log_error("Failed to parse reply: %s", bus_error_message(&error));
1199                         dbus_error_free(&error);
1200                         return  -EIO;
1201                 }
1202         }
1203
1204         if (path)
1205                 printf("%s\n", path);
1206
1207 finish:
1208         if ((!bus || avoid_bus()) && path)
1209                 free(path);
1210
1211         return r;
1212
1213 }
1214
1215 struct job_info {
1216         uint32_t id;
1217         char *name, *type, *state;
1218 };
1219
1220 static void list_jobs_print(struct job_info* jobs, size_t n) {
1221         size_t i;
1222         struct job_info *j;
1223         const char *on, *off;
1224         bool shorten = false;
1225
1226         assert(n == 0 || jobs);
1227
1228         if (n == 0) {
1229                 on = ansi_highlight_green();
1230                 off = ansi_highlight_off();
1231
1232                 printf("%sNo jobs running.%s\n", on, off);
1233                 return;
1234         }
1235
1236         pager_open_if_enabled();
1237
1238         {
1239                 /* JOB UNIT TYPE STATE */
1240                 unsigned l0 = 3, l1 = 4, l2 = 4, l3 = 5;
1241
1242                 for (i = 0, j = jobs; i < n; i++, j++) {
1243                         assert(j->name && j->type && j->state);
1244                         l0 = MAX(l0, DECIMAL_STR_WIDTH(j->id));
1245                         l1 = MAX(l1, strlen(j->name));
1246                         l2 = MAX(l2, strlen(j->type));
1247                         l3 = MAX(l3, strlen(j->state));
1248                 }
1249
1250                 if (!arg_full && l0 + 1 + l1 + l2 + 1 + l3 > columns()) {
1251                         l1 = MAX(33u, columns() - l0 - l2 - l3 - 3);
1252                         shorten = true;
1253                 }
1254
1255                 if (on_tty())
1256                         printf("%*s %-*s %-*s %-*s\n",
1257                                l0, "JOB",
1258                                l1, "UNIT",
1259                                l2, "TYPE",
1260                                l3, "STATE");
1261
1262                 for (i = 0, j = jobs; i < n; i++, j++) {
1263                         _cleanup_free_ char *e = NULL;
1264
1265                         if (streq(j->state, "running")) {
1266                                 on = ansi_highlight();
1267                                 off = ansi_highlight_off();
1268                         } else
1269                                 on = off = "";
1270
1271                         e = shorten ? ellipsize(j->name, l1, 33) : NULL;
1272                         printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
1273                                l0, j->id,
1274                                on, l1, e ? e : j->name, off,
1275                                l2, j->type,
1276                                on, l3, j->state, off);
1277                 }
1278         }
1279
1280         on = ansi_highlight();
1281         off = ansi_highlight_off();
1282
1283         if (on_tty())
1284                 printf("\n%s%zu jobs listed%s.\n", on, n, off);
1285 }
1286
1287 static int list_jobs(DBusConnection *bus, char **args) {
1288         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
1289         DBusMessageIter iter, sub, sub2;
1290         int r;
1291         struct job_info *jobs = NULL;
1292         size_t size = 0, used = 0;
1293
1294         r = bus_method_call_with_reply(
1295                         bus,
1296                         "org.freedesktop.systemd1",
1297                         "/org/freedesktop/systemd1",
1298                         "org.freedesktop.systemd1.Manager",
1299                         "ListJobs",
1300                         &reply,
1301                         NULL,
1302                         DBUS_TYPE_INVALID);
1303         if (r < 0)
1304                 return r;
1305
1306         if (!dbus_message_iter_init(reply, &iter) ||
1307             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1308             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
1309                 log_error("Failed to parse reply.");
1310                 return -EIO;
1311         }
1312
1313         dbus_message_iter_recurse(&iter, &sub);
1314
1315         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1316                 const char *name, *type, *state, *job_path, *unit_path;
1317                 uint32_t id;
1318
1319                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1320                         log_error("Failed to parse reply.");
1321                         return -EIO;
1322                 }
1323
1324                 dbus_message_iter_recurse(&sub, &sub2);
1325
1326                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1327                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1328                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1329                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1330                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1331                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1332                         log_error("Failed to parse reply.");
1333                         r = -EIO;
1334                         goto finish;
1335                 }
1336
1337                 if (!GREEDY_REALLOC(jobs, size, used + 1)) {
1338                         r = log_oom();
1339                         goto finish;
1340                 }
1341
1342                 jobs[used++] = (struct job_info) { id,
1343                                                    strdup(name),
1344                                                    strdup(type),
1345                                                    strdup(state) };
1346                 if (!jobs[used-1].name || !jobs[used-1].type || !jobs[used-1].state) {
1347                         r = log_oom();
1348                         goto finish;
1349                 }
1350
1351                 dbus_message_iter_next(&sub);
1352         }
1353
1354         list_jobs_print(jobs, used);
1355
1356  finish:
1357         while (used--) {
1358                 free(jobs[used].name);
1359                 free(jobs[used].type);
1360                 free(jobs[used].state);
1361         }
1362         free(jobs);
1363
1364         return 0;
1365 }
1366
1367 static int cancel_job(DBusConnection *bus, char **args) {
1368         char **name;
1369
1370         assert(args);
1371
1372         if (strv_length(args) <= 1)
1373                 return daemon_reload(bus, args);
1374
1375         STRV_FOREACH(name, args+1) {
1376                 uint32_t id;
1377                 int r;
1378
1379                 r = safe_atou32(*name, &id);
1380                 if (r < 0) {
1381                         log_error("Failed to parse job id: %s", strerror(-r));
1382                         return r;
1383                 }
1384
1385                 r = bus_method_call_with_reply(
1386                                 bus,
1387                                 "org.freedesktop.systemd1",
1388                                 "/org/freedesktop/systemd1",
1389                                 "org.freedesktop.systemd1.Manager",
1390                                 "CancelJob",
1391                                 NULL,
1392                                 NULL,
1393                                 DBUS_TYPE_UINT32, &id,
1394                                 DBUS_TYPE_INVALID);
1395                 if (r < 0)
1396                         return r;
1397         }
1398
1399         return 0;
1400 }
1401
1402 static int need_daemon_reload(DBusConnection *bus, const char *unit) {
1403         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
1404         _cleanup_dbus_error_free_ DBusError error;
1405         dbus_bool_t b = FALSE;
1406         DBusMessageIter iter, sub;
1407         const char
1408                 *interface = "org.freedesktop.systemd1.Unit",
1409                 *property = "NeedDaemonReload",
1410                 *path;
1411         _cleanup_free_ char *n = NULL;
1412         int r;
1413
1414         dbus_error_init(&error);
1415
1416         /* We ignore all errors here, since this is used to show a warning only */
1417
1418         n = unit_name_mangle(unit);
1419         if (!n)
1420                 return log_oom();
1421
1422         r = bus_method_call_with_reply(
1423                         bus,
1424                         "org.freedesktop.systemd1",
1425                         "/org/freedesktop/systemd1",
1426                         "org.freedesktop.systemd1.Manager",
1427                         "GetUnit",
1428                         &reply,
1429                         &error,
1430                         DBUS_TYPE_STRING, &n,
1431                         DBUS_TYPE_INVALID);
1432         if (r < 0)
1433                 return r;
1434
1435         if (!dbus_message_get_args(reply, NULL,
1436                                    DBUS_TYPE_OBJECT_PATH, &path,
1437                                    DBUS_TYPE_INVALID))
1438                 return -EIO;
1439
1440         dbus_message_unref(reply);
1441         reply = NULL;
1442
1443         r = bus_method_call_with_reply(
1444                         bus,
1445                         "org.freedesktop.systemd1",
1446                         path,
1447                         "org.freedesktop.DBus.Properties",
1448                         "Get",
1449                         &reply,
1450                         &error,
1451                         DBUS_TYPE_STRING, &interface,
1452                         DBUS_TYPE_STRING, &property,
1453                         DBUS_TYPE_INVALID);
1454         if (r < 0)
1455                 return r;
1456
1457         if (!dbus_message_iter_init(reply, &iter) ||
1458             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1459                 return -EIO;
1460
1461         dbus_message_iter_recurse(&iter, &sub);
1462         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1463                 return -EIO;
1464
1465         dbus_message_iter_get_basic(&sub, &b);
1466         return b;
1467 }
1468
1469 typedef struct WaitData {
1470         Set *set;
1471
1472         char *name;
1473         char *result;
1474 } WaitData;
1475
1476 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1477         _cleanup_dbus_error_free_ DBusError error;
1478         WaitData *d = data;
1479
1480         dbus_error_init(&error);
1481
1482         assert(connection);
1483         assert(message);
1484         assert(d);
1485
1486         log_debug("Got D-Bus request: %s.%s() on %s",
1487                   dbus_message_get_interface(message),
1488                   dbus_message_get_member(message),
1489                   dbus_message_get_path(message));
1490
1491         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1492                 log_error("Warning! D-Bus connection terminated.");
1493                 dbus_connection_close(connection);
1494
1495         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1496                 uint32_t id;
1497                 const char *path, *result, *unit;
1498
1499                 if (dbus_message_get_args(message, &error,
1500                                           DBUS_TYPE_UINT32, &id,
1501                                           DBUS_TYPE_OBJECT_PATH, &path,
1502                                           DBUS_TYPE_STRING, &unit,
1503                                           DBUS_TYPE_STRING, &result,
1504                                           DBUS_TYPE_INVALID)) {
1505
1506                         free(set_remove(d->set, (char*) path));
1507
1508                         if (!isempty(result))
1509                                 d->result = strdup(result);
1510
1511                         if (!isempty(unit))
1512                                 d->name = strdup(unit);
1513
1514                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1515                 }
1516 #ifndef NOLEGACY
1517                 dbus_error_free(&error);
1518                 if (dbus_message_get_args(message, &error,
1519                                           DBUS_TYPE_UINT32, &id,
1520                                           DBUS_TYPE_OBJECT_PATH, &path,
1521                                           DBUS_TYPE_STRING, &result,
1522                                           DBUS_TYPE_INVALID)) {
1523                         /* Compatibility with older systemd versions <
1524                          * 183 during upgrades. This should be dropped
1525                          * one day. */
1526                         free(set_remove(d->set, (char*) path));
1527
1528                         if (*result)
1529                                 d->result = strdup(result);
1530
1531                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1532                 }
1533 #endif
1534
1535                 log_error("Failed to parse message: %s", bus_error_message(&error));
1536         }
1537
1538         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1539 }
1540
1541 static int enable_wait_for_jobs(DBusConnection *bus) {
1542         DBusError error;
1543
1544         assert(bus);
1545
1546         if (private_bus)
1547                 return 0;
1548
1549         dbus_error_init(&error);
1550         dbus_bus_add_match(bus,
1551                            "type='signal',"
1552                            "sender='org.freedesktop.systemd1',"
1553                            "interface='org.freedesktop.systemd1.Manager',"
1554                            "member='JobRemoved',"
1555                            "path='/org/freedesktop/systemd1'",
1556                            &error);
1557
1558         if (dbus_error_is_set(&error)) {
1559                 log_error("Failed to add match: %s", bus_error_message(&error));
1560                 dbus_error_free(&error);
1561                 return -EIO;
1562         }
1563
1564         /* This is slightly dirty, since we don't undo the match registrations. */
1565         return 0;
1566 }
1567
1568 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1569         int r = 0;
1570         WaitData d = { .set = s };
1571
1572         assert(bus);
1573         assert(s);
1574
1575         if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL))
1576                 return log_oom();
1577
1578         while (!set_isempty(s)) {
1579
1580                 if (!dbus_connection_read_write_dispatch(bus, -1)) {
1581                         log_error("Disconnected from bus.");
1582                         return -ECONNREFUSED;
1583                 }
1584
1585                 if (!d.result)
1586                         goto free_name;
1587
1588                 if (!arg_quiet) {
1589                         if (streq(d.result, "timeout"))
1590                                 log_error("Job for %s timed out.", strna(d.name));
1591                         else if (streq(d.result, "canceled"))
1592                                 log_error("Job for %s canceled.", strna(d.name));
1593                         else if (streq(d.result, "dependency"))
1594                                 log_error("A dependency job for %s failed. See 'journalctl -xn' for details.", strna(d.name));
1595                         else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1596                                 log_error("Job for %s failed. See 'systemctl status %s' and 'journalctl -xn' for details.", strna(d.name), strna(d.name));
1597                 }
1598
1599                 if (streq_ptr(d.result, "timeout"))
1600                         r = -ETIME;
1601                 else if (streq_ptr(d.result, "canceled"))
1602                         r = -ECANCELED;
1603                 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1604                         r = -EIO;
1605
1606                 free(d.result);
1607                 d.result = NULL;
1608
1609         free_name:
1610                 free(d.name);
1611                 d.name = NULL;
1612         }
1613
1614         dbus_connection_remove_filter(bus, wait_filter, &d);
1615         return r;
1616 }
1617
1618 static int check_one_unit(DBusConnection *bus, const char *name, char **check_states, bool quiet) {
1619         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
1620         _cleanup_free_ char *n = NULL;
1621         DBusMessageIter iter, sub;
1622         const char
1623                 *interface = "org.freedesktop.systemd1.Unit",
1624                 *property = "ActiveState";
1625         const char *state, *path;
1626         DBusError error;
1627         int r;
1628
1629         assert(name);
1630
1631         dbus_error_init(&error);
1632
1633         n = unit_name_mangle(name);
1634         if (!n)
1635                 return log_oom();
1636
1637         r = bus_method_call_with_reply (
1638                         bus,
1639                         "org.freedesktop.systemd1",
1640                         "/org/freedesktop/systemd1",
1641                         "org.freedesktop.systemd1.Manager",
1642                         "GetUnit",
1643                         &reply,
1644                         &error,
1645                         DBUS_TYPE_STRING, &n,
1646                         DBUS_TYPE_INVALID);
1647         if (r < 0) {
1648                 dbus_error_free(&error);
1649
1650                 if (!quiet)
1651                         puts("unknown");
1652                 return 0;
1653         }
1654
1655         if (!dbus_message_get_args(reply, NULL,
1656                                    DBUS_TYPE_OBJECT_PATH, &path,
1657                                    DBUS_TYPE_INVALID)) {
1658                 log_error("Failed to parse reply.");
1659                 return -EIO;
1660         }
1661
1662         dbus_message_unref(reply);
1663         reply = NULL;
1664
1665         r = bus_method_call_with_reply(
1666                         bus,
1667                         "org.freedesktop.systemd1",
1668                         path,
1669                         "org.freedesktop.DBus.Properties",
1670                         "Get",
1671                         &reply,
1672                         NULL,
1673                         DBUS_TYPE_STRING, &interface,
1674                         DBUS_TYPE_STRING, &property,
1675                         DBUS_TYPE_INVALID);
1676         if (r < 0) {
1677                 if (!quiet)
1678                         puts("unknown");
1679                 return 0;
1680         }
1681
1682         if (!dbus_message_iter_init(reply, &iter) ||
1683             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
1684                 log_error("Failed to parse reply.");
1685                 return r;
1686         }
1687
1688         dbus_message_iter_recurse(&iter, &sub);
1689
1690         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
1691                 log_error("Failed to parse reply.");
1692                 return r;
1693         }
1694
1695         dbus_message_iter_get_basic(&sub, &state);
1696
1697         if (!quiet)
1698                 puts(state);
1699
1700         return strv_find(check_states, state) ? 1 : 0;
1701 }
1702
1703 static void check_triggering_units(
1704                 DBusConnection *bus,
1705                 const char *unit_name) {
1706
1707         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
1708         DBusMessageIter iter, sub;
1709         const char *interface = "org.freedesktop.systemd1.Unit",
1710                    *load_state_property = "LoadState",
1711                    *triggered_by_property = "TriggeredBy",
1712                    *state;
1713         _cleanup_free_ char *unit_path = NULL, *n = NULL;
1714         bool print_warning_label = true;
1715         int r;
1716
1717         n = unit_name_mangle(unit_name);
1718         if (!n) {
1719                 log_oom();
1720                 return;
1721         }
1722
1723         unit_path = unit_dbus_path_from_name(n);
1724         if (!unit_path) {
1725                 log_oom();
1726                 return;
1727         }
1728
1729         r = bus_method_call_with_reply(
1730                         bus,
1731                         "org.freedesktop.systemd1",
1732                         unit_path,
1733                         "org.freedesktop.DBus.Properties",
1734                         "Get",
1735                         &reply,
1736                         NULL,
1737                         DBUS_TYPE_STRING, &interface,
1738                         DBUS_TYPE_STRING, &load_state_property,
1739                         DBUS_TYPE_INVALID);
1740         if (r < 0)
1741                 return;
1742
1743         if (!dbus_message_iter_init(reply, &iter) ||
1744             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1745                 log_error("Failed to parse reply.");
1746                 return;
1747         }
1748
1749         dbus_message_iter_recurse(&iter, &sub);
1750
1751         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
1752             log_error("Failed to parse reply.");
1753             return;
1754         }
1755
1756         dbus_message_iter_get_basic(&sub, &state);
1757
1758         if (streq(state, "masked"))
1759             return;
1760
1761         dbus_message_unref(reply);
1762         reply = NULL;
1763
1764         r = bus_method_call_with_reply(
1765                         bus,
1766                         "org.freedesktop.systemd1",
1767                         unit_path,
1768                         "org.freedesktop.DBus.Properties",
1769                         "Get",
1770                         &reply,
1771                         NULL,
1772                         DBUS_TYPE_STRING, &interface,
1773                         DBUS_TYPE_STRING, &triggered_by_property,
1774                         DBUS_TYPE_INVALID);
1775         if (r < 0)
1776                 return;
1777
1778         if (!dbus_message_iter_init(reply, &iter) ||
1779             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1780                 log_error("Failed to parse reply.");
1781                 return;
1782         }
1783
1784         dbus_message_iter_recurse(&iter, &sub);
1785         dbus_message_iter_recurse(&sub, &iter);
1786         sub = iter;
1787
1788         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1789                 const char * const check_states[] = {
1790                         "active",
1791                         "reloading",
1792                         NULL
1793                 };
1794                 const char *service_trigger;
1795
1796                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1797                         log_error("Failed to parse reply.");
1798                         return;
1799                 }
1800
1801                 dbus_message_iter_get_basic(&sub, &service_trigger);
1802
1803                 r = check_one_unit(bus, service_trigger, (char**) check_states, true);
1804                 if (r < 0)
1805                         return;
1806                 if (r > 0) {
1807                         if (print_warning_label) {
1808                                 log_warning("Warning: Stopping %s, but it can still be activated by:", unit_name);
1809                                 print_warning_label = false;
1810                         }
1811
1812                         log_warning("  %s", service_trigger);
1813                 }
1814
1815                 dbus_message_iter_next(&sub);
1816         }
1817 }
1818
1819 static int start_unit_one(
1820                 DBusConnection *bus,
1821                 const char *method,
1822                 const char *name,
1823                 const char *mode,
1824                 DBusError *error,
1825                 Set *s) {
1826
1827         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
1828         _cleanup_free_ char *n;
1829         const char *path;
1830         int r;
1831
1832         assert(method);
1833         assert(name);
1834         assert(mode);
1835         assert(error);
1836
1837         n = unit_name_mangle(name);
1838         if (!n)
1839                 return log_oom();
1840
1841         r = bus_method_call_with_reply(
1842                         bus,
1843                         "org.freedesktop.systemd1",
1844                         "/org/freedesktop/systemd1",
1845                         "org.freedesktop.systemd1.Manager",
1846                         method,
1847                         &reply,
1848                         error,
1849                         DBUS_TYPE_STRING, &n,
1850                         DBUS_TYPE_STRING, &mode,
1851                         DBUS_TYPE_INVALID);
1852         if (r) {
1853                 if (r == -ENOENT && arg_action != ACTION_SYSTEMCTL)
1854                         /* There's always a fallback possible for
1855                          * legacy actions. */
1856                         r = -EADDRNOTAVAIL;
1857                 else
1858                         log_error("Failed to issue method call: %s", bus_error_message(error));
1859
1860                 return r;
1861         }
1862
1863         if (!dbus_message_get_args(reply, error,
1864                                    DBUS_TYPE_OBJECT_PATH, &path,
1865                                    DBUS_TYPE_INVALID)) {
1866                 log_error("Failed to parse reply: %s", bus_error_message(error));
1867                 return -EIO;
1868         }
1869
1870         if (need_daemon_reload(bus, n) > 0)
1871                 log_warning("Warning: Unit file of %s changed on disk, 'systemctl %sdaemon-reload' recommended.",
1872                             n, arg_scope == UNIT_FILE_SYSTEM ? "" : "--user ");
1873
1874         if (s) {
1875                 char *p;
1876
1877                 p = strdup(path);
1878                 if (!p)
1879                         return log_oom();
1880
1881                 r = set_consume(s, p);
1882                 if (r < 0) {
1883                         log_error("Failed to add path to set.");
1884                         return r;
1885                 }
1886         }
1887
1888         return 0;
1889 }
1890
1891 static const struct {
1892         const char *target;
1893         const char *verb;
1894         const char *mode;
1895 } action_table[_ACTION_MAX] = {
1896         [ACTION_HALT]         = { SPECIAL_HALT_TARGET,         "halt",         "replace-irreversibly" },
1897         [ACTION_POWEROFF]     = { SPECIAL_POWEROFF_TARGET,     "poweroff",     "replace-irreversibly" },
1898         [ACTION_REBOOT]       = { SPECIAL_REBOOT_TARGET,       "reboot",       "replace-irreversibly" },
1899         [ACTION_KEXEC]        = { SPECIAL_KEXEC_TARGET,        "kexec",        "replace-irreversibly" },
1900         [ACTION_RUNLEVEL2]    = { SPECIAL_RUNLEVEL2_TARGET,    NULL,           "isolate" },
1901         [ACTION_RUNLEVEL3]    = { SPECIAL_RUNLEVEL3_TARGET,    NULL,           "isolate" },
1902         [ACTION_RUNLEVEL4]    = { SPECIAL_RUNLEVEL4_TARGET,    NULL,           "isolate" },
1903         [ACTION_RUNLEVEL5]    = { SPECIAL_RUNLEVEL5_TARGET,    NULL,           "isolate" },
1904         [ACTION_RESCUE]       = { SPECIAL_RESCUE_TARGET,       "rescue",       "isolate" },
1905         [ACTION_EMERGENCY]    = { SPECIAL_EMERGENCY_TARGET,    "emergency",    "isolate" },
1906         [ACTION_DEFAULT]      = { SPECIAL_DEFAULT_TARGET,      "default",      "isolate" },
1907         [ACTION_EXIT]         = { SPECIAL_EXIT_TARGET,         "exit",         "replace-irreversibly" },
1908         [ACTION_SUSPEND]      = { SPECIAL_SUSPEND_TARGET,      "suspend",      "replace-irreversibly" },
1909         [ACTION_HIBERNATE]    = { SPECIAL_HIBERNATE_TARGET,    "hibernate",    "replace-irreversibly" },
1910         [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" },
1911 };
1912
1913 static enum action verb_to_action(const char *verb) {
1914         enum action i;
1915
1916         for (i = ACTION_INVALID; i < _ACTION_MAX; i++)
1917                 if (action_table[i].verb && streq(verb, action_table[i].verb))
1918                         return i;
1919         return ACTION_INVALID;
1920 }
1921
1922 static int start_unit(DBusConnection *bus, char **args) {
1923
1924         int r, ret = 0;
1925         const char *method, *mode, *one_name;
1926         _cleanup_set_free_free_ Set *s = NULL;
1927         _cleanup_dbus_error_free_ DBusError error;
1928         char **name;
1929
1930         dbus_error_init(&error);
1931
1932         assert(bus);
1933
1934         ask_password_agent_open_if_enabled();
1935
1936         if (arg_action == ACTION_SYSTEMCTL) {
1937                 enum action action;
1938                 method =
1939                         streq(args[0], "stop") ||
1940                         streq(args[0], "condstop")              ? "StopUnit" :
1941                         streq(args[0], "reload")                ? "ReloadUnit" :
1942                         streq(args[0], "restart")               ? "RestartUnit" :
1943
1944                         streq(args[0], "try-restart")           ||
1945                         streq(args[0], "condrestart")           ? "TryRestartUnit" :
1946
1947                         streq(args[0], "reload-or-restart")     ? "ReloadOrRestartUnit" :
1948
1949                         streq(args[0], "reload-or-try-restart") ||
1950                         streq(args[0], "condreload") ||
1951
1952                         streq(args[0], "force-reload")          ? "ReloadOrTryRestartUnit" :
1953                                                                   "StartUnit";
1954                 action = verb_to_action(args[0]);
1955
1956                 mode = streq(args[0], "isolate") ? "isolate" :
1957                        action_table[action].mode ?: arg_job_mode;
1958
1959                 one_name = action_table[action].target;
1960
1961         } else {
1962                 assert(arg_action < ELEMENTSOF(action_table));
1963                 assert(action_table[arg_action].target);
1964
1965                 method = "StartUnit";
1966
1967                 mode = action_table[arg_action].mode;
1968                 one_name = action_table[arg_action].target;
1969         }
1970
1971         if (!arg_no_block) {
1972                 ret = enable_wait_for_jobs(bus);
1973                 if (ret < 0) {
1974                         log_error("Could not watch jobs: %s", strerror(-ret));
1975                         return ret;
1976                 }
1977
1978                 s = set_new(string_hash_func, string_compare_func);
1979                 if (!s)
1980                         return log_oom();
1981         }
1982
1983         if (one_name) {
1984                 ret = start_unit_one(bus, method, one_name, mode, &error, s);
1985                 if (ret < 0)
1986                         ret = translate_bus_error_to_exit_status(ret, &error);
1987         } else {
1988                 STRV_FOREACH(name, args+1) {
1989                         r = start_unit_one(bus, method, *name, mode, &error, s);
1990                         if (r < 0) {
1991                                 ret = translate_bus_error_to_exit_status(r, &error);
1992                                 dbus_error_free(&error);
1993                         }
1994                 }
1995         }
1996
1997         if (!arg_no_block) {
1998                 r = wait_for_jobs(bus, s);
1999                 if (r < 0)
2000                         return r;
2001
2002                 /* When stopping units, warn if they can still be triggered by
2003                  * another active unit (socket, path, timer) */
2004                 if (!arg_quiet && streq(method, "StopUnit")) {
2005                         if (one_name)
2006                                 check_triggering_units(bus, one_name);
2007                         else
2008                                 STRV_FOREACH(name, args+1)
2009                                         check_triggering_units(bus, *name);
2010                 }
2011         }
2012
2013         return ret;
2014 }
2015
2016 /* Ask systemd-logind, which might grant access to unprivileged users
2017  * through PolicyKit */
2018 static int reboot_with_logind(DBusConnection *bus, enum action a) {
2019 #ifdef HAVE_LOGIND
2020         const char *method;
2021         dbus_bool_t interactive = true;
2022
2023         if (!bus)
2024                 return -EIO;
2025
2026         polkit_agent_open_if_enabled();
2027
2028         switch (a) {
2029
2030         case ACTION_REBOOT:
2031                 method = "Reboot";
2032                 break;
2033
2034         case ACTION_POWEROFF:
2035                 method = "PowerOff";
2036                 break;
2037
2038         case ACTION_SUSPEND:
2039                 method = "Suspend";
2040                 break;
2041
2042         case ACTION_HIBERNATE:
2043                 method = "Hibernate";
2044                 break;
2045
2046         case ACTION_HYBRID_SLEEP:
2047                 method = "HybridSleep";
2048                 break;
2049
2050         default:
2051                 return -EINVAL;
2052         }
2053
2054         return bus_method_call_with_reply(
2055                         bus,
2056                         "org.freedesktop.login1",
2057                         "/org/freedesktop/login1",
2058                         "org.freedesktop.login1.Manager",
2059                         method,
2060                         NULL,
2061                         NULL,
2062                         DBUS_TYPE_BOOLEAN, &interactive,
2063                         DBUS_TYPE_INVALID);
2064 #else
2065         return -ENOSYS;
2066 #endif
2067 }
2068
2069 static int check_inhibitors(DBusConnection *bus, enum action a) {
2070 #ifdef HAVE_LOGIND
2071         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
2072         DBusMessageIter iter, sub, sub2;
2073         int r;
2074         unsigned c = 0;
2075         _cleanup_strv_free_ char **sessions = NULL;
2076         char **s;
2077
2078         if (!bus)
2079                 return 0;
2080
2081         if (arg_ignore_inhibitors || arg_force > 0)
2082                 return 0;
2083
2084         if (arg_when > 0)
2085                 return 0;
2086
2087         if (geteuid() == 0)
2088                 return 0;
2089
2090         if (!on_tty())
2091                 return 0;
2092
2093         r = bus_method_call_with_reply(
2094                         bus,
2095                         "org.freedesktop.login1",
2096                         "/org/freedesktop/login1",
2097                         "org.freedesktop.login1.Manager",
2098                         "ListInhibitors",
2099                         &reply,
2100                         NULL,
2101                         DBUS_TYPE_INVALID);
2102         if (r < 0)
2103                 /* If logind is not around, then there are no inhibitors... */
2104                 return 0;
2105
2106         if (!dbus_message_iter_init(reply, &iter) ||
2107             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2108             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
2109                 log_error("Failed to parse reply.");
2110                 return -EIO;
2111         }
2112
2113         dbus_message_iter_recurse(&iter, &sub);
2114         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2115                 const char *what, *who, *why, *mode;
2116                 uint32_t uid, pid;
2117                 _cleanup_strv_free_ char **sv = NULL;
2118                 _cleanup_free_ char *comm = NULL, *user = NULL;
2119
2120                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
2121                         log_error("Failed to parse reply.");
2122                         return -EIO;
2123                 }
2124
2125                 dbus_message_iter_recurse(&sub, &sub2);
2126
2127                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &what, true) < 0 ||
2128                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &who, true) < 0 ||
2129                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &why, true) < 0 ||
2130                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &mode, true) < 0 ||
2131                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 ||
2132                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, false) < 0) {
2133                         log_error("Failed to parse reply.");
2134                         return -EIO;
2135                 }
2136
2137                 if (!streq(mode, "block"))
2138                         goto next;
2139
2140                 sv = strv_split(what, ":");
2141                 if (!sv)
2142                         return log_oom();
2143
2144                 if (!strv_contains(sv,
2145                                   a == ACTION_HALT ||
2146                                   a == ACTION_POWEROFF ||
2147                                   a == ACTION_REBOOT ||
2148                                   a == ACTION_KEXEC ? "shutdown" : "sleep"))
2149                         goto next;
2150
2151                 get_process_comm(pid, &comm);
2152                 user = uid_to_name(uid);
2153                 log_warning("Operation inhibited by \"%s\" (PID %lu \"%s\", user %s), reason is \"%s\".",
2154                             who, (unsigned long) pid, strna(comm), strna(user), why);
2155                 c++;
2156
2157         next:
2158                 dbus_message_iter_next(&sub);
2159         }
2160
2161         dbus_message_iter_recurse(&iter, &sub);
2162
2163         /* Check for current sessions */
2164         sd_get_sessions(&sessions);
2165         STRV_FOREACH(s, sessions) {
2166                 uid_t uid;
2167                 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
2168
2169                 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
2170                         continue;
2171
2172                 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
2173                         continue;
2174
2175                 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
2176                         continue;
2177
2178                 sd_session_get_tty(*s, &tty);
2179                 sd_session_get_seat(*s, &seat);
2180                 sd_session_get_service(*s, &service);
2181                 user = uid_to_name(uid);
2182
2183                 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
2184                 c++;
2185         }
2186
2187         if (c <= 0)
2188                 return 0;
2189
2190         log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
2191                   action_table[a].verb);
2192
2193         return -EPERM;
2194 #else
2195         return 0;
2196 #endif
2197 }
2198
2199 static int start_special(DBusConnection *bus, char **args) {
2200         enum action a;
2201         int r;
2202
2203         assert(args);
2204
2205         a = verb_to_action(args[0]);
2206
2207         r = check_inhibitors(bus, a);
2208         if (r < 0)
2209                 return r;
2210
2211         if (arg_force >= 2 && geteuid() != 0) {
2212                 log_error("Must be root.");
2213                 return -EPERM;
2214         }
2215
2216         if (arg_force >= 2 &&
2217             (a == ACTION_HALT ||
2218              a == ACTION_POWEROFF ||
2219              a == ACTION_REBOOT))
2220                 halt_now(a);
2221
2222         if (arg_force >= 1 &&
2223             (a == ACTION_HALT ||
2224              a == ACTION_POWEROFF ||
2225              a == ACTION_REBOOT ||
2226              a == ACTION_KEXEC ||
2227              a == ACTION_EXIT))
2228                 return daemon_reload(bus, args);
2229
2230         /* first try logind, to allow authentication with polkit */
2231         if (geteuid() != 0 &&
2232             (a == ACTION_POWEROFF ||
2233              a == ACTION_REBOOT ||
2234              a == ACTION_SUSPEND ||
2235              a == ACTION_HIBERNATE ||
2236              a == ACTION_HYBRID_SLEEP)) {
2237                 r = reboot_with_logind(bus, a);
2238                 if (r >= 0)
2239                         return r;
2240         }
2241
2242         r = start_unit(bus, args);
2243         if (r == EXIT_SUCCESS)
2244                 warn_wall(a);
2245
2246         return r;
2247 }
2248
2249 static int check_unit_active(DBusConnection *bus, char **args) {
2250         const char * const check_states[] = {
2251                 "active",
2252                 "reloading",
2253                 NULL
2254         };
2255
2256         char **name;
2257         int r = 3; /* According to LSB: "program is not running" */
2258
2259         assert(bus);
2260         assert(args);
2261
2262         STRV_FOREACH(name, args+1) {
2263                 int state;
2264
2265                 state = check_one_unit(bus, *name, (char**) check_states, arg_quiet);
2266                 if (state < 0)
2267                         return state;
2268                 if (state > 0)
2269                         r = 0;
2270         }
2271
2272         return r;
2273 }
2274
2275 static int check_unit_failed(DBusConnection *bus, char **args) {
2276         const char * const check_states[] = {
2277                 "failed",
2278                 NULL
2279         };
2280
2281         char **name;
2282         int r = 1;
2283
2284         assert(bus);
2285         assert(args);
2286
2287         STRV_FOREACH(name, args+1) {
2288                 int state;
2289
2290                 state = check_one_unit(bus, *name, (char**) check_states, arg_quiet);
2291                 if (state < 0)
2292                         return state;
2293                 if (state > 0)
2294                         r = 0;
2295         }
2296
2297         return r;
2298 }
2299
2300 static int kill_unit(DBusConnection *bus, char **args) {
2301         char **name;
2302         int r = 0;
2303
2304         assert(bus);
2305         assert(args);
2306
2307         if (!arg_kill_who)
2308                 arg_kill_who = "all";
2309
2310         STRV_FOREACH(name, args+1) {
2311                 _cleanup_free_ char *n = NULL;
2312
2313                 n = unit_name_mangle(*name);
2314                 if (!n)
2315                         return log_oom();
2316
2317                 r = bus_method_call_with_reply(
2318                                 bus,
2319                                 "org.freedesktop.systemd1",
2320                                 "/org/freedesktop/systemd1",
2321                                 "org.freedesktop.systemd1.Manager",
2322                                 "KillUnit",
2323                                 NULL,
2324                                 NULL,
2325                                 DBUS_TYPE_STRING, &n,
2326                                 DBUS_TYPE_STRING, &arg_kill_who,
2327                                 DBUS_TYPE_INT32, &arg_signal,
2328                                 DBUS_TYPE_INVALID);
2329                 if (r < 0)
2330                         return r;
2331         }
2332         return 0;
2333 }
2334
2335 typedef struct ExecStatusInfo {
2336         char *name;
2337
2338         char *path;
2339         char **argv;
2340
2341         bool ignore;
2342
2343         usec_t start_timestamp;
2344         usec_t exit_timestamp;
2345         pid_t pid;
2346         int code;
2347         int status;
2348
2349         LIST_FIELDS(struct ExecStatusInfo, exec);
2350 } ExecStatusInfo;
2351
2352 static void exec_status_info_free(ExecStatusInfo *i) {
2353         assert(i);
2354
2355         free(i->name);
2356         free(i->path);
2357         strv_free(i->argv);
2358         free(i);
2359 }
2360
2361 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
2362         uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
2363         DBusMessageIter sub2, sub3;
2364         const char*path;
2365         unsigned n;
2366         uint32_t pid;
2367         int32_t code, status;
2368         dbus_bool_t ignore;
2369
2370         assert(i);
2371         assert(i);
2372
2373         if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
2374                 return -EIO;
2375
2376         dbus_message_iter_recurse(sub, &sub2);
2377
2378         if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
2379                 return -EIO;
2380
2381         i->path = strdup(path);
2382         if (!i->path)
2383                 return -ENOMEM;
2384
2385         if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
2386             dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
2387                 return -EIO;
2388
2389         n = 0;
2390         dbus_message_iter_recurse(&sub2, &sub3);
2391         while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
2392                 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
2393                 dbus_message_iter_next(&sub3);
2394                 n++;
2395         }
2396
2397         i->argv = new0(char*, n+1);
2398         if (!i->argv)
2399                 return -ENOMEM;
2400
2401         n = 0;
2402         dbus_message_iter_recurse(&sub2, &sub3);
2403         while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
2404                 const char *s;
2405
2406                 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
2407                 dbus_message_iter_get_basic(&sub3, &s);
2408                 dbus_message_iter_next(&sub3);
2409
2410                 i->argv[n] = strdup(s);
2411                 if (!i->argv[n])
2412                         return -ENOMEM;
2413
2414                 n++;
2415         }
2416
2417         if (!dbus_message_iter_next(&sub2) ||
2418             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
2419             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
2420             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
2421             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
2422             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
2423             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
2424             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
2425             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
2426                 return -EIO;
2427
2428         i->ignore = ignore;
2429         i->start_timestamp = (usec_t) start_timestamp;
2430         i->exit_timestamp = (usec_t) exit_timestamp;
2431         i->pid = (pid_t) pid;
2432         i->code = code;
2433         i->status = status;
2434
2435         return 0;
2436 }
2437
2438 typedef struct UnitStatusInfo {
2439         const char *id;
2440         const char *load_state;
2441         const char *active_state;
2442         const char *sub_state;
2443         const char *unit_file_state;
2444
2445         const char *description;
2446         const char *following;
2447
2448         char **documentation;
2449
2450         const char *fragment_path;
2451         const char *source_path;
2452         const char *control_group;
2453
2454         char **dropin_paths;
2455
2456         const char *load_error;
2457         const char *result;
2458
2459         usec_t inactive_exit_timestamp;
2460         usec_t inactive_exit_timestamp_monotonic;
2461         usec_t active_enter_timestamp;
2462         usec_t active_exit_timestamp;
2463         usec_t inactive_enter_timestamp;
2464
2465         bool need_daemon_reload;
2466
2467         /* Service */
2468         pid_t main_pid;
2469         pid_t control_pid;
2470         const char *status_text;
2471         const char *pid_file;
2472         bool running:1;
2473
2474         usec_t start_timestamp;
2475         usec_t exit_timestamp;
2476
2477         int exit_code, exit_status;
2478
2479         usec_t condition_timestamp;
2480         bool condition_result;
2481         bool failed_condition_trigger;
2482         bool failed_condition_negate;
2483         const char *failed_condition;
2484         const char *failed_condition_param;
2485
2486         /* Socket */
2487         unsigned n_accepted;
2488         unsigned n_connections;
2489         bool accept;
2490
2491         /* Pairs of type, path */
2492         char **listen;
2493
2494         /* Device */
2495         const char *sysfs_path;
2496
2497         /* Mount, Automount */
2498         const char *where;
2499
2500         /* Swap */
2501         const char *what;
2502
2503         LIST_HEAD(ExecStatusInfo, exec);
2504 } UnitStatusInfo;
2505
2506 static void print_status_info(UnitStatusInfo *i,
2507                               bool *ellipsized) {
2508         ExecStatusInfo *p;
2509         const char *on, *off, *ss;
2510         usec_t timestamp;
2511         char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
2512         char since2[FORMAT_TIMESTAMP_MAX], *s2;
2513         const char *path;
2514         int flags =
2515                 arg_all * OUTPUT_SHOW_ALL |
2516                 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
2517                 on_tty() * OUTPUT_COLOR |
2518                 !arg_quiet * OUTPUT_WARN_CUTOFF |
2519                 arg_full * OUTPUT_FULL_WIDTH;
2520         char **t, **t2;
2521
2522         assert(i);
2523
2524         /* This shows pretty information about a unit. See
2525          * print_property() for a low-level property printer */
2526
2527         printf("%s", strna(i->id));
2528
2529         if (i->description && !streq_ptr(i->id, i->description))
2530                 printf(" - %s", i->description);
2531
2532         printf("\n");
2533
2534         if (i->following)
2535                 printf("   Follow: unit currently follows state of %s\n", i->following);
2536
2537         if (streq_ptr(i->load_state, "error")) {
2538                 on = ansi_highlight_red();
2539                 off = ansi_highlight_off();
2540         } else
2541                 on = off = "";
2542
2543         path = i->source_path ? i->source_path : i->fragment_path;
2544
2545         if (i->load_error)
2546                 printf("   Loaded: %s%s%s (Reason: %s)\n",
2547                        on, strna(i->load_state), off, i->load_error);
2548         else if (path && i->unit_file_state)
2549                 printf("   Loaded: %s%s%s (%s; %s)\n",
2550                        on, strna(i->load_state), off, path, i->unit_file_state);
2551         else if (path)
2552                 printf("   Loaded: %s%s%s (%s)\n",
2553                        on, strna(i->load_state), off, path);
2554         else
2555                 printf("   Loaded: %s%s%s\n",
2556                        on, strna(i->load_state), off);
2557
2558         if (!strv_isempty(i->dropin_paths)) {
2559                 char ** dropin;
2560                 char * dir = NULL;
2561                 bool last = false;
2562
2563                 STRV_FOREACH(dropin, i->dropin_paths) {
2564                         if (! dir || last) {
2565                                 printf(dir ? "        " : "  Drop-In: ");
2566
2567                                 free(dir);
2568
2569                                 if (path_get_parent(*dropin, &dir) < 0) {
2570                                         log_oom();
2571                                         return;
2572                                 }
2573
2574                                 printf("%s\n           %s", dir,
2575                                        draw_special_char(DRAW_TREE_RIGHT));
2576                         }
2577
2578                         last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir));
2579
2580                         printf("%s%s", path_get_file_name(*dropin), last ? "\n" : ", ");
2581                 }
2582
2583                 free(dir);
2584         }
2585
2586         ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2587
2588         if (streq_ptr(i->active_state, "failed")) {
2589                 on = ansi_highlight_red();
2590                 off = ansi_highlight_off();
2591         } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2592                 on = ansi_highlight_green();
2593                 off = ansi_highlight_off();
2594         } else
2595                 on = off = "";
2596
2597         if (ss)
2598                 printf("   Active: %s%s (%s)%s",
2599                        on, strna(i->active_state), ss, off);
2600         else
2601                 printf("   Active: %s%s%s",
2602                        on, strna(i->active_state), off);
2603
2604         if (!isempty(i->result) && !streq(i->result, "success"))
2605                 printf(" (Result: %s)", i->result);
2606
2607         timestamp = (streq_ptr(i->active_state, "active")      ||
2608                      streq_ptr(i->active_state, "reloading"))   ? i->active_enter_timestamp :
2609                     (streq_ptr(i->active_state, "inactive")    ||
2610                      streq_ptr(i->active_state, "failed"))      ? i->inactive_enter_timestamp :
2611                     streq_ptr(i->active_state, "activating")    ? i->inactive_exit_timestamp :
2612                                                                   i->active_exit_timestamp;
2613
2614         s1 = format_timestamp_relative(since1, sizeof(since1), timestamp);
2615         s2 = format_timestamp(since2, sizeof(since2), timestamp);
2616
2617         if (s1)
2618                 printf(" since %s; %s\n", s2, s1);
2619         else if (s2)
2620                 printf(" since %s\n", s2);
2621         else
2622                 printf("\n");
2623
2624         if (!i->condition_result && i->condition_timestamp > 0) {
2625                 s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp);
2626                 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2627
2628                 printf("           start condition failed at %s%s%s\n",
2629                        s2, s1 ? "; " : "", s1 ? s1 : "");
2630                 if (i->failed_condition_trigger)
2631                         printf("           none of the trigger conditions were met\n");
2632                 else if (i->failed_condition)
2633                         printf("           %s=%s%s was not met\n",
2634                                i->failed_condition,
2635                                i->failed_condition_negate ? "!" : "",
2636                                i->failed_condition_param);
2637         }
2638
2639         if (i->sysfs_path)
2640                 printf("   Device: %s\n", i->sysfs_path);
2641         if (i->where)
2642                 printf("    Where: %s\n", i->where);
2643         if (i->what)
2644                 printf("     What: %s\n", i->what);
2645
2646         STRV_FOREACH(t, i->documentation)
2647                 printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", *t);
2648
2649         STRV_FOREACH_PAIR(t, t2, i->listen)
2650                 printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t);
2651
2652         if (i->accept)
2653                 printf(" Accepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2654
2655         LIST_FOREACH(exec, p, i->exec) {
2656                 _cleanup_free_ char *argv = NULL;
2657                 bool good;
2658
2659                 /* Only show exited processes here */
2660                 if (p->code == 0)
2661                         continue;
2662
2663                 argv = strv_join(p->argv, " ");
2664                 printf("  Process: %u %s=%s ", p->pid, p->name, strna(argv));
2665
2666                 good = is_clean_exit_lsb(p->code, p->status, NULL);
2667                 if (!good) {
2668                         on = ansi_highlight_red();
2669                         off = ansi_highlight_off();
2670                 } else
2671                         on = off = "";
2672
2673                 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2674
2675                 if (p->code == CLD_EXITED) {
2676                         const char *c;
2677
2678                         printf("status=%i", p->status);
2679
2680                         c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD);
2681                         if (c)
2682                                 printf("/%s", c);
2683
2684                 } else
2685                         printf("signal=%s", signal_to_string(p->status));
2686
2687                 printf(")%s\n", off);
2688
2689                 if (i->main_pid == p->pid &&
2690                     i->start_timestamp == p->start_timestamp &&
2691                     i->exit_timestamp == p->start_timestamp)
2692                         /* Let's not show this twice */
2693                         i->main_pid = 0;
2694
2695                 if (p->pid == i->control_pid)
2696                         i->control_pid = 0;
2697         }
2698
2699         if (i->main_pid > 0 || i->control_pid > 0) {
2700                 if (i->main_pid > 0) {
2701                         printf(" Main PID: %u", (unsigned) i->main_pid);
2702
2703                         if (i->running) {
2704                                 _cleanup_free_ char *comm = NULL;
2705                                 get_process_comm(i->main_pid, &comm);
2706                                 if (comm)
2707                                         printf(" (%s)", comm);
2708                         } else if (i->exit_code > 0) {
2709                                 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2710
2711                                 if (i->exit_code == CLD_EXITED) {
2712                                         const char *c;
2713
2714                                         printf("status=%i", i->exit_status);
2715
2716                                         c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD);
2717                                         if (c)
2718                                                 printf("/%s", c);
2719
2720                                 } else
2721                                         printf("signal=%s", signal_to_string(i->exit_status));
2722                                 printf(")");
2723                         }
2724
2725                         if (i->control_pid > 0)
2726                                 printf(";");
2727                 }
2728
2729                 if (i->control_pid > 0) {
2730                         _cleanup_free_ char *c = NULL;
2731
2732                         printf(" %8s: %u", i->main_pid ? "" : " Control", (unsigned) i->control_pid);
2733
2734                         get_process_comm(i->control_pid, &c);
2735                         if (c)
2736                                 printf(" (%s)", c);
2737                 }
2738
2739                 printf("\n");
2740         }
2741
2742         if (i->status_text)
2743                 printf("   Status: \"%s\"\n", i->status_text);
2744
2745         if (i->control_group &&
2746             (i->main_pid > 0 || i->control_pid > 0 || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0)) {
2747                 unsigned c;
2748
2749                 printf("   CGroup: %s\n", i->control_group);
2750
2751                 if (arg_transport != TRANSPORT_SSH) {
2752                         unsigned k = 0;
2753                         pid_t extra[2];
2754                         char prefix[] = "           ";
2755
2756                         c = columns();
2757                         if (c > sizeof(prefix) - 1)
2758                                 c -= sizeof(prefix) - 1;
2759                         else
2760                                 c = 0;
2761
2762                         if (i->main_pid > 0)
2763                                 extra[k++] = i->main_pid;
2764
2765                         if (i->control_pid > 0)
2766                                 extra[k++] = i->control_pid;
2767
2768                         show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix,
2769                                                       c, false, extra, k, flags);
2770                 }
2771         }
2772
2773         if (i->id && arg_transport != TRANSPORT_SSH) {
2774                 printf("\n");
2775                 show_journal_by_unit(stdout,
2776                                      i->id,
2777                                      arg_output,
2778                                      0,
2779                                      i->inactive_exit_timestamp_monotonic,
2780                                      arg_lines,
2781                                      getuid(),
2782                                      flags,
2783                                      arg_scope == UNIT_FILE_SYSTEM,
2784                                      ellipsized);
2785         }
2786
2787         if (i->need_daemon_reload)
2788                 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %sdaemon-reload' recommended.\n",
2789                        ansi_highlight_red(),
2790                        ansi_highlight_off(),
2791                        arg_scope == UNIT_FILE_SYSTEM ? "" : "--user ");
2792 }
2793
2794 static void show_unit_help(UnitStatusInfo *i) {
2795         char **p;
2796
2797         assert(i);
2798
2799         if (!i->documentation) {
2800                 log_info("Documentation for %s not known.", i->id);
2801                 return;
2802         }
2803
2804         STRV_FOREACH(p, i->documentation) {
2805
2806                 if (startswith(*p, "man:")) {
2807                         size_t k;
2808                         char *e = NULL;
2809                         _cleanup_free_ char *page = NULL, *section = NULL;
2810                         const char *args[4] = { "man", NULL, NULL, NULL };
2811                         pid_t pid;
2812
2813                         k = strlen(*p);
2814
2815                         if ((*p)[k-1] == ')')
2816                                 e = strrchr(*p, '(');
2817
2818                         if (e) {
2819                                 page = strndup((*p) + 4, e - *p - 4);
2820                                 section = strndup(e + 1, *p + k - e - 2);
2821                                 if (!page || !section) {
2822                                         log_oom();
2823                                         return;
2824                                 }
2825
2826                                 args[1] = section;
2827                                 args[2] = page;
2828                         } else
2829                                 args[1] = *p + 4;
2830
2831                         pid = fork();
2832                         if (pid < 0) {
2833                                 log_error("Failed to fork: %m");
2834                                 continue;
2835                         }
2836
2837                         if (pid == 0) {
2838                                 /* Child */
2839                                 execvp(args[0], (char**) args);
2840                                 log_error("Failed to execute man: %m");
2841                                 _exit(EXIT_FAILURE);
2842                         }
2843
2844                         wait_for_terminate(pid, NULL);
2845                 } else
2846                         log_info("Can't show: %s", *p);
2847         }
2848 }
2849
2850 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2851
2852         assert(name);
2853         assert(iter);
2854         assert(i);
2855
2856         switch (dbus_message_iter_get_arg_type(iter)) {
2857
2858         case DBUS_TYPE_STRING: {
2859                 const char *s;
2860
2861                 dbus_message_iter_get_basic(iter, &s);
2862
2863                 if (!isempty(s)) {
2864                         if (streq(name, "Id"))
2865                                 i->id = s;
2866                         else if (streq(name, "LoadState"))
2867                                 i->load_state = s;
2868                         else if (streq(name, "ActiveState"))
2869                                 i->active_state = s;
2870                         else if (streq(name, "SubState"))
2871                                 i->sub_state = s;
2872                         else if (streq(name, "Description"))
2873                                 i->description = s;
2874                         else if (streq(name, "FragmentPath"))
2875                                 i->fragment_path = s;
2876                         else if (streq(name, "SourcePath"))
2877                                 i->source_path = s;
2878 #ifndef NOLEGACY
2879                         else if (streq(name, "DefaultControlGroup")) {
2880                                 const char *e;
2881                                 e = startswith(s, SYSTEMD_CGROUP_CONTROLLER ":");
2882                                 if (e)
2883                                         i->control_group = e;
2884                         }
2885 #endif
2886                         else if (streq(name, "ControlGroup"))
2887                                 i->control_group = s;
2888                         else if (streq(name, "StatusText"))
2889                                 i->status_text = s;
2890                         else if (streq(name, "PIDFile"))
2891                                 i->pid_file = s;
2892                         else if (streq(name, "SysFSPath"))
2893                                 i->sysfs_path = s;
2894                         else if (streq(name, "Where"))
2895                                 i->where = s;
2896                         else if (streq(name, "What"))
2897                                 i->what = s;
2898                         else if (streq(name, "Following"))
2899                                 i->following = s;
2900                         else if (streq(name, "UnitFileState"))
2901                                 i->unit_file_state = s;
2902                         else if (streq(name, "Result"))
2903                                 i->result = s;
2904                 }
2905
2906                 break;
2907         }
2908
2909         case DBUS_TYPE_BOOLEAN: {
2910                 dbus_bool_t b;
2911
2912                 dbus_message_iter_get_basic(iter, &b);
2913
2914                 if (streq(name, "Accept"))
2915                         i->accept = b;
2916                 else if (streq(name, "NeedDaemonReload"))
2917                         i->need_daemon_reload = b;
2918                 else if (streq(name, "ConditionResult"))
2919                         i->condition_result = b;
2920
2921                 break;
2922         }
2923
2924         case DBUS_TYPE_UINT32: {
2925                 uint32_t u;
2926
2927                 dbus_message_iter_get_basic(iter, &u);
2928
2929                 if (streq(name, "MainPID")) {
2930                         if (u > 0) {
2931                                 i->main_pid = (pid_t) u;
2932                                 i->running = true;
2933                         }
2934                 } else if (streq(name, "ControlPID"))
2935                         i->control_pid = (pid_t) u;
2936                 else if (streq(name, "ExecMainPID")) {
2937                         if (u > 0)
2938                                 i->main_pid = (pid_t) u;
2939                 } else if (streq(name, "NAccepted"))
2940                         i->n_accepted = u;
2941                 else if (streq(name, "NConnections"))
2942                         i->n_connections = u;
2943
2944                 break;
2945         }
2946
2947         case DBUS_TYPE_INT32: {
2948                 int32_t j;
2949
2950                 dbus_message_iter_get_basic(iter, &j);
2951
2952                 if (streq(name, "ExecMainCode"))
2953                         i->exit_code = (int) j;
2954                 else if (streq(name, "ExecMainStatus"))
2955                         i->exit_status = (int) j;
2956
2957                 break;
2958         }
2959
2960         case DBUS_TYPE_UINT64: {
2961                 uint64_t u;
2962
2963                 dbus_message_iter_get_basic(iter, &u);
2964
2965                 if (streq(name, "ExecMainStartTimestamp"))
2966                         i->start_timestamp = (usec_t) u;
2967                 else if (streq(name, "ExecMainExitTimestamp"))
2968                         i->exit_timestamp = (usec_t) u;
2969                 else if (streq(name, "ActiveEnterTimestamp"))
2970                         i->active_enter_timestamp = (usec_t) u;
2971                 else if (streq(name, "InactiveEnterTimestamp"))
2972                         i->inactive_enter_timestamp = (usec_t) u;
2973                 else if (streq(name, "InactiveExitTimestamp"))
2974                         i->inactive_exit_timestamp = (usec_t) u;
2975                 else if (streq(name, "InactiveExitTimestampMonotonic"))
2976                         i->inactive_exit_timestamp_monotonic = (usec_t) u;
2977                 else if (streq(name, "ActiveExitTimestamp"))
2978                         i->active_exit_timestamp = (usec_t) u;
2979                 else if (streq(name, "ConditionTimestamp"))
2980                         i->condition_timestamp = (usec_t) u;
2981
2982                 break;
2983         }
2984
2985         case DBUS_TYPE_ARRAY: {
2986
2987                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2988                     startswith(name, "Exec")) {
2989                         DBusMessageIter sub;
2990
2991                         dbus_message_iter_recurse(iter, &sub);
2992                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2993                                 ExecStatusInfo *info;
2994                                 int r;
2995
2996                                 info = new0(ExecStatusInfo, 1);
2997                                 if (!info)
2998                                         return -ENOMEM;
2999
3000                                 info->name = strdup(name);
3001                                 if (!info->name) {
3002                                         free(info);
3003                                         return -ENOMEM;
3004                                 }
3005
3006                                 r = exec_status_info_deserialize(&sub, info);
3007                                 if (r < 0) {
3008                                         free(info);
3009                                         return r;
3010                                 }
3011
3012                                 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
3013
3014                                 dbus_message_iter_next(&sub);
3015                         }
3016
3017                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
3018                            streq(name, "Listen")) {
3019                         DBusMessageIter sub, sub2;
3020
3021                         dbus_message_iter_recurse(iter, &sub);
3022                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3023                                 const char *type, *path;
3024
3025                                 dbus_message_iter_recurse(&sub, &sub2);
3026
3027                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
3028                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0) {
3029                                         int r;
3030
3031                                         r = strv_extend(&i->listen, type);
3032                                         if (r < 0)
3033                                                 return r;
3034                                         r = strv_extend(&i->listen, path);
3035                                         if (r < 0)
3036                                                 return r;
3037                                 }
3038
3039                                 dbus_message_iter_next(&sub);
3040                         }
3041
3042                         return 0;
3043
3044                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING &&
3045                            streq(name, "DropInPaths")) {
3046                         int r = bus_parse_strv_iter(iter, &i->dropin_paths);
3047                         if (r < 0)
3048                                 return r;
3049
3050                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING &&
3051                            streq(name, "Documentation")) {
3052
3053                         DBusMessageIter sub;
3054
3055                         dbus_message_iter_recurse(iter, &sub);
3056                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING) {
3057                                 const char *s;
3058                                 int r;
3059
3060                                 dbus_message_iter_get_basic(&sub, &s);
3061
3062                                 r = strv_extend(&i->documentation, s);
3063                                 if (r < 0)
3064                                         return r;
3065
3066                                 dbus_message_iter_next(&sub);
3067                         }
3068
3069                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
3070                            streq(name, "Conditions")) {
3071                         DBusMessageIter sub, sub2;
3072
3073                         dbus_message_iter_recurse(iter, &sub);
3074                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3075                                 const char *cond, *param;
3076                                 dbus_bool_t trigger, negate;
3077                                 dbus_int32_t state;
3078
3079                                 dbus_message_iter_recurse(&sub, &sub2);
3080                                 log_debug("here");
3081
3082                                 if(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &cond, true) >= 0 &&
3083                                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &trigger, true) >= 0 &&
3084                                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &negate, true) >= 0 &&
3085                                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &param, true) >= 0 &&
3086                                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &state, false) >= 0) {
3087                                         log_debug("%s %d %d %s %d", cond, trigger, negate, param, state);
3088                                         if (state < 0 && (!trigger || !i->failed_condition)) {
3089                                                 i->failed_condition = cond;
3090                                                 i->failed_condition_trigger = trigger;
3091                                                 i->failed_condition_negate = negate;
3092                                                 i->failed_condition_param = param;
3093                                         }
3094                                 }
3095
3096                                 dbus_message_iter_next(&sub);
3097                         }
3098                 }
3099
3100                 break;
3101         }
3102
3103         case DBUS_TYPE_STRUCT: {
3104
3105                 if (streq(name, "LoadError")) {
3106                         DBusMessageIter sub;
3107                         const char *n, *message;
3108                         int r;
3109
3110                         dbus_message_iter_recurse(iter, &sub);
3111
3112                         r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
3113                         if (r < 0)
3114                                 return r;
3115
3116                         r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
3117                         if (r < 0)
3118                                 return r;
3119
3120                         if (!isempty(message))
3121                                 i->load_error = message;
3122                 }
3123
3124                 break;
3125         }
3126         }
3127
3128         return 0;
3129 }
3130
3131 static int print_property(const char *name, DBusMessageIter *iter) {
3132         assert(name);
3133         assert(iter);
3134
3135         /* This is a low-level property printer, see
3136          * print_status_info() for the nicer output */
3137
3138         if (arg_properties && !strv_find(arg_properties, name))
3139                 return 0;
3140
3141         switch (dbus_message_iter_get_arg_type(iter)) {
3142
3143         case DBUS_TYPE_STRUCT: {
3144                 DBusMessageIter sub;
3145                 dbus_message_iter_recurse(iter, &sub);
3146
3147                 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
3148                         uint32_t u;
3149
3150                         dbus_message_iter_get_basic(&sub, &u);
3151
3152                         if (u)
3153                                 printf("%s=%u\n", name, (unsigned) u);
3154                         else if (arg_all)
3155                                 printf("%s=\n", name);
3156
3157                         return 0;
3158                 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
3159                         const char *s;
3160
3161                         dbus_message_iter_get_basic(&sub, &s);
3162
3163                         if (arg_all || s[0])
3164                                 printf("%s=%s\n", name, s);
3165
3166                         return 0;
3167                 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
3168                         const char *a = NULL, *b = NULL;
3169
3170                         if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
3171                                 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
3172
3173                         if (arg_all || !isempty(a) || !isempty(b))
3174                                 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
3175
3176                         return 0;
3177                 }
3178
3179                 break;
3180         }
3181
3182         case DBUS_TYPE_ARRAY:
3183
3184                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
3185                         DBusMessageIter sub, sub2;
3186
3187                         dbus_message_iter_recurse(iter, &sub);
3188                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3189                                 const char *path;
3190                                 dbus_bool_t ignore;
3191
3192                                 dbus_message_iter_recurse(&sub, &sub2);
3193
3194                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
3195                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
3196                                         printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
3197
3198                                 dbus_message_iter_next(&sub);
3199                         }
3200
3201                         return 0;
3202
3203                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
3204                         DBusMessageIter sub, sub2;
3205
3206                         dbus_message_iter_recurse(iter, &sub);
3207
3208                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3209                                 const char *type, *path;
3210
3211                                 dbus_message_iter_recurse(&sub, &sub2);
3212
3213                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
3214                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
3215                                         printf("%s=%s\n", type, path);
3216
3217                                 dbus_message_iter_next(&sub);
3218                         }
3219
3220                         return 0;
3221
3222                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Listen")) {
3223                         DBusMessageIter sub, sub2;
3224
3225                         dbus_message_iter_recurse(iter, &sub);
3226                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3227                                 const char *type, *path;
3228
3229                                 dbus_message_iter_recurse(&sub, &sub2);
3230
3231                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
3232                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
3233                                         printf("Listen%s=%s\n", type, path);
3234
3235                                 dbus_message_iter_next(&sub);
3236                         }
3237
3238                         return 0;
3239
3240                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
3241                         DBusMessageIter sub, sub2;
3242
3243                         dbus_message_iter_recurse(iter, &sub);
3244                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3245                                 const char *base;
3246                                 uint64_t value, next_elapse;
3247
3248                                 dbus_message_iter_recurse(&sub, &sub2);
3249
3250                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
3251                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
3252                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
3253                                         char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
3254
3255                                         printf("%s={ value=%s ; next_elapse=%s }\n",
3256                                                base,
3257                                                format_timespan(timespan1, sizeof(timespan1), value, 0),
3258                                                format_timespan(timespan2, sizeof(timespan2), next_elapse, 0));
3259                                 }
3260
3261                                 dbus_message_iter_next(&sub);
3262                         }
3263
3264                         return 0;
3265
3266                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
3267                         DBusMessageIter sub;
3268
3269                         dbus_message_iter_recurse(iter, &sub);
3270                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3271                                 ExecStatusInfo info = {};
3272
3273                                 if (exec_status_info_deserialize(&sub, &info) >= 0) {
3274                                         char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
3275                                         _cleanup_free_ char *t;
3276
3277                                         t = strv_join(info.argv, " ");
3278
3279                                         printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
3280                                                name,
3281                                                strna(info.path),
3282                                                strna(t),
3283                                                yes_no(info.ignore),
3284                                                strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
3285                                                strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
3286                                                (unsigned) info. pid,
3287                                                sigchld_code_to_string(info.code),
3288                                                info.status,
3289                                                info.code == CLD_EXITED ? "" : "/",
3290                                                strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
3291                                 }
3292
3293                                 free(info.path);
3294                                 strv_free(info.argv);
3295
3296                                 dbus_message_iter_next(&sub);
3297                         }
3298
3299                         return 0;
3300
3301                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "DeviceAllow")) {
3302                         DBusMessageIter sub, sub2;
3303
3304                         dbus_message_iter_recurse(iter, &sub);
3305                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3306                                 const char *path, *rwm;
3307
3308                                 dbus_message_iter_recurse(&sub, &sub2);
3309
3310                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
3311                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &rwm, false) >= 0)
3312                                         printf("%s=%s %s\n", name, strna(path), strna(rwm));
3313
3314                                 dbus_message_iter_next(&sub);
3315                         }
3316                         return 0;
3317
3318                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && (streq(name, "BlockIOReadBandwidth") || streq(name, "BlockIOWriteBandwidth"))) {
3319                         DBusMessageIter sub, sub2;
3320
3321                         dbus_message_iter_recurse(iter, &sub);
3322                         while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
3323                                 const char *path;
3324                                 uint64_t bandwidth;
3325
3326                                 dbus_message_iter_recurse(&sub, &sub2);
3327
3328                                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
3329                                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &bandwidth, false) >= 0)
3330                                         printf("%s=%s %" PRIu64 "\n", name, strna(path), bandwidth);
3331
3332                                 dbus_message_iter_next(&sub);
3333                         }
3334                         return 0;
3335                 }
3336
3337
3338                 break;
3339         }
3340
3341         if (generic_print_property(name, iter, arg_all) > 0)
3342                 return 0;
3343
3344         if (arg_all)
3345                 printf("%s=[unprintable]\n", name);
3346
3347         return 0;
3348 }
3349
3350 static int show_one(const char *verb,
3351                     DBusConnection *bus,
3352                     const char *path,
3353                     bool show_properties,
3354                     bool *new_line,
3355                     bool *ellipsized) {
3356         _cleanup_free_ DBusMessage *reply = NULL;
3357         const char *interface = "";
3358         int r;
3359         DBusMessageIter iter, sub, sub2, sub3;
3360         UnitStatusInfo info = {};
3361         ExecStatusInfo *p;
3362
3363         assert(path);
3364         assert(new_line);
3365
3366         r = bus_method_call_with_reply(
3367                         bus,
3368                         "org.freedesktop.systemd1",
3369                         path,
3370                         "org.freedesktop.DBus.Properties",
3371                         "GetAll",
3372                         &reply,
3373                         NULL,
3374                         DBUS_TYPE_STRING, &interface,
3375                         DBUS_TYPE_INVALID);
3376         if (r < 0)
3377                 return r;
3378
3379         if (!dbus_message_iter_init(reply, &iter) ||
3380             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3381             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY)  {
3382                 log_error("Failed to parse reply.");
3383                 return -EIO;
3384         }
3385
3386         dbus_message_iter_recurse(&iter, &sub);
3387
3388         if (*new_line)
3389                 printf("\n");
3390
3391         *new_line = true;
3392
3393         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3394                 const char *name;
3395
3396                 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_DICT_ENTRY);
3397                 dbus_message_iter_recurse(&sub, &sub2);
3398
3399                 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
3400                     dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
3401                         log_error("Failed to parse reply.");
3402                         return -EIO;
3403                 }
3404
3405                 dbus_message_iter_recurse(&sub2, &sub3);
3406
3407                 if (show_properties)
3408                         r = print_property(name, &sub3);
3409                 else
3410                         r = status_property(name, &sub3, &info);
3411                 if (r < 0) {
3412                         log_error("Failed to parse reply.");
3413                         return -EIO;
3414                 }
3415
3416                 dbus_message_iter_next(&sub);
3417         }
3418
3419         r = 0;
3420
3421         if (!show_properties) {
3422                 if (streq(verb, "help"))
3423                         show_unit_help(&info);
3424                 else
3425                         print_status_info(&info, ellipsized);
3426         }
3427
3428         strv_free(info.documentation);
3429         strv_free(info.dropin_paths);
3430         strv_free(info.listen);
3431
3432         if (!streq_ptr(info.active_state, "active") &&
3433             !streq_ptr(info.active_state, "reloading") &&
3434             streq(verb, "status")) {
3435                 /* According to LSB: "program not running" */
3436                 /* 0: program is running or service is OK
3437                  * 1: program is dead and /var/run pid file exists
3438                  * 2: program is dead and /var/lock lock file exists
3439                  * 3: program is not running
3440                  * 4: program or service status is unknown
3441                  */
3442                 if (info.pid_file && access(info.pid_file, F_OK) == 0)
3443                         r = 1;
3444                 else
3445                         r = 3;
3446         }
3447
3448         while ((p = info.exec)) {
3449                 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
3450                 exec_status_info_free(p);
3451         }
3452
3453         return r;
3454 }
3455
3456 static int show_one_by_pid(const char *verb,
3457                            DBusConnection *bus,
3458                            uint32_t pid,
3459                            bool *new_line,
3460                            bool *ellipsized) {
3461         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
3462         const char *path = NULL;
3463         _cleanup_dbus_error_free_ DBusError error;
3464         int r;
3465
3466         dbus_error_init(&error);
3467
3468         r = bus_method_call_with_reply(
3469                         bus,
3470                         "org.freedesktop.systemd1",
3471                         "/org/freedesktop/systemd1",
3472                         "org.freedesktop.systemd1.Manager",
3473                         "GetUnitByPID",
3474                         &reply,
3475                         NULL,
3476                         DBUS_TYPE_UINT32, &pid,
3477                         DBUS_TYPE_INVALID);
3478         if (r < 0)
3479                 return r;
3480
3481         if (!dbus_message_get_args(reply, &error,
3482                                    DBUS_TYPE_OBJECT_PATH, &path,
3483                                    DBUS_TYPE_INVALID)) {
3484                 log_error("Failed to parse reply: %s", bus_error_message(&error));
3485                 return -EIO;
3486         }
3487
3488         r = show_one(verb, bus, path, false, new_line, ellipsized);
3489         return r;
3490 }
3491
3492 static int show_all(const char* verb,
3493                     DBusConnection *bus,
3494                     bool show_properties,
3495                     bool *new_line,
3496                     bool *ellipsized) {
3497         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
3498         _cleanup_free_ struct unit_info *unit_infos = NULL;
3499         unsigned c = 0;
3500         const struct unit_info *u;
3501         int r;
3502
3503         r = get_unit_list(bus, &reply, &unit_infos, &c);
3504         if (r < 0)
3505                 return r;
3506
3507         qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
3508
3509         for (u = unit_infos; u < unit_infos + c; u++) {
3510                 _cleanup_free_ char *p = NULL;
3511
3512                 if (!output_show_unit(u))
3513                         continue;
3514
3515                 p = unit_dbus_path_from_name(u->id);
3516                 if (!p)
3517                         return log_oom();
3518
3519                 printf("%s -> '%s'\n", u->id, p);
3520
3521                 r = show_one(verb, bus, p, show_properties, new_line, ellipsized);
3522                 if (r != 0)
3523                         return r;
3524         }
3525
3526         return 0;
3527 }
3528
3529 static int show(DBusConnection *bus, char **args) {
3530         int r, ret = 0;
3531         bool show_properties, show_status, new_line = false;
3532         char **name;
3533         bool ellipsized = false;
3534
3535         assert(bus);
3536         assert(args);
3537
3538         show_properties = streq(args[0], "show");
3539         show_status = streq(args[0], "status");
3540
3541         if (show_properties)
3542                 pager_open_if_enabled();
3543
3544         /* If no argument is specified inspect the manager itself */
3545
3546         if (show_properties && strv_length(args) <= 1)
3547                 return show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line, &ellipsized);
3548
3549         if (show_status && strv_length(args) <= 1)
3550                 ret = show_all(args[0], bus, false, &new_line, &ellipsized);
3551         else
3552                 STRV_FOREACH(name, args+1) {
3553                         uint32_t id;
3554
3555                         if (safe_atou32(*name, &id) < 0) {
3556                                 _cleanup_free_ char *p = NULL, *n = NULL;
3557                                 /* Interpret as unit name */
3558
3559                                 n = unit_name_mangle(*name);
3560                                 if (!n)
3561                                         return log_oom();
3562
3563                                 p = unit_dbus_path_from_name(n);
3564                                 if (!p)
3565                                         return log_oom();
3566
3567                                 r = show_one(args[0], bus, p, show_properties, &new_line, &ellipsized);
3568                                 if (r != 0)
3569                                         ret = r;
3570
3571                         } else if (show_properties) {
3572                                 _cleanup_free_ char *p = NULL;
3573
3574                                 /* Interpret as job id */
3575                                 if (asprintf(&p, "/org/freedesktop/systemd1/job/%u", id) < 0)
3576                                         return log_oom();
3577
3578                                 r = show_one(args[0], bus, p, show_properties, &new_line, &ellipsized);
3579                                 if (r != 0)
3580                                         ret = r;
3581
3582                         } else {
3583                                 /* Interpret as PID */
3584                                 r = show_one_by_pid(args[0], bus, id, &new_line, &ellipsized);
3585                                 if (r != 0)
3586                                         ret = r;
3587                         }
3588                 }
3589
3590         if (ellipsized && !arg_quiet)
3591                 printf("Hint: Some lines were ellipsized, use -l to show in full.\n");
3592
3593         return ret;
3594 }
3595
3596 static int append_assignment(DBusMessageIter *iter, const char *assignment) {
3597         const char *eq;
3598         char *field;
3599         DBusMessageIter sub;
3600         int r;
3601
3602         assert(iter);
3603         assert(assignment);
3604
3605         eq = strchr(assignment, '=');
3606         if (!eq) {
3607                 log_error("Not an assignment: %s", assignment);
3608                 return -EINVAL;
3609         }
3610
3611         field = strndupa(assignment, eq - assignment);
3612         eq ++;
3613
3614         if (!dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &field))
3615                 return log_oom();
3616
3617         if (streq(field, "CPUAccounting") ||
3618             streq(field, "MemoryAccounting") ||
3619             streq(field, "BlockIOAccounting")) {
3620                 dbus_bool_t b;
3621
3622                 r = parse_boolean(eq);
3623                 if (r < 0) {
3624                         log_error("Failed to parse boolean assignment %s.", assignment);
3625                         return -EINVAL;
3626                 }
3627
3628                 b = r;
3629                 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "b", &sub) ||
3630                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_BOOLEAN, &b))
3631                         return log_oom();
3632
3633         } else if (streq(field, "MemoryLimit") || streq(field, "MemorySoftLimit")) {
3634                 off_t bytes;
3635                 uint64_t u;
3636
3637                 r = parse_bytes(eq, &bytes);
3638                 if (r < 0) {
3639                         log_error("Failed to parse bytes specification %s", assignment);
3640                         return -EINVAL;
3641                 }
3642
3643                 u = bytes;
3644                 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "t", &sub) ||
3645                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT64, &u))
3646                         return log_oom();
3647
3648         } else if (streq(field, "CPUShares") || streq(field, "BlockIOWeight")) {
3649                 uint64_t u;
3650
3651                 r = safe_atou64(eq, &u);
3652                 if (r < 0) {
3653                         log_error("Failed to parse %s value %s.", field, eq);
3654                         return -EINVAL;
3655                 }
3656
3657                 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "t", &sub) ||
3658                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT64, &u))
3659                         return log_oom();
3660
3661         } else if (streq(field, "DevicePolicy")) {
3662
3663                 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "s", &sub) ||
3664                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &eq))
3665                         return log_oom();
3666
3667         } else if (streq(field, "DeviceAllow")) {
3668                 DBusMessageIter sub2;
3669
3670                 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a(ss)", &sub) ||
3671                     !dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "(ss)", &sub2))
3672                         return log_oom();
3673
3674                 if (!isempty(eq)) {
3675                         const char *path, *rwm;
3676                         DBusMessageIter sub3;
3677                         char *e;
3678
3679                         e = strchr(eq, ' ');
3680                         if (e) {
3681                                 path = strndupa(eq, e - eq);
3682                                 rwm = e+1;
3683                         } else {
3684                                 path = eq;
3685                                 rwm = "";
3686                         }
3687
3688                         if (!path_startswith(path, "/dev")) {
3689                                 log_error("%s is not a device file in /dev.", path);
3690                                 return -EINVAL;
3691                         }
3692
3693                         if (!dbus_message_iter_open_container(&sub2, DBUS_TYPE_STRUCT, NULL, &sub3) ||
3694                             !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &path) ||
3695                             !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &rwm) ||
3696                             !dbus_message_iter_close_container(&sub2, &sub3))
3697                                 return log_oom();
3698                 }
3699
3700                 if (!dbus_message_iter_close_container(&sub, &sub2))
3701                         return log_oom();
3702
3703         } else if (streq(field, "BlockIOReadBandwidth") || streq(field, "BlockIOWriteBandwidth")) {
3704                 DBusMessageIter sub2;
3705
3706                 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a(st)", &sub) ||
3707                     !dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "(st)", &sub2))
3708                         return log_oom();
3709
3710                 if (!isempty(eq)) {
3711                         const char *path, *bandwidth;
3712                         DBusMessageIter sub3;
3713                         uint64_t u;
3714                         off_t bytes;
3715                         char *e;
3716
3717                         e = strchr(eq, ' ');
3718                         if (e) {
3719                                 path = strndupa(eq, e - eq);
3720                                 bandwidth = e+1;
3721                         } else {
3722                                 log_error("Failed to parse %s value %s.", field, eq);
3723                                 return -EINVAL;
3724                         }
3725
3726                         if (!path_startswith(path, "/dev")) {
3727                                 log_error("%s is not a device file in /dev.", path);
3728                                 return -EINVAL;
3729                         }
3730
3731                         r = parse_bytes(bandwidth, &bytes);
3732                         if (r < 0) {
3733                                 log_error("Failed to parse byte value %s.", bandwidth);
3734                                 return -EINVAL;
3735                         }
3736
3737                         u = (uint64_t) bytes;
3738
3739                         if (!dbus_message_iter_open_container(&sub2, DBUS_TYPE_STRUCT, NULL, &sub3) ||
3740                             !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &path) ||
3741                             !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_UINT64, &u) ||
3742                             !dbus_message_iter_close_container(&sub2, &sub3))
3743                                 return log_oom();
3744                 }
3745
3746                 if (!dbus_message_iter_close_container(&sub, &sub2))
3747                         return log_oom();
3748
3749         } else if (streq(field, "BlockIODeviceWeight")) {
3750                 DBusMessageIter sub2;
3751
3752                 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a(st)", &sub) ||
3753                     !dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "(st)", &sub2))
3754                         return log_oom();
3755
3756                 if (!isempty(eq)) {
3757                         const char *path, *weight;
3758                         DBusMessageIter sub3;
3759                         uint64_t u;
3760                         char *e;
3761
3762                         e = strchr(eq, ' ');
3763                         if (e) {
3764                                 path = strndupa(eq, e - eq);
3765                                 weight = e+1;
3766                         } else {
3767                                 log_error("Failed to parse %s value %s.", field, eq);
3768                                 return -EINVAL;
3769                         }
3770
3771                         if (!path_startswith(path, "/dev")) {
3772                                 log_error("%s is not a device file in /dev.", path);
3773                                 return -EINVAL;
3774                         }
3775
3776                         r = safe_atou64(weight, &u);
3777                         if (r < 0) {
3778                                 log_error("Failed to parse %s value %s.", field, weight);
3779                                 return -EINVAL;
3780                         }
3781                         if (!dbus_message_iter_open_container(&sub2, DBUS_TYPE_STRUCT, NULL, &sub3) ||
3782                             !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &path) ||
3783                             !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_UINT64, &u) ||
3784                             !dbus_message_iter_close_container(&sub2, &sub3))
3785                                 return log_oom();
3786                 }
3787
3788                 if (!dbus_message_iter_close_container(&sub, &sub2))
3789                         return log_oom();
3790
3791         } else {
3792                 log_error("Unknown assignment %s.", assignment);
3793                 return -EINVAL;
3794         }
3795
3796         if (!dbus_message_iter_close_container(iter, &sub))
3797                 return log_oom();
3798
3799         return 0;
3800 }
3801
3802 static int set_property(DBusConnection *bus, char **args) {
3803
3804         _cleanup_free_ DBusMessage *m = NULL, *reply = NULL;
3805         DBusMessageIter iter, sub;
3806         dbus_bool_t runtime;
3807         DBusError error;
3808         char **i;
3809         int r;
3810
3811         dbus_error_init(&error);
3812
3813         m = dbus_message_new_method_call(
3814                         "org.freedesktop.systemd1",
3815                         "/org/freedesktop/systemd1",
3816                         "org.freedesktop.systemd1.Manager",
3817                         "SetUnitProperties");
3818         if (!m)
3819                 return log_oom();
3820
3821         dbus_message_iter_init_append(m, &iter);
3822
3823         runtime = arg_runtime;
3824
3825         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args[1]) ||
3826             !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &runtime) ||
3827             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sv)", &sub))
3828                 return log_oom();
3829
3830         STRV_FOREACH(i, args + 2) {
3831                 DBusMessageIter sub2;
3832
3833                 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
3834                         return log_oom();
3835
3836                 r = append_assignment(&sub2, *i);
3837                 if (r < 0)
3838                         return r;
3839
3840                 if (!dbus_message_iter_close_container(&sub, &sub2))
3841                         return log_oom();
3842
3843         }
3844
3845         if (!dbus_message_iter_close_container(&iter, &sub))
3846                 return log_oom();
3847
3848         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3849         if (!reply) {
3850                 log_error("Failed to issue method call: %s", bus_error_message(&error));
3851                 dbus_error_free(&error);
3852                 return -EIO;
3853         }
3854
3855         return 0;
3856 }
3857
3858 static int snapshot(DBusConnection *bus, char **args) {
3859         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
3860         DBusError error;
3861         int r;
3862         dbus_bool_t cleanup = FALSE;
3863         DBusMessageIter iter, sub;
3864         const char
3865                 *path, *id,
3866                 *interface = "org.freedesktop.systemd1.Unit",
3867                 *property = "Id";
3868         _cleanup_free_ char *n = NULL;
3869
3870         dbus_error_init(&error);
3871
3872         if (strv_length(args) > 1)
3873                 n = unit_name_mangle_with_suffix(args[1], ".snapshot");
3874         else
3875                 n = strdup("");
3876         if (!n)
3877                 return log_oom();
3878
3879         r = bus_method_call_with_reply(
3880                         bus,
3881                         "org.freedesktop.systemd1",
3882                         "/org/freedesktop/systemd1",
3883                         "org.freedesktop.systemd1.Manager",
3884                         "CreateSnapshot",
3885                         &reply,
3886                         NULL,
3887                         DBUS_TYPE_STRING, &n,
3888                         DBUS_TYPE_BOOLEAN, &cleanup,
3889                         DBUS_TYPE_INVALID);
3890         if (r < 0)
3891                 return r;
3892
3893         if (!dbus_message_get_args(reply, &error,
3894                                    DBUS_TYPE_OBJECT_PATH, &path,
3895                                    DBUS_TYPE_INVALID)) {
3896                 log_error("Failed to parse reply: %s", bus_error_message(&error));
3897                 dbus_error_free(&error);
3898                 return -EIO;
3899         }
3900
3901         dbus_message_unref(reply);
3902         reply = NULL;
3903
3904         r = bus_method_call_with_reply (
3905                         bus,
3906                         "org.freedesktop.systemd1",
3907                         path,
3908                         "org.freedesktop.DBus.Properties",
3909                         "Get",
3910                         &reply,
3911                         NULL,
3912                         DBUS_TYPE_STRING, &interface,
3913                         DBUS_TYPE_STRING, &property,
3914                         DBUS_TYPE_INVALID);
3915         if (r < 0)
3916                 return r;
3917
3918         if (!dbus_message_iter_init(reply, &iter) ||
3919             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
3920                 log_error("Failed to parse reply.");
3921                 return -EIO;
3922         }
3923
3924         dbus_message_iter_recurse(&iter, &sub);
3925
3926         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
3927                 log_error("Failed to parse reply.");
3928                 return -EIO;
3929         }
3930
3931         dbus_message_iter_get_basic(&sub, &id);
3932
3933         if (!arg_quiet)
3934                 puts(id);
3935
3936         return 0;
3937 }
3938
3939 static int delete_snapshot(DBusConnection *bus, char **args) {
3940         char **name;
3941
3942         assert(args);
3943
3944         STRV_FOREACH(name, args+1) {
3945                 _cleanup_free_ char *n = NULL;
3946                 int r;
3947
3948                 n = unit_name_mangle_with_suffix(*name, ".snapshot");
3949                 if (!n)
3950                         return log_oom();
3951
3952                 r = bus_method_call_with_reply(
3953                                 bus,
3954                                 "org.freedesktop.systemd1",
3955                                 "/org/freedesktop/systemd1",
3956                                 "org.freedesktop.systemd1.Manager",
3957                                 "RemoveSnapshot",
3958                                 NULL,
3959                                 NULL,
3960                                 DBUS_TYPE_STRING, &n,
3961                                 DBUS_TYPE_INVALID);
3962                 if (r < 0)
3963                         return r;
3964         }
3965
3966         return 0;
3967 }
3968
3969 static int daemon_reload(DBusConnection *bus, char **args) {
3970         int r;
3971         const char *method;
3972         DBusError error;
3973
3974         if (arg_action == ACTION_RELOAD)
3975                 method = "Reload";
3976         else if (arg_action == ACTION_REEXEC)
3977                 method = "Reexecute";
3978         else {
3979                 assert(arg_action == ACTION_SYSTEMCTL);
3980
3981                 method =
3982                         streq(args[0], "clear-jobs")    ||
3983                         streq(args[0], "cancel")        ? "ClearJobs" :
3984                         streq(args[0], "daemon-reexec") ? "Reexecute" :
3985                         streq(args[0], "reset-failed")  ? "ResetFailed" :
3986                         streq(args[0], "halt")          ? "Halt" :
3987                         streq(args[0], "poweroff")      ? "PowerOff" :
3988                         streq(args[0], "reboot")        ? "Reboot" :
3989                         streq(args[0], "kexec")         ? "KExec" :
3990                         streq(args[0], "exit")          ? "Exit" :
3991                                     /* "daemon-reload" */ "Reload";
3992         }
3993
3994         r = bus_method_call_with_reply(
3995                         bus,
3996                         "org.freedesktop.systemd1",
3997                         "/org/freedesktop/systemd1",
3998                         "org.freedesktop.systemd1.Manager",
3999                         method,
4000                         NULL,
4001                         &error,
4002                         DBUS_TYPE_INVALID);
4003
4004         if (r == -ENOENT && arg_action != ACTION_SYSTEMCTL)
4005                 /* There's always a fallback possible for
4006                  * legacy actions. */
4007                 r = -EADDRNOTAVAIL;
4008         else if ((r == -ETIMEDOUT || r == -ECONNRESET) && streq(method, "Reexecute"))
4009                 /* On reexecution, we expect a disconnect, not a
4010                  * reply */
4011                 r = 0;
4012         else if (r < 0)
4013                 log_error("Failed to issue method call: %s", bus_error_message(&error));
4014
4015         dbus_error_free(&error);
4016         return r;
4017 }
4018
4019 static int reset_failed(DBusConnection *bus, char **args) {
4020         int r = 0;
4021         char **name;
4022
4023         if (strv_length(args) <= 1)
4024                 return daemon_reload(bus, args);
4025
4026         STRV_FOREACH(name, args+1) {
4027                 _cleanup_free_ char *n;
4028
4029                 n = unit_name_mangle(*name);
4030                 if (!n)
4031                         return log_oom();
4032
4033                 r = bus_method_call_with_reply(
4034                                 bus,
4035                                 "org.freedesktop.systemd1",
4036                                 "/org/freedesktop/systemd1",
4037                                 "org.freedesktop.systemd1.Manager",
4038                                 "ResetFailedUnit",
4039                                 NULL,
4040                                 NULL,
4041                                 DBUS_TYPE_STRING, &n,
4042                                 DBUS_TYPE_INVALID);
4043                 if (r < 0)
4044                         return r;
4045         }
4046
4047         return 0;
4048 }
4049
4050 static int show_enviroment(DBusConnection *bus, char **args) {
4051         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
4052         DBusMessageIter iter, sub, sub2;
4053         int r;
4054         const char
4055                 *interface = "org.freedesktop.systemd1.Manager",
4056                 *property = "Environment";
4057
4058         pager_open_if_enabled();
4059
4060         r = bus_method_call_with_reply(
4061                         bus,
4062                         "org.freedesktop.systemd1",
4063                         "/org/freedesktop/systemd1",
4064                         "org.freedesktop.DBus.Properties",
4065                         "Get",
4066                         &reply,
4067                         NULL,
4068                         DBUS_TYPE_STRING, &interface,
4069                         DBUS_TYPE_STRING, &property,
4070                         DBUS_TYPE_INVALID);
4071         if (r < 0)
4072                 return r;
4073
4074         if (!dbus_message_iter_init(reply, &iter) ||
4075             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
4076                 log_error("Failed to parse reply.");
4077                 return -EIO;
4078         }
4079
4080         dbus_message_iter_recurse(&iter, &sub);
4081
4082         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
4083             dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING)  {
4084                 log_error("Failed to parse reply.");
4085                 return -EIO;
4086         }
4087
4088         dbus_message_iter_recurse(&sub, &sub2);
4089
4090         while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
4091                 const char *text;
4092
4093                 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
4094                         log_error("Failed to parse reply.");
4095                         return -EIO;
4096                 }
4097
4098                 dbus_message_iter_get_basic(&sub2, &text);
4099                 puts(text);
4100
4101                 dbus_message_iter_next(&sub2);
4102         }
4103
4104         return 0;
4105 }
4106
4107 static int switch_root(DBusConnection *bus, char **args) {
4108         unsigned l;
4109         const char *root;
4110         _cleanup_free_ char *init = NULL;
4111
4112         l = strv_length(args);
4113         if (l < 2 || l > 3) {
4114                 log_error("Wrong number of arguments.");
4115                 return -EINVAL;
4116         }
4117
4118         root = args[1];
4119
4120         if (l >= 3)
4121                 init = strdup(args[2]);
4122         else {
4123                 parse_env_file("/proc/cmdline", WHITESPACE,
4124                                "init", &init,
4125                                NULL);
4126
4127                 if (!init)
4128                         init = strdup("");
4129         }
4130         if (!init)
4131                 return log_oom();
4132
4133         log_debug("switching root - root: %s; init: %s", root, init);
4134
4135         return bus_method_call_with_reply(
4136                         bus,
4137                         "org.freedesktop.systemd1",
4138                         "/org/freedesktop/systemd1",
4139                         "org.freedesktop.systemd1.Manager",
4140                         "SwitchRoot",
4141                         NULL,
4142                         NULL,
4143                         DBUS_TYPE_STRING, &root,
4144                         DBUS_TYPE_STRING, &init,
4145                         DBUS_TYPE_INVALID);
4146 }
4147
4148 static int set_environment(DBusConnection *bus, char **args) {
4149         _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL;
4150         DBusError error;
4151         const char *method;
4152         DBusMessageIter iter;
4153         int r;
4154
4155         assert(bus);
4156         assert(args);
4157
4158         dbus_error_init(&error);
4159
4160         method = streq(args[0], "set-environment")
4161                 ? "SetEnvironment"
4162                 : "UnsetEnvironment";
4163
4164         m = dbus_message_new_method_call(
4165                         "org.freedesktop.systemd1",
4166                         "/org/freedesktop/systemd1",
4167                         "org.freedesktop.systemd1.Manager",
4168                         method);
4169         if (!m)
4170                 return log_oom();
4171
4172         dbus_message_iter_init_append(m, &iter);
4173
4174         r = bus_append_strv_iter(&iter, args + 1);
4175         if (r < 0)
4176                 return log_oom();
4177
4178         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
4179         if (!reply) {
4180                 log_error("Failed to issue method call: %s", bus_error_message(&error));
4181                 dbus_error_free(&error);
4182                 return -EIO;
4183         }
4184
4185         return 0;
4186 }
4187
4188 static int enable_sysv_units(char **args) {
4189         int r = 0;
4190
4191 #if defined(HAVE_SYSV_COMPAT) && defined(HAVE_CHKCONFIG)
4192         const char *verb = args[0];
4193         unsigned f = 1, t = 1;
4194         LookupPaths paths = {};
4195
4196         if (arg_scope != UNIT_FILE_SYSTEM)
4197                 return 0;
4198
4199         if (!streq(verb, "enable") &&
4200             !streq(verb, "disable") &&
4201             !streq(verb, "is-enabled"))
4202                 return 0;
4203
4204         /* Processes all SysV units, and reshuffles the array so that
4205          * afterwards only the native units remain */
4206
4207         r = lookup_paths_init(&paths, SYSTEMD_SYSTEM, false, NULL, NULL, NULL);
4208         if (r < 0)
4209                 return r;
4210
4211         r = 0;
4212         for (f = 1; args[f]; f++) {
4213                 const char *name;
4214                 _cleanup_free_ char *p = NULL, *q = NULL;
4215                 bool found_native = false, found_sysv;
4216                 unsigned c = 1;
4217                 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
4218                 char **k, *l;
4219                 int j;
4220                 pid_t pid;
4221                 siginfo_t status;
4222
4223                 name = args[f];
4224
4225                 if (!endswith(name, ".service"))
4226                         continue;
4227
4228                 if (path_is_absolute(name))
4229                         continue;
4230
4231                 STRV_FOREACH(k, paths.unit_path) {
4232                         if (!isempty(arg_root))
4233                                 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
4234                         else
4235                                 asprintf(&p, "%s/%s", *k, name);
4236
4237                         if (!p) {
4238                                 r = log_oom();
4239                                 goto finish;
4240                         }
4241
4242                         found_native = access(p, F_OK) >= 0;
4243                         free(p);
4244                         p = NULL;
4245
4246                         if (found_native)
4247                                 break;
4248                 }
4249
4250                 if (found_native)
4251                         continue;
4252
4253                 if (!isempty(arg_root))
4254                         asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
4255                 else
4256                         asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
4257                 if (!p) {
4258                         r = log_oom();
4259                         goto finish;
4260                 }
4261
4262                 p[strlen(p) - sizeof(".service") + 1] = 0;
4263                 found_sysv = access(p, F_OK) >= 0;
4264
4265                 if (!found_sysv)
4266                         continue;
4267
4268                 /* Mark this entry, so that we don't try enabling it as native unit */
4269                 args[f] = (char*) "";
4270
4271                 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
4272
4273                 if (!isempty(arg_root))
4274                         argv[c++] = q = strappend("--root=", arg_root);
4275
4276                 argv[c++] = path_get_file_name(p);
4277                 argv[c++] =
4278                         streq(verb, "enable") ? "on" :
4279                         streq(verb, "disable") ? "off" : "--level=5";
4280                 argv[c] = NULL;
4281
4282                 l = strv_join((char**)argv, " ");
4283                 if (!l) {
4284                         r = log_oom();
4285                         goto finish;
4286                 }
4287
4288                 log_info("Executing %s", l);
4289                 free(l);
4290
4291                 pid = fork();
4292                 if (pid < 0) {
4293                         log_error("Failed to fork: %m");
4294                         r = -errno;
4295                         goto finish;
4296                 } else if (pid == 0) {
4297                         /* Child */
4298
4299                         execv(argv[0], (char**) argv);
4300                         _exit(EXIT_FAILURE);
4301                 }
4302
4303                 j = wait_for_terminate(pid, &status);
4304                 if (j < 0) {
4305                         log_error("Failed to wait for child: %s", strerror(-r));
4306                         r = j;
4307                         goto finish;
4308                 }
4309
4310                 if (status.si_code == CLD_EXITED) {
4311                         if (streq(verb, "is-enabled")) {
4312                                 if (status.si_status == 0) {
4313                                         if (!arg_quiet)
4314                                                 puts("enabled");
4315                                         r = 1;
4316                                 } else {
4317                                         if (!arg_quiet)
4318                                                 puts("disabled");
4319                                 }
4320
4321                         } else if (status.si_status != 0) {
4322                                 r = -EINVAL;
4323                                 goto finish;
4324                         }
4325                 } else {
4326                         r = -EPROTO;
4327                         goto finish;
4328                 }
4329         }
4330
4331 finish:
4332         lookup_paths_free(&paths);
4333
4334         /* Drop all SysV units */
4335         for (f = 1, t = 1; args[f]; f++) {
4336
4337                 if (isempty(args[f]))
4338                         continue;
4339
4340                 args[t++] = args[f];
4341         }
4342
4343         args[t] = NULL;
4344
4345 #endif
4346         return r;
4347 }
4348
4349 static int mangle_names(char **original_names, char ***mangled_names) {
4350         char **i, **l, **name;
4351
4352         l = new(char*, strv_length(original_names) + 1);
4353         if (!l)
4354                 return log_oom();
4355
4356         i = l;
4357         STRV_FOREACH(name, original_names) {
4358
4359                 /* When enabling units qualified path names are OK,
4360                  * too, hence allow them explicitly. */
4361
4362                 if (is_path(*name))
4363                         *i = strdup(*name);
4364                 else
4365                         *i = unit_name_mangle(*name);
4366
4367                 if (!*i) {
4368                         strv_free(l);
4369                         return log_oom();
4370                 }
4371
4372                 i++;
4373         }
4374
4375         *i = NULL;
4376         *mangled_names = l;
4377
4378         return 0;
4379 }
4380
4381 static int enable_unit(DBusConnection *bus, char **args) {
4382         const char *verb = args[0];
4383         UnitFileChange *changes = NULL;
4384         unsigned n_changes = 0, i;
4385         int carries_install_info = -1;
4386         _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL;
4387         int r;
4388         _cleanup_dbus_error_free_ DBusError error;
4389         _cleanup_strv_free_ char **mangled_names = NULL;
4390
4391         dbus_error_init(&error);
4392
4393         r = enable_sysv_units(args);
4394         if (r < 0)
4395                 return r;
4396
4397         if (!args[1])
4398                 return 0;
4399
4400         r = mangle_names(args+1, &mangled_names);
4401         if (r < 0)
4402                 goto finish;
4403
4404         if (!bus || avoid_bus()) {
4405                 if (streq(verb, "enable")) {
4406                         r = unit_file_enable(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4407                         carries_install_info = r;
4408                 } else if (streq(verb, "disable"))
4409                         r = unit_file_disable(arg_scope, arg_runtime, arg_root, mangled_names, &changes, &n_changes);
4410                 else if (streq(verb, "reenable")) {
4411                         r = unit_file_reenable(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4412                         carries_install_info = r;
4413                 } else if (streq(verb, "link"))
4414                         r = unit_file_link(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4415                 else if (streq(verb, "preset")) {
4416                         r = unit_file_preset(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4417                         carries_install_info = r;
4418                 } else if (streq(verb, "mask"))
4419                         r = unit_file_mask(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4420                 else if (streq(verb, "unmask"))
4421                         r = unit_file_unmask(arg_scope, arg_runtime, arg_root, mangled_names, &changes, &n_changes);
4422                 else if (streq(verb, "set-default"))
4423                         r = unit_file_set_default(arg_scope, arg_root, args[1], &changes, &n_changes);
4424                 else
4425                         assert_not_reached("Unknown verb");
4426
4427                 if (r < 0) {
4428                         log_error("Operation failed: %s", strerror(-r));
4429                         goto finish;
4430                 }
4431
4432                 if (!arg_quiet) {
4433                         for (i = 0; i < n_changes; i++) {
4434                                 if (changes[i].type == UNIT_FILE_SYMLINK)
4435                                         log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
4436                                 else
4437                                         log_info("rm '%s'", changes[i].path);
4438                         }
4439                 }
4440
4441                 r = 0;
4442         } else {
4443                 const char *method;
4444                 bool send_force = true, expect_carries_install_info = false;
4445                 dbus_bool_t a, b;
4446                 DBusMessageIter iter, sub, sub2;
4447
4448                 if (streq(verb, "enable")) {
4449                         method = "EnableUnitFiles";
4450                         expect_carries_install_info = true;
4451                 } else if (streq(verb, "disable")) {
4452                         method = "DisableUnitFiles";
4453                         send_force = false;
4454                 } else if (streq(verb, "reenable")) {
4455                         method = "ReenableUnitFiles";
4456                         expect_carries_install_info = true;
4457                 } else if (streq(verb, "link"))
4458                         method = "LinkUnitFiles";
4459                 else if (streq(verb, "preset")) {
4460                         method = "PresetUnitFiles";
4461                         expect_carries_install_info = true;
4462                 } else if (streq(verb, "mask"))
4463                         method = "MaskUnitFiles";
4464                 else if (streq(verb, "unmask")) {
4465                         method = "UnmaskUnitFiles";
4466                         send_force = false;
4467                 } else if (streq(verb, "set-default")) {
4468                         method = "SetDefaultTarget";
4469                 } else
4470                         assert_not_reached("Unknown verb");
4471
4472                 m = dbus_message_new_method_call(
4473                                 "org.freedesktop.systemd1",
4474                                 "/org/freedesktop/systemd1",
4475                                 "org.freedesktop.systemd1.Manager",
4476                                 method);
4477                 if (!m) {
4478                         r = log_oom();
4479                         goto finish;
4480                 }
4481
4482                 dbus_message_iter_init_append(m, &iter);
4483
4484                 r = bus_append_strv_iter(&iter, mangled_names);
4485                 if (r < 0) {
4486                         log_error("Failed to append unit files.");
4487                         goto finish;
4488                 }
4489
4490                 a = arg_runtime;
4491                 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
4492                         log_error("Failed to append runtime boolean.");
4493                         r = -ENOMEM;
4494                         goto finish;
4495                 }
4496
4497                 if (send_force) {
4498                         b = arg_force;
4499
4500                         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
4501                                 log_error("Failed to append force boolean.");
4502                                 r = -ENOMEM;
4503                                 goto finish;
4504                         }
4505                 }
4506
4507                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
4508                 if (!reply) {
4509                         log_error("Failed to issue method call: %s", bus_error_message(&error));
4510                         r = -EIO;
4511                         goto finish;
4512                 }
4513
4514                 if (!dbus_message_iter_init(reply, &iter)) {
4515                         log_error("Failed to initialize iterator.");
4516                         goto finish;
4517                 }
4518
4519                 if (expect_carries_install_info) {
4520                         r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
4521                         if (r < 0) {
4522                                 log_error("Failed to parse reply.");
4523                                 goto finish;
4524                         }
4525
4526                         carries_install_info = b;
4527                 }
4528
4529                 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
4530                     dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
4531                         log_error("Failed to parse reply.");
4532                         r = -EIO;
4533                         goto finish;
4534                 }
4535
4536                 dbus_message_iter_recurse(&iter, &sub);
4537                 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
4538                         const char *type, *path, *source;
4539
4540                         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
4541                                 log_error("Failed to parse reply.");
4542                                 r = -EIO;
4543                                 goto finish;
4544                         }
4545
4546                         dbus_message_iter_recurse(&sub, &sub2);
4547
4548                         if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
4549                             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
4550                             bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
4551                                 log_error("Failed to parse reply.");
4552                                 r = -EIO;
4553                                 goto finish;
4554                         }
4555
4556                         if (!arg_quiet) {
4557                                 if (streq(type, "symlink"))
4558                                         log_info("ln -s '%s' '%s'", source, path);
4559                                 else
4560                                         log_info("rm '%s'", path);
4561                         }
4562
4563                         dbus_message_iter_next(&sub);
4564                 }
4565
4566                 /* Try to reload if enabeld */
4567                 if (!arg_no_reload)
4568                         r = daemon_reload(bus, args);
4569         }
4570
4571         if (carries_install_info == 0)
4572                 log_warning("The unit files have no [Install] section. They are not meant to be enabled\n"
4573                             "using systemctl.\n"
4574                             "Possible reasons for having this kind of units are:\n"
4575                             "1) A unit may be statically enabled by being symlinked from another unit's\n"
4576                             "   .wants/ or .requires/ directory.\n"
4577                             "2) A unit's purpose may be to act as a helper for some other unit which has\n"
4578                             "   a requirement dependency on it.\n"
4579                             "3) A unit may be started when needed via activation (socket, path, timer,\n"
4580                             "   D-Bus, udev, scripted systemctl call, ...).\n");
4581
4582 finish:
4583         unit_file_changes_free(changes, n_changes);
4584
4585         return r;
4586 }
4587
4588 static int unit_is_enabled(DBusConnection *bus, char **args) {
4589         _cleanup_dbus_error_free_ DBusError error;
4590         int r;
4591         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
4592         bool enabled;
4593         char **name;
4594         char *n;
4595
4596         dbus_error_init(&error);
4597
4598         r = enable_sysv_units(args);
4599         if (r < 0)
4600                 return r;
4601
4602         enabled = r > 0;
4603
4604         if (!bus || avoid_bus()) {
4605
4606                 STRV_FOREACH(name, args+1) {
4607                         UnitFileState state;
4608
4609                         n = unit_name_mangle(*name);
4610                         if (!n)
4611                                 return log_oom();
4612
4613                         state = unit_file_get_state(arg_scope, arg_root, n);
4614
4615                         free(n);
4616
4617                         if (state < 0)
4618                                 return state;
4619
4620                         if (state == UNIT_FILE_ENABLED ||
4621                             state == UNIT_FILE_ENABLED_RUNTIME ||
4622                             state == UNIT_FILE_STATIC)
4623                                 enabled = true;
4624
4625                         if (!arg_quiet)
4626                                 puts(unit_file_state_to_string(state));
4627                 }
4628
4629         } else {
4630                 STRV_FOREACH(name, args+1) {
4631                         const char *s;
4632
4633                         n = unit_name_mangle(*name);
4634                         if (!n)
4635                                 return log_oom();
4636
4637                         r = bus_method_call_with_reply (
4638                                         bus,
4639                                         "org.freedesktop.systemd1",
4640                                         "/org/freedesktop/systemd1",
4641                                         "org.freedesktop.systemd1.Manager",
4642                                         "GetUnitFileState",
4643                                         &reply,
4644                                         NULL,
4645                                         DBUS_TYPE_STRING, &n,
4646                                         DBUS_TYPE_INVALID);
4647
4648                         free(n);
4649
4650                         if (r)
4651                                 return r;
4652
4653                         if (!dbus_message_get_args(reply, &error,
4654                                                    DBUS_TYPE_STRING, &s,
4655                                                    DBUS_TYPE_INVALID)) {
4656                                 log_error("Failed to parse reply: %s", bus_error_message(&error));
4657                                 return -EIO;
4658                         }
4659
4660                         dbus_message_unref(reply);
4661                         reply = NULL;
4662
4663                         if (streq(s, "enabled") ||
4664                             streq(s, "enabled-runtime") ||
4665                             streq(s, "static"))
4666                                 enabled = true;
4667
4668                         if (!arg_quiet)
4669                                 puts(s);
4670                 }
4671         }
4672
4673         return enabled ? 0 : 1;
4674 }
4675
4676 static int systemctl_help(void) {
4677
4678         pager_open_if_enabled();
4679
4680         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4681                "Query or send control commands to the systemd manager.\n\n"
4682                "  -h --help           Show this help\n"
4683                "     --version        Show package version\n"
4684                "  -t --type=TYPE      List only units of a particular type\n"
4685                "     --state=STATE    List only units with particular LOAD or SUB or ACTIVE state\n"
4686                "  -p --property=NAME  Show only properties by this name\n"
4687                "  -a --all            Show all loaded units/properties, including dead/empty\n"
4688                "                      ones. To list all units installed on the system, use\n"
4689                "                      the 'list-unit-files' command instead.\n"
4690                "     --reverse        Show reverse dependencies with 'list-dependencies'\n"
4691                "  -l --full           Don't ellipsize unit names on output\n"
4692                "     --fail           When queueing a new job, fail if conflicting jobs are\n"
4693                "                      pending\n"
4694                "     --irreversible   When queueing a new job, make sure it cannot be implicitly\n"
4695                "                      cancelled\n"
4696                "     --ignore-dependencies\n"
4697                "                      When queueing a new job, ignore all its dependencies\n"
4698                "     --show-types     When showing sockets, explicitly show their type\n"
4699                "  -i --ignore-inhibitors\n"
4700                "                      When shutting down or sleeping, ignore inhibitors\n"
4701                "     --kill-who=WHO   Who to send signal to\n"
4702                "  -s --signal=SIGNAL  Which signal to send\n"
4703                "  -H --host=[USER@]HOST\n"
4704                "                      Show information for remote host\n"
4705                "  -P --privileged     Acquire privileges before execution\n"
4706                "  -q --quiet          Suppress output\n"
4707                "     --no-block       Do not wait until operation finished\n"
4708                "     --no-wall        Don't send wall message before halt/power-off/reboot\n"
4709                "     --no-reload      When enabling/disabling unit files, don't reload daemon\n"
4710                "                      configuration\n"
4711                "     --no-legend      Do not print a legend (column headers and hints)\n"
4712                "     --no-pager       Do not pipe output into a pager\n"
4713                "     --no-ask-password\n"
4714                "                      Do not ask for system passwords\n"
4715                "     --system         Connect to system manager\n"
4716                "     --user           Connect to user service manager\n"
4717                "     --global         Enable/disable unit files globally\n"
4718                "     --runtime        Enable unit files only temporarily until next reboot\n"
4719                "  -f --force          When enabling unit files, override existing symlinks\n"
4720                "                      When shutting down, execute action immediately\n"
4721                "     --root=PATH      Enable unit files in the specified root directory\n"
4722                "  -n --lines=INTEGER  Numer of journal entries to show\n"
4723                "  -o --output=STRING  Change journal output mode (short, short-monotonic,\n"
4724                "                      verbose, export, json, json-pretty, json-sse, cat)\n\n"
4725                "Unit Commands:\n"
4726                "  list-units                      List loaded units\n"
4727                "  list-sockets                    List loaded sockets ordered by address\n"
4728                "  start [NAME...]                 Start (activate) one or more units\n"
4729                "  stop [NAME...]                  Stop (deactivate) one or more units\n"
4730                "  reload [NAME...]                Reload one or more units\n"
4731                "  restart [NAME...]               Start or restart one or more units\n"
4732                "  try-restart [NAME...]           Restart one or more units if active\n"
4733                "  reload-or-restart [NAME...]     Reload one or more units if possible,\n"
4734                "                                  otherwise start or restart\n"
4735                "  reload-or-try-restart [NAME...] Reload one or more units if possible,\n"
4736                "                                  otherwise restart if active\n"
4737                "  isolate [NAME]                  Start one unit and stop all others\n"
4738                "  kill [NAME...]                  Send signal to processes of a unit\n"
4739                "  is-active [NAME...]             Check whether units are active\n"
4740                "  is-failed [NAME...]             Check whether units are failed\n"
4741                "  status [NAME...|PID...]         Show runtime status of one or more units\n"
4742                "  show [NAME...|JOB...]           Show properties of one or more\n"
4743                "                                  units/jobs or the manager\n"
4744                "  set-property [NAME] [ASSIGNMENT...]\n"
4745                "                                  Sets one or more properties of a unit\n"
4746                "  help [NAME...|PID...]           Show manual for one or more units\n"
4747                "  reset-failed [NAME...]          Reset failed state for all, one, or more\n"
4748                "                                  units\n"
4749                "  list-dependencies [NAME]        Recursively show units which are required\n"
4750                "                                  or wanted by this unit or by which this\n"
4751                "                                  unit is required or wanted\n\n"
4752                "Unit File Commands:\n"
4753                "  list-unit-files                 List installed unit files\n"
4754                "  enable [NAME...]                Enable one or more unit files\n"
4755                "  disable [NAME...]               Disable one or more unit files\n"
4756                "  reenable [NAME...]              Reenable one or more unit files\n"
4757                "  preset [NAME...]                Enable/disable one or more unit files\n"
4758                "                                  based on preset configuration\n"
4759                "  is-enabled [NAME...]            Check whether unit files are enabled\n\n"
4760                "  mask [NAME...]                  Mask one or more units\n"
4761                "  unmask [NAME...]                Unmask one or more units\n"
4762                "  link [PATH...]                  Link one or more units files into\n"
4763                "                                  the search path\n"
4764                "  get-default                     Get the name of the default target\n"
4765                "  set-default NAME                Set the default target\n\n"
4766                "Job Commands:\n"
4767                "  list-jobs                       List jobs\n"
4768                "  cancel [JOB...]                 Cancel all, one, or more jobs\n\n"
4769                "Snapshot Commands:\n"
4770                "  snapshot [NAME]                 Create a snapshot\n"
4771                "  delete [NAME...]                Remove one or more snapshots\n\n"
4772                "Environment Commands:\n"
4773                "  show-environment                Dump environment\n"
4774                "  set-environment [NAME=VALUE...] Set one or more environment variables\n"
4775                "  unset-environment [NAME...]     Unset one or more environment variables\n\n"
4776                "Manager Lifecycle Commands:\n"
4777                "  daemon-reload                   Reload systemd manager configuration\n"
4778                "  daemon-reexec                   Reexecute systemd manager\n\n"
4779                "System Commands:\n"
4780                "  default                         Enter system default mode\n"
4781                "  rescue                          Enter system rescue mode\n"
4782                "  emergency                       Enter system emergency mode\n"
4783                "  halt                            Shut down and halt the system\n"
4784                "  poweroff                        Shut down and power-off the system\n"
4785                "  reboot                          Shut down and reboot the system\n"
4786                "  kexec                           Shut down and reboot the system with kexec\n"
4787                "  exit                            Request user instance exit\n"
4788                "  switch-root [ROOT] [INIT]       Change to a different root file system\n"
4789                "  suspend                         Suspend the system\n"
4790                "  hibernate                       Hibernate the system\n"
4791                "  hybrid-sleep                    Hibernate and suspend the system\n",
4792                program_invocation_short_name);
4793
4794         return 0;
4795 }
4796
4797 static int halt_help(void) {
4798
4799         printf("%s [OPTIONS...]\n\n"
4800                "%s the system.\n\n"
4801                "     --help      Show this help\n"
4802                "     --halt      Halt the machine\n"
4803                "  -p --poweroff  Switch off the machine\n"
4804                "     --reboot    Reboot the machine\n"
4805                "  -f --force     Force immediate halt/power-off/reboot\n"
4806                "  -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4807                "  -d --no-wtmp   Don't write wtmp record\n"
4808                "     --no-wall   Don't send wall message before halt/power-off/reboot\n",
4809                program_invocation_short_name,
4810                arg_action == ACTION_REBOOT   ? "Reboot" :
4811                arg_action == ACTION_POWEROFF ? "Power off" :
4812                                                "Halt");
4813
4814         return 0;
4815 }
4816
4817 static int shutdown_help(void) {
4818
4819         printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4820                "Shut down the system.\n\n"
4821                "     --help      Show this help\n"
4822                "  -H --halt      Halt the machine\n"
4823                "  -P --poweroff  Power-off the machine\n"
4824                "  -r --reboot    Reboot the machine\n"
4825                "  -h             Equivalent to --poweroff, overridden by --halt\n"
4826                "  -k             Don't halt/power-off/reboot, just send warnings\n"
4827                "     --no-wall   Don't send wall message before halt/power-off/reboot\n"
4828                "  -c             Cancel a pending shutdown\n",
4829                program_invocation_short_name);
4830
4831         return 0;
4832 }
4833
4834 static int telinit_help(void) {
4835
4836         printf("%s [OPTIONS...] {COMMAND}\n\n"
4837                "Send control commands to the init daemon.\n\n"
4838                "     --help      Show this help\n"
4839                "     --no-wall   Don't send wall message before halt/power-off/reboot\n\n"
4840                "Commands:\n"
4841                "  0              Power-off the machine\n"
4842                "  6              Reboot the machine\n"
4843                "  2, 3, 4, 5     Start runlevelX.target unit\n"
4844                "  1, s, S        Enter rescue mode\n"
4845                "  q, Q           Reload init daemon configuration\n"
4846                "  u, U           Reexecute init daemon\n",
4847                program_invocation_short_name);
4848
4849         return 0;
4850 }
4851
4852 static int runlevel_help(void) {
4853
4854         printf("%s [OPTIONS...]\n\n"
4855                "Prints the previous and current runlevel of the init system.\n\n"
4856                "     --help      Show this help\n",
4857                program_invocation_short_name);
4858
4859         return 0;
4860 }
4861
4862 static int help_types(void) {
4863         int i;
4864         const char *t;
4865
4866         puts("Available unit types:");
4867         for(i = 0; i < _UNIT_TYPE_MAX; i++) {
4868                 t = unit_type_to_string(i);
4869                 if (t)
4870                         puts(t);
4871         }
4872
4873         return 0;
4874 }
4875
4876 static int systemctl_parse_argv(int argc, char *argv[]) {
4877
4878         enum {
4879                 ARG_FAIL = 0x100,
4880                 ARG_REVERSE,
4881                 ARG_AFTER,
4882                 ARG_BEFORE,
4883                 ARG_SHOW_TYPES,
4884                 ARG_IRREVERSIBLE,
4885                 ARG_IGNORE_DEPENDENCIES,
4886                 ARG_VERSION,
4887                 ARG_USER,
4888                 ARG_SYSTEM,
4889                 ARG_GLOBAL,
4890                 ARG_NO_BLOCK,
4891                 ARG_NO_LEGEND,
4892                 ARG_NO_PAGER,
4893                 ARG_NO_WALL,
4894                 ARG_ROOT,
4895                 ARG_NO_RELOAD,
4896                 ARG_KILL_WHO,
4897                 ARG_NO_ASK_PASSWORD,
4898                 ARG_FAILED,
4899                 ARG_RUNTIME,
4900                 ARG_FORCE,
4901                 ARG_PLAIN,
4902                 ARG_STATE
4903         };
4904
4905         static const struct option options[] = {
4906                 { "help",                no_argument,       NULL, 'h'                     },
4907                 { "version",             no_argument,       NULL, ARG_VERSION             },
4908                 { "type",                required_argument, NULL, 't'                     },
4909                 { "property",            required_argument, NULL, 'p'                     },
4910                 { "all",                 no_argument,       NULL, 'a'                     },
4911                 { "reverse",             no_argument,       NULL, ARG_REVERSE             },
4912                 { "after",               no_argument,       NULL, ARG_AFTER               },
4913                 { "before",              no_argument,       NULL, ARG_BEFORE              },
4914                 { "show-types",          no_argument,       NULL, ARG_SHOW_TYPES          },
4915                 { "failed",              no_argument,       NULL, ARG_FAILED              }, /* compatibility only */
4916                 { "full",                no_argument,       NULL, 'l'                     },
4917                 { "fail",                no_argument,       NULL, ARG_FAIL                },
4918                 { "irreversible",        no_argument,       NULL, ARG_IRREVERSIBLE        },
4919                 { "ignore-dependencies", no_argument,       NULL, ARG_IGNORE_DEPENDENCIES },
4920                 { "ignore-inhibitors",   no_argument,       NULL, 'i'                     },
4921                 { "user",                no_argument,       NULL, ARG_USER                },
4922                 { "system",              no_argument,       NULL, ARG_SYSTEM              },
4923                 { "global",              no_argument,       NULL, ARG_GLOBAL              },
4924                 { "no-block",            no_argument,       NULL, ARG_NO_BLOCK            },
4925                 { "no-legend",           no_argument,       NULL, ARG_NO_LEGEND           },
4926                 { "no-pager",            no_argument,       NULL, ARG_NO_PAGER            },
4927                 { "no-wall",             no_argument,       NULL, ARG_NO_WALL             },
4928                 { "quiet",               no_argument,       NULL, 'q'                     },
4929                 { "root",                required_argument, NULL, ARG_ROOT                },
4930                 { "force",               no_argument,       NULL, ARG_FORCE               },
4931                 { "no-reload",           no_argument,       NULL, ARG_NO_RELOAD           },
4932                 { "kill-who",            required_argument, NULL, ARG_KILL_WHO            },
4933                 { "signal",              required_argument, NULL, 's'                     },
4934                 { "no-ask-password",     no_argument,       NULL, ARG_NO_ASK_PASSWORD     },
4935                 { "host",                required_argument, NULL, 'H'                     },
4936                 { "privileged",          no_argument,       NULL, 'P'                     },
4937                 { "runtime",             no_argument,       NULL, ARG_RUNTIME             },
4938                 { "lines",               required_argument, NULL, 'n'                     },
4939                 { "output",              required_argument, NULL, 'o'                     },
4940                 { "plain",               no_argument,       NULL, ARG_PLAIN               },
4941                 { "state",               required_argument, NULL, ARG_STATE               },
4942                 { NULL,                  0,                 NULL, 0                       }
4943         };
4944
4945         int c;
4946
4947         assert(argc >= 0);
4948         assert(argv);
4949
4950         while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:Pn:o:i", options, NULL)) >= 0) {
4951
4952                 switch (c) {
4953
4954                 case 'h':
4955                         systemctl_help();
4956                         return 0;
4957
4958                 case ARG_VERSION:
4959                         puts(PACKAGE_STRING);
4960                         puts(SYSTEMD_FEATURES);
4961                         return 0;
4962
4963                 case 't': {
4964                         char *word, *state;
4965                         size_t size;
4966
4967                         FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
4968                                 _cleanup_free_ char *type;
4969
4970                                 type = strndup(word, size);
4971                                 if (!type)
4972                                         return -ENOMEM;
4973
4974                                 if (streq(type, "help")) {
4975                                         help_types();
4976                                         return 0;
4977                                 }
4978
4979                                 if (unit_type_from_string(type) >= 0) {
4980                                         if (strv_push(&arg_types, type))
4981                                                 return log_oom();
4982                                         type = NULL;
4983                                         continue;
4984                                 }
4985
4986                                 /* It's much nicer to use --state= for
4987                                  * load states, but let's support this
4988                                  * in --types= too for compatibility
4989                                  * with old versions */
4990                                 if (unit_load_state_from_string(optarg) >= 0) {
4991                                         if (strv_push(&arg_states, type) < 0)
4992                                                 return log_oom();
4993                                         type = NULL;
4994                                         continue;
4995                                 }
4996
4997                                 log_error("Unknown unit type or load state '%s'.", type);
4998                                 log_info("Use -t help to see a list of allowed values.");
4999                                 return -EINVAL;
5000                         }
5001
5002                         break;
5003                 }
5004
5005                 case 'p': {
5006                         /* Make sure that if the empty property list
5007                            was specified, we won't show any properties. */
5008                         if (isempty(optarg) && !arg_properties) {
5009                                 arg_properties = strv_new(NULL, NULL);
5010                                 if (!arg_properties)
5011                                         return log_oom();
5012                         } else {
5013                                 char *word, *state;
5014                                 size_t size;
5015
5016                                 FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
5017                                         char *prop;
5018
5019                                         prop = strndup(word, size);
5020                                         if (!prop)
5021                                                 return log_oom();
5022
5023                                         if (strv_push(&arg_properties, prop) < 0) {
5024                                                 free(prop);
5025                                                 return log_oom();
5026                                         }
5027                                 }
5028                         }
5029
5030                         /* If the user asked for a particular
5031                          * property, show it to him, even if it is
5032                          * empty. */
5033                         arg_all = true;
5034
5035                         break;
5036                 }
5037
5038                 case 'a':
5039                         arg_all = true;
5040                         break;
5041
5042                 case ARG_REVERSE:
5043                         arg_dependency = DEPENDENCY_REVERSE;
5044                         break;
5045
5046                 case ARG_AFTER:
5047                         arg_dependency = DEPENDENCY_AFTER;
5048                         break;
5049
5050                 case ARG_BEFORE:
5051                         arg_dependency = DEPENDENCY_BEFORE;
5052                         break;
5053
5054                 case ARG_SHOW_TYPES:
5055                         arg_show_types = true;
5056                         break;
5057
5058                 case ARG_FAIL:
5059                         arg_job_mode = "fail";
5060                         break;
5061
5062                 case ARG_IRREVERSIBLE:
5063                         arg_job_mode = "replace-irreversibly";
5064                         break;
5065
5066                 case ARG_IGNORE_DEPENDENCIES:
5067                         arg_job_mode = "ignore-dependencies";
5068                         break;
5069
5070                 case ARG_USER:
5071                         arg_scope = UNIT_FILE_USER;
5072                         break;
5073
5074                 case ARG_SYSTEM:
5075                         arg_scope = UNIT_FILE_SYSTEM;
5076                         break;
5077
5078                 case ARG_GLOBAL:
5079                         arg_scope = UNIT_FILE_GLOBAL;
5080                         break;
5081
5082                 case ARG_NO_BLOCK:
5083                         arg_no_block = true;
5084                         break;
5085
5086                 case ARG_NO_LEGEND:
5087                         arg_no_legend = true;
5088                         break;
5089
5090                 case ARG_NO_PAGER:
5091                         arg_no_pager = true;
5092                         break;
5093
5094                 case ARG_NO_WALL:
5095                         arg_no_wall = true;
5096                         break;
5097
5098                 case ARG_ROOT:
5099                         arg_root = optarg;
5100                         break;
5101
5102                 case 'l':
5103                         arg_full = true;
5104                         break;
5105
5106                 case ARG_FAILED:
5107                         if (strv_extend(&arg_states, "failed") < 0)
5108                                 return log_oom();
5109
5110                         break;
5111
5112                 case 'q':
5113                         arg_quiet = true;
5114                         break;
5115
5116                 case ARG_FORCE:
5117                         arg_force ++;
5118                         break;
5119
5120                 case 'f':
5121                         arg_force ++;
5122                         break;
5123
5124                 case ARG_NO_RELOAD:
5125                         arg_no_reload = true;
5126                         break;
5127
5128                 case ARG_KILL_WHO:
5129                         arg_kill_who = optarg;
5130                         break;
5131
5132                 case 's':
5133                         if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
5134                                 log_error("Failed to parse signal string %s.", optarg);
5135                                 return -EINVAL;
5136                         }
5137                         break;
5138
5139                 case ARG_NO_ASK_PASSWORD:
5140                         arg_ask_password = false;
5141                         break;
5142
5143                 case 'P':
5144                         arg_transport = TRANSPORT_POLKIT;
5145                         break;
5146
5147                 case 'H':
5148                         arg_transport = TRANSPORT_SSH;
5149                         parse_user_at_host(optarg, &arg_user, &arg_host);
5150                         break;
5151
5152                 case ARG_RUNTIME:
5153                         arg_runtime = true;
5154                         break;
5155
5156                 case 'n':
5157                         if (safe_atou(optarg, &arg_lines) < 0) {
5158                                 log_error("Failed to parse lines '%s'", optarg);
5159                                 return -EINVAL;
5160                         }
5161                         break;
5162
5163                 case 'o':
5164                         arg_output = output_mode_from_string(optarg);
5165                         if (arg_output < 0) {
5166                                 log_error("Unknown output '%s'.", optarg);
5167                                 return -EINVAL;
5168                         }
5169                         break;
5170
5171                 case 'i':
5172                         arg_ignore_inhibitors = true;
5173                         break;
5174
5175                 case ARG_PLAIN:
5176                         arg_plain = true;
5177                         break;
5178
5179                 case ARG_STATE: {
5180                         char *word, *state;
5181                         size_t size;
5182
5183                         FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
5184                                 char *s;
5185
5186                                 s = strndup(word, size);
5187                                 if (!s)
5188                                         return log_oom();
5189
5190                                 if (strv_push(&arg_states, s) < 0) {
5191                                         free(s);
5192                                         return log_oom();
5193                                 }
5194                         }
5195                         break;
5196                 }
5197
5198                 case '?':
5199                         return -EINVAL;
5200
5201                 default:
5202                         log_error("Unknown option code '%c'.", c);
5203                         return -EINVAL;
5204                 }
5205         }
5206
5207         if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
5208                 log_error("Cannot access user instance remotely.");
5209                 return -EINVAL;
5210         }
5211
5212         return 1;
5213 }
5214
5215 static int halt_parse_argv(int argc, char *argv[]) {
5216
5217         enum {
5218                 ARG_HELP = 0x100,
5219                 ARG_HALT,
5220                 ARG_REBOOT,
5221                 ARG_NO_WALL
5222         };
5223
5224         static const struct option options[] = {
5225                 { "help",      no_argument,       NULL, ARG_HELP    },
5226                 { "halt",      no_argument,       NULL, ARG_HALT    },
5227                 { "poweroff",  no_argument,       NULL, 'p'         },
5228                 { "reboot",    no_argument,       NULL, ARG_REBOOT  },
5229                 { "force",     no_argument,       NULL, 'f'         },
5230                 { "wtmp-only", no_argument,       NULL, 'w'         },
5231                 { "no-wtmp",   no_argument,       NULL, 'd'         },
5232                 { "no-wall",   no_argument,       NULL, ARG_NO_WALL },
5233                 { NULL,        0,                 NULL, 0           }
5234         };
5235
5236         int c, runlevel;
5237
5238         assert(argc >= 0);
5239         assert(argv);
5240
5241         if (utmp_get_runlevel(&runlevel, NULL) >= 0)
5242                 if (runlevel == '0' || runlevel == '6')
5243                         arg_force = 2;
5244
5245         while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
5246                 switch (c) {
5247
5248                 case ARG_HELP:
5249                         halt_help();
5250                         return 0;
5251
5252                 case ARG_HALT:
5253                         arg_action = ACTION_HALT;
5254                         break;
5255
5256                 case 'p':
5257                         if (arg_action != ACTION_REBOOT)
5258                                 arg_action = ACTION_POWEROFF;
5259                         break;
5260
5261                 case ARG_REBOOT:
5262                         arg_action = ACTION_REBOOT;
5263                         break;
5264
5265                 case 'f':
5266                         arg_force = 2;
5267                         break;
5268
5269                 case 'w':
5270                         arg_dry = true;
5271                         break;
5272
5273                 case 'd':
5274                         arg_no_wtmp = true;
5275                         break;
5276
5277                 case ARG_NO_WALL:
5278                         arg_no_wall = true;
5279                         break;
5280
5281                 case 'i':
5282                 case 'h':
5283                 case 'n':
5284                         /* Compatibility nops */
5285                         break;
5286
5287                 case '?':
5288                         return -EINVAL;
5289
5290                 default:
5291                         log_error("Unknown option code '%c'.", c);
5292                         return -EINVAL;
5293                 }
5294         }
5295
5296         if (optind < argc) {
5297                 log_error("Too many arguments.");
5298                 return -EINVAL;
5299         }
5300
5301         return 1;
5302 }
5303
5304 static int parse_time_spec(const char *t, usec_t *_u) {
5305         assert(t);
5306         assert(_u);
5307
5308         if (streq(t, "now"))
5309                 *_u = 0;
5310         else if (!strchr(t, ':')) {
5311                 uint64_t u;
5312
5313                 if (safe_atou64(t, &u) < 0)
5314                         return -EINVAL;
5315
5316                 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
5317         } else {
5318                 char *e = NULL;
5319                 long hour, minute;
5320                 struct tm tm = {};
5321                 time_t s;
5322                 usec_t n;
5323
5324                 errno = 0;
5325                 hour = strtol(t, &e, 10);
5326                 if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
5327                         return -EINVAL;
5328
5329                 minute = strtol(e+1, &e, 10);
5330                 if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
5331                         return -EINVAL;
5332
5333                 n = now(CLOCK_REALTIME);
5334                 s = (time_t) (n / USEC_PER_SEC);
5335
5336                 assert_se(localtime_r(&s, &tm));
5337
5338                 tm.tm_hour = (int) hour;
5339                 tm.tm_min = (int) minute;
5340                 tm.tm_sec = 0;
5341
5342                 assert_se(s = mktime(&tm));
5343
5344                 *_u = (usec_t) s * USEC_PER_SEC;
5345
5346                 while (*_u <= n)
5347                         *_u += USEC_PER_DAY;
5348         }
5349
5350         return 0;
5351 }
5352
5353 static int shutdown_parse_argv(int argc, char *argv[]) {
5354
5355         enum {
5356                 ARG_HELP = 0x100,
5357                 ARG_NO_WALL
5358         };
5359
5360         static const struct option options[] = {
5361                 { "help",      no_argument,       NULL, ARG_HELP    },
5362                 { "halt",      no_argument,       NULL, 'H'         },
5363                 { "poweroff",  no_argument,       NULL, 'P'         },
5364                 { "reboot",    no_argument,       NULL, 'r'         },
5365                 { "kexec",     no_argument,       NULL, 'K'         }, /* not documented extension */
5366                 { "no-wall",   no_argument,       NULL, ARG_NO_WALL },
5367                 { NULL,        0,                 NULL, 0           }
5368         };
5369
5370         int c, r;
5371
5372         assert(argc >= 0);
5373         assert(argv);
5374
5375         while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
5376                 switch (c) {
5377
5378                 case ARG_HELP:
5379                         shutdown_help();
5380                         return 0;
5381
5382                 case 'H':
5383                         arg_action = ACTION_HALT;
5384                         break;
5385
5386                 case 'P':
5387                         arg_action = ACTION_POWEROFF;
5388                         break;
5389
5390                 case 'r':
5391                         if (kexec_loaded())
5392                                 arg_action = ACTION_KEXEC;
5393                         else
5394                                 arg_action = ACTION_REBOOT;
5395                         break;
5396
5397                 case 'K':
5398                         arg_action = ACTION_KEXEC;
5399                         break;
5400
5401                 case 'h':
5402                         if (arg_action != ACTION_HALT)
5403                                 arg_action = ACTION_POWEROFF;
5404                         break;
5405
5406                 case 'k':
5407                         arg_dry = true;
5408                         break;
5409
5410                 case ARG_NO_WALL:
5411                         arg_no_wall = true;
5412                         break;
5413
5414                 case 't':
5415                 case 'a':
5416                         /* Compatibility nops */
5417                         break;
5418
5419                 case 'c':
5420                         arg_action = ACTION_CANCEL_SHUTDOWN;
5421                         break;
5422
5423                 case '?':
5424                         return -EINVAL;
5425
5426                 default:
5427                         log_error("Unknown option code '%c'.", c);
5428                         return -EINVAL;
5429                 }
5430         }
5431
5432         if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) {
5433                 r = parse_time_spec(argv[optind], &arg_when);
5434                 if (r < 0) {
5435                         log_error("Failed to parse time specification: %s", argv[optind]);
5436                         return r;
5437                 }
5438         } else
5439                 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
5440
5441         if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN)
5442                 /* No time argument for shutdown cancel */
5443                 arg_wall = argv + optind;
5444         else if (argc > optind + 1)
5445                 /* We skip the time argument */
5446                 arg_wall = argv + optind + 1;
5447
5448         optind = argc;
5449
5450         return 1;
5451 }
5452
5453 static int telinit_parse_argv(int argc, char *argv[]) {
5454
5455         enum {
5456                 ARG_HELP = 0x100,
5457                 ARG_NO_WALL
5458         };
5459
5460         static const struct option options[] = {
5461                 { "help",      no_argument,       NULL, ARG_HELP    },
5462                 { "no-wall",   no_argument,       NULL, ARG_NO_WALL },
5463                 { NULL,        0,                 NULL, 0           }
5464         };
5465
5466         static const struct {
5467                 char from;
5468                 enum action to;
5469         } table[] = {
5470                 { '0', ACTION_POWEROFF },
5471                 { '6', ACTION_REBOOT },
5472                 { '1', ACTION_RESCUE },
5473                 { '2', ACTION_RUNLEVEL2 },
5474                 { '3', ACTION_RUNLEVEL3 },
5475                 { '4', ACTION_RUNLEVEL4 },
5476                 { '5', ACTION_RUNLEVEL5 },
5477                 { 's', ACTION_RESCUE },
5478                 { 'S', ACTION_RESCUE },
5479                 { 'q', ACTION_RELOAD },
5480                 { 'Q', ACTION_RELOAD },
5481                 { 'u', ACTION_REEXEC },
5482                 { 'U', ACTION_REEXEC }
5483         };
5484
5485         unsigned i;
5486         int c;
5487
5488         assert(argc >= 0);
5489         assert(argv);
5490
5491         while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
5492                 switch (c) {
5493
5494                 case ARG_HELP:
5495                         telinit_help();
5496                         return 0;
5497
5498                 case ARG_NO_WALL:
5499                         arg_no_wall = true;
5500                         break;
5501
5502                 case '?':
5503                         return -EINVAL;
5504
5505                 default:
5506                         log_error("Unknown option code '%c'.", c);
5507                         return -EINVAL;
5508                 }
5509         }
5510
5511         if (optind >= argc) {
5512                 telinit_help();
5513                 return -EINVAL;
5514         }
5515
5516         if (optind + 1 < argc) {
5517                 log_error("Too many arguments.");
5518                 return -EINVAL;
5519         }
5520
5521         if (strlen(argv[optind]) != 1) {
5522                 log_error("Expected single character argument.");
5523                 return -EINVAL;
5524         }
5525
5526         for (i = 0; i < ELEMENTSOF(table); i++)
5527                 if (table[i].from == argv[optind][0])
5528                         break;
5529
5530         if (i >= ELEMENTSOF(table)) {
5531                 log_error("Unknown command '%s'.", argv[optind]);
5532                 return -EINVAL;
5533         }
5534
5535         arg_action = table[i].to;
5536
5537         optind ++;
5538
5539         return 1;
5540 }
5541
5542 static int runlevel_parse_argv(int argc, char *argv[]) {
5543
5544         enum {
5545                 ARG_HELP = 0x100,
5546         };
5547
5548         static const struct option options[] = {
5549                 { "help",      no_argument,       NULL, ARG_HELP    },
5550                 { NULL,        0,                 NULL, 0           }
5551         };
5552
5553         int c;
5554
5555         assert(argc >= 0);
5556         assert(argv);
5557
5558         while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
5559                 switch (c) {
5560
5561                 case ARG_HELP:
5562                         runlevel_help();
5563                         return 0;
5564
5565                 case '?':
5566                         return -EINVAL;
5567
5568                 default:
5569                         log_error("Unknown option code '%c'.", c);
5570                         return -EINVAL;
5571                 }
5572         }
5573
5574         if (optind < argc) {
5575                 log_error("Too many arguments.");
5576                 return -EINVAL;
5577         }
5578
5579         return 1;
5580 }
5581
5582 static int parse_argv(int argc, char *argv[]) {
5583         assert(argc >= 0);
5584         assert(argv);
5585
5586         if (program_invocation_short_name) {
5587
5588                 if (strstr(program_invocation_short_name, "halt")) {
5589                         arg_action = ACTION_HALT;
5590                         return halt_parse_argv(argc, argv);
5591                 } else if (strstr(program_invocation_short_name, "poweroff")) {
5592                         arg_action = ACTION_POWEROFF;
5593                         return halt_parse_argv(argc, argv);
5594                 } else if (strstr(program_invocation_short_name, "reboot")) {
5595                         if (kexec_loaded())
5596                                 arg_action = ACTION_KEXEC;
5597                         else
5598                                 arg_action = ACTION_REBOOT;
5599                         return halt_parse_argv(argc, argv);
5600                 } else if (strstr(program_invocation_short_name, "shutdown")) {
5601                         arg_action = ACTION_POWEROFF;
5602                         return shutdown_parse_argv(argc, argv);
5603                 } else if (strstr(program_invocation_short_name, "init")) {
5604
5605                         if (sd_booted() > 0) {
5606                                 arg_action = ACTION_INVALID;
5607                                 return telinit_parse_argv(argc, argv);
5608                         } else {
5609                                 /* Hmm, so some other init system is
5610                                  * running, we need to forward this
5611                                  * request to it. For now we simply
5612                                  * guess that it is Upstart. */
5613
5614                                 execv(TELINIT, argv);
5615
5616                                 log_error("Couldn't find an alternative telinit implementation to spawn.");
5617                                 return -EIO;
5618                         }
5619
5620                 } else if (strstr(program_invocation_short_name, "runlevel")) {
5621                         arg_action = ACTION_RUNLEVEL;
5622                         return runlevel_parse_argv(argc, argv);
5623                 }
5624         }
5625
5626         arg_action = ACTION_SYSTEMCTL;
5627         return systemctl_parse_argv(argc, argv);
5628 }
5629
5630 _pure_ static int action_to_runlevel(void) {
5631
5632         static const char table[_ACTION_MAX] = {
5633                 [ACTION_HALT] =      '0',
5634                 [ACTION_POWEROFF] =  '0',
5635                 [ACTION_REBOOT] =    '6',
5636                 [ACTION_RUNLEVEL2] = '2',
5637                 [ACTION_RUNLEVEL3] = '3',
5638                 [ACTION_RUNLEVEL4] = '4',
5639                 [ACTION_RUNLEVEL5] = '5',
5640                 [ACTION_RESCUE] =    '1'
5641         };
5642
5643         assert(arg_action < _ACTION_MAX);
5644
5645         return table[arg_action];
5646 }
5647
5648 static int talk_upstart(void) {
5649         _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL;
5650         _cleanup_dbus_error_free_ DBusError error;
5651         int previous, rl, r;
5652         char
5653                 env1_buf[] = "RUNLEVEL=X",
5654                 env2_buf[] = "PREVLEVEL=X";
5655         char *env1 = env1_buf, *env2 = env2_buf;
5656         const char *emit = "runlevel";
5657         dbus_bool_t b_false = FALSE;
5658         DBusMessageIter iter, sub;
5659         DBusConnection *bus;
5660
5661         dbus_error_init(&error);
5662
5663         if (!(rl = action_to_runlevel()))
5664                 return 0;
5665
5666         if (utmp_get_runlevel(&previous, NULL) < 0)
5667                 previous = 'N';
5668
5669         if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
5670                 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
5671                         r = 0;
5672                         goto finish;
5673                 }
5674
5675                 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
5676                 r = -EIO;
5677                 goto finish;
5678         }
5679
5680         if ((r = bus_check_peercred(bus)) < 0) {
5681                 log_error("Failed to verify owner of bus.");
5682                 goto finish;
5683         }
5684
5685         if (!(m = dbus_message_new_method_call(
5686                               "com.ubuntu.Upstart",
5687                               "/com/ubuntu/Upstart",
5688                               "com.ubuntu.Upstart0_6",
5689                               "EmitEvent"))) {
5690
5691                 log_error("Could not allocate message.");
5692                 r = -ENOMEM;
5693                 goto finish;
5694         }
5695
5696         dbus_message_iter_init_append(m, &iter);
5697
5698         env1_buf[sizeof(env1_buf)-2] = rl;
5699         env2_buf[sizeof(env2_buf)-2] = previous;
5700
5701         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
5702             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
5703             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
5704             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
5705             !dbus_message_iter_close_container(&iter, &sub) ||
5706             !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
5707                 log_error("Could not append arguments to message.");
5708                 r = -ENOMEM;
5709                 goto finish;
5710         }
5711
5712         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
5713
5714                 if (bus_error_is_no_service(&error)) {
5715                         r = -EADDRNOTAVAIL;
5716                         goto finish;
5717                 }
5718
5719                 log_error("Failed to issue method call: %s", bus_error_message(&error));
5720                 r = -EIO;
5721                 goto finish;
5722         }
5723
5724         r = 1;
5725
5726 finish:
5727         if (bus) {
5728                 dbus_connection_flush(bus);
5729                 dbus_connection_close(bus);
5730                 dbus_connection_unref(bus);
5731         }
5732
5733         return r;
5734 }
5735
5736 static int talk_initctl(void) {
5737         struct init_request request = {};
5738         int r;
5739         _cleanup_close_ int fd = -1;
5740         char rl;
5741
5742         rl = action_to_runlevel();
5743         if (!rl)
5744                 return 0;
5745
5746         request.magic = INIT_MAGIC;
5747         request.sleeptime = 0;
5748         request.cmd = INIT_CMD_RUNLVL;
5749         request.runlevel = rl;
5750
5751         fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY);
5752         if (fd < 0) {
5753                 if (errno == ENOENT)
5754                         return 0;
5755
5756                 log_error("Failed to open "INIT_FIFO": %m");
5757                 return -errno;
5758         }
5759
5760         errno = 0;
5761         r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
5762         if (r) {
5763                 log_error("Failed to write to "INIT_FIFO": %m");
5764                 return errno > 0 ? -errno : -EIO;
5765         }
5766
5767         return 1;
5768 }
5769
5770 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
5771
5772         static const struct {
5773                 const char* verb;
5774                 const enum {
5775                         MORE,
5776                         LESS,
5777                         EQUAL
5778                 } argc_cmp;
5779                 const int argc;
5780                 int (* const dispatch)(DBusConnection *bus, char **args);
5781         } verbs[] = {
5782                 { "list-units",            LESS,  1, list_units        },
5783                 { "list-unit-files",       EQUAL, 1, list_unit_files   },
5784                 { "list-sockets",          LESS,  1, list_sockets      },
5785                 { "list-jobs",             EQUAL, 1, list_jobs         },
5786                 { "clear-jobs",            EQUAL, 1, daemon_reload     },
5787                 { "cancel",                MORE,  2, cancel_job        },
5788                 { "start",                 MORE,  2, start_unit        },
5789                 { "stop",                  MORE,  2, start_unit        },
5790                 { "condstop",              MORE,  2, start_unit        }, /* For compatibility with ALTLinux */
5791                 { "reload",                MORE,  2, start_unit        },
5792                 { "restart",               MORE,  2, start_unit        },
5793                 { "try-restart",           MORE,  2, start_unit        },
5794                 { "reload-or-restart",     MORE,  2, start_unit        },
5795                 { "reload-or-try-restart", MORE,  2, start_unit        },
5796                 { "force-reload",          MORE,  2, start_unit        }, /* For compatibility with SysV */
5797                 { "condreload",            MORE,  2, start_unit        }, /* For compatibility with ALTLinux */
5798                 { "condrestart",           MORE,  2, start_unit        }, /* For compatibility with RH */
5799                 { "isolate",               EQUAL, 2, start_unit        },
5800                 { "kill",                  MORE,  2, kill_unit         },
5801                 { "is-active",             MORE,  2, check_unit_active },
5802                 { "check",                 MORE,  2, check_unit_active },
5803                 { "is-failed",             MORE,  2, check_unit_failed },
5804                 { "show",                  MORE,  1, show              },
5805                 { "status",                MORE,  1, show              },
5806                 { "help",                  MORE,  2, show              },
5807                 { "snapshot",              LESS,  2, snapshot          },
5808                 { "delete",                MORE,  2, delete_snapshot   },
5809                 { "daemon-reload",         EQUAL, 1, daemon_reload     },
5810                 { "daemon-reexec",         EQUAL, 1, daemon_reload     },
5811                 { "show-environment",      EQUAL, 1, show_enviroment   },
5812                 { "set-environment",       MORE,  2, set_environment   },
5813                 { "unset-environment",     MORE,  2, set_environment   },
5814                 { "halt",                  EQUAL, 1, start_special     },
5815                 { "poweroff",              EQUAL, 1, start_special     },
5816                 { "reboot",                EQUAL, 1, start_special     },
5817                 { "kexec",                 EQUAL, 1, start_special     },
5818                 { "suspend",               EQUAL, 1, start_special     },
5819                 { "hibernate",             EQUAL, 1, start_special     },
5820                 { "hybrid-sleep",          EQUAL, 1, start_special     },
5821                 { "default",               EQUAL, 1, start_special     },
5822                 { "rescue",                EQUAL, 1, start_special     },
5823                 { "emergency",             EQUAL, 1, start_special     },
5824                 { "exit",                  EQUAL, 1, start_special     },
5825                 { "reset-failed",          MORE,  1, reset_failed      },
5826                 { "enable",                MORE,  2, enable_unit       },
5827                 { "disable",               MORE,  2, enable_unit       },
5828                 { "is-enabled",            MORE,  2, unit_is_enabled   },
5829                 { "reenable",              MORE,  2, enable_unit       },
5830                 { "preset",                MORE,  2, enable_unit       },
5831                 { "mask",                  MORE,  2, enable_unit       },
5832                 { "unmask",                MORE,  2, enable_unit       },
5833                 { "link",                  MORE,  2, enable_unit       },
5834                 { "switch-root",           MORE,  2, switch_root       },
5835                 { "list-dependencies",     LESS,  2, list_dependencies },
5836                 { "set-default",           EQUAL, 2, enable_unit       },
5837                 { "get-default",           LESS,  1, get_default       },
5838                 { "set-property",          MORE,  3, set_property      },
5839         };
5840
5841         int left;
5842         unsigned i;
5843
5844         assert(argc >= 0);
5845         assert(argv);
5846         assert(error);
5847
5848         left = argc - optind;
5849
5850         if (left <= 0)
5851                 /* Special rule: no arguments means "list-units" */
5852                 i = 0;
5853         else {
5854                 if (streq(argv[optind], "help") && !argv[optind+1]) {
5855                         log_error("This command expects one or more "
5856                                   "unit names. Did you mean --help?");
5857                         return -EINVAL;
5858                 }
5859
5860                 for (i = 0; i < ELEMENTSOF(verbs); i++)
5861                         if (streq(argv[optind], verbs[i].verb))
5862                                 break;
5863
5864                 if (i >= ELEMENTSOF(verbs)) {
5865                         log_error("Unknown operation '%s'.", argv[optind]);
5866                         return -EINVAL;
5867                 }
5868         }
5869
5870         switch (verbs[i].argc_cmp) {
5871
5872         case EQUAL:
5873                 if (left != verbs[i].argc) {
5874                         log_error("Invalid number of arguments.");
5875                         return -EINVAL;
5876                 }
5877
5878                 break;
5879
5880         case MORE:
5881                 if (left < verbs[i].argc) {
5882                         log_error("Too few arguments.");
5883                         return -EINVAL;
5884                 }
5885
5886                 break;
5887
5888         case LESS:
5889                 if (left > verbs[i].argc) {
5890                         log_error("Too many arguments.");
5891                         return -EINVAL;
5892                 }
5893
5894                 break;
5895
5896         default:
5897                 assert_not_reached("Unknown comparison operator.");
5898         }
5899
5900         /* Require a bus connection for all operations but
5901          * enable/disable */
5902         if (!streq(verbs[i].verb, "enable") &&
5903             !streq(verbs[i].verb, "disable") &&
5904             !streq(verbs[i].verb, "is-enabled") &&
5905             !streq(verbs[i].verb, "list-unit-files") &&
5906             !streq(verbs[i].verb, "reenable") &&
5907             !streq(verbs[i].verb, "preset") &&
5908             !streq(verbs[i].verb, "mask") &&
5909             !streq(verbs[i].verb, "unmask") &&
5910             !streq(verbs[i].verb, "link") &&
5911             !streq(verbs[i].verb, "set-default") &&
5912             !streq(verbs[i].verb, "get-default")) {
5913
5914                 if (running_in_chroot() > 0) {
5915                         log_info("Running in chroot, ignoring request.");
5916                         return 0;
5917                 }
5918
5919                 if (((!streq(verbs[i].verb, "reboot") &&
5920                       !streq(verbs[i].verb, "halt") &&
5921                       !streq(verbs[i].verb, "poweroff")) || arg_force <= 0) && !bus) {
5922                         log_error("Failed to get D-Bus connection: %s",
5923                                   dbus_error_is_set(error) ? error->message : "No connection to service manager.");
5924                         return -EIO;
5925                 }
5926
5927         } else {
5928
5929                 if (!bus && !avoid_bus()) {
5930                         log_error("Failed to get D-Bus connection: %s",
5931                                   dbus_error_is_set(error) ? error->message : "No connection to service manager.");
5932                         return -EIO;
5933                 }
5934         }
5935
5936         return verbs[i].dispatch(bus, argv + optind);
5937 }
5938
5939 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
5940         _cleanup_close_ int fd;
5941         struct sd_shutdown_command c = {
5942                 .usec = t,
5943                 .mode = mode,
5944                 .dry_run = dry_run,
5945                 .warn_wall = warn,
5946         };
5947         union sockaddr_union sockaddr = {
5948                 .un.sun_family = AF_UNIX,
5949                 .un.sun_path = "/run/systemd/shutdownd",
5950         };
5951         struct iovec iovec[2] = {
5952                 {.iov_base = (char*) &c,
5953                  .iov_len = offsetof(struct sd_shutdown_command, wall_message),
5954                 }
5955         };
5956         struct msghdr msghdr = {
5957                 .msg_name = &sockaddr,
5958                 .msg_namelen = offsetof(struct sockaddr_un, sun_path)
5959                                + sizeof("/run/systemd/shutdownd") - 1,
5960                 .msg_iov = iovec,
5961                 .msg_iovlen = 1,
5962         };
5963
5964         fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
5965         if (fd < 0)
5966                 return -errno;
5967
5968         if (!isempty(message)) {
5969                 iovec[1].iov_base = (char*) message;
5970                 iovec[1].iov_len = strlen(message);
5971                 msghdr.msg_iovlen++;
5972         }
5973
5974         if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0)
5975                 return -errno;
5976
5977         return 0;
5978 }
5979
5980 static int reload_with_fallback(DBusConnection *bus) {
5981
5982         if (bus) {
5983                 /* First, try systemd via D-Bus. */
5984                 if (daemon_reload(bus, NULL) >= 0)
5985                         return 0;
5986         }
5987
5988         /* Nothing else worked, so let's try signals */
5989         assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5990
5991         if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5992                 log_error("kill() failed: %m");
5993                 return -errno;
5994         }
5995
5996         return 0;
5997 }
5998
5999 static int start_with_fallback(DBusConnection *bus) {
6000
6001         if (bus) {
6002                 /* First, try systemd via D-Bus. */
6003                 if (start_unit(bus, NULL) >= 0)
6004                         goto done;
6005         }
6006
6007         /* Hmm, talking to systemd via D-Bus didn't work. Then
6008          * let's try to talk to Upstart via D-Bus. */
6009         if (talk_upstart() > 0)
6010                 goto done;
6011
6012         /* Nothing else worked, so let's try
6013          * /dev/initctl */
6014         if (talk_initctl() > 0)
6015                 goto done;
6016
6017         log_error("Failed to talk to init daemon.");
6018         return -EIO;
6019
6020 done:
6021         warn_wall(arg_action);
6022         return 0;
6023 }
6024
6025 static _noreturn_ void halt_now(enum action a) {
6026
6027        /* Make sure C-A-D is handled by the kernel from this
6028          * point on... */
6029         reboot(RB_ENABLE_CAD);
6030
6031         switch (a) {
6032
6033         case ACTION_HALT:
6034                 log_info("Halting.");
6035                 reboot(RB_HALT_SYSTEM);
6036                 break;
6037
6038         case ACTION_POWEROFF:
6039                 log_info("Powering off.");
6040                 reboot(RB_POWER_OFF);
6041                 break;
6042
6043         case ACTION_REBOOT:
6044                 log_info("Rebooting.");
6045                 reboot(RB_AUTOBOOT);
6046                 break;
6047
6048         default:
6049                 assert_not_reached("Unknown halt action.");
6050         }
6051
6052         assert_not_reached("Uh? This shouldn't happen.");
6053 }
6054
6055 static int halt_main(DBusConnection *bus) {
6056         int r;
6057
6058         r = check_inhibitors(bus, arg_action);
6059         if (r < 0)
6060                 return r;
6061
6062         if (geteuid() != 0) {
6063                 /* Try logind if we are a normal user and no special
6064                  * mode applies. Maybe PolicyKit allows us to shutdown
6065                  * the machine. */
6066
6067                 if (arg_when <= 0 &&
6068                     !arg_dry &&
6069                     arg_force <= 0 &&
6070                     (arg_action == ACTION_POWEROFF ||
6071                      arg_action == ACTION_REBOOT)) {
6072                         r = reboot_with_logind(bus, arg_action);
6073                         if (r >= 0)
6074                                 return r;
6075                 }
6076
6077                 log_error("Must be root.");
6078                 return -EPERM;
6079         }
6080
6081         if (arg_when > 0) {
6082                 _cleanup_free_ char *m;
6083
6084                 m = strv_join(arg_wall, " ");
6085                 r = send_shutdownd(arg_when,
6086                                    arg_action == ACTION_HALT     ? 'H' :
6087                                    arg_action == ACTION_POWEROFF ? 'P' :
6088                                    arg_action == ACTION_KEXEC    ? 'K' :
6089                                                                    'r',
6090                                    arg_dry,
6091                                    !arg_no_wall,
6092                                    m);
6093
6094                 if (r < 0)
6095                         log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
6096                 else {
6097                         char date[FORMAT_TIMESTAMP_MAX];
6098
6099                         log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
6100                                  format_timestamp(date, sizeof(date), arg_when));
6101                         return 0;
6102                 }
6103         }
6104
6105         if (!arg_dry && !arg_force)
6106                 return start_with_fallback(bus);
6107
6108         if (!arg_no_wtmp) {
6109                 if (sd_booted() > 0)
6110                         log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
6111                 else {
6112                         r = utmp_put_shutdown();
6113                         if (r < 0)
6114                                 log_warning("Failed to write utmp record: %s", strerror(-r));
6115                 }
6116         }
6117
6118         if (arg_dry)
6119                 return 0;
6120
6121         halt_now(arg_action);
6122         /* We should never reach this. */
6123         return -ENOSYS;
6124 }
6125
6126 static int runlevel_main(void) {
6127         int r, runlevel, previous;
6128
6129         r = utmp_get_runlevel(&runlevel, &previous);
6130         if (r < 0) {
6131                 puts("unknown");
6132                 return r;
6133         }
6134
6135         printf("%c %c\n",
6136                previous <= 0 ? 'N' : previous,
6137                runlevel <= 0 ? 'N' : runlevel);
6138
6139         return 0;
6140 }
6141
6142 int main(int argc, char*argv[]) {
6143         int r, retval = EXIT_FAILURE;
6144         DBusConnection *bus = NULL;
6145         _cleanup_dbus_error_free_ DBusError error;
6146
6147         dbus_error_init(&error);
6148
6149         setlocale(LC_ALL, "");
6150         log_parse_environment();
6151         log_open();
6152
6153         r = parse_argv(argc, argv);
6154         if (r < 0)
6155                 goto finish;
6156         else if (r == 0) {
6157                 retval = EXIT_SUCCESS;
6158                 goto finish;
6159         }
6160
6161         /* /sbin/runlevel doesn't need to communicate via D-Bus, so
6162          * let's shortcut this */
6163         if (arg_action == ACTION_RUNLEVEL) {
6164                 r = runlevel_main();
6165                 retval = r < 0 ? EXIT_FAILURE : r;
6166                 goto finish;
6167         }
6168
6169         if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
6170                 log_info("Running in chroot, ignoring request.");
6171                 retval = 0;
6172                 goto finish;
6173         }
6174
6175         if (!avoid_bus()) {
6176                 if (arg_transport == TRANSPORT_NORMAL)
6177                         bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
6178                 else if (arg_transport == TRANSPORT_POLKIT) {
6179                         bus_connect_system_polkit(&bus, &error);
6180                         private_bus = false;
6181                 } else if (arg_transport == TRANSPORT_SSH) {
6182                         bus_connect_system_ssh(arg_user, arg_host, &bus, &error);
6183                         private_bus = false;
6184                 } else
6185                         assert_not_reached("Uh, invalid transport...");
6186         }
6187
6188         switch (arg_action) {
6189
6190         case ACTION_SYSTEMCTL:
6191                 r = systemctl_main(bus, argc, argv, &error);
6192                 break;
6193
6194         case ACTION_HALT:
6195         case ACTION_POWEROFF:
6196         case ACTION_REBOOT:
6197         case ACTION_KEXEC:
6198                 r = halt_main(bus);
6199                 break;
6200
6201         case ACTION_RUNLEVEL2:
6202         case ACTION_RUNLEVEL3:
6203         case ACTION_RUNLEVEL4:
6204         case ACTION_RUNLEVEL5:
6205         case ACTION_RESCUE:
6206         case ACTION_EMERGENCY:
6207         case ACTION_DEFAULT:
6208                 r = start_with_fallback(bus);
6209                 break;
6210
6211         case ACTION_RELOAD:
6212         case ACTION_REEXEC:
6213                 r = reload_with_fallback(bus);
6214                 break;
6215
6216         case ACTION_CANCEL_SHUTDOWN: {
6217                 char *m = NULL;
6218
6219                 if (arg_wall) {
6220                         m = strv_join(arg_wall, " ");
6221                         if (!m) {
6222                                 retval = EXIT_FAILURE;
6223                                 goto finish;
6224                         }
6225                 }
6226                 r = send_shutdownd(arg_when, SD_SHUTDOWN_NONE, false, !arg_no_wall, m);
6227                 if (r < 0)
6228                         log_warning("Failed to talk to shutdownd, shutdown hasn't been cancelled: %s", strerror(-r));
6229                 free(m);
6230                 break;
6231         }
6232
6233         case ACTION_INVALID:
6234         case ACTION_RUNLEVEL:
6235         default:
6236                 assert_not_reached("Unknown action");
6237         }
6238
6239         retval = r < 0 ? EXIT_FAILURE : r;
6240
6241 finish:
6242         if (bus) {
6243                 dbus_connection_flush(bus);
6244                 dbus_connection_close(bus);
6245                 dbus_connection_unref(bus);
6246         }
6247
6248         dbus_shutdown();
6249
6250         strv_free(arg_types);
6251         strv_free(arg_states);
6252         strv_free(arg_properties);
6253
6254         pager_close();
6255         ask_password_agent_close();
6256         polkit_agent_close();
6257
6258         return retval;
6259 }