]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemctl.c
systemctl: when called as shutdown, properly handle user specified wall message
[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>
7e4249b9
LP
32
33#include <dbus/dbus.h>
34
35#include "log.h"
36#include "util.h"
37#include "macro.h"
38#include "set.h"
e4b61340 39#include "utmp-wtmp.h"
514f4ef5 40#include "special.h"
eb22ac37 41#include "initreq.h"
e4a9373f 42#include "strv.h"
7e4249b9
LP
43
44static const char *arg_type = NULL;
45static bool arg_all = false;
46static bool arg_replace = false;
47static bool arg_session = false;
48static bool arg_block = false;
e4b61340
LP
49static bool arg_immediate = false;
50static bool arg_no_wtmp = false;
51static bool arg_no_sync = false;
514f4ef5 52static bool arg_no_wall = false;
e4b61340
LP
53static bool arg_dry = false;
54static char **arg_wall = NULL;
55enum action {
56 ACTION_INVALID,
57 ACTION_SYSTEMCTL,
58 ACTION_HALT,
59 ACTION_POWEROFF,
60 ACTION_REBOOT,
e4b61340
LP
61 ACTION_RUNLEVEL2,
62 ACTION_RUNLEVEL3,
63 ACTION_RUNLEVEL4,
64 ACTION_RUNLEVEL5,
65 ACTION_RESCUE,
514f4ef5
LP
66 ACTION_EMERGENCY,
67 ACTION_DEFAULT,
e4b61340
LP
68 ACTION_RELOAD,
69 ACTION_REEXEC,
70 ACTION_RUNLEVEL,
71 _ACTION_MAX
72} arg_action = ACTION_SYSTEMCTL;
73
74static bool error_is_no_service(DBusError *error) {
75
514f4ef5
LP
76 assert(error);
77
e4b61340
LP
78 if (!dbus_error_is_set(error))
79 return false;
80
81 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
82 return true;
83
84 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
85 return true;
86
87 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
88}
7e4249b9
LP
89
90static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
91
514f4ef5
LP
92 assert(iter);
93 assert(data);
94
7e4249b9
LP
95 if (dbus_message_iter_get_arg_type(iter) != type)
96 return -EIO;
97
98 dbus_message_iter_get_basic(iter, data);
99
100 if (!dbus_message_iter_next(iter) != !next)
101 return -EIO;
102
103 return 0;
104}
105
106static int columns(void) {
107 static int parsed_columns = 0;
108 const char *e;
109
110 if (parsed_columns > 0)
111 return parsed_columns;
112
113 if ((e = getenv("COLUMNS")))
114 parsed_columns = atoi(e);
115
116 if (parsed_columns <= 0) {
117 struct winsize ws;
118 zero(ws);
119
120 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
121 parsed_columns = ws.ws_col;
122 }
123
124 if (parsed_columns <= 0)
125 parsed_columns = 80;
126
127 return parsed_columns;
e4b61340 128
7e4249b9
LP
129}
130
514f4ef5 131static void warn_wall(enum action action) {
ef2f1067 132 static const char *table[_ACTION_MAX] = {
514f4ef5
LP
133 [ACTION_HALT] = "The system is going down for system halt NOW!",
134 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
135 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
136 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
137 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
ef2f1067
LP
138 };
139
514f4ef5
LP
140 if (arg_no_wall)
141 return;
142
e4a9373f
LP
143 if (arg_wall) {
144 char *p;
145
146 if (!(p = strv_join(arg_wall, " "))) {
147 log_error("Failed to join strings.");
148 return;
149 }
150
151 if (*p) {
152 utmp_wall(p);
153 free(p);
154 return;
155 }
156
157 free(p);
158 }
159
514f4ef5 160 if (!table[action])
ef2f1067
LP
161 return;
162
514f4ef5 163 utmp_wall(table[action]);
ef2f1067
LP
164}
165
7e4249b9
LP
166static int list_units(DBusConnection *bus, char **args, unsigned n) {
167 DBusMessage *m = NULL, *reply = NULL;
168 DBusError error;
169 int r;
170 DBusMessageIter iter, sub, sub2;
171 unsigned k = 0;
172
173 dbus_error_init(&error);
174
514f4ef5
LP
175 assert(bus);
176
7e4249b9
LP
177 if (!(m = dbus_message_new_method_call(
178 "org.freedesktop.systemd1",
179 "/org/freedesktop/systemd1",
180 "org.freedesktop.systemd1.Manager",
181 "ListUnits"))) {
182 log_error("Could not allocate message.");
183 return -ENOMEM;
184 }
185
186 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
187 log_error("Failed to issue method call: %s", error.message);
188 r = -EIO;
189 goto finish;
190 }
191
192 if (!dbus_message_iter_init(reply, &iter) ||
193 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
194 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
195 log_error("Failed to parse reply.");
196 r = -EIO;
197 goto finish;
198 }
199
200 dbus_message_iter_recurse(&iter, &sub);
201
202 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
203
204 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
205 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
206 uint32_t job_id;
207
208 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
209 log_error("Failed to parse reply.");
210 r = -EIO;
211 goto finish;
212 }
213
214 dbus_message_iter_recurse(&sub, &sub2);
215
216 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
217 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
218 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
219 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
220 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
221 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
222 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
223 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
224 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
225 log_error("Failed to parse reply.");
226 r = -EIO;
227 goto finish;
228 }
229
230 if ((!arg_type || ((dot = strrchr(id, '.')) &&
231 streq(dot+1, arg_type))) &&
232 (arg_all || !streq(active_state, "inactive"))) {
233
234 int a = 0, b = 0;
235
236 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
237
238 if (job_id != 0)
239 printf(" %-15s%n", job_type, &b);
240 else
241 b = 1 + 15;
242
243 if (a + b + 2 < columns()) {
244 if (job_id == 0)
245 printf(" ");
246
247 printf("%.*s", columns() - a - b - 2, description);
248 }
249
250 fputs("\n", stdout);
251 k++;
252 }
253
254 dbus_message_iter_next(&sub);
255 }
256
257 if (arg_all)
258 printf("\n%u units listed.\n", k);
259 else
260 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
261
262 r = 0;
263
264finish:
265 if (m)
266 dbus_message_unref(m);
267
268 if (reply)
269 dbus_message_unref(reply);
270
271 dbus_error_free(&error);
272
273 return r;
274}
275
276static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
277 DBusMessage *m = NULL, *reply = NULL;
278 DBusError error;
279 int r;
280 DBusMessageIter iter, sub, sub2;
281 unsigned k = 0;
282
283 dbus_error_init(&error);
284
514f4ef5
LP
285 assert(bus);
286
7e4249b9
LP
287 if (!(m = dbus_message_new_method_call(
288 "org.freedesktop.systemd1",
289 "/org/freedesktop/systemd1",
290 "org.freedesktop.systemd1.Manager",
291 "ListJobs"))) {
292 log_error("Could not allocate message.");
293 return -ENOMEM;
294 }
295
296 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
297 log_error("Failed to issue method call: %s", error.message);
298 r = -EIO;
299 goto finish;
300 }
301
302 if (!dbus_message_iter_init(reply, &iter) ||
303 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
304 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
305 log_error("Failed to parse reply.");
306 r = -EIO;
307 goto finish;
308 }
309
310 dbus_message_iter_recurse(&iter, &sub);
311
312 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
313
314 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
315 const char *name, *type, *state, *job_path, *unit_path;
316 uint32_t id;
317
318 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
319 log_error("Failed to parse reply.");
320 r = -EIO;
321 goto finish;
322 }
323
324 dbus_message_iter_recurse(&sub, &sub2);
325
326 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
327 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
328 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
329 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
330 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
331 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
332 log_error("Failed to parse reply.");
333 r = -EIO;
334 goto finish;
335 }
336
337 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
338 k++;
339
340 dbus_message_iter_next(&sub);
341 }
342
343 printf("\n%u jobs listed.\n", k);
344 r = 0;
345
346finish:
347 if (m)
348 dbus_message_unref(m);
349
350 if (reply)
351 dbus_message_unref(reply);
352
353 dbus_error_free(&error);
354
355 return r;
356}
357
358static int load_unit(DBusConnection *bus, char **args, unsigned n) {
359 DBusMessage *m = NULL, *reply = NULL;
360 DBusError error;
361 int r;
362 unsigned i;
363
364 dbus_error_init(&error);
365
514f4ef5
LP
366 assert(bus);
367 assert(args);
368
7e4249b9
LP
369 for (i = 1; i < n; i++) {
370
371 if (!(m = dbus_message_new_method_call(
372 "org.freedesktop.systemd1",
373 "/org/freedesktop/systemd1",
374 "org.freedesktop.systemd1.Manager",
375 "LoadUnit"))) {
376 log_error("Could not allocate message.");
377 r = -ENOMEM;
378 goto finish;
379 }
380
381 if (!dbus_message_append_args(m,
382 DBUS_TYPE_STRING, &args[i],
383 DBUS_TYPE_INVALID)) {
384 log_error("Could not append arguments to message.");
385 r = -ENOMEM;
386 goto finish;
387 }
388
389 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
390 log_error("Failed to issue method call: %s", error.message);
391 r = -EIO;
392 goto finish;
393 }
394
395 dbus_message_unref(m);
396 dbus_message_unref(reply);
397
398 m = reply = NULL;
399 }
400
401 r = 0;
402
403finish:
404 if (m)
405 dbus_message_unref(m);
406
407 if (reply)
408 dbus_message_unref(reply);
409
410 dbus_error_free(&error);
411
412 return r;
413}
414
415static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
416 DBusMessage *m = NULL, *reply = NULL;
417 DBusError error;
418 int r;
419 unsigned i;
420
421 dbus_error_init(&error);
422
514f4ef5
LP
423 assert(bus);
424 assert(args);
425
7e4249b9
LP
426 for (i = 1; i < n; i++) {
427 unsigned id;
428 const char *path;
429
430 if (!(m = dbus_message_new_method_call(
431 "org.freedesktop.systemd1",
432 "/org/freedesktop/systemd1",
433 "org.freedesktop.systemd1.Manager",
434 "GetJob"))) {
435 log_error("Could not allocate message.");
436 r = -ENOMEM;
437 goto finish;
438 }
439
440 if ((r = safe_atou(args[i], &id)) < 0) {
441 log_error("Failed to parse job id: %s", strerror(-r));
442 goto finish;
443 }
444
445 assert_cc(sizeof(uint32_t) == sizeof(id));
446 if (!dbus_message_append_args(m,
447 DBUS_TYPE_UINT32, &id,
448 DBUS_TYPE_INVALID)) {
449 log_error("Could not append arguments to message.");
450 r = -ENOMEM;
451 goto finish;
452 }
453
454 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
455 log_error("Failed to issue method call: %s", error.message);
456 r = -EIO;
457 goto finish;
458 }
459
460 if (!dbus_message_get_args(reply, &error,
461 DBUS_TYPE_OBJECT_PATH, &path,
462 DBUS_TYPE_INVALID)) {
463 log_error("Failed to parse reply: %s", error.message);
464 r = -EIO;
465 goto finish;
466 }
467
468 dbus_message_unref(m);
469 if (!(m = dbus_message_new_method_call(
470 "org.freedesktop.systemd1",
471 path,
472 "org.freedesktop.systemd1.Job",
473 "Cancel"))) {
474 log_error("Could not allocate message.");
475 r = -ENOMEM;
476 goto finish;
477 }
478
479 dbus_message_unref(reply);
480 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
481 log_error("Failed to issue method call: %s", error.message);
482 r = -EIO;
483 goto finish;
484 }
485
486 dbus_message_unref(m);
487 dbus_message_unref(reply);
488 m = reply = NULL;
489 }
490
491 r = 0;
492
493finish:
494 if (m)
495 dbus_message_unref(m);
496
497 if (reply)
498 dbus_message_unref(reply);
499
500 dbus_error_free(&error);
501
502 return r;
503}
504
505static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
506 DBusError error;
507 Set *s = data;
508
509 assert(connection);
510 assert(message);
511 assert(s);
512
513 dbus_error_init(&error);
514
515 /* log_debug("Got D-Bus request: %s.%s() on %s", */
516 /* dbus_message_get_interface(message), */
517 /* dbus_message_get_member(message), */
518 /* dbus_message_get_path(message)); */
519
520 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
521 log_error("Warning! D-Bus connection terminated.");
522 dbus_connection_close(connection);
523
524 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
525 uint32_t id;
526 const char *path;
527
528 if (!dbus_message_get_args(message, &error,
529 DBUS_TYPE_UINT32, &id,
530 DBUS_TYPE_OBJECT_PATH, &path,
531 DBUS_TYPE_INVALID))
532 log_error("Failed to parse message: %s", error.message);
533 else {
534 char *p;
535
536 if ((p = set_remove(s, (char*) path)))
537 free(p);
538 }
539 }
540
541 dbus_error_free(&error);
542 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
543}
544
479ef5d3 545static int enable_wait_for_jobs(DBusConnection *bus) {
7e4249b9
LP
546 DBusError error;
547 DBusMessage *m = NULL, *reply = NULL;
548 int r;
549
550 assert(bus);
7e4249b9
LP
551
552 dbus_error_init(&error);
553
554 dbus_bus_add_match(bus,
555 "type='signal',"
556 "sender='org.freedesktop.systemd1',"
557 "interface='org.freedesktop.systemd1.Manager',"
558 "member='JobRemoved',"
559 "path='/org/freedesktop/systemd1'",
560 &error);
561
562 if (dbus_error_is_set(&error)) {
563 log_error("Failed to add match: %s", error.message);
564 r = -EIO;
565 goto finish;
566 }
567
7e4249b9
LP
568 if (!(m = dbus_message_new_method_call(
569 "org.freedesktop.systemd1",
570 "/org/freedesktop/systemd1",
571 "org.freedesktop.systemd1.Manager",
572 "Subscribe"))) {
573 log_error("Could not allocate message.");
574 r = -ENOMEM;
575 goto finish;
576 }
577
578 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
579 log_error("Failed to issue method call: %s", error.message);
580 r = -EIO;
581 goto finish;
582 }
583
7e4249b9
LP
584 r = 0;
585
586finish:
479ef5d3 587 /* This is slightly dirty, since we don't undo the match registrations. */
7e4249b9
LP
588
589 if (m)
590 dbus_message_unref(m);
591
592 if (reply)
593 dbus_message_unref(reply);
594
595 dbus_error_free(&error);
596
597 return r;
598}
599
479ef5d3
LP
600static int wait_for_jobs(DBusConnection *bus, Set *s) {
601 int r;
602
603 assert(bus);
604 assert(s);
605
606 if (!dbus_connection_add_filter(bus, wait_filter, s, NULL)) {
607 log_error("Failed to add filter.");
608 r = -ENOMEM;
609 goto finish;
610 }
611
612 while (!set_isempty(s) &&
613 dbus_connection_read_write_dispatch(bus, -1))
614 ;
615
616 r = 0;
617
618finish:
619 /* This is slightly dirty, since we don't undo the filter registration. */
620
621 return r;
622}
623
e4b61340
LP
624static int start_unit_one(
625 DBusConnection *bus,
626 const char *method,
627 const char *name,
628 const char *mode,
629 Set *s) {
7e4249b9 630
7e4249b9
LP
631 DBusMessage *m = NULL, *reply = NULL;
632 DBusError error;
633 int r;
7e4249b9 634
e4b61340
LP
635 assert(bus);
636 assert(method);
637 assert(name);
638 assert(mode);
639 assert(!arg_block || s);
7e4249b9 640
e4b61340 641 dbus_error_init(&error);
479ef5d3 642
7e4249b9
LP
643 if (!(m = dbus_message_new_method_call(
644 "org.freedesktop.systemd1",
645 "/org/freedesktop/systemd1",
646 "org.freedesktop.systemd1.Manager",
e4b61340 647 method))) {
7e4249b9
LP
648 log_error("Could not allocate message.");
649 r = -ENOMEM;
650 goto finish;
651 }
652
653 if (!dbus_message_append_args(m,
e4b61340 654 DBUS_TYPE_STRING, &name,
7e4249b9
LP
655 DBUS_TYPE_STRING, &mode,
656 DBUS_TYPE_INVALID)) {
657 log_error("Could not append arguments to message.");
658 r = -ENOMEM;
659 goto finish;
660 }
661
662 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
e4b61340
LP
663
664 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
665 /* There's always a fallback possible for
666 * legacy actions. */
667 r = 0;
668 goto finish;
669 }
670
7e4249b9
LP
671 log_error("Failed to issue method call: %s", error.message);
672 r = -EIO;
673 goto finish;
674 }
675
676 if (arg_block) {
677 const char *path;
e4b61340 678 char *p;
7e4249b9
LP
679
680 if (!dbus_message_get_args(reply, &error,
681 DBUS_TYPE_OBJECT_PATH, &path,
682 DBUS_TYPE_INVALID)) {
683 log_error("Failed to parse reply: %s", error.message);
684 r = -EIO;
685 goto finish;
686 }
687
7e4249b9
LP
688 if (!(p = strdup(path))) {
689 log_error("Failed to duplicate path.");
690 r = -ENOMEM;
691 goto finish;
692 }
693
694 if ((r = set_put(s, p)) < 0) {
e4b61340 695 free(p);
7e4249b9
LP
696 log_error("Failed to add path to set.");
697 goto finish;
698 }
e4b61340 699 }
7e4249b9 700
e4b61340 701 r = 1;
7e4249b9
LP
702
703finish:
7e4249b9
LP
704 if (m)
705 dbus_message_unref(m);
706
707 if (reply)
708 dbus_message_unref(reply);
709
710 dbus_error_free(&error);
711
712 return r;
713}
714
514f4ef5
LP
715static enum action verb_to_action(const char *verb) {
716 if (streq(verb, "halt"))
717 return ACTION_HALT;
718 else if (streq(verb, "poweroff"))
719 return ACTION_POWEROFF;
720 else if (streq(verb, "reboot"))
721 return ACTION_REBOOT;
722 else if (streq(verb, "rescue"))
723 return ACTION_RESCUE;
724 else if (streq(verb, "emergency"))
725 return ACTION_EMERGENCY;
726 else if (streq(verb, "default"))
727 return ACTION_DEFAULT;
728 else
729 return ACTION_INVALID;
730}
731
e4b61340
LP
732static int start_unit(DBusConnection *bus, char **args, unsigned n) {
733
734 static const char * const table[_ACTION_MAX] = {
514f4ef5
LP
735 [ACTION_HALT] = SPECIAL_HALT_TARGET,
736 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
737 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
738 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
739 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
740 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
741 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
742 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
743 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
744 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
e4b61340
LP
745 };
746
747 int r;
748 unsigned i;
514f4ef5 749 const char *method, *mode, *one_name;
e4b61340
LP
750 Set *s = NULL;
751
514f4ef5
LP
752 assert(bus);
753
e4b61340
LP
754 if (arg_action == ACTION_SYSTEMCTL) {
755 method =
e4b61340
LP
756 streq(args[0], "stop") ? "StopUnit" :
757 streq(args[0], "reload") ? "ReloadUnit" :
758 streq(args[0], "restart") ? "RestartUnit" :
514f4ef5 759 "StartUnit";
e4b61340
LP
760
761 mode =
514f4ef5
LP
762 (streq(args[0], "isolate") ||
763 streq(args[0], "rescue") ||
764 streq(args[0], "emergency")) ? "isolate" :
765 arg_replace ? "replace" :
766 "fail";
e4b61340 767
514f4ef5 768 one_name = table[verb_to_action(args[0])];
e4b61340 769
e4b61340
LP
770 } else {
771 assert(arg_action < ELEMENTSOF(table));
772 assert(table[arg_action]);
773
774 method = "StartUnit";
514f4ef5
LP
775
776 mode = (arg_action == ACTION_EMERGENCY ||
777 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
778
779 one_name = table[arg_action];
780 }
781
782 if (arg_block) {
783 if ((r = enable_wait_for_jobs(bus)) < 0) {
784 log_error("Could not watch jobs: %s", strerror(-r));
785 goto finish;
786 }
787
788 if (!(s = set_new(string_hash_func, string_compare_func))) {
789 log_error("Failed to allocate set.");
790 r = -ENOMEM;
791 goto finish;
792 }
e4b61340
LP
793 }
794
795 r = 0;
796
514f4ef5
LP
797 if (one_name) {
798 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
799 goto finish;
800 } else {
e4b61340
LP
801 for (i = 1; i < n; i++)
802 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
803 goto finish;
e4b61340
LP
804 }
805
514f4ef5
LP
806 if (arg_block)
807 r = wait_for_jobs(bus, s);
808
e4b61340
LP
809finish:
810 if (s)
811 set_free_free(s);
812
813 return r;
814}
815
514f4ef5
LP
816static int start_special(DBusConnection *bus, char **args, unsigned n) {
817 assert(bus);
818 assert(args);
819
820 warn_wall(verb_to_action(args[0]));
821
822 return start_unit(bus, args, n);
823}
824
7e4249b9
LP
825static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
826 DBusError error;
827 DBusMessage *m = NULL, *reply = NULL;
828
829 assert(connection);
830 assert(message);
831
832 dbus_error_init(&error);
833
834 /* log_debug("Got D-Bus request: %s.%s() on %s", */
835 /* dbus_message_get_interface(message), */
836 /* dbus_message_get_member(message), */
837 /* dbus_message_get_path(message)); */
838
839 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
840 log_error("Warning! D-Bus connection terminated.");
841 dbus_connection_close(connection);
842
843 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
844 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
845 const char *id, *path;
846
847 if (!dbus_message_get_args(message, &error,
848 DBUS_TYPE_STRING, &id,
849 DBUS_TYPE_OBJECT_PATH, &path,
850 DBUS_TYPE_INVALID))
851 log_error("Failed to parse message: %s", error.message);
852 else if (streq(dbus_message_get_member(message), "UnitNew"))
853 printf("Unit %s added.\n", id);
854 else
855 printf("Unit %s removed.\n", id);
856
857 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
858 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
859 uint32_t id;
860 const char *path;
861
862 if (!dbus_message_get_args(message, &error,
863 DBUS_TYPE_UINT32, &id,
864 DBUS_TYPE_OBJECT_PATH, &path,
865 DBUS_TYPE_INVALID))
866 log_error("Failed to parse message: %s", error.message);
867 else if (streq(dbus_message_get_member(message), "JobNew"))
868 printf("Job %u added.\n", id);
869 else
870 printf("Job %u removed.\n", id);
871
872
873 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
874 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
875
876 const char *path, *interface, *property = "Id";
877 DBusMessageIter iter, sub;
878
879 path = dbus_message_get_path(message);
880 interface = dbus_message_get_interface(message);
881
882 if (!(m = dbus_message_new_method_call(
883 "org.freedesktop.systemd1",
884 path,
885 "org.freedesktop.DBus.Properties",
886 "Get"))) {
887 log_error("Could not allocate message.");
888 goto oom;
889 }
890
891 if (!dbus_message_append_args(m,
892 DBUS_TYPE_STRING, &interface,
893 DBUS_TYPE_STRING, &property,
894 DBUS_TYPE_INVALID)) {
895 log_error("Could not append arguments to message.");
896 goto finish;
897 }
898
899 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
900 log_error("Failed to issue method call: %s", error.message);
901 goto finish;
902 }
903
904 if (!dbus_message_iter_init(reply, &iter) ||
905 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
906 log_error("Failed to parse reply.");
907 goto finish;
908 }
909
910 dbus_message_iter_recurse(&iter, &sub);
911
912 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
913 const char *id;
914
915 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
916 log_error("Failed to parse reply.");
917 goto finish;
918 }
919
920 dbus_message_iter_get_basic(&sub, &id);
921 printf("Unit %s changed.\n", id);
922 } else {
923 uint32_t id;
924
925 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
926 log_error("Failed to parse reply.");
927 goto finish;
928 }
929
930 dbus_message_iter_get_basic(&sub, &id);
931 printf("Job %u changed.\n", id);
932 }
933 }
934
935finish:
936 if (m)
937 dbus_message_unref(m);
938
939 if (reply)
940 dbus_message_unref(reply);
941
942 dbus_error_free(&error);
943 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
944
945oom:
946 if (m)
947 dbus_message_unref(m);
948
949 if (reply)
950 dbus_message_unref(reply);
951
952 dbus_error_free(&error);
953 return DBUS_HANDLER_RESULT_NEED_MEMORY;
954}
955
956static int monitor(DBusConnection *bus, char **args, unsigned n) {
957 DBusMessage *m = NULL, *reply = NULL;
958 DBusError error;
959 int r;
960
961 dbus_error_init(&error);
962
963 dbus_bus_add_match(bus,
964 "type='signal',"
965 "sender='org.freedesktop.systemd1',"
966 "interface='org.freedesktop.systemd1.Manager',"
967 "path='/org/freedesktop/systemd1'",
968 &error);
969
970 if (dbus_error_is_set(&error)) {
971 log_error("Failed to add match: %s", error.message);
972 r = -EIO;
973 goto finish;
974 }
975
976 dbus_bus_add_match(bus,
977 "type='signal',"
978 "sender='org.freedesktop.systemd1',"
979 "interface='org.freedesktop.systemd1.Unit',"
980 "member='Changed'",
981 &error);
982
983 if (dbus_error_is_set(&error)) {
984 log_error("Failed to add match: %s", error.message);
985 r = -EIO;
986 goto finish;
987 }
988
989 dbus_bus_add_match(bus,
990 "type='signal',"
991 "sender='org.freedesktop.systemd1',"
992 "interface='org.freedesktop.systemd1.Job',"
993 "member='Changed'",
994 &error);
995
996 if (dbus_error_is_set(&error)) {
997 log_error("Failed to add match: %s", error.message);
998 r = -EIO;
999 goto finish;
1000 }
1001
1002 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1003 log_error("Failed to add filter.");
1004 r = -ENOMEM;
1005 goto finish;
1006 }
1007
1008 if (!(m = dbus_message_new_method_call(
1009 "org.freedesktop.systemd1",
1010 "/org/freedesktop/systemd1",
1011 "org.freedesktop.systemd1.Manager",
1012 "Subscribe"))) {
1013 log_error("Could not allocate message.");
1014 r = -ENOMEM;
1015 goto finish;
1016 }
1017
1018 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1019 log_error("Failed to issue method call: %s", error.message);
1020 r = -EIO;
1021 goto finish;
1022 }
1023
1024 while (dbus_connection_read_write_dispatch(bus, -1))
1025 ;
1026
1027 r = 0;
1028
1029finish:
1030
1031 /* This is slightly dirty, since we don't undo the filter or the matches. */
1032
1033 if (m)
1034 dbus_message_unref(m);
1035
1036 if (reply)
1037 dbus_message_unref(reply);
1038
1039 dbus_error_free(&error);
1040
1041 return r;
1042}
1043
1044static int dump(DBusConnection *bus, char **args, unsigned n) {
1045 DBusMessage *m = NULL, *reply = NULL;
1046 DBusError error;
1047 int r;
1048 const char *text;
1049
1050 dbus_error_init(&error);
1051
1052 if (!(m = dbus_message_new_method_call(
1053 "org.freedesktop.systemd1",
1054 "/org/freedesktop/systemd1",
1055 "org.freedesktop.systemd1.Manager",
1056 "Dump"))) {
1057 log_error("Could not allocate message.");
1058 return -ENOMEM;
1059 }
1060
1061 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1062 log_error("Failed to issue method call: %s", error.message);
1063 r = -EIO;
1064 goto finish;
1065 }
1066
1067 if (!dbus_message_get_args(reply, &error,
1068 DBUS_TYPE_STRING, &text,
1069 DBUS_TYPE_INVALID)) {
1070 log_error("Failed to parse reply: %s", error.message);
1071 r = -EIO;
1072 goto finish;
1073 }
1074
1075 fputs(text, stdout);
1076
1077 r = 0;
1078
1079finish:
1080 if (m)
1081 dbus_message_unref(m);
1082
1083 if (reply)
1084 dbus_message_unref(reply);
1085
1086 dbus_error_free(&error);
1087
1088 return r;
1089}
1090
1091static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1092 DBusMessage *m = NULL, *reply = NULL;
1093 DBusError error;
1094 int r;
1095 const char *name = "", *path, *id;
1096 dbus_bool_t cleanup = FALSE;
1097 DBusMessageIter iter, sub;
1098 const char
1099 *interface = "org.freedesktop.systemd1.Unit",
1100 *property = "Id";
1101
1102 dbus_error_init(&error);
1103
1104 if (!(m = dbus_message_new_method_call(
1105 "org.freedesktop.systemd1",
1106 "/org/freedesktop/systemd1",
1107 "org.freedesktop.systemd1.Manager",
1108 "CreateSnapshot"))) {
1109 log_error("Could not allocate message.");
1110 return -ENOMEM;
1111 }
1112
1113 if (n > 1)
1114 name = args[1];
1115
1116 if (!dbus_message_append_args(m,
1117 DBUS_TYPE_STRING, &name,
1118 DBUS_TYPE_BOOLEAN, &cleanup,
1119 DBUS_TYPE_INVALID)) {
1120 log_error("Could not append arguments to message.");
1121 r = -ENOMEM;
1122 goto finish;
1123 }
1124
1125 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1126 log_error("Failed to issue method call: %s", error.message);
1127 r = -EIO;
1128 goto finish;
1129 }
1130
1131 if (!dbus_message_get_args(reply, &error,
1132 DBUS_TYPE_OBJECT_PATH, &path,
1133 DBUS_TYPE_INVALID)) {
1134 log_error("Failed to parse reply: %s", error.message);
1135 r = -EIO;
1136 goto finish;
1137 }
1138
1139 dbus_message_unref(m);
1140 if (!(m = dbus_message_new_method_call(
1141 "org.freedesktop.systemd1",
1142 path,
1143 "org.freedesktop.DBus.Properties",
1144 "Get"))) {
1145 log_error("Could not allocate message.");
1146 return -ENOMEM;
1147 }
1148
1149 if (!dbus_message_append_args(m,
1150 DBUS_TYPE_STRING, &interface,
1151 DBUS_TYPE_STRING, &property,
1152 DBUS_TYPE_INVALID)) {
1153 log_error("Could not append arguments to message.");
1154 r = -ENOMEM;
1155 goto finish;
1156 }
1157
1158 dbus_message_unref(reply);
1159 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1160 log_error("Failed to issue method call: %s", error.message);
1161 r = -EIO;
1162 goto finish;
1163 }
1164
1165 if (!dbus_message_iter_init(reply, &iter) ||
1166 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1167 log_error("Failed to parse reply.");
1168 r = -EIO;
1169 goto finish;
1170 }
1171
1172 dbus_message_iter_recurse(&iter, &sub);
1173
1174 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1175 log_error("Failed to parse reply.");
1176 r = -EIO;
1177 goto finish;
1178 }
1179
1180 dbus_message_iter_get_basic(&sub, &id);
1181 puts(id);
1182 r = 0;
1183
1184finish:
1185 if (m)
1186 dbus_message_unref(m);
1187
1188 if (reply)
1189 dbus_message_unref(reply);
1190
1191 dbus_error_free(&error);
1192
1193 return r;
1194}
1195
1196static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1197 DBusMessage *m = NULL, *reply = NULL;
1198 DBusError error;
1199 int r;
1200 const char *method;
1201
1202 dbus_error_init(&error);
1203
e4b61340
LP
1204 if (arg_action == ACTION_RELOAD)
1205 method = "Reload";
1206 else if (arg_action == ACTION_REEXEC)
1207 method = "Reexecute";
1208 else {
1209 assert(arg_action == ACTION_SYSTEMCTL);
1210
1211 method =
1212 streq(args[0], "clear-jobs") ? "ClearJobs" :
1213 streq(args[0], "daemon-reload") ? "Reload" :
1214 streq(args[0], "daemon-reexec") ? "Reexecute" :
1215 "Exit";
1216 }
7e4249b9
LP
1217
1218 if (!(m = dbus_message_new_method_call(
1219 "org.freedesktop.systemd1",
1220 "/org/freedesktop/systemd1",
1221 "org.freedesktop.systemd1.Manager",
1222 method))) {
1223 log_error("Could not allocate message.");
1224 return -ENOMEM;
1225 }
1226
1227 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
e4b61340
LP
1228
1229 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1230 /* There's always a fallback possible for
1231 * legacy actions. */
1232 r = 0;
1233 goto finish;
1234 }
1235
7e4249b9
LP
1236 log_error("Failed to issue method call: %s", error.message);
1237 r = -EIO;
1238 goto finish;
1239 }
1240
e4b61340 1241 r = 1;
7e4249b9
LP
1242
1243finish:
1244 if (m)
1245 dbus_message_unref(m);
1246
1247 if (reply)
1248 dbus_message_unref(reply);
1249
1250 dbus_error_free(&error);
1251
1252 return r;
1253}
1254
1255static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1256 DBusMessage *m = NULL, *reply = NULL;
1257 DBusError error;
1258 DBusMessageIter iter, sub, sub2;
1259 int r;
1260 const char
1261 *interface = "org.freedesktop.systemd1.Manager",
1262 *property = "Environment";
1263
1264 dbus_error_init(&error);
1265
1266 if (!(m = dbus_message_new_method_call(
1267 "org.freedesktop.systemd1",
1268 "/org/freedesktop/systemd1",
1269 "org.freedesktop.DBus.Properties",
1270 "Get"))) {
1271 log_error("Could not allocate message.");
1272 return -ENOMEM;
1273 }
1274
1275 if (!dbus_message_append_args(m,
1276 DBUS_TYPE_STRING, &interface,
1277 DBUS_TYPE_STRING, &property,
1278 DBUS_TYPE_INVALID)) {
1279 log_error("Could not append arguments to message.");
1280 r = -ENOMEM;
1281 goto finish;
1282 }
1283
1284 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1285 log_error("Failed to issue method call: %s", error.message);
1286 r = -EIO;
1287 goto finish;
1288 }
1289
1290 if (!dbus_message_iter_init(reply, &iter) ||
1291 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1292 log_error("Failed to parse reply.");
1293 r = -EIO;
1294 goto finish;
1295 }
1296
1297 dbus_message_iter_recurse(&iter, &sub);
1298
1299 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1300 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1301 log_error("Failed to parse reply.");
1302 r = -EIO;
1303 goto finish;
1304 }
1305
1306 dbus_message_iter_recurse(&sub, &sub2);
1307
1308 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1309 const char *text;
1310
1311 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1312 log_error("Failed to parse reply.");
1313 r = -EIO;
1314 goto finish;
1315 }
1316
1317 dbus_message_iter_get_basic(&sub2, &text);
1318 printf("%s\n", text);
1319
1320 dbus_message_iter_next(&sub2);
1321 }
1322
1323 r = 0;
1324
1325finish:
1326 if (m)
1327 dbus_message_unref(m);
1328
1329 if (reply)
1330 dbus_message_unref(reply);
1331
1332 dbus_error_free(&error);
1333
1334 return r;
1335}
1336
1337static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1338 DBusMessage *m = NULL, *reply = NULL;
1339 DBusError error;
1340 int r;
1341 const char *method;
1342 DBusMessageIter iter, sub;
1343 unsigned i;
1344
1345 dbus_error_init(&error);
1346
1347 method = streq(args[0], "set-environment")
1348 ? "SetEnvironment"
1349 : "UnsetEnvironment";
1350
1351 if (!(m = dbus_message_new_method_call(
1352 "org.freedesktop.systemd1",
1353 "/org/freedesktop/systemd1",
1354 "org.freedesktop.systemd1.Manager",
1355 method))) {
1356
1357 log_error("Could not allocate message.");
1358 return -ENOMEM;
1359 }
1360
1361 dbus_message_iter_init_append(m, &iter);
1362
1363 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
1364 log_error("Could not append arguments to message.");
1365 r = -ENOMEM;
1366 goto finish;
1367 }
1368
1369 for (i = 1; i < n; i++)
1370 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
1371 log_error("Could not append arguments to message.");
1372 r = -ENOMEM;
1373 goto finish;
1374 }
1375
1376 if (!dbus_message_iter_close_container(&iter, &sub)) {
1377 log_error("Could not append arguments to message.");
1378 r = -ENOMEM;
1379 goto finish;
1380 }
1381
1382 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1383 log_error("Failed to issue method call: %s", error.message);
1384 r = -EIO;
1385 goto finish;
1386 }
1387
1388 r = 0;
1389
1390finish:
1391 if (m)
1392 dbus_message_unref(m);
1393
1394 if (reply)
1395 dbus_message_unref(reply);
1396
1397 dbus_error_free(&error);
1398
1399 return r;
1400}
1401
e4b61340 1402static int systemctl_help(void) {
7e4249b9
LP
1403
1404 printf("%s [options]\n\n"
514f4ef5 1405 "Send control commands to the init daemon.\n\n"
7e4249b9
LP
1406 " -h --help Show this help\n"
1407 " -t --type=TYPE List only units of a particular type\n"
1408 " -a --all Show all units, including dead ones\n"
1409 " --replace When installing a new job, replace existing conflicting ones\n"
1410 " --system Connect to system bus\n"
1411 " --session Connect to session bus\n"
514f4ef5
LP
1412 " --block Wait until operation finished\n"
1413 " --no-wall Don't send wall message before reboot/halt/power-off\n\n"
7e4249b9
LP
1414 "Commands:\n"
1415 " list-units List units\n"
1416 " list-jobs List jobs\n"
1417 " clear-jobs Cancel all jobs\n"
1418 " load [NAME...] Load one or more units\n"
1419 " cancel [JOB...] Cancel one or more jobs\n"
1420 " start [NAME...] Start one or more units\n"
1421 " stop [NAME...] Stop one or more units\n"
1422 " restart [NAME...] Restart one or more units\n"
1423 " reload [NAME...] Reload one or more units\n"
1424 " isolate [NAME] Start one unit and stop all others\n"
1425 " monitor Monitor unit/job changes\n"
1426 " dump Dump server status\n"
1427 " snapshot [NAME] Create a snapshot\n"
514f4ef5
LP
1428 " daemon-reload Reload init daemon configuration\n"
1429 " daemon-reexecute Reexecute init daemon\n"
1430 " daemon-exit Ask the init daemon to quit\n"
7e4249b9
LP
1431 " show-environment Dump environment\n"
1432 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
514f4ef5
LP
1433 " unset-environment [NAME...] Unset one or more environment variables\n"
1434 " halt Shut down and halt the system\n"
1435 " reboot Shut down and reboot the system\n"
1436 " poweroff Shut down and power off the system\n"
1437 " default Enter default mode\n"
1438 " rescue Enter rescue mode\n"
1439 " emergency Enter emergency mode\n",
5b6319dc 1440 program_invocation_short_name);
7e4249b9
LP
1441
1442 return 0;
1443}
1444
e4b61340
LP
1445static int halt_help(void) {
1446
1447 printf("%s [options]\n\n"
1448 "%s the system.\n\n"
1449 " --help Show this help\n"
1450 " --halt Halt the machine\n"
1451 " -p --poweroff Switch off the machine\n"
1452 " --reboot Reboot the machine\n"
1453 " -f --force Force immediate reboot/halt/power-off\n"
1454 " -w --wtmp-only Don't reboot/halt/power-off, just write wtmp record\n"
1455 " -d --no-wtmp Don't write wtmp record\n"
514f4ef5
LP
1456 " -n --no-sync Don't sync before reboot/halt/power-off\n"
1457 " --no-wall Don't send wall message before reboot/halt/power-off\n",
e4b61340
LP
1458 program_invocation_short_name,
1459 arg_action == ACTION_REBOOT ? "Reboot" :
1460 arg_action == ACTION_POWEROFF ? "Power off" :
1461 "Halt");
1462
1463 return 0;
1464}
1465
1466static int shutdown_help(void) {
1467
514f4ef5 1468 printf("%s [options] [IGNORED] [WALL...]\n\n"
e4b61340
LP
1469 "Shut down the system.\n\n"
1470 " --help Show this help\n"
1471 " -H --halt Halt the machine\n"
1472 " -P --poweroff Power-off the machine\n"
1473 " -r --reboot Reboot the machine\n"
1474 " -h Equivalent to --poweroff, overriden by --halt\n"
514f4ef5
LP
1475 " -k Don't reboot/halt/power-off, just send warnings\n"
1476 " --no-wall Don't send wall message before reboot/halt/power-off\n",
e4b61340
LP
1477 program_invocation_short_name);
1478
1479 return 0;
1480}
1481
1482static int telinit_help(void) {
1483
1484 printf("%s [options]\n\n"
514f4ef5
LP
1485 "Send control commands to the init daemon.\n\n"
1486 " --help Show this help\n"
1487 " --no-wall Don't send wall message before reboot/halt/power-off\n\n"
e4b61340
LP
1488 "Commands:\n"
1489 " 0 Power-off the machine\n"
1490 " 6 Reboot the machine\n"
514f4ef5
LP
1491 " 2, 3, 4, 5 Start runlevelX.target unit\n"
1492 " 1, s, S Enter rescue mode\n"
1493 " q, Q Reload init daemon configuration\n"
1494 " u, U Reexecute init daemon\n",
e4b61340
LP
1495 program_invocation_short_name);
1496
1497 return 0;
1498}
1499
1500static int runlevel_help(void) {
1501
1502 printf("%s [options]\n\n"
1503 "Prints the previous and current runlevel of the init system.\n\n"
1504 " --help Show this help\n",
1505 program_invocation_short_name);
1506
1507 return 0;
1508}
1509
1510static int systemctl_parse_argv(int argc, char *argv[]) {
7e4249b9
LP
1511
1512 enum {
1513 ARG_REPLACE = 0x100,
1514 ARG_SESSION,
1515 ARG_SYSTEM,
514f4ef5
LP
1516 ARG_BLOCK,
1517 ARG_NO_WALL
7e4249b9
LP
1518 };
1519
1520 static const struct option options[] = {
1521 { "help", no_argument, NULL, 'h' },
1522 { "type", required_argument, NULL, 't' },
1523 { "all", no_argument, NULL, 'a' },
1524 { "replace", no_argument, NULL, ARG_REPLACE },
1525 { "session", no_argument, NULL, ARG_SESSION },
1526 { "system", no_argument, NULL, ARG_SYSTEM },
b08a3550 1527 { "block", no_argument, NULL, ARG_BLOCK },
514f4ef5 1528 { "no-wall", no_argument, NULL, ARG_NO_WALL },
b08a3550 1529 { NULL, 0, NULL, 0 }
7e4249b9
LP
1530 };
1531
1532 int c;
1533
e4b61340 1534 assert(argc >= 0);
7e4249b9
LP
1535 assert(argv);
1536
1537 while ((c = getopt_long(argc, argv, "hta", options, NULL)) >= 0) {
1538
1539 switch (c) {
1540
1541 case 'h':
e4b61340 1542 systemctl_help();
7e4249b9
LP
1543 return 0;
1544
1545 case 't':
1546 arg_type = optarg;
1547 break;
1548
1549 case 'a':
1550 arg_all = true;
1551 break;
1552
1553 case ARG_REPLACE:
1554 arg_replace = true;
1555 break;
1556
1557 case ARG_SESSION:
1558 arg_session = true;
1559 break;
1560
1561 case ARG_SYSTEM:
1562 arg_session = false;
1563 break;
1564
1565 case ARG_BLOCK:
1566 arg_block = true;
1567 break;
1568
514f4ef5
LP
1569 case ARG_NO_WALL:
1570 arg_no_wall = true;
1571 break;
1572
7e4249b9
LP
1573 case '?':
1574 return -EINVAL;
1575
1576 default:
1577 log_error("Unknown option code %c", c);
1578 return -EINVAL;
1579 }
1580 }
1581
1582 return 1;
1583}
1584
e4b61340
LP
1585static int halt_parse_argv(int argc, char *argv[]) {
1586
1587 enum {
1588 ARG_HELP = 0x100,
1589 ARG_HALT,
514f4ef5
LP
1590 ARG_REBOOT,
1591 ARG_NO_WALL
e4b61340
LP
1592 };
1593
1594 static const struct option options[] = {
1595 { "help", no_argument, NULL, ARG_HELP },
1596 { "halt", no_argument, NULL, ARG_HALT },
1597 { "poweroff", no_argument, NULL, 'p' },
1598 { "reboot", no_argument, NULL, ARG_REBOOT },
1599 { "force", no_argument, NULL, 'f' },
1600 { "wtmp-only", no_argument, NULL, 'w' },
1601 { "no-wtmp", no_argument, NULL, 'd' },
1602 { "no-sync", no_argument, NULL, 'n' },
514f4ef5 1603 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
1604 { NULL, 0, NULL, 0 }
1605 };
1606
1607 int c, runlevel;
1608
1609 assert(argc >= 0);
1610 assert(argv);
1611
1612 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
1613 if (runlevel == '0' || runlevel == '6')
1614 arg_immediate = true;
1615
1616 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
1617 switch (c) {
1618
1619 case ARG_HELP:
1620 halt_help();
1621 return 0;
1622
1623 case ARG_HALT:
1624 arg_action = ACTION_HALT;
1625 break;
1626
1627 case 'p':
1628 arg_action = ACTION_POWEROFF;
1629 break;
1630
1631 case ARG_REBOOT:
1632 arg_action = ACTION_REBOOT;
1633 break;
1634
1635 case 'f':
1636 arg_immediate = true;
1637 break;
1638
1639 case 'w':
1640 arg_dry = true;
1641 break;
1642
1643 case 'd':
1644 arg_no_wtmp = true;
1645 break;
1646
1647 case 'n':
1648 arg_no_sync = true;
1649 break;
1650
514f4ef5
LP
1651 case ARG_NO_WALL:
1652 arg_no_wall = true;
1653 break;
1654
e4b61340
LP
1655 case 'i':
1656 case 'h':
1657 /* Compatibility nops */
1658 break;
1659
1660 case '?':
1661 return -EINVAL;
1662
1663 default:
1664 log_error("Unknown option code %c", c);
1665 return -EINVAL;
1666 }
1667 }
1668
1669 if (optind < argc) {
1670 log_error("Too many arguments.");
1671 return -EINVAL;
1672 }
1673
1674 return 1;
1675}
1676
1677static int shutdown_parse_argv(int argc, char *argv[]) {
1678
1679 enum {
1680 ARG_HELP = 0x100,
514f4ef5 1681 ARG_NO_WALL
e4b61340
LP
1682 };
1683
1684 static const struct option options[] = {
1685 { "help", no_argument, NULL, ARG_HELP },
1686 { "halt", no_argument, NULL, 'H' },
1687 { "poweroff", no_argument, NULL, 'P' },
1688 { "reboot", no_argument, NULL, 'r' },
514f4ef5 1689 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
1690 { NULL, 0, NULL, 0 }
1691 };
1692
1693 int c;
1694
1695 assert(argc >= 0);
1696 assert(argv);
1697
1698 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
1699 switch (c) {
1700
1701 case ARG_HELP:
1702 shutdown_help();
1703 return 0;
1704
1705 case 'H':
1706 arg_action = ACTION_HALT;
1707 break;
1708
1709 case 'P':
1710 arg_action = ACTION_POWEROFF;
1711 break;
1712
1713 case 'r':
1714 arg_action = ACTION_REBOOT;
1715 break;
1716
1717 case 'h':
1718 if (arg_action != ACTION_HALT)
1719 arg_action = ACTION_POWEROFF;
1720 break;
1721
1722 case 'k':
1723 arg_dry = true;
1724 break;
1725
514f4ef5
LP
1726 case ARG_NO_WALL:
1727 arg_no_wall = true;
1728 break;
1729
e4b61340
LP
1730 case 't':
1731 case 'a':
1732 /* Compatibility nops */
1733 break;
1734
1735 case '?':
1736 return -EINVAL;
1737
1738 default:
1739 log_error("Unknown option code %c", c);
1740 return -EINVAL;
1741 }
1742 }
1743
1744 /* We ignore the time argument */
1745 if (argc > optind + 1)
1746 arg_wall = argv + optind + 1;
1747
1748 optind = argc;
1749
1750 return 1;
1751
1752}
1753
1754static int telinit_parse_argv(int argc, char *argv[]) {
1755
1756 enum {
1757 ARG_HELP = 0x100,
514f4ef5 1758 ARG_NO_WALL
e4b61340
LP
1759 };
1760
1761 static const struct option options[] = {
1762 { "help", no_argument, NULL, ARG_HELP },
514f4ef5 1763 { "no-wall", no_argument, NULL, ARG_NO_WALL },
e4b61340
LP
1764 { NULL, 0, NULL, 0 }
1765 };
1766
1767 static const struct {
1768 char from;
1769 enum action to;
1770 } table[] = {
1771 { '0', ACTION_POWEROFF },
1772 { '6', ACTION_REBOOT },
ef2f1067 1773 { '1', ACTION_RESCUE },
e4b61340
LP
1774 { '2', ACTION_RUNLEVEL2 },
1775 { '3', ACTION_RUNLEVEL3 },
1776 { '4', ACTION_RUNLEVEL4 },
1777 { '5', ACTION_RUNLEVEL5 },
1778 { 's', ACTION_RESCUE },
1779 { 'S', ACTION_RESCUE },
1780 { 'q', ACTION_RELOAD },
1781 { 'Q', ACTION_RELOAD },
1782 { 'u', ACTION_REEXEC },
1783 { 'U', ACTION_REEXEC }
1784 };
1785
1786 unsigned i;
1787 int c;
1788
1789 assert(argc >= 0);
1790 assert(argv);
1791
1792 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1793 switch (c) {
1794
1795 case ARG_HELP:
1796 telinit_help();
1797 return 0;
1798
514f4ef5
LP
1799 case ARG_NO_WALL:
1800 arg_no_wall = true;
1801 break;
1802
e4b61340
LP
1803 case '?':
1804 return -EINVAL;
1805
1806 default:
1807 log_error("Unknown option code %c", c);
1808 return -EINVAL;
1809 }
1810 }
1811
1812 if (optind >= argc) {
1813 log_error("Argument missing.");
1814 return -EINVAL;
1815 }
1816
1817 if (optind + 1 < argc) {
1818 log_error("Too many arguments.");
1819 return -EINVAL;
1820 }
1821
1822 if (strlen(argv[optind]) != 1) {
1823 log_error("Expected single character argument.");
1824 return -EINVAL;
1825 }
1826
1827 for (i = 0; i < ELEMENTSOF(table); i++)
1828 if (table[i].from == argv[optind][0])
1829 break;
1830
1831 if (i >= ELEMENTSOF(table)) {
1832 log_error("Unknown command %s.", argv[optind]);
1833 return -EINVAL;
1834 }
1835
1836 arg_action = table[i].to;
1837
1838 optind ++;
1839
1840 return 1;
1841}
1842
1843static int runlevel_parse_argv(int argc, char *argv[]) {
1844
1845 enum {
1846 ARG_HELP = 0x100,
1847 };
1848
1849 static const struct option options[] = {
1850 { "help", no_argument, NULL, ARG_HELP },
1851 { NULL, 0, NULL, 0 }
1852 };
1853
1854 int c;
1855
1856 assert(argc >= 0);
1857 assert(argv);
1858
1859 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1860 switch (c) {
1861
1862 case ARG_HELP:
1863 runlevel_help();
1864 return 0;
1865
1866 case '?':
1867 return -EINVAL;
1868
1869 default:
1870 log_error("Unknown option code %c", c);
1871 return -EINVAL;
1872 }
1873 }
1874
1875 if (optind < argc) {
1876 log_error("Too many arguments.");
1877 return -EINVAL;
1878 }
1879
1880 return 1;
1881}
1882
1883static int parse_argv(int argc, char *argv[]) {
1884 assert(argc >= 0);
1885 assert(argv);
1886
1887 if (program_invocation_short_name) {
1888
1889 if (strstr(program_invocation_short_name, "halt")) {
1890 arg_action = ACTION_HALT;
1891 return halt_parse_argv(argc, argv);
1892 } else if (strstr(program_invocation_short_name, "poweroff")) {
1893 arg_action = ACTION_POWEROFF;
1894 return halt_parse_argv(argc, argv);
1895 } else if (strstr(program_invocation_short_name, "reboot")) {
1896 arg_action = ACTION_REBOOT;
1897 return halt_parse_argv(argc, argv);
1898 } else if (strstr(program_invocation_short_name, "shutdown")) {
1899 arg_action = ACTION_POWEROFF;
1900 return shutdown_parse_argv(argc, argv);
1901 } else if (strstr(program_invocation_short_name, "init")) {
1902 arg_action = ACTION_INVALID;
1903 return telinit_parse_argv(argc, argv);
1904 } else if (strstr(program_invocation_short_name, "runlevel")) {
1905 arg_action = ACTION_RUNLEVEL;
1906 return runlevel_parse_argv(argc, argv);
1907 }
1908 }
1909
1910 arg_action = ACTION_SYSTEMCTL;
1911 return systemctl_parse_argv(argc, argv);
1912}
1913
d55ae9e6 1914static int action_to_runlevel(void) {
eb22ac37
LP
1915
1916 static const char table[_ACTION_MAX] = {
1917 [ACTION_HALT] = '0',
1918 [ACTION_POWEROFF] = '0',
1919 [ACTION_REBOOT] = '6',
1920 [ACTION_RUNLEVEL2] = '2',
1921 [ACTION_RUNLEVEL3] = '3',
1922 [ACTION_RUNLEVEL4] = '4',
1923 [ACTION_RUNLEVEL5] = '5',
1924 [ACTION_RESCUE] = '1'
1925 };
1926
d55ae9e6
LP
1927 assert(arg_action < _ACTION_MAX);
1928
1929 return table[arg_action];
1930}
1931
1932static int talk_upstart(DBusConnection *bus) {
1933 DBusMessage *m = NULL, *reply = NULL;
1934 DBusError error;
1935 int previous, rl, r;
1936 char
1937 env1_buf[] = "RUNLEVEL=X",
1938 env2_buf[] = "PREVLEVEL=X";
1939 char *env1 = env1_buf, *env2 = env2_buf;
1940 const char *emit = "runlevel";
1941 dbus_bool_t b_false = FALSE;
1942 DBusMessageIter iter, sub;
1943
1944 dbus_error_init(&error);
1945
1946 if (!(rl = action_to_runlevel()))
1947 return 0;
1948
1949 if (utmp_get_runlevel(&previous, NULL) < 0)
1950 previous = 'N';
1951
1952 if (!(m = dbus_message_new_method_call(
1953 "com.ubuntu.Upstart",
1954 "/com/ubuntu/Upstart",
1955 "com.ubuntu.Upstart0_6",
1956 "EmitEvent"))) {
1957
1958 log_error("Could not allocate message.");
1959 return -ENOMEM;
1960 }
1961
1962 dbus_message_iter_init_append(m, &iter);
1963
1964 env1_buf[sizeof(env1_buf)-2] = rl;
1965 env2_buf[sizeof(env2_buf)-2] = previous;
1966
1967 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
1968 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
1969 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
1970 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
1971 !dbus_message_iter_close_container(&iter, &sub) ||
1972 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
1973 log_error("Could not append arguments to message.");
1974 r = -ENOMEM;
1975 goto finish;
1976 }
1977
1978 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1979
1980 if (error_is_no_service(&error)) {
1981 r = 0;
1982 goto finish;
1983 }
1984
1985 log_error("Failed to issue method call: %s", error.message);
1986 r = -EIO;
1987 goto finish;
1988 }
1989
1990 r = 1;
1991
1992finish:
1993 if (m)
1994 dbus_message_unref(m);
1995
1996 if (reply)
1997 dbus_message_unref(reply);
1998
1999 dbus_error_free(&error);
2000
2001 return r;
2002}
2003
2004static int talk_initctl(void) {
eb22ac37
LP
2005 struct init_request request;
2006 int r, fd;
d55ae9e6 2007 char rl;
eb22ac37 2008
d55ae9e6 2009 if (!(rl = action_to_runlevel()))
eb22ac37
LP
2010 return 0;
2011
2012 zero(request);
2013 request.magic = INIT_MAGIC;
2014 request.sleeptime = 0;
2015 request.cmd = INIT_CMD_RUNLVL;
d55ae9e6
LP
2016 request.runlevel = rl;
2017
2018 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2019
2020 if (errno == ENOENT)
2021 return 0;
eb22ac37 2022
d55ae9e6 2023 log_error("Failed to open "INIT_FIFO": %m");
eb22ac37 2024 return -errno;
d55ae9e6 2025 }
eb22ac37 2026
d55ae9e6 2027 errno = 0;
eb22ac37
LP
2028 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2029 close_nointr_nofail(fd);
2030
d55ae9e6
LP
2031 if (r < 0) {
2032 log_error("Failed to write to "INIT_FIFO": %m");
eb22ac37 2033 return errno ? -errno : -EIO;
d55ae9e6 2034 }
eb22ac37
LP
2035
2036 return 1;
e4b61340
LP
2037}
2038
2039static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
7e4249b9 2040
7e4249b9
LP
2041 static const struct {
2042 const char* verb;
2043 const enum {
2044 MORE,
2045 LESS,
2046 EQUAL
2047 } argc_cmp;
2048 const int argc;
2049 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
2050 } verbs[] = {
2051 { "list-units", LESS, 1, list_units },
2052 { "list-jobs", EQUAL, 1, list_jobs },
2053 { "clear-jobs", EQUAL, 1, clear_jobs },
2054 { "load", MORE, 2, load_unit },
2055 { "cancel", MORE, 2, cancel_job },
2056 { "start", MORE, 2, start_unit },
2057 { "stop", MORE, 2, start_unit },
2058 { "reload", MORE, 2, start_unit },
2059 { "restart", MORE, 2, start_unit },
e4b61340 2060 { "isolate", EQUAL, 2, start_unit },
7e4249b9
LP
2061 { "monitor", EQUAL, 1, monitor },
2062 { "dump", EQUAL, 1, dump },
2063 { "snapshot", LESS, 2, snapshot },
2064 { "daemon-reload", EQUAL, 1, clear_jobs },
2065 { "daemon-reexec", EQUAL, 1, clear_jobs },
2066 { "daemon-exit", EQUAL, 1, clear_jobs },
2067 { "show-environment", EQUAL, 1, show_enviroment },
2068 { "set-environment", MORE, 2, set_environment },
2069 { "unset-environment", MORE, 2, set_environment },
514f4ef5
LP
2070 { "halt", EQUAL, 1, start_special },
2071 { "poweroff", EQUAL, 1, start_special },
2072 { "reboot", EQUAL, 1, start_special },
2073 { "default", EQUAL, 1, start_special },
2074 { "rescue", EQUAL, 1, start_special },
2075 { "emergency", EQUAL, 1, start_special },
7e4249b9
LP
2076 };
2077
e4b61340 2078 int left;
7e4249b9 2079 unsigned i;
7e4249b9 2080
e4b61340
LP
2081 assert(bus);
2082 assert(argc >= 0);
2083 assert(argv);
7e4249b9
LP
2084
2085 left = argc - optind;
2086
2087 if (left <= 0)
2088 /* Special rule: no arguments means "list-units" */
2089 i = 0;
2090 else {
2091 for (i = 0; i < ELEMENTSOF(verbs); i++)
2092 if (streq(argv[optind], verbs[i].verb))
2093 break;
2094
2095 if (i >= ELEMENTSOF(verbs)) {
2096 log_error("Unknown operation %s", argv[optind]);
e4b61340 2097 return -EINVAL;
7e4249b9
LP
2098 }
2099 }
2100
2101 switch (verbs[i].argc_cmp) {
2102
2103 case EQUAL:
2104 if (left != verbs[i].argc) {
2105 log_error("Invalid number of arguments.");
e4b61340 2106 return -EINVAL;
7e4249b9
LP
2107 }
2108
2109 break;
2110
2111 case MORE:
2112 if (left < verbs[i].argc) {
2113 log_error("Too few arguments.");
e4b61340 2114 return -EINVAL;
7e4249b9
LP
2115 }
2116
2117 break;
2118
2119 case LESS:
2120 if (left > verbs[i].argc) {
2121 log_error("Too many arguments.");
e4b61340 2122 return -EINVAL;
7e4249b9
LP
2123 }
2124
2125 break;
2126
2127 default:
2128 assert_not_reached("Unknown comparison operator.");
2129 }
2130
e4b61340
LP
2131 return verbs[i].dispatch(bus, argv + optind, left);
2132}
2133
2134static int reload_with_fallback(DBusConnection *bus) {
2135 int r;
2136
2137 if (bus) {
2138 /* First, try systemd via D-Bus. */
2139 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2140 return 0;
2141 }
2142
2143 /* Nothing else worked, so let's try signals */
2144 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2145
2146 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2147 log_error("kill() failed: %m");
2148 return -errno;
2149 }
2150
2151 return 0;
2152}
2153
2154static int start_with_fallback(DBusConnection *bus) {
2155 int r;
2156
514f4ef5 2157 warn_wall(arg_action);
ef2f1067 2158
e4b61340
LP
2159 if (bus) {
2160 /* First, try systemd via D-Bus. */
2161 if ((r = start_unit(bus, NULL, 0)) > 0)
2162 return 0;
2163
2164 /* Hmm, talking to systemd via D-Bus didn't work. Then
2165 * let's try to talk to Upstart via D-Bus. */
2166 if ((r = talk_upstart(bus)) > 0)
2167 return 0;
2168 }
2169
2170 /* Nothing else worked, so let's try
2171 * /dev/initctl */
d55ae9e6
LP
2172 if ((r = talk_initctl()) != 0)
2173 return 0;
2174
2175 log_error("Failed to talk to init daemon.");
2176 return -EIO;
e4b61340
LP
2177}
2178
2179static int halt_main(DBusConnection *bus) {
2180 int r;
2181
2182 if (!arg_immediate)
2183 return start_with_fallback(bus);
2184
2185 if (!arg_no_wtmp)
2186 if ((r = utmp_put_shutdown(0)) < 0)
2187 log_warning("Failed to write utmp record: %s", strerror(-r));
2188
2189 if (!arg_no_sync)
2190 sync();
2191
2192 if (arg_dry)
2193 return 0;
2194
2195 /* Make sure C-A-D is handled by the kernel from this
2196 * point on... */
2197 reboot(RB_ENABLE_CAD);
2198
2199 switch (arg_action) {
2200
2201 case ACTION_HALT:
2202 log_info("Halting");
2203 reboot(RB_HALT_SYSTEM);
2204 break;
2205
2206 case ACTION_POWEROFF:
2207 log_info("Powering off");
2208 reboot(RB_POWER_OFF);
2209 break;
2210
2211 case ACTION_REBOOT:
2212 log_info("Rebooting");
2213 reboot(RB_AUTOBOOT);
2214 break;
2215
2216 default:
2217 assert_not_reached("Unknown halt action.");
2218 }
2219
2220 /* We should never reach this. */
2221 return -ENOSYS;
2222}
2223
2224static int runlevel_main(void) {
2225 int r, runlevel, previous;
2226
2227 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2228 printf("unknown");
2229 return r;
2230 }
2231
2232 printf("%c %c\n",
2233 previous <= 0 ? 'N' : previous,
2234 runlevel <= 0 ? 'N' : runlevel);
2235
2236 return 0;
2237}
2238
2239int main(int argc, char*argv[]) {
2240 int r, retval = 1;
2241 DBusConnection *bus = NULL;
2242 DBusError error;
2243
2244 dbus_error_init(&error);
2245
2246 log_parse_environment();
2247
2248 if ((r = parse_argv(argc, argv)) < 0)
2249 goto finish;
2250 else if (r == 0) {
2251 retval = 0;
7e4249b9
LP
2252 goto finish;
2253 }
2254
e4b61340
LP
2255 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2256 * let's shortcut this */
2257 if (arg_action == ACTION_RUNLEVEL) {
2258 retval = runlevel_main() < 0;
2259 goto finish;
2260 }
2261
2262 if ((bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error)))
2263 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2264
2265 switch (arg_action) {
2266
2267 case ACTION_SYSTEMCTL: {
2268
2269 if (!bus) {
2270 log_error("Failed to get D-Bus connection: %s", error.message);
2271 goto finish;
2272 }
2273
2274 retval = systemctl_main(bus, argc, argv) < 0;
2275 break;
2276 }
2277
2278 case ACTION_HALT:
2279 case ACTION_POWEROFF:
2280 case ACTION_REBOOT:
2281 retval = halt_main(bus) < 0;
2282 break;
2283
e4b61340
LP
2284 case ACTION_RUNLEVEL2:
2285 case ACTION_RUNLEVEL3:
2286 case ACTION_RUNLEVEL4:
2287 case ACTION_RUNLEVEL5:
2288 case ACTION_RESCUE:
514f4ef5 2289 case ACTION_EMERGENCY:
eb22ac37 2290 case ACTION_DEFAULT:
e4b61340
LP
2291 retval = start_with_fallback(bus) < 0;
2292 break;
7e4249b9 2293
e4b61340
LP
2294 case ACTION_RELOAD:
2295 case ACTION_REEXEC:
2296 retval = reload_with_fallback(bus) < 0;
2297 break;
2298
eb22ac37
LP
2299 case ACTION_INVALID:
2300 case ACTION_RUNLEVEL:
e4b61340
LP
2301 default:
2302 assert_not_reached("Unknown action");
2303 }
7e4249b9
LP
2304
2305finish:
2306
2307 if (bus)
2308 dbus_connection_unref(bus);
2309
e4b61340
LP
2310 dbus_error_free(&error);
2311
7e4249b9
LP
2312 dbus_shutdown();
2313
2314 return retval;
2315}