]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl.c
units: fix rescue.service race with plymouth
[thirdparty/systemd.git] / src / systemctl.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
7e4249b9
LP
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
e4b61340 22#include <sys/reboot.h>
7e4249b9
LP
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>
eb22ac37 31#include <fcntl.h>
f1c5860b 32#include <sys/socket.h>
ee5762e3 33#include <sys/stat.h>
0e098b15 34#include <stddef.h>
501fc174 35#include <sys/prctl.h>
7e4249b9
LP
36#include <dbus/dbus.h>
37
38#include "log.h"
39#include "util.h"
40#include "macro.h"
41#include "set.h"
e4b61340 42#include "utmp-wtmp.h"
514f4ef5 43#include "special.h"
eb22ac37 44#include "initreq.h"
e4a9373f 45#include "strv.h"
9a1ac7b9 46#include "dbus-common.h"
ab35fb1b 47#include "cgroup-show.h"
c6c18be3 48#include "cgroup-util.h"
582a507f 49#include "list.h"
ee5762e3
LP
50#include "path-lookup.h"
51#include "conf-parser.h"
52#include "sd-daemon.h"
f6144808 53#include "shutdownd.h"
d06dacd0 54#include "exit-status.h"
22f4096c 55#include "bus-errors.h"
7d568925 56#include "build.h"
71fad675 57#include "unit-name.h"
1968a360 58#include "pager.h"
729e3769
LP
59#include "spawn-agent.h"
60#include "install.h"
7e4249b9
LP
61
62static const char *arg_type = NULL;
ea4a240d 63static char **arg_property = NULL;
7e4249b9 64static bool arg_all = false;
e67c3609 65static const char *arg_job_mode = "replace";
729e3769 66static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
e4b61340 67static bool arg_immediate = false;
ee5762e3 68static bool arg_no_block = false;
0736af98 69static bool arg_no_pager = false;
e4b61340
LP
70static bool arg_no_wtmp = false;
71static bool arg_no_sync = false;
514f4ef5 72static bool arg_no_wall = false;
ee5762e3 73static bool arg_no_reload = false;
e4b61340 74static bool arg_dry = false;
0183528f 75static bool arg_quiet = false;
ee5762e3
LP
76static bool arg_full = false;
77static bool arg_force = false;
501fc174 78static bool arg_ask_password = false;
30732560 79static bool arg_failed = false;
729e3769 80static bool arg_runtime = false;
e4b61340 81static char **arg_wall = NULL;
8a0867d6
LP
82static const char *arg_kill_who = NULL;
83static const char *arg_kill_mode = NULL;
84static int arg_signal = SIGTERM;
69fc152f 85static const char *arg_root = NULL;
f6144808 86static usec_t arg_when = 0;
4445a875 87static enum action {
e4b61340
LP
88 ACTION_INVALID,
89 ACTION_SYSTEMCTL,
90 ACTION_HALT,
91 ACTION_POWEROFF,
92 ACTION_REBOOT,
20b09ca7
LP
93 ACTION_KEXEC,
94 ACTION_EXIT,
e4b61340
LP
95 ACTION_RUNLEVEL2,
96 ACTION_RUNLEVEL3,
97 ACTION_RUNLEVEL4,
98 ACTION_RUNLEVEL5,
99 ACTION_RESCUE,
514f4ef5
LP
100 ACTION_EMERGENCY,
101 ACTION_DEFAULT,
e4b61340
LP
102 ACTION_RELOAD,
103 ACTION_REEXEC,
104 ACTION_RUNLEVEL,
f6144808 105 ACTION_CANCEL_SHUTDOWN,
e4b61340
LP
106 _ACTION_MAX
107} arg_action = ACTION_SYSTEMCTL;
4445a875
LP
108static enum dot {
109 DOT_ALL,
110 DOT_ORDER,
111 DOT_REQUIRE
112} arg_dot = DOT_ALL;
a8f11321
LP
113static enum transport {
114 TRANSPORT_NORMAL,
115 TRANSPORT_SSH,
116 TRANSPORT_POLKIT
117} arg_transport = TRANSPORT_NORMAL;
118static const char *arg_host = NULL;
e4b61340 119
f4579ce7
LP
120static bool private_bus = false;
121
729e3769 122static int daemon_reload(DBusConnection *bus, char **args);
1968a360 123
2ee68f72 124static bool on_tty(void) {
2cc59dbf
LP
125 static int t = -1;
126
75d12d57
LP
127 /* Note that this is invoked relatively early, before we start
128 * the pager. That means the value we return reflects whether
129 * we originally were started on a tty, not if we currently
060ed82e 130 * are. But this is intended, since we want colour and so on
75d12d57
LP
131 * when run in our own pager. */
132
2cc59dbf
LP
133 if (_unlikely_(t < 0))
134 t = isatty(STDOUT_FILENO) > 0;
135
2ee68f72
LP
136 return t;
137}
138
3b0727f5
LP
139static void pager_open_if_enabled(void) {
140 on_tty();
141
729e3769
LP
142 if (arg_no_pager)
143 return;
3b0727f5 144
729e3769
LP
145 pager_open();
146}
c0f9c7da 147
729e3769 148static void agent_open_if_enabled(void) {
501fc174 149
729e3769 150 /* Open the password agent as a child process if necessary */
501fc174
LP
151
152 if (!arg_ask_password)
153 return;
715554e7 154
729e3769 155 if (arg_scope != UNIT_FILE_SYSTEM)
501fc174
LP
156 return;
157
729e3769 158 agent_open();
501fc174
LP
159}
160
2ee68f72
LP
161static const char *ansi_highlight(bool b) {
162
163 if (!on_tty())
2cc59dbf
LP
164 return "";
165
166 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
167}
168
2ee68f72
LP
169static const char *ansi_highlight_green(bool b) {
170
171 if (!on_tty())
172 return "";
173
174 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
175}
176
f73e33d9 177static bool error_is_no_service(const DBusError *error) {
514f4ef5
LP
178 assert(error);
179
e4b61340
LP
180 if (!dbus_error_is_set(error))
181 return false;
182
183 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
184 return true;
185
186 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
187 return true;
188
189 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
190}
7e4249b9 191
22f4096c
LP
192static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
193 assert(error);
194
195 if (!dbus_error_is_set(error))
196 return r;
197
198 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
199 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
200 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
201 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
202 return EXIT_NOPERMISSION;
203
204 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
205 return EXIT_NOTINSTALLED;
206
207 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
208 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
209 return EXIT_NOTIMPLEMENTED;
210
211 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
212 return EXIT_NOTCONFIGURED;
213
214 if (r != 0)
215 return r;
216
217 return EXIT_FAILURE;
218}
219
514f4ef5 220static void warn_wall(enum action action) {
ef2f1067 221 static const char *table[_ACTION_MAX] = {
514f4ef5
LP
222 [ACTION_HALT] = "The system is going down for system halt NOW!",
223 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
224 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
20b09ca7 225 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
514f4ef5
LP
226 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
227 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
ef2f1067
LP
228 };
229
514f4ef5
LP
230 if (arg_no_wall)
231 return;
232
e4a9373f
LP
233 if (arg_wall) {
234 char *p;
235
236 if (!(p = strv_join(arg_wall, " "))) {
237 log_error("Failed to join strings.");
238 return;
239 }
240
241 if (*p) {
7af53310 242 utmp_wall(p, NULL);
e4a9373f
LP
243 free(p);
244 return;
245 }
246
247 free(p);
248 }
249
514f4ef5 250 if (!table[action])
ef2f1067
LP
251 return;
252
7af53310 253 utmp_wall(table[action], NULL);
ef2f1067
LP
254}
255
729e3769
LP
256static bool avoid_bus(void) {
257
258 if (running_in_chroot() > 0)
259 return true;
260
261 if (sd_booted() <= 0)
262 return true;
263
264 if (!isempty(arg_root))
265 return true;
266
267 if (arg_scope == UNIT_FILE_GLOBAL)
268 return true;
269
270 return false;
271}
272
36c32ba2
LP
273struct unit_info {
274 const char *id;
275 const char *description;
276 const char *load_state;
277 const char *active_state;
278 const char *sub_state;
279 const char *following;
280 const char *unit_path;
281 uint32_t job_id;
282 const char *job_type;
283 const char *job_path;
284};
285
286static int compare_unit_info(const void *a, const void *b) {
287 const char *d1, *d2;
288 const struct unit_info *u = a, *v = b;
289
290 d1 = strrchr(u->id, '.');
291 d2 = strrchr(v->id, '.');
292
293 if (d1 && d2) {
294 int r;
295
a2a3a5b9 296 if ((r = strcasecmp(d1, d2)) != 0)
36c32ba2
LP
297 return r;
298 }
299
a2a3a5b9 300 return strcasecmp(u->id, v->id);
36c32ba2
LP
301}
302
30732560 303static bool output_show_unit(const struct unit_info *u) {
33330222 304 const char *dot;
b036fc00 305
30732560
LP
306 if (arg_failed)
307 return streq(u->active_state, "failed");
308
33330222
ZJS
309 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
310 streq(dot+1, arg_type))) &&
311 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
312}
313
eb68c413 314static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
688c6725 315 unsigned active_len, sub_len, job_len, n_shown = 0;
b036fc00 316 const struct unit_info *u;
33330222 317
b036fc00
LP
318 active_len = sizeof("ACTIVE")-1;
319 sub_len = sizeof("SUB")-1;
320 job_len = sizeof("JOB")-1;
321
322 for (u = unit_infos; u < unit_infos + c; u++) {
30732560 323 if (!output_show_unit(u))
b036fc00
LP
324 continue;
325
326 active_len = MAX(active_len, strlen(u->active_state));
327 sub_len = MAX(sub_len, strlen(u->sub_state));
328 if (u->job_id != 0)
329 job_len = MAX(job_len, strlen(u->job_type));
33330222
ZJS
330 }
331
eb68c413 332 if (on_tty()) {
33330222
ZJS
333 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
334 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
964d124e 335 if (columns() >= 80+12 || arg_full || !arg_no_pager)
33330222 336 printf(" %s\n", "DESCRIPTION");
eb68c413 337 else
33330222 338 printf("\n");
eb68c413
ZJS
339 }
340
b036fc00
LP
341 for (u = unit_infos; u < unit_infos + c; u++) {
342 char *e;
343 int a = 0, b = 0;
344 const char *on_loaded, *off_loaded;
345 const char *on_active, *off_active;
346
30732560 347 if (!output_show_unit(u))
b036fc00
LP
348 continue;
349
688c6725
LP
350 n_shown++;
351
00dc5d76
LP
352 if (!streq(u->load_state, "loaded") &&
353 !streq(u->load_state, "banned")) {
b036fc00
LP
354 on_loaded = ansi_highlight(true);
355 off_loaded = ansi_highlight(false);
356 } else
357 on_loaded = off_loaded = "";
358
359 if (streq(u->active_state, "failed")) {
360 on_active = ansi_highlight(true);
361 off_active = ansi_highlight(false);
362 } else
363 on_active = off_active = "";
eb68c413 364
b036fc00 365 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
eb68c413 366
b036fc00
LP
367 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
368 e ? e : u->id,
369 on_loaded, u->load_state, off_loaded,
370 on_active, active_len, u->active_state,
371 sub_len, u->sub_state, off_active,
372 &a);
eb68c413 373
b036fc00
LP
374 free(e);
375
376 a -= strlen(on_loaded) + strlen(off_loaded);
377 a -= strlen(on_active) + strlen(off_active);
378
379 if (u->job_id != 0)
380 printf(" %-*s", job_len, u->job_type);
381 else
382 b = 1 + job_len;
383
384 if (a + b + 1 < columns()) {
385 if (u->job_id == 0)
386 printf(" %-*s", job_len, "");
387
964d124e 388 if (arg_full || !arg_no_pager)
b036fc00
LP
389 printf(" %s", u->description);
390 else
391 printf(" %.*s", columns() - a - b - 1, u->description);
eb68c413 392 }
b036fc00
LP
393
394 fputs("\n", stdout);
eb68c413
ZJS
395 }
396
9b2fcafb 397 if (on_tty()) {
eb68c413
ZJS
398 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
399 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
400 "SUB = The low-level unit activation state, values depend on unit type.\n"
401 "JOB = Pending job for the unit.\n");
402
403 if (arg_all)
688c6725 404 printf("\n%u units listed.\n", n_shown);
eb68c413 405 else
688c6725 406 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
eb68c413
ZJS
407 }
408}
409
729e3769 410static int list_units(DBusConnection *bus, char **args) {
7e4249b9
LP
411 DBusMessage *m = NULL, *reply = NULL;
412 DBusError error;
413 int r;
414 DBusMessageIter iter, sub, sub2;
eb68c413 415 unsigned c = 0, n_units = 0;
36c32ba2 416 struct unit_info *unit_infos = NULL;
7e4249b9
LP
417
418 dbus_error_init(&error);
419
514f4ef5
LP
420 assert(bus);
421
1968a360 422 pager_open_if_enabled();
ec14911e 423
7e4249b9
LP
424 if (!(m = dbus_message_new_method_call(
425 "org.freedesktop.systemd1",
426 "/org/freedesktop/systemd1",
427 "org.freedesktop.systemd1.Manager",
428 "ListUnits"))) {
429 log_error("Could not allocate message.");
430 return -ENOMEM;
431 }
432
433 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 434 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
435 r = -EIO;
436 goto finish;
437 }
438
439 if (!dbus_message_iter_init(reply, &iter) ||
440 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
441 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
442 log_error("Failed to parse reply.");
443 r = -EIO;
444 goto finish;
445 }
446
447 dbus_message_iter_recurse(&iter, &sub);
448
7e4249b9 449 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
36c32ba2 450 struct unit_info *u;
7e4249b9
LP
451
452 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
453 log_error("Failed to parse reply.");
454 r = -EIO;
455 goto finish;
456 }
457
36c32ba2
LP
458 if (c >= n_units) {
459 struct unit_info *w;
460
461 n_units = MAX(2*c, 16);
462 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
463
464 if (!w) {
465 log_error("Failed to allocate unit array.");
466 r = -ENOMEM;
467 goto finish;
468 }
469
470 unit_infos = w;
471 }
472
473 u = unit_infos+c;
474
7e4249b9
LP
475 dbus_message_iter_recurse(&sub, &sub2);
476
36c32ba2
LP
477 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
478 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
479 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
486 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
7e4249b9
LP
487 log_error("Failed to parse reply.");
488 r = -EIO;
489 goto finish;
490 }
491
36c32ba2
LP
492 dbus_message_iter_next(&sub);
493 c++;
494 }
495
bd40a2d8
LP
496 if (c > 0) {
497 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
498 output_units_list(unit_infos, c);
499 }
4445a875
LP
500
501 r = 0;
502
503finish:
504 if (m)
505 dbus_message_unref(m);
506
507 if (reply)
508 dbus_message_unref(reply);
509
36c32ba2
LP
510 free(unit_infos);
511
4445a875
LP
512 dbus_error_free(&error);
513
514 return r;
515}
516
729e3769
LP
517static int compare_unit_file_list(const void *a, const void *b) {
518 const char *d1, *d2;
519 const UnitFileList *u = a, *v = b;
520
521 d1 = strrchr(u->path, '.');
522 d2 = strrchr(v->path, '.');
523
524 if (d1 && d2) {
525 int r;
526
527 r = strcasecmp(d1, d2);
528 if (r != 0)
529 return r;
530 }
531
532 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
533}
534
535static bool output_show_unit_file(const UnitFileList *u) {
536 const char *dot;
537
538 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
539}
540
541static void output_unit_file_list(const UnitFileList *units, unsigned c) {
542 unsigned n_shown = 0;
543 const UnitFileList *u;
544
545 if (on_tty())
546 printf("%-25s %-6s\n", "UNIT FILE", "STATE");
547
548 for (u = units; u < units + c; u++) {
549 char *e;
550 const char *on, *off;
551 const char *id;
552
553 if (!output_show_unit_file(u))
554 continue;
555
556 n_shown++;
557
558 if (u->state == UNIT_FILE_MASKED ||
559 u->state == UNIT_FILE_MASKED_RUNTIME ||
560 u->state == UNIT_FILE_DISABLED) {
561 on = ansi_highlight(true);
562 off = ansi_highlight(false);
563 } else if (u->state == UNIT_FILE_ENABLED) {
564 on = ansi_highlight_green(true);
565 off = ansi_highlight_green(false);
566 } else
567 on = off = "";
568
569 id = file_name_from_path(u->path);
570
571 e = arg_full ? NULL : ellipsize(id, 25, 33);
572
573 printf("%-25s %s%-6s%s\n",
574 e ? e : id,
575 on, unit_file_state_to_string(u->state), off);
576
577 free(e);
578 }
579
580 if (on_tty())
581 printf("\n%u unit files listed.\n", n_shown);
582}
583
584static int list_unit_files(DBusConnection *bus, char **args) {
585 DBusMessage *m = NULL, *reply = NULL;
586 DBusError error;
587 int r;
588 DBusMessageIter iter, sub, sub2;
589 unsigned c = 0, n_units = 0;
590 UnitFileList *units = NULL;
591
592 dbus_error_init(&error);
593
594 assert(bus);
595
596 pager_open_if_enabled();
597
598 if (avoid_bus()) {
599 Hashmap *h;
600 UnitFileList *u;
601 Iterator i;
602
603 h = hashmap_new(string_hash_func, string_compare_func);
604 if (!h) {
605 log_error("Out of memory");
606 return -ENOMEM;
607 }
608
609 r = unit_file_get_list(arg_scope, arg_root, h);
610 if (r < 0) {
611 log_error("Failed to get unit file list: %s", strerror(-r));
612 return r;
613 }
614
615 n_units = hashmap_size(h);
616 units = new(UnitFileList, n_units);
617 if (!units) {
618 unit_file_list_free(h);
619 log_error("Out of memory");
620 return -ENOMEM;
621 }
622
623 HASHMAP_FOREACH(u, h, i) {
624 memcpy(units + c++, u, sizeof(UnitFileList));
625 free(u);
626 }
627
628 hashmap_free(h);
629 } else {
630 m = dbus_message_new_method_call(
631 "org.freedesktop.systemd1",
632 "/org/freedesktop/systemd1",
633 "org.freedesktop.systemd1.Manager",
634 "ListUnitFiles");
635 if (!m) {
636 log_error("Could not allocate message.");
637 return -ENOMEM;
638 }
639
640 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
641 if (!reply) {
642 log_error("Failed to issue method call: %s", bus_error_message(&error));
643 r = -EIO;
644 goto finish;
645 }
646
647 if (!dbus_message_iter_init(reply, &iter) ||
648 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
649 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
650 log_error("Failed to parse reply.");
651 r = -EIO;
652 goto finish;
653 }
654
655 dbus_message_iter_recurse(&iter, &sub);
656
657 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
658 UnitFileList *u;
659 const char *state;
660
661 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
662 log_error("Failed to parse reply.");
663 r = -EIO;
664 goto finish;
665 }
666
667 if (c >= n_units) {
668 UnitFileList *w;
669
670 n_units = MAX(2*c, 16);
671 w = realloc(units, sizeof(struct UnitFileList) * n_units);
672
673 if (!w) {
674 log_error("Failed to allocate unit array.");
675 r = -ENOMEM;
676 goto finish;
677 }
678
679 units = w;
680 }
681
682 u = units+c;
683
684 dbus_message_iter_recurse(&sub, &sub2);
685
686 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
687 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
688 log_error("Failed to parse reply.");
689 r = -EIO;
690 goto finish;
691 }
692
693 u->state = unit_file_state_from_string(state);
694
695 dbus_message_iter_next(&sub);
696 c++;
697 }
698 }
699
700 if (c > 0) {
701 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
702 output_unit_file_list(units, c);
703 }
704
705 r = 0;
706
707finish:
708 if (m)
709 dbus_message_unref(m);
710
711 if (reply)
712 dbus_message_unref(reply);
713
714 free(units);
715
716 dbus_error_free(&error);
717
718 return r;
719}
720
4445a875
LP
721static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
722 static const char * const colors[] = {
723 "Requires", "[color=\"black\"]",
724 "RequiresOverridable", "[color=\"black\"]",
725 "Requisite", "[color=\"darkblue\"]",
726 "RequisiteOverridable", "[color=\"darkblue\"]",
727 "Wants", "[color=\"darkgrey\"]",
728 "Conflicts", "[color=\"red\"]",
69dd2852 729 "ConflictedBy", "[color=\"red\"]",
4445a875
LP
730 "After", "[color=\"green\"]"
731 };
732
733 const char *c = NULL;
734 unsigned i;
735
736 assert(name);
737 assert(prop);
738 assert(iter);
739
740 for (i = 0; i < ELEMENTSOF(colors); i += 2)
741 if (streq(colors[i], prop)) {
742 c = colors[i+1];
743 break;
744 }
745
746 if (!c)
747 return 0;
748
749 if (arg_dot != DOT_ALL)
750 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
751 return 0;
752
753 switch (dbus_message_iter_get_arg_type(iter)) {
754
755 case DBUS_TYPE_ARRAY:
756
757 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
758 DBusMessageIter sub;
759
760 dbus_message_iter_recurse(iter, &sub);
761
762 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
763 const char *s;
764
765 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
766 dbus_message_iter_get_basic(&sub, &s);
767 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
768
769 dbus_message_iter_next(&sub);
770 }
771
772 return 0;
773 }
774 }
775
776 return 0;
777}
778
779static int dot_one(DBusConnection *bus, const char *name, const char *path) {
780 DBusMessage *m = NULL, *reply = NULL;
781 const char *interface = "org.freedesktop.systemd1.Unit";
782 int r;
783 DBusError error;
784 DBusMessageIter iter, sub, sub2, sub3;
785
786 assert(bus);
787 assert(path);
788
789 dbus_error_init(&error);
790
791 if (!(m = dbus_message_new_method_call(
792 "org.freedesktop.systemd1",
793 path,
794 "org.freedesktop.DBus.Properties",
795 "GetAll"))) {
796 log_error("Could not allocate message.");
797 r = -ENOMEM;
798 goto finish;
799 }
800
801 if (!dbus_message_append_args(m,
802 DBUS_TYPE_STRING, &interface,
803 DBUS_TYPE_INVALID)) {
804 log_error("Could not append arguments to message.");
805 r = -ENOMEM;
806 goto finish;
807 }
808
809 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 810 log_error("Failed to issue method call: %s", bus_error_message(&error));
4445a875
LP
811 r = -EIO;
812 goto finish;
813 }
814
815 if (!dbus_message_iter_init(reply, &iter) ||
816 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
817 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
818 log_error("Failed to parse reply.");
819 r = -EIO;
820 goto finish;
821 }
822
823 dbus_message_iter_recurse(&iter, &sub);
824
825 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
826 const char *prop;
827
828 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
829 log_error("Failed to parse reply.");
830 r = -EIO;
831 goto finish;
832 }
833
834 dbus_message_iter_recurse(&sub, &sub2);
835
836 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
837 log_error("Failed to parse reply.");
838 r = -EIO;
839 goto finish;
840 }
841
842 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
843 log_error("Failed to parse reply.");
844 r = -EIO;
845 goto finish;
846 }
847
848 dbus_message_iter_recurse(&sub2, &sub3);
849
850 if (dot_one_property(name, prop, &sub3)) {
851 log_error("Failed to parse reply.");
852 r = -EIO;
853 goto finish;
854 }
855
856 dbus_message_iter_next(&sub);
857 }
858
e364ad06
LP
859 r = 0;
860
4445a875
LP
861finish:
862 if (m)
863 dbus_message_unref(m);
864
865 if (reply)
866 dbus_message_unref(reply);
867
868 dbus_error_free(&error);
869
870 return r;
871}
872
729e3769 873static int dot(DBusConnection *bus, char **args) {
4445a875
LP
874 DBusMessage *m = NULL, *reply = NULL;
875 DBusError error;
876 int r;
877 DBusMessageIter iter, sub, sub2;
878
879 dbus_error_init(&error);
880
881 assert(bus);
882
883 if (!(m = dbus_message_new_method_call(
884 "org.freedesktop.systemd1",
885 "/org/freedesktop/systemd1",
886 "org.freedesktop.systemd1.Manager",
887 "ListUnits"))) {
888 log_error("Could not allocate message.");
889 return -ENOMEM;
890 }
891
892 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 893 log_error("Failed to issue method call: %s", bus_error_message(&error));
4445a875
LP
894 r = -EIO;
895 goto finish;
896 }
897
898 if (!dbus_message_iter_init(reply, &iter) ||
899 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
900 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
901 log_error("Failed to parse reply.");
902 r = -EIO;
903 goto finish;
904 }
905
906 printf("digraph systemd {\n");
907
908 dbus_message_iter_recurse(&iter, &sub);
909 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
4a4d6b4b 910 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
4445a875
LP
911
912 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
913 log_error("Failed to parse reply.");
914 r = -EIO;
915 goto finish;
916 }
917
918 dbus_message_iter_recurse(&sub, &sub2);
919
920 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
921 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
922 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
923 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
924 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
4a4d6b4b 925 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
4445a875
LP
926 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
927 log_error("Failed to parse reply.");
928 r = -EIO;
929 goto finish;
930 }
931
932 if ((r = dot_one(bus, id, unit_path)) < 0)
933 goto finish;
934
935 /* printf("\t\"%s\";\n", id); */
936 dbus_message_iter_next(&sub);
937 }
938
939 printf("}\n");
940
941 log_info(" Color legend: black = Requires\n"
942 " dark blue = Requisite\n"
943 " dark grey = Wants\n"
944 " red = Conflicts\n"
945 " green = After\n");
946
ef3a24de 947 if (on_tty())
4445a875
LP
948 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
949 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
7e4249b9
LP
950
951 r = 0;
952
953finish:
954 if (m)
955 dbus_message_unref(m);
956
957 if (reply)
958 dbus_message_unref(reply);
959
960 dbus_error_free(&error);
961
962 return r;
963}
964
729e3769 965static int list_jobs(DBusConnection *bus, char **args) {
7e4249b9
LP
966 DBusMessage *m = NULL, *reply = NULL;
967 DBusError error;
968 int r;
969 DBusMessageIter iter, sub, sub2;
970 unsigned k = 0;
971
972 dbus_error_init(&error);
973
514f4ef5
LP
974 assert(bus);
975
1968a360 976 pager_open_if_enabled();
ec14911e 977
7e4249b9
LP
978 if (!(m = dbus_message_new_method_call(
979 "org.freedesktop.systemd1",
980 "/org/freedesktop/systemd1",
981 "org.freedesktop.systemd1.Manager",
982 "ListJobs"))) {
983 log_error("Could not allocate message.");
984 return -ENOMEM;
985 }
986
987 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 988 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
989 r = -EIO;
990 goto finish;
991 }
992
993 if (!dbus_message_iter_init(reply, &iter) ||
994 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
995 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
996 log_error("Failed to parse reply.");
997 r = -EIO;
998 goto finish;
999 }
1000
1001 dbus_message_iter_recurse(&iter, &sub);
1002
ef3a24de 1003 if (on_tty())
f73e33d9 1004 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
7e4249b9
LP
1005
1006 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1007 const char *name, *type, *state, *job_path, *unit_path;
1008 uint32_t id;
8fe914ec 1009 char *e;
7e4249b9
LP
1010
1011 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1012 log_error("Failed to parse reply.");
1013 r = -EIO;
1014 goto finish;
1015 }
1016
1017 dbus_message_iter_recurse(&sub, &sub2);
1018
1019 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1020 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1021 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1022 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1023 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1024 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1025 log_error("Failed to parse reply.");
1026 r = -EIO;
1027 goto finish;
1028 }
1029
f73e33d9
LP
1030 e = arg_full ? NULL : ellipsize(name, 25, 33);
1031 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
8fe914ec
LP
1032 free(e);
1033
7e4249b9
LP
1034 k++;
1035
1036 dbus_message_iter_next(&sub);
1037 }
1038
ef3a24de 1039 if (on_tty())
f73e33d9
LP
1040 printf("\n%u jobs listed.\n", k);
1041
7e4249b9
LP
1042 r = 0;
1043
1044finish:
1045 if (m)
1046 dbus_message_unref(m);
1047
1048 if (reply)
1049 dbus_message_unref(reply);
1050
1051 dbus_error_free(&error);
1052
1053 return r;
1054}
1055
729e3769 1056static int load_unit(DBusConnection *bus, char **args) {
7e4249b9
LP
1057 DBusMessage *m = NULL, *reply = NULL;
1058 DBusError error;
1059 int r;
729e3769 1060 char **name;
7e4249b9
LP
1061
1062 dbus_error_init(&error);
1063
514f4ef5
LP
1064 assert(bus);
1065 assert(args);
1066
729e3769 1067 STRV_FOREACH(name, args+1) {
7e4249b9
LP
1068
1069 if (!(m = dbus_message_new_method_call(
1070 "org.freedesktop.systemd1",
1071 "/org/freedesktop/systemd1",
1072 "org.freedesktop.systemd1.Manager",
1073 "LoadUnit"))) {
1074 log_error("Could not allocate message.");
1075 r = -ENOMEM;
1076 goto finish;
1077 }
1078
1079 if (!dbus_message_append_args(m,
729e3769 1080 DBUS_TYPE_STRING, name,
7e4249b9
LP
1081 DBUS_TYPE_INVALID)) {
1082 log_error("Could not append arguments to message.");
1083 r = -ENOMEM;
1084 goto finish;
1085 }
1086
1087 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 1088 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
1089 r = -EIO;
1090 goto finish;
1091 }
1092
1093 dbus_message_unref(m);
1094 dbus_message_unref(reply);
1095
1096 m = reply = NULL;
1097 }
1098
1099 r = 0;
1100
1101finish:
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
729e3769 1113static int cancel_job(DBusConnection *bus, char **args) {
7e4249b9
LP
1114 DBusMessage *m = NULL, *reply = NULL;
1115 DBusError error;
1116 int r;
729e3769 1117 char **name;
7e4249b9
LP
1118
1119 dbus_error_init(&error);
1120
514f4ef5
LP
1121 assert(bus);
1122 assert(args);
1123
729e3769
LP
1124 if (strv_length(args) <= 1)
1125 return daemon_reload(bus, args);
ee5762e3 1126
729e3769 1127 STRV_FOREACH(name, args+1) {
7e4249b9
LP
1128 unsigned id;
1129 const char *path;
1130
1131 if (!(m = dbus_message_new_method_call(
1132 "org.freedesktop.systemd1",
1133 "/org/freedesktop/systemd1",
1134 "org.freedesktop.systemd1.Manager",
1135 "GetJob"))) {
1136 log_error("Could not allocate message.");
1137 r = -ENOMEM;
1138 goto finish;
1139 }
1140
729e3769 1141 if ((r = safe_atou(*name, &id)) < 0) {
7e4249b9
LP
1142 log_error("Failed to parse job id: %s", strerror(-r));
1143 goto finish;
1144 }
1145
1146 assert_cc(sizeof(uint32_t) == sizeof(id));
1147 if (!dbus_message_append_args(m,
1148 DBUS_TYPE_UINT32, &id,
1149 DBUS_TYPE_INVALID)) {
1150 log_error("Could not append arguments to message.");
1151 r = -ENOMEM;
1152 goto finish;
1153 }
1154
1155 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 1156 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
1157 r = -EIO;
1158 goto finish;
1159 }
1160
1161 if (!dbus_message_get_args(reply, &error,
1162 DBUS_TYPE_OBJECT_PATH, &path,
1163 DBUS_TYPE_INVALID)) {
4cf5d675 1164 log_error("Failed to parse reply: %s", bus_error_message(&error));
7e4249b9
LP
1165 r = -EIO;
1166 goto finish;
1167 }
1168
1169 dbus_message_unref(m);
1170 if (!(m = dbus_message_new_method_call(
1171 "org.freedesktop.systemd1",
1172 path,
1173 "org.freedesktop.systemd1.Job",
1174 "Cancel"))) {
1175 log_error("Could not allocate message.");
1176 r = -ENOMEM;
1177 goto finish;
1178 }
1179
1180 dbus_message_unref(reply);
1181 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 1182 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
1183 r = -EIO;
1184 goto finish;
1185 }
1186
1187 dbus_message_unref(m);
1188 dbus_message_unref(reply);
1189 m = reply = NULL;
1190 }
1191
1192 r = 0;
1193
1194finish:
1195 if (m)
1196 dbus_message_unref(m);
1197
1198 if (reply)
1199 dbus_message_unref(reply);
1200
1201 dbus_error_free(&error);
1202
1203 return r;
1204}
1205
ee5762e3 1206static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
e364ad06 1207 DBusMessage *m = NULL, *reply = NULL;
45fb0699
LP
1208 dbus_bool_t b = FALSE;
1209 DBusMessageIter iter, sub;
1210 const char
1211 *interface = "org.freedesktop.systemd1.Unit",
1212 *property = "NeedDaemonReload",
1213 *path;
1214
1215 /* We ignore all errors here, since this is used to show a warning only */
1216
1217 if (!(m = dbus_message_new_method_call(
1218 "org.freedesktop.systemd1",
1219 "/org/freedesktop/systemd1",
1220 "org.freedesktop.systemd1.Manager",
1221 "GetUnit")))
1222 goto finish;
1223
1224 if (!dbus_message_append_args(m,
1225 DBUS_TYPE_STRING, &unit,
1226 DBUS_TYPE_INVALID))
1227 goto finish;
1228
1229 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1230 goto finish;
1231
1232 if (!dbus_message_get_args(reply, NULL,
1233 DBUS_TYPE_OBJECT_PATH, &path,
1234 DBUS_TYPE_INVALID))
1235 goto finish;
1236
1237 dbus_message_unref(m);
1238 if (!(m = dbus_message_new_method_call(
1239 "org.freedesktop.systemd1",
1240 path,
1241 "org.freedesktop.DBus.Properties",
1242 "Get")))
1243 goto finish;
1244
1245 if (!dbus_message_append_args(m,
1246 DBUS_TYPE_STRING, &interface,
1247 DBUS_TYPE_STRING, &property,
1248 DBUS_TYPE_INVALID)) {
1249 goto finish;
1250 }
1251
1252 dbus_message_unref(reply);
1253 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1254 goto finish;
1255
1256 if (!dbus_message_iter_init(reply, &iter) ||
1257 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1258 goto finish;
1259
1260 dbus_message_iter_recurse(&iter, &sub);
1261
1262 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1263 goto finish;
1264
1265 dbus_message_iter_get_basic(&sub, &b);
1266
1267finish:
1268 if (m)
1269 dbus_message_unref(m);
1270
1271 if (reply)
1272 dbus_message_unref(reply);
1273
1274 return b;
1275}
1276
5e374895
LP
1277typedef struct WaitData {
1278 Set *set;
5d44db4a 1279 char *result;
5e374895
LP
1280} WaitData;
1281
7e4249b9
LP
1282static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1283 DBusError error;
5e374895 1284 WaitData *d = data;
7e4249b9
LP
1285
1286 assert(connection);
1287 assert(message);
5e374895 1288 assert(d);
7e4249b9
LP
1289
1290 dbus_error_init(&error);
1291
54165a39
LP
1292 log_debug("Got D-Bus request: %s.%s() on %s",
1293 dbus_message_get_interface(message),
1294 dbus_message_get_member(message),
1295 dbus_message_get_path(message));
7e4249b9
LP
1296
1297 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1298 log_error("Warning! D-Bus connection terminated.");
1299 dbus_connection_close(connection);
1300
1301 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1302 uint32_t id;
5d44db4a 1303 const char *path, *result;
5e374895 1304 dbus_bool_t success = true;
7e4249b9 1305
5d44db4a
LP
1306 if (dbus_message_get_args(message, &error,
1307 DBUS_TYPE_UINT32, &id,
1308 DBUS_TYPE_OBJECT_PATH, &path,
1309 DBUS_TYPE_STRING, &result,
1310 DBUS_TYPE_INVALID)) {
7e4249b9
LP
1311 char *p;
1312
5d44db4a
LP
1313 if ((p = set_remove(d->set, (char*) path)))
1314 free(p);
1315
1316 if (*result)
1317 d->result = strdup(result);
1318
1319 goto finish;
1320 }
1321#ifndef LEGACY
1322 dbus_error_free(&error);
1323
1324 if (dbus_message_get_args(message, &error,
1325 DBUS_TYPE_UINT32, &id,
1326 DBUS_TYPE_OBJECT_PATH, &path,
1327 DBUS_TYPE_BOOLEAN, &success,
1328 DBUS_TYPE_INVALID)) {
1329 char *p;
1330
1331 /* Compatibility with older systemd versions <
1332 * 19 during upgrades. This should be dropped
1333 * one day */
1334
5e374895 1335 if ((p = set_remove(d->set, (char*) path)))
7e4249b9 1336 free(p);
5e374895
LP
1337
1338 if (!success)
5d44db4a
LP
1339 d->result = strdup("failed");
1340
1341 goto finish;
7e4249b9 1342 }
5d44db4a
LP
1343#endif
1344
1345 log_error("Failed to parse message: %s", bus_error_message(&error));
7e4249b9
LP
1346 }
1347
5d44db4a 1348finish:
7e4249b9
LP
1349 dbus_error_free(&error);
1350 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1351}
1352
479ef5d3 1353static int enable_wait_for_jobs(DBusConnection *bus) {
7e4249b9 1354 DBusError error;
7e4249b9
LP
1355
1356 assert(bus);
7e4249b9 1357
f4579ce7
LP
1358 if (private_bus)
1359 return 0;
1360
7e4249b9 1361 dbus_error_init(&error);
7e4249b9
LP
1362 dbus_bus_add_match(bus,
1363 "type='signal',"
1364 "sender='org.freedesktop.systemd1',"
1365 "interface='org.freedesktop.systemd1.Manager',"
1366 "member='JobRemoved',"
1367 "path='/org/freedesktop/systemd1'",
1368 &error);
1369
1370 if (dbus_error_is_set(&error)) {
4cf5d675 1371 log_error("Failed to add match: %s", bus_error_message(&error));
a567261a
LP
1372 dbus_error_free(&error);
1373 return -EIO;
7e4249b9
LP
1374 }
1375
479ef5d3 1376 /* This is slightly dirty, since we don't undo the match registrations. */
a567261a 1377 return 0;
7e4249b9
LP
1378}
1379
479ef5d3
LP
1380static int wait_for_jobs(DBusConnection *bus, Set *s) {
1381 int r;
5e374895 1382 WaitData d;
479ef5d3
LP
1383
1384 assert(bus);
1385 assert(s);
1386
5e374895
LP
1387 zero(d);
1388 d.set = s;
5e374895
LP
1389
1390 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
479ef5d3
LP
1391 log_error("Failed to add filter.");
1392 r = -ENOMEM;
1393 goto finish;
1394 }
1395
1396 while (!set_isempty(s) &&
1397 dbus_connection_read_write_dispatch(bus, -1))
1398 ;
1399
5d44db4a
LP
1400 if (!arg_quiet && d.result) {
1401 if (streq(d.result, "timeout"))
1402 log_error("Job timed out.");
1403 else if (streq(d.result, "canceled"))
1404 log_error("Job canceled.");
1405 else if (streq(d.result, "dependency"))
1406 log_error("A dependency job failed. See system logs for details.");
d68201e9 1407 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
5d44db4a
LP
1408 log_error("Job failed. See system logs and 'systemctl status' for details.");
1409 }
5e374895 1410
8e20e31a
LP
1411 if (streq_ptr(d.result, "timeout"))
1412 r = -ETIME;
1413 else if (streq_ptr(d.result, "canceled"))
1414 r = -ECANCELED;
d68201e9 1415 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
8e20e31a
LP
1416 r = -EIO;
1417 else
1418 r = 0;
1419
5d44db4a 1420 free(d.result);
479ef5d3
LP
1421
1422finish:
1423 /* This is slightly dirty, since we don't undo the filter registration. */
1424
1425 return r;
1426}
1427
e4b61340
LP
1428static int start_unit_one(
1429 DBusConnection *bus,
1430 const char *method,
1431 const char *name,
1432 const char *mode,
22f4096c 1433 DBusError *error,
e4b61340 1434 Set *s) {
7e4249b9 1435
7e4249b9 1436 DBusMessage *m = NULL, *reply = NULL;
45fb0699 1437 const char *path;
7e4249b9 1438 int r;
7e4249b9 1439
e4b61340
LP
1440 assert(bus);
1441 assert(method);
1442 assert(name);
1443 assert(mode);
22f4096c 1444 assert(error);
6e905d93 1445 assert(arg_no_block || s);
7e4249b9
LP
1446
1447 if (!(m = dbus_message_new_method_call(
1448 "org.freedesktop.systemd1",
1449 "/org/freedesktop/systemd1",
1450 "org.freedesktop.systemd1.Manager",
e4b61340 1451 method))) {
7e4249b9
LP
1452 log_error("Could not allocate message.");
1453 r = -ENOMEM;
1454 goto finish;
1455 }
1456
1457 if (!dbus_message_append_args(m,
e4b61340 1458 DBUS_TYPE_STRING, &name,
7e4249b9
LP
1459 DBUS_TYPE_STRING, &mode,
1460 DBUS_TYPE_INVALID)) {
1461 log_error("Could not append arguments to message.");
1462 r = -ENOMEM;
1463 goto finish;
1464 }
1465
22f4096c 1466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
e4b61340 1467
22f4096c 1468 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
e4b61340
LP
1469 /* There's always a fallback possible for
1470 * legacy actions. */
706900b7 1471 r = -EADDRNOTAVAIL;
e4b61340
LP
1472 goto finish;
1473 }
1474
22f4096c 1475 log_error("Failed to issue method call: %s", bus_error_message(error));
7e4249b9
LP
1476 r = -EIO;
1477 goto finish;
1478 }
1479
22f4096c 1480 if (!dbus_message_get_args(reply, error,
45fb0699
LP
1481 DBUS_TYPE_OBJECT_PATH, &path,
1482 DBUS_TYPE_INVALID)) {
22f4096c 1483 log_error("Failed to parse reply: %s", bus_error_message(error));
45fb0699
LP
1484 r = -EIO;
1485 goto finish;
1486 }
1487
ee5762e3 1488 if (need_daemon_reload(bus, name))
729e3769
LP
1489 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1490 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
45fb0699 1491
6e905d93 1492 if (!arg_no_block) {
e4b61340 1493 char *p;
7e4249b9 1494
7e4249b9
LP
1495 if (!(p = strdup(path))) {
1496 log_error("Failed to duplicate path.");
1497 r = -ENOMEM;
1498 goto finish;
1499 }
1500
1501 if ((r = set_put(s, p)) < 0) {
e4b61340 1502 free(p);
7e4249b9
LP
1503 log_error("Failed to add path to set.");
1504 goto finish;
1505 }
e4b61340 1506 }
7e4249b9 1507
706900b7 1508 r = 0;
22f4096c 1509
7e4249b9 1510finish:
7e4249b9
LP
1511 if (m)
1512 dbus_message_unref(m);
1513
1514 if (reply)
1515 dbus_message_unref(reply);
1516
7e4249b9
LP
1517 return r;
1518}
1519
514f4ef5
LP
1520static enum action verb_to_action(const char *verb) {
1521 if (streq(verb, "halt"))
1522 return ACTION_HALT;
1523 else if (streq(verb, "poweroff"))
1524 return ACTION_POWEROFF;
1525 else if (streq(verb, "reboot"))
1526 return ACTION_REBOOT;
20b09ca7
LP
1527 else if (streq(verb, "kexec"))
1528 return ACTION_KEXEC;
514f4ef5
LP
1529 else if (streq(verb, "rescue"))
1530 return ACTION_RESCUE;
1531 else if (streq(verb, "emergency"))
1532 return ACTION_EMERGENCY;
1533 else if (streq(verb, "default"))
1534 return ACTION_DEFAULT;
20b09ca7
LP
1535 else if (streq(verb, "exit"))
1536 return ACTION_EXIT;
514f4ef5
LP
1537 else
1538 return ACTION_INVALID;
1539}
1540
729e3769 1541static int start_unit(DBusConnection *bus, char **args) {
e4b61340
LP
1542
1543 static const char * const table[_ACTION_MAX] = {
514f4ef5
LP
1544 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1545 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1546 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
20b09ca7 1547 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
514f4ef5
LP
1548 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1549 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1550 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1551 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1552 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
f057408c 1553 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
20b09ca7
LP
1554 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1555 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
e4b61340
LP
1556 };
1557
22f4096c 1558 int r, ret = 0;
514f4ef5 1559 const char *method, *mode, *one_name;
e4b61340 1560 Set *s = NULL;
22f4096c 1561 DBusError error;
729e3769 1562 char **name;
22f4096c
LP
1563
1564 dbus_error_init(&error);
e4b61340 1565
514f4ef5
LP
1566 assert(bus);
1567
729e3769 1568 agent_open_if_enabled();
501fc174 1569
e4b61340
LP
1570 if (arg_action == ACTION_SYSTEMCTL) {
1571 method =
a76f7be2
LP
1572 streq(args[0], "stop") ||
1573 streq(args[0], "condstop") ? "StopUnit" :
6f28c033
LP
1574 streq(args[0], "reload") ? "ReloadUnit" :
1575 streq(args[0], "restart") ? "RestartUnit" :
d68201e9 1576
aa5939a3
MS
1577 streq(args[0], "try-restart") ||
1578 streq(args[0], "condrestart") ? "TryRestartUnit" :
d68201e9 1579
6f28c033 1580 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
d68201e9 1581
9d8a57ff 1582 streq(args[0], "reload-or-try-restart") ||
64e5f1b7 1583 streq(args[0], "condreload") ||
d68201e9 1584
aa5939a3 1585 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
6f28c033 1586 "StartUnit";
e4b61340
LP
1587
1588 mode =
514f4ef5
LP
1589 (streq(args[0], "isolate") ||
1590 streq(args[0], "rescue") ||
e67c3609 1591 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
e4b61340 1592
514f4ef5 1593 one_name = table[verb_to_action(args[0])];
e4b61340 1594
e4b61340
LP
1595 } else {
1596 assert(arg_action < ELEMENTSOF(table));
1597 assert(table[arg_action]);
1598
1599 method = "StartUnit";
514f4ef5
LP
1600
1601 mode = (arg_action == ACTION_EMERGENCY ||
6f0d624e
LP
1602 arg_action == ACTION_RESCUE ||
1603 arg_action == ACTION_RUNLEVEL2 ||
1604 arg_action == ACTION_RUNLEVEL3 ||
1605 arg_action == ACTION_RUNLEVEL4 ||
1606 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
514f4ef5
LP
1607
1608 one_name = table[arg_action];
1609 }
1610
6e905d93 1611 if (!arg_no_block) {
22f4096c
LP
1612 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1613 log_error("Could not watch jobs: %s", strerror(-ret));
514f4ef5
LP
1614 goto finish;
1615 }
1616
1617 if (!(s = set_new(string_hash_func, string_compare_func))) {
1618 log_error("Failed to allocate set.");
22f4096c 1619 ret = -ENOMEM;
514f4ef5
LP
1620 goto finish;
1621 }
e4b61340
LP
1622 }
1623
514f4ef5 1624 if (one_name) {
22f4096c 1625 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
514f4ef5
LP
1626 goto finish;
1627 } else {
729e3769
LP
1628 STRV_FOREACH(name, args+1)
1629 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
706900b7 1630 ret = translate_bus_error_to_exit_status(r, &error);
22f4096c
LP
1631 dbus_error_free(&error);
1632 }
e4b61340
LP
1633 }
1634
6e905d93 1635 if (!arg_no_block)
22f4096c
LP
1636 if ((r = wait_for_jobs(bus, s)) < 0) {
1637 ret = r;
fbc43921 1638 goto finish;
22f4096c 1639 }
514f4ef5 1640
e4b61340
LP
1641finish:
1642 if (s)
1643 set_free_free(s);
1644
22f4096c
LP
1645 dbus_error_free(&error);
1646
1647 return ret;
e4b61340
LP
1648}
1649
729e3769 1650static int start_special(DBusConnection *bus, char **args) {
983d9c90
LP
1651 int r;
1652
514f4ef5
LP
1653 assert(bus);
1654 assert(args);
1655
20b09ca7
LP
1656 if (arg_force &&
1657 (streq(args[0], "halt") ||
1658 streq(args[0], "poweroff") ||
1659 streq(args[0], "reboot") ||
1660 streq(args[0], "kexec") ||
1661 streq(args[0], "exit")))
729e3769 1662 return daemon_reload(bus, args);
20b09ca7 1663
729e3769 1664 r = start_unit(bus, args);
983d9c90
LP
1665
1666 if (r >= 0)
1667 warn_wall(verb_to_action(args[0]));
514f4ef5 1668
983d9c90 1669 return r;
514f4ef5
LP
1670}
1671
729e3769 1672static int check_unit(DBusConnection *bus, char **args) {
0183528f
LP
1673 DBusMessage *m = NULL, *reply = NULL;
1674 const char
1675 *interface = "org.freedesktop.systemd1.Unit",
1676 *property = "ActiveState";
22f4096c 1677 int r = 3; /* According to LSB: "program is not running" */
0183528f 1678 DBusError error;
729e3769 1679 char **name;
0183528f
LP
1680
1681 assert(bus);
1682 assert(args);
1683
1684 dbus_error_init(&error);
1685
729e3769 1686 STRV_FOREACH(name, args+1) {
0183528f
LP
1687 const char *path = NULL;
1688 const char *state;
1689 DBusMessageIter iter, sub;
1690
1691 if (!(m = dbus_message_new_method_call(
1692 "org.freedesktop.systemd1",
1693 "/org/freedesktop/systemd1",
1694 "org.freedesktop.systemd1.Manager",
1695 "GetUnit"))) {
1696 log_error("Could not allocate message.");
1697 r = -ENOMEM;
1698 goto finish;
1699 }
1700
1701 if (!dbus_message_append_args(m,
729e3769 1702 DBUS_TYPE_STRING, name,
0183528f
LP
1703 DBUS_TYPE_INVALID)) {
1704 log_error("Could not append arguments to message.");
1705 r = -ENOMEM;
1706 goto finish;
1707 }
1708
1709 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1710
1711 /* Hmm, cannot figure out anything about this unit... */
1712 if (!arg_quiet)
1713 puts("unknown");
1714
ed2d7a44 1715 dbus_error_free(&error);
8a0867d6 1716 dbus_message_unref(m);
78e39b43 1717 m = NULL;
0183528f
LP
1718 continue;
1719 }
1720
1721 if (!dbus_message_get_args(reply, &error,
1722 DBUS_TYPE_OBJECT_PATH, &path,
1723 DBUS_TYPE_INVALID)) {
4cf5d675 1724 log_error("Failed to parse reply: %s", bus_error_message(&error));
0183528f
LP
1725 r = -EIO;
1726 goto finish;
1727 }
1728
1729 dbus_message_unref(m);
1730 if (!(m = dbus_message_new_method_call(
1731 "org.freedesktop.systemd1",
1732 path,
1733 "org.freedesktop.DBus.Properties",
1734 "Get"))) {
1735 log_error("Could not allocate message.");
1736 r = -ENOMEM;
1737 goto finish;
1738 }
1739
1740 if (!dbus_message_append_args(m,
1741 DBUS_TYPE_STRING, &interface,
1742 DBUS_TYPE_STRING, &property,
1743 DBUS_TYPE_INVALID)) {
1744 log_error("Could not append arguments to message.");
1745 r = -ENOMEM;
1746 goto finish;
1747 }
1748
1749 dbus_message_unref(reply);
1750 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 1751 log_error("Failed to issue method call: %s", bus_error_message(&error));
0183528f
LP
1752 r = -EIO;
1753 goto finish;
1754 }
1755
1756 if (!dbus_message_iter_init(reply, &iter) ||
1757 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1758 log_error("Failed to parse reply.");
1759 r = -EIO;
1760 goto finish;
1761 }
1762
1763 dbus_message_iter_recurse(&iter, &sub);
1764
1765 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1766 log_error("Failed to parse reply.");
1767 r = -EIO;
1768 goto finish;
1769 }
1770
1771 dbus_message_iter_get_basic(&sub, &state);
1772
1773 if (!arg_quiet)
1774 puts(state);
1775
f1e36d67 1776 if (streq(state, "active") || streq(state, "reloading"))
0183528f
LP
1777 r = 0;
1778
1779 dbus_message_unref(m);
1780 dbus_message_unref(reply);
1781 m = reply = NULL;
1782 }
1783
1784finish:
1785 if (m)
1786 dbus_message_unref(m);
1787
1788 if (reply)
1789 dbus_message_unref(reply);
1790
1791 dbus_error_free(&error);
1792
1793 return r;
48220598
LP
1794}
1795
729e3769 1796static int kill_unit(DBusConnection *bus, char **args) {
8a0867d6
LP
1797 DBusMessage *m = NULL, *reply = NULL;
1798 int r = 0;
1799 DBusError error;
729e3769 1800 char **name;
8a0867d6
LP
1801
1802 assert(bus);
1803 assert(args);
1804
1805 dbus_error_init(&error);
1806
1807 if (!arg_kill_who)
1808 arg_kill_who = "all";
1809
1810 if (!arg_kill_mode)
1811 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1812
729e3769 1813 STRV_FOREACH(name, args+1) {
8a0867d6
LP
1814
1815 if (!(m = dbus_message_new_method_call(
1816 "org.freedesktop.systemd1",
1817 "/org/freedesktop/systemd1",
1818 "org.freedesktop.systemd1.Manager",
1819 "KillUnit"))) {
1820 log_error("Could not allocate message.");
1821 r = -ENOMEM;
1822 goto finish;
1823 }
1824
1825 if (!dbus_message_append_args(m,
729e3769 1826 DBUS_TYPE_STRING, name,
8a0867d6
LP
1827 DBUS_TYPE_STRING, &arg_kill_who,
1828 DBUS_TYPE_STRING, &arg_kill_mode,
1829 DBUS_TYPE_INT32, &arg_signal,
1830 DBUS_TYPE_INVALID)) {
1831 log_error("Could not append arguments to message.");
1832 r = -ENOMEM;
1833 goto finish;
1834 }
1835
1836 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1837 log_error("Failed to issue method call: %s", bus_error_message(&error));
1838 dbus_error_free(&error);
1839 r = -EIO;
1840 }
1841
1842 dbus_message_unref(m);
1843
1844 if (reply)
1845 dbus_message_unref(reply);
1846 m = reply = NULL;
1847 }
1848
1849finish:
1850 if (m)
1851 dbus_message_unref(m);
1852
1853 if (reply)
1854 dbus_message_unref(reply);
1855
1856 dbus_error_free(&error);
1857
1858 return r;
1859}
1860
582a507f 1861typedef struct ExecStatusInfo {
0129173a
LP
1862 char *name;
1863
582a507f
LP
1864 char *path;
1865 char **argv;
1866
b708e7ce
LP
1867 bool ignore;
1868
582a507f
LP
1869 usec_t start_timestamp;
1870 usec_t exit_timestamp;
1871 pid_t pid;
1872 int code;
1873 int status;
1874
1875 LIST_FIELDS(struct ExecStatusInfo, exec);
1876} ExecStatusInfo;
1877
1878static void exec_status_info_free(ExecStatusInfo *i) {
1879 assert(i);
1880
0129173a 1881 free(i->name);
582a507f
LP
1882 free(i->path);
1883 strv_free(i->argv);
1884 free(i);
1885}
1886
1887static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
b21a0ef8 1888 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
582a507f
LP
1889 DBusMessageIter sub2, sub3;
1890 const char*path;
1891 unsigned n;
1892 uint32_t pid;
1893 int32_t code, status;
b708e7ce 1894 dbus_bool_t ignore;
582a507f
LP
1895
1896 assert(i);
1897 assert(i);
1898
1899 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1900 return -EIO;
1901
1902 dbus_message_iter_recurse(sub, &sub2);
1903
1904 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1905 return -EIO;
1906
1907 if (!(i->path = strdup(path)))
1908 return -ENOMEM;
1909
1910 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1911 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1912 return -EIO;
1913
1914 n = 0;
1915 dbus_message_iter_recurse(&sub2, &sub3);
1916 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1917 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1918 dbus_message_iter_next(&sub3);
1919 n++;
1920 }
1921
1922
1923 if (!(i->argv = new0(char*, n+1)))
1924 return -ENOMEM;
1925
1926 n = 0;
1927 dbus_message_iter_recurse(&sub2, &sub3);
1928 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1929 const char *s;
1930
1931 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1932 dbus_message_iter_get_basic(&sub3, &s);
1933 dbus_message_iter_next(&sub3);
1934
1935 if (!(i->argv[n++] = strdup(s)))
1936 return -ENOMEM;
1937 }
1938
1939 if (!dbus_message_iter_next(&sub2) ||
b708e7ce 1940 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
582a507f 1941 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
b21a0ef8 1942 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
582a507f 1943 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
b21a0ef8 1944 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
582a507f
LP
1945 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1946 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1947 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1948 return -EIO;
1949
b708e7ce 1950 i->ignore = ignore;
582a507f
LP
1951 i->start_timestamp = (usec_t) start_timestamp;
1952 i->exit_timestamp = (usec_t) exit_timestamp;
1953 i->pid = (pid_t) pid;
1954 i->code = code;
1955 i->status = status;
1956
1957 return 0;
1958}
1959
61cbdc4b
LP
1960typedef struct UnitStatusInfo {
1961 const char *id;
1962 const char *load_state;
1963 const char *active_state;
1964 const char *sub_state;
a4375746 1965 const char *unit_file_state;
61cbdc4b
LP
1966
1967 const char *description;
4a9e2fff 1968 const char *following;
61cbdc4b 1969
c31b4423 1970 const char *path;
61cbdc4b
LP
1971 const char *default_control_group;
1972
9f39404c
LP
1973 const char *load_error;
1974
584be568
LP
1975 usec_t inactive_exit_timestamp;
1976 usec_t active_enter_timestamp;
1977 usec_t active_exit_timestamp;
1978 usec_t inactive_enter_timestamp;
1979
45fb0699
LP
1980 bool need_daemon_reload;
1981
61cbdc4b
LP
1982 /* Service */
1983 pid_t main_pid;
1984 pid_t control_pid;
1985 const char *status_text;
d06dacd0 1986 bool running:1;
07459bb6 1987#ifdef HAVE_SYSV_COMPAT
d06dacd0 1988 bool is_sysv:1;
07459bb6 1989#endif
61cbdc4b
LP
1990
1991 usec_t start_timestamp;
1992 usec_t exit_timestamp;
1993
1994 int exit_code, exit_status;
1995
90bbc946
LP
1996 usec_t condition_timestamp;
1997 bool condition_result;
1998
61cbdc4b
LP
1999 /* Socket */
2000 unsigned n_accepted;
2001 unsigned n_connections;
b8131a87 2002 bool accept;
61cbdc4b
LP
2003
2004 /* Device */
2005 const char *sysfs_path;
2006
2007 /* Mount, Automount */
2008 const char *where;
2009
2010 /* Swap */
2011 const char *what;
582a507f
LP
2012
2013 LIST_HEAD(ExecStatusInfo, exec);
61cbdc4b
LP
2014} UnitStatusInfo;
2015
2016static void print_status_info(UnitStatusInfo *i) {
582a507f 2017 ExecStatusInfo *p;
2ee68f72 2018 const char *on, *off, *ss;
584be568
LP
2019 usec_t timestamp;
2020 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2021 char since2[FORMAT_TIMESTAMP_MAX], *s2;
582a507f 2022
61cbdc4b
LP
2023 assert(i);
2024
2025 /* This shows pretty information about a unit. See
2026 * print_property() for a low-level property printer */
2027
2028 printf("%s", strna(i->id));
2029
2030 if (i->description && !streq_ptr(i->id, i->description))
2031 printf(" - %s", i->description);
2032
2033 printf("\n");
2034
4a9e2fff
LP
2035 if (i->following)
2036 printf("\t Follow: unit currently follows state of %s\n", i->following);
2037
00dc5d76
LP
2038 if (streq_ptr(i->load_state, "failed") ||
2039 streq_ptr(i->load_state, "banned")) {
c31b4423
LP
2040 on = ansi_highlight(true);
2041 off = ansi_highlight(false);
2042 } else
2043 on = off = "";
2044
9f39404c
LP
2045 if (i->load_error)
2046 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
a4375746
LP
2047 else if (i->path && i->unit_file_state)
2048 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
9f39404c 2049 else if (i->path)
c31b4423 2050 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
61cbdc4b 2051 else
c31b4423 2052 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
61cbdc4b 2053
2ee68f72
LP
2054 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2055
fdf20a31 2056 if (streq_ptr(i->active_state, "failed")) {
2ee68f72
LP
2057 on = ansi_highlight(true);
2058 off = ansi_highlight(false);
2059 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2060 on = ansi_highlight_green(true);
2061 off = ansi_highlight_green(false);
2062 } else
2063 on = off = "";
2064
2065 if (ss)
584be568 2066 printf("\t Active: %s%s (%s)%s",
2ee68f72
LP
2067 on,
2068 strna(i->active_state),
2069 ss,
2070 off);
2071 else
584be568 2072 printf("\t Active: %s%s%s",
2ee68f72
LP
2073 on,
2074 strna(i->active_state),
2075 off);
61cbdc4b 2076
584be568
LP
2077 timestamp = (streq_ptr(i->active_state, "active") ||
2078 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2079 (streq_ptr(i->active_state, "inactive") ||
fdf20a31 2080 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
584be568
LP
2081 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2082 i->active_exit_timestamp;
2083
2084 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2085 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2086
2087 if (s1)
538da63d 2088 printf(" since %s; %s\n", s2, s1);
584be568 2089 else if (s2)
538da63d 2090 printf(" since %s\n", s2);
584be568
LP
2091 else
2092 printf("\n");
2093
90bbc946
LP
2094 if (!i->condition_result && i->condition_timestamp > 0) {
2095 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2096 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2097
2098 if (s1)
2099 printf("\t start condition failed at %s; %s\n", s2, s1);
2100 else if (s2)
2101 printf("\t start condition failed at %s\n", s2);
2102 }
2103
61cbdc4b
LP
2104 if (i->sysfs_path)
2105 printf("\t Device: %s\n", i->sysfs_path);
9feeba4b 2106 if (i->where)
61cbdc4b 2107 printf("\t Where: %s\n", i->where);
9feeba4b 2108 if (i->what)
61cbdc4b
LP
2109 printf("\t What: %s\n", i->what);
2110
b8131a87 2111 if (i->accept)
61cbdc4b
LP
2112 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2113
582a507f
LP
2114 LIST_FOREACH(exec, p, i->exec) {
2115 char *t;
9a57c629 2116 bool good;
582a507f
LP
2117
2118 /* Only show exited processes here */
2119 if (p->code == 0)
2120 continue;
2121
2122 t = strv_join(p->argv, " ");
9a57c629 2123 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
582a507f
LP
2124 free(t);
2125
9a57c629
LP
2126#ifdef HAVE_SYSV_COMPAT
2127 if (i->is_sysv)
2128 good = is_clean_exit_lsb(p->code, p->status);
2129 else
2130#endif
2131 good = is_clean_exit(p->code, p->status);
2132
2133 if (!good) {
2134 on = ansi_highlight(true);
2135 off = ansi_highlight(false);
2136 } else
2137 on = off = "";
2138
2139 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2140
d06dacd0
LP
2141 if (p->code == CLD_EXITED) {
2142 const char *c;
2143
582a507f 2144 printf("status=%i", p->status);
d06dacd0 2145
07459bb6 2146#ifdef HAVE_SYSV_COMPAT
d06dacd0 2147 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
07459bb6
FF
2148#else
2149 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2150#endif
d06dacd0
LP
2151 printf("/%s", c);
2152
2153 } else
582a507f 2154 printf("signal=%s", signal_to_string(p->status));
9a57c629
LP
2155
2156 printf(")%s\n", off);
2157
2158 on = off = NULL;
582a507f
LP
2159
2160 if (i->main_pid == p->pid &&
2161 i->start_timestamp == p->start_timestamp &&
2162 i->exit_timestamp == p->start_timestamp)
2163 /* Let's not show this twice */
2164 i->main_pid = 0;
2165
2166 if (p->pid == i->control_pid)
2167 i->control_pid = 0;
2168 }
2169
61cbdc4b
LP
2170 if (i->main_pid > 0 || i->control_pid > 0) {
2171 printf("\t");
2172
2173 if (i->main_pid > 0) {
f3d41013 2174 printf("Main PID: %u", (unsigned) i->main_pid);
61cbdc4b
LP
2175
2176 if (i->running) {
2177 char *t = NULL;
2178 get_process_name(i->main_pid, &t);
2179 if (t) {
2180 printf(" (%s)", t);
2181 free(t);
2182 }
6d4fc029 2183 } else if (i->exit_code > 0) {
61cbdc4b
LP
2184 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2185
d06dacd0
LP
2186 if (i->exit_code == CLD_EXITED) {
2187 const char *c;
2188
61cbdc4b 2189 printf("status=%i", i->exit_status);
d06dacd0 2190
07459bb6 2191#ifdef HAVE_SYSV_COMPAT
d06dacd0 2192 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
07459bb6
FF
2193#else
2194 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2195#endif
d06dacd0
LP
2196 printf("/%s", c);
2197
2198 } else
582a507f 2199 printf("signal=%s", signal_to_string(i->exit_status));
6d4fc029
LP
2200 printf(")");
2201 }
61cbdc4b
LP
2202 }
2203
2204 if (i->main_pid > 0 && i->control_pid > 0)
2205 printf(";");
2206
2207 if (i->control_pid > 0) {
2208 char *t = NULL;
2209
2210 printf(" Control: %u", (unsigned) i->control_pid);
2211
2212 get_process_name(i->control_pid, &t);
2213 if (t) {
2214 printf(" (%s)", t);
2215 free(t);
2216 }
2217 }
2218
2219 printf("\n");
2220 }
2221
17bb7382
LP
2222 if (i->status_text)
2223 printf("\t Status: \"%s\"\n", i->status_text);
2224
c59760ee 2225 if (i->default_control_group) {
ab35fb1b
LP
2226 unsigned c;
2227
61cbdc4b 2228 printf("\t CGroup: %s\n", i->default_control_group);
ab35fb1b 2229
a8f11321
LP
2230 if (arg_transport != TRANSPORT_SSH) {
2231 if ((c = columns()) > 18)
2232 c -= 18;
2233 else
2234 c = 0;
ab35fb1b 2235
a8f11321
LP
2236 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2237 }
c59760ee 2238 }
45fb0699
LP
2239
2240 if (i->need_daemon_reload)
2cc59dbf
LP
2241 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2242 ansi_highlight(true),
2243 ansi_highlight(false),
729e3769 2244 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
61cbdc4b
LP
2245}
2246
2247static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2248
a4c279f8
LP
2249 assert(name);
2250 assert(iter);
2251 assert(i);
2252
61cbdc4b
LP
2253 switch (dbus_message_iter_get_arg_type(iter)) {
2254
2255 case DBUS_TYPE_STRING: {
2256 const char *s;
2257
2258 dbus_message_iter_get_basic(iter, &s);
2259
a4c279f8 2260 if (!isempty(s)) {
61cbdc4b
LP
2261 if (streq(name, "Id"))
2262 i->id = s;
2263 else if (streq(name, "LoadState"))
2264 i->load_state = s;
2265 else if (streq(name, "ActiveState"))
2266 i->active_state = s;
2267 else if (streq(name, "SubState"))
2268 i->sub_state = s;
2269 else if (streq(name, "Description"))
2270 i->description = s;
2271 else if (streq(name, "FragmentPath"))
c31b4423 2272 i->path = s;
07459bb6 2273#ifdef HAVE_SYSV_COMPAT
d06dacd0
LP
2274 else if (streq(name, "SysVPath")) {
2275 i->is_sysv = true;
c31b4423 2276 i->path = s;
07459bb6
FF
2277 }
2278#endif
2279 else if (streq(name, "DefaultControlGroup"))
61cbdc4b
LP
2280 i->default_control_group = s;
2281 else if (streq(name, "StatusText"))
2282 i->status_text = s;
2283 else if (streq(name, "SysFSPath"))
2284 i->sysfs_path = s;
2285 else if (streq(name, "Where"))
2286 i->where = s;
2287 else if (streq(name, "What"))
2288 i->what = s;
4a9e2fff
LP
2289 else if (streq(name, "Following"))
2290 i->following = s;
a4375746
LP
2291 else if (streq(name, "UnitFileState"))
2292 i->unit_file_state = s;
61cbdc4b
LP
2293 }
2294
2295 break;
2296 }
2297
b8131a87
LP
2298 case DBUS_TYPE_BOOLEAN: {
2299 dbus_bool_t b;
2300
2301 dbus_message_iter_get_basic(iter, &b);
2302
2303 if (streq(name, "Accept"))
2304 i->accept = b;
45fb0699
LP
2305 else if (streq(name, "NeedDaemonReload"))
2306 i->need_daemon_reload = b;
90bbc946
LP
2307 else if (streq(name, "ConditionResult"))
2308 i->condition_result = b;
b8131a87
LP
2309
2310 break;
2311 }
2312
61cbdc4b
LP
2313 case DBUS_TYPE_UINT32: {
2314 uint32_t u;
2315
2316 dbus_message_iter_get_basic(iter, &u);
2317
2318 if (streq(name, "MainPID")) {
2319 if (u > 0) {
2320 i->main_pid = (pid_t) u;
2321 i->running = true;
2322 }
2323 } else if (streq(name, "ControlPID"))
2324 i->control_pid = (pid_t) u;
2325 else if (streq(name, "ExecMainPID")) {
2326 if (u > 0)
2327 i->main_pid = (pid_t) u;
2328 } else if (streq(name, "NAccepted"))
2329 i->n_accepted = u;
2330 else if (streq(name, "NConnections"))
2331 i->n_connections = u;
2332
2333 break;
2334 }
2335
2336 case DBUS_TYPE_INT32: {
2337 int32_t j;
2338
2339 dbus_message_iter_get_basic(iter, &j);
2340
2341 if (streq(name, "ExecMainCode"))
2342 i->exit_code = (int) j;
2343 else if (streq(name, "ExecMainStatus"))
2344 i->exit_status = (int) j;
2345
2346 break;
2347 }
2348
2349 case DBUS_TYPE_UINT64: {
2350 uint64_t u;
2351
2352 dbus_message_iter_get_basic(iter, &u);
2353
2354 if (streq(name, "ExecMainStartTimestamp"))
2355 i->start_timestamp = (usec_t) u;
2356 else if (streq(name, "ExecMainExitTimestamp"))
2357 i->exit_timestamp = (usec_t) u;
584be568
LP
2358 else if (streq(name, "ActiveEnterTimestamp"))
2359 i->active_enter_timestamp = (usec_t) u;
2360 else if (streq(name, "InactiveEnterTimestamp"))
2361 i->inactive_enter_timestamp = (usec_t) u;
2362 else if (streq(name, "InactiveExitTimestamp"))
2363 i->inactive_exit_timestamp = (usec_t) u;
2364 else if (streq(name, "ActiveExitTimestamp"))
2365 i->active_exit_timestamp = (usec_t) u;
90bbc946
LP
2366 else if (streq(name, "ConditionTimestamp"))
2367 i->condition_timestamp = (usec_t) u;
61cbdc4b
LP
2368
2369 break;
2370 }
582a507f
LP
2371
2372 case DBUS_TYPE_ARRAY: {
2373
2374 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2375 startswith(name, "Exec")) {
2376 DBusMessageIter sub;
2377
2378 dbus_message_iter_recurse(iter, &sub);
2379 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2380 ExecStatusInfo *info;
2381 int r;
2382
2383 if (!(info = new0(ExecStatusInfo, 1)))
2384 return -ENOMEM;
2385
0129173a
LP
2386 if (!(info->name = strdup(name))) {
2387 free(info);
2388 return -ENOMEM;
2389 }
2390
582a507f
LP
2391 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2392 free(info);
2393 return r;
2394 }
2395
2396 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2397
2398 dbus_message_iter_next(&sub);
2399 }
2400 }
2401
2402 break;
2403 }
9f39404c
LP
2404
2405 case DBUS_TYPE_STRUCT: {
2406
2407 if (streq(name, "LoadError")) {
2408 DBusMessageIter sub;
2409 const char *n, *message;
2410 int r;
2411
2412 dbus_message_iter_recurse(iter, &sub);
2413
2414 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2415 if (r < 0)
2416 return r;
2417
2418 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2419 if (r < 0)
2420 return r;
2421
2422 if (!isempty(message))
2423 i->load_error = message;
2424 }
2425
2426 break;
2427 }
61cbdc4b
LP
2428 }
2429
2430 return 0;
2431}
2432
48220598
LP
2433static int print_property(const char *name, DBusMessageIter *iter) {
2434 assert(name);
2435 assert(iter);
2436
61cbdc4b
LP
2437 /* This is a low-level property printer, see
2438 * print_status_info() for the nicer output */
2439
ea4a240d 2440 if (arg_property && !strv_find(arg_property, name))
48220598
LP
2441 return 0;
2442
2443 switch (dbus_message_iter_get_arg_type(iter)) {
2444
48220598
LP
2445 case DBUS_TYPE_STRUCT: {
2446 DBusMessageIter sub;
2447 dbus_message_iter_recurse(iter, &sub);
2448
ebf57b80 2449 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
48220598
LP
2450 uint32_t u;
2451
2452 dbus_message_iter_get_basic(&sub, &u);
2453
2454 if (u)
2455 printf("%s=%u\n", name, (unsigned) u);
2456 else if (arg_all)
2457 printf("%s=\n", name);
2458
2459 return 0;
ebf57b80 2460 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
48220598
LP
2461 const char *s;
2462
2463 dbus_message_iter_get_basic(&sub, &s);
2464
2465 if (arg_all || s[0])
2466 printf("%s=%s\n", name, s);
2467
2468 return 0;
9f39404c
LP
2469 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2470 const char *a = NULL, *b = NULL;
2471
f786e80d 2472 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
9f39404c
LP
2473 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2474
2475 if (arg_all || !isempty(a) || !isempty(b))
2476 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
f786e80d
LP
2477
2478 return 0;
48220598
LP
2479 }
2480
2481 break;
2482 }
2483
2484 case DBUS_TYPE_ARRAY:
2485
a4c279f8 2486 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
8c7be95e
LP
2487 DBusMessageIter sub, sub2;
2488
2489 dbus_message_iter_recurse(iter, &sub);
2490 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2491 const char *path;
2492 dbus_bool_t ignore;
2493
2494 dbus_message_iter_recurse(&sub, &sub2);
2495
2496 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
ecdcbc5e 2498 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
8c7be95e
LP
2499
2500 dbus_message_iter_next(&sub);
2501 }
2502
2503 return 0;
2504
ebf57b80
LP
2505 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2506 DBusMessageIter sub, sub2;
2507
2508 dbus_message_iter_recurse(iter, &sub);
ebf57b80
LP
2509 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2510 const char *type, *path;
2511
2512 dbus_message_iter_recurse(&sub, &sub2);
2513
2514 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2515 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2516 printf("%s=%s\n", type, path);
2517
2518 dbus_message_iter_next(&sub);
2519 }
2520
707e5e52 2521 return 0;
582a507f 2522
707e5e52
LP
2523 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2524 DBusMessageIter sub, sub2;
2525
2526 dbus_message_iter_recurse(iter, &sub);
707e5e52
LP
2527 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2528 const char *base;
2529 uint64_t value, next_elapse;
2530
2531 dbus_message_iter_recurse(&sub, &sub2);
2532
2533 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2534 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
552e4331
LP
2535 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2536 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2537
2538 printf("%s={ value=%s ; next_elapse=%s }\n",
fe68089d 2539 base,
552e4331
LP
2540 format_timespan(timespan1, sizeof(timespan1), value),
2541 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2542 }
fe68089d
LP
2543
2544 dbus_message_iter_next(&sub);
2545 }
2546
2547 return 0;
fe68089d 2548
d8bbda91
LP
2549 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2550 DBusMessageIter sub, sub2;
2551
2552 dbus_message_iter_recurse(iter, &sub);
2553 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2554 const char *controller, *attr, *value;
2555
2556 dbus_message_iter_recurse(&sub, &sub2);
2557
2558 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2559 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2560 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2561
2562 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2563 controller,
2564 attr,
2565 value);
2566 }
2567
2568 dbus_message_iter_next(&sub);
2569 }
2570
2571 return 0;
2572
582a507f
LP
2573 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2574 DBusMessageIter sub;
fe68089d
LP
2575
2576 dbus_message_iter_recurse(iter, &sub);
fe68089d 2577 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
582a507f 2578 ExecStatusInfo info;
fe68089d 2579
582a507f
LP
2580 zero(info);
2581 if (exec_status_info_deserialize(&sub, &info) >= 0) {
fe68089d 2582 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
582a507f
LP
2583 char *t;
2584
2585 t = strv_join(info.argv, " ");
2586
ecdcbc5e 2587 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
582a507f
LP
2588 name,
2589 strna(info.path),
2590 strna(t),
b708e7ce 2591 yes_no(info.ignore),
582a507f
LP
2592 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2593 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2594 (unsigned) info. pid,
2595 sigchld_code_to_string(info.code),
2596 info.status,
2597 info.code == CLD_EXITED ? "" : "/",
2598 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
fe68089d 2599
582a507f 2600 free(t);
fe68089d
LP
2601 }
2602
582a507f
LP
2603 free(info.path);
2604 strv_free(info.argv);
707e5e52
LP
2605
2606 dbus_message_iter_next(&sub);
2607 }
2608
48220598
LP
2609 return 0;
2610 }
2611
2612 break;
2613 }
2614
a4c279f8
LP
2615 if (generic_print_property(name, iter, arg_all) > 0)
2616 return 0;
2617
48220598
LP
2618 if (arg_all)
2619 printf("%s=[unprintable]\n", name);
2620
2621 return 0;
2622}
2623
be8088a2 2624static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
48220598
LP
2625 DBusMessage *m = NULL, *reply = NULL;
2626 const char *interface = "";
2627 int r;
2628 DBusError error;
2629 DBusMessageIter iter, sub, sub2, sub3;
61cbdc4b 2630 UnitStatusInfo info;
582a507f 2631 ExecStatusInfo *p;
48220598
LP
2632
2633 assert(bus);
2634 assert(path);
61cbdc4b 2635 assert(new_line);
48220598 2636
61cbdc4b 2637 zero(info);
48220598
LP
2638 dbus_error_init(&error);
2639
2640 if (!(m = dbus_message_new_method_call(
2641 "org.freedesktop.systemd1",
2642 path,
2643 "org.freedesktop.DBus.Properties",
2644 "GetAll"))) {
2645 log_error("Could not allocate message.");
2646 r = -ENOMEM;
2647 goto finish;
2648 }
2649
2650 if (!dbus_message_append_args(m,
2651 DBUS_TYPE_STRING, &interface,
2652 DBUS_TYPE_INVALID)) {
2653 log_error("Could not append arguments to message.");
2654 r = -ENOMEM;
2655 goto finish;
2656 }
2657
2658 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 2659 log_error("Failed to issue method call: %s", bus_error_message(&error));
48220598
LP
2660 r = -EIO;
2661 goto finish;
2662 }
2663
2664 if (!dbus_message_iter_init(reply, &iter) ||
2665 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2666 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2667 log_error("Failed to parse reply.");
2668 r = -EIO;
2669 goto finish;
2670 }
2671
2672 dbus_message_iter_recurse(&iter, &sub);
2673
61cbdc4b
LP
2674 if (*new_line)
2675 printf("\n");
2676
2677 *new_line = true;
2678
48220598
LP
2679 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2680 const char *name;
2681
2682 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2683 log_error("Failed to parse reply.");
2684 r = -EIO;
2685 goto finish;
2686 }
2687
2688 dbus_message_iter_recurse(&sub, &sub2);
0183528f 2689
48220598
LP
2690 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2691 log_error("Failed to parse reply.");
2692 r = -EIO;
2693 goto finish;
2694 }
2695
2696 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2697 log_error("Failed to parse reply.");
2698 r = -EIO;
2699 goto finish;
2700 }
2701
2702 dbus_message_iter_recurse(&sub2, &sub3);
2703
61cbdc4b
LP
2704 if (show_properties)
2705 r = print_property(name, &sub3);
2706 else
2707 r = status_property(name, &sub3, &info);
2708
2709 if (r < 0) {
48220598
LP
2710 log_error("Failed to parse reply.");
2711 r = -EIO;
2712 goto finish;
2713 }
2714
2715 dbus_message_iter_next(&sub);
2716 }
2717
f1e36d67
LP
2718 r = 0;
2719
22f4096c
LP
2720 if (!show_properties)
2721 print_status_info(&info);
f1e36d67 2722
22f4096c 2723 if (!streq_ptr(info.active_state, "active") &&
be8088a2
LP
2724 !streq_ptr(info.active_state, "reloading") &&
2725 streq(verb, "status"))
22f4096c
LP
2726 /* According to LSB: "program not running" */
2727 r = 3;
61cbdc4b 2728
582a507f
LP
2729 while ((p = info.exec)) {
2730 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2731 exec_status_info_free(p);
2732 }
2733
48220598
LP
2734finish:
2735 if (m)
2736 dbus_message_unref(m);
2737
2738 if (reply)
2739 dbus_message_unref(reply);
2740
2741 dbus_error_free(&error);
2742
2743 return r;
2744}
2745
729e3769 2746static int show(DBusConnection *bus, char **args) {
48220598 2747 DBusMessage *m = NULL, *reply = NULL;
22f4096c 2748 int r, ret = 0;
48220598 2749 DBusError error;
61cbdc4b 2750 bool show_properties, new_line = false;
729e3769 2751 char **name;
48220598
LP
2752
2753 assert(bus);
2754 assert(args);
2755
2756 dbus_error_init(&error);
2757
61cbdc4b
LP
2758 show_properties = !streq(args[0], "status");
2759
ec14911e 2760 if (show_properties)
1968a360 2761 pager_open_if_enabled();
ec14911e 2762
729e3769 2763 if (show_properties && strv_length(args) <= 1) {
48220598
LP
2764 /* If not argument is specified inspect the manager
2765 * itself */
2766
be8088a2 2767 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
48220598
LP
2768 goto finish;
2769 }
2770
729e3769 2771 STRV_FOREACH(name, args+1) {
48220598
LP
2772 const char *path = NULL;
2773 uint32_t id;
2774
729e3769 2775 if (safe_atou32(*name, &id) < 0) {
598b557b
LP
2776
2777 /* Interpret as unit name */
48220598
LP
2778
2779 if (!(m = dbus_message_new_method_call(
2780 "org.freedesktop.systemd1",
2781 "/org/freedesktop/systemd1",
2782 "org.freedesktop.systemd1.Manager",
e87d1818 2783 "LoadUnit"))) {
48220598 2784 log_error("Could not allocate message.");
22f4096c 2785 ret = -ENOMEM;
48220598
LP
2786 goto finish;
2787 }
2788
2789 if (!dbus_message_append_args(m,
729e3769 2790 DBUS_TYPE_STRING, name,
48220598
LP
2791 DBUS_TYPE_INVALID)) {
2792 log_error("Could not append arguments to message.");
22f4096c 2793 ret = -ENOMEM;
48220598
LP
2794 goto finish;
2795 }
2796
ed2d7a44
LP
2797 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2798
2799 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
4cf5d675 2800 log_error("Failed to issue method call: %s", bus_error_message(&error));
22f4096c 2801 ret = -EIO;
ed2d7a44
LP
2802 goto finish;
2803 }
2804
2805 dbus_error_free(&error);
2806
2807 dbus_message_unref(m);
2808 if (!(m = dbus_message_new_method_call(
2809 "org.freedesktop.systemd1",
2810 "/org/freedesktop/systemd1",
2811 "org.freedesktop.systemd1.Manager",
2812 "GetUnit"))) {
2813 log_error("Could not allocate message.");
22f4096c 2814 ret = -ENOMEM;
ed2d7a44
LP
2815 goto finish;
2816 }
2817
2818 if (!dbus_message_append_args(m,
729e3769 2819 DBUS_TYPE_STRING, name,
ed2d7a44
LP
2820 DBUS_TYPE_INVALID)) {
2821 log_error("Could not append arguments to message.");
22f4096c 2822 ret = -ENOMEM;
ed2d7a44
LP
2823 goto finish;
2824 }
2825
2826 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 2827 log_error("Failed to issue method call: %s", bus_error_message(&error));
22f4096c
LP
2828
2829 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2830 ret = 4; /* According to LSB: "program or service status is unknown" */
2831 else
2832 ret = -EIO;
ed2d7a44
LP
2833 goto finish;
2834 }
2835 }
2836
598b557b
LP
2837 } else if (show_properties) {
2838
2839 /* Interpret as job id */
48220598
LP
2840
2841 if (!(m = dbus_message_new_method_call(
2842 "org.freedesktop.systemd1",
2843 "/org/freedesktop/systemd1",
2844 "org.freedesktop.systemd1.Manager",
2845 "GetJob"))) {
2846 log_error("Could not allocate message.");
22f4096c 2847 ret = -ENOMEM;
48220598
LP
2848 goto finish;
2849 }
2850
2851 if (!dbus_message_append_args(m,
2852 DBUS_TYPE_UINT32, &id,
2853 DBUS_TYPE_INVALID)) {
2854 log_error("Could not append arguments to message.");
22f4096c 2855 ret = -ENOMEM;
48220598
LP
2856 goto finish;
2857 }
48220598 2858
598b557b 2859 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 2860 log_error("Failed to issue method call: %s", bus_error_message(&error));
22f4096c 2861 ret = -EIO;
598b557b
LP
2862 goto finish;
2863 }
2864 } else {
2865
2866 /* Interpret as PID */
2867
2868 if (!(m = dbus_message_new_method_call(
2869 "org.freedesktop.systemd1",
2870 "/org/freedesktop/systemd1",
2871 "org.freedesktop.systemd1.Manager",
2872 "GetUnitByPID"))) {
2873 log_error("Could not allocate message.");
22f4096c 2874 ret = -ENOMEM;
598b557b
LP
2875 goto finish;
2876 }
2877
2878 if (!dbus_message_append_args(m,
2879 DBUS_TYPE_UINT32, &id,
2880 DBUS_TYPE_INVALID)) {
2881 log_error("Could not append arguments to message.");
22f4096c 2882 ret = -ENOMEM;
598b557b
LP
2883 goto finish;
2884 }
2885
ed2d7a44 2886 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 2887 log_error("Failed to issue method call: %s", bus_error_message(&error));
22f4096c 2888 ret = -EIO;
ed2d7a44
LP
2889 goto finish;
2890 }
48220598
LP
2891 }
2892
2893 if (!dbus_message_get_args(reply, &error,
2894 DBUS_TYPE_OBJECT_PATH, &path,
2895 DBUS_TYPE_INVALID)) {
4cf5d675 2896 log_error("Failed to parse reply: %s", bus_error_message(&error));
22f4096c 2897 ret = -EIO;
48220598
LP
2898 goto finish;
2899 }
2900
be8088a2 2901 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
22f4096c 2902 ret = r;
48220598
LP
2903
2904 dbus_message_unref(m);
2905 dbus_message_unref(reply);
2906 m = reply = NULL;
2907 }
2908
48220598
LP
2909finish:
2910 if (m)
2911 dbus_message_unref(m);
2912
2913 if (reply)
2914 dbus_message_unref(reply);
2915
2916 dbus_error_free(&error);
2917
22f4096c 2918 return ret;
0183528f
LP
2919}
2920
729e3769 2921static int dump(DBusConnection *bus, char **args) {
7e4249b9
LP
2922 DBusMessage *m = NULL, *reply = NULL;
2923 DBusError error;
2924 int r;
2925 const char *text;
2926
2927 dbus_error_init(&error);
2928
1968a360 2929 pager_open_if_enabled();
ec14911e 2930
7e4249b9
LP
2931 if (!(m = dbus_message_new_method_call(
2932 "org.freedesktop.systemd1",
2933 "/org/freedesktop/systemd1",
2934 "org.freedesktop.systemd1.Manager",
2935 "Dump"))) {
2936 log_error("Could not allocate message.");
2937 return -ENOMEM;
2938 }
2939
2940 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 2941 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
2942 r = -EIO;
2943 goto finish;
2944 }
2945
2946 if (!dbus_message_get_args(reply, &error,
2947 DBUS_TYPE_STRING, &text,
2948 DBUS_TYPE_INVALID)) {
4cf5d675 2949 log_error("Failed to parse reply: %s", bus_error_message(&error));
7e4249b9
LP
2950 r = -EIO;
2951 goto finish;
2952 }
2953
2954 fputs(text, stdout);
2955
2956 r = 0;
2957
2958finish:
2959 if (m)
2960 dbus_message_unref(m);
2961
2962 if (reply)
2963 dbus_message_unref(reply);
2964
2965 dbus_error_free(&error);
2966
2967 return r;
2968}
2969
729e3769 2970static int snapshot(DBusConnection *bus, char **args) {
7e4249b9
LP
2971 DBusMessage *m = NULL, *reply = NULL;
2972 DBusError error;
2973 int r;
2974 const char *name = "", *path, *id;
2975 dbus_bool_t cleanup = FALSE;
2976 DBusMessageIter iter, sub;
2977 const char
2978 *interface = "org.freedesktop.systemd1.Unit",
2979 *property = "Id";
2980
2981 dbus_error_init(&error);
2982
2983 if (!(m = dbus_message_new_method_call(
2984 "org.freedesktop.systemd1",
2985 "/org/freedesktop/systemd1",
2986 "org.freedesktop.systemd1.Manager",
2987 "CreateSnapshot"))) {
2988 log_error("Could not allocate message.");
2989 return -ENOMEM;
2990 }
2991
729e3769 2992 if (strv_length(args) > 1)
7e4249b9
LP
2993 name = args[1];
2994
2995 if (!dbus_message_append_args(m,
2996 DBUS_TYPE_STRING, &name,
2997 DBUS_TYPE_BOOLEAN, &cleanup,
2998 DBUS_TYPE_INVALID)) {
2999 log_error("Could not append arguments to message.");
3000 r = -ENOMEM;
3001 goto finish;
3002 }
3003
3004 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 3005 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
3006 r = -EIO;
3007 goto finish;
3008 }
3009
3010 if (!dbus_message_get_args(reply, &error,
3011 DBUS_TYPE_OBJECT_PATH, &path,
3012 DBUS_TYPE_INVALID)) {
4cf5d675 3013 log_error("Failed to parse reply: %s", bus_error_message(&error));
7e4249b9
LP
3014 r = -EIO;
3015 goto finish;
3016 }
3017
3018 dbus_message_unref(m);
3019 if (!(m = dbus_message_new_method_call(
3020 "org.freedesktop.systemd1",
3021 path,
3022 "org.freedesktop.DBus.Properties",
3023 "Get"))) {
3024 log_error("Could not allocate message.");
3025 return -ENOMEM;
3026 }
3027
3028 if (!dbus_message_append_args(m,
3029 DBUS_TYPE_STRING, &interface,
3030 DBUS_TYPE_STRING, &property,
3031 DBUS_TYPE_INVALID)) {
3032 log_error("Could not append arguments to message.");
3033 r = -ENOMEM;
3034 goto finish;
3035 }
3036
3037 dbus_message_unref(reply);
3038 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 3039 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
3040 r = -EIO;
3041 goto finish;
3042 }
3043
3044 if (!dbus_message_iter_init(reply, &iter) ||
3045 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3046 log_error("Failed to parse reply.");
3047 r = -EIO;
3048 goto finish;
3049 }
3050
3051 dbus_message_iter_recurse(&iter, &sub);
3052
3053 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3054 log_error("Failed to parse reply.");
3055 r = -EIO;
3056 goto finish;
3057 }
3058
3059 dbus_message_iter_get_basic(&sub, &id);
0183528f
LP
3060
3061 if (!arg_quiet)
3062 puts(id);
7e4249b9
LP
3063 r = 0;
3064
3065finish:
3066 if (m)
3067 dbus_message_unref(m);
3068
3069 if (reply)
3070 dbus_message_unref(reply);
3071
3072 dbus_error_free(&error);
3073
3074 return r;
3075}
3076
729e3769 3077static int delete_snapshot(DBusConnection *bus, char **args) {
6759e7a7
LP
3078 DBusMessage *m = NULL, *reply = NULL;
3079 int r;
3080 DBusError error;
729e3769 3081 char **name;
6759e7a7
LP
3082
3083 assert(bus);
3084 assert(args);
3085
3086 dbus_error_init(&error);
3087
729e3769 3088 STRV_FOREACH(name, args+1) {
6759e7a7
LP
3089 const char *path = NULL;
3090
3091 if (!(m = dbus_message_new_method_call(
3092 "org.freedesktop.systemd1",
3093 "/org/freedesktop/systemd1",
3094 "org.freedesktop.systemd1.Manager",
3095 "GetUnit"))) {
3096 log_error("Could not allocate message.");
3097 r = -ENOMEM;
3098 goto finish;
3099 }
3100
3101 if (!dbus_message_append_args(m,
729e3769 3102 DBUS_TYPE_STRING, name,
6759e7a7
LP
3103 DBUS_TYPE_INVALID)) {
3104 log_error("Could not append arguments to message.");
3105 r = -ENOMEM;
3106 goto finish;
3107 }
3108
3109 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 3110 log_error("Failed to issue method call: %s", bus_error_message(&error));
6759e7a7
LP
3111 r = -EIO;
3112 goto finish;
3113 }
3114
3115 if (!dbus_message_get_args(reply, &error,
3116 DBUS_TYPE_OBJECT_PATH, &path,
3117 DBUS_TYPE_INVALID)) {
4cf5d675 3118 log_error("Failed to parse reply: %s", bus_error_message(&error));
6759e7a7
LP
3119 r = -EIO;
3120 goto finish;
3121 }
3122
3123 dbus_message_unref(m);
3124 if (!(m = dbus_message_new_method_call(
3125 "org.freedesktop.systemd1",
3126 path,
3127 "org.freedesktop.systemd1.Snapshot",
3128 "Remove"))) {
3129 log_error("Could not allocate message.");
3130 r = -ENOMEM;
3131 goto finish;
3132 }
3133
3134 dbus_message_unref(reply);
3135 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 3136 log_error("Failed to issue method call: %s", bus_error_message(&error));
6759e7a7
LP
3137 r = -EIO;
3138 goto finish;
3139 }
3140
3141 dbus_message_unref(m);
3142 dbus_message_unref(reply);
3143 m = reply = NULL;
3144 }
3145
3146 r = 0;
3147
3148finish:
3149 if (m)
3150 dbus_message_unref(m);
3151
3152 if (reply)
3153 dbus_message_unref(reply);
3154
3155 dbus_error_free(&error);
3156
3157 return r;
3158}
3159
729e3769 3160static int daemon_reload(DBusConnection *bus, char **args) {
7e4249b9
LP
3161 DBusMessage *m = NULL, *reply = NULL;
3162 DBusError error;
3163 int r;
3164 const char *method;
3165
3166 dbus_error_init(&error);
3167
e4b61340
LP
3168 if (arg_action == ACTION_RELOAD)
3169 method = "Reload";
3170 else if (arg_action == ACTION_REEXEC)
3171 method = "Reexecute";
3172 else {
3173 assert(arg_action == ACTION_SYSTEMCTL);
3174
3175 method =
20b09ca7
LP
3176 streq(args[0], "clear-jobs") ||
3177 streq(args[0], "cancel") ? "ClearJobs" :
3178 streq(args[0], "daemon-reexec") ? "Reexecute" :
3179 streq(args[0], "reset-failed") ? "ResetFailed" :
3180 streq(args[0], "halt") ? "Halt" :
3181 streq(args[0], "poweroff") ? "PowerOff" :
3182 streq(args[0], "reboot") ? "Reboot" :
3183 streq(args[0], "kexec") ? "KExec" :
3184 streq(args[0], "exit") ? "Exit" :
3185 /* "daemon-reload" */ "Reload";
e4b61340 3186 }
7e4249b9
LP
3187
3188 if (!(m = dbus_message_new_method_call(
3189 "org.freedesktop.systemd1",
3190 "/org/freedesktop/systemd1",
3191 "org.freedesktop.systemd1.Manager",
3192 method))) {
3193 log_error("Could not allocate message.");
3194 return -ENOMEM;
3195 }
3196
3197 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
e4b61340
LP
3198
3199 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3200 /* There's always a fallback possible for
3201 * legacy actions. */
aabd9b11 3202 r = -EADDRNOTAVAIL;
e4b61340
LP
3203 goto finish;
3204 }
3205
b23de6af
LP
3206 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3207 /* On reexecution, we expect a disconnect, not
3208 * a reply */
3209 r = 0;
3210 goto finish;
3211 }
3212
4cf5d675 3213 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
3214 r = -EIO;
3215 goto finish;
3216 }
3217
aabd9b11 3218 r = 0;
7e4249b9
LP
3219
3220finish:
3221 if (m)
3222 dbus_message_unref(m);
3223
3224 if (reply)
3225 dbus_message_unref(reply);
3226
3227 dbus_error_free(&error);
3228
3229 return r;
3230}
3231
729e3769 3232static int reset_failed(DBusConnection *bus, char **args) {
5632e374 3233 DBusMessage *m = NULL, *reply = NULL;
5632e374
LP
3234 int r;
3235 DBusError error;
729e3769 3236 char **name;
5632e374
LP
3237
3238 assert(bus);
3239 dbus_error_init(&error);
3240
729e3769
LP
3241 if (strv_length(args) <= 1)
3242 return daemon_reload(bus, args);
5632e374 3243
729e3769 3244 STRV_FOREACH(name, args+1) {
5632e374
LP
3245
3246 if (!(m = dbus_message_new_method_call(
3247 "org.freedesktop.systemd1",
3248 "/org/freedesktop/systemd1",
3249 "org.freedesktop.systemd1.Manager",
fdf20a31 3250 "ResetFailedUnit"))) {
5632e374
LP
3251 log_error("Could not allocate message.");
3252 r = -ENOMEM;
3253 goto finish;
3254 }
3255
3256 if (!dbus_message_append_args(m,
729e3769 3257 DBUS_TYPE_STRING, name,
5632e374
LP
3258 DBUS_TYPE_INVALID)) {
3259 log_error("Could not append arguments to message.");
3260 r = -ENOMEM;
3261 goto finish;
3262 }
3263
3264 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 3265 log_error("Failed to issue method call: %s", bus_error_message(&error));
5632e374
LP
3266 r = -EIO;
3267 goto finish;
3268 }
3269
3270 dbus_message_unref(m);
3271 dbus_message_unref(reply);
3272 m = reply = NULL;
3273 }
3274
3275 r = 0;
3276
3277finish:
3278 if (m)
3279 dbus_message_unref(m);
3280
3281 if (reply)
3282 dbus_message_unref(reply);
3283
3284 dbus_error_free(&error);
3285
3286 return r;
3287}
3288
729e3769 3289static int show_enviroment(DBusConnection *bus, char **args) {
7e4249b9
LP
3290 DBusMessage *m = NULL, *reply = NULL;
3291 DBusError error;
3292 DBusMessageIter iter, sub, sub2;
3293 int r;
3294 const char
3295 *interface = "org.freedesktop.systemd1.Manager",
3296 *property = "Environment";
3297
3298 dbus_error_init(&error);
3299
1968a360 3300 pager_open_if_enabled();
ec14911e 3301
7e4249b9
LP
3302 if (!(m = dbus_message_new_method_call(
3303 "org.freedesktop.systemd1",
3304 "/org/freedesktop/systemd1",
3305 "org.freedesktop.DBus.Properties",
3306 "Get"))) {
3307 log_error("Could not allocate message.");
3308 return -ENOMEM;
3309 }
3310
3311 if (!dbus_message_append_args(m,
3312 DBUS_TYPE_STRING, &interface,
3313 DBUS_TYPE_STRING, &property,
3314 DBUS_TYPE_INVALID)) {
3315 log_error("Could not append arguments to message.");
3316 r = -ENOMEM;
3317 goto finish;
3318 }
3319
3320 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 3321 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
3322 r = -EIO;
3323 goto finish;
3324 }
3325
3326 if (!dbus_message_iter_init(reply, &iter) ||
3327 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3328 log_error("Failed to parse reply.");
3329 r = -EIO;
3330 goto finish;
3331 }
3332
3333 dbus_message_iter_recurse(&iter, &sub);
3334
3335 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3336 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3337 log_error("Failed to parse reply.");
3338 r = -EIO;
3339 goto finish;
3340 }
3341
3342 dbus_message_iter_recurse(&sub, &sub2);
3343
3344 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3345 const char *text;
3346
3347 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3348 log_error("Failed to parse reply.");
3349 r = -EIO;
3350 goto finish;
3351 }
3352
3353 dbus_message_iter_get_basic(&sub2, &text);
3354 printf("%s\n", text);
3355
3356 dbus_message_iter_next(&sub2);
3357 }
3358
3359 r = 0;
3360
3361finish:
3362 if (m)
3363 dbus_message_unref(m);
3364
3365 if (reply)
3366 dbus_message_unref(reply);
3367
3368 dbus_error_free(&error);
3369
3370 return r;
3371}
3372
729e3769 3373static int set_environment(DBusConnection *bus, char **args) {
7e4249b9
LP
3374 DBusMessage *m = NULL, *reply = NULL;
3375 DBusError error;
3376 int r;
3377 const char *method;
3378 DBusMessageIter iter, sub;
729e3769 3379 char **name;
7e4249b9
LP
3380
3381 dbus_error_init(&error);
3382
3383 method = streq(args[0], "set-environment")
3384 ? "SetEnvironment"
3385 : "UnsetEnvironment";
3386
3387 if (!(m = dbus_message_new_method_call(
3388 "org.freedesktop.systemd1",
3389 "/org/freedesktop/systemd1",
3390 "org.freedesktop.systemd1.Manager",
3391 method))) {
3392
3393 log_error("Could not allocate message.");
3394 return -ENOMEM;
3395 }
3396
3397 dbus_message_iter_init_append(m, &iter);
3398
3399 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3400 log_error("Could not append arguments to message.");
3401 r = -ENOMEM;
3402 goto finish;
3403 }
3404
729e3769
LP
3405 STRV_FOREACH(name, args+1)
3406 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
7e4249b9
LP
3407 log_error("Could not append arguments to message.");
3408 r = -ENOMEM;
3409 goto finish;
3410 }
3411
3412 if (!dbus_message_iter_close_container(&iter, &sub)) {
3413 log_error("Could not append arguments to message.");
3414 r = -ENOMEM;
3415 goto finish;
3416 }
3417
3418 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 3419 log_error("Failed to issue method call: %s", bus_error_message(&error));
7e4249b9
LP
3420 r = -EIO;
3421 goto finish;
3422 }
3423
3424 r = 0;
3425
3426finish:
3427 if (m)
3428 dbus_message_unref(m);
3429
3430 if (reply)
3431 dbus_message_unref(reply);
3432
3433 dbus_error_free(&error);
3434
3435 return r;
3436}
3437
729e3769
LP
3438static int enable_sysv_units(char **args) {
3439 int r = 0;
ee5762e3 3440
729e3769
LP
3441#if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3442 const char *verb = args[0];
3443 unsigned f = 1, t = 1;
3444 LookupPaths paths;
ee5762e3 3445
729e3769
LP
3446 if (arg_scope != UNIT_FILE_SYSTEM)
3447 return 0;
ee5762e3 3448
729e3769
LP
3449 if (!streq(verb, "enable") &&
3450 !streq(verb, "disable") &&
3451 !streq(verb, "is-enabled"))
3452 return 0;
ee5762e3 3453
729e3769
LP
3454 /* Processes all SysV units, and reshuffles the array so that
3455 * afterwards only the native units remain */
ee5762e3 3456
729e3769
LP
3457 zero(paths);
3458 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3459 if (r < 0)
3460 return r;
ee5762e3 3461
729e3769 3462 r = 0;
ee5762e3 3463
729e3769
LP
3464 for (f = 1; args[f]; f++) {
3465 const char *name;
3466 char *p;
3467 bool found_native = false, found_sysv;
3468 unsigned c = 1;
3469 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3470 char **k, *l, *q = NULL;
3471 int j;
3472 pid_t pid;
3473 siginfo_t status;
ee5762e3 3474
729e3769 3475 name = args[f];
ee5762e3 3476
729e3769
LP
3477 if (!endswith(name, ".service"))
3478 continue;
ee5762e3 3479
729e3769
LP
3480 if (path_is_absolute(name))
3481 continue;
ee5762e3 3482
729e3769
LP
3483 STRV_FOREACH(k, paths.unit_path) {
3484 p = NULL;
ee5762e3 3485
729e3769
LP
3486 if (!isempty(arg_root))
3487 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3488 else
3489 asprintf(&p, "%s/%s", *k, name);
ee5762e3 3490
729e3769
LP
3491 if (!p) {
3492 log_error("No memory");
3493 r = -ENOMEM;
3494 goto finish;
3495 }
ee5762e3 3496
729e3769
LP
3497 found_native = access(p, F_OK) >= 0;
3498 free(p);
ee5762e3 3499
729e3769
LP
3500 if (found_native)
3501 break;
3502 }
ee5762e3 3503
729e3769
LP
3504 if (found_native)
3505 continue;
ee5762e3 3506
729e3769
LP
3507 p = NULL;
3508 if (!isempty(arg_root))
3509 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3510 else
3511 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3512 if (!p) {
3513 log_error("No memory");
3514 r = -ENOMEM;
3515 goto finish;
3516 }
ee5762e3 3517
729e3769
LP
3518 p[strlen(p) - sizeof(".service") + 1] = 0;
3519 found_sysv = access(p, F_OK) >= 0;
ee5762e3 3520
729e3769
LP
3521 if (!found_sysv) {
3522 free(p);
3523 continue;
71fad675
LP
3524 }
3525
729e3769
LP
3526 /* Mark this entry, so that we don't try enabling it as native unit */
3527 args[f] = (char*) "";
ee5762e3 3528
729e3769 3529 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
ee5762e3 3530
729e3769
LP
3531 if (!isempty(arg_root))
3532 argv[c++] = q = strappend("--root=", arg_root);
ee5762e3 3533
729e3769
LP
3534 argv[c++] = file_name_from_path(p);
3535 argv[c++] =
3536 streq(verb, "enable") ? "on" :
3537 streq(verb, "disable") ? "off" : "--level=5";
3538 argv[c] = NULL;
ee5762e3 3539
729e3769
LP
3540 l = strv_join((char**)argv, " ");
3541 if (!l) {
3542 log_error("No memory.");
3543 free(q);
3544 free(p);
3545 r = -ENOMEM;
3546 goto finish;
3547 }
ee5762e3 3548
729e3769
LP
3549 log_info("Executing %s", l);
3550 free(l);
ee5762e3 3551
729e3769
LP
3552 pid = fork();
3553 if (pid < 0) {
3554 log_error("Failed to fork: %m");
3555 free(p);
3556 free(q);
3557 r = -errno;
3558 goto finish;
3559 } else if (pid == 0) {
3560 /* Child */
ee5762e3 3561
729e3769
LP
3562 execv(argv[0], (char**) argv);
3563 _exit(EXIT_FAILURE);
3564 }
ee5762e3 3565
729e3769
LP
3566 free(p);
3567 free(q);
ee5762e3 3568
729e3769
LP
3569 j = wait_for_terminate(pid, &status);
3570 if (j < 0) {
3571 log_error("Failed to wait for child: %s", strerror(-r));
3572 r = j;
3573 goto finish;
3574 }
ee5762e3 3575
729e3769
LP
3576 if (status.si_code == CLD_EXITED) {
3577 if (streq(verb, "is-enabled")) {
3578 if (status.si_status == 0) {
3579 if (!arg_quiet)
3580 puts("enabled");
3581 r = 1;
3582 } else {
3583 if (!arg_quiet)
3584 puts("disabled");
3585 }
ee5762e3 3586
729e3769
LP
3587 } else if (status.si_status != 0) {
3588 r = -EINVAL;
3589 goto finish;
3590 }
3591 } else {
3592 r = -EPROTO;
3593 goto finish;
3594 }
ee5762e3
LP
3595 }
3596
729e3769
LP
3597finish:
3598 lookup_paths_free(&paths);
ee5762e3 3599
729e3769
LP
3600 /* Drop all SysV units */
3601 for (f = 1, t = 1; args[f]; f++) {
ee5762e3 3602
729e3769 3603 if (isempty(args[f]))
ee5762e3
LP
3604 continue;
3605
729e3769
LP
3606 args[t++] = args[f];
3607 }
ee5762e3 3608
729e3769 3609 args[t] = NULL;
ee5762e3 3610
729e3769
LP
3611#endif
3612 return r;
3613}
ee5762e3 3614
729e3769
LP
3615static int enable_unit(DBusConnection *bus, char **args) {
3616 const char *verb = args[0];
3617 UnitFileChange *changes = NULL;
3618 unsigned n_changes = 0, i;
3619 int carries_install_info = -1;
3620 DBusMessage *m = NULL, *reply = NULL;
3621 int r;
3622 DBusError error;
ee5762e3 3623
729e3769 3624 dbus_error_init(&error);
ee5762e3 3625
729e3769
LP
3626 r = enable_sysv_units(args);
3627 if (r < 0)
3628 return r;
ee5762e3 3629
729e3769
LP
3630 if (!bus || avoid_bus()) {
3631 if (streq(verb, "enable")) {
3632 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3633 carries_install_info = r;
3634 } else if (streq(verb, "disable"))
3635 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3636 else if (streq(verb, "reenable")) {
3637 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3638 carries_install_info = r;
3639 } else if (streq(verb, "link"))
3640 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3641 else if (streq(verb, "preset")) {
3642 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3643 carries_install_info = r;
3644 } else if (streq(verb, "mask"))
3645 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3646 else if (streq(verb, "unmask"))
3647 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3648 else
3649 assert_not_reached("Unknown verb");
ee5762e3 3650
729e3769
LP
3651 if (r < 0) {
3652 log_error("Operation failed: %s", strerror(-r));
3653 goto finish;
ee5762e3
LP
3654 }
3655
729e3769
LP
3656 for (i = 0; i < n_changes; i++) {
3657 if (changes[i].type == UNIT_FILE_SYMLINK)
3658 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3659 else
3660 log_info("rm '%s'", changes[i].path);
ee5762e3
LP
3661 }
3662
729e3769
LP
3663 } else {
3664 const char *method;
3665 bool send_force = true, expect_carries_install_info = false;
3666 dbus_bool_t a, b;
3667 DBusMessageIter iter, sub, sub2;
3668
3669 if (streq(verb, "enable")) {
3670 method = "EnableUnitFiles";
3671 expect_carries_install_info = true;
3672 } else if (streq(verb, "disable")) {
3673 method = "DisableUnitFiles";
3674 send_force = false;
3675 } else if (streq(verb, "reenable")) {
3676 method = "ReenableUnitFiles";
3677 expect_carries_install_info = true;
3678 } else if (streq(verb, "link"))
3679 method = "LinkUnitFiles";
3680 else if (streq(verb, "preset")) {
3681 method = "PresetUnitFiles";
3682 expect_carries_install_info = true;
3683 } else if (streq(verb, "mask"))
3684 method = "MaskUnitFiles";
3685 else if (streq(verb, "unmask")) {
3686 method = "UnmaskUnitFiles";
3687 send_force = false;
3688 } else
3689 assert_not_reached("Unknown verb");
3690
3691 m = dbus_message_new_method_call(
3692 "org.freedesktop.systemd1",
3693 "/org/freedesktop/systemd1",
3694 "org.freedesktop.systemd1.Manager",
3695 method);
3696 if (!m) {
ee5762e3
LP
3697 log_error("Out of memory");
3698 r = -ENOMEM;
3699 goto finish;
3700 }
3701
729e3769 3702 dbus_message_iter_init_append(m, &iter);
ee5762e3 3703
729e3769
LP
3704 r = bus_append_strv_iter(&iter, args+1);
3705 if (r < 0) {
3706 log_error("Failed to append unit files.");
ee5762e3
LP
3707 goto finish;
3708 }
3709
729e3769
LP
3710 a = arg_runtime;
3711 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3712 log_error("Failed to append runtime boolean.");
ee5762e3
LP
3713 r = -ENOMEM;
3714 goto finish;
3715 }
3716
729e3769
LP
3717 if (send_force) {
3718 b = arg_force;
be394c48 3719
729e3769
LP
3720 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3721 log_error("Failed to append force boolean.");
3722 r = -ENOMEM;
3723 goto finish;
3724 }
09adcdf7 3725 }
ee5762e3 3726
729e3769
LP
3727 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3728 if (!reply) {
3729 log_error("Failed to issue method call: %s", bus_error_message(&error));
3730 r = -EIO;
3731 goto finish;
ee5762e3
LP
3732 }
3733
729e3769
LP
3734 if (!dbus_message_iter_init(reply, &iter)) {
3735 log_error("Failed to initialize iterator.");
3736 goto finish;
3737 }
be394c48 3738
729e3769
LP
3739 if (expect_carries_install_info) {
3740 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3741 if (r < 0) {
3742 log_error("Failed to parse reply.");
3743 goto finish;
3744 }
ee5762e3 3745
729e3769 3746 carries_install_info = b;
ee5762e3
LP
3747 }
3748
729e3769
LP
3749 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3750 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3751 log_error("Failed to parse reply.");
3752 r = -EIO;
3753 goto finish;
ee5762e3
LP
3754 }
3755
729e3769
LP
3756 dbus_message_iter_recurse(&iter, &sub);
3757 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3758 const char *type, *path, *source;
c8b2e52c 3759
729e3769
LP
3760 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3761 log_error("Failed to parse reply.");
3762 r = -EIO;
3763 goto finish;
c8b2e52c
LP
3764 }
3765
729e3769 3766 dbus_message_iter_recurse(&sub, &sub2);
c8b2e52c 3767
729e3769
LP
3768 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3769 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3770 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3771 log_error("Failed to parse reply.");
3772 r = -EIO;
3773 goto finish;
c8b2e52c
LP
3774 }
3775
729e3769
LP
3776 if (streq(type, "symlink"))
3777 log_info("ln -s '%s' '%s'", source, path);
3778 else
3779 log_info("rm '%s'", path);
b77398f7 3780
729e3769
LP
3781 dbus_message_iter_next(&sub);
3782 }
b77398f7 3783
729e3769
LP
3784 /* Try to reload if enabeld */
3785 if (!arg_no_reload)
3786 r = daemon_reload(bus, args);
b647f10d 3787 }
3d3961f2 3788
729e3769
LP
3789 if (carries_install_info == 0)
3790 log_warning("Warning: unit files do not carry install information. No operation executed.");
ee5762e3 3791
729e3769
LP
3792finish:
3793 if (m)
3794 dbus_message_unref(m);
ee5762e3 3795
729e3769
LP
3796 if (reply)
3797 dbus_message_unref(reply);
ee5762e3 3798
729e3769 3799 unit_file_changes_free(changes, n_changes);
ee5762e3 3800
729e3769
LP
3801 dbus_error_free(&error);
3802 return r;
ee5762e3
LP
3803}
3804
729e3769 3805static int unit_is_enabled(DBusConnection *bus, char **args) {
ee5762e3
LP
3806 DBusError error;
3807 int r;
729e3769
LP
3808 DBusMessage *m = NULL, *reply = NULL;
3809 bool enabled;
3810 char **name;
ee5762e3
LP
3811
3812 dbus_error_init(&error);
3813
729e3769
LP
3814 r = enable_sysv_units(args);
3815 if (r < 0)
3816 return r;
ee5762e3 3817
729e3769 3818 enabled = r > 0;
ee5762e3 3819
729e3769 3820 if (!bus || avoid_bus()) {
ee5762e3 3821
729e3769
LP
3822 STRV_FOREACH(name, args+1) {
3823 UnitFileState state;
ee5762e3 3824
729e3769
LP
3825 state = unit_file_get_state(arg_scope, arg_root, *name);
3826 if (state < 0) {
3827 r = state;
3828 goto finish;
3829 }
ee5762e3 3830
729e3769
LP
3831 if (state == UNIT_FILE_ENABLED ||
3832 state == UNIT_FILE_ENABLED_RUNTIME ||
3833 state == UNIT_FILE_STATIC)
3834 enabled = true;
3835
3836 if (!arg_quiet)
3837 puts(unit_file_state_to_string(state));
71fad675 3838 }
ee5762e3 3839
729e3769
LP
3840 } else {
3841 STRV_FOREACH(name, args+1) {
3842 const char *s;
63a723f3 3843
729e3769
LP
3844 m = dbus_message_new_method_call(
3845 "org.freedesktop.systemd1",
3846 "/org/freedesktop/systemd1",
3847 "org.freedesktop.systemd1.Manager",
3848 "GetUnitFileState");
3849 if (!m) {
3850 log_error("Out of memory");
3851 r = -ENOMEM;
3852 goto finish;
3853 }
ee5762e3 3854
729e3769
LP
3855 if (!dbus_message_append_args(m,
3856 DBUS_TYPE_STRING, name,
3857 DBUS_TYPE_INVALID)) {
3858 log_error("Could not append arguments to message.");
3859 r = -ENOMEM;
3860 goto finish;
3861 }
ee5762e3 3862
729e3769
LP
3863 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3864 if (!reply) {
3865 log_error("Failed to issue method call: %s", bus_error_message(&error));
3866 r = -EIO;
3867 goto finish;
3868 }
ee5762e3 3869
729e3769
LP
3870 if (!dbus_message_get_args(reply, &error,
3871 DBUS_TYPE_STRING, &s,
3872 DBUS_TYPE_INVALID)) {
3873 log_error("Failed to parse reply: %s", bus_error_message(&error));
3874 r = -EIO;
ee5762e3
LP
3875 goto finish;
3876 }
3877
729e3769
LP
3878 dbus_message_unref(m);
3879 dbus_message_unref(reply);
3880 m = reply = NULL;
ee5762e3 3881
729e3769
LP
3882 if (streq(s, "enabled") ||
3883 streq(s, "enabled-runtime") ||
3884 streq(s, "static"))
3885 enabled = true;
3886
3887 if (!arg_quiet)
3888 puts(s);
560d8f23 3889 }
ee5762e3
LP
3890 }
3891
729e3769 3892 r = enabled ? 0 : 1;
ee5762e3 3893
729e3769
LP
3894finish:
3895 if (m)
3896 dbus_message_unref(m);
ee5762e3 3897
729e3769
LP
3898 if (reply)
3899 dbus_message_unref(reply);
ee5762e3 3900
729e3769 3901 dbus_error_free(&error);
ee5762e3
LP
3902 return r;
3903}
3904
e4b61340 3905static int systemctl_help(void) {
7e4249b9 3906
729e3769
LP
3907 pager_open_if_enabled();
3908
2e33c433 3909 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
729e3769 3910 "Query or send control commands to the systemd manager.\n\n"
8a0867d6
LP
3911 " -h --help Show this help\n"
3912 " --version Show package version\n"
3913 " -t --type=TYPE List only units of a particular type\n"
3914 " -p --property=NAME Show only properties by this name\n"
3915 " -a --all Show all units/properties, including dead/empty ones\n"
30732560 3916 " --failed Show only failed units\n"
8a0867d6
LP
3917 " --full Don't ellipsize unit names on output\n"
3918 " --fail When queueing a new job, fail if conflicting jobs are\n"
3919 " pending\n"
e67c3609
LP
3920 " --ignore-dependencies\n"
3921 " When queueing a new job, ignore all its dependencies\n"
a8f11321
LP
3922 " --kill-who=WHO Who to send signal to\n"
3923 " -s --signal=SIGNAL Which signal to send\n"
aca4c786 3924 " -H --host=[USER@]HOST\n"
a8f11321
LP
3925 " Show information for remote host\n"
3926 " -P --privileged Acquire privileges before execution\n"
8a0867d6
LP
3927 " -q --quiet Suppress output\n"
3928 " --no-block Do not wait until operation finished\n"
8a0867d6 3929 " --no-wall Don't send wall message before halt/power-off/reboot\n"
8a0867d6
LP
3930 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3931 " configuration\n"
69fc152f 3932 " --no-pager Do not pipe output into a pager\n"
501fc174
LP
3933 " --no-ask-password\n"
3934 " Do not ask for system passwords\n"
a8f11321
LP
3935 " --order When generating graph for dot, show only order\n"
3936 " --require When generating graph for dot, show only requirement\n"
3937 " --system Connect to system manager\n"
3938 " --user Connect to user service manager\n"
3939 " --global Enable/disable unit files globally\n"
8a0867d6
LP
3940 " -f --force When enabling unit files, override existing symlinks\n"
3941 " When shutting down, execute action immediately\n"
729e3769
LP
3942 " --root=PATH Enable unit files in the specified root directory\n"
3943 " --runtime Enable unit files only temporarily until next reboot\n\n"
34c4b47b 3944 "Unit Commands:\n"
729e3769 3945 " list-units List loaded units\n"
ee5762e3
LP
3946 " start [NAME...] Start (activate) one or more units\n"
3947 " stop [NAME...] Stop (deactivate) one or more units\n"
7e4249b9 3948 " reload [NAME...] Reload one or more units\n"
6f28c033
LP
3949 " restart [NAME...] Start or restart one or more units\n"
3950 " try-restart [NAME...] Restart one or more units if active\n"
3951 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3952 " otherwise start or restart\n"
3953 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3954 " otherwise restart if active\n"
7e4249b9 3955 " isolate [NAME] Start one unit and stop all others\n"
8a0867d6 3956 " kill [NAME...] Send signal to processes of a unit\n"
ee5762e3 3957 " is-active [NAME...] Check whether units are active\n"
75676b72 3958 " status [NAME...|PID...] Show runtime status of one or more units\n"
6f28c033 3959 " show [NAME...|JOB...] Show properties of one or more\n"
ee5762e3 3960 " units/jobs or the manager\n"
fdf20a31
MM
3961 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3962 " units\n"
34c4b47b
LP
3963 " load [NAME...] Load one or more units\n\n"
3964 "Unit File Commands:\n"
729e3769 3965 " list-unit-files List installed unit files\n"
ee5762e3
LP
3966 " enable [NAME...] Enable one or more unit files\n"
3967 " disable [NAME...] Disable one or more unit files\n"
729e3769
LP
3968 " reenable [NAME...] Reenable one or more unit files\n"
3969 " preset [NAME...] Enable/disable one or more unit files\n"
3970 " based on preset configuration\n"
3971 " mask [NAME...] Mask one or more units\n"
3972 " unmask [NAME...] Unmask one or more units\n"
3973 " link [PATH...] Link one or more units files into\n"
3974 " the search path\n"
34c4b47b
LP
3975 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3976 "Job Commands:\n"
48220598 3977 " list-jobs List jobs\n"
34c4b47b
LP
3978 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3979 "Status Commands:\n"
7e4249b9 3980 " dump Dump server status\n"
34c4b47b
LP
3981 " dot Dump dependency graph for dot(1)\n\n"
3982 "Snapshot Commands:\n"
7e4249b9 3983 " snapshot [NAME] Create a snapshot\n"
34c4b47b
LP
3984 " delete [NAME...] Remove one or more snapshots\n\n"
3985 "Environment Commands:\n"
7e4249b9
LP
3986 " show-environment Dump environment\n"
3987 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
34c4b47b
LP
3988 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3989 "Manager Lifecycle Commands:\n"
3990 " daemon-reload Reload systemd manager configuration\n"
3991 " daemon-reexec Reexecute systemd manager\n\n"
3992 "System Commands:\n"
20b09ca7
LP
3993 " default Enter system default mode\n"
3994 " rescue Enter system rescue mode\n"
3995 " emergency Enter system emergency mode\n"
514f4ef5 3996 " halt Shut down and halt the system\n"
2e33c433 3997 " poweroff Shut down and power-off the system\n"
514f4ef5 3998 " reboot Shut down and reboot the system\n"
20b09ca7 3999 " kexec Shut down and reboot the system with kexec\n"
af2d49f7 4000 " exit Ask for user instance termination\n",
5b6319dc 4001 program_invocation_short_name);
7e4249b9
LP
4002
4003 return 0;
4004}
4005
e4b61340
LP
4006static int halt_help(void) {
4007
2e33c433 4008 printf("%s [OPTIONS...]\n\n"
e4b61340
LP
4009 "%s the system.\n\n"
4010 " --help Show this help\n"
4011 " --halt Halt the machine\n"
4012 " -p --poweroff Switch off the machine\n"
4013 " --reboot Reboot the machine\n"
2e33c433
LP
4014 " -f --force Force immediate halt/power-off/reboot\n"
4015 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
e4b61340 4016 " -d --no-wtmp Don't write wtmp record\n"
2e33c433
LP
4017 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4018 " --no-wall Don't send wall message before halt/power-off/reboot\n",
e4b61340
LP
4019 program_invocation_short_name,
4020 arg_action == ACTION_REBOOT ? "Reboot" :
4021 arg_action == ACTION_POWEROFF ? "Power off" :
4022 "Halt");
4023
4024 return 0;
4025}
4026
4027static int shutdown_help(void) {
4028
08e4b1c5 4029 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
e4b61340
LP
4030 "Shut down the system.\n\n"
4031 " --help Show this help\n"
4032 " -H --halt Halt the machine\n"
4033 " -P --poweroff Power-off the machine\n"
4034 " -r --reboot Reboot the machine\n"
4035 " -h Equivalent to --poweroff, overriden by --halt\n"
2e33c433 4036 " -k Don't halt/power-off/reboot, just send warnings\n"
f6144808 4037 " --no-wall Don't send wall message before halt/power-off/reboot\n"
f6144808 4038 " -c Cancel a pending shutdown\n",
e4b61340
LP
4039 program_invocation_short_name);
4040
4041 return 0;
4042}
4043
4044static int telinit_help(void) {
4045
2e33c433 4046 printf("%s [OPTIONS...] {COMMAND}\n\n"
514f4ef5
LP
4047 "Send control commands to the init daemon.\n\n"
4048 " --help Show this help\n"
2e33c433 4049 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
e4b61340
LP
4050 "Commands:\n"
4051 " 0 Power-off the machine\n"
4052 " 6 Reboot the machine\n"
514f4ef5
LP
4053 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4054 " 1, s, S Enter rescue mode\n"
4055 " q, Q Reload init daemon configuration\n"
4056 " u, U Reexecute init daemon\n",
e4b61340
LP
4057 program_invocation_short_name);
4058
4059 return 0;
4060}
4061
4062static int runlevel_help(void) {
4063
2e33c433 4064 printf("%s [OPTIONS...]\n\n"
e4b61340
LP
4065 "Prints the previous and current runlevel of the init system.\n\n"
4066 " --help Show this help\n",
4067 program_invocation_short_name);
4068
4069 return 0;
4070}
4071
4072static int systemctl_parse_argv(int argc, char *argv[]) {
7e4249b9
LP
4073
4074 enum {
90d473a1 4075 ARG_FAIL = 0x100,
e67c3609 4076 ARG_IGNORE_DEPENDENCIES,
35df8f27 4077 ARG_VERSION,
af2d49f7 4078 ARG_USER,
7e4249b9 4079 ARG_SYSTEM,
ee5762e3 4080 ARG_GLOBAL,
6e905d93 4081 ARG_NO_BLOCK,
611efaac 4082 ARG_NO_PAGER,
4445a875
LP
4083 ARG_NO_WALL,
4084 ARG_ORDER,
8fe914ec 4085 ARG_REQUIRE,
be394c48 4086 ARG_ROOT,
ee5762e3 4087 ARG_FULL,
ee5762e3 4088 ARG_NO_RELOAD,
8a0867d6 4089 ARG_KILL_MODE,
501fc174 4090 ARG_KILL_WHO,
30732560 4091 ARG_NO_ASK_PASSWORD,
729e3769
LP
4092 ARG_FAILED,
4093 ARG_RUNTIME
7e4249b9
LP
4094 };
4095
4096 static const struct option options[] = {
ee5762e3 4097 { "help", no_argument, NULL, 'h' },
35df8f27 4098 { "version", no_argument, NULL, ARG_VERSION },
ee5762e3
LP
4099 { "type", required_argument, NULL, 't' },
4100 { "property", required_argument, NULL, 'p' },
4101 { "all", no_argument, NULL, 'a' },
30732560 4102 { "failed", no_argument, NULL, ARG_FAILED },
ee5762e3
LP
4103 { "full", no_argument, NULL, ARG_FULL },
4104 { "fail", no_argument, NULL, ARG_FAIL },
e67c3609 4105 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
af2d49f7 4106 { "user", no_argument, NULL, ARG_USER },
ee5762e3
LP
4107 { "system", no_argument, NULL, ARG_SYSTEM },
4108 { "global", no_argument, NULL, ARG_GLOBAL },
4109 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
0736af98 4110 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
ee5762e3
LP
4111 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4112 { "quiet", no_argument, NULL, 'q' },
4113 { "order", no_argument, NULL, ARG_ORDER },
4114 { "require", no_argument, NULL, ARG_REQUIRE },
be394c48 4115 { "root", required_argument, NULL, ARG_ROOT },
b4f27ccc 4116 { "force", no_argument, NULL, 'f' },
ee5762e3 4117 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
69fc152f 4118 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
8a0867d6
LP
4119 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4120 { "signal", required_argument, NULL, 's' },
501fc174 4121 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
a8f11321
LP
4122 { "host", required_argument, NULL, 'H' },
4123 { "privileged",no_argument, NULL, 'P' },
729e3769 4124 { "runtime", no_argument, NULL, ARG_RUNTIME },
ee5762e3 4125 { NULL, 0, NULL, 0 }
7e4249b9
LP
4126 };
4127
4128 int c;
4129
e4b61340 4130 assert(argc >= 0);
7e4249b9
LP
4131 assert(argv);
4132
501fc174
LP
4133 /* Only when running as systemctl we ask for passwords */
4134 arg_ask_password = true;
4135
a8f11321 4136 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
7e4249b9
LP
4137
4138 switch (c) {
4139
4140 case 'h':
e4b61340 4141 systemctl_help();
7e4249b9 4142 return 0;
35df8f27
LP
4143
4144 case ARG_VERSION:
4145 puts(PACKAGE_STRING);
7d568925
LP
4146 puts(DISTRIBUTION);
4147 puts(SYSTEMD_FEATURES);
35df8f27 4148 return 0;
7e4249b9
LP
4149
4150 case 't':
4151 arg_type = optarg;
4152 break;
4153
ea4a240d
LP
4154 case 'p': {
4155 char **l;
4156
4157 if (!(l = strv_append(arg_property, optarg)))
4158 return -ENOMEM;
4159
4160 strv_free(arg_property);
4161 arg_property = l;
48220598
LP
4162
4163 /* If the user asked for a particular
4164 * property, show it to him, even if it is
4165 * empty. */
4166 arg_all = true;
4167 break;
ea4a240d 4168 }
48220598 4169
7e4249b9
LP
4170 case 'a':
4171 arg_all = true;
4172 break;
4173
90d473a1 4174 case ARG_FAIL:
e67c3609
LP
4175 arg_job_mode = "fail";
4176 break;
4177
4178 case ARG_IGNORE_DEPENDENCIES:
4179 arg_job_mode = "ignore-dependencies";
7e4249b9
LP
4180 break;
4181
af2d49f7 4182 case ARG_USER:
729e3769 4183 arg_scope = UNIT_FILE_USER;
7e4249b9
LP
4184 break;
4185
4186 case ARG_SYSTEM:
729e3769
LP
4187 arg_scope = UNIT_FILE_SYSTEM;
4188 break;
4189
4190 case ARG_GLOBAL:
4191 arg_scope = UNIT_FILE_GLOBAL;
7e4249b9
LP
4192 break;
4193
6e905d93
LP
4194 case ARG_NO_BLOCK:
4195 arg_no_block = true;
7e4249b9
LP
4196 break;
4197
611efaac
LP
4198 case ARG_NO_PAGER:
4199 arg_no_pager = true;
4200 break;
0736af98 4201
514f4ef5
LP
4202 case ARG_NO_WALL:
4203 arg_no_wall = true;
4204 break;
4205
4445a875
LP
4206 case ARG_ORDER:
4207 arg_dot = DOT_ORDER;
4208 break;
4209
4210 case ARG_REQUIRE:
4211 arg_dot = DOT_REQUIRE;
4212 break;
4213
be394c48
FC
4214 case ARG_ROOT:
4215 arg_root = optarg;
4216 break;
4217
8fe914ec
LP
4218 case ARG_FULL:
4219 arg_full = true;
4220 break;
4221
30732560
LP
4222 case ARG_FAILED:
4223 arg_failed = true;
4224 break;
4225
0183528f
LP
4226 case 'q':
4227 arg_quiet = true;
4228 break;
4229
b4f27ccc 4230 case 'f':
ee5762e3
LP
4231 arg_force = true;
4232 break;
4233
4234 case ARG_NO_RELOAD:
4235 arg_no_reload = true;
4236 break;
4237
8a0867d6
LP
4238 case ARG_KILL_WHO:
4239 arg_kill_who = optarg;
4240 break;
4241
4242 case ARG_KILL_MODE:
4243 arg_kill_mode = optarg;
4244 break;
4245
4246 case 's':
4247 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4248 log_error("Failed to parse signal string %s.", optarg);
4249 return -EINVAL;
4250 }
4251 break;
4252
501fc174
LP
4253 case ARG_NO_ASK_PASSWORD:
4254 arg_ask_password = false;
4255 break;
4256
a8f11321
LP
4257 case 'P':
4258 arg_transport = TRANSPORT_POLKIT;
4259 break;
4260
4261 case 'H':
4262 arg_transport = TRANSPORT_SSH;
4263 arg_host = optarg;
4264 break;
4265
729e3769
LP
4266 case ARG_RUNTIME:
4267 arg_runtime = true;
4268 break;
4269
7e4249b9
LP
4270 case '?':
4271 return -EINVAL;
4272
4273 default:
4274 log_error("Unknown option code %c", c);
4275 return -EINVAL;
4276 }
4277 }
4278
729e3769 4279 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
a8f11321
LP
4280 log_error("Cannot access user instance remotely.");
4281 return -EINVAL;
4282 }
4283
7e4249b9
LP
4284 return 1;
4285}
4286
e4b61340
LP
4287static int halt_parse_argv(int argc, char *argv[]) {
4288
4289 enum {
4290 ARG_HELP = 0x100,
4291 ARG_HALT,
514f4ef5
LP
4292 ARG_REBOOT,
4293 ARG_NO_WALL
e4b61340
LP
4294 };
4295
4296 static const struct option options[] = {
4297 { "help", no_argument, NULL, ARG_HELP },
4298 { "halt", no_argument, NULL, ARG_HALT },
4299 { "poweroff", no_argument, NULL, 'p' },
4300 { "reboot", no_argument, NULL, ARG_REBOOT },
4301 { "force", no_argument, NULL, 'f' },
4302 { "wtmp-only", no_argument, NULL, 'w' },
4303 { "no-wtmp", no_argument, NULL, 'd' },
4304 { "no-sync", no_argument, NULL, 'n' },
514f4ef5 4305 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
4306 { NULL, 0, NULL, 0 }
4307 };
4308
4309 int c, runlevel;
4310
4311 assert(argc >= 0);
4312 assert(argv);
4313
4314 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4315 if (runlevel == '0' || runlevel == '6')
4316 arg_immediate = true;
4317
4318 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4319 switch (c) {
4320
4321 case ARG_HELP:
4322 halt_help();
4323 return 0;
4324
4325 case ARG_HALT:
4326 arg_action = ACTION_HALT;
4327 break;
4328
4329 case 'p':
a042efad
MS
4330 if (arg_action != ACTION_REBOOT)
4331 arg_action = ACTION_POWEROFF;
e4b61340
LP
4332 break;
4333
4334 case ARG_REBOOT:
4335 arg_action = ACTION_REBOOT;
4336 break;
4337
4338 case 'f':
4339 arg_immediate = true;
4340 break;
4341
4342 case 'w':
4343 arg_dry = true;
4344 break;
4345
4346 case 'd':
4347 arg_no_wtmp = true;
4348 break;
4349
4350 case 'n':
4351 arg_no_sync = true;
4352 break;
4353
514f4ef5
LP
4354 case ARG_NO_WALL:
4355 arg_no_wall = true;
4356 break;
4357
e4b61340
LP
4358 case 'i':
4359 case 'h':
4360 /* Compatibility nops */
4361 break;
4362
4363 case '?':
4364 return -EINVAL;
4365
4366 default:
4367 log_error("Unknown option code %c", c);
4368 return -EINVAL;
4369 }
4370 }
4371
4372 if (optind < argc) {
4373 log_error("Too many arguments.");
4374 return -EINVAL;
4375 }
4376
4377 return 1;
4378}
4379
f6144808
LP
4380static int parse_time_spec(const char *t, usec_t *_u) {
4381 assert(t);
4382 assert(_u);
4383
4384 if (streq(t, "now"))
4385 *_u = 0;
1a639877 4386 else if (!strchr(t, ':')) {
f6144808
LP
4387 uint64_t u;
4388
1a639877 4389 if (safe_atou64(t, &u) < 0)
f6144808
LP
4390 return -EINVAL;
4391
4392 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4393 } else {
4394 char *e = NULL;
4395 long hour, minute;
4396 struct tm tm;
4397 time_t s;
4398 usec_t n;
4399
4400 errno = 0;
4401 hour = strtol(t, &e, 10);
4402 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4403 return -EINVAL;
4404
4405 minute = strtol(e+1, &e, 10);
4406 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4407 return -EINVAL;
4408
4409 n = now(CLOCK_REALTIME);
08e4b1c5
LP
4410 s = (time_t) (n / USEC_PER_SEC);
4411
4412 zero(tm);
f6144808
LP
4413 assert_se(localtime_r(&s, &tm));
4414
4415 tm.tm_hour = (int) hour;
4416 tm.tm_min = (int) minute;
08e4b1c5 4417 tm.tm_sec = 0;
f6144808
LP
4418
4419 assert_se(s = mktime(&tm));
4420
4421 *_u = (usec_t) s * USEC_PER_SEC;
4422
4423 while (*_u <= n)
4424 *_u += USEC_PER_DAY;
4425 }
4426
4427 return 0;
4428}
4429
e4b61340
LP
4430static int shutdown_parse_argv(int argc, char *argv[]) {
4431
4432 enum {
4433 ARG_HELP = 0x100,
514f4ef5 4434 ARG_NO_WALL
e4b61340
LP
4435 };
4436
4437 static const struct option options[] = {
4438 { "help", no_argument, NULL, ARG_HELP },
4439 { "halt", no_argument, NULL, 'H' },
4440 { "poweroff", no_argument, NULL, 'P' },
4441 { "reboot", no_argument, NULL, 'r' },
514f4ef5 4442 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
4443 { NULL, 0, NULL, 0 }
4444 };
4445
f6144808 4446 int c, r;
e4b61340
LP
4447
4448 assert(argc >= 0);
4449 assert(argv);
4450
f6144808 4451 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
e4b61340
LP
4452 switch (c) {
4453
4454 case ARG_HELP:
4455 shutdown_help();
4456 return 0;
4457
4458 case 'H':
4459 arg_action = ACTION_HALT;
4460 break;
4461
4462 case 'P':
4463 arg_action = ACTION_POWEROFF;
4464 break;
4465
4466 case 'r':
5622dde3
KS
4467 if (kexec_loaded())
4468 arg_action = ACTION_KEXEC;
4469 else
4470 arg_action = ACTION_REBOOT;
e4b61340
LP
4471 break;
4472
4473 case 'h':
4474 if (arg_action != ACTION_HALT)
4475 arg_action = ACTION_POWEROFF;
4476 break;
4477
4478 case 'k':
4479 arg_dry = true;
4480 break;
4481
514f4ef5
LP
4482 case ARG_NO_WALL:
4483 arg_no_wall = true;
4484 break;
4485
e4b61340
LP
4486 case 't':
4487 case 'a':
4488 /* Compatibility nops */
4489 break;
4490
f6144808
LP
4491 case 'c':
4492 arg_action = ACTION_CANCEL_SHUTDOWN;
4493 break;
4494
e4b61340
LP
4495 case '?':
4496 return -EINVAL;
4497
4498 default:
4499 log_error("Unknown option code %c", c);
4500 return -EINVAL;
4501 }
4502 }
4503
6b5ad000 4504 if (argc > optind) {
f6144808
LP
4505 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4506 log_error("Failed to parse time specification: %s", argv[optind]);
4507 return r;
4508 }
6b5ad000 4509 } else
08e4b1c5 4510 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
442b9094 4511
f6144808 4512 /* We skip the time argument */
e4b61340
LP
4513 if (argc > optind + 1)
4514 arg_wall = argv + optind + 1;
4515
4516 optind = argc;
4517
4518 return 1;
e4b61340
LP
4519}
4520
4521static int telinit_parse_argv(int argc, char *argv[]) {
4522
4523 enum {
4524 ARG_HELP = 0x100,
514f4ef5 4525 ARG_NO_WALL
e4b61340
LP
4526 };
4527
4528 static const struct option options[] = {
4529 { "help", no_argument, NULL, ARG_HELP },
514f4ef5 4530 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
4531 { NULL, 0, NULL, 0 }
4532 };
4533
4534 static const struct {
4535 char from;
4536 enum action to;
4537 } table[] = {
4538 { '0', ACTION_POWEROFF },
4539 { '6', ACTION_REBOOT },
ef2f1067 4540 { '1', ACTION_RESCUE },
e4b61340
LP
4541 { '2', ACTION_RUNLEVEL2 },
4542 { '3', ACTION_RUNLEVEL3 },
4543 { '4', ACTION_RUNLEVEL4 },
4544 { '5', ACTION_RUNLEVEL5 },
4545 { 's', ACTION_RESCUE },
4546 { 'S', ACTION_RESCUE },
4547 { 'q', ACTION_RELOAD },
4548 { 'Q', ACTION_RELOAD },
4549 { 'u', ACTION_REEXEC },
4550 { 'U', ACTION_REEXEC }
4551 };
4552
4553 unsigned i;
4554 int c;
4555
4556 assert(argc >= 0);
4557 assert(argv);
4558
4559 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4560 switch (c) {
4561
4562 case ARG_HELP:
4563 telinit_help();
4564 return 0;
4565
514f4ef5
LP
4566 case ARG_NO_WALL:
4567 arg_no_wall = true;
4568 break;
4569
e4b61340
LP
4570 case '?':
4571 return -EINVAL;
4572
4573 default:
4574 log_error("Unknown option code %c", c);
4575 return -EINVAL;
4576 }
4577 }
4578
4579 if (optind >= argc) {
2f02ce40 4580 telinit_help();
e4b61340
LP
4581 return -EINVAL;
4582 }
4583
4584 if (optind + 1 < argc) {
4585 log_error("Too many arguments.");
4586 return -EINVAL;
4587 }
4588
4589 if (strlen(argv[optind]) != 1) {
4590 log_error("Expected single character argument.");
4591 return -EINVAL;
4592 }
4593
4594 for (i = 0; i < ELEMENTSOF(table); i++)
4595 if (table[i].from == argv[optind][0])
4596 break;
4597
4598 if (i >= ELEMENTSOF(table)) {
4599 log_error("Unknown command %s.", argv[optind]);
4600 return -EINVAL;
4601 }
4602
4603 arg_action = table[i].to;
4604
4605 optind ++;
4606
4607 return 1;
4608}
4609
4610static int runlevel_parse_argv(int argc, char *argv[]) {
4611
4612 enum {
4613 ARG_HELP = 0x100,
4614 };
4615
4616 static const struct option options[] = {
4617 { "help", no_argument, NULL, ARG_HELP },
4618 { NULL, 0, NULL, 0 }
4619 };
4620
4621 int c;
4622
4623 assert(argc >= 0);
4624 assert(argv);
4625
4626 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4627 switch (c) {
4628
4629 case ARG_HELP:
4630 runlevel_help();
4631 return 0;
4632
4633 case '?':
4634 return -EINVAL;
4635
4636 default:
4637 log_error("Unknown option code %c", c);
4638 return -EINVAL;
4639 }
4640 }
4641
4642 if (optind < argc) {
4643 log_error("Too many arguments.");
4644 return -EINVAL;
4645 }
4646
4647 return 1;
4648}
4649
4650static int parse_argv(int argc, char *argv[]) {
4651 assert(argc >= 0);
4652 assert(argv);
4653
4654 if (program_invocation_short_name) {
4655
4656 if (strstr(program_invocation_short_name, "halt")) {
4657 arg_action = ACTION_HALT;
4658 return halt_parse_argv(argc, argv);
4659 } else if (strstr(program_invocation_short_name, "poweroff")) {
4660 arg_action = ACTION_POWEROFF;
4661 return halt_parse_argv(argc, argv);
4662 } else if (strstr(program_invocation_short_name, "reboot")) {
5622dde3
KS
4663 if (kexec_loaded())
4664 arg_action = ACTION_KEXEC;
4665 else
4666 arg_action = ACTION_REBOOT;
e4b61340
LP
4667 return halt_parse_argv(argc, argv);
4668 } else if (strstr(program_invocation_short_name, "shutdown")) {
4669 arg_action = ACTION_POWEROFF;
4670 return shutdown_parse_argv(argc, argv);
4671 } else if (strstr(program_invocation_short_name, "init")) {
d5ca5f11
LP
4672
4673 if (sd_booted() > 0) {
4674 arg_action = ACTION_INVALID;
4675 return telinit_parse_argv(argc, argv);
4676 } else {
4677 /* Hmm, so some other init system is
4678 * running, we need to forward this
4679 * request to it. For now we simply
4680 * guess that it is Upstart. */
4681
4682 execv("/lib/upstart/telinit", argv);
4683
4684 log_error("Couldn't find an alternative telinit implementation to spawn.");
4685 return -EIO;
4686 }
4687
e4b61340
LP
4688 } else if (strstr(program_invocation_short_name, "runlevel")) {
4689 arg_action = ACTION_RUNLEVEL;
4690 return runlevel_parse_argv(argc, argv);
4691 }
4692 }
4693
4694 arg_action = ACTION_SYSTEMCTL;
4695 return systemctl_parse_argv(argc, argv);
4696}
4697
d55ae9e6 4698static int action_to_runlevel(void) {
eb22ac37
LP
4699
4700 static const char table[_ACTION_MAX] = {
4701 [ACTION_HALT] = '0',
4702 [ACTION_POWEROFF] = '0',
4703 [ACTION_REBOOT] = '6',
4704 [ACTION_RUNLEVEL2] = '2',
4705 [ACTION_RUNLEVEL3] = '3',
4706 [ACTION_RUNLEVEL4] = '4',
4707 [ACTION_RUNLEVEL5] = '5',
4708 [ACTION_RESCUE] = '1'
4709 };
4710
d55ae9e6
LP
4711 assert(arg_action < _ACTION_MAX);
4712
4713 return table[arg_action];
4714}
4715
f1c5860b 4716static int talk_upstart(void) {
d55ae9e6
LP
4717 DBusMessage *m = NULL, *reply = NULL;
4718 DBusError error;
4719 int previous, rl, r;
4720 char
4721 env1_buf[] = "RUNLEVEL=X",
4722 env2_buf[] = "PREVLEVEL=X";
4723 char *env1 = env1_buf, *env2 = env2_buf;
4724 const char *emit = "runlevel";
4725 dbus_bool_t b_false = FALSE;
4726 DBusMessageIter iter, sub;
f1c5860b 4727 DBusConnection *bus;
d55ae9e6
LP
4728
4729 dbus_error_init(&error);
4730
4731 if (!(rl = action_to_runlevel()))
4732 return 0;
4733
4734 if (utmp_get_runlevel(&previous, NULL) < 0)
4735 previous = 'N';
4736
b574246b 4737 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
f1c5860b
LP
4738 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4739 r = 0;
4740 goto finish;
4741 }
4742
4cf5d675 4743 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
f1c5860b
LP
4744 r = -EIO;
4745 goto finish;
4746 }
4747
4748 if ((r = bus_check_peercred(bus)) < 0) {
4749 log_error("Failed to verify owner of bus.");
4750 goto finish;
4751 }
4752
d55ae9e6
LP
4753 if (!(m = dbus_message_new_method_call(
4754 "com.ubuntu.Upstart",
4755 "/com/ubuntu/Upstart",
4756 "com.ubuntu.Upstart0_6",
4757 "EmitEvent"))) {
4758
4759 log_error("Could not allocate message.");
f1c5860b
LP
4760 r = -ENOMEM;
4761 goto finish;
d55ae9e6
LP
4762 }
4763
4764 dbus_message_iter_init_append(m, &iter);
4765
4766 env1_buf[sizeof(env1_buf)-2] = rl;
4767 env2_buf[sizeof(env2_buf)-2] = previous;
4768
4769 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4770 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4771 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4772 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4773 !dbus_message_iter_close_container(&iter, &sub) ||
4774 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4775 log_error("Could not append arguments to message.");
4776 r = -ENOMEM;
4777 goto finish;
4778 }
4779
4780 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4781
4782 if (error_is_no_service(&error)) {
aabd9b11 4783 r = -EADDRNOTAVAIL;
d55ae9e6
LP
4784 goto finish;
4785 }
4786
4cf5d675 4787 log_error("Failed to issue method call: %s", bus_error_message(&error));
d55ae9e6
LP
4788 r = -EIO;
4789 goto finish;
4790 }
4791
0a55b298 4792 r = 1;
d55ae9e6
LP
4793
4794finish:
4795 if (m)
4796 dbus_message_unref(m);
4797
4798 if (reply)
4799 dbus_message_unref(reply);
4800
b574246b 4801 if (bus) {
5d452f9c 4802 dbus_connection_flush(bus);
b574246b 4803 dbus_connection_close(bus);
f1c5860b 4804 dbus_connection_unref(bus);
b574246b 4805 }
f1c5860b 4806
d55ae9e6
LP
4807 dbus_error_free(&error);
4808
4809 return r;
4810}
4811
4812static int talk_initctl(void) {
eb22ac37
LP
4813 struct init_request request;
4814 int r, fd;
d55ae9e6 4815 char rl;
eb22ac37 4816
d55ae9e6 4817 if (!(rl = action_to_runlevel()))
eb22ac37
LP
4818 return 0;
4819
4820 zero(request);
4821 request.magic = INIT_MAGIC;
4822 request.sleeptime = 0;
4823 request.cmd = INIT_CMD_RUNLVL;
d55ae9e6
LP
4824 request.runlevel = rl;
4825
4826 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4827
4828 if (errno == ENOENT)
4829 return 0;
eb22ac37 4830
d55ae9e6 4831 log_error("Failed to open "INIT_FIFO": %m");
eb22ac37 4832 return -errno;
d55ae9e6 4833 }
eb22ac37 4834
d55ae9e6 4835 errno = 0;
eb22ac37
LP
4836 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4837 close_nointr_nofail(fd);
4838
d55ae9e6
LP
4839 if (r < 0) {
4840 log_error("Failed to write to "INIT_FIFO": %m");
eb22ac37 4841 return errno ? -errno : -EIO;
d55ae9e6 4842 }
eb22ac37
LP
4843
4844 return 1;
e4b61340
LP
4845}
4846
ee5762e3 4847static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
7e4249b9 4848
7e4249b9
LP
4849 static const struct {
4850 const char* verb;
4851 const enum {
4852 MORE,
4853 LESS,
4854 EQUAL
4855 } argc_cmp;
4856 const int argc;
729e3769 4857 int (* const dispatch)(DBusConnection *bus, char **args);
7e4249b9 4858 } verbs[] = {
ee5762e3 4859 { "list-units", LESS, 1, list_units },
729e3769 4860 { "list-unit-files", EQUAL, 1, list_unit_files },
ee5762e3
LP
4861 { "list-jobs", EQUAL, 1, list_jobs },
4862 { "clear-jobs", EQUAL, 1, daemon_reload },
4863 { "load", MORE, 2, load_unit },
4864 { "cancel", MORE, 2, cancel_job },
4865 { "start", MORE, 2, start_unit },
4866 { "stop", MORE, 2, start_unit },
a76f7be2 4867 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
ee5762e3
LP
4868 { "reload", MORE, 2, start_unit },
4869 { "restart", MORE, 2, start_unit },
4870 { "try-restart", MORE, 2, start_unit },
4871 { "reload-or-restart", MORE, 2, start_unit },
4872 { "reload-or-try-restart", MORE, 2, start_unit },
4873 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
64e5f1b7 4874 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
ee5762e3
LP
4875 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4876 { "isolate", EQUAL, 2, start_unit },
8a0867d6 4877 { "kill", MORE, 2, kill_unit },
ee5762e3
LP
4878 { "is-active", MORE, 2, check_unit },
4879 { "check", MORE, 2, check_unit },
4880 { "show", MORE, 1, show },
4881 { "status", MORE, 2, show },
ee5762e3
LP
4882 { "dump", EQUAL, 1, dump },
4883 { "dot", EQUAL, 1, dot },
4884 { "snapshot", LESS, 2, snapshot },
4885 { "delete", MORE, 2, delete_snapshot },
4886 { "daemon-reload", EQUAL, 1, daemon_reload },
4887 { "daemon-reexec", EQUAL, 1, daemon_reload },
ee5762e3
LP
4888 { "show-environment", EQUAL, 1, show_enviroment },
4889 { "set-environment", MORE, 2, set_environment },
4890 { "unset-environment", MORE, 2, set_environment },
4891 { "halt", EQUAL, 1, start_special },
4892 { "poweroff", EQUAL, 1, start_special },
4893 { "reboot", EQUAL, 1, start_special },
20b09ca7 4894 { "kexec", EQUAL, 1, start_special },
ee5762e3
LP
4895 { "default", EQUAL, 1, start_special },
4896 { "rescue", EQUAL, 1, start_special },
4897 { "emergency", EQUAL, 1, start_special },
20b09ca7 4898 { "exit", EQUAL, 1, start_special },
fdf20a31 4899 { "reset-failed", MORE, 1, reset_failed },
ee5762e3
LP
4900 { "enable", MORE, 2, enable_unit },
4901 { "disable", MORE, 2, enable_unit },
729e3769
LP
4902 { "is-enabled", MORE, 2, unit_is_enabled },
4903 { "reenable", MORE, 2, enable_unit },
4904 { "preset", MORE, 2, enable_unit },
4905 { "mask", MORE, 2, enable_unit },
4906 { "unmask", MORE, 2, enable_unit },
4907 { "link", MORE, 2, enable_unit }
7e4249b9
LP
4908 };
4909
e4b61340 4910 int left;
7e4249b9 4911 unsigned i;
7e4249b9 4912
e4b61340
LP
4913 assert(argc >= 0);
4914 assert(argv);
ee5762e3 4915 assert(error);
7e4249b9
LP
4916
4917 left = argc - optind;
4918
4919 if (left <= 0)
4920 /* Special rule: no arguments means "list-units" */
4921 i = 0;
4922 else {
0183528f
LP
4923 if (streq(argv[optind], "help")) {
4924 systemctl_help();
4925 return 0;
4926 }
4927
7e4249b9
LP
4928 for (i = 0; i < ELEMENTSOF(verbs); i++)
4929 if (streq(argv[optind], verbs[i].verb))
4930 break;
4931
4932 if (i >= ELEMENTSOF(verbs)) {
4933 log_error("Unknown operation %s", argv[optind]);
e4b61340 4934 return -EINVAL;
7e4249b9
LP
4935 }
4936 }
4937
4938 switch (verbs[i].argc_cmp) {
4939
4940 case EQUAL:
4941 if (left != verbs[i].argc) {
4942 log_error("Invalid number of arguments.");
e4b61340 4943 return -EINVAL;
7e4249b9
LP
4944 }
4945
4946 break;
4947
4948 case MORE:
4949 if (left < verbs[i].argc) {
4950 log_error("Too few arguments.");
e4b61340 4951 return -EINVAL;
7e4249b9
LP
4952 }
4953
4954 break;
4955
4956 case LESS:
4957 if (left > verbs[i].argc) {
4958 log_error("Too many arguments.");
e4b61340 4959 return -EINVAL;
7e4249b9
LP
4960 }
4961
4962 break;
4963
4964 default:
4965 assert_not_reached("Unknown comparison operator.");
4966 }
4967
ee5762e3
LP
4968 /* Require a bus connection for all operations but
4969 * enable/disable */
729e3769
LP
4970 if (!streq(verbs[i].verb, "enable") &&
4971 !streq(verbs[i].verb, "disable") &&
4972 !streq(verbs[i].verb, "is-enable") &&
4973 !streq(verbs[i].verb, "reenable") &&
4974 !streq(verbs[i].verb, "preset") &&
4975 !streq(verbs[i].verb, "mask") &&
4976 !streq(verbs[i].verb, "unmask") &&
4977 !streq(verbs[i].verb, "link")) {
82e23ddd
LP
4978
4979 if (running_in_chroot() > 0) {
4980 log_info("Running in chroot, ignoring request.");
4981 return 0;
4982 }
4983
729e3769 4984 if (!bus && !avoid_bus()) {
82e23ddd
LP
4985 log_error("Failed to get D-Bus connection: %s", error->message);
4986 return -EIO;
4987 }
ee5762e3
LP
4988 }
4989
729e3769 4990 return verbs[i].dispatch(bus, argv + optind);
e4b61340
LP
4991}
4992
52c00215 4993static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
f6144808
LP
4994 int fd = -1;
4995 struct msghdr msghdr;
4996 struct iovec iovec;
4997 union sockaddr_union sockaddr;
f6144808
LP
4998 struct shutdownd_command c;
4999
5000 zero(c);
5001 c.elapse = t;
5002 c.mode = mode;
52c00215 5003 c.dry_run = dry_run;
9be9828c
LP
5004 c.warn_wall = warn;
5005
5006 if (message)
5007 strncpy(c.wall_message, message, sizeof(c.wall_message));
f6144808
LP
5008
5009 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5010 return -errno;
5011
5012 zero(sockaddr);
5013 sockaddr.sa.sa_family = AF_UNIX;
5014 sockaddr.un.sun_path[0] = 0;
2b583ce6 5015 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
f6144808
LP
5016
5017 zero(iovec);
5018 iovec.iov_base = (char*) &c;
5019 iovec.iov_len = sizeof(c);
5020
f6144808
LP
5021 zero(msghdr);
5022 msghdr.msg_name = &sockaddr;
2b583ce6 5023 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
f6144808
LP
5024
5025 msghdr.msg_iov = &iovec;
5026 msghdr.msg_iovlen = 1;
f6144808
LP
5027
5028 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5029 close_nointr_nofail(fd);
5030 return -errno;
5031 }
5032
5033 close_nointr_nofail(fd);
5034 return 0;
5035}
5036
e4b61340 5037static int reload_with_fallback(DBusConnection *bus) {
e4b61340
LP
5038
5039 if (bus) {
5040 /* First, try systemd via D-Bus. */
d76702a7 5041 if (daemon_reload(bus, NULL) >= 0)
e4b61340
LP
5042 return 0;
5043 }
5044
5045 /* Nothing else worked, so let's try signals */
5046 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5047
5048 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5049 log_error("kill() failed: %m");
5050 return -errno;
5051 }
5052
5053 return 0;
5054}
5055
5056static int start_with_fallback(DBusConnection *bus) {
e4b61340
LP
5057
5058 if (bus) {
5059 /* First, try systemd via D-Bus. */
729e3769 5060 if (start_unit(bus, NULL) >= 0)
983d9c90 5061 goto done;
e4b61340
LP
5062 }
5063
ec7f7f20
LP
5064 /* Hmm, talking to systemd via D-Bus didn't work. Then
5065 * let's try to talk to Upstart via D-Bus. */
e364ad06 5066 if (talk_upstart() > 0)
ec7f7f20
LP
5067 goto done;
5068
e4b61340
LP
5069 /* Nothing else worked, so let's try
5070 * /dev/initctl */
fbc43921 5071 if (talk_initctl() > 0)
983d9c90 5072 goto done;
d55ae9e6
LP
5073
5074 log_error("Failed to talk to init daemon.");
5075 return -EIO;
983d9c90
LP
5076
5077done:
5078 warn_wall(arg_action);
5079 return 0;
e4b61340
LP
5080}
5081
5082static int halt_main(DBusConnection *bus) {
5083 int r;
5084
bc8c2f5c 5085 if (geteuid() != 0) {
cc8a7a61 5086 log_error("Must be root.");
bc8c2f5c
LP
5087 return -EPERM;
5088 }
5089
f6144808 5090 if (arg_when > 0) {
9be9828c 5091 char *m;
08e4b1c5 5092 char date[FORMAT_TIMESTAMP_MAX];
9be9828c
LP
5093
5094 m = strv_join(arg_wall, " ");
5095 r = send_shutdownd(arg_when,
5096 arg_action == ACTION_HALT ? 'H' :
5097 arg_action == ACTION_POWEROFF ? 'P' :
5098 'r',
52c00215 5099 arg_dry,
9be9828c
LP
5100 !arg_no_wall,
5101 m);
5102 free(m);
5103
5104 if (r < 0)
f6144808 5105 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
08e4b1c5
LP
5106 else {
5107 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5108 format_timestamp(date, sizeof(date), arg_when));
f6144808 5109 return 0;
08e4b1c5 5110 }
f6144808
LP
5111 }
5112
d47b555b 5113 if (!arg_dry && !arg_immediate)
e4b61340
LP
5114 return start_with_fallback(bus);
5115
d90e1a30
LP
5116 if (!arg_no_wtmp) {
5117 if (sd_booted() > 0)
5118 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5119 else if ((r = utmp_put_shutdown(0)) < 0)
e4b61340 5120 log_warning("Failed to write utmp record: %s", strerror(-r));
d90e1a30 5121 }
e4b61340
LP
5122
5123 if (!arg_no_sync)
5124 sync();
5125
5126 if (arg_dry)
5127 return 0;
5128
5129 /* Make sure C-A-D is handled by the kernel from this
5130 * point on... */
5131 reboot(RB_ENABLE_CAD);
5132
5133 switch (arg_action) {
5134
5135 case ACTION_HALT:
3059b1c1 5136 log_info("Halting.");
e4b61340
LP
5137 reboot(RB_HALT_SYSTEM);
5138 break;
5139
5140 case ACTION_POWEROFF:
3059b1c1 5141 log_info("Powering off.");
e4b61340
LP
5142 reboot(RB_POWER_OFF);
5143 break;
5144
5145 case ACTION_REBOOT:
3059b1c1 5146 log_info("Rebooting.");
e4b61340
LP
5147 reboot(RB_AUTOBOOT);
5148 break;
5149
5150 default:
5151 assert_not_reached("Unknown halt action.");
5152 }
5153
5154 /* We should never reach this. */
5155 return -ENOSYS;
5156}
5157
5158static int runlevel_main(void) {
5159 int r, runlevel, previous;
5160
729e3769
LP
5161 r = utmp_get_runlevel(&runlevel, &previous);
5162 if (r < 0) {
5163 puts("unknown");
e4b61340
LP
5164 return r;
5165 }
5166
5167 printf("%c %c\n",
5168 previous <= 0 ? 'N' : previous,
5169 runlevel <= 0 ? 'N' : runlevel);
5170
5171 return 0;
5172}
5173
5174int main(int argc, char*argv[]) {
22f4096c 5175 int r, retval = EXIT_FAILURE;
e4b61340
LP
5176 DBusConnection *bus = NULL;
5177 DBusError error;
5178
5179 dbus_error_init(&error);
5180
5181 log_parse_environment();
2396fb04 5182 log_open();
e4b61340
LP
5183
5184 if ((r = parse_argv(argc, argv)) < 0)
5185 goto finish;
5186 else if (r == 0) {
22f4096c 5187 retval = EXIT_SUCCESS;
7e4249b9
LP
5188 goto finish;
5189 }
5190
e4b61340
LP
5191 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5192 * let's shortcut this */
5193 if (arg_action == ACTION_RUNLEVEL) {
22f4096c
LP
5194 r = runlevel_main();
5195 retval = r < 0 ? EXIT_FAILURE : r;
e4b61340
LP
5196 goto finish;
5197 }
5198
82e23ddd
LP
5199 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5200 log_info("Running in chroot, ignoring request.");
5201 retval = 0;
5202 goto finish;
5203 }
5204
729e3769
LP
5205 if (!avoid_bus()) {
5206 if (arg_transport == TRANSPORT_NORMAL)
5207 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5208 else if (arg_transport == TRANSPORT_POLKIT) {
5209 bus_connect_system_polkit(&bus, &error);
5210 private_bus = false;
5211 } else if (arg_transport == TRANSPORT_SSH) {
5212 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5213 private_bus = false;
5214 } else
5215 assert_not_reached("Uh, invalid transport...");
5216 }
e4b61340
LP
5217
5218 switch (arg_action) {
5219
22f4096c
LP
5220 case ACTION_SYSTEMCTL:
5221 r = systemctl_main(bus, argc, argv, &error);
e4b61340 5222 break;
e4b61340
LP
5223
5224 case ACTION_HALT:
5225 case ACTION_POWEROFF:
5226 case ACTION_REBOOT:
5622dde3 5227 case ACTION_KEXEC:
22f4096c 5228 r = halt_main(bus);
e4b61340
LP
5229 break;
5230
e4b61340
LP
5231 case ACTION_RUNLEVEL2:
5232 case ACTION_RUNLEVEL3:
5233 case ACTION_RUNLEVEL4:
5234 case ACTION_RUNLEVEL5:
5235 case ACTION_RESCUE:
514f4ef5 5236 case ACTION_EMERGENCY:
eb22ac37 5237 case ACTION_DEFAULT:
22f4096c 5238 r = start_with_fallback(bus);
e4b61340 5239 break;
7e4249b9 5240
e4b61340
LP
5241 case ACTION_RELOAD:
5242 case ACTION_REEXEC:
22f4096c 5243 r = reload_with_fallback(bus);
e4b61340
LP
5244 break;
5245
f6144808 5246 case ACTION_CANCEL_SHUTDOWN:
52c00215 5247 r = send_shutdownd(0, 0, false, false, NULL);
f6144808
LP
5248 break;
5249
eb22ac37
LP
5250 case ACTION_INVALID:
5251 case ACTION_RUNLEVEL:
e4b61340
LP
5252 default:
5253 assert_not_reached("Unknown action");
5254 }
7e4249b9 5255
22f4096c
LP
5256 retval = r < 0 ? EXIT_FAILURE : r;
5257
7e4249b9 5258finish:
b574246b 5259 if (bus) {
5d452f9c 5260 dbus_connection_flush(bus);
b574246b 5261 dbus_connection_close(bus);
7e4249b9 5262 dbus_connection_unref(bus);
b574246b 5263 }
7e4249b9 5264
e4b61340
LP
5265 dbus_error_free(&error);
5266
7e4249b9
LP
5267 dbus_shutdown();
5268
ea4a240d
LP
5269 strv_free(arg_property);
5270
1888c907 5271 pager_close();
c0f9c7da 5272 agent_close();
1888c907 5273
7e4249b9
LP
5274 return retval;
5275}