chiark / gitweb /
86b5ae0b039b4f61e1e80fa2a37430cc2915171d
[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   Copyright 2013 Marc-Antoine Perennou
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <sys/reboot.h>
24 #include <linux/reboot.h>
25 #include <sys/syscall.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <locale.h>
29 #include <stdbool.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/ioctl.h>
33 #include <termios.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <stddef.h>
39 #include <sys/prctl.h>
40 #include <fnmatch.h>
41
42 #include "sd-daemon.h"
43 #include "sd-shutdown.h"
44 #include "sd-login.h"
45 #include "sd-bus.h"
46 #include "log.h"
47 #include "util.h"
48 #include "macro.h"
49 #include "set.h"
50 #include "utmp-wtmp.h"
51 #include "special.h"
52 #include "initreq.h"
53 #include "path-util.h"
54 #include "strv.h"
55 #include "cgroup-show.h"
56 #include "cgroup-util.h"
57 #include "list.h"
58 #include "path-lookup.h"
59 #include "conf-parser.h"
60 #include "exit-status.h"
61 #include "build.h"
62 #include "unit-name.h"
63 #include "pager.h"
64 #include "spawn-ask-password-agent.h"
65 #include "spawn-polkit-agent.h"
66 #include "install.h"
67 #include "logs-show.h"
68 #include "socket-util.h"
69 #include "fileio.h"
70 #include "copy.h"
71 #include "env-util.h"
72 #include "bus-util.h"
73 #include "bus-message.h"
74 #include "bus-error.h"
75 #include "bus-common-errors.h"
76 #include "mkdir.h"
77 #include "dropin.h"
78
79 static char **arg_types = NULL;
80 static char **arg_states = NULL;
81 static char **arg_properties = NULL;
82 static bool arg_all = false;
83 static enum dependency {
84         DEPENDENCY_FORWARD,
85         DEPENDENCY_REVERSE,
86         DEPENDENCY_AFTER,
87         DEPENDENCY_BEFORE,
88         _DEPENDENCY_MAX
89 } arg_dependency = DEPENDENCY_FORWARD;
90 static const char *arg_job_mode = "replace";
91 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
92 static bool arg_no_block = false;
93 static bool arg_no_legend = false;
94 static bool arg_no_pager = false;
95 static bool arg_no_wtmp = false;
96 static bool arg_no_wall = false;
97 static bool arg_no_reload = false;
98 static bool arg_show_types = false;
99 static bool arg_ignore_inhibitors = false;
100 static bool arg_dry = false;
101 static bool arg_quiet = false;
102 static bool arg_full = false;
103 static bool arg_recursive = false;
104 static int arg_force = 0;
105 static bool arg_ask_password = true;
106 static bool arg_runtime = false;
107 static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
108 static char **arg_wall = NULL;
109 static const char *arg_kill_who = NULL;
110 static int arg_signal = SIGTERM;
111 static const char *arg_root = NULL;
112 static usec_t arg_when = 0;
113 static enum action {
114         _ACTION_INVALID,
115         ACTION_SYSTEMCTL,
116         ACTION_HALT,
117         ACTION_POWEROFF,
118         ACTION_REBOOT,
119         ACTION_KEXEC,
120         ACTION_EXIT,
121         ACTION_SUSPEND,
122         ACTION_HIBERNATE,
123         ACTION_HYBRID_SLEEP,
124         ACTION_RUNLEVEL2,
125         ACTION_RUNLEVEL3,
126         ACTION_RUNLEVEL4,
127         ACTION_RUNLEVEL5,
128         ACTION_RESCUE,
129         ACTION_EMERGENCY,
130         ACTION_DEFAULT,
131         ACTION_RELOAD,
132         ACTION_REEXEC,
133         ACTION_RUNLEVEL,
134         ACTION_CANCEL_SHUTDOWN,
135         _ACTION_MAX
136 } arg_action = ACTION_SYSTEMCTL;
137 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
138 static char *arg_host = NULL;
139 static unsigned arg_lines = 10;
140 static OutputMode arg_output = OUTPUT_SHORT;
141 static bool arg_plain = false;
142
143 static bool original_stdout_is_tty;
144
145 static int daemon_reload(sd_bus *bus, char **args);
146 static int halt_now(enum action a);
147 static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet);
148
149 static char** strv_skip_first(char **strv) {
150         if (strv_length(strv) > 0)
151                 return strv + 1;
152         return NULL;
153 }
154
155 static void pager_open_if_enabled(void) {
156
157         if (arg_no_pager)
158                 return;
159
160         pager_open(false);
161 }
162
163 static void ask_password_agent_open_if_enabled(void) {
164
165         /* Open the password agent as a child process if necessary */
166
167         if (!arg_ask_password)
168                 return;
169
170         if (arg_scope != UNIT_FILE_SYSTEM)
171                 return;
172
173         if (arg_transport != BUS_TRANSPORT_LOCAL)
174                 return;
175
176         ask_password_agent_open();
177 }
178
179 #ifdef HAVE_LOGIND
180 static void polkit_agent_open_if_enabled(void) {
181
182         /* Open the polkit agent as a child process if necessary */
183
184         if (!arg_ask_password)
185                 return;
186
187         if (arg_scope != UNIT_FILE_SYSTEM)
188                 return;
189
190         if (arg_transport != BUS_TRANSPORT_LOCAL)
191                 return;
192
193         polkit_agent_open();
194 }
195 #endif
196
197 static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) {
198         assert(error);
199
200         if (!sd_bus_error_is_set(error))
201                 return r;
202
203         if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) ||
204             sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
205             sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
206             sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
207                 return EXIT_NOPERMISSION;
208
209         if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
210                 return EXIT_NOTINSTALLED;
211
212         if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
213             sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED))
214                 return EXIT_NOTIMPLEMENTED;
215
216         if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
217                 return EXIT_NOTCONFIGURED;
218
219         if (r != 0)
220                 return r;
221
222         return EXIT_FAILURE;
223 }
224
225 static void warn_wall(enum action a) {
226         static const char *table[_ACTION_MAX] = {
227                 [ACTION_HALT]            = "The system is going down for system halt NOW!",
228                 [ACTION_REBOOT]          = "The system is going down for reboot NOW!",
229                 [ACTION_POWEROFF]        = "The system is going down for power-off NOW!",
230                 [ACTION_KEXEC]           = "The system is going down for kexec reboot NOW!",
231                 [ACTION_RESCUE]          = "The system is going down to rescue mode NOW!",
232                 [ACTION_EMERGENCY]       = "The system is going down to emergency mode NOW!",
233                 [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
234         };
235
236         if (arg_no_wall)
237                 return;
238
239         if (arg_wall) {
240                 _cleanup_free_ char *p;
241
242                 p = strv_join(arg_wall, " ");
243                 if (!p) {
244                         log_oom();
245                         return;
246                 }
247
248                 if (*p) {
249                         utmp_wall(p, NULL, NULL);
250                         return;
251                 }
252         }
253
254         if (!table[a])
255                 return;
256
257         utmp_wall(table[a], NULL, NULL);
258 }
259
260 static bool avoid_bus(void) {
261
262         if (running_in_chroot() > 0)
263                 return true;
264
265         if (sd_booted() <= 0)
266                 return true;
267
268         if (!isempty(arg_root))
269                 return true;
270
271         if (arg_scope == UNIT_FILE_GLOBAL)
272                 return true;
273
274         return false;
275 }
276
277 static int compare_unit_info(const void *a, const void *b) {
278         const UnitInfo *u = a, *v = b;
279         const char *d1, *d2;
280         int r;
281
282         /* First, order by machine */
283         if (!u->machine && v->machine)
284                 return -1;
285         if (u->machine && !v->machine)
286                 return 1;
287         if (u->machine && v->machine) {
288                 r = strcasecmp(u->machine, v->machine);
289                 if (r != 0)
290                         return r;
291         }
292
293         /* Second, order by unit type */
294         d1 = strrchr(u->id, '.');
295         d2 = strrchr(v->id, '.');
296         if (d1 && d2) {
297                 r = strcasecmp(d1, d2);
298                 if (r != 0)
299                         return r;
300         }
301
302         /* Third, order by name */
303         return strcasecmp(u->id, v->id);
304 }
305
306 static bool output_show_unit(const UnitInfo *u, char **patterns) {
307         if (!strv_isempty(patterns)) {
308                 char **pattern;
309
310                 STRV_FOREACH(pattern, patterns)
311                         if (fnmatch(*pattern, u->id, FNM_NOESCAPE) == 0)
312                                 goto next;
313                 return false;
314         }
315
316 next:
317         if (arg_types) {
318                 const char *dot;
319
320                 dot = strrchr(u->id, '.');
321                 if (!dot)
322                         return false;
323
324                 if (!strv_find(arg_types, dot+1))
325                         return false;
326         }
327
328         if (arg_all)
329                 return true;
330
331         if (u->job_id > 0)
332                 return true;
333
334         if (streq(u->active_state, "inactive") || u->following[0])
335                 return false;
336
337         return true;
338 }
339
340 static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
341         unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len;
342         const UnitInfo *u;
343         unsigned n_shown = 0;
344         int job_count = 0;
345
346         max_id_len = strlen("UNIT");
347         load_len = strlen("LOAD");
348         active_len = strlen("ACTIVE");
349         sub_len = strlen("SUB");
350         job_len = strlen("JOB");
351         desc_len = 0;
352
353         for (u = unit_infos; u < unit_infos + c; u++) {
354                 max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0));
355                 load_len = MAX(load_len, strlen(u->load_state));
356                 active_len = MAX(active_len, strlen(u->active_state));
357                 sub_len = MAX(sub_len, strlen(u->sub_state));
358
359                 if (u->job_id != 0) {
360                         job_len = MAX(job_len, strlen(u->job_type));
361                         job_count++;
362                 }
363
364                 if (!arg_no_legend &&
365                     (streq(u->active_state, "failed") ||
366                      STR_IN_SET(u->load_state, "error", "not-found", "masked")))
367                         circle_len = 2;
368         }
369
370         if (!arg_full && original_stdout_is_tty) {
371                 unsigned basic_len;
372
373                 id_len = MIN(max_id_len, 25u);
374                 basic_len = circle_len + 5 + id_len + 5 + active_len + sub_len;
375
376                 if (job_count)
377                         basic_len += job_len + 1;
378
379                 if (basic_len < (unsigned) columns()) {
380                         unsigned extra_len, incr;
381                         extra_len = columns() - basic_len;
382
383                         /* Either UNIT already got 25, or is fully satisfied.
384                          * Grant up to 25 to DESC now. */
385                         incr = MIN(extra_len, 25u);
386                         desc_len += incr;
387                         extra_len -= incr;
388
389                         /* split the remaining space between UNIT and DESC,
390                          * but do not give UNIT more than it needs. */
391                         if (extra_len > 0) {
392                                 incr = MIN(extra_len / 2, max_id_len - id_len);
393                                 id_len += incr;
394                                 desc_len += extra_len - incr;
395                         }
396                 }
397         } else
398                 id_len = max_id_len;
399
400         for (u = unit_infos; u < unit_infos + c; u++) {
401                 _cleanup_free_ char *e = NULL, *j = NULL;
402                 const char *on_loaded = "", *off_loaded = "";
403                 const char *on_active = "", *off_active = "";
404                 const char *on_circle = "", *off_circle = "";
405                 const char *id;
406                 bool circle = false;
407
408                 if (!n_shown && !arg_no_legend) {
409
410                         if (circle_len > 0)
411                                 fputs("  ", stdout);
412
413                         printf("%-*s %-*s %-*s %-*s ",
414                                id_len, "UNIT",
415                                load_len, "LOAD",
416                                active_len, "ACTIVE",
417                                sub_len, "SUB");
418
419                         if (job_count)
420                                 printf("%-*s ", job_len, "JOB");
421
422                         if (!arg_full && arg_no_pager)
423                                 printf("%.*s\n", desc_len, "DESCRIPTION");
424                         else
425                                 printf("%s\n", "DESCRIPTION");
426                 }
427
428                 n_shown++;
429
430                 if (STR_IN_SET(u->load_state, "error", "not-found", "masked")) {
431                         on_loaded = ansi_highlight_red();
432                         on_circle = ansi_highlight_yellow();
433                         off_loaded = off_circle = ansi_highlight_off();
434                         circle = true;
435                 }
436
437                 if (streq(u->active_state, "failed")) {
438                         on_circle = on_active = ansi_highlight_red();
439                         off_circle = off_active = ansi_highlight_off();
440                         circle = true;
441                 }
442
443                 if (u->machine) {
444                         j = strjoin(u->machine, ":", u->id, NULL);
445                         if (!j)
446                                 return log_oom();
447
448                         id = j;
449                 } else
450                         id = u->id;
451
452                 if (arg_full) {
453                         e = ellipsize(id, id_len, 33);
454                         if (!e)
455                                 return log_oom();
456
457                         id = e;
458                 }
459
460                 if (circle_len > 0)
461                         printf("%s%s%s ", on_circle, circle ? draw_special_char(DRAW_BLACK_CIRCLE) : " ", off_circle);
462
463                 printf("%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
464                        on_active, id_len, id, off_active,
465                        on_loaded, load_len, u->load_state, off_loaded,
466                        on_active, active_len, u->active_state,
467                        sub_len, u->sub_state, off_active,
468                        job_count ? job_len + 1 : 0, u->job_id ? u->job_type : "");
469
470                 if (desc_len > 0)
471                         printf("%.*s\n", desc_len, u->description);
472                 else
473                         printf("%s\n", u->description);
474         }
475
476         if (!arg_no_legend) {
477                 const char *on, *off;
478
479                 if (n_shown) {
480                         puts("\n"
481                              "LOAD   = Reflects whether the unit definition was properly loaded.\n"
482                              "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
483                              "SUB    = The low-level unit activation state, values depend on unit type.");
484                         puts(job_count ? "JOB    = Pending job for the unit.\n" : "");
485                         on = ansi_highlight();
486                         off = ansi_highlight_off();
487                 } else {
488                         on = ansi_highlight_red();
489                         off = ansi_highlight_off();
490                 }
491
492                 if (arg_all)
493                         printf("%s%u loaded units listed.%s\n"
494                                "To show all installed unit files use 'systemctl list-unit-files'.\n",
495                                on, n_shown, off);
496                 else
497                         printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
498                                "To show all installed unit files use 'systemctl list-unit-files'.\n",
499                                on, n_shown, off);
500         }
501
502         return 0;
503 }
504
505 static int get_unit_list(
506                 sd_bus *bus,
507                 const char *machine,
508                 char **patterns,
509                 UnitInfo **unit_infos,
510                 int c,
511                 sd_bus_message **_reply) {
512
513         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
514         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
515         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
516         size_t size = c;
517         int r;
518         UnitInfo u;
519
520         assert(bus);
521         assert(unit_infos);
522         assert(_reply);
523
524         r = sd_bus_message_new_method_call(
525                         bus,
526                         &m,
527                         "org.freedesktop.systemd1",
528                         "/org/freedesktop/systemd1",
529                         "org.freedesktop.systemd1.Manager",
530                         "ListUnitsFiltered");
531
532         if (r < 0)
533                 return bus_log_create_error(r);
534
535         r = sd_bus_message_append_strv(m, arg_states);
536         if (r < 0)
537                 return bus_log_create_error(r);
538
539         r = sd_bus_call(bus, m, 0, &error, &reply);
540         if (r < 0) {
541                 log_error("Failed to list units: %s", bus_error_message(&error, r));
542                 return r;
543         }
544
545         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
546         if (r < 0)
547                 return bus_log_parse_error(r);
548
549         while ((r = bus_parse_unit_info(reply, &u)) > 0) {
550                 u.machine = machine;
551
552                 if (!output_show_unit(&u, patterns))
553                         continue;
554
555                 if (!GREEDY_REALLOC(*unit_infos, size, c+1))
556                         return log_oom();
557
558                 (*unit_infos)[c++] = u;
559         }
560         if (r < 0)
561                 return bus_log_parse_error(r);
562
563         r = sd_bus_message_exit_container(reply);
564         if (r < 0)
565                 return bus_log_parse_error(r);
566
567         *_reply = reply;
568         reply = NULL;
569
570         return c;
571 }
572
573 static void message_set_freep(Set **set) {
574         sd_bus_message *m;
575
576         while ((m = set_steal_first(*set)))
577                 sd_bus_message_unref(m);
578
579         set_free(*set);
580 }
581
582 static int get_unit_list_recursive(
583                 sd_bus *bus,
584                 char **patterns,
585                 UnitInfo **_unit_infos,
586                 Set **_replies,
587                 char ***_machines) {
588
589         _cleanup_free_ UnitInfo *unit_infos = NULL;
590         _cleanup_(message_set_freep) Set *replies;
591         sd_bus_message *reply;
592         int c, r;
593
594         assert(bus);
595         assert(_replies);
596         assert(_unit_infos);
597         assert(_machines);
598
599         replies = set_new(NULL);
600         if (!replies)
601                 return log_oom();
602
603         c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply);
604         if (c < 0)
605                 return c;
606
607         r = set_put(replies, reply);
608         if (r < 0) {
609                 sd_bus_message_unref(reply);
610                 return r;
611         }
612
613         if (arg_recursive) {
614                 _cleanup_strv_free_ char **machines = NULL;
615                 char **i;
616
617                 r = sd_get_machine_names(&machines);
618                 if (r < 0)
619                         return r;
620
621                 STRV_FOREACH(i, machines) {
622                         _cleanup_bus_close_unref_ sd_bus *container = NULL;
623                         int k;
624
625                         r = sd_bus_open_system_machine(&container, *i);
626                         if (r < 0) {
627                                 log_error_errno(r, "Failed to connect to container %s: %m", *i);
628                                 continue;
629                         }
630
631                         k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply);
632                         if (k < 0)
633                                 return k;
634
635                         c = k;
636
637                         r = set_put(replies, reply);
638                         if (r < 0) {
639                                 sd_bus_message_unref(reply);
640                                 return r;
641                         }
642                 }
643
644                 *_machines = machines;
645                 machines = NULL;
646         } else
647                 *_machines = NULL;
648
649         *_unit_infos = unit_infos;
650         unit_infos = NULL;
651
652         *_replies = replies;
653         replies = NULL;
654
655         return c;
656 }
657
658 static int list_units(sd_bus *bus, char **args) {
659         _cleanup_free_ UnitInfo *unit_infos = NULL;
660         _cleanup_(message_set_freep) Set *replies = NULL;
661         _cleanup_strv_free_ char **machines = NULL;
662         int r;
663
664         pager_open_if_enabled();
665
666         r = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines);
667         if (r < 0)
668                 return r;
669
670         qsort_safe(unit_infos, r, sizeof(UnitInfo), compare_unit_info);
671         return output_units_list(unit_infos, r);
672 }
673
674 static int get_triggered_units(
675                 sd_bus *bus,
676                 const char* path,
677                 char*** ret) {
678
679         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
680         int r;
681
682         r = sd_bus_get_property_strv(
683                         bus,
684                         "org.freedesktop.systemd1",
685                         path,
686                         "org.freedesktop.systemd1.Unit",
687                         "Triggers",
688                         &error,
689                         ret);
690
691         if (r < 0)
692                 log_error("Failed to determine triggers: %s", bus_error_message(&error, r));
693
694         return 0;
695 }
696
697 static int get_listening(
698                 sd_bus *bus,
699                 const char* unit_path,
700                 char*** listening) {
701
702         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
703         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
704         const char *type, *path;
705         int r, n = 0;
706
707         r = sd_bus_get_property(
708                         bus,
709                         "org.freedesktop.systemd1",
710                         unit_path,
711                         "org.freedesktop.systemd1.Socket",
712                         "Listen",
713                         &error,
714                         &reply,
715                         "a(ss)");
716         if (r < 0) {
717                 log_error("Failed to get list of listening sockets: %s", bus_error_message(&error, r));
718                 return r;
719         }
720
721         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
722         if (r < 0)
723                 return bus_log_parse_error(r);
724
725         while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) {
726
727                 r = strv_extend(listening, type);
728                 if (r < 0)
729                         return log_oom();
730
731                 r = strv_extend(listening, path);
732                 if (r < 0)
733                         return log_oom();
734
735                 n++;
736         }
737         if (r < 0)
738                 return bus_log_parse_error(r);
739
740         r = sd_bus_message_exit_container(reply);
741         if (r < 0)
742                 return bus_log_parse_error(r);
743
744         return n;
745 }
746
747 struct socket_info {
748         const char *machine;
749         const char* id;
750
751         char* type;
752         char* path;
753
754         /* Note: triggered is a list here, although it almost certainly
755          * will always be one unit. Nevertheless, dbus API allows for multiple
756          * values, so let's follow that. */
757         char** triggered;
758
759         /* The strv above is shared. free is set only in the first one. */
760         bool own_triggered;
761 };
762
763 static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) {
764         int o;
765
766         assert(a);
767         assert(b);
768
769         if (!a->machine && b->machine)
770                 return -1;
771         if (a->machine && !b->machine)
772                 return 1;
773         if (a->machine && b->machine) {
774                 o = strcasecmp(a->machine, b->machine);
775                 if (o != 0)
776                         return o;
777         }
778
779         o = strcmp(a->path, b->path);
780         if (o == 0)
781                 o = strcmp(a->type, b->type);
782
783         return o;
784 }
785
786 static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
787         struct socket_info *s;
788         unsigned pathlen = strlen("LISTEN"),
789                 typelen = strlen("TYPE") * arg_show_types,
790                 socklen = strlen("UNIT"),
791                 servlen = strlen("ACTIVATES");
792         const char *on, *off;
793
794         for (s = socket_infos; s < socket_infos + cs; s++) {
795                 unsigned tmp = 0;
796                 char **a;
797
798                 socklen = MAX(socklen, strlen(s->id));
799                 if (arg_show_types)
800                         typelen = MAX(typelen, strlen(s->type));
801                 pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0));
802
803                 STRV_FOREACH(a, s->triggered)
804                         tmp += strlen(*a) + 2*(a != s->triggered);
805                 servlen = MAX(servlen, tmp);
806         }
807
808         if (cs) {
809                 if (!arg_no_legend)
810                         printf("%-*s %-*.*s%-*s %s\n",
811                                pathlen, "LISTEN",
812                                typelen + arg_show_types, typelen + arg_show_types, "TYPE ",
813                                socklen, "UNIT",
814                                "ACTIVATES");
815
816                 for (s = socket_infos; s < socket_infos + cs; s++) {
817                         _cleanup_free_ char *j = NULL;
818                         const char *path;
819                         char **a;
820
821                         if (s->machine) {
822                                 j = strjoin(s->machine, ":", s->path, NULL);
823                                 if (!j)
824                                         return log_oom();
825                                 path = j;
826                         } else
827                                 path = s->path;
828
829                         if (arg_show_types)
830                                 printf("%-*s %-*s %-*s",
831                                        pathlen, path, typelen, s->type, socklen, s->id);
832                         else
833                                 printf("%-*s %-*s",
834                                        pathlen, path, socklen, s->id);
835                         STRV_FOREACH(a, s->triggered)
836                                 printf("%s %s",
837                                        a == s->triggered ? "" : ",", *a);
838                         printf("\n");
839                 }
840
841                 on = ansi_highlight();
842                 off = ansi_highlight_off();
843                 if (!arg_no_legend)
844                         printf("\n");
845         } else {
846                 on = ansi_highlight_red();
847                 off = ansi_highlight_off();
848         }
849
850         if (!arg_no_legend) {
851                 printf("%s%u sockets listed.%s\n", on, cs, off);
852                 if (!arg_all)
853                         printf("Pass --all to see loaded but inactive sockets, too.\n");
854         }
855
856         return 0;
857 }
858
859 static int list_sockets(sd_bus *bus, char **args) {
860         _cleanup_(message_set_freep) Set *replies = NULL;
861         _cleanup_strv_free_ char **machines = NULL;
862         _cleanup_free_ UnitInfo *unit_infos = NULL;
863         _cleanup_free_ struct socket_info *socket_infos = NULL;
864         const UnitInfo *u;
865         struct socket_info *s;
866         unsigned cs = 0;
867         size_t size = 0;
868         int r = 0, n;
869
870         pager_open_if_enabled();
871
872         n = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines);
873         if (n < 0)
874                 return n;
875
876         for (u = unit_infos; u < unit_infos + n; u++) {
877                 _cleanup_strv_free_ char **listening = NULL, **triggered = NULL;
878                 int i, c;
879
880                 if (!endswith(u->id, ".socket"))
881                         continue;
882
883                 r = get_triggered_units(bus, u->unit_path, &triggered);
884                 if (r < 0)
885                         goto cleanup;
886
887                 c = get_listening(bus, u->unit_path, &listening);
888                 if (c < 0) {
889                         r = c;
890                         goto cleanup;
891                 }
892
893                 if (!GREEDY_REALLOC(socket_infos, size, cs + c)) {
894                         r = log_oom();
895                         goto cleanup;
896                 }
897
898                 for (i = 0; i < c; i++)
899                         socket_infos[cs + i] = (struct socket_info) {
900                                 .machine = u->machine,
901                                 .id = u->id,
902                                 .type = listening[i*2],
903                                 .path = listening[i*2 + 1],
904                                 .triggered = triggered,
905                                 .own_triggered = i==0,
906                         };
907
908                 /* from this point on we will cleanup those socket_infos */
909                 cs += c;
910                 free(listening);
911                 listening = triggered = NULL; /* avoid cleanup */
912         }
913
914         qsort_safe(socket_infos, cs, sizeof(struct socket_info),
915                    (__compar_fn_t) socket_info_compare);
916
917         output_sockets_list(socket_infos, cs);
918
919  cleanup:
920         assert(cs == 0 || socket_infos);
921         for (s = socket_infos; s < socket_infos + cs; s++) {
922                 free(s->type);
923                 free(s->path);
924                 if (s->own_triggered)
925                         strv_free(s->triggered);
926         }
927
928         return r;
929 }
930
931 static int get_next_elapse(
932                 sd_bus *bus,
933                 const char *path,
934                 dual_timestamp *next) {
935
936         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
937         dual_timestamp t;
938         int r;
939
940         assert(bus);
941         assert(path);
942         assert(next);
943
944         r = sd_bus_get_property_trivial(
945                         bus,
946                         "org.freedesktop.systemd1",
947                         path,
948                         "org.freedesktop.systemd1.Timer",
949                         "NextElapseUSecMonotonic",
950                         &error,
951                         't',
952                         &t.monotonic);
953         if (r < 0) {
954                 log_error("Failed to get next elapsation time: %s", bus_error_message(&error, r));
955                 return r;
956         }
957
958         r = sd_bus_get_property_trivial(
959                         bus,
960                         "org.freedesktop.systemd1",
961                         path,
962                         "org.freedesktop.systemd1.Timer",
963                         "NextElapseUSecRealtime",
964                         &error,
965                         't',
966                         &t.realtime);
967         if (r < 0) {
968                 log_error("Failed to get next elapsation time: %s", bus_error_message(&error, r));
969                 return r;
970         }
971
972         *next = t;
973         return 0;
974 }
975
976 static int get_last_trigger(
977                 sd_bus *bus,
978                 const char *path,
979                 usec_t *last) {
980
981         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
982         int r;
983
984         assert(bus);
985         assert(path);
986         assert(last);
987
988         r = sd_bus_get_property_trivial(
989                         bus,
990                         "org.freedesktop.systemd1",
991                         path,
992                         "org.freedesktop.systemd1.Timer",
993                         "LastTriggerUSec",
994                         &error,
995                         't',
996                         last);
997         if (r < 0) {
998                 log_error("Failed to get last trigger time: %s", bus_error_message(&error, r));
999                 return r;
1000         }
1001
1002         return 0;
1003 }
1004
1005 struct timer_info {
1006         const char* machine;
1007         const char* id;
1008         usec_t next_elapse;
1009         usec_t last_trigger;
1010         char** triggered;
1011 };
1012
1013 static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) {
1014         int o;
1015
1016         assert(a);
1017         assert(b);
1018
1019         if (!a->machine && b->machine)
1020                 return -1;
1021         if (a->machine && !b->machine)
1022                 return 1;
1023         if (a->machine && b->machine) {
1024                 o = strcasecmp(a->machine, b->machine);
1025                 if (o != 0)
1026                         return o;
1027         }
1028
1029         if (a->next_elapse < b->next_elapse)
1030                 return -1;
1031         if (a->next_elapse > b->next_elapse)
1032                 return 1;
1033
1034         return strcmp(a->id, b->id);
1035 }
1036
1037 static int output_timers_list(struct timer_info *timer_infos, unsigned n) {
1038         struct timer_info *t;
1039         unsigned
1040                 nextlen = strlen("NEXT"),
1041                 leftlen = strlen("LEFT"),
1042                 lastlen = strlen("LAST"),
1043                 passedlen = strlen("PASSED"),
1044                 unitlen = strlen("UNIT"),
1045                 activatelen = strlen("ACTIVATES");
1046
1047         const char *on, *off;
1048
1049         assert(timer_infos || n == 0);
1050
1051         for (t = timer_infos; t < timer_infos + n; t++) {
1052                 unsigned ul = 0;
1053                 char **a;
1054
1055                 if (t->next_elapse > 0) {
1056                         char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = "";
1057
1058                         format_timestamp(tstamp, sizeof(tstamp), t->next_elapse);
1059                         nextlen = MAX(nextlen, strlen(tstamp) + 1);
1060
1061                         format_timestamp_relative(trel, sizeof(trel), t->next_elapse);
1062                         leftlen = MAX(leftlen, strlen(trel));
1063                 }
1064
1065                 if (t->last_trigger > 0) {
1066                         char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = "";
1067
1068                         format_timestamp(tstamp, sizeof(tstamp), t->last_trigger);
1069                         lastlen = MAX(lastlen, strlen(tstamp) + 1);
1070
1071                         format_timestamp_relative(trel, sizeof(trel), t->last_trigger);
1072                         passedlen = MAX(passedlen, strlen(trel));
1073                 }
1074
1075                 unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0));
1076
1077                 STRV_FOREACH(a, t->triggered)
1078                         ul += strlen(*a) + 2*(a != t->triggered);
1079
1080                 activatelen = MAX(activatelen, ul);
1081         }
1082
1083         if (n > 0) {
1084                 if (!arg_no_legend)
1085                         printf("%-*s %-*s %-*s %-*s %-*s %s\n",
1086                                nextlen,   "NEXT",
1087                                leftlen,   "LEFT",
1088                                lastlen,   "LAST",
1089                                passedlen, "PASSED",
1090                                unitlen,   "UNIT",
1091                                           "ACTIVATES");
1092
1093                 for (t = timer_infos; t < timer_infos + n; t++) {
1094                         _cleanup_free_ char *j = NULL;
1095                         const char *unit;
1096                         char tstamp1[FORMAT_TIMESTAMP_MAX] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a";
1097                         char tstamp2[FORMAT_TIMESTAMP_MAX] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a";
1098                         char **a;
1099
1100                         format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse);
1101                         format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse);
1102
1103                         format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger);
1104                         format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger);
1105
1106                         if (t->machine) {
1107                                 j = strjoin(t->machine, ":", t->id, NULL);
1108                                 if (!j)
1109                                         return log_oom();
1110                                 unit = j;
1111                         } else
1112                                 unit = t->id;
1113
1114                         printf("%-*s %-*s %-*s %-*s %-*s",
1115                                nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit);
1116
1117                         STRV_FOREACH(a, t->triggered)
1118                                 printf("%s %s",
1119                                        a == t->triggered ? "" : ",", *a);
1120                         printf("\n");
1121                 }
1122
1123                 on = ansi_highlight();
1124                 off = ansi_highlight_off();
1125                 if (!arg_no_legend)
1126                         printf("\n");
1127         } else {
1128                 on = ansi_highlight_red();
1129                 off = ansi_highlight_off();
1130         }
1131
1132         if (!arg_no_legend) {
1133                 printf("%s%u timers listed.%s\n", on, n, off);
1134                 if (!arg_all)
1135                         printf("Pass --all to see loaded but inactive timers, too.\n");
1136         }
1137
1138         return 0;
1139 }
1140
1141 static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) {
1142         usec_t next_elapse;
1143
1144         assert(nw);
1145         assert(next);
1146
1147         if (next->monotonic != USEC_INFINITY && next->monotonic > 0) {
1148                 usec_t converted;
1149
1150                 if (next->monotonic > nw->monotonic)
1151                         converted = nw->realtime + (next->monotonic - nw->monotonic);
1152                 else
1153                         converted = nw->realtime - (nw->monotonic - next->monotonic);
1154
1155                 if (next->realtime != USEC_INFINITY && next->realtime > 0)
1156                         next_elapse = MIN(converted, next->realtime);
1157                 else
1158                         next_elapse = converted;
1159
1160         } else
1161                 next_elapse = next->realtime;
1162
1163         return next_elapse;
1164 }
1165
1166 static int list_timers(sd_bus *bus, char **args) {
1167         _cleanup_(message_set_freep) Set *replies = NULL;
1168         _cleanup_strv_free_ char **machines = NULL;
1169         _cleanup_free_ struct timer_info *timer_infos = NULL;
1170         _cleanup_free_ UnitInfo *unit_infos = NULL;
1171         struct timer_info *t;
1172         const UnitInfo *u;
1173         size_t size = 0;
1174         int n, c = 0;
1175         dual_timestamp nw;
1176         int r = 0;
1177
1178         pager_open_if_enabled();
1179
1180         n = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines);
1181         if (n < 0)
1182                 return n;
1183
1184         dual_timestamp_get(&nw);
1185
1186         for (u = unit_infos; u < unit_infos + n; u++) {
1187                 _cleanup_strv_free_ char **triggered = NULL;
1188                 dual_timestamp next = {};
1189                 usec_t m, last = 0;
1190
1191                 if (!endswith(u->id, ".timer"))
1192                         continue;
1193
1194                 r = get_triggered_units(bus, u->unit_path, &triggered);
1195                 if (r < 0)
1196                         goto cleanup;
1197
1198                 r = get_next_elapse(bus, u->unit_path, &next);
1199                 if (r < 0)
1200                         goto cleanup;
1201
1202                 get_last_trigger(bus, u->unit_path, &last);
1203
1204                 if (!GREEDY_REALLOC(timer_infos, size, c+1)) {
1205                         r = log_oom();
1206                         goto cleanup;
1207                 }
1208
1209                 m = calc_next_elapse(&nw, &next);
1210
1211                 timer_infos[c++] = (struct timer_info) {
1212                         .machine = u->machine,
1213                         .id = u->id,
1214                         .next_elapse = m,
1215                         .last_trigger = last,
1216                         .triggered = triggered,
1217                 };
1218
1219                 triggered = NULL; /* avoid cleanup */
1220         }
1221
1222         qsort_safe(timer_infos, c, sizeof(struct timer_info),
1223                    (__compar_fn_t) timer_info_compare);
1224
1225         output_timers_list(timer_infos, c);
1226
1227  cleanup:
1228         for (t = timer_infos; t < timer_infos + c; t++)
1229                 strv_free(t->triggered);
1230
1231         return r;
1232 }
1233
1234 static int compare_unit_file_list(const void *a, const void *b) {
1235         const char *d1, *d2;
1236         const UnitFileList *u = a, *v = b;
1237
1238         d1 = strrchr(u->path, '.');
1239         d2 = strrchr(v->path, '.');
1240
1241         if (d1 && d2) {
1242                 int r;
1243
1244                 r = strcasecmp(d1, d2);
1245                 if (r != 0)
1246                         return r;
1247         }
1248
1249         return strcasecmp(basename(u->path), basename(v->path));
1250 }
1251
1252 static bool output_show_unit_file(const UnitFileList *u, char **patterns) {
1253         if (!strv_isempty(patterns)) {
1254                 char **pattern;
1255
1256                 STRV_FOREACH(pattern, patterns)
1257                         if (fnmatch(*pattern, basename(u->path), FNM_NOESCAPE) == 0)
1258                                 goto next;
1259                 return false;
1260         }
1261
1262 next:
1263         if (!strv_isempty(arg_types)) {
1264                 const char *dot;
1265
1266                 dot = strrchr(u->path, '.');
1267                 if (!dot)
1268                         return false;
1269
1270                 if (!strv_find(arg_types, dot+1))
1271                         return false;
1272         }
1273
1274         if (!strv_isempty(arg_states)) {
1275                 if (!strv_find(arg_states, unit_file_state_to_string(u->state)))
1276                         return false;
1277         }
1278
1279         return true;
1280 }
1281
1282 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
1283         unsigned max_id_len, id_cols, state_cols;
1284         const UnitFileList *u;
1285
1286         max_id_len = strlen("UNIT FILE");
1287         state_cols = strlen("STATE");
1288
1289         for (u = units; u < units + c; u++) {
1290                 max_id_len = MAX(max_id_len, strlen(basename(u->path)));
1291                 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
1292         }
1293
1294         if (!arg_full) {
1295                 unsigned basic_cols;
1296
1297                 id_cols = MIN(max_id_len, 25u);
1298                 basic_cols = 1 + id_cols + state_cols;
1299                 if (basic_cols < (unsigned) columns())
1300                         id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
1301         } else
1302                 id_cols = max_id_len;
1303
1304         if (!arg_no_legend)
1305                 printf("%-*s %-*s\n",
1306                        id_cols, "UNIT FILE",
1307                        state_cols, "STATE");
1308
1309         for (u = units; u < units + c; u++) {
1310                 _cleanup_free_ char *e = NULL;
1311                 const char *on, *off;
1312                 const char *id;
1313
1314                 if (u->state == UNIT_FILE_MASKED ||
1315                     u->state == UNIT_FILE_MASKED_RUNTIME ||
1316                     u->state == UNIT_FILE_DISABLED ||
1317                     u->state == UNIT_FILE_INVALID) {
1318                         on  = ansi_highlight_red();
1319                         off = ansi_highlight_off();
1320                 } else if (u->state == UNIT_FILE_ENABLED) {
1321                         on  = ansi_highlight_green();
1322                         off = ansi_highlight_off();
1323                 } else
1324                         on = off = "";
1325
1326                 id = basename(u->path);
1327
1328                 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
1329
1330                 printf("%-*s %s%-*s%s\n",
1331                        id_cols, e ? e : id,
1332                        on, state_cols, unit_file_state_to_string(u->state), off);
1333         }
1334
1335         if (!arg_no_legend)
1336                 printf("\n%u unit files listed.\n", c);
1337 }
1338
1339 static int list_unit_files(sd_bus *bus, char **args) {
1340         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1341         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1342         _cleanup_free_ UnitFileList *units = NULL;
1343         UnitFileList *unit;
1344         size_t size = 0;
1345         unsigned c = 0;
1346         const char *state;
1347         char *path;
1348         int r;
1349
1350         pager_open_if_enabled();
1351
1352         if (avoid_bus()) {
1353                 Hashmap *h;
1354                 UnitFileList *u;
1355                 Iterator i;
1356                 unsigned n_units;
1357
1358                 h = hashmap_new(&string_hash_ops);
1359                 if (!h)
1360                         return log_oom();
1361
1362                 r = unit_file_get_list(arg_scope, arg_root, h);
1363                 if (r < 0) {
1364                         unit_file_list_free(h);
1365                         log_error_errno(r, "Failed to get unit file list: %m");
1366                         return r;
1367                 }
1368
1369                 n_units = hashmap_size(h);
1370
1371                 units = new(UnitFileList, n_units);
1372                 if (!units && n_units > 0) {
1373                         unit_file_list_free(h);
1374                         return log_oom();
1375                 }
1376
1377                 HASHMAP_FOREACH(u, h, i) {
1378                         if (!output_show_unit_file(u, strv_skip_first(args)))
1379                                 continue;
1380
1381                         units[c++] = *u;
1382                         free(u);
1383                 }
1384
1385                 assert(c <= n_units);
1386                 hashmap_free(h);
1387         } else {
1388                 r = sd_bus_call_method(
1389                                 bus,
1390                                 "org.freedesktop.systemd1",
1391                                 "/org/freedesktop/systemd1",
1392                                 "org.freedesktop.systemd1.Manager",
1393                                 "ListUnitFiles",
1394                                 &error,
1395                                 &reply,
1396                                 NULL);
1397                 if (r < 0) {
1398                         log_error("Failed to list unit files: %s", bus_error_message(&error, r));
1399                         return r;
1400                 }
1401
1402                 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
1403                 if (r < 0)
1404                         return bus_log_parse_error(r);
1405
1406                 while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) {
1407
1408                         if (!GREEDY_REALLOC(units, size, c + 1))
1409                                 return log_oom();
1410
1411                         units[c] = (struct UnitFileList) {
1412                                 path,
1413                                 unit_file_state_from_string(state)
1414                         };
1415
1416                         if (output_show_unit_file(&units[c], strv_skip_first(args)))
1417                                 c ++;
1418
1419                 }
1420                 if (r < 0)
1421                         return bus_log_parse_error(r);
1422
1423                 r = sd_bus_message_exit_container(reply);
1424                 if (r < 0)
1425                         return bus_log_parse_error(r);
1426         }
1427
1428         qsort_safe(units, c, sizeof(UnitFileList), compare_unit_file_list);
1429         output_unit_file_list(units, c);
1430
1431         if (avoid_bus()) {
1432                 for (unit = units; unit < units + c; unit++)
1433                         free(unit->path);
1434         }
1435
1436         return 0;
1437 }
1438
1439 static int list_dependencies_print(const char *name, int level, unsigned int branches, bool last) {
1440         _cleanup_free_ char *n = NULL;
1441         size_t max_len = MAX(columns(),20u);
1442         size_t len = 0;
1443         int i;
1444
1445         if (!arg_plain) {
1446
1447                 for (i = level - 1; i >= 0; i--) {
1448                         len += 2;
1449                         if (len > max_len - 3 && !arg_full) {
1450                                 printf("%s...\n",max_len % 2 ? "" : " ");
1451                                 return 0;
1452                         }
1453                         printf("%s", draw_special_char(branches & (1 << i) ? DRAW_TREE_VERTICAL : DRAW_TREE_SPACE));
1454                 }
1455                 len += 2;
1456
1457                 if (len > max_len - 3 && !arg_full) {
1458                         printf("%s...\n",max_len % 2 ? "" : " ");
1459                         return 0;
1460                 }
1461
1462                 printf("%s", draw_special_char(last ? DRAW_TREE_RIGHT : DRAW_TREE_BRANCH));
1463         }
1464
1465         if (arg_full){
1466                 printf("%s\n", name);
1467                 return 0;
1468         }
1469
1470         n = ellipsize(name, max_len-len, 100);
1471         if (!n)
1472                 return log_oom();
1473
1474         printf("%s\n", n);
1475         return 0;
1476 }
1477
1478 static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) {
1479
1480         static const char *dependencies[_DEPENDENCY_MAX] = {
1481                 [DEPENDENCY_FORWARD] = "Requires\0"
1482                                        "RequiresOverridable\0"
1483                                        "Requisite\0"
1484                                        "RequisiteOverridable\0"
1485                                        "Wants\0"
1486                                        "BindsTo\0",
1487                 [DEPENDENCY_REVERSE] = "RequiredBy\0"
1488                                        "RequiredByOverridable\0"
1489                                        "WantedBy\0"
1490                                        "PartOf\0"
1491                                        "BoundBy\0",
1492                 [DEPENDENCY_AFTER]   = "After\0",
1493                 [DEPENDENCY_BEFORE]  = "Before\0",
1494         };
1495
1496         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1497         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1498         _cleanup_strv_free_ char **ret = NULL;
1499         _cleanup_free_ char *path = NULL;
1500         int r;
1501
1502         assert(bus);
1503         assert(name);
1504         assert(deps);
1505         assert_cc(ELEMENTSOF(dependencies) == _DEPENDENCY_MAX);
1506
1507         path = unit_dbus_path_from_name(name);
1508         if (!path)
1509                 return log_oom();
1510
1511         r = sd_bus_call_method(
1512                         bus,
1513                         "org.freedesktop.systemd1",
1514                         path,
1515                         "org.freedesktop.DBus.Properties",
1516                         "GetAll",
1517                         &error,
1518                         &reply,
1519                         "s", "org.freedesktop.systemd1.Unit");
1520         if (r < 0) {
1521                 log_error("Failed to get properties of %s: %s", name, bus_error_message(&error, r));
1522                 return r;
1523         }
1524
1525         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
1526         if (r < 0)
1527                 return bus_log_parse_error(r);
1528
1529         while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1530                 const char *prop;
1531
1532                 r = sd_bus_message_read(reply, "s", &prop);
1533                 if (r < 0)
1534                         return bus_log_parse_error(r);
1535
1536                 if (!nulstr_contains(dependencies[arg_dependency], prop)) {
1537                         r = sd_bus_message_skip(reply, "v");
1538                         if (r < 0)
1539                                 return bus_log_parse_error(r);
1540                 } else {
1541
1542                         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, "as");
1543                         if (r < 0)
1544                                 return bus_log_parse_error(r);
1545
1546                         r = bus_message_read_strv_extend(reply, &ret);
1547                         if (r < 0)
1548                                 return bus_log_parse_error(r);
1549
1550                         r = sd_bus_message_exit_container(reply);
1551                         if (r < 0)
1552                                 return bus_log_parse_error(r);
1553                 }
1554
1555                 r = sd_bus_message_exit_container(reply);
1556                 if (r < 0)
1557                         return bus_log_parse_error(r);
1558
1559         }
1560         if (r < 0)
1561                 return bus_log_parse_error(r);
1562
1563         r = sd_bus_message_exit_container(reply);
1564         if (r < 0)
1565                 return bus_log_parse_error(r);
1566
1567         *deps = ret;
1568         ret = NULL;
1569
1570         return 0;
1571 }
1572
1573 static int list_dependencies_compare(const void *_a, const void *_b) {
1574         const char **a = (const char**) _a, **b = (const char**) _b;
1575
1576         if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET)
1577                 return 1;
1578         if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET)
1579                 return -1;
1580
1581         return strcasecmp(*a, *b);
1582 }
1583
1584 static int list_dependencies_one(
1585                 sd_bus *bus,
1586                 const char *name,
1587                 int level,
1588                 char ***units,
1589                 unsigned int branches) {
1590
1591         _cleanup_strv_free_ char **deps = NULL;
1592         char **c;
1593         int r = 0;
1594
1595         assert(bus);
1596         assert(name);
1597         assert(units);
1598
1599         r = strv_extend(units, name);
1600         if (r < 0)
1601                 return log_oom();
1602
1603         r = list_dependencies_get_dependencies(bus, name, &deps);
1604         if (r < 0)
1605                 return r;
1606
1607         qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
1608
1609         STRV_FOREACH(c, deps) {
1610                 int state;
1611
1612                 if (strv_contains(*units, *c)) {
1613                         if (!arg_plain) {
1614                                 r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1);
1615                                 if (r < 0)
1616                                         return r;
1617                         }
1618                         continue;
1619                 }
1620
1621                 state = check_one_unit(bus, *c, "activating\0active\0reloading\0", true);
1622                 if (state > 0)
1623                         printf("%s%s%s ", ansi_highlight_green(), draw_special_char(DRAW_BLACK_CIRCLE), ansi_highlight_off());
1624                 else
1625                         printf("%s%s%s ", ansi_highlight_red(), draw_special_char(DRAW_BLACK_CIRCLE), ansi_highlight_off());
1626
1627                 r = list_dependencies_print(*c, level, branches, c[1] == NULL);
1628                 if (r < 0)
1629                         return r;
1630
1631                 if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) {
1632                        r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1));
1633                        if (r < 0)
1634                                return r;
1635                 }
1636         }
1637
1638         if (!arg_plain)
1639                 strv_remove(*units, name);
1640
1641         return 0;
1642 }
1643
1644 static int list_dependencies(sd_bus *bus, char **args) {
1645         _cleanup_strv_free_ char **units = NULL;
1646         _cleanup_free_ char *unit = NULL;
1647         const char *u;
1648
1649         assert(bus);
1650
1651         if (args[1]) {
1652                 unit = unit_name_mangle(args[1], MANGLE_NOGLOB);
1653                 if (!unit)
1654                         return log_oom();
1655                 u = unit;
1656         } else
1657                 u = SPECIAL_DEFAULT_TARGET;
1658
1659         pager_open_if_enabled();
1660
1661         puts(u);
1662
1663         return list_dependencies_one(bus, u, 0, &units, 0);
1664 }
1665
1666 struct machine_info {
1667         bool is_host;
1668         char *name;
1669         char *state;
1670         char *control_group;
1671         uint32_t n_failed_units;
1672         uint32_t n_jobs;
1673         usec_t timestamp;
1674 };
1675
1676 static const struct bus_properties_map machine_info_property_map[] = {
1677         { "SystemState",        "s", NULL, offsetof(struct machine_info, state)          },
1678         { "NJobs",              "u", NULL, offsetof(struct machine_info, n_jobs)         },
1679         { "NFailedUnits",       "u", NULL, offsetof(struct machine_info, n_failed_units) },
1680         { "ControlGroup",       "s", NULL, offsetof(struct machine_info, control_group)  },
1681         { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp)      },
1682         {}
1683 };
1684
1685 static void free_machines_list(struct machine_info *machine_infos, int n) {
1686         int i;
1687
1688         if (!machine_infos)
1689                 return;
1690
1691         for (i = 0; i < n; i++) {
1692                 free(machine_infos[i].name);
1693                 free(machine_infos[i].state);
1694                 free(machine_infos[i].control_group);
1695         }
1696
1697         free(machine_infos);
1698 }
1699
1700 static int compare_machine_info(const void *a, const void *b) {
1701         const struct machine_info *u = a, *v = b;
1702
1703         if (u->is_host != v->is_host)
1704                 return u->is_host > v->is_host ? -1 : 1;
1705
1706         return strcasecmp(u->name, v->name);
1707 }
1708
1709 static int get_machine_properties(sd_bus *bus, struct machine_info *mi) {
1710         _cleanup_bus_close_unref_ sd_bus *container = NULL;
1711         int r;
1712
1713         assert(mi);
1714
1715         if (!bus) {
1716                 r = sd_bus_open_system_machine(&container, mi->name);
1717                 if (r < 0)
1718                         return r;
1719
1720                 bus = container;
1721         }
1722
1723         r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, mi);
1724         if (r < 0)
1725                 return r;
1726
1727         return 0;
1728 }
1729
1730 static bool output_show_machine(const char *name, char **patterns) {
1731         char **i;
1732
1733         assert(name);
1734
1735         if (strv_isempty(patterns))
1736                 return true;
1737
1738         STRV_FOREACH(i, patterns)
1739                 if (fnmatch(*i, name, FNM_NOESCAPE) == 0)
1740                         return true;
1741
1742         return false;
1743 }
1744
1745 static int get_machine_list(
1746                 sd_bus *bus,
1747                 struct machine_info **_machine_infos,
1748                 char **patterns) {
1749
1750         struct machine_info *machine_infos = NULL;
1751         _cleanup_strv_free_ char **m = NULL;
1752         _cleanup_free_ char *hn = NULL;
1753         size_t sz = 0;
1754         char **i;
1755         int c = 0;
1756
1757         hn = gethostname_malloc();
1758         if (!hn)
1759                 return log_oom();
1760
1761         if (output_show_machine(hn, patterns)) {
1762                 if (!GREEDY_REALLOC0(machine_infos, sz, c+1))
1763                         return log_oom();
1764
1765                 machine_infos[c].is_host = true;
1766                 machine_infos[c].name = hn;
1767                 hn = NULL;
1768
1769                 get_machine_properties(bus, &machine_infos[c]);
1770                 c++;
1771         }
1772
1773         sd_get_machine_names(&m);
1774         STRV_FOREACH(i, m) {
1775                 _cleanup_free_ char *class = NULL;
1776
1777                 if (!output_show_machine(*i, patterns))
1778                         continue;
1779
1780                 sd_machine_get_class(*i, &class);
1781                 if (!streq_ptr(class, "container"))
1782                         continue;
1783
1784                 if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) {
1785                         free_machines_list(machine_infos, c);
1786                         return log_oom();
1787                 }
1788
1789                 machine_infos[c].is_host = false;
1790                 machine_infos[c].name = strdup(*i);
1791                 if (!machine_infos[c].name) {
1792                         free_machines_list(machine_infos, c);
1793                         return log_oom();
1794                 }
1795
1796                 get_machine_properties(NULL, &machine_infos[c]);
1797                 c++;
1798         }
1799
1800         *_machine_infos = machine_infos;
1801         return c;
1802 }
1803
1804 static void output_machines_list(struct machine_info *machine_infos, unsigned n) {
1805         struct machine_info *m;
1806         unsigned
1807                 circle_len = 0,
1808                 namelen = sizeof("NAME") - 1,
1809                 statelen = sizeof("STATE") - 1,
1810                 failedlen = sizeof("FAILED") - 1,
1811                 jobslen = sizeof("JOBS") - 1;
1812
1813         assert(machine_infos || n == 0);
1814
1815         for (m = machine_infos; m < machine_infos + n; m++) {
1816                 namelen = MAX(namelen, strlen(m->name) + (m->is_host ? sizeof(" (host)") - 1 : 0));
1817                 statelen = MAX(statelen, m->state ? strlen(m->state) : 0);
1818                 failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units));
1819                 jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs));
1820
1821                 if (!arg_no_legend && !streq_ptr(m->state, "running"))
1822                         circle_len = 2;
1823         }
1824
1825         if (!arg_no_legend) {
1826                 if (circle_len > 0)
1827                         fputs("  ", stdout);
1828
1829                 printf("%-*s %-*s %-*s %-*s\n",
1830                          namelen, "NAME",
1831                         statelen, "STATE",
1832                        failedlen, "FAILED",
1833                          jobslen, "JOBS");
1834         }
1835
1836         for (m = machine_infos; m < machine_infos + n; m++) {
1837                 const char *on_state = "", *off_state = "";
1838                 const char *on_failed = "", *off_failed = "";
1839                 bool circle = false;
1840
1841                 if (streq_ptr(m->state, "degraded")) {
1842                         on_state = ansi_highlight_red();
1843                         off_state = ansi_highlight_off();
1844                         circle = true;
1845                 } else if (!streq_ptr(m->state, "running")) {
1846                         on_state = ansi_highlight_yellow();
1847                         off_state = ansi_highlight_off();
1848                         circle = true;
1849                 }
1850
1851                 if (m->n_failed_units > 0) {
1852                         on_failed = ansi_highlight_red();
1853                         off_failed = ansi_highlight_off();
1854                 } else
1855                         on_failed = off_failed = "";
1856
1857                 if (circle_len > 0)
1858                         printf("%s%s%s ", on_state, circle ? draw_special_char(DRAW_BLACK_CIRCLE) : " ", off_state);
1859
1860                 if (m->is_host)
1861                         printf("%-*s (host) %s%-*s%s %s%*u%s %*u\n",
1862                                (int) (namelen - (sizeof(" (host)")-1)), strna(m->name),
1863                                on_state, statelen, strna(m->state), off_state,
1864                                on_failed, failedlen, m->n_failed_units, off_failed,
1865                                jobslen, m->n_jobs);
1866                 else
1867                         printf("%-*s %s%-*s%s %s%*u%s %*u\n",
1868                                namelen, strna(m->name),
1869                                on_state, statelen, strna(m->state), off_state,
1870                                on_failed, failedlen, m->n_failed_units, off_failed,
1871                                jobslen, m->n_jobs);
1872         }
1873
1874         if (!arg_no_legend)
1875                 printf("\n%u machines listed.\n", n);
1876 }
1877
1878 static int list_machines(sd_bus *bus, char **args) {
1879         struct machine_info *machine_infos = NULL;
1880         int r;
1881
1882         assert(bus);
1883
1884         if (geteuid() != 0) {
1885                 log_error("Must be root.");
1886                 return -EPERM;
1887         }
1888
1889         pager_open_if_enabled();
1890
1891         r = get_machine_list(bus, &machine_infos, strv_skip_first(args));
1892         if (r < 0)
1893                 return r;
1894
1895         qsort_safe(machine_infos, r, sizeof(struct machine_info), compare_machine_info);
1896         output_machines_list(machine_infos, r);
1897         free_machines_list(machine_infos, r);
1898
1899         return 0;
1900 }
1901
1902 static int get_default(sd_bus *bus, char **args) {
1903         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1904         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1905         _cleanup_free_ char *_path = NULL;
1906         const char *path;
1907         int r;
1908
1909         if (!bus || avoid_bus()) {
1910                 r = unit_file_get_default(arg_scope, arg_root, &_path);
1911                 if (r < 0)
1912                         return log_error_errno(r, "Failed to get default target: %m");
1913                 path = _path;
1914
1915         } else {
1916                 r = sd_bus_call_method(
1917                                 bus,
1918                                 "org.freedesktop.systemd1",
1919                                 "/org/freedesktop/systemd1",
1920                                 "org.freedesktop.systemd1.Manager",
1921                                 "GetDefaultTarget",
1922                                 &error,
1923                                 &reply,
1924                                 NULL);
1925                 if (r < 0) {
1926                         log_error("Failed to get default target: %s", bus_error_message(&error, -r));
1927                         return r;
1928                 }
1929
1930                 r = sd_bus_message_read(reply, "s", &path);
1931                 if (r < 0)
1932                         return bus_log_parse_error(r);
1933         }
1934
1935         if (path)
1936                 printf("%s\n", path);
1937
1938         return 0;
1939 }
1940
1941 static void dump_unit_file_changes(const UnitFileChange *changes, unsigned n_changes) {
1942         unsigned i;
1943
1944         assert(changes || n_changes == 0);
1945
1946         for (i = 0; i < n_changes; i++) {
1947                 if (changes[i].type == UNIT_FILE_SYMLINK)
1948                         log_info("Created symlink from %s to %s.", changes[i].path, changes[i].source);
1949                 else
1950                         log_info("Removed symlink %s.", changes[i].path);
1951         }
1952 }
1953
1954 static int set_default(sd_bus *bus, char **args) {
1955         _cleanup_free_ char *unit = NULL;
1956         UnitFileChange *changes = NULL;
1957         unsigned n_changes = 0;
1958         int r;
1959
1960         unit = unit_name_mangle_with_suffix(args[1], MANGLE_NOGLOB, ".target");
1961         if (!unit)
1962                 return log_oom();
1963
1964         if (!bus || avoid_bus()) {
1965                 r = unit_file_set_default(arg_scope, arg_root, unit, true, &changes, &n_changes);
1966                 if (r < 0)
1967                         return log_error_errno(r, "Failed to set default target: %m");
1968
1969                 if (!arg_quiet)
1970                         dump_unit_file_changes(changes, n_changes);
1971
1972                 r = 0;
1973         } else {
1974                 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
1975                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1976
1977                 r = sd_bus_message_new_method_call(
1978                                 bus,
1979                                 &m,
1980                                 "org.freedesktop.systemd1",
1981                                 "/org/freedesktop/systemd1",
1982                                 "org.freedesktop.systemd1.Manager",
1983                                 "SetDefaultTarget");
1984                 if (r < 0)
1985                         return bus_log_create_error(r);
1986
1987                 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1988                 if (r < 0)
1989                         return bus_log_create_error(r);
1990
1991                 r = sd_bus_message_append(m, "sb", unit, 1);
1992                 if (r < 0)
1993                         return bus_log_create_error(r);
1994
1995                 r = sd_bus_call(bus, m, 0, &error, &reply);
1996                 if (r < 0) {
1997                         log_error("Failed to set default target: %s", bus_error_message(&error, -r));
1998                         return r;
1999                 }
2000
2001                 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
2002                 if (r < 0)
2003                         return r;
2004
2005                 /* Try to reload if enabled */
2006                 if (!arg_no_reload)
2007                         r = daemon_reload(bus, args);
2008                 else
2009                         r = 0;
2010         }
2011
2012         unit_file_changes_free(changes, n_changes);
2013
2014         return r;
2015 }
2016
2017 struct job_info {
2018         uint32_t id;
2019         const char *name, *type, *state;
2020 };
2021
2022 static void output_jobs_list(const struct job_info* jobs, unsigned n, bool skipped) {
2023         unsigned id_len, unit_len, type_len, state_len;
2024         const struct job_info *j;
2025         const char *on, *off;
2026         bool shorten = false;
2027
2028         assert(n == 0 || jobs);
2029
2030         if (n == 0) {
2031                 on = ansi_highlight_green();
2032                 off = ansi_highlight_off();
2033
2034                 printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off);
2035                 return;
2036         }
2037
2038         pager_open_if_enabled();
2039
2040         id_len = strlen("JOB");
2041         unit_len = strlen("UNIT");
2042         type_len = strlen("TYPE");
2043         state_len = strlen("STATE");
2044
2045         for (j = jobs; j < jobs + n; j++) {
2046                 uint32_t id = j->id;
2047                 assert(j->name && j->type && j->state);
2048
2049                 id_len = MAX(id_len, DECIMAL_STR_WIDTH(id));
2050                 unit_len = MAX(unit_len, strlen(j->name));
2051                 type_len = MAX(type_len, strlen(j->type));
2052                 state_len = MAX(state_len, strlen(j->state));
2053         }
2054
2055         if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) {
2056                 unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3);
2057                 shorten = true;
2058         }
2059
2060         if (!arg_no_legend)
2061                 printf("%*s %-*s %-*s %-*s\n",
2062                        id_len, "JOB",
2063                        unit_len, "UNIT",
2064                        type_len, "TYPE",
2065                        state_len, "STATE");
2066
2067         for (j = jobs; j < jobs + n; j++) {
2068                 _cleanup_free_ char *e = NULL;
2069
2070                 if (streq(j->state, "running")) {
2071                         on = ansi_highlight();
2072                         off = ansi_highlight_off();
2073                 } else
2074                         on = off = "";
2075
2076                 e = shorten ? ellipsize(j->name, unit_len, 33) : NULL;
2077                 printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
2078                        id_len, j->id,
2079                        on, unit_len, e ? e : j->name, off,
2080                        type_len, j->type,
2081                        on, state_len, j->state, off);
2082         }
2083
2084         if (!arg_no_legend) {
2085                 on = ansi_highlight();
2086                 off = ansi_highlight_off();
2087
2088                 printf("\n%s%u jobs listed%s.\n", on, n, off);
2089         }
2090 }
2091
2092 static bool output_show_job(struct job_info *job, char **patterns) {
2093         char **pattern;
2094
2095         assert(job);
2096
2097         if (strv_isempty(patterns))
2098                 return true;
2099
2100         STRV_FOREACH(pattern, patterns)
2101                 if (fnmatch(*pattern, job->name, FNM_NOESCAPE) == 0)
2102                         return true;
2103         return false;
2104 }
2105
2106 static int list_jobs(sd_bus *bus, char **args) {
2107         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2108         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2109         const char *name, *type, *state, *job_path, *unit_path;
2110         _cleanup_free_ struct job_info *jobs = NULL;
2111         size_t size = 0;
2112         unsigned c = 0;
2113         uint32_t id;
2114         int r;
2115         bool skipped = false;
2116
2117         r = sd_bus_call_method(
2118                         bus,
2119                         "org.freedesktop.systemd1",
2120                         "/org/freedesktop/systemd1",
2121                         "org.freedesktop.systemd1.Manager",
2122                         "ListJobs",
2123                         &error,
2124                         &reply,
2125                         NULL);
2126         if (r < 0) {
2127                 log_error("Failed to list jobs: %s", bus_error_message(&error, r));
2128                 return r;
2129         }
2130
2131         r = sd_bus_message_enter_container(reply, 'a', "(usssoo)");
2132         if (r < 0)
2133                 return bus_log_parse_error(r);
2134
2135         while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, &job_path, &unit_path)) > 0) {
2136                 struct job_info job = { id, name, type, state };
2137
2138                 if (!output_show_job(&job, strv_skip_first(args))) {
2139                         skipped = true;
2140                         continue;
2141                 }
2142
2143                 if (!GREEDY_REALLOC(jobs, size, c + 1))
2144                         return log_oom();
2145
2146                 jobs[c++] = job;
2147         }
2148         if (r < 0)
2149                 return bus_log_parse_error(r);
2150
2151         r = sd_bus_message_exit_container(reply);
2152         if (r < 0)
2153                 return bus_log_parse_error(r);
2154
2155         output_jobs_list(jobs, c, skipped);
2156         return r;
2157 }
2158
2159 static int cancel_job(sd_bus *bus, char **args) {
2160         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2161         char **name;
2162         int r = 0;
2163
2164         assert(args);
2165
2166         if (strv_length(args) <= 1)
2167                 return daemon_reload(bus, args);
2168
2169         STRV_FOREACH(name, args+1) {
2170                 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2171                 uint32_t id;
2172                 int q;
2173
2174                 q = safe_atou32(*name, &id);
2175                 if (q < 0)
2176                         return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name);
2177
2178                 q = sd_bus_message_new_method_call(
2179                                 bus,
2180                                 &m,
2181                                 "org.freedesktop.systemd1",
2182                                 "/org/freedesktop/systemd1",
2183                                 "org.freedesktop.systemd1.Manager",
2184                                 "CancelJob");
2185                 if (q < 0)
2186                         return bus_log_create_error(q);
2187
2188                 q = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
2189                 if (q < 0)
2190                         return bus_log_create_error(1);
2191
2192                 q = sd_bus_message_append(m, "u", id);
2193                 if (q < 0)
2194                         return bus_log_create_error(q);
2195
2196                 q = sd_bus_call(bus, m, 0, &error, NULL);
2197                 if (q < 0) {
2198                         log_error("Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q));
2199                         if (r == 0)
2200                                 r = q;
2201                 }
2202         }
2203
2204         return r;
2205 }
2206
2207 static int need_daemon_reload(sd_bus *bus, const char *unit) {
2208         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2209         const char *path;
2210         int b, r;
2211
2212         /* We ignore all errors here, since this is used to show a
2213          * warning only */
2214
2215         /* We don't use unit_dbus_path_from_name() directly since we
2216          * don't want to load the unit if it isn't loaded. */
2217
2218         r = sd_bus_call_method(
2219                         bus,
2220                         "org.freedesktop.systemd1",
2221                         "/org/freedesktop/systemd1",
2222                         "org.freedesktop.systemd1.Manager",
2223                         "GetUnit",
2224                         NULL,
2225                         &reply,
2226                         "s", unit);
2227         if (r < 0)
2228                 return r;
2229
2230         r = sd_bus_message_read(reply, "o", &path);
2231         if (r < 0)
2232                 return r;
2233
2234         r = sd_bus_get_property_trivial(
2235                         bus,
2236                         "org.freedesktop.systemd1",
2237                         path,
2238                         "org.freedesktop.systemd1.Unit",
2239                         "NeedDaemonReload",
2240                         NULL,
2241                         'b', &b);
2242         if (r < 0)
2243                 return r;
2244
2245         return b;
2246 }
2247
2248 static void warn_unit_file_changed(const char *name) {
2249         log_warning("%sWarning:%s %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.",
2250                     ansi_highlight_red(),
2251                     ansi_highlight_off(),
2252                     name,
2253                     arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
2254 }
2255
2256 static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **unit_path) {
2257         char **p;
2258
2259         assert(lp);
2260         assert(unit_name);
2261         assert(unit_path);
2262
2263         STRV_FOREACH(p, lp->unit_path) {
2264                 _cleanup_free_ char *path;
2265
2266                 path = path_join(arg_root, *p, unit_name);
2267                 if (!path)
2268                         return log_oom();
2269
2270                 if (access(path, F_OK) == 0) {
2271                         *unit_path = path;
2272                         path = NULL;
2273                         return 1;
2274                 }
2275         }
2276
2277         return 0;
2278 }
2279
2280 static int unit_find_paths(sd_bus *bus,
2281                            const char *unit_name,
2282                            bool avoid_bus_cache,
2283                            LookupPaths *lp,
2284                            char **fragment_path,
2285                            char ***dropin_paths) {
2286         int r;
2287
2288         /**
2289          * Finds where the unit is defined on disk. Returns 0 if the unit
2290          * is not found. Returns 1 if it is found, and sets
2291          * - the path to the unit in *path, if it exists on disk,
2292          * - and a strv of existing drop-ins in *dropins,
2293          *   if the arg is not NULL and any dropins were found.
2294          */
2295
2296         assert(unit_name);
2297         assert(fragment_path);
2298         assert(lp);
2299
2300         if (!avoid_bus_cache && !unit_name_is_template(unit_name)) {
2301                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2302                 _cleanup_free_ char *unit = NULL;
2303                 _cleanup_free_ char *path = NULL;
2304                 _cleanup_strv_free_ char **dropins = NULL;
2305
2306                 unit = unit_dbus_path_from_name(unit_name);
2307                 if (!unit)
2308                         return log_oom();
2309
2310                 if (need_daemon_reload(bus, unit_name) > 0)
2311                         warn_unit_file_changed(unit_name);
2312
2313                 r = sd_bus_get_property_string(
2314                                 bus,
2315                                 "org.freedesktop.systemd1",
2316                                 unit,
2317                                 "org.freedesktop.systemd1.Unit",
2318                                 "FragmentPath",
2319                                 &error,
2320                                 &path);
2321                 if (r < 0)
2322                         return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r));
2323
2324                 r = sd_bus_get_property_strv(
2325                                 bus,
2326                                 "org.freedesktop.systemd1",
2327                                 unit,
2328                                 "org.freedesktop.systemd1.Unit",
2329                                 "DropInPaths",
2330                                 &error,
2331                                 &dropins);
2332                 if (r < 0)
2333                         return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r));
2334
2335                 r = 0;
2336                 if (!isempty(path)) {
2337                         *fragment_path = path;
2338                         path = NULL;
2339                         r = 1;
2340                 }
2341
2342                 if (dropin_paths && !strv_isempty(dropins)) {
2343                         *dropin_paths = dropins;
2344                         dropins = NULL;
2345                         r = 1;
2346                 }
2347         } else {
2348                 _cleanup_set_free_ Set *names;
2349
2350                 names = set_new(NULL);
2351                 if (!names)
2352                         return -ENOMEM;
2353
2354                 r = set_put(names, unit_name);
2355                 if (r < 0)
2356                         return r;
2357
2358                 r = unit_file_find_path(lp, unit_name, fragment_path);
2359                 if (r < 0)
2360                         return r;
2361
2362                 if (r == 0) {
2363                         _cleanup_free_ char *template;
2364
2365                         template = unit_name_template(unit_name);
2366                         if (!template)
2367                                 return log_oom();
2368
2369                         if (!streq(template, unit_name)) {
2370                                 r = unit_file_find_path(lp, template, fragment_path);
2371                                 if (r < 0)
2372                                         return r;
2373                         }
2374                 }
2375
2376                 if (dropin_paths)
2377                         r = unit_file_find_dropin_paths(lp->unit_path, NULL, names, dropin_paths);
2378         }
2379
2380         return r;
2381 }
2382
2383 static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet) {
2384         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2385         _cleanup_free_ char *n = NULL, *state = NULL;
2386         const char *path;
2387         int r;
2388
2389         assert(name);
2390
2391         n = unit_name_mangle(name, MANGLE_NOGLOB);
2392         if (!n)
2393                 return log_oom();
2394
2395         /* We don't use unit_dbus_path_from_name() directly since we
2396          * don't want to load the unit if it isn't loaded. */
2397
2398         r = sd_bus_call_method(
2399                         bus,
2400                         "org.freedesktop.systemd1",
2401                         "/org/freedesktop/systemd1",
2402                         "org.freedesktop.systemd1.Manager",
2403                         "GetUnit",
2404                         NULL,
2405                         &reply,
2406                         "s", n);
2407         if (r < 0) {
2408                 if (!quiet)
2409                         puts("unknown");
2410                 return 0;
2411         }
2412
2413         r = sd_bus_message_read(reply, "o", &path);
2414         if (r < 0)
2415                 return bus_log_parse_error(r);
2416
2417         r = sd_bus_get_property_string(
2418                         bus,
2419                         "org.freedesktop.systemd1",
2420                         path,
2421                         "org.freedesktop.systemd1.Unit",
2422                         "ActiveState",
2423                         NULL,
2424                         &state);
2425         if (r < 0) {
2426                 if (!quiet)
2427                         puts("unknown");
2428                 return 0;
2429         }
2430
2431         if (!quiet)
2432                 puts(state);
2433
2434         return nulstr_contains(good_states, state);
2435 }
2436
2437 static int check_triggering_units(
2438                 sd_bus *bus,
2439                 const char *name) {
2440
2441         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2442         _cleanup_free_ char *path = NULL, *n = NULL, *state = NULL;
2443         _cleanup_strv_free_ char **triggered_by = NULL;
2444         bool print_warning_label = true;
2445         char **i;
2446         int r;
2447
2448         n = unit_name_mangle(name, MANGLE_NOGLOB);
2449         if (!n)
2450                 return log_oom();
2451
2452         path = unit_dbus_path_from_name(n);
2453         if (!path)
2454                 return log_oom();
2455
2456         r = sd_bus_get_property_string(
2457                         bus,
2458                         "org.freedesktop.systemd1",
2459                         path,
2460                         "org.freedesktop.systemd1.Unit",
2461                         "LoadState",
2462                         &error,
2463                         &state);
2464         if (r < 0) {
2465                 log_error("Failed to get load state of %s: %s", n, bus_error_message(&error, r));
2466                 return r;
2467         }
2468
2469         if (streq(state, "masked"))
2470                 return 0;
2471
2472         r = sd_bus_get_property_strv(
2473                         bus,
2474                         "org.freedesktop.systemd1",
2475                         path,
2476                         "org.freedesktop.systemd1.Unit",
2477                         "TriggeredBy",
2478                         &error,
2479                         &triggered_by);
2480         if (r < 0) {
2481                 log_error("Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r));
2482                 return r;
2483         }
2484
2485         STRV_FOREACH(i, triggered_by) {
2486                 r = check_one_unit(bus, *i, "active\0reloading\0", true);
2487                 if (r < 0)
2488                         return log_error_errno(r, "Failed to check unit: %m");
2489
2490                 if (r == 0)
2491                         continue;
2492
2493                 if (print_warning_label) {
2494                         log_warning("Warning: Stopping %s, but it can still be activated by:", n);
2495                         print_warning_label = false;
2496                 }
2497
2498                 log_warning("  %s", *i);
2499         }
2500
2501         return 0;
2502 }
2503
2504 static const struct {
2505         const char *verb;
2506         const char *method;
2507 } unit_actions[] = {
2508         { "start",                 "StartUnit" },
2509         { "stop",                  "StopUnit" },
2510         { "condstop",              "StopUnit" },
2511         { "reload",                "ReloadUnit" },
2512         { "restart",               "RestartUnit" },
2513         { "try-restart",           "TryRestartUnit" },
2514         { "condrestart",           "TryRestartUnit" },
2515         { "reload-or-restart",     "ReloadOrRestartUnit" },
2516         { "reload-or-try-restart", "ReloadOrTryRestartUnit" },
2517         { "condreload",            "ReloadOrTryRestartUnit" },
2518         { "force-reload",          "ReloadOrTryRestartUnit" }
2519 };
2520
2521 static const char *verb_to_method(const char *verb) {
2522        uint i;
2523
2524        for (i = 0; i < ELEMENTSOF(unit_actions); i++)
2525                 if (streq_ptr(unit_actions[i].verb, verb))
2526                         return unit_actions[i].method;
2527
2528        return "StartUnit";
2529 }
2530
2531 static const char *method_to_verb(const char *method) {
2532        uint i;
2533
2534        for (i = 0; i < ELEMENTSOF(unit_actions); i++)
2535                 if (streq_ptr(unit_actions[i].method, method))
2536                         return unit_actions[i].verb;
2537
2538        return "n/a";
2539 }
2540
2541 static int start_unit_one(
2542                 sd_bus *bus,
2543                 const char *method,
2544                 const char *name,
2545                 const char *mode,
2546                 sd_bus_error *error,
2547                 BusWaitForJobs *w) {
2548
2549         _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
2550         const char *path;
2551         int r;
2552
2553         assert(method);
2554         assert(name);
2555         assert(mode);
2556         assert(error);
2557
2558         log_debug("Calling manager for %s on %s, %s", method, name, mode);
2559
2560         r = sd_bus_message_new_method_call(
2561                         bus,
2562                         &m,
2563                         "org.freedesktop.systemd1",
2564                         "/org/freedesktop/systemd1",
2565                         "org.freedesktop.systemd1.Manager",
2566                         method);
2567         if (r < 0)
2568                 return bus_log_create_error(r);
2569
2570         r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
2571         if (r < 0)
2572                 return bus_log_create_error(r);
2573
2574         r = sd_bus_message_append(m, "ss", name, mode);
2575         if (r < 0)
2576                 return bus_log_create_error(r);
2577
2578         r = sd_bus_call(bus, m, 0, error, &reply);
2579         if (r < 0) {
2580                 const char *verb;
2581
2582                 if (r == -ENOENT && arg_action != ACTION_SYSTEMCTL)
2583                         /* There's always a fallback possible for
2584                          * legacy actions. */
2585                         return -EADDRNOTAVAIL;
2586
2587                 verb = method_to_verb(method);
2588
2589                 log_error("Failed to %s %s: %s", verb, name, bus_error_message(error, r));
2590                 return r;
2591         }
2592
2593         r = sd_bus_message_read(reply, "o", &path);
2594         if (r < 0)
2595                 return bus_log_parse_error(r);
2596
2597         if (need_daemon_reload(bus, name) > 0)
2598                 warn_unit_file_changed(name);
2599
2600         if (w) {
2601                 log_debug("Adding %s to the set", path);
2602                 r = bus_wait_for_jobs_add(w, path);
2603                 if (r < 0)
2604                         return log_oom();
2605         }
2606
2607         return 0;
2608 }
2609
2610 static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) {
2611
2612         _cleanup_strv_free_ char **mangled = NULL, **globs = NULL;
2613         char **name;
2614         int r = 0, i;
2615
2616         STRV_FOREACH(name, names) {
2617                 char *t;
2618
2619                 if (suffix)
2620                         t = unit_name_mangle_with_suffix(*name, MANGLE_GLOB, suffix);
2621                 else
2622                         t = unit_name_mangle(*name, MANGLE_GLOB);
2623                 if (!t)
2624                         return log_oom();
2625
2626                 if (string_is_glob(t))
2627                         r = strv_consume(&globs, t);
2628                 else
2629                         r = strv_consume(&mangled, t);
2630                 if (r < 0)
2631                         return log_oom();
2632         }
2633
2634         /* Query the manager only if any of the names are a glob, since
2635          * this is fairly expensive */
2636         if (!strv_isempty(globs)) {
2637                 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2638                 _cleanup_free_ UnitInfo *unit_infos = NULL;
2639
2640                 if (!bus)
2641                         return log_error_errno(ENOTSUP, "Unit name globbing without bus is not implemented.");
2642
2643                 r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply);
2644                 if (r < 0)
2645                         return r;
2646
2647                 for (i = 0; i < r; i++)
2648                         if (strv_extend(&mangled, unit_infos[i].id) < 0)
2649                                 return log_oom();
2650         }
2651
2652         *ret = mangled;
2653         mangled = NULL; /* do not free */
2654
2655         return 0;
2656 }
2657
2658 static const struct {
2659         const char *target;
2660         const char *verb;
2661         const char *mode;
2662 } action_table[_ACTION_MAX] = {
2663         [ACTION_HALT]         = { SPECIAL_HALT_TARGET,         "halt",         "replace-irreversibly" },
2664         [ACTION_POWEROFF]     = { SPECIAL_POWEROFF_TARGET,     "poweroff",     "replace-irreversibly" },
2665         [ACTION_REBOOT]       = { SPECIAL_REBOOT_TARGET,       "reboot",       "replace-irreversibly" },
2666         [ACTION_KEXEC]        = { SPECIAL_KEXEC_TARGET,        "kexec",        "replace-irreversibly" },
2667         [ACTION_RUNLEVEL2]    = { SPECIAL_RUNLEVEL2_TARGET,    NULL,           "isolate" },
2668         [ACTION_RUNLEVEL3]    = { SPECIAL_RUNLEVEL3_TARGET,    NULL,           "isolate" },
2669         [ACTION_RUNLEVEL4]    = { SPECIAL_RUNLEVEL4_TARGET,    NULL,           "isolate" },
2670         [ACTION_RUNLEVEL5]    = { SPECIAL_RUNLEVEL5_TARGET,    NULL,           "isolate" },
2671         [ACTION_RESCUE]       = { SPECIAL_RESCUE_TARGET,       "rescue",       "isolate" },
2672         [ACTION_EMERGENCY]    = { SPECIAL_EMERGENCY_TARGET,    "emergency",    "isolate" },
2673         [ACTION_DEFAULT]      = { SPECIAL_DEFAULT_TARGET,      "default",      "isolate" },
2674         [ACTION_EXIT]         = { SPECIAL_EXIT_TARGET,         "exit",         "replace-irreversibly" },
2675         [ACTION_SUSPEND]      = { SPECIAL_SUSPEND_TARGET,      "suspend",      "replace-irreversibly" },
2676         [ACTION_HIBERNATE]    = { SPECIAL_HIBERNATE_TARGET,    "hibernate",    "replace-irreversibly" },
2677         [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" },
2678 };
2679
2680 static enum action verb_to_action(const char *verb) {
2681         enum action i;
2682
2683         for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
2684                 if (streq_ptr(action_table[i].verb, verb))
2685                         return i;
2686
2687         return _ACTION_INVALID;
2688 }
2689
2690 static int start_unit(sd_bus *bus, char **args) {
2691         _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
2692         const char *method, *mode, *one_name, *suffix = NULL;
2693         _cleanup_strv_free_ char **names = NULL;
2694         char **name;
2695         int r = 0;
2696
2697         assert(bus);
2698
2699         ask_password_agent_open_if_enabled();
2700
2701         if (arg_action == ACTION_SYSTEMCTL) {
2702                 enum action action;
2703                 method = verb_to_method(args[0]);
2704                 action = verb_to_action(args[0]);
2705
2706                 if (streq(args[0], "isolate")) {
2707                         mode = "isolate";
2708                         suffix = ".target";
2709                 } else
2710                         mode = action_table[action].mode ?: arg_job_mode;
2711
2712                 one_name = action_table[action].target;
2713         } else {
2714                 assert(arg_action < ELEMENTSOF(action_table));
2715                 assert(action_table[arg_action].target);
2716
2717                 method = "StartUnit";
2718
2719                 mode = action_table[arg_action].mode;
2720                 one_name = action_table[arg_action].target;
2721         }
2722
2723         if (one_name)
2724                 names = strv_new(one_name, NULL);
2725         else {
2726                 r = expand_names(bus, args + 1, suffix, &names);
2727                 if (r < 0)
2728                         log_error_errno(r, "Failed to expand names: %m");
2729         }
2730
2731         if (!arg_no_block) {
2732                 r = bus_wait_for_jobs_new(bus, &w);
2733                 if (r < 0)
2734                         return log_error_errno(r, "Could not watch jobs: %m");
2735         }
2736
2737         STRV_FOREACH(name, names) {
2738                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2739                 int q;
2740
2741                 q = start_unit_one(bus, method, *name, mode, &error, w);
2742                 if (r >= 0 && q < 0)
2743                         r = translate_bus_error_to_exit_status(q, &error);
2744         }
2745
2746         if (!arg_no_block) {
2747                 int q;
2748
2749                 q = bus_wait_for_jobs(w, arg_quiet);
2750                 if (q < 0)
2751                         return q;
2752
2753                 /* When stopping units, warn if they can still be triggered by
2754                  * another active unit (socket, path, timer) */
2755                 if (!arg_quiet && streq(method, "StopUnit"))
2756                         STRV_FOREACH(name, names)
2757                                 check_triggering_units(bus, *name);
2758         }
2759
2760         return r;
2761 }
2762
2763 /* Ask systemd-logind, which might grant access to unprivileged users
2764  * through PolicyKit */
2765 static int reboot_with_logind(sd_bus *bus, enum action a) {
2766 #ifdef HAVE_LOGIND
2767         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2768         const char *method;
2769         int r;
2770
2771         if (!bus)
2772                 return -EIO;
2773
2774         polkit_agent_open_if_enabled();
2775
2776         switch (a) {
2777
2778         case ACTION_REBOOT:
2779                 method = "Reboot";
2780                 break;
2781
2782         case ACTION_POWEROFF:
2783                 method = "PowerOff";
2784                 break;
2785
2786         case ACTION_SUSPEND:
2787                 method = "Suspend";
2788                 break;
2789
2790         case ACTION_HIBERNATE:
2791                 method = "Hibernate";
2792                 break;
2793
2794         case ACTION_HYBRID_SLEEP:
2795                 method = "HybridSleep";
2796                 break;
2797
2798         default:
2799                 return -EINVAL;
2800         }
2801
2802         r = sd_bus_call_method(
2803                         bus,
2804                         "org.freedesktop.login1",
2805                         "/org/freedesktop/login1",
2806                         "org.freedesktop.login1.Manager",
2807                         method,
2808                         &error,
2809                         NULL,
2810                         "b", arg_ask_password);
2811         if (r < 0)
2812                 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
2813
2814         return r;
2815 #else
2816         return -ENOSYS;
2817 #endif
2818 }
2819
2820 static int check_inhibitors(sd_bus *bus, enum action a) {
2821 #ifdef HAVE_LOGIND
2822         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2823         _cleanup_strv_free_ char **sessions = NULL;
2824         const char *what, *who, *why, *mode;
2825         uint32_t uid, pid;
2826         unsigned c = 0;
2827         char **s;
2828         int r;
2829
2830         if (!bus)
2831                 return 0;
2832
2833         if (arg_ignore_inhibitors || arg_force > 0)
2834                 return 0;
2835
2836         if (arg_when > 0)
2837                 return 0;
2838
2839         if (geteuid() == 0)
2840                 return 0;
2841
2842         if (!on_tty())
2843                 return 0;
2844
2845         r = sd_bus_call_method(
2846                         bus,
2847                         "org.freedesktop.login1",
2848                         "/org/freedesktop/login1",
2849                         "org.freedesktop.login1.Manager",
2850                         "ListInhibitors",
2851                         NULL,
2852                         &reply,
2853                         NULL);
2854         if (r < 0)
2855                 /* If logind is not around, then there are no inhibitors... */
2856                 return 0;
2857
2858         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
2859         if (r < 0)
2860                 return bus_log_parse_error(r);
2861
2862         while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
2863                 _cleanup_free_ char *comm = NULL, *user = NULL;
2864                 _cleanup_strv_free_ char **sv = NULL;
2865
2866                 if (!streq(mode, "block"))
2867                         continue;
2868
2869                 sv = strv_split(what, ":");
2870                 if (!sv)
2871                         return log_oom();
2872
2873                 if (!strv_contains(sv,
2874                                   a == ACTION_HALT ||
2875                                   a == ACTION_POWEROFF ||
2876                                   a == ACTION_REBOOT ||
2877                                   a == ACTION_KEXEC ? "shutdown" : "sleep"))
2878                         continue;
2879
2880                 get_process_comm(pid, &comm);
2881                 user = uid_to_name(uid);
2882
2883                 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
2884                             who, pid, strna(comm), strna(user), why);
2885
2886                 c++;
2887         }
2888         if (r < 0)
2889                 return bus_log_parse_error(r);
2890
2891         r = sd_bus_message_exit_container(reply);
2892         if (r < 0)
2893                 return bus_log_parse_error(r);
2894
2895         /* Check for current sessions */
2896         sd_get_sessions(&sessions);
2897         STRV_FOREACH(s, sessions) {
2898                 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
2899
2900                 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
2901                         continue;
2902
2903                 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
2904                         continue;
2905
2906                 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
2907                         continue;
2908
2909                 sd_session_get_tty(*s, &tty);
2910                 sd_session_get_seat(*s, &seat);
2911                 sd_session_get_service(*s, &service);
2912                 user = uid_to_name(uid);
2913
2914                 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
2915                 c++;
2916         }
2917
2918         if (c <= 0)
2919                 return 0;
2920
2921         log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
2922                   action_table[a].verb);
2923
2924         return -EPERM;
2925 #else
2926         return 0;
2927 #endif
2928 }
2929
2930 static int start_special(sd_bus *bus, char **args) {
2931         enum action a;
2932         int r;
2933
2934         assert(args);
2935
2936         a = verb_to_action(args[0]);
2937
2938         r = check_inhibitors(bus, a);
2939         if (r < 0)
2940                 return r;
2941
2942         if (arg_force >= 2 && geteuid() != 0) {
2943                 log_error("Must be root.");
2944                 return -EPERM;
2945         }
2946
2947         if (arg_force >= 2 &&
2948             (a == ACTION_HALT ||
2949              a == ACTION_POWEROFF ||
2950              a == ACTION_REBOOT))
2951                 return halt_now(a);
2952
2953         if (arg_force >= 1 &&
2954             (a == ACTION_HALT ||
2955              a == ACTION_POWEROFF ||
2956              a == ACTION_REBOOT ||
2957              a == ACTION_KEXEC ||
2958              a == ACTION_EXIT))
2959                 return daemon_reload(bus, args);
2960
2961         /* first try logind, to allow authentication with polkit */
2962         if (geteuid() != 0 &&
2963             (a == ACTION_POWEROFF ||
2964              a == ACTION_REBOOT ||
2965              a == ACTION_SUSPEND ||
2966              a == ACTION_HIBERNATE ||
2967              a == ACTION_HYBRID_SLEEP)) {
2968                 r = reboot_with_logind(bus, a);
2969                 if (r >= 0 || IN_SET(r, -ENOTSUP, -EINPROGRESS))
2970                         return r;
2971         }
2972
2973         r = start_unit(bus, args);
2974         if (r == EXIT_SUCCESS)
2975                 warn_wall(a);
2976
2977         return r;
2978 }
2979
2980 static int check_unit_generic(sd_bus *bus, int code, const char *good_states, char **args) {
2981         _cleanup_strv_free_ char **names = NULL;
2982         char **name;
2983         int r;
2984
2985         assert(bus);
2986         assert(args);
2987
2988         r = expand_names(bus, args, NULL, &names);
2989         if (r < 0)
2990                 return log_error_errno(r, "Failed to expand names: %m");
2991
2992         STRV_FOREACH(name, names) {
2993                 int state;
2994
2995                 state = check_one_unit(bus, *name, good_states, arg_quiet);
2996                 if (state < 0)
2997                         return state;
2998                 if (state == 0)
2999                         r = code;
3000         }
3001
3002         return r;
3003 }
3004
3005 static int check_unit_active(sd_bus *bus, char **args) {
3006         /* According to LSB: 3, "program is not running" */
3007         return check_unit_generic(bus, 3, "active\0reloading\0", args + 1);
3008 }
3009
3010 static int check_unit_failed(sd_bus *bus, char **args) {
3011         return check_unit_generic(bus, 1, "failed\0", args + 1);
3012 }
3013
3014 static int kill_unit(sd_bus *bus, char **args) {
3015         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3016         _cleanup_strv_free_ char **names = NULL;
3017         char **name;
3018         int r, q;
3019
3020         assert(bus);
3021         assert(args);
3022
3023         if (!arg_kill_who)
3024                 arg_kill_who = "all";
3025
3026         r = expand_names(bus, args + 1, NULL, &names);
3027         if (r < 0)
3028                 log_error_errno(r, "Failed to expand names: %m");
3029
3030         STRV_FOREACH(name, names) {
3031                 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3032
3033                 q = sd_bus_message_new_method_call(
3034                                 bus,
3035                                 &m,
3036                                 "org.freedesktop.systemd1",
3037                                 "/org/freedesktop/systemd1",
3038                                 "org.freedesktop.systemd1.Manager",
3039                                 "KillUnit");
3040                 if (q < 0)
3041                         return bus_log_create_error(q);
3042
3043                 q = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
3044                 if (q < 0)
3045                         return bus_log_create_error(q);
3046
3047                 q = sd_bus_message_append(m, "ssi", *names, arg_kill_who, arg_signal);
3048                 if (q < 0)
3049                         return bus_log_create_error(q);
3050
3051                 q = sd_bus_call(bus, m, 0, &error, NULL);
3052                 if (q < 0) {
3053                         log_error("Failed to kill unit %s: %s", *names, bus_error_message(&error, q));
3054                         if (r == 0)
3055                                 r = q;
3056                 }
3057         }
3058
3059         return r;
3060 }
3061
3062 typedef struct ExecStatusInfo {
3063         char *name;
3064
3065         char *path;
3066         char **argv;
3067
3068         bool ignore;
3069
3070         usec_t start_timestamp;
3071         usec_t exit_timestamp;
3072         pid_t pid;
3073         int code;
3074         int status;
3075
3076         LIST_FIELDS(struct ExecStatusInfo, exec);
3077 } ExecStatusInfo;
3078
3079 static void exec_status_info_free(ExecStatusInfo *i) {
3080         assert(i);
3081
3082         free(i->name);
3083         free(i->path);
3084         strv_free(i->argv);
3085         free(i);
3086 }
3087
3088 static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i) {
3089         uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
3090         const char *path;
3091         uint32_t pid;
3092         int32_t code, status;
3093         int ignore, r;
3094
3095         assert(m);
3096         assert(i);
3097
3098         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sasbttttuii");
3099         if (r < 0)
3100                 return bus_log_parse_error(r);
3101         else if (r == 0)
3102                 return 0;
3103
3104         r = sd_bus_message_read(m, "s", &path);
3105         if (r < 0)
3106                 return bus_log_parse_error(r);
3107
3108         i->path = strdup(path);
3109         if (!i->path)
3110                 return log_oom();
3111
3112         r = sd_bus_message_read_strv(m, &i->argv);
3113         if (r < 0)
3114                 return bus_log_parse_error(r);
3115
3116         r = sd_bus_message_read(m,
3117                                 "bttttuii",
3118                                 &ignore,
3119                                 &start_timestamp, &start_timestamp_monotonic,
3120                                 &exit_timestamp, &exit_timestamp_monotonic,
3121                                 &pid,
3122                                 &code, &status);
3123         if (r < 0)
3124                 return bus_log_parse_error(r);
3125
3126         i->ignore = ignore;
3127         i->start_timestamp = (usec_t) start_timestamp;
3128         i->exit_timestamp = (usec_t) exit_timestamp;
3129         i->pid = (pid_t) pid;
3130         i->code = code;
3131         i->status = status;
3132
3133         r = sd_bus_message_exit_container(m);
3134         if (r < 0)
3135                 return bus_log_parse_error(r);
3136
3137         return 1;
3138 }
3139
3140 typedef struct UnitStatusInfo {
3141         const char *id;
3142         const char *load_state;
3143         const char *active_state;
3144         const char *sub_state;
3145         const char *unit_file_state;
3146         const char *unit_file_preset;
3147
3148         const char *description;
3149         const char *following;
3150
3151         char **documentation;
3152
3153         const char *fragment_path;
3154         const char *source_path;
3155         const char *control_group;
3156
3157         char **dropin_paths;
3158
3159         const char *load_error;
3160         const char *result;
3161
3162         usec_t inactive_exit_timestamp;
3163         usec_t inactive_exit_timestamp_monotonic;
3164         usec_t active_enter_timestamp;
3165         usec_t active_exit_timestamp;
3166         usec_t inactive_enter_timestamp;
3167
3168         bool need_daemon_reload;
3169
3170         /* Service */
3171         pid_t main_pid;
3172         pid_t control_pid;
3173         const char *status_text;
3174         const char *pid_file;
3175         bool running:1;
3176         int status_errno;
3177
3178         usec_t start_timestamp;
3179         usec_t exit_timestamp;
3180
3181         int exit_code, exit_status;
3182
3183         usec_t condition_timestamp;
3184         bool condition_result;
3185         bool failed_condition_trigger;
3186         bool failed_condition_negate;
3187         const char *failed_condition;
3188         const char *failed_condition_parameter;
3189
3190         usec_t assert_timestamp;
3191         bool assert_result;
3192         bool failed_assert_trigger;
3193         bool failed_assert_negate;
3194         const char *failed_assert;
3195         const char *failed_assert_parameter;
3196
3197         /* Socket */
3198         unsigned n_accepted;
3199         unsigned n_connections;
3200         bool accept;
3201
3202         /* Pairs of type, path */
3203         char **listen;
3204
3205         /* Device */
3206         const char *sysfs_path;
3207
3208         /* Mount, Automount */
3209         const char *where;
3210
3211         /* Swap */
3212         const char *what;
3213
3214         LIST_HEAD(ExecStatusInfo, exec);
3215 } UnitStatusInfo;
3216
3217 static void print_status_info(
3218                 UnitStatusInfo *i,
3219                 bool *ellipsized) {
3220
3221         ExecStatusInfo *p;
3222         const char *active_on, *active_off, *on, *off, *ss;
3223         usec_t timestamp;
3224         char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
3225         char since2[FORMAT_TIMESTAMP_MAX], *s2;
3226         const char *path;
3227         int flags =
3228                 arg_all * OUTPUT_SHOW_ALL |
3229                 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
3230                 on_tty() * OUTPUT_COLOR |
3231                 !arg_quiet * OUTPUT_WARN_CUTOFF |
3232                 arg_full * OUTPUT_FULL_WIDTH;
3233         char **t, **t2;
3234
3235         assert(i);
3236
3237         /* This shows pretty information about a unit. See
3238          * print_property() for a low-level property printer */
3239
3240         if (streq_ptr(i->active_state, "failed")) {
3241                 active_on = ansi_highlight_red();
3242                 active_off = ansi_highlight_off();
3243         } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
3244                 active_on = ansi_highlight_green();
3245                 active_off = ansi_highlight_off();
3246         } else
3247                 active_on = active_off = "";
3248
3249         printf("%s%s%s %s", active_on, draw_special_char(DRAW_BLACK_CIRCLE), active_off, strna(i->id));
3250
3251         if (i->description && !streq_ptr(i->id, i->description))
3252                 printf(" - %s", i->description);
3253
3254         printf("\n");
3255
3256         if (i->following)
3257                 printf("   Follow: unit currently follows state of %s\n", i->following);
3258
3259         if (streq_ptr(i->load_state, "error")) {
3260                 on = ansi_highlight_red();
3261                 off = ansi_highlight_off();
3262         } else
3263                 on = off = "";
3264
3265         path = i->source_path ? i->source_path : i->fragment_path;
3266
3267         if (i->load_error)
3268                 printf("   Loaded: %s%s%s (Reason: %s)\n",
3269                        on, strna(i->load_state), off, i->load_error);
3270         else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset))
3271                 printf("   Loaded: %s%s%s (%s; %s; vendor preset: %s)\n",
3272                        on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset);
3273         else if (path && !isempty(i->unit_file_state))
3274                 printf("   Loaded: %s%s%s (%s; %s)\n",
3275                        on, strna(i->load_state), off, path, i->unit_file_state);
3276         else if (path)
3277                 printf("   Loaded: %s%s%s (%s)\n",
3278                        on, strna(i->load_state), off, path);
3279         else
3280                 printf("   Loaded: %s%s%s\n",
3281                        on, strna(i->load_state), off);
3282
3283         if (!strv_isempty(i->dropin_paths)) {
3284                 _cleanup_free_ char *dir = NULL;
3285                 bool last = false;
3286                 char ** dropin;
3287
3288                 STRV_FOREACH(dropin, i->dropin_paths) {
3289                         if (! dir || last) {
3290                                 printf(dir ? "        " : "  Drop-In: ");
3291
3292                                 free(dir);
3293                                 dir = NULL;
3294
3295                                 if (path_get_parent(*dropin, &dir) < 0) {
3296                                         log_oom();
3297                                         return;
3298                                 }
3299
3300                                 printf("%s\n           %s", dir,
3301                                        draw_special_char(DRAW_TREE_RIGHT));
3302                         }
3303
3304                         last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir));
3305
3306                         printf("%s%s", basename(*dropin), last ? "\n" : ", ");
3307                 }
3308         }
3309
3310         ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
3311         if (ss)
3312                 printf("   Active: %s%s (%s)%s",
3313                        active_on, strna(i->active_state), ss, active_off);
3314         else
3315                 printf("   Active: %s%s%s",
3316                        active_on, strna(i->active_state), active_off);
3317
3318         if (!isempty(i->result) && !streq(i->result, "success"))
3319                 printf(" (Result: %s)", i->result);
3320
3321         timestamp = (streq_ptr(i->active_state, "active")      ||
3322                      streq_ptr(i->active_state, "reloading"))   ? i->active_enter_timestamp :
3323                     (streq_ptr(i->active_state, "inactive")    ||
3324                      streq_ptr(i->active_state, "failed"))      ? i->inactive_enter_timestamp :
3325                     streq_ptr(i->active_state, "activating")    ? i->inactive_exit_timestamp :
3326                                                                   i->active_exit_timestamp;
3327
3328         s1 = format_timestamp_relative(since1, sizeof(since1), timestamp);
3329         s2 = format_timestamp(since2, sizeof(since2), timestamp);
3330
3331         if (s1)
3332                 printf(" since %s; %s\n", s2, s1);
3333         else if (s2)
3334                 printf(" since %s\n", s2);
3335         else
3336                 printf("\n");
3337
3338         if (!i->condition_result && i->condition_timestamp > 0) {
3339                 s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp);
3340                 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
3341
3342                 printf("Condition: start %scondition failed%s at %s%s%s\n",
3343                        ansi_highlight_yellow(), ansi_highlight_off(),
3344                        s2, s1 ? "; " : "", s1 ? s1 : "");
3345                 if (i->failed_condition_trigger)
3346                         printf("           none of the trigger conditions were met\n");
3347                 else if (i->failed_condition)
3348                         printf("           %s=%s%s was not met\n",
3349                                i->failed_condition,
3350                                i->failed_condition_negate ? "!" : "",
3351                                i->failed_condition_parameter);
3352         }
3353
3354         if (!i->assert_result && i->assert_timestamp > 0) {
3355                 s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp);
3356                 s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp);
3357
3358                 printf("   Assert: start %sassertion failed%s at %s%s%s\n",
3359                        ansi_highlight_red(), ansi_highlight_off(),
3360                        s2, s1 ? "; " : "", s1 ? s1 : "");
3361                 if (i->failed_assert_trigger)
3362                         printf("           none of the trigger assertions were met\n");
3363                 else if (i->failed_assert)
3364                         printf("           %s=%s%s was not met\n",
3365                                i->failed_assert,
3366                                i->failed_assert_negate ? "!" : "",
3367                                i->failed_assert_parameter);
3368         }
3369
3370         if (i->sysfs_path)
3371                 printf("   Device: %s\n", i->sysfs_path);
3372         if (i->where)
3373                 printf("    Where: %s\n", i->where);
3374         if (i->what)
3375                 printf("     What: %s\n", i->what);
3376
3377         STRV_FOREACH(t, i->documentation)
3378                 printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", *t);
3379
3380         STRV_FOREACH_PAIR(t, t2, i->listen)
3381                 printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t);
3382
3383         if (i->accept)
3384                 printf(" Accepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
3385
3386         LIST_FOREACH(exec, p, i->exec) {
3387                 _cleanup_free_ char *argv = NULL;
3388                 bool good;
3389
3390                 /* Only show exited processes here */
3391                 if (p->code == 0)
3392                         continue;
3393
3394                 argv = strv_join(p->argv, " ");
3395                 printf("  Process: %u %s=%s ", p->pid, p->name, strna(argv));
3396
3397                 good = is_clean_exit_lsb(p->code, p->status, NULL);
3398                 if (!good) {
3399                         on = ansi_highlight_red();
3400                         off = ansi_highlight_off();
3401                 } else
3402                         on = off = "";
3403
3404                 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
3405
3406                 if (p->code == CLD_EXITED) {
3407                         const char *c;
3408
3409                         printf("status=%i", p->status);
3410
3411                         c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD);
3412                         if (c)
3413                                 printf("/%s", c);
3414
3415                 } else
3416                         printf("signal=%s", signal_to_string(p->status));
3417
3418                 printf(")%s\n", off);
3419
3420                 if (i->main_pid == p->pid &&
3421                     i->start_timestamp == p->start_timestamp &&
3422                     i->exit_timestamp == p->start_timestamp)
3423                         /* Let's not show this twice */
3424                         i->main_pid = 0;
3425
3426                 if (p->pid == i->control_pid)
3427                         i->control_pid = 0;
3428         }
3429
3430         if (i->main_pid > 0 || i->control_pid > 0) {
3431                 if (i->main_pid > 0) {
3432                         printf(" Main PID: "PID_FMT, i->main_pid);
3433
3434                         if (i->running) {
3435                                 _cleanup_free_ char *comm = NULL;
3436                                 get_process_comm(i->main_pid, &comm);
3437                                 if (comm)
3438                                         printf(" (%s)", comm);
3439                         } else if (i->exit_code > 0) {
3440                                 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
3441
3442                                 if (i->exit_code == CLD_EXITED) {
3443                                         const char *c;
3444
3445                                         printf("status=%i", i->exit_status);
3446
3447                                         c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD);
3448                                         if (c)
3449                                                 printf("/%s", c);
3450
3451                                 } else
3452                                         printf("signal=%s", signal_to_string(i->exit_status));
3453                                 printf(")");
3454                         }
3455
3456                         if (i->control_pid > 0)
3457                                 printf(";");
3458                 }
3459
3460                 if (i->control_pid > 0) {
3461                         _cleanup_free_ char *c = NULL;
3462
3463                         printf(" %8s: "PID_FMT, i->main_pid ? "" : " Control", i->control_pid);
3464
3465                         get_process_comm(i->control_pid, &c);
3466                         if (c)
3467                                 printf(" (%s)", c);
3468                 }
3469
3470                 printf("\n");
3471         }
3472
3473         if (i->status_text)
3474                 printf("   Status: \"%s\"\n", i->status_text);
3475         if (i->status_errno > 0)
3476                 printf("    Error: %i (%s)\n", i->status_errno, strerror(i->status_errno));
3477
3478         if (i->control_group &&
3479             (i->main_pid > 0 || i->control_pid > 0 ||
3480              ((arg_transport != BUS_TRANSPORT_LOCAL && arg_transport != BUS_TRANSPORT_MACHINE) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0))) {
3481                 unsigned c;
3482
3483                 printf("   CGroup: %s\n", i->control_group);
3484
3485                 if (arg_transport == BUS_TRANSPORT_LOCAL || arg_transport == BUS_TRANSPORT_MACHINE) {
3486                         unsigned k = 0;
3487                         pid_t extra[2];
3488                         static const char prefix[] = "           ";
3489
3490                         c = columns();
3491                         if (c > sizeof(prefix) - 1)
3492                                 c -= sizeof(prefix) - 1;
3493                         else
3494                                 c = 0;
3495
3496                         if (i->main_pid > 0)
3497                                 extra[k++] = i->main_pid;
3498
3499                         if (i->control_pid > 0)
3500                                 extra[k++] = i->control_pid;
3501
3502                         show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, false, extra, k, flags);
3503                 }
3504         }
3505
3506         if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) {
3507                 show_journal_by_unit(stdout,
3508                                      i->id,
3509                                      arg_output,
3510                                      0,
3511                                      i->inactive_exit_timestamp_monotonic,
3512                                      arg_lines,
3513                                      getuid(),
3514                                      flags | OUTPUT_BEGIN_NEWLINE,
3515                                      arg_scope == UNIT_FILE_SYSTEM,
3516                                      ellipsized);
3517         }
3518
3519         if (i->need_daemon_reload)
3520                 warn_unit_file_changed(i->id);
3521 }
3522
3523 static void show_unit_help(UnitStatusInfo *i) {
3524         char **p;
3525
3526         assert(i);
3527
3528         if (!i->documentation) {
3529                 log_info("Documentation for %s not known.", i->id);
3530                 return;
3531         }
3532
3533         STRV_FOREACH(p, i->documentation)
3534                 if (startswith(*p, "man:"))
3535                         show_man_page(*p + 4, false);
3536                 else
3537                         log_info("Can't show: %s", *p);
3538 }
3539
3540 static int status_property(const char *name, sd_bus_message *m, UnitStatusInfo *i, const char *contents) {
3541         int r;
3542
3543         assert(name);
3544         assert(m);
3545         assert(i);
3546
3547         switch (contents[0]) {
3548
3549         case SD_BUS_TYPE_STRING: {
3550                 const char *s;
3551
3552                 r = sd_bus_message_read(m, "s", &s);
3553                 if (r < 0)
3554                         return bus_log_parse_error(r);
3555
3556                 if (!isempty(s)) {
3557                         if (streq(name, "Id"))
3558                                 i->id = s;
3559                         else if (streq(name, "LoadState"))
3560                                 i->load_state = s;
3561                         else if (streq(name, "ActiveState"))
3562                                 i->active_state = s;
3563                         else if (streq(name, "SubState"))
3564                                 i->sub_state = s;
3565                         else if (streq(name, "Description"))
3566                                 i->description = s;
3567                         else if (streq(name, "FragmentPath"))
3568                                 i->fragment_path = s;
3569                         else if (streq(name, "SourcePath"))
3570                                 i->source_path = s;
3571 #ifndef NOLEGACY
3572                         else if (streq(name, "DefaultControlGroup")) {
3573                                 const char *e;
3574                                 e = startswith(s, SYSTEMD_CGROUP_CONTROLLER ":");
3575                                 if (e)
3576                                         i->control_group = e;
3577                         }
3578 #endif
3579                         else if (streq(name, "ControlGroup"))
3580                                 i->control_group = s;
3581                         else if (streq(name, "StatusText"))
3582                                 i->status_text = s;
3583                         else if (streq(name, "PIDFile"))
3584                                 i->pid_file = s;
3585                         else if (streq(name, "SysFSPath"))
3586                                 i->sysfs_path = s;
3587                         else if (streq(name, "Where"))
3588                                 i->where = s;
3589                         else if (streq(name, "What"))
3590                                 i->what = s;
3591                         else if (streq(name, "Following"))
3592                                 i->following = s;
3593                         else if (streq(name, "UnitFileState"))
3594                                 i->unit_file_state = s;
3595                         else if (streq(name, "UnitFilePreset"))
3596                                 i->unit_file_preset = s;
3597                         else if (streq(name, "Result"))
3598                                 i->result = s;
3599                 }
3600
3601                 break;
3602         }
3603
3604         case SD_BUS_TYPE_BOOLEAN: {
3605                 int b;
3606
3607                 r = sd_bus_message_read(m, "b", &b);
3608                 if (r < 0)
3609                         return bus_log_parse_error(r);
3610
3611                 if (streq(name, "Accept"))
3612                        &n