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 General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
37 #include <dbus/dbus.h>
43 #include "utmp-wtmp.h"
47 #include "dbus-common.h"
48 #include "cgroup-show.h"
49 #include "cgroup-util.h"
51 #include "path-lookup.h"
52 #include "conf-parser.h"
53 #include "sd-daemon.h"
54 #include "shutdownd.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
58 #include "unit-name.h"
60 static const char *arg_type
= NULL
;
61 static char **arg_property
= NULL
;
62 static bool arg_all
= false;
63 static const char *arg_job_mode
= "replace";
64 static bool arg_user
= false;
65 static bool arg_global
= false;
66 static bool arg_immediate
= false;
67 static bool arg_no_block
= false;
68 static bool arg_no_pager
= false;
69 static bool arg_no_wtmp
= false;
70 static bool arg_no_sync
= false;
71 static bool arg_no_wall
= false;
72 static bool arg_no_reload
= false;
73 static bool arg_dry
= false;
74 static bool arg_quiet
= false;
75 static bool arg_full
= false;
76 static bool arg_force
= false;
77 static bool arg_defaults
= false;
78 static bool arg_ask_password
= false;
79 static bool arg_failed
= false;
80 static char **arg_wall
= NULL
;
81 static const char *arg_kill_who
= NULL
;
82 static const char *arg_kill_mode
= NULL
;
83 static int arg_signal
= SIGTERM
;
84 static usec_t arg_when
= 0;
103 ACTION_CANCEL_SHUTDOWN
,
105 } arg_action
= ACTION_SYSTEMCTL
;
112 static bool private_bus
= false;
114 static pid_t pager_pid
= 0;
115 static pid_t agent_pid
= 0;
117 static int daemon_reload(DBusConnection
*bus
, char **args
, unsigned n
);
118 static void pager_open(void);
120 static bool on_tty(void) {
123 /* Note that this is invoked relatively early, before we start
124 * the pager. That means the value we return reflects whether
125 * we originally were started on a tty, not if we currently
126 * are. But this is intended, since we want colour and so on
127 * when run in our own pager. */
129 if (_unlikely_(t
< 0))
130 t
= isatty(STDOUT_FILENO
) > 0;
135 static void spawn_ask_password_agent(void) {
141 /* We check STDIN here, not STDOUT, since this is about input,
143 if (!isatty(STDIN_FILENO
))
146 if (!arg_ask_password
)
154 /* Spawns a temporary TTY agent, making sure it goes away when
157 if ((agent_pid
= fork()) < 0)
160 if (agent_pid
== 0) {
164 bool stdout_is_tty
, stderr_is_tty
;
166 /* Make sure the agent goes away when the parent dies */
167 if (prctl(PR_SET_PDEATHSIG
, SIGTERM
) < 0)
170 /* Check whether our parent died before we were able
171 * to set the death signal */
172 if (getppid() != parent
)
175 /* Don't leak fds to the agent */
176 close_all_fds(NULL
, 0);
178 stdout_is_tty
= isatty(STDOUT_FILENO
);
179 stderr_is_tty
= isatty(STDERR_FILENO
);
181 if (!stdout_is_tty
|| !stderr_is_tty
) {
182 /* Detach from stdout/stderr. and reopen
183 * /dev/tty for them. This is important to
184 * ensure that when systemctl is started via
185 * popen() or a similar call that expects to
186 * read EOF we actually do generate EOF and
187 * not delay this indefinitely by because we
188 * keep an unused copy of stdin around. */
189 if ((fd
= open("/dev/tty", O_WRONLY
)) < 0) {
190 log_error("Failed to open /dev/tty: %m");
195 dup2(fd
, STDOUT_FILENO
);
198 dup2(fd
, STDERR_FILENO
);
204 execl(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH
, SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH
, "--watch", NULL
);
206 log_error("Unable to execute agent: %m");
211 static const char *ansi_highlight(bool b
) {
216 return b
? ANSI_HIGHLIGHT_ON
: ANSI_HIGHLIGHT_OFF
;
219 static const char *ansi_highlight_green(bool b
) {
224 return b
? ANSI_HIGHLIGHT_GREEN_ON
: ANSI_HIGHLIGHT_OFF
;
227 static bool error_is_no_service(const DBusError
*error
) {
230 if (!dbus_error_is_set(error
))
233 if (dbus_error_has_name(error
, DBUS_ERROR_NAME_HAS_NO_OWNER
))
236 if (dbus_error_has_name(error
, DBUS_ERROR_SERVICE_UNKNOWN
))
239 return startswith(error
->name
, "org.freedesktop.DBus.Error.Spawn.");
242 static int translate_bus_error_to_exit_status(int r
, const DBusError
*error
) {
245 if (!dbus_error_is_set(error
))
248 if (dbus_error_has_name(error
, DBUS_ERROR_ACCESS_DENIED
) ||
249 dbus_error_has_name(error
, BUS_ERROR_ONLY_BY_DEPENDENCY
) ||
250 dbus_error_has_name(error
, BUS_ERROR_NO_ISOLATION
) ||
251 dbus_error_has_name(error
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
))
252 return EXIT_NOPERMISSION
;
254 if (dbus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
))
255 return EXIT_NOTINSTALLED
;
257 if (dbus_error_has_name(error
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
) ||
258 dbus_error_has_name(error
, BUS_ERROR_NOT_SUPPORTED
))
259 return EXIT_NOTIMPLEMENTED
;
261 if (dbus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
))
262 return EXIT_NOTCONFIGURED
;
270 static int bus_iter_get_basic_and_next(DBusMessageIter
*iter
, int type
, void *data
, bool next
) {
275 if (dbus_message_iter_get_arg_type(iter
) != type
)
278 dbus_message_iter_get_basic(iter
, data
);
280 if (!dbus_message_iter_next(iter
) != !next
)
286 static void warn_wall(enum action action
) {
287 static const char *table
[_ACTION_MAX
] = {
288 [ACTION_HALT
] = "The system is going down for system halt NOW!",
289 [ACTION_REBOOT
] = "The system is going down for reboot NOW!",
290 [ACTION_POWEROFF
] = "The system is going down for power-off NOW!",
291 [ACTION_KEXEC
] = "The system is going down for kexec reboot NOW!",
292 [ACTION_RESCUE
] = "The system is going down to rescue mode NOW!",
293 [ACTION_EMERGENCY
] = "The system is going down to emergency mode NOW!"
302 if (!(p
= strv_join(arg_wall
, " "))) {
303 log_error("Failed to join strings.");
319 utmp_wall(table
[action
], NULL
);
324 const char *description
;
325 const char *load_state
;
326 const char *active_state
;
327 const char *sub_state
;
328 const char *following
;
329 const char *unit_path
;
331 const char *job_type
;
332 const char *job_path
;
335 static int compare_unit_info(const void *a
, const void *b
) {
337 const struct unit_info
*u
= a
, *v
= b
;
339 d1
= strrchr(u
->id
, '.');
340 d2
= strrchr(v
->id
, '.');
345 if ((r
= strcasecmp(d1
, d2
)) != 0)
349 return strcasecmp(u
->id
, v
->id
);
352 static bool output_show_unit(const struct unit_info
*u
) {
356 return streq(u
->active_state
, "failed");
358 return (!arg_type
|| ((dot
= strrchr(u
->id
, '.')) &&
359 streq(dot
+1, arg_type
))) &&
360 (arg_all
|| !(streq(u
->active_state
, "inactive") || u
->following
[0]) || u
->job_id
> 0);
363 static void output_units_list(const struct unit_info
*unit_infos
, unsigned c
) {
364 unsigned active_len
, sub_len
, job_len
, n_shown
= 0;
365 const struct unit_info
*u
;
367 active_len
= sizeof("ACTIVE")-1;
368 sub_len
= sizeof("SUB")-1;
369 job_len
= sizeof("JOB")-1;
371 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
372 if (!output_show_unit(u
))
375 active_len
= MAX(active_len
, strlen(u
->active_state
));
376 sub_len
= MAX(sub_len
, strlen(u
->sub_state
));
378 job_len
= MAX(job_len
, strlen(u
->job_type
));
382 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
383 active_len
, "ACTIVE", sub_len
, "SUB", job_len
, "JOB");
384 if (columns() >= 80+12 || arg_full
)
385 printf(" %s\n", "DESCRIPTION");
390 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
393 const char *on_loaded
, *off_loaded
;
394 const char *on_active
, *off_active
;
396 if (!output_show_unit(u
))
401 if (!streq(u
->load_state
, "loaded") &&
402 !streq(u
->load_state
, "banned")) {
403 on_loaded
= ansi_highlight(true);
404 off_loaded
= ansi_highlight(false);
406 on_loaded
= off_loaded
= "";
408 if (streq(u
->active_state
, "failed")) {
409 on_active
= ansi_highlight(true);
410 off_active
= ansi_highlight(false);
412 on_active
= off_active
= "";
414 e
= arg_full
? NULL
: ellipsize(u
->id
, 25, 33);
416 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
418 on_loaded
, u
->load_state
, off_loaded
,
419 on_active
, active_len
, u
->active_state
,
420 sub_len
, u
->sub_state
, off_active
,
425 a
-= strlen(on_loaded
) + strlen(off_loaded
);
426 a
-= strlen(on_active
) + strlen(off_active
);
429 printf(" %-*s", job_len
, u
->job_type
);
433 if (a
+ b
+ 1 < columns()) {
435 printf(" %-*s", job_len
, "");
438 printf(" %s", u
->description
);
440 printf(" %.*s", columns() - a
- b
- 1, u
->description
);
447 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
448 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
449 "SUB = The low-level unit activation state, values depend on unit type.\n"
450 "JOB = Pending job for the unit.\n");
453 printf("\n%u units listed.\n", n_shown
);
455 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown
);
459 static int list_units(DBusConnection
*bus
, char **args
, unsigned n
) {
460 DBusMessage
*m
= NULL
, *reply
= NULL
;
463 DBusMessageIter iter
, sub
, sub2
;
464 unsigned c
= 0, n_units
= 0;
465 struct unit_info
*unit_infos
= NULL
;
467 dbus_error_init(&error
);
473 if (!(m
= dbus_message_new_method_call(
474 "org.freedesktop.systemd1",
475 "/org/freedesktop/systemd1",
476 "org.freedesktop.systemd1.Manager",
478 log_error("Could not allocate message.");
482 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
483 log_error("Failed to issue method call: %s", bus_error_message(&error
));
488 if (!dbus_message_iter_init(reply
, &iter
) ||
489 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
490 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
491 log_error("Failed to parse reply.");
496 dbus_message_iter_recurse(&iter
, &sub
);
498 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
501 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
502 log_error("Failed to parse reply.");
510 n_units
= MAX(2*c
, 16);
511 w
= realloc(unit_infos
, sizeof(struct unit_info
) * n_units
);
514 log_error("Failed to allocate unit array.");
524 dbus_message_iter_recurse(&sub
, &sub2
);
526 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->id
, true) < 0 ||
527 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->description
, true) < 0 ||
528 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->load_state
, true) < 0 ||
529 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->active_state
, true) < 0 ||
530 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->sub_state
, true) < 0 ||
531 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->following
, true) < 0 ||
532 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->unit_path
, true) < 0 ||
533 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &u
->job_id
, true) < 0 ||
534 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->job_type
, true) < 0 ||
535 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->job_path
, false) < 0) {
536 log_error("Failed to parse reply.");
541 dbus_message_iter_next(&sub
);
546 qsort(unit_infos
, c
, sizeof(struct unit_info
), compare_unit_info
);
547 output_units_list(unit_infos
, c
);
554 dbus_message_unref(m
);
557 dbus_message_unref(reply
);
561 dbus_error_free(&error
);
566 static int dot_one_property(const char *name
, const char *prop
, DBusMessageIter
*iter
) {
567 static const char * const colors
[] = {
568 "Requires", "[color=\"black\"]",
569 "RequiresOverridable", "[color=\"black\"]",
570 "Requisite", "[color=\"darkblue\"]",
571 "RequisiteOverridable", "[color=\"darkblue\"]",
572 "Wants", "[color=\"darkgrey\"]",
573 "Conflicts", "[color=\"red\"]",
574 "ConflictedBy", "[color=\"red\"]",
575 "After", "[color=\"green\"]"
578 const char *c
= NULL
;
585 for (i
= 0; i
< ELEMENTSOF(colors
); i
+= 2)
586 if (streq(colors
[i
], prop
)) {
594 if (arg_dot
!= DOT_ALL
)
595 if ((arg_dot
== DOT_ORDER
) != streq(prop
, "After"))
598 switch (dbus_message_iter_get_arg_type(iter
)) {
600 case DBUS_TYPE_ARRAY
:
602 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
605 dbus_message_iter_recurse(iter
, &sub
);
607 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
610 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
611 dbus_message_iter_get_basic(&sub
, &s
);
612 printf("\t\"%s\"->\"%s\" %s;\n", name
, s
, c
);
614 dbus_message_iter_next(&sub
);
624 static int dot_one(DBusConnection
*bus
, const char *name
, const char *path
) {
625 DBusMessage
*m
= NULL
, *reply
= NULL
;
626 const char *interface
= "org.freedesktop.systemd1.Unit";
629 DBusMessageIter iter
, sub
, sub2
, sub3
;
634 dbus_error_init(&error
);
636 if (!(m
= dbus_message_new_method_call(
637 "org.freedesktop.systemd1",
639 "org.freedesktop.DBus.Properties",
641 log_error("Could not allocate message.");
646 if (!dbus_message_append_args(m
,
647 DBUS_TYPE_STRING
, &interface
,
648 DBUS_TYPE_INVALID
)) {
649 log_error("Could not append arguments to message.");
654 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
655 log_error("Failed to issue method call: %s", bus_error_message(&error
));
660 if (!dbus_message_iter_init(reply
, &iter
) ||
661 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
662 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
663 log_error("Failed to parse reply.");
668 dbus_message_iter_recurse(&iter
, &sub
);
670 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
673 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
674 log_error("Failed to parse reply.");
679 dbus_message_iter_recurse(&sub
, &sub2
);
681 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &prop
, true) < 0) {
682 log_error("Failed to parse reply.");
687 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
688 log_error("Failed to parse reply.");
693 dbus_message_iter_recurse(&sub2
, &sub3
);
695 if (dot_one_property(name
, prop
, &sub3
)) {
696 log_error("Failed to parse reply.");
701 dbus_message_iter_next(&sub
);
708 dbus_message_unref(m
);
711 dbus_message_unref(reply
);
713 dbus_error_free(&error
);
718 static int dot(DBusConnection
*bus
, char **args
, unsigned n
) {
719 DBusMessage
*m
= NULL
, *reply
= NULL
;
722 DBusMessageIter iter
, sub
, sub2
;
724 dbus_error_init(&error
);
728 if (!(m
= dbus_message_new_method_call(
729 "org.freedesktop.systemd1",
730 "/org/freedesktop/systemd1",
731 "org.freedesktop.systemd1.Manager",
733 log_error("Could not allocate message.");
737 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
738 log_error("Failed to issue method call: %s", bus_error_message(&error
));
743 if (!dbus_message_iter_init(reply
, &iter
) ||
744 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
745 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
746 log_error("Failed to parse reply.");
751 printf("digraph systemd {\n");
753 dbus_message_iter_recurse(&iter
, &sub
);
754 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
755 const char *id
, *description
, *load_state
, *active_state
, *sub_state
, *following
, *unit_path
;
757 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
758 log_error("Failed to parse reply.");
763 dbus_message_iter_recurse(&sub
, &sub2
);
765 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &id
, true) < 0 ||
766 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &description
, true) < 0 ||
767 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &load_state
, true) < 0 ||
768 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &active_state
, true) < 0 ||
769 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &sub_state
, true) < 0 ||
770 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &following
, true) < 0 ||
771 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, true) < 0) {
772 log_error("Failed to parse reply.");
777 if ((r
= dot_one(bus
, id
, unit_path
)) < 0)
780 /* printf("\t\"%s\";\n", id); */
781 dbus_message_iter_next(&sub
);
786 log_info(" Color legend: black = Requires\n"
787 " dark blue = Requisite\n"
788 " dark grey = Wants\n"
792 if (isatty(fileno(stdout
)))
793 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
794 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
800 dbus_message_unref(m
);
803 dbus_message_unref(reply
);
805 dbus_error_free(&error
);
810 static int list_jobs(DBusConnection
*bus
, char **args
, unsigned n
) {
811 DBusMessage
*m
= NULL
, *reply
= NULL
;
814 DBusMessageIter iter
, sub
, sub2
;
817 dbus_error_init(&error
);
823 if (!(m
= dbus_message_new_method_call(
824 "org.freedesktop.systemd1",
825 "/org/freedesktop/systemd1",
826 "org.freedesktop.systemd1.Manager",
828 log_error("Could not allocate message.");
832 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
833 log_error("Failed to issue method call: %s", bus_error_message(&error
));
838 if (!dbus_message_iter_init(reply
, &iter
) ||
839 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
840 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
841 log_error("Failed to parse reply.");
846 dbus_message_iter_recurse(&iter
, &sub
);
848 if (isatty(STDOUT_FILENO
))
849 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
851 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
852 const char *name
, *type
, *state
, *job_path
, *unit_path
;
856 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
857 log_error("Failed to parse reply.");
862 dbus_message_iter_recurse(&sub
, &sub2
);
864 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &id
, true) < 0 ||
865 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0 ||
866 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) < 0 ||
867 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &state
, true) < 0 ||
868 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &job_path
, true) < 0 ||
869 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, false) < 0) {
870 log_error("Failed to parse reply.");
875 e
= arg_full
? NULL
: ellipsize(name
, 25, 33);
876 printf("%4u %-25s %-15s %-7s\n", id
, e
? e
: name
, type
, state
);
881 dbus_message_iter_next(&sub
);
884 if (isatty(STDOUT_FILENO
))
885 printf("\n%u jobs listed.\n", k
);
891 dbus_message_unref(m
);
894 dbus_message_unref(reply
);
896 dbus_error_free(&error
);
901 static int load_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
902 DBusMessage
*m
= NULL
, *reply
= NULL
;
907 dbus_error_init(&error
);
912 for (i
= 1; i
< n
; i
++) {
914 if (!(m
= dbus_message_new_method_call(
915 "org.freedesktop.systemd1",
916 "/org/freedesktop/systemd1",
917 "org.freedesktop.systemd1.Manager",
919 log_error("Could not allocate message.");
924 if (!dbus_message_append_args(m
,
925 DBUS_TYPE_STRING
, &args
[i
],
926 DBUS_TYPE_INVALID
)) {
927 log_error("Could not append arguments to message.");
932 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
933 log_error("Failed to issue method call: %s", bus_error_message(&error
));
938 dbus_message_unref(m
);
939 dbus_message_unref(reply
);
948 dbus_message_unref(m
);
951 dbus_message_unref(reply
);
953 dbus_error_free(&error
);
958 static int cancel_job(DBusConnection
*bus
, char **args
, unsigned n
) {
959 DBusMessage
*m
= NULL
, *reply
= NULL
;
964 dbus_error_init(&error
);
970 return daemon_reload(bus
, args
, n
);
972 for (i
= 1; i
< n
; i
++) {
976 if (!(m
= dbus_message_new_method_call(
977 "org.freedesktop.systemd1",
978 "/org/freedesktop/systemd1",
979 "org.freedesktop.systemd1.Manager",
981 log_error("Could not allocate message.");
986 if ((r
= safe_atou(args
[i
], &id
)) < 0) {
987 log_error("Failed to parse job id: %s", strerror(-r
));
991 assert_cc(sizeof(uint32_t) == sizeof(id
));
992 if (!dbus_message_append_args(m
,
993 DBUS_TYPE_UINT32
, &id
,
994 DBUS_TYPE_INVALID
)) {
995 log_error("Could not append arguments to message.");
1000 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1001 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1006 if (!dbus_message_get_args(reply
, &error
,
1007 DBUS_TYPE_OBJECT_PATH
, &path
,
1008 DBUS_TYPE_INVALID
)) {
1009 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1014 dbus_message_unref(m
);
1015 if (!(m
= dbus_message_new_method_call(
1016 "org.freedesktop.systemd1",
1018 "org.freedesktop.systemd1.Job",
1020 log_error("Could not allocate message.");
1025 dbus_message_unref(reply
);
1026 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1027 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1032 dbus_message_unref(m
);
1033 dbus_message_unref(reply
);
1041 dbus_message_unref(m
);
1044 dbus_message_unref(reply
);
1046 dbus_error_free(&error
);
1051 static bool need_daemon_reload(DBusConnection
*bus
, const char *unit
) {
1052 DBusMessage
*m
= NULL
, *reply
= NULL
;
1053 dbus_bool_t b
= FALSE
;
1054 DBusMessageIter iter
, sub
;
1056 *interface
= "org.freedesktop.systemd1.Unit",
1057 *property
= "NeedDaemonReload",
1060 /* We ignore all errors here, since this is used to show a warning only */
1062 if (!(m
= dbus_message_new_method_call(
1063 "org.freedesktop.systemd1",
1064 "/org/freedesktop/systemd1",
1065 "org.freedesktop.systemd1.Manager",
1069 if (!dbus_message_append_args(m
,
1070 DBUS_TYPE_STRING
, &unit
,
1074 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
)))
1077 if (!dbus_message_get_args(reply
, NULL
,
1078 DBUS_TYPE_OBJECT_PATH
, &path
,
1082 dbus_message_unref(m
);
1083 if (!(m
= dbus_message_new_method_call(
1084 "org.freedesktop.systemd1",
1086 "org.freedesktop.DBus.Properties",
1090 if (!dbus_message_append_args(m
,
1091 DBUS_TYPE_STRING
, &interface
,
1092 DBUS_TYPE_STRING
, &property
,
1093 DBUS_TYPE_INVALID
)) {
1097 dbus_message_unref(reply
);
1098 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
)))
1101 if (!dbus_message_iter_init(reply
, &iter
) ||
1102 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
)
1105 dbus_message_iter_recurse(&iter
, &sub
);
1107 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_BOOLEAN
)
1110 dbus_message_iter_get_basic(&sub
, &b
);
1114 dbus_message_unref(m
);
1117 dbus_message_unref(reply
);
1122 typedef struct WaitData
{
1127 static DBusHandlerResult
wait_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
1135 dbus_error_init(&error
);
1137 log_debug("Got D-Bus request: %s.%s() on %s",
1138 dbus_message_get_interface(message
),
1139 dbus_message_get_member(message
),
1140 dbus_message_get_path(message
));
1142 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
1143 log_error("Warning! D-Bus connection terminated.");
1144 dbus_connection_close(connection
);
1146 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1148 const char *path
, *result
;
1149 dbus_bool_t success
= true;
1151 if (dbus_message_get_args(message
, &error
,
1152 DBUS_TYPE_UINT32
, &id
,
1153 DBUS_TYPE_OBJECT_PATH
, &path
,
1154 DBUS_TYPE_STRING
, &result
,
1155 DBUS_TYPE_INVALID
)) {
1158 if ((p
= set_remove(d
->set
, (char*) path
)))
1162 d
->result
= strdup(result
);
1167 dbus_error_free(&error
);
1169 if (dbus_message_get_args(message
, &error
,
1170 DBUS_TYPE_UINT32
, &id
,
1171 DBUS_TYPE_OBJECT_PATH
, &path
,
1172 DBUS_TYPE_BOOLEAN
, &success
,
1173 DBUS_TYPE_INVALID
)) {
1176 /* Compatibility with older systemd versions <
1177 * 19 during upgrades. This should be dropped
1180 if ((p
= set_remove(d
->set
, (char*) path
)))
1184 d
->result
= strdup("failed");
1190 log_error("Failed to parse message: %s", bus_error_message(&error
));
1194 dbus_error_free(&error
);
1195 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1198 static int enable_wait_for_jobs(DBusConnection
*bus
) {
1206 dbus_error_init(&error
);
1207 dbus_bus_add_match(bus
,
1209 "sender='org.freedesktop.systemd1',"
1210 "interface='org.freedesktop.systemd1.Manager',"
1211 "member='JobRemoved',"
1212 "path='/org/freedesktop/systemd1'",
1215 if (dbus_error_is_set(&error
)) {
1216 log_error("Failed to add match: %s", bus_error_message(&error
));
1217 dbus_error_free(&error
);
1221 /* This is slightly dirty, since we don't undo the match registrations. */
1225 static int wait_for_jobs(DBusConnection
*bus
, Set
*s
) {
1235 if (!dbus_connection_add_filter(bus
, wait_filter
, &d
, NULL
)) {
1236 log_error("Failed to add filter.");
1241 while (!set_isempty(s
) &&
1242 dbus_connection_read_write_dispatch(bus
, -1))
1245 if (!arg_quiet
&& d
.result
) {
1246 if (streq(d
.result
, "timeout"))
1247 log_error("Job timed out.");
1248 else if (streq(d
.result
, "canceled"))
1249 log_error("Job canceled.");
1250 else if (streq(d
.result
, "dependency"))
1251 log_error("A dependency job failed. See system logs for details.");
1252 else if (!streq(d
.result
, "done") && !streq(d
.result
, "skipped"))
1253 log_error("Job failed. See system logs and 'systemctl status' for details.");
1256 if (streq_ptr(d
.result
, "timeout"))
1258 else if (streq_ptr(d
.result
, "canceled"))
1260 else if (!streq_ptr(d
.result
, "done") && !streq_ptr(d
.result
, "skipped"))
1268 /* This is slightly dirty, since we don't undo the filter registration. */
1273 static int start_unit_one(
1274 DBusConnection
*bus
,
1281 DBusMessage
*m
= NULL
, *reply
= NULL
;
1290 assert(arg_no_block
|| s
);
1292 if (!(m
= dbus_message_new_method_call(
1293 "org.freedesktop.systemd1",
1294 "/org/freedesktop/systemd1",
1295 "org.freedesktop.systemd1.Manager",
1297 log_error("Could not allocate message.");
1302 if (!dbus_message_append_args(m
,
1303 DBUS_TYPE_STRING
, &name
,
1304 DBUS_TYPE_STRING
, &mode
,
1305 DBUS_TYPE_INVALID
)) {
1306 log_error("Could not append arguments to message.");
1311 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, error
))) {
1313 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(error
)) {
1314 /* There's always a fallback possible for
1315 * legacy actions. */
1320 log_error("Failed to issue method call: %s", bus_error_message(error
));
1325 if (!dbus_message_get_args(reply
, error
,
1326 DBUS_TYPE_OBJECT_PATH
, &path
,
1327 DBUS_TYPE_INVALID
)) {
1328 log_error("Failed to parse reply: %s", bus_error_message(error
));
1333 if (need_daemon_reload(bus
, name
))
1334 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1335 arg_user
? "--user" : "--system");
1337 if (!arg_no_block
) {
1340 if (!(p
= strdup(path
))) {
1341 log_error("Failed to duplicate path.");
1346 if ((r
= set_put(s
, p
)) < 0) {
1348 log_error("Failed to add path to set.");
1357 dbus_message_unref(m
);
1360 dbus_message_unref(reply
);
1365 static enum action
verb_to_action(const char *verb
) {
1366 if (streq(verb
, "halt"))
1368 else if (streq(verb
, "poweroff"))
1369 return ACTION_POWEROFF
;
1370 else if (streq(verb
, "reboot"))
1371 return ACTION_REBOOT
;
1372 else if (streq(verb
, "kexec"))
1373 return ACTION_KEXEC
;
1374 else if (streq(verb
, "rescue"))
1375 return ACTION_RESCUE
;
1376 else if (streq(verb
, "emergency"))
1377 return ACTION_EMERGENCY
;
1378 else if (streq(verb
, "default"))
1379 return ACTION_DEFAULT
;
1380 else if (streq(verb
, "exit"))
1383 return ACTION_INVALID
;
1386 static int start_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1388 static const char * const table
[_ACTION_MAX
] = {
1389 [ACTION_HALT
] = SPECIAL_HALT_TARGET
,
1390 [ACTION_POWEROFF
] = SPECIAL_POWEROFF_TARGET
,
1391 [ACTION_REBOOT
] = SPECIAL_REBOOT_TARGET
,
1392 [ACTION_KEXEC
] = SPECIAL_KEXEC_TARGET
,
1393 [ACTION_RUNLEVEL2
] = SPECIAL_RUNLEVEL2_TARGET
,
1394 [ACTION_RUNLEVEL3
] = SPECIAL_RUNLEVEL3_TARGET
,
1395 [ACTION_RUNLEVEL4
] = SPECIAL_RUNLEVEL4_TARGET
,
1396 [ACTION_RUNLEVEL5
] = SPECIAL_RUNLEVEL5_TARGET
,
1397 [ACTION_RESCUE
] = SPECIAL_RESCUE_TARGET
,
1398 [ACTION_EMERGENCY
] = SPECIAL_EMERGENCY_TARGET
,
1399 [ACTION_DEFAULT
] = SPECIAL_DEFAULT_TARGET
,
1400 [ACTION_EXIT
] = SPECIAL_EXIT_TARGET
1405 const char *method
, *mode
, *one_name
;
1409 dbus_error_init(&error
);
1413 spawn_ask_password_agent();
1415 if (arg_action
== ACTION_SYSTEMCTL
) {
1417 streq(args
[0], "stop") ? "StopUnit" :
1418 streq(args
[0], "reload") ? "ReloadUnit" :
1419 streq(args
[0], "restart") ? "RestartUnit" :
1421 streq(args
[0], "try-restart") ||
1422 streq(args
[0], "condrestart") ? "TryRestartUnit" :
1424 streq(args
[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1426 streq(args
[0], "reload-or-try-restart") ||
1427 streq(args
[0], "condreload") ||
1429 streq(args
[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1433 (streq(args
[0], "isolate") ||
1434 streq(args
[0], "rescue") ||
1435 streq(args
[0], "emergency")) ? "isolate" : arg_job_mode
;
1437 one_name
= table
[verb_to_action(args
[0])];
1440 assert(arg_action
< ELEMENTSOF(table
));
1441 assert(table
[arg_action
]);
1443 method
= "StartUnit";
1445 mode
= (arg_action
== ACTION_EMERGENCY
||
1446 arg_action
== ACTION_RESCUE
||
1447 arg_action
== ACTION_RUNLEVEL2
||
1448 arg_action
== ACTION_RUNLEVEL3
||
1449 arg_action
== ACTION_RUNLEVEL4
||
1450 arg_action
== ACTION_RUNLEVEL5
) ? "isolate" : "replace";
1452 one_name
= table
[arg_action
];
1455 if (!arg_no_block
) {
1456 if ((ret
= enable_wait_for_jobs(bus
)) < 0) {
1457 log_error("Could not watch jobs: %s", strerror(-ret
));
1461 if (!(s
= set_new(string_hash_func
, string_compare_func
))) {
1462 log_error("Failed to allocate set.");
1469 if ((ret
= start_unit_one(bus
, method
, one_name
, mode
, &error
, s
)) <= 0)
1472 for (i
= 1; i
< n
; i
++)
1473 if ((r
= start_unit_one(bus
, method
, args
[i
], mode
, &error
, s
)) != 0) {
1474 ret
= translate_bus_error_to_exit_status(r
, &error
);
1475 dbus_error_free(&error
);
1480 if ((r
= wait_for_jobs(bus
, s
)) < 0) {
1489 dbus_error_free(&error
);
1494 static int start_special(DBusConnection
*bus
, char **args
, unsigned n
) {
1501 (streq(args
[0], "halt") ||
1502 streq(args
[0], "poweroff") ||
1503 streq(args
[0], "reboot") ||
1504 streq(args
[0], "kexec") ||
1505 streq(args
[0], "exit")))
1506 return daemon_reload(bus
, args
, n
);
1508 r
= start_unit(bus
, args
, n
);
1511 warn_wall(verb_to_action(args
[0]));
1516 static int check_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1517 DBusMessage
*m
= NULL
, *reply
= NULL
;
1519 *interface
= "org.freedesktop.systemd1.Unit",
1520 *property
= "ActiveState";
1521 int r
= 3; /* According to LSB: "program is not running" */
1528 dbus_error_init(&error
);
1530 for (i
= 1; i
< n
; i
++) {
1531 const char *path
= NULL
;
1533 DBusMessageIter iter
, sub
;
1535 if (!(m
= dbus_message_new_method_call(
1536 "org.freedesktop.systemd1",
1537 "/org/freedesktop/systemd1",
1538 "org.freedesktop.systemd1.Manager",
1540 log_error("Could not allocate message.");
1545 if (!dbus_message_append_args(m
,
1546 DBUS_TYPE_STRING
, &args
[i
],
1547 DBUS_TYPE_INVALID
)) {
1548 log_error("Could not append arguments to message.");
1553 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1555 /* Hmm, cannot figure out anything about this unit... */
1559 dbus_error_free(&error
);
1560 dbus_message_unref(m
);
1564 if (!dbus_message_get_args(reply
, &error
,
1565 DBUS_TYPE_OBJECT_PATH
, &path
,
1566 DBUS_TYPE_INVALID
)) {
1567 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1572 dbus_message_unref(m
);
1573 if (!(m
= dbus_message_new_method_call(
1574 "org.freedesktop.systemd1",
1576 "org.freedesktop.DBus.Properties",
1578 log_error("Could not allocate message.");
1583 if (!dbus_message_append_args(m
,
1584 DBUS_TYPE_STRING
, &interface
,
1585 DBUS_TYPE_STRING
, &property
,
1586 DBUS_TYPE_INVALID
)) {
1587 log_error("Could not append arguments to message.");
1592 dbus_message_unref(reply
);
1593 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1594 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1599 if (!dbus_message_iter_init(reply
, &iter
) ||
1600 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
1601 log_error("Failed to parse reply.");
1606 dbus_message_iter_recurse(&iter
, &sub
);
1608 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
1609 log_error("Failed to parse reply.");
1614 dbus_message_iter_get_basic(&sub
, &state
);
1619 if (streq(state
, "active") || streq(state
, "reloading"))
1622 dbus_message_unref(m
);
1623 dbus_message_unref(reply
);
1629 dbus_message_unref(m
);
1632 dbus_message_unref(reply
);
1634 dbus_error_free(&error
);
1639 static int kill_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1640 DBusMessage
*m
= NULL
, *reply
= NULL
;
1648 dbus_error_init(&error
);
1651 arg_kill_who
= "all";
1654 arg_kill_mode
= streq(arg_kill_who
, "all") ? "control-group" : "process";
1656 for (i
= 1; i
< n
; i
++) {
1658 if (!(m
= dbus_message_new_method_call(
1659 "org.freedesktop.systemd1",
1660 "/org/freedesktop/systemd1",
1661 "org.freedesktop.systemd1.Manager",
1663 log_error("Could not allocate message.");
1668 if (!dbus_message_append_args(m
,
1669 DBUS_TYPE_STRING
, &args
[i
],
1670 DBUS_TYPE_STRING
, &arg_kill_who
,
1671 DBUS_TYPE_STRING
, &arg_kill_mode
,
1672 DBUS_TYPE_INT32
, &arg_signal
,
1673 DBUS_TYPE_INVALID
)) {
1674 log_error("Could not append arguments to message.");
1679 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1680 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1681 dbus_error_free(&error
);
1685 dbus_message_unref(m
);
1688 dbus_message_unref(reply
);
1694 dbus_message_unref(m
);
1697 dbus_message_unref(reply
);
1699 dbus_error_free(&error
);
1704 typedef struct ExecStatusInfo
{
1712 usec_t start_timestamp
;
1713 usec_t exit_timestamp
;
1718 LIST_FIELDS(struct ExecStatusInfo
, exec
);
1721 static void exec_status_info_free(ExecStatusInfo
*i
) {
1730 static int exec_status_info_deserialize(DBusMessageIter
*sub
, ExecStatusInfo
*i
) {
1731 uint64_t start_timestamp
, exit_timestamp
;
1732 DBusMessageIter sub2
, sub3
;
1736 int32_t code
, status
;
1742 if (dbus_message_iter_get_arg_type(sub
) != DBUS_TYPE_STRUCT
)
1745 dbus_message_iter_recurse(sub
, &sub2
);
1747 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) < 0)
1750 if (!(i
->path
= strdup(path
)))
1753 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_ARRAY
||
1754 dbus_message_iter_get_element_type(&sub2
) != DBUS_TYPE_STRING
)
1758 dbus_message_iter_recurse(&sub2
, &sub3
);
1759 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
1760 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
1761 dbus_message_iter_next(&sub3
);
1766 if (!(i
->argv
= new0(char*, n
+1)))
1770 dbus_message_iter_recurse(&sub2
, &sub3
);
1771 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
1774 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
1775 dbus_message_iter_get_basic(&sub3
, &s
);
1776 dbus_message_iter_next(&sub3
);
1778 if (!(i
->argv
[n
++] = strdup(s
)))
1782 if (!dbus_message_iter_next(&sub2
) ||
1783 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, true) < 0 ||
1784 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &start_timestamp
, true) < 0 ||
1785 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &exit_timestamp
, true) < 0 ||
1786 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &pid
, true) < 0 ||
1787 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &code
, true) < 0 ||
1788 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &status
, false) < 0)
1792 i
->start_timestamp
= (usec_t
) start_timestamp
;
1793 i
->exit_timestamp
= (usec_t
) exit_timestamp
;
1794 i
->pid
= (pid_t
) pid
;
1801 typedef struct UnitStatusInfo
{
1803 const char *load_state
;
1804 const char *active_state
;
1805 const char *sub_state
;
1807 const char *description
;
1808 const char *following
;
1811 const char *default_control_group
;
1813 usec_t inactive_exit_timestamp
;
1814 usec_t active_enter_timestamp
;
1815 usec_t active_exit_timestamp
;
1816 usec_t inactive_enter_timestamp
;
1818 bool need_daemon_reload
;
1823 const char *status_text
;
1825 #ifdef HAVE_SYSV_COMPAT
1829 usec_t start_timestamp
;
1830 usec_t exit_timestamp
;
1832 int exit_code
, exit_status
;
1834 usec_t condition_timestamp
;
1835 bool condition_result
;
1838 unsigned n_accepted
;
1839 unsigned n_connections
;
1843 const char *sysfs_path
;
1845 /* Mount, Automount */
1851 LIST_HEAD(ExecStatusInfo
, exec
);
1854 static void print_status_info(UnitStatusInfo
*i
) {
1856 const char *on
, *off
, *ss
;
1858 char since1
[FORMAT_TIMESTAMP_PRETTY_MAX
], *s1
;
1859 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
1863 /* This shows pretty information about a unit. See
1864 * print_property() for a low-level property printer */
1866 printf("%s", strna(i
->id
));
1868 if (i
->description
&& !streq_ptr(i
->id
, i
->description
))
1869 printf(" - %s", i
->description
);
1874 printf("\t Follow: unit currently follows state of %s\n", i
->following
);
1876 if (streq_ptr(i
->load_state
, "failed") ||
1877 streq_ptr(i
->load_state
, "banned")) {
1878 on
= ansi_highlight(true);
1879 off
= ansi_highlight(false);
1884 printf("\t Loaded: %s%s%s (%s)\n", on
, strna(i
->load_state
), off
, i
->path
);
1886 printf("\t Loaded: %s%s%s\n", on
, strna(i
->load_state
), off
);
1888 ss
= streq_ptr(i
->active_state
, i
->sub_state
) ? NULL
: i
->sub_state
;
1890 if (streq_ptr(i
->active_state
, "failed")) {
1891 on
= ansi_highlight(true);
1892 off
= ansi_highlight(false);
1893 } else if (streq_ptr(i
->active_state
, "active") || streq_ptr(i
->active_state
, "reloading")) {
1894 on
= ansi_highlight_green(true);
1895 off
= ansi_highlight_green(false);
1900 printf("\t Active: %s%s (%s)%s",
1902 strna(i
->active_state
),
1906 printf("\t Active: %s%s%s",
1908 strna(i
->active_state
),
1911 timestamp
= (streq_ptr(i
->active_state
, "active") ||
1912 streq_ptr(i
->active_state
, "reloading")) ? i
->active_enter_timestamp
:
1913 (streq_ptr(i
->active_state
, "inactive") ||
1914 streq_ptr(i
->active_state
, "failed")) ? i
->inactive_enter_timestamp
:
1915 streq_ptr(i
->active_state
, "activating") ? i
->inactive_exit_timestamp
:
1916 i
->active_exit_timestamp
;
1918 s1
= format_timestamp_pretty(since1
, sizeof(since1
), timestamp
);
1919 s2
= format_timestamp(since2
, sizeof(since2
), timestamp
);
1922 printf(" since %s; %s\n", s2
, s1
);
1924 printf(" since %s\n", s2
);
1928 if (!i
->condition_result
&& i
->condition_timestamp
> 0) {
1929 s1
= format_timestamp_pretty(since1
, sizeof(since1
), i
->condition_timestamp
);
1930 s2
= format_timestamp(since2
, sizeof(since2
), i
->condition_timestamp
);
1933 printf("\t start condition failed at %s; %s\n", s2
, s1
);
1935 printf("\t start condition failed at %s\n", s2
);
1939 printf("\t Device: %s\n", i
->sysfs_path
);
1941 printf("\t Where: %s\n", i
->where
);
1943 printf("\t What: %s\n", i
->what
);
1946 printf("\tAccepted: %u; Connected: %u\n", i
->n_accepted
, i
->n_connections
);
1948 LIST_FOREACH(exec
, p
, i
->exec
) {
1952 /* Only show exited processes here */
1956 t
= strv_join(p
->argv
, " ");
1957 printf("\t Process: %u %s=%s ", p
->pid
, p
->name
, strna(t
));
1960 #ifdef HAVE_SYSV_COMPAT
1962 good
= is_clean_exit_lsb(p
->code
, p
->status
);
1965 good
= is_clean_exit(p
->code
, p
->status
);
1968 on
= ansi_highlight(true);
1969 off
= ansi_highlight(false);
1973 printf("%s(code=%s, ", on
, sigchld_code_to_string(p
->code
));
1975 if (p
->code
== CLD_EXITED
) {
1978 printf("status=%i", p
->status
);
1980 #ifdef HAVE_SYSV_COMPAT
1981 if ((c
= exit_status_to_string(p
->status
, i
->is_sysv
? EXIT_STATUS_LSB
: EXIT_STATUS_SYSTEMD
)))
1983 if ((c
= exit_status_to_string(p
->status
, EXIT_STATUS_SYSTEMD
)))
1988 printf("signal=%s", signal_to_string(p
->status
));
1990 printf(")%s\n", off
);
1994 if (i
->main_pid
== p
->pid
&&
1995 i
->start_timestamp
== p
->start_timestamp
&&
1996 i
->exit_timestamp
== p
->start_timestamp
)
1997 /* Let's not show this twice */
2000 if (p
->pid
== i
->control_pid
)
2004 if (i
->main_pid
> 0 || i
->control_pid
> 0) {
2007 if (i
->main_pid
> 0) {
2008 printf("Main PID: %u", (unsigned) i
->main_pid
);
2012 get_process_name(i
->main_pid
, &t
);
2017 } else if (i
->exit_code
> 0) {
2018 printf(" (code=%s, ", sigchld_code_to_string(i
->exit_code
));
2020 if (i
->exit_code
== CLD_EXITED
) {
2023 printf("status=%i", i
->exit_status
);
2025 #ifdef HAVE_SYSV_COMPAT
2026 if ((c
= exit_status_to_string(i
->exit_status
, i
->is_sysv
? EXIT_STATUS_LSB
: EXIT_STATUS_SYSTEMD
)))
2028 if ((c
= exit_status_to_string(i
->exit_status
, EXIT_STATUS_SYSTEMD
)))
2033 printf("signal=%s", signal_to_string(i
->exit_status
));
2038 if (i
->main_pid
> 0 && i
->control_pid
> 0)
2041 if (i
->control_pid
> 0) {
2044 printf(" Control: %u", (unsigned) i
->control_pid
);
2046 get_process_name(i
->control_pid
, &t
);
2057 printf("\t Status: \"%s\"\n", i
->status_text
);
2059 if (i
->default_control_group
) {
2062 printf("\t CGroup: %s\n", i
->default_control_group
);
2064 if ((c
= columns()) > 18)
2069 show_cgroup_by_path(i
->default_control_group
, "\t\t ", c
);
2072 if (i
->need_daemon_reload
)
2073 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2074 ansi_highlight(true),
2075 ansi_highlight(false),
2076 arg_user
? "--user" : "--system");
2079 static int status_property(const char *name
, DBusMessageIter
*iter
, UnitStatusInfo
*i
) {
2081 switch (dbus_message_iter_get_arg_type(iter
)) {
2083 case DBUS_TYPE_STRING
: {
2086 dbus_message_iter_get_basic(iter
, &s
);
2089 if (streq(name
, "Id"))
2091 else if (streq(name
, "LoadState"))
2093 else if (streq(name
, "ActiveState"))
2094 i
->active_state
= s
;
2095 else if (streq(name
, "SubState"))
2097 else if (streq(name
, "Description"))
2099 else if (streq(name
, "FragmentPath"))
2101 #ifdef HAVE_SYSV_COMPAT
2102 else if (streq(name
, "SysVPath")) {
2107 else if (streq(name
, "DefaultControlGroup"))
2108 i
->default_control_group
= s
;
2109 else if (streq(name
, "StatusText"))
2111 else if (streq(name
, "SysFSPath"))
2113 else if (streq(name
, "Where"))
2115 else if (streq(name
, "What"))
2117 else if (streq(name
, "Following"))
2124 case DBUS_TYPE_BOOLEAN
: {
2127 dbus_message_iter_get_basic(iter
, &b
);
2129 if (streq(name
, "Accept"))
2131 else if (streq(name
, "NeedDaemonReload"))
2132 i
->need_daemon_reload
= b
;
2133 else if (streq(name
, "ConditionResult"))
2134 i
->condition_result
= b
;
2139 case DBUS_TYPE_UINT32
: {
2142 dbus_message_iter_get_basic(iter
, &u
);
2144 if (streq(name
, "MainPID")) {
2146 i
->main_pid
= (pid_t
) u
;
2149 } else if (streq(name
, "ControlPID"))
2150 i
->control_pid
= (pid_t
) u
;
2151 else if (streq(name
, "ExecMainPID")) {
2153 i
->main_pid
= (pid_t
) u
;
2154 } else if (streq(name
, "NAccepted"))
2156 else if (streq(name
, "NConnections"))
2157 i
->n_connections
= u
;
2162 case DBUS_TYPE_INT32
: {
2165 dbus_message_iter_get_basic(iter
, &j
);
2167 if (streq(name
, "ExecMainCode"))
2168 i
->exit_code
= (int) j
;
2169 else if (streq(name
, "ExecMainStatus"))
2170 i
->exit_status
= (int) j
;
2175 case DBUS_TYPE_UINT64
: {
2178 dbus_message_iter_get_basic(iter
, &u
);
2180 if (streq(name
, "ExecMainStartTimestamp"))
2181 i
->start_timestamp
= (usec_t
) u
;
2182 else if (streq(name
, "ExecMainExitTimestamp"))
2183 i
->exit_timestamp
= (usec_t
) u
;
2184 else if (streq(name
, "ActiveEnterTimestamp"))
2185 i
->active_enter_timestamp
= (usec_t
) u
;
2186 else if (streq(name
, "InactiveEnterTimestamp"))
2187 i
->inactive_enter_timestamp
= (usec_t
) u
;
2188 else if (streq(name
, "InactiveExitTimestamp"))
2189 i
->inactive_exit_timestamp
= (usec_t
) u
;
2190 else if (streq(name
, "ActiveExitTimestamp"))
2191 i
->active_exit_timestamp
= (usec_t
) u
;
2192 else if (streq(name
, "ConditionTimestamp"))
2193 i
->condition_timestamp
= (usec_t
) u
;
2198 case DBUS_TYPE_ARRAY
: {
2200 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&&
2201 startswith(name
, "Exec")) {
2202 DBusMessageIter sub
;
2204 dbus_message_iter_recurse(iter
, &sub
);
2205 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2206 ExecStatusInfo
*info
;
2209 if (!(info
= new0(ExecStatusInfo
, 1)))
2212 if (!(info
->name
= strdup(name
))) {
2217 if ((r
= exec_status_info_deserialize(&sub
, info
)) < 0) {
2222 LIST_PREPEND(ExecStatusInfo
, exec
, i
->exec
, info
);
2224 dbus_message_iter_next(&sub
);
2235 static int print_property(const char *name
, DBusMessageIter
*iter
) {
2239 /* This is a low-level property printer, see
2240 * print_status_info() for the nicer output */
2242 if (arg_property
&& !strv_find(arg_property
, name
))
2245 switch (dbus_message_iter_get_arg_type(iter
)) {
2247 case DBUS_TYPE_STRING
: {
2249 dbus_message_iter_get_basic(iter
, &s
);
2251 if (arg_all
|| s
[0])
2252 printf("%s=%s\n", name
, s
);
2257 case DBUS_TYPE_BOOLEAN
: {
2259 dbus_message_iter_get_basic(iter
, &b
);
2260 printf("%s=%s\n", name
, yes_no(b
));
2265 case DBUS_TYPE_UINT64
: {
2267 dbus_message_iter_get_basic(iter
, &u
);
2269 /* Yes, heuristics! But we can change this check
2270 * should it turn out to not be sufficient */
2272 if (strstr(name
, "Timestamp")) {
2273 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
2275 if ((t
= format_timestamp(timestamp
, sizeof(timestamp
), u
)) || arg_all
)
2276 printf("%s=%s\n", name
, strempty(t
));
2277 } else if (strstr(name
, "USec")) {
2278 char timespan
[FORMAT_TIMESPAN_MAX
];
2280 printf("%s=%s\n", name
, format_timespan(timespan
, sizeof(timespan
), u
));
2282 printf("%s=%llu\n", name
, (unsigned long long) u
);
2287 case DBUS_TYPE_UINT32
: {
2289 dbus_message_iter_get_basic(iter
, &u
);
2291 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
2292 printf("%s=%04o\n", name
, u
);
2294 printf("%s=%u\n", name
, (unsigned) u
);
2299 case DBUS_TYPE_INT32
: {
2301 dbus_message_iter_get_basic(iter
, &i
);
2303 printf("%s=%i\n", name
, (int) i
);
2307 case DBUS_TYPE_DOUBLE
: {
2309 dbus_message_iter_get_basic(iter
, &d
);
2311 printf("%s=%g\n", name
, d
);
2315 case DBUS_TYPE_STRUCT
: {
2316 DBusMessageIter sub
;
2317 dbus_message_iter_recurse(iter
, &sub
);
2319 if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_UINT32
&& streq(name
, "Job")) {
2322 dbus_message_iter_get_basic(&sub
, &u
);
2325 printf("%s=%u\n", name
, (unsigned) u
);
2327 printf("%s=\n", name
);
2330 } else if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
&& streq(name
, "Unit")) {
2333 dbus_message_iter_get_basic(&sub
, &s
);
2335 if (arg_all
|| s
[0])
2336 printf("%s=%s\n", name
, s
);
2344 case DBUS_TYPE_ARRAY
:
2346 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
2347 DBusMessageIter sub
;
2350 dbus_message_iter_recurse(iter
, &sub
);
2352 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2353 printf("%s=", name
);
2355 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2358 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
2359 dbus_message_iter_get_basic(&sub
, &s
);
2360 printf("%s%s", space
? " " : "", s
);
2363 dbus_message_iter_next(&sub
);
2371 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_BYTE
) {
2372 DBusMessageIter sub
;
2374 dbus_message_iter_recurse(iter
, &sub
);
2376 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2377 printf("%s=", name
);
2379 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2382 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_BYTE
);
2383 dbus_message_iter_get_basic(&sub
, &u
);
2386 dbus_message_iter_next(&sub
);
2394 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "EnvironmentFiles")) {
2395 DBusMessageIter sub
, sub2
;
2397 dbus_message_iter_recurse(iter
, &sub
);
2398 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2402 dbus_message_iter_recurse(&sub
, &sub2
);
2404 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) >= 0 &&
2405 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, false) >= 0)
2406 printf("EnvironmentFile=%s (ignore=%s)\n", path
, yes_no(ignore
));
2408 dbus_message_iter_next(&sub
);
2413 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Paths")) {
2414 DBusMessageIter sub
, sub2
;
2416 dbus_message_iter_recurse(iter
, &sub
);
2417 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2418 const char *type
, *path
;
2420 dbus_message_iter_recurse(&sub
, &sub2
);
2422 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) >= 0 &&
2423 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, false) >= 0)
2424 printf("%s=%s\n", type
, path
);
2426 dbus_message_iter_next(&sub
);
2431 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Timers")) {
2432 DBusMessageIter sub
, sub2
;
2434 dbus_message_iter_recurse(iter
, &sub
);
2435 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2437 uint64_t value
, next_elapse
;
2439 dbus_message_iter_recurse(&sub
, &sub2
);
2441 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &base
, true) >= 0 &&
2442 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &value
, true) >= 0 &&
2443 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &next_elapse
, false) >= 0) {
2444 char timespan1
[FORMAT_TIMESPAN_MAX
], timespan2
[FORMAT_TIMESPAN_MAX
];
2446 printf("%s={ value=%s ; next_elapse=%s }\n",
2448 format_timespan(timespan1
, sizeof(timespan1
), value
),
2449 format_timespan(timespan2
, sizeof(timespan2
), next_elapse
));
2452 dbus_message_iter_next(&sub
);
2457 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& startswith(name
, "Exec")) {
2458 DBusMessageIter sub
;
2460 dbus_message_iter_recurse(iter
, &sub
);
2461 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2462 ExecStatusInfo info
;
2465 if (exec_status_info_deserialize(&sub
, &info
) >= 0) {
2466 char timestamp1
[FORMAT_TIMESTAMP_MAX
], timestamp2
[FORMAT_TIMESTAMP_MAX
];
2469 t
= strv_join(info
.argv
, " ");
2471 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2475 yes_no(info
.ignore
),
2476 strna(format_timestamp(timestamp1
, sizeof(timestamp1
), info
.start_timestamp
)),
2477 strna(format_timestamp(timestamp2
, sizeof(timestamp2
), info
.exit_timestamp
)),
2478 (unsigned) info
. pid
,
2479 sigchld_code_to_string(info
.code
),
2481 info
.code
== CLD_EXITED
? "" : "/",
2482 strempty(info
.code
== CLD_EXITED
? NULL
: signal_to_string(info
.status
)));
2488 strv_free(info
.argv
);
2490 dbus_message_iter_next(&sub
);
2500 printf("%s=[unprintable]\n", name
);
2505 static int show_one(const char *verb
, DBusConnection
*bus
, const char *path
, bool show_properties
, bool *new_line
) {
2506 DBusMessage
*m
= NULL
, *reply
= NULL
;
2507 const char *interface
= "";
2510 DBusMessageIter iter
, sub
, sub2
, sub3
;
2511 UnitStatusInfo info
;
2519 dbus_error_init(&error
);
2521 if (!(m
= dbus_message_new_method_call(
2522 "org.freedesktop.systemd1",
2524 "org.freedesktop.DBus.Properties",
2526 log_error("Could not allocate message.");
2531 if (!dbus_message_append_args(m
,
2532 DBUS_TYPE_STRING
, &interface
,
2533 DBUS_TYPE_INVALID
)) {
2534 log_error("Could not append arguments to message.");
2539 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2540 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2545 if (!dbus_message_iter_init(reply
, &iter
) ||
2546 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
2547 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
2548 log_error("Failed to parse reply.");
2553 dbus_message_iter_recurse(&iter
, &sub
);
2560 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2563 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
2564 log_error("Failed to parse reply.");
2569 dbus_message_iter_recurse(&sub
, &sub2
);
2571 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0) {
2572 log_error("Failed to parse reply.");
2577 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
2578 log_error("Failed to parse reply.");
2583 dbus_message_iter_recurse(&sub2
, &sub3
);
2585 if (show_properties
)
2586 r
= print_property(name
, &sub3
);
2588 r
= status_property(name
, &sub3
, &info
);
2591 log_error("Failed to parse reply.");
2596 dbus_message_iter_next(&sub
);
2601 if (!show_properties
)
2602 print_status_info(&info
);
2604 if (!streq_ptr(info
.active_state
, "active") &&
2605 !streq_ptr(info
.active_state
, "reloading") &&
2606 streq(verb
, "status"))
2607 /* According to LSB: "program not running" */
2610 while ((p
= info
.exec
)) {
2611 LIST_REMOVE(ExecStatusInfo
, exec
, info
.exec
, p
);
2612 exec_status_info_free(p
);
2617 dbus_message_unref(m
);
2620 dbus_message_unref(reply
);
2622 dbus_error_free(&error
);
2627 static int show(DBusConnection
*bus
, char **args
, unsigned n
) {
2628 DBusMessage
*m
= NULL
, *reply
= NULL
;
2632 bool show_properties
, new_line
= false;
2637 dbus_error_init(&error
);
2639 show_properties
= !streq(args
[0], "status");
2641 if (show_properties
)
2644 if (show_properties
&& n
<= 1) {
2645 /* If not argument is specified inspect the manager
2648 ret
= show_one(args
[0], bus
, "/org/freedesktop/systemd1", show_properties
, &new_line
);
2652 for (i
= 1; i
< n
; i
++) {
2653 const char *path
= NULL
;
2656 if (safe_atou32(args
[i
], &id
) < 0) {
2658 /* Interpret as unit name */
2660 if (!(m
= dbus_message_new_method_call(
2661 "org.freedesktop.systemd1",
2662 "/org/freedesktop/systemd1",
2663 "org.freedesktop.systemd1.Manager",
2665 log_error("Could not allocate message.");
2670 if (!dbus_message_append_args(m
,
2671 DBUS_TYPE_STRING
, &args
[i
],
2672 DBUS_TYPE_INVALID
)) {
2673 log_error("Could not append arguments to message.");
2678 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2680 if (!dbus_error_has_name(&error
, DBUS_ERROR_ACCESS_DENIED
)) {
2681 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2686 dbus_error_free(&error
);
2688 dbus_message_unref(m
);
2689 if (!(m
= dbus_message_new_method_call(
2690 "org.freedesktop.systemd1",
2691 "/org/freedesktop/systemd1",
2692 "org.freedesktop.systemd1.Manager",
2694 log_error("Could not allocate message.");
2699 if (!dbus_message_append_args(m
,
2700 DBUS_TYPE_STRING
, &args
[i
],
2701 DBUS_TYPE_INVALID
)) {
2702 log_error("Could not append arguments to message.");
2707 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2708 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2710 if (dbus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
))
2711 ret
= 4; /* According to LSB: "program or service status is unknown" */
2718 } else if (show_properties
) {
2720 /* Interpret as job id */
2722 if (!(m
= dbus_message_new_method_call(
2723 "org.freedesktop.systemd1",
2724 "/org/freedesktop/systemd1",
2725 "org.freedesktop.systemd1.Manager",
2727 log_error("Could not allocate message.");
2732 if (!dbus_message_append_args(m
,
2733 DBUS_TYPE_UINT32
, &id
,
2734 DBUS_TYPE_INVALID
)) {
2735 log_error("Could not append arguments to message.");
2740 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2741 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2747 /* Interpret as PID */
2749 if (!(m
= dbus_message_new_method_call(
2750 "org.freedesktop.systemd1",
2751 "/org/freedesktop/systemd1",
2752 "org.freedesktop.systemd1.Manager",
2754 log_error("Could not allocate message.");
2759 if (!dbus_message_append_args(m
,
2760 DBUS_TYPE_UINT32
, &id
,
2761 DBUS_TYPE_INVALID
)) {
2762 log_error("Could not append arguments to message.");
2767 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2768 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2774 if (!dbus_message_get_args(reply
, &error
,
2775 DBUS_TYPE_OBJECT_PATH
, &path
,
2776 DBUS_TYPE_INVALID
)) {
2777 log_error("Failed to parse reply: %s", bus_error_message(&error
));
2782 if ((r
= show_one(args
[0], bus
, path
, show_properties
, &new_line
)) != 0)
2785 dbus_message_unref(m
);
2786 dbus_message_unref(reply
);
2792 dbus_message_unref(m
);
2795 dbus_message_unref(reply
);
2797 dbus_error_free(&error
);
2802 static DBusHandlerResult
monitor_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
2804 DBusMessage
*m
= NULL
, *reply
= NULL
;
2809 dbus_error_init(&error
);
2811 log_debug("Got D-Bus request: %s.%s() on %s",
2812 dbus_message_get_interface(message
),
2813 dbus_message_get_member(message
),
2814 dbus_message_get_path(message
));
2816 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
2817 log_error("Warning! D-Bus connection terminated.");
2818 dbus_connection_close(connection
);
2820 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2821 dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2822 const char *id
, *path
;
2824 if (!dbus_message_get_args(message
, &error
,
2825 DBUS_TYPE_STRING
, &id
,
2826 DBUS_TYPE_OBJECT_PATH
, &path
,
2828 log_error("Failed to parse message: %s", bus_error_message(&error
));
2829 else if (streq(dbus_message_get_member(message
), "UnitNew"))
2830 printf("Unit %s added.\n", id
);
2832 printf("Unit %s removed.\n", id
);
2834 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobNew") ||
2835 dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2837 const char *path
, *result
;
2839 if (!dbus_message_get_args(message
, &error
,
2840 DBUS_TYPE_UINT32
, &id
,
2841 DBUS_TYPE_OBJECT_PATH
, &path
,
2842 DBUS_TYPE_STRING
, &result
,
2844 log_error("Failed to parse message: %s", bus_error_message(&error
));
2845 else if (streq(dbus_message_get_member(message
), "JobNew"))
2846 printf("Job %u added.\n", id
);
2848 printf("Job %u removed.\n", id
);
2851 } else if (dbus_message_is_signal(message
, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2853 const char *path
, *interface
, *property
= "Id";
2854 DBusMessageIter iter
, sub
;
2856 path
= dbus_message_get_path(message
);
2858 if (!dbus_message_get_args(message
, &error
,
2859 DBUS_TYPE_STRING
, &interface
,
2860 DBUS_TYPE_INVALID
)) {
2861 log_error("Failed to parse message: %s", bus_error_message(&error
));
2865 if (!streq(interface
, "org.freedesktop.systemd1.Job") &&
2866 !streq(interface
, "org.freedesktop.systemd1.Unit"))
2869 if (!(m
= dbus_message_new_method_call(
2870 "org.freedesktop.systemd1",
2872 "org.freedesktop.DBus.Properties",
2874 log_error("Could not allocate message.");
2878 if (!dbus_message_append_args(m
,
2879 DBUS_TYPE_STRING
, &interface
,
2880 DBUS_TYPE_STRING
, &property
,
2881 DBUS_TYPE_INVALID
)) {
2882 log_error("Could not append arguments to message.");
2886 if (!(reply
= dbus_connection_send_with_reply_and_block(connection
, m
, -1, &error
))) {
2887 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2891 if (!dbus_message_iter_init(reply
, &iter
) ||
2892 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
2893 log_error("Failed to parse reply.");
2897 dbus_message_iter_recurse(&iter
, &sub
);
2899 if (streq(interface
, "org.freedesktop.systemd1.Unit")) {
2902 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
2903 log_error("Failed to parse reply.");
2907 dbus_message_iter_get_basic(&sub
, &id
);
2908 printf("Unit %s changed.\n", id
);
2912 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_UINT32
) {
2913 log_error("Failed to parse reply.");
2917 dbus_message_iter_get_basic(&sub
, &id
);
2918 printf("Job %u changed.\n", id
);
2924 dbus_message_unref(m
);
2927 dbus_message_unref(reply
);
2929 dbus_error_free(&error
);
2930 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
2934 dbus_message_unref(m
);
2937 dbus_message_unref(reply
);
2939 dbus_error_free(&error
);
2940 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
2943 static int monitor(DBusConnection
*bus
, char **args
, unsigned n
) {
2944 DBusMessage
*m
= NULL
, *reply
= NULL
;
2948 dbus_error_init(&error
);
2951 dbus_bus_add_match(bus
,
2953 "sender='org.freedesktop.systemd1',"
2954 "interface='org.freedesktop.systemd1.Manager',"
2955 "path='/org/freedesktop/systemd1'",
2958 if (dbus_error_is_set(&error
)) {
2959 log_error("Failed to add match: %s", bus_error_message(&error
));
2964 dbus_bus_add_match(bus
,
2966 "sender='org.freedesktop.systemd1',"
2967 "interface='org.freedesktop.DBus.Properties',"
2968 "member='PropertiesChanged'",
2971 if (dbus_error_is_set(&error
)) {
2972 log_error("Failed to add match: %s", bus_error_message(&error
));
2978 if (!dbus_connection_add_filter(bus
, monitor_filter
, NULL
, NULL
)) {
2979 log_error("Failed to add filter.");
2984 if (!(m
= dbus_message_new_method_call(
2985 "org.freedesktop.systemd1",
2986 "/org/freedesktop/systemd1",
2987 "org.freedesktop.systemd1.Manager",
2989 log_error("Could not allocate message.");
2994 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2995 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3000 while (dbus_connection_read_write_dispatch(bus
, -1))
3007 /* This is slightly dirty, since we don't undo the filter or the matches. */
3010 dbus_message_unref(m
);
3013 dbus_message_unref(reply
);
3015 dbus_error_free(&error
);
3020 static int dump(DBusConnection
*bus
, char **args
, unsigned n
) {
3021 DBusMessage
*m
= NULL
, *reply
= NULL
;
3026 dbus_error_init(&error
);
3030 if (!(m
= dbus_message_new_method_call(
3031 "org.freedesktop.systemd1",
3032 "/org/freedesktop/systemd1",
3033 "org.freedesktop.systemd1.Manager",
3035 log_error("Could not allocate message.");
3039 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3040 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3045 if (!dbus_message_get_args(reply
, &error
,
3046 DBUS_TYPE_STRING
, &text
,
3047 DBUS_TYPE_INVALID
)) {
3048 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3053 fputs(text
, stdout
);
3059 dbus_message_unref(m
);
3062 dbus_message_unref(reply
);
3064 dbus_error_free(&error
);
3069 static int snapshot(DBusConnection
*bus
, char **args
, unsigned n
) {
3070 DBusMessage
*m
= NULL
, *reply
= NULL
;
3073 const char *name
= "", *path
, *id
;
3074 dbus_bool_t cleanup
= FALSE
;
3075 DBusMessageIter iter
, sub
;
3077 *interface
= "org.freedesktop.systemd1.Unit",
3080 dbus_error_init(&error
);
3082 if (!(m
= dbus_message_new_method_call(
3083 "org.freedesktop.systemd1",
3084 "/org/freedesktop/systemd1",
3085 "org.freedesktop.systemd1.Manager",
3086 "CreateSnapshot"))) {
3087 log_error("Could not allocate message.");
3094 if (!dbus_message_append_args(m
,
3095 DBUS_TYPE_STRING
, &name
,
3096 DBUS_TYPE_BOOLEAN
, &cleanup
,
3097 DBUS_TYPE_INVALID
)) {
3098 log_error("Could not append arguments to message.");
3103 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3104 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3109 if (!dbus_message_get_args(reply
, &error
,
3110 DBUS_TYPE_OBJECT_PATH
, &path
,
3111 DBUS_TYPE_INVALID
)) {
3112 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3117 dbus_message_unref(m
);
3118 if (!(m
= dbus_message_new_method_call(
3119 "org.freedesktop.systemd1",
3121 "org.freedesktop.DBus.Properties",
3123 log_error("Could not allocate message.");
3127 if (!dbus_message_append_args(m
,
3128 DBUS_TYPE_STRING
, &interface
,
3129 DBUS_TYPE_STRING
, &property
,
3130 DBUS_TYPE_INVALID
)) {
3131 log_error("Could not append arguments to message.");
3136 dbus_message_unref(reply
);
3137 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3138 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3143 if (!dbus_message_iter_init(reply
, &iter
) ||
3144 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3145 log_error("Failed to parse reply.");
3150 dbus_message_iter_recurse(&iter
, &sub
);
3152 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
3153 log_error("Failed to parse reply.");
3158 dbus_message_iter_get_basic(&sub
, &id
);
3166 dbus_message_unref(m
);
3169 dbus_message_unref(reply
);
3171 dbus_error_free(&error
);
3176 static int delete_snapshot(DBusConnection
*bus
, char **args
, unsigned n
) {
3177 DBusMessage
*m
= NULL
, *reply
= NULL
;
3185 dbus_error_init(&error
);
3187 for (i
= 1; i
< n
; i
++) {
3188 const char *path
= NULL
;
3190 if (!(m
= dbus_message_new_method_call(
3191 "org.freedesktop.systemd1",
3192 "/org/freedesktop/systemd1",
3193 "org.freedesktop.systemd1.Manager",
3195 log_error("Could not allocate message.");
3200 if (!dbus_message_append_args(m
,
3201 DBUS_TYPE_STRING
, &args
[i
],
3202 DBUS_TYPE_INVALID
)) {
3203 log_error("Could not append arguments to message.");
3208 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3209 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3214 if (!dbus_message_get_args(reply
, &error
,
3215 DBUS_TYPE_OBJECT_PATH
, &path
,
3216 DBUS_TYPE_INVALID
)) {
3217 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3222 dbus_message_unref(m
);
3223 if (!(m
= dbus_message_new_method_call(
3224 "org.freedesktop.systemd1",
3226 "org.freedesktop.systemd1.Snapshot",
3228 log_error("Could not allocate message.");
3233 dbus_message_unref(reply
);
3234 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3235 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3240 dbus_message_unref(m
);
3241 dbus_message_unref(reply
);
3249 dbus_message_unref(m
);
3252 dbus_message_unref(reply
);
3254 dbus_error_free(&error
);
3259 static int daemon_reload(DBusConnection
*bus
, char **args
, unsigned n
) {
3260 DBusMessage
*m
= NULL
, *reply
= NULL
;
3265 dbus_error_init(&error
);
3267 if (arg_action
== ACTION_RELOAD
)
3269 else if (arg_action
== ACTION_REEXEC
)
3270 method
= "Reexecute";
3272 assert(arg_action
== ACTION_SYSTEMCTL
);
3275 streq(args
[0], "clear-jobs") ||
3276 streq(args
[0], "cancel") ? "ClearJobs" :
3277 streq(args
[0], "daemon-reexec") ? "Reexecute" :
3278 streq(args
[0], "reset-failed") ? "ResetFailed" :
3279 streq(args
[0], "halt") ? "Halt" :
3280 streq(args
[0], "poweroff") ? "PowerOff" :
3281 streq(args
[0], "reboot") ? "Reboot" :
3282 streq(args
[0], "kexec") ? "KExec" :
3283 streq(args
[0], "exit") ? "Exit" :
3284 /* "daemon-reload" */ "Reload";
3287 if (!(m
= dbus_message_new_method_call(
3288 "org.freedesktop.systemd1",
3289 "/org/freedesktop/systemd1",
3290 "org.freedesktop.systemd1.Manager",
3292 log_error("Could not allocate message.");
3296 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3298 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(&error
)) {
3299 /* There's always a fallback possible for
3300 * legacy actions. */
3305 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3314 dbus_message_unref(m
);
3317 dbus_message_unref(reply
);
3319 dbus_error_free(&error
);
3324 static int reset_failed(DBusConnection
*bus
, char **args
, unsigned n
) {
3325 DBusMessage
*m
= NULL
, *reply
= NULL
;
3331 dbus_error_init(&error
);
3334 return daemon_reload(bus
, args
, n
);
3336 for (i
= 1; i
< n
; i
++) {
3338 if (!(m
= dbus_message_new_method_call(
3339 "org.freedesktop.systemd1",
3340 "/org/freedesktop/systemd1",
3341 "org.freedesktop.systemd1.Manager",
3342 "ResetFailedUnit"))) {
3343 log_error("Could not allocate message.");
3348 if (!dbus_message_append_args(m
,
3349 DBUS_TYPE_STRING
, args
+ i
,
3350 DBUS_TYPE_INVALID
)) {
3351 log_error("Could not append arguments to message.");
3356 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3357 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3362 dbus_message_unref(m
);
3363 dbus_message_unref(reply
);
3371 dbus_message_unref(m
);
3374 dbus_message_unref(reply
);
3376 dbus_error_free(&error
);
3381 static int show_enviroment(DBusConnection
*bus
, char **args
, unsigned n
) {
3382 DBusMessage
*m
= NULL
, *reply
= NULL
;
3384 DBusMessageIter iter
, sub
, sub2
;
3387 *interface
= "org.freedesktop.systemd1.Manager",
3388 *property
= "Environment";
3390 dbus_error_init(&error
);
3394 if (!(m
= dbus_message_new_method_call(
3395 "org.freedesktop.systemd1",
3396 "/org/freedesktop/systemd1",
3397 "org.freedesktop.DBus.Properties",
3399 log_error("Could not allocate message.");
3403 if (!dbus_message_append_args(m
,
3404 DBUS_TYPE_STRING
, &interface
,
3405 DBUS_TYPE_STRING
, &property
,
3406 DBUS_TYPE_INVALID
)) {
3407 log_error("Could not append arguments to message.");
3412 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3413 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3418 if (!dbus_message_iter_init(reply
, &iter
) ||
3419 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3420 log_error("Failed to parse reply.");
3425 dbus_message_iter_recurse(&iter
, &sub
);
3427 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_ARRAY
||
3428 dbus_message_iter_get_element_type(&sub
) != DBUS_TYPE_STRING
) {
3429 log_error("Failed to parse reply.");
3434 dbus_message_iter_recurse(&sub
, &sub2
);
3436 while (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_INVALID
) {
3439 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_STRING
) {
3440 log_error("Failed to parse reply.");
3445 dbus_message_iter_get_basic(&sub2
, &text
);
3446 printf("%s\n", text
);
3448 dbus_message_iter_next(&sub2
);
3455 dbus_message_unref(m
);
3458 dbus_message_unref(reply
);
3460 dbus_error_free(&error
);
3465 static int set_environment(DBusConnection
*bus
, char **args
, unsigned n
) {
3466 DBusMessage
*m
= NULL
, *reply
= NULL
;
3470 DBusMessageIter iter
, sub
;
3473 dbus_error_init(&error
);
3475 method
= streq(args
[0], "set-environment")
3477 : "UnsetEnvironment";
3479 if (!(m
= dbus_message_new_method_call(
3480 "org.freedesktop.systemd1",
3481 "/org/freedesktop/systemd1",
3482 "org.freedesktop.systemd1.Manager",
3485 log_error("Could not allocate message.");
3489 dbus_message_iter_init_append(m
, &iter
);
3491 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
)) {
3492 log_error("Could not append arguments to message.");
3497 for (i
= 1; i
< n
; i
++)
3498 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &args
[i
])) {
3499 log_error("Could not append arguments to message.");
3504 if (!dbus_message_iter_close_container(&iter
, &sub
)) {
3505 log_error("Could not append arguments to message.");
3510 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3511 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3520 dbus_message_unref(m
);
3523 dbus_message_unref(reply
);
3525 dbus_error_free(&error
);
3538 static Hashmap
*will_install
= NULL
, *have_installed
= NULL
;
3539 static Set
*remove_symlinks_to
= NULL
;
3540 static unsigned n_symlinks
= 0;
3542 static void install_info_free(InstallInfo
*i
) {
3547 strv_free(i
->aliases
);
3548 strv_free(i
->wanted_by
);
3552 static void install_info_hashmap_free(Hashmap
*m
) {
3555 while ((i
= hashmap_steal_first(m
)))
3556 install_info_free(i
);
3561 static int install_info_add(const char *name
) {
3565 assert(will_install
);
3567 if (!unit_name_is_valid_no_type(name
, true)) {
3568 log_warning("Unit name %s is not a valid unit name.", name
);
3572 if (hashmap_get(have_installed
, name
) ||
3573 hashmap_get(will_install
, name
))
3576 if (!(i
= new0(InstallInfo
, 1))) {
3581 if (!(i
->name
= strdup(name
))) {
3586 if ((r
= hashmap_put(will_install
, i
->name
, i
)) < 0)
3593 install_info_free(i
);
3598 static int config_parse_also(
3599 const char *filename
,
3601 const char *section
,
3615 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
3619 if (!(n
= strndup(w
, l
)))
3622 if ((r
= install_info_add(n
)) < 0) {
3623 log_warning("Cannot install unit %s: %s", n
, strerror(-r
));
3634 static int mark_symlink_for_removal(const char *p
) {
3639 assert(path_is_absolute(p
));
3641 if (!remove_symlinks_to
)
3644 if (!(n
= strdup(p
)))
3647 path_kill_slashes(n
);
3649 if ((r
= set_put(remove_symlinks_to
, n
)) < 0) {
3651 return r
== -EEXIST
? 0 : r
;
3657 static int remove_marked_symlinks_fd(int fd
, const char *config_path
, const char *root
, bool *deleted
) {
3666 if (!(d
= fdopendir(fd
))) {
3667 close_nointr_nofail(fd
);
3673 while ((de
= readdir(d
))) {
3674 bool is_dir
= false, is_link
= false;
3676 if (ignore_file(de
->d_name
))
3679 if (de
->d_type
== DT_LNK
)
3681 else if (de
->d_type
== DT_DIR
)
3683 else if (de
->d_type
== DT_UNKNOWN
) {
3686 if (fstatat(fd
, de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
3687 log_error("Failed to stat %s/%s: %m", root
, de
->d_name
);
3694 is_link
= S_ISLNK(st
.st_mode
);
3695 is_dir
= S_ISDIR(st
.st_mode
);
3703 if ((nfd
= openat(fd
, de
->d_name
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
)) < 0) {
3704 log_error("Failed to open %s/%s: %m", root
, de
->d_name
);
3711 if (asprintf(&p
, "%s/%s", root
, de
->d_name
) < 0) {
3712 log_error("Failed to allocate directory string.");
3713 close_nointr_nofail(nfd
);
3718 /* This will close nfd, regardless whether it succeeds or not */
3719 q
= remove_marked_symlinks_fd(nfd
, config_path
, p
, deleted
);
3725 } else if (is_link
) {
3729 if (asprintf(&p
, "%s/%s", root
, de
->d_name
) < 0) {
3730 log_error("Failed to allocate symlink string.");
3735 if ((q
= readlink_and_make_absolute(p
, &dest
)) < 0) {
3736 log_error("Cannot read symlink %s: %s", p
, strerror(-q
));
3744 if ((c
= canonicalize_file_name(dest
))) {
3745 /* This might fail if the destination
3746 * is already removed */
3752 path_kill_slashes(dest
);
3753 if (set_get(remove_symlinks_to
, dest
)) {
3756 log_info("rm '%s'", p
);
3758 if (unlink(p
) < 0) {
3759 log_error("Cannot unlink symlink %s: %m", p
);
3764 rmdir_parents(p
, config_path
);
3765 path_kill_slashes(p
);
3767 if (!set_get(remove_symlinks_to
, p
)) {
3769 if ((r
= mark_symlink_for_removal(p
)) < 0) {
3788 static int remove_marked_symlinks(const char *config_path
) {
3792 assert(config_path
);
3794 if (set_size(remove_symlinks_to
) <= 0)
3797 if ((fd
= open(config_path
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
)) < 0)
3804 if ((cfd
= dup(fd
)) < 0) {
3809 /* This takes possession of cfd and closes it */
3810 if ((q
= remove_marked_symlinks_fd(cfd
, config_path
, config_path
, &deleted
)) < 0) {
3816 close_nointr_nofail(fd
);
3821 static int create_symlink(const char *verb
, const char *old_path
, const char *new_path
) {
3828 if (streq(verb
, "enable")) {
3831 mkdir_parents(new_path
, 0755);
3833 if (symlink(old_path
, new_path
) >= 0) {
3836 log_info("ln -s '%s' '%s'", old_path
, new_path
);
3841 if (errno
!= EEXIST
) {
3842 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
3846 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3848 if (errno
== EINVAL
) {
3849 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path
, new_path
);
3853 log_error("readlink() failed: %s", strerror(-r
));
3857 if (streq(dest
, old_path
)) {
3863 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path
, new_path
, dest
);
3872 log_info("ln -s '%s' '%s'", old_path
, new_path
);
3874 if (symlink(old_path
, new_path
) >= 0)
3877 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
3880 } else if (streq(verb
, "disable")) {
3883 if ((r
= mark_symlink_for_removal(old_path
)) < 0)
3886 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3887 if (errno
== ENOENT
)
3890 if (errno
== EINVAL
) {
3891 log_warning("File %s not a symlink, ignoring.", old_path
);
3895 log_error("readlink() failed: %s", strerror(-r
));
3899 if (!streq(dest
, old_path
)) {
3900 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path
, old_path
, dest
);
3907 if ((r
= mark_symlink_for_removal(new_path
)) < 0)
3911 log_info("rm '%s'", new_path
);
3913 if (unlink(new_path
) >= 0)
3916 log_error("Cannot unlink %s: %m", new_path
);
3919 } else if (streq(verb
, "is-enabled")) {
3922 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3924 if (errno
== ENOENT
|| errno
== EINVAL
)
3927 log_error("readlink() failed: %s", strerror(-r
));
3931 if (streq(dest
, old_path
)) {
3939 assert_not_reached("Unknown action.");
3942 static int install_info_symlink_alias(const char *verb
, InstallInfo
*i
, const char *config_path
) {
3944 char *alias_path
= NULL
;
3949 assert(config_path
);
3951 STRV_FOREACH(s
, i
->aliases
) {
3954 if (!(alias_path
= path_make_absolute(*s
, config_path
))) {
3955 log_error("Out of memory");
3960 if ((r
= create_symlink(verb
, i
->path
, alias_path
)) != 0)
3963 if (streq(verb
, "disable"))
3964 rmdir_parents(alias_path
, config_path
);
3974 static int install_info_symlink_wants(const char *verb
, InstallInfo
*i
, const char *config_path
) {
3976 char *alias_path
= NULL
;
3981 assert(config_path
);
3983 STRV_FOREACH(s
, i
->wanted_by
) {
3984 if (!unit_name_is_valid_no_type(*s
, true)) {
3985 log_error("Invalid name %s.", *s
);
3993 if (asprintf(&alias_path
, "%s/%s.wants/%s", config_path
, *s
, i
->name
) < 0) {
3994 log_error("Out of memory");
3999 if ((r
= create_symlink(verb
, i
->path
, alias_path
)) != 0)
4002 if (streq(verb
, "disable"))
4003 rmdir_parents(alias_path
, config_path
);
4014 static int install_info_apply(const char *verb
, LookupPaths
*paths
, InstallInfo
*i
, const char *config_path
) {
4016 const ConfigItem items
[] = {
4017 { "Alias", config_parse_strv
, &i
->aliases
, "Install" },
4018 { "WantedBy", config_parse_strv
, &i
->wanted_by
, "Install" },
4019 { "Also", config_parse_also
, NULL
, "Install" },
4021 { NULL
, NULL
, NULL
, NULL
}
4025 char *filename
= NULL
;
4032 STRV_FOREACH(p
, paths
->unit_path
) {
4035 if (!(filename
= path_make_absolute(i
->name
, *p
))) {
4036 log_error("Out of memory");
4040 /* Ensure that we don't follow symlinks */
4041 if ((fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NOCTTY
)) >= 0)
4042 if ((f
= fdopen(fd
, "re")))
4045 if (errno
== ELOOP
) {
4046 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
4051 if (errno
!= ENOENT
) {
4052 log_error("Failed to open %s: %m", filename
);
4062 #if (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA)) && defined (HAVE_SYSV_COMPAT)
4064 if (endswith(i
->name
, ".service")) {
4068 if (asprintf(&sysv
, SYSTEM_SYSVINIT_PATH
"/%s", i
->name
) < 0) {
4069 log_error("Out of memory");
4073 sysv
[strlen(sysv
) - sizeof(".service") + 1] = 0;
4074 exists
= access(sysv
, F_OK
) >= 0;
4080 const char *argv
[] = {
4087 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i
->name
);
4089 argv
[1] = file_name_from_path(sysv
);
4091 streq(verb
, "enable") ? "on" :
4092 streq(verb
, "disable") ? "off" : "--level=3";
4094 log_info("Executing %s %s %s", argv
[0], argv
[1], strempty(argv
[2]));
4096 if ((pid
= fork()) < 0) {
4097 log_error("Failed to fork: %m");
4100 } else if (pid
== 0) {
4101 execv(argv
[0], (char**) argv
);
4102 _exit(EXIT_FAILURE
);
4107 if ((r
= wait_for_terminate(pid
, &status
)) < 0)
4110 if (status
.si_code
== CLD_EXITED
) {
4112 if (streq(verb
, "is-enabled"))
4113 return status
.si_status
== 0 ? 1 : 0;
4115 if (status
.si_status
== 0)
4118 return status
.si_status
== 0 ? 0 : -EINVAL
;
4129 log_error("Couldn't find %s.", i
->name
);
4135 if ((r
= config_parse(filename
, f
, NULL
, items
, true, i
)) < 0) {
4140 n_symlinks
+= strv_length(i
->aliases
);
4141 n_symlinks
+= strv_length(i
->wanted_by
);
4145 if ((r
= install_info_symlink_alias(verb
, i
, config_path
)) != 0)
4148 if ((r
= install_info_symlink_wants(verb
, i
, config_path
)) != 0)
4151 if ((r
= mark_symlink_for_removal(filename
)) < 0)
4154 if ((r
= remove_marked_symlinks(config_path
)) < 0)
4160 static char *get_config_path(void) {
4162 if (arg_user
&& arg_global
)
4163 return strdup(USER_CONFIG_UNIT_PATH
);
4168 if (user_config_home(&p
) < 0)
4174 return strdup(SYSTEM_CONFIG_UNIT_PATH
);
4177 static int enable_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
4181 char *config_path
= NULL
;
4184 const char *verb
= args
[0];
4186 dbus_error_init(&error
);
4189 if ((r
= lookup_paths_init(&paths
, arg_user
? MANAGER_USER
: MANAGER_SYSTEM
)) < 0) {
4190 log_error("Failed to determine lookup paths: %s", strerror(-r
));
4194 if (!(config_path
= get_config_path())) {
4195 log_error("Failed to determine config path");
4200 will_install
= hashmap_new(string_hash_func
, string_compare_func
);
4201 have_installed
= hashmap_new(string_hash_func
, string_compare_func
);
4203 if (!will_install
|| !have_installed
) {
4204 log_error("Failed to allocate unit sets.");
4209 if (!arg_defaults
&& streq(verb
, "disable"))
4210 if (!(remove_symlinks_to
= set_new(string_hash_func
, string_compare_func
))) {
4211 log_error("Failed to allocate symlink sets.");
4216 for (j
= 1; j
< n
; j
++)
4217 if ((r
= install_info_add(args
[j
])) < 0) {
4218 log_warning("Cannot install unit %s: %s", args
[j
], strerror(-r
));
4224 while ((i
= hashmap_first(will_install
))) {
4227 assert_se(hashmap_move_one(have_installed
, will_install
, i
->name
) == 0);
4229 if ((q
= install_info_apply(verb
, &paths
, i
, config_path
)) != 0) {
4237 /* In test mode and found something */
4243 if (streq(verb
, "is-enabled"))
4244 r
= r
> 0 ? 0 : -ENOENT
;
4246 if (n_symlinks
<= 0)
4247 log_warning("Unit files contain no applicable installation information. Ignoring.");
4250 /* Don't try to reload anything if the user asked us to not do this */
4252 /* Don't try to reload anything when updating a unit globally */
4254 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4255 (arg_user
|| sd_booted() > 0) &&
4256 /* Don't try to reload anything if we are running in a chroot environment */
4257 (arg_user
|| running_in_chroot() <= 0) ) {
4260 if ((q
= daemon_reload(bus
, args
, n
)) < 0)
4266 install_info_hashmap_free(will_install
);
4267 install_info_hashmap_free(have_installed
);
4269 set_free_free(remove_symlinks_to
);
4271 lookup_paths_free(&paths
);
4278 static int systemctl_help(void) {
4280 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4281 "Send control commands to or query the systemd manager.\n\n"
4282 " -h --help Show this help\n"
4283 " --version Show package version\n"
4284 " -t --type=TYPE List only units of a particular type\n"
4285 " -p --property=NAME Show only properties by this name\n"
4286 " -a --all Show all units/properties, including dead/empty ones\n"
4287 " --failed Show only failed units\n"
4288 " --full Don't ellipsize unit names on output\n"
4289 " --fail When queueing a new job, fail if conflicting jobs are\n"
4291 " --ignore-dependencies\n"
4292 " When queueing a new job, ignore all its dependencies\n"
4293 " -q --quiet Suppress output\n"
4294 " --no-block Do not wait until operation finished\n"
4295 " --no-pager Do not pipe output into a pager.\n"
4296 " --system Connect to system manager\n"
4297 " --user Connect to user service manager\n"
4298 " --order When generating graph for dot, show only order\n"
4299 " --require When generating graph for dot, show only requirement\n"
4300 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4301 " --global Enable/disable unit files globally\n"
4302 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4304 " --no-ask-password\n"
4305 " Do not ask for system passwords\n"
4306 " --kill-mode=MODE How to send signal\n"
4307 " --kill-who=WHO Who to send signal to\n"
4308 " -s --signal=SIGNAL Which signal to send\n"
4309 " -f --force When enabling unit files, override existing symlinks\n"
4310 " When shutting down, execute action immediately\n"
4311 " --defaults When disabling unit files, remove default symlinks only\n\n"
4313 " list-units List units\n"
4314 " start [NAME...] Start (activate) one or more units\n"
4315 " stop [NAME...] Stop (deactivate) one or more units\n"
4316 " reload [NAME...] Reload one or more units\n"
4317 " restart [NAME...] Start or restart one or more units\n"
4318 " try-restart [NAME...] Restart one or more units if active\n"
4319 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4320 " otherwise start or restart\n"
4321 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4322 " otherwise restart if active\n"
4323 " isolate [NAME] Start one unit and stop all others\n"
4324 " kill [NAME...] Send signal to processes of a unit\n"
4325 " is-active [NAME...] Check whether units are active\n"
4326 " status [NAME...|PID...] Show runtime status of one or more units\n"
4327 " show [NAME...|JOB...] Show properties of one or more\n"
4328 " units/jobs or the manager\n"
4329 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4331 " enable [NAME...] Enable one or more unit files\n"
4332 " disable [NAME...] Disable one or more unit files\n"
4333 " is-enabled [NAME...] Check whether unit files are enabled\n"
4334 " load [NAME...] Load one or more units\n"
4335 " list-jobs List jobs\n"
4336 " cancel [JOB...] Cancel all, one, or more jobs\n"
4337 " monitor Monitor unit/job changes\n"
4338 " dump Dump server status\n"
4339 " dot Dump dependency graph for dot(1)\n"
4340 " snapshot [NAME] Create a snapshot\n"
4341 " delete [NAME...] Remove one or more snapshots\n"
4342 " daemon-reload Reload systemd manager configuration\n"
4343 " daemon-reexec Reexecute systemd manager\n"
4344 " show-environment Dump environment\n"
4345 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4346 " unset-environment [NAME...] Unset one or more environment variables\n"
4347 " default Enter system default mode\n"
4348 " rescue Enter system rescue mode\n"
4349 " emergency Enter system emergency mode\n"
4350 " halt Shut down and halt the system\n"
4351 " poweroff Shut down and power-off the system\n"
4352 " reboot Shut down and reboot the system\n"
4353 " kexec Shut down and reboot the system with kexec\n"
4354 " exit Ask for user instance termination\n",
4355 program_invocation_short_name
);
4360 static int halt_help(void) {
4362 printf("%s [OPTIONS...]\n\n"
4363 "%s the system.\n\n"
4364 " --help Show this help\n"
4365 " --halt Halt the machine\n"
4366 " -p --poweroff Switch off the machine\n"
4367 " --reboot Reboot the machine\n"
4368 " -f --force Force immediate halt/power-off/reboot\n"
4369 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4370 " -d --no-wtmp Don't write wtmp record\n"
4371 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4372 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4373 program_invocation_short_name
,
4374 arg_action
== ACTION_REBOOT
? "Reboot" :
4375 arg_action
== ACTION_POWEROFF
? "Power off" :
4381 static int shutdown_help(void) {
4383 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4384 "Shut down the system.\n\n"
4385 " --help Show this help\n"
4386 " -H --halt Halt the machine\n"
4387 " -P --poweroff Power-off the machine\n"
4388 " -r --reboot Reboot the machine\n"
4389 " -h Equivalent to --poweroff, overriden by --halt\n"
4390 " -k Don't halt/power-off/reboot, just send warnings\n"
4391 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4392 " -c Cancel a pending shutdown\n",
4393 program_invocation_short_name
);
4398 static int telinit_help(void) {
4400 printf("%s [OPTIONS...] {COMMAND}\n\n"
4401 "Send control commands to the init daemon.\n\n"
4402 " --help Show this help\n"
4403 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4405 " 0 Power-off the machine\n"
4406 " 6 Reboot the machine\n"
4407 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4408 " 1, s, S Enter rescue mode\n"
4409 " q, Q Reload init daemon configuration\n"
4410 " u, U Reexecute init daemon\n",
4411 program_invocation_short_name
);
4416 static int runlevel_help(void) {
4418 printf("%s [OPTIONS...]\n\n"
4419 "Prints the previous and current runlevel of the init system.\n\n"
4420 " --help Show this help\n",
4421 program_invocation_short_name
);
4426 static int systemctl_parse_argv(int argc
, char *argv
[]) {
4430 ARG_IGNORE_DEPENDENCIES
,
4445 ARG_NO_ASK_PASSWORD
,
4449 static const struct option options
[] = {
4450 { "help", no_argument
, NULL
, 'h' },
4451 { "version", no_argument
, NULL
, ARG_VERSION
},
4452 { "type", required_argument
, NULL
, 't' },
4453 { "property", required_argument
, NULL
, 'p' },
4454 { "all", no_argument
, NULL
, 'a' },
4455 { "failed", no_argument
, NULL
, ARG_FAILED
},
4456 { "full", no_argument
, NULL
, ARG_FULL
},
4457 { "fail", no_argument
, NULL
, ARG_FAIL
},
4458 { "ignore-dependencies", no_argument
, NULL
, ARG_IGNORE_DEPENDENCIES
},
4459 { "user", no_argument
, NULL
, ARG_USER
},
4460 { "system", no_argument
, NULL
, ARG_SYSTEM
},
4461 { "global", no_argument
, NULL
, ARG_GLOBAL
},
4462 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
4463 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
4464 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4465 { "quiet", no_argument
, NULL
, 'q' },
4466 { "order", no_argument
, NULL
, ARG_ORDER
},
4467 { "require", no_argument
, NULL
, ARG_REQUIRE
},
4468 { "force", no_argument
, NULL
, 'f' },
4469 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
4470 { "defaults", no_argument
, NULL
, ARG_DEFAULTS
},
4471 { "kill-mode", required_argument
, NULL
, ARG_KILL_MODE
},
4472 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
4473 { "signal", required_argument
, NULL
, 's' },
4474 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
4475 { NULL
, 0, NULL
, 0 }
4483 /* Only when running as systemctl we ask for passwords */
4484 arg_ask_password
= true;
4486 while ((c
= getopt_long(argc
, argv
, "ht:p:aqfs:", options
, NULL
)) >= 0) {
4495 puts(PACKAGE_STRING
);
4497 puts(SYSTEMD_FEATURES
);
4507 if (!(l
= strv_append(arg_property
, optarg
)))
4510 strv_free(arg_property
);
4513 /* If the user asked for a particular
4514 * property, show it to him, even if it is
4525 arg_job_mode
= "fail";
4528 case ARG_IGNORE_DEPENDENCIES
:
4529 arg_job_mode
= "ignore-dependencies";
4541 arg_no_block
= true;
4545 arg_no_pager
= true;
4553 arg_dot
= DOT_ORDER
;
4557 arg_dot
= DOT_REQUIRE
;
4577 arg_no_reload
= true;
4586 arg_defaults
= true;
4590 arg_kill_who
= optarg
;
4594 arg_kill_mode
= optarg
;
4598 if ((arg_signal
= signal_from_string_try_harder(optarg
)) < 0) {
4599 log_error("Failed to parse signal string %s.", optarg
);
4604 case ARG_NO_ASK_PASSWORD
:
4605 arg_ask_password
= false;
4612 log_error("Unknown option code %c", c
);
4620 static int halt_parse_argv(int argc
, char *argv
[]) {
4629 static const struct option options
[] = {
4630 { "help", no_argument
, NULL
, ARG_HELP
},
4631 { "halt", no_argument
, NULL
, ARG_HALT
},
4632 { "poweroff", no_argument
, NULL
, 'p' },
4633 { "reboot", no_argument
, NULL
, ARG_REBOOT
},
4634 { "force", no_argument
, NULL
, 'f' },
4635 { "wtmp-only", no_argument
, NULL
, 'w' },
4636 { "no-wtmp", no_argument
, NULL
, 'd' },
4637 { "no-sync", no_argument
, NULL
, 'n' },
4638 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4639 { NULL
, 0, NULL
, 0 }
4647 if (utmp_get_runlevel(&runlevel
, NULL
) >= 0)
4648 if (runlevel
== '0' || runlevel
== '6')
4649 arg_immediate
= true;
4651 while ((c
= getopt_long(argc
, argv
, "pfwdnih", options
, NULL
)) >= 0) {
4659 arg_action
= ACTION_HALT
;
4663 if (arg_action
!= ACTION_REBOOT
)
4664 arg_action
= ACTION_POWEROFF
;
4668 arg_action
= ACTION_REBOOT
;
4672 arg_immediate
= true;
4693 /* Compatibility nops */
4700 log_error("Unknown option code %c", c
);
4705 if (optind
< argc
) {
4706 log_error("Too many arguments.");
4713 static int parse_time_spec(const char *t
, usec_t
*_u
) {
4717 if (streq(t
, "now"))
4719 else if (t
[0] == '+') {
4722 if (safe_atou64(t
+ 1, &u
) < 0)
4725 *_u
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
* u
;
4734 hour
= strtol(t
, &e
, 10);
4735 if (errno
!= 0 || *e
!= ':' || hour
< 0 || hour
> 23)
4738 minute
= strtol(e
+1, &e
, 10);
4739 if (errno
!= 0 || *e
!= 0 || minute
< 0 || minute
> 59)
4742 n
= now(CLOCK_REALTIME
);
4743 s
= (time_t) (n
/ USEC_PER_SEC
);
4746 assert_se(localtime_r(&s
, &tm
));
4748 tm
.tm_hour
= (int) hour
;
4749 tm
.tm_min
= (int) minute
;
4752 assert_se(s
= mktime(&tm
));
4754 *_u
= (usec_t
) s
* USEC_PER_SEC
;
4757 *_u
+= USEC_PER_DAY
;
4763 static bool kexec_loaded(void) {
4764 bool loaded
= false;
4767 if (read_one_line_file("/sys/kernel/kexec_loaded", &s
) >= 0) {
4775 static int shutdown_parse_argv(int argc
, char *argv
[]) {
4782 static const struct option options
[] = {
4783 { "help", no_argument
, NULL
, ARG_HELP
},
4784 { "halt", no_argument
, NULL
, 'H' },
4785 { "poweroff", no_argument
, NULL
, 'P' },
4786 { "reboot", no_argument
, NULL
, 'r' },
4787 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4788 { NULL
, 0, NULL
, 0 }
4796 while ((c
= getopt_long(argc
, argv
, "HPrhkt:afFc", options
, NULL
)) >= 0) {
4804 arg_action
= ACTION_HALT
;
4808 arg_action
= ACTION_POWEROFF
;
4813 arg_action
= ACTION_KEXEC
;
4815 arg_action
= ACTION_REBOOT
;
4819 if (arg_action
!= ACTION_HALT
)
4820 arg_action
= ACTION_POWEROFF
;
4833 /* Compatibility nops */
4837 arg_action
= ACTION_CANCEL_SHUTDOWN
;
4844 log_error("Unknown option code %c", c
);
4849 if (argc
> optind
) {
4850 if ((r
= parse_time_spec(argv
[optind
], &arg_when
)) < 0) {
4851 log_error("Failed to parse time specification: %s", argv
[optind
]);
4855 arg_when
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
;
4857 /* We skip the time argument */
4858 if (argc
> optind
+ 1)
4859 arg_wall
= argv
+ optind
+ 1;
4866 static int telinit_parse_argv(int argc
, char *argv
[]) {
4873 static const struct option options
[] = {
4874 { "help", no_argument
, NULL
, ARG_HELP
},
4875 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4876 { NULL
, 0, NULL
, 0 }
4879 static const struct {
4883 { '0', ACTION_POWEROFF
},
4884 { '6', ACTION_REBOOT
},
4885 { '1', ACTION_RESCUE
},
4886 { '2', ACTION_RUNLEVEL2
},
4887 { '3', ACTION_RUNLEVEL3
},
4888 { '4', ACTION_RUNLEVEL4
},
4889 { '5', ACTION_RUNLEVEL5
},
4890 { 's', ACTION_RESCUE
},
4891 { 'S', ACTION_RESCUE
},
4892 { 'q', ACTION_RELOAD
},
4893 { 'Q', ACTION_RELOAD
},
4894 { 'u', ACTION_REEXEC
},
4895 { 'U', ACTION_REEXEC
}
4904 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
4919 log_error("Unknown option code %c", c
);
4924 if (optind
>= argc
) {
4929 if (optind
+ 1 < argc
) {
4930 log_error("Too many arguments.");
4934 if (strlen(argv
[optind
]) != 1) {
4935 log_error("Expected single character argument.");
4939 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
4940 if (table
[i
].from
== argv
[optind
][0])
4943 if (i
>= ELEMENTSOF(table
)) {
4944 log_error("Unknown command %s.", argv
[optind
]);
4948 arg_action
= table
[i
].to
;
4955 static int runlevel_parse_argv(int argc
, char *argv
[]) {
4961 static const struct option options
[] = {
4962 { "help", no_argument
, NULL
, ARG_HELP
},
4963 { NULL
, 0, NULL
, 0 }
4971 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
4982 log_error("Unknown option code %c", c
);
4987 if (optind
< argc
) {
4988 log_error("Too many arguments.");
4995 static int parse_argv(int argc
, char *argv
[]) {
4999 if (program_invocation_short_name
) {
5001 if (strstr(program_invocation_short_name
, "halt")) {
5002 arg_action
= ACTION_HALT
;
5003 return halt_parse_argv(argc
, argv
);
5004 } else if (strstr(program_invocation_short_name
, "poweroff")) {
5005 arg_action
= ACTION_POWEROFF
;
5006 return halt_parse_argv(argc
, argv
);
5007 } else if (strstr(program_invocation_short_name
, "reboot")) {
5009 arg_action
= ACTION_KEXEC
;
5011 arg_action
= ACTION_REBOOT
;
5012 return halt_parse_argv(argc
, argv
);
5013 } else if (strstr(program_invocation_short_name
, "shutdown")) {
5014 arg_action
= ACTION_POWEROFF
;
5015 return shutdown_parse_argv(argc
, argv
);
5016 } else if (strstr(program_invocation_short_name
, "init")) {
5018 if (sd_booted() > 0) {
5019 arg_action
= ACTION_INVALID
;
5020 return telinit_parse_argv(argc
, argv
);
5022 /* Hmm, so some other init system is
5023 * running, we need to forward this
5024 * request to it. For now we simply
5025 * guess that it is Upstart. */
5027 execv("/lib/upstart/telinit", argv
);
5029 log_error("Couldn't find an alternative telinit implementation to spawn.");
5033 } else if (strstr(program_invocation_short_name
, "runlevel")) {
5034 arg_action
= ACTION_RUNLEVEL
;
5035 return runlevel_parse_argv(argc
, argv
);
5039 arg_action
= ACTION_SYSTEMCTL
;
5040 return systemctl_parse_argv(argc
, argv
);
5043 static int action_to_runlevel(void) {
5045 static const char table
[_ACTION_MAX
] = {
5046 [ACTION_HALT
] = '0',
5047 [ACTION_POWEROFF
] = '0',
5048 [ACTION_REBOOT
] = '6',
5049 [ACTION_RUNLEVEL2
] = '2',
5050 [ACTION_RUNLEVEL3
] = '3',
5051 [ACTION_RUNLEVEL4
] = '4',
5052 [ACTION_RUNLEVEL5
] = '5',
5053 [ACTION_RESCUE
] = '1'
5056 assert(arg_action
< _ACTION_MAX
);
5058 return table
[arg_action
];
5061 static int talk_upstart(void) {
5062 DBusMessage
*m
= NULL
, *reply
= NULL
;
5064 int previous
, rl
, r
;
5066 env1_buf
[] = "RUNLEVEL=X",
5067 env2_buf
[] = "PREVLEVEL=X";
5068 char *env1
= env1_buf
, *env2
= env2_buf
;
5069 const char *emit
= "runlevel";
5070 dbus_bool_t b_false
= FALSE
;
5071 DBusMessageIter iter
, sub
;
5072 DBusConnection
*bus
;
5074 dbus_error_init(&error
);
5076 if (!(rl
= action_to_runlevel()))
5079 if (utmp_get_runlevel(&previous
, NULL
) < 0)
5082 if (!(bus
= dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error
))) {
5083 if (dbus_error_has_name(&error
, DBUS_ERROR_NO_SERVER
)) {
5088 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error
));
5093 if ((r
= bus_check_peercred(bus
)) < 0) {
5094 log_error("Failed to verify owner of bus.");
5098 if (!(m
= dbus_message_new_method_call(
5099 "com.ubuntu.Upstart",
5100 "/com/ubuntu/Upstart",
5101 "com.ubuntu.Upstart0_6",
5104 log_error("Could not allocate message.");
5109 dbus_message_iter_init_append(m
, &iter
);
5111 env1_buf
[sizeof(env1_buf
)-2] = rl
;
5112 env2_buf
[sizeof(env2_buf
)-2] = previous
;
5114 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &emit
) ||
5115 !dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
) ||
5116 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env1
) ||
5117 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env2
) ||
5118 !dbus_message_iter_close_container(&iter
, &sub
) ||
5119 !dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &b_false
)) {
5120 log_error("Could not append arguments to message.");
5125 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
5127 if (error_is_no_service(&error
)) {
5132 log_error("Failed to issue method call: %s", bus_error_message(&error
));
5141 dbus_message_unref(m
);
5144 dbus_message_unref(reply
);
5147 dbus_connection_flush(bus
);
5148 dbus_connection_close(bus
);
5149 dbus_connection_unref(bus
);
5152 dbus_error_free(&error
);
5157 static int talk_initctl(void) {
5158 struct init_request request
;
5162 if (!(rl
= action_to_runlevel()))
5166 request
.magic
= INIT_MAGIC
;
5167 request
.sleeptime
= 0;
5168 request
.cmd
= INIT_CMD_RUNLVL
;
5169 request
.runlevel
= rl
;
5171 if ((fd
= open(INIT_FIFO
, O_WRONLY
|O_NDELAY
|O_CLOEXEC
|O_NOCTTY
)) < 0) {
5173 if (errno
== ENOENT
)
5176 log_error("Failed to open "INIT_FIFO
": %m");
5181 r
= loop_write(fd
, &request
, sizeof(request
), false) != sizeof(request
);
5182 close_nointr_nofail(fd
);
5185 log_error("Failed to write to "INIT_FIFO
": %m");
5186 return errno
? -errno
: -EIO
;
5192 static int systemctl_main(DBusConnection
*bus
, int argc
, char *argv
[], DBusError
*error
) {
5194 static const struct {
5202 int (* const dispatch
)(DBusConnection
*bus
, char **args
, unsigned n
);
5204 { "list-units", LESS
, 1, list_units
},
5205 { "list-jobs", EQUAL
, 1, list_jobs
},
5206 { "clear-jobs", EQUAL
, 1, daemon_reload
},
5207 { "load", MORE
, 2, load_unit
},
5208 { "cancel", MORE
, 2, cancel_job
},
5209 { "start", MORE
, 2, start_unit
},
5210 { "stop", MORE
, 2, start_unit
},
5211 { "reload", MORE
, 2, start_unit
},
5212 { "restart", MORE
, 2, start_unit
},
5213 { "try-restart", MORE
, 2, start_unit
},
5214 { "reload-or-restart", MORE
, 2, start_unit
},
5215 { "reload-or-try-restart", MORE
, 2, start_unit
},
5216 { "force-reload", MORE
, 2, start_unit
}, /* For compatibility with SysV */
5217 { "condreload", MORE
, 2, start_unit
}, /* For compatibility with ALTLinux */
5218 { "condrestart", MORE
, 2, start_unit
}, /* For compatibility with RH */
5219 { "isolate", EQUAL
, 2, start_unit
},
5220 { "kill", MORE
, 2, kill_unit
},
5221 { "is-active", MORE
, 2, check_unit
},
5222 { "check", MORE
, 2, check_unit
},
5223 { "show", MORE
, 1, show
},
5224 { "status", MORE
, 2, show
},
5225 { "monitor", EQUAL
, 1, monitor
},
5226 { "dump", EQUAL
, 1, dump
},
5227 { "dot", EQUAL
, 1, dot
},
5228 { "snapshot", LESS
, 2, snapshot
},
5229 { "delete", MORE
, 2, delete_snapshot
},
5230 { "daemon-reload", EQUAL
, 1, daemon_reload
},
5231 { "daemon-reexec", EQUAL
, 1, daemon_reload
},
5232 { "show-environment", EQUAL
, 1, show_enviroment
},
5233 { "set-environment", MORE
, 2, set_environment
},
5234 { "unset-environment", MORE
, 2, set_environment
},
5235 { "halt", EQUAL
, 1, start_special
},
5236 { "poweroff", EQUAL
, 1, start_special
},
5237 { "reboot", EQUAL
, 1, start_special
},
5238 { "kexec", EQUAL
, 1, start_special
},
5239 { "default", EQUAL
, 1, start_special
},
5240 { "rescue", EQUAL
, 1, start_special
},
5241 { "emergency", EQUAL
, 1, start_special
},
5242 { "exit", EQUAL
, 1, start_special
},
5243 { "reset-failed", MORE
, 1, reset_failed
},
5244 { "enable", MORE
, 2, enable_unit
},
5245 { "disable", MORE
, 2, enable_unit
},
5246 { "is-enabled", MORE
, 2, enable_unit
}
5256 left
= argc
- optind
;
5259 /* Special rule: no arguments means "list-units" */
5262 if (streq(argv
[optind
], "help")) {
5267 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
5268 if (streq(argv
[optind
], verbs
[i
].verb
))
5271 if (i
>= ELEMENTSOF(verbs
)) {
5272 log_error("Unknown operation %s", argv
[optind
]);
5277 switch (verbs
[i
].argc_cmp
) {
5280 if (left
!= verbs
[i
].argc
) {
5281 log_error("Invalid number of arguments.");
5288 if (left
< verbs
[i
].argc
) {
5289 log_error("Too few arguments.");
5296 if (left
> verbs
[i
].argc
) {
5297 log_error("Too many arguments.");
5304 assert_not_reached("Unknown comparison operator.");
5307 /* Require a bus connection for all operations but
5309 if (!streq(verbs
[i
].verb
, "enable") &&
5310 !streq(verbs
[i
].verb
, "disable") &&
5312 log_error("Failed to get D-Bus connection: %s", error
->message
);
5316 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
5319 static int send_shutdownd(usec_t t
, char mode
, bool warn
, const char *message
) {
5321 struct msghdr msghdr
;
5323 union sockaddr_union sockaddr
;
5324 struct shutdownd_command c
;
5332 strncpy(c
.wall_message
, message
, sizeof(c
.wall_message
));
5334 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0)) < 0)
5338 sockaddr
.sa
.sa_family
= AF_UNIX
;
5339 sockaddr
.un
.sun_path
[0] = 0;
5340 strncpy(sockaddr
.un
.sun_path
+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr
.un
.sun_path
)-1);
5343 iovec
.iov_base
= (char*) &c
;
5344 iovec
.iov_len
= sizeof(c
);
5347 msghdr
.msg_name
= &sockaddr
;
5348 msghdr
.msg_namelen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5350 msghdr
.msg_iov
= &iovec
;
5351 msghdr
.msg_iovlen
= 1;
5353 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) < 0) {
5354 close_nointr_nofail(fd
);
5358 close_nointr_nofail(fd
);
5362 static int reload_with_fallback(DBusConnection
*bus
) {
5365 /* First, try systemd via D-Bus. */
5366 if (daemon_reload(bus
, NULL
, 0) > 0)
5370 /* Nothing else worked, so let's try signals */
5371 assert(arg_action
== ACTION_RELOAD
|| arg_action
== ACTION_REEXEC
);
5373 if (kill(1, arg_action
== ACTION_RELOAD
? SIGHUP
: SIGTERM
) < 0) {
5374 log_error("kill() failed: %m");
5381 static int start_with_fallback(DBusConnection
*bus
) {
5384 /* First, try systemd via D-Bus. */
5385 if (start_unit(bus
, NULL
, 0) >= 0)
5389 /* Hmm, talking to systemd via D-Bus didn't work. Then
5390 * let's try to talk to Upstart via D-Bus. */
5391 if (talk_upstart() > 0)
5394 /* Nothing else worked, so let's try
5396 if (talk_initctl() > 0)
5399 log_error("Failed to talk to init daemon.");
5403 warn_wall(arg_action
);
5407 static int halt_main(DBusConnection
*bus
) {
5410 if (geteuid() != 0) {
5411 log_error("Must be root.");
5417 char date
[FORMAT_TIMESTAMP_MAX
];
5419 m
= strv_join(arg_wall
, " ");
5420 r
= send_shutdownd(arg_when
,
5421 arg_action
== ACTION_HALT
? 'H' :
5422 arg_action
== ACTION_POWEROFF
? 'P' :
5429 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r
));
5431 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5432 format_timestamp(date
, sizeof(date
), arg_when
));
5437 if (!arg_dry
&& !arg_immediate
)
5438 return start_with_fallback(bus
);
5441 if (sd_booted() > 0)
5442 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5443 else if ((r
= utmp_put_shutdown(0)) < 0)
5444 log_warning("Failed to write utmp record: %s", strerror(-r
));
5453 /* Make sure C-A-D is handled by the kernel from this
5455 reboot(RB_ENABLE_CAD
);
5457 switch (arg_action
) {
5460 log_info("Halting.");
5461 reboot(RB_HALT_SYSTEM
);
5464 case ACTION_POWEROFF
:
5465 log_info("Powering off.");
5466 reboot(RB_POWER_OFF
);
5470 log_info("Rebooting.");
5471 reboot(RB_AUTOBOOT
);
5475 assert_not_reached("Unknown halt action.");
5478 /* We should never reach this. */
5482 static int runlevel_main(void) {
5483 int r
, runlevel
, previous
;
5485 if ((r
= utmp_get_runlevel(&runlevel
, &previous
)) < 0) {
5486 printf("unknown\n");
5491 previous
<= 0 ? 'N' : previous
,
5492 runlevel
<= 0 ? 'N' : runlevel
);
5497 static void pager_open(void) {
5505 if (!on_tty() || arg_no_pager
)
5508 if ((pager
= getenv("PAGER")))
5509 if (!*pager
|| streq(pager
, "cat"))
5512 /* Determine and cache number of columns before we spawn the
5513 * pager so that we get the value from the actual tty */
5517 log_error("Failed to create pager pipe: %m");
5521 parent_pid
= getpid();
5524 if (pager_pid
< 0) {
5525 log_error("Failed to fork pager: %m");
5530 /* In the child start the pager */
5531 if (pager_pid
== 0) {
5533 dup2(fd
[0], STDIN_FILENO
);
5536 setenv("LESS", "FRSX", 0);
5538 /* Make sure the pager goes away when the parent dies */
5539 if (prctl(PR_SET_PDEATHSIG
, SIGTERM
) < 0)
5540 _exit(EXIT_FAILURE
);
5542 /* Check whether our parent died before we were able
5543 * to set the death signal */
5544 if (getppid() != parent_pid
)
5545 _exit(EXIT_SUCCESS
);
5548 execlp(pager
, pager
, NULL
);
5549 execl("/bin/sh", "sh", "-c", pager
, NULL
);
5551 /* Debian's alternatives command for pagers is
5552 * called 'pager'. Note that we do not call
5553 * sensible-pagers here, since that is just a
5554 * shell script that implements a logic that
5555 * is similar to this one anyway, but is
5556 * Debian-specific. */
5557 execlp("pager", "pager", NULL
);
5559 execlp("less", "less", NULL
);
5560 execlp("more", "more", NULL
);
5563 log_error("Unable to execute pager: %m");
5564 _exit(EXIT_FAILURE
);
5567 /* Return in the parent */
5568 if (dup2(fd
[1], STDOUT_FILENO
) < 0)
5569 log_error("Failed to duplicate pager pipe: %m");
5574 static void pager_close(void) {
5580 /* Inform pager that we are done */
5582 kill(pager_pid
, SIGCONT
);
5583 wait_for_terminate(pager_pid
, &dummy
);
5587 static void agent_close(void) {
5593 /* Inform agent that we are done */
5594 kill(agent_pid
, SIGTERM
);
5595 kill(agent_pid
, SIGCONT
);
5596 wait_for_terminate(agent_pid
, &dummy
);
5600 int main(int argc
, char*argv
[]) {
5601 int r
, retval
= EXIT_FAILURE
;
5602 DBusConnection
*bus
= NULL
;
5605 dbus_error_init(&error
);
5607 log_parse_environment();
5610 if ((r
= parse_argv(argc
, argv
)) < 0)
5613 retval
= EXIT_SUCCESS
;
5617 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5618 * let's shortcut this */
5619 if (arg_action
== ACTION_RUNLEVEL
) {
5620 r
= runlevel_main();
5621 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5625 bus_connect(arg_user
? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &bus
, &private_bus
, &error
);
5627 switch (arg_action
) {
5629 case ACTION_SYSTEMCTL
:
5630 r
= systemctl_main(bus
, argc
, argv
, &error
);
5634 case ACTION_POWEROFF
:
5640 case ACTION_RUNLEVEL2
:
5641 case ACTION_RUNLEVEL3
:
5642 case ACTION_RUNLEVEL4
:
5643 case ACTION_RUNLEVEL5
:
5645 case ACTION_EMERGENCY
:
5646 case ACTION_DEFAULT
:
5647 r
= start_with_fallback(bus
);
5652 r
= reload_with_fallback(bus
);
5655 case ACTION_CANCEL_SHUTDOWN
:
5656 r
= send_shutdownd(0, 0, false, NULL
);
5659 case ACTION_INVALID
:
5660 case ACTION_RUNLEVEL
:
5662 assert_not_reached("Unknown action");
5665 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5670 dbus_connection_flush(bus
);
5671 dbus_connection_close(bus
);
5672 dbus_connection_unref(bus
);
5675 dbus_error_free(&error
);
5679 strv_free(arg_property
);