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