1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
36 #include <dbus/dbus.h>
38 #include <systemd/sd-daemon.h>
39 #include <systemd/sd-shutdown.h>
45 #include "utmp-wtmp.h"
48 #include "path-util.h"
50 #include "dbus-common.h"
51 #include "cgroup-show.h"
52 #include "cgroup-util.h"
54 #include "path-lookup.h"
55 #include "conf-parser.h"
56 #include "exit-status.h"
57 #include "bus-errors.h"
59 #include "unit-name.h"
61 #include "spawn-ask-password-agent.h"
62 #include "spawn-polkit-agent.h"
64 #include "logs-show.h"
65 #include "path-util.h"
67 static const char *arg_type
= NULL
;
68 static const char *arg_load_state
= NULL
;
69 static char **arg_property
= NULL
;
70 static bool arg_all
= false;
71 static const char *arg_job_mode
= "replace";
72 static UnitFileScope arg_scope
= UNIT_FILE_SYSTEM
;
73 static bool arg_no_block
= false;
74 static bool arg_no_legend
= false;
75 static bool arg_no_pager
= false;
76 static bool arg_no_wtmp
= false;
77 static bool arg_no_sync
= false;
78 static bool arg_no_wall
= false;
79 static bool arg_no_reload
= false;
80 static bool arg_dry
= false;
81 static bool arg_quiet
= false;
82 static bool arg_full
= false;
83 static int arg_force
= 0;
84 static bool arg_ask_password
= true;
85 static bool arg_failed
= false;
86 static bool arg_runtime
= false;
87 static char **arg_wall
= NULL
;
88 static const char *arg_kill_who
= NULL
;
89 static const char *arg_kill_mode
= NULL
;
90 static int arg_signal
= SIGTERM
;
91 static const char *arg_root
= NULL
;
92 static usec_t arg_when
= 0;
113 ACTION_CANCEL_SHUTDOWN
,
115 } arg_action
= ACTION_SYSTEMCTL
;
121 static enum transport
{
125 } arg_transport
= TRANSPORT_NORMAL
;
126 static const char *arg_host
= NULL
;
127 static bool arg_follow
= false;
128 static unsigned arg_lines
= 10;
129 static OutputMode arg_output
= OUTPUT_SHORT
;
131 static bool private_bus
= false;
133 static int daemon_reload(DBusConnection
*bus
, char **args
);
134 static void halt_now(enum action a
);
136 static bool on_tty(void) {
139 /* Note that this is invoked relatively early, before we start
140 * the pager. That means the value we return reflects whether
141 * we originally were started on a tty, not if we currently
142 * are. But this is intended, since we want colour and so on
143 * when run in our own pager. */
145 if (_unlikely_(t
< 0))
146 t
= isatty(STDOUT_FILENO
) > 0;
151 static void pager_open_if_enabled(void) {
153 /* Cache result before we open the pager */
162 static void ask_password_agent_open_if_enabled(void) {
164 /* Open the password agent as a child process if necessary */
166 if (!arg_ask_password
)
169 if (arg_scope
!= UNIT_FILE_SYSTEM
)
172 ask_password_agent_open();
176 static void polkit_agent_open_if_enabled(void) {
178 /* Open the polkit agent as a child process if necessary */
180 if (!arg_ask_password
)
183 if (arg_scope
!= UNIT_FILE_SYSTEM
)
190 static const char *ansi_highlight_red(bool b
) {
195 return b
? ANSI_HIGHLIGHT_RED_ON
: ANSI_HIGHLIGHT_OFF
;
198 static const char *ansi_highlight_green(bool b
) {
203 return b
? ANSI_HIGHLIGHT_GREEN_ON
: ANSI_HIGHLIGHT_OFF
;
206 static bool error_is_no_service(const DBusError
*error
) {
209 if (!dbus_error_is_set(error
))
212 if (dbus_error_has_name(error
, DBUS_ERROR_NAME_HAS_NO_OWNER
))
215 if (dbus_error_has_name(error
, DBUS_ERROR_SERVICE_UNKNOWN
))
218 return startswith(error
->name
, "org.freedesktop.DBus.Error.Spawn.");
221 static int translate_bus_error_to_exit_status(int r
, const DBusError
*error
) {
224 if (!dbus_error_is_set(error
))
227 if (dbus_error_has_name(error
, DBUS_ERROR_ACCESS_DENIED
) ||
228 dbus_error_has_name(error
, BUS_ERROR_ONLY_BY_DEPENDENCY
) ||
229 dbus_error_has_name(error
, BUS_ERROR_NO_ISOLATION
) ||
230 dbus_error_has_name(error
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
))
231 return EXIT_NOPERMISSION
;
233 if (dbus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
))
234 return EXIT_NOTINSTALLED
;
236 if (dbus_error_has_name(error
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
) ||
237 dbus_error_has_name(error
, BUS_ERROR_NOT_SUPPORTED
))
238 return EXIT_NOTIMPLEMENTED
;
240 if (dbus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
))
241 return EXIT_NOTCONFIGURED
;
249 static void warn_wall(enum action a
) {
250 static const char *table
[_ACTION_MAX
] = {
251 [ACTION_HALT
] = "The system is going down for system halt NOW!",
252 [ACTION_REBOOT
] = "The system is going down for reboot NOW!",
253 [ACTION_POWEROFF
] = "The system is going down for power-off NOW!",
254 [ACTION_KEXEC
] = "The system is going down for kexec reboot NOW!",
255 [ACTION_RESCUE
] = "The system is going down to rescue mode NOW!",
256 [ACTION_EMERGENCY
] = "The system is going down to emergency mode NOW!"
265 p
= strv_join(arg_wall
, " ");
267 log_error("Failed to join strings.");
283 utmp_wall(table
[a
], NULL
);
286 static bool avoid_bus(void) {
288 if (running_in_chroot() > 0)
291 if (sd_booted() <= 0)
294 if (!isempty(arg_root
))
297 if (arg_scope
== UNIT_FILE_GLOBAL
)
305 const char *description
;
306 const char *load_state
;
307 const char *active_state
;
308 const char *sub_state
;
309 const char *following
;
310 const char *unit_path
;
312 const char *job_type
;
313 const char *job_path
;
316 static int compare_unit_info(const void *a
, const void *b
) {
318 const struct unit_info
*u
= a
, *v
= b
;
320 d1
= strrchr(u
->id
, '.');
321 d2
= strrchr(v
->id
, '.');
326 if ((r
= strcasecmp(d1
, d2
)) != 0)
330 return strcasecmp(u
->id
, v
->id
);
333 static bool output_show_unit(const struct unit_info
*u
) {
337 return streq(u
->active_state
, "failed");
339 return (!arg_type
|| ((dot
= strrchr(u
->id
, '.')) &&
340 streq(dot
+1, arg_type
))) &&
341 (!arg_load_state
|| streq(u
->load_state
, arg_load_state
)) &&
342 (arg_all
|| !(streq(u
->active_state
, "inactive")
343 || u
->following
[0]) || u
->job_id
> 0);
346 static void output_units_list(const struct unit_info
*unit_infos
, unsigned c
) {
347 unsigned id_len
, max_id_len
, active_len
, sub_len
, job_len
, desc_len
, n_shown
= 0;
348 const struct unit_info
*u
;
350 max_id_len
= sizeof("UNIT")-1;
351 active_len
= sizeof("ACTIVE")-1;
352 sub_len
= sizeof("SUB")-1;
353 job_len
= sizeof("JOB")-1;
356 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
357 if (!output_show_unit(u
))
360 max_id_len
= MAX(max_id_len
, strlen(u
->id
));
361 active_len
= MAX(active_len
, strlen(u
->active_state
));
362 sub_len
= MAX(sub_len
, strlen(u
->sub_state
));
364 job_len
= MAX(job_len
, strlen(u
->job_type
));
369 id_len
= MIN(max_id_len
, 25);
370 basic_len
= 5 + id_len
+ 6 + active_len
+ sub_len
+ job_len
;
371 if (basic_len
< (unsigned) columns()) {
372 unsigned extra_len
, incr
;
373 extra_len
= columns() - basic_len
;
374 /* Either UNIT already got 25, or is fully satisfied.
375 * Grant up to 25 to DESC now. */
376 incr
= MIN(extra_len
, 25);
379 /* split the remaining space between UNIT and DESC,
380 * but do not give UNIT more than it needs. */
382 incr
= MIN(extra_len
/ 2, max_id_len
- id_len
);
384 desc_len
+= extra_len
- incr
;
390 if (!arg_no_legend
) {
391 printf("%-*s %-6s %-*s %-*s %-*s ", id_len
, "UNIT", "LOAD",
392 active_len
, "ACTIVE", sub_len
, "SUB", job_len
, "JOB");
393 if (!arg_full
&& arg_no_pager
)
394 printf("%.*s\n", desc_len
, "DESCRIPTION");
396 printf("%s\n", "DESCRIPTION");
399 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
401 const char *on_loaded
, *off_loaded
;
402 const char *on_active
, *off_active
;
404 if (!output_show_unit(u
))
409 if (streq(u
->load_state
, "error")) {
410 on_loaded
= ansi_highlight_red(true);
411 off_loaded
= ansi_highlight_red(false);
413 on_loaded
= off_loaded
= "";
415 if (streq(u
->active_state
, "failed")) {
416 on_active
= ansi_highlight_red(true);
417 off_active
= ansi_highlight_red(false);
419 on_active
= off_active
= "";
421 e
= arg_full
? NULL
: ellipsize(u
->id
, id_len
, 33);
423 printf("%-*s %s%-6s%s %s%-*s %-*s%s %-*s ",
424 id_len
, e
? e
: u
->id
,
425 on_loaded
, u
->load_state
, off_loaded
,
426 on_active
, active_len
, u
->active_state
,
427 sub_len
, u
->sub_state
, off_active
,
428 job_len
, u
->job_id
? u
->job_type
: "");
429 if (!arg_full
&& arg_no_pager
)
430 printf("%.*s\n", desc_len
, u
->description
);
432 printf("%s\n", u
->description
);
437 if (!arg_no_legend
) {
438 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
439 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
440 "SUB = The low-level unit activation state, values depend on unit type.\n"
441 "JOB = Pending job for the unit.\n");
444 printf("\n%u units listed.\n", n_shown
);
446 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown
);
450 static int list_units(DBusConnection
*bus
, char **args
) {
451 DBusMessage
*m
= NULL
, *reply
= NULL
;
454 DBusMessageIter iter
, sub
, sub2
;
455 unsigned c
= 0, n_units
= 0;
456 struct unit_info
*unit_infos
= NULL
;
458 dbus_error_init(&error
);
462 pager_open_if_enabled();
464 if (!(m
= dbus_message_new_method_call(
465 "org.freedesktop.systemd1",
466 "/org/freedesktop/systemd1",
467 "org.freedesktop.systemd1.Manager",
469 log_error("Could not allocate message.");
473 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
474 log_error("Failed to issue method call: %s", bus_error_message(&error
));
479 if (!dbus_message_iter_init(reply
, &iter
) ||
480 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
481 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
482 log_error("Failed to parse reply.");
487 dbus_message_iter_recurse(&iter
, &sub
);
489 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
492 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
493 log_error("Failed to parse reply.");
501 n_units
= MAX(2*c
, 16);
502 w
= realloc(unit_infos
, sizeof(struct unit_info
) * n_units
);
505 log_error("Failed to allocate unit array.");
515 dbus_message_iter_recurse(&sub
, &sub2
);
517 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->id
, true) < 0 ||
518 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->description
, true) < 0 ||
519 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->load_state
, true) < 0 ||
520 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->active_state
, true) < 0 ||
521 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->sub_state
, true) < 0 ||
522 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->following
, true) < 0 ||
523 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->unit_path
, true) < 0 ||
524 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &u
->job_id
, true) < 0 ||
525 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->job_type
, true) < 0 ||
526 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->job_path
, false) < 0) {
527 log_error("Failed to parse reply.");
532 dbus_message_iter_next(&sub
);
537 qsort(unit_infos
, c
, sizeof(struct unit_info
), compare_unit_info
);
538 output_units_list(unit_infos
, c
);
545 dbus_message_unref(m
);
548 dbus_message_unref(reply
);
552 dbus_error_free(&error
);
557 static int compare_unit_file_list(const void *a
, const void *b
) {
559 const UnitFileList
*u
= a
, *v
= b
;
561 d1
= strrchr(u
->path
, '.');
562 d2
= strrchr(v
->path
, '.');
567 r
= strcasecmp(d1
, d2
);
572 return strcasecmp(path_get_file_name(u
->path
), path_get_file_name(v
->path
));
575 static bool output_show_unit_file(const UnitFileList
*u
) {
578 return !arg_type
|| ((dot
= strrchr(u
->path
, '.')) && streq(dot
+1, arg_type
));
581 static void output_unit_file_list(const UnitFileList
*units
, unsigned c
) {
582 unsigned max_id_len
, id_cols
, state_cols
, n_shown
= 0;
583 const UnitFileList
*u
;
585 max_id_len
= sizeof("UNIT FILE")-1;
586 state_cols
= sizeof("STATE")-1;
587 for (u
= units
; u
< units
+ c
; u
++) {
588 if (!output_show_unit_file(u
))
591 max_id_len
= MAX(max_id_len
, strlen(path_get_file_name(u
->path
)));
592 state_cols
= MAX(state_cols
, strlen(unit_file_state_to_string(u
->state
)));
597 id_cols
= MIN(max_id_len
, 25);
598 basic_cols
= 1 + id_cols
+ state_cols
;
599 if (basic_cols
< (unsigned) columns())
600 id_cols
+= MIN(columns() - basic_cols
, max_id_len
- id_cols
);
602 id_cols
= max_id_len
;
605 printf("%-*s %-*s\n", id_cols
, "UNIT FILE", state_cols
, "STATE");
607 for (u
= units
; u
< units
+ c
; u
++) {
609 const char *on
, *off
;
612 if (!output_show_unit_file(u
))
617 if (u
->state
== UNIT_FILE_MASKED
||
618 u
->state
== UNIT_FILE_MASKED_RUNTIME
||
619 u
->state
== UNIT_FILE_DISABLED
) {
620 on
= ansi_highlight_red(true);
621 off
= ansi_highlight_red(false);
622 } else if (u
->state
== UNIT_FILE_ENABLED
) {
623 on
= ansi_highlight_green(true);
624 off
= ansi_highlight_green(false);
628 id
= path_get_file_name(u
->path
);
630 e
= arg_full
? NULL
: ellipsize(id
, id_cols
, 33);
632 printf("%-*s %s%-*s%s\n",
634 on
, state_cols
, unit_file_state_to_string(u
->state
), off
);
640 printf("\n%u unit files listed.\n", n_shown
);
643 static int list_unit_files(DBusConnection
*bus
, char **args
) {
644 DBusMessage
*m
= NULL
, *reply
= NULL
;
647 DBusMessageIter iter
, sub
, sub2
;
648 unsigned c
= 0, n_units
= 0;
649 UnitFileList
*units
= NULL
;
651 dbus_error_init(&error
);
653 pager_open_if_enabled();
660 h
= hashmap_new(string_hash_func
, string_compare_func
);
662 log_error("Out of memory");
666 r
= unit_file_get_list(arg_scope
, arg_root
, h
);
668 unit_file_list_free(h
);
669 log_error("Failed to get unit file list: %s", strerror(-r
));
673 n_units
= hashmap_size(h
);
674 units
= new(UnitFileList
, n_units
);
676 unit_file_list_free(h
);
677 log_error("Out of memory");
681 HASHMAP_FOREACH(u
, h
, i
) {
682 memcpy(units
+ c
++, u
, sizeof(UnitFileList
));
690 m
= dbus_message_new_method_call(
691 "org.freedesktop.systemd1",
692 "/org/freedesktop/systemd1",
693 "org.freedesktop.systemd1.Manager",
696 log_error("Could not allocate message.");
700 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
702 log_error("Failed to issue method call: %s", bus_error_message(&error
));
707 if (!dbus_message_iter_init(reply
, &iter
) ||
708 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
709 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
710 log_error("Failed to parse reply.");
715 dbus_message_iter_recurse(&iter
, &sub
);
717 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
721 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
722 log_error("Failed to parse reply.");
730 n_units
= MAX(2*c
, 16);
731 w
= realloc(units
, sizeof(struct UnitFileList
) * n_units
);
734 log_error("Failed to allocate unit array.");
744 dbus_message_iter_recurse(&sub
, &sub2
);
746 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->path
, true) < 0 ||
747 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &state
, false) < 0) {
748 log_error("Failed to parse reply.");
753 u
->state
= unit_file_state_from_string(state
);
755 dbus_message_iter_next(&sub
);
761 qsort(units
, c
, sizeof(UnitFileList
), compare_unit_file_list
);
762 output_unit_file_list(units
, c
);
769 dbus_message_unref(m
);
772 dbus_message_unref(reply
);
776 dbus_error_free(&error
);
781 static int dot_one_property(const char *name
, const char *prop
, DBusMessageIter
*iter
) {
782 static const char * const colors
[] = {
783 "Requires", "[color=\"black\"]",
784 "RequiresOverridable", "[color=\"black\"]",
785 "Requisite", "[color=\"darkblue\"]",
786 "RequisiteOverridable", "[color=\"darkblue\"]",
787 "Wants", "[color=\"darkgrey\"]",
788 "Conflicts", "[color=\"red\"]",
789 "ConflictedBy", "[color=\"red\"]",
790 "After", "[color=\"green\"]"
793 const char *c
= NULL
;
800 for (i
= 0; i
< ELEMENTSOF(colors
); i
+= 2)
801 if (streq(colors
[i
], prop
)) {
809 if (arg_dot
!= DOT_ALL
)
810 if ((arg_dot
== DOT_ORDER
) != streq(prop
, "After"))
813 switch (dbus_message_iter_get_arg_type(iter
)) {
815 case DBUS_TYPE_ARRAY
:
817 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
820 dbus_message_iter_recurse(iter
, &sub
);
822 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
825 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
826 dbus_message_iter_get_basic(&sub
, &s
);
827 printf("\t\"%s\"->\"%s\" %s;\n", name
, s
, c
);
829 dbus_message_iter_next(&sub
);
839 static int dot_one(DBusConnection
*bus
, const char *name
, const char *path
) {
840 DBusMessage
*m
= NULL
, *reply
= NULL
;
841 const char *interface
= "org.freedesktop.systemd1.Unit";
844 DBusMessageIter iter
, sub
, sub2
, sub3
;
849 dbus_error_init(&error
);
851 if (!(m
= dbus_message_new_method_call(
852 "org.freedesktop.systemd1",
854 "org.freedesktop.DBus.Properties",
856 log_error("Could not allocate message.");
861 if (!dbus_message_append_args(m
,
862 DBUS_TYPE_STRING
, &interface
,
863 DBUS_TYPE_INVALID
)) {
864 log_error("Could not append arguments to message.");
869 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
870 log_error("Failed to issue method call: %s", bus_error_message(&error
));
875 if (!dbus_message_iter_init(reply
, &iter
) ||
876 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
877 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
878 log_error("Failed to parse reply.");
883 dbus_message_iter_recurse(&iter
, &sub
);
885 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
888 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
889 log_error("Failed to parse reply.");
894 dbus_message_iter_recurse(&sub
, &sub2
);
896 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &prop
, true) < 0) {
897 log_error("Failed to parse reply.");
902 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
903 log_error("Failed to parse reply.");
908 dbus_message_iter_recurse(&sub2
, &sub3
);
910 if (dot_one_property(name
, prop
, &sub3
)) {
911 log_error("Failed to parse reply.");
916 dbus_message_iter_next(&sub
);
923 dbus_message_unref(m
);
926 dbus_message_unref(reply
);
928 dbus_error_free(&error
);
933 static int dot(DBusConnection
*bus
, char **args
) {
934 DBusMessage
*m
= NULL
, *reply
= NULL
;
937 DBusMessageIter iter
, sub
, sub2
;
939 dbus_error_init(&error
);
943 if (!(m
= dbus_message_new_method_call(
944 "org.freedesktop.systemd1",
945 "/org/freedesktop/systemd1",
946 "org.freedesktop.systemd1.Manager",
948 log_error("Could not allocate message.");
952 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
953 log_error("Failed to issue method call: %s", bus_error_message(&error
));
958 if (!dbus_message_iter_init(reply
, &iter
) ||
959 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
960 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
961 log_error("Failed to parse reply.");
966 printf("digraph systemd {\n");
968 dbus_message_iter_recurse(&iter
, &sub
);
969 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
970 const char *id
, *description
, *load_state
, *active_state
, *sub_state
, *following
, *unit_path
;
972 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
973 log_error("Failed to parse reply.");
978 dbus_message_iter_recurse(&sub
, &sub2
);
980 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &id
, true) < 0 ||
981 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &description
, true) < 0 ||
982 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &load_state
, true) < 0 ||
983 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &active_state
, true) < 0 ||
984 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &sub_state
, true) < 0 ||
985 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &following
, true) < 0 ||
986 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, true) < 0) {
987 log_error("Failed to parse reply.");
992 if ((r
= dot_one(bus
, id
, unit_path
)) < 0)
995 /* printf("\t\"%s\";\n", id); */
996 dbus_message_iter_next(&sub
);
1001 log_info(" Color legend: black = Requires\n"
1002 " dark blue = Requisite\n"
1003 " dark grey = Wants\n"
1004 " red = Conflicts\n"
1005 " green = After\n");
1008 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
1009 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
1015 dbus_message_unref(m
);
1018 dbus_message_unref(reply
);
1020 dbus_error_free(&error
);
1025 static int list_jobs(DBusConnection
*bus
, char **args
) {
1026 DBusMessage
*m
= NULL
, *reply
= NULL
;
1029 DBusMessageIter iter
, sub
, sub2
;
1032 dbus_error_init(&error
);
1036 pager_open_if_enabled();
1038 if (!(m
= dbus_message_new_method_call(
1039 "org.freedesktop.systemd1",
1040 "/org/freedesktop/systemd1",
1041 "org.freedesktop.systemd1.Manager",
1043 log_error("Could not allocate message.");
1047 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1048 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1053 if (!dbus_message_iter_init(reply
, &iter
) ||
1054 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
1055 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
1056 log_error("Failed to parse reply.");
1061 dbus_message_iter_recurse(&iter
, &sub
);
1064 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1066 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1067 const char *name
, *type
, *state
, *job_path
, *unit_path
;
1071 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
1072 log_error("Failed to parse reply.");
1077 dbus_message_iter_recurse(&sub
, &sub2
);
1079 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &id
, true) < 0 ||
1080 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0 ||
1081 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) < 0 ||
1082 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &state
, true) < 0 ||
1083 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &job_path
, true) < 0 ||
1084 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, false) < 0) {
1085 log_error("Failed to parse reply.");
1090 e
= arg_full
? NULL
: ellipsize(name
, 25, 33);
1091 printf("%4u %-25s %-15s %-7s\n", id
, e
? e
: name
, type
, state
);
1096 dbus_message_iter_next(&sub
);
1100 printf("\n%u jobs listed.\n", k
);
1106 dbus_message_unref(m
);
1109 dbus_message_unref(reply
);
1111 dbus_error_free(&error
);
1116 static int load_unit(DBusConnection
*bus
, char **args
) {
1117 DBusMessage
*m
= NULL
;
1122 dbus_error_init(&error
);
1127 STRV_FOREACH(name
, args
+1) {
1132 if (!(m
= dbus_message_new_method_call(
1133 "org.freedesktop.systemd1",
1134 "/org/freedesktop/systemd1",
1135 "org.freedesktop.systemd1.Manager",
1137 log_error("Could not allocate message.");
1142 n
= unit_name_mangle(*name
);
1143 b
= dbus_message_append_args(m
,
1144 DBUS_TYPE_STRING
, n
? &n
: name
,
1148 log_error("Could not append arguments to message.");
1153 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
1155 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1160 dbus_message_unref(m
);
1161 dbus_message_unref(reply
);
1170 dbus_message_unref(m
);
1172 dbus_error_free(&error
);
1177 static int cancel_job(DBusConnection
*bus
, char **args
) {
1178 DBusMessage
*m
= NULL
, *reply
= NULL
;
1183 dbus_error_init(&error
);
1188 if (strv_length(args
) <= 1)
1189 return daemon_reload(bus
, args
);
1191 STRV_FOREACH(name
, args
+1) {
1195 if (!(m
= dbus_message_new_method_call(
1196 "org.freedesktop.systemd1",
1197 "/org/freedesktop/systemd1",
1198 "org.freedesktop.systemd1.Manager",
1200 log_error("Could not allocate message.");
1205 if ((r
= safe_atou(*name
, &id
)) < 0) {
1206 log_error("Failed to parse job id: %s", strerror(-r
));
1210 assert_cc(sizeof(uint32_t) == sizeof(id
));
1211 if (!dbus_message_append_args(m
,
1212 DBUS_TYPE_UINT32
, &id
,
1213 DBUS_TYPE_INVALID
)) {
1214 log_error("Could not append arguments to message.");
1219 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1220 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1225 if (!dbus_message_get_args(reply
, &error
,
1226 DBUS_TYPE_OBJECT_PATH
, &path
,
1227 DBUS_TYPE_INVALID
)) {
1228 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1233 dbus_message_unref(m
);
1234 if (!(m
= dbus_message_new_method_call(
1235 "org.freedesktop.systemd1",
1237 "org.freedesktop.systemd1.Job",
1239 log_error("Could not allocate message.");
1244 dbus_message_unref(reply
);
1245 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1246 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1251 dbus_message_unref(m
);
1252 dbus_message_unref(reply
);
1260 dbus_message_unref(m
);
1263 dbus_message_unref(reply
);
1265 dbus_error_free(&error
);
1270 static bool need_daemon_reload(DBusConnection
*bus
, const char *unit
) {
1271 DBusMessage
*m
= NULL
, *reply
= NULL
;
1272 dbus_bool_t b
= FALSE
;
1273 DBusMessageIter iter
, sub
;
1275 *interface
= "org.freedesktop.systemd1.Unit",
1276 *property
= "NeedDaemonReload",
1281 /* We ignore all errors here, since this is used to show a warning only */
1283 m
= dbus_message_new_method_call(
1284 "org.freedesktop.systemd1",
1285 "/org/freedesktop/systemd1",
1286 "org.freedesktop.systemd1.Manager",
1291 n
= unit_name_mangle(unit
);
1292 k
= dbus_message_append_args(m
,
1293 DBUS_TYPE_STRING
, n
? (const char**) &n
: &unit
,
1299 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
);
1303 if (!dbus_message_get_args(reply
, NULL
,
1304 DBUS_TYPE_OBJECT_PATH
, &path
,
1308 dbus_message_unref(m
);
1309 m
= dbus_message_new_method_call(
1310 "org.freedesktop.systemd1",
1312 "org.freedesktop.DBus.Properties",
1317 if (!dbus_message_append_args(m
,
1318 DBUS_TYPE_STRING
, &interface
,
1319 DBUS_TYPE_STRING
, &property
,
1320 DBUS_TYPE_INVALID
)) {
1324 dbus_message_unref(reply
);
1325 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
);
1329 if (!dbus_message_iter_init(reply
, &iter
) ||
1330 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
)
1333 dbus_message_iter_recurse(&iter
, &sub
);
1335 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_BOOLEAN
)
1338 dbus_message_iter_get_basic(&sub
, &b
);
1342 dbus_message_unref(m
);
1345 dbus_message_unref(reply
);
1350 typedef struct WaitData
{
1355 static DBusHandlerResult
wait_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
1363 dbus_error_init(&error
);
1365 log_debug("Got D-Bus request: %s.%s() on %s",
1366 dbus_message_get_interface(message
),
1367 dbus_message_get_member(message
),
1368 dbus_message_get_path(message
));
1370 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
1371 log_error("Warning! D-Bus connection terminated.");
1372 dbus_connection_close(connection
);
1374 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1376 const char *path
, *result
, *unit
;
1377 dbus_bool_t success
= true;
1379 if (dbus_message_get_args(message
, &error
,
1380 DBUS_TYPE_UINT32
, &id
,
1381 DBUS_TYPE_OBJECT_PATH
, &path
,
1382 DBUS_TYPE_STRING
, &unit
,
1383 DBUS_TYPE_STRING
, &result
,
1384 DBUS_TYPE_INVALID
)) {
1387 p
= set_remove(d
->set
, (char*) path
);
1391 d
->result
= strdup(result
);
1396 dbus_error_free(&error
);
1397 if (dbus_message_get_args(message
, &error
,
1398 DBUS_TYPE_UINT32
, &id
,
1399 DBUS_TYPE_OBJECT_PATH
, &path
,
1400 DBUS_TYPE_STRING
, &result
,
1401 DBUS_TYPE_INVALID
)) {
1404 /* Compatibility with older systemd versions <
1405 * 183 during upgrades. This should be dropped
1407 p
= set_remove(d
->set
, (char*) path
);
1411 d
->result
= strdup(result
);
1416 dbus_error_free(&error
);
1417 if (dbus_message_get_args(message
, &error
,
1418 DBUS_TYPE_UINT32
, &id
,
1419 DBUS_TYPE_OBJECT_PATH
, &path
,
1420 DBUS_TYPE_BOOLEAN
, &success
,
1421 DBUS_TYPE_INVALID
)) {
1424 /* Compatibility with older systemd versions <
1425 * 19 during upgrades. This should be dropped
1428 p
= set_remove(d
->set
, (char*) path
);
1432 d
->result
= strdup("failed");
1438 log_error("Failed to parse message: %s", bus_error_message(&error
));
1442 dbus_error_free(&error
);
1443 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1446 static int enable_wait_for_jobs(DBusConnection
*bus
) {
1454 dbus_error_init(&error
);
1455 dbus_bus_add_match(bus
,
1457 "sender='org.freedesktop.systemd1',"
1458 "interface='org.freedesktop.systemd1.Manager',"
1459 "member='JobRemoved',"
1460 "path='/org/freedesktop/systemd1'",
1463 if (dbus_error_is_set(&error
)) {
1464 log_error("Failed to add match: %s", bus_error_message(&error
));
1465 dbus_error_free(&error
);
1469 /* This is slightly dirty, since we don't undo the match registrations. */
1473 static int wait_for_jobs(DBusConnection
*bus
, Set
*s
) {
1483 if (!dbus_connection_add_filter(bus
, wait_filter
, &d
, NULL
)) {
1484 log_error("Failed to add filter.");
1489 while (!set_isempty(s
) &&
1490 dbus_connection_read_write_dispatch(bus
, -1))
1493 if (!arg_quiet
&& d
.result
) {
1494 if (streq(d
.result
, "timeout"))
1495 log_error("Job timed out.");
1496 else if (streq(d
.result
, "canceled"))
1497 log_error("Job canceled.");
1498 else if (streq(d
.result
, "dependency"))
1499 log_error("A dependency job failed. See system journal for details.");
1500 else if (!streq(d
.result
, "done") && !streq(d
.result
, "skipped"))
1501 log_error("Job failed. See system journal and 'systemctl status' for details.");
1504 if (streq_ptr(d
.result
, "timeout"))
1506 else if (streq_ptr(d
.result
, "canceled"))
1508 else if (!streq_ptr(d
.result
, "done") && !streq_ptr(d
.result
, "skipped"))
1516 /* This is slightly dirty, since we don't undo the filter registration. */
1521 static int check_one_unit(DBusConnection
*bus
, char *name
, bool quiet
) {
1522 DBusMessage
*m
= NULL
, *reply
= NULL
;
1524 DBusMessageIter iter
, sub
;
1526 *interface
= "org.freedesktop.systemd1.Unit",
1527 *property
= "ActiveState";
1528 const char *path
= NULL
;
1530 int r
= 3; /* According to LSB: "program is not running" */
1537 dbus_error_init(&error
);
1539 m
= dbus_message_new_method_call(
1540 "org.freedesktop.systemd1",
1541 "/org/freedesktop/systemd1",
1542 "org.freedesktop.systemd1.Manager",
1545 log_error("Could not allocate message.");
1550 n
= unit_name_mangle(name
);
1551 b
= dbus_message_append_args(m
,
1552 DBUS_TYPE_STRING
, n
? &n
: &name
,
1556 log_error("Could not append arguments to message.");
1561 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
1563 /* Hmm, cannot figure out anything about this unit... */
1570 if (!dbus_message_get_args(reply
, &error
,
1571 DBUS_TYPE_OBJECT_PATH
, &path
,
1572 DBUS_TYPE_INVALID
)) {
1573 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1578 dbus_message_unref(m
);
1579 m
= dbus_message_new_method_call(
1580 "org.freedesktop.systemd1",
1582 "org.freedesktop.DBus.Properties",
1585 log_error("Could not allocate message.");
1590 if (!dbus_message_append_args(m
,
1591 DBUS_TYPE_STRING
, &interface
,
1592 DBUS_TYPE_STRING
, &property
,
1593 DBUS_TYPE_INVALID
)) {
1594 log_error("Could not append arguments to message.");
1599 dbus_message_unref(reply
);
1600 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
1602 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1607 if (!dbus_message_iter_init(reply
, &iter
) ||
1608 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
1609 log_error("Failed to parse reply.");
1614 dbus_message_iter_recurse(&iter
, &sub
);
1616 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
1617 log_error("Failed to parse reply.");
1622 dbus_message_iter_get_basic(&sub
, &state
);
1627 if (streq(state
, "active") || streq(state
, "reloading"))
1632 dbus_message_unref(m
);
1635 dbus_message_unref(reply
);
1637 dbus_error_free(&error
);
1642 static void check_triggering_units(
1643 DBusConnection
*bus
,
1644 const char *unit_name
) {
1647 DBusMessage
*m
= NULL
, *reply
= NULL
;
1648 DBusMessageIter iter
, sub
;
1649 char *service_trigger
= NULL
;
1650 const char *interface
= "org.freedesktop.systemd1.Unit",
1651 *triggered_by_property
= "TriggeredBy";
1653 char *unit_path
= NULL
, *n
= NULL
;
1654 bool print_warning_label
= true;
1656 dbus_error_init(&error
);
1658 n
= unit_name_mangle(unit_name
);
1659 unit_path
= unit_dbus_path_from_name(n
? n
: unit_name
);
1662 log_error("Could not allocate dbus path.");
1666 m
= dbus_message_new_method_call("org.freedesktop.systemd1",
1668 "org.freedesktop.DBus.Properties",
1671 log_error("Could not allocate message.");
1675 if (!dbus_message_append_args(m
,
1676 DBUS_TYPE_STRING
, &interface
,
1677 DBUS_TYPE_STRING
, &triggered_by_property
,
1678 DBUS_TYPE_INVALID
)) {
1679 log_error("Could not append arguments to message.");
1683 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
1685 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1689 if (!dbus_message_iter_init(reply
, &iter
) ||
1690 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
1691 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1696 dbus_message_iter_recurse(&iter
, &sub
);
1697 dbus_message_iter_recurse(&sub
, &iter
);
1700 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1703 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
1704 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1708 dbus_message_iter_get_basic(&sub
, &service_trigger
);
1710 r
= check_one_unit(bus
, service_trigger
, true);
1714 if (print_warning_label
) {
1715 log_warning("Warning: Stopping %s, but it can still be activated by:", unit_name
);
1716 print_warning_label
= false;
1718 log_warning(" %s", service_trigger
);
1721 dbus_message_iter_next(&sub
);
1725 dbus_message_unref(m
);
1728 dbus_message_unref(reply
);
1730 dbus_error_free(&error
);
1735 static int start_unit_one(
1736 DBusConnection
*bus
,
1743 DBusMessage
*m
= NULL
, *reply
= NULL
;
1754 assert(arg_no_block
|| s
);
1756 m
= dbus_message_new_method_call(
1757 "org.freedesktop.systemd1",
1758 "/org/freedesktop/systemd1",
1759 "org.freedesktop.systemd1.Manager",
1762 log_error("Could not allocate message.");
1767 n
= unit_name_mangle(name
);
1768 b
= dbus_message_append_args(m
,
1769 DBUS_TYPE_STRING
, n
? (const char **) &n
: &name
,
1770 DBUS_TYPE_STRING
, &mode
,
1774 log_error("Could not append arguments to message.");
1779 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, error
);
1782 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(error
)) {
1783 /* There's always a fallback possible for
1784 * legacy actions. */
1789 log_error("Failed to issue method call: %s", bus_error_message(error
));
1794 if (!dbus_message_get_args(reply
, error
,
1795 DBUS_TYPE_OBJECT_PATH
, &path
,
1796 DBUS_TYPE_INVALID
)) {
1797 log_error("Failed to parse reply: %s", bus_error_message(error
));
1802 if (need_daemon_reload(bus
, name
))
1803 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1804 arg_scope
== UNIT_FILE_SYSTEM
? "--system" : "--user");
1806 if (!arg_no_block
) {
1809 if (!(p
= strdup(path
))) {
1810 log_error("Failed to duplicate path.");
1815 if ((r
= set_put(s
, p
)) < 0) {
1817 log_error("Failed to add path to set.");
1822 /* When stopping a unit warn if it can still be triggered by
1823 * another active unit (socket, path, timer) */
1824 if (!arg_quiet
&& streq(method
, "StopUnit"))
1825 check_triggering_units(bus
, name
);
1831 dbus_message_unref(m
);
1834 dbus_message_unref(reply
);
1839 static enum action
verb_to_action(const char *verb
) {
1840 if (streq(verb
, "halt"))
1842 else if (streq(verb
, "poweroff"))
1843 return ACTION_POWEROFF
;
1844 else if (streq(verb
, "reboot"))
1845 return ACTION_REBOOT
;
1846 else if (streq(verb
, "kexec"))
1847 return ACTION_KEXEC
;
1848 else if (streq(verb
, "rescue"))
1849 return ACTION_RESCUE
;
1850 else if (streq(verb
, "emergency"))
1851 return ACTION_EMERGENCY
;
1852 else if (streq(verb
, "default"))
1853 return ACTION_DEFAULT
;
1854 else if (streq(verb
, "exit"))
1856 else if (streq(verb
, "suspend"))
1857 return ACTION_SUSPEND
;
1858 else if (streq(verb
, "hibernate"))
1859 return ACTION_HIBERNATE
;
1861 return ACTION_INVALID
;
1864 static int start_unit(DBusConnection
*bus
, char **args
) {
1866 static const char * const table
[_ACTION_MAX
] = {
1867 [ACTION_HALT
] = SPECIAL_HALT_TARGET
,
1868 [ACTION_POWEROFF
] = SPECIAL_POWEROFF_TARGET
,
1869 [ACTION_REBOOT
] = SPECIAL_REBOOT_TARGET
,
1870 [ACTION_KEXEC
] = SPECIAL_KEXEC_TARGET
,
1871 [ACTION_RUNLEVEL2
] = SPECIAL_RUNLEVEL2_TARGET
,
1872 [ACTION_RUNLEVEL3
] = SPECIAL_RUNLEVEL3_TARGET
,
1873 [ACTION_RUNLEVEL4
] = SPECIAL_RUNLEVEL4_TARGET
,
1874 [ACTION_RUNLEVEL5
] = SPECIAL_RUNLEVEL5_TARGET
,
1875 [ACTION_RESCUE
] = SPECIAL_RESCUE_TARGET
,
1876 [ACTION_EMERGENCY
] = SPECIAL_EMERGENCY_TARGET
,
1877 [ACTION_DEFAULT
] = SPECIAL_DEFAULT_TARGET
,
1878 [ACTION_EXIT
] = SPECIAL_EXIT_TARGET
,
1879 [ACTION_SUSPEND
] = SPECIAL_SUSPEND_TARGET
,
1880 [ACTION_HIBERNATE
] = SPECIAL_HIBERNATE_TARGET
1884 const char *method
, *mode
, *one_name
;
1889 dbus_error_init(&error
);
1893 ask_password_agent_open_if_enabled();
1895 if (arg_action
== ACTION_SYSTEMCTL
) {
1897 streq(args
[0], "stop") ||
1898 streq(args
[0], "condstop") ? "StopUnit" :
1899 streq(args
[0], "reload") ? "ReloadUnit" :
1900 streq(args
[0], "restart") ? "RestartUnit" :
1902 streq(args
[0], "try-restart") ||
1903 streq(args
[0], "condrestart") ? "TryRestartUnit" :
1905 streq(args
[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1907 streq(args
[0], "reload-or-try-restart") ||
1908 streq(args
[0], "condreload") ||
1910 streq(args
[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1914 (streq(args
[0], "isolate") ||
1915 streq(args
[0], "rescue") ||
1916 streq(args
[0], "emergency")) ? "isolate" : arg_job_mode
;
1918 one_name
= table
[verb_to_action(args
[0])];
1921 assert(arg_action
< ELEMENTSOF(table
));
1922 assert(table
[arg_action
]);
1924 method
= "StartUnit";
1926 mode
= (arg_action
== ACTION_EMERGENCY
||
1927 arg_action
== ACTION_RESCUE
||
1928 arg_action
== ACTION_RUNLEVEL2
||
1929 arg_action
== ACTION_RUNLEVEL3
||
1930 arg_action
== ACTION_RUNLEVEL4
||
1931 arg_action
== ACTION_RUNLEVEL5
) ? "isolate" : "replace";
1933 one_name
= table
[arg_action
];
1936 if (!arg_no_block
) {
1937 if ((ret
= enable_wait_for_jobs(bus
)) < 0) {
1938 log_error("Could not watch jobs: %s", strerror(-ret
));
1942 if (!(s
= set_new(string_hash_func
, string_compare_func
))) {
1943 log_error("Failed to allocate set.");
1950 if ((ret
= start_unit_one(bus
, method
, one_name
, mode
, &error
, s
)) <= 0)
1953 STRV_FOREACH(name
, args
+1)
1954 if ((r
= start_unit_one(bus
, method
, *name
, mode
, &error
, s
)) != 0) {
1955 ret
= translate_bus_error_to_exit_status(r
, &error
);
1956 dbus_error_free(&error
);
1961 if ((r
= wait_for_jobs(bus
, s
)) < 0) {
1970 dbus_error_free(&error
);
1975 /* Ask systemd-logind, which might grant access to unprivileged users
1976 * through PolicyKit */
1977 static int reboot_with_logind(DBusConnection
*bus
, enum action a
) {
1980 DBusMessage
*m
= NULL
, *reply
= NULL
;
1982 dbus_bool_t interactive
= true;
1985 dbus_error_init(&error
);
1987 polkit_agent_open_if_enabled();
1995 case ACTION_POWEROFF
:
1996 method
= "PowerOff";
1999 case ACTION_SUSPEND
:
2003 case ACTION_HIBERNATE
:
2004 method
= "Hibernate";
2011 m
= dbus_message_new_method_call(
2012 "org.freedesktop.login1",
2013 "/org/freedesktop/login1",
2014 "org.freedesktop.login1.Manager",
2017 log_error("Could not allocate message.");
2022 if (!dbus_message_append_args(m
,
2023 DBUS_TYPE_BOOLEAN
, &interactive
,
2024 DBUS_TYPE_INVALID
)) {
2025 log_error("Could not append arguments to message.");
2030 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
2032 if (error_is_no_service(&error
)) {
2033 log_debug("Failed to issue method call: %s", bus_error_message(&error
));
2038 if (dbus_error_has_name(&error
, DBUS_ERROR_ACCESS_DENIED
)) {
2039 log_debug("Failed to issue method call: %s", bus_error_message(&error
));
2044 log_info("Failed to issue method call: %s", bus_error_message(&error
));
2053 dbus_message_unref(m
);
2056 dbus_message_unref(reply
);
2058 dbus_error_free(&error
);
2066 static int start_special(DBusConnection
*bus
, char **args
) {
2072 a
= verb_to_action(args
[0]);
2074 if (arg_force
>= 2 && geteuid() != 0) {
2075 log_error("Must be root.");
2079 if (arg_force
>= 2 &&
2080 (a
== ACTION_HALT
||
2081 a
== ACTION_POWEROFF
||
2082 a
== ACTION_REBOOT
))
2085 if (arg_force
>= 1 &&
2086 (a
== ACTION_HALT
||
2087 a
== ACTION_POWEROFF
||
2088 a
== ACTION_REBOOT
||
2089 a
== ACTION_KEXEC
||
2091 return daemon_reload(bus
, args
);
2093 /* first try logind, to allow authentication with polkit */
2094 if (geteuid() != 0 &&
2095 (a
== ACTION_POWEROFF
||
2096 a
== ACTION_REBOOT
||
2097 a
== ACTION_SUSPEND
||
2098 a
== ACTION_HIBERNATE
)) {
2099 r
= reboot_with_logind(bus
, a
);
2104 r
= start_unit(bus
, args
);
2111 static int check_unit(DBusConnection
*bus
, char **args
) {
2113 int r
= 3; /* According to LSB: "program is not running" */
2118 STRV_FOREACH(name
, args
+1) {
2119 int state
= check_one_unit(bus
, *name
, arg_quiet
);
2129 static int kill_unit(DBusConnection
*bus
, char **args
) {
2130 DBusMessage
*m
= NULL
;
2138 dbus_error_init(&error
);
2141 arg_kill_who
= "all";
2144 arg_kill_mode
= streq(arg_kill_who
, "all") ? "control-group" : "process";
2146 STRV_FOREACH(name
, args
+1) {
2151 m
= dbus_message_new_method_call(
2152 "org.freedesktop.systemd1",
2153 "/org/freedesktop/systemd1",
2154 "org.freedesktop.systemd1.Manager",
2157 log_error("Could not allocate message.");
2162 n
= unit_name_mangle(*name
);
2163 b
= dbus_message_append_args(m
,
2164 DBUS_TYPE_STRING
, n
? &n
: name
,
2165 DBUS_TYPE_STRING
, &arg_kill_who
,
2166 DBUS_TYPE_STRING
, &arg_kill_mode
,
2167 DBUS_TYPE_INT32
, &arg_signal
,
2171 log_error("Could not append arguments to message.");
2176 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
2178 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2179 dbus_error_free(&error
);
2183 dbus_message_unref(m
);
2186 dbus_message_unref(reply
);
2192 dbus_message_unref(m
);
2194 dbus_error_free(&error
);
2199 typedef struct ExecStatusInfo
{
2207 usec_t start_timestamp
;
2208 usec_t exit_timestamp
;
2213 LIST_FIELDS(struct ExecStatusInfo
, exec
);
2216 static void exec_status_info_free(ExecStatusInfo
*i
) {
2225 static int exec_status_info_deserialize(DBusMessageIter
*sub
, ExecStatusInfo
*i
) {
2226 uint64_t start_timestamp
, exit_timestamp
, start_timestamp_monotonic
, exit_timestamp_monotonic
;
2227 DBusMessageIter sub2
, sub3
;
2231 int32_t code
, status
;
2237 if (dbus_message_iter_get_arg_type(sub
) != DBUS_TYPE_STRUCT
)
2240 dbus_message_iter_recurse(sub
, &sub2
);
2242 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) < 0)
2245 if (!(i
->path
= strdup(path
)))
2248 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_ARRAY
||
2249 dbus_message_iter_get_element_type(&sub2
) != DBUS_TYPE_STRING
)
2253 dbus_message_iter_recurse(&sub2
, &sub3
);
2254 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
2255 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
2256 dbus_message_iter_next(&sub3
);
2261 if (!(i
->argv
= new0(char*, n
+1)))
2265 dbus_message_iter_recurse(&sub2
, &sub3
);
2266 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
2269 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
2270 dbus_message_iter_get_basic(&sub3
, &s
);
2271 dbus_message_iter_next(&sub3
);
2273 if (!(i
->argv
[n
++] = strdup(s
)))
2277 if (!dbus_message_iter_next(&sub2
) ||
2278 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, true) < 0 ||
2279 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &start_timestamp
, true) < 0 ||
2280 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &start_timestamp_monotonic
, true) < 0 ||
2281 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &exit_timestamp
, true) < 0 ||
2282 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &exit_timestamp_monotonic
, true) < 0 ||
2283 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &pid
, true) < 0 ||
2284 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &code
, true) < 0 ||
2285 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &status
, false) < 0)
2289 i
->start_timestamp
= (usec_t
) start_timestamp
;
2290 i
->exit_timestamp
= (usec_t
) exit_timestamp
;
2291 i
->pid
= (pid_t
) pid
;
2298 typedef struct UnitStatusInfo
{
2300 const char *load_state
;
2301 const char *active_state
;
2302 const char *sub_state
;
2303 const char *unit_file_state
;
2305 const char *description
;
2306 const char *following
;
2308 char **documentation
;
2310 const char *fragment_path
;
2311 const char *source_path
;
2312 const char *default_control_group
;
2314 const char *load_error
;
2317 usec_t inactive_exit_timestamp
;
2318 usec_t inactive_exit_timestamp_monotonic
;
2319 usec_t active_enter_timestamp
;
2320 usec_t active_exit_timestamp
;
2321 usec_t inactive_enter_timestamp
;
2323 bool need_daemon_reload
;
2328 const char *status_text
;
2331 usec_t start_timestamp
;
2332 usec_t exit_timestamp
;
2334 int exit_code
, exit_status
;
2336 usec_t condition_timestamp
;
2337 bool condition_result
;
2340 unsigned n_accepted
;
2341 unsigned n_connections
;
2345 const char *sysfs_path
;
2347 /* Mount, Automount */
2353 LIST_HEAD(ExecStatusInfo
, exec
);
2356 static void print_status_info(UnitStatusInfo
*i
) {
2358 const char *on
, *off
, *ss
;
2360 char since1
[FORMAT_TIMESTAMP_PRETTY_MAX
], *s1
;
2361 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
2366 /* This shows pretty information about a unit. See
2367 * print_property() for a low-level property printer */
2369 printf("%s", strna(i
->id
));
2371 if (i
->description
&& !streq_ptr(i
->id
, i
->description
))
2372 printf(" - %s", i
->description
);
2377 printf("\t Follow: unit currently follows state of %s\n", i
->following
);
2379 if (streq_ptr(i
->load_state
, "error")) {
2380 on
= ansi_highlight_red(true);
2381 off
= ansi_highlight_red(false);
2385 path
= i
->source_path
? i
->source_path
: i
->fragment_path
;
2388 printf("\t Loaded: %s%s%s (Reason: %s)\n", on
, strna(i
->load_state
), off
, i
->load_error
);
2389 else if (path
&& i
->unit_file_state
)
2390 printf("\t Loaded: %s%s%s (%s; %s)\n", on
, strna(i
->load_state
), off
, path
, i
->unit_file_state
);
2392 printf("\t Loaded: %s%s%s (%s)\n", on
, strna(i
->load_state
), off
, path
);
2394 printf("\t Loaded: %s%s%s\n", on
, strna(i
->load_state
), off
);
2396 ss
= streq_ptr(i
->active_state
, i
->sub_state
) ? NULL
: i
->sub_state
;
2398 if (streq_ptr(i
->active_state
, "failed")) {
2399 on
= ansi_highlight_red(true);
2400 off
= ansi_highlight_red(false);
2401 } else if (streq_ptr(i
->active_state
, "active") || streq_ptr(i
->active_state
, "reloading")) {
2402 on
= ansi_highlight_green(true);
2403 off
= ansi_highlight_green(false);
2408 printf("\t Active: %s%s (%s)%s",
2410 strna(i
->active_state
),
2414 printf("\t Active: %s%s%s",
2416 strna(i
->active_state
),
2419 if (!isempty(i
->result
) && !streq(i
->result
, "success"))
2420 printf(" (Result: %s)", i
->result
);
2422 timestamp
= (streq_ptr(i
->active_state
, "active") ||
2423 streq_ptr(i
->active_state
, "reloading")) ? i
->active_enter_timestamp
:
2424 (streq_ptr(i
->active_state
, "inactive") ||
2425 streq_ptr(i
->active_state
, "failed")) ? i
->inactive_enter_timestamp
:
2426 streq_ptr(i
->active_state
, "activating") ? i
->inactive_exit_timestamp
:
2427 i
->active_exit_timestamp
;
2429 s1
= format_timestamp_pretty(since1
, sizeof(since1
), timestamp
);
2430 s2
= format_timestamp(since2
, sizeof(since2
), timestamp
);
2433 printf(" since %s; %s\n", s2
, s1
);
2435 printf(" since %s\n", s2
);
2439 if (!i
->condition_result
&& i
->condition_timestamp
> 0) {
2440 s1
= format_timestamp_pretty(since1
, sizeof(since1
), i
->condition_timestamp
);
2441 s2
= format_timestamp(since2
, sizeof(since2
), i
->condition_timestamp
);
2444 printf("\t start condition failed at %s; %s\n", s2
, s1
);
2446 printf("\t start condition failed at %s\n", s2
);
2450 printf("\t Device: %s\n", i
->sysfs_path
);
2452 printf("\t Where: %s\n", i
->where
);
2454 printf("\t What: %s\n", i
->what
);
2456 if (!strv_isempty(i
->documentation
)) {
2460 STRV_FOREACH(t
, i
->documentation
) {
2462 printf("\t Docs: %s\n", *t
);
2465 printf("\t %s\n", *t
);
2470 printf("\tAccepted: %u; Connected: %u\n", i
->n_accepted
, i
->n_connections
);
2472 LIST_FOREACH(exec
, p
, i
->exec
) {
2476 /* Only show exited processes here */
2480 t
= strv_join(p
->argv
, " ");
2481 printf("\t Process: %u %s=%s ", p
->pid
, p
->name
, strna(t
));
2484 good
= is_clean_exit_lsb(p
->code
, p
->status
);
2486 on
= ansi_highlight_red(true);
2487 off
= ansi_highlight_red(false);
2491 printf("%s(code=%s, ", on
, sigchld_code_to_string(p
->code
));
2493 if (p
->code
== CLD_EXITED
) {
2496 printf("status=%i", p
->status
);
2498 c
= exit_status_to_string(p
->status
, EXIT_STATUS_SYSTEMD
);
2503 printf("signal=%s", signal_to_string(p
->status
));
2505 printf(")%s\n", off
);
2507 if (i
->main_pid
== p
->pid
&&
2508 i
->start_timestamp
== p
->start_timestamp
&&
2509 i
->exit_timestamp
== p
->start_timestamp
)
2510 /* Let's not show this twice */
2513 if (p
->pid
== i
->control_pid
)
2517 if (i
->main_pid
> 0 || i
->control_pid
> 0) {
2520 if (i
->main_pid
> 0) {
2521 printf("Main PID: %u", (unsigned) i
->main_pid
);
2525 get_process_comm(i
->main_pid
, &t
);
2530 } else if (i
->exit_code
> 0) {
2531 printf(" (code=%s, ", sigchld_code_to_string(i
->exit_code
));
2533 if (i
->exit_code
== CLD_EXITED
) {
2536 printf("status=%i", i
->exit_status
);
2538 c
= exit_status_to_string(i
->exit_status
, EXIT_STATUS_SYSTEMD
);
2543 printf("signal=%s", signal_to_string(i
->exit_status
));
2548 if (i
->main_pid
> 0 && i
->control_pid
> 0)
2551 if (i
->control_pid
> 0) {
2554 printf(" Control: %u", (unsigned) i
->control_pid
);
2556 get_process_comm(i
->control_pid
, &t
);
2567 printf("\t Status: \"%s\"\n", i
->status_text
);
2569 if (i
->default_control_group
) {
2572 printf("\t CGroup: %s\n", i
->default_control_group
);
2574 if (arg_transport
!= TRANSPORT_SSH
) {
2584 if (i
->main_pid
> 0)
2585 extra
[k
++] = i
->main_pid
;
2587 if (i
->control_pid
> 0)
2588 extra
[k
++] = i
->control_pid
;
2590 show_cgroup_and_extra_by_spec(i
->default_control_group
, "\t\t ", c
, false, arg_all
, extra
, k
);
2594 if (i
->id
&& arg_transport
!= TRANSPORT_SSH
) {
2595 int flags
= (arg_lines
*OUTPUT_SHOW_ALL
|
2596 arg_follow
*OUTPUT_FOLLOW
|
2597 !arg_quiet
*OUTPUT_WARN_CUTOFF
);
2599 show_journal_by_unit(i
->id
, arg_output
, 0,
2600 i
->inactive_exit_timestamp_monotonic
,
2604 if (i
->need_daemon_reload
)
2605 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2606 ansi_highlight_red(true),
2607 ansi_highlight_red(false),
2608 arg_scope
== UNIT_FILE_SYSTEM
? "--system" : "--user");
2611 static void show_unit_help(UnitStatusInfo
*i
) {
2616 if (!i
->documentation
) {
2617 log_info("Documentation for %s not known.", i
->id
);
2621 STRV_FOREACH(p
, i
->documentation
) {
2623 if (startswith(*p
, "man:")) {
2626 char *page
= NULL
, *section
= NULL
;
2627 const char *args
[4] = { "man", NULL
, NULL
, NULL
};
2632 if ((*p
)[k
-1] == ')')
2633 e
= strrchr(*p
, '(');
2636 page
= strndup((*p
) + 4, e
- *p
- 4);
2638 log_error("Out of memory.");
2642 section
= strndup(e
+ 1, *p
+ k
- e
- 2);
2645 log_error("Out of memory");
2656 log_error("Failed to fork: %m");
2664 execvp(args
[0], (char**) args
);
2665 log_error("Failed to execute man: %m");
2666 _exit(EXIT_FAILURE
);
2672 wait_for_terminate(pid
, NULL
);
2674 log_info("Can't show: %s", *p
);
2678 static int status_property(const char *name
, DBusMessageIter
*iter
, UnitStatusInfo
*i
) {
2684 switch (dbus_message_iter_get_arg_type(iter
)) {
2686 case DBUS_TYPE_STRING
: {
2689 dbus_message_iter_get_basic(iter
, &s
);
2692 if (streq(name
, "Id"))
2694 else if (streq(name
, "LoadState"))
2696 else if (streq(name
, "ActiveState"))
2697 i
->active_state
= s
;
2698 else if (streq(name
, "SubState"))
2700 else if (streq(name
, "Description"))
2702 else if (streq(name
, "FragmentPath"))
2703 i
->fragment_path
= s
;
2704 else if (streq(name
, "SourcePath"))
2706 else if (streq(name
, "DefaultControlGroup"))
2707 i
->default_control_group
= s
;
2708 else if (streq(name
, "StatusText"))
2710 else if (streq(name
, "SysFSPath"))
2712 else if (streq(name
, "Where"))
2714 else if (streq(name
, "What"))
2716 else if (streq(name
, "Following"))
2718 else if (streq(name
, "UnitFileState"))
2719 i
->unit_file_state
= s
;
2720 else if (streq(name
, "Result"))
2727 case DBUS_TYPE_BOOLEAN
: {
2730 dbus_message_iter_get_basic(iter
, &b
);
2732 if (streq(name
, "Accept"))
2734 else if (streq(name
, "NeedDaemonReload"))
2735 i
->need_daemon_reload
= b
;
2736 else if (streq(name
, "ConditionResult"))
2737 i
->condition_result
= b
;
2742 case DBUS_TYPE_UINT32
: {
2745 dbus_message_iter_get_basic(iter
, &u
);
2747 if (streq(name
, "MainPID")) {
2749 i
->main_pid
= (pid_t
) u
;
2752 } else if (streq(name
, "ControlPID"))
2753 i
->control_pid
= (pid_t
) u
;
2754 else if (streq(name
, "ExecMainPID")) {
2756 i
->main_pid
= (pid_t
) u
;
2757 } else if (streq(name
, "NAccepted"))
2759 else if (streq(name
, "NConnections"))
2760 i
->n_connections
= u
;
2765 case DBUS_TYPE_INT32
: {
2768 dbus_message_iter_get_basic(iter
, &j
);
2770 if (streq(name
, "ExecMainCode"))
2771 i
->exit_code
= (int) j
;
2772 else if (streq(name
, "ExecMainStatus"))
2773 i
->exit_status
= (int) j
;
2778 case DBUS_TYPE_UINT64
: {
2781 dbus_message_iter_get_basic(iter
, &u
);
2783 if (streq(name
, "ExecMainStartTimestamp"))
2784 i
->start_timestamp
= (usec_t
) u
;
2785 else if (streq(name
, "ExecMainExitTimestamp"))
2786 i
->exit_timestamp
= (usec_t
) u
;
2787 else if (streq(name
, "ActiveEnterTimestamp"))
2788 i
->active_enter_timestamp
= (usec_t
) u
;
2789 else if (streq(name
, "InactiveEnterTimestamp"))
2790 i
->inactive_enter_timestamp
= (usec_t
) u
;
2791 else if (streq(name
, "InactiveExitTimestamp"))
2792 i
->inactive_exit_timestamp
= (usec_t
) u
;
2793 else if (streq(name
, "InactiveExitTimestampMonotonic"))
2794 i
->inactive_exit_timestamp_monotonic
= (usec_t
) u
;
2795 else if (streq(name
, "ActiveExitTimestamp"))
2796 i
->active_exit_timestamp
= (usec_t
) u
;
2797 else if (streq(name
, "ConditionTimestamp"))
2798 i
->condition_timestamp
= (usec_t
) u
;
2803 case DBUS_TYPE_ARRAY
: {
2805 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&&
2806 startswith(name
, "Exec")) {
2807 DBusMessageIter sub
;
2809 dbus_message_iter_recurse(iter
, &sub
);
2810 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2811 ExecStatusInfo
*info
;
2814 if (!(info
= new0(ExecStatusInfo
, 1)))
2817 if (!(info
->name
= strdup(name
))) {
2822 if ((r
= exec_status_info_deserialize(&sub
, info
)) < 0) {
2827 LIST_PREPEND(ExecStatusInfo
, exec
, i
->exec
, info
);
2829 dbus_message_iter_next(&sub
);
2831 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
&&
2832 streq(name
, "Documentation")) {
2834 DBusMessageIter sub
;
2836 dbus_message_iter_recurse(iter
, &sub
);
2837 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
) {
2841 dbus_message_iter_get_basic(&sub
, &s
);
2843 l
= strv_append(i
->documentation
, s
);
2847 strv_free(i
->documentation
);
2848 i
->documentation
= l
;
2850 dbus_message_iter_next(&sub
);
2857 case DBUS_TYPE_STRUCT
: {
2859 if (streq(name
, "LoadError")) {
2860 DBusMessageIter sub
;
2861 const char *n
, *message
;
2864 dbus_message_iter_recurse(iter
, &sub
);
2866 r
= bus_iter_get_basic_and_next(&sub
, DBUS_TYPE_STRING
, &n
, true);
2870 r
= bus_iter_get_basic_and_next(&sub
, DBUS_TYPE_STRING
, &message
, false);
2874 if (!isempty(message
))
2875 i
->load_error
= message
;
2885 static int print_property(const char *name
, DBusMessageIter
*iter
) {
2889 /* This is a low-level property printer, see
2890 * print_status_info() for the nicer output */
2892 if (arg_property
&& !strv_find(arg_property
, name
))
2895 switch (dbus_message_iter_get_arg_type(iter
)) {
2897 case DBUS_TYPE_STRUCT
: {
2898 DBusMessageIter sub
;
2899 dbus_message_iter_recurse(iter
, &sub
);
2901 if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_UINT32
&& streq(name
, "Job")) {
2904 dbus_message_iter_get_basic(&sub
, &u
);
2907 printf("%s=%u\n", name
, (unsigned) u
);
2909 printf("%s=\n", name
);
2912 } else if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
&& streq(name
, "Unit")) {
2915 dbus_message_iter_get_basic(&sub
, &s
);
2917 if (arg_all
|| s
[0])
2918 printf("%s=%s\n", name
, s
);
2921 } else if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
&& streq(name
, "LoadError")) {
2922 const char *a
= NULL
, *b
= NULL
;
2924 if (bus_iter_get_basic_and_next(&sub
, DBUS_TYPE_STRING
, &a
, true) >= 0)
2925 bus_iter_get_basic_and_next(&sub
, DBUS_TYPE_STRING
, &b
, false);
2927 if (arg_all
|| !isempty(a
) || !isempty(b
))
2928 printf("%s=%s \"%s\"\n", name
, strempty(a
), strempty(b
));
2936 case DBUS_TYPE_ARRAY
:
2938 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "EnvironmentFiles")) {
2939 DBusMessageIter sub
, sub2
;
2941 dbus_message_iter_recurse(iter
, &sub
);
2942 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2946 dbus_message_iter_recurse(&sub
, &sub2
);
2948 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) >= 0 &&
2949 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, false) >= 0)
2950 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path
, yes_no(ignore
));
2952 dbus_message_iter_next(&sub
);
2957 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Paths")) {
2958 DBusMessageIter sub
, sub2
;
2960 dbus_message_iter_recurse(iter
, &sub
);
2961 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2962 const char *type
, *path
;
2964 dbus_message_iter_recurse(&sub
, &sub2
);
2966 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) >= 0 &&
2967 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, false) >= 0)
2968 printf("%s=%s\n", type
, path
);
2970 dbus_message_iter_next(&sub
);
2975 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Timers")) {
2976 DBusMessageIter sub
, sub2
;
2978 dbus_message_iter_recurse(iter
, &sub
);
2979 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2981 uint64_t value
, next_elapse
;
2983 dbus_message_iter_recurse(&sub
, &sub2
);
2985 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &base
, true) >= 0 &&
2986 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &value
, true) >= 0 &&
2987 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &next_elapse
, false) >= 0) {
2988 char timespan1
[FORMAT_TIMESPAN_MAX
], timespan2
[FORMAT_TIMESPAN_MAX
];
2990 printf("%s={ value=%s ; next_elapse=%s }\n",
2992 format_timespan(timespan1
, sizeof(timespan1
), value
),
2993 format_timespan(timespan2
, sizeof(timespan2
), next_elapse
));
2996 dbus_message_iter_next(&sub
);
3001 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "ControlGroupAttributes")) {
3002 DBusMessageIter sub
, sub2
;
3004 dbus_message_iter_recurse(iter
, &sub
);
3005 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
3006 const char *controller
, *attr
, *value
;
3008 dbus_message_iter_recurse(&sub
, &sub2
);
3010 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &controller
, true) >= 0 &&
3011 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &attr
, true) >= 0 &&
3012 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &value
, false) >= 0) {
3014 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
3020 dbus_message_iter_next(&sub
);
3025 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& startswith(name
, "Exec")) {
3026 DBusMessageIter sub
;
3028 dbus_message_iter_recurse(iter
, &sub
);
3029 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
3030 ExecStatusInfo info
;
3033 if (exec_status_info_deserialize(&sub
, &info
) >= 0) {
3034 char timestamp1
[FORMAT_TIMESTAMP_MAX
], timestamp2
[FORMAT_TIMESTAMP_MAX
];
3037 t
= strv_join(info
.argv
, " ");
3039 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
3043 yes_no(info
.ignore
),
3044 strna(format_timestamp(timestamp1
, sizeof(timestamp1
), info
.start_timestamp
)),
3045 strna(format_timestamp(timestamp2
, sizeof(timestamp2
), info
.exit_timestamp
)),
3046 (unsigned) info
. pid
,
3047 sigchld_code_to_string(info
.code
),
3049 info
.code
== CLD_EXITED
? "" : "/",
3050 strempty(info
.code
== CLD_EXITED
? NULL
: signal_to_string(info
.status
)));
3056 strv_free(info
.argv
);
3058 dbus_message_iter_next(&sub
);
3067 if (generic_print_property(name
, iter
, arg_all
) > 0)
3071 printf("%s=[unprintable]\n", name
);
3076 static int show_one(const char *verb
, DBusConnection
*bus
, const char *path
, bool show_properties
, bool *new_line
) {
3077 DBusMessage
*m
= NULL
, *reply
= NULL
;
3078 const char *interface
= "";
3081 DBusMessageIter iter
, sub
, sub2
, sub3
;
3082 UnitStatusInfo info
;
3090 dbus_error_init(&error
);
3092 if (!(m
= dbus_message_new_method_call(
3093 "org.freedesktop.systemd1",
3095 "org.freedesktop.DBus.Properties",
3097 log_error("Could not allocate message.");
3102 if (!dbus_message_append_args(m
,
3103 DBUS_TYPE_STRING
, &interface
,
3104 DBUS_TYPE_INVALID
)) {
3105 log_error("Could not append arguments to message.");
3110 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3111 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3116 if (!dbus_message_iter_init(reply
, &iter
) ||
3117 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
3118 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
3119 log_error("Failed to parse reply.");
3124 dbus_message_iter_recurse(&iter
, &sub
);
3131 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
3134 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
3135 log_error("Failed to parse reply.");
3140 dbus_message_iter_recurse(&sub
, &sub2
);
3142 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0) {
3143 log_error("Failed to parse reply.");
3148 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
3149 log_error("Failed to parse reply.");
3154 dbus_message_iter_recurse(&sub2
, &sub3
);
3156 if (show_properties
)
3157 r
= print_property(name
, &sub3
);
3159 r
= status_property(name
, &sub3
, &info
);
3162 log_error("Failed to parse reply.");
3167 dbus_message_iter_next(&sub
);
3172 if (!show_properties
) {
3173 if (streq(verb
, "help"))
3174 show_unit_help(&info
);
3176 print_status_info(&info
);
3179 strv_free(info
.documentation
);
3181 if (!streq_ptr(info
.active_state
, "active") &&
3182 !streq_ptr(info
.active_state
, "reloading") &&
3183 streq(verb
, "status"))
3184 /* According to LSB: "program not running" */
3187 while ((p
= info
.exec
)) {
3188 LIST_REMOVE(ExecStatusInfo
, exec
, info
.exec
, p
);
3189 exec_status_info_free(p
);
3194 dbus_message_unref(m
);
3197 dbus_message_unref(reply
);
3199 dbus_error_free(&error
);
3204 static int show_one_by_pid(const char *verb
, DBusConnection
*bus
, uint32_t pid
, bool *new_line
) {
3205 DBusMessage
*m
= NULL
, *reply
= NULL
;
3206 const char *path
= NULL
;
3210 dbus_error_init(&error
);
3212 m
= dbus_message_new_method_call(
3213 "org.freedesktop.systemd1",
3214 "/org/freedesktop/systemd1",
3215 "org.freedesktop.systemd1.Manager",
3218 log_error("Could not allocate message.");
3223 if (!dbus_message_append_args(m
,
3224 DBUS_TYPE_UINT32
, &pid
,
3225 DBUS_TYPE_INVALID
)) {
3226 log_error("Could not append arguments to message.");
3231 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
3233 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3238 if (!dbus_message_get_args(reply
, &error
,
3239 DBUS_TYPE_OBJECT_PATH
, &path
,
3240 DBUS_TYPE_INVALID
)) {
3241 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3246 r
= show_one(verb
, bus
, path
, false, new_line
);
3250 dbus_message_unref(m
);
3253 dbus_message_unref(reply
);
3255 dbus_error_free(&error
);
3260 static int show(DBusConnection
*bus
, char **args
) {
3262 bool show_properties
, new_line
= false;
3268 show_properties
= streq(args
[0], "show");
3270 if (show_properties
)
3271 pager_open_if_enabled();
3273 if (show_properties
&& strv_length(args
) <= 1) {
3274 /* If not argument is specified inspect the manager
3277 return show_one(args
[0], bus
, "/org/freedesktop/systemd1", show_properties
, &new_line
);
3280 STRV_FOREACH(name
, args
+1) {
3283 if (safe_atou32(*name
, &id
) < 0) {
3285 /* Interpret as unit name */
3287 n
= unit_name_mangle(*name
);
3288 p
= unit_dbus_path_from_name(n
? n
: *name
);
3291 log_error("Out of memory");
3295 r
= show_one(args
[0], bus
, p
, show_properties
, &new_line
);
3301 } else if (show_properties
) {
3303 /* Interpret as job id */
3306 if (asprintf(&p
, "/org/freedesktop/systemd1/job/%u", id
) < 0) {
3307 log_error("Out of memory");
3311 r
= show_one(args
[0], bus
, p
, show_properties
, &new_line
);
3319 /* Interpret as PID */
3321 r
= show_one_by_pid(args
[0], bus
, id
, &new_line
);
3330 static int dump(DBusConnection
*bus
, char **args
) {
3331 DBusMessage
*m
= NULL
, *reply
= NULL
;
3336 dbus_error_init(&error
);
3338 pager_open_if_enabled();
3340 if (!(m
= dbus_message_new_method_call(
3341 "org.freedesktop.systemd1",
3342 "/org/freedesktop/systemd1",
3343 "org.freedesktop.systemd1.Manager",
3345 log_error("Could not allocate message.");
3349 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3350 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3355 if (!dbus_message_get_args(reply
, &error
,
3356 DBUS_TYPE_STRING
, &text
,
3357 DBUS_TYPE_INVALID
)) {
3358 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3363 fputs(text
, stdout
);
3369 dbus_message_unref(m
);
3372 dbus_message_unref(reply
);
3374 dbus_error_free(&error
);
3379 static int snapshot(DBusConnection
*bus
, char **args
) {
3380 DBusMessage
*m
= NULL
, *reply
= NULL
;
3383 const char *name
= "", *path
, *id
;
3384 dbus_bool_t cleanup
= FALSE
;
3385 DBusMessageIter iter
, sub
;
3387 *interface
= "org.freedesktop.systemd1.Unit",
3392 dbus_error_init(&error
);
3394 m
= dbus_message_new_method_call(
3395 "org.freedesktop.systemd1",
3396 "/org/freedesktop/systemd1",
3397 "org.freedesktop.systemd1.Manager",
3400 log_error("Could not allocate message.");
3404 if (strv_length(args
) > 1)
3407 n
= unit_name_mangle(name
);
3408 b
= dbus_message_append_args(m
,
3409 DBUS_TYPE_STRING
, n
? (const char**) &n
: &name
,
3410 DBUS_TYPE_BOOLEAN
, &cleanup
,
3414 log_error("Could not append arguments to message.");
3419 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
3421 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3426 if (!dbus_message_get_args(reply
, &error
,
3427 DBUS_TYPE_OBJECT_PATH
, &path
,
3428 DBUS_TYPE_INVALID
)) {
3429 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3434 dbus_message_unref(m
);
3435 m
= dbus_message_new_method_call(
3436 "org.freedesktop.systemd1",
3438 "org.freedesktop.DBus.Properties",
3441 log_error("Could not allocate message.");
3445 if (!dbus_message_append_args(m
,
3446 DBUS_TYPE_STRING
, &interface
,
3447 DBUS_TYPE_STRING
, &property
,
3448 DBUS_TYPE_INVALID
)) {
3449 log_error("Could not append arguments to message.");
3454 dbus_message_unref(reply
);
3455 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
3457 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3462 if (!dbus_message_iter_init(reply
, &iter
) ||
3463 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3464 log_error("Failed to parse reply.");
3469 dbus_message_iter_recurse(&iter
, &sub
);
3471 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
3472 log_error("Failed to parse reply.");
3477 dbus_message_iter_get_basic(&sub
, &id
);
3485 dbus_message_unref(m
);
3488 dbus_message_unref(reply
);
3490 dbus_error_free(&error
);
3495 static int delete_snapshot(DBusConnection
*bus
, char **args
) {
3496 DBusMessage
*m
= NULL
, *reply
= NULL
;
3504 dbus_error_init(&error
);
3506 STRV_FOREACH(name
, args
+1) {
3507 const char *path
= NULL
;
3511 m
= dbus_message_new_method_call(
3512 "org.freedesktop.systemd1",
3513 "/org/freedesktop/systemd1",
3514 "org.freedesktop.systemd1.Manager",
3517 log_error("Could not allocate message.");
3522 n
= unit_name_mangle(*name
);
3523 b
= dbus_message_append_args(m
,
3524 DBUS_TYPE_STRING
, n
? &n
: name
,
3528 log_error("Could not append arguments to message.");
3533 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
3535 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3540 if (!dbus_message_get_args(reply
, &error
,
3541 DBUS_TYPE_OBJECT_PATH
, &path
,
3542 DBUS_TYPE_INVALID
)) {
3543 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3548 dbus_message_unref(m
);
3549 m
= dbus_message_new_method_call(
3550 "org.freedesktop.systemd1",
3552 "org.freedesktop.systemd1.Snapshot",
3555 log_error("Could not allocate message.");
3560 dbus_message_unref(reply
);
3561 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
3563 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3568 dbus_message_unref(m
);
3569 dbus_message_unref(reply
);
3577 dbus_message_unref(m
);
3580 dbus_message_unref(reply
);
3582 dbus_error_free(&error
);
3587 static int daemon_reload(DBusConnection
*bus
, char **args
) {
3588 DBusMessage
*m
= NULL
, *reply
= NULL
;
3593 dbus_error_init(&error
);
3595 if (arg_action
== ACTION_RELOAD
)
3597 else if (arg_action
== ACTION_REEXEC
)
3598 method
= "Reexecute";
3600 assert(arg_action
== ACTION_SYSTEMCTL
);
3603 streq(args
[0], "clear-jobs") ||
3604 streq(args
[0], "cancel") ? "ClearJobs" :
3605 streq(args
[0], "daemon-reexec") ? "Reexecute" :
3606 streq(args
[0], "reset-failed") ? "ResetFailed" :
3607 streq(args
[0], "halt") ? "Halt" :
3608 streq(args
[0], "poweroff") ? "PowerOff" :
3609 streq(args
[0], "reboot") ? "Reboot" :
3610 streq(args
[0], "kexec") ? "KExec" :
3611 streq(args
[0], "exit") ? "Exit" :
3612 /* "daemon-reload" */ "Reload";
3615 if (!(m
= dbus_message_new_method_call(
3616 "org.freedesktop.systemd1",
3617 "/org/freedesktop/systemd1",
3618 "org.freedesktop.systemd1.Manager",
3620 log_error("Could not allocate message.");
3624 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3626 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(&error
)) {
3627 /* There's always a fallback possible for
3628 * legacy actions. */
3633 if (streq(method
, "Reexecute") && dbus_error_has_name(&error
, DBUS_ERROR_NO_REPLY
)) {
3634 /* On reexecution, we expect a disconnect, not
3640 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3649 dbus_message_unref(m
);
3652 dbus_message_unref(reply
);
3654 dbus_error_free(&error
);
3659 static int reset_failed(DBusConnection
*bus
, char **args
) {
3660 DBusMessage
*m
= NULL
;
3666 dbus_error_init(&error
);
3668 if (strv_length(args
) <= 1)
3669 return daemon_reload(bus
, args
);
3671 STRV_FOREACH(name
, args
+1) {
3676 m
= dbus_message_new_method_call(
3677 "org.freedesktop.systemd1",
3678 "/org/freedesktop/systemd1",
3679 "org.freedesktop.systemd1.Manager",
3682 log_error("Could not allocate message.");
3687 n
= unit_name_mangle(*name
);
3688 b
= dbus_message_append_args(m
,
3689 DBUS_TYPE_STRING
, n
? &n
: name
,
3693 log_error("Could not append arguments to message.");
3698 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
3700 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3705 dbus_message_unref(m
);
3706 dbus_message_unref(reply
);
3714 dbus_message_unref(m
);
3716 dbus_error_free(&error
);
3721 static int show_enviroment(DBusConnection
*bus
, char **args
) {
3722 DBusMessage
*m
= NULL
, *reply
= NULL
;
3724 DBusMessageIter iter
, sub
, sub2
;
3727 *interface
= "org.freedesktop.systemd1.Manager",
3728 *property
= "Environment";
3730 dbus_error_init(&error
);
3732 pager_open_if_enabled();
3734 if (!(m
= dbus_message_new_method_call(
3735 "org.freedesktop.systemd1",
3736 "/org/freedesktop/systemd1",
3737 "org.freedesktop.DBus.Properties",
3739 log_error("Could not allocate message.");
3743 if (!dbus_message_append_args(m
,
3744 DBUS_TYPE_STRING
, &interface
,
3745 DBUS_TYPE_STRING
, &property
,
3746 DBUS_TYPE_INVALID
)) {
3747 log_error("Could not append arguments to message.");
3752 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3753 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3758 if (!dbus_message_iter_init(reply
, &iter
) ||
3759 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3760 log_error("Failed to parse reply.");
3765 dbus_message_iter_recurse(&iter
, &sub
);
3767 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_ARRAY
||
3768 dbus_message_iter_get_element_type(&sub
) != DBUS_TYPE_STRING
) {
3769 log_error("Failed to parse reply.");
3774 dbus_message_iter_recurse(&sub
, &sub2
);
3776 while (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_INVALID
) {
3779 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_STRING
) {
3780 log_error("Failed to parse reply.");
3785 dbus_message_iter_get_basic(&sub2
, &text
);
3786 printf("%s\n", text
);
3788 dbus_message_iter_next(&sub2
);
3795 dbus_message_unref(m
);
3798 dbus_message_unref(reply
);
3800 dbus_error_free(&error
);
3805 static int switch_root(DBusConnection
*bus
, char **args
) {
3806 DBusMessage
*m
= NULL
, *reply
= NULL
;
3808 const char *root
, *init
;
3812 dbus_error_init(&error
);
3814 l
= strv_length(args
);
3815 if (l
< 2 || l
> 3) {
3816 log_error("Wrong number of arguments.");
3821 init
= l
>= 3 ? args
[2] : "";
3823 m
= dbus_message_new_method_call(
3824 "org.freedesktop.systemd1",
3825 "/org/freedesktop/systemd1",
3826 "org.freedesktop.systemd1.Manager",
3829 log_error("Could not allocate message.");
3833 if (!dbus_message_append_args(
3835 DBUS_TYPE_STRING
, &root
,
3836 DBUS_TYPE_STRING
, &init
,
3837 DBUS_TYPE_INVALID
)) {
3838 log_error("Could not append arguments to message.");
3843 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
3845 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3854 dbus_message_unref(m
);
3857 dbus_message_unref(reply
);
3859 dbus_error_free(&error
);
3864 static int set_environment(DBusConnection
*bus
, char **args
) {
3865 DBusMessage
*m
= NULL
, *reply
= NULL
;
3869 DBusMessageIter iter
, sub
;
3872 dbus_error_init(&error
);
3874 method
= streq(args
[0], "set-environment")
3876 : "UnsetEnvironment";
3878 if (!(m
= dbus_message_new_method_call(
3879 "org.freedesktop.systemd1",
3880 "/org/freedesktop/systemd1",
3881 "org.freedesktop.systemd1.Manager",
3884 log_error("Could not allocate message.");
3888 dbus_message_iter_init_append(m
, &iter
);
3890 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
)) {
3891 log_error("Could not append arguments to message.");
3896 STRV_FOREACH(name
, args
+1)
3897 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, name
)) {
3898 log_error("Could not append arguments to message.");
3903 if (!dbus_message_iter_close_container(&iter
, &sub
)) {
3904 log_error("Could not append arguments to message.");
3909 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3910 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3919 dbus_message_unref(m
);
3922 dbus_message_unref(reply
);
3924 dbus_error_free(&error
);
3929 static int enable_sysv_units(char **args
) {
3932 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_ALTLINUX) || defined(TARGET_MAGEIA))
3933 const char *verb
= args
[0];
3934 unsigned f
= 1, t
= 1;
3937 if (arg_scope
!= UNIT_FILE_SYSTEM
)
3940 if (!streq(verb
, "enable") &&
3941 !streq(verb
, "disable") &&
3942 !streq(verb
, "is-enabled"))
3945 /* Processes all SysV units, and reshuffles the array so that
3946 * afterwards only the native units remain */
3949 r
= lookup_paths_init(&paths
, MANAGER_SYSTEM
, false, NULL
, NULL
, NULL
);
3954 for (f
= 1; args
[f
]; f
++) {
3957 bool found_native
= false, found_sysv
;
3959 const char *argv
[6] = { "/sbin/chkconfig", NULL
, NULL
, NULL
, NULL
};
3960 char **k
, *l
, *q
= NULL
;
3967 if (!endswith(name
, ".service"))
3970 if (path_is_absolute(name
))
3973 STRV_FOREACH(k
, paths
.unit_path
) {
3976 if (!isempty(arg_root
))
3977 asprintf(&p
, "%s/%s/%s", arg_root
, *k
, name
);
3979 asprintf(&p
, "%s/%s", *k
, name
);
3982 log_error("No memory");
3987 found_native
= access(p
, F_OK
) >= 0;
3998 if (!isempty(arg_root
))
3999 asprintf(&p
, "%s/" SYSTEM_SYSVINIT_PATH
"/%s", arg_root
, name
);
4001 asprintf(&p
, SYSTEM_SYSVINIT_PATH
"/%s", name
);
4003 log_error("No memory");
4008 p
[strlen(p
) - sizeof(".service") + 1] = 0;
4009 found_sysv
= access(p
, F_OK
) >= 0;
4016 /* Mark this entry, so that we don't try enabling it as native unit */
4017 args
[f
] = (char*) "";
4019 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name
);
4021 if (!isempty(arg_root
))
4022 argv
[c
++] = q
= strappend("--root=", arg_root
);
4024 argv
[c
++] = path_get_file_name(p
);
4026 streq(verb
, "enable") ? "on" :
4027 streq(verb
, "disable") ? "off" : "--level=5";
4030 l
= strv_join((char**)argv
, " ");
4032 log_error("No memory.");
4039 log_info("Executing %s", l
);
4044 log_error("Failed to fork: %m");
4049 } else if (pid
== 0) {
4052 execv(argv
[0], (char**) argv
);
4053 _exit(EXIT_FAILURE
);
4059 j
= wait_for_terminate(pid
, &status
);
4061 log_error("Failed to wait for child: %s", strerror(-r
));
4066 if (status
.si_code
== CLD_EXITED
) {
4067 if (streq(verb
, "is-enabled")) {
4068 if (status
.si_status
== 0) {
4077 } else if (status
.si_status
!= 0) {
4088 lookup_paths_free(&paths
);
4090 /* Drop all SysV units */
4091 for (f
= 1, t
= 1; args
[f
]; f
++) {
4093 if (isempty(args
[f
]))
4096 args
[t
++] = args
[f
];
4105 static int enable_unit(DBusConnection
*bus
, char **args
) {
4106 const char *verb
= args
[0];
4107 UnitFileChange
*changes
= NULL
;
4108 unsigned n_changes
= 0, i
;
4109 int carries_install_info
= -1;
4110 DBusMessage
*m
= NULL
, *reply
= NULL
;
4114 r
= enable_sysv_units(args
);
4121 dbus_error_init(&error
);
4123 if (!bus
|| avoid_bus()) {
4124 if (streq(verb
, "enable")) {
4125 r
= unit_file_enable(arg_scope
, arg_runtime
, arg_root
, args
+1, arg_force
, &changes
, &n_changes
);
4126 carries_install_info
= r
;
4127 } else if (streq(verb
, "disable"))
4128 r
= unit_file_disable(arg_scope
, arg_runtime
, arg_root
, args
+1, &changes
, &n_changes
);
4129 else if (streq(verb
, "reenable")) {
4130 r
= unit_file_reenable(arg_scope
, arg_runtime
, arg_root
, args
+1, arg_force
, &changes
, &n_changes
);
4131 carries_install_info
= r
;
4132 } else if (streq(verb
, "link"))
4133 r
= unit_file_link(arg_scope
, arg_runtime
, arg_root
, args
+1, arg_force
, &changes
, &n_changes
);
4134 else if (streq(verb
, "preset")) {
4135 r
= unit_file_preset(arg_scope
, arg_runtime
, arg_root
, args
+1, arg_force
, &changes
, &n_changes
);
4136 carries_install_info
= r
;
4137 } else if (streq(verb
, "mask"))
4138 r
= unit_file_mask(arg_scope
, arg_runtime
, arg_root
, args
+1, arg_force
, &changes
, &n_changes
);
4139 else if (streq(verb
, "unmask"))
4140 r
= unit_file_unmask(arg_scope
, arg_runtime
, arg_root
, args
+1, &changes
, &n_changes
);
4142 assert_not_reached("Unknown verb");
4145 log_error("Operation failed: %s", strerror(-r
));
4150 for (i
= 0; i
< n_changes
; i
++) {
4151 if (changes
[i
].type
== UNIT_FILE_SYMLINK
)
4152 log_info("ln -s '%s' '%s'", changes
[i
].source
, changes
[i
].path
);
4154 log_info("rm '%s'", changes
[i
].path
);
4160 bool send_force
= true, expect_carries_install_info
= false;
4162 DBusMessageIter iter
, sub
, sub2
;
4164 if (streq(verb
, "enable")) {
4165 method
= "EnableUnitFiles";
4166 expect_carries_install_info
= true;
4167 } else if (streq(verb
, "disable")) {
4168 method
= "DisableUnitFiles";
4170 } else if (streq(verb
, "reenable")) {
4171 method
= "ReenableUnitFiles";
4172 expect_carries_install_info
= true;
4173 } else if (streq(verb
, "link"))
4174 method
= "LinkUnitFiles";
4175 else if (streq(verb
, "preset")) {
4176 method
= "PresetUnitFiles";
4177 expect_carries_install_info
= true;
4178 } else if (streq(verb
, "mask"))
4179 method
= "MaskUnitFiles";
4180 else if (streq(verb
, "unmask")) {
4181 method
= "UnmaskUnitFiles";
4184 assert_not_reached("Unknown verb");
4186 m
= dbus_message_new_method_call(
4187 "org.freedesktop.systemd1",
4188 "/org/freedesktop/systemd1",
4189 "org.freedesktop.systemd1.Manager",
4192 log_error("Out of memory");
4197 dbus_message_iter_init_append(m
, &iter
);
4199 r
= bus_append_strv_iter(&iter
, args
+1);
4201 log_error("Failed to append unit files.");
4206 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &a
)) {
4207 log_error("Failed to append runtime boolean.");
4215 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &b
)) {
4216 log_error("Failed to append force boolean.");
4222 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
4224 log_error("Failed to issue method call: %s", bus_error_message(&error
));
4229 if (!dbus_message_iter_init(reply
, &iter
)) {
4230 log_error("Failed to initialize iterator.");
4234 if (expect_carries_install_info
) {
4235 r
= bus_iter_get_basic_and_next(&iter
, DBUS_TYPE_BOOLEAN
, &b
, true);
4237 log_error("Failed to parse reply.");
4241 carries_install_info
= b
;
4244 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
4245 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
4246 log_error("Failed to parse reply.");
4251 dbus_message_iter_recurse(&iter
, &sub
);
4252 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
4253 const char *type
, *path
, *source
;
4255 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
4256 log_error("Failed to parse reply.");
4261 dbus_message_iter_recurse(&sub
, &sub2
);
4263 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) < 0 ||
4264 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) < 0 ||
4265 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &source
, false) < 0) {
4266 log_error("Failed to parse reply.");
4272 if (streq(type
, "symlink"))
4273 log_info("ln -s '%s' '%s'", source
, path
);
4275 log_info("rm '%s'", path
);
4278 dbus_message_iter_next(&sub
);
4281 /* Try to reload if enabeld */
4283 r
= daemon_reload(bus
, args
);
4286 if (carries_install_info
== 0)
4287 log_warning("The unit files have no [Install] section. They are not meant to be enabled using systemctl.");
4291 dbus_message_unref(m
);
4294 dbus_message_unref(reply
);
4296 unit_file_changes_free(changes
, n_changes
);
4298 dbus_error_free(&error
);
4302 static int unit_is_enabled(DBusConnection
*bus
, char **args
) {
4305 DBusMessage
*m
= NULL
, *reply
= NULL
;
4309 dbus_error_init(&error
);
4311 r
= enable_sysv_units(args
);
4317 if (!bus
|| avoid_bus()) {
4319 STRV_FOREACH(name
, args
+1) {
4320 UnitFileState state
;
4322 state
= unit_file_get_state(arg_scope
, arg_root
, *name
);
4328 if (state
== UNIT_FILE_ENABLED
||
4329 state
== UNIT_FILE_ENABLED_RUNTIME
||
4330 state
== UNIT_FILE_STATIC
)
4334 puts(unit_file_state_to_string(state
));
4338 STRV_FOREACH(name
, args
+1) {
4341 m
= dbus_message_new_method_call(
4342 "org.freedesktop.systemd1",
4343 "/org/freedesktop/systemd1",
4344 "org.freedesktop.systemd1.Manager",
4345 "GetUnitFileState");
4347 log_error("Out of memory");
4352 if (!dbus_message_append_args(m
,
4353 DBUS_TYPE_STRING
, name
,
4354 DBUS_TYPE_INVALID
)) {
4355 log_error("Could not append arguments to message.");
4360 reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
);
4362 log_error("Failed to issue method call: %s", bus_error_message(&error
));
4367 if (!dbus_message_get_args(reply
, &error
,
4368 DBUS_TYPE_STRING
, &s
,
4369 DBUS_TYPE_INVALID
)) {
4370 log_error("Failed to parse reply: %s", bus_error_message(&error
));
4375 dbus_message_unref(m
);
4376 dbus_message_unref(reply
);
4379 if (streq(s
, "enabled") ||
4380 streq(s
, "enabled-runtime") ||
4389 r
= enabled
? 0 : 1;
4393 dbus_message_unref(m
);
4396 dbus_message_unref(reply
);
4398 dbus_error_free(&error
);
4402 static int systemctl_help(void) {
4404 pager_open_if_enabled();
4406 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4407 "Query or send control commands to the systemd manager.\n\n"
4408 " -h --help Show this help\n"
4409 " --version Show package version\n"
4410 " -t --type=TYPE List only units of a particular type\n"
4411 " -p --property=NAME Show only properties by this name\n"
4412 " -a --all Show all units/properties, including dead/empty ones\n"
4413 " --failed Show only failed units\n"
4414 " --full Don't ellipsize unit names on output\n"
4415 " --fail When queueing a new job, fail if conflicting jobs are\n"
4417 " --ignore-dependencies\n"
4418 " When queueing a new job, ignore all its dependencies\n"
4419 " --kill-who=WHO Who to send signal to\n"
4420 " -s --signal=SIGNAL Which signal to send\n"
4421 " -H --host=[USER@]HOST\n"
4422 " Show information for remote host\n"
4423 " -P --privileged Acquire privileges before execution\n"
4424 " -q --quiet Suppress output\n"
4425 " --no-block Do not wait until operation finished\n"
4426 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4427 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4429 " --no-legend Do not print a legend (column headers and hints)\n"
4430 " --no-pager Do not pipe output into a pager\n"
4431 " --no-ask-password\n"
4432 " Do not ask for system passwords\n"
4433 " --order When generating graph for dot, show only order\n"
4434 " --require When generating graph for dot, show only requirement\n"
4435 " --system Connect to system manager\n"
4436 " --user Connect to user service manager\n"
4437 " --global Enable/disable unit files globally\n"
4438 " -f --force When enabling unit files, override existing symlinks\n"
4439 " When shutting down, execute action immediately\n"
4440 " --root=PATH Enable unit files in the specified root directory\n"
4441 " --runtime Enable unit files only temporarily until next reboot\n"
4442 " -n --lines=INTEGER Journal entries to show\n"
4443 " --follow Follow journal\n"
4444 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
4445 " verbose, export, json, cat)\n\n"
4447 " list-units List loaded units\n"
4448 " start [NAME...] Start (activate) one or more units\n"
4449 " stop [NAME...] Stop (deactivate) one or more units\n"
4450 " reload [NAME...] Reload one or more units\n"
4451 " restart [NAME...] Start or restart one or more units\n"
4452 " try-restart [NAME...] Restart one or more units if active\n"
4453 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4454 " otherwise start or restart\n"
4455 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4456 " otherwise restart if active\n"
4457 " isolate [NAME] Start one unit and stop all others\n"
4458 " kill [NAME...] Send signal to processes of a unit\n"
4459 " is-active [NAME...] Check whether units are active\n"
4460 " status [NAME...|PID...] Show runtime status of one or more units\n"
4461 " show [NAME...|JOB...] Show properties of one or more\n"
4462 " units/jobs or the manager\n"
4463 " help [NAME...|PID...] Show manual for one or more units\n"
4464 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4466 " load [NAME...] Load one or more units\n\n"
4467 "Unit File Commands:\n"
4468 " list-unit-files List installed unit files\n"
4469 " enable [NAME...] Enable one or more unit files\n"
4470 " disable [NAME...] Disable one or more unit files\n"
4471 " reenable [NAME...] Reenable one or more unit files\n"
4472 " preset [NAME...] Enable/disable one or more unit files\n"
4473 " based on preset configuration\n"
4474 " mask [NAME...] Mask one or more units\n"
4475 " unmask [NAME...] Unmask one or more units\n"
4476 " link [PATH...] Link one or more units files into\n"
4477 " the search path\n"
4478 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
4480 " list-jobs List jobs\n"
4481 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
4482 "Status Commands:\n"
4483 " dump Dump server status\n"
4484 " dot Dump dependency graph for dot(1)\n\n"
4485 "Snapshot Commands:\n"
4486 " snapshot [NAME] Create a snapshot\n"
4487 " delete [NAME...] Remove one or more snapshots\n\n"
4488 "Environment Commands:\n"
4489 " show-environment Dump environment\n"
4490 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4491 " unset-environment [NAME...] Unset one or more environment variables\n\n"
4492 "Manager Lifecycle Commands:\n"
4493 " daemon-reload Reload systemd manager configuration\n"
4494 " daemon-reexec Reexecute systemd manager\n\n"
4495 "System Commands:\n"
4496 " default Enter system default mode\n"
4497 " rescue Enter system rescue mode\n"
4498 " emergency Enter system emergency mode\n"
4499 " halt Shut down and halt the system\n"
4500 " poweroff Shut down and power-off the system\n"
4501 " reboot Shut down and reboot the system\n"
4502 " kexec Shut down and reboot the system with kexec\n"
4503 " exit Request user instance exit\n"
4504 " switch-root [ROOT] [INIT] Change to a different root file system\n"
4505 " suspend Suspend the system\n"
4506 " hibernate Hibernate the system\n",
4507 program_invocation_short_name
);
4512 static int halt_help(void) {
4514 printf("%s [OPTIONS...]\n\n"
4515 "%s the system.\n\n"
4516 " --help Show this help\n"
4517 " --halt Halt the machine\n"
4518 " -p --poweroff Switch off the machine\n"
4519 " --reboot Reboot the machine\n"
4520 " -f --force Force immediate halt/power-off/reboot\n"
4521 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4522 " -d --no-wtmp Don't write wtmp record\n"
4523 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4524 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4525 program_invocation_short_name
,
4526 arg_action
== ACTION_REBOOT
? "Reboot" :
4527 arg_action
== ACTION_POWEROFF
? "Power off" :
4533 static int shutdown_help(void) {
4535 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4536 "Shut down the system.\n\n"
4537 " --help Show this help\n"
4538 " -H --halt Halt the machine\n"
4539 " -P --poweroff Power-off the machine\n"
4540 " -r --reboot Reboot the machine\n"
4541 " -h Equivalent to --poweroff, overridden by --halt\n"
4542 " -k Don't halt/power-off/reboot, just send warnings\n"
4543 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4544 " -c Cancel a pending shutdown\n",
4545 program_invocation_short_name
);
4550 static int telinit_help(void) {
4552 printf("%s [OPTIONS...] {COMMAND}\n\n"
4553 "Send control commands to the init daemon.\n\n"
4554 " --help Show this help\n"
4555 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4557 " 0 Power-off the machine\n"
4558 " 6 Reboot the machine\n"
4559 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4560 " 1, s, S Enter rescue mode\n"
4561 " q, Q Reload init daemon configuration\n"
4562 " u, U Reexecute init daemon\n",
4563 program_invocation_short_name
);
4568 static int runlevel_help(void) {
4570 printf("%s [OPTIONS...]\n\n"
4571 "Prints the previous and current runlevel of the init system.\n\n"
4572 " --help Show this help\n",
4573 program_invocation_short_name
);
4578 static int systemctl_parse_argv(int argc
, char *argv
[]) {
4582 ARG_IGNORE_DEPENDENCIES
,
4598 ARG_NO_ASK_PASSWORD
,
4605 static const struct option options
[] = {
4606 { "help", no_argument
, NULL
, 'h' },
4607 { "version", no_argument
, NULL
, ARG_VERSION
},
4608 { "type", required_argument
, NULL
, 't' },
4609 { "property", required_argument
, NULL
, 'p' },
4610 { "all", no_argument
, NULL
, 'a' },
4611 { "failed", no_argument
, NULL
, ARG_FAILED
},
4612 { "full", no_argument
, NULL
, ARG_FULL
},
4613 { "fail", no_argument
, NULL
, ARG_FAIL
},
4614 { "ignore-dependencies", no_argument
, NULL
, ARG_IGNORE_DEPENDENCIES
},
4615 { "user", no_argument
, NULL
, ARG_USER
},
4616 { "system", no_argument
, NULL
, ARG_SYSTEM
},
4617 { "global", no_argument
, NULL
, ARG_GLOBAL
},
4618 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
4619 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
4620 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
4621 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4622 { "quiet", no_argument
, NULL
, 'q' },
4623 { "order", no_argument
, NULL
, ARG_ORDER
},
4624 { "require", no_argument
, NULL
, ARG_REQUIRE
},
4625 { "root", required_argument
, NULL
, ARG_ROOT
},
4626 { "force", no_argument
, NULL
, ARG_FORCE
},
4627 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
4628 { "kill-mode", required_argument
, NULL
, ARG_KILL_MODE
}, /* undocumented on purpose */
4629 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
4630 { "signal", required_argument
, NULL
, 's' },
4631 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
4632 { "host", required_argument
, NULL
, 'H' },
4633 { "privileged",no_argument
, NULL
, 'P' },
4634 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
4635 { "lines", required_argument
, NULL
, 'n' },
4636 { "follow", no_argument
, NULL
, ARG_FOLLOW
},
4637 { "output", required_argument
, NULL
, 'o' },
4638 { NULL
, 0, NULL
, 0 }
4646 while ((c
= getopt_long(argc
, argv
, "ht:p:aqfs:H:Pn:o:", options
, NULL
)) >= 0) {
4655 puts(PACKAGE_STRING
);
4657 puts(SYSTEMD_FEATURES
);
4661 if (unit_type_from_string(optarg
) >= 0) {
4665 if (unit_load_state_from_string(optarg
) >= 0) {
4666 arg_load_state
= optarg
;
4669 log_error("Unkown unit type or load state '%s'.",
4675 if (!(l
= strv_append(arg_property
, optarg
)))
4678 strv_free(arg_property
);
4681 /* If the user asked for a particular
4682 * property, show it to him, even if it is
4693 arg_job_mode
= "fail";
4696 case ARG_IGNORE_DEPENDENCIES
:
4697 arg_job_mode
= "ignore-dependencies";
4701 arg_scope
= UNIT_FILE_USER
;
4705 arg_scope
= UNIT_FILE_SYSTEM
;
4709 arg_scope
= UNIT_FILE_GLOBAL
;
4713 arg_no_block
= true;
4717 arg_no_legend
= true;
4721 arg_no_pager
= true;
4729 arg_dot
= DOT_ORDER
;
4733 arg_dot
= DOT_REQUIRE
;
4761 /* -f is short for both --follow and --force! */
4767 arg_no_reload
= true;
4771 arg_kill_who
= optarg
;
4775 arg_kill_mode
= optarg
;
4779 if ((arg_signal
= signal_from_string_try_harder(optarg
)) < 0) {
4780 log_error("Failed to parse signal string %s.", optarg
);
4785 case ARG_NO_ASK_PASSWORD
:
4786 arg_ask_password
= false;
4790 arg_transport
= TRANSPORT_POLKIT
;
4794 arg_transport
= TRANSPORT_SSH
;
4803 if (safe_atou(optarg
, &arg_lines
) < 0) {
4804 log_error("Failed to parse lines '%s'", optarg
);
4810 arg_output
= output_mode_from_string(optarg
);
4811 if (arg_output
< 0) {
4812 log_error("Unknown output '%s'.", optarg
);
4821 log_error("Unknown option code '%c'.", c
);
4826 if (arg_transport
!= TRANSPORT_NORMAL
&& arg_scope
!= UNIT_FILE_SYSTEM
) {
4827 log_error("Cannot access user instance remotely.");
4834 static int halt_parse_argv(int argc
, char *argv
[]) {
4843 static const struct option options
[] = {
4844 { "help", no_argument
, NULL
, ARG_HELP
},
4845 { "halt", no_argument
, NULL
, ARG_HALT
},
4846 { "poweroff", no_argument
, NULL
, 'p' },
4847 { "reboot", no_argument
, NULL
, ARG_REBOOT
},
4848 { "force", no_argument
, NULL
, 'f' },
4849 { "wtmp-only", no_argument
, NULL
, 'w' },
4850 { "no-wtmp", no_argument
, NULL
, 'd' },
4851 { "no-sync", no_argument
, NULL
, 'n' },
4852 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4853 { NULL
, 0, NULL
, 0 }
4861 if (utmp_get_runlevel(&runlevel
, NULL
) >= 0)
4862 if (runlevel
== '0' || runlevel
== '6')
4865 while ((c
= getopt_long(argc
, argv
, "pfwdnih", options
, NULL
)) >= 0) {
4873 arg_action
= ACTION_HALT
;
4877 if (arg_action
!= ACTION_REBOOT
)
4878 arg_action
= ACTION_POWEROFF
;
4882 arg_action
= ACTION_REBOOT
;
4907 /* Compatibility nops */
4914 log_error("Unknown option code '%c'.", c
);
4919 if (optind
< argc
) {
4920 log_error("Too many arguments.");
4927 static int parse_time_spec(const char *t
, usec_t
*_u
) {
4931 if (streq(t
, "now"))
4933 else if (!strchr(t
, ':')) {
4936 if (safe_atou64(t
, &u
) < 0)
4939 *_u
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
* u
;
4948 hour
= strtol(t
, &e
, 10);
4949 if (errno
!= 0 || *e
!= ':' || hour
< 0 || hour
> 23)
4952 minute
= strtol(e
+1, &e
, 10);
4953 if (errno
!= 0 || *e
!= 0 || minute
< 0 || minute
> 59)
4956 n
= now(CLOCK_REALTIME
);
4957 s
= (time_t) (n
/ USEC_PER_SEC
);
4960 assert_se(localtime_r(&s
, &tm
));
4962 tm
.tm_hour
= (int) hour
;
4963 tm
.tm_min
= (int) minute
;
4966 assert_se(s
= mktime(&tm
));
4968 *_u
= (usec_t
) s
* USEC_PER_SEC
;
4971 *_u
+= USEC_PER_DAY
;
4977 static int shutdown_parse_argv(int argc
, char *argv
[]) {
4984 static const struct option options
[] = {
4985 { "help", no_argument
, NULL
, ARG_HELP
},
4986 { "halt", no_argument
, NULL
, 'H' },
4987 { "poweroff", no_argument
, NULL
, 'P' },
4988 { "reboot", no_argument
, NULL
, 'r' },
4989 { "kexec", no_argument
, NULL
, 'K' }, /* not documented extension */
4990 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4991 { NULL
, 0, NULL
, 0 }
4999 while ((c
= getopt_long(argc
, argv
, "HPrhkt:afFc", options
, NULL
)) >= 0) {
5007 arg_action
= ACTION_HALT
;
5011 arg_action
= ACTION_POWEROFF
;
5016 arg_action
= ACTION_KEXEC
;
5018 arg_action
= ACTION_REBOOT
;
5022 arg_action
= ACTION_KEXEC
;
5026 if (arg_action
!= ACTION_HALT
)
5027 arg_action
= ACTION_POWEROFF
;
5040 /* Compatibility nops */
5044 arg_action
= ACTION_CANCEL_SHUTDOWN
;
5051 log_error("Unknown option code '%c'.", c
);
5056 if (argc
> optind
) {
5057 r
= parse_time_spec(argv
[optind
], &arg_when
);
5059 log_error("Failed to parse time specification: %s", argv
[optind
]);
5063 arg_when
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
;
5065 /* We skip the time argument */
5066 if (argc
> optind
+ 1)
5067 arg_wall
= argv
+ optind
+ 1;
5074 static int telinit_parse_argv(int argc
, char *argv
[]) {
5081 static const struct option options
[] = {
5082 { "help", no_argument
, NULL
, ARG_HELP
},
5083 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
5084 { NULL
, 0, NULL
, 0 }
5087 static const struct {
5091 { '0', ACTION_POWEROFF
},
5092 { '6', ACTION_REBOOT
},
5093 { '1', ACTION_RESCUE
},
5094 { '2', ACTION_RUNLEVEL2
},
5095 { '3', ACTION_RUNLEVEL3
},
5096 { '4', ACTION_RUNLEVEL4
},
5097 { '5', ACTION_RUNLEVEL5
},
5098 { 's', ACTION_RESCUE
},
5099 { 'S', ACTION_RESCUE
},
5100 { 'q', ACTION_RELOAD
},
5101 { 'Q', ACTION_RELOAD
},
5102 { 'u', ACTION_REEXEC
},
5103 { 'U', ACTION_REEXEC
}
5112 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
5127 log_error("Unknown option code '%c'.", c
);
5132 if (optind
>= argc
) {
5137 if (optind
+ 1 < argc
) {
5138 log_error("Too many arguments.");
5142 if (strlen(argv
[optind
]) != 1) {
5143 log_error("Expected single character argument.");
5147 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
5148 if (table
[i
].from
== argv
[optind
][0])
5151 if (i
>= ELEMENTSOF(table
)) {
5152 log_error("Unknown command '%s'.", argv
[optind
]);
5156 arg_action
= table
[i
].to
;
5163 static int runlevel_parse_argv(int argc
, char *argv
[]) {
5169 static const struct option options
[] = {
5170 { "help", no_argument
, NULL
, ARG_HELP
},
5171 { NULL
, 0, NULL
, 0 }
5179 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
5190 log_error("Unknown option code '%c'.", c
);
5195 if (optind
< argc
) {
5196 log_error("Too many arguments.");
5203 static int parse_argv(int argc
, char *argv
[]) {
5207 if (program_invocation_short_name
) {
5209 if (strstr(program_invocation_short_name
, "halt")) {
5210 arg_action
= ACTION_HALT
;
5211 return halt_parse_argv(argc
, argv
);
5212 } else if (strstr(program_invocation_short_name
, "poweroff")) {
5213 arg_action
= ACTION_POWEROFF
;
5214 return halt_parse_argv(argc
, argv
);
5215 } else if (strstr(program_invocation_short_name
, "reboot")) {
5217 arg_action
= ACTION_KEXEC
;
5219 arg_action
= ACTION_REBOOT
;
5220 return halt_parse_argv(argc
, argv
);
5221 } else if (strstr(program_invocation_short_name
, "shutdown")) {
5222 arg_action
= ACTION_POWEROFF
;
5223 return shutdown_parse_argv(argc
, argv
);
5224 } else if (strstr(program_invocation_short_name
, "init")) {
5226 if (sd_booted() > 0) {
5227 arg_action
= ACTION_INVALID
;
5228 return telinit_parse_argv(argc
, argv
);
5230 /* Hmm, so some other init system is
5231 * running, we need to forward this
5232 * request to it. For now we simply
5233 * guess that it is Upstart. */
5235 execv("/lib/upstart/telinit", argv
);
5237 log_error("Couldn't find an alternative telinit implementation to spawn.");
5241 } else if (strstr(program_invocation_short_name
, "runlevel")) {
5242 arg_action
= ACTION_RUNLEVEL
;
5243 return runlevel_parse_argv(argc
, argv
);
5247 arg_action
= ACTION_SYSTEMCTL
;
5248 return systemctl_parse_argv(argc
, argv
);
5251 static int action_to_runlevel(void) {
5253 static const char table
[_ACTION_MAX
] = {
5254 [ACTION_HALT
] = '0',
5255 [ACTION_POWEROFF
] = '0',
5256 [ACTION_REBOOT
] = '6',
5257 [ACTION_RUNLEVEL2
] = '2',
5258 [ACTION_RUNLEVEL3
] = '3',
5259 [ACTION_RUNLEVEL4
] = '4',
5260 [ACTION_RUNLEVEL5
] = '5',
5261 [ACTION_RESCUE
] = '1'
5264 assert(arg_action
< _ACTION_MAX
);
5266 return table
[arg_action
];
5269 static int talk_upstart(void) {
5270 DBusMessage
*m
= NULL
, *reply
= NULL
;
5272 int previous
, rl
, r
;
5274 env1_buf
[] = "RUNLEVEL=X",
5275 env2_buf
[] = "PREVLEVEL=X";
5276 char *env1
= env1_buf
, *env2
= env2_buf
;
5277 const char *emit
= "runlevel";
5278 dbus_bool_t b_false
= FALSE
;
5279 DBusMessageIter iter
, sub
;
5280 DBusConnection
*bus
;
5282 dbus_error_init(&error
);
5284 if (!(rl
= action_to_runlevel()))
5287 if (utmp_get_runlevel(&previous
, NULL
) < 0)
5290 if (!(bus
= dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error
))) {
5291 if (dbus_error_has_name(&error
, DBUS_ERROR_NO_SERVER
)) {
5296 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error
));
5301 if ((r
= bus_check_peercred(bus
)) < 0) {
5302 log_error("Failed to verify owner of bus.");
5306 if (!(m
= dbus_message_new_method_call(
5307 "com.ubuntu.Upstart",
5308 "/com/ubuntu/Upstart",
5309 "com.ubuntu.Upstart0_6",
5312 log_error("Could not allocate message.");
5317 dbus_message_iter_init_append(m
, &iter
);
5319 env1_buf
[sizeof(env1_buf
)-2] = rl
;
5320 env2_buf
[sizeof(env2_buf
)-2] = previous
;
5322 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &emit
) ||
5323 !dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
) ||
5324 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env1
) ||
5325 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env2
) ||
5326 !dbus_message_iter_close_container(&iter
, &sub
) ||
5327 !dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &b_false
)) {
5328 log_error("Could not append arguments to message.");
5333 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
5335 if (error_is_no_service(&error
)) {
5340 log_error("Failed to issue method call: %s", bus_error_message(&error
));
5349 dbus_message_unref(m
);
5352 dbus_message_unref(reply
);
5355 dbus_connection_flush(bus
);
5356 dbus_connection_close(bus
);
5357 dbus_connection_unref(bus
);
5360 dbus_error_free(&error
);
5365 static int talk_initctl(void) {
5366 struct init_request request
;
5370 if (!(rl
= action_to_runlevel()))
5374 request
.magic
= INIT_MAGIC
;
5375 request
.sleeptime
= 0;
5376 request
.cmd
= INIT_CMD_RUNLVL
;
5377 request
.runlevel
= rl
;
5379 if ((fd
= open(INIT_FIFO
, O_WRONLY
|O_NDELAY
|O_CLOEXEC
|O_NOCTTY
)) < 0) {
5381 if (errno
== ENOENT
)
5384 log_error("Failed to open "INIT_FIFO
": %m");
5389 r
= loop_write(fd
, &request
, sizeof(request
), false) != sizeof(request
);
5390 close_nointr_nofail(fd
);
5393 log_error("Failed to write to "INIT_FIFO
": %m");
5394 return errno
? -errno
: -EIO
;
5400 static int systemctl_main(DBusConnection
*bus
, int argc
, char *argv
[], DBusError
*error
) {
5402 static const struct {
5410 int (* const dispatch
)(DBusConnection
*bus
, char **args
);
5412 { "list-units", LESS
, 1, list_units
},
5413 { "list-unit-files", EQUAL
, 1, list_unit_files
},
5414 { "list-jobs", EQUAL
, 1, list_jobs
},
5415 { "clear-jobs", EQUAL
, 1, daemon_reload
},
5416 { "load", MORE
, 2, load_unit
},
5417 { "cancel", MORE
, 2, cancel_job
},
5418 { "start", MORE
, 2, start_unit
},
5419 { "stop", MORE
, 2, start_unit
},
5420 { "condstop", MORE
, 2, start_unit
}, /* For compatibility with ALTLinux */
5421 { "reload", MORE
, 2, start_unit
},
5422 { "restart", MORE
, 2, start_unit
},
5423 { "try-restart", MORE
, 2, start_unit
},
5424 { "reload-or-restart", MORE
, 2, start_unit
},
5425 { "reload-or-try-restart", MORE
, 2, start_unit
},
5426 { "force-reload", MORE
, 2, start_unit
}, /* For compatibility with SysV */
5427 { "condreload", MORE
, 2, start_unit
}, /* For compatibility with ALTLinux */
5428 { "condrestart", MORE
, 2, start_unit
}, /* For compatibility with RH */
5429 { "isolate", EQUAL
, 2, start_unit
},
5430 { "kill", MORE
, 2, kill_unit
},
5431 { "is-active", MORE
, 2, check_unit
},
5432 { "check", MORE
, 2, check_unit
},
5433 { "show", MORE
, 1, show
},
5434 { "status", MORE
, 2, show
},
5435 { "help", MORE
, 2, show
},
5436 { "dump", EQUAL
, 1, dump
},
5437 { "dot", EQUAL
, 1, dot
},
5438 { "snapshot", LESS
, 2, snapshot
},
5439 { "delete", MORE
, 2, delete_snapshot
},
5440 { "daemon-reload", EQUAL
, 1, daemon_reload
},
5441 { "daemon-reexec", EQUAL
, 1, daemon_reload
},
5442 { "show-environment", EQUAL
, 1, show_enviroment
},
5443 { "set-environment", MORE
, 2, set_environment
},
5444 { "unset-environment", MORE
, 2, set_environment
},
5445 { "halt", EQUAL
, 1, start_special
},
5446 { "poweroff", EQUAL
, 1, start_special
},
5447 { "reboot", EQUAL
, 1, start_special
},
5448 { "kexec", EQUAL
, 1, start_special
},
5449 { "suspend", EQUAL
, 1, start_special
},
5450 { "hibernate", EQUAL
, 1, start_special
},
5451 { "default", EQUAL
, 1, start_special
},
5452 { "rescue", EQUAL
, 1, start_special
},
5453 { "emergency", EQUAL
, 1, start_special
},
5454 { "exit", EQUAL
, 1, start_special
},
5455 { "reset-failed", MORE
, 1, reset_failed
},
5456 { "enable", MORE
, 2, enable_unit
},
5457 { "disable", MORE
, 2, enable_unit
},
5458 { "is-enabled", MORE
, 2, unit_is_enabled
},
5459 { "reenable", MORE
, 2, enable_unit
},
5460 { "preset", MORE
, 2, enable_unit
},
5461 { "mask", MORE
, 2, enable_unit
},
5462 { "unmask", MORE
, 2, enable_unit
},
5463 { "link", MORE
, 2, enable_unit
},
5464 { "switch-root", MORE
, 2, switch_root
},
5474 left
= argc
- optind
;
5477 /* Special rule: no arguments means "list-units" */
5480 if (streq(argv
[optind
], "help") && !argv
[optind
+1]) {
5481 log_error("This command expects one or more "
5482 "unit names. Did you mean --help?");
5486 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
5487 if (streq(argv
[optind
], verbs
[i
].verb
))
5490 if (i
>= ELEMENTSOF(verbs
)) {
5491 log_error("Unknown operation '%s'.", argv
[optind
]);
5496 switch (verbs
[i
].argc_cmp
) {
5499 if (left
!= verbs
[i
].argc
) {
5500 log_error("Invalid number of arguments.");
5507 if (left
< verbs
[i
].argc
) {
5508 log_error("Too few arguments.");
5515 if (left
> verbs
[i
].argc
) {
5516 log_error("Too many arguments.");
5523 assert_not_reached("Unknown comparison operator.");
5526 /* Require a bus connection for all operations but
5528 if (!streq(verbs
[i
].verb
, "enable") &&
5529 !streq(verbs
[i
].verb
, "disable") &&
5530 !streq(verbs
[i
].verb
, "is-enabled") &&
5531 !streq(verbs
[i
].verb
, "list-unit-files") &&
5532 !streq(verbs
[i
].verb
, "reenable") &&
5533 !streq(verbs
[i
].verb
, "preset") &&
5534 !streq(verbs
[i
].verb
, "mask") &&
5535 !streq(verbs
[i
].verb
, "unmask") &&
5536 !streq(verbs
[i
].verb
, "link")) {
5538 if (running_in_chroot() > 0) {
5539 log_info("Running in chroot, ignoring request.");
5543 if (((!streq(verbs
[i
].verb
, "reboot") &&
5544 !streq(verbs
[i
].verb
, "halt") &&
5545 !streq(verbs
[i
].verb
, "poweroff")) || arg_force
<= 0) && !bus
) {
5546 log_error("Failed to get D-Bus connection: %s",
5547 dbus_error_is_set(error
) ? error
->message
: "No connection to service manager.");
5553 if (!bus
&& !avoid_bus()) {
5554 log_error("Failed to get D-Bus connection: %s",
5555 dbus_error_is_set(error
) ? error
->message
: "No connection to service manager.");
5560 return verbs
[i
].dispatch(bus
, argv
+ optind
);
5563 static int send_shutdownd(usec_t t
, char mode
, bool dry_run
, bool warn
, const char *message
) {
5565 struct msghdr msghdr
;
5566 struct iovec iovec
[2];
5567 union sockaddr_union sockaddr
;
5568 struct sd_shutdown_command c
;
5570 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
5577 c
.dry_run
= dry_run
;
5581 sockaddr
.sa
.sa_family
= AF_UNIX
;
5582 strncpy(sockaddr
.un
.sun_path
, "/run/systemd/shutdownd", sizeof(sockaddr
.un
.sun_path
));
5585 msghdr
.msg_name
= &sockaddr
;
5586 msghdr
.msg_namelen
= offsetof(struct sockaddr_un
, sun_path
) + sizeof("/run/systemd/shutdownd") - 1;
5589 iovec
[0].iov_base
= (char*) &c
;
5590 iovec
[0].iov_len
= offsetof(struct sd_shutdown_command
, wall_message
);
5592 if (isempty(message
))
5593 msghdr
.msg_iovlen
= 1;
5595 iovec
[1].iov_base
= (char*) message
;
5596 iovec
[1].iov_len
= strlen(message
);
5597 msghdr
.msg_iovlen
= 2;
5599 msghdr
.msg_iov
= iovec
;
5601 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) < 0) {
5602 close_nointr_nofail(fd
);
5606 close_nointr_nofail(fd
);
5610 static int reload_with_fallback(DBusConnection
*bus
) {
5613 /* First, try systemd via D-Bus. */
5614 if (daemon_reload(bus
, NULL
) >= 0)
5618 /* Nothing else worked, so let's try signals */
5619 assert(arg_action
== ACTION_RELOAD
|| arg_action
== ACTION_REEXEC
);
5621 if (kill(1, arg_action
== ACTION_RELOAD
? SIGHUP
: SIGTERM
) < 0) {
5622 log_error("kill() failed: %m");
5629 static int start_with_fallback(DBusConnection
*bus
) {
5632 /* First, try systemd via D-Bus. */
5633 if (start_unit(bus
, NULL
) >= 0)
5637 /* Hmm, talking to systemd via D-Bus didn't work. Then
5638 * let's try to talk to Upstart via D-Bus. */
5639 if (talk_upstart() > 0)
5642 /* Nothing else worked, so let's try
5644 if (talk_initctl() > 0)
5647 log_error("Failed to talk to init daemon.");
5651 warn_wall(arg_action
);
5655 static void halt_now(enum action a
) {
5657 /* Make sure C-A-D is handled by the kernel from this
5659 reboot(RB_ENABLE_CAD
);
5664 log_info("Halting.");
5665 reboot(RB_HALT_SYSTEM
);
5668 case ACTION_POWEROFF
:
5669 log_info("Powering off.");
5670 reboot(RB_POWER_OFF
);
5674 log_info("Rebooting.");
5675 reboot(RB_AUTOBOOT
);
5679 assert_not_reached("Unknown halt action.");
5682 assert_not_reached("Uh? This shouldn't happen.");
5685 static int halt_main(DBusConnection
*bus
) {
5688 if (geteuid() != 0) {
5689 /* Try logind if we are a normal user and no special
5690 * mode applies. Maybe PolicyKit allows us to shutdown
5693 if (arg_when
<= 0 &&
5696 (arg_action
== ACTION_POWEROFF
||
5697 arg_action
== ACTION_REBOOT
)) {
5698 r
= reboot_with_logind(bus
, arg_action
);
5703 log_error("Must be root.");
5710 m
= strv_join(arg_wall
, " ");
5711 r
= send_shutdownd(arg_when
,
5712 arg_action
== ACTION_HALT
? 'H' :
5713 arg_action
== ACTION_POWEROFF
? 'P' :
5714 arg_action
== ACTION_KEXEC
? 'K' :
5722 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r
));
5724 char date
[FORMAT_TIMESTAMP_MAX
];
5726 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5727 format_timestamp(date
, sizeof(date
), arg_when
));
5732 if (!arg_dry
&& !arg_force
)
5733 return start_with_fallback(bus
);
5736 if (sd_booted() > 0)
5737 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5739 r
= utmp_put_shutdown();
5741 log_warning("Failed to write utmp record: %s", strerror(-r
));
5751 halt_now(arg_action
);
5752 /* We should never reach this. */
5756 static int runlevel_main(void) {
5757 int r
, runlevel
, previous
;
5759 r
= utmp_get_runlevel(&runlevel
, &previous
);
5766 previous
<= 0 ? 'N' : previous
,
5767 runlevel
<= 0 ? 'N' : runlevel
);
5772 int main(int argc
, char*argv
[]) {
5773 int r
, retval
= EXIT_FAILURE
;
5774 DBusConnection
*bus
= NULL
;
5777 dbus_error_init(&error
);
5779 log_parse_environment();
5782 r
= parse_argv(argc
, argv
);
5786 retval
= EXIT_SUCCESS
;
5790 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5791 * let's shortcut this */
5792 if (arg_action
== ACTION_RUNLEVEL
) {
5793 r
= runlevel_main();
5794 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5798 if (running_in_chroot() > 0 && arg_action
!= ACTION_SYSTEMCTL
) {
5799 log_info("Running in chroot, ignoring request.");
5805 if (arg_transport
== TRANSPORT_NORMAL
)
5806 bus_connect(arg_scope
== UNIT_FILE_SYSTEM
? DBUS_BUS_SYSTEM
: DBUS_BUS_SESSION
, &bus
, &private_bus
, &error
);
5807 else if (arg_transport
== TRANSPORT_POLKIT
) {
5808 bus_connect_system_polkit(&bus
, &error
);
5809 private_bus
= false;
5810 } else if (arg_transport
== TRANSPORT_SSH
) {
5811 bus_connect_system_ssh(NULL
, arg_host
, &bus
, &error
);
5812 private_bus
= false;
5814 assert_not_reached("Uh, invalid transport...");
5817 switch (arg_action
) {
5819 case ACTION_SYSTEMCTL
:
5820 r
= systemctl_main(bus
, argc
, argv
, &error
);
5824 case ACTION_POWEROFF
:
5830 case ACTION_RUNLEVEL2
:
5831 case ACTION_RUNLEVEL3
:
5832 case ACTION_RUNLEVEL4
:
5833 case ACTION_RUNLEVEL5
:
5835 case ACTION_EMERGENCY
:
5836 case ACTION_DEFAULT
:
5837 r
= start_with_fallback(bus
);
5842 r
= reload_with_fallback(bus
);
5845 case ACTION_CANCEL_SHUTDOWN
:
5846 r
= send_shutdownd(0, 0, false, false, NULL
);
5849 case ACTION_INVALID
:
5850 case ACTION_RUNLEVEL
:
5852 assert_not_reached("Unknown action");
5855 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5859 dbus_connection_flush(bus
);
5860 dbus_connection_close(bus
);
5861 dbus_connection_unref(bus
);
5864 dbus_error_free(&error
);
5868 strv_free(arg_property
);
5871 ask_password_agent_close();
5872 polkit_agent_close();