]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl.c
socket: SELinux support for socket creation.
[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;
48220598 50static const 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" :
1090 streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" :
1091 "StartUnit";
e4b61340
LP
1092
1093 mode =
514f4ef5
LP
1094 (streq(args[0], "isolate") ||
1095 streq(args[0], "rescue") ||
1096 streq(args[0], "emergency")) ? "isolate" :
90d473a1
LP
1097 arg_fail ? "fail" :
1098 "replace";
e4b61340 1099
514f4ef5 1100 one_name = table[verb_to_action(args[0])];
e4b61340 1101
e4b61340
LP
1102 } else {
1103 assert(arg_action < ELEMENTSOF(table));
1104 assert(table[arg_action]);
1105
1106 method = "StartUnit";
514f4ef5
LP
1107
1108 mode = (arg_action == ACTION_EMERGENCY ||
1109 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1110
1111 one_name = table[arg_action];
1112 }
1113
6e905d93 1114 if (!arg_no_block) {
514f4ef5
LP
1115 if ((r = enable_wait_for_jobs(bus)) < 0) {
1116 log_error("Could not watch jobs: %s", strerror(-r));
1117 goto finish;
1118 }
1119
1120 if (!(s = set_new(string_hash_func, string_compare_func))) {
1121 log_error("Failed to allocate set.");
1122 r = -ENOMEM;
1123 goto finish;
1124 }
e4b61340
LP
1125 }
1126
1127 r = 0;
1128
514f4ef5
LP
1129 if (one_name) {
1130 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1131 goto finish;
1132 } else {
e4b61340
LP
1133 for (i = 1; i < n; i++)
1134 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1135 goto finish;
e4b61340
LP
1136 }
1137
6e905d93 1138 if (!arg_no_block)
514f4ef5
LP
1139 r = wait_for_jobs(bus, s);
1140
e4b61340
LP
1141finish:
1142 if (s)
1143 set_free_free(s);
1144
1145 return r;
1146}
1147
514f4ef5 1148static int start_special(DBusConnection *bus, char **args, unsigned n) {
983d9c90
LP
1149 int r;
1150
514f4ef5
LP
1151 assert(bus);
1152 assert(args);
1153
983d9c90
LP
1154 r = start_unit(bus, args, n);
1155
1156 if (r >= 0)
1157 warn_wall(verb_to_action(args[0]));
514f4ef5 1158
983d9c90 1159 return r;
514f4ef5
LP
1160}
1161
0183528f
LP
1162static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1163 DBusMessage *m = NULL, *reply = NULL;
1164 const char
1165 *interface = "org.freedesktop.systemd1.Unit",
1166 *property = "ActiveState";
1167 int r = -EADDRNOTAVAIL;
1168 DBusError error;
1169 unsigned i;
1170
1171 assert(bus);
1172 assert(args);
1173
1174 dbus_error_init(&error);
1175
1176 for (i = 1; i < n; i++) {
1177 const char *path = NULL;
1178 const char *state;
1179 DBusMessageIter iter, sub;
1180
1181 if (!(m = dbus_message_new_method_call(
1182 "org.freedesktop.systemd1",
1183 "/org/freedesktop/systemd1",
1184 "org.freedesktop.systemd1.Manager",
1185 "GetUnit"))) {
1186 log_error("Could not allocate message.");
1187 r = -ENOMEM;
1188 goto finish;
1189 }
1190
1191 if (!dbus_message_append_args(m,
1192 DBUS_TYPE_STRING, &args[i],
1193 DBUS_TYPE_INVALID)) {
1194 log_error("Could not append arguments to message.");
1195 r = -ENOMEM;
1196 goto finish;
1197 }
1198
1199 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1200
1201 /* Hmm, cannot figure out anything about this unit... */
1202 if (!arg_quiet)
1203 puts("unknown");
1204
ed2d7a44 1205 dbus_error_free(&error);
0183528f
LP
1206 continue;
1207 }
1208
1209 if (!dbus_message_get_args(reply, &error,
1210 DBUS_TYPE_OBJECT_PATH, &path,
1211 DBUS_TYPE_INVALID)) {
1212 log_error("Failed to parse reply: %s", error.message);
1213 r = -EIO;
1214 goto finish;
1215 }
1216
1217 dbus_message_unref(m);
1218 if (!(m = dbus_message_new_method_call(
1219 "org.freedesktop.systemd1",
1220 path,
1221 "org.freedesktop.DBus.Properties",
1222 "Get"))) {
1223 log_error("Could not allocate message.");
1224 r = -ENOMEM;
1225 goto finish;
1226 }
1227
1228 if (!dbus_message_append_args(m,
1229 DBUS_TYPE_STRING, &interface,
1230 DBUS_TYPE_STRING, &property,
1231 DBUS_TYPE_INVALID)) {
1232 log_error("Could not append arguments to message.");
1233 r = -ENOMEM;
1234 goto finish;
1235 }
1236
1237 dbus_message_unref(reply);
1238 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1239 log_error("Failed to issue method call: %s", error.message);
1240 r = -EIO;
1241 goto finish;
1242 }
1243
1244 if (!dbus_message_iter_init(reply, &iter) ||
1245 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1246 log_error("Failed to parse reply.");
1247 r = -EIO;
1248 goto finish;
1249 }
1250
1251 dbus_message_iter_recurse(&iter, &sub);
1252
1253 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1254 log_error("Failed to parse reply.");
1255 r = -EIO;
1256 goto finish;
1257 }
1258
1259 dbus_message_iter_get_basic(&sub, &state);
1260
1261 if (!arg_quiet)
1262 puts(state);
1263
01b1b079 1264 if (streq(state, "active") || startswith(state, "reloading"))
0183528f
LP
1265 r = 0;
1266
1267 dbus_message_unref(m);
1268 dbus_message_unref(reply);
1269 m = reply = NULL;
1270 }
1271
1272finish:
1273 if (m)
1274 dbus_message_unref(m);
1275
1276 if (reply)
1277 dbus_message_unref(reply);
1278
1279 dbus_error_free(&error);
1280
1281 return r;
48220598
LP
1282}
1283
582a507f
LP
1284typedef struct ExecStatusInfo {
1285 char *path;
1286 char **argv;
1287
b708e7ce
LP
1288 bool ignore;
1289
582a507f
LP
1290 usec_t start_timestamp;
1291 usec_t exit_timestamp;
1292 pid_t pid;
1293 int code;
1294 int status;
1295
1296 LIST_FIELDS(struct ExecStatusInfo, exec);
1297} ExecStatusInfo;
1298
1299static void exec_status_info_free(ExecStatusInfo *i) {
1300 assert(i);
1301
1302 free(i->path);
1303 strv_free(i->argv);
1304 free(i);
1305}
1306
1307static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1308 uint64_t start_timestamp, exit_timestamp;
1309 DBusMessageIter sub2, sub3;
1310 const char*path;
1311 unsigned n;
1312 uint32_t pid;
1313 int32_t code, status;
b708e7ce 1314 dbus_bool_t ignore;
582a507f
LP
1315
1316 assert(i);
1317 assert(i);
1318
1319 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1320 return -EIO;
1321
1322 dbus_message_iter_recurse(sub, &sub2);
1323
1324 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1325 return -EIO;
1326
1327 if (!(i->path = strdup(path)))
1328 return -ENOMEM;
1329
1330 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1331 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1332 return -EIO;
1333
1334 n = 0;
1335 dbus_message_iter_recurse(&sub2, &sub3);
1336 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1337 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1338 dbus_message_iter_next(&sub3);
1339 n++;
1340 }
1341
1342
1343 if (!(i->argv = new0(char*, n+1)))
1344 return -ENOMEM;
1345
1346 n = 0;
1347 dbus_message_iter_recurse(&sub2, &sub3);
1348 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1349 const char *s;
1350
1351 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1352 dbus_message_iter_get_basic(&sub3, &s);
1353 dbus_message_iter_next(&sub3);
1354
1355 if (!(i->argv[n++] = strdup(s)))
1356 return -ENOMEM;
1357 }
1358
1359 if (!dbus_message_iter_next(&sub2) ||
b708e7ce 1360 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
582a507f
LP
1361 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1362 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1363 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1364 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1365 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1366 return -EIO;
1367
b708e7ce 1368 i->ignore = ignore;
582a507f
LP
1369 i->start_timestamp = (usec_t) start_timestamp;
1370 i->exit_timestamp = (usec_t) exit_timestamp;
1371 i->pid = (pid_t) pid;
1372 i->code = code;
1373 i->status = status;
1374
1375 return 0;
1376}
1377
61cbdc4b
LP
1378typedef struct UnitStatusInfo {
1379 const char *id;
1380 const char *load_state;
1381 const char *active_state;
1382 const char *sub_state;
1383
1384 const char *description;
1385
1386 const char *fragment_path;
1387 const char *default_control_group;
1388
45fb0699
LP
1389 bool need_daemon_reload;
1390
61cbdc4b
LP
1391 /* Service */
1392 pid_t main_pid;
1393 pid_t control_pid;
1394 const char *status_text;
1395 bool running;
1396
1397 usec_t start_timestamp;
1398 usec_t exit_timestamp;
1399
1400 int exit_code, exit_status;
1401
1402 /* Socket */
1403 unsigned n_accepted;
1404 unsigned n_connections;
b8131a87 1405 bool accept;
61cbdc4b
LP
1406
1407 /* Device */
1408 const char *sysfs_path;
1409
1410 /* Mount, Automount */
1411 const char *where;
1412
1413 /* Swap */
1414 const char *what;
582a507f
LP
1415
1416 LIST_HEAD(ExecStatusInfo, exec);
61cbdc4b
LP
1417} UnitStatusInfo;
1418
1419static void print_status_info(UnitStatusInfo *i) {
582a507f
LP
1420 ExecStatusInfo *p;
1421
61cbdc4b
LP
1422 assert(i);
1423
1424 /* This shows pretty information about a unit. See
1425 * print_property() for a low-level property printer */
1426
1427 printf("%s", strna(i->id));
1428
1429 if (i->description && !streq_ptr(i->id, i->description))
1430 printf(" - %s", i->description);
1431
1432 printf("\n");
1433
1434 if (i->fragment_path)
1435 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1436 else if (streq_ptr(i->load_state, "failed"))
2cc59dbf
LP
1437 printf("\t Loaded: %s%s%s\n",
1438 ansi_highlight(true),
1439 strna(i->load_state),
1440 ansi_highlight(false));
61cbdc4b
LP
1441 else
1442 printf("\t Loaded: %s\n", strna(i->load_state));
1443
582a507f
LP
1444 if (streq_ptr(i->active_state, "maintenance")) {
1445 if (streq_ptr(i->active_state, i->sub_state))
2cc59dbf
LP
1446 printf("\t Active: %s%s%s\n",
1447 ansi_highlight(true),
1448 strna(i->active_state),
1449 ansi_highlight(false));
582a507f 1450 else
2cc59dbf
LP
1451 printf("\t Active: %s%s (%s)%s\n",
1452 ansi_highlight(true),
582a507f 1453 strna(i->active_state),
2cc59dbf
LP
1454 strna(i->sub_state),
1455 ansi_highlight(false));
582a507f
LP
1456 } else {
1457 if (streq_ptr(i->active_state, i->sub_state))
1458 printf("\t Active: %s\n",
1459 strna(i->active_state));
1460 else
1461 printf("\t Active: %s (%s)\n",
1462 strna(i->active_state),
1463 strna(i->sub_state));
1464 }
61cbdc4b
LP
1465
1466 if (i->sysfs_path)
1467 printf("\t Device: %s\n", i->sysfs_path);
1468 else if (i->where)
1469 printf("\t Where: %s\n", i->where);
1470 else if (i->what)
1471 printf("\t What: %s\n", i->what);
1472
b8131a87 1473 if (i->accept)
61cbdc4b
LP
1474 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1475
582a507f
LP
1476 LIST_FOREACH(exec, p, i->exec) {
1477 char *t;
1478
1479 /* Only show exited processes here */
1480 if (p->code == 0)
1481 continue;
1482
1483 t = strv_join(p->argv, " ");
1484 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1485 free(t);
1486
1487 if (p->code == CLD_EXITED)
1488 printf("status=%i", p->status);
1489 else
1490 printf("signal=%s", signal_to_string(p->status));
1491 printf(")\n");
1492
1493 if (i->main_pid == p->pid &&
1494 i->start_timestamp == p->start_timestamp &&
1495 i->exit_timestamp == p->start_timestamp)
1496 /* Let's not show this twice */
1497 i->main_pid = 0;
1498
1499 if (p->pid == i->control_pid)
1500 i->control_pid = 0;
1501 }
1502
61cbdc4b
LP
1503 if (i->main_pid > 0 || i->control_pid > 0) {
1504 printf("\t");
1505
1506 if (i->main_pid > 0) {
582a507f 1507 printf(" Main: %u", (unsigned) i->main_pid);
61cbdc4b
LP
1508
1509 if (i->running) {
1510 char *t = NULL;
1511 get_process_name(i->main_pid, &t);
1512 if (t) {
1513 printf(" (%s)", t);
1514 free(t);
1515 }
1516 } else {
1517 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1518
1519 if (i->exit_code == CLD_EXITED)
1520 printf("status=%i", i->exit_status);
1521 else
582a507f 1522 printf("signal=%s", signal_to_string(i->exit_status));
2cc59dbf 1523 printf(")"); }
61cbdc4b
LP
1524 }
1525
1526 if (i->main_pid > 0 && i->control_pid > 0)
1527 printf(";");
1528
1529 if (i->control_pid > 0) {
1530 char *t = NULL;
1531
1532 printf(" Control: %u", (unsigned) i->control_pid);
1533
1534 get_process_name(i->control_pid, &t);
1535 if (t) {
1536 printf(" (%s)", t);
1537 free(t);
1538 }
1539 }
1540
1541 printf("\n");
1542 }
1543
17bb7382
LP
1544 if (i->status_text)
1545 printf("\t Status: \"%s\"\n", i->status_text);
1546
c59760ee 1547 if (i->default_control_group) {
ab35fb1b
LP
1548 unsigned c;
1549
61cbdc4b 1550 printf("\t CGroup: %s\n", i->default_control_group);
ab35fb1b
LP
1551
1552 if ((c = columns()) > 18)
1553 c -= 18;
1554 else
1555 c = 0;
1556
35d2e7ec 1557 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
c59760ee 1558 }
45fb0699
LP
1559
1560 if (i->need_daemon_reload)
2cc59dbf
LP
1561 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1562 ansi_highlight(true),
1563 ansi_highlight(false),
45fb0699 1564 arg_session ? "--session" : "--system");
61cbdc4b
LP
1565}
1566
1567static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1568
1569 switch (dbus_message_iter_get_arg_type(iter)) {
1570
1571 case DBUS_TYPE_STRING: {
1572 const char *s;
1573
1574 dbus_message_iter_get_basic(iter, &s);
1575
1576 if (s[0]) {
1577 if (streq(name, "Id"))
1578 i->id = s;
1579 else if (streq(name, "LoadState"))
1580 i->load_state = s;
1581 else if (streq(name, "ActiveState"))
1582 i->active_state = s;
1583 else if (streq(name, "SubState"))
1584 i->sub_state = s;
1585 else if (streq(name, "Description"))
1586 i->description = s;
1587 else if (streq(name, "FragmentPath"))
1588 i->fragment_path = s;
1589 else if (streq(name, "DefaultControlGroup"))
1590 i->default_control_group = s;
1591 else if (streq(name, "StatusText"))
1592 i->status_text = s;
1593 else if (streq(name, "SysFSPath"))
1594 i->sysfs_path = s;
1595 else if (streq(name, "Where"))
1596 i->where = s;
1597 else if (streq(name, "What"))
1598 i->what = s;
1599 }
1600
1601 break;
1602 }
1603
b8131a87
LP
1604 case DBUS_TYPE_BOOLEAN: {
1605 dbus_bool_t b;
1606
1607 dbus_message_iter_get_basic(iter, &b);
1608
1609 if (streq(name, "Accept"))
1610 i->accept = b;
45fb0699
LP
1611 else if (streq(name, "NeedDaemonReload"))
1612 i->need_daemon_reload = b;
b8131a87
LP
1613
1614 break;
1615 }
1616
61cbdc4b
LP
1617 case DBUS_TYPE_UINT32: {
1618 uint32_t u;
1619
1620 dbus_message_iter_get_basic(iter, &u);
1621
1622 if (streq(name, "MainPID")) {
1623 if (u > 0) {
1624 i->main_pid = (pid_t) u;
1625 i->running = true;
1626 }
1627 } else if (streq(name, "ControlPID"))
1628 i->control_pid = (pid_t) u;
1629 else if (streq(name, "ExecMainPID")) {
1630 if (u > 0)
1631 i->main_pid = (pid_t) u;
1632 } else if (streq(name, "NAccepted"))
1633 i->n_accepted = u;
1634 else if (streq(name, "NConnections"))
1635 i->n_connections = u;
1636
1637 break;
1638 }
1639
1640 case DBUS_TYPE_INT32: {
1641 int32_t j;
1642
1643 dbus_message_iter_get_basic(iter, &j);
1644
1645 if (streq(name, "ExecMainCode"))
1646 i->exit_code = (int) j;
1647 else if (streq(name, "ExecMainStatus"))
1648 i->exit_status = (int) j;
1649
1650 break;
1651 }
1652
1653 case DBUS_TYPE_UINT64: {
1654 uint64_t u;
1655
1656 dbus_message_iter_get_basic(iter, &u);
1657
1658 if (streq(name, "ExecMainStartTimestamp"))
1659 i->start_timestamp = (usec_t) u;
1660 else if (streq(name, "ExecMainExitTimestamp"))
1661 i->exit_timestamp = (usec_t) u;
1662
1663 break;
1664 }
582a507f
LP
1665
1666 case DBUS_TYPE_ARRAY: {
1667
1668 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1669 startswith(name, "Exec")) {
1670 DBusMessageIter sub;
1671
1672 dbus_message_iter_recurse(iter, &sub);
1673 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1674 ExecStatusInfo *info;
1675 int r;
1676
1677 if (!(info = new0(ExecStatusInfo, 1)))
1678 return -ENOMEM;
1679
1680 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1681 free(info);
1682 return r;
1683 }
1684
1685 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1686
1687 dbus_message_iter_next(&sub);
1688 }
1689 }
1690
1691 break;
1692 }
61cbdc4b
LP
1693 }
1694
1695 return 0;
1696}
1697
48220598
LP
1698static int print_property(const char *name, DBusMessageIter *iter) {
1699 assert(name);
1700 assert(iter);
1701
61cbdc4b
LP
1702 /* This is a low-level property printer, see
1703 * print_status_info() for the nicer output */
1704
48220598
LP
1705 if (arg_property && !streq(name, arg_property))
1706 return 0;
1707
1708 switch (dbus_message_iter_get_arg_type(iter)) {
1709
1710 case DBUS_TYPE_STRING: {
1711 const char *s;
1712 dbus_message_iter_get_basic(iter, &s);
1713
1714 if (arg_all || s[0])
1715 printf("%s=%s\n", name, s);
1716
1717 return 0;
1718 }
1719
1720 case DBUS_TYPE_BOOLEAN: {
1721 dbus_bool_t b;
1722 dbus_message_iter_get_basic(iter, &b);
1723 printf("%s=%s\n", name, yes_no(b));
1724
1725 return 0;
1726 }
1727
1728 case DBUS_TYPE_UINT64: {
1729 uint64_t u;
1730 dbus_message_iter_get_basic(iter, &u);
1731
1732 /* Yes, heuristics! But we can change this check
1733 * should it turn out to not be sufficient */
1734
14ad1d14 1735 if (strstr(name, "Timestamp")) {
48220598
LP
1736 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1737
1738 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1739 printf("%s=%s\n", name, strempty(t));
552e4331
LP
1740 } else if (strstr(name, "USec")) {
1741 char timespan[FORMAT_TIMESPAN_MAX];
1742
1743 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
48220598
LP
1744 } else
1745 printf("%s=%llu\n", name, (unsigned long long) u);
1746
1747 return 0;
1748 }
1749
1750 case DBUS_TYPE_UINT32: {
1751 uint32_t u;
1752 dbus_message_iter_get_basic(iter, &u);
1753
5bd07073 1754 if (strstr(name, "UMask") || strstr(name, "Mode"))
48220598
LP
1755 printf("%s=%04o\n", name, u);
1756 else
1757 printf("%s=%u\n", name, (unsigned) u);
1758
1759 return 0;
1760 }
1761
1762 case DBUS_TYPE_INT32: {
1763 int32_t i;
1764 dbus_message_iter_get_basic(iter, &i);
1765
1766 printf("%s=%i\n", name, (int) i);
1767 return 0;
1768 }
1769
1770 case DBUS_TYPE_STRUCT: {
1771 DBusMessageIter sub;
1772 dbus_message_iter_recurse(iter, &sub);
1773
ebf57b80 1774 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
48220598
LP
1775 uint32_t u;
1776
1777 dbus_message_iter_get_basic(&sub, &u);
1778
1779 if (u)
1780 printf("%s=%u\n", name, (unsigned) u);
1781 else if (arg_all)
1782 printf("%s=\n", name);
1783
1784 return 0;
ebf57b80 1785 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
48220598
LP
1786 const char *s;
1787
1788 dbus_message_iter_get_basic(&sub, &s);
1789
1790 if (arg_all || s[0])
1791 printf("%s=%s\n", name, s);
1792
1793 return 0;
1794 }
1795
1796 break;
1797 }
1798
1799 case DBUS_TYPE_ARRAY:
1800
1801 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1802 DBusMessageIter sub;
1803 bool space = false;
1804
1805 dbus_message_iter_recurse(iter, &sub);
48220598
LP
1806 if (arg_all ||
1807 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1808 printf("%s=", name);
1809
1810 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1811 const char *s;
1812
1813 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1814 dbus_message_iter_get_basic(&sub, &s);
1815 printf("%s%s", space ? " " : "", s);
1816
1817 space = true;
1818 dbus_message_iter_next(&sub);
1819 }
1820
1821 puts("");
1822 }
1823
ebf57b80 1824 return 0;
582a507f 1825
82c121a4
LP
1826 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1827 DBusMessageIter sub;
1828
1829 dbus_message_iter_recurse(iter, &sub);
82c121a4
LP
1830 if (arg_all ||
1831 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1832 printf("%s=", name);
1833
1834 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1835 uint8_t u;
ebf57b80 1836
82c121a4
LP
1837 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1838 dbus_message_iter_get_basic(&sub, &u);
1839 printf("%02x", u);
1840
1841 dbus_message_iter_next(&sub);
1842 }
1843
1844 puts("");
1845 }
1846
1847 return 0;
582a507f 1848
ebf57b80
LP
1849 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1850 DBusMessageIter sub, sub2;
1851
1852 dbus_message_iter_recurse(iter, &sub);
ebf57b80
LP
1853 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1854 const char *type, *path;
1855
1856 dbus_message_iter_recurse(&sub, &sub2);
1857
1858 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1859 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1860 printf("%s=%s\n", type, path);
1861
1862 dbus_message_iter_next(&sub);
1863 }
1864
707e5e52 1865 return 0;
582a507f 1866
707e5e52
LP
1867 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1868 DBusMessageIter sub, sub2;
1869
1870 dbus_message_iter_recurse(iter, &sub);
707e5e52
LP
1871 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1872 const char *base;
1873 uint64_t value, next_elapse;
1874
1875 dbus_message_iter_recurse(&sub, &sub2);
1876
1877 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1878 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
552e4331
LP
1879 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1880 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1881
1882 printf("%s={ value=%s ; next_elapse=%s }\n",
fe68089d 1883 base,
552e4331
LP
1884 format_timespan(timespan1, sizeof(timespan1), value),
1885 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1886 }
fe68089d
LP
1887
1888 dbus_message_iter_next(&sub);
1889 }
1890
1891 return 0;
fe68089d 1892
582a507f
LP
1893 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1894 DBusMessageIter sub;
fe68089d
LP
1895
1896 dbus_message_iter_recurse(iter, &sub);
fe68089d 1897 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
582a507f 1898 ExecStatusInfo info;
fe68089d 1899
582a507f
LP
1900 zero(info);
1901 if (exec_status_info_deserialize(&sub, &info) >= 0) {
fe68089d 1902 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
582a507f
LP
1903 char *t;
1904
1905 t = strv_join(info.argv, " ");
1906
b708e7ce 1907 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
1908 name,
1909 strna(info.path),
1910 strna(t),
b708e7ce 1911 yes_no(info.ignore),
582a507f
LP
1912 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1913 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1914 (unsigned) info. pid,
1915 sigchld_code_to_string(info.code),
1916 info.status,
1917 info.code == CLD_EXITED ? "" : "/",
1918 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
fe68089d 1919
582a507f 1920 free(t);
fe68089d
LP
1921 }
1922
582a507f
LP
1923 free(info.path);
1924 strv_free(info.argv);
707e5e52
LP
1925
1926 dbus_message_iter_next(&sub);
1927 }
1928
48220598
LP
1929 return 0;
1930 }
1931
1932 break;
1933 }
1934
1935 if (arg_all)
1936 printf("%s=[unprintable]\n", name);
1937
1938 return 0;
1939}
1940
61cbdc4b 1941static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
48220598
LP
1942 DBusMessage *m = NULL, *reply = NULL;
1943 const char *interface = "";
1944 int r;
1945 DBusError error;
1946 DBusMessageIter iter, sub, sub2, sub3;
61cbdc4b 1947 UnitStatusInfo info;
582a507f 1948 ExecStatusInfo *p;
48220598
LP
1949
1950 assert(bus);
1951 assert(path);
61cbdc4b 1952 assert(new_line);
48220598 1953
61cbdc4b 1954 zero(info);
48220598
LP
1955 dbus_error_init(&error);
1956
1957 if (!(m = dbus_message_new_method_call(
1958 "org.freedesktop.systemd1",
1959 path,
1960 "org.freedesktop.DBus.Properties",
1961 "GetAll"))) {
1962 log_error("Could not allocate message.");
1963 r = -ENOMEM;
1964 goto finish;
1965 }
1966
1967 if (!dbus_message_append_args(m,
1968 DBUS_TYPE_STRING, &interface,
1969 DBUS_TYPE_INVALID)) {
1970 log_error("Could not append arguments to message.");
1971 r = -ENOMEM;
1972 goto finish;
1973 }
1974
1975 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1976 log_error("Failed to issue method call: %s", error.message);
1977 r = -EIO;
1978 goto finish;
1979 }
1980
1981 if (!dbus_message_iter_init(reply, &iter) ||
1982 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1983 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1984 log_error("Failed to parse reply.");
1985 r = -EIO;
1986 goto finish;
1987 }
1988
1989 dbus_message_iter_recurse(&iter, &sub);
1990
61cbdc4b
LP
1991 if (*new_line)
1992 printf("\n");
1993
1994 *new_line = true;
1995
48220598
LP
1996 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1997 const char *name;
1998
1999 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2000 log_error("Failed to parse reply.");
2001 r = -EIO;
2002 goto finish;
2003 }
2004
2005 dbus_message_iter_recurse(&sub, &sub2);
0183528f 2006
48220598
LP
2007 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2008 log_error("Failed to parse reply.");
2009 r = -EIO;
2010 goto finish;
2011 }
2012
2013 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2014 log_error("Failed to parse reply.");
2015 r = -EIO;
2016 goto finish;
2017 }
2018
2019 dbus_message_iter_recurse(&sub2, &sub3);
2020
61cbdc4b
LP
2021 if (show_properties)
2022 r = print_property(name, &sub3);
2023 else
2024 r = status_property(name, &sub3, &info);
2025
2026 if (r < 0) {
48220598
LP
2027 log_error("Failed to parse reply.");
2028 r = -EIO;
2029 goto finish;
2030 }
2031
2032 dbus_message_iter_next(&sub);
2033 }
2034
61cbdc4b
LP
2035 if (!show_properties)
2036 print_status_info(&info);
2037
582a507f
LP
2038 while ((p = info.exec)) {
2039 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2040 exec_status_info_free(p);
2041 }
2042
48220598
LP
2043 r = 0;
2044
2045finish:
2046 if (m)
2047 dbus_message_unref(m);
2048
2049 if (reply)
2050 dbus_message_unref(reply);
2051
2052 dbus_error_free(&error);
2053
2054 return r;
2055}
2056
2057static int show(DBusConnection *bus, char **args, unsigned n) {
2058 DBusMessage *m = NULL, *reply = NULL;
2059 int r;
2060 DBusError error;
2061 unsigned i;
61cbdc4b 2062 bool show_properties, new_line = false;
48220598
LP
2063
2064 assert(bus);
2065 assert(args);
2066
2067 dbus_error_init(&error);
2068
61cbdc4b
LP
2069 show_properties = !streq(args[0], "status");
2070
2071 if (show_properties && n <= 1) {
48220598
LP
2072 /* If not argument is specified inspect the manager
2073 * itself */
2074
61cbdc4b 2075 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
48220598
LP
2076 goto finish;
2077 }
2078
2079 for (i = 1; i < n; i++) {
2080 const char *path = NULL;
2081 uint32_t id;
2082
61cbdc4b 2083 if (!show_properties || safe_atou32(args[i], &id) < 0) {
48220598
LP
2084
2085 if (!(m = dbus_message_new_method_call(
2086 "org.freedesktop.systemd1",
2087 "/org/freedesktop/systemd1",
2088 "org.freedesktop.systemd1.Manager",
e87d1818 2089 "LoadUnit"))) {
48220598
LP
2090 log_error("Could not allocate message.");
2091 r = -ENOMEM;
2092 goto finish;
2093 }
2094
2095 if (!dbus_message_append_args(m,
2096 DBUS_TYPE_STRING, &args[i],
2097 DBUS_TYPE_INVALID)) {
2098 log_error("Could not append arguments to message.");
2099 r = -ENOMEM;
2100 goto finish;
2101 }
2102
ed2d7a44
LP
2103 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2104
2105 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2106 log_error("Failed to issue method call: %s", error.message);
2107 r = -EIO;
2108 goto finish;
2109 }
2110
2111 dbus_error_free(&error);
2112
2113 dbus_message_unref(m);
2114 if (!(m = dbus_message_new_method_call(
2115 "org.freedesktop.systemd1",
2116 "/org/freedesktop/systemd1",
2117 "org.freedesktop.systemd1.Manager",
2118 "GetUnit"))) {
2119 log_error("Could not allocate message.");
2120 r = -ENOMEM;
2121 goto finish;
2122 }
2123
2124 if (!dbus_message_append_args(m,
2125 DBUS_TYPE_STRING, &args[i],
2126 DBUS_TYPE_INVALID)) {
2127 log_error("Could not append arguments to message.");
2128 r = -ENOMEM;
2129 goto finish;
2130 }
2131
2132 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2133 log_error("Failed to issue method call: %s", error.message);
2134 r = -EIO;
2135 goto finish;
2136 }
2137 }
2138
48220598
LP
2139 } else {
2140
2141 if (!(m = dbus_message_new_method_call(
2142 "org.freedesktop.systemd1",
2143 "/org/freedesktop/systemd1",
2144 "org.freedesktop.systemd1.Manager",
2145 "GetJob"))) {
2146 log_error("Could not allocate message.");
2147 r = -ENOMEM;
2148 goto finish;
2149 }
2150
2151 if (!dbus_message_append_args(m,
2152 DBUS_TYPE_UINT32, &id,
2153 DBUS_TYPE_INVALID)) {
2154 log_error("Could not append arguments to message.");
2155 r = -ENOMEM;
2156 goto finish;
2157 }
48220598 2158
ed2d7a44
LP
2159 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2160 log_error("Failed to issue method call: %s", error.message);
2161 r = -EIO;
2162 goto finish;
2163 }
48220598
LP
2164 }
2165
2166 if (!dbus_message_get_args(reply, &error,
2167 DBUS_TYPE_OBJECT_PATH, &path,
2168 DBUS_TYPE_INVALID)) {
2169 log_error("Failed to parse reply: %s", error.message);
2170 r = -EIO;
2171 goto finish;
2172 }
2173
61cbdc4b 2174 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
48220598
LP
2175 goto finish;
2176
2177 dbus_message_unref(m);
2178 dbus_message_unref(reply);
2179 m = reply = NULL;
2180 }
2181
2182 r = 0;
2183
2184finish:
2185 if (m)
2186 dbus_message_unref(m);
2187
2188 if (reply)
2189 dbus_message_unref(reply);
2190
2191 dbus_error_free(&error);
2192
2193 return r;
0183528f
LP
2194}
2195
7e4249b9
LP
2196static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2197 DBusError error;
2198 DBusMessage *m = NULL, *reply = NULL;
2199
2200 assert(connection);
2201 assert(message);
2202
2203 dbus_error_init(&error);
2204
54165a39
LP
2205 log_debug("Got D-Bus request: %s.%s() on %s",
2206 dbus_message_get_interface(message),
2207 dbus_message_get_member(message),
2208 dbus_message_get_path(message));
7e4249b9
LP
2209
2210 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2211 log_error("Warning! D-Bus connection terminated.");
2212 dbus_connection_close(connection);
2213
2214 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2215 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2216 const char *id, *path;
2217
2218 if (!dbus_message_get_args(message, &error,
2219 DBUS_TYPE_STRING, &id,
2220 DBUS_TYPE_OBJECT_PATH, &path,
2221 DBUS_TYPE_INVALID))
2222 log_error("Failed to parse message: %s", error.message);
2223 else if (streq(dbus_message_get_member(message), "UnitNew"))
2224 printf("Unit %s added.\n", id);
2225 else
2226 printf("Unit %s removed.\n", id);
2227
2228 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2229 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2230 uint32_t id;
2231 const char *path;
2232
2233 if (!dbus_message_get_args(message, &error,
2234 DBUS_TYPE_UINT32, &id,
2235 DBUS_TYPE_OBJECT_PATH, &path,
2236 DBUS_TYPE_INVALID))
2237 log_error("Failed to parse message: %s", error.message);
2238 else if (streq(dbus_message_get_member(message), "JobNew"))
2239 printf("Job %u added.\n", id);
2240 else
2241 printf("Job %u removed.\n", id);
2242
2243
2244 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2245 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2246
2247 const char *path, *interface, *property = "Id";
2248 DBusMessageIter iter, sub;
2249
2250 path = dbus_message_get_path(message);
2251 interface = dbus_message_get_interface(message);
2252
2253 if (!(m = dbus_message_new_method_call(
2254 "org.freedesktop.systemd1",
2255 path,
2256 "org.freedesktop.DBus.Properties",
2257 "Get"))) {
2258 log_error("Could not allocate message.");
2259 goto oom;
2260 }
2261
2262 if (!dbus_message_append_args(m,
2263 DBUS_TYPE_STRING, &interface,
2264 DBUS_TYPE_STRING, &property,
2265 DBUS_TYPE_INVALID)) {
2266 log_error("Could not append arguments to message.");
2267 goto finish;
2268 }
2269
2270 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2271 log_error("Failed to issue method call: %s", error.message);
2272 goto finish;
2273 }
2274
2275 if (!dbus_message_iter_init(reply, &iter) ||
2276 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2277 log_error("Failed to parse reply.");
2278 goto finish;
2279 }
2280
2281 dbus_message_iter_recurse(&iter, &sub);
2282
2283 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2284 const char *id;
2285
2286 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2287 log_error("Failed to parse reply.");
2288 goto finish;
2289 }
2290
2291 dbus_message_iter_get_basic(&sub, &id);
2292 printf("Unit %s changed.\n", id);
2293 } else {
2294 uint32_t id;
2295
2296 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2297 log_error("Failed to parse reply.");
2298 goto finish;
2299 }
2300
2301 dbus_message_iter_get_basic(&sub, &id);
2302 printf("Job %u changed.\n", id);
2303 }
2304 }
2305
2306finish:
2307 if (m)
2308 dbus_message_unref(m);
2309
2310 if (reply)
2311 dbus_message_unref(reply);
2312
2313 dbus_error_free(&error);
2314 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2315
2316oom:
2317 if (m)
2318 dbus_message_unref(m);
2319
2320 if (reply)
2321 dbus_message_unref(reply);
2322
2323 dbus_error_free(&error);
2324 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2325}
2326
2327static int monitor(DBusConnection *bus, char **args, unsigned n) {
2328 DBusMessage *m = NULL, *reply = NULL;
2329 DBusError error;
2330 int r;
2331
2332 dbus_error_init(&error);
2333
f4579ce7
LP
2334 if (!private_bus) {
2335 dbus_bus_add_match(bus,
2336 "type='signal',"
2337 "sender='org.freedesktop.systemd1',"
2338 "interface='org.freedesktop.systemd1.Manager',"
2339 "path='/org/freedesktop/systemd1'",
2340 &error);
7e4249b9 2341
f4579ce7
LP
2342 if (dbus_error_is_set(&error)) {
2343 log_error("Failed to add match: %s", error.message);
2344 r = -EIO;
2345 goto finish;
2346 }
7e4249b9 2347
f4579ce7
LP
2348 dbus_bus_add_match(bus,
2349 "type='signal',"
2350 "sender='org.freedesktop.systemd1',"
2351 "interface='org.freedesktop.systemd1.Unit',"
2352 "member='Changed'",
2353 &error);
7e4249b9 2354
f4579ce7
LP
2355 if (dbus_error_is_set(&error)) {
2356 log_error("Failed to add match: %s", error.message);
2357 r = -EIO;
2358 goto finish;
2359 }
7e4249b9 2360
f4579ce7
LP
2361 dbus_bus_add_match(bus,
2362 "type='signal',"
2363 "sender='org.freedesktop.systemd1',"
2364 "interface='org.freedesktop.systemd1.Job',"
2365 "member='Changed'",
2366 &error);
7e4249b9 2367
f4579ce7
LP
2368 if (dbus_error_is_set(&error)) {
2369 log_error("Failed to add match: %s", error.message);
2370 r = -EIO;
2371 goto finish;
2372 }
7e4249b9
LP
2373 }
2374
2375 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2376 log_error("Failed to add filter.");
2377 r = -ENOMEM;
2378 goto finish;
2379 }
2380
2381 if (!(m = dbus_message_new_method_call(
2382 "org.freedesktop.systemd1",
2383 "/org/freedesktop/systemd1",
2384 "org.freedesktop.systemd1.Manager",
2385 "Subscribe"))) {
2386 log_error("Could not allocate message.");
2387 r = -ENOMEM;
2388 goto finish;
2389 }
2390
2391 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2392 log_error("Failed to issue method call: %s", error.message);
2393 r = -EIO;
2394 goto finish;
2395 }
2396
2397 while (dbus_connection_read_write_dispatch(bus, -1))
2398 ;
2399
2400 r = 0;
2401
2402finish:
2403
2404 /* This is slightly dirty, since we don't undo the filter or the matches. */
2405
2406 if (m)
2407 dbus_message_unref(m);
2408
2409 if (reply)
2410 dbus_message_unref(reply);
2411
2412 dbus_error_free(&error);
2413
2414 return r;
2415}
2416
2417static int dump(DBusConnection *bus, char **args, unsigned n) {
2418 DBusMessage *m = NULL, *reply = NULL;
2419 DBusError error;
2420 int r;
2421 const char *text;
2422
2423 dbus_error_init(&error);
2424
2425 if (!(m = dbus_message_new_method_call(
2426 "org.freedesktop.systemd1",
2427 "/org/freedesktop/systemd1",
2428 "org.freedesktop.systemd1.Manager",
2429 "Dump"))) {
2430 log_error("Could not allocate message.");
2431 return -ENOMEM;
2432 }
2433
2434 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2435 log_error("Failed to issue method call: %s", error.message);
2436 r = -EIO;
2437 goto finish;
2438 }
2439
2440 if (!dbus_message_get_args(reply, &error,
2441 DBUS_TYPE_STRING, &text,
2442 DBUS_TYPE_INVALID)) {
2443 log_error("Failed to parse reply: %s", error.message);
2444 r = -EIO;
2445 goto finish;
2446 }
2447
2448 fputs(text, stdout);
2449
2450 r = 0;
2451
2452finish:
2453 if (m)
2454 dbus_message_unref(m);
2455
2456 if (reply)
2457 dbus_message_unref(reply);
2458
2459 dbus_error_free(&error);
2460
2461 return r;
2462}
2463
2464static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2465 DBusMessage *m = NULL, *reply = NULL;
2466 DBusError error;
2467 int r;
2468 const char *name = "", *path, *id;
2469 dbus_bool_t cleanup = FALSE;
2470 DBusMessageIter iter, sub;
2471 const char
2472 *interface = "org.freedesktop.systemd1.Unit",
2473 *property = "Id";
2474
2475 dbus_error_init(&error);
2476
2477 if (!(m = dbus_message_new_method_call(
2478 "org.freedesktop.systemd1",
2479 "/org/freedesktop/systemd1",
2480 "org.freedesktop.systemd1.Manager",
2481 "CreateSnapshot"))) {
2482 log_error("Could not allocate message.");
2483 return -ENOMEM;
2484 }
2485
2486 if (n > 1)
2487 name = args[1];
2488
2489 if (!dbus_message_append_args(m,
2490 DBUS_TYPE_STRING, &name,
2491 DBUS_TYPE_BOOLEAN, &cleanup,
2492 DBUS_TYPE_INVALID)) {
2493 log_error("Could not append arguments to message.");
2494 r = -ENOMEM;
2495 goto finish;
2496 }
2497
2498 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2499 log_error("Failed to issue method call: %s", error.message);
2500 r = -EIO;
2501 goto finish;
2502 }
2503
2504 if (!dbus_message_get_args(reply, &error,
2505 DBUS_TYPE_OBJECT_PATH, &path,
2506 DBUS_TYPE_INVALID)) {
2507 log_error("Failed to parse reply: %s", error.message);
2508 r = -EIO;
2509 goto finish;
2510 }
2511
2512 dbus_message_unref(m);
2513 if (!(m = dbus_message_new_method_call(
2514 "org.freedesktop.systemd1",
2515 path,
2516 "org.freedesktop.DBus.Properties",
2517 "Get"))) {
2518 log_error("Could not allocate message.");
2519 return -ENOMEM;
2520 }
2521
2522 if (!dbus_message_append_args(m,
2523 DBUS_TYPE_STRING, &interface,
2524 DBUS_TYPE_STRING, &property,
2525 DBUS_TYPE_INVALID)) {
2526 log_error("Could not append arguments to message.");
2527 r = -ENOMEM;
2528 goto finish;
2529 }
2530
2531 dbus_message_unref(reply);
2532 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2533 log_error("Failed to issue method call: %s", error.message);
2534 r = -EIO;
2535 goto finish;
2536 }
2537
2538 if (!dbus_message_iter_init(reply, &iter) ||
2539 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2540 log_error("Failed to parse reply.");
2541 r = -EIO;
2542 goto finish;
2543 }
2544
2545 dbus_message_iter_recurse(&iter, &sub);
2546
2547 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2548 log_error("Failed to parse reply.");
2549 r = -EIO;
2550 goto finish;
2551 }
2552
2553 dbus_message_iter_get_basic(&sub, &id);
0183528f
LP
2554
2555 if (!arg_quiet)
2556 puts(id);
7e4249b9
LP
2557 r = 0;
2558
2559finish:
2560 if (m)
2561 dbus_message_unref(m);
2562
2563 if (reply)
2564 dbus_message_unref(reply);
2565
2566 dbus_error_free(&error);
2567
2568 return r;
2569}
2570
6759e7a7
LP
2571static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2572 DBusMessage *m = NULL, *reply = NULL;
2573 int r;
2574 DBusError error;
2575 unsigned i;
2576
2577 assert(bus);
2578 assert(args);
2579
2580 dbus_error_init(&error);
2581
2582 for (i = 1; i < n; i++) {
2583 const char *path = NULL;
2584
2585 if (!(m = dbus_message_new_method_call(
2586 "org.freedesktop.systemd1",
2587 "/org/freedesktop/systemd1",
2588 "org.freedesktop.systemd1.Manager",
2589 "GetUnit"))) {
2590 log_error("Could not allocate message.");
2591 r = -ENOMEM;
2592 goto finish;
2593 }
2594
2595 if (!dbus_message_append_args(m,
2596 DBUS_TYPE_STRING, &args[i],
2597 DBUS_TYPE_INVALID)) {
2598 log_error("Could not append arguments to message.");
2599 r = -ENOMEM;
2600 goto finish;
2601 }
2602
2603 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2604 log_error("Failed to issue method call: %s", error.message);
2605 r = -EIO;
2606 goto finish;
2607 }
2608
2609 if (!dbus_message_get_args(reply, &error,
2610 DBUS_TYPE_OBJECT_PATH, &path,
2611 DBUS_TYPE_INVALID)) {
2612 log_error("Failed to parse reply: %s", error.message);
2613 r = -EIO;
2614 goto finish;
2615 }
2616
2617 dbus_message_unref(m);
2618 if (!(m = dbus_message_new_method_call(
2619 "org.freedesktop.systemd1",
2620 path,
2621 "org.freedesktop.systemd1.Snapshot",
2622 "Remove"))) {
2623 log_error("Could not allocate message.");
2624 r = -ENOMEM;
2625 goto finish;
2626 }
2627
2628 dbus_message_unref(reply);
2629 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2630 log_error("Failed to issue method call: %s", error.message);
2631 r = -EIO;
2632 goto finish;
2633 }
2634
2635 dbus_message_unref(m);
2636 dbus_message_unref(reply);
2637 m = reply = NULL;
2638 }
2639
2640 r = 0;
2641
2642finish:
2643 if (m)
2644 dbus_message_unref(m);
2645
2646 if (reply)
2647 dbus_message_unref(reply);
2648
2649 dbus_error_free(&error);
2650
2651 return r;
2652}
2653
7e4249b9
LP
2654static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2655 DBusMessage *m = NULL, *reply = NULL;
2656 DBusError error;
2657 int r;
2658 const char *method;
2659
2660 dbus_error_init(&error);
2661
e4b61340
LP
2662 if (arg_action == ACTION_RELOAD)
2663 method = "Reload";
2664 else if (arg_action == ACTION_REEXEC)
2665 method = "Reexecute";
2666 else {
2667 assert(arg_action == ACTION_SYSTEMCTL);
2668
2669 method =
5632e374
LP
2670 streq(args[0], "clear-jobs") ? "ClearJobs" :
2671 streq(args[0], "daemon-reload") ? "Reload" :
2672 streq(args[0], "daemon-reexec") ? "Reexecute" :
2673 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2674 "Exit";
e4b61340 2675 }
7e4249b9
LP
2676
2677 if (!(m = dbus_message_new_method_call(
2678 "org.freedesktop.systemd1",
2679 "/org/freedesktop/systemd1",
2680 "org.freedesktop.systemd1.Manager",
2681 method))) {
2682 log_error("Could not allocate message.");
2683 return -ENOMEM;
2684 }
2685
2686 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
e4b61340
LP
2687
2688 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2689 /* There's always a fallback possible for
2690 * legacy actions. */
2691 r = 0;
2692 goto finish;
2693 }
2694
7e4249b9
LP
2695 log_error("Failed to issue method call: %s", error.message);
2696 r = -EIO;
2697 goto finish;
2698 }
2699
e4b61340 2700 r = 1;
7e4249b9
LP
2701
2702finish:
2703 if (m)
2704 dbus_message_unref(m);
2705
2706 if (reply)
2707 dbus_message_unref(reply);
2708
2709 dbus_error_free(&error);
2710
2711 return r;
2712}
2713
5632e374
LP
2714static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2715 DBusMessage *m = NULL, *reply = NULL;
2716 unsigned i;
2717 int r;
2718 DBusError error;
2719
2720 assert(bus);
2721 dbus_error_init(&error);
2722
2723 if (n <= 1)
2724 return clear_jobs(bus, args, n);
2725
2726 for (i = 1; i < n; i++) {
2727
2728 if (!(m = dbus_message_new_method_call(
2729 "org.freedesktop.systemd1",
2730 "/org/freedesktop/systemd1",
2731 "org.freedesktop.systemd1.Manager",
2732 "ResetMaintenanceUnit"))) {
2733 log_error("Could not allocate message.");
2734 r = -ENOMEM;
2735 goto finish;
2736 }
2737
2738 if (!dbus_message_append_args(m,
2739 DBUS_TYPE_STRING, args + i,
2740 DBUS_TYPE_INVALID)) {
2741 log_error("Could not append arguments to message.");
2742 r = -ENOMEM;
2743 goto finish;
2744 }
2745
2746 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2747 log_error("Failed to issue method call: %s", error.message);
2748 r = -EIO;
2749 goto finish;
2750 }
2751
2752 dbus_message_unref(m);
2753 dbus_message_unref(reply);
2754 m = reply = NULL;
2755 }
2756
2757 r = 0;
2758
2759finish:
2760 if (m)
2761 dbus_message_unref(m);
2762
2763 if (reply)
2764 dbus_message_unref(reply);
2765
2766 dbus_error_free(&error);
2767
2768 return r;
2769}
2770
7e4249b9
LP
2771static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2772 DBusMessage *m = NULL, *reply = NULL;
2773 DBusError error;
2774 DBusMessageIter iter, sub, sub2;
2775 int r;
2776 const char
2777 *interface = "org.freedesktop.systemd1.Manager",
2778 *property = "Environment";
2779
2780 dbus_error_init(&error);
2781
2782 if (!(m = dbus_message_new_method_call(
2783 "org.freedesktop.systemd1",
2784 "/org/freedesktop/systemd1",
2785 "org.freedesktop.DBus.Properties",
2786 "Get"))) {
2787 log_error("Could not allocate message.");
2788 return -ENOMEM;
2789 }
2790
2791 if (!dbus_message_append_args(m,
2792 DBUS_TYPE_STRING, &interface,
2793 DBUS_TYPE_STRING, &property,
2794 DBUS_TYPE_INVALID)) {
2795 log_error("Could not append arguments to message.");
2796 r = -ENOMEM;
2797 goto finish;
2798 }
2799
2800 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2801 log_error("Failed to issue method call: %s", error.message);
2802 r = -EIO;
2803 goto finish;
2804 }
2805
2806 if (!dbus_message_iter_init(reply, &iter) ||
2807 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2808 log_error("Failed to parse reply.");
2809 r = -EIO;
2810 goto finish;
2811 }
2812
2813 dbus_message_iter_recurse(&iter, &sub);
2814
2815 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2816 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2817 log_error("Failed to parse reply.");
2818 r = -EIO;
2819 goto finish;
2820 }
2821
2822 dbus_message_iter_recurse(&sub, &sub2);
2823
2824 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2825 const char *text;
2826
2827 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2828 log_error("Failed to parse reply.");
2829 r = -EIO;
2830 goto finish;
2831 }
2832
2833 dbus_message_iter_get_basic(&sub2, &text);
2834 printf("%s\n", text);
2835
2836 dbus_message_iter_next(&sub2);
2837 }
2838
2839 r = 0;
2840
2841finish:
2842 if (m)
2843 dbus_message_unref(m);
2844
2845 if (reply)
2846 dbus_message_unref(reply);
2847
2848 dbus_error_free(&error);
2849
2850 return r;
2851}
2852
2853static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2854 DBusMessage *m = NULL, *reply = NULL;
2855 DBusError error;
2856 int r;
2857 const char *method;
2858 DBusMessageIter iter, sub;
2859 unsigned i;
2860
2861 dbus_error_init(&error);
2862
2863 method = streq(args[0], "set-environment")
2864 ? "SetEnvironment"
2865 : "UnsetEnvironment";
2866
2867 if (!(m = dbus_message_new_method_call(
2868 "org.freedesktop.systemd1",
2869 "/org/freedesktop/systemd1",
2870 "org.freedesktop.systemd1.Manager",
2871 method))) {
2872
2873 log_error("Could not allocate message.");
2874 return -ENOMEM;
2875 }
2876
2877 dbus_message_iter_init_append(m, &iter);
2878
2879 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2880 log_error("Could not append arguments to message.");
2881 r = -ENOMEM;
2882 goto finish;
2883 }
2884
2885 for (i = 1; i < n; i++)
2886 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2887 log_error("Could not append arguments to message.");
2888 r = -ENOMEM;
2889 goto finish;
2890 }
2891
2892 if (!dbus_message_iter_close_container(&iter, &sub)) {
2893 log_error("Could not append arguments to message.");
2894 r = -ENOMEM;
2895 goto finish;
2896 }
2897
2898 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2899 log_error("Failed to issue method call: %s", error.message);
2900 r = -EIO;
2901 goto finish;
2902 }
2903
2904 r = 0;
2905
2906finish:
2907 if (m)
2908 dbus_message_unref(m);
2909
2910 if (reply)
2911 dbus_message_unref(reply);
2912
2913 dbus_error_free(&error);
2914
2915 return r;
2916}
2917
e4b61340 2918static int systemctl_help(void) {
7e4249b9 2919
2e33c433 2920 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
5632e374 2921 "Send control commands to or query the systemd manager.\n\n"
48220598
LP
2922 " -h --help Show this help\n"
2923 " -t --type=TYPE List only units of a particular type\n"
393a2f9b 2924 " -p --property=NAME Show only properties by this name\n"
48220598 2925 " -a --all Show all units/properties, including dead/empty ones\n"
8fe914ec 2926 " --full Don't ellipsize unit names.\n"
6f28c033
LP
2927 " --fail When installing a new job, fail if conflicting jobs are\n"
2928 " pending\n"
48220598
LP
2929 " --system Connect to system bus\n"
2930 " --session Connect to session bus\n"
4445a875
LP
2931 " --order When generating graph for dot, show only order\n"
2932 " --require When generating graph for dot, show only requirement\n"
48220598
LP
2933 " -q --quiet Suppress output\n"
2934 " --no-block Do not wait until operation finished\n"
2935 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
7e4249b9
LP
2936 "Commands:\n"
2937 " list-units List units\n"
7e4249b9
LP
2938 " start [NAME...] Start one or more units\n"
2939 " stop [NAME...] Stop one or more units\n"
7e4249b9 2940 " reload [NAME...] Reload one or more units\n"
6f28c033
LP
2941 " restart [NAME...] Start or restart one or more units\n"
2942 " try-restart [NAME...] Restart one or more units if active\n"
2943 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2944 " otherwise start or restart\n"
2945 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2946 " otherwise restart if active\n"
7e4249b9 2947 " isolate [NAME] Start one unit and stop all others\n"
6f28c033 2948 " check [NAME...] Check whether units are active\n"
61cbdc4b 2949 " status [NAME...] Show status of one or more units\n"
6f28c033
LP
2950 " show [NAME...|JOB...] Show properties of one or more\n"
2951 " units/jobs/manager\n"
5632e374
LP
2952 " reset-maintenance [NAME...] Reset maintenance state for all, one\n"
2953 " or more units\n"
48220598
LP
2954 " load [NAME...] Load one or more units\n"
2955 " list-jobs List jobs\n"
2956 " cancel [JOB...] Cancel one or more jobs\n"
2957 " clear-jobs Cancel all jobs\n"
7e4249b9
LP
2958 " monitor Monitor unit/job changes\n"
2959 " dump Dump server status\n"
4445a875 2960 " dot Dump dependency graph for dot(1)\n"
7e4249b9 2961 " snapshot [NAME] Create a snapshot\n"
6759e7a7 2962 " delete [NAME...] Remove one or more snapshots\n"
5ec7ed4e
LP
2963 " daemon-reload Reload systemd manager configuration\n"
2964 " daemon-reexec Reexecute systemd manager\n"
2965 " daemon-exit Ask the systemd manager to quit\n"
7e4249b9
LP
2966 " show-environment Dump environment\n"
2967 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
514f4ef5
LP
2968 " unset-environment [NAME...] Unset one or more environment variables\n"
2969 " halt Shut down and halt the system\n"
2e33c433 2970 " poweroff Shut down and power-off the system\n"
514f4ef5 2971 " reboot Shut down and reboot the system\n"
6f28c033
LP
2972 " rescue Enter system rescue mode\n"
2973 " emergency Enter system emergency mode\n"
2974 " default Enter system default mode\n",
5b6319dc 2975 program_invocation_short_name);
7e4249b9
LP
2976
2977 return 0;
2978}
2979
e4b61340
LP
2980static int halt_help(void) {
2981
2e33c433 2982 printf("%s [OPTIONS...]\n\n"
e4b61340
LP
2983 "%s the system.\n\n"
2984 " --help Show this help\n"
2985 " --halt Halt the machine\n"
2986 " -p --poweroff Switch off the machine\n"
2987 " --reboot Reboot the machine\n"
2e33c433
LP
2988 " -f --force Force immediate halt/power-off/reboot\n"
2989 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
e4b61340 2990 " -d --no-wtmp Don't write wtmp record\n"
2e33c433
LP
2991 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2992 " --no-wall Don't send wall message before halt/power-off/reboot\n",
e4b61340
LP
2993 program_invocation_short_name,
2994 arg_action == ACTION_REBOOT ? "Reboot" :
2995 arg_action == ACTION_POWEROFF ? "Power off" :
2996 "Halt");
2997
2998 return 0;
2999}
3000
3001static int shutdown_help(void) {
3002
2e33c433 3003 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
e4b61340
LP
3004 "Shut down the system.\n\n"
3005 " --help Show this help\n"
3006 " -H --halt Halt the machine\n"
3007 " -P --poweroff Power-off the machine\n"
3008 " -r --reboot Reboot the machine\n"
3009 " -h Equivalent to --poweroff, overriden by --halt\n"
2e33c433
LP
3010 " -k Don't halt/power-off/reboot, just send warnings\n"
3011 " --no-wall Don't send wall message before halt/power-off/reboot\n",
e4b61340
LP
3012 program_invocation_short_name);
3013
3014 return 0;
3015}
3016
3017static int telinit_help(void) {
3018
2e33c433 3019 printf("%s [OPTIONS...] {COMMAND}\n\n"
514f4ef5
LP
3020 "Send control commands to the init daemon.\n\n"
3021 " --help Show this help\n"
2e33c433 3022 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
e4b61340
LP
3023 "Commands:\n"
3024 " 0 Power-off the machine\n"
3025 " 6 Reboot the machine\n"
514f4ef5
LP
3026 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3027 " 1, s, S Enter rescue mode\n"
3028 " q, Q Reload init daemon configuration\n"
3029 " u, U Reexecute init daemon\n",
e4b61340
LP
3030 program_invocation_short_name);
3031
3032 return 0;
3033}
3034
3035static int runlevel_help(void) {
3036
2e33c433 3037 printf("%s [OPTIONS...]\n\n"
e4b61340
LP
3038 "Prints the previous and current runlevel of the init system.\n\n"
3039 " --help Show this help\n",
3040 program_invocation_short_name);
3041
3042 return 0;
3043}
3044
3045static int systemctl_parse_argv(int argc, char *argv[]) {
7e4249b9
LP
3046
3047 enum {
90d473a1 3048 ARG_FAIL = 0x100,
7e4249b9
LP
3049 ARG_SESSION,
3050 ARG_SYSTEM,
6e905d93 3051 ARG_NO_BLOCK,
4445a875
LP
3052 ARG_NO_WALL,
3053 ARG_ORDER,
8fe914ec
LP
3054 ARG_REQUIRE,
3055 ARG_FULL
7e4249b9
LP
3056 };
3057
3058 static const struct option options[] = {
6e905d93
LP
3059 { "help", no_argument, NULL, 'h' },
3060 { "type", required_argument, NULL, 't' },
48220598 3061 { "property", required_argument, NULL, 'p' },
6e905d93 3062 { "all", no_argument, NULL, 'a' },
8fe914ec 3063 { "full", no_argument, NULL, ARG_FULL },
90d473a1 3064 { "fail", no_argument, NULL, ARG_FAIL },
6e905d93
LP
3065 { "session", no_argument, NULL, ARG_SESSION },
3066 { "system", no_argument, NULL, ARG_SYSTEM },
3067 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3068 { "no-wall", no_argument, NULL, ARG_NO_WALL },
0183528f 3069 { "quiet", no_argument, NULL, 'q' },
4445a875
LP
3070 { "order", no_argument, NULL, ARG_ORDER },
3071 { "require", no_argument, NULL, ARG_REQUIRE },
6e905d93 3072 { NULL, 0, NULL, 0 }
7e4249b9
LP
3073 };
3074
3075 int c;
3076
e4b61340 3077 assert(argc >= 0);
7e4249b9
LP
3078 assert(argv);
3079
48220598 3080 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
7e4249b9
LP
3081
3082 switch (c) {
3083
3084 case 'h':
e4b61340 3085 systemctl_help();
7e4249b9
LP
3086 return 0;
3087
3088 case 't':
3089 arg_type = optarg;
3090 break;
3091
48220598
LP
3092 case 'p':
3093 arg_property = optarg;
3094
3095 /* If the user asked for a particular
3096 * property, show it to him, even if it is
3097 * empty. */
3098 arg_all = true;
3099 break;
3100
7e4249b9
LP
3101 case 'a':
3102 arg_all = true;
3103 break;
3104
90d473a1
LP
3105 case ARG_FAIL:
3106 arg_fail = true;
7e4249b9
LP
3107 break;
3108
3109 case ARG_SESSION:
3110 arg_session = true;
3111 break;
3112
3113 case ARG_SYSTEM:
3114 arg_session = false;
3115 break;
3116
6e905d93
LP
3117 case ARG_NO_BLOCK:
3118 arg_no_block = true;
7e4249b9
LP
3119 break;
3120
514f4ef5
LP
3121 case ARG_NO_WALL:
3122 arg_no_wall = true;
3123 break;
3124
4445a875
LP
3125 case ARG_ORDER:
3126 arg_dot = DOT_ORDER;
3127 break;
3128
3129 case ARG_REQUIRE:
3130 arg_dot = DOT_REQUIRE;
3131 break;
3132
8fe914ec
LP
3133 case ARG_FULL:
3134 arg_full = true;
3135 break;
3136
0183528f
LP
3137 case 'q':
3138 arg_quiet = true;
3139 break;
3140
7e4249b9
LP
3141 case '?':
3142 return -EINVAL;
3143
3144 default:
3145 log_error("Unknown option code %c", c);
3146 return -EINVAL;
3147 }
3148 }
3149
3150 return 1;
3151}
3152
e4b61340
LP
3153static int halt_parse_argv(int argc, char *argv[]) {
3154
3155 enum {
3156 ARG_HELP = 0x100,
3157 ARG_HALT,
514f4ef5
LP
3158 ARG_REBOOT,
3159 ARG_NO_WALL
e4b61340
LP
3160 };
3161
3162 static const struct option options[] = {
3163 { "help", no_argument, NULL, ARG_HELP },
3164 { "halt", no_argument, NULL, ARG_HALT },
3165 { "poweroff", no_argument, NULL, 'p' },
3166 { "reboot", no_argument, NULL, ARG_REBOOT },
3167 { "force", no_argument, NULL, 'f' },
3168 { "wtmp-only", no_argument, NULL, 'w' },
3169 { "no-wtmp", no_argument, NULL, 'd' },
3170 { "no-sync", no_argument, NULL, 'n' },
514f4ef5 3171 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
3172 { NULL, 0, NULL, 0 }
3173 };
3174
3175 int c, runlevel;
3176
3177 assert(argc >= 0);
3178 assert(argv);
3179
3180 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3181 if (runlevel == '0' || runlevel == '6')
3182 arg_immediate = true;
3183
3184 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3185 switch (c) {
3186
3187 case ARG_HELP:
3188 halt_help();
3189 return 0;
3190
3191 case ARG_HALT:
3192 arg_action = ACTION_HALT;
3193 break;
3194
3195 case 'p':
3196 arg_action = ACTION_POWEROFF;
3197 break;
3198
3199 case ARG_REBOOT:
3200 arg_action = ACTION_REBOOT;
3201 break;
3202
3203 case 'f':
3204 arg_immediate = true;
3205 break;
3206
3207 case 'w':
3208 arg_dry = true;
3209 break;
3210
3211 case 'd':
3212 arg_no_wtmp = true;
3213 break;
3214
3215 case 'n':
3216 arg_no_sync = true;
3217 break;
3218
514f4ef5
LP
3219 case ARG_NO_WALL:
3220 arg_no_wall = true;
3221 break;
3222
e4b61340
LP
3223 case 'i':
3224 case 'h':
3225 /* Compatibility nops */
3226 break;
3227
3228 case '?':
3229 return -EINVAL;
3230
3231 default:
3232 log_error("Unknown option code %c", c);
3233 return -EINVAL;
3234 }
3235 }
3236
3237 if (optind < argc) {
3238 log_error("Too many arguments.");
3239 return -EINVAL;
3240 }
3241
3242 return 1;
3243}
3244
3245static int shutdown_parse_argv(int argc, char *argv[]) {
3246
3247 enum {
3248 ARG_HELP = 0x100,
514f4ef5 3249 ARG_NO_WALL
e4b61340
LP
3250 };
3251
3252 static const struct option options[] = {
3253 { "help", no_argument, NULL, ARG_HELP },
3254 { "halt", no_argument, NULL, 'H' },
3255 { "poweroff", no_argument, NULL, 'P' },
3256 { "reboot", no_argument, NULL, 'r' },
514f4ef5 3257 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
3258 { NULL, 0, NULL, 0 }
3259 };
3260
3261 int c;
3262
3263 assert(argc >= 0);
3264 assert(argv);
3265
3266 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3267 switch (c) {
3268
3269 case ARG_HELP:
3270 shutdown_help();
3271 return 0;
3272
3273 case 'H':
3274 arg_action = ACTION_HALT;
3275 break;
3276
3277 case 'P':
3278 arg_action = ACTION_POWEROFF;
3279 break;
3280
3281 case 'r':
3282 arg_action = ACTION_REBOOT;
3283 break;
3284
3285 case 'h':
3286 if (arg_action != ACTION_HALT)
3287 arg_action = ACTION_POWEROFF;
3288 break;
3289
3290 case 'k':
3291 arg_dry = true;
3292 break;
3293
514f4ef5
LP
3294 case ARG_NO_WALL:
3295 arg_no_wall = true;
3296 break;
3297
e4b61340
LP
3298 case 't':
3299 case 'a':
3300 /* Compatibility nops */
3301 break;
3302
3303 case '?':
3304 return -EINVAL;
3305
3306 default:
3307 log_error("Unknown option code %c", c);
3308 return -EINVAL;
3309 }
3310 }
3311
4545812f
LP
3312 if (argc > optind && !streq(argv[optind], "now"))
3313 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
442b9094 3314
e4b61340
LP
3315 /* We ignore the time argument */
3316 if (argc > optind + 1)
3317 arg_wall = argv + optind + 1;
3318
3319 optind = argc;
3320
3321 return 1;
e4b61340
LP
3322}
3323
3324static int telinit_parse_argv(int argc, char *argv[]) {
3325
3326 enum {
3327 ARG_HELP = 0x100,
514f4ef5 3328 ARG_NO_WALL
e4b61340
LP
3329 };
3330
3331 static const struct option options[] = {
3332 { "help", no_argument, NULL, ARG_HELP },
514f4ef5 3333 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
3334 { NULL, 0, NULL, 0 }
3335 };
3336
3337 static const struct {
3338 char from;
3339 enum action to;
3340 } table[] = {
3341 { '0', ACTION_POWEROFF },
3342 { '6', ACTION_REBOOT },
ef2f1067 3343 { '1', ACTION_RESCUE },
e4b61340
LP
3344 { '2', ACTION_RUNLEVEL2 },
3345 { '3', ACTION_RUNLEVEL3 },
3346 { '4', ACTION_RUNLEVEL4 },
3347 { '5', ACTION_RUNLEVEL5 },
3348 { 's', ACTION_RESCUE },
3349 { 'S', ACTION_RESCUE },
3350 { 'q', ACTION_RELOAD },
3351 { 'Q', ACTION_RELOAD },
3352 { 'u', ACTION_REEXEC },
3353 { 'U', ACTION_REEXEC }
3354 };
3355
3356 unsigned i;
3357 int c;
3358
3359 assert(argc >= 0);
3360 assert(argv);
3361
3362 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3363 switch (c) {
3364
3365 case ARG_HELP:
3366 telinit_help();
3367 return 0;
3368
514f4ef5
LP
3369 case ARG_NO_WALL:
3370 arg_no_wall = true;
3371 break;
3372
e4b61340
LP
3373 case '?':
3374 return -EINVAL;
3375
3376 default:
3377 log_error("Unknown option code %c", c);
3378 return -EINVAL;
3379 }
3380 }
3381
3382 if (optind >= argc) {
2f02ce40 3383 telinit_help();
e4b61340
LP
3384 return -EINVAL;
3385 }
3386
3387 if (optind + 1 < argc) {
3388 log_error("Too many arguments.");
3389 return -EINVAL;
3390 }
3391
3392 if (strlen(argv[optind]) != 1) {
3393 log_error("Expected single character argument.");
3394 return -EINVAL;
3395 }
3396
3397 for (i = 0; i < ELEMENTSOF(table); i++)
3398 if (table[i].from == argv[optind][0])
3399 break;
3400
3401 if (i >= ELEMENTSOF(table)) {
3402 log_error("Unknown command %s.", argv[optind]);
3403 return -EINVAL;
3404 }
3405
3406 arg_action = table[i].to;
3407
3408 optind ++;
3409
3410 return 1;
3411}
3412
3413static int runlevel_parse_argv(int argc, char *argv[]) {
3414
3415 enum {
3416 ARG_HELP = 0x100,
3417 };
3418
3419 static const struct option options[] = {
3420 { "help", no_argument, NULL, ARG_HELP },
3421 { NULL, 0, NULL, 0 }
3422 };
3423
3424 int c;
3425
3426 assert(argc >= 0);
3427 assert(argv);
3428
3429 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3430 switch (c) {
3431
3432 case ARG_HELP:
3433 runlevel_help();
3434 return 0;
3435
3436 case '?':
3437 return -EINVAL;
3438
3439 default:
3440 log_error("Unknown option code %c", c);
3441 return -EINVAL;
3442 }
3443 }
3444
3445 if (optind < argc) {
3446 log_error("Too many arguments.");
3447 return -EINVAL;
3448 }
3449
3450 return 1;
3451}
3452
3453static int parse_argv(int argc, char *argv[]) {
3454 assert(argc >= 0);
3455 assert(argv);
3456
3457 if (program_invocation_short_name) {
3458
3459 if (strstr(program_invocation_short_name, "halt")) {
3460 arg_action = ACTION_HALT;
3461 return halt_parse_argv(argc, argv);
3462 } else if (strstr(program_invocation_short_name, "poweroff")) {
3463 arg_action = ACTION_POWEROFF;
3464 return halt_parse_argv(argc, argv);
3465 } else if (strstr(program_invocation_short_name, "reboot")) {
3466 arg_action = ACTION_REBOOT;
3467 return halt_parse_argv(argc, argv);
3468 } else if (strstr(program_invocation_short_name, "shutdown")) {
3469 arg_action = ACTION_POWEROFF;
3470 return shutdown_parse_argv(argc, argv);
3471 } else if (strstr(program_invocation_short_name, "init")) {
3472 arg_action = ACTION_INVALID;
3473 return telinit_parse_argv(argc, argv);
3474 } else if (strstr(program_invocation_short_name, "runlevel")) {
3475 arg_action = ACTION_RUNLEVEL;
3476 return runlevel_parse_argv(argc, argv);
3477 }
3478 }
3479
3480 arg_action = ACTION_SYSTEMCTL;
3481 return systemctl_parse_argv(argc, argv);
3482}
3483
d55ae9e6 3484static int action_to_runlevel(void) {
eb22ac37
LP
3485
3486 static const char table[_ACTION_MAX] = {
3487 [ACTION_HALT] = '0',
3488 [ACTION_POWEROFF] = '0',
3489 [ACTION_REBOOT] = '6',
3490 [ACTION_RUNLEVEL2] = '2',
3491 [ACTION_RUNLEVEL3] = '3',
3492 [ACTION_RUNLEVEL4] = '4',
3493 [ACTION_RUNLEVEL5] = '5',
3494 [ACTION_RESCUE] = '1'
3495 };
3496
d55ae9e6
LP
3497 assert(arg_action < _ACTION_MAX);
3498
3499 return table[arg_action];
3500}
3501
f1c5860b 3502static int talk_upstart(void) {
d55ae9e6
LP
3503 DBusMessage *m = NULL, *reply = NULL;
3504 DBusError error;
3505 int previous, rl, r;
3506 char
3507 env1_buf[] = "RUNLEVEL=X",
3508 env2_buf[] = "PREVLEVEL=X";
3509 char *env1 = env1_buf, *env2 = env2_buf;
3510 const char *emit = "runlevel";
3511 dbus_bool_t b_false = FALSE;
3512 DBusMessageIter iter, sub;
f1c5860b 3513 DBusConnection *bus;
d55ae9e6
LP
3514
3515 dbus_error_init(&error);
3516
3517 if (!(rl = action_to_runlevel()))
3518 return 0;
3519
3520 if (utmp_get_runlevel(&previous, NULL) < 0)
3521 previous = 'N';
3522
b574246b 3523 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
f1c5860b
LP
3524 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3525 r = 0;
3526 goto finish;
3527 }
3528
3529 log_error("Failed to connect to Upstart bus: %s", error.message);
3530 r = -EIO;
3531 goto finish;
3532 }
3533
3534 if ((r = bus_check_peercred(bus)) < 0) {
3535 log_error("Failed to verify owner of bus.");
3536 goto finish;
3537 }
3538
d55ae9e6
LP
3539 if (!(m = dbus_message_new_method_call(
3540 "com.ubuntu.Upstart",
3541 "/com/ubuntu/Upstart",
3542 "com.ubuntu.Upstart0_6",
3543 "EmitEvent"))) {
3544
3545 log_error("Could not allocate message.");
f1c5860b
LP
3546 r = -ENOMEM;
3547 goto finish;
d55ae9e6
LP
3548 }
3549
3550 dbus_message_iter_init_append(m, &iter);
3551
3552 env1_buf[sizeof(env1_buf)-2] = rl;
3553 env2_buf[sizeof(env2_buf)-2] = previous;
3554
3555 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3556 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3557 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3558 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3559 !dbus_message_iter_close_container(&iter, &sub) ||
3560 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3561 log_error("Could not append arguments to message.");
3562 r = -ENOMEM;
3563 goto finish;
3564 }
3565
3566 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3567
3568 if (error_is_no_service(&error)) {
3569 r = 0;
3570 goto finish;
3571 }
3572
3573 log_error("Failed to issue method call: %s", error.message);
3574 r = -EIO;
3575 goto finish;
3576 }
3577
3578 r = 1;
3579
3580finish:
3581 if (m)
3582 dbus_message_unref(m);
3583
3584 if (reply)
3585 dbus_message_unref(reply);
3586
b574246b
LP
3587 if (bus) {
3588 dbus_connection_close(bus);
f1c5860b 3589 dbus_connection_unref(bus);
b574246b 3590 }
f1c5860b 3591
d55ae9e6
LP
3592 dbus_error_free(&error);
3593
3594 return r;
3595}
3596
3597static int talk_initctl(void) {
eb22ac37
LP
3598 struct init_request request;
3599 int r, fd;
d55ae9e6 3600 char rl;
eb22ac37 3601
d55ae9e6 3602 if (!(rl = action_to_runlevel()))
eb22ac37
LP
3603 return 0;
3604
3605 zero(request);
3606 request.magic = INIT_MAGIC;
3607 request.sleeptime = 0;
3608 request.cmd = INIT_CMD_RUNLVL;
d55ae9e6
LP
3609 request.runlevel = rl;
3610
3611 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3612
3613 if (errno == ENOENT)
3614 return 0;
eb22ac37 3615
d55ae9e6 3616 log_error("Failed to open "INIT_FIFO": %m");
eb22ac37 3617 return -errno;
d55ae9e6 3618 }
eb22ac37 3619
d55ae9e6 3620 errno = 0;
eb22ac37
LP
3621 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3622 close_nointr_nofail(fd);
3623
d55ae9e6
LP
3624 if (r < 0) {
3625 log_error("Failed to write to "INIT_FIFO": %m");
eb22ac37 3626 return errno ? -errno : -EIO;
d55ae9e6 3627 }
eb22ac37
LP
3628
3629 return 1;
e4b61340
LP
3630}
3631
3632static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
7e4249b9 3633
7e4249b9
LP
3634 static const struct {
3635 const char* verb;
3636 const enum {
3637 MORE,
3638 LESS,
3639 EQUAL
3640 } argc_cmp;
3641 const int argc;
3642 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3643 } verbs[] = {
3644 { "list-units", LESS, 1, list_units },
3645 { "list-jobs", EQUAL, 1, list_jobs },
3646 { "clear-jobs", EQUAL, 1, clear_jobs },
3647 { "load", MORE, 2, load_unit },
3648 { "cancel", MORE, 2, cancel_job },
3649 { "start", MORE, 2, start_unit },
3650 { "stop", MORE, 2, start_unit },
3651 { "reload", MORE, 2, start_unit },
3652 { "restart", MORE, 2, start_unit },
6f28c033
LP
3653 { "try-restart", MORE, 2, start_unit },
3654 { "reload-or-restart", MORE, 2, start_unit },
3655 { "reload-or-try-restart", MORE, 2, start_unit },
e4b61340 3656 { "isolate", EQUAL, 2, start_unit },
0183528f 3657 { "check", MORE, 2, check_unit },
48220598 3658 { "show", MORE, 1, show },
61cbdc4b 3659 { "status", MORE, 2, show },
7e4249b9
LP
3660 { "monitor", EQUAL, 1, monitor },
3661 { "dump", EQUAL, 1, dump },
4445a875 3662 { "dot", EQUAL, 1, dot },
7e4249b9 3663 { "snapshot", LESS, 2, snapshot },
6759e7a7 3664 { "delete", MORE, 2, delete_snapshot },
7e4249b9
LP
3665 { "daemon-reload", EQUAL, 1, clear_jobs },
3666 { "daemon-reexec", EQUAL, 1, clear_jobs },
3667 { "daemon-exit", EQUAL, 1, clear_jobs },
3668 { "show-environment", EQUAL, 1, show_enviroment },
3669 { "set-environment", MORE, 2, set_environment },
3670 { "unset-environment", MORE, 2, set_environment },
514f4ef5
LP
3671 { "halt", EQUAL, 1, start_special },
3672 { "poweroff", EQUAL, 1, start_special },
3673 { "reboot", EQUAL, 1, start_special },
3674 { "default", EQUAL, 1, start_special },
3675 { "rescue", EQUAL, 1, start_special },
4445a875 3676 { "emergency", EQUAL, 1, start_special },
5632e374 3677 { "reset-maintenance", MORE, 1, reset_maintenance },
7e4249b9
LP
3678 };
3679
e4b61340 3680 int left;
7e4249b9 3681 unsigned i;
7e4249b9 3682
e4b61340
LP
3683 assert(bus);
3684 assert(argc >= 0);
3685 assert(argv);
7e4249b9
LP
3686
3687 left = argc - optind;
3688
3689 if (left <= 0)
3690 /* Special rule: no arguments means "list-units" */
3691 i = 0;
3692 else {
0183528f
LP
3693 if (streq(argv[optind], "help")) {
3694 systemctl_help();
3695 return 0;
3696 }
3697
7e4249b9
LP
3698 for (i = 0; i < ELEMENTSOF(verbs); i++)
3699 if (streq(argv[optind], verbs[i].verb))
3700 break;
3701
3702 if (i >= ELEMENTSOF(verbs)) {
3703 log_error("Unknown operation %s", argv[optind]);
e4b61340 3704 return -EINVAL;
7e4249b9
LP
3705 }
3706 }
3707
3708 switch (verbs[i].argc_cmp) {
3709
3710 case EQUAL:
3711 if (left != verbs[i].argc) {
3712 log_error("Invalid number of arguments.");
e4b61340 3713 return -EINVAL;
7e4249b9
LP
3714 }
3715
3716 break;
3717
3718 case MORE:
3719 if (left < verbs[i].argc) {
3720 log_error("Too few arguments.");
e4b61340 3721 return -EINVAL;
7e4249b9
LP
3722 }
3723
3724 break;
3725
3726 case LESS:
3727 if (left > verbs[i].argc) {
3728 log_error("Too many arguments.");
e4b61340 3729 return -EINVAL;
7e4249b9
LP
3730 }
3731
3732 break;
3733
3734 default:
3735 assert_not_reached("Unknown comparison operator.");
3736 }
3737
e4b61340
LP
3738 return verbs[i].dispatch(bus, argv + optind, left);
3739}
3740
3741static int reload_with_fallback(DBusConnection *bus) {
3742 int r;
3743
3744 if (bus) {
3745 /* First, try systemd via D-Bus. */
3746 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3747 return 0;
3748 }
3749
3750 /* Nothing else worked, so let's try signals */
3751 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3752
3753 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3754 log_error("kill() failed: %m");
3755 return -errno;
3756 }
3757
3758 return 0;
3759}
3760
3761static int start_with_fallback(DBusConnection *bus) {
3762 int r;
3763
ef2f1067 3764
e4b61340
LP
3765 if (bus) {
3766 /* First, try systemd via D-Bus. */
3767 if ((r = start_unit(bus, NULL, 0)) > 0)
983d9c90 3768 goto done;
e4b61340
LP
3769
3770 /* Hmm, talking to systemd via D-Bus didn't work. Then
3771 * let's try to talk to Upstart via D-Bus. */
f1c5860b 3772 if ((r = talk_upstart()) > 0)
983d9c90 3773 goto done;
e4b61340
LP
3774 }
3775
3776 /* Nothing else worked, so let's try
3777 * /dev/initctl */
d55ae9e6 3778 if ((r = talk_initctl()) != 0)
983d9c90 3779 goto done;
d55ae9e6
LP
3780
3781 log_error("Failed to talk to init daemon.");
3782 return -EIO;
983d9c90
LP
3783
3784done:
3785 warn_wall(arg_action);
3786 return 0;
e4b61340
LP
3787}
3788
3789static int halt_main(DBusConnection *bus) {
3790 int r;
3791
bc8c2f5c
LP
3792 if (geteuid() != 0) {
3793 log_error("Must to be root.");
3794 return -EPERM;
3795 }
3796
d47b555b 3797 if (!arg_dry && !arg_immediate)
e4b61340
LP
3798 return start_with_fallback(bus);
3799
3800 if (!arg_no_wtmp)
3801 if ((r = utmp_put_shutdown(0)) < 0)
3802 log_warning("Failed to write utmp record: %s", strerror(-r));
3803
3804 if (!arg_no_sync)
3805 sync();
3806
3807 if (arg_dry)
3808 return 0;
3809
3810 /* Make sure C-A-D is handled by the kernel from this
3811 * point on... */
3812 reboot(RB_ENABLE_CAD);
3813
3814 switch (arg_action) {
3815
3816 case ACTION_HALT:
3817 log_info("Halting");
3818 reboot(RB_HALT_SYSTEM);
3819 break;
3820
3821 case ACTION_POWEROFF:
3822 log_info("Powering off");
3823 reboot(RB_POWER_OFF);
3824 break;
3825
3826 case ACTION_REBOOT:
3827 log_info("Rebooting");
3828 reboot(RB_AUTOBOOT);
3829 break;
3830
3831 default:
3832 assert_not_reached("Unknown halt action.");
3833 }
3834
3835 /* We should never reach this. */
3836 return -ENOSYS;
3837}
3838
3839static int runlevel_main(void) {
3840 int r, runlevel, previous;
3841
3842 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3843 printf("unknown");
3844 return r;
3845 }
3846
3847 printf("%c %c\n",
3848 previous <= 0 ? 'N' : previous,
3849 runlevel <= 0 ? 'N' : runlevel);
3850
3851 return 0;
3852}
3853
3854int main(int argc, char*argv[]) {
3855 int r, retval = 1;
3856 DBusConnection *bus = NULL;
3857 DBusError error;
3858
3859 dbus_error_init(&error);
3860
3861 log_parse_environment();
3862
3863 if ((r = parse_argv(argc, argv)) < 0)
3864 goto finish;
3865 else if (r == 0) {
3866 retval = 0;
7e4249b9
LP
3867 goto finish;
3868 }
3869
e4b61340
LP
3870 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3871 * let's shortcut this */
3872 if (arg_action == ACTION_RUNLEVEL) {
3873 retval = runlevel_main() < 0;
3874 goto finish;
3875 }
3876
f4579ce7 3877 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
e4b61340
LP
3878
3879 switch (arg_action) {
3880
3881 case ACTION_SYSTEMCTL: {
3882
3883 if (!bus) {
3884 log_error("Failed to get D-Bus connection: %s", error.message);
3885 goto finish;
3886 }
3887
3888 retval = systemctl_main(bus, argc, argv) < 0;
3889 break;
3890 }
3891
3892 case ACTION_HALT:
3893 case ACTION_POWEROFF:
3894 case ACTION_REBOOT:
3895 retval = halt_main(bus) < 0;
3896 break;
3897
e4b61340
LP
3898 case ACTION_RUNLEVEL2:
3899 case ACTION_RUNLEVEL3:
3900 case ACTION_RUNLEVEL4:
3901 case ACTION_RUNLEVEL5:
3902 case ACTION_RESCUE:
514f4ef5 3903 case ACTION_EMERGENCY:
eb22ac37 3904 case ACTION_DEFAULT:
e4b61340
LP
3905 retval = start_with_fallback(bus) < 0;
3906 break;
7e4249b9 3907
e4b61340
LP
3908 case ACTION_RELOAD:
3909 case ACTION_REEXEC:
3910 retval = reload_with_fallback(bus) < 0;
3911 break;
3912
eb22ac37
LP
3913 case ACTION_INVALID:
3914 case ACTION_RUNLEVEL:
e4b61340
LP
3915 default:
3916 assert_not_reached("Unknown action");
3917 }
7e4249b9
LP
3918
3919finish:
3920
b574246b
LP
3921 if (bus) {
3922 dbus_connection_close(bus);
7e4249b9 3923 dbus_connection_unref(bus);
b574246b 3924 }
7e4249b9 3925
e4b61340
LP
3926 dbus_error_free(&error);
3927
7e4249b9
LP
3928 dbus_shutdown();
3929
3930 return retval;
3931}