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