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
;
111 static enum transport
{
115 } arg_transport
= TRANSPORT_NORMAL
;
116 static const char *arg_host
= NULL
;
118 static bool private_bus
= false;
120 static pid_t pager_pid
= 0;
121 static pid_t agent_pid
= 0;
123 static int daemon_reload(DBusConnection
*bus
, char **args
, unsigned n
);
124 static void pager_open(void);
126 static bool on_tty(void) {
129 /* Note that this is invoked relatively early, before we start
130 * the pager. That means the value we return reflects whether
131 * we originally were started on a tty, not if we currently
132 * are. But this is intended, since we want colour and so on
133 * when run in our own pager. */
135 if (_unlikely_(t
< 0))
136 t
= isatty(STDOUT_FILENO
) > 0;
141 static void spawn_ask_password_agent(void) {
147 /* We check STDIN here, not STDOUT, since this is about input,
149 if (!isatty(STDIN_FILENO
))
152 if (!arg_ask_password
)
160 /* Spawns a temporary TTY agent, making sure it goes away when
163 if ((agent_pid
= fork()) < 0)
166 if (agent_pid
== 0) {
170 bool stdout_is_tty
, stderr_is_tty
;
172 /* Make sure the agent goes away when the parent dies */
173 if (prctl(PR_SET_PDEATHSIG
, SIGTERM
) < 0)
176 /* Check whether our parent died before we were able
177 * to set the death signal */
178 if (getppid() != parent
)
181 /* Don't leak fds to the agent */
182 close_all_fds(NULL
, 0);
184 stdout_is_tty
= isatty(STDOUT_FILENO
);
185 stderr_is_tty
= isatty(STDERR_FILENO
);
187 if (!stdout_is_tty
|| !stderr_is_tty
) {
188 /* Detach from stdout/stderr. and reopen
189 * /dev/tty for them. This is important to
190 * ensure that when systemctl is started via
191 * popen() or a similar call that expects to
192 * read EOF we actually do generate EOF and
193 * not delay this indefinitely by because we
194 * keep an unused copy of stdin around. */
195 if ((fd
= open("/dev/tty", O_WRONLY
)) < 0) {
196 log_error("Failed to open /dev/tty: %m");
201 dup2(fd
, STDOUT_FILENO
);
204 dup2(fd
, STDERR_FILENO
);
210 execl(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH
, SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH
, "--watch", NULL
);
212 log_error("Unable to execute agent: %m");
217 static const char *ansi_highlight(bool b
) {
222 return b
? ANSI_HIGHLIGHT_ON
: ANSI_HIGHLIGHT_OFF
;
225 static const char *ansi_highlight_green(bool b
) {
230 return b
? ANSI_HIGHLIGHT_GREEN_ON
: ANSI_HIGHLIGHT_OFF
;
233 static bool error_is_no_service(const DBusError
*error
) {
236 if (!dbus_error_is_set(error
))
239 if (dbus_error_has_name(error
, DBUS_ERROR_NAME_HAS_NO_OWNER
))
242 if (dbus_error_has_name(error
, DBUS_ERROR_SERVICE_UNKNOWN
))
245 return startswith(error
->name
, "org.freedesktop.DBus.Error.Spawn.");
248 static int translate_bus_error_to_exit_status(int r
, const DBusError
*error
) {
251 if (!dbus_error_is_set(error
))
254 if (dbus_error_has_name(error
, DBUS_ERROR_ACCESS_DENIED
) ||
255 dbus_error_has_name(error
, BUS_ERROR_ONLY_BY_DEPENDENCY
) ||
256 dbus_error_has_name(error
, BUS_ERROR_NO_ISOLATION
) ||
257 dbus_error_has_name(error
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
))
258 return EXIT_NOPERMISSION
;
260 if (dbus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
))
261 return EXIT_NOTINSTALLED
;
263 if (dbus_error_has_name(error
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
) ||
264 dbus_error_has_name(error
, BUS_ERROR_NOT_SUPPORTED
))
265 return EXIT_NOTIMPLEMENTED
;
267 if (dbus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
))
268 return EXIT_NOTCONFIGURED
;
276 static int bus_iter_get_basic_and_next(DBusMessageIter
*iter
, int type
, void *data
, bool next
) {
281 if (dbus_message_iter_get_arg_type(iter
) != type
)
284 dbus_message_iter_get_basic(iter
, data
);
286 if (!dbus_message_iter_next(iter
) != !next
)
292 static void warn_wall(enum action action
) {
293 static const char *table
[_ACTION_MAX
] = {
294 [ACTION_HALT
] = "The system is going down for system halt NOW!",
295 [ACTION_REBOOT
] = "The system is going down for reboot NOW!",
296 [ACTION_POWEROFF
] = "The system is going down for power-off NOW!",
297 [ACTION_KEXEC
] = "The system is going down for kexec reboot NOW!",
298 [ACTION_RESCUE
] = "The system is going down to rescue mode NOW!",
299 [ACTION_EMERGENCY
] = "The system is going down to emergency mode NOW!"
308 if (!(p
= strv_join(arg_wall
, " "))) {
309 log_error("Failed to join strings.");
325 utmp_wall(table
[action
], NULL
);
330 const char *description
;
331 const char *load_state
;
332 const char *active_state
;
333 const char *sub_state
;
334 const char *following
;
335 const char *unit_path
;
337 const char *job_type
;
338 const char *job_path
;
341 static int compare_unit_info(const void *a
, const void *b
) {
343 const struct unit_info
*u
= a
, *v
= b
;
345 d1
= strrchr(u
->id
, '.');
346 d2
= strrchr(v
->id
, '.');
351 if ((r
= strcasecmp(d1
, d2
)) != 0)
355 return strcasecmp(u
->id
, v
->id
);
358 static bool output_show_unit(const struct unit_info
*u
) {
362 return streq(u
->active_state
, "failed");
364 return (!arg_type
|| ((dot
= strrchr(u
->id
, '.')) &&
365 streq(dot
+1, arg_type
))) &&
366 (arg_all
|| !(streq(u
->active_state
, "inactive") || u
->following
[0]) || u
->job_id
> 0);
369 static void output_units_list(const struct unit_info
*unit_infos
, unsigned c
) {
370 unsigned active_len
, sub_len
, job_len
, n_shown
= 0;
371 const struct unit_info
*u
;
373 active_len
= sizeof("ACTIVE")-1;
374 sub_len
= sizeof("SUB")-1;
375 job_len
= sizeof("JOB")-1;
377 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
378 if (!output_show_unit(u
))
381 active_len
= MAX(active_len
, strlen(u
->active_state
));
382 sub_len
= MAX(sub_len
, strlen(u
->sub_state
));
384 job_len
= MAX(job_len
, strlen(u
->job_type
));
388 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
389 active_len
, "ACTIVE", sub_len
, "SUB", job_len
, "JOB");
390 if (columns() >= 80+12 || arg_full
|| !arg_no_pager
)
391 printf(" %s\n", "DESCRIPTION");
396 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
399 const char *on_loaded
, *off_loaded
;
400 const char *on_active
, *off_active
;
402 if (!output_show_unit(u
))
407 if (!streq(u
->load_state
, "loaded") &&
408 !streq(u
->load_state
, "banned")) {
409 on_loaded
= ansi_highlight(true);
410 off_loaded
= ansi_highlight(false);
412 on_loaded
= off_loaded
= "";
414 if (streq(u
->active_state
, "failed")) {
415 on_active
= ansi_highlight(true);
416 off_active
= ansi_highlight(false);
418 on_active
= off_active
= "";
420 e
= arg_full
? NULL
: ellipsize(u
->id
, 25, 33);
422 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
424 on_loaded
, u
->load_state
, off_loaded
,
425 on_active
, active_len
, u
->active_state
,
426 sub_len
, u
->sub_state
, off_active
,
431 a
-= strlen(on_loaded
) + strlen(off_loaded
);
432 a
-= strlen(on_active
) + strlen(off_active
);
435 printf(" %-*s", job_len
, u
->job_type
);
439 if (a
+ b
+ 1 < columns()) {
441 printf(" %-*s", job_len
, "");
443 if (arg_full
|| !arg_no_pager
)
444 printf(" %s", u
->description
);
446 printf(" %.*s", columns() - a
- b
- 1, u
->description
);
453 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
454 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
455 "SUB = The low-level unit activation state, values depend on unit type.\n"
456 "JOB = Pending job for the unit.\n");
459 printf("\n%u units listed.\n", n_shown
);
461 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown
);
465 static int list_units(DBusConnection
*bus
, char **args
, unsigned n
) {
466 DBusMessage
*m
= NULL
, *reply
= NULL
;
469 DBusMessageIter iter
, sub
, sub2
;
470 unsigned c
= 0, n_units
= 0;
471 struct unit_info
*unit_infos
= NULL
;
473 dbus_error_init(&error
);
479 if (!(m
= dbus_message_new_method_call(
480 "org.freedesktop.systemd1",
481 "/org/freedesktop/systemd1",
482 "org.freedesktop.systemd1.Manager",
484 log_error("Could not allocate message.");
488 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
489 log_error("Failed to issue method call: %s", bus_error_message(&error
));
494 if (!dbus_message_iter_init(reply
, &iter
) ||
495 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
496 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
497 log_error("Failed to parse reply.");
502 dbus_message_iter_recurse(&iter
, &sub
);
504 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
507 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
508 log_error("Failed to parse reply.");
516 n_units
= MAX(2*c
, 16);
517 w
= realloc(unit_infos
, sizeof(struct unit_info
) * n_units
);
520 log_error("Failed to allocate unit array.");
530 dbus_message_iter_recurse(&sub
, &sub2
);
532 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->id
, true) < 0 ||
533 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->description
, true) < 0 ||
534 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->load_state
, true) < 0 ||
535 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->active_state
, true) < 0 ||
536 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->sub_state
, true) < 0 ||
537 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->following
, true) < 0 ||
538 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->unit_path
, true) < 0 ||
539 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &u
->job_id
, true) < 0 ||
540 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->job_type
, true) < 0 ||
541 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->job_path
, false) < 0) {
542 log_error("Failed to parse reply.");
547 dbus_message_iter_next(&sub
);
552 qsort(unit_infos
, c
, sizeof(struct unit_info
), compare_unit_info
);
553 output_units_list(unit_infos
, c
);
560 dbus_message_unref(m
);
563 dbus_message_unref(reply
);
567 dbus_error_free(&error
);
572 static int dot_one_property(const char *name
, const char *prop
, DBusMessageIter
*iter
) {
573 static const char * const colors
[] = {
574 "Requires", "[color=\"black\"]",
575 "RequiresOverridable", "[color=\"black\"]",
576 "Requisite", "[color=\"darkblue\"]",
577 "RequisiteOverridable", "[color=\"darkblue\"]",
578 "Wants", "[color=\"darkgrey\"]",
579 "Conflicts", "[color=\"red\"]",
580 "ConflictedBy", "[color=\"red\"]",
581 "After", "[color=\"green\"]"
584 const char *c
= NULL
;
591 for (i
= 0; i
< ELEMENTSOF(colors
); i
+= 2)
592 if (streq(colors
[i
], prop
)) {
600 if (arg_dot
!= DOT_ALL
)
601 if ((arg_dot
== DOT_ORDER
) != streq(prop
, "After"))
604 switch (dbus_message_iter_get_arg_type(iter
)) {
606 case DBUS_TYPE_ARRAY
:
608 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
611 dbus_message_iter_recurse(iter
, &sub
);
613 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
616 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
617 dbus_message_iter_get_basic(&sub
, &s
);
618 printf("\t\"%s\"->\"%s\" %s;\n", name
, s
, c
);
620 dbus_message_iter_next(&sub
);
630 static int dot_one(DBusConnection
*bus
, const char *name
, const char *path
) {
631 DBusMessage
*m
= NULL
, *reply
= NULL
;
632 const char *interface
= "org.freedesktop.systemd1.Unit";
635 DBusMessageIter iter
, sub
, sub2
, sub3
;
640 dbus_error_init(&error
);
642 if (!(m
= dbus_message_new_method_call(
643 "org.freedesktop.systemd1",
645 "org.freedesktop.DBus.Properties",
647 log_error("Could not allocate message.");
652 if (!dbus_message_append_args(m
,
653 DBUS_TYPE_STRING
, &interface
,
654 DBUS_TYPE_INVALID
)) {
655 log_error("Could not append arguments to message.");
660 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
661 log_error("Failed to issue method call: %s", bus_error_message(&error
));
666 if (!dbus_message_iter_init(reply
, &iter
) ||
667 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
668 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
669 log_error("Failed to parse reply.");
674 dbus_message_iter_recurse(&iter
, &sub
);
676 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
679 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
680 log_error("Failed to parse reply.");
685 dbus_message_iter_recurse(&sub
, &sub2
);
687 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &prop
, true) < 0) {
688 log_error("Failed to parse reply.");
693 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
694 log_error("Failed to parse reply.");
699 dbus_message_iter_recurse(&sub2
, &sub3
);
701 if (dot_one_property(name
, prop
, &sub3
)) {
702 log_error("Failed to parse reply.");
707 dbus_message_iter_next(&sub
);
714 dbus_message_unref(m
);
717 dbus_message_unref(reply
);
719 dbus_error_free(&error
);
724 static int dot(DBusConnection
*bus
, char **args
, unsigned n
) {
725 DBusMessage
*m
= NULL
, *reply
= NULL
;
728 DBusMessageIter iter
, sub
, sub2
;
730 dbus_error_init(&error
);
734 if (!(m
= dbus_message_new_method_call(
735 "org.freedesktop.systemd1",
736 "/org/freedesktop/systemd1",
737 "org.freedesktop.systemd1.Manager",
739 log_error("Could not allocate message.");
743 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
744 log_error("Failed to issue method call: %s", bus_error_message(&error
));
749 if (!dbus_message_iter_init(reply
, &iter
) ||
750 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
751 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
752 log_error("Failed to parse reply.");
757 printf("digraph systemd {\n");
759 dbus_message_iter_recurse(&iter
, &sub
);
760 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
761 const char *id
, *description
, *load_state
, *active_state
, *sub_state
, *following
, *unit_path
;
763 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
764 log_error("Failed to parse reply.");
769 dbus_message_iter_recurse(&sub
, &sub2
);
771 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &id
, true) < 0 ||
772 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &description
, true) < 0 ||
773 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &load_state
, true) < 0 ||
774 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &active_state
, true) < 0 ||
775 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &sub_state
, true) < 0 ||
776 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &following
, true) < 0 ||
777 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, true) < 0) {
778 log_error("Failed to parse reply.");
783 if ((r
= dot_one(bus
, id
, unit_path
)) < 0)
786 /* printf("\t\"%s\";\n", id); */
787 dbus_message_iter_next(&sub
);
792 log_info(" Color legend: black = Requires\n"
793 " dark blue = Requisite\n"
794 " dark grey = Wants\n"
798 if (isatty(fileno(stdout
)))
799 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
800 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
806 dbus_message_unref(m
);
809 dbus_message_unref(reply
);
811 dbus_error_free(&error
);
816 static int list_jobs(DBusConnection
*bus
, char **args
, unsigned n
) {
817 DBusMessage
*m
= NULL
, *reply
= NULL
;
820 DBusMessageIter iter
, sub
, sub2
;
823 dbus_error_init(&error
);
829 if (!(m
= dbus_message_new_method_call(
830 "org.freedesktop.systemd1",
831 "/org/freedesktop/systemd1",
832 "org.freedesktop.systemd1.Manager",
834 log_error("Could not allocate message.");
838 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
839 log_error("Failed to issue method call: %s", bus_error_message(&error
));
844 if (!dbus_message_iter_init(reply
, &iter
) ||
845 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
846 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
847 log_error("Failed to parse reply.");
852 dbus_message_iter_recurse(&iter
, &sub
);
854 if (isatty(STDOUT_FILENO
))
855 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
857 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
858 const char *name
, *type
, *state
, *job_path
, *unit_path
;
862 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
863 log_error("Failed to parse reply.");
868 dbus_message_iter_recurse(&sub
, &sub2
);
870 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &id
, true) < 0 ||
871 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0 ||
872 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) < 0 ||
873 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &state
, true) < 0 ||
874 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &job_path
, true) < 0 ||
875 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, false) < 0) {
876 log_error("Failed to parse reply.");
881 e
= arg_full
? NULL
: ellipsize(name
, 25, 33);
882 printf("%4u %-25s %-15s %-7s\n", id
, e
? e
: name
, type
, state
);
887 dbus_message_iter_next(&sub
);
890 if (isatty(STDOUT_FILENO
))
891 printf("\n%u jobs listed.\n", k
);
897 dbus_message_unref(m
);
900 dbus_message_unref(reply
);
902 dbus_error_free(&error
);
907 static int load_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
908 DBusMessage
*m
= NULL
, *reply
= NULL
;
913 dbus_error_init(&error
);
918 for (i
= 1; i
< n
; i
++) {
920 if (!(m
= dbus_message_new_method_call(
921 "org.freedesktop.systemd1",
922 "/org/freedesktop/systemd1",
923 "org.freedesktop.systemd1.Manager",
925 log_error("Could not allocate message.");
930 if (!dbus_message_append_args(m
,
931 DBUS_TYPE_STRING
, &args
[i
],
932 DBUS_TYPE_INVALID
)) {
933 log_error("Could not append arguments to message.");
938 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
939 log_error("Failed to issue method call: %s", bus_error_message(&error
));
944 dbus_message_unref(m
);
945 dbus_message_unref(reply
);
954 dbus_message_unref(m
);
957 dbus_message_unref(reply
);
959 dbus_error_free(&error
);
964 static int cancel_job(DBusConnection
*bus
, char **args
, unsigned n
) {
965 DBusMessage
*m
= NULL
, *reply
= NULL
;
970 dbus_error_init(&error
);
976 return daemon_reload(bus
, args
, n
);
978 for (i
= 1; i
< n
; i
++) {
982 if (!(m
= dbus_message_new_method_call(
983 "org.freedesktop.systemd1",
984 "/org/freedesktop/systemd1",
985 "org.freedesktop.systemd1.Manager",
987 log_error("Could not allocate message.");
992 if ((r
= safe_atou(args
[i
], &id
)) < 0) {
993 log_error("Failed to parse job id: %s", strerror(-r
));
997 assert_cc(sizeof(uint32_t) == sizeof(id
));
998 if (!dbus_message_append_args(m
,
999 DBUS_TYPE_UINT32
, &id
,
1000 DBUS_TYPE_INVALID
)) {
1001 log_error("Could not append arguments to message.");
1006 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1007 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1012 if (!dbus_message_get_args(reply
, &error
,
1013 DBUS_TYPE_OBJECT_PATH
, &path
,
1014 DBUS_TYPE_INVALID
)) {
1015 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1020 dbus_message_unref(m
);
1021 if (!(m
= dbus_message_new_method_call(
1022 "org.freedesktop.systemd1",
1024 "org.freedesktop.systemd1.Job",
1026 log_error("Could not allocate message.");
1031 dbus_message_unref(reply
);
1032 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1033 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1038 dbus_message_unref(m
);
1039 dbus_message_unref(reply
);
1047 dbus_message_unref(m
);
1050 dbus_message_unref(reply
);
1052 dbus_error_free(&error
);
1057 static bool need_daemon_reload(DBusConnection
*bus
, const char *unit
) {
1058 DBusMessage
*m
= NULL
, *reply
= NULL
;
1059 dbus_bool_t b
= FALSE
;
1060 DBusMessageIter iter
, sub
;
1062 *interface
= "org.freedesktop.systemd1.Unit",
1063 *property
= "NeedDaemonReload",
1066 /* We ignore all errors here, since this is used to show a warning only */
1068 if (!(m
= dbus_message_new_method_call(
1069 "org.freedesktop.systemd1",
1070 "/org/freedesktop/systemd1",
1071 "org.freedesktop.systemd1.Manager",
1075 if (!dbus_message_append_args(m
,
1076 DBUS_TYPE_STRING
, &unit
,
1080 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
)))
1083 if (!dbus_message_get_args(reply
, NULL
,
1084 DBUS_TYPE_OBJECT_PATH
, &path
,
1088 dbus_message_unref(m
);
1089 if (!(m
= dbus_message_new_method_call(
1090 "org.freedesktop.systemd1",
1092 "org.freedesktop.DBus.Properties",
1096 if (!dbus_message_append_args(m
,
1097 DBUS_TYPE_STRING
, &interface
,
1098 DBUS_TYPE_STRING
, &property
,
1099 DBUS_TYPE_INVALID
)) {
1103 dbus_message_unref(reply
);
1104 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
)))
1107 if (!dbus_message_iter_init(reply
, &iter
) ||
1108 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
)
1111 dbus_message_iter_recurse(&iter
, &sub
);
1113 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_BOOLEAN
)
1116 dbus_message_iter_get_basic(&sub
, &b
);
1120 dbus_message_unref(m
);
1123 dbus_message_unref(reply
);
1128 typedef struct WaitData
{
1133 static DBusHandlerResult
wait_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
1141 dbus_error_init(&error
);
1143 log_debug("Got D-Bus request: %s.%s() on %s",
1144 dbus_message_get_interface(message
),
1145 dbus_message_get_member(message
),
1146 dbus_message_get_path(message
));
1148 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
1149 log_error("Warning! D-Bus connection terminated.");
1150 dbus_connection_close(connection
);
1152 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1154 const char *path
, *result
;
1155 dbus_bool_t success
= true;
1157 if (dbus_message_get_args(message
, &error
,
1158 DBUS_TYPE_UINT32
, &id
,
1159 DBUS_TYPE_OBJECT_PATH
, &path
,
1160 DBUS_TYPE_STRING
, &result
,
1161 DBUS_TYPE_INVALID
)) {
1164 if ((p
= set_remove(d
->set
, (char*) path
)))
1168 d
->result
= strdup(result
);
1173 dbus_error_free(&error
);
1175 if (dbus_message_get_args(message
, &error
,
1176 DBUS_TYPE_UINT32
, &id
,
1177 DBUS_TYPE_OBJECT_PATH
, &path
,
1178 DBUS_TYPE_BOOLEAN
, &success
,
1179 DBUS_TYPE_INVALID
)) {
1182 /* Compatibility with older systemd versions <
1183 * 19 during upgrades. This should be dropped
1186 if ((p
= set_remove(d
->set
, (char*) path
)))
1190 d
->result
= strdup("failed");
1196 log_error("Failed to parse message: %s", bus_error_message(&error
));
1200 dbus_error_free(&error
);
1201 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1204 static int enable_wait_for_jobs(DBusConnection
*bus
) {
1212 dbus_error_init(&error
);
1213 dbus_bus_add_match(bus
,
1215 "sender='org.freedesktop.systemd1',"
1216 "interface='org.freedesktop.systemd1.Manager',"
1217 "member='JobRemoved',"
1218 "path='/org/freedesktop/systemd1'",
1221 if (dbus_error_is_set(&error
)) {
1222 log_error("Failed to add match: %s", bus_error_message(&error
));
1223 dbus_error_free(&error
);
1227 /* This is slightly dirty, since we don't undo the match registrations. */
1231 static int wait_for_jobs(DBusConnection
*bus
, Set
*s
) {
1241 if (!dbus_connection_add_filter(bus
, wait_filter
, &d
, NULL
)) {
1242 log_error("Failed to add filter.");
1247 while (!set_isempty(s
) &&
1248 dbus_connection_read_write_dispatch(bus
, -1))
1251 if (!arg_quiet
&& d
.result
) {
1252 if (streq(d
.result
, "timeout"))
1253 log_error("Job timed out.");
1254 else if (streq(d
.result
, "canceled"))
1255 log_error("Job canceled.");
1256 else if (streq(d
.result
, "dependency"))
1257 log_error("A dependency job failed. See system logs for details.");
1258 else if (!streq(d
.result
, "done") && !streq(d
.result
, "skipped"))
1259 log_error("Job failed. See system logs and 'systemctl status' for details.");
1262 if (streq_ptr(d
.result
, "timeout"))
1264 else if (streq_ptr(d
.result
, "canceled"))
1266 else if (!streq_ptr(d
.result
, "done") && !streq_ptr(d
.result
, "skipped"))
1274 /* This is slightly dirty, since we don't undo the filter registration. */
1279 static int start_unit_one(
1280 DBusConnection
*bus
,
1287 DBusMessage
*m
= NULL
, *reply
= NULL
;
1296 assert(arg_no_block
|| s
);
1298 if (!(m
= dbus_message_new_method_call(
1299 "org.freedesktop.systemd1",
1300 "/org/freedesktop/systemd1",
1301 "org.freedesktop.systemd1.Manager",
1303 log_error("Could not allocate message.");
1308 if (!dbus_message_append_args(m
,
1309 DBUS_TYPE_STRING
, &name
,
1310 DBUS_TYPE_STRING
, &mode
,
1311 DBUS_TYPE_INVALID
)) {
1312 log_error("Could not append arguments to message.");
1317 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, error
))) {
1319 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(error
)) {
1320 /* There's always a fallback possible for
1321 * legacy actions. */
1326 log_error("Failed to issue method call: %s", bus_error_message(error
));
1331 if (!dbus_message_get_args(reply
, error
,
1332 DBUS_TYPE_OBJECT_PATH
, &path
,
1333 DBUS_TYPE_INVALID
)) {
1334 log_error("Failed to parse reply: %s", bus_error_message(error
));
1339 if (need_daemon_reload(bus
, name
))
1340 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1341 arg_user
? "--user" : "--system");
1343 if (!arg_no_block
) {
1346 if (!(p
= strdup(path
))) {
1347 log_error("Failed to duplicate path.");
1352 if ((r
= set_put(s
, p
)) < 0) {
1354 log_error("Failed to add path to set.");
1363 dbus_message_unref(m
);
1366 dbus_message_unref(reply
);
1371 static enum action
verb_to_action(const char *verb
) {
1372 if (streq(verb
, "halt"))
1374 else if (streq(verb
, "poweroff"))
1375 return ACTION_POWEROFF
;
1376 else if (streq(verb
, "reboot"))
1377 return ACTION_REBOOT
;
1378 else if (streq(verb
, "kexec"))
1379 return ACTION_KEXEC
;
1380 else if (streq(verb
, "rescue"))
1381 return ACTION_RESCUE
;
1382 else if (streq(verb
, "emergency"))
1383 return ACTION_EMERGENCY
;
1384 else if (streq(verb
, "default"))
1385 return ACTION_DEFAULT
;
1386 else if (streq(verb
, "exit"))
1389 return ACTION_INVALID
;
1392 static int start_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1394 static const char * const table
[_ACTION_MAX
] = {
1395 [ACTION_HALT
] = SPECIAL_HALT_TARGET
,
1396 [ACTION_POWEROFF
] = SPECIAL_POWEROFF_TARGET
,
1397 [ACTION_REBOOT
] = SPECIAL_REBOOT_TARGET
,
1398 [ACTION_KEXEC
] = SPECIAL_KEXEC_TARGET
,
1399 [ACTION_RUNLEVEL2
] = SPECIAL_RUNLEVEL2_TARGET
,
1400 [ACTION_RUNLEVEL3
] = SPECIAL_RUNLEVEL3_TARGET
,
1401 [ACTION_RUNLEVEL4
] = SPECIAL_RUNLEVEL4_TARGET
,
1402 [ACTION_RUNLEVEL5
] = SPECIAL_RUNLEVEL5_TARGET
,
1403 [ACTION_RESCUE
] = SPECIAL_RESCUE_TARGET
,
1404 [ACTION_EMERGENCY
] = SPECIAL_EMERGENCY_TARGET
,
1405 [ACTION_DEFAULT
] = SPECIAL_DEFAULT_TARGET
,
1406 [ACTION_EXIT
] = SPECIAL_EXIT_TARGET
1411 const char *method
, *mode
, *one_name
;
1415 dbus_error_init(&error
);
1419 spawn_ask_password_agent();
1421 if (arg_action
== ACTION_SYSTEMCTL
) {
1423 streq(args
[0], "stop") ||
1424 streq(args
[0], "condstop") ? "StopUnit" :
1425 streq(args
[0], "reload") ? "ReloadUnit" :
1426 streq(args
[0], "restart") ? "RestartUnit" :
1428 streq(args
[0], "try-restart") ||
1429 streq(args
[0], "condrestart") ? "TryRestartUnit" :
1431 streq(args
[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1433 streq(args
[0], "reload-or-try-restart") ||
1434 streq(args
[0], "condreload") ||
1436 streq(args
[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1440 (streq(args
[0], "isolate") ||
1441 streq(args
[0], "rescue") ||
1442 streq(args
[0], "emergency")) ? "isolate" : arg_job_mode
;
1444 one_name
= table
[verb_to_action(args
[0])];
1447 assert(arg_action
< ELEMENTSOF(table
));
1448 assert(table
[arg_action
]);
1450 method
= "StartUnit";
1452 mode
= (arg_action
== ACTION_EMERGENCY
||
1453 arg_action
== ACTION_RESCUE
||
1454 arg_action
== ACTION_RUNLEVEL2
||
1455 arg_action
== ACTION_RUNLEVEL3
||
1456 arg_action
== ACTION_RUNLEVEL4
||
1457 arg_action
== ACTION_RUNLEVEL5
) ? "isolate" : "replace";
1459 one_name
= table
[arg_action
];
1462 if (!arg_no_block
) {
1463 if ((ret
= enable_wait_for_jobs(bus
)) < 0) {
1464 log_error("Could not watch jobs: %s", strerror(-ret
));
1468 if (!(s
= set_new(string_hash_func
, string_compare_func
))) {
1469 log_error("Failed to allocate set.");
1476 if ((ret
= start_unit_one(bus
, method
, one_name
, mode
, &error
, s
)) <= 0)
1479 for (i
= 1; i
< n
; i
++)
1480 if ((r
= start_unit_one(bus
, method
, args
[i
], mode
, &error
, s
)) != 0) {
1481 ret
= translate_bus_error_to_exit_status(r
, &error
);
1482 dbus_error_free(&error
);
1487 if ((r
= wait_for_jobs(bus
, s
)) < 0) {
1496 dbus_error_free(&error
);
1501 static int start_special(DBusConnection
*bus
, char **args
, unsigned n
) {
1508 (streq(args
[0], "halt") ||
1509 streq(args
[0], "poweroff") ||
1510 streq(args
[0], "reboot") ||
1511 streq(args
[0], "kexec") ||
1512 streq(args
[0], "exit")))
1513 return daemon_reload(bus
, args
, n
);
1515 r
= start_unit(bus
, args
, n
);
1518 warn_wall(verb_to_action(args
[0]));
1523 static int check_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1524 DBusMessage
*m
= NULL
, *reply
= NULL
;
1526 *interface
= "org.freedesktop.systemd1.Unit",
1527 *property
= "ActiveState";
1528 int r
= 3; /* According to LSB: "program is not running" */
1535 dbus_error_init(&error
);
1537 for (i
= 1; i
< n
; i
++) {
1538 const char *path
= NULL
;
1540 DBusMessageIter iter
, sub
;
1542 if (!(m
= dbus_message_new_method_call(
1543 "org.freedesktop.systemd1",
1544 "/org/freedesktop/systemd1",
1545 "org.freedesktop.systemd1.Manager",
1547 log_error("Could not allocate message.");
1552 if (!dbus_message_append_args(m
,
1553 DBUS_TYPE_STRING
, &args
[i
],
1554 DBUS_TYPE_INVALID
)) {
1555 log_error("Could not append arguments to message.");
1560 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1562 /* Hmm, cannot figure out anything about this unit... */
1566 dbus_error_free(&error
);
1567 dbus_message_unref(m
);
1572 if (!dbus_message_get_args(reply
, &error
,
1573 DBUS_TYPE_OBJECT_PATH
, &path
,
1574 DBUS_TYPE_INVALID
)) {
1575 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1580 dbus_message_unref(m
);
1581 if (!(m
= dbus_message_new_method_call(
1582 "org.freedesktop.systemd1",
1584 "org.freedesktop.DBus.Properties",
1586 log_error("Could not allocate message.");
1591 if (!dbus_message_append_args(m
,
1592 DBUS_TYPE_STRING
, &interface
,
1593 DBUS_TYPE_STRING
, &property
,
1594 DBUS_TYPE_INVALID
)) {
1595 log_error("Could not append arguments to message.");
1600 dbus_message_unref(reply
);
1601 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1602 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1607 if (!dbus_message_iter_init(reply
, &iter
) ||
1608 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
1609 log_error("Failed to parse reply.");
1614 dbus_message_iter_recurse(&iter
, &sub
);
1616 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
1617 log_error("Failed to parse reply.");
1622 dbus_message_iter_get_basic(&sub
, &state
);
1627 if (streq(state
, "active") || streq(state
, "reloading"))
1630 dbus_message_unref(m
);
1631 dbus_message_unref(reply
);
1637 dbus_message_unref(m
);
1640 dbus_message_unref(reply
);
1642 dbus_error_free(&error
);
1647 static int kill_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1648 DBusMessage
*m
= NULL
, *reply
= NULL
;
1656 dbus_error_init(&error
);
1659 arg_kill_who
= "all";
1662 arg_kill_mode
= streq(arg_kill_who
, "all") ? "control-group" : "process";
1664 for (i
= 1; i
< n
; i
++) {
1666 if (!(m
= dbus_message_new_method_call(
1667 "org.freedesktop.systemd1",
1668 "/org/freedesktop/systemd1",
1669 "org.freedesktop.systemd1.Manager",
1671 log_error("Could not allocate message.");
1676 if (!dbus_message_append_args(m
,
1677 DBUS_TYPE_STRING
, &args
[i
],
1678 DBUS_TYPE_STRING
, &arg_kill_who
,
1679 DBUS_TYPE_STRING
, &arg_kill_mode
,
1680 DBUS_TYPE_INT32
, &arg_signal
,
1681 DBUS_TYPE_INVALID
)) {
1682 log_error("Could not append arguments to message.");
1687 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1688 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1689 dbus_error_free(&error
);
1693 dbus_message_unref(m
);
1696 dbus_message_unref(reply
);
1702 dbus_message_unref(m
);
1705 dbus_message_unref(reply
);
1707 dbus_error_free(&error
);
1712 typedef struct ExecStatusInfo
{
1720 usec_t start_timestamp
;
1721 usec_t exit_timestamp
;
1726 LIST_FIELDS(struct ExecStatusInfo
, exec
);
1729 static void exec_status_info_free(ExecStatusInfo
*i
) {
1738 static int exec_status_info_deserialize(DBusMessageIter
*sub
, ExecStatusInfo
*i
) {
1739 uint64_t start_timestamp
, exit_timestamp
, start_timestamp_monotonic
, exit_timestamp_monotonic
;
1740 DBusMessageIter sub2
, sub3
;
1744 int32_t code
, status
;
1750 if (dbus_message_iter_get_arg_type(sub
) != DBUS_TYPE_STRUCT
)
1753 dbus_message_iter_recurse(sub
, &sub2
);
1755 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) < 0)
1758 if (!(i
->path
= strdup(path
)))
1761 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_ARRAY
||
1762 dbus_message_iter_get_element_type(&sub2
) != DBUS_TYPE_STRING
)
1766 dbus_message_iter_recurse(&sub2
, &sub3
);
1767 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
1768 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
1769 dbus_message_iter_next(&sub3
);
1774 if (!(i
->argv
= new0(char*, n
+1)))
1778 dbus_message_iter_recurse(&sub2
, &sub3
);
1779 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
1782 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
1783 dbus_message_iter_get_basic(&sub3
, &s
);
1784 dbus_message_iter_next(&sub3
);
1786 if (!(i
->argv
[n
++] = strdup(s
)))
1790 if (!dbus_message_iter_next(&sub2
) ||
1791 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, true) < 0 ||
1792 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &start_timestamp
, true) < 0 ||
1793 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &start_timestamp_monotonic
, true) < 0 ||
1794 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &exit_timestamp
, true) < 0 ||
1795 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &exit_timestamp_monotonic
, true) < 0 ||
1796 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &pid
, true) < 0 ||
1797 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &code
, true) < 0 ||
1798 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &status
, false) < 0)
1802 i
->start_timestamp
= (usec_t
) start_timestamp
;
1803 i
->exit_timestamp
= (usec_t
) exit_timestamp
;
1804 i
->pid
= (pid_t
) pid
;
1811 typedef struct UnitStatusInfo
{
1813 const char *load_state
;
1814 const char *active_state
;
1815 const char *sub_state
;
1817 const char *description
;
1818 const char *following
;
1821 const char *default_control_group
;
1823 usec_t inactive_exit_timestamp
;
1824 usec_t active_enter_timestamp
;
1825 usec_t active_exit_timestamp
;
1826 usec_t inactive_enter_timestamp
;
1828 bool need_daemon_reload
;
1833 const char *status_text
;
1835 #ifdef HAVE_SYSV_COMPAT
1839 usec_t start_timestamp
;
1840 usec_t exit_timestamp
;
1842 int exit_code
, exit_status
;
1844 usec_t condition_timestamp
;
1845 bool condition_result
;
1848 unsigned n_accepted
;
1849 unsigned n_connections
;
1853 const char *sysfs_path
;
1855 /* Mount, Automount */
1861 LIST_HEAD(ExecStatusInfo
, exec
);
1864 static void print_status_info(UnitStatusInfo
*i
) {
1866 const char *on
, *off
, *ss
;
1868 char since1
[FORMAT_TIMESTAMP_PRETTY_MAX
], *s1
;
1869 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
1873 /* This shows pretty information about a unit. See
1874 * print_property() for a low-level property printer */
1876 printf("%s", strna(i
->id
));
1878 if (i
->description
&& !streq_ptr(i
->id
, i
->description
))
1879 printf(" - %s", i
->description
);
1884 printf("\t Follow: unit currently follows state of %s\n", i
->following
);
1886 if (streq_ptr(i
->load_state
, "failed") ||
1887 streq_ptr(i
->load_state
, "banned")) {
1888 on
= ansi_highlight(true);
1889 off
= ansi_highlight(false);
1894 printf("\t Loaded: %s%s%s (%s)\n", on
, strna(i
->load_state
), off
, i
->path
);
1896 printf("\t Loaded: %s%s%s\n", on
, strna(i
->load_state
), off
);
1898 ss
= streq_ptr(i
->active_state
, i
->sub_state
) ? NULL
: i
->sub_state
;
1900 if (streq_ptr(i
->active_state
, "failed")) {
1901 on
= ansi_highlight(true);
1902 off
= ansi_highlight(false);
1903 } else if (streq_ptr(i
->active_state
, "active") || streq_ptr(i
->active_state
, "reloading")) {
1904 on
= ansi_highlight_green(true);
1905 off
= ansi_highlight_green(false);
1910 printf("\t Active: %s%s (%s)%s",
1912 strna(i
->active_state
),
1916 printf("\t Active: %s%s%s",
1918 strna(i
->active_state
),
1921 timestamp
= (streq_ptr(i
->active_state
, "active") ||
1922 streq_ptr(i
->active_state
, "reloading")) ? i
->active_enter_timestamp
:
1923 (streq_ptr(i
->active_state
, "inactive") ||
1924 streq_ptr(i
->active_state
, "failed")) ? i
->inactive_enter_timestamp
:
1925 streq_ptr(i
->active_state
, "activating") ? i
->inactive_exit_timestamp
:
1926 i
->active_exit_timestamp
;
1928 s1
= format_timestamp_pretty(since1
, sizeof(since1
), timestamp
);
1929 s2
= format_timestamp(since2
, sizeof(since2
), timestamp
);
1932 printf(" since %s; %s\n", s2
, s1
);
1934 printf(" since %s\n", s2
);
1938 if (!i
->condition_result
&& i
->condition_timestamp
> 0) {
1939 s1
= format_timestamp_pretty(since1
, sizeof(since1
), i
->condition_timestamp
);
1940 s2
= format_timestamp(since2
, sizeof(since2
), i
->condition_timestamp
);
1943 printf("\t start condition failed at %s; %s\n", s2
, s1
);
1945 printf("\t start condition failed at %s\n", s2
);
1949 printf("\t Device: %s\n", i
->sysfs_path
);
1951 printf("\t Where: %s\n", i
->where
);
1953 printf("\t What: %s\n", i
->what
);
1956 printf("\tAccepted: %u; Connected: %u\n", i
->n_accepted
, i
->n_connections
);
1958 LIST_FOREACH(exec
, p
, i
->exec
) {
1962 /* Only show exited processes here */
1966 t
= strv_join(p
->argv
, " ");
1967 printf("\t Process: %u %s=%s ", p
->pid
, p
->name
, strna(t
));
1970 #ifdef HAVE_SYSV_COMPAT
1972 good
= is_clean_exit_lsb(p
->code
, p
->status
);
1975 good
= is_clean_exit(p
->code
, p
->status
);
1978 on
= ansi_highlight(true);
1979 off
= ansi_highlight(false);
1983 printf("%s(code=%s, ", on
, sigchld_code_to_string(p
->code
));
1985 if (p
->code
== CLD_EXITED
) {
1988 printf("status=%i", p
->status
);
1990 #ifdef HAVE_SYSV_COMPAT
1991 if ((c
= exit_status_to_string(p
->status
, i
->is_sysv
? EXIT_STATUS_LSB
: EXIT_STATUS_SYSTEMD
)))
1993 if ((c
= exit_status_to_string(p
->status
, EXIT_STATUS_SYSTEMD
)))
1998 printf("signal=%s", signal_to_string(p
->status
));
2000 printf(")%s\n", off
);
2004 if (i
->main_pid
== p
->pid
&&
2005 i
->start_timestamp
== p
->start_timestamp
&&
2006 i
->exit_timestamp
== p
->start_timestamp
)
2007 /* Let's not show this twice */
2010 if (p
->pid
== i
->control_pid
)
2014 if (i
->main_pid
> 0 || i
->control_pid
> 0) {
2017 if (i
->main_pid
> 0) {
2018 printf("Main PID: %u", (unsigned) i
->main_pid
);
2022 get_process_name(i
->main_pid
, &t
);
2027 } else if (i
->exit_code
> 0) {
2028 printf(" (code=%s, ", sigchld_code_to_string(i
->exit_code
));
2030 if (i
->exit_code
== CLD_EXITED
) {
2033 printf("status=%i", i
->exit_status
);
2035 #ifdef HAVE_SYSV_COMPAT
2036 if ((c
= exit_status_to_string(i
->exit_status
, i
->is_sysv
? EXIT_STATUS_LSB
: EXIT_STATUS_SYSTEMD
)))
2038 if ((c
= exit_status_to_string(i
->exit_status
, EXIT_STATUS_SYSTEMD
)))
2043 printf("signal=%s", signal_to_string(i
->exit_status
));
2048 if (i
->main_pid
> 0 && i
->control_pid
> 0)
2051 if (i
->control_pid
> 0) {
2054 printf(" Control: %u", (unsigned) i
->control_pid
);
2056 get_process_name(i
->control_pid
, &t
);
2067 printf("\t Status: \"%s\"\n", i
->status_text
);
2069 if (i
->default_control_group
) {
2072 printf("\t CGroup: %s\n", i
->default_control_group
);
2074 if (arg_transport
!= TRANSPORT_SSH
) {
2075 if ((c
= columns()) > 18)
2080 show_cgroup_by_path(i
->default_control_group
, "\t\t ", c
);
2084 if (i
->need_daemon_reload
)
2085 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2086 ansi_highlight(true),
2087 ansi_highlight(false),
2088 arg_user
? "--user" : "--system");
2091 static int status_property(const char *name
, DBusMessageIter
*iter
, UnitStatusInfo
*i
) {
2093 switch (dbus_message_iter_get_arg_type(iter
)) {
2095 case DBUS_TYPE_STRING
: {
2098 dbus_message_iter_get_basic(iter
, &s
);
2101 if (streq(name
, "Id"))
2103 else if (streq(name
, "LoadState"))
2105 else if (streq(name
, "ActiveState"))
2106 i
->active_state
= s
;
2107 else if (streq(name
, "SubState"))
2109 else if (streq(name
, "Description"))
2111 else if (streq(name
, "FragmentPath"))
2113 #ifdef HAVE_SYSV_COMPAT
2114 else if (streq(name
, "SysVPath")) {
2119 else if (streq(name
, "DefaultControlGroup"))
2120 i
->default_control_group
= s
;
2121 else if (streq(name
, "StatusText"))
2123 else if (streq(name
, "SysFSPath"))
2125 else if (streq(name
, "Where"))
2127 else if (streq(name
, "What"))
2129 else if (streq(name
, "Following"))
2136 case DBUS_TYPE_BOOLEAN
: {
2139 dbus_message_iter_get_basic(iter
, &b
);
2141 if (streq(name
, "Accept"))
2143 else if (streq(name
, "NeedDaemonReload"))
2144 i
->need_daemon_reload
= b
;
2145 else if (streq(name
, "ConditionResult"))
2146 i
->condition_result
= b
;
2151 case DBUS_TYPE_UINT32
: {
2154 dbus_message_iter_get_basic(iter
, &u
);
2156 if (streq(name
, "MainPID")) {
2158 i
->main_pid
= (pid_t
) u
;
2161 } else if (streq(name
, "ControlPID"))
2162 i
->control_pid
= (pid_t
) u
;
2163 else if (streq(name
, "ExecMainPID")) {
2165 i
->main_pid
= (pid_t
) u
;
2166 } else if (streq(name
, "NAccepted"))
2168 else if (streq(name
, "NConnections"))
2169 i
->n_connections
= u
;
2174 case DBUS_TYPE_INT32
: {
2177 dbus_message_iter_get_basic(iter
, &j
);
2179 if (streq(name
, "ExecMainCode"))
2180 i
->exit_code
= (int) j
;
2181 else if (streq(name
, "ExecMainStatus"))
2182 i
->exit_status
= (int) j
;
2187 case DBUS_TYPE_UINT64
: {
2190 dbus_message_iter_get_basic(iter
, &u
);
2192 if (streq(name
, "ExecMainStartTimestamp"))
2193 i
->start_timestamp
= (usec_t
) u
;
2194 else if (streq(name
, "ExecMainExitTimestamp"))
2195 i
->exit_timestamp
= (usec_t
) u
;
2196 else if (streq(name
, "ActiveEnterTimestamp"))
2197 i
->active_enter_timestamp
= (usec_t
) u
;
2198 else if (streq(name
, "InactiveEnterTimestamp"))
2199 i
->inactive_enter_timestamp
= (usec_t
) u
;
2200 else if (streq(name
, "InactiveExitTimestamp"))
2201 i
->inactive_exit_timestamp
= (usec_t
) u
;
2202 else if (streq(name
, "ActiveExitTimestamp"))
2203 i
->active_exit_timestamp
= (usec_t
) u
;
2204 else if (streq(name
, "ConditionTimestamp"))
2205 i
->condition_timestamp
= (usec_t
) u
;
2210 case DBUS_TYPE_ARRAY
: {
2212 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&&
2213 startswith(name
, "Exec")) {
2214 DBusMessageIter sub
;
2216 dbus_message_iter_recurse(iter
, &sub
);
2217 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2218 ExecStatusInfo
*info
;
2221 if (!(info
= new0(ExecStatusInfo
, 1)))
2224 if (!(info
->name
= strdup(name
))) {
2229 if ((r
= exec_status_info_deserialize(&sub
, info
)) < 0) {
2234 LIST_PREPEND(ExecStatusInfo
, exec
, i
->exec
, info
);
2236 dbus_message_iter_next(&sub
);
2247 static int print_property(const char *name
, DBusMessageIter
*iter
) {
2251 /* This is a low-level property printer, see
2252 * print_status_info() for the nicer output */
2254 if (arg_property
&& !strv_find(arg_property
, name
))
2257 switch (dbus_message_iter_get_arg_type(iter
)) {
2259 case DBUS_TYPE_STRING
: {
2261 dbus_message_iter_get_basic(iter
, &s
);
2263 if (arg_all
|| s
[0])
2264 printf("%s=%s\n", name
, s
);
2269 case DBUS_TYPE_BOOLEAN
: {
2271 dbus_message_iter_get_basic(iter
, &b
);
2272 printf("%s=%s\n", name
, yes_no(b
));
2277 case DBUS_TYPE_UINT64
: {
2279 dbus_message_iter_get_basic(iter
, &u
);
2281 /* Yes, heuristics! But we can change this check
2282 * should it turn out to not be sufficient */
2284 if (endswith(name
, "Timestamp")) {
2285 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
2287 if ((t
= format_timestamp(timestamp
, sizeof(timestamp
), u
)) || arg_all
)
2288 printf("%s=%s\n", name
, strempty(t
));
2289 } else if (strstr(name
, "USec")) {
2290 char timespan
[FORMAT_TIMESPAN_MAX
];
2292 printf("%s=%s\n", name
, format_timespan(timespan
, sizeof(timespan
), u
));
2294 printf("%s=%llu\n", name
, (unsigned long long) u
);
2299 case DBUS_TYPE_UINT32
: {
2301 dbus_message_iter_get_basic(iter
, &u
);
2303 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
2304 printf("%s=%04o\n", name
, u
);
2306 printf("%s=%u\n", name
, (unsigned) u
);
2311 case DBUS_TYPE_INT32
: {
2313 dbus_message_iter_get_basic(iter
, &i
);
2315 printf("%s=%i\n", name
, (int) i
);
2319 case DBUS_TYPE_DOUBLE
: {
2321 dbus_message_iter_get_basic(iter
, &d
);
2323 printf("%s=%g\n", name
, d
);
2327 case DBUS_TYPE_STRUCT
: {
2328 DBusMessageIter sub
;
2329 dbus_message_iter_recurse(iter
, &sub
);
2331 if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_UINT32
&& streq(name
, "Job")) {
2334 dbus_message_iter_get_basic(&sub
, &u
);
2337 printf("%s=%u\n", name
, (unsigned) u
);
2339 printf("%s=\n", name
);
2342 } else if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
&& streq(name
, "Unit")) {
2345 dbus_message_iter_get_basic(&sub
, &s
);
2347 if (arg_all
|| s
[0])
2348 printf("%s=%s\n", name
, s
);
2356 case DBUS_TYPE_ARRAY
:
2358 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
2359 DBusMessageIter sub
;
2362 dbus_message_iter_recurse(iter
, &sub
);
2364 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2365 printf("%s=", name
);
2367 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2370 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
2371 dbus_message_iter_get_basic(&sub
, &s
);
2372 printf("%s%s", space
? " " : "", s
);
2375 dbus_message_iter_next(&sub
);
2383 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_BYTE
) {
2384 DBusMessageIter sub
;
2386 dbus_message_iter_recurse(iter
, &sub
);
2388 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2389 printf("%s=", name
);
2391 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2394 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_BYTE
);
2395 dbus_message_iter_get_basic(&sub
, &u
);
2398 dbus_message_iter_next(&sub
);
2406 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "EnvironmentFiles")) {
2407 DBusMessageIter sub
, sub2
;
2409 dbus_message_iter_recurse(iter
, &sub
);
2410 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2414 dbus_message_iter_recurse(&sub
, &sub2
);
2416 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) >= 0 &&
2417 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, false) >= 0)
2418 printf("EnvironmentFile=%s (ignore=%s)\n", path
, yes_no(ignore
));
2420 dbus_message_iter_next(&sub
);
2425 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Paths")) {
2426 DBusMessageIter sub
, sub2
;
2428 dbus_message_iter_recurse(iter
, &sub
);
2429 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2430 const char *type
, *path
;
2432 dbus_message_iter_recurse(&sub
, &sub2
);
2434 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) >= 0 &&
2435 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, false) >= 0)
2436 printf("%s=%s\n", type
, path
);
2438 dbus_message_iter_next(&sub
);
2443 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Timers")) {
2444 DBusMessageIter sub
, sub2
;
2446 dbus_message_iter_recurse(iter
, &sub
);
2447 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2449 uint64_t value
, next_elapse
;
2451 dbus_message_iter_recurse(&sub
, &sub2
);
2453 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &base
, true) >= 0 &&
2454 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &value
, true) >= 0 &&
2455 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &next_elapse
, false) >= 0) {
2456 char timespan1
[FORMAT_TIMESPAN_MAX
], timespan2
[FORMAT_TIMESPAN_MAX
];
2458 printf("%s={ value=%s ; next_elapse=%s }\n",
2460 format_timespan(timespan1
, sizeof(timespan1
), value
),
2461 format_timespan(timespan2
, sizeof(timespan2
), next_elapse
));
2464 dbus_message_iter_next(&sub
);
2469 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& startswith(name
, "Exec")) {
2470 DBusMessageIter sub
;
2472 dbus_message_iter_recurse(iter
, &sub
);
2473 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2474 ExecStatusInfo info
;
2477 if (exec_status_info_deserialize(&sub
, &info
) >= 0) {
2478 char timestamp1
[FORMAT_TIMESTAMP_MAX
], timestamp2
[FORMAT_TIMESTAMP_MAX
];
2481 t
= strv_join(info
.argv
, " ");
2483 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2487 yes_no(info
.ignore
),
2488 strna(format_timestamp(timestamp1
, sizeof(timestamp1
), info
.start_timestamp
)),
2489 strna(format_timestamp(timestamp2
, sizeof(timestamp2
), info
.exit_timestamp
)),
2490 (unsigned) info
. pid
,
2491 sigchld_code_to_string(info
.code
),
2493 info
.code
== CLD_EXITED
? "" : "/",
2494 strempty(info
.code
== CLD_EXITED
? NULL
: signal_to_string(info
.status
)));
2500 strv_free(info
.argv
);
2502 dbus_message_iter_next(&sub
);
2512 printf("%s=[unprintable]\n", name
);
2517 static int show_one(const char *verb
, DBusConnection
*bus
, const char *path
, bool show_properties
, bool *new_line
) {
2518 DBusMessage
*m
= NULL
, *reply
= NULL
;
2519 const char *interface
= "";
2522 DBusMessageIter iter
, sub
, sub2
, sub3
;
2523 UnitStatusInfo info
;
2531 dbus_error_init(&error
);
2533 if (!(m
= dbus_message_new_method_call(
2534 "org.freedesktop.systemd1",
2536 "org.freedesktop.DBus.Properties",
2538 log_error("Could not allocate message.");
2543 if (!dbus_message_append_args(m
,
2544 DBUS_TYPE_STRING
, &interface
,
2545 DBUS_TYPE_INVALID
)) {
2546 log_error("Could not append arguments to message.");
2551 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2552 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2557 if (!dbus_message_iter_init(reply
, &iter
) ||
2558 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
2559 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
2560 log_error("Failed to parse reply.");
2565 dbus_message_iter_recurse(&iter
, &sub
);
2572 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2575 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
2576 log_error("Failed to parse reply.");
2581 dbus_message_iter_recurse(&sub
, &sub2
);
2583 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0) {
2584 log_error("Failed to parse reply.");
2589 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
2590 log_error("Failed to parse reply.");
2595 dbus_message_iter_recurse(&sub2
, &sub3
);
2597 if (show_properties
)
2598 r
= print_property(name
, &sub3
);
2600 r
= status_property(name
, &sub3
, &info
);
2603 log_error("Failed to parse reply.");
2608 dbus_message_iter_next(&sub
);
2613 if (!show_properties
)
2614 print_status_info(&info
);
2616 if (!streq_ptr(info
.active_state
, "active") &&
2617 !streq_ptr(info
.active_state
, "reloading") &&
2618 streq(verb
, "status"))
2619 /* According to LSB: "program not running" */
2622 while ((p
= info
.exec
)) {
2623 LIST_REMOVE(ExecStatusInfo
, exec
, info
.exec
, p
);
2624 exec_status_info_free(p
);
2629 dbus_message_unref(m
);
2632 dbus_message_unref(reply
);
2634 dbus_error_free(&error
);
2639 static int show(DBusConnection
*bus
, char **args
, unsigned n
) {
2640 DBusMessage
*m
= NULL
, *reply
= NULL
;
2644 bool show_properties
, new_line
= false;
2649 dbus_error_init(&error
);
2651 show_properties
= !streq(args
[0], "status");
2653 if (show_properties
)
2656 if (show_properties
&& n
<= 1) {
2657 /* If not argument is specified inspect the manager
2660 ret
= show_one(args
[0], bus
, "/org/freedesktop/systemd1", show_properties
, &new_line
);
2664 for (i
= 1; i
< n
; i
++) {
2665 const char *path
= NULL
;
2668 if (safe_atou32(args
[i
], &id
) < 0) {
2670 /* Interpret as unit name */
2672 if (!(m
= dbus_message_new_method_call(
2673 "org.freedesktop.systemd1",
2674 "/org/freedesktop/systemd1",
2675 "org.freedesktop.systemd1.Manager",
2677 log_error("Could not allocate message.");
2682 if (!dbus_message_append_args(m
,
2683 DBUS_TYPE_STRING
, &args
[i
],
2684 DBUS_TYPE_INVALID
)) {
2685 log_error("Could not append arguments to message.");
2690 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2692 if (!dbus_error_has_name(&error
, DBUS_ERROR_ACCESS_DENIED
)) {
2693 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2698 dbus_error_free(&error
);
2700 dbus_message_unref(m
);
2701 if (!(m
= dbus_message_new_method_call(
2702 "org.freedesktop.systemd1",
2703 "/org/freedesktop/systemd1",
2704 "org.freedesktop.systemd1.Manager",
2706 log_error("Could not allocate message.");
2711 if (!dbus_message_append_args(m
,
2712 DBUS_TYPE_STRING
, &args
[i
],
2713 DBUS_TYPE_INVALID
)) {
2714 log_error("Could not append arguments to message.");
2719 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2720 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2722 if (dbus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
))
2723 ret
= 4; /* According to LSB: "program or service status is unknown" */
2730 } else if (show_properties
) {
2732 /* Interpret as job id */
2734 if (!(m
= dbus_message_new_method_call(
2735 "org.freedesktop.systemd1",
2736 "/org/freedesktop/systemd1",
2737 "org.freedesktop.systemd1.Manager",
2739 log_error("Could not allocate message.");
2744 if (!dbus_message_append_args(m
,
2745 DBUS_TYPE_UINT32
, &id
,
2746 DBUS_TYPE_INVALID
)) {
2747 log_error("Could not append arguments to message.");
2752 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2753 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2759 /* Interpret as PID */
2761 if (!(m
= dbus_message_new_method_call(
2762 "org.freedesktop.systemd1",
2763 "/org/freedesktop/systemd1",
2764 "org.freedesktop.systemd1.Manager",
2766 log_error("Could not allocate message.");
2771 if (!dbus_message_append_args(m
,
2772 DBUS_TYPE_UINT32
, &id
,
2773 DBUS_TYPE_INVALID
)) {
2774 log_error("Could not append arguments to message.");
2779 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2780 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2786 if (!dbus_message_get_args(reply
, &error
,
2787 DBUS_TYPE_OBJECT_PATH
, &path
,
2788 DBUS_TYPE_INVALID
)) {
2789 log_error("Failed to parse reply: %s", bus_error_message(&error
));
2794 if ((r
= show_one(args
[0], bus
, path
, show_properties
, &new_line
)) != 0)
2797 dbus_message_unref(m
);
2798 dbus_message_unref(reply
);
2804 dbus_message_unref(m
);
2807 dbus_message_unref(reply
);
2809 dbus_error_free(&error
);
2814 static DBusHandlerResult
monitor_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
2816 DBusMessage
*m
= NULL
, *reply
= NULL
;
2821 dbus_error_init(&error
);
2823 log_debug("Got D-Bus request: %s.%s() on %s",
2824 dbus_message_get_interface(message
),
2825 dbus_message_get_member(message
),
2826 dbus_message_get_path(message
));
2828 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
2829 log_error("Warning! D-Bus connection terminated.");
2830 dbus_connection_close(connection
);
2832 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2833 dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2834 const char *id
, *path
;
2836 if (!dbus_message_get_args(message
, &error
,
2837 DBUS_TYPE_STRING
, &id
,
2838 DBUS_TYPE_OBJECT_PATH
, &path
,
2840 log_error("Failed to parse message: %s", bus_error_message(&error
));
2841 else if (streq(dbus_message_get_member(message
), "UnitNew"))
2842 printf("Unit %s added.\n", id
);
2844 printf("Unit %s removed.\n", id
);
2846 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobNew")) {
2850 if (!dbus_message_get_args(message
, &error
,
2851 DBUS_TYPE_UINT32
, &id
,
2852 DBUS_TYPE_OBJECT_PATH
, &path
,
2854 log_error("Failed to parse message: %s", bus_error_message(&error
));
2856 printf("Job %u added.\n", id
);
2859 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2861 const char *path
, *result
;
2863 if (!dbus_message_get_args(message
, &error
,
2864 DBUS_TYPE_UINT32
, &id
,
2865 DBUS_TYPE_OBJECT_PATH
, &path
,
2866 DBUS_TYPE_STRING
, &result
,
2868 log_error("Failed to parse message: %s", bus_error_message(&error
));
2870 printf("Job %u removed (result=%s).\n", id
, result
);
2873 } else if (dbus_message_is_signal(message
, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2875 const char *path
, *interface
, *property
= "Id";
2876 DBusMessageIter iter
, sub
;
2878 path
= dbus_message_get_path(message
);
2880 if (!dbus_message_get_args(message
, &error
,
2881 DBUS_TYPE_STRING
, &interface
,
2882 DBUS_TYPE_INVALID
)) {
2883 log_error("Failed to parse message: %s", bus_error_message(&error
));
2887 if (!streq(interface
, "org.freedesktop.systemd1.Job") &&
2888 !streq(interface
, "org.freedesktop.systemd1.Unit"))
2891 if (!(m
= dbus_message_new_method_call(
2892 "org.freedesktop.systemd1",
2894 "org.freedesktop.DBus.Properties",
2896 log_error("Could not allocate message.");
2900 if (!dbus_message_append_args(m
,
2901 DBUS_TYPE_STRING
, &interface
,
2902 DBUS_TYPE_STRING
, &property
,
2903 DBUS_TYPE_INVALID
)) {
2904 log_error("Could not append arguments to message.");
2908 if (!(reply
= dbus_connection_send_with_reply_and_block(connection
, m
, -1, &error
))) {
2909 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2913 if (!dbus_message_iter_init(reply
, &iter
) ||
2914 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
2915 log_error("Failed to parse reply.");
2919 dbus_message_iter_recurse(&iter
, &sub
);
2921 if (streq(interface
, "org.freedesktop.systemd1.Unit")) {
2924 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
2925 log_error("Failed to parse reply.");
2929 dbus_message_iter_get_basic(&sub
, &id
);
2930 printf("Unit %s changed.\n", id
);
2934 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_UINT32
) {
2935 log_error("Failed to parse reply.");
2939 dbus_message_iter_get_basic(&sub
, &id
);
2940 printf("Job %u changed.\n", id
);
2946 dbus_message_unref(m
);
2949 dbus_message_unref(reply
);
2951 dbus_error_free(&error
);
2952 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
2956 dbus_message_unref(m
);
2959 dbus_message_unref(reply
);
2961 dbus_error_free(&error
);
2962 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
2965 static int monitor(DBusConnection
*bus
, char **args
, unsigned n
) {
2966 DBusMessage
*m
= NULL
, *reply
= NULL
;
2970 dbus_error_init(&error
);
2973 dbus_bus_add_match(bus
,
2975 "sender='org.freedesktop.systemd1',"
2976 "interface='org.freedesktop.systemd1.Manager',"
2977 "path='/org/freedesktop/systemd1'",
2980 if (dbus_error_is_set(&error
)) {
2981 log_error("Failed to add match: %s", bus_error_message(&error
));
2986 dbus_bus_add_match(bus
,
2988 "sender='org.freedesktop.systemd1',"
2989 "interface='org.freedesktop.DBus.Properties',"
2990 "member='PropertiesChanged'",
2993 if (dbus_error_is_set(&error
)) {
2994 log_error("Failed to add match: %s", bus_error_message(&error
));
3000 if (!dbus_connection_add_filter(bus
, monitor_filter
, NULL
, NULL
)) {
3001 log_error("Failed to add filter.");
3006 if (!(m
= dbus_message_new_method_call(
3007 "org.freedesktop.systemd1",
3008 "/org/freedesktop/systemd1",
3009 "org.freedesktop.systemd1.Manager",
3011 log_error("Could not allocate message.");
3016 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3017 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3022 while (dbus_connection_read_write_dispatch(bus
, -1))
3029 /* This is slightly dirty, since we don't undo the filter or the matches. */
3032 dbus_message_unref(m
);
3035 dbus_message_unref(reply
);
3037 dbus_error_free(&error
);
3042 static int dump(DBusConnection
*bus
, char **args
, unsigned n
) {
3043 DBusMessage
*m
= NULL
, *reply
= NULL
;
3048 dbus_error_init(&error
);
3052 if (!(m
= dbus_message_new_method_call(
3053 "org.freedesktop.systemd1",
3054 "/org/freedesktop/systemd1",
3055 "org.freedesktop.systemd1.Manager",
3057 log_error("Could not allocate message.");
3061 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3062 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3067 if (!dbus_message_get_args(reply
, &error
,
3068 DBUS_TYPE_STRING
, &text
,
3069 DBUS_TYPE_INVALID
)) {
3070 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3075 fputs(text
, stdout
);
3081 dbus_message_unref(m
);
3084 dbus_message_unref(reply
);
3086 dbus_error_free(&error
);
3091 static int snapshot(DBusConnection
*bus
, char **args
, unsigned n
) {
3092 DBusMessage
*m
= NULL
, *reply
= NULL
;
3095 const char *name
= "", *path
, *id
;
3096 dbus_bool_t cleanup
= FALSE
;
3097 DBusMessageIter iter
, sub
;
3099 *interface
= "org.freedesktop.systemd1.Unit",
3102 dbus_error_init(&error
);
3104 if (!(m
= dbus_message_new_method_call(
3105 "org.freedesktop.systemd1",
3106 "/org/freedesktop/systemd1",
3107 "org.freedesktop.systemd1.Manager",
3108 "CreateSnapshot"))) {
3109 log_error("Could not allocate message.");
3116 if (!dbus_message_append_args(m
,
3117 DBUS_TYPE_STRING
, &name
,
3118 DBUS_TYPE_BOOLEAN
, &cleanup
,
3119 DBUS_TYPE_INVALID
)) {
3120 log_error("Could not append arguments to message.");
3125 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3126 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3131 if (!dbus_message_get_args(reply
, &error
,
3132 DBUS_TYPE_OBJECT_PATH
, &path
,
3133 DBUS_TYPE_INVALID
)) {
3134 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3139 dbus_message_unref(m
);
3140 if (!(m
= dbus_message_new_method_call(
3141 "org.freedesktop.systemd1",
3143 "org.freedesktop.DBus.Properties",
3145 log_error("Could not allocate message.");
3149 if (!dbus_message_append_args(m
,
3150 DBUS_TYPE_STRING
, &interface
,
3151 DBUS_TYPE_STRING
, &property
,
3152 DBUS_TYPE_INVALID
)) {
3153 log_error("Could not append arguments to message.");
3158 dbus_message_unref(reply
);
3159 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3160 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3165 if (!dbus_message_iter_init(reply
, &iter
) ||
3166 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3167 log_error("Failed to parse reply.");
3172 dbus_message_iter_recurse(&iter
, &sub
);
3174 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
3175 log_error("Failed to parse reply.");
3180 dbus_message_iter_get_basic(&sub
, &id
);
3188 dbus_message_unref(m
);
3191 dbus_message_unref(reply
);
3193 dbus_error_free(&error
);
3198 static int delete_snapshot(DBusConnection
*bus
, char **args
, unsigned n
) {
3199 DBusMessage
*m
= NULL
, *reply
= NULL
;
3207 dbus_error_init(&error
);
3209 for (i
= 1; i
< n
; i
++) {
3210 const char *path
= NULL
;
3212 if (!(m
= dbus_message_new_method_call(
3213 "org.freedesktop.systemd1",
3214 "/org/freedesktop/systemd1",
3215 "org.freedesktop.systemd1.Manager",
3217 log_error("Could not allocate message.");
3222 if (!dbus_message_append_args(m
,
3223 DBUS_TYPE_STRING
, &args
[i
],
3224 DBUS_TYPE_INVALID
)) {
3225 log_error("Could not append arguments to message.");
3230 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3231 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3236 if (!dbus_message_get_args(reply
, &error
,
3237 DBUS_TYPE_OBJECT_PATH
, &path
,
3238 DBUS_TYPE_INVALID
)) {
3239 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3244 dbus_message_unref(m
);
3245 if (!(m
= dbus_message_new_method_call(
3246 "org.freedesktop.systemd1",
3248 "org.freedesktop.systemd1.Snapshot",
3250 log_error("Could not allocate message.");
3255 dbus_message_unref(reply
);
3256 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3257 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3262 dbus_message_unref(m
);
3263 dbus_message_unref(reply
);
3271 dbus_message_unref(m
);
3274 dbus_message_unref(reply
);
3276 dbus_error_free(&error
);
3281 static int daemon_reload(DBusConnection
*bus
, char **args
, unsigned n
) {
3282 DBusMessage
*m
= NULL
, *reply
= NULL
;
3287 dbus_error_init(&error
);
3289 if (arg_action
== ACTION_RELOAD
)
3291 else if (arg_action
== ACTION_REEXEC
)
3292 method
= "Reexecute";
3294 assert(arg_action
== ACTION_SYSTEMCTL
);
3297 streq(args
[0], "clear-jobs") ||
3298 streq(args
[0], "cancel") ? "ClearJobs" :
3299 streq(args
[0], "daemon-reexec") ? "Reexecute" :
3300 streq(args
[0], "reset-failed") ? "ResetFailed" :
3301 streq(args
[0], "halt") ? "Halt" :
3302 streq(args
[0], "poweroff") ? "PowerOff" :
3303 streq(args
[0], "reboot") ? "Reboot" :
3304 streq(args
[0], "kexec") ? "KExec" :
3305 streq(args
[0], "exit") ? "Exit" :
3306 /* "daemon-reload" */ "Reload";
3309 if (!(m
= dbus_message_new_method_call(
3310 "org.freedesktop.systemd1",
3311 "/org/freedesktop/systemd1",
3312 "org.freedesktop.systemd1.Manager",
3314 log_error("Could not allocate message.");
3318 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3320 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(&error
)) {
3321 /* There's always a fallback possible for
3322 * legacy actions. */
3327 if (streq(method
, "Reexecute") && dbus_error_has_name(&error
, DBUS_ERROR_NO_REPLY
)) {
3328 /* On reexecution, we expect a disconnect, not
3334 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3343 dbus_message_unref(m
);
3346 dbus_message_unref(reply
);
3348 dbus_error_free(&error
);
3353 static int reset_failed(DBusConnection
*bus
, char **args
, unsigned n
) {
3354 DBusMessage
*m
= NULL
, *reply
= NULL
;
3360 dbus_error_init(&error
);
3363 return daemon_reload(bus
, args
, n
);
3365 for (i
= 1; i
< n
; i
++) {
3367 if (!(m
= dbus_message_new_method_call(
3368 "org.freedesktop.systemd1",
3369 "/org/freedesktop/systemd1",
3370 "org.freedesktop.systemd1.Manager",
3371 "ResetFailedUnit"))) {
3372 log_error("Could not allocate message.");
3377 if (!dbus_message_append_args(m
,
3378 DBUS_TYPE_STRING
, args
+ i
,
3379 DBUS_TYPE_INVALID
)) {
3380 log_error("Could not append arguments to message.");
3385 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3386 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3391 dbus_message_unref(m
);
3392 dbus_message_unref(reply
);
3400 dbus_message_unref(m
);
3403 dbus_message_unref(reply
);
3405 dbus_error_free(&error
);
3410 static int show_enviroment(DBusConnection
*bus
, char **args
, unsigned n
) {
3411 DBusMessage
*m
= NULL
, *reply
= NULL
;
3413 DBusMessageIter iter
, sub
, sub2
;
3416 *interface
= "org.freedesktop.systemd1.Manager",
3417 *property
= "Environment";
3419 dbus_error_init(&error
);
3423 if (!(m
= dbus_message_new_method_call(
3424 "org.freedesktop.systemd1",
3425 "/org/freedesktop/systemd1",
3426 "org.freedesktop.DBus.Properties",
3428 log_error("Could not allocate message.");
3432 if (!dbus_message_append_args(m
,
3433 DBUS_TYPE_STRING
, &interface
,
3434 DBUS_TYPE_STRING
, &property
,
3435 DBUS_TYPE_INVALID
)) {
3436 log_error("Could not append arguments to message.");
3441 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3442 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3447 if (!dbus_message_iter_init(reply
, &iter
) ||
3448 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3449 log_error("Failed to parse reply.");
3454 dbus_message_iter_recurse(&iter
, &sub
);
3456 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_ARRAY
||
3457 dbus_message_iter_get_element_type(&sub
) != DBUS_TYPE_STRING
) {
3458 log_error("Failed to parse reply.");
3463 dbus_message_iter_recurse(&sub
, &sub2
);
3465 while (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_INVALID
) {
3468 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_STRING
) {
3469 log_error("Failed to parse reply.");
3474 dbus_message_iter_get_basic(&sub2
, &text
);
3475 printf("%s\n", text
);
3477 dbus_message_iter_next(&sub2
);
3484 dbus_message_unref(m
);
3487 dbus_message_unref(reply
);
3489 dbus_error_free(&error
);
3494 static int set_environment(DBusConnection
*bus
, char **args
, unsigned n
) {
3495 DBusMessage
*m
= NULL
, *reply
= NULL
;
3499 DBusMessageIter iter
, sub
;
3502 dbus_error_init(&error
);
3504 method
= streq(args
[0], "set-environment")
3506 : "UnsetEnvironment";
3508 if (!(m
= dbus_message_new_method_call(
3509 "org.freedesktop.systemd1",
3510 "/org/freedesktop/systemd1",
3511 "org.freedesktop.systemd1.Manager",
3514 log_error("Could not allocate message.");
3518 dbus_message_iter_init_append(m
, &iter
);
3520 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
)) {
3521 log_error("Could not append arguments to message.");
3526 for (i
= 1; i
< n
; i
++)
3527 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &args
[i
])) {
3528 log_error("Could not append arguments to message.");
3533 if (!dbus_message_iter_close_container(&iter
, &sub
)) {
3534 log_error("Could not append arguments to message.");
3539 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3540 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3549 dbus_message_unref(m
);
3552 dbus_message_unref(reply
);
3554 dbus_error_free(&error
);
3567 static Hashmap
*will_install
= NULL
, *have_installed
= NULL
;
3568 static Set
*remove_symlinks_to
= NULL
;
3569 static unsigned n_symlinks
= 0;
3571 static void install_info_free(InstallInfo
*i
) {
3576 strv_free(i
->aliases
);
3577 strv_free(i
->wanted_by
);
3581 static void install_info_hashmap_free(Hashmap
*m
) {
3584 while ((i
= hashmap_steal_first(m
)))
3585 install_info_free(i
);
3590 static int install_info_add(const char *name
) {
3594 assert(will_install
);
3596 if (!unit_name_is_valid_no_type(name
, true)) {
3597 log_warning("Unit name %s is not a valid unit name.", name
);
3601 if (hashmap_get(have_installed
, name
) ||
3602 hashmap_get(will_install
, name
))
3605 if (!(i
= new0(InstallInfo
, 1))) {
3610 if (!(i
->name
= strdup(name
))) {
3615 if ((r
= hashmap_put(will_install
, i
->name
, i
)) < 0)
3622 install_info_free(i
);
3627 static int config_parse_also(
3628 const char *filename
,
3630 const char *section
,
3645 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
3649 if (!(n
= strndup(w
, l
)))
3652 if ((r
= install_info_add(n
)) < 0) {
3653 log_warning("Cannot install unit %s: %s", n
, strerror(-r
));
3664 static int mark_symlink_for_removal(const char *p
) {
3669 assert(path_is_absolute(p
));
3671 if (!remove_symlinks_to
)
3674 if (!(n
= strdup(p
)))
3677 path_kill_slashes(n
);
3679 if ((r
= set_put(remove_symlinks_to
, n
)) < 0) {
3681 return r
== -EEXIST
? 0 : r
;
3687 static int remove_marked_symlinks_fd(int fd
, const char *config_path
, const char *root
, bool *deleted
) {
3696 if (!(d
= fdopendir(fd
))) {
3697 close_nointr_nofail(fd
);
3703 while ((de
= readdir(d
))) {
3704 bool is_dir
= false, is_link
= false;
3706 if (ignore_file(de
->d_name
))
3709 if (de
->d_type
== DT_LNK
)
3711 else if (de
->d_type
== DT_DIR
)
3713 else if (de
->d_type
== DT_UNKNOWN
) {
3716 if (fstatat(fd
, de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
3717 log_error("Failed to stat %s/%s: %m", root
, de
->d_name
);
3724 is_link
= S_ISLNK(st
.st_mode
);
3725 is_dir
= S_ISDIR(st
.st_mode
);
3733 if ((nfd
= openat(fd
, de
->d_name
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
)) < 0) {
3734 log_error("Failed to open %s/%s: %m", root
, de
->d_name
);
3741 if (asprintf(&p
, "%s/%s", root
, de
->d_name
) < 0) {
3742 log_error("Failed to allocate directory string.");
3743 close_nointr_nofail(nfd
);
3748 /* This will close nfd, regardless whether it succeeds or not */
3749 q
= remove_marked_symlinks_fd(nfd
, config_path
, p
, deleted
);
3755 } else if (is_link
) {
3759 if (asprintf(&p
, "%s/%s", root
, de
->d_name
) < 0) {
3760 log_error("Failed to allocate symlink string.");
3765 if ((q
= readlink_and_make_absolute(p
, &dest
)) < 0) {
3766 log_error("Cannot read symlink %s: %s", p
, strerror(-q
));
3774 if ((c
= canonicalize_file_name(dest
))) {
3775 /* This might fail if the destination
3776 * is already removed */
3782 path_kill_slashes(dest
);
3783 if (set_get(remove_symlinks_to
, dest
)) {
3786 log_info("rm '%s'", p
);
3788 if (unlink(p
) < 0) {
3789 log_error("Cannot unlink symlink %s: %m", p
);
3794 rmdir_parents(p
, config_path
);
3795 path_kill_slashes(p
);
3797 if (!set_get(remove_symlinks_to
, p
)) {
3799 if ((r
= mark_symlink_for_removal(p
)) < 0) {
3818 static int remove_marked_symlinks(const char *config_path
) {
3822 assert(config_path
);
3824 if (set_size(remove_symlinks_to
) <= 0)
3827 if ((fd
= open(config_path
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
)) < 0)
3834 if ((cfd
= dup(fd
)) < 0) {
3839 /* This takes possession of cfd and closes it */
3840 if ((q
= remove_marked_symlinks_fd(cfd
, config_path
, config_path
, &deleted
)) < 0) {
3846 close_nointr_nofail(fd
);
3851 static int create_symlink(const char *verb
, const char *old_path
, const char *new_path
) {
3858 if (streq(verb
, "enable")) {
3861 mkdir_parents(new_path
, 0755);
3863 if (symlink(old_path
, new_path
) >= 0) {
3866 log_info("ln -s '%s' '%s'", old_path
, new_path
);
3871 if (errno
!= EEXIST
) {
3872 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
3876 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3878 if (errno
== EINVAL
) {
3879 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path
, new_path
);
3883 log_error("readlink() failed: %s", strerror(-r
));
3887 if (streq(dest
, old_path
)) {
3893 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path
, new_path
, dest
);
3902 log_info("ln -s '%s' '%s'", old_path
, new_path
);
3904 if (symlink(old_path
, new_path
) >= 0)
3907 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
3910 } else if (streq(verb
, "disable")) {
3913 if ((r
= mark_symlink_for_removal(old_path
)) < 0)
3916 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3917 if (errno
== ENOENT
)
3920 if (errno
== EINVAL
) {
3921 log_warning("File %s not a symlink, ignoring.", old_path
);
3925 log_error("readlink() failed: %s", strerror(-r
));
3929 if (!streq(dest
, old_path
)) {
3930 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path
, old_path
, dest
);
3937 if ((r
= mark_symlink_for_removal(new_path
)) < 0)
3941 log_info("rm '%s'", new_path
);
3943 if (unlink(new_path
) >= 0)
3946 log_error("Cannot unlink %s: %m", new_path
);
3949 } else if (streq(verb
, "is-enabled")) {
3952 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3954 if (errno
== ENOENT
|| errno
== EINVAL
)
3957 log_error("readlink() failed: %s", strerror(-r
));
3961 if (streq(dest
, old_path
)) {
3970 assert_not_reached("Unknown action.");
3973 static int install_info_symlink_alias(const char *verb
, InstallInfo
*i
, const char *config_path
) {
3975 char *alias_path
= NULL
;
3980 assert(config_path
);
3982 STRV_FOREACH(s
, i
->aliases
) {
3985 if (!(alias_path
= path_make_absolute(*s
, config_path
))) {
3986 log_error("Out of memory");
3991 if ((r
= create_symlink(verb
, i
->path
, alias_path
)) != 0)
3994 if (streq(verb
, "disable"))
3995 rmdir_parents(alias_path
, config_path
);
4005 static int install_info_symlink_wants(const char *verb
, InstallInfo
*i
, const char *config_path
) {
4007 char *alias_path
= NULL
;
4012 assert(config_path
);
4014 STRV_FOREACH(s
, i
->wanted_by
) {
4015 if (!unit_name_is_valid_no_type(*s
, true)) {
4016 log_error("Invalid name %s.", *s
);
4024 if (asprintf(&alias_path
, "%s/%s.wants/%s", config_path
, *s
, i
->name
) < 0) {
4025 log_error("Out of memory");
4030 if ((r
= create_symlink(verb
, i
->path
, alias_path
)) != 0)
4033 if (streq(verb
, "disable"))
4034 rmdir_parents(alias_path
, config_path
);
4045 static int install_info_apply(const char *verb
, LookupPaths
*paths
, InstallInfo
*i
, const char *config_path
) {
4047 const ConfigItem items
[] = {
4048 { "Alias", config_parse_strv
, 0, &i
->aliases
, "Install" },
4049 { "WantedBy", config_parse_strv
, 0, &i
->wanted_by
, "Install" },
4050 { "Also", config_parse_also
, 0, NULL
, "Install" },
4052 { NULL
, NULL
, 0, NULL
, NULL
}
4056 char *filename
= NULL
;
4063 STRV_FOREACH(p
, paths
->unit_path
) {
4066 if (!(filename
= path_make_absolute(i
->name
, *p
))) {
4067 log_error("Out of memory");
4071 /* Ensure that we don't follow symlinks */
4072 if ((fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NOCTTY
)) >= 0)
4073 if ((f
= fdopen(fd
, "re")))
4076 if (errno
== ELOOP
) {
4077 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
4082 if (errno
!= ENOENT
) {
4083 log_error("Failed to open %s: %m", filename
);
4093 #if (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX)) && defined (HAVE_SYSV_COMPAT)
4095 if (endswith(i
->name
, ".service")) {
4099 if (asprintf(&sysv
, SYSTEM_SYSVINIT_PATH
"/%s", i
->name
) < 0) {
4100 log_error("Out of memory");
4104 sysv
[strlen(sysv
) - sizeof(".service") + 1] = 0;
4105 exists
= access(sysv
, F_OK
) >= 0;
4111 const char *argv
[] = {
4118 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i
->name
);
4120 argv
[1] = file_name_from_path(sysv
);
4122 streq(verb
, "enable") ? "on" :
4123 streq(verb
, "disable") ? "off" : "--level=5";
4125 log_info("Executing %s %s %s", argv
[0], argv
[1], strempty(argv
[2]));
4127 if ((pid
= fork()) < 0) {
4128 log_error("Failed to fork: %m");
4131 } else if (pid
== 0) {
4132 execv(argv
[0], (char**) argv
);
4133 _exit(EXIT_FAILURE
);
4138 if ((r
= wait_for_terminate(pid
, &status
)) < 0)
4141 if (status
.si_code
== CLD_EXITED
) {
4143 if (streq(verb
, "is-enabled"))
4144 return status
.si_status
== 0 ? 1 : 0;
4146 if (status
.si_status
== 0)
4149 return status
.si_status
== 0 ? 0 : -EINVAL
;
4160 log_error("Couldn't find %s.", i
->name
);
4166 if ((r
= config_parse(filename
, f
, NULL
, items
, true, i
)) < 0) {
4171 /* Consider unit files stored in /lib and /usr always enabled
4172 * if they have no [Install] data. */
4173 if (streq(verb
, "is-enabled") &&
4174 strv_isempty(i
->aliases
) &&
4175 strv_isempty(i
->wanted_by
) &&
4176 !path_startswith(filename
, "/etc")) {
4181 n_symlinks
+= strv_length(i
->aliases
);
4182 n_symlinks
+= strv_length(i
->wanted_by
);
4186 if ((r
= install_info_symlink_alias(verb
, i
, config_path
)) != 0)
4189 if ((r
= install_info_symlink_wants(verb
, i
, config_path
)) != 0)
4192 if ((r
= mark_symlink_for_removal(filename
)) < 0)
4195 if ((r
= remove_marked_symlinks(config_path
)) < 0)
4201 static char *get_config_path(void) {
4203 if (arg_user
&& arg_global
)
4204 return strdup(USER_CONFIG_UNIT_PATH
);
4209 if (user_config_home(&p
) < 0)
4215 return strdup(SYSTEM_CONFIG_UNIT_PATH
);
4218 static int enable_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
4222 char *config_path
= NULL
;
4225 const char *verb
= args
[0];
4227 dbus_error_init(&error
);
4230 if ((r
= lookup_paths_init(&paths
, arg_user
? MANAGER_USER
: MANAGER_SYSTEM
)) < 0) {
4231 log_error("Failed to determine lookup paths: %s", strerror(-r
));
4235 if (!(config_path
= get_config_path())) {
4236 log_error("Failed to determine config path");
4241 will_install
= hashmap_new(string_hash_func
, string_compare_func
);
4242 have_installed
= hashmap_new(string_hash_func
, string_compare_func
);
4244 if (!will_install
|| !have_installed
) {
4245 log_error("Failed to allocate unit sets.");
4250 if (!arg_defaults
&& streq(verb
, "disable"))
4251 if (!(remove_symlinks_to
= set_new(string_hash_func
, string_compare_func
))) {
4252 log_error("Failed to allocate symlink sets.");
4257 for (j
= 1; j
< n
; j
++)
4258 if ((r
= install_info_add(args
[j
])) < 0) {
4259 log_warning("Cannot install unit %s: %s", args
[j
], strerror(-r
));
4265 while ((i
= hashmap_first(will_install
))) {
4268 assert_se(hashmap_move_one(have_installed
, will_install
, i
->name
) == 0);
4270 if ((q
= install_info_apply(verb
, &paths
, i
, config_path
)) != 0) {
4278 /* In test mode and found something */
4284 if (streq(verb
, "is-enabled"))
4285 r
= r
> 0 ? 0 : -ENOENT
;
4287 if (n_symlinks
<= 0)
4288 log_warning("Unit files contain no applicable installation information. Ignoring.");
4291 /* Don't try to reload anything if the user asked us to not do this */
4293 /* Don't try to reload anything when updating a unit globally */
4295 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4296 (arg_user
|| sd_booted() > 0) &&
4297 /* Don't try to reload anything if we are running in a chroot environment */
4298 (arg_user
|| running_in_chroot() <= 0) ) {
4301 if ((q
= daemon_reload(bus
, args
, n
)) < 0)
4307 install_info_hashmap_free(will_install
);
4308 install_info_hashmap_free(have_installed
);
4310 set_free_free(remove_symlinks_to
);
4312 lookup_paths_free(&paths
);
4319 static int systemctl_help(void) {
4321 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4322 "Send control commands to or query the systemd manager.\n\n"
4323 " -h --help Show this help\n"
4324 " --version Show package version\n"
4325 " -t --type=TYPE List only units of a particular type\n"
4326 " -p --property=NAME Show only properties by this name\n"
4327 " -a --all Show all units/properties, including dead/empty ones\n"
4328 " --failed Show only failed units\n"
4329 " --full Don't ellipsize unit names on output\n"
4330 " --fail When queueing a new job, fail if conflicting jobs are\n"
4332 " --ignore-dependencies\n"
4333 " When queueing a new job, ignore all its dependencies\n"
4334 " --kill-mode=MODE How to send signal\n"
4335 " --kill-who=WHO Who to send signal to\n"
4336 " -s --signal=SIGNAL Which signal to send\n"
4337 " -H --host=[user@]host\n"
4338 " Show information for remote host\n"
4339 " -P --privileged Acquire privileges before execution\n"
4340 " -q --quiet Suppress output\n"
4341 " --no-block Do not wait until operation finished\n"
4342 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4343 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4345 " --no-pager Do not pipe output into a pager.\n"
4346 " --no-ask-password\n"
4347 " Do not ask for system passwords\n"
4348 " --order When generating graph for dot, show only order\n"
4349 " --require When generating graph for dot, show only requirement\n"
4350 " --system Connect to system manager\n"
4351 " --user Connect to user service manager\n"
4352 " --global Enable/disable unit files globally\n"
4353 " -f --force When enabling unit files, override existing symlinks\n"
4354 " When shutting down, execute action immediately\n"
4355 " --defaults When disabling unit files, remove default symlinks only\n\n"
4357 " list-units List units\n"
4358 " start [NAME...] Start (activate) one or more units\n"
4359 " stop [NAME...] Stop (deactivate) one or more units\n"
4360 " reload [NAME...] Reload one or more units\n"
4361 " restart [NAME...] Start or restart one or more units\n"
4362 " try-restart [NAME...] Restart one or more units if active\n"
4363 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4364 " otherwise start or restart\n"
4365 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4366 " otherwise restart if active\n"
4367 " isolate [NAME] Start one unit and stop all others\n"
4368 " kill [NAME...] Send signal to processes of a unit\n"
4369 " is-active [NAME...] Check whether units are active\n"
4370 " status [NAME...|PID...] Show runtime status of one or more units\n"
4371 " show [NAME...|JOB...] Show properties of one or more\n"
4372 " units/jobs or the manager\n"
4373 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4375 " enable [NAME...] Enable one or more unit files\n"
4376 " disable [NAME...] Disable one or more unit files\n"
4377 " is-enabled [NAME...] Check whether unit files are enabled\n"
4378 " load [NAME...] Load one or more units\n"
4379 " list-jobs List jobs\n"
4380 " cancel [JOB...] Cancel all, one, or more jobs\n"
4381 " monitor Monitor unit/job changes\n"
4382 " dump Dump server status\n"
4383 " dot Dump dependency graph for dot(1)\n"
4384 " snapshot [NAME] Create a snapshot\n"
4385 " delete [NAME...] Remove one or more snapshots\n"
4386 " daemon-reload Reload systemd manager configuration\n"
4387 " daemon-reexec Reexecute systemd manager\n"
4388 " show-environment Dump environment\n"
4389 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4390 " unset-environment [NAME...] Unset one or more environment variables\n"
4391 " default Enter system default mode\n"
4392 " rescue Enter system rescue mode\n"
4393 " emergency Enter system emergency mode\n"
4394 " halt Shut down and halt the system\n"
4395 " poweroff Shut down and power-off the system\n"
4396 " reboot Shut down and reboot the system\n"
4397 " kexec Shut down and reboot the system with kexec\n"
4398 " exit Ask for user instance termination\n",
4399 program_invocation_short_name
);
4404 static int halt_help(void) {
4406 printf("%s [OPTIONS...]\n\n"
4407 "%s the system.\n\n"
4408 " --help Show this help\n"
4409 " --halt Halt the machine\n"
4410 " -p --poweroff Switch off the machine\n"
4411 " --reboot Reboot the machine\n"
4412 " -f --force Force immediate halt/power-off/reboot\n"
4413 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4414 " -d --no-wtmp Don't write wtmp record\n"
4415 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4416 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4417 program_invocation_short_name
,
4418 arg_action
== ACTION_REBOOT
? "Reboot" :
4419 arg_action
== ACTION_POWEROFF
? "Power off" :
4425 static int shutdown_help(void) {
4427 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4428 "Shut down the system.\n\n"
4429 " --help Show this help\n"
4430 " -H --halt Halt the machine\n"
4431 " -P --poweroff Power-off the machine\n"
4432 " -r --reboot Reboot the machine\n"
4433 " -h Equivalent to --poweroff, overriden by --halt\n"
4434 " -k Don't halt/power-off/reboot, just send warnings\n"
4435 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4436 " -c Cancel a pending shutdown\n",
4437 program_invocation_short_name
);
4442 static int telinit_help(void) {
4444 printf("%s [OPTIONS...] {COMMAND}\n\n"
4445 "Send control commands to the init daemon.\n\n"
4446 " --help Show this help\n"
4447 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4449 " 0 Power-off the machine\n"
4450 " 6 Reboot the machine\n"
4451 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4452 " 1, s, S Enter rescue mode\n"
4453 " q, Q Reload init daemon configuration\n"
4454 " u, U Reexecute init daemon\n",
4455 program_invocation_short_name
);
4460 static int runlevel_help(void) {
4462 printf("%s [OPTIONS...]\n\n"
4463 "Prints the previous and current runlevel of the init system.\n\n"
4464 " --help Show this help\n",
4465 program_invocation_short_name
);
4470 static int systemctl_parse_argv(int argc
, char *argv
[]) {
4474 ARG_IGNORE_DEPENDENCIES
,
4489 ARG_NO_ASK_PASSWORD
,
4493 static const struct option options
[] = {
4494 { "help", no_argument
, NULL
, 'h' },
4495 { "version", no_argument
, NULL
, ARG_VERSION
},
4496 { "type", required_argument
, NULL
, 't' },
4497 { "property", required_argument
, NULL
, 'p' },
4498 { "all", no_argument
, NULL
, 'a' },
4499 { "failed", no_argument
, NULL
, ARG_FAILED
},
4500 { "full", no_argument
, NULL
, ARG_FULL
},
4501 { "fail", no_argument
, NULL
, ARG_FAIL
},
4502 { "ignore-dependencies", no_argument
, NULL
, ARG_IGNORE_DEPENDENCIES
},
4503 { "user", no_argument
, NULL
, ARG_USER
},
4504 { "system", no_argument
, NULL
, ARG_SYSTEM
},
4505 { "global", no_argument
, NULL
, ARG_GLOBAL
},
4506 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
4507 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
4508 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4509 { "quiet", no_argument
, NULL
, 'q' },
4510 { "order", no_argument
, NULL
, ARG_ORDER
},
4511 { "require", no_argument
, NULL
, ARG_REQUIRE
},
4512 { "force", no_argument
, NULL
, 'f' },
4513 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
4514 { "defaults", no_argument
, NULL
, ARG_DEFAULTS
},
4515 { "kill-mode", required_argument
, NULL
, ARG_KILL_MODE
},
4516 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
4517 { "signal", required_argument
, NULL
, 's' },
4518 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
4519 { "host", required_argument
, NULL
, 'H' },
4520 { "privileged",no_argument
, NULL
, 'P' },
4521 { NULL
, 0, NULL
, 0 }
4529 /* Only when running as systemctl we ask for passwords */
4530 arg_ask_password
= true;
4532 while ((c
= getopt_long(argc
, argv
, "ht:p:aqfs:H:P", options
, NULL
)) >= 0) {
4541 puts(PACKAGE_STRING
);
4543 puts(SYSTEMD_FEATURES
);
4553 if (!(l
= strv_append(arg_property
, optarg
)))
4556 strv_free(arg_property
);
4559 /* If the user asked for a particular
4560 * property, show it to him, even if it is
4571 arg_job_mode
= "fail";
4574 case ARG_IGNORE_DEPENDENCIES
:
4575 arg_job_mode
= "ignore-dependencies";
4587 arg_no_block
= true;
4591 arg_no_pager
= true;
4599 arg_dot
= DOT_ORDER
;
4603 arg_dot
= DOT_REQUIRE
;
4623 arg_no_reload
= true;
4632 arg_defaults
= true;
4636 arg_kill_who
= optarg
;
4640 arg_kill_mode
= optarg
;
4644 if ((arg_signal
= signal_from_string_try_harder(optarg
)) < 0) {
4645 log_error("Failed to parse signal string %s.", optarg
);
4650 case ARG_NO_ASK_PASSWORD
:
4651 arg_ask_password
= false;
4655 arg_transport
= TRANSPORT_POLKIT
;
4659 arg_transport
= TRANSPORT_SSH
;
4667 log_error("Unknown option code %c", c
);
4672 if (arg_transport
!= TRANSPORT_NORMAL
&& arg_user
) {
4673 log_error("Cannot access user instance remotely.");
4680 static int halt_parse_argv(int argc
, char *argv
[]) {
4689 static const struct option options
[] = {
4690 { "help", no_argument
, NULL
, ARG_HELP
},
4691 { "halt", no_argument
, NULL
, ARG_HALT
},
4692 { "poweroff", no_argument
, NULL
, 'p' },
4693 { "reboot", no_argument
, NULL
, ARG_REBOOT
},
4694 { "force", no_argument
, NULL
, 'f' },
4695 { "wtmp-only", no_argument
, NULL
, 'w' },
4696 { "no-wtmp", no_argument
, NULL
, 'd' },
4697 { "no-sync", no_argument
, NULL
, 'n' },
4698 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4699 { NULL
, 0, NULL
, 0 }
4707 if (utmp_get_runlevel(&runlevel
, NULL
) >= 0)
4708 if (runlevel
== '0' || runlevel
== '6')
4709 arg_immediate
= true;
4711 while ((c
= getopt_long(argc
, argv
, "pfwdnih", options
, NULL
)) >= 0) {
4719 arg_action
= ACTION_HALT
;
4723 if (arg_action
!= ACTION_REBOOT
)
4724 arg_action
= ACTION_POWEROFF
;
4728 arg_action
= ACTION_REBOOT
;
4732 arg_immediate
= true;
4753 /* Compatibility nops */
4760 log_error("Unknown option code %c", c
);
4765 if (optind
< argc
) {
4766 log_error("Too many arguments.");
4773 static int parse_time_spec(const char *t
, usec_t
*_u
) {
4777 if (streq(t
, "now"))
4779 else if (t
[0] == '+') {
4782 if (safe_atou64(t
+ 1, &u
) < 0)
4785 *_u
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
* u
;
4794 hour
= strtol(t
, &e
, 10);
4795 if (errno
!= 0 || *e
!= ':' || hour
< 0 || hour
> 23)
4798 minute
= strtol(e
+1, &e
, 10);
4799 if (errno
!= 0 || *e
!= 0 || minute
< 0 || minute
> 59)
4802 n
= now(CLOCK_REALTIME
);
4803 s
= (time_t) (n
/ USEC_PER_SEC
);
4806 assert_se(localtime_r(&s
, &tm
));
4808 tm
.tm_hour
= (int) hour
;
4809 tm
.tm_min
= (int) minute
;
4812 assert_se(s
= mktime(&tm
));
4814 *_u
= (usec_t
) s
* USEC_PER_SEC
;
4817 *_u
+= USEC_PER_DAY
;
4823 static bool kexec_loaded(void) {
4824 bool loaded
= false;
4827 if (read_one_line_file("/sys/kernel/kexec_loaded", &s
) >= 0) {
4835 static int shutdown_parse_argv(int argc
, char *argv
[]) {
4842 static const struct option options
[] = {
4843 { "help", no_argument
, NULL
, ARG_HELP
},
4844 { "halt", no_argument
, NULL
, 'H' },
4845 { "poweroff", no_argument
, NULL
, 'P' },
4846 { "reboot", no_argument
, NULL
, 'r' },
4847 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4848 { NULL
, 0, NULL
, 0 }
4856 while ((c
= getopt_long(argc
, argv
, "HPrhkt:afFc", options
, NULL
)) >= 0) {
4864 arg_action
= ACTION_HALT
;
4868 arg_action
= ACTION_POWEROFF
;
4873 arg_action
= ACTION_KEXEC
;
4875 arg_action
= ACTION_REBOOT
;
4879 if (arg_action
!= ACTION_HALT
)
4880 arg_action
= ACTION_POWEROFF
;
4893 /* Compatibility nops */
4897 arg_action
= ACTION_CANCEL_SHUTDOWN
;
4904 log_error("Unknown option code %c", c
);
4909 if (argc
> optind
) {
4910 if ((r
= parse_time_spec(argv
[optind
], &arg_when
)) < 0) {
4911 log_error("Failed to parse time specification: %s", argv
[optind
]);
4915 arg_when
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
;
4917 /* We skip the time argument */
4918 if (argc
> optind
+ 1)
4919 arg_wall
= argv
+ optind
+ 1;
4926 static int telinit_parse_argv(int argc
, char *argv
[]) {
4933 static const struct option options
[] = {
4934 { "help", no_argument
, NULL
, ARG_HELP
},
4935 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4936 { NULL
, 0, NULL
, 0 }
4939 static const struct {
4943 { '0', ACTION_POWEROFF
},
4944 { '6', ACTION_REBOOT
},
4945 { '1', ACTION_RESCUE
},
4946 { '2', ACTION_RUNLEVEL2
},
4947 { '3', ACTION_RUNLEVEL3
},
4948 { '4', ACTION_RUNLEVEL4
},
4949 { '5', ACTION_RUNLEVEL5
},
4950 { 's', ACTION_RESCUE
},
4951 { 'S', ACTION_RESCUE
},
4952 { 'q', ACTION_RELOAD
},
4953 { 'Q', ACTION_RELOAD
},
4954 { 'u', ACTION_REEXEC
},
4955 { 'U', ACTION_REEXEC
}
4964 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
4979 log_error("Unknown option code %c", c
);
4984 if (optind
>= argc
) {
4989 if (optind
+ 1 < argc
) {
4990 log_error("Too many arguments.");
4994 if (strlen(argv
[optind
]) != 1) {
4995 log_error("Expected single character argument.");
4999 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
5000 if (table
[i
].from
== argv
[optind
][0])
5003 if (i
>= ELEMENTSOF(table
)) {
5004 log_error("Unknown command %s.", argv
[optind
]);
5008 arg_action
= table
[i
].to
;
5015 static int runlevel_parse_argv(int argc
, char *argv
[]) {
5021 static const struct option options
[] = {
5022 { "help", no_argument
, NULL
, ARG_HELP
},
5023 { NULL
, 0, NULL
, 0 }
5031 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
5042 log_error("Unknown option code %c", c
);
5047 if (optind
< argc
) {
5048 log_error("Too many arguments.");
5055 static int parse_argv(int argc
, char *argv
[]) {
5059 if (program_invocation_short_name
) {
5061 if (strstr(program_invocation_short_name
, "halt")) {
5062 arg_action
= ACTION_HALT
;
5063 return halt_parse_argv(argc
, argv
);
5064 } else if (strstr(program_invocation_short_name
, "poweroff")) {
5065 arg_action
= ACTION_POWEROFF
;
5066 return halt_parse_argv(argc
, argv
);
5067 } else if (strstr(program_invocation_short_name
, "reboot")) {
5069 arg_action
= ACTION_KEXEC
;
5071 arg_action
= ACTION_REBOOT
;
5072 return halt_parse_argv(argc
, argv
);
5073 } else if (strstr(program_invocation_short_name
, "shutdown")) {
5074 arg_action
= ACTION_POWEROFF
;
5075 return shutdown_parse_argv(argc
, argv
);
5076 } else if (strstr(program_invocation_short_name
, "init")) {
5078 if (sd_booted() > 0) {
5079 arg_action
= ACTION_INVALID
;
5080 return telinit_parse_argv(argc
, argv
);
5082 /* Hmm, so some other init system is
5083 * running, we need to forward this
5084 * request to it. For now we simply
5085 * guess that it is Upstart. */
5087 execv("/lib/upstart/telinit", argv
);
5089 log_error("Couldn't find an alternative telinit implementation to spawn.");
5093 } else if (strstr(program_invocation_short_name
, "runlevel")) {
5094 arg_action
= ACTION_RUNLEVEL
;
5095 return runlevel_parse_argv(argc
, argv
);
5099 arg_action
= ACTION_SYSTEMCTL
;
5100 return systemctl_parse_argv(argc
, argv
);
5103 static int action_to_runlevel(void) {
5105 static const char table
[_ACTION_MAX
] = {
5106 [ACTION_HALT
] = '0',
5107 [ACTION_POWEROFF
] = '0',
5108 [ACTION_REBOOT
] = '6',
5109 [ACTION_RUNLEVEL2
] = '2',
5110 [ACTION_RUNLEVEL3
] = '3',
5111 [ACTION_RUNLEVEL4
] = '4',
5112 [ACTION_RUNLEVEL5
] = '5',
5113 [ACTION_RESCUE
] = '1'
5116 assert(arg_action
< _ACTION_MAX
);
5118 return table
[arg_action
];
5121 static int talk_upstart(void) {
5122 DBusMessage
*m
= NULL
, *reply
= NULL
;
5124 int previous
, rl
, r
;
5126 env1_buf
[] = "RUNLEVEL=X",
5127 env2_buf
[] = "PREVLEVEL=X";
5128 char *env1
= env1_buf
, *env2
= env2_buf
;
5129 const char *emit
= "runlevel";
5130 dbus_bool_t b_false
= FALSE
;
5131 DBusMessageIter iter
, sub
;
5132 DBusConnection
*bus
;
5134 dbus_error_init(&error
);
5136 if (!(rl
= action_to_runlevel()))
5139 if (utmp_get_runlevel(&previous
, NULL
) < 0)
5142 if (!(bus
= dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error
))) {
5143 if (dbus_error_has_name(&error
, DBUS_ERROR_NO_SERVER
)) {
5148 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error
));
5153 if ((r
= bus_check_peercred(bus
)) < 0) {
5154 log_error("Failed to verify owner of bus.");
5158 if (!(m
= dbus_message_new_method_call(
5159 "com.ubuntu.Upstart",
5160 "/com/ubuntu/Upstart",
5161 "com.ubuntu.Upstart0_6",
5164 log_error("Could not allocate message.");
5169 dbus_message_iter_init_append(m
, &iter
);
5171 env1_buf
[sizeof(env1_buf
)-2] = rl
;
5172 env2_buf
[sizeof(env2_buf
)-2] = previous
;
5174 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &emit
) ||
5175 !dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
) ||
5176 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env1
) ||
5177 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env2
) ||
5178 !dbus_message_iter_close_container(&iter
, &sub
) ||
5179 !dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &b_false
)) {
5180 log_error("Could not append arguments to message.");
5185 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
5187 if (error_is_no_service(&error
)) {
5192 log_error("Failed to issue method call: %s", bus_error_message(&error
));
5201 dbus_message_unref(m
);
5204 dbus_message_unref(reply
);
5207 dbus_connection_flush(bus
);
5208 dbus_connection_close(bus
);
5209 dbus_connection_unref(bus
);
5212 dbus_error_free(&error
);
5217 static int talk_initctl(void) {
5218 struct init_request request
;
5222 if (!(rl
= action_to_runlevel()))
5226 request
.magic
= INIT_MAGIC
;
5227 request
.sleeptime
= 0;
5228 request
.cmd
= INIT_CMD_RUNLVL
;
5229 request
.runlevel
= rl
;
5231 if ((fd
= open(INIT_FIFO
, O_WRONLY
|O_NDELAY
|O_CLOEXEC
|O_NOCTTY
)) < 0) {
5233 if (errno
== ENOENT
)
5236 log_error("Failed to open "INIT_FIFO
": %m");
5241 r
= loop_write(fd
, &request
, sizeof(request
), false) != sizeof(request
);
5242 close_nointr_nofail(fd
);
5245 log_error("Failed to write to "INIT_FIFO
": %m");
5246 return errno
? -errno
: -EIO
;
5252 static int systemctl_main(DBusConnection
*bus
, int argc
, char *argv
[], DBusError
*error
) {
5254 static const struct {
5262 int (* const dispatch
)(DBusConnection
*bus
, char **args
, unsigned n
);
5264 { "list-units", LESS
, 1, list_units
},
5265 { "list-jobs", EQUAL
, 1, list_jobs
},
5266 { "clear-jobs", EQUAL
, 1, daemon_reload
},
5267 { "load", MORE
, 2, load_unit
},
5268 { "cancel", MORE
, 2, cancel_job
},
5269 { "start", MORE
, 2, start_unit
},
5270 { "stop", MORE
, 2, start_unit
},
5271 { "condstop", MORE
, 2, start_unit
}, /* For compatibility with ALTLinux */
5272 { "reload", MORE
, 2, start_unit
},
5273 { "restart", MORE
, 2, start_unit
},
5274 { "try-restart", MORE
, 2, start_unit
},
5275 { "reload-or-restart", MORE
, 2, start_unit
},
5276 { "reload-or-try-restart", MORE
, 2, start_unit
},
5277 { "force-reload", MORE
, 2, start_unit
}, /* For compatibility with SysV */
5278 { "condreload", MORE
, 2, start_unit
}, /* For compatibility with ALTLinux */
5279 { "condrestart", MORE
, 2, start_unit
}, /* For compatibility with RH */
5280 { "isolate", EQUAL
, 2, start_unit
},
5281 { "kill", MORE
, 2, kill_unit
},
5282 { "is-active", MORE
, 2, check_unit
},
5283 { "check", MORE
, 2, check_unit
},
5284 { "show", MORE
, 1, show
},
5285 { "status", MORE
, 2, show
},
5286 { "monitor", EQUAL
, 1, monitor
},
5287 { "dump", EQUAL
, 1, dump
},
5288 { "dot", EQUAL
, 1, dot
},
5289 { "snapshot", LESS
, 2, snapshot
},
5290 { "delete", MORE
, 2, delete_snapshot
},
5291 { "daemon-reload", EQUAL
, 1, daemon_reload
},
5292 { "daemon-reexec", EQUAL
, 1, daemon_reload
},
5293 { "show-environment", EQUAL
, 1, show_enviroment
},
5294 { "set-environment", MORE
, 2, set_environment
},
5295 { "unset-environment", MORE
, 2, set_environment
},
5296 { "halt", EQUAL
, 1, start_special
},
5297 { "poweroff", EQUAL
, 1, start_special
},
5298 { "reboot", EQUAL
, 1, start_special
},
5299 { "kexec", EQUAL
, 1, start_special
},
5300 { "default", EQUAL
, 1, start_special
},
5301 { "rescue", EQUAL
, 1, start_special
},
5302 { "emergency", EQUAL
, 1, start_special
},
5303 { "exit", EQUAL
, 1, start_special
},
5304 { "reset-failed", MORE
, 1, reset_failed
},
5305 { "enable", MORE
, 2, enable_unit
},
5306 { "disable", MORE
, 2, enable_unit
},
5307 { "is-enabled", MORE
, 2, enable_unit
}
5317 left
= argc
- optind
;
5320 /* Special rule: no arguments means "list-units" */
5323 if (streq(argv
[optind
], "help")) {
5328 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
5329 if (streq(argv
[optind
], verbs
[i
].verb
))
5332 if (i
>= ELEMENTSOF(verbs
)) {
5333 log_error("Unknown operation %s", argv
[optind
]);
5338 switch (verbs
[i
].argc_cmp
) {
5341 if (left
!= verbs
[i
].argc
) {
5342 log_error("Invalid number of arguments.");
5349 if (left
< verbs
[i
].argc
) {
5350 log_error("Too few arguments.");
5357 if (left
> verbs
[i
].argc
) {
5358 log_error("Too many arguments.");
5365 assert_not_reached("Unknown comparison operator.");
5368 /* Require a bus connection for all operations but
5370 if (!streq(verbs
[i
].verb
, "enable") && !streq(verbs
[i
].verb
, "disable")) {
5372 if (running_in_chroot() > 0) {
5373 log_info("Running in chroot, ignoring request.");
5378 log_error("Failed to get D-Bus connection: %s", error
->message
);
5383 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
5386 static int send_shutdownd(usec_t t
, char mode
, bool warn
, const char *message
) {
5388 struct msghdr msghdr
;
5390 union sockaddr_union sockaddr
;
5391 struct shutdownd_command c
;
5399 strncpy(c
.wall_message
, message
, sizeof(c
.wall_message
));
5401 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0)) < 0)
5405 sockaddr
.sa
.sa_family
= AF_UNIX
;
5406 sockaddr
.un
.sun_path
[0] = 0;
5407 strncpy(sockaddr
.un
.sun_path
, "/run/systemd/shutdownd", sizeof(sockaddr
.un
.sun_path
));
5410 iovec
.iov_base
= (char*) &c
;
5411 iovec
.iov_len
= sizeof(c
);
5414 msghdr
.msg_name
= &sockaddr
;
5415 msghdr
.msg_namelen
= offsetof(struct sockaddr_un
, sun_path
) + sizeof("/run/systemd/shutdownd") - 1;
5417 msghdr
.msg_iov
= &iovec
;
5418 msghdr
.msg_iovlen
= 1;
5420 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) < 0) {
5421 close_nointr_nofail(fd
);
5425 close_nointr_nofail(fd
);
5429 static int reload_with_fallback(DBusConnection
*bus
) {
5432 /* First, try systemd via D-Bus. */
5433 if (daemon_reload(bus
, NULL
, 0) > 0)
5437 /* Nothing else worked, so let's try signals */
5438 assert(arg_action
== ACTION_RELOAD
|| arg_action
== ACTION_REEXEC
);
5440 if (kill(1, arg_action
== ACTION_RELOAD
? SIGHUP
: SIGTERM
) < 0) {
5441 log_error("kill() failed: %m");
5448 static int start_with_fallback(DBusConnection
*bus
) {
5451 /* First, try systemd via D-Bus. */
5452 if (start_unit(bus
, NULL
, 0) >= 0)
5456 /* Hmm, talking to systemd via D-Bus didn't work. Then
5457 * let's try to talk to Upstart via D-Bus. */
5458 if (talk_upstart() > 0)
5461 /* Nothing else worked, so let's try
5463 if (talk_initctl() > 0)
5466 log_error("Failed to talk to init daemon.");
5470 warn_wall(arg_action
);
5474 static int halt_main(DBusConnection
*bus
) {
5477 if (geteuid() != 0) {
5478 log_error("Must be root.");
5484 char date
[FORMAT_TIMESTAMP_MAX
];
5486 m
= strv_join(arg_wall
, " ");
5487 r
= send_shutdownd(arg_when
,
5488 arg_action
== ACTION_HALT
? 'H' :
5489 arg_action
== ACTION_POWEROFF
? 'P' :
5496 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r
));
5498 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5499 format_timestamp(date
, sizeof(date
), arg_when
));
5504 if (!arg_dry
&& !arg_immediate
)
5505 return start_with_fallback(bus
);
5508 if (sd_booted() > 0)
5509 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5510 else if ((r
= utmp_put_shutdown(0)) < 0)
5511 log_warning("Failed to write utmp record: %s", strerror(-r
));
5520 /* Make sure C-A-D is handled by the kernel from this
5522 reboot(RB_ENABLE_CAD
);
5524 switch (arg_action
) {
5527 log_info("Halting.");
5528 reboot(RB_HALT_SYSTEM
);
5531 case ACTION_POWEROFF
:
5532 log_info("Powering off.");
5533 reboot(RB_POWER_OFF
);
5537 log_info("Rebooting.");
5538 reboot(RB_AUTOBOOT
);
5542 assert_not_reached("Unknown halt action.");
5545 /* We should never reach this. */
5549 static int runlevel_main(void) {
5550 int r
, runlevel
, previous
;
5552 if ((r
= utmp_get_runlevel(&runlevel
, &previous
)) < 0) {
5553 printf("unknown\n");
5558 previous
<= 0 ? 'N' : previous
,
5559 runlevel
<= 0 ? 'N' : runlevel
);
5564 static void pager_open(void) {
5572 if (!on_tty() || arg_no_pager
)
5575 if ((pager
= getenv("PAGER")))
5576 if (!*pager
|| streq(pager
, "cat"))
5579 /* Determine and cache number of columns before we spawn the
5580 * pager so that we get the value from the actual tty */
5584 log_error("Failed to create pager pipe: %m");
5588 parent_pid
= getpid();
5591 if (pager_pid
< 0) {
5592 log_error("Failed to fork pager: %m");
5597 /* In the child start the pager */
5598 if (pager_pid
== 0) {
5600 dup2(fd
[0], STDIN_FILENO
);
5603 setenv("LESS", "FRSX", 0);
5605 /* Make sure the pager goes away when the parent dies */
5606 if (prctl(PR_SET_PDEATHSIG
, SIGTERM
) < 0)
5607 _exit(EXIT_FAILURE
);
5609 /* Check whether our parent died before we were able
5610 * to set the death signal */
5611 if (getppid() != parent_pid
)
5612 _exit(EXIT_SUCCESS
);
5615 execlp(pager
, pager
, NULL
);
5616 execl("/bin/sh", "sh", "-c", pager
, NULL
);
5618 /* Debian's alternatives command for pagers is
5619 * called 'pager'. Note that we do not call
5620 * sensible-pagers here, since that is just a
5621 * shell script that implements a logic that
5622 * is similar to this one anyway, but is
5623 * Debian-specific. */
5624 execlp("pager", "pager", NULL
);
5626 execlp("less", "less", NULL
);
5627 execlp("more", "more", NULL
);
5630 log_error("Unable to execute pager: %m");
5631 _exit(EXIT_FAILURE
);
5634 /* Return in the parent */
5635 if (dup2(fd
[1], STDOUT_FILENO
) < 0)
5636 log_error("Failed to duplicate pager pipe: %m");
5641 static void pager_close(void) {
5647 /* Inform pager that we are done */
5649 kill(pager_pid
, SIGCONT
);
5650 wait_for_terminate(pager_pid
, &dummy
);
5654 static void agent_close(void) {
5660 /* Inform agent that we are done */
5661 kill(agent_pid
, SIGTERM
);
5662 kill(agent_pid
, SIGCONT
);
5663 wait_for_terminate(agent_pid
, &dummy
);
5667 int main(int argc
, char*argv
[]) {
5668 int r
, retval
= EXIT_FAILURE
;
5669 DBusConnection
*bus
= NULL
;
5672 dbus_error_init(&error
);
5674 log_parse_environment();
5677 if ((r
= parse_argv(argc
, argv
)) < 0)
5680 retval
= EXIT_SUCCESS
;
5684 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5685 * let's shortcut this */
5686 if (arg_action
== ACTION_RUNLEVEL
) {
5687 r
= runlevel_main();
5688 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5692 if (running_in_chroot() > 0 && arg_action
!= ACTION_SYSTEMCTL
) {
5693 log_info("Running in chroot, ignoring request.");
5698 if (arg_transport
== TRANSPORT_NORMAL
)
5699 bus_connect(arg_user
? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &bus
, &private_bus
, &error
);
5700 else if (arg_transport
== TRANSPORT_POLKIT
) {
5701 bus_connect_system_polkit(&bus
, &error
);
5702 private_bus
= false;
5703 } else if (arg_transport
== TRANSPORT_SSH
) {
5704 bus_connect_system_ssh(NULL
, arg_host
, &bus
, &error
);
5705 private_bus
= false;
5707 assert_not_reached("Uh, invalid transport...");
5709 switch (arg_action
) {
5711 case ACTION_SYSTEMCTL
:
5712 r
= systemctl_main(bus
, argc
, argv
, &error
);
5716 case ACTION_POWEROFF
:
5722 case ACTION_RUNLEVEL2
:
5723 case ACTION_RUNLEVEL3
:
5724 case ACTION_RUNLEVEL4
:
5725 case ACTION_RUNLEVEL5
:
5727 case ACTION_EMERGENCY
:
5728 case ACTION_DEFAULT
:
5729 r
= start_with_fallback(bus
);
5734 r
= reload_with_fallback(bus
);
5737 case ACTION_CANCEL_SHUTDOWN
:
5738 r
= send_shutdownd(0, 0, false, NULL
);
5741 case ACTION_INVALID
:
5742 case ACTION_RUNLEVEL
:
5744 assert_not_reached("Unknown action");
5747 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5752 dbus_connection_flush(bus
);
5753 dbus_connection_close(bus
);
5754 dbus_connection_unref(bus
);
5757 dbus_error_free(&error
);
5761 strv_free(arg_property
);