]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl.c
dbus: complete exec status coverage
[thirdparty/systemd.git] / src / systemctl.c
CommitLineData
7e4249b9
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
e4b61340 22#include <sys/reboot.h>
7e4249b9
LP
23#include <stdio.h>
24#include <getopt.h>
25#include <stdbool.h>
26#include <string.h>
27#include <errno.h>
28#include <sys/ioctl.h>
29#include <termios.h>
30#include <unistd.h>
eb22ac37 31#include <fcntl.h>
f1c5860b 32#include <sys/socket.h>
7e4249b9
LP
33
34#include <dbus/dbus.h>
35
36#include "log.h"
37#include "util.h"
38#include "macro.h"
39#include "set.h"
e4b61340 40#include "utmp-wtmp.h"
514f4ef5 41#include "special.h"
eb22ac37 42#include "initreq.h"
e4a9373f 43#include "strv.h"
7e4249b9
LP
44
45static const char *arg_type = NULL;
48220598 46static const char *arg_property = NULL;
7e4249b9
LP
47static bool arg_all = false;
48static bool arg_replace = false;
49static bool arg_session = false;
6e905d93 50static bool arg_no_block = false;
e4b61340
LP
51static bool arg_immediate = false;
52static bool arg_no_wtmp = false;
53static bool arg_no_sync = false;
514f4ef5 54static bool arg_no_wall = false;
e4b61340 55static bool arg_dry = false;
0183528f 56static bool arg_quiet = false;
e4b61340
LP
57static char **arg_wall = NULL;
58enum action {
59 ACTION_INVALID,
60 ACTION_SYSTEMCTL,
61 ACTION_HALT,
62 ACTION_POWEROFF,
63 ACTION_REBOOT,
e4b61340
LP
64 ACTION_RUNLEVEL2,
65 ACTION_RUNLEVEL3,
66 ACTION_RUNLEVEL4,
67 ACTION_RUNLEVEL5,
68 ACTION_RESCUE,
514f4ef5
LP
69 ACTION_EMERGENCY,
70 ACTION_DEFAULT,
e4b61340
LP
71 ACTION_RELOAD,
72 ACTION_REEXEC,
73 ACTION_RUNLEVEL,
74 _ACTION_MAX
75} arg_action = ACTION_SYSTEMCTL;
76
77static bool error_is_no_service(DBusError *error) {
78
514f4ef5
LP
79 assert(error);
80
e4b61340
LP
81 if (!dbus_error_is_set(error))
82 return false;
83
84 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
85 return true;
86
87 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
88 return true;
89
90 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
91}
7e4249b9
LP
92
93static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
94
514f4ef5
LP
95 assert(iter);
96 assert(data);
97
7e4249b9
LP
98 if (dbus_message_iter_get_arg_type(iter) != type)
99 return -EIO;
100
101 dbus_message_iter_get_basic(iter, data);
102
103 if (!dbus_message_iter_next(iter) != !next)
104 return -EIO;
105
106 return 0;
107}
108
f1c5860b
LP
109static int bus_check_peercred(DBusConnection *c) {
110 int fd;
111 struct ucred ucred;
112 socklen_t l;
113
114 assert(c);
115
116 assert_se(dbus_connection_get_unix_fd(c, &fd));
117
118 l = sizeof(struct ucred);
119 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
120 log_error("SO_PEERCRED failed: %m");
121 return -errno;
122 }
123
124 if (l != sizeof(struct ucred)) {
125 log_error("SO_PEERCRED returned wrong size.");
126 return -E2BIG;
127 }
128
129 if (ucred.uid != 0)
130 return -EPERM;
131
132 return 1;
133}
134
7e4249b9
LP
135static int columns(void) {
136 static int parsed_columns = 0;
137 const char *e;
138
139 if (parsed_columns > 0)
140 return parsed_columns;
141
142 if ((e = getenv("COLUMNS")))
143 parsed_columns = atoi(e);
144
145 if (parsed_columns <= 0) {
146 struct winsize ws;
147 zero(ws);
148
149 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
150 parsed_columns = ws.ws_col;
151 }
152
153 if (parsed_columns <= 0)
154 parsed_columns = 80;
155
156 return parsed_columns;
e4b61340 157
7e4249b9
LP
158}
159
514f4ef5 160static void warn_wall(enum action action) {
ef2f1067 161 static const char *table[_ACTION_MAX] = {
514f4ef5
LP
162 [ACTION_HALT] = "The system is going down for system halt NOW!",
163 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
164 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
165 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
166 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
ef2f1067
LP
167 };
168
514f4ef5
LP
169 if (arg_no_wall)
170 return;
171
e4a9373f
LP
172 if (arg_wall) {
173 char *p;
174
175 if (!(p = strv_join(arg_wall, " "))) {
176 log_error("Failed to join strings.");
177 return;
178 }
179
180 if (*p) {
181 utmp_wall(p);
182 free(p);
183 return;
184 }
185
186 free(p);
187 }
188
514f4ef5 189 if (!table[action])
ef2f1067
LP
190 return;
191
514f4ef5 192 utmp_wall(table[action]);
ef2f1067
LP
193}
194
7e4249b9
LP
195static int list_units(DBusConnection *bus, char **args, unsigned n) {
196 DBusMessage *m = NULL, *reply = NULL;
197 DBusError error;
198 int r;
199 DBusMessageIter iter, sub, sub2;
200 unsigned k = 0;
201
202 dbus_error_init(&error);
203
514f4ef5
LP
204 assert(bus);
205
7e4249b9
LP
206 if (!(m = dbus_message_new_method_call(
207 "org.freedesktop.systemd1",
208 "/org/freedesktop/systemd1",
209 "org.freedesktop.systemd1.Manager",
210 "ListUnits"))) {
211 log_error("Could not allocate message.");
212 return -ENOMEM;
213 }
214
215 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
216 log_error("Failed to issue method call: %s", error.message);
217 r = -EIO;
218 goto finish;
219 }
220
221 if (!dbus_message_iter_init(reply, &iter) ||
222 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
223 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
224 log_error("Failed to parse reply.");
225 r = -EIO;
226 goto finish;
227 }
228
229 dbus_message_iter_recurse(&iter, &sub);
230
231 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
232
233 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
234 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
235 uint32_t job_id;
236
237 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
238 log_error("Failed to parse reply.");
239 r = -EIO;
240 goto finish;
241 }
242
243 dbus_message_iter_recurse(&sub, &sub2);
244
245 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
246 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
247 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
248 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
249 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
250 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
251 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
252 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
253 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
254 log_error("Failed to parse reply.");
255 r = -EIO;
256 goto finish;
257 }
258
259 if ((!arg_type || ((dot = strrchr(id, '.')) &&
260 streq(dot+1, arg_type))) &&
261 (arg_all || !streq(active_state, "inactive"))) {
262
263 int a = 0, b = 0;
264
265 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
266
267 if (job_id != 0)
268 printf(" %-15s%n", job_type, &b);
269 else
270 b = 1 + 15;
271
272 if (a + b + 2 < columns()) {
273 if (job_id == 0)
274 printf(" ");
275
276 printf("%.*s", columns() - a - b - 2, description);
277 }
278
279 fputs("\n", stdout);
280 k++;
281 }
282
283 dbus_message_iter_next(&sub);
284 }
285
286 if (arg_all)
287 printf("\n%u units listed.\n", k);
288 else
289 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
290
291 r = 0;
292
293finish:
294 if (m)
295 dbus_message_unref(m);
296
297 if (reply)
298 dbus_message_unref(reply);
299
300 dbus_error_free(&error);
301
302 return r;
303}
304
305static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
306 DBusMessage *m = NULL, *reply = NULL;
307 DBusError error;
308 int r;
309 DBusMessageIter iter, sub, sub2;
310 unsigned k = 0;
311
312 dbus_error_init(&error);
313
514f4ef5
LP
314 assert(bus);
315
7e4249b9
LP
316 if (!(m = dbus_message_new_method_call(
317 "org.freedesktop.systemd1",
318 "/org/freedesktop/systemd1",
319 "org.freedesktop.systemd1.Manager",
320 "ListJobs"))) {
321 log_error("Could not allocate message.");
322 return -ENOMEM;
323 }
324
325 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
326 log_error("Failed to issue method call: %s", error.message);
327 r = -EIO;
328 goto finish;
329 }
330
331 if (!dbus_message_iter_init(reply, &iter) ||
332 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
333 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
334 log_error("Failed to parse reply.");
335 r = -EIO;
336 goto finish;
337 }
338
339 dbus_message_iter_recurse(&iter, &sub);
340
341 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
342
343 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
344 const char *name, *type, *state, *job_path, *unit_path;
345 uint32_t id;
346
347 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
348 log_error("Failed to parse reply.");
349 r = -EIO;
350 goto finish;
351 }
352
353 dbus_message_iter_recurse(&sub, &sub2);
354
355 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
356 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
357 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
358 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
359 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
360 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
361 log_error("Failed to parse reply.");
362 r = -EIO;
363 goto finish;
364 }
365
366 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
367 k++;
368
369 dbus_message_iter_next(&sub);
370 }
371
372 printf("\n%u jobs listed.\n", k);
373 r = 0;
374
375finish:
376 if (m)
377 dbus_message_unref(m);
378
379 if (reply)
380 dbus_message_unref(reply);
381
382 dbus_error_free(&error);
383
384 return r;
385}
386
387static int load_unit(DBusConnection *bus, char **args, unsigned n) {
388 DBusMessage *m = NULL, *reply = NULL;
389 DBusError error;
390 int r;
391 unsigned i;
392
393 dbus_error_init(&error);
394
514f4ef5
LP
395 assert(bus);
396 assert(args);
397
7e4249b9
LP
398 for (i = 1; i < n; i++) {
399
400 if (!(m = dbus_message_new_method_call(
401 "org.freedesktop.systemd1",
402 "/org/freedesktop/systemd1",
403 "org.freedesktop.systemd1.Manager",
404 "LoadUnit"))) {
405 log_error("Could not allocate message.");
406 r = -ENOMEM;
407 goto finish;
408 }
409
410 if (!dbus_message_append_args(m,
411 DBUS_TYPE_STRING, &args[i],
412 DBUS_TYPE_INVALID)) {
413 log_error("Could not append arguments to message.");
414 r = -ENOMEM;
415 goto finish;
416 }
417
418 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
419 log_error("Failed to issue method call: %s", error.message);
420 r = -EIO;
421 goto finish;
422 }
423
424 dbus_message_unref(m);
425 dbus_message_unref(reply);
426
427 m = reply = NULL;
428 }
429
430 r = 0;
431
432finish:
433 if (m)
434 dbus_message_unref(m);
435
436 if (reply)
437 dbus_message_unref(reply);
438
439 dbus_error_free(&error);
440
441 return r;
442}
443
444static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
445 DBusMessage *m = NULL, *reply = NULL;
446 DBusError error;
447 int r;
448 unsigned i;
449
450 dbus_error_init(&error);
451
514f4ef5
LP
452 assert(bus);
453 assert(args);
454
7e4249b9
LP
455 for (i = 1; i < n; i++) {
456 unsigned id;
457 const char *path;
458
459 if (!(m = dbus_message_new_method_call(
460 "org.freedesktop.systemd1",
461 "/org/freedesktop/systemd1",
462 "org.freedesktop.systemd1.Manager",
463 "GetJob"))) {
464 log_error("Could not allocate message.");
465 r = -ENOMEM;
466 goto finish;
467 }
468
469 if ((r = safe_atou(args[i], &id)) < 0) {
470 log_error("Failed to parse job id: %s", strerror(-r));
471 goto finish;
472 }
473
474 assert_cc(sizeof(uint32_t) == sizeof(id));
475 if (!dbus_message_append_args(m,
476 DBUS_TYPE_UINT32, &id,
477 DBUS_TYPE_INVALID)) {
478 log_error("Could not append arguments to message.");
479 r = -ENOMEM;
480 goto finish;
481 }
482
483 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
484 log_error("Failed to issue method call: %s", error.message);
485 r = -EIO;
486 goto finish;
487 }
488
489 if (!dbus_message_get_args(reply, &error,
490 DBUS_TYPE_OBJECT_PATH, &path,
491 DBUS_TYPE_INVALID)) {
492 log_error("Failed to parse reply: %s", error.message);
493 r = -EIO;
494 goto finish;
495 }
496
497 dbus_message_unref(m);
498 if (!(m = dbus_message_new_method_call(
499 "org.freedesktop.systemd1",
500 path,
501 "org.freedesktop.systemd1.Job",
502 "Cancel"))) {
503 log_error("Could not allocate message.");
504 r = -ENOMEM;
505 goto finish;
506 }
507
508 dbus_message_unref(reply);
509 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
510 log_error("Failed to issue method call: %s", error.message);
511 r = -EIO;
512 goto finish;
513 }
514
515 dbus_message_unref(m);
516 dbus_message_unref(reply);
517 m = reply = NULL;
518 }
519
520 r = 0;
521
522finish:
523 if (m)
524 dbus_message_unref(m);
525
526 if (reply)
527 dbus_message_unref(reply);
528
529 dbus_error_free(&error);
530
531 return r;
532}
533
5e374895
LP
534typedef struct WaitData {
535 Set *set;
536 bool failed;
537} WaitData;
538
7e4249b9
LP
539static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
540 DBusError error;
5e374895 541 WaitData *d = data;
7e4249b9
LP
542
543 assert(connection);
544 assert(message);
5e374895 545 assert(d);
7e4249b9
LP
546
547 dbus_error_init(&error);
548
549 /* log_debug("Got D-Bus request: %s.%s() on %s", */
550 /* dbus_message_get_interface(message), */
551 /* dbus_message_get_member(message), */
552 /* dbus_message_get_path(message)); */
553
554 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
555 log_error("Warning! D-Bus connection terminated.");
556 dbus_connection_close(connection);
557
558 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
559 uint32_t id;
560 const char *path;
5e374895 561 dbus_bool_t success = true;
7e4249b9
LP
562
563 if (!dbus_message_get_args(message, &error,
564 DBUS_TYPE_UINT32, &id,
565 DBUS_TYPE_OBJECT_PATH, &path,
5e374895 566 DBUS_TYPE_BOOLEAN, &success,
7e4249b9
LP
567 DBUS_TYPE_INVALID))
568 log_error("Failed to parse message: %s", error.message);
569 else {
570 char *p;
571
5e374895 572 if ((p = set_remove(d->set, (char*) path)))
7e4249b9 573 free(p);
5e374895
LP
574
575 if (!success)
576 d->failed = true;
7e4249b9
LP
577 }
578 }
579
580 dbus_error_free(&error);
581 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
582}
583
479ef5d3 584static int enable_wait_for_jobs(DBusConnection *bus) {
7e4249b9
LP
585 DBusError error;
586 DBusMessage *m = NULL, *reply = NULL;
587 int r;
588
589 assert(bus);
7e4249b9
LP
590
591 dbus_error_init(&error);
592
593 dbus_bus_add_match(bus,
594 "type='signal',"
595 "sender='org.freedesktop.systemd1',"
596 "interface='org.freedesktop.systemd1.Manager',"
597 "member='JobRemoved',"
598 "path='/org/freedesktop/systemd1'",
599 &error);
600
601 if (dbus_error_is_set(&error)) {
602 log_error("Failed to add match: %s", error.message);
603 r = -EIO;
604 goto finish;
605 }
606
7e4249b9
LP
607 if (!(m = dbus_message_new_method_call(
608 "org.freedesktop.systemd1",
609 "/org/freedesktop/systemd1",
610 "org.freedesktop.systemd1.Manager",
611 "Subscribe"))) {
612 log_error("Could not allocate message.");
613 r = -ENOMEM;
614 goto finish;
615 }
616
617 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
618 log_error("Failed to issue method call: %s", error.message);
619 r = -EIO;
620 goto finish;
621 }
622
7e4249b9
LP
623 r = 0;
624
625finish:
479ef5d3 626 /* This is slightly dirty, since we don't undo the match registrations. */
7e4249b9
LP
627
628 if (m)
629 dbus_message_unref(m);
630
631 if (reply)
632 dbus_message_unref(reply);
633
634 dbus_error_free(&error);
635
636 return r;
637}
638
479ef5d3
LP
639static int wait_for_jobs(DBusConnection *bus, Set *s) {
640 int r;
5e374895 641 WaitData d;
479ef5d3
LP
642
643 assert(bus);
644 assert(s);
645
5e374895
LP
646 zero(d);
647 d.set = s;
648 d.failed = false;
649
650 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
479ef5d3
LP
651 log_error("Failed to add filter.");
652 r = -ENOMEM;
653 goto finish;
654 }
655
656 while (!set_isempty(s) &&
657 dbus_connection_read_write_dispatch(bus, -1))
658 ;
659
5e374895
LP
660 if (!arg_quiet && d.failed)
661 log_error("Job failed, see logs for details.");
662
663 r = d.failed ? -EIO : 0;
479ef5d3
LP
664
665finish:
666 /* This is slightly dirty, since we don't undo the filter registration. */
667
668 return r;
669}
670
e4b61340
LP
671static int start_unit_one(
672 DBusConnection *bus,
673 const char *method,
674 const char *name,
675 const char *mode,
676 Set *s) {
7e4249b9 677
7e4249b9
LP
678 DBusMessage *m = NULL, *reply = NULL;
679 DBusError error;
680 int r;
7e4249b9 681
e4b61340
LP
682 assert(bus);
683 assert(method);
684 assert(name);
685 assert(mode);
6e905d93 686 assert(arg_no_block || s);
7e4249b9 687
e4b61340 688 dbus_error_init(&error);
479ef5d3 689
7e4249b9
LP
690 if (!(m = dbus_message_new_method_call(
691 "org.freedesktop.systemd1",
692 "/org/freedesktop/systemd1",
693 "org.freedesktop.systemd1.Manager",
e4b61340 694 method))) {
7e4249b9
LP
695 log_error("Could not allocate message.");
696 r = -ENOMEM;
697 goto finish;
698 }
699
700 if (!dbus_message_append_args(m,
e4b61340 701 DBUS_TYPE_STRING, &name,
7e4249b9
LP
702 DBUS_TYPE_STRING, &mode,
703 DBUS_TYPE_INVALID)) {
704 log_error("Could not append arguments to message.");
705 r = -ENOMEM;
706 goto finish;
707 }
708
709 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
e4b61340
LP
710
711 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
712 /* There's always a fallback possible for
713 * legacy actions. */
714 r = 0;
715 goto finish;
716 }
717
7e4249b9
LP
718 log_error("Failed to issue method call: %s", error.message);
719 r = -EIO;
720 goto finish;
721 }
722
6e905d93 723 if (!arg_no_block) {
7e4249b9 724 const char *path;
e4b61340 725 char *p;
7e4249b9
LP
726
727 if (!dbus_message_get_args(reply, &error,
728 DBUS_TYPE_OBJECT_PATH, &path,
729 DBUS_TYPE_INVALID)) {
730 log_error("Failed to parse reply: %s", error.message);
731 r = -EIO;
732 goto finish;
733 }
734
7e4249b9
LP
735 if (!(p = strdup(path))) {
736 log_error("Failed to duplicate path.");
737 r = -ENOMEM;
738 goto finish;
739 }
740
741 if ((r = set_put(s, p)) < 0) {
e4b61340 742 free(p);
7e4249b9
LP
743 log_error("Failed to add path to set.");
744 goto finish;
745 }
e4b61340 746 }
7e4249b9 747
e4b61340 748 r = 1;
7e4249b9
LP
749
750finish:
7e4249b9
LP
751 if (m)
752 dbus_message_unref(m);
753
754 if (reply)
755 dbus_message_unref(reply);
756
757 dbus_error_free(&error);
758
759 return r;
760}
761
514f4ef5
LP
762static enum action verb_to_action(const char *verb) {
763 if (streq(verb, "halt"))
764 return ACTION_HALT;
765 else if (streq(verb, "poweroff"))
766 return ACTION_POWEROFF;
767 else if (streq(verb, "reboot"))
768 return ACTION_REBOOT;
769 else if (streq(verb, "rescue"))
770 return ACTION_RESCUE;
771 else if (streq(verb, "emergency"))
772 return ACTION_EMERGENCY;
773 else if (streq(verb, "default"))
774 return ACTION_DEFAULT;
775 else
776 return ACTION_INVALID;
777}
778
e4b61340
LP
779static int start_unit(DBusConnection *bus, char **args, unsigned n) {
780
781 static const char * const table[_ACTION_MAX] = {
514f4ef5
LP
782 [ACTION_HALT] = SPECIAL_HALT_TARGET,
783 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
784 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
785 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
786 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
787 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
788 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
789 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
790 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
791 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
e4b61340
LP
792 };
793
794 int r;
795 unsigned i;
514f4ef5 796 const char *method, *mode, *one_name;
e4b61340
LP
797 Set *s = NULL;
798
514f4ef5
LP
799 assert(bus);
800
e4b61340
LP
801 if (arg_action == ACTION_SYSTEMCTL) {
802 method =
e4b61340
LP
803 streq(args[0], "stop") ? "StopUnit" :
804 streq(args[0], "reload") ? "ReloadUnit" :
805 streq(args[0], "restart") ? "RestartUnit" :
514f4ef5 806 "StartUnit";
e4b61340
LP
807
808 mode =
514f4ef5
LP
809 (streq(args[0], "isolate") ||
810 streq(args[0], "rescue") ||
811 streq(args[0], "emergency")) ? "isolate" :
812 arg_replace ? "replace" :
813 "fail";
e4b61340 814
514f4ef5 815 one_name = table[verb_to_action(args[0])];
e4b61340 816
e4b61340
LP
817 } else {
818 assert(arg_action < ELEMENTSOF(table));
819 assert(table[arg_action]);
820
821 method = "StartUnit";
514f4ef5
LP
822
823 mode = (arg_action == ACTION_EMERGENCY ||
824 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
825
826 one_name = table[arg_action];
827 }
828
6e905d93 829 if (!arg_no_block) {
514f4ef5
LP
830 if ((r = enable_wait_for_jobs(bus)) < 0) {
831 log_error("Could not watch jobs: %s", strerror(-r));
832 goto finish;
833 }
834
835 if (!(s = set_new(string_hash_func, string_compare_func))) {
836 log_error("Failed to allocate set.");
837 r = -ENOMEM;
838 goto finish;
839 }
e4b61340
LP
840 }
841
842 r = 0;
843
514f4ef5
LP
844 if (one_name) {
845 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
846 goto finish;
847 } else {
e4b61340
LP
848 for (i = 1; i < n; i++)
849 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
850 goto finish;
e4b61340
LP
851 }
852
6e905d93 853 if (!arg_no_block)
514f4ef5
LP
854 r = wait_for_jobs(bus, s);
855
e4b61340
LP
856finish:
857 if (s)
858 set_free_free(s);
859
860 return r;
861}
862
514f4ef5
LP
863static int start_special(DBusConnection *bus, char **args, unsigned n) {
864 assert(bus);
865 assert(args);
866
867 warn_wall(verb_to_action(args[0]));
868
869 return start_unit(bus, args, n);
870}
871
0183528f
LP
872static int check_unit(DBusConnection *bus, char **args, unsigned n) {
873 DBusMessage *m = NULL, *reply = NULL;
874 const char
875 *interface = "org.freedesktop.systemd1.Unit",
876 *property = "ActiveState";
877 int r = -EADDRNOTAVAIL;
878 DBusError error;
879 unsigned i;
880
881 assert(bus);
882 assert(args);
883
884 dbus_error_init(&error);
885
886 for (i = 1; i < n; i++) {
887 const char *path = NULL;
888 const char *state;
889 DBusMessageIter iter, sub;
890
891 if (!(m = dbus_message_new_method_call(
892 "org.freedesktop.systemd1",
893 "/org/freedesktop/systemd1",
894 "org.freedesktop.systemd1.Manager",
895 "GetUnit"))) {
896 log_error("Could not allocate message.");
897 r = -ENOMEM;
898 goto finish;
899 }
900
901 if (!dbus_message_append_args(m,
902 DBUS_TYPE_STRING, &args[i],
903 DBUS_TYPE_INVALID)) {
904 log_error("Could not append arguments to message.");
905 r = -ENOMEM;
906 goto finish;
907 }
908
909 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
910
911 /* Hmm, cannot figure out anything about this unit... */
912 if (!arg_quiet)
913 puts("unknown");
914
915 continue;
916 }
917
918 if (!dbus_message_get_args(reply, &error,
919 DBUS_TYPE_OBJECT_PATH, &path,
920 DBUS_TYPE_INVALID)) {
921 log_error("Failed to parse reply: %s", error.message);
922 r = -EIO;
923 goto finish;
924 }
925
926 dbus_message_unref(m);
927 if (!(m = dbus_message_new_method_call(
928 "org.freedesktop.systemd1",
929 path,
930 "org.freedesktop.DBus.Properties",
931 "Get"))) {
932 log_error("Could not allocate message.");
933 r = -ENOMEM;
934 goto finish;
935 }
936
937 if (!dbus_message_append_args(m,
938 DBUS_TYPE_STRING, &interface,
939 DBUS_TYPE_STRING, &property,
940 DBUS_TYPE_INVALID)) {
941 log_error("Could not append arguments to message.");
942 r = -ENOMEM;
943 goto finish;
944 }
945
946 dbus_message_unref(reply);
947 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
948 log_error("Failed to issue method call: %s", error.message);
949 r = -EIO;
950 goto finish;
951 }
952
953 if (!dbus_message_iter_init(reply, &iter) ||
954 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
955 log_error("Failed to parse reply.");
956 r = -EIO;
957 goto finish;
958 }
959
960 dbus_message_iter_recurse(&iter, &sub);
961
962 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
963 log_error("Failed to parse reply.");
964 r = -EIO;
965 goto finish;
966 }
967
968 dbus_message_iter_get_basic(&sub, &state);
969
970 if (!arg_quiet)
971 puts(state);
972
01b1b079 973 if (streq(state, "active") || startswith(state, "reloading"))
0183528f
LP
974 r = 0;
975
976 dbus_message_unref(m);
977 dbus_message_unref(reply);
978 m = reply = NULL;
979 }
980
981finish:
982 if (m)
983 dbus_message_unref(m);
984
985 if (reply)
986 dbus_message_unref(reply);
987
988 dbus_error_free(&error);
989
990 return r;
48220598
LP
991}
992
993static int print_property(const char *name, DBusMessageIter *iter) {
994 assert(name);
995 assert(iter);
996
997 if (arg_property && !streq(name, arg_property))
998 return 0;
999
1000 switch (dbus_message_iter_get_arg_type(iter)) {
1001
1002 case DBUS_TYPE_STRING: {
1003 const char *s;
1004 dbus_message_iter_get_basic(iter, &s);
1005
1006 if (arg_all || s[0])
1007 printf("%s=%s\n", name, s);
1008
1009 return 0;
1010 }
1011
1012 case DBUS_TYPE_BOOLEAN: {
1013 dbus_bool_t b;
1014 dbus_message_iter_get_basic(iter, &b);
1015 printf("%s=%s\n", name, yes_no(b));
1016
1017 return 0;
1018 }
1019
1020 case DBUS_TYPE_UINT64: {
1021 uint64_t u;
1022 dbus_message_iter_get_basic(iter, &u);
1023
1024 /* Yes, heuristics! But we can change this check
1025 * should it turn out to not be sufficient */
1026
14ad1d14 1027 if (strstr(name, "Timestamp")) {
48220598
LP
1028 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1029
1030 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1031 printf("%s=%s\n", name, strempty(t));
1032 } else
1033 printf("%s=%llu\n", name, (unsigned long long) u);
1034
1035 return 0;
1036 }
1037
1038 case DBUS_TYPE_UINT32: {
1039 uint32_t u;
1040 dbus_message_iter_get_basic(iter, &u);
1041
5bd07073 1042 if (strstr(name, "UMask") || strstr(name, "Mode"))
48220598
LP
1043 printf("%s=%04o\n", name, u);
1044 else
1045 printf("%s=%u\n", name, (unsigned) u);
1046
1047 return 0;
1048 }
1049
1050 case DBUS_TYPE_INT32: {
1051 int32_t i;
1052 dbus_message_iter_get_basic(iter, &i);
1053
1054 printf("%s=%i\n", name, (int) i);
1055 return 0;
1056 }
1057
1058 case DBUS_TYPE_STRUCT: {
1059 DBusMessageIter sub;
1060 dbus_message_iter_recurse(iter, &sub);
1061
ebf57b80 1062 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
48220598
LP
1063 uint32_t u;
1064
1065 dbus_message_iter_get_basic(&sub, &u);
1066
1067 if (u)
1068 printf("%s=%u\n", name, (unsigned) u);
1069 else if (arg_all)
1070 printf("%s=\n", name);
1071
1072 return 0;
ebf57b80 1073 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
48220598
LP
1074 const char *s;
1075
1076 dbus_message_iter_get_basic(&sub, &s);
1077
1078 if (arg_all || s[0])
1079 printf("%s=%s\n", name, s);
1080
1081 return 0;
1082 }
1083
1084 break;
1085 }
1086
1087 case DBUS_TYPE_ARRAY:
1088
1089 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1090 DBusMessageIter sub;
1091 bool space = false;
1092
1093 dbus_message_iter_recurse(iter, &sub);
1094
1095 if (arg_all ||
1096 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1097 printf("%s=", name);
1098
1099 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1100 const char *s;
1101
1102 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1103 dbus_message_iter_get_basic(&sub, &s);
1104 printf("%s%s", space ? " " : "", s);
1105
1106 space = true;
1107 dbus_message_iter_next(&sub);
1108 }
1109
1110 puts("");
1111 }
1112
ebf57b80 1113 return 0;
82c121a4
LP
1114 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1115 DBusMessageIter sub;
1116
1117 dbus_message_iter_recurse(iter, &sub);
1118
1119 if (arg_all ||
1120 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1121 printf("%s=", name);
1122
1123 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1124 uint8_t u;
ebf57b80 1125
82c121a4
LP
1126 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1127 dbus_message_iter_get_basic(&sub, &u);
1128 printf("%02x", u);
1129
1130 dbus_message_iter_next(&sub);
1131 }
1132
1133 puts("");
1134 }
1135
1136 return 0;
ebf57b80
LP
1137 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1138 DBusMessageIter sub, sub2;
1139
1140 dbus_message_iter_recurse(iter, &sub);
1141
1142 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1143 const char *type, *path;
1144
1145 dbus_message_iter_recurse(&sub, &sub2);
1146
1147 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1148 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1149 printf("%s=%s\n", type, path);
1150
1151 dbus_message_iter_next(&sub);
1152 }
1153
707e5e52
LP
1154 return 0;
1155 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1156 DBusMessageIter sub, sub2;
1157
1158 dbus_message_iter_recurse(iter, &sub);
1159
1160 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1161 const char *base;
1162 uint64_t value, next_elapse;
1163
1164 dbus_message_iter_recurse(&sub, &sub2);
1165
1166 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1167 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1168 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0)
1169 printf("%s=%llu\n", base, (unsigned long long) value);
1170
1171 dbus_message_iter_next(&sub);
1172 }
1173
48220598
LP
1174 return 0;
1175 }
1176
1177 break;
1178 }
1179
1180 if (arg_all)
1181 printf("%s=[unprintable]\n", name);
1182
1183 return 0;
1184}
1185
1186static int show_one(DBusConnection *bus, const char *path) {
1187 DBusMessage *m = NULL, *reply = NULL;
1188 const char *interface = "";
1189 int r;
1190 DBusError error;
1191 DBusMessageIter iter, sub, sub2, sub3;
1192
1193 assert(bus);
1194 assert(path);
1195
1196 dbus_error_init(&error);
1197
1198 if (!(m = dbus_message_new_method_call(
1199 "org.freedesktop.systemd1",
1200 path,
1201 "org.freedesktop.DBus.Properties",
1202 "GetAll"))) {
1203 log_error("Could not allocate message.");
1204 r = -ENOMEM;
1205 goto finish;
1206 }
1207
1208 if (!dbus_message_append_args(m,
1209 DBUS_TYPE_STRING, &interface,
1210 DBUS_TYPE_INVALID)) {
1211 log_error("Could not append arguments to message.");
1212 r = -ENOMEM;
1213 goto finish;
1214 }
1215
1216 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1217 log_error("Failed to issue method call: %s", error.message);
1218 r = -EIO;
1219 goto finish;
1220 }
1221
1222 if (!dbus_message_iter_init(reply, &iter) ||
1223 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1224 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1225 log_error("Failed to parse reply.");
1226 r = -EIO;
1227 goto finish;
1228 }
1229
1230 dbus_message_iter_recurse(&iter, &sub);
1231
1232 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1233 const char *name;
1234
1235 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1236 log_error("Failed to parse reply.");
1237 r = -EIO;
1238 goto finish;
1239 }
1240
1241 dbus_message_iter_recurse(&sub, &sub2);
0183528f 1242
48220598
LP
1243 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1244 log_error("Failed to parse reply.");
1245 r = -EIO;
1246 goto finish;
1247 }
1248
1249 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1250 log_error("Failed to parse reply.");
1251 r = -EIO;
1252 goto finish;
1253 }
1254
1255 dbus_message_iter_recurse(&sub2, &sub3);
1256
1257 if (print_property(name, &sub3) < 0) {
1258 log_error("Failed to parse reply.");
1259 r = -EIO;
1260 goto finish;
1261 }
1262
1263 dbus_message_iter_next(&sub);
1264 }
1265
1266 r = 0;
1267
1268finish:
1269 if (m)
1270 dbus_message_unref(m);
1271
1272 if (reply)
1273 dbus_message_unref(reply);
1274
1275 dbus_error_free(&error);
1276
1277 return r;
1278}
1279
1280static int show(DBusConnection *bus, char **args, unsigned n) {
1281 DBusMessage *m = NULL, *reply = NULL;
1282 int r;
1283 DBusError error;
1284 unsigned i;
1285
1286 assert(bus);
1287 assert(args);
1288
1289 dbus_error_init(&error);
1290
1291 if (n <= 1) {
1292 /* If not argument is specified inspect the manager
1293 * itself */
1294
1295 r = show_one(bus, "/org/freedesktop/systemd1");
1296 goto finish;
1297 }
1298
1299 for (i = 1; i < n; i++) {
1300 const char *path = NULL;
1301 uint32_t id;
1302
1303 if (safe_atou32(args[i], &id) < 0) {
1304
1305 if (!(m = dbus_message_new_method_call(
1306 "org.freedesktop.systemd1",
1307 "/org/freedesktop/systemd1",
1308 "org.freedesktop.systemd1.Manager",
e87d1818 1309 "LoadUnit"))) {
48220598
LP
1310 log_error("Could not allocate message.");
1311 r = -ENOMEM;
1312 goto finish;
1313 }
1314
1315 if (!dbus_message_append_args(m,
1316 DBUS_TYPE_STRING, &args[i],
1317 DBUS_TYPE_INVALID)) {
1318 log_error("Could not append arguments to message.");
1319 r = -ENOMEM;
1320 goto finish;
1321 }
1322
1323 } else {
1324
1325 if (!(m = dbus_message_new_method_call(
1326 "org.freedesktop.systemd1",
1327 "/org/freedesktop/systemd1",
1328 "org.freedesktop.systemd1.Manager",
1329 "GetJob"))) {
1330 log_error("Could not allocate message.");
1331 r = -ENOMEM;
1332 goto finish;
1333 }
1334
1335 if (!dbus_message_append_args(m,
1336 DBUS_TYPE_UINT32, &id,
1337 DBUS_TYPE_INVALID)) {
1338 log_error("Could not append arguments to message.");
1339 r = -ENOMEM;
1340 goto finish;
1341 }
1342 }
1343
1344 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1345 log_error("Failed to issue method call: %s", error.message);
1346 r = -EIO;
1347 goto finish;
1348 }
1349
1350 if (!dbus_message_get_args(reply, &error,
1351 DBUS_TYPE_OBJECT_PATH, &path,
1352 DBUS_TYPE_INVALID)) {
1353 log_error("Failed to parse reply: %s", error.message);
1354 r = -EIO;
1355 goto finish;
1356 }
1357
1358 if ((r = show_one(bus, path)) < 0)
1359 goto finish;
1360
1361 dbus_message_unref(m);
1362 dbus_message_unref(reply);
1363 m = reply = NULL;
1364 }
1365
1366 r = 0;
1367
1368finish:
1369 if (m)
1370 dbus_message_unref(m);
1371
1372 if (reply)
1373 dbus_message_unref(reply);
1374
1375 dbus_error_free(&error);
1376
1377 return r;
0183528f
LP
1378}
1379
7e4249b9
LP
1380static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1381 DBusError error;
1382 DBusMessage *m = NULL, *reply = NULL;
1383
1384 assert(connection);
1385 assert(message);
1386
1387 dbus_error_init(&error);
1388
1389 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1390 /* dbus_message_get_interface(message), */
1391 /* dbus_message_get_member(message), */
1392 /* dbus_message_get_path(message)); */
1393
1394 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1395 log_error("Warning! D-Bus connection terminated.");
1396 dbus_connection_close(connection);
1397
1398 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1399 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1400 const char *id, *path;
1401
1402 if (!dbus_message_get_args(message, &error,
1403 DBUS_TYPE_STRING, &id,
1404 DBUS_TYPE_OBJECT_PATH, &path,
1405 DBUS_TYPE_INVALID))
1406 log_error("Failed to parse message: %s", error.message);
1407 else if (streq(dbus_message_get_member(message), "UnitNew"))
1408 printf("Unit %s added.\n", id);
1409 else
1410 printf("Unit %s removed.\n", id);
1411
1412 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1413 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1414 uint32_t id;
1415 const char *path;
1416
1417 if (!dbus_message_get_args(message, &error,
1418 DBUS_TYPE_UINT32, &id,
1419 DBUS_TYPE_OBJECT_PATH, &path,
1420 DBUS_TYPE_INVALID))
1421 log_error("Failed to parse message: %s", error.message);
1422 else if (streq(dbus_message_get_member(message), "JobNew"))
1423 printf("Job %u added.\n", id);
1424 else
1425 printf("Job %u removed.\n", id);
1426
1427
1428 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1429 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1430
1431 const char *path, *interface, *property = "Id";
1432 DBusMessageIter iter, sub;
1433
1434 path = dbus_message_get_path(message);
1435 interface = dbus_message_get_interface(message);
1436
1437 if (!(m = dbus_message_new_method_call(
1438 "org.freedesktop.systemd1",
1439 path,
1440 "org.freedesktop.DBus.Properties",
1441 "Get"))) {
1442 log_error("Could not allocate message.");
1443 goto oom;
1444 }
1445
1446 if (!dbus_message_append_args(m,
1447 DBUS_TYPE_STRING, &interface,
1448 DBUS_TYPE_STRING, &property,
1449 DBUS_TYPE_INVALID)) {
1450 log_error("Could not append arguments to message.");
1451 goto finish;
1452 }
1453
1454 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1455 log_error("Failed to issue method call: %s", error.message);
1456 goto finish;
1457 }
1458
1459 if (!dbus_message_iter_init(reply, &iter) ||
1460 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1461 log_error("Failed to parse reply.");
1462 goto finish;
1463 }
1464
1465 dbus_message_iter_recurse(&iter, &sub);
1466
1467 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1468 const char *id;
1469
1470 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1471 log_error("Failed to parse reply.");
1472 goto finish;
1473 }
1474
1475 dbus_message_iter_get_basic(&sub, &id);
1476 printf("Unit %s changed.\n", id);
1477 } else {
1478 uint32_t id;
1479
1480 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1481 log_error("Failed to parse reply.");
1482 goto finish;
1483 }
1484
1485 dbus_message_iter_get_basic(&sub, &id);
1486 printf("Job %u changed.\n", id);
1487 }
1488 }
1489
1490finish:
1491 if (m)
1492 dbus_message_unref(m);
1493
1494 if (reply)
1495 dbus_message_unref(reply);
1496
1497 dbus_error_free(&error);
1498 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1499
1500oom:
1501 if (m)
1502 dbus_message_unref(m);
1503
1504 if (reply)
1505 dbus_message_unref(reply);
1506
1507 dbus_error_free(&error);
1508 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1509}
1510
1511static int monitor(DBusConnection *bus, char **args, unsigned n) {
1512 DBusMessage *m = NULL, *reply = NULL;
1513 DBusError error;
1514 int r;
1515
1516 dbus_error_init(&error);
1517
1518 dbus_bus_add_match(bus,
1519 "type='signal',"
1520 "sender='org.freedesktop.systemd1',"
1521 "interface='org.freedesktop.systemd1.Manager',"
1522 "path='/org/freedesktop/systemd1'",
1523 &error);
1524
1525 if (dbus_error_is_set(&error)) {
1526 log_error("Failed to add match: %s", error.message);
1527 r = -EIO;
1528 goto finish;
1529 }
1530
1531 dbus_bus_add_match(bus,
1532 "type='signal',"
1533 "sender='org.freedesktop.systemd1',"
1534 "interface='org.freedesktop.systemd1.Unit',"
1535 "member='Changed'",
1536 &error);
1537
1538 if (dbus_error_is_set(&error)) {
1539 log_error("Failed to add match: %s", error.message);
1540 r = -EIO;
1541 goto finish;
1542 }
1543
1544 dbus_bus_add_match(bus,
1545 "type='signal',"
1546 "sender='org.freedesktop.systemd1',"
1547 "interface='org.freedesktop.systemd1.Job',"
1548 "member='Changed'",
1549 &error);
1550
1551 if (dbus_error_is_set(&error)) {
1552 log_error("Failed to add match: %s", error.message);
1553 r = -EIO;
1554 goto finish;
1555 }
1556
1557 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1558 log_error("Failed to add filter.");
1559 r = -ENOMEM;
1560 goto finish;
1561 }
1562
1563 if (!(m = dbus_message_new_method_call(
1564 "org.freedesktop.systemd1",
1565 "/org/freedesktop/systemd1",
1566 "org.freedesktop.systemd1.Manager",
1567 "Subscribe"))) {
1568 log_error("Could not allocate message.");
1569 r = -ENOMEM;
1570 goto finish;
1571 }
1572
1573 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1574 log_error("Failed to issue method call: %s", error.message);
1575 r = -EIO;
1576 goto finish;
1577 }
1578
1579 while (dbus_connection_read_write_dispatch(bus, -1))
1580 ;
1581
1582 r = 0;
1583
1584finish:
1585
1586 /* This is slightly dirty, since we don't undo the filter or the matches. */
1587
1588 if (m)
1589 dbus_message_unref(m);
1590
1591 if (reply)
1592 dbus_message_unref(reply);
1593
1594 dbus_error_free(&error);
1595
1596 return r;
1597}
1598
1599static int dump(DBusConnection *bus, char **args, unsigned n) {
1600 DBusMessage *m = NULL, *reply = NULL;
1601 DBusError error;
1602 int r;
1603 const char *text;
1604
1605 dbus_error_init(&error);
1606
1607 if (!(m = dbus_message_new_method_call(
1608 "org.freedesktop.systemd1",
1609 "/org/freedesktop/systemd1",
1610 "org.freedesktop.systemd1.Manager",
1611 "Dump"))) {
1612 log_error("Could not allocate message.");
1613 return -ENOMEM;
1614 }
1615
1616 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1617 log_error("Failed to issue method call: %s", error.message);
1618 r = -EIO;
1619 goto finish;
1620 }
1621
1622 if (!dbus_message_get_args(reply, &error,
1623 DBUS_TYPE_STRING, &text,
1624 DBUS_TYPE_INVALID)) {
1625 log_error("Failed to parse reply: %s", error.message);
1626 r = -EIO;
1627 goto finish;
1628 }
1629
1630 fputs(text, stdout);
1631
1632 r = 0;
1633
1634finish:
1635 if (m)
1636 dbus_message_unref(m);
1637
1638 if (reply)
1639 dbus_message_unref(reply);
1640
1641 dbus_error_free(&error);
1642
1643 return r;
1644}
1645
1646static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1647 DBusMessage *m = NULL, *reply = NULL;
1648 DBusError error;
1649 int r;
1650 const char *name = "", *path, *id;
1651 dbus_bool_t cleanup = FALSE;
1652 DBusMessageIter iter, sub;
1653 const char
1654 *interface = "org.freedesktop.systemd1.Unit",
1655 *property = "Id";
1656
1657 dbus_error_init(&error);
1658
1659 if (!(m = dbus_message_new_method_call(
1660 "org.freedesktop.systemd1",
1661 "/org/freedesktop/systemd1",
1662 "org.freedesktop.systemd1.Manager",
1663 "CreateSnapshot"))) {
1664 log_error("Could not allocate message.");
1665 return -ENOMEM;
1666 }
1667
1668 if (n > 1)
1669 name = args[1];
1670
1671 if (!dbus_message_append_args(m,
1672 DBUS_TYPE_STRING, &name,
1673 DBUS_TYPE_BOOLEAN, &cleanup,
1674 DBUS_TYPE_INVALID)) {
1675 log_error("Could not append arguments to message.");
1676 r = -ENOMEM;
1677 goto finish;
1678 }
1679
1680 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1681 log_error("Failed to issue method call: %s", error.message);
1682 r = -EIO;
1683 goto finish;
1684 }
1685
1686 if (!dbus_message_get_args(reply, &error,
1687 DBUS_TYPE_OBJECT_PATH, &path,
1688 DBUS_TYPE_INVALID)) {
1689 log_error("Failed to parse reply: %s", error.message);
1690 r = -EIO;
1691 goto finish;
1692 }
1693
1694 dbus_message_unref(m);
1695 if (!(m = dbus_message_new_method_call(
1696 "org.freedesktop.systemd1",
1697 path,
1698 "org.freedesktop.DBus.Properties",
1699 "Get"))) {
1700 log_error("Could not allocate message.");
1701 return -ENOMEM;
1702 }
1703
1704 if (!dbus_message_append_args(m,
1705 DBUS_TYPE_STRING, &interface,
1706 DBUS_TYPE_STRING, &property,
1707 DBUS_TYPE_INVALID)) {
1708 log_error("Could not append arguments to message.");
1709 r = -ENOMEM;
1710 goto finish;
1711 }
1712
1713 dbus_message_unref(reply);
1714 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1715 log_error("Failed to issue method call: %s", error.message);
1716 r = -EIO;
1717 goto finish;
1718 }
1719
1720 if (!dbus_message_iter_init(reply, &iter) ||
1721 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1722 log_error("Failed to parse reply.");
1723 r = -EIO;
1724 goto finish;
1725 }
1726
1727 dbus_message_iter_recurse(&iter, &sub);
1728
1729 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1730 log_error("Failed to parse reply.");
1731 r = -EIO;
1732 goto finish;
1733 }
1734
1735 dbus_message_iter_get_basic(&sub, &id);
0183528f
LP
1736
1737 if (!arg_quiet)
1738 puts(id);
7e4249b9
LP
1739 r = 0;
1740
1741finish:
1742 if (m)
1743 dbus_message_unref(m);
1744
1745 if (reply)
1746 dbus_message_unref(reply);
1747
1748 dbus_error_free(&error);
1749
1750 return r;
1751}
1752
6759e7a7
LP
1753static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
1754 DBusMessage *m = NULL, *reply = NULL;
1755 int r;
1756 DBusError error;
1757 unsigned i;
1758
1759 assert(bus);
1760 assert(args);
1761
1762 dbus_error_init(&error);
1763
1764 for (i = 1; i < n; i++) {
1765 const char *path = NULL;
1766
1767 if (!(m = dbus_message_new_method_call(
1768 "org.freedesktop.systemd1",
1769 "/org/freedesktop/systemd1",
1770 "org.freedesktop.systemd1.Manager",
1771 "GetUnit"))) {
1772 log_error("Could not allocate message.");
1773 r = -ENOMEM;
1774 goto finish;
1775 }
1776
1777 if (!dbus_message_append_args(m,
1778 DBUS_TYPE_STRING, &args[i],
1779 DBUS_TYPE_INVALID)) {
1780 log_error("Could not append arguments to message.");
1781 r = -ENOMEM;
1782 goto finish;
1783 }
1784
1785 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1786 log_error("Failed to issue method call: %s", error.message);
1787 r = -EIO;
1788 goto finish;
1789 }
1790
1791 if (!dbus_message_get_args(reply, &error,
1792 DBUS_TYPE_OBJECT_PATH, &path,
1793 DBUS_TYPE_INVALID)) {
1794 log_error("Failed to parse reply: %s", error.message);
1795 r = -EIO;
1796 goto finish;
1797 }
1798
1799 dbus_message_unref(m);
1800 if (!(m = dbus_message_new_method_call(
1801 "org.freedesktop.systemd1",
1802 path,
1803 "org.freedesktop.systemd1.Snapshot",
1804 "Remove"))) {
1805 log_error("Could not allocate message.");
1806 r = -ENOMEM;
1807 goto finish;
1808 }
1809
1810 dbus_message_unref(reply);
1811 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1812 log_error("Failed to issue method call: %s", error.message);
1813 r = -EIO;
1814 goto finish;
1815 }
1816
1817 dbus_message_unref(m);
1818 dbus_message_unref(reply);
1819 m = reply = NULL;
1820 }
1821
1822 r = 0;
1823
1824finish:
1825 if (m)
1826 dbus_message_unref(m);
1827
1828 if (reply)
1829 dbus_message_unref(reply);
1830
1831 dbus_error_free(&error);
1832
1833 return r;
1834}
1835
7e4249b9
LP
1836static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1837 DBusMessage *m = NULL, *reply = NULL;
1838 DBusError error;
1839 int r;
1840 const char *method;
1841
1842 dbus_error_init(&error);
1843
e4b61340
LP
1844 if (arg_action == ACTION_RELOAD)
1845 method = "Reload";
1846 else if (arg_action == ACTION_REEXEC)
1847 method = "Reexecute";
1848 else {
1849 assert(arg_action == ACTION_SYSTEMCTL);
1850
1851 method =
1852 streq(args[0], "clear-jobs") ? "ClearJobs" :
1853 streq(args[0], "daemon-reload") ? "Reload" :
1854 streq(args[0], "daemon-reexec") ? "Reexecute" :
1855 "Exit";
1856 }
7e4249b9
LP
1857
1858 if (!(m = dbus_message_new_method_call(
1859 "org.freedesktop.systemd1",
1860 "/org/freedesktop/systemd1",
1861 "org.freedesktop.systemd1.Manager",
1862 method))) {
1863 log_error("Could not allocate message.");
1864 return -ENOMEM;
1865 }
1866
1867 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
e4b61340
LP
1868
1869 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1870 /* There's always a fallback possible for
1871 * legacy actions. */
1872 r = 0;
1873 goto finish;
1874 }
1875
7e4249b9
LP
1876 log_error("Failed to issue method call: %s", error.message);
1877 r = -EIO;
1878 goto finish;
1879 }
1880
e4b61340 1881 r = 1;
7e4249b9
LP
1882
1883finish:
1884 if (m)
1885 dbus_message_unref(m);
1886
1887 if (reply)
1888 dbus_message_unref(reply);
1889
1890 dbus_error_free(&error);
1891
1892 return r;
1893}
1894
1895static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1896 DBusMessage *m = NULL, *reply = NULL;
1897 DBusError error;
1898 DBusMessageIter iter, sub, sub2;
1899 int r;
1900 const char
1901 *interface = "org.freedesktop.systemd1.Manager",
1902 *property = "Environment";
1903
1904 dbus_error_init(&error);
1905
1906 if (!(m = dbus_message_new_method_call(
1907 "org.freedesktop.systemd1",
1908 "/org/freedesktop/systemd1",
1909 "org.freedesktop.DBus.Properties",
1910 "Get"))) {
1911 log_error("Could not allocate message.");
1912 return -ENOMEM;
1913 }
1914
1915 if (!dbus_message_append_args(m,
1916 DBUS_TYPE_STRING, &interface,
1917 DBUS_TYPE_STRING, &property,
1918 DBUS_TYPE_INVALID)) {
1919 log_error("Could not append arguments to message.");
1920 r = -ENOMEM;
1921 goto finish;
1922 }
1923
1924 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1925 log_error("Failed to issue method call: %s", error.message);
1926 r = -EIO;
1927 goto finish;
1928 }
1929
1930 if (!dbus_message_iter_init(reply, &iter) ||
1931 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1932 log_error("Failed to parse reply.");
1933 r = -EIO;
1934 goto finish;
1935 }
1936
1937 dbus_message_iter_recurse(&iter, &sub);
1938
1939 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1940 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1941 log_error("Failed to parse reply.");
1942 r = -EIO;
1943 goto finish;
1944 }
1945
1946 dbus_message_iter_recurse(&sub, &sub2);
1947
1948 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1949 const char *text;
1950
1951 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1952 log_error("Failed to parse reply.");
1953 r = -EIO;
1954 goto finish;
1955 }
1956
1957 dbus_message_iter_get_basic(&sub2, &text);
1958 printf("%s\n", text);
1959
1960 dbus_message_iter_next(&sub2);
1961 }
1962
1963 r = 0;
1964
1965finish:
1966 if (m)
1967 dbus_message_unref(m);
1968
1969 if (reply)
1970 dbus_message_unref(reply);
1971
1972 dbus_error_free(&error);
1973
1974 return r;
1975}
1976
1977static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1978 DBusMessage *m = NULL, *reply = NULL;
1979 DBusError error;
1980 int r;
1981 const char *method;
1982 DBusMessageIter iter, sub;
1983 unsigned i;
1984
1985 dbus_error_init(&error);
1986
1987 method = streq(args[0], "set-environment")
1988 ? "SetEnvironment"
1989 : "UnsetEnvironment";
1990
1991 if (!(m = dbus_message_new_method_call(
1992 "org.freedesktop.systemd1",
1993 "/org/freedesktop/systemd1",
1994 "org.freedesktop.systemd1.Manager",
1995 method))) {
1996
1997 log_error("Could not allocate message.");
1998 return -ENOMEM;
1999 }
2000
2001 dbus_message_iter_init_append(m, &iter);
2002
2003 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2004 log_error("Could not append arguments to message.");
2005 r = -ENOMEM;
2006 goto finish;
2007 }
2008
2009 for (i = 1; i < n; i++)
2010 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2011 log_error("Could not append arguments to message.");
2012 r = -ENOMEM;
2013 goto finish;
2014 }
2015
2016 if (!dbus_message_iter_close_container(&iter, &sub)) {
2017 log_error("Could not append arguments to message.");
2018 r = -ENOMEM;
2019 goto finish;
2020 }
2021
2022 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2023 log_error("Failed to issue method call: %s", error.message);
2024 r = -EIO;
2025 goto finish;
2026 }
2027
2028 r = 0;
2029
2030finish:
2031 if (m)
2032 dbus_message_unref(m);
2033
2034 if (reply)
2035 dbus_message_unref(reply);
2036
2037 dbus_error_free(&error);
2038
2039 return r;
2040}
2041
e4b61340 2042static int systemctl_help(void) {
7e4249b9 2043
2e33c433 2044 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
5ec7ed4e 2045 "Send control commands to the systemd manager.\n\n"
48220598
LP
2046 " -h --help Show this help\n"
2047 " -t --type=TYPE List only units of a particular type\n"
2048 " -p --property=NAME Show only property by this name\n"
2049 " -a --all Show all units/properties, including dead/empty ones\n"
2050 " --replace When installing a new job, replace existing conflicting ones\n"
2051 " --system Connect to system bus\n"
2052 " --session Connect to session bus\n"
2053 " -q --quiet Suppress output\n"
2054 " --no-block Do not wait until operation finished\n"
2055 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
7e4249b9
LP
2056 "Commands:\n"
2057 " list-units List units\n"
7e4249b9
LP
2058 " start [NAME...] Start one or more units\n"
2059 " stop [NAME...] Stop one or more units\n"
2060 " restart [NAME...] Restart one or more units\n"
2061 " reload [NAME...] Reload one or more units\n"
2062 " isolate [NAME] Start one unit and stop all others\n"
0183528f 2063 " check [NAME...] Check whether any of the passed units are active\n"
48220598
LP
2064 " show [NAME...|JOB...] Show information about one or more units\n"
2065 " load [NAME...] Load one or more units\n"
2066 " list-jobs List jobs\n"
2067 " cancel [JOB...] Cancel one or more jobs\n"
2068 " clear-jobs Cancel all jobs\n"
7e4249b9
LP
2069 " monitor Monitor unit/job changes\n"
2070 " dump Dump server status\n"
2071 " snapshot [NAME] Create a snapshot\n"
6759e7a7 2072 " delete [NAME...] Remove one or more snapshots\n"
5ec7ed4e
LP
2073 " daemon-reload Reload systemd manager configuration\n"
2074 " daemon-reexec Reexecute systemd manager\n"
2075 " daemon-exit Ask the systemd manager to quit\n"
7e4249b9
LP
2076 " show-environment Dump environment\n"
2077 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
514f4ef5
LP
2078 " unset-environment [NAME...] Unset one or more environment variables\n"
2079 " halt Shut down and halt the system\n"
2e33c433 2080 " poweroff Shut down and power-off the system\n"
514f4ef5 2081 " reboot Shut down and reboot the system\n"
514f4ef5
LP
2082 " default Enter default mode\n"
2083 " rescue Enter rescue mode\n"
2084 " emergency Enter emergency mode\n",
5b6319dc 2085 program_invocation_short_name);
7e4249b9
LP
2086
2087 return 0;
2088}
2089
e4b61340
LP
2090static int halt_help(void) {
2091
2e33c433 2092 printf("%s [OPTIONS...]\n\n"
e4b61340
LP
2093 "%s the system.\n\n"
2094 " --help Show this help\n"
2095 " --halt Halt the machine\n"
2096 " -p --poweroff Switch off the machine\n"
2097 " --reboot Reboot the machine\n"
2e33c433
LP
2098 " -f --force Force immediate halt/power-off/reboot\n"
2099 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
e4b61340 2100 " -d --no-wtmp Don't write wtmp record\n"
2e33c433
LP
2101 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2102 " --no-wall Don't send wall message before halt/power-off/reboot\n",
e4b61340
LP
2103 program_invocation_short_name,
2104 arg_action == ACTION_REBOOT ? "Reboot" :
2105 arg_action == ACTION_POWEROFF ? "Power off" :
2106 "Halt");
2107
2108 return 0;
2109}
2110
2111static int shutdown_help(void) {
2112
2e33c433 2113 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
e4b61340
LP
2114 "Shut down the system.\n\n"
2115 " --help Show this help\n"
2116 " -H --halt Halt the machine\n"
2117 " -P --poweroff Power-off the machine\n"
2118 " -r --reboot Reboot the machine\n"
2119 " -h Equivalent to --poweroff, overriden by --halt\n"
2e33c433
LP
2120 " -k Don't halt/power-off/reboot, just send warnings\n"
2121 " --no-wall Don't send wall message before halt/power-off/reboot\n",
e4b61340
LP
2122 program_invocation_short_name);
2123
2124 return 0;
2125}
2126
2127static int telinit_help(void) {
2128
2e33c433 2129 printf("%s [OPTIONS...] {COMMAND}\n\n"
514f4ef5
LP
2130 "Send control commands to the init daemon.\n\n"
2131 " --help Show this help\n"
2e33c433 2132 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
e4b61340
LP
2133 "Commands:\n"
2134 " 0 Power-off the machine\n"
2135 " 6 Reboot the machine\n"
514f4ef5
LP
2136 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2137 " 1, s, S Enter rescue mode\n"
2138 " q, Q Reload init daemon configuration\n"
2139 " u, U Reexecute init daemon\n",
e4b61340
LP
2140 program_invocation_short_name);
2141
2142 return 0;
2143}
2144
2145static int runlevel_help(void) {
2146
2e33c433 2147 printf("%s [OPTIONS...]\n\n"
e4b61340
LP
2148 "Prints the previous and current runlevel of the init system.\n\n"
2149 " --help Show this help\n",
2150 program_invocation_short_name);
2151
2152 return 0;
2153}
2154
2155static int systemctl_parse_argv(int argc, char *argv[]) {
7e4249b9
LP
2156
2157 enum {
2158 ARG_REPLACE = 0x100,
2159 ARG_SESSION,
2160 ARG_SYSTEM,
6e905d93 2161 ARG_NO_BLOCK,
514f4ef5 2162 ARG_NO_WALL
7e4249b9
LP
2163 };
2164
2165 static const struct option options[] = {
6e905d93
LP
2166 { "help", no_argument, NULL, 'h' },
2167 { "type", required_argument, NULL, 't' },
48220598 2168 { "property", required_argument, NULL, 'p' },
6e905d93
LP
2169 { "all", no_argument, NULL, 'a' },
2170 { "replace", no_argument, NULL, ARG_REPLACE },
2171 { "session", no_argument, NULL, ARG_SESSION },
2172 { "system", no_argument, NULL, ARG_SYSTEM },
2173 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2174 { "no-wall", no_argument, NULL, ARG_NO_WALL },
0183528f 2175 { "quiet", no_argument, NULL, 'q' },
6e905d93 2176 { NULL, 0, NULL, 0 }
7e4249b9
LP
2177 };
2178
2179 int c;
2180
e4b61340 2181 assert(argc >= 0);
7e4249b9
LP
2182 assert(argv);
2183
48220598 2184 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
7e4249b9
LP
2185
2186 switch (c) {
2187
2188 case 'h':
e4b61340 2189 systemctl_help();
7e4249b9
LP
2190 return 0;
2191
2192 case 't':
2193 arg_type = optarg;
2194 break;
2195
48220598
LP
2196 case 'p':
2197 arg_property = optarg;
2198
2199 /* If the user asked for a particular
2200 * property, show it to him, even if it is
2201 * empty. */
2202 arg_all = true;
2203 break;
2204
7e4249b9
LP
2205 case 'a':
2206 arg_all = true;
2207 break;
2208
2209 case ARG_REPLACE:
2210 arg_replace = true;
2211 break;
2212
2213 case ARG_SESSION:
2214 arg_session = true;
2215 break;
2216
2217 case ARG_SYSTEM:
2218 arg_session = false;
2219 break;
2220
6e905d93
LP
2221 case ARG_NO_BLOCK:
2222 arg_no_block = true;
7e4249b9
LP
2223 break;
2224
514f4ef5
LP
2225 case ARG_NO_WALL:
2226 arg_no_wall = true;
2227 break;
2228
0183528f
LP
2229 case 'q':
2230 arg_quiet = true;
2231 break;
2232
7e4249b9
LP
2233 case '?':
2234 return -EINVAL;
2235
2236 default:
2237 log_error("Unknown option code %c", c);
2238 return -EINVAL;
2239 }
2240 }
2241
2242 return 1;
2243}
2244
e4b61340
LP
2245static int halt_parse_argv(int argc, char *argv[]) {
2246
2247 enum {
2248 ARG_HELP = 0x100,
2249 ARG_HALT,
514f4ef5
LP
2250 ARG_REBOOT,
2251 ARG_NO_WALL
e4b61340
LP
2252 };
2253
2254 static const struct option options[] = {
2255 { "help", no_argument, NULL, ARG_HELP },
2256 { "halt", no_argument, NULL, ARG_HALT },
2257 { "poweroff", no_argument, NULL, 'p' },
2258 { "reboot", no_argument, NULL, ARG_REBOOT },
2259 { "force", no_argument, NULL, 'f' },
2260 { "wtmp-only", no_argument, NULL, 'w' },
2261 { "no-wtmp", no_argument, NULL, 'd' },
2262 { "no-sync", no_argument, NULL, 'n' },
514f4ef5 2263 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
2264 { NULL, 0, NULL, 0 }
2265 };
2266
2267 int c, runlevel;
2268
2269 assert(argc >= 0);
2270 assert(argv);
2271
2272 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2273 if (runlevel == '0' || runlevel == '6')
2274 arg_immediate = true;
2275
2276 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2277 switch (c) {
2278
2279 case ARG_HELP:
2280 halt_help();
2281 return 0;
2282
2283 case ARG_HALT:
2284 arg_action = ACTION_HALT;
2285 break;
2286
2287 case 'p':
2288 arg_action = ACTION_POWEROFF;
2289 break;
2290
2291 case ARG_REBOOT:
2292 arg_action = ACTION_REBOOT;
2293 break;
2294
2295 case 'f':
2296 arg_immediate = true;
2297 break;
2298
2299 case 'w':
2300 arg_dry = true;
2301 break;
2302
2303 case 'd':
2304 arg_no_wtmp = true;
2305 break;
2306
2307 case 'n':
2308 arg_no_sync = true;
2309 break;
2310
514f4ef5
LP
2311 case ARG_NO_WALL:
2312 arg_no_wall = true;
2313 break;
2314
e4b61340
LP
2315 case 'i':
2316 case 'h':
2317 /* Compatibility nops */
2318 break;
2319
2320 case '?':
2321 return -EINVAL;
2322
2323 default:
2324 log_error("Unknown option code %c", c);
2325 return -EINVAL;
2326 }
2327 }
2328
2329 if (optind < argc) {
2330 log_error("Too many arguments.");
2331 return -EINVAL;
2332 }
2333
2334 return 1;
2335}
2336
2337static int shutdown_parse_argv(int argc, char *argv[]) {
2338
2339 enum {
2340 ARG_HELP = 0x100,
514f4ef5 2341 ARG_NO_WALL
e4b61340
LP
2342 };
2343
2344 static const struct option options[] = {
2345 { "help", no_argument, NULL, ARG_HELP },
2346 { "halt", no_argument, NULL, 'H' },
2347 { "poweroff", no_argument, NULL, 'P' },
2348 { "reboot", no_argument, NULL, 'r' },
514f4ef5 2349 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
2350 { NULL, 0, NULL, 0 }
2351 };
2352
2353 int c;
2354
2355 assert(argc >= 0);
2356 assert(argv);
2357
2358 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2359 switch (c) {
2360
2361 case ARG_HELP:
2362 shutdown_help();
2363 return 0;
2364
2365 case 'H':
2366 arg_action = ACTION_HALT;
2367 break;
2368
2369 case 'P':
2370 arg_action = ACTION_POWEROFF;
2371 break;
2372
2373 case 'r':
2374 arg_action = ACTION_REBOOT;
2375 break;
2376
2377 case 'h':
2378 if (arg_action != ACTION_HALT)
2379 arg_action = ACTION_POWEROFF;
2380 break;
2381
2382 case 'k':
2383 arg_dry = true;
2384 break;
2385
514f4ef5
LP
2386 case ARG_NO_WALL:
2387 arg_no_wall = true;
2388 break;
2389
e4b61340
LP
2390 case 't':
2391 case 'a':
2392 /* Compatibility nops */
2393 break;
2394
2395 case '?':
2396 return -EINVAL;
2397
2398 default:
2399 log_error("Unknown option code %c", c);
2400 return -EINVAL;
2401 }
2402 }
2403
4545812f
LP
2404 if (argc > optind && !streq(argv[optind], "now"))
2405 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
442b9094 2406
e4b61340
LP
2407 /* We ignore the time argument */
2408 if (argc > optind + 1)
2409 arg_wall = argv + optind + 1;
2410
2411 optind = argc;
2412
2413 return 1;
e4b61340
LP
2414}
2415
2416static int telinit_parse_argv(int argc, char *argv[]) {
2417
2418 enum {
2419 ARG_HELP = 0x100,
514f4ef5 2420 ARG_NO_WALL
e4b61340
LP
2421 };
2422
2423 static const struct option options[] = {
2424 { "help", no_argument, NULL, ARG_HELP },
514f4ef5 2425 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
2426 { NULL, 0, NULL, 0 }
2427 };
2428
2429 static const struct {
2430 char from;
2431 enum action to;
2432 } table[] = {
2433 { '0', ACTION_POWEROFF },
2434 { '6', ACTION_REBOOT },
ef2f1067 2435 { '1', ACTION_RESCUE },
e4b61340
LP
2436 { '2', ACTION_RUNLEVEL2 },
2437 { '3', ACTION_RUNLEVEL3 },
2438 { '4', ACTION_RUNLEVEL4 },
2439 { '5', ACTION_RUNLEVEL5 },
2440 { 's', ACTION_RESCUE },
2441 { 'S', ACTION_RESCUE },
2442 { 'q', ACTION_RELOAD },
2443 { 'Q', ACTION_RELOAD },
2444 { 'u', ACTION_REEXEC },
2445 { 'U', ACTION_REEXEC }
2446 };
2447
2448 unsigned i;
2449 int c;
2450
2451 assert(argc >= 0);
2452 assert(argv);
2453
2454 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2455 switch (c) {
2456
2457 case ARG_HELP:
2458 telinit_help();
2459 return 0;
2460
514f4ef5
LP
2461 case ARG_NO_WALL:
2462 arg_no_wall = true;
2463 break;
2464
e4b61340
LP
2465 case '?':
2466 return -EINVAL;
2467
2468 default:
2469 log_error("Unknown option code %c", c);
2470 return -EINVAL;
2471 }
2472 }
2473
2474 if (optind >= argc) {
2f02ce40 2475 telinit_help();
e4b61340
LP
2476 return -EINVAL;
2477 }
2478
2479 if (optind + 1 < argc) {
2480 log_error("Too many arguments.");
2481 return -EINVAL;
2482 }
2483
2484 if (strlen(argv[optind]) != 1) {
2485 log_error("Expected single character argument.");
2486 return -EINVAL;
2487 }
2488
2489 for (i = 0; i < ELEMENTSOF(table); i++)
2490 if (table[i].from == argv[optind][0])
2491 break;
2492
2493 if (i >= ELEMENTSOF(table)) {
2494 log_error("Unknown command %s.", argv[optind]);
2495 return -EINVAL;
2496 }
2497
2498 arg_action = table[i].to;
2499
2500 optind ++;
2501
2502 return 1;
2503}
2504
2505static int runlevel_parse_argv(int argc, char *argv[]) {
2506
2507 enum {
2508 ARG_HELP = 0x100,
2509 };
2510
2511 static const struct option options[] = {
2512 { "help", no_argument, NULL, ARG_HELP },
2513 { NULL, 0, NULL, 0 }
2514 };
2515
2516 int c;
2517
2518 assert(argc >= 0);
2519 assert(argv);
2520
2521 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2522 switch (c) {
2523
2524 case ARG_HELP:
2525 runlevel_help();
2526 return 0;
2527
2528 case '?':
2529 return -EINVAL;
2530
2531 default:
2532 log_error("Unknown option code %c", c);
2533 return -EINVAL;
2534 }
2535 }
2536
2537 if (optind < argc) {
2538 log_error("Too many arguments.");
2539 return -EINVAL;
2540 }
2541
2542 return 1;
2543}
2544
2545static int parse_argv(int argc, char *argv[]) {
2546 assert(argc >= 0);
2547 assert(argv);
2548
2549 if (program_invocation_short_name) {
2550
2551 if (strstr(program_invocation_short_name, "halt")) {
2552 arg_action = ACTION_HALT;
2553 return halt_parse_argv(argc, argv);
2554 } else if (strstr(program_invocation_short_name, "poweroff")) {
2555 arg_action = ACTION_POWEROFF;
2556 return halt_parse_argv(argc, argv);
2557 } else if (strstr(program_invocation_short_name, "reboot")) {
2558 arg_action = ACTION_REBOOT;
2559 return halt_parse_argv(argc, argv);
2560 } else if (strstr(program_invocation_short_name, "shutdown")) {
2561 arg_action = ACTION_POWEROFF;
2562 return shutdown_parse_argv(argc, argv);
2563 } else if (strstr(program_invocation_short_name, "init")) {
2564 arg_action = ACTION_INVALID;
2565 return telinit_parse_argv(argc, argv);
2566 } else if (strstr(program_invocation_short_name, "runlevel")) {
2567 arg_action = ACTION_RUNLEVEL;
2568 return runlevel_parse_argv(argc, argv);
2569 }
2570 }
2571
2572 arg_action = ACTION_SYSTEMCTL;
2573 return systemctl_parse_argv(argc, argv);
2574}
2575
d55ae9e6 2576static int action_to_runlevel(void) {
eb22ac37
LP
2577
2578 static const char table[_ACTION_MAX] = {
2579 [ACTION_HALT] = '0',
2580 [ACTION_POWEROFF] = '0',
2581 [ACTION_REBOOT] = '6',
2582 [ACTION_RUNLEVEL2] = '2',
2583 [ACTION_RUNLEVEL3] = '3',
2584 [ACTION_RUNLEVEL4] = '4',
2585 [ACTION_RUNLEVEL5] = '5',
2586 [ACTION_RESCUE] = '1'
2587 };
2588
d55ae9e6
LP
2589 assert(arg_action < _ACTION_MAX);
2590
2591 return table[arg_action];
2592}
2593
f1c5860b 2594static int talk_upstart(void) {
d55ae9e6
LP
2595 DBusMessage *m = NULL, *reply = NULL;
2596 DBusError error;
2597 int previous, rl, r;
2598 char
2599 env1_buf[] = "RUNLEVEL=X",
2600 env2_buf[] = "PREVLEVEL=X";
2601 char *env1 = env1_buf, *env2 = env2_buf;
2602 const char *emit = "runlevel";
2603 dbus_bool_t b_false = FALSE;
2604 DBusMessageIter iter, sub;
f1c5860b 2605 DBusConnection *bus;
d55ae9e6
LP
2606
2607 dbus_error_init(&error);
2608
2609 if (!(rl = action_to_runlevel()))
2610 return 0;
2611
2612 if (utmp_get_runlevel(&previous, NULL) < 0)
2613 previous = 'N';
2614
f1c5860b
LP
2615 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2616 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2617 r = 0;
2618 goto finish;
2619 }
2620
2621 log_error("Failed to connect to Upstart bus: %s", error.message);
2622 r = -EIO;
2623 goto finish;
2624 }
2625
2626 if ((r = bus_check_peercred(bus)) < 0) {
2627 log_error("Failed to verify owner of bus.");
2628 goto finish;
2629 }
2630
d55ae9e6
LP
2631 if (!(m = dbus_message_new_method_call(
2632 "com.ubuntu.Upstart",
2633 "/com/ubuntu/Upstart",
2634 "com.ubuntu.Upstart0_6",
2635 "EmitEvent"))) {
2636
2637 log_error("Could not allocate message.");
f1c5860b
LP
2638 r = -ENOMEM;
2639 goto finish;
d55ae9e6
LP
2640 }
2641
2642 dbus_message_iter_init_append(m, &iter);
2643
2644 env1_buf[sizeof(env1_buf)-2] = rl;
2645 env2_buf[sizeof(env2_buf)-2] = previous;
2646
2647 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2648 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2649 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2650 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2651 !dbus_message_iter_close_container(&iter, &sub) ||
2652 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2653 log_error("Could not append arguments to message.");
2654 r = -ENOMEM;
2655 goto finish;
2656 }
2657
2658 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2659
2660 if (error_is_no_service(&error)) {
2661 r = 0;
2662 goto finish;
2663 }
2664
2665 log_error("Failed to issue method call: %s", error.message);
2666 r = -EIO;
2667 goto finish;
2668 }
2669
2670 r = 1;
2671
2672finish:
2673 if (m)
2674 dbus_message_unref(m);
2675
2676 if (reply)
2677 dbus_message_unref(reply);
2678
f1c5860b
LP
2679 if (bus)
2680 dbus_connection_unref(bus);
2681
d55ae9e6
LP
2682 dbus_error_free(&error);
2683
2684 return r;
2685}
2686
2687static int talk_initctl(void) {
eb22ac37
LP
2688 struct init_request request;
2689 int r, fd;
d55ae9e6 2690 char rl;
eb22ac37 2691
d55ae9e6 2692 if (!(rl = action_to_runlevel()))
eb22ac37
LP
2693 return 0;
2694
2695 zero(request);
2696 request.magic = INIT_MAGIC;
2697 request.sleeptime = 0;
2698 request.cmd = INIT_CMD_RUNLVL;
d55ae9e6
LP
2699 request.runlevel = rl;
2700
2701 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2702
2703 if (errno == ENOENT)
2704 return 0;
eb22ac37 2705
d55ae9e6 2706 log_error("Failed to open "INIT_FIFO": %m");
eb22ac37 2707 return -errno;
d55ae9e6 2708 }
eb22ac37 2709
d55ae9e6 2710 errno = 0;
eb22ac37
LP
2711 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2712 close_nointr_nofail(fd);
2713
d55ae9e6
LP
2714 if (r < 0) {
2715 log_error("Failed to write to "INIT_FIFO": %m");
eb22ac37 2716 return errno ? -errno : -EIO;
d55ae9e6 2717 }
eb22ac37
LP
2718
2719 return 1;
e4b61340
LP
2720}
2721
2722static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
7e4249b9 2723
7e4249b9
LP
2724 static const struct {
2725 const char* verb;
2726 const enum {
2727 MORE,
2728 LESS,
2729 EQUAL
2730 } argc_cmp;
2731 const int argc;
2732 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
2733 } verbs[] = {
2734 { "list-units", LESS, 1, list_units },
2735 { "list-jobs", EQUAL, 1, list_jobs },
2736 { "clear-jobs", EQUAL, 1, clear_jobs },
2737 { "load", MORE, 2, load_unit },
2738 { "cancel", MORE, 2, cancel_job },
2739 { "start", MORE, 2, start_unit },
2740 { "stop", MORE, 2, start_unit },
2741 { "reload", MORE, 2, start_unit },
2742 { "restart", MORE, 2, start_unit },
e4b61340 2743 { "isolate", EQUAL, 2, start_unit },
0183528f 2744 { "check", MORE, 2, check_unit },
48220598 2745 { "show", MORE, 1, show },
7e4249b9
LP
2746 { "monitor", EQUAL, 1, monitor },
2747 { "dump", EQUAL, 1, dump },
2748 { "snapshot", LESS, 2, snapshot },
6759e7a7 2749 { "delete", MORE, 2, delete_snapshot },
7e4249b9
LP
2750 { "daemon-reload", EQUAL, 1, clear_jobs },
2751 { "daemon-reexec", EQUAL, 1, clear_jobs },
2752 { "daemon-exit", EQUAL, 1, clear_jobs },
2753 { "show-environment", EQUAL, 1, show_enviroment },
2754 { "set-environment", MORE, 2, set_environment },
2755 { "unset-environment", MORE, 2, set_environment },
514f4ef5
LP
2756 { "halt", EQUAL, 1, start_special },
2757 { "poweroff", EQUAL, 1, start_special },
2758 { "reboot", EQUAL, 1, start_special },
2759 { "default", EQUAL, 1, start_special },
2760 { "rescue", EQUAL, 1, start_special },
2761 { "emergency", EQUAL, 1, start_special },
7e4249b9
LP
2762 };
2763
e4b61340 2764 int left;
7e4249b9 2765 unsigned i;
7e4249b9 2766
e4b61340
LP
2767 assert(bus);
2768 assert(argc >= 0);
2769 assert(argv);
7e4249b9
LP
2770
2771 left = argc - optind;
2772
2773 if (left <= 0)
2774 /* Special rule: no arguments means "list-units" */
2775 i = 0;
2776 else {
0183528f
LP
2777 if (streq(argv[optind], "help")) {
2778 systemctl_help();
2779 return 0;
2780 }
2781
7e4249b9
LP
2782 for (i = 0; i < ELEMENTSOF(verbs); i++)
2783 if (streq(argv[optind], verbs[i].verb))
2784 break;
2785
2786 if (i >= ELEMENTSOF(verbs)) {
2787 log_error("Unknown operation %s", argv[optind]);
e4b61340 2788 return -EINVAL;
7e4249b9
LP
2789 }
2790 }
2791
2792 switch (verbs[i].argc_cmp) {
2793
2794 case EQUAL:
2795 if (left != verbs[i].argc) {
2796 log_error("Invalid number of arguments.");
e4b61340 2797 return -EINVAL;
7e4249b9
LP
2798 }
2799
2800 break;
2801
2802 case MORE:
2803 if (left < verbs[i].argc) {
2804 log_error("Too few arguments.");
e4b61340 2805 return -EINVAL;
7e4249b9
LP
2806 }
2807
2808 break;
2809
2810 case LESS:
2811 if (left > verbs[i].argc) {
2812 log_error("Too many arguments.");
e4b61340 2813 return -EINVAL;
7e4249b9
LP
2814 }
2815
2816 break;
2817
2818 default:
2819 assert_not_reached("Unknown comparison operator.");
2820 }
2821
e4b61340
LP
2822 return verbs[i].dispatch(bus, argv + optind, left);
2823}
2824
2825static int reload_with_fallback(DBusConnection *bus) {
2826 int r;
2827
2828 if (bus) {
2829 /* First, try systemd via D-Bus. */
2830 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2831 return 0;
2832 }
2833
2834 /* Nothing else worked, so let's try signals */
2835 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2836
2837 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2838 log_error("kill() failed: %m");
2839 return -errno;
2840 }
2841
2842 return 0;
2843}
2844
2845static int start_with_fallback(DBusConnection *bus) {
2846 int r;
2847
514f4ef5 2848 warn_wall(arg_action);
ef2f1067 2849
e4b61340
LP
2850 if (bus) {
2851 /* First, try systemd via D-Bus. */
2852 if ((r = start_unit(bus, NULL, 0)) > 0)
2853 return 0;
2854
2855 /* Hmm, talking to systemd via D-Bus didn't work. Then
2856 * let's try to talk to Upstart via D-Bus. */
f1c5860b 2857 if ((r = talk_upstart()) > 0)
e4b61340
LP
2858 return 0;
2859 }
2860
2861 /* Nothing else worked, so let's try
2862 * /dev/initctl */
d55ae9e6
LP
2863 if ((r = talk_initctl()) != 0)
2864 return 0;
2865
2866 log_error("Failed to talk to init daemon.");
2867 return -EIO;
e4b61340
LP
2868}
2869
2870static int halt_main(DBusConnection *bus) {
2871 int r;
2872
2873 if (!arg_immediate)
2874 return start_with_fallback(bus);
2875
2876 if (!arg_no_wtmp)
2877 if ((r = utmp_put_shutdown(0)) < 0)
2878 log_warning("Failed to write utmp record: %s", strerror(-r));
2879
2880 if (!arg_no_sync)
2881 sync();
2882
2883 if (arg_dry)
2884 return 0;
2885
2886 /* Make sure C-A-D is handled by the kernel from this
2887 * point on... */
2888 reboot(RB_ENABLE_CAD);
2889
2890 switch (arg_action) {
2891
2892 case ACTION_HALT:
2893 log_info("Halting");
2894 reboot(RB_HALT_SYSTEM);
2895 break;
2896
2897 case ACTION_POWEROFF:
2898 log_info("Powering off");
2899 reboot(RB_POWER_OFF);
2900 break;
2901
2902 case ACTION_REBOOT:
2903 log_info("Rebooting");
2904 reboot(RB_AUTOBOOT);
2905 break;
2906
2907 default:
2908 assert_not_reached("Unknown halt action.");
2909 }
2910
2911 /* We should never reach this. */
2912 return -ENOSYS;
2913}
2914
2915static int runlevel_main(void) {
2916 int r, runlevel, previous;
2917
2918 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2919 printf("unknown");
2920 return r;
2921 }
2922
2923 printf("%c %c\n",
2924 previous <= 0 ? 'N' : previous,
2925 runlevel <= 0 ? 'N' : runlevel);
2926
2927 return 0;
2928}
2929
2930int main(int argc, char*argv[]) {
2931 int r, retval = 1;
2932 DBusConnection *bus = NULL;
2933 DBusError error;
2934
2935 dbus_error_init(&error);
2936
2937 log_parse_environment();
2938
2939 if ((r = parse_argv(argc, argv)) < 0)
2940 goto finish;
2941 else if (r == 0) {
2942 retval = 0;
7e4249b9
LP
2943 goto finish;
2944 }
2945
e4b61340
LP
2946 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2947 * let's shortcut this */
2948 if (arg_action == ACTION_RUNLEVEL) {
2949 retval = runlevel_main() < 0;
2950 goto finish;
2951 }
2952
5e8d1c9a 2953 /* If we are root, then let's not go via the bus */
f1c5860b 2954 if (geteuid() == 0 && !arg_session) {
5e8d1c9a 2955 bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error);
f1c5860b
LP
2956
2957 if (bus && bus_check_peercred(bus) < 0) {
2958 log_error("Failed to verify owner of bus.");
2959 goto finish;
2960 }
2961 } else
5e8d1c9a
LP
2962 bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error);
2963
2964 if (bus)
e4b61340
LP
2965 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2966
2967 switch (arg_action) {
2968
2969 case ACTION_SYSTEMCTL: {
2970
2971 if (!bus) {
2972 log_error("Failed to get D-Bus connection: %s", error.message);
2973 goto finish;
2974 }
2975
2976 retval = systemctl_main(bus, argc, argv) < 0;
2977 break;
2978 }
2979
2980 case ACTION_HALT:
2981 case ACTION_POWEROFF:
2982 case ACTION_REBOOT:
2983 retval = halt_main(bus) < 0;
2984 break;
2985
e4b61340
LP
2986 case ACTION_RUNLEVEL2:
2987 case ACTION_RUNLEVEL3:
2988 case ACTION_RUNLEVEL4:
2989 case ACTION_RUNLEVEL5:
2990 case ACTION_RESCUE:
514f4ef5 2991 case ACTION_EMERGENCY:
eb22ac37 2992 case ACTION_DEFAULT:
e4b61340
LP
2993 retval = start_with_fallback(bus) < 0;
2994 break;
7e4249b9 2995
e4b61340
LP
2996 case ACTION_RELOAD:
2997 case ACTION_REEXEC:
2998 retval = reload_with_fallback(bus) < 0;
2999 break;
3000
eb22ac37
LP
3001 case ACTION_INVALID:
3002 case ACTION_RUNLEVEL:
e4b61340
LP
3003 default:
3004 assert_not_reached("Unknown action");
3005 }
7e4249b9
LP
3006
3007finish:
3008
3009 if (bus)
3010 dbus_connection_unref(bus);
3011
e4b61340
LP
3012 dbus_error_free(&error);
3013
7e4249b9
LP
3014 dbus_shutdown();
3015
3016 return retval;
3017}