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