]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl.c
systemctl: support force-reload and condrestart as aliases for reload-or-try-restart
[thirdparty/systemd.git] / src / systemctl.c
CommitLineData
7e4249b9
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
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>
7e4249b9
LP
33
34#include <dbus/dbus.h>
35
36#include "log.h"
37#include "util.h"
38#include "macro.h"
39#include "set.h"
e4b61340 40#include "utmp-wtmp.h"
514f4ef5 41#include "special.h"
eb22ac37 42#include "initreq.h"
e4a9373f 43#include "strv.h"
9a1ac7b9 44#include "dbus-common.h"
ab35fb1b 45#include "cgroup-show.h"
c6c18be3 46#include "cgroup-util.h"
582a507f 47#include "list.h"
7e4249b9
LP
48
49static const char *arg_type = NULL;
ea4a240d 50static char **arg_property = NULL;
7e4249b9 51static bool arg_all = false;
90d473a1 52static bool arg_fail = false;
7e4249b9 53static bool arg_session = false;
6e905d93 54static bool arg_no_block = false;
e4b61340
LP
55static bool arg_immediate = false;
56static bool arg_no_wtmp = false;
57static bool arg_no_sync = false;
514f4ef5 58static bool arg_no_wall = false;
e4b61340 59static bool arg_dry = false;
0183528f 60static bool arg_quiet = false;
8fe914ec 61static char arg_full = false;
e4b61340 62static char **arg_wall = NULL;
4445a875 63static enum action {
e4b61340
LP
64 ACTION_INVALID,
65 ACTION_SYSTEMCTL,
66 ACTION_HALT,
67 ACTION_POWEROFF,
68 ACTION_REBOOT,
e4b61340
LP
69 ACTION_RUNLEVEL2,
70 ACTION_RUNLEVEL3,
71 ACTION_RUNLEVEL4,
72 ACTION_RUNLEVEL5,
73 ACTION_RESCUE,
514f4ef5
LP
74 ACTION_EMERGENCY,
75 ACTION_DEFAULT,
e4b61340
LP
76 ACTION_RELOAD,
77 ACTION_REEXEC,
78 ACTION_RUNLEVEL,
79 _ACTION_MAX
80} arg_action = ACTION_SYSTEMCTL;
4445a875
LP
81static enum dot {
82 DOT_ALL,
83 DOT_ORDER,
84 DOT_REQUIRE
85} arg_dot = DOT_ALL;
e4b61340 86
f4579ce7
LP
87static bool private_bus = false;
88
2cc59dbf
LP
89static const char *ansi_highlight(bool b) {
90 static int t = -1;
91
92 if (_unlikely_(t < 0))
93 t = isatty(STDOUT_FILENO) > 0;
94
95 if (!t)
96 return "";
97
98 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
99}
100
e4b61340
LP
101static bool error_is_no_service(DBusError *error) {
102
514f4ef5
LP
103 assert(error);
104
e4b61340
LP
105 if (!dbus_error_is_set(error))
106 return false;
107
108 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
109 return true;
110
111 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
112 return true;
113
114 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
115}
7e4249b9
LP
116
117static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
118
514f4ef5
LP
119 assert(iter);
120 assert(data);
121
7e4249b9
LP
122 if (dbus_message_iter_get_arg_type(iter) != type)
123 return -EIO;
124
125 dbus_message_iter_get_basic(iter, data);
126
127 if (!dbus_message_iter_next(iter) != !next)
128 return -EIO;
129
130 return 0;
131}
132
514f4ef5 133static void warn_wall(enum action action) {
ef2f1067 134 static const char *table[_ACTION_MAX] = {
514f4ef5
LP
135 [ACTION_HALT] = "The system is going down for system halt NOW!",
136 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
137 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
138 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
139 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
ef2f1067
LP
140 };
141
514f4ef5
LP
142 if (arg_no_wall)
143 return;
144
e4a9373f
LP
145 if (arg_wall) {
146 char *p;
147
148 if (!(p = strv_join(arg_wall, " "))) {
149 log_error("Failed to join strings.");
150 return;
151 }
152
153 if (*p) {
154 utmp_wall(p);
155 free(p);
156 return;
157 }
158
159 free(p);
160 }
161
514f4ef5 162 if (!table[action])
ef2f1067
LP
163 return;
164
514f4ef5 165 utmp_wall(table[action]);
ef2f1067
LP
166}
167
7e4249b9
LP
168static int list_units(DBusConnection *bus, char **args, unsigned n) {
169 DBusMessage *m = NULL, *reply = NULL;
170 DBusError error;
171 int r;
172 DBusMessageIter iter, sub, sub2;
173 unsigned k = 0;
174
175 dbus_error_init(&error);
176
514f4ef5
LP
177 assert(bus);
178
7e4249b9
LP
179 if (!(m = dbus_message_new_method_call(
180 "org.freedesktop.systemd1",
181 "/org/freedesktop/systemd1",
182 "org.freedesktop.systemd1.Manager",
183 "ListUnits"))) {
184 log_error("Could not allocate message.");
185 return -ENOMEM;
186 }
187
188 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
189 log_error("Failed to issue method call: %s", error.message);
190 r = -EIO;
191 goto finish;
192 }
193
194 if (!dbus_message_iter_init(reply, &iter) ||
195 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
196 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
197 log_error("Failed to parse reply.");
198 r = -EIO;
199 goto finish;
200 }
201
202 dbus_message_iter_recurse(&iter, &sub);
203
d0642824
LP
204 if (isatty(STDOUT_FILENO))
205 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
7e4249b9
LP
206
207 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
8fe914ec 208 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path, *job_type, *job_path, *dot;
7e4249b9
LP
209 uint32_t job_id;
210
211 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
212 log_error("Failed to parse reply.");
213 r = -EIO;
214 goto finish;
215 }
216
217 dbus_message_iter_recurse(&sub, &sub2);
218
219 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
220 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
221 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
222 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
223 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
8fe914ec 224 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
4445a875 225 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
7e4249b9
LP
226 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
227 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
228 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
229 log_error("Failed to parse reply.");
230 r = -EIO;
231 goto finish;
232 }
233
234 if ((!arg_type || ((dot = strrchr(id, '.')) &&
235 streq(dot+1, arg_type))) &&
8fe914ec
LP
236 (arg_all || !(streq(active_state, "inactive") || following[0]) || job_id > 0)) {
237 char *e;
7e4249b9
LP
238 int a = 0, b = 0;
239
e0376b17 240 if (streq(active_state, "maintenance"))
2cc59dbf 241 fputs(ansi_highlight(true), stdout);
e0376b17 242
8fe914ec
LP
243 e = arg_full ? NULL : ellipsize(id, 45, 33);
244 printf("%-45s %-6s %-12s %-12s%n", e ? e : id, load_state, active_state, sub_state, &a);
245 free(e);
7e4249b9
LP
246
247 if (job_id != 0)
0ff3dea7 248 printf(" => %-12s%n", job_type, &b);
7e4249b9 249 else
e0376b17 250 b = 1 + 15;
7e4249b9
LP
251
252 if (a + b + 2 < columns()) {
253 if (job_id == 0)
254 printf(" ");
255
e0376b17 256 printf(" %.*s", columns() - a - b - 2, description);
7e4249b9
LP
257 }
258
e0376b17 259 if (streq(active_state, "maintenance"))
2cc59dbf 260 fputs(ansi_highlight(false), stdout);
e0376b17 261
7e4249b9
LP
262 fputs("\n", stdout);
263 k++;
264 }
265
266 dbus_message_iter_next(&sub);
267 }
268
d0642824 269 if (isatty(STDOUT_FILENO)) {
0ff3dea7 270
d0642824
LP
271 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
272 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
273 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
274 "JOB = Job, shows pending jobs for the unit.\n");
275
276 if (arg_all)
277 printf("\n%u units listed.\n", k);
278 else
279 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
280 }
4445a875
LP
281
282 r = 0;
283
284finish:
285 if (m)
286 dbus_message_unref(m);
287
288 if (reply)
289 dbus_message_unref(reply);
290
291 dbus_error_free(&error);
292
293 return r;
294}
295
296static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
297 static const char * const colors[] = {
298 "Requires", "[color=\"black\"]",
299 "RequiresOverridable", "[color=\"black\"]",
300 "Requisite", "[color=\"darkblue\"]",
301 "RequisiteOverridable", "[color=\"darkblue\"]",
302 "Wants", "[color=\"darkgrey\"]",
303 "Conflicts", "[color=\"red\"]",
304 "After", "[color=\"green\"]"
305 };
306
307 const char *c = NULL;
308 unsigned i;
309
310 assert(name);
311 assert(prop);
312 assert(iter);
313
314 for (i = 0; i < ELEMENTSOF(colors); i += 2)
315 if (streq(colors[i], prop)) {
316 c = colors[i+1];
317 break;
318 }
319
320 if (!c)
321 return 0;
322
323 if (arg_dot != DOT_ALL)
324 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
325 return 0;
326
327 switch (dbus_message_iter_get_arg_type(iter)) {
328
329 case DBUS_TYPE_ARRAY:
330
331 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
332 DBusMessageIter sub;
333
334 dbus_message_iter_recurse(iter, &sub);
335
336 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
337 const char *s;
338
339 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
340 dbus_message_iter_get_basic(&sub, &s);
341 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
342
343 dbus_message_iter_next(&sub);
344 }
345
346 return 0;
347 }
348 }
349
350 return 0;
351}
352
353static int dot_one(DBusConnection *bus, const char *name, const char *path) {
354 DBusMessage *m = NULL, *reply = NULL;
355 const char *interface = "org.freedesktop.systemd1.Unit";
356 int r;
357 DBusError error;
358 DBusMessageIter iter, sub, sub2, sub3;
359
360 assert(bus);
361 assert(path);
362
363 dbus_error_init(&error);
364
365 if (!(m = dbus_message_new_method_call(
366 "org.freedesktop.systemd1",
367 path,
368 "org.freedesktop.DBus.Properties",
369 "GetAll"))) {
370 log_error("Could not allocate message.");
371 r = -ENOMEM;
372 goto finish;
373 }
374
375 if (!dbus_message_append_args(m,
376 DBUS_TYPE_STRING, &interface,
377 DBUS_TYPE_INVALID)) {
378 log_error("Could not append arguments to message.");
379 r = -ENOMEM;
380 goto finish;
381 }
382
383 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
384 log_error("Failed to issue method call: %s", error.message);
385 r = -EIO;
386 goto finish;
387 }
388
389 if (!dbus_message_iter_init(reply, &iter) ||
390 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
391 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
392 log_error("Failed to parse reply.");
393 r = -EIO;
394 goto finish;
395 }
396
397 dbus_message_iter_recurse(&iter, &sub);
398
399 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
400 const char *prop;
401
402 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
403 log_error("Failed to parse reply.");
404 r = -EIO;
405 goto finish;
406 }
407
408 dbus_message_iter_recurse(&sub, &sub2);
409
410 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
411 log_error("Failed to parse reply.");
412 r = -EIO;
413 goto finish;
414 }
415
416 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
417 log_error("Failed to parse reply.");
418 r = -EIO;
419 goto finish;
420 }
421
422 dbus_message_iter_recurse(&sub2, &sub3);
423
424 if (dot_one_property(name, prop, &sub3)) {
425 log_error("Failed to parse reply.");
426 r = -EIO;
427 goto finish;
428 }
429
430 dbus_message_iter_next(&sub);
431 }
432
433finish:
434 if (m)
435 dbus_message_unref(m);
436
437 if (reply)
438 dbus_message_unref(reply);
439
440 dbus_error_free(&error);
441
442 return r;
443}
444
445static int dot(DBusConnection *bus, char **args, unsigned n) {
446 DBusMessage *m = NULL, *reply = NULL;
447 DBusError error;
448 int r;
449 DBusMessageIter iter, sub, sub2;
450
451 dbus_error_init(&error);
452
453 assert(bus);
454
455 if (!(m = dbus_message_new_method_call(
456 "org.freedesktop.systemd1",
457 "/org/freedesktop/systemd1",
458 "org.freedesktop.systemd1.Manager",
459 "ListUnits"))) {
460 log_error("Could not allocate message.");
461 return -ENOMEM;
462 }
463
464 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
465 log_error("Failed to issue method call: %s", error.message);
466 r = -EIO;
467 goto finish;
468 }
469
470 if (!dbus_message_iter_init(reply, &iter) ||
471 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
472 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
473 log_error("Failed to parse reply.");
474 r = -EIO;
475 goto finish;
476 }
477
478 printf("digraph systemd {\n");
479
480 dbus_message_iter_recurse(&iter, &sub);
481 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
482 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path;
483
484 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
485 log_error("Failed to parse reply.");
486 r = -EIO;
487 goto finish;
488 }
489
490 dbus_message_iter_recurse(&sub, &sub2);
491
492 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
493 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
494 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
495 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
496 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
498 log_error("Failed to parse reply.");
499 r = -EIO;
500 goto finish;
501 }
502
503 if ((r = dot_one(bus, id, unit_path)) < 0)
504 goto finish;
505
506 /* printf("\t\"%s\";\n", id); */
507 dbus_message_iter_next(&sub);
508 }
509
510 printf("}\n");
511
512 log_info(" Color legend: black = Requires\n"
513 " dark blue = Requisite\n"
514 " dark grey = Wants\n"
515 " red = Conflicts\n"
516 " green = After\n");
517
518 if (isatty(fileno(stdout)))
519 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
520 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
7e4249b9
LP
521
522 r = 0;
523
524finish:
525 if (m)
526 dbus_message_unref(m);
527
528 if (reply)
529 dbus_message_unref(reply);
530
531 dbus_error_free(&error);
532
533 return r;
534}
535
536static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
537 DBusMessage *m = NULL, *reply = NULL;
538 DBusError error;
539 int r;
540 DBusMessageIter iter, sub, sub2;
541 unsigned k = 0;
542
543 dbus_error_init(&error);
544
514f4ef5
LP
545 assert(bus);
546
7e4249b9
LP
547 if (!(m = dbus_message_new_method_call(
548 "org.freedesktop.systemd1",
549 "/org/freedesktop/systemd1",
550 "org.freedesktop.systemd1.Manager",
551 "ListJobs"))) {
552 log_error("Could not allocate message.");
553 return -ENOMEM;
554 }
555
556 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
557 log_error("Failed to issue method call: %s", error.message);
558 r = -EIO;
559 goto finish;
560 }
561
562 if (!dbus_message_iter_init(reply, &iter) ||
563 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
564 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
565 log_error("Failed to parse reply.");
566 r = -EIO;
567 goto finish;
568 }
569
570 dbus_message_iter_recurse(&iter, &sub);
571
572 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
573
574 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
575 const char *name, *type, *state, *job_path, *unit_path;
576 uint32_t id;
8fe914ec 577 char *e;
7e4249b9
LP
578
579 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
580 log_error("Failed to parse reply.");
581 r = -EIO;
582 goto finish;
583 }
584
585 dbus_message_iter_recurse(&sub, &sub2);
586
587 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
588 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
589 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
590 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
591 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
592 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
593 log_error("Failed to parse reply.");
594 r = -EIO;
595 goto finish;
596 }
597
8fe914ec
LP
598 e = arg_full ? NULL : ellipsize(name, 45, 33);
599 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
600 free(e);
601
7e4249b9
LP
602 k++;
603
604 dbus_message_iter_next(&sub);
605 }
606
607 printf("\n%u jobs listed.\n", k);
608 r = 0;
609
610finish:
611 if (m)
612 dbus_message_unref(m);
613
614 if (reply)
615 dbus_message_unref(reply);
616
617 dbus_error_free(&error);
618
619 return r;
620}
621
622static int load_unit(DBusConnection *bus, char **args, unsigned n) {
623 DBusMessage *m = NULL, *reply = NULL;
624 DBusError error;
625 int r;
626 unsigned i;
627
628 dbus_error_init(&error);
629
514f4ef5
LP
630 assert(bus);
631 assert(args);
632
7e4249b9
LP
633 for (i = 1; i < n; i++) {
634
635 if (!(m = dbus_message_new_method_call(
636 "org.freedesktop.systemd1",
637 "/org/freedesktop/systemd1",
638 "org.freedesktop.systemd1.Manager",
639 "LoadUnit"))) {
640 log_error("Could not allocate message.");
641 r = -ENOMEM;
642 goto finish;
643 }
644
645 if (!dbus_message_append_args(m,
646 DBUS_TYPE_STRING, &args[i],
647 DBUS_TYPE_INVALID)) {
648 log_error("Could not append arguments to message.");
649 r = -ENOMEM;
650 goto finish;
651 }
652
653 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
654 log_error("Failed to issue method call: %s", error.message);
655 r = -EIO;
656 goto finish;
657 }
658
659 dbus_message_unref(m);
660 dbus_message_unref(reply);
661
662 m = reply = NULL;
663 }
664
665 r = 0;
666
667finish:
668 if (m)
669 dbus_message_unref(m);
670
671 if (reply)
672 dbus_message_unref(reply);
673
674 dbus_error_free(&error);
675
676 return r;
677}
678
679static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
680 DBusMessage *m = NULL, *reply = NULL;
681 DBusError error;
682 int r;
683 unsigned i;
684
685 dbus_error_init(&error);
686
514f4ef5
LP
687 assert(bus);
688 assert(args);
689
7e4249b9
LP
690 for (i = 1; i < n; i++) {
691 unsigned id;
692 const char *path;
693
694 if (!(m = dbus_message_new_method_call(
695 "org.freedesktop.systemd1",
696 "/org/freedesktop/systemd1",
697 "org.freedesktop.systemd1.Manager",
698 "GetJob"))) {
699 log_error("Could not allocate message.");
700 r = -ENOMEM;
701 goto finish;
702 }
703
704 if ((r = safe_atou(args[i], &id)) < 0) {
705 log_error("Failed to parse job id: %s", strerror(-r));
706 goto finish;
707 }
708
709 assert_cc(sizeof(uint32_t) == sizeof(id));
710 if (!dbus_message_append_args(m,
711 DBUS_TYPE_UINT32, &id,
712 DBUS_TYPE_INVALID)) {
713 log_error("Could not append arguments to message.");
714 r = -ENOMEM;
715 goto finish;
716 }
717
718 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
719 log_error("Failed to issue method call: %s", error.message);
720 r = -EIO;
721 goto finish;
722 }
723
724 if (!dbus_message_get_args(reply, &error,
725 DBUS_TYPE_OBJECT_PATH, &path,
726 DBUS_TYPE_INVALID)) {
727 log_error("Failed to parse reply: %s", error.message);
728 r = -EIO;
729 goto finish;
730 }
731
732 dbus_message_unref(m);
733 if (!(m = dbus_message_new_method_call(
734 "org.freedesktop.systemd1",
735 path,
736 "org.freedesktop.systemd1.Job",
737 "Cancel"))) {
738 log_error("Could not allocate message.");
739 r = -ENOMEM;
740 goto finish;
741 }
742
743 dbus_message_unref(reply);
744 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
745 log_error("Failed to issue method call: %s", error.message);
746 r = -EIO;
747 goto finish;
748 }
749
750 dbus_message_unref(m);
751 dbus_message_unref(reply);
752 m = reply = NULL;
753 }
754
755 r = 0;
756
757finish:
758 if (m)
759 dbus_message_unref(m);
760
761 if (reply)
762 dbus_message_unref(reply);
763
764 dbus_error_free(&error);
765
766 return r;
767}
768
45fb0699
LP
769static bool unit_need_daemon_reload(DBusConnection *bus, const char *unit) {
770 DBusMessage *m, *reply;
771 dbus_bool_t b = FALSE;
772 DBusMessageIter iter, sub;
773 const char
774 *interface = "org.freedesktop.systemd1.Unit",
775 *property = "NeedDaemonReload",
776 *path;
777
778 /* We ignore all errors here, since this is used to show a warning only */
779
780 if (!(m = dbus_message_new_method_call(
781 "org.freedesktop.systemd1",
782 "/org/freedesktop/systemd1",
783 "org.freedesktop.systemd1.Manager",
784 "GetUnit")))
785 goto finish;
786
787 if (!dbus_message_append_args(m,
788 DBUS_TYPE_STRING, &unit,
789 DBUS_TYPE_INVALID))
790 goto finish;
791
792 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
793 goto finish;
794
795 if (!dbus_message_get_args(reply, NULL,
796 DBUS_TYPE_OBJECT_PATH, &path,
797 DBUS_TYPE_INVALID))
798 goto finish;
799
800 dbus_message_unref(m);
801 if (!(m = dbus_message_new_method_call(
802 "org.freedesktop.systemd1",
803 path,
804 "org.freedesktop.DBus.Properties",
805 "Get")))
806 goto finish;
807
808 if (!dbus_message_append_args(m,
809 DBUS_TYPE_STRING, &interface,
810 DBUS_TYPE_STRING, &property,
811 DBUS_TYPE_INVALID)) {
812 goto finish;
813 }
814
815 dbus_message_unref(reply);
816 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
817 goto finish;
818
819 if (!dbus_message_iter_init(reply, &iter) ||
820 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
821 goto finish;
822
823 dbus_message_iter_recurse(&iter, &sub);
824
825 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
826 goto finish;
827
828 dbus_message_iter_get_basic(&sub, &b);
829
830finish:
831 if (m)
832 dbus_message_unref(m);
833
834 if (reply)
835 dbus_message_unref(reply);
836
837 return b;
838}
839
5e374895
LP
840typedef struct WaitData {
841 Set *set;
842 bool failed;
843} WaitData;
844
7e4249b9
LP
845static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
846 DBusError error;
5e374895 847 WaitData *d = data;
7e4249b9
LP
848
849 assert(connection);
850 assert(message);
5e374895 851 assert(d);
7e4249b9
LP
852
853 dbus_error_init(&error);
854
54165a39
LP
855 log_debug("Got D-Bus request: %s.%s() on %s",
856 dbus_message_get_interface(message),
857 dbus_message_get_member(message),
858 dbus_message_get_path(message));
7e4249b9
LP
859
860 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
861 log_error("Warning! D-Bus connection terminated.");
862 dbus_connection_close(connection);
863
864 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
865 uint32_t id;
866 const char *path;
5e374895 867 dbus_bool_t success = true;
7e4249b9
LP
868
869 if (!dbus_message_get_args(message, &error,
870 DBUS_TYPE_UINT32, &id,
871 DBUS_TYPE_OBJECT_PATH, &path,
5e374895 872 DBUS_TYPE_BOOLEAN, &success,
7e4249b9
LP
873 DBUS_TYPE_INVALID))
874 log_error("Failed to parse message: %s", error.message);
875 else {
876 char *p;
877
5e374895 878 if ((p = set_remove(d->set, (char*) path)))
7e4249b9 879 free(p);
5e374895
LP
880
881 if (!success)
882 d->failed = true;
7e4249b9
LP
883 }
884 }
885
886 dbus_error_free(&error);
887 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
888}
889
479ef5d3 890static int enable_wait_for_jobs(DBusConnection *bus) {
7e4249b9 891 DBusError error;
7e4249b9
LP
892
893 assert(bus);
7e4249b9 894
f4579ce7
LP
895 if (private_bus)
896 return 0;
897
7e4249b9 898 dbus_error_init(&error);
7e4249b9
LP
899 dbus_bus_add_match(bus,
900 "type='signal',"
901 "sender='org.freedesktop.systemd1',"
902 "interface='org.freedesktop.systemd1.Manager',"
903 "member='JobRemoved',"
904 "path='/org/freedesktop/systemd1'",
905 &error);
906
907 if (dbus_error_is_set(&error)) {
908 log_error("Failed to add match: %s", error.message);
a567261a
LP
909 dbus_error_free(&error);
910 return -EIO;
7e4249b9
LP
911 }
912
479ef5d3 913 /* This is slightly dirty, since we don't undo the match registrations. */
a567261a 914 return 0;
7e4249b9
LP
915}
916
479ef5d3
LP
917static int wait_for_jobs(DBusConnection *bus, Set *s) {
918 int r;
5e374895 919 WaitData d;
479ef5d3
LP
920
921 assert(bus);
922 assert(s);
923
5e374895
LP
924 zero(d);
925 d.set = s;
926 d.failed = false;
927
928 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
479ef5d3
LP
929 log_error("Failed to add filter.");
930 r = -ENOMEM;
931 goto finish;
932 }
933
934 while (!set_isempty(s) &&
935 dbus_connection_read_write_dispatch(bus, -1))
936 ;
937
5e374895
LP
938 if (!arg_quiet && d.failed)
939 log_error("Job failed, see logs for details.");
940
941 r = d.failed ? -EIO : 0;
479ef5d3
LP
942
943finish:
944 /* This is slightly dirty, since we don't undo the filter registration. */
945
946 return r;
947}
948
e4b61340
LP
949static int start_unit_one(
950 DBusConnection *bus,
951 const char *method,
952 const char *name,
953 const char *mode,
954 Set *s) {
7e4249b9 955
7e4249b9
LP
956 DBusMessage *m = NULL, *reply = NULL;
957 DBusError error;
45fb0699 958 const char *path;
7e4249b9 959 int r;
7e4249b9 960
e4b61340
LP
961 assert(bus);
962 assert(method);
963 assert(name);
964 assert(mode);
6e905d93 965 assert(arg_no_block || s);
7e4249b9 966
e4b61340 967 dbus_error_init(&error);
479ef5d3 968
7e4249b9
LP
969 if (!(m = dbus_message_new_method_call(
970 "org.freedesktop.systemd1",
971 "/org/freedesktop/systemd1",
972 "org.freedesktop.systemd1.Manager",
e4b61340 973 method))) {
7e4249b9
LP
974 log_error("Could not allocate message.");
975 r = -ENOMEM;
976 goto finish;
977 }
978
979 if (!dbus_message_append_args(m,
e4b61340 980 DBUS_TYPE_STRING, &name,
7e4249b9
LP
981 DBUS_TYPE_STRING, &mode,
982 DBUS_TYPE_INVALID)) {
983 log_error("Could not append arguments to message.");
984 r = -ENOMEM;
985 goto finish;
986 }
987
988 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
e4b61340
LP
989
990 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
991 /* There's always a fallback possible for
992 * legacy actions. */
993 r = 0;
994 goto finish;
995 }
996
7e4249b9
LP
997 log_error("Failed to issue method call: %s", error.message);
998 r = -EIO;
999 goto finish;
1000 }
1001
45fb0699
LP
1002 if (!dbus_message_get_args(reply, &error,
1003 DBUS_TYPE_OBJECT_PATH, &path,
1004 DBUS_TYPE_INVALID)) {
1005 log_error("Failed to parse reply: %s", error.message);
1006 r = -EIO;
1007 goto finish;
1008 }
1009
1010 if (unit_need_daemon_reload(bus, name))
1011 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1012 arg_session ? "--session" : "--system");
1013
6e905d93 1014 if (!arg_no_block) {
e4b61340 1015 char *p;
7e4249b9 1016
7e4249b9
LP
1017 if (!(p = strdup(path))) {
1018 log_error("Failed to duplicate path.");
1019 r = -ENOMEM;
1020 goto finish;
1021 }
1022
1023 if ((r = set_put(s, p)) < 0) {
e4b61340 1024 free(p);
7e4249b9
LP
1025 log_error("Failed to add path to set.");
1026 goto finish;
1027 }
e4b61340 1028 }
7e4249b9 1029
e4b61340 1030 r = 1;
7e4249b9
LP
1031
1032finish:
7e4249b9
LP
1033 if (m)
1034 dbus_message_unref(m);
1035
1036 if (reply)
1037 dbus_message_unref(reply);
1038
1039 dbus_error_free(&error);
1040
1041 return r;
1042}
1043
514f4ef5
LP
1044static enum action verb_to_action(const char *verb) {
1045 if (streq(verb, "halt"))
1046 return ACTION_HALT;
1047 else if (streq(verb, "poweroff"))
1048 return ACTION_POWEROFF;
1049 else if (streq(verb, "reboot"))
1050 return ACTION_REBOOT;
1051 else if (streq(verb, "rescue"))
1052 return ACTION_RESCUE;
1053 else if (streq(verb, "emergency"))
1054 return ACTION_EMERGENCY;
1055 else if (streq(verb, "default"))
1056 return ACTION_DEFAULT;
1057 else
1058 return ACTION_INVALID;
1059}
1060
e4b61340
LP
1061static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1062
1063 static const char * const table[_ACTION_MAX] = {
514f4ef5
LP
1064 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1065 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1066 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1067 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1068 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1069 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1070 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1071 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
f057408c 1072 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
514f4ef5 1073 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
e4b61340
LP
1074 };
1075
1076 int r;
1077 unsigned i;
514f4ef5 1078 const char *method, *mode, *one_name;
e4b61340
LP
1079 Set *s = NULL;
1080
514f4ef5
LP
1081 assert(bus);
1082
e4b61340
LP
1083 if (arg_action == ACTION_SYSTEMCTL) {
1084 method =
6f28c033
LP
1085 streq(args[0], "stop") ? "StopUnit" :
1086 streq(args[0], "reload") ? "ReloadUnit" :
1087 streq(args[0], "restart") ? "RestartUnit" :
1088 streq(args[0], "try-restart") ? "TryRestartUnit" :
1089 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
9d8a57ff
LP
1090 streq(args[0], "reload-or-try-restart") ||
1091 streq(args[0], "force-reload") ||
1092 streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
6f28c033 1093 "StartUnit";
e4b61340
LP
1094
1095 mode =
514f4ef5
LP
1096 (streq(args[0], "isolate") ||
1097 streq(args[0], "rescue") ||
1098 streq(args[0], "emergency")) ? "isolate" :
90d473a1
LP
1099 arg_fail ? "fail" :
1100 "replace";
e4b61340 1101
514f4ef5 1102 one_name = table[verb_to_action(args[0])];
e4b61340 1103
e4b61340
LP
1104 } else {
1105 assert(arg_action < ELEMENTSOF(table));
1106 assert(table[arg_action]);
1107
1108 method = "StartUnit";
514f4ef5
LP
1109
1110 mode = (arg_action == ACTION_EMERGENCY ||
1111 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1112
1113 one_name = table[arg_action];
1114 }
1115
6e905d93 1116 if (!arg_no_block) {
514f4ef5
LP
1117 if ((r = enable_wait_for_jobs(bus)) < 0) {
1118 log_error("Could not watch jobs: %s", strerror(-r));
1119 goto finish;
1120 }
1121
1122 if (!(s = set_new(string_hash_func, string_compare_func))) {
1123 log_error("Failed to allocate set.");
1124 r = -ENOMEM;
1125 goto finish;
1126 }
e4b61340
LP
1127 }
1128
1129 r = 0;
1130
514f4ef5
LP
1131 if (one_name) {
1132 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1133 goto finish;
1134 } else {
e4b61340
LP
1135 for (i = 1; i < n; i++)
1136 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1137 goto finish;
e4b61340
LP
1138 }
1139
6e905d93 1140 if (!arg_no_block)
514f4ef5
LP
1141 r = wait_for_jobs(bus, s);
1142
e4b61340
LP
1143finish:
1144 if (s)
1145 set_free_free(s);
1146
1147 return r;
1148}
1149
514f4ef5 1150static int start_special(DBusConnection *bus, char **args, unsigned n) {
983d9c90
LP
1151 int r;
1152
514f4ef5
LP
1153 assert(bus);
1154 assert(args);
1155
983d9c90
LP
1156 r = start_unit(bus, args, n);
1157
1158 if (r >= 0)
1159 warn_wall(verb_to_action(args[0]));
514f4ef5 1160
983d9c90 1161 return r;
514f4ef5
LP
1162}
1163
0183528f
LP
1164static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1165 DBusMessage *m = NULL, *reply = NULL;
1166 const char
1167 *interface = "org.freedesktop.systemd1.Unit",
1168 *property = "ActiveState";
1169 int r = -EADDRNOTAVAIL;
1170 DBusError error;
1171 unsigned i;
1172
1173 assert(bus);
1174 assert(args);
1175
1176 dbus_error_init(&error);
1177
1178 for (i = 1; i < n; i++) {
1179 const char *path = NULL;
1180 const char *state;
1181 DBusMessageIter iter, sub;
1182
1183 if (!(m = dbus_message_new_method_call(
1184 "org.freedesktop.systemd1",
1185 "/org/freedesktop/systemd1",
1186 "org.freedesktop.systemd1.Manager",
1187 "GetUnit"))) {
1188 log_error("Could not allocate message.");
1189 r = -ENOMEM;
1190 goto finish;
1191 }
1192
1193 if (!dbus_message_append_args(m,
1194 DBUS_TYPE_STRING, &args[i],
1195 DBUS_TYPE_INVALID)) {
1196 log_error("Could not append arguments to message.");
1197 r = -ENOMEM;
1198 goto finish;
1199 }
1200
1201 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1202
1203 /* Hmm, cannot figure out anything about this unit... */
1204 if (!arg_quiet)
1205 puts("unknown");
1206
ed2d7a44 1207 dbus_error_free(&error);
0183528f
LP
1208 continue;
1209 }
1210
1211 if (!dbus_message_get_args(reply, &error,
1212 DBUS_TYPE_OBJECT_PATH, &path,
1213 DBUS_TYPE_INVALID)) {
1214 log_error("Failed to parse reply: %s", error.message);
1215 r = -EIO;
1216 goto finish;
1217 }
1218
1219 dbus_message_unref(m);
1220 if (!(m = dbus_message_new_method_call(
1221 "org.freedesktop.systemd1",
1222 path,
1223 "org.freedesktop.DBus.Properties",
1224 "Get"))) {
1225 log_error("Could not allocate message.");
1226 r = -ENOMEM;
1227 goto finish;
1228 }
1229
1230 if (!dbus_message_append_args(m,
1231 DBUS_TYPE_STRING, &interface,
1232 DBUS_TYPE_STRING, &property,
1233 DBUS_TYPE_INVALID)) {
1234 log_error("Could not append arguments to message.");
1235 r = -ENOMEM;
1236 goto finish;
1237 }
1238
1239 dbus_message_unref(reply);
1240 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1241 log_error("Failed to issue method call: %s", error.message);
1242 r = -EIO;
1243 goto finish;
1244 }
1245
1246 if (!dbus_message_iter_init(reply, &iter) ||
1247 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1248 log_error("Failed to parse reply.");
1249 r = -EIO;
1250 goto finish;
1251 }
1252
1253 dbus_message_iter_recurse(&iter, &sub);
1254
1255 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1256 log_error("Failed to parse reply.");
1257 r = -EIO;
1258 goto finish;
1259 }
1260
1261 dbus_message_iter_get_basic(&sub, &state);
1262
1263 if (!arg_quiet)
1264 puts(state);
1265
01b1b079 1266 if (streq(state, "active") || startswith(state, "reloading"))
0183528f
LP
1267 r = 0;
1268
1269 dbus_message_unref(m);
1270 dbus_message_unref(reply);
1271 m = reply = NULL;
1272 }
1273
1274finish:
1275 if (m)
1276 dbus_message_unref(m);
1277
1278 if (reply)
1279 dbus_message_unref(reply);
1280
1281 dbus_error_free(&error);
1282
1283 return r;
48220598
LP
1284}
1285
582a507f
LP
1286typedef struct ExecStatusInfo {
1287 char *path;
1288 char **argv;
1289
b708e7ce
LP
1290 bool ignore;
1291
582a507f
LP
1292 usec_t start_timestamp;
1293 usec_t exit_timestamp;
1294 pid_t pid;
1295 int code;
1296 int status;
1297
1298 LIST_FIELDS(struct ExecStatusInfo, exec);
1299} ExecStatusInfo;
1300
1301static void exec_status_info_free(ExecStatusInfo *i) {
1302 assert(i);
1303
1304 free(i->path);
1305 strv_free(i->argv);
1306 free(i);
1307}
1308
1309static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1310 uint64_t start_timestamp, exit_timestamp;
1311 DBusMessageIter sub2, sub3;
1312 const char*path;
1313 unsigned n;
1314 uint32_t pid;
1315 int32_t code, status;
b708e7ce 1316 dbus_bool_t ignore;
582a507f
LP
1317
1318 assert(i);
1319 assert(i);
1320
1321 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1322 return -EIO;
1323
1324 dbus_message_iter_recurse(sub, &sub2);
1325
1326 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1327 return -EIO;
1328
1329 if (!(i->path = strdup(path)))
1330 return -ENOMEM;
1331
1332 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1333 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1334 return -EIO;
1335
1336 n = 0;
1337 dbus_message_iter_recurse(&sub2, &sub3);
1338 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1339 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1340 dbus_message_iter_next(&sub3);
1341 n++;
1342 }
1343
1344
1345 if (!(i->argv = new0(char*, n+1)))
1346 return -ENOMEM;
1347
1348 n = 0;
1349 dbus_message_iter_recurse(&sub2, &sub3);
1350 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1351 const char *s;
1352
1353 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1354 dbus_message_iter_get_basic(&sub3, &s);
1355 dbus_message_iter_next(&sub3);
1356
1357 if (!(i->argv[n++] = strdup(s)))
1358 return -ENOMEM;
1359 }
1360
1361 if (!dbus_message_iter_next(&sub2) ||
b708e7ce 1362 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
582a507f
LP
1363 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1364 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1365 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1366 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1367 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1368 return -EIO;
1369
b708e7ce 1370 i->ignore = ignore;
582a507f
LP
1371 i->start_timestamp = (usec_t) start_timestamp;
1372 i->exit_timestamp = (usec_t) exit_timestamp;
1373 i->pid = (pid_t) pid;
1374 i->code = code;
1375 i->status = status;
1376
1377 return 0;
1378}
1379
61cbdc4b
LP
1380typedef struct UnitStatusInfo {
1381 const char *id;
1382 const char *load_state;
1383 const char *active_state;
1384 const char *sub_state;
1385
1386 const char *description;
1387
1388 const char *fragment_path;
1389 const char *default_control_group;
1390
45fb0699
LP
1391 bool need_daemon_reload;
1392
61cbdc4b
LP
1393 /* Service */
1394 pid_t main_pid;
1395 pid_t control_pid;
1396 const char *status_text;
1397 bool running;
1398
1399 usec_t start_timestamp;
1400 usec_t exit_timestamp;
1401
1402 int exit_code, exit_status;
1403
1404 /* Socket */
1405 unsigned n_accepted;
1406 unsigned n_connections;
b8131a87 1407 bool accept;
61cbdc4b
LP
1408
1409 /* Device */
1410 const char *sysfs_path;
1411
1412 /* Mount, Automount */
1413 const char *where;
1414
1415 /* Swap */
1416 const char *what;
582a507f
LP
1417
1418 LIST_HEAD(ExecStatusInfo, exec);
61cbdc4b
LP
1419} UnitStatusInfo;
1420
1421static void print_status_info(UnitStatusInfo *i) {
582a507f
LP
1422 ExecStatusInfo *p;
1423
61cbdc4b
LP
1424 assert(i);
1425
1426 /* This shows pretty information about a unit. See
1427 * print_property() for a low-level property printer */
1428
1429 printf("%s", strna(i->id));
1430
1431 if (i->description && !streq_ptr(i->id, i->description))
1432 printf(" - %s", i->description);
1433
1434 printf("\n");
1435
1436 if (i->fragment_path)
1437 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1438 else if (streq_ptr(i->load_state, "failed"))
2cc59dbf
LP
1439 printf("\t Loaded: %s%s%s\n",
1440 ansi_highlight(true),
1441 strna(i->load_state),
1442 ansi_highlight(false));
61cbdc4b
LP
1443 else
1444 printf("\t Loaded: %s\n", strna(i->load_state));
1445
582a507f
LP
1446 if (streq_ptr(i->active_state, "maintenance")) {
1447 if (streq_ptr(i->active_state, i->sub_state))
2cc59dbf
LP
1448 printf("\t Active: %s%s%s\n",
1449 ansi_highlight(true),
1450 strna(i->active_state),
1451 ansi_highlight(false));
582a507f 1452 else
2cc59dbf
LP
1453 printf("\t Active: %s%s (%s)%s\n",
1454 ansi_highlight(true),
582a507f 1455 strna(i->active_state),
2cc59dbf
LP
1456 strna(i->sub_state),
1457 ansi_highlight(false));
582a507f
LP
1458 } else {
1459 if (streq_ptr(i->active_state, i->sub_state))
1460 printf("\t Active: %s\n",
1461 strna(i->active_state));
1462 else
1463 printf("\t Active: %s (%s)\n",
1464 strna(i->active_state),
1465 strna(i->sub_state));
1466 }
61cbdc4b
LP
1467
1468 if (i->sysfs_path)
1469 printf("\t Device: %s\n", i->sysfs_path);
1470 else if (i->where)
1471 printf("\t Where: %s\n", i->where);
1472 else if (i->what)
1473 printf("\t What: %s\n", i->what);
1474
b8131a87 1475 if (i->accept)
61cbdc4b
LP
1476 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1477
582a507f
LP
1478 LIST_FOREACH(exec, p, i->exec) {
1479 char *t;
1480
1481 /* Only show exited processes here */
1482 if (p->code == 0)
1483 continue;
1484
1485 t = strv_join(p->argv, " ");
1486 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1487 free(t);
1488
1489 if (p->code == CLD_EXITED)
1490 printf("status=%i", p->status);
1491 else
1492 printf("signal=%s", signal_to_string(p->status));
1493 printf(")\n");
1494
1495 if (i->main_pid == p->pid &&
1496 i->start_timestamp == p->start_timestamp &&
1497 i->exit_timestamp == p->start_timestamp)
1498 /* Let's not show this twice */
1499 i->main_pid = 0;
1500
1501 if (p->pid == i->control_pid)
1502 i->control_pid = 0;
1503 }
1504
61cbdc4b
LP
1505 if (i->main_pid > 0 || i->control_pid > 0) {
1506 printf("\t");
1507
1508 if (i->main_pid > 0) {
582a507f 1509 printf(" Main: %u", (unsigned) i->main_pid);
61cbdc4b
LP
1510
1511 if (i->running) {
1512 char *t = NULL;
1513 get_process_name(i->main_pid, &t);
1514 if (t) {
1515 printf(" (%s)", t);
1516 free(t);
1517 }
1518 } else {
1519 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1520
1521 if (i->exit_code == CLD_EXITED)
1522 printf("status=%i", i->exit_status);
1523 else
582a507f 1524 printf("signal=%s", signal_to_string(i->exit_status));
2cc59dbf 1525 printf(")"); }
61cbdc4b
LP
1526 }
1527
1528 if (i->main_pid > 0 && i->control_pid > 0)
1529 printf(";");
1530
1531 if (i->control_pid > 0) {
1532 char *t = NULL;
1533
1534 printf(" Control: %u", (unsigned) i->control_pid);
1535
1536 get_process_name(i->control_pid, &t);
1537 if (t) {
1538 printf(" (%s)", t);
1539 free(t);
1540 }
1541 }
1542
1543 printf("\n");
1544 }
1545
17bb7382
LP
1546 if (i->status_text)
1547 printf("\t Status: \"%s\"\n", i->status_text);
1548
c59760ee 1549 if (i->default_control_group) {
ab35fb1b
LP
1550 unsigned c;
1551
61cbdc4b 1552 printf("\t CGroup: %s\n", i->default_control_group);
ab35fb1b
LP
1553
1554 if ((c = columns()) > 18)
1555 c -= 18;
1556 else
1557 c = 0;
1558
35d2e7ec 1559 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
c59760ee 1560 }
45fb0699
LP
1561
1562 if (i->need_daemon_reload)
2cc59dbf
LP
1563 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1564 ansi_highlight(true),
1565 ansi_highlight(false),
45fb0699 1566 arg_session ? "--session" : "--system");
61cbdc4b
LP
1567}
1568
1569static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1570
1571 switch (dbus_message_iter_get_arg_type(iter)) {
1572
1573 case DBUS_TYPE_STRING: {
1574 const char *s;
1575
1576 dbus_message_iter_get_basic(iter, &s);
1577
1578 if (s[0]) {
1579 if (streq(name, "Id"))
1580 i->id = s;
1581 else if (streq(name, "LoadState"))
1582 i->load_state = s;
1583 else if (streq(name, "ActiveState"))
1584 i->active_state = s;
1585 else if (streq(name, "SubState"))
1586 i->sub_state = s;
1587 else if (streq(name, "Description"))
1588 i->description = s;
1589 else if (streq(name, "FragmentPath"))
1590 i->fragment_path = s;
1591 else if (streq(name, "DefaultControlGroup"))
1592 i->default_control_group = s;
1593 else if (streq(name, "StatusText"))
1594 i->status_text = s;
1595 else if (streq(name, "SysFSPath"))
1596 i->sysfs_path = s;
1597 else if (streq(name, "Where"))
1598 i->where = s;
1599 else if (streq(name, "What"))
1600 i->what = s;
1601 }
1602
1603 break;
1604 }
1605
b8131a87
LP
1606 case DBUS_TYPE_BOOLEAN: {
1607 dbus_bool_t b;
1608
1609 dbus_message_iter_get_basic(iter, &b);
1610
1611 if (streq(name, "Accept"))
1612 i->accept = b;
45fb0699
LP
1613 else if (streq(name, "NeedDaemonReload"))
1614 i->need_daemon_reload = b;
b8131a87
LP
1615
1616 break;
1617 }
1618
61cbdc4b
LP
1619 case DBUS_TYPE_UINT32: {
1620 uint32_t u;
1621
1622 dbus_message_iter_get_basic(iter, &u);
1623
1624 if (streq(name, "MainPID")) {
1625 if (u > 0) {
1626 i->main_pid = (pid_t) u;
1627 i->running = true;
1628 }
1629 } else if (streq(name, "ControlPID"))
1630 i->control_pid = (pid_t) u;
1631 else if (streq(name, "ExecMainPID")) {
1632 if (u > 0)
1633 i->main_pid = (pid_t) u;
1634 } else if (streq(name, "NAccepted"))
1635 i->n_accepted = u;
1636 else if (streq(name, "NConnections"))
1637 i->n_connections = u;
1638
1639 break;
1640 }
1641
1642 case DBUS_TYPE_INT32: {
1643 int32_t j;
1644
1645 dbus_message_iter_get_basic(iter, &j);
1646
1647 if (streq(name, "ExecMainCode"))
1648 i->exit_code = (int) j;
1649 else if (streq(name, "ExecMainStatus"))
1650 i->exit_status = (int) j;
1651
1652 break;
1653 }
1654
1655 case DBUS_TYPE_UINT64: {
1656 uint64_t u;
1657
1658 dbus_message_iter_get_basic(iter, &u);
1659
1660 if (streq(name, "ExecMainStartTimestamp"))
1661 i->start_timestamp = (usec_t) u;
1662 else if (streq(name, "ExecMainExitTimestamp"))
1663 i->exit_timestamp = (usec_t) u;
1664
1665 break;
1666 }
582a507f
LP
1667
1668 case DBUS_TYPE_ARRAY: {
1669
1670 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1671 startswith(name, "Exec")) {
1672 DBusMessageIter sub;
1673
1674 dbus_message_iter_recurse(iter, &sub);
1675 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1676 ExecStatusInfo *info;
1677 int r;
1678
1679 if (!(info = new0(ExecStatusInfo, 1)))
1680 return -ENOMEM;
1681
1682 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1683 free(info);
1684 return r;
1685 }
1686
1687 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1688
1689 dbus_message_iter_next(&sub);
1690 }
1691 }
1692
1693 break;
1694 }
61cbdc4b
LP
1695 }
1696
1697 return 0;
1698}
1699
48220598
LP
1700static int print_property(const char *name, DBusMessageIter *iter) {
1701 assert(name);
1702 assert(iter);
1703
61cbdc4b
LP
1704 /* This is a low-level property printer, see
1705 * print_status_info() for the nicer output */
1706
ea4a240d 1707 if (arg_property && !strv_find(arg_property, name))
48220598
LP
1708 return 0;
1709
1710 switch (dbus_message_iter_get_arg_type(iter)) {
1711
1712 case DBUS_TYPE_STRING: {
1713 const char *s;
1714 dbus_message_iter_get_basic(iter, &s);
1715
1716 if (arg_all || s[0])
1717 printf("%s=%s\n", name, s);
1718
1719 return 0;
1720 }
1721
1722 case DBUS_TYPE_BOOLEAN: {
1723 dbus_bool_t b;
1724 dbus_message_iter_get_basic(iter, &b);
1725 printf("%s=%s\n", name, yes_no(b));
1726
1727 return 0;
1728 }
1729
1730 case DBUS_TYPE_UINT64: {
1731 uint64_t u;
1732 dbus_message_iter_get_basic(iter, &u);
1733
1734 /* Yes, heuristics! But we can change this check
1735 * should it turn out to not be sufficient */
1736
14ad1d14 1737 if (strstr(name, "Timestamp")) {
48220598
LP
1738 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1739
1740 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1741 printf("%s=%s\n", name, strempty(t));
552e4331
LP
1742 } else if (strstr(name, "USec")) {
1743 char timespan[FORMAT_TIMESPAN_MAX];
1744
1745 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
48220598
LP
1746 } else
1747 printf("%s=%llu\n", name, (unsigned long long) u);
1748
1749 return 0;
1750 }
1751
1752 case DBUS_TYPE_UINT32: {
1753 uint32_t u;
1754 dbus_message_iter_get_basic(iter, &u);
1755
5bd07073 1756 if (strstr(name, "UMask") || strstr(name, "Mode"))
48220598
LP
1757 printf("%s=%04o\n", name, u);
1758 else
1759 printf("%s=%u\n", name, (unsigned) u);
1760
1761 return 0;
1762 }
1763
1764 case DBUS_TYPE_INT32: {
1765 int32_t i;
1766 dbus_message_iter_get_basic(iter, &i);
1767
1768 printf("%s=%i\n", name, (int) i);
1769 return 0;
1770 }
1771
1772 case DBUS_TYPE_STRUCT: {
1773 DBusMessageIter sub;
1774 dbus_message_iter_recurse(iter, &sub);
1775
ebf57b80 1776 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
48220598
LP
1777 uint32_t u;
1778
1779 dbus_message_iter_get_basic(&sub, &u);
1780
1781 if (u)
1782 printf("%s=%u\n", name, (unsigned) u);
1783 else if (arg_all)
1784 printf("%s=\n", name);
1785
1786 return 0;
ebf57b80 1787 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
48220598
LP
1788 const char *s;
1789
1790 dbus_message_iter_get_basic(&sub, &s);
1791
1792 if (arg_all || s[0])
1793 printf("%s=%s\n", name, s);
1794
1795 return 0;
1796 }
1797
1798 break;
1799 }
1800
1801 case DBUS_TYPE_ARRAY:
1802
1803 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1804 DBusMessageIter sub;
1805 bool space = false;
1806
1807 dbus_message_iter_recurse(iter, &sub);
48220598
LP
1808 if (arg_all ||
1809 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1810 printf("%s=", name);
1811
1812 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1813 const char *s;
1814
1815 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1816 dbus_message_iter_get_basic(&sub, &s);
1817 printf("%s%s", space ? " " : "", s);
1818
1819 space = true;
1820 dbus_message_iter_next(&sub);
1821 }
1822
1823 puts("");
1824 }
1825
ebf57b80 1826 return 0;
582a507f 1827
82c121a4
LP
1828 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1829 DBusMessageIter sub;
1830
1831 dbus_message_iter_recurse(iter, &sub);
82c121a4
LP
1832 if (arg_all ||
1833 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1834 printf("%s=", name);
1835
1836 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1837 uint8_t u;
ebf57b80 1838
82c121a4
LP
1839 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1840 dbus_message_iter_get_basic(&sub, &u);
1841 printf("%02x", u);
1842
1843 dbus_message_iter_next(&sub);
1844 }
1845
1846 puts("");
1847 }
1848
1849 return 0;
582a507f 1850
ebf57b80
LP
1851 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1852 DBusMessageIter sub, sub2;
1853
1854 dbus_message_iter_recurse(iter, &sub);
ebf57b80
LP
1855 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1856 const char *type, *path;
1857
1858 dbus_message_iter_recurse(&sub, &sub2);
1859
1860 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1861 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1862 printf("%s=%s\n", type, path);
1863
1864 dbus_message_iter_next(&sub);
1865 }
1866
707e5e52 1867 return 0;
582a507f 1868
707e5e52
LP
1869 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1870 DBusMessageIter sub, sub2;
1871
1872 dbus_message_iter_recurse(iter, &sub);
707e5e52
LP
1873 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1874 const char *base;
1875 uint64_t value, next_elapse;
1876
1877 dbus_message_iter_recurse(&sub, &sub2);
1878
1879 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1880 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
552e4331
LP
1881 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1882 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1883
1884 printf("%s={ value=%s ; next_elapse=%s }\n",
fe68089d 1885 base,
552e4331
LP
1886 format_timespan(timespan1, sizeof(timespan1), value),
1887 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1888 }
fe68089d
LP
1889
1890 dbus_message_iter_next(&sub);
1891 }
1892
1893 return 0;
fe68089d 1894
582a507f
LP
1895 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1896 DBusMessageIter sub;
fe68089d
LP
1897
1898 dbus_message_iter_recurse(iter, &sub);
fe68089d 1899 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
582a507f 1900 ExecStatusInfo info;
fe68089d 1901
582a507f
LP
1902 zero(info);
1903 if (exec_status_info_deserialize(&sub, &info) >= 0) {
fe68089d 1904 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
582a507f
LP
1905 char *t;
1906
1907 t = strv_join(info.argv, " ");
1908
b708e7ce 1909 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
1910 name,
1911 strna(info.path),
1912 strna(t),
b708e7ce 1913 yes_no(info.ignore),
582a507f
LP
1914 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1915 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1916 (unsigned) info. pid,
1917 sigchld_code_to_string(info.code),
1918 info.status,
1919 info.code == CLD_EXITED ? "" : "/",
1920 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
fe68089d 1921
582a507f 1922 free(t);
fe68089d
LP
1923 }
1924
582a507f
LP
1925 free(info.path);
1926 strv_free(info.argv);
707e5e52
LP
1927
1928 dbus_message_iter_next(&sub);
1929 }
1930
48220598
LP
1931 return 0;
1932 }
1933
1934 break;
1935 }
1936
1937 if (arg_all)
1938 printf("%s=[unprintable]\n", name);
1939
1940 return 0;
1941}
1942
61cbdc4b 1943static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
48220598
LP
1944 DBusMessage *m = NULL, *reply = NULL;
1945 const char *interface = "";
1946 int r;
1947 DBusError error;
1948 DBusMessageIter iter, sub, sub2, sub3;
61cbdc4b 1949 UnitStatusInfo info;
582a507f 1950 ExecStatusInfo *p;
48220598
LP
1951
1952 assert(bus);
1953 assert(path);
61cbdc4b 1954 assert(new_line);
48220598 1955
61cbdc4b 1956 zero(info);
48220598
LP
1957 dbus_error_init(&error);
1958
1959 if (!(m = dbus_message_new_method_call(
1960 "org.freedesktop.systemd1",
1961 path,
1962 "org.freedesktop.DBus.Properties",
1963 "GetAll"))) {
1964 log_error("Could not allocate message.");
1965 r = -ENOMEM;
1966 goto finish;
1967 }
1968
1969 if (!dbus_message_append_args(m,
1970 DBUS_TYPE_STRING, &interface,
1971 DBUS_TYPE_INVALID)) {
1972 log_error("Could not append arguments to message.");
1973 r = -ENOMEM;
1974 goto finish;
1975 }
1976
1977 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1978 log_error("Failed to issue method call: %s", error.message);
1979 r = -EIO;
1980 goto finish;
1981 }
1982
1983 if (!dbus_message_iter_init(reply, &iter) ||
1984 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1985 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1986 log_error("Failed to parse reply.");
1987 r = -EIO;
1988 goto finish;
1989 }
1990
1991 dbus_message_iter_recurse(&iter, &sub);
1992
61cbdc4b
LP
1993 if (*new_line)
1994 printf("\n");
1995
1996 *new_line = true;
1997
48220598
LP
1998 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1999 const char *name;
2000
2001 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2002 log_error("Failed to parse reply.");
2003 r = -EIO;
2004 goto finish;
2005 }
2006
2007 dbus_message_iter_recurse(&sub, &sub2);
0183528f 2008
48220598
LP
2009 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2010 log_error("Failed to parse reply.");
2011 r = -EIO;
2012 goto finish;
2013 }
2014
2015 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2016 log_error("Failed to parse reply.");
2017 r = -EIO;
2018 goto finish;
2019 }
2020
2021 dbus_message_iter_recurse(&sub2, &sub3);
2022
61cbdc4b
LP
2023 if (show_properties)
2024 r = print_property(name, &sub3);
2025 else
2026 r = status_property(name, &sub3, &info);
2027
2028 if (r < 0) {
48220598
LP
2029 log_error("Failed to parse reply.");
2030 r = -EIO;
2031 goto finish;
2032 }
2033
2034 dbus_message_iter_next(&sub);
2035 }
2036
61cbdc4b
LP
2037 if (!show_properties)
2038 print_status_info(&info);
2039
582a507f
LP
2040 while ((p = info.exec)) {
2041 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2042 exec_status_info_free(p);
2043 }
2044
48220598
LP
2045 r = 0;
2046
2047finish:
2048 if (m)
2049 dbus_message_unref(m);
2050
2051 if (reply)
2052 dbus_message_unref(reply);
2053
2054 dbus_error_free(&error);
2055
2056 return r;
2057}
2058
2059static int show(DBusConnection *bus, char **args, unsigned n) {
2060 DBusMessage *m = NULL, *reply = NULL;
2061 int r;
2062 DBusError error;
2063 unsigned i;
61cbdc4b 2064 bool show_properties, new_line = false;
48220598
LP
2065
2066 assert(bus);
2067 assert(args);
2068
2069 dbus_error_init(&error);
2070
61cbdc4b
LP
2071 show_properties = !streq(args[0], "status");
2072
2073 if (show_properties && n <= 1) {
48220598
LP
2074 /* If not argument is specified inspect the manager
2075 * itself */
2076
61cbdc4b 2077 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
48220598
LP
2078 goto finish;
2079 }
2080
2081 for (i = 1; i < n; i++) {
2082 const char *path = NULL;
2083 uint32_t id;
2084
61cbdc4b 2085 if (!show_properties || safe_atou32(args[i], &id) < 0) {
48220598
LP
2086
2087 if (!(m = dbus_message_new_method_call(
2088 "org.freedesktop.systemd1",
2089 "/org/freedesktop/systemd1",
2090 "org.freedesktop.systemd1.Manager",
e87d1818 2091 "LoadUnit"))) {
48220598
LP
2092 log_error("Could not allocate message.");
2093 r = -ENOMEM;
2094 goto finish;
2095 }
2096
2097 if (!dbus_message_append_args(m,
2098 DBUS_TYPE_STRING, &args[i],
2099 DBUS_TYPE_INVALID)) {
2100 log_error("Could not append arguments to message.");
2101 r = -ENOMEM;
2102 goto finish;
2103 }
2104
ed2d7a44
LP
2105 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2106
2107 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2108 log_error("Failed to issue method call: %s", error.message);
2109 r = -EIO;
2110 goto finish;
2111 }
2112
2113 dbus_error_free(&error);
2114
2115 dbus_message_unref(m);
2116 if (!(m = dbus_message_new_method_call(
2117 "org.freedesktop.systemd1",
2118 "/org/freedesktop/systemd1",
2119 "org.freedesktop.systemd1.Manager",
2120 "GetUnit"))) {
2121 log_error("Could not allocate message.");
2122 r = -ENOMEM;
2123 goto finish;
2124 }
2125
2126 if (!dbus_message_append_args(m,
2127 DBUS_TYPE_STRING, &args[i],
2128 DBUS_TYPE_INVALID)) {
2129 log_error("Could not append arguments to message.");
2130 r = -ENOMEM;
2131 goto finish;
2132 }
2133
2134 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2135 log_error("Failed to issue method call: %s", error.message);
2136 r = -EIO;
2137 goto finish;
2138 }
2139 }
2140
48220598
LP
2141 } else {
2142
2143 if (!(m = dbus_message_new_method_call(
2144 "org.freedesktop.systemd1",
2145 "/org/freedesktop/systemd1",
2146 "org.freedesktop.systemd1.Manager",
2147 "GetJob"))) {
2148 log_error("Could not allocate message.");
2149 r = -ENOMEM;
2150 goto finish;
2151 }
2152
2153 if (!dbus_message_append_args(m,
2154 DBUS_TYPE_UINT32, &id,
2155 DBUS_TYPE_INVALID)) {
2156 log_error("Could not append arguments to message.");
2157 r = -ENOMEM;
2158 goto finish;
2159 }
48220598 2160
ed2d7a44
LP
2161 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2162 log_error("Failed to issue method call: %s", error.message);
2163 r = -EIO;
2164 goto finish;
2165 }
48220598
LP
2166 }
2167
2168 if (!dbus_message_get_args(reply, &error,
2169 DBUS_TYPE_OBJECT_PATH, &path,
2170 DBUS_TYPE_INVALID)) {
2171 log_error("Failed to parse reply: %s", error.message);
2172 r = -EIO;
2173 goto finish;
2174 }
2175
61cbdc4b 2176 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
48220598
LP
2177 goto finish;
2178
2179 dbus_message_unref(m);
2180 dbus_message_unref(reply);
2181 m = reply = NULL;
2182 }
2183
2184 r = 0;
2185
2186finish:
2187 if (m)
2188 dbus_message_unref(m);
2189
2190 if (reply)
2191 dbus_message_unref(reply);
2192
2193 dbus_error_free(&error);
2194
2195 return r;
0183528f
LP
2196}
2197
7e4249b9
LP
2198static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2199 DBusError error;
2200 DBusMessage *m = NULL, *reply = NULL;
2201
2202 assert(connection);
2203 assert(message);
2204
2205 dbus_error_init(&error);
2206
54165a39
LP
2207 log_debug("Got D-Bus request: %s.%s() on %s",
2208 dbus_message_get_interface(message),
2209 dbus_message_get_member(message),
2210 dbus_message_get_path(message));
7e4249b9
LP
2211
2212 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2213 log_error("Warning! D-Bus connection terminated.");
2214 dbus_connection_close(connection);
2215
2216 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2217 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2218 const char *id, *path;
2219
2220 if (!dbus_message_get_args(message, &error,
2221 DBUS_TYPE_STRING, &id,
2222 DBUS_TYPE_OBJECT_PATH, &path,
2223 DBUS_TYPE_INVALID))
2224 log_error("Failed to parse message: %s", error.message);
2225 else if (streq(dbus_message_get_member(message), "UnitNew"))
2226 printf("Unit %s added.\n", id);
2227 else
2228 printf("Unit %s removed.\n", id);
2229
2230 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2231 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2232 uint32_t id;
2233 const char *path;
2234
2235 if (!dbus_message_get_args(message, &error,
2236 DBUS_TYPE_UINT32, &id,
2237 DBUS_TYPE_OBJECT_PATH, &path,
2238 DBUS_TYPE_INVALID))
2239 log_error("Failed to parse message: %s", error.message);
2240 else if (streq(dbus_message_get_member(message), "JobNew"))
2241 printf("Job %u added.\n", id);
2242 else
2243 printf("Job %u removed.\n", id);
2244
2245
2246 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2247 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2248
2249 const char *path, *interface, *property = "Id";
2250 DBusMessageIter iter, sub;
2251
2252 path = dbus_message_get_path(message);
2253 interface = dbus_message_get_interface(message);
2254
2255 if (!(m = dbus_message_new_method_call(
2256 "org.freedesktop.systemd1",
2257 path,
2258 "org.freedesktop.DBus.Properties",
2259 "Get"))) {
2260 log_error("Could not allocate message.");
2261 goto oom;
2262 }
2263
2264 if (!dbus_message_append_args(m,
2265 DBUS_TYPE_STRING, &interface,
2266 DBUS_TYPE_STRING, &property,
2267 DBUS_TYPE_INVALID)) {
2268 log_error("Could not append arguments to message.");
2269 goto finish;
2270 }
2271
2272 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2273 log_error("Failed to issue method call: %s", error.message);
2274 goto finish;
2275 }
2276
2277 if (!dbus_message_iter_init(reply, &iter) ||
2278 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2279 log_error("Failed to parse reply.");
2280 goto finish;
2281 }
2282
2283 dbus_message_iter_recurse(&iter, &sub);
2284
2285 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2286 const char *id;
2287
2288 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2289 log_error("Failed to parse reply.");
2290 goto finish;
2291 }
2292
2293 dbus_message_iter_get_basic(&sub, &id);
2294 printf("Unit %s changed.\n", id);
2295 } else {
2296 uint32_t id;
2297
2298 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2299 log_error("Failed to parse reply.");
2300 goto finish;
2301 }
2302
2303 dbus_message_iter_get_basic(&sub, &id);
2304 printf("Job %u changed.\n", id);
2305 }
2306 }
2307
2308finish:
2309 if (m)
2310 dbus_message_unref(m);
2311
2312 if (reply)
2313 dbus_message_unref(reply);
2314
2315 dbus_error_free(&error);
2316 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2317
2318oom:
2319 if (m)
2320 dbus_message_unref(m);
2321
2322 if (reply)
2323 dbus_message_unref(reply);
2324
2325 dbus_error_free(&error);
2326 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2327}
2328
2329static int monitor(DBusConnection *bus, char **args, unsigned n) {
2330 DBusMessage *m = NULL, *reply = NULL;
2331 DBusError error;
2332 int r;
2333
2334 dbus_error_init(&error);
2335
f4579ce7
LP
2336 if (!private_bus) {
2337 dbus_bus_add_match(bus,
2338 "type='signal',"
2339 "sender='org.freedesktop.systemd1',"
2340 "interface='org.freedesktop.systemd1.Manager',"
2341 "path='/org/freedesktop/systemd1'",
2342 &error);
7e4249b9 2343
f4579ce7
LP
2344 if (dbus_error_is_set(&error)) {
2345 log_error("Failed to add match: %s", error.message);
2346 r = -EIO;
2347 goto finish;
2348 }
7e4249b9 2349
f4579ce7
LP
2350 dbus_bus_add_match(bus,
2351 "type='signal',"
2352 "sender='org.freedesktop.systemd1',"
2353 "interface='org.freedesktop.systemd1.Unit',"
2354 "member='Changed'",
2355 &error);
7e4249b9 2356
f4579ce7
LP
2357 if (dbus_error_is_set(&error)) {
2358 log_error("Failed to add match: %s", error.message);
2359 r = -EIO;
2360 goto finish;
2361 }
7e4249b9 2362
f4579ce7
LP
2363 dbus_bus_add_match(bus,
2364 "type='signal',"
2365 "sender='org.freedesktop.systemd1',"
2366 "interface='org.freedesktop.systemd1.Job',"
2367 "member='Changed'",
2368 &error);
7e4249b9 2369
f4579ce7
LP
2370 if (dbus_error_is_set(&error)) {
2371 log_error("Failed to add match: %s", error.message);
2372 r = -EIO;
2373 goto finish;
2374 }
7e4249b9
LP
2375 }
2376
2377 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2378 log_error("Failed to add filter.");
2379 r = -ENOMEM;
2380 goto finish;
2381 }
2382
2383 if (!(m = dbus_message_new_method_call(
2384 "org.freedesktop.systemd1",
2385 "/org/freedesktop/systemd1",
2386 "org.freedesktop.systemd1.Manager",
2387 "Subscribe"))) {
2388 log_error("Could not allocate message.");
2389 r = -ENOMEM;
2390 goto finish;
2391 }
2392
2393 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2394 log_error("Failed to issue method call: %s", error.message);
2395 r = -EIO;
2396 goto finish;
2397 }
2398
2399 while (dbus_connection_read_write_dispatch(bus, -1))
2400 ;
2401
2402 r = 0;
2403
2404finish:
2405
2406 /* This is slightly dirty, since we don't undo the filter or the matches. */
2407
2408 if (m)
2409 dbus_message_unref(m);
2410
2411 if (reply)
2412 dbus_message_unref(reply);
2413
2414 dbus_error_free(&error);
2415
2416 return r;
2417}
2418
2419static int dump(DBusConnection *bus, char **args, unsigned n) {
2420 DBusMessage *m = NULL, *reply = NULL;
2421 DBusError error;
2422 int r;
2423 const char *text;
2424
2425 dbus_error_init(&error);
2426
2427 if (!(m = dbus_message_new_method_call(
2428 "org.freedesktop.systemd1",
2429 "/org/freedesktop/systemd1",
2430 "org.freedesktop.systemd1.Manager",
2431 "Dump"))) {
2432 log_error("Could not allocate message.");
2433 return -ENOMEM;
2434 }
2435
2436 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2437 log_error("Failed to issue method call: %s", error.message);
2438 r = -EIO;
2439 goto finish;
2440 }
2441
2442 if (!dbus_message_get_args(reply, &error,
2443 DBUS_TYPE_STRING, &text,
2444 DBUS_TYPE_INVALID)) {
2445 log_error("Failed to parse reply: %s", error.message);
2446 r = -EIO;
2447 goto finish;
2448 }
2449
2450 fputs(text, stdout);
2451
2452 r = 0;
2453
2454finish:
2455 if (m)
2456 dbus_message_unref(m);
2457
2458 if (reply)
2459 dbus_message_unref(reply);
2460
2461 dbus_error_free(&error);
2462
2463 return r;
2464}
2465
2466static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2467 DBusMessage *m = NULL, *reply = NULL;
2468 DBusError error;
2469 int r;
2470 const char *name = "", *path, *id;
2471 dbus_bool_t cleanup = FALSE;
2472 DBusMessageIter iter, sub;
2473 const char
2474 *interface = "org.freedesktop.systemd1.Unit",
2475 *property = "Id";
2476
2477 dbus_error_init(&error);
2478
2479 if (!(m = dbus_message_new_method_call(
2480 "org.freedesktop.systemd1",
2481 "/org/freedesktop/systemd1",
2482 "org.freedesktop.systemd1.Manager",
2483 "CreateSnapshot"))) {
2484 log_error("Could not allocate message.");
2485 return -ENOMEM;
2486 }
2487
2488 if (n > 1)
2489 name = args[1];
2490
2491 if (!dbus_message_append_args(m,
2492 DBUS_TYPE_STRING, &name,
2493 DBUS_TYPE_BOOLEAN, &cleanup,
2494 DBUS_TYPE_INVALID)) {
2495 log_error("Could not append arguments to message.");
2496 r = -ENOMEM;
2497 goto finish;
2498 }
2499
2500 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2501 log_error("Failed to issue method call: %s", error.message);
2502 r = -EIO;
2503 goto finish;
2504 }
2505
2506 if (!dbus_message_get_args(reply, &error,
2507 DBUS_TYPE_OBJECT_PATH, &path,
2508 DBUS_TYPE_INVALID)) {
2509 log_error("Failed to parse reply: %s", error.message);
2510 r = -EIO;
2511 goto finish;
2512 }
2513
2514 dbus_message_unref(m);
2515 if (!(m = dbus_message_new_method_call(
2516 "org.freedesktop.systemd1",
2517 path,
2518 "org.freedesktop.DBus.Properties",
2519 "Get"))) {
2520 log_error("Could not allocate message.");
2521 return -ENOMEM;
2522 }
2523
2524 if (!dbus_message_append_args(m,
2525 DBUS_TYPE_STRING, &interface,
2526 DBUS_TYPE_STRING, &property,
2527 DBUS_TYPE_INVALID)) {
2528 log_error("Could not append arguments to message.");
2529 r = -ENOMEM;
2530 goto finish;
2531 }
2532
2533 dbus_message_unref(reply);
2534 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2535 log_error("Failed to issue method call: %s", error.message);
2536 r = -EIO;
2537 goto finish;
2538 }
2539
2540 if (!dbus_message_iter_init(reply, &iter) ||
2541 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2542 log_error("Failed to parse reply.");
2543 r = -EIO;
2544 goto finish;
2545 }
2546
2547 dbus_message_iter_recurse(&iter, &sub);
2548
2549 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2550 log_error("Failed to parse reply.");
2551 r = -EIO;
2552 goto finish;
2553 }
2554
2555 dbus_message_iter_get_basic(&sub, &id);
0183528f
LP
2556
2557 if (!arg_quiet)
2558 puts(id);
7e4249b9
LP
2559 r = 0;
2560
2561finish:
2562 if (m)
2563 dbus_message_unref(m);
2564
2565 if (reply)
2566 dbus_message_unref(reply);
2567
2568 dbus_error_free(&error);
2569
2570 return r;
2571}
2572
6759e7a7
LP
2573static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2574 DBusMessage *m = NULL, *reply = NULL;
2575 int r;
2576 DBusError error;
2577 unsigned i;
2578
2579 assert(bus);
2580 assert(args);
2581
2582 dbus_error_init(&error);
2583
2584 for (i = 1; i < n; i++) {
2585 const char *path = NULL;
2586
2587 if (!(m = dbus_message_new_method_call(
2588 "org.freedesktop.systemd1",
2589 "/org/freedesktop/systemd1",
2590 "org.freedesktop.systemd1.Manager",
2591 "GetUnit"))) {
2592 log_error("Could not allocate message.");
2593 r = -ENOMEM;
2594 goto finish;
2595 }
2596
2597 if (!dbus_message_append_args(m,
2598 DBUS_TYPE_STRING, &args[i],
2599 DBUS_TYPE_INVALID)) {
2600 log_error("Could not append arguments to message.");
2601 r = -ENOMEM;
2602 goto finish;
2603 }
2604
2605 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2606 log_error("Failed to issue method call: %s", error.message);
2607 r = -EIO;
2608 goto finish;
2609 }
2610
2611 if (!dbus_message_get_args(reply, &error,
2612 DBUS_TYPE_OBJECT_PATH, &path,
2613 DBUS_TYPE_INVALID)) {
2614 log_error("Failed to parse reply: %s", error.message);
2615 r = -EIO;
2616 goto finish;
2617 }
2618
2619 dbus_message_unref(m);
2620 if (!(m = dbus_message_new_method_call(
2621 "org.freedesktop.systemd1",
2622 path,
2623 "org.freedesktop.systemd1.Snapshot",
2624 "Remove"))) {
2625 log_error("Could not allocate message.");
2626 r = -ENOMEM;
2627 goto finish;
2628 }
2629
2630 dbus_message_unref(reply);
2631 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2632 log_error("Failed to issue method call: %s", error.message);
2633 r = -EIO;
2634 goto finish;
2635 }
2636
2637 dbus_message_unref(m);
2638 dbus_message_unref(reply);
2639 m = reply = NULL;
2640 }
2641
2642 r = 0;
2643
2644finish:
2645 if (m)
2646 dbus_message_unref(m);
2647
2648 if (reply)
2649 dbus_message_unref(reply);
2650
2651 dbus_error_free(&error);
2652
2653 return r;
2654}
2655
7e4249b9
LP
2656static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2657 DBusMessage *m = NULL, *reply = NULL;
2658 DBusError error;
2659 int r;
2660 const char *method;
2661
2662 dbus_error_init(&error);
2663
e4b61340
LP
2664 if (arg_action == ACTION_RELOAD)
2665 method = "Reload";
2666 else if (arg_action == ACTION_REEXEC)
2667 method = "Reexecute";
2668 else {
2669 assert(arg_action == ACTION_SYSTEMCTL);
2670
2671 method =
5632e374
LP
2672 streq(args[0], "clear-jobs") ? "ClearJobs" :
2673 streq(args[0], "daemon-reload") ? "Reload" :
2674 streq(args[0], "daemon-reexec") ? "Reexecute" :
2675 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2676 "Exit";
e4b61340 2677 }
7e4249b9
LP
2678
2679 if (!(m = dbus_message_new_method_call(
2680 "org.freedesktop.systemd1",
2681 "/org/freedesktop/systemd1",
2682 "org.freedesktop.systemd1.Manager",
2683 method))) {
2684 log_error("Could not allocate message.");
2685 return -ENOMEM;
2686 }
2687
2688 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
e4b61340
LP
2689
2690 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2691 /* There's always a fallback possible for
2692 * legacy actions. */
2693 r = 0;
2694 goto finish;
2695 }
2696
7e4249b9
LP
2697 log_error("Failed to issue method call: %s", error.message);
2698 r = -EIO;
2699 goto finish;
2700 }
2701
e4b61340 2702 r = 1;
7e4249b9
LP
2703
2704finish:
2705 if (m)
2706 dbus_message_unref(m);
2707
2708 if (reply)
2709 dbus_message_unref(reply);
2710
2711 dbus_error_free(&error);
2712
2713 return r;
2714}
2715
5632e374
LP
2716static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2717 DBusMessage *m = NULL, *reply = NULL;
2718 unsigned i;
2719 int r;
2720 DBusError error;
2721
2722 assert(bus);
2723 dbus_error_init(&error);
2724
2725 if (n <= 1)
2726 return clear_jobs(bus, args, n);
2727
2728 for (i = 1; i < n; i++) {
2729
2730 if (!(m = dbus_message_new_method_call(
2731 "org.freedesktop.systemd1",
2732 "/org/freedesktop/systemd1",
2733 "org.freedesktop.systemd1.Manager",
2734 "ResetMaintenanceUnit"))) {
2735 log_error("Could not allocate message.");
2736 r = -ENOMEM;
2737 goto finish;
2738 }
2739
2740 if (!dbus_message_append_args(m,
2741 DBUS_TYPE_STRING, args + i,
2742 DBUS_TYPE_INVALID)) {
2743 log_error("Could not append arguments to message.");
2744 r = -ENOMEM;
2745 goto finish;
2746 }
2747
2748 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2749 log_error("Failed to issue method call: %s", error.message);
2750 r = -EIO;
2751 goto finish;
2752 }
2753
2754 dbus_message_unref(m);
2755 dbus_message_unref(reply);
2756 m = reply = NULL;
2757 }
2758
2759 r = 0;
2760
2761finish:
2762 if (m)
2763 dbus_message_unref(m);
2764
2765 if (reply)
2766 dbus_message_unref(reply);
2767
2768 dbus_error_free(&error);
2769
2770 return r;
2771}
2772
7e4249b9
LP
2773static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2774 DBusMessage *m = NULL, *reply = NULL;
2775 DBusError error;
2776 DBusMessageIter iter, sub, sub2;
2777 int r;
2778 const char
2779 *interface = "org.freedesktop.systemd1.Manager",
2780 *property = "Environment";
2781
2782 dbus_error_init(&error);
2783
2784 if (!(m = dbus_message_new_method_call(
2785 "org.freedesktop.systemd1",
2786 "/org/freedesktop/systemd1",
2787 "org.freedesktop.DBus.Properties",
2788 "Get"))) {
2789 log_error("Could not allocate message.");
2790 return -ENOMEM;
2791 }
2792
2793 if (!dbus_message_append_args(m,
2794 DBUS_TYPE_STRING, &interface,
2795 DBUS_TYPE_STRING, &property,
2796 DBUS_TYPE_INVALID)) {
2797 log_error("Could not append arguments to message.");
2798 r = -ENOMEM;
2799 goto finish;
2800 }
2801
2802 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2803 log_error("Failed to issue method call: %s", error.message);
2804 r = -EIO;
2805 goto finish;
2806 }
2807
2808 if (!dbus_message_iter_init(reply, &iter) ||
2809 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2810 log_error("Failed to parse reply.");
2811 r = -EIO;
2812 goto finish;
2813 }
2814
2815 dbus_message_iter_recurse(&iter, &sub);
2816
2817 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2818 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2819 log_error("Failed to parse reply.");
2820 r = -EIO;
2821 goto finish;
2822 }
2823
2824 dbus_message_iter_recurse(&sub, &sub2);
2825
2826 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2827 const char *text;
2828
2829 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2830 log_error("Failed to parse reply.");
2831 r = -EIO;
2832 goto finish;
2833 }
2834
2835 dbus_message_iter_get_basic(&sub2, &text);
2836 printf("%s\n", text);
2837
2838 dbus_message_iter_next(&sub2);
2839 }
2840
2841 r = 0;
2842
2843finish:
2844 if (m)
2845 dbus_message_unref(m);
2846
2847 if (reply)
2848 dbus_message_unref(reply);
2849
2850 dbus_error_free(&error);
2851
2852 return r;
2853}
2854
2855static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2856 DBusMessage *m = NULL, *reply = NULL;
2857 DBusError error;
2858 int r;
2859 const char *method;
2860 DBusMessageIter iter, sub;
2861 unsigned i;
2862
2863 dbus_error_init(&error);
2864
2865 method = streq(args[0], "set-environment")
2866 ? "SetEnvironment"
2867 : "UnsetEnvironment";
2868
2869 if (!(m = dbus_message_new_method_call(
2870 "org.freedesktop.systemd1",
2871 "/org/freedesktop/systemd1",
2872 "org.freedesktop.systemd1.Manager",
2873 method))) {
2874
2875 log_error("Could not allocate message.");
2876 return -ENOMEM;
2877 }
2878
2879 dbus_message_iter_init_append(m, &iter);
2880
2881 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2882 log_error("Could not append arguments to message.");
2883 r = -ENOMEM;
2884 goto finish;
2885 }
2886
2887 for (i = 1; i < n; i++)
2888 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2889 log_error("Could not append arguments to message.");
2890 r = -ENOMEM;
2891 goto finish;
2892 }
2893
2894 if (!dbus_message_iter_close_container(&iter, &sub)) {
2895 log_error("Could not append arguments to message.");
2896 r = -ENOMEM;
2897 goto finish;
2898 }
2899
2900 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2901 log_error("Failed to issue method call: %s", error.message);
2902 r = -EIO;
2903 goto finish;
2904 }
2905
2906 r = 0;
2907
2908finish:
2909 if (m)
2910 dbus_message_unref(m);
2911
2912 if (reply)
2913 dbus_message_unref(reply);
2914
2915 dbus_error_free(&error);
2916
2917 return r;
2918}
2919
e4b61340 2920static int systemctl_help(void) {
7e4249b9 2921
2e33c433 2922 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
5632e374 2923 "Send control commands to or query the systemd manager.\n\n"
48220598
LP
2924 " -h --help Show this help\n"
2925 " -t --type=TYPE List only units of a particular type\n"
393a2f9b 2926 " -p --property=NAME Show only properties by this name\n"
48220598 2927 " -a --all Show all units/properties, including dead/empty ones\n"
8fe914ec 2928 " --full Don't ellipsize unit names.\n"
6f28c033
LP
2929 " --fail When installing a new job, fail if conflicting jobs are\n"
2930 " pending\n"
48220598
LP
2931 " --system Connect to system bus\n"
2932 " --session Connect to session bus\n"
4445a875
LP
2933 " --order When generating graph for dot, show only order\n"
2934 " --require When generating graph for dot, show only requirement\n"
48220598
LP
2935 " -q --quiet Suppress output\n"
2936 " --no-block Do not wait until operation finished\n"
2937 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
7e4249b9
LP
2938 "Commands:\n"
2939 " list-units List units\n"
7e4249b9
LP
2940 " start [NAME...] Start one or more units\n"
2941 " stop [NAME...] Stop one or more units\n"
7e4249b9 2942 " reload [NAME...] Reload one or more units\n"
6f28c033
LP
2943 " restart [NAME...] Start or restart one or more units\n"
2944 " try-restart [NAME...] Restart one or more units if active\n"
2945 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2946 " otherwise start or restart\n"
2947 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2948 " otherwise restart if active\n"
7e4249b9 2949 " isolate [NAME] Start one unit and stop all others\n"
6f28c033 2950 " check [NAME...] Check whether units are active\n"
61cbdc4b 2951 " status [NAME...] Show status of one or more units\n"
6f28c033
LP
2952 " show [NAME...|JOB...] Show properties of one or more\n"
2953 " units/jobs/manager\n"
5632e374
LP
2954 " reset-maintenance [NAME...] Reset maintenance state for all, one\n"
2955 " or more units\n"
48220598
LP
2956 " load [NAME...] Load one or more units\n"
2957 " list-jobs List jobs\n"
2958 " cancel [JOB...] Cancel one or more jobs\n"
2959 " clear-jobs Cancel all jobs\n"
7e4249b9
LP
2960 " monitor Monitor unit/job changes\n"
2961 " dump Dump server status\n"
4445a875 2962 " dot Dump dependency graph for dot(1)\n"
7e4249b9 2963 " snapshot [NAME] Create a snapshot\n"
6759e7a7 2964 " delete [NAME...] Remove one or more snapshots\n"
5ec7ed4e
LP
2965 " daemon-reload Reload systemd manager configuration\n"
2966 " daemon-reexec Reexecute systemd manager\n"
2967 " daemon-exit Ask the systemd manager to quit\n"
7e4249b9
LP
2968 " show-environment Dump environment\n"
2969 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
514f4ef5
LP
2970 " unset-environment [NAME...] Unset one or more environment variables\n"
2971 " halt Shut down and halt the system\n"
2e33c433 2972 " poweroff Shut down and power-off the system\n"
514f4ef5 2973 " reboot Shut down and reboot the system\n"
6f28c033
LP
2974 " rescue Enter system rescue mode\n"
2975 " emergency Enter system emergency mode\n"
2976 " default Enter system default mode\n",
5b6319dc 2977 program_invocation_short_name);
7e4249b9
LP
2978
2979 return 0;
2980}
2981
e4b61340
LP
2982static int halt_help(void) {
2983
2e33c433 2984 printf("%s [OPTIONS...]\n\n"
e4b61340
LP
2985 "%s the system.\n\n"
2986 " --help Show this help\n"
2987 " --halt Halt the machine\n"
2988 " -p --poweroff Switch off the machine\n"
2989 " --reboot Reboot the machine\n"
2e33c433
LP
2990 " -f --force Force immediate halt/power-off/reboot\n"
2991 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
e4b61340 2992 " -d --no-wtmp Don't write wtmp record\n"
2e33c433
LP
2993 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2994 " --no-wall Don't send wall message before halt/power-off/reboot\n",
e4b61340
LP
2995 program_invocation_short_name,
2996 arg_action == ACTION_REBOOT ? "Reboot" :
2997 arg_action == ACTION_POWEROFF ? "Power off" :
2998 "Halt");
2999
3000 return 0;
3001}
3002
3003static int shutdown_help(void) {
3004
2e33c433 3005 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
e4b61340
LP
3006 "Shut down the system.\n\n"
3007 " --help Show this help\n"
3008 " -H --halt Halt the machine\n"
3009 " -P --poweroff Power-off the machine\n"
3010 " -r --reboot Reboot the machine\n"
3011 " -h Equivalent to --poweroff, overriden by --halt\n"
2e33c433
LP
3012 " -k Don't halt/power-off/reboot, just send warnings\n"
3013 " --no-wall Don't send wall message before halt/power-off/reboot\n",
e4b61340
LP
3014 program_invocation_short_name);
3015
3016 return 0;
3017}
3018
3019static int telinit_help(void) {
3020
2e33c433 3021 printf("%s [OPTIONS...] {COMMAND}\n\n"
514f4ef5
LP
3022 "Send control commands to the init daemon.\n\n"
3023 " --help Show this help\n"
2e33c433 3024 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
e4b61340
LP
3025 "Commands:\n"
3026 " 0 Power-off the machine\n"
3027 " 6 Reboot the machine\n"
514f4ef5
LP
3028 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3029 " 1, s, S Enter rescue mode\n"
3030 " q, Q Reload init daemon configuration\n"
3031 " u, U Reexecute init daemon\n",
e4b61340
LP
3032 program_invocation_short_name);
3033
3034 return 0;
3035}
3036
3037static int runlevel_help(void) {
3038
2e33c433 3039 printf("%s [OPTIONS...]\n\n"
e4b61340
LP
3040 "Prints the previous and current runlevel of the init system.\n\n"
3041 " --help Show this help\n",
3042 program_invocation_short_name);
3043
3044 return 0;
3045}
3046
3047static int systemctl_parse_argv(int argc, char *argv[]) {
7e4249b9
LP
3048
3049 enum {
90d473a1 3050 ARG_FAIL = 0x100,
7e4249b9
LP
3051 ARG_SESSION,
3052 ARG_SYSTEM,
6e905d93 3053 ARG_NO_BLOCK,
4445a875
LP
3054 ARG_NO_WALL,
3055 ARG_ORDER,
8fe914ec
LP
3056 ARG_REQUIRE,
3057 ARG_FULL
7e4249b9
LP
3058 };
3059
3060 static const struct option options[] = {
6e905d93
LP
3061 { "help", no_argument, NULL, 'h' },
3062 { "type", required_argument, NULL, 't' },
48220598 3063 { "property", required_argument, NULL, 'p' },
6e905d93 3064 { "all", no_argument, NULL, 'a' },
8fe914ec 3065 { "full", no_argument, NULL, ARG_FULL },
90d473a1 3066 { "fail", no_argument, NULL, ARG_FAIL },
6e905d93
LP
3067 { "session", no_argument, NULL, ARG_SESSION },
3068 { "system", no_argument, NULL, ARG_SYSTEM },
3069 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3070 { "no-wall", no_argument, NULL, ARG_NO_WALL },
0183528f 3071 { "quiet", no_argument, NULL, 'q' },
4445a875
LP
3072 { "order", no_argument, NULL, ARG_ORDER },
3073 { "require", no_argument, NULL, ARG_REQUIRE },
6e905d93 3074 { NULL, 0, NULL, 0 }
7e4249b9
LP
3075 };
3076
3077 int c;
3078
e4b61340 3079 assert(argc >= 0);
7e4249b9
LP
3080 assert(argv);
3081
48220598 3082 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
7e4249b9
LP
3083
3084 switch (c) {
3085
3086 case 'h':
e4b61340 3087 systemctl_help();
7e4249b9
LP
3088 return 0;
3089
3090 case 't':
3091 arg_type = optarg;
3092 break;
3093
ea4a240d
LP
3094 case 'p': {
3095 char **l;
3096
3097 if (!(l = strv_append(arg_property, optarg)))
3098 return -ENOMEM;
3099
3100 strv_free(arg_property);
3101 arg_property = l;
48220598
LP
3102
3103 /* If the user asked for a particular
3104 * property, show it to him, even if it is
3105 * empty. */
3106 arg_all = true;
3107 break;
ea4a240d 3108 }
48220598 3109
7e4249b9
LP
3110 case 'a':
3111 arg_all = true;
3112 break;
3113
90d473a1
LP
3114 case ARG_FAIL:
3115 arg_fail = true;
7e4249b9
LP
3116 break;
3117
3118 case ARG_SESSION:
3119 arg_session = true;
3120 break;
3121
3122 case ARG_SYSTEM:
3123 arg_session = false;
3124 break;
3125
6e905d93
LP
3126 case ARG_NO_BLOCK:
3127 arg_no_block = true;
7e4249b9
LP
3128 break;
3129
514f4ef5
LP
3130 case ARG_NO_WALL:
3131 arg_no_wall = true;
3132 break;
3133
4445a875
LP
3134 case ARG_ORDER:
3135 arg_dot = DOT_ORDER;
3136 break;
3137
3138 case ARG_REQUIRE:
3139 arg_dot = DOT_REQUIRE;
3140 break;
3141
8fe914ec
LP
3142 case ARG_FULL:
3143 arg_full = true;
3144 break;
3145
0183528f
LP
3146 case 'q':
3147 arg_quiet = true;
3148 break;
3149
7e4249b9
LP
3150 case '?':
3151 return -EINVAL;
3152
3153 default:
3154 log_error("Unknown option code %c", c);
3155 return -EINVAL;
3156 }
3157 }
3158
3159 return 1;
3160}
3161
e4b61340
LP
3162static int halt_parse_argv(int argc, char *argv[]) {
3163
3164 enum {
3165 ARG_HELP = 0x100,
3166 ARG_HALT,
514f4ef5
LP
3167 ARG_REBOOT,
3168 ARG_NO_WALL
e4b61340
LP
3169 };
3170
3171 static const struct option options[] = {
3172 { "help", no_argument, NULL, ARG_HELP },
3173 { "halt", no_argument, NULL, ARG_HALT },
3174 { "poweroff", no_argument, NULL, 'p' },
3175 { "reboot", no_argument, NULL, ARG_REBOOT },
3176 { "force", no_argument, NULL, 'f' },
3177 { "wtmp-only", no_argument, NULL, 'w' },
3178 { "no-wtmp", no_argument, NULL, 'd' },
3179 { "no-sync", no_argument, NULL, 'n' },
514f4ef5 3180 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
3181 { NULL, 0, NULL, 0 }
3182 };
3183
3184 int c, runlevel;
3185
3186 assert(argc >= 0);
3187 assert(argv);
3188
3189 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3190 if (runlevel == '0' || runlevel == '6')
3191 arg_immediate = true;
3192
3193 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3194 switch (c) {
3195
3196 case ARG_HELP:
3197 halt_help();
3198 return 0;
3199
3200 case ARG_HALT:
3201 arg_action = ACTION_HALT;
3202 break;
3203
3204 case 'p':
3205 arg_action = ACTION_POWEROFF;
3206 break;
3207
3208 case ARG_REBOOT:
3209 arg_action = ACTION_REBOOT;
3210 break;
3211
3212 case 'f':
3213 arg_immediate = true;
3214 break;
3215
3216 case 'w':
3217 arg_dry = true;
3218 break;
3219
3220 case 'd':
3221 arg_no_wtmp = true;
3222 break;
3223
3224 case 'n':
3225 arg_no_sync = true;
3226 break;
3227
514f4ef5
LP
3228 case ARG_NO_WALL:
3229 arg_no_wall = true;
3230 break;
3231
e4b61340
LP
3232 case 'i':
3233 case 'h':
3234 /* Compatibility nops */
3235 break;
3236
3237 case '?':
3238 return -EINVAL;
3239
3240 default:
3241 log_error("Unknown option code %c", c);
3242 return -EINVAL;
3243 }
3244 }
3245
3246 if (optind < argc) {
3247 log_error("Too many arguments.");
3248 return -EINVAL;
3249 }
3250
3251 return 1;
3252}
3253
3254static int shutdown_parse_argv(int argc, char *argv[]) {
3255
3256 enum {
3257 ARG_HELP = 0x100,
514f4ef5 3258 ARG_NO_WALL
e4b61340
LP
3259 };
3260
3261 static const struct option options[] = {
3262 { "help", no_argument, NULL, ARG_HELP },
3263 { "halt", no_argument, NULL, 'H' },
3264 { "poweroff", no_argument, NULL, 'P' },
3265 { "reboot", no_argument, NULL, 'r' },
514f4ef5 3266 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
3267 { NULL, 0, NULL, 0 }
3268 };
3269
3270 int c;
3271
3272 assert(argc >= 0);
3273 assert(argv);
3274
3275 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3276 switch (c) {
3277
3278 case ARG_HELP:
3279 shutdown_help();
3280 return 0;
3281
3282 case 'H':
3283 arg_action = ACTION_HALT;
3284 break;
3285
3286 case 'P':
3287 arg_action = ACTION_POWEROFF;
3288 break;
3289
3290 case 'r':
3291 arg_action = ACTION_REBOOT;
3292 break;
3293
3294 case 'h':
3295 if (arg_action != ACTION_HALT)
3296 arg_action = ACTION_POWEROFF;
3297 break;
3298
3299 case 'k':
3300 arg_dry = true;
3301 break;
3302
514f4ef5
LP
3303 case ARG_NO_WALL:
3304 arg_no_wall = true;
3305 break;
3306
e4b61340
LP
3307 case 't':
3308 case 'a':
3309 /* Compatibility nops */
3310 break;
3311
3312 case '?':
3313 return -EINVAL;
3314
3315 default:
3316 log_error("Unknown option code %c", c);
3317 return -EINVAL;
3318 }
3319 }
3320
4545812f
LP
3321 if (argc > optind && !streq(argv[optind], "now"))
3322 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
442b9094 3323
e4b61340
LP
3324 /* We ignore the time argument */
3325 if (argc > optind + 1)
3326 arg_wall = argv + optind + 1;
3327
3328 optind = argc;
3329
3330 return 1;
e4b61340
LP
3331}
3332
3333static int telinit_parse_argv(int argc, char *argv[]) {
3334
3335 enum {
3336 ARG_HELP = 0x100,
514f4ef5 3337 ARG_NO_WALL
e4b61340
LP
3338 };
3339
3340 static const struct option options[] = {
3341 { "help", no_argument, NULL, ARG_HELP },
514f4ef5 3342 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
3343 { NULL, 0, NULL, 0 }
3344 };
3345
3346 static const struct {
3347 char from;
3348 enum action to;
3349 } table[] = {
3350 { '0', ACTION_POWEROFF },
3351 { '6', ACTION_REBOOT },
ef2f1067 3352 { '1', ACTION_RESCUE },
e4b61340
LP
3353 { '2', ACTION_RUNLEVEL2 },
3354 { '3', ACTION_RUNLEVEL3 },
3355 { '4', ACTION_RUNLEVEL4 },
3356 { '5', ACTION_RUNLEVEL5 },
3357 { 's', ACTION_RESCUE },
3358 { 'S', ACTION_RESCUE },
3359 { 'q', ACTION_RELOAD },
3360 { 'Q', ACTION_RELOAD },
3361 { 'u', ACTION_REEXEC },
3362 { 'U', ACTION_REEXEC }
3363 };
3364
3365 unsigned i;
3366 int c;
3367
3368 assert(argc >= 0);
3369 assert(argv);
3370
3371 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3372 switch (c) {
3373
3374 case ARG_HELP:
3375 telinit_help();
3376 return 0;
3377
514f4ef5
LP
3378 case ARG_NO_WALL:
3379 arg_no_wall = true;
3380 break;
3381
e4b61340
LP
3382 case '?':
3383 return -EINVAL;
3384
3385 default:
3386 log_error("Unknown option code %c", c);
3387 return -EINVAL;
3388 }
3389 }
3390
3391 if (optind >= argc) {
2f02ce40 3392 telinit_help();
e4b61340
LP
3393 return -EINVAL;
3394 }
3395
3396 if (optind + 1 < argc) {
3397 log_error("Too many arguments.");
3398 return -EINVAL;
3399 }
3400
3401 if (strlen(argv[optind]) != 1) {
3402 log_error("Expected single character argument.");
3403 return -EINVAL;
3404 }
3405
3406 for (i = 0; i < ELEMENTSOF(table); i++)
3407 if (table[i].from == argv[optind][0])
3408 break;
3409
3410 if (i >= ELEMENTSOF(table)) {
3411 log_error("Unknown command %s.", argv[optind]);
3412 return -EINVAL;
3413 }
3414
3415 arg_action = table[i].to;
3416
3417 optind ++;
3418
3419 return 1;
3420}
3421
3422static int runlevel_parse_argv(int argc, char *argv[]) {
3423
3424 enum {
3425 ARG_HELP = 0x100,
3426 };
3427
3428 static const struct option options[] = {
3429 { "help", no_argument, NULL, ARG_HELP },
3430 { NULL, 0, NULL, 0 }
3431 };
3432
3433 int c;
3434
3435 assert(argc >= 0);
3436 assert(argv);
3437
3438 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3439 switch (c) {
3440
3441 case ARG_HELP:
3442 runlevel_help();
3443 return 0;
3444
3445 case '?':
3446 return -EINVAL;
3447
3448 default:
3449 log_error("Unknown option code %c", c);
3450 return -EINVAL;
3451 }
3452 }
3453
3454 if (optind < argc) {
3455 log_error("Too many arguments.");
3456 return -EINVAL;
3457 }
3458
3459 return 1;
3460}
3461
3462static int parse_argv(int argc, char *argv[]) {
3463 assert(argc >= 0);
3464 assert(argv);
3465
3466 if (program_invocation_short_name) {
3467
3468 if (strstr(program_invocation_short_name, "halt")) {
3469 arg_action = ACTION_HALT;
3470 return halt_parse_argv(argc, argv);
3471 } else if (strstr(program_invocation_short_name, "poweroff")) {
3472 arg_action = ACTION_POWEROFF;
3473 return halt_parse_argv(argc, argv);
3474 } else if (strstr(program_invocation_short_name, "reboot")) {
3475 arg_action = ACTION_REBOOT;
3476 return halt_parse_argv(argc, argv);
3477 } else if (strstr(program_invocation_short_name, "shutdown")) {
3478 arg_action = ACTION_POWEROFF;
3479 return shutdown_parse_argv(argc, argv);
3480 } else if (strstr(program_invocation_short_name, "init")) {
3481 arg_action = ACTION_INVALID;
3482 return telinit_parse_argv(argc, argv);
3483 } else if (strstr(program_invocation_short_name, "runlevel")) {
3484 arg_action = ACTION_RUNLEVEL;
3485 return runlevel_parse_argv(argc, argv);
3486 }
3487 }
3488
3489 arg_action = ACTION_SYSTEMCTL;
3490 return systemctl_parse_argv(argc, argv);
3491}
3492
d55ae9e6 3493static int action_to_runlevel(void) {
eb22ac37
LP
3494
3495 static const char table[_ACTION_MAX] = {
3496 [ACTION_HALT] = '0',
3497 [ACTION_POWEROFF] = '0',
3498 [ACTION_REBOOT] = '6',
3499 [ACTION_RUNLEVEL2] = '2',
3500 [ACTION_RUNLEVEL3] = '3',
3501 [ACTION_RUNLEVEL4] = '4',
3502 [ACTION_RUNLEVEL5] = '5',
3503 [ACTION_RESCUE] = '1'
3504 };
3505
d55ae9e6
LP
3506 assert(arg_action < _ACTION_MAX);
3507
3508 return table[arg_action];
3509}
3510
f1c5860b 3511static int talk_upstart(void) {
d55ae9e6
LP
3512 DBusMessage *m = NULL, *reply = NULL;
3513 DBusError error;
3514 int previous, rl, r;
3515 char
3516 env1_buf[] = "RUNLEVEL=X",
3517 env2_buf[] = "PREVLEVEL=X";
3518 char *env1 = env1_buf, *env2 = env2_buf;
3519 const char *emit = "runlevel";
3520 dbus_bool_t b_false = FALSE;
3521 DBusMessageIter iter, sub;
f1c5860b 3522 DBusConnection *bus;
d55ae9e6
LP
3523
3524 dbus_error_init(&error);
3525
3526 if (!(rl = action_to_runlevel()))
3527 return 0;
3528
3529 if (utmp_get_runlevel(&previous, NULL) < 0)
3530 previous = 'N';
3531
b574246b 3532 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
f1c5860b
LP
3533 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3534 r = 0;
3535 goto finish;
3536 }
3537
3538 log_error("Failed to connect to Upstart bus: %s", error.message);
3539 r = -EIO;
3540 goto finish;
3541 }
3542
3543 if ((r = bus_check_peercred(bus)) < 0) {
3544 log_error("Failed to verify owner of bus.");
3545 goto finish;
3546 }
3547
d55ae9e6
LP
3548 if (!(m = dbus_message_new_method_call(
3549 "com.ubuntu.Upstart",
3550 "/com/ubuntu/Upstart",
3551 "com.ubuntu.Upstart0_6",
3552 "EmitEvent"))) {
3553
3554 log_error("Could not allocate message.");
f1c5860b
LP
3555 r = -ENOMEM;
3556 goto finish;
d55ae9e6
LP
3557 }
3558
3559 dbus_message_iter_init_append(m, &iter);
3560
3561 env1_buf[sizeof(env1_buf)-2] = rl;
3562 env2_buf[sizeof(env2_buf)-2] = previous;
3563
3564 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3565 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3566 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3567 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3568 !dbus_message_iter_close_container(&iter, &sub) ||
3569 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3570 log_error("Could not append arguments to message.");
3571 r = -ENOMEM;
3572 goto finish;
3573 }
3574
3575 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3576
3577 if (error_is_no_service(&error)) {
3578 r = 0;
3579 goto finish;
3580 }
3581
3582 log_error("Failed to issue method call: %s", error.message);
3583 r = -EIO;
3584 goto finish;
3585 }
3586
3587 r = 1;
3588
3589finish:
3590 if (m)
3591 dbus_message_unref(m);
3592
3593 if (reply)
3594 dbus_message_unref(reply);
3595
b574246b
LP
3596 if (bus) {
3597 dbus_connection_close(bus);
f1c5860b 3598 dbus_connection_unref(bus);
b574246b 3599 }
f1c5860b 3600
d55ae9e6
LP
3601 dbus_error_free(&error);
3602
3603 return r;
3604}
3605
3606static int talk_initctl(void) {
eb22ac37
LP
3607 struct init_request request;
3608 int r, fd;
d55ae9e6 3609 char rl;
eb22ac37 3610
d55ae9e6 3611 if (!(rl = action_to_runlevel()))
eb22ac37
LP
3612 return 0;
3613
3614 zero(request);
3615 request.magic = INIT_MAGIC;
3616 request.sleeptime = 0;
3617 request.cmd = INIT_CMD_RUNLVL;
d55ae9e6
LP
3618 request.runlevel = rl;
3619
3620 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3621
3622 if (errno == ENOENT)
3623 return 0;
eb22ac37 3624
d55ae9e6 3625 log_error("Failed to open "INIT_FIFO": %m");
eb22ac37 3626 return -errno;
d55ae9e6 3627 }
eb22ac37 3628
d55ae9e6 3629 errno = 0;
eb22ac37
LP
3630 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3631 close_nointr_nofail(fd);
3632
d55ae9e6
LP
3633 if (r < 0) {
3634 log_error("Failed to write to "INIT_FIFO": %m");
eb22ac37 3635 return errno ? -errno : -EIO;
d55ae9e6 3636 }
eb22ac37
LP
3637
3638 return 1;
e4b61340
LP
3639}
3640
3641static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
7e4249b9 3642
7e4249b9
LP
3643 static const struct {
3644 const char* verb;
3645 const enum {
3646 MORE,
3647 LESS,
3648 EQUAL
3649 } argc_cmp;
3650 const int argc;
3651 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3652 } verbs[] = {
3653 { "list-units", LESS, 1, list_units },
3654 { "list-jobs", EQUAL, 1, list_jobs },
3655 { "clear-jobs", EQUAL, 1, clear_jobs },
3656 { "load", MORE, 2, load_unit },
3657 { "cancel", MORE, 2, cancel_job },
3658 { "start", MORE, 2, start_unit },
3659 { "stop", MORE, 2, start_unit },
3660 { "reload", MORE, 2, start_unit },
3661 { "restart", MORE, 2, start_unit },
6f28c033
LP
3662 { "try-restart", MORE, 2, start_unit },
3663 { "reload-or-restart", MORE, 2, start_unit },
3664 { "reload-or-try-restart", MORE, 2, start_unit },
9d8a57ff
LP
3665 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
3666 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
e4b61340 3667 { "isolate", EQUAL, 2, start_unit },
0183528f 3668 { "check", MORE, 2, check_unit },
48220598 3669 { "show", MORE, 1, show },
61cbdc4b 3670 { "status", MORE, 2, show },
7e4249b9
LP
3671 { "monitor", EQUAL, 1, monitor },
3672 { "dump", EQUAL, 1, dump },
4445a875 3673 { "dot", EQUAL, 1, dot },
7e4249b9 3674 { "snapshot", LESS, 2, snapshot },
6759e7a7 3675 { "delete", MORE, 2, delete_snapshot },
7e4249b9
LP
3676 { "daemon-reload", EQUAL, 1, clear_jobs },
3677 { "daemon-reexec", EQUAL, 1, clear_jobs },
3678 { "daemon-exit", EQUAL, 1, clear_jobs },
3679 { "show-environment", EQUAL, 1, show_enviroment },
3680 { "set-environment", MORE, 2, set_environment },
3681 { "unset-environment", MORE, 2, set_environment },
514f4ef5
LP
3682 { "halt", EQUAL, 1, start_special },
3683 { "poweroff", EQUAL, 1, start_special },
3684 { "reboot", EQUAL, 1, start_special },
3685 { "default", EQUAL, 1, start_special },
3686 { "rescue", EQUAL, 1, start_special },
4445a875 3687 { "emergency", EQUAL, 1, start_special },
5632e374 3688 { "reset-maintenance", MORE, 1, reset_maintenance },
7e4249b9
LP
3689 };
3690
e4b61340 3691 int left;
7e4249b9 3692 unsigned i;
7e4249b9 3693
e4b61340
LP
3694 assert(bus);
3695 assert(argc >= 0);
3696 assert(argv);
7e4249b9
LP
3697
3698 left = argc - optind;
3699
3700 if (left <= 0)
3701 /* Special rule: no arguments means "list-units" */
3702 i = 0;
3703 else {
0183528f
LP
3704 if (streq(argv[optind], "help")) {
3705 systemctl_help();
3706 return 0;
3707 }
3708
7e4249b9
LP
3709 for (i = 0; i < ELEMENTSOF(verbs); i++)
3710 if (streq(argv[optind], verbs[i].verb))
3711 break;
3712
3713 if (i >= ELEMENTSOF(verbs)) {
3714 log_error("Unknown operation %s", argv[optind]);
e4b61340 3715 return -EINVAL;
7e4249b9
LP
3716 }
3717 }
3718
3719 switch (verbs[i].argc_cmp) {
3720
3721 case EQUAL:
3722 if (left != verbs[i].argc) {
3723 log_error("Invalid number of arguments.");
e4b61340 3724 return -EINVAL;
7e4249b9
LP
3725 }
3726
3727 break;
3728
3729 case MORE:
3730 if (left < verbs[i].argc) {
3731 log_error("Too few arguments.");
e4b61340 3732 return -EINVAL;
7e4249b9
LP
3733 }
3734
3735 break;
3736
3737 case LESS:
3738 if (left > verbs[i].argc) {
3739 log_error("Too many arguments.");
e4b61340 3740 return -EINVAL;
7e4249b9
LP
3741 }
3742
3743 break;
3744
3745 default:
3746 assert_not_reached("Unknown comparison operator.");
3747 }
3748
e4b61340
LP
3749 return verbs[i].dispatch(bus, argv + optind, left);
3750}
3751
3752static int reload_with_fallback(DBusConnection *bus) {
3753 int r;
3754
3755 if (bus) {
3756 /* First, try systemd via D-Bus. */
3757 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3758 return 0;
3759 }
3760
3761 /* Nothing else worked, so let's try signals */
3762 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3763
3764 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3765 log_error("kill() failed: %m");
3766 return -errno;
3767 }
3768
3769 return 0;
3770}
3771
3772static int start_with_fallback(DBusConnection *bus) {
3773 int r;
3774
ef2f1067 3775
e4b61340
LP
3776 if (bus) {
3777 /* First, try systemd via D-Bus. */
3778 if ((r = start_unit(bus, NULL, 0)) > 0)
983d9c90 3779 goto done;
e4b61340
LP
3780
3781 /* Hmm, talking to systemd via D-Bus didn't work. Then
3782 * let's try to talk to Upstart via D-Bus. */
f1c5860b 3783 if ((r = talk_upstart()) > 0)
983d9c90 3784 goto done;
e4b61340
LP
3785 }
3786
3787 /* Nothing else worked, so let's try
3788 * /dev/initctl */
d55ae9e6 3789 if ((r = talk_initctl()) != 0)
983d9c90 3790 goto done;
d55ae9e6
LP
3791
3792 log_error("Failed to talk to init daemon.");
3793 return -EIO;
983d9c90
LP
3794
3795done:
3796 warn_wall(arg_action);
3797 return 0;
e4b61340
LP
3798}
3799
3800static int halt_main(DBusConnection *bus) {
3801 int r;
3802
bc8c2f5c
LP
3803 if (geteuid() != 0) {
3804 log_error("Must to be root.");
3805 return -EPERM;
3806 }
3807
d47b555b 3808 if (!arg_dry && !arg_immediate)
e4b61340
LP
3809 return start_with_fallback(bus);
3810
3811 if (!arg_no_wtmp)
3812 if ((r = utmp_put_shutdown(0)) < 0)
3813 log_warning("Failed to write utmp record: %s", strerror(-r));
3814
3815 if (!arg_no_sync)
3816 sync();
3817
3818 if (arg_dry)
3819 return 0;
3820
3821 /* Make sure C-A-D is handled by the kernel from this
3822 * point on... */
3823 reboot(RB_ENABLE_CAD);
3824
3825 switch (arg_action) {
3826
3827 case ACTION_HALT:
3828 log_info("Halting");
3829 reboot(RB_HALT_SYSTEM);
3830 break;
3831
3832 case ACTION_POWEROFF:
3833 log_info("Powering off");
3834 reboot(RB_POWER_OFF);
3835 break;
3836
3837 case ACTION_REBOOT:
3838 log_info("Rebooting");
3839 reboot(RB_AUTOBOOT);
3840 break;
3841
3842 default:
3843 assert_not_reached("Unknown halt action.");
3844 }
3845
3846 /* We should never reach this. */
3847 return -ENOSYS;
3848}
3849
3850static int runlevel_main(void) {
3851 int r, runlevel, previous;
3852
3853 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3854 printf("unknown");
3855 return r;
3856 }
3857
3858 printf("%c %c\n",
3859 previous <= 0 ? 'N' : previous,
3860 runlevel <= 0 ? 'N' : runlevel);
3861
3862 return 0;
3863}
3864
3865int main(int argc, char*argv[]) {
3866 int r, retval = 1;
3867 DBusConnection *bus = NULL;
3868 DBusError error;
3869
3870 dbus_error_init(&error);
3871
3872 log_parse_environment();
3873
3874 if ((r = parse_argv(argc, argv)) < 0)
3875 goto finish;
3876 else if (r == 0) {
3877 retval = 0;
7e4249b9
LP
3878 goto finish;
3879 }
3880
e4b61340
LP
3881 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3882 * let's shortcut this */
3883 if (arg_action == ACTION_RUNLEVEL) {
3884 retval = runlevel_main() < 0;
3885 goto finish;
3886 }
3887
f4579ce7 3888 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
e4b61340
LP
3889
3890 switch (arg_action) {
3891
3892 case ACTION_SYSTEMCTL: {
3893
3894 if (!bus) {
3895 log_error("Failed to get D-Bus connection: %s", error.message);
3896 goto finish;
3897 }
3898
3899 retval = systemctl_main(bus, argc, argv) < 0;
3900 break;
3901 }
3902
3903 case ACTION_HALT:
3904 case ACTION_POWEROFF:
3905 case ACTION_REBOOT:
3906 retval = halt_main(bus) < 0;
3907 break;
3908
e4b61340
LP
3909 case ACTION_RUNLEVEL2:
3910 case ACTION_RUNLEVEL3:
3911 case ACTION_RUNLEVEL4:
3912 case ACTION_RUNLEVEL5:
3913 case ACTION_RESCUE:
514f4ef5 3914 case ACTION_EMERGENCY:
eb22ac37 3915 case ACTION_DEFAULT:
e4b61340
LP
3916 retval = start_with_fallback(bus) < 0;
3917 break;
7e4249b9 3918
e4b61340
LP
3919 case ACTION_RELOAD:
3920 case ACTION_REEXEC:
3921 retval = reload_with_fallback(bus) < 0;
3922 break;
3923
eb22ac37
LP
3924 case ACTION_INVALID:
3925 case ACTION_RUNLEVEL:
e4b61340
LP
3926 default:
3927 assert_not_reached("Unknown action");
3928 }
7e4249b9
LP
3929
3930finish:
3931
b574246b
LP
3932 if (bus) {
3933 dbus_connection_close(bus);
7e4249b9 3934 dbus_connection_unref(bus);
b574246b 3935 }
7e4249b9 3936
e4b61340
LP
3937 dbus_error_free(&error);
3938
7e4249b9
LP
3939 dbus_shutdown();
3940
ea4a240d
LP
3941 strv_free(arg_property);
3942
7e4249b9
LP
3943 return retval;
3944}