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