1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
37 #include <dbus/dbus.h>
43 #include "utmp-wtmp.h"
47 #include "dbus-common.h"
48 #include "cgroup-show.h"
49 #include "cgroup-util.h"
51 #include "path-lookup.h"
52 #include "conf-parser.h"
53 #include "sd-daemon.h"
54 #include "shutdownd.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
58 #include "unit-name.h"
60 static const char *arg_type
= NULL
;
61 static char **arg_property
= NULL
;
62 static bool arg_all
= false;
63 static const char *arg_job_mode
= "replace";
64 static bool arg_user
= false;
65 static bool arg_global
= false;
66 static bool arg_immediate
= false;
67 static bool arg_no_block
= false;
68 static bool arg_no_pager
= false;
69 static bool arg_no_wtmp
= false;
70 static bool arg_no_sync
= false;
71 static bool arg_no_wall
= false;
72 static bool arg_no_reload
= false;
73 static bool arg_dry
= false;
74 static bool arg_quiet
= false;
75 static bool arg_full
= false;
76 static bool arg_force
= false;
77 static bool arg_defaults
= false;
78 static bool arg_ask_password
= false;
79 static bool arg_failed
= false;
80 static char **arg_wall
= NULL
;
81 static const char *arg_kill_who
= NULL
;
82 static const char *arg_kill_mode
= NULL
;
83 static int arg_signal
= SIGTERM
;
84 static usec_t arg_when
= 0;
103 ACTION_CANCEL_SHUTDOWN
,
105 } arg_action
= ACTION_SYSTEMCTL
;
112 static bool private_bus
= false;
114 static pid_t pager_pid
= 0;
115 static pid_t agent_pid
= 0;
117 static int daemon_reload(DBusConnection
*bus
, char **args
, unsigned n
);
118 static void pager_open(void);
120 static bool on_tty(void) {
123 /* Note that this is invoked relatively early, before we start
124 * the pager. That means the value we return reflects whether
125 * we originally were started on a tty, not if we currently
126 * are. But this is intended, since we want colour and so on
127 * when run in our own pager. */
129 if (_unlikely_(t
< 0))
130 t
= isatty(STDOUT_FILENO
) > 0;
135 static void spawn_ask_password_agent(void) {
141 /* We check STDIN here, not STDOUT, since this is about input,
143 if (!isatty(STDIN_FILENO
))
146 if (!arg_ask_password
)
154 /* Spawns a temporary TTY agent, making sure it goes away when
157 if ((agent_pid
= fork()) < 0)
160 if (agent_pid
== 0) {
164 bool stdout_is_tty
, stderr_is_tty
;
166 /* Make sure the agent goes away when the parent dies */
167 if (prctl(PR_SET_PDEATHSIG
, SIGTERM
) < 0)
170 /* Check whether our parent died before we were able
171 * to set the death signal */
172 if (getppid() != parent
)
175 /* Don't leak fds to the agent */
176 close_all_fds(NULL
, 0);
178 stdout_is_tty
= isatty(STDOUT_FILENO
);
179 stderr_is_tty
= isatty(STDERR_FILENO
);
181 if (!stdout_is_tty
|| !stderr_is_tty
) {
182 /* Detach from stdout/stderr. and reopen
183 * /dev/tty for them. This is important to
184 * ensure that when systemctl is started via
185 * popen() or a similar call that expects to
186 * read EOF we actually do generate EOF and
187 * not delay this indefinitely by because we
188 * keep an unused copy of stdin around. */
189 if ((fd
= open("/dev/tty", O_WRONLY
)) < 0) {
190 log_error("Failed to open /dev/tty: %m");
195 dup2(fd
, STDOUT_FILENO
);
198 dup2(fd
, STDERR_FILENO
);
204 execl(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH
, SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH
, "--watch", NULL
);
206 log_error("Unable to execute agent: %m");
211 static const char *ansi_highlight(bool b
) {
216 return b
? ANSI_HIGHLIGHT_ON
: ANSI_HIGHLIGHT_OFF
;
219 static const char *ansi_highlight_green(bool b
) {
224 return b
? ANSI_HIGHLIGHT_GREEN_ON
: ANSI_HIGHLIGHT_OFF
;
227 static bool error_is_no_service(const DBusError
*error
) {
230 if (!dbus_error_is_set(error
))
233 if (dbus_error_has_name(error
, DBUS_ERROR_NAME_HAS_NO_OWNER
))
236 if (dbus_error_has_name(error
, DBUS_ERROR_SERVICE_UNKNOWN
))
239 return startswith(error
->name
, "org.freedesktop.DBus.Error.Spawn.");
242 static int translate_bus_error_to_exit_status(int r
, const DBusError
*error
) {
245 if (!dbus_error_is_set(error
))
248 if (dbus_error_has_name(error
, DBUS_ERROR_ACCESS_DENIED
) ||
249 dbus_error_has_name(error
, BUS_ERROR_ONLY_BY_DEPENDENCY
) ||
250 dbus_error_has_name(error
, BUS_ERROR_NO_ISOLATION
) ||
251 dbus_error_has_name(error
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
))
252 return EXIT_NOPERMISSION
;
254 if (dbus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
))
255 return EXIT_NOTINSTALLED
;
257 if (dbus_error_has_name(error
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
) ||
258 dbus_error_has_name(error
, BUS_ERROR_NOT_SUPPORTED
))
259 return EXIT_NOTIMPLEMENTED
;
261 if (dbus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
))
262 return EXIT_NOTCONFIGURED
;
270 static int bus_iter_get_basic_and_next(DBusMessageIter
*iter
, int type
, void *data
, bool next
) {
275 if (dbus_message_iter_get_arg_type(iter
) != type
)
278 dbus_message_iter_get_basic(iter
, data
);
280 if (!dbus_message_iter_next(iter
) != !next
)
286 static void warn_wall(enum action action
) {
287 static const char *table
[_ACTION_MAX
] = {
288 [ACTION_HALT
] = "The system is going down for system halt NOW!",
289 [ACTION_REBOOT
] = "The system is going down for reboot NOW!",
290 [ACTION_POWEROFF
] = "The system is going down for power-off NOW!",
291 [ACTION_KEXEC
] = "The system is going down for kexec reboot NOW!",
292 [ACTION_RESCUE
] = "The system is going down to rescue mode NOW!",
293 [ACTION_EMERGENCY
] = "The system is going down to emergency mode NOW!"
302 if (!(p
= strv_join(arg_wall
, " "))) {
303 log_error("Failed to join strings.");
319 utmp_wall(table
[action
], NULL
);
324 const char *description
;
325 const char *load_state
;
326 const char *active_state
;
327 const char *sub_state
;
328 const char *following
;
329 const char *unit_path
;
331 const char *job_type
;
332 const char *job_path
;
335 static int compare_unit_info(const void *a
, const void *b
) {
337 const struct unit_info
*u
= a
, *v
= b
;
339 d1
= strrchr(u
->id
, '.');
340 d2
= strrchr(v
->id
, '.');
345 if ((r
= strcasecmp(d1
, d2
)) != 0)
349 return strcasecmp(u
->id
, v
->id
);
352 static bool output_show_unit(const struct unit_info
*u
) {
356 return streq(u
->active_state
, "failed");
358 return (!arg_type
|| ((dot
= strrchr(u
->id
, '.')) &&
359 streq(dot
+1, arg_type
))) &&
360 (arg_all
|| !(streq(u
->active_state
, "inactive") || u
->following
[0]) || u
->job_id
> 0);
363 static void output_units_list(const struct unit_info
*unit_infos
, unsigned c
) {
364 unsigned active_len
, sub_len
, job_len
, n_shown
= 0;
365 const struct unit_info
*u
;
367 active_len
= sizeof("ACTIVE")-1;
368 sub_len
= sizeof("SUB")-1;
369 job_len
= sizeof("JOB")-1;
371 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
372 if (!output_show_unit(u
))
375 active_len
= MAX(active_len
, strlen(u
->active_state
));
376 sub_len
= MAX(sub_len
, strlen(u
->sub_state
));
378 job_len
= MAX(job_len
, strlen(u
->job_type
));
382 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
383 active_len
, "ACTIVE", sub_len
, "SUB", job_len
, "JOB");
384 if (columns() >= 80+12 || arg_full
)
385 printf(" %s\n", "DESCRIPTION");
390 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
393 const char *on_loaded
, *off_loaded
;
394 const char *on_active
, *off_active
;
396 if (!output_show_unit(u
))
401 if (!streq(u
->load_state
, "loaded") &&
402 !streq(u
->load_state
, "banned")) {
403 on_loaded
= ansi_highlight(true);
404 off_loaded
= ansi_highlight(false);
406 on_loaded
= off_loaded
= "";
408 if (streq(u
->active_state
, "failed")) {
409 on_active
= ansi_highlight(true);
410 off_active
= ansi_highlight(false);
412 on_active
= off_active
= "";
414 e
= arg_full
? NULL
: ellipsize(u
->id
, 25, 33);
416 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
418 on_loaded
, u
->load_state
, off_loaded
,
419 on_active
, active_len
, u
->active_state
,
420 sub_len
, u
->sub_state
, off_active
,
425 a
-= strlen(on_loaded
) + strlen(off_loaded
);
426 a
-= strlen(on_active
) + strlen(off_active
);
429 printf(" %-*s", job_len
, u
->job_type
);
433 if (a
+ b
+ 1 < columns()) {
435 printf(" %-*s", job_len
, "");
438 printf(" %s", u
->description
);
440 printf(" %.*s", columns() - a
- b
- 1, u
->description
);
447 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
448 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
449 "SUB = The low-level unit activation state, values depend on unit type.\n"
450 "JOB = Pending job for the unit.\n");
453 printf("\n%u units listed.\n", n_shown
);
455 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown
);
459 static int list_units(DBusConnection
*bus
, char **args
, unsigned n
) {
460 DBusMessage
*m
= NULL
, *reply
= NULL
;
463 DBusMessageIter iter
, sub
, sub2
;
464 unsigned c
= 0, n_units
= 0;
465 struct unit_info
*unit_infos
= NULL
;
467 dbus_error_init(&error
);
473 if (!(m
= dbus_message_new_method_call(
474 "org.freedesktop.systemd1",
475 "/org/freedesktop/systemd1",
476 "org.freedesktop.systemd1.Manager",
478 log_error("Could not allocate message.");
482 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
483 log_error("Failed to issue method call: %s", bus_error_message(&error
));
488 if (!dbus_message_iter_init(reply
, &iter
) ||
489 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
490 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
491 log_error("Failed to parse reply.");
496 dbus_message_iter_recurse(&iter
, &sub
);
498 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
501 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
502 log_error("Failed to parse reply.");
510 n_units
= MAX(2*c
, 16);
511 w
= realloc(unit_infos
, sizeof(struct unit_info
) * n_units
);
514 log_error("Failed to allocate unit array.");
524 dbus_message_iter_recurse(&sub
, &sub2
);
526 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->id
, true) < 0 ||
527 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->description
, true) < 0 ||
528 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->load_state
, true) < 0 ||
529 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->active_state
, true) < 0 ||
530 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->sub_state
, true) < 0 ||
531 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->following
, true) < 0 ||
532 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->unit_path
, true) < 0 ||
533 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &u
->job_id
, true) < 0 ||
534 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->job_type
, true) < 0 ||
535 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->job_path
, false) < 0) {
536 log_error("Failed to parse reply.");
541 dbus_message_iter_next(&sub
);
546 qsort(unit_infos
, c
, sizeof(struct unit_info
), compare_unit_info
);
547 output_units_list(unit_infos
, c
);
554 dbus_message_unref(m
);
557 dbus_message_unref(reply
);
561 dbus_error_free(&error
);
566 static int dot_one_property(const char *name
, const char *prop
, DBusMessageIter
*iter
) {
567 static const char * const colors
[] = {
568 "Requires", "[color=\"black\"]",
569 "RequiresOverridable", "[color=\"black\"]",
570 "Requisite", "[color=\"darkblue\"]",
571 "RequisiteOverridable", "[color=\"darkblue\"]",
572 "Wants", "[color=\"darkgrey\"]",
573 "Conflicts", "[color=\"red\"]",
574 "ConflictedBy", "[color=\"red\"]",
575 "After", "[color=\"green\"]"
578 const char *c
= NULL
;
585 for (i
= 0; i
< ELEMENTSOF(colors
); i
+= 2)
586 if (streq(colors
[i
], prop
)) {
594 if (arg_dot
!= DOT_ALL
)
595 if ((arg_dot
== DOT_ORDER
) != streq(prop
, "After"))
598 switch (dbus_message_iter_get_arg_type(iter
)) {
600 case DBUS_TYPE_ARRAY
:
602 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
605 dbus_message_iter_recurse(iter
, &sub
);
607 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
610 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
611 dbus_message_iter_get_basic(&sub
, &s
);
612 printf("\t\"%s\"->\"%s\" %s;\n", name
, s
, c
);
614 dbus_message_iter_next(&sub
);
624 static int dot_one(DBusConnection
*bus
, const char *name
, const char *path
) {
625 DBusMessage
*m
= NULL
, *reply
= NULL
;
626 const char *interface
= "org.freedesktop.systemd1.Unit";
629 DBusMessageIter iter
, sub
, sub2
, sub3
;
634 dbus_error_init(&error
);
636 if (!(m
= dbus_message_new_method_call(
637 "org.freedesktop.systemd1",
639 "org.freedesktop.DBus.Properties",
641 log_error("Could not allocate message.");
646 if (!dbus_message_append_args(m
,
647 DBUS_TYPE_STRING
, &interface
,
648 DBUS_TYPE_INVALID
)) {
649 log_error("Could not append arguments to message.");
654 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
655 log_error("Failed to issue method call: %s", bus_error_message(&error
));
660 if (!dbus_message_iter_init(reply
, &iter
) ||
661 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
662 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
663 log_error("Failed to parse reply.");
668 dbus_message_iter_recurse(&iter
, &sub
);
670 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
673 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
674 log_error("Failed to parse reply.");
679 dbus_message_iter_recurse(&sub
, &sub2
);
681 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &prop
, true) < 0) {
682 log_error("Failed to parse reply.");
687 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
688 log_error("Failed to parse reply.");
693 dbus_message_iter_recurse(&sub2
, &sub3
);
695 if (dot_one_property(name
, prop
, &sub3
)) {
696 log_error("Failed to parse reply.");
701 dbus_message_iter_next(&sub
);
708 dbus_message_unref(m
);
711 dbus_message_unref(reply
);
713 dbus_error_free(&error
);
718 static int dot(DBusConnection
*bus
, char **args
, unsigned n
) {
719 DBusMessage
*m
= NULL
, *reply
= NULL
;
722 DBusMessageIter iter
, sub
, sub2
;
724 dbus_error_init(&error
);
728 if (!(m
= dbus_message_new_method_call(
729 "org.freedesktop.systemd1",
730 "/org/freedesktop/systemd1",
731 "org.freedesktop.systemd1.Manager",
733 log_error("Could not allocate message.");
737 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
738 log_error("Failed to issue method call: %s", bus_error_message(&error
));
743 if (!dbus_message_iter_init(reply
, &iter
) ||
744 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
745 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
746 log_error("Failed to parse reply.");
751 printf("digraph systemd {\n");
753 dbus_message_iter_recurse(&iter
, &sub
);
754 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
755 const char *id
, *description
, *load_state
, *active_state
, *sub_state
, *following
, *unit_path
;
757 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
758 log_error("Failed to parse reply.");
763 dbus_message_iter_recurse(&sub
, &sub2
);
765 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &id
, true) < 0 ||
766 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &description
, true) < 0 ||
767 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &load_state
, true) < 0 ||
768 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &active_state
, true) < 0 ||
769 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &sub_state
, true) < 0 ||
770 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &following
, true) < 0 ||
771 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, true) < 0) {
772 log_error("Failed to parse reply.");
777 if ((r
= dot_one(bus
, id
, unit_path
)) < 0)
780 /* printf("\t\"%s\";\n", id); */
781 dbus_message_iter_next(&sub
);
786 log_info(" Color legend: black = Requires\n"
787 " dark blue = Requisite\n"
788 " dark grey = Wants\n"
792 if (isatty(fileno(stdout
)))
793 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
794 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
800 dbus_message_unref(m
);
803 dbus_message_unref(reply
);
805 dbus_error_free(&error
);
810 static int list_jobs(DBusConnection
*bus
, char **args
, unsigned n
) {
811 DBusMessage
*m
= NULL
, *reply
= NULL
;
814 DBusMessageIter iter
, sub
, sub2
;
817 dbus_error_init(&error
);
823 if (!(m
= dbus_message_new_method_call(
824 "org.freedesktop.systemd1",
825 "/org/freedesktop/systemd1",
826 "org.freedesktop.systemd1.Manager",
828 log_error("Could not allocate message.");
832 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
833 log_error("Failed to issue method call: %s", bus_error_message(&error
));
838 if (!dbus_message_iter_init(reply
, &iter
) ||
839 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
840 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
841 log_error("Failed to parse reply.");
846 dbus_message_iter_recurse(&iter
, &sub
);
848 if (isatty(STDOUT_FILENO
))
849 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
851 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
852 const char *name
, *type
, *state
, *job_path
, *unit_path
;
856 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
857 log_error("Failed to parse reply.");
862 dbus_message_iter_recurse(&sub
, &sub2
);
864 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &id
, true) < 0 ||
865 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0 ||
866 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) < 0 ||
867 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &state
, true) < 0 ||
868 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &job_path
, true) < 0 ||
869 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, false) < 0) {
870 log_error("Failed to parse reply.");
875 e
= arg_full
? NULL
: ellipsize(name
, 25, 33);
876 printf("%4u %-25s %-15s %-7s\n", id
, e
? e
: name
, type
, state
);
881 dbus_message_iter_next(&sub
);
884 if (isatty(STDOUT_FILENO
))
885 printf("\n%u jobs listed.\n", k
);
891 dbus_message_unref(m
);
894 dbus_message_unref(reply
);
896 dbus_error_free(&error
);
901 static int load_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
902 DBusMessage
*m
= NULL
, *reply
= NULL
;
907 dbus_error_init(&error
);
912 for (i
= 1; i
< n
; i
++) {
914 if (!(m
= dbus_message_new_method_call(
915 "org.freedesktop.systemd1",
916 "/org/freedesktop/systemd1",
917 "org.freedesktop.systemd1.Manager",
919 log_error("Could not allocate message.");
924 if (!dbus_message_append_args(m
,
925 DBUS_TYPE_STRING
, &args
[i
],
926 DBUS_TYPE_INVALID
)) {
927 log_error("Could not append arguments to message.");
932 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
933 log_error("Failed to issue method call: %s", bus_error_message(&error
));
938 dbus_message_unref(m
);
939 dbus_message_unref(reply
);
948 dbus_message_unref(m
);
951 dbus_message_unref(reply
);
953 dbus_error_free(&error
);
958 static int cancel_job(DBusConnection
*bus
, char **args
, unsigned n
) {
959 DBusMessage
*m
= NULL
, *reply
= NULL
;
964 dbus_error_init(&error
);
970 return daemon_reload(bus
, args
, n
);
972 for (i
= 1; i
< n
; i
++) {
976 if (!(m
= dbus_message_new_method_call(
977 "org.freedesktop.systemd1",
978 "/org/freedesktop/systemd1",
979 "org.freedesktop.systemd1.Manager",
981 log_error("Could not allocate message.");
986 if ((r
= safe_atou(args
[i
], &id
)) < 0) {
987 log_error("Failed to parse job id: %s", strerror(-r
));
991 assert_cc(sizeof(uint32_t) == sizeof(id
));
992 if (!dbus_message_append_args(m
,
993 DBUS_TYPE_UINT32
, &id
,
994 DBUS_TYPE_INVALID
)) {
995 log_error("Could not append arguments to message.");
1000 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1001 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1006 if (!dbus_message_get_args(reply
, &error
,
1007 DBUS_TYPE_OBJECT_PATH
, &path
,
1008 DBUS_TYPE_INVALID
)) {
1009 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1014 dbus_message_unref(m
);
1015 if (!(m
= dbus_message_new_method_call(
1016 "org.freedesktop.systemd1",
1018 "org.freedesktop.systemd1.Job",
1020 log_error("Could not allocate message.");
1025 dbus_message_unref(reply
);
1026 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1027 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1032 dbus_message_unref(m
);
1033 dbus_message_unref(reply
);
1041 dbus_message_unref(m
);
1044 dbus_message_unref(reply
);
1046 dbus_error_free(&error
);
1051 static bool need_daemon_reload(DBusConnection
*bus
, const char *unit
) {
1052 DBusMessage
*m
= NULL
, *reply
= NULL
;
1053 dbus_bool_t b
= FALSE
;
1054 DBusMessageIter iter
, sub
;
1056 *interface
= "org.freedesktop.systemd1.Unit",
1057 *property
= "NeedDaemonReload",
1060 /* We ignore all errors here, since this is used to show a warning only */
1062 if (!(m
= dbus_message_new_method_call(
1063 "org.freedesktop.systemd1",
1064 "/org/freedesktop/systemd1",
1065 "org.freedesktop.systemd1.Manager",
1069 if (!dbus_message_append_args(m
,
1070 DBUS_TYPE_STRING
, &unit
,
1074 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
)))
1077 if (!dbus_message_get_args(reply
, NULL
,
1078 DBUS_TYPE_OBJECT_PATH
, &path
,
1082 dbus_message_unref(m
);
1083 if (!(m
= dbus_message_new_method_call(
1084 "org.freedesktop.systemd1",
1086 "org.freedesktop.DBus.Properties",
1090 if (!dbus_message_append_args(m
,
1091 DBUS_TYPE_STRING
, &interface
,
1092 DBUS_TYPE_STRING
, &property
,
1093 DBUS_TYPE_INVALID
)) {
1097 dbus_message_unref(reply
);
1098 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
)))
1101 if (!dbus_message_iter_init(reply
, &iter
) ||
1102 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
)
1105 dbus_message_iter_recurse(&iter
, &sub
);
1107 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_BOOLEAN
)
1110 dbus_message_iter_get_basic(&sub
, &b
);
1114 dbus_message_unref(m
);
1117 dbus_message_unref(reply
);
1122 typedef struct WaitData
{
1127 static DBusHandlerResult
wait_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
1135 dbus_error_init(&error
);
1137 log_debug("Got D-Bus request: %s.%s() on %s",
1138 dbus_message_get_interface(message
),
1139 dbus_message_get_member(message
),
1140 dbus_message_get_path(message
));
1142 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
1143 log_error("Warning! D-Bus connection terminated.");
1144 dbus_connection_close(connection
);
1146 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1148 const char *path
, *result
;
1149 dbus_bool_t success
= true;
1151 if (dbus_message_get_args(message
, &error
,
1152 DBUS_TYPE_UINT32
, &id
,
1153 DBUS_TYPE_OBJECT_PATH
, &path
,
1154 DBUS_TYPE_STRING
, &result
,
1155 DBUS_TYPE_INVALID
)) {
1158 if ((p
= set_remove(d
->set
, (char*) path
)))
1162 d
->result
= strdup(result
);
1167 dbus_error_free(&error
);
1169 if (dbus_message_get_args(message
, &error
,
1170 DBUS_TYPE_UINT32
, &id
,
1171 DBUS_TYPE_OBJECT_PATH
, &path
,
1172 DBUS_TYPE_BOOLEAN
, &success
,
1173 DBUS_TYPE_INVALID
)) {
1176 /* Compatibility with older systemd versions <
1177 * 19 during upgrades. This should be dropped
1180 if ((p
= set_remove(d
->set
, (char*) path
)))
1184 d
->result
= strdup("failed");
1190 log_error("Failed to parse message: %s", bus_error_message(&error
));
1194 dbus_error_free(&error
);
1195 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1198 static int enable_wait_for_jobs(DBusConnection
*bus
) {
1206 dbus_error_init(&error
);
1207 dbus_bus_add_match(bus
,
1209 "sender='org.freedesktop.systemd1',"
1210 "interface='org.freedesktop.systemd1.Manager',"
1211 "member='JobRemoved',"
1212 "path='/org/freedesktop/systemd1'",
1215 if (dbus_error_is_set(&error
)) {
1216 log_error("Failed to add match: %s", bus_error_message(&error
));
1217 dbus_error_free(&error
);
1221 /* This is slightly dirty, since we don't undo the match registrations. */
1225 static int wait_for_jobs(DBusConnection
*bus
, Set
*s
) {
1235 if (!dbus_connection_add_filter(bus
, wait_filter
, &d
, NULL
)) {
1236 log_error("Failed to add filter.");
1241 while (!set_isempty(s
) &&
1242 dbus_connection_read_write_dispatch(bus
, -1))
1245 if (!arg_quiet
&& d
.result
) {
1246 if (streq(d
.result
, "timeout"))
1247 log_error("Job timed out.");
1248 else if (streq(d
.result
, "canceled"))
1249 log_error("Job canceled.");
1250 else if (streq(d
.result
, "dependency"))
1251 log_error("A dependency job failed. See system logs for details.");
1252 else if (!streq(d
.result
, "done"))
1253 log_error("Job failed. See system logs and 'systemctl status' for details.");
1256 if (streq_ptr(d
.result
, "timeout"))
1258 else if (streq_ptr(d
.result
, "canceled"))
1260 else if (!streq_ptr(d
.result
, "done"))
1268 /* This is slightly dirty, since we don't undo the filter registration. */
1273 static int start_unit_one(
1274 DBusConnection
*bus
,
1281 DBusMessage
*m
= NULL
, *reply
= NULL
;
1290 assert(arg_no_block
|| s
);
1292 if (!(m
= dbus_message_new_method_call(
1293 "org.freedesktop.systemd1",
1294 "/org/freedesktop/systemd1",
1295 "org.freedesktop.systemd1.Manager",
1297 log_error("Could not allocate message.");
1302 if (!dbus_message_append_args(m
,
1303 DBUS_TYPE_STRING
, &name
,
1304 DBUS_TYPE_STRING
, &mode
,
1305 DBUS_TYPE_INVALID
)) {
1306 log_error("Could not append arguments to message.");
1311 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, error
))) {
1313 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(error
)) {
1314 /* There's always a fallback possible for
1315 * legacy actions. */
1320 log_error("Failed to issue method call: %s", bus_error_message(error
));
1325 if (!dbus_message_get_args(reply
, error
,
1326 DBUS_TYPE_OBJECT_PATH
, &path
,
1327 DBUS_TYPE_INVALID
)) {
1328 log_error("Failed to parse reply: %s", bus_error_message(error
));
1333 if (need_daemon_reload(bus
, name
))
1334 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1335 arg_user
? "--user" : "--system");
1337 if (!arg_no_block
) {
1340 if (!(p
= strdup(path
))) {
1341 log_error("Failed to duplicate path.");
1346 if ((r
= set_put(s
, p
)) < 0) {
1348 log_error("Failed to add path to set.");
1357 dbus_message_unref(m
);
1360 dbus_message_unref(reply
);
1365 static enum action
verb_to_action(const char *verb
) {
1366 if (streq(verb
, "halt"))
1368 else if (streq(verb
, "poweroff"))
1369 return ACTION_POWEROFF
;
1370 else if (streq(verb
, "reboot"))
1371 return ACTION_REBOOT
;
1372 else if (streq(verb
, "kexec"))
1373 return ACTION_KEXEC
;
1374 else if (streq(verb
, "rescue"))
1375 return ACTION_RESCUE
;
1376 else if (streq(verb
, "emergency"))
1377 return ACTION_EMERGENCY
;
1378 else if (streq(verb
, "default"))
1379 return ACTION_DEFAULT
;
1380 else if (streq(verb
, "exit"))
1383 return ACTION_INVALID
;
1386 static int start_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1388 static const char * const table
[_ACTION_MAX
] = {
1389 [ACTION_HALT
] = SPECIAL_HALT_TARGET
,
1390 [ACTION_POWEROFF
] = SPECIAL_POWEROFF_TARGET
,
1391 [ACTION_REBOOT
] = SPECIAL_REBOOT_TARGET
,
1392 [ACTION_KEXEC
] = SPECIAL_KEXEC_TARGET
,
1393 [ACTION_RUNLEVEL2
] = SPECIAL_RUNLEVEL2_TARGET
,
1394 [ACTION_RUNLEVEL3
] = SPECIAL_RUNLEVEL3_TARGET
,
1395 [ACTION_RUNLEVEL4
] = SPECIAL_RUNLEVEL4_TARGET
,
1396 [ACTION_RUNLEVEL5
] = SPECIAL_RUNLEVEL5_TARGET
,
1397 [ACTION_RESCUE
] = SPECIAL_RESCUE_TARGET
,
1398 [ACTION_EMERGENCY
] = SPECIAL_EMERGENCY_TARGET
,
1399 [ACTION_DEFAULT
] = SPECIAL_DEFAULT_TARGET
,
1400 [ACTION_EXIT
] = SPECIAL_EXIT_TARGET
1405 const char *method
, *mode
, *one_name
;
1409 dbus_error_init(&error
);
1413 spawn_ask_password_agent();
1415 if (arg_action
== ACTION_SYSTEMCTL
) {
1417 streq(args
[0], "stop") ? "StopUnit" :
1418 streq(args
[0], "reload") ? "ReloadUnit" :
1419 streq(args
[0], "restart") ? "RestartUnit" :
1420 streq(args
[0], "try-restart") ||
1421 streq(args
[0], "condrestart") ? "TryRestartUnit" :
1422 streq(args
[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1423 streq(args
[0], "reload-or-try-restart") ||
1424 streq(args
[0], "condreload") ||
1425 streq(args
[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1429 (streq(args
[0], "isolate") ||
1430 streq(args
[0], "rescue") ||
1431 streq(args
[0], "emergency")) ? "isolate" : arg_job_mode
;
1433 one_name
= table
[verb_to_action(args
[0])];
1436 assert(arg_action
< ELEMENTSOF(table
));
1437 assert(table
[arg_action
]);
1439 method
= "StartUnit";
1441 mode
= (arg_action
== ACTION_EMERGENCY
||
1442 arg_action
== ACTION_RESCUE
||
1443 arg_action
== ACTION_RUNLEVEL2
||
1444 arg_action
== ACTION_RUNLEVEL3
||
1445 arg_action
== ACTION_RUNLEVEL4
||
1446 arg_action
== ACTION_RUNLEVEL5
) ? "isolate" : "replace";
1448 one_name
= table
[arg_action
];
1451 if (!arg_no_block
) {
1452 if ((ret
= enable_wait_for_jobs(bus
)) < 0) {
1453 log_error("Could not watch jobs: %s", strerror(-ret
));
1457 if (!(s
= set_new(string_hash_func
, string_compare_func
))) {
1458 log_error("Failed to allocate set.");
1465 if ((ret
= start_unit_one(bus
, method
, one_name
, mode
, &error
, s
)) <= 0)
1468 for (i
= 1; i
< n
; i
++)
1469 if ((r
= start_unit_one(bus
, method
, args
[i
], mode
, &error
, s
)) != 0) {
1470 ret
= translate_bus_error_to_exit_status(r
, &error
);
1471 dbus_error_free(&error
);
1476 if ((r
= wait_for_jobs(bus
, s
)) < 0) {
1485 dbus_error_free(&error
);
1490 static int start_special(DBusConnection
*bus
, char **args
, unsigned n
) {
1497 (streq(args
[0], "halt") ||
1498 streq(args
[0], "poweroff") ||
1499 streq(args
[0], "reboot") ||
1500 streq(args
[0], "kexec") ||
1501 streq(args
[0], "exit")))
1502 return daemon_reload(bus
, args
, n
);
1504 r
= start_unit(bus
, args
, n
);
1507 warn_wall(verb_to_action(args
[0]));
1512 static int check_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1513 DBusMessage
*m
= NULL
, *reply
= NULL
;
1515 *interface
= "org.freedesktop.systemd1.Unit",
1516 *property
= "ActiveState";
1517 int r
= 3; /* According to LSB: "program is not running" */
1524 dbus_error_init(&error
);
1526 for (i
= 1; i
< n
; i
++) {
1527 const char *path
= NULL
;
1529 DBusMessageIter iter
, sub
;
1531 if (!(m
= dbus_message_new_method_call(
1532 "org.freedesktop.systemd1",
1533 "/org/freedesktop/systemd1",
1534 "org.freedesktop.systemd1.Manager",
1536 log_error("Could not allocate message.");
1541 if (!dbus_message_append_args(m
,
1542 DBUS_TYPE_STRING
, &args
[i
],
1543 DBUS_TYPE_INVALID
)) {
1544 log_error("Could not append arguments to message.");
1549 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1551 /* Hmm, cannot figure out anything about this unit... */
1555 dbus_error_free(&error
);
1556 dbus_message_unref(m
);
1560 if (!dbus_message_get_args(reply
, &error
,
1561 DBUS_TYPE_OBJECT_PATH
, &path
,
1562 DBUS_TYPE_INVALID
)) {
1563 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1568 dbus_message_unref(m
);
1569 if (!(m
= dbus_message_new_method_call(
1570 "org.freedesktop.systemd1",
1572 "org.freedesktop.DBus.Properties",
1574 log_error("Could not allocate message.");
1579 if (!dbus_message_append_args(m
,
1580 DBUS_TYPE_STRING
, &interface
,
1581 DBUS_TYPE_STRING
, &property
,
1582 DBUS_TYPE_INVALID
)) {
1583 log_error("Could not append arguments to message.");
1588 dbus_message_unref(reply
);
1589 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1590 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1595 if (!dbus_message_iter_init(reply
, &iter
) ||
1596 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
1597 log_error("Failed to parse reply.");
1602 dbus_message_iter_recurse(&iter
, &sub
);
1604 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
1605 log_error("Failed to parse reply.");
1610 dbus_message_iter_get_basic(&sub
, &state
);
1615 if (streq(state
, "active") || streq(state
, "reloading"))
1618 dbus_message_unref(m
);
1619 dbus_message_unref(reply
);
1625 dbus_message_unref(m
);
1628 dbus_message_unref(reply
);
1630 dbus_error_free(&error
);
1635 static int kill_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1636 DBusMessage
*m
= NULL
, *reply
= NULL
;
1644 dbus_error_init(&error
);
1647 arg_kill_who
= "all";
1650 arg_kill_mode
= streq(arg_kill_who
, "all") ? "control-group" : "process";
1652 for (i
= 1; i
< n
; i
++) {
1654 if (!(m
= dbus_message_new_method_call(
1655 "org.freedesktop.systemd1",
1656 "/org/freedesktop/systemd1",
1657 "org.freedesktop.systemd1.Manager",
1659 log_error("Could not allocate message.");
1664 if (!dbus_message_append_args(m
,
1665 DBUS_TYPE_STRING
, &args
[i
],
1666 DBUS_TYPE_STRING
, &arg_kill_who
,
1667 DBUS_TYPE_STRING
, &arg_kill_mode
,
1668 DBUS_TYPE_INT32
, &arg_signal
,
1669 DBUS_TYPE_INVALID
)) {
1670 log_error("Could not append arguments to message.");
1675 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1676 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1677 dbus_error_free(&error
);
1681 dbus_message_unref(m
);
1684 dbus_message_unref(reply
);
1690 dbus_message_unref(m
);
1693 dbus_message_unref(reply
);
1695 dbus_error_free(&error
);
1700 typedef struct ExecStatusInfo
{
1708 usec_t start_timestamp
;
1709 usec_t exit_timestamp
;
1714 LIST_FIELDS(struct ExecStatusInfo
, exec
);
1717 static void exec_status_info_free(ExecStatusInfo
*i
) {
1726 static int exec_status_info_deserialize(DBusMessageIter
*sub
, ExecStatusInfo
*i
) {
1727 uint64_t start_timestamp
, exit_timestamp
;
1728 DBusMessageIter sub2
, sub3
;
1732 int32_t code
, status
;
1738 if (dbus_message_iter_get_arg_type(sub
) != DBUS_TYPE_STRUCT
)
1741 dbus_message_iter_recurse(sub
, &sub2
);
1743 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) < 0)
1746 if (!(i
->path
= strdup(path
)))
1749 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_ARRAY
||
1750 dbus_message_iter_get_element_type(&sub2
) != DBUS_TYPE_STRING
)
1754 dbus_message_iter_recurse(&sub2
, &sub3
);
1755 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
1756 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
1757 dbus_message_iter_next(&sub3
);
1762 if (!(i
->argv
= new0(char*, n
+1)))
1766 dbus_message_iter_recurse(&sub2
, &sub3
);
1767 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
1770 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
1771 dbus_message_iter_get_basic(&sub3
, &s
);
1772 dbus_message_iter_next(&sub3
);
1774 if (!(i
->argv
[n
++] = strdup(s
)))
1778 if (!dbus_message_iter_next(&sub2
) ||
1779 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, true) < 0 ||
1780 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &start_timestamp
, true) < 0 ||
1781 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &exit_timestamp
, true) < 0 ||
1782 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &pid
, true) < 0 ||
1783 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &code
, true) < 0 ||
1784 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &status
, false) < 0)
1788 i
->start_timestamp
= (usec_t
) start_timestamp
;
1789 i
->exit_timestamp
= (usec_t
) exit_timestamp
;
1790 i
->pid
= (pid_t
) pid
;
1797 typedef struct UnitStatusInfo
{
1799 const char *load_state
;
1800 const char *active_state
;
1801 const char *sub_state
;
1803 const char *description
;
1804 const char *following
;
1807 const char *default_control_group
;
1809 usec_t inactive_exit_timestamp
;
1810 usec_t active_enter_timestamp
;
1811 usec_t active_exit_timestamp
;
1812 usec_t inactive_enter_timestamp
;
1814 bool need_daemon_reload
;
1819 const char *status_text
;
1821 #ifdef HAVE_SYSV_COMPAT
1825 usec_t start_timestamp
;
1826 usec_t exit_timestamp
;
1828 int exit_code
, exit_status
;
1831 unsigned n_accepted
;
1832 unsigned n_connections
;
1836 const char *sysfs_path
;
1838 /* Mount, Automount */
1844 LIST_HEAD(ExecStatusInfo
, exec
);
1847 static void print_status_info(UnitStatusInfo
*i
) {
1849 const char *on
, *off
, *ss
;
1851 char since1
[FORMAT_TIMESTAMP_PRETTY_MAX
], *s1
;
1852 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
1856 /* This shows pretty information about a unit. See
1857 * print_property() for a low-level property printer */
1859 printf("%s", strna(i
->id
));
1861 if (i
->description
&& !streq_ptr(i
->id
, i
->description
))
1862 printf(" - %s", i
->description
);
1867 printf("\t Follow: unit currently follows state of %s\n", i
->following
);
1869 if (streq_ptr(i
->load_state
, "failed") ||
1870 streq_ptr(i
->load_state
, "banned")) {
1871 on
= ansi_highlight(true);
1872 off
= ansi_highlight(false);
1877 printf("\t Loaded: %s%s%s (%s)\n", on
, strna(i
->load_state
), off
, i
->path
);
1879 printf("\t Loaded: %s%s%s\n", on
, strna(i
->load_state
), off
);
1881 ss
= streq_ptr(i
->active_state
, i
->sub_state
) ? NULL
: i
->sub_state
;
1883 if (streq_ptr(i
->active_state
, "failed")) {
1884 on
= ansi_highlight(true);
1885 off
= ansi_highlight(false);
1886 } else if (streq_ptr(i
->active_state
, "active") || streq_ptr(i
->active_state
, "reloading")) {
1887 on
= ansi_highlight_green(true);
1888 off
= ansi_highlight_green(false);
1893 printf("\t Active: %s%s (%s)%s",
1895 strna(i
->active_state
),
1899 printf("\t Active: %s%s%s",
1901 strna(i
->active_state
),
1904 timestamp
= (streq_ptr(i
->active_state
, "active") ||
1905 streq_ptr(i
->active_state
, "reloading")) ? i
->active_enter_timestamp
:
1906 (streq_ptr(i
->active_state
, "inactive") ||
1907 streq_ptr(i
->active_state
, "failed")) ? i
->inactive_enter_timestamp
:
1908 streq_ptr(i
->active_state
, "activating") ? i
->inactive_exit_timestamp
:
1909 i
->active_exit_timestamp
;
1911 s1
= format_timestamp_pretty(since1
, sizeof(since1
), timestamp
);
1912 s2
= format_timestamp(since2
, sizeof(since2
), timestamp
);
1915 printf(" since %s; %s\n", s2
, s1
);
1917 printf(" since %s\n", s2
);
1922 printf("\t Device: %s\n", i
->sysfs_path
);
1924 printf("\t Where: %s\n", i
->where
);
1926 printf("\t What: %s\n", i
->what
);
1929 printf("\tAccepted: %u; Connected: %u\n", i
->n_accepted
, i
->n_connections
);
1931 LIST_FOREACH(exec
, p
, i
->exec
) {
1935 /* Only show exited processes here */
1939 t
= strv_join(p
->argv
, " ");
1940 printf("\t Process: %u %s=%s ", p
->pid
, p
->name
, strna(t
));
1943 #ifdef HAVE_SYSV_COMPAT
1945 good
= is_clean_exit_lsb(p
->code
, p
->status
);
1948 good
= is_clean_exit(p
->code
, p
->status
);
1951 on
= ansi_highlight(true);
1952 off
= ansi_highlight(false);
1956 printf("%s(code=%s, ", on
, sigchld_code_to_string(p
->code
));
1958 if (p
->code
== CLD_EXITED
) {
1961 printf("status=%i", p
->status
);
1963 #ifdef HAVE_SYSV_COMPAT
1964 if ((c
= exit_status_to_string(p
->status
, i
->is_sysv
? EXIT_STATUS_LSB
: EXIT_STATUS_SYSTEMD
)))
1966 if ((c
= exit_status_to_string(p
->status
, EXIT_STATUS_SYSTEMD
)))
1971 printf("signal=%s", signal_to_string(p
->status
));
1973 printf(")%s\n", off
);
1977 if (i
->main_pid
== p
->pid
&&
1978 i
->start_timestamp
== p
->start_timestamp
&&
1979 i
->exit_timestamp
== p
->start_timestamp
)
1980 /* Let's not show this twice */
1983 if (p
->pid
== i
->control_pid
)
1987 if (i
->main_pid
> 0 || i
->control_pid
> 0) {
1990 if (i
->main_pid
> 0) {
1991 printf("Main PID: %u", (unsigned) i
->main_pid
);
1995 get_process_name(i
->main_pid
, &t
);
2000 } else if (i
->exit_code
> 0) {
2001 printf(" (code=%s, ", sigchld_code_to_string(i
->exit_code
));
2003 if (i
->exit_code
== CLD_EXITED
) {
2006 printf("status=%i", i
->exit_status
);
2008 #ifdef HAVE_SYSV_COMPAT
2009 if ((c
= exit_status_to_string(i
->exit_status
, i
->is_sysv
? EXIT_STATUS_LSB
: EXIT_STATUS_SYSTEMD
)))
2011 if ((c
= exit_status_to_string(i
->exit_status
, EXIT_STATUS_SYSTEMD
)))
2016 printf("signal=%s", signal_to_string(i
->exit_status
));
2021 if (i
->main_pid
> 0 && i
->control_pid
> 0)
2024 if (i
->control_pid
> 0) {
2027 printf(" Control: %u", (unsigned) i
->control_pid
);
2029 get_process_name(i
->control_pid
, &t
);
2040 printf("\t Status: \"%s\"\n", i
->status_text
);
2042 if (i
->default_control_group
) {
2045 printf("\t CGroup: %s\n", i
->default_control_group
);
2047 if ((c
= columns()) > 18)
2052 show_cgroup_by_path(i
->default_control_group
, "\t\t ", c
);
2055 if (i
->need_daemon_reload
)
2056 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2057 ansi_highlight(true),
2058 ansi_highlight(false),
2059 arg_user
? "--user" : "--system");
2062 static int status_property(const char *name
, DBusMessageIter
*iter
, UnitStatusInfo
*i
) {
2064 switch (dbus_message_iter_get_arg_type(iter
)) {
2066 case DBUS_TYPE_STRING
: {
2069 dbus_message_iter_get_basic(iter
, &s
);
2072 if (streq(name
, "Id"))
2074 else if (streq(name
, "LoadState"))
2076 else if (streq(name
, "ActiveState"))
2077 i
->active_state
= s
;
2078 else if (streq(name
, "SubState"))
2080 else if (streq(name
, "Description"))
2082 else if (streq(name
, "FragmentPath"))
2084 #ifdef HAVE_SYSV_COMPAT
2085 else if (streq(name
, "SysVPath")) {
2090 else if (streq(name
, "DefaultControlGroup"))
2091 i
->default_control_group
= s
;
2092 else if (streq(name
, "StatusText"))
2094 else if (streq(name
, "SysFSPath"))
2096 else if (streq(name
, "Where"))
2098 else if (streq(name
, "What"))
2100 else if (streq(name
, "Following"))
2107 case DBUS_TYPE_BOOLEAN
: {
2110 dbus_message_iter_get_basic(iter
, &b
);
2112 if (streq(name
, "Accept"))
2114 else if (streq(name
, "NeedDaemonReload"))
2115 i
->need_daemon_reload
= b
;
2120 case DBUS_TYPE_UINT32
: {
2123 dbus_message_iter_get_basic(iter
, &u
);
2125 if (streq(name
, "MainPID")) {
2127 i
->main_pid
= (pid_t
) u
;
2130 } else if (streq(name
, "ControlPID"))
2131 i
->control_pid
= (pid_t
) u
;
2132 else if (streq(name
, "ExecMainPID")) {
2134 i
->main_pid
= (pid_t
) u
;
2135 } else if (streq(name
, "NAccepted"))
2137 else if (streq(name
, "NConnections"))
2138 i
->n_connections
= u
;
2143 case DBUS_TYPE_INT32
: {
2146 dbus_message_iter_get_basic(iter
, &j
);
2148 if (streq(name
, "ExecMainCode"))
2149 i
->exit_code
= (int) j
;
2150 else if (streq(name
, "ExecMainStatus"))
2151 i
->exit_status
= (int) j
;
2156 case DBUS_TYPE_UINT64
: {
2159 dbus_message_iter_get_basic(iter
, &u
);
2161 if (streq(name
, "ExecMainStartTimestamp"))
2162 i
->start_timestamp
= (usec_t
) u
;
2163 else if (streq(name
, "ExecMainExitTimestamp"))
2164 i
->exit_timestamp
= (usec_t
) u
;
2165 else if (streq(name
, "ActiveEnterTimestamp"))
2166 i
->active_enter_timestamp
= (usec_t
) u
;
2167 else if (streq(name
, "InactiveEnterTimestamp"))
2168 i
->inactive_enter_timestamp
= (usec_t
) u
;
2169 else if (streq(name
, "InactiveExitTimestamp"))
2170 i
->inactive_exit_timestamp
= (usec_t
) u
;
2171 else if (streq(name
, "ActiveExitTimestamp"))
2172 i
->active_exit_timestamp
= (usec_t
) u
;
2177 case DBUS_TYPE_ARRAY
: {
2179 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&&
2180 startswith(name
, "Exec")) {
2181 DBusMessageIter sub
;
2183 dbus_message_iter_recurse(iter
, &sub
);
2184 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2185 ExecStatusInfo
*info
;
2188 if (!(info
= new0(ExecStatusInfo
, 1)))
2191 if (!(info
->name
= strdup(name
))) {
2196 if ((r
= exec_status_info_deserialize(&sub
, info
)) < 0) {
2201 LIST_PREPEND(ExecStatusInfo
, exec
, i
->exec
, info
);
2203 dbus_message_iter_next(&sub
);
2214 static int print_property(const char *name
, DBusMessageIter
*iter
) {
2218 /* This is a low-level property printer, see
2219 * print_status_info() for the nicer output */
2221 if (arg_property
&& !strv_find(arg_property
, name
))
2224 switch (dbus_message_iter_get_arg_type(iter
)) {
2226 case DBUS_TYPE_STRING
: {
2228 dbus_message_iter_get_basic(iter
, &s
);
2230 if (arg_all
|| s
[0])
2231 printf("%s=%s\n", name
, s
);
2236 case DBUS_TYPE_BOOLEAN
: {
2238 dbus_message_iter_get_basic(iter
, &b
);
2239 printf("%s=%s\n", name
, yes_no(b
));
2244 case DBUS_TYPE_UINT64
: {
2246 dbus_message_iter_get_basic(iter
, &u
);
2248 /* Yes, heuristics! But we can change this check
2249 * should it turn out to not be sufficient */
2251 if (strstr(name
, "Timestamp")) {
2252 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
2254 if ((t
= format_timestamp(timestamp
, sizeof(timestamp
), u
)) || arg_all
)
2255 printf("%s=%s\n", name
, strempty(t
));
2256 } else if (strstr(name
, "USec")) {
2257 char timespan
[FORMAT_TIMESPAN_MAX
];
2259 printf("%s=%s\n", name
, format_timespan(timespan
, sizeof(timespan
), u
));
2261 printf("%s=%llu\n", name
, (unsigned long long) u
);
2266 case DBUS_TYPE_UINT32
: {
2268 dbus_message_iter_get_basic(iter
, &u
);
2270 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
2271 printf("%s=%04o\n", name
, u
);
2273 printf("%s=%u\n", name
, (unsigned) u
);
2278 case DBUS_TYPE_INT32
: {
2280 dbus_message_iter_get_basic(iter
, &i
);
2282 printf("%s=%i\n", name
, (int) i
);
2286 case DBUS_TYPE_DOUBLE
: {
2288 dbus_message_iter_get_basic(iter
, &d
);
2290 printf("%s=%g\n", name
, d
);
2294 case DBUS_TYPE_STRUCT
: {
2295 DBusMessageIter sub
;
2296 dbus_message_iter_recurse(iter
, &sub
);
2298 if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_UINT32
&& streq(name
, "Job")) {
2301 dbus_message_iter_get_basic(&sub
, &u
);
2304 printf("%s=%u\n", name
, (unsigned) u
);
2306 printf("%s=\n", name
);
2309 } else if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
&& streq(name
, "Unit")) {
2312 dbus_message_iter_get_basic(&sub
, &s
);
2314 if (arg_all
|| s
[0])
2315 printf("%s=%s\n", name
, s
);
2323 case DBUS_TYPE_ARRAY
:
2325 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
2326 DBusMessageIter sub
;
2329 dbus_message_iter_recurse(iter
, &sub
);
2331 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2332 printf("%s=", name
);
2334 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2337 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
2338 dbus_message_iter_get_basic(&sub
, &s
);
2339 printf("%s%s", space
? " " : "", s
);
2342 dbus_message_iter_next(&sub
);
2350 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_BYTE
) {
2351 DBusMessageIter sub
;
2353 dbus_message_iter_recurse(iter
, &sub
);
2355 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2356 printf("%s=", name
);
2358 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2361 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_BYTE
);
2362 dbus_message_iter_get_basic(&sub
, &u
);
2365 dbus_message_iter_next(&sub
);
2373 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "EnvironmentFiles")) {
2374 DBusMessageIter sub
, sub2
;
2376 dbus_message_iter_recurse(iter
, &sub
);
2377 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2381 dbus_message_iter_recurse(&sub
, &sub2
);
2383 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) >= 0 &&
2384 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, false) >= 0)
2385 printf("EnvironmentFile=%s (ignore=%s)\n", path
, yes_no(ignore
));
2387 dbus_message_iter_next(&sub
);
2392 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Paths")) {
2393 DBusMessageIter sub
, sub2
;
2395 dbus_message_iter_recurse(iter
, &sub
);
2396 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2397 const char *type
, *path
;
2399 dbus_message_iter_recurse(&sub
, &sub2
);
2401 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) >= 0 &&
2402 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, false) >= 0)
2403 printf("%s=%s\n", type
, path
);
2405 dbus_message_iter_next(&sub
);
2410 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Timers")) {
2411 DBusMessageIter sub
, sub2
;
2413 dbus_message_iter_recurse(iter
, &sub
);
2414 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2416 uint64_t value
, next_elapse
;
2418 dbus_message_iter_recurse(&sub
, &sub2
);
2420 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &base
, true) >= 0 &&
2421 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &value
, true) >= 0 &&
2422 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &next_elapse
, false) >= 0) {
2423 char timespan1
[FORMAT_TIMESPAN_MAX
], timespan2
[FORMAT_TIMESPAN_MAX
];
2425 printf("%s={ value=%s ; next_elapse=%s }\n",
2427 format_timespan(timespan1
, sizeof(timespan1
), value
),
2428 format_timespan(timespan2
, sizeof(timespan2
), next_elapse
));
2431 dbus_message_iter_next(&sub
);
2436 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& startswith(name
, "Exec")) {
2437 DBusMessageIter sub
;
2439 dbus_message_iter_recurse(iter
, &sub
);
2440 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2441 ExecStatusInfo info
;
2444 if (exec_status_info_deserialize(&sub
, &info
) >= 0) {
2445 char timestamp1
[FORMAT_TIMESTAMP_MAX
], timestamp2
[FORMAT_TIMESTAMP_MAX
];
2448 t
= strv_join(info
.argv
, " ");
2450 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2454 yes_no(info
.ignore
),
2455 strna(format_timestamp(timestamp1
, sizeof(timestamp1
), info
.start_timestamp
)),
2456 strna(format_timestamp(timestamp2
, sizeof(timestamp2
), info
.exit_timestamp
)),
2457 (unsigned) info
. pid
,
2458 sigchld_code_to_string(info
.code
),
2460 info
.code
== CLD_EXITED
? "" : "/",
2461 strempty(info
.code
== CLD_EXITED
? NULL
: signal_to_string(info
.status
)));
2467 strv_free(info
.argv
);
2469 dbus_message_iter_next(&sub
);
2479 printf("%s=[unprintable]\n", name
);
2484 static int show_one(const char *verb
, DBusConnection
*bus
, const char *path
, bool show_properties
, bool *new_line
) {
2485 DBusMessage
*m
= NULL
, *reply
= NULL
;
2486 const char *interface
= "";
2489 DBusMessageIter iter
, sub
, sub2
, sub3
;
2490 UnitStatusInfo info
;
2498 dbus_error_init(&error
);
2500 if (!(m
= dbus_message_new_method_call(
2501 "org.freedesktop.systemd1",
2503 "org.freedesktop.DBus.Properties",
2505 log_error("Could not allocate message.");
2510 if (!dbus_message_append_args(m
,
2511 DBUS_TYPE_STRING
, &interface
,
2512 DBUS_TYPE_INVALID
)) {
2513 log_error("Could not append arguments to message.");
2518 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2519 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2524 if (!dbus_message_iter_init(reply
, &iter
) ||
2525 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
2526 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
2527 log_error("Failed to parse reply.");
2532 dbus_message_iter_recurse(&iter
, &sub
);
2539 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2542 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
2543 log_error("Failed to parse reply.");
2548 dbus_message_iter_recurse(&sub
, &sub2
);
2550 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0) {
2551 log_error("Failed to parse reply.");
2556 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
2557 log_error("Failed to parse reply.");
2562 dbus_message_iter_recurse(&sub2
, &sub3
);
2564 if (show_properties
)
2565 r
= print_property(name
, &sub3
);
2567 r
= status_property(name
, &sub3
, &info
);
2570 log_error("Failed to parse reply.");
2575 dbus_message_iter_next(&sub
);
2580 if (!show_properties
)
2581 print_status_info(&info
);
2583 if (!streq_ptr(info
.active_state
, "active") &&
2584 !streq_ptr(info
.active_state
, "reloading") &&
2585 streq(verb
, "status"))
2586 /* According to LSB: "program not running" */
2589 while ((p
= info
.exec
)) {
2590 LIST_REMOVE(ExecStatusInfo
, exec
, info
.exec
, p
);
2591 exec_status_info_free(p
);
2596 dbus_message_unref(m
);
2599 dbus_message_unref(reply
);
2601 dbus_error_free(&error
);
2606 static int show(DBusConnection
*bus
, char **args
, unsigned n
) {
2607 DBusMessage
*m
= NULL
, *reply
= NULL
;
2611 bool show_properties
, new_line
= false;
2616 dbus_error_init(&error
);
2618 show_properties
= !streq(args
[0], "status");
2620 if (show_properties
)
2623 if (show_properties
&& n
<= 1) {
2624 /* If not argument is specified inspect the manager
2627 ret
= show_one(args
[0], bus
, "/org/freedesktop/systemd1", show_properties
, &new_line
);
2631 for (i
= 1; i
< n
; i
++) {
2632 const char *path
= NULL
;
2635 if (safe_atou32(args
[i
], &id
) < 0) {
2637 /* Interpret as unit name */
2639 if (!(m
= dbus_message_new_method_call(
2640 "org.freedesktop.systemd1",
2641 "/org/freedesktop/systemd1",
2642 "org.freedesktop.systemd1.Manager",
2644 log_error("Could not allocate message.");
2649 if (!dbus_message_append_args(m
,
2650 DBUS_TYPE_STRING
, &args
[i
],
2651 DBUS_TYPE_INVALID
)) {
2652 log_error("Could not append arguments to message.");
2657 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2659 if (!dbus_error_has_name(&error
, DBUS_ERROR_ACCESS_DENIED
)) {
2660 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2665 dbus_error_free(&error
);
2667 dbus_message_unref(m
);
2668 if (!(m
= dbus_message_new_method_call(
2669 "org.freedesktop.systemd1",
2670 "/org/freedesktop/systemd1",
2671 "org.freedesktop.systemd1.Manager",
2673 log_error("Could not allocate message.");
2678 if (!dbus_message_append_args(m
,
2679 DBUS_TYPE_STRING
, &args
[i
],
2680 DBUS_TYPE_INVALID
)) {
2681 log_error("Could not append arguments to message.");
2686 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2687 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2689 if (dbus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
))
2690 ret
= 4; /* According to LSB: "program or service status is unknown" */
2697 } else if (show_properties
) {
2699 /* Interpret as job id */
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_UINT32
, &id
,
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
));
2726 /* Interpret as PID */
2728 if (!(m
= dbus_message_new_method_call(
2729 "org.freedesktop.systemd1",
2730 "/org/freedesktop/systemd1",
2731 "org.freedesktop.systemd1.Manager",
2733 log_error("Could not allocate message.");
2738 if (!dbus_message_append_args(m
,
2739 DBUS_TYPE_UINT32
, &id
,
2740 DBUS_TYPE_INVALID
)) {
2741 log_error("Could not append arguments to message.");
2746 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2747 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2753 if (!dbus_message_get_args(reply
, &error
,
2754 DBUS_TYPE_OBJECT_PATH
, &path
,
2755 DBUS_TYPE_INVALID
)) {
2756 log_error("Failed to parse reply: %s", bus_error_message(&error
));
2761 if ((r
= show_one(args
[0], bus
, path
, show_properties
, &new_line
)) != 0)
2764 dbus_message_unref(m
);
2765 dbus_message_unref(reply
);
2771 dbus_message_unref(m
);
2774 dbus_message_unref(reply
);
2776 dbus_error_free(&error
);
2781 static DBusHandlerResult
monitor_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
2783 DBusMessage
*m
= NULL
, *reply
= NULL
;
2788 dbus_error_init(&error
);
2790 log_debug("Got D-Bus request: %s.%s() on %s",
2791 dbus_message_get_interface(message
),
2792 dbus_message_get_member(message
),
2793 dbus_message_get_path(message
));
2795 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
2796 log_error("Warning! D-Bus connection terminated.");
2797 dbus_connection_close(connection
);
2799 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2800 dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2801 const char *id
, *path
;
2803 if (!dbus_message_get_args(message
, &error
,
2804 DBUS_TYPE_STRING
, &id
,
2805 DBUS_TYPE_OBJECT_PATH
, &path
,
2807 log_error("Failed to parse message: %s", bus_error_message(&error
));
2808 else if (streq(dbus_message_get_member(message
), "UnitNew"))
2809 printf("Unit %s added.\n", id
);
2811 printf("Unit %s removed.\n", id
);
2813 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobNew") ||
2814 dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2816 const char *path
, *result
;
2818 if (!dbus_message_get_args(message
, &error
,
2819 DBUS_TYPE_UINT32
, &id
,
2820 DBUS_TYPE_OBJECT_PATH
, &path
,
2821 DBUS_TYPE_STRING
, &result
,
2823 log_error("Failed to parse message: %s", bus_error_message(&error
));
2824 else if (streq(dbus_message_get_member(message
), "JobNew"))
2825 printf("Job %u added.\n", id
);
2827 printf("Job %u removed.\n", id
);
2830 } else if (dbus_message_is_signal(message
, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2832 const char *path
, *interface
, *property
= "Id";
2833 DBusMessageIter iter
, sub
;
2835 path
= dbus_message_get_path(message
);
2837 if (!dbus_message_get_args(message
, &error
,
2838 DBUS_TYPE_STRING
, &interface
,
2839 DBUS_TYPE_INVALID
)) {
2840 log_error("Failed to parse message: %s", bus_error_message(&error
));
2844 if (!streq(interface
, "org.freedesktop.systemd1.Job") &&
2845 !streq(interface
, "org.freedesktop.systemd1.Unit"))
2848 if (!(m
= dbus_message_new_method_call(
2849 "org.freedesktop.systemd1",
2851 "org.freedesktop.DBus.Properties",
2853 log_error("Could not allocate message.");
2857 if (!dbus_message_append_args(m
,
2858 DBUS_TYPE_STRING
, &interface
,
2859 DBUS_TYPE_STRING
, &property
,
2860 DBUS_TYPE_INVALID
)) {
2861 log_error("Could not append arguments to message.");
2865 if (!(reply
= dbus_connection_send_with_reply_and_block(connection
, m
, -1, &error
))) {
2866 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2870 if (!dbus_message_iter_init(reply
, &iter
) ||
2871 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
2872 log_error("Failed to parse reply.");
2876 dbus_message_iter_recurse(&iter
, &sub
);
2878 if (streq(interface
, "org.freedesktop.systemd1.Unit")) {
2881 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
2882 log_error("Failed to parse reply.");
2886 dbus_message_iter_get_basic(&sub
, &id
);
2887 printf("Unit %s changed.\n", id
);
2891 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_UINT32
) {
2892 log_error("Failed to parse reply.");
2896 dbus_message_iter_get_basic(&sub
, &id
);
2897 printf("Job %u changed.\n", id
);
2903 dbus_message_unref(m
);
2906 dbus_message_unref(reply
);
2908 dbus_error_free(&error
);
2909 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
2913 dbus_message_unref(m
);
2916 dbus_message_unref(reply
);
2918 dbus_error_free(&error
);
2919 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
2922 static int monitor(DBusConnection
*bus
, char **args
, unsigned n
) {
2923 DBusMessage
*m
= NULL
, *reply
= NULL
;
2927 dbus_error_init(&error
);
2930 dbus_bus_add_match(bus
,
2932 "sender='org.freedesktop.systemd1',"
2933 "interface='org.freedesktop.systemd1.Manager',"
2934 "path='/org/freedesktop/systemd1'",
2937 if (dbus_error_is_set(&error
)) {
2938 log_error("Failed to add match: %s", bus_error_message(&error
));
2943 dbus_bus_add_match(bus
,
2945 "sender='org.freedesktop.systemd1',"
2946 "interface='org.freedesktop.DBus.Properties',"
2947 "member='PropertiesChanged'",
2950 if (dbus_error_is_set(&error
)) {
2951 log_error("Failed to add match: %s", bus_error_message(&error
));
2957 if (!dbus_connection_add_filter(bus
, monitor_filter
, NULL
, NULL
)) {
2958 log_error("Failed to add filter.");
2963 if (!(m
= dbus_message_new_method_call(
2964 "org.freedesktop.systemd1",
2965 "/org/freedesktop/systemd1",
2966 "org.freedesktop.systemd1.Manager",
2968 log_error("Could not allocate message.");
2973 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2974 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2979 while (dbus_connection_read_write_dispatch(bus
, -1))
2986 /* This is slightly dirty, since we don't undo the filter or the matches. */
2989 dbus_message_unref(m
);
2992 dbus_message_unref(reply
);
2994 dbus_error_free(&error
);
2999 static int dump(DBusConnection
*bus
, char **args
, unsigned n
) {
3000 DBusMessage
*m
= NULL
, *reply
= NULL
;
3005 dbus_error_init(&error
);
3009 if (!(m
= dbus_message_new_method_call(
3010 "org.freedesktop.systemd1",
3011 "/org/freedesktop/systemd1",
3012 "org.freedesktop.systemd1.Manager",
3014 log_error("Could not allocate message.");
3018 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3019 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3024 if (!dbus_message_get_args(reply
, &error
,
3025 DBUS_TYPE_STRING
, &text
,
3026 DBUS_TYPE_INVALID
)) {
3027 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3032 fputs(text
, stdout
);
3038 dbus_message_unref(m
);
3041 dbus_message_unref(reply
);
3043 dbus_error_free(&error
);
3048 static int snapshot(DBusConnection
*bus
, char **args
, unsigned n
) {
3049 DBusMessage
*m
= NULL
, *reply
= NULL
;
3052 const char *name
= "", *path
, *id
;
3053 dbus_bool_t cleanup
= FALSE
;
3054 DBusMessageIter iter
, sub
;
3056 *interface
= "org.freedesktop.systemd1.Unit",
3059 dbus_error_init(&error
);
3061 if (!(m
= dbus_message_new_method_call(
3062 "org.freedesktop.systemd1",
3063 "/org/freedesktop/systemd1",
3064 "org.freedesktop.systemd1.Manager",
3065 "CreateSnapshot"))) {
3066 log_error("Could not allocate message.");
3073 if (!dbus_message_append_args(m
,
3074 DBUS_TYPE_STRING
, &name
,
3075 DBUS_TYPE_BOOLEAN
, &cleanup
,
3076 DBUS_TYPE_INVALID
)) {
3077 log_error("Could not append arguments to message.");
3082 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3083 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3088 if (!dbus_message_get_args(reply
, &error
,
3089 DBUS_TYPE_OBJECT_PATH
, &path
,
3090 DBUS_TYPE_INVALID
)) {
3091 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3096 dbus_message_unref(m
);
3097 if (!(m
= dbus_message_new_method_call(
3098 "org.freedesktop.systemd1",
3100 "org.freedesktop.DBus.Properties",
3102 log_error("Could not allocate message.");
3106 if (!dbus_message_append_args(m
,
3107 DBUS_TYPE_STRING
, &interface
,
3108 DBUS_TYPE_STRING
, &property
,
3109 DBUS_TYPE_INVALID
)) {
3110 log_error("Could not append arguments to message.");
3115 dbus_message_unref(reply
);
3116 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3117 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3122 if (!dbus_message_iter_init(reply
, &iter
) ||
3123 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3124 log_error("Failed to parse reply.");
3129 dbus_message_iter_recurse(&iter
, &sub
);
3131 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
3132 log_error("Failed to parse reply.");
3137 dbus_message_iter_get_basic(&sub
, &id
);
3145 dbus_message_unref(m
);
3148 dbus_message_unref(reply
);
3150 dbus_error_free(&error
);
3155 static int delete_snapshot(DBusConnection
*bus
, char **args
, unsigned n
) {
3156 DBusMessage
*m
= NULL
, *reply
= NULL
;
3164 dbus_error_init(&error
);
3166 for (i
= 1; i
< n
; i
++) {
3167 const char *path
= NULL
;
3169 if (!(m
= dbus_message_new_method_call(
3170 "org.freedesktop.systemd1",
3171 "/org/freedesktop/systemd1",
3172 "org.freedesktop.systemd1.Manager",
3174 log_error("Could not allocate message.");
3179 if (!dbus_message_append_args(m
,
3180 DBUS_TYPE_STRING
, &args
[i
],
3181 DBUS_TYPE_INVALID
)) {
3182 log_error("Could not append arguments to message.");
3187 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3188 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3193 if (!dbus_message_get_args(reply
, &error
,
3194 DBUS_TYPE_OBJECT_PATH
, &path
,
3195 DBUS_TYPE_INVALID
)) {
3196 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3201 dbus_message_unref(m
);
3202 if (!(m
= dbus_message_new_method_call(
3203 "org.freedesktop.systemd1",
3205 "org.freedesktop.systemd1.Snapshot",
3207 log_error("Could not allocate message.");
3212 dbus_message_unref(reply
);
3213 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3214 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3219 dbus_message_unref(m
);
3220 dbus_message_unref(reply
);
3228 dbus_message_unref(m
);
3231 dbus_message_unref(reply
);
3233 dbus_error_free(&error
);
3238 static int daemon_reload(DBusConnection
*bus
, char **args
, unsigned n
) {
3239 DBusMessage
*m
= NULL
, *reply
= NULL
;
3244 dbus_error_init(&error
);
3246 if (arg_action
== ACTION_RELOAD
)
3248 else if (arg_action
== ACTION_REEXEC
)
3249 method
= "Reexecute";
3251 assert(arg_action
== ACTION_SYSTEMCTL
);
3254 streq(args
[0], "clear-jobs") ||
3255 streq(args
[0], "cancel") ? "ClearJobs" :
3256 streq(args
[0], "daemon-reexec") ? "Reexecute" :
3257 streq(args
[0], "reset-failed") ? "ResetFailed" :
3258 streq(args
[0], "halt") ? "Halt" :
3259 streq(args
[0], "poweroff") ? "PowerOff" :
3260 streq(args
[0], "reboot") ? "Reboot" :
3261 streq(args
[0], "kexec") ? "KExec" :
3262 streq(args
[0], "exit") ? "Exit" :
3263 /* "daemon-reload" */ "Reload";
3266 if (!(m
= dbus_message_new_method_call(
3267 "org.freedesktop.systemd1",
3268 "/org/freedesktop/systemd1",
3269 "org.freedesktop.systemd1.Manager",
3271 log_error("Could not allocate message.");
3275 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3277 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(&error
)) {
3278 /* There's always a fallback possible for
3279 * legacy actions. */
3284 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3293 dbus_message_unref(m
);
3296 dbus_message_unref(reply
);
3298 dbus_error_free(&error
);
3303 static int reset_failed(DBusConnection
*bus
, char **args
, unsigned n
) {
3304 DBusMessage
*m
= NULL
, *reply
= NULL
;
3310 dbus_error_init(&error
);
3313 return daemon_reload(bus
, args
, n
);
3315 for (i
= 1; i
< n
; i
++) {
3317 if (!(m
= dbus_message_new_method_call(
3318 "org.freedesktop.systemd1",
3319 "/org/freedesktop/systemd1",
3320 "org.freedesktop.systemd1.Manager",
3321 "ResetFailedUnit"))) {
3322 log_error("Could not allocate message.");
3327 if (!dbus_message_append_args(m
,
3328 DBUS_TYPE_STRING
, args
+ i
,
3329 DBUS_TYPE_INVALID
)) {
3330 log_error("Could not append arguments to message.");
3335 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3336 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3341 dbus_message_unref(m
);
3342 dbus_message_unref(reply
);
3350 dbus_message_unref(m
);
3353 dbus_message_unref(reply
);
3355 dbus_error_free(&error
);
3360 static int show_enviroment(DBusConnection
*bus
, char **args
, unsigned n
) {
3361 DBusMessage
*m
= NULL
, *reply
= NULL
;
3363 DBusMessageIter iter
, sub
, sub2
;
3366 *interface
= "org.freedesktop.systemd1.Manager",
3367 *property
= "Environment";
3369 dbus_error_init(&error
);
3373 if (!(m
= dbus_message_new_method_call(
3374 "org.freedesktop.systemd1",
3375 "/org/freedesktop/systemd1",
3376 "org.freedesktop.DBus.Properties",
3378 log_error("Could not allocate message.");
3382 if (!dbus_message_append_args(m
,
3383 DBUS_TYPE_STRING
, &interface
,
3384 DBUS_TYPE_STRING
, &property
,
3385 DBUS_TYPE_INVALID
)) {
3386 log_error("Could not append arguments to message.");
3391 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3392 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3397 if (!dbus_message_iter_init(reply
, &iter
) ||
3398 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3399 log_error("Failed to parse reply.");
3404 dbus_message_iter_recurse(&iter
, &sub
);
3406 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_ARRAY
||
3407 dbus_message_iter_get_element_type(&sub
) != DBUS_TYPE_STRING
) {
3408 log_error("Failed to parse reply.");
3413 dbus_message_iter_recurse(&sub
, &sub2
);
3415 while (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_INVALID
) {
3418 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_STRING
) {
3419 log_error("Failed to parse reply.");
3424 dbus_message_iter_get_basic(&sub2
, &text
);
3425 printf("%s\n", text
);
3427 dbus_message_iter_next(&sub2
);
3434 dbus_message_unref(m
);
3437 dbus_message_unref(reply
);
3439 dbus_error_free(&error
);
3444 static int set_environment(DBusConnection
*bus
, char **args
, unsigned n
) {
3445 DBusMessage
*m
= NULL
, *reply
= NULL
;
3449 DBusMessageIter iter
, sub
;
3452 dbus_error_init(&error
);
3454 method
= streq(args
[0], "set-environment")
3456 : "UnsetEnvironment";
3458 if (!(m
= dbus_message_new_method_call(
3459 "org.freedesktop.systemd1",
3460 "/org/freedesktop/systemd1",
3461 "org.freedesktop.systemd1.Manager",
3464 log_error("Could not allocate message.");
3468 dbus_message_iter_init_append(m
, &iter
);
3470 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
)) {
3471 log_error("Could not append arguments to message.");
3476 for (i
= 1; i
< n
; i
++)
3477 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &args
[i
])) {
3478 log_error("Could not append arguments to message.");
3483 if (!dbus_message_iter_close_container(&iter
, &sub
)) {
3484 log_error("Could not append arguments to message.");
3489 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3490 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3499 dbus_message_unref(m
);
3502 dbus_message_unref(reply
);
3504 dbus_error_free(&error
);
3517 static Hashmap
*will_install
= NULL
, *have_installed
= NULL
;
3518 static Set
*remove_symlinks_to
= NULL
;
3519 static unsigned n_symlinks
= 0;
3521 static void install_info_free(InstallInfo
*i
) {
3526 strv_free(i
->aliases
);
3527 strv_free(i
->wanted_by
);
3531 static void install_info_hashmap_free(Hashmap
*m
) {
3534 while ((i
= hashmap_steal_first(m
)))
3535 install_info_free(i
);
3540 static int install_info_add(const char *name
) {
3544 assert(will_install
);
3546 if (!unit_name_is_valid_no_type(name
, true)) {
3547 log_warning("Unit name %s is not a valid unit name.", name
);
3551 if (hashmap_get(have_installed
, name
) ||
3552 hashmap_get(will_install
, name
))
3555 if (!(i
= new0(InstallInfo
, 1))) {
3560 if (!(i
->name
= strdup(name
))) {
3565 if ((r
= hashmap_put(will_install
, i
->name
, i
)) < 0)
3572 install_info_free(i
);
3577 static int config_parse_also(
3578 const char *filename
,
3580 const char *section
,
3594 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
3598 if (!(n
= strndup(w
, l
)))
3601 if ((r
= install_info_add(n
)) < 0) {
3602 log_warning("Cannot install unit %s: %s", n
, strerror(-r
));
3613 static int mark_symlink_for_removal(const char *p
) {
3618 assert(path_is_absolute(p
));
3620 if (!remove_symlinks_to
)
3623 if (!(n
= strdup(p
)))
3626 path_kill_slashes(n
);
3628 if ((r
= set_put(remove_symlinks_to
, n
)) < 0) {
3630 return r
== -EEXIST
? 0 : r
;
3636 static int remove_marked_symlinks_fd(int fd
, const char *config_path
, const char *root
, bool *deleted
) {
3645 if (!(d
= fdopendir(fd
))) {
3646 close_nointr_nofail(fd
);
3652 while ((de
= readdir(d
))) {
3653 bool is_dir
= false, is_link
= false;
3655 if (ignore_file(de
->d_name
))
3658 if (de
->d_type
== DT_LNK
)
3660 else if (de
->d_type
== DT_DIR
)
3662 else if (de
->d_type
== DT_UNKNOWN
) {
3665 if (fstatat(fd
, de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
3666 log_error("Failed to stat %s/%s: %m", root
, de
->d_name
);
3673 is_link
= S_ISLNK(st
.st_mode
);
3674 is_dir
= S_ISDIR(st
.st_mode
);
3682 if ((nfd
= openat(fd
, de
->d_name
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
)) < 0) {
3683 log_error("Failed to open %s/%s: %m", root
, de
->d_name
);
3690 if (asprintf(&p
, "%s/%s", root
, de
->d_name
) < 0) {
3691 log_error("Failed to allocate directory string.");
3692 close_nointr_nofail(nfd
);
3697 /* This will close nfd, regardless whether it succeeds or not */
3698 q
= remove_marked_symlinks_fd(nfd
, config_path
, p
, deleted
);
3704 } else if (is_link
) {
3708 if (asprintf(&p
, "%s/%s", root
, de
->d_name
) < 0) {
3709 log_error("Failed to allocate symlink string.");
3714 if ((q
= readlink_and_make_absolute(p
, &dest
)) < 0) {
3715 log_error("Cannot read symlink %s: %s", p
, strerror(-q
));
3723 if ((c
= canonicalize_file_name(dest
))) {
3724 /* This might fail if the destination
3725 * is already removed */
3731 path_kill_slashes(dest
);
3732 if (set_get(remove_symlinks_to
, dest
)) {
3735 log_info("rm '%s'", p
);
3737 if (unlink(p
) < 0) {
3738 log_error("Cannot unlink symlink %s: %m", p
);
3743 rmdir_parents(p
, config_path
);
3744 path_kill_slashes(p
);
3746 if (!set_get(remove_symlinks_to
, p
)) {
3748 if ((r
= mark_symlink_for_removal(p
)) < 0) {
3767 static int remove_marked_symlinks(const char *config_path
) {
3771 assert(config_path
);
3773 if (set_size(remove_symlinks_to
) <= 0)
3776 if ((fd
= open(config_path
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
)) < 0)
3783 if ((cfd
= dup(fd
)) < 0) {
3788 /* This takes possession of cfd and closes it */
3789 if ((q
= remove_marked_symlinks_fd(cfd
, config_path
, config_path
, &deleted
)) < 0) {
3795 close_nointr_nofail(fd
);
3800 static int create_symlink(const char *verb
, const char *old_path
, const char *new_path
) {
3807 if (streq(verb
, "enable")) {
3810 mkdir_parents(new_path
, 0755);
3812 if (symlink(old_path
, new_path
) >= 0) {
3815 log_info("ln -s '%s' '%s'", old_path
, new_path
);
3820 if (errno
!= EEXIST
) {
3821 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
3825 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3827 if (errno
== EINVAL
) {
3828 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path
, new_path
);
3832 log_error("readlink() failed: %s", strerror(-r
));
3836 if (streq(dest
, old_path
)) {
3842 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path
, new_path
, dest
);
3851 log_info("ln -s '%s' '%s'", old_path
, new_path
);
3853 if (symlink(old_path
, new_path
) >= 0)
3856 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
3859 } else if (streq(verb
, "disable")) {
3862 if ((r
= mark_symlink_for_removal(old_path
)) < 0)
3865 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3866 if (errno
== ENOENT
)
3869 if (errno
== EINVAL
) {
3870 log_warning("File %s not a symlink, ignoring.", old_path
);
3874 log_error("readlink() failed: %s", strerror(-r
));
3878 if (!streq(dest
, old_path
)) {
3879 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path
, old_path
, dest
);
3886 if ((r
= mark_symlink_for_removal(new_path
)) < 0)
3890 log_info("rm '%s'", new_path
);
3892 if (unlink(new_path
) >= 0)
3895 log_error("Cannot unlink %s: %m", new_path
);
3898 } else if (streq(verb
, "is-enabled")) {
3901 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3903 if (errno
== ENOENT
|| errno
== EINVAL
)
3906 log_error("readlink() failed: %s", strerror(-r
));
3910 if (streq(dest
, old_path
)) {
3918 assert_not_reached("Unknown action.");
3921 static int install_info_symlink_alias(const char *verb
, InstallInfo
*i
, const char *config_path
) {
3923 char *alias_path
= NULL
;
3928 assert(config_path
);
3930 STRV_FOREACH(s
, i
->aliases
) {
3933 if (!(alias_path
= path_make_absolute(*s
, config_path
))) {
3934 log_error("Out of memory");
3939 if ((r
= create_symlink(verb
, i
->path
, alias_path
)) != 0)
3942 if (streq(verb
, "disable"))
3943 rmdir_parents(alias_path
, config_path
);
3953 static int install_info_symlink_wants(const char *verb
, InstallInfo
*i
, const char *config_path
) {
3955 char *alias_path
= NULL
;
3960 assert(config_path
);
3962 STRV_FOREACH(s
, i
->wanted_by
) {
3963 if (!unit_name_is_valid_no_type(*s
, true)) {
3964 log_error("Invalid name %s.", *s
);
3972 if (asprintf(&alias_path
, "%s/%s.wants/%s", config_path
, *s
, i
->name
) < 0) {
3973 log_error("Out of memory");
3978 if ((r
= create_symlink(verb
, i
->path
, alias_path
)) != 0)
3981 if (streq(verb
, "disable"))
3982 rmdir_parents(alias_path
, config_path
);
3993 static int install_info_apply(const char *verb
, LookupPaths
*paths
, InstallInfo
*i
, const char *config_path
) {
3995 const ConfigItem items
[] = {
3996 { "Alias", config_parse_strv
, &i
->aliases
, "Install" },
3997 { "WantedBy", config_parse_strv
, &i
->wanted_by
, "Install" },
3998 { "Also", config_parse_also
, NULL
, "Install" },
4000 { NULL
, NULL
, NULL
, NULL
}
4004 char *filename
= NULL
;
4011 STRV_FOREACH(p
, paths
->unit_path
) {
4014 if (!(filename
= path_make_absolute(i
->name
, *p
))) {
4015 log_error("Out of memory");
4019 /* Ensure that we don't follow symlinks */
4020 if ((fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NOCTTY
)) >= 0)
4021 if ((f
= fdopen(fd
, "re")))
4024 if (errno
== ELOOP
) {
4025 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
4030 if (errno
!= ENOENT
) {
4031 log_error("Failed to open %s: %m", filename
);
4041 #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT)
4043 if (endswith(i
->name
, ".service")) {
4047 if (asprintf(&sysv
, SYSTEM_SYSVINIT_PATH
"/%s", i
->name
) < 0) {
4048 log_error("Out of memory");
4052 sysv
[strlen(sysv
) - sizeof(".service") + 1] = 0;
4053 exists
= access(sysv
, F_OK
) >= 0;
4059 const char *argv
[] = {
4066 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i
->name
);
4068 argv
[1] = file_name_from_path(sysv
);
4070 streq(verb
, "enable") ? "on" :
4071 streq(verb
, "disable") ? "off" : NULL
;
4073 log_info("Executing %s %s %s", argv
[0], argv
[1], strempty(argv
[2]));
4075 if ((pid
= fork()) < 0) {
4076 log_error("Failed to fork: %m");
4079 } else if (pid
== 0) {
4080 execv(argv
[0], (char**) argv
);
4081 _exit(EXIT_FAILURE
);
4086 if ((r
= wait_for_terminate(pid
, &status
)) < 0)
4089 if (status
.si_code
== CLD_EXITED
) {
4090 if (status
.si_status
== 0 && (streq(verb
, "enable") || streq(verb
, "disable")))
4093 return status
.si_status
== 0 ? 0 : -EINVAL
;
4103 log_error("Couldn't find %s.", i
->name
);
4109 if ((r
= config_parse(filename
, f
, NULL
, items
, true, i
)) < 0) {
4114 n_symlinks
+= strv_length(i
->aliases
);
4115 n_symlinks
+= strv_length(i
->wanted_by
);
4119 if ((r
= install_info_symlink_alias(verb
, i
, config_path
)) != 0)
4122 if ((r
= install_info_symlink_wants(verb
, i
, config_path
)) != 0)
4125 if ((r
= mark_symlink_for_removal(filename
)) < 0)
4128 if ((r
= remove_marked_symlinks(config_path
)) < 0)
4134 static char *get_config_path(void) {
4136 if (arg_user
&& arg_global
)
4137 return strdup(USER_CONFIG_UNIT_PATH
);
4142 if (user_config_home(&p
) < 0)
4148 return strdup(SYSTEM_CONFIG_UNIT_PATH
);
4151 static int enable_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
4155 char *config_path
= NULL
;
4158 const char *verb
= args
[0];
4160 dbus_error_init(&error
);
4163 if ((r
= lookup_paths_init(&paths
, arg_user
? MANAGER_USER
: MANAGER_SYSTEM
)) < 0) {
4164 log_error("Failed to determine lookup paths: %s", strerror(-r
));
4168 if (!(config_path
= get_config_path())) {
4169 log_error("Failed to determine config path");
4174 will_install
= hashmap_new(string_hash_func
, string_compare_func
);
4175 have_installed
= hashmap_new(string_hash_func
, string_compare_func
);
4177 if (!will_install
|| !have_installed
) {
4178 log_error("Failed to allocate unit sets.");
4183 if (!arg_defaults
&& streq(verb
, "disable"))
4184 if (!(remove_symlinks_to
= set_new(string_hash_func
, string_compare_func
))) {
4185 log_error("Failed to allocate symlink sets.");
4190 for (j
= 1; j
< n
; j
++)
4191 if ((r
= install_info_add(args
[j
])) < 0) {
4192 log_warning("Cannot install unit %s: %s", args
[j
], strerror(-r
));
4196 while ((i
= hashmap_first(will_install
))) {
4199 assert_se(hashmap_move_one(have_installed
, will_install
, i
->name
) == 0);
4201 if ((q
= install_info_apply(verb
, &paths
, i
, config_path
)) != 0) {
4209 /* In test mode and found something */
4215 if (streq(verb
, "is-enabled"))
4216 r
= r
> 0 ? 0 : -ENOENT
;
4218 if (n_symlinks
<= 0)
4219 log_warning("Unit files contain no applicable installation information. Ignoring.");
4222 /* Don't try to reload anything if the user asked us to not do this */
4224 /* Don't try to reload anything when updating a unit globally */
4226 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4227 (arg_user
|| sd_booted() > 0) &&
4228 /* Don't try to reload anything if we are running in a chroot environment */
4229 (arg_user
|| running_in_chroot() <= 0) ) {
4232 if ((q
= daemon_reload(bus
, args
, n
)) < 0)
4238 install_info_hashmap_free(will_install
);
4239 install_info_hashmap_free(have_installed
);
4241 set_free_free(remove_symlinks_to
);
4243 lookup_paths_free(&paths
);
4250 static int systemctl_help(void) {
4252 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4253 "Send control commands to or query the systemd manager.\n\n"
4254 " -h --help Show this help\n"
4255 " --version Show package version\n"
4256 " -t --type=TYPE List only units of a particular type\n"
4257 " -p --property=NAME Show only properties by this name\n"
4258 " -a --all Show all units/properties, including dead/empty ones\n"
4259 " --failed Show only failed units\n"
4260 " --full Don't ellipsize unit names on output\n"
4261 " --fail When queueing a new job, fail if conflicting jobs are\n"
4263 " --ignore-dependencies\n"
4264 " When queueing a new job, ignore all its dependencies\n"
4265 " -q --quiet Suppress output\n"
4266 " --no-block Do not wait until operation finished\n"
4267 " --no-pager Do not pipe output into a pager.\n"
4268 " --system Connect to system manager\n"
4269 " --user Connect to user service manager\n"
4270 " --order When generating graph for dot, show only order\n"
4271 " --require When generating graph for dot, show only requirement\n"
4272 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4273 " --global Enable/disable unit files globally\n"
4274 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4276 " --no-ask-password\n"
4277 " Do not ask for system passwords\n"
4278 " --kill-mode=MODE How to send signal\n"
4279 " --kill-who=WHO Who to send signal to\n"
4280 " -s --signal=SIGNAL Which signal to send\n"
4281 " -f --force When enabling unit files, override existing symlinks\n"
4282 " When shutting down, execute action immediately\n"
4283 " --defaults When disabling unit files, remove default symlinks only\n\n"
4285 " list-units List units\n"
4286 " start [NAME...] Start (activate) one or more units\n"
4287 " stop [NAME...] Stop (deactivate) one or more units\n"
4288 " reload [NAME...] Reload one or more units\n"
4289 " restart [NAME...] Start or restart one or more units\n"
4290 " try-restart [NAME...] Restart one or more units if active\n"
4291 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4292 " otherwise start or restart\n"
4293 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4294 " otherwise restart if active\n"
4295 " isolate [NAME] Start one unit and stop all others\n"
4296 " kill [NAME...] Send signal to processes of a unit\n"
4297 " is-active [NAME...] Check whether units are active\n"
4298 " status [NAME...|PID...] Show runtime status of one or more units\n"
4299 " show [NAME...|JOB...] Show properties of one or more\n"
4300 " units/jobs or the manager\n"
4301 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4303 " enable [NAME...] Enable one or more unit files\n"
4304 " disable [NAME...] Disable one or more unit files\n"
4305 " is-enabled [NAME...] Check whether unit files are enabled\n"
4306 " load [NAME...] Load one or more units\n"
4307 " list-jobs List jobs\n"
4308 " cancel [JOB...] Cancel all, one, or more jobs\n"
4309 " monitor Monitor unit/job changes\n"
4310 " dump Dump server status\n"
4311 " dot Dump dependency graph for dot(1)\n"
4312 " snapshot [NAME] Create a snapshot\n"
4313 " delete [NAME...] Remove one or more snapshots\n"
4314 " daemon-reload Reload systemd manager configuration\n"
4315 " daemon-reexec Reexecute systemd manager\n"
4316 " show-environment Dump environment\n"
4317 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4318 " unset-environment [NAME...] Unset one or more environment variables\n"
4319 " default Enter system default mode\n"
4320 " rescue Enter system rescue mode\n"
4321 " emergency Enter system emergency mode\n"
4322 " halt Shut down and halt the system\n"
4323 " poweroff Shut down and power-off the system\n"
4324 " reboot Shut down and reboot the system\n"
4325 " kexec Shut down and reboot the system with kexec\n"
4326 " exit Ask for user instance termination\n",
4327 program_invocation_short_name
);
4332 static int halt_help(void) {
4334 printf("%s [OPTIONS...]\n\n"
4335 "%s the system.\n\n"
4336 " --help Show this help\n"
4337 " --halt Halt the machine\n"
4338 " -p --poweroff Switch off the machine\n"
4339 " --reboot Reboot the machine\n"
4340 " -f --force Force immediate halt/power-off/reboot\n"
4341 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4342 " -d --no-wtmp Don't write wtmp record\n"
4343 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4344 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4345 program_invocation_short_name
,
4346 arg_action
== ACTION_REBOOT
? "Reboot" :
4347 arg_action
== ACTION_POWEROFF
? "Power off" :
4353 static int shutdown_help(void) {
4355 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4356 "Shut down the system.\n\n"
4357 " --help Show this help\n"
4358 " -H --halt Halt the machine\n"
4359 " -P --poweroff Power-off the machine\n"
4360 " -r --reboot Reboot the machine\n"
4361 " -h Equivalent to --poweroff, overriden by --halt\n"
4362 " -k Don't halt/power-off/reboot, just send warnings\n"
4363 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4364 " -c Cancel a pending shutdown\n",
4365 program_invocation_short_name
);
4370 static int telinit_help(void) {
4372 printf("%s [OPTIONS...] {COMMAND}\n\n"
4373 "Send control commands to the init daemon.\n\n"
4374 " --help Show this help\n"
4375 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4377 " 0 Power-off the machine\n"
4378 " 6 Reboot the machine\n"
4379 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4380 " 1, s, S Enter rescue mode\n"
4381 " q, Q Reload init daemon configuration\n"
4382 " u, U Reexecute init daemon\n",
4383 program_invocation_short_name
);
4388 static int runlevel_help(void) {
4390 printf("%s [OPTIONS...]\n\n"
4391 "Prints the previous and current runlevel of the init system.\n\n"
4392 " --help Show this help\n",
4393 program_invocation_short_name
);
4398 static int systemctl_parse_argv(int argc
, char *argv
[]) {
4402 ARG_IGNORE_DEPENDENCIES
,
4417 ARG_NO_ASK_PASSWORD
,
4421 static const struct option options
[] = {
4422 { "help", no_argument
, NULL
, 'h' },
4423 { "version", no_argument
, NULL
, ARG_VERSION
},
4424 { "type", required_argument
, NULL
, 't' },
4425 { "property", required_argument
, NULL
, 'p' },
4426 { "all", no_argument
, NULL
, 'a' },
4427 { "failed", no_argument
, NULL
, ARG_FAILED
},
4428 { "full", no_argument
, NULL
, ARG_FULL
},
4429 { "fail", no_argument
, NULL
, ARG_FAIL
},
4430 { "ignore-dependencies", no_argument
, NULL
, ARG_IGNORE_DEPENDENCIES
},
4431 { "user", no_argument
, NULL
, ARG_USER
},
4432 { "system", no_argument
, NULL
, ARG_SYSTEM
},
4433 { "global", no_argument
, NULL
, ARG_GLOBAL
},
4434 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
4435 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
4436 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4437 { "quiet", no_argument
, NULL
, 'q' },
4438 { "order", no_argument
, NULL
, ARG_ORDER
},
4439 { "require", no_argument
, NULL
, ARG_REQUIRE
},
4440 { "force", no_argument
, NULL
, 'f' },
4441 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
4442 { "defaults", no_argument
, NULL
, ARG_DEFAULTS
},
4443 { "kill-mode", required_argument
, NULL
, ARG_KILL_MODE
},
4444 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
4445 { "signal", required_argument
, NULL
, 's' },
4446 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
4447 { NULL
, 0, NULL
, 0 }
4455 /* Only when running as systemctl we ask for passwords */
4456 arg_ask_password
= true;
4458 while ((c
= getopt_long(argc
, argv
, "ht:p:aqfs:", options
, NULL
)) >= 0) {
4467 puts(PACKAGE_STRING
);
4469 puts(SYSTEMD_FEATURES
);
4479 if (!(l
= strv_append(arg_property
, optarg
)))
4482 strv_free(arg_property
);
4485 /* If the user asked for a particular
4486 * property, show it to him, even if it is
4497 arg_job_mode
= "fail";
4500 case ARG_IGNORE_DEPENDENCIES
:
4501 arg_job_mode
= "ignore-dependencies";
4513 arg_no_block
= true;
4517 arg_no_pager
= true;
4525 arg_dot
= DOT_ORDER
;
4529 arg_dot
= DOT_REQUIRE
;
4549 arg_no_reload
= true;
4558 arg_defaults
= true;
4562 arg_kill_who
= optarg
;
4566 arg_kill_mode
= optarg
;
4570 if ((arg_signal
= signal_from_string_try_harder(optarg
)) < 0) {
4571 log_error("Failed to parse signal string %s.", optarg
);
4576 case ARG_NO_ASK_PASSWORD
:
4577 arg_ask_password
= false;
4584 log_error("Unknown option code %c", c
);
4592 static int halt_parse_argv(int argc
, char *argv
[]) {
4601 static const struct option options
[] = {
4602 { "help", no_argument
, NULL
, ARG_HELP
},
4603 { "halt", no_argument
, NULL
, ARG_HALT
},
4604 { "poweroff", no_argument
, NULL
, 'p' },
4605 { "reboot", no_argument
, NULL
, ARG_REBOOT
},
4606 { "force", no_argument
, NULL
, 'f' },
4607 { "wtmp-only", no_argument
, NULL
, 'w' },
4608 { "no-wtmp", no_argument
, NULL
, 'd' },
4609 { "no-sync", no_argument
, NULL
, 'n' },
4610 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4611 { NULL
, 0, NULL
, 0 }
4619 if (utmp_get_runlevel(&runlevel
, NULL
) >= 0)
4620 if (runlevel
== '0' || runlevel
== '6')
4621 arg_immediate
= true;
4623 while ((c
= getopt_long(argc
, argv
, "pfwdnih", options
, NULL
)) >= 0) {
4631 arg_action
= ACTION_HALT
;
4635 if (arg_action
!= ACTION_REBOOT
)
4636 arg_action
= ACTION_POWEROFF
;
4640 arg_action
= ACTION_REBOOT
;
4644 arg_immediate
= true;
4665 /* Compatibility nops */
4672 log_error("Unknown option code %c", c
);
4677 if (optind
< argc
) {
4678 log_error("Too many arguments.");
4685 static int parse_time_spec(const char *t
, usec_t
*_u
) {
4689 if (streq(t
, "now"))
4691 else if (t
[0] == '+') {
4694 if (safe_atou64(t
+ 1, &u
) < 0)
4697 *_u
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
* u
;
4706 hour
= strtol(t
, &e
, 10);
4707 if (errno
!= 0 || *e
!= ':' || hour
< 0 || hour
> 23)
4710 minute
= strtol(e
+1, &e
, 10);
4711 if (errno
!= 0 || *e
!= 0 || minute
< 0 || minute
> 59)
4714 n
= now(CLOCK_REALTIME
);
4715 s
= (time_t) (n
/ USEC_PER_SEC
);
4718 assert_se(localtime_r(&s
, &tm
));
4720 tm
.tm_hour
= (int) hour
;
4721 tm
.tm_min
= (int) minute
;
4724 assert_se(s
= mktime(&tm
));
4726 *_u
= (usec_t
) s
* USEC_PER_SEC
;
4729 *_u
+= USEC_PER_DAY
;
4735 static bool kexec_loaded(void) {
4736 bool loaded
= false;
4739 if (read_one_line_file("/sys/kernel/kexec_loaded", &s
) >= 0) {
4747 static int shutdown_parse_argv(int argc
, char *argv
[]) {
4754 static const struct option options
[] = {
4755 { "help", no_argument
, NULL
, ARG_HELP
},
4756 { "halt", no_argument
, NULL
, 'H' },
4757 { "poweroff", no_argument
, NULL
, 'P' },
4758 { "reboot", no_argument
, NULL
, 'r' },
4759 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4760 { NULL
, 0, NULL
, 0 }
4768 while ((c
= getopt_long(argc
, argv
, "HPrhkt:afFc", options
, NULL
)) >= 0) {
4776 arg_action
= ACTION_HALT
;
4780 arg_action
= ACTION_POWEROFF
;
4785 arg_action
= ACTION_KEXEC
;
4787 arg_action
= ACTION_REBOOT
;
4791 if (arg_action
!= ACTION_HALT
)
4792 arg_action
= ACTION_POWEROFF
;
4805 /* Compatibility nops */
4809 arg_action
= ACTION_CANCEL_SHUTDOWN
;
4816 log_error("Unknown option code %c", c
);
4821 if (argc
> optind
) {
4822 if ((r
= parse_time_spec(argv
[optind
], &arg_when
)) < 0) {
4823 log_error("Failed to parse time specification: %s", argv
[optind
]);
4827 arg_when
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
;
4829 /* We skip the time argument */
4830 if (argc
> optind
+ 1)
4831 arg_wall
= argv
+ optind
+ 1;
4838 static int telinit_parse_argv(int argc
, char *argv
[]) {
4845 static const struct option options
[] = {
4846 { "help", no_argument
, NULL
, ARG_HELP
},
4847 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4848 { NULL
, 0, NULL
, 0 }
4851 static const struct {
4855 { '0', ACTION_POWEROFF
},
4856 { '6', ACTION_REBOOT
},
4857 { '1', ACTION_RESCUE
},
4858 { '2', ACTION_RUNLEVEL2
},
4859 { '3', ACTION_RUNLEVEL3
},
4860 { '4', ACTION_RUNLEVEL4
},
4861 { '5', ACTION_RUNLEVEL5
},
4862 { 's', ACTION_RESCUE
},
4863 { 'S', ACTION_RESCUE
},
4864 { 'q', ACTION_RELOAD
},
4865 { 'Q', ACTION_RELOAD
},
4866 { 'u', ACTION_REEXEC
},
4867 { 'U', ACTION_REEXEC
}
4876 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
4891 log_error("Unknown option code %c", c
);
4896 if (optind
>= argc
) {
4901 if (optind
+ 1 < argc
) {
4902 log_error("Too many arguments.");
4906 if (strlen(argv
[optind
]) != 1) {
4907 log_error("Expected single character argument.");
4911 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
4912 if (table
[i
].from
== argv
[optind
][0])
4915 if (i
>= ELEMENTSOF(table
)) {
4916 log_error("Unknown command %s.", argv
[optind
]);
4920 arg_action
= table
[i
].to
;
4927 static int runlevel_parse_argv(int argc
, char *argv
[]) {
4933 static const struct option options
[] = {
4934 { "help", no_argument
, NULL
, ARG_HELP
},
4935 { NULL
, 0, NULL
, 0 }
4943 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
4954 log_error("Unknown option code %c", c
);
4959 if (optind
< argc
) {
4960 log_error("Too many arguments.");
4967 static int parse_argv(int argc
, char *argv
[]) {
4971 if (program_invocation_short_name
) {
4973 if (strstr(program_invocation_short_name
, "halt")) {
4974 arg_action
= ACTION_HALT
;
4975 return halt_parse_argv(argc
, argv
);
4976 } else if (strstr(program_invocation_short_name
, "poweroff")) {
4977 arg_action
= ACTION_POWEROFF
;
4978 return halt_parse_argv(argc
, argv
);
4979 } else if (strstr(program_invocation_short_name
, "reboot")) {
4981 arg_action
= ACTION_KEXEC
;
4983 arg_action
= ACTION_REBOOT
;
4984 return halt_parse_argv(argc
, argv
);
4985 } else if (strstr(program_invocation_short_name
, "shutdown")) {
4986 arg_action
= ACTION_POWEROFF
;
4987 return shutdown_parse_argv(argc
, argv
);
4988 } else if (strstr(program_invocation_short_name
, "init")) {
4990 if (sd_booted() > 0) {
4991 arg_action
= ACTION_INVALID
;
4992 return telinit_parse_argv(argc
, argv
);
4994 /* Hmm, so some other init system is
4995 * running, we need to forward this
4996 * request to it. For now we simply
4997 * guess that it is Upstart. */
4999 execv("/lib/upstart/telinit", argv
);
5001 log_error("Couldn't find an alternative telinit implementation to spawn.");
5005 } else if (strstr(program_invocation_short_name
, "runlevel")) {
5006 arg_action
= ACTION_RUNLEVEL
;
5007 return runlevel_parse_argv(argc
, argv
);
5011 arg_action
= ACTION_SYSTEMCTL
;
5012 return systemctl_parse_argv(argc
, argv
);
5015 static int action_to_runlevel(void) {
5017 static const char table
[_ACTION_MAX
] = {
5018 [ACTION_HALT
] = '0',
5019 [ACTION_POWEROFF
] = '0',
5020 [ACTION_REBOOT
] = '6',
5021 [ACTION_RUNLEVEL2
] = '2',
5022 [ACTION_RUNLEVEL3
] = '3',
5023 [ACTION_RUNLEVEL4
] = '4',
5024 [ACTION_RUNLEVEL5
] = '5',
5025 [ACTION_RESCUE
] = '1'
5028 assert(arg_action
< _ACTION_MAX
);
5030 return table
[arg_action
];
5033 static int talk_upstart(void) {
5034 DBusMessage
*m
= NULL
, *reply
= NULL
;
5036 int previous
, rl
, r
;
5038 env1_buf
[] = "RUNLEVEL=X",
5039 env2_buf
[] = "PREVLEVEL=X";
5040 char *env1
= env1_buf
, *env2
= env2_buf
;
5041 const char *emit
= "runlevel";
5042 dbus_bool_t b_false
= FALSE
;
5043 DBusMessageIter iter
, sub
;
5044 DBusConnection
*bus
;
5046 dbus_error_init(&error
);
5048 if (!(rl
= action_to_runlevel()))
5051 if (utmp_get_runlevel(&previous
, NULL
) < 0)
5054 if (!(bus
= dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error
))) {
5055 if (dbus_error_has_name(&error
, DBUS_ERROR_NO_SERVER
)) {
5060 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error
));
5065 if ((r
= bus_check_peercred(bus
)) < 0) {
5066 log_error("Failed to verify owner of bus.");
5070 if (!(m
= dbus_message_new_method_call(
5071 "com.ubuntu.Upstart",
5072 "/com/ubuntu/Upstart",
5073 "com.ubuntu.Upstart0_6",
5076 log_error("Could not allocate message.");
5081 dbus_message_iter_init_append(m
, &iter
);
5083 env1_buf
[sizeof(env1_buf
)-2] = rl
;
5084 env2_buf
[sizeof(env2_buf
)-2] = previous
;
5086 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &emit
) ||
5087 !dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
) ||
5088 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env1
) ||
5089 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env2
) ||
5090 !dbus_message_iter_close_container(&iter
, &sub
) ||
5091 !dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &b_false
)) {
5092 log_error("Could not append arguments to message.");
5097 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
5099 if (error_is_no_service(&error
)) {
5104 log_error("Failed to issue method call: %s", bus_error_message(&error
));
5113 dbus_message_unref(m
);
5116 dbus_message_unref(reply
);
5119 dbus_connection_flush(bus
);
5120 dbus_connection_close(bus
);
5121 dbus_connection_unref(bus
);
5124 dbus_error_free(&error
);
5129 static int talk_initctl(void) {
5130 struct init_request request
;
5134 if (!(rl
= action_to_runlevel()))
5138 request
.magic
= INIT_MAGIC
;
5139 request
.sleeptime
= 0;
5140 request
.cmd
= INIT_CMD_RUNLVL
;
5141 request
.runlevel
= rl
;
5143 if ((fd
= open(INIT_FIFO
, O_WRONLY
|O_NDELAY
|O_CLOEXEC
|O_NOCTTY
)) < 0) {
5145 if (errno
== ENOENT
)
5148 log_error("Failed to open "INIT_FIFO
": %m");
5153 r
= loop_write(fd
, &request
, sizeof(request
), false) != sizeof(request
);
5154 close_nointr_nofail(fd
);
5157 log_error("Failed to write to "INIT_FIFO
": %m");
5158 return errno
? -errno
: -EIO
;
5164 static int systemctl_main(DBusConnection
*bus
, int argc
, char *argv
[], DBusError
*error
) {
5166 static const struct {
5174 int (* const dispatch
)(DBusConnection
*bus
, char **args
, unsigned n
);
5176 { "list-units", LESS
, 1, list_units
},
5177 { "list-jobs", EQUAL
, 1, list_jobs
},
5178 { "clear-jobs", EQUAL
, 1, daemon_reload
},
5179 { "load", MORE
, 2, load_unit
},
5180 { "cancel", MORE
, 2, cancel_job
},
5181 { "start", MORE
, 2, start_unit
},
5182 { "stop", MORE
, 2, start_unit
},
5183 { "reload", MORE
, 2, start_unit
},
5184 { "restart", MORE
, 2, start_unit
},
5185 { "try-restart", MORE
, 2, start_unit
},
5186 { "reload-or-restart", MORE
, 2, start_unit
},
5187 { "reload-or-try-restart", MORE
, 2, start_unit
},
5188 { "force-reload", MORE
, 2, start_unit
}, /* For compatibility with SysV */
5189 { "condreload", MORE
, 2, start_unit
}, /* For compatibility with ALTLinux */
5190 { "condrestart", MORE
, 2, start_unit
}, /* For compatibility with RH */
5191 { "isolate", EQUAL
, 2, start_unit
},
5192 { "kill", MORE
, 2, kill_unit
},
5193 { "is-active", MORE
, 2, check_unit
},
5194 { "check", MORE
, 2, check_unit
},
5195 { "show", MORE
, 1, show
},
5196 { "status", MORE
, 2, show
},
5197 { "monitor", EQUAL
, 1, monitor
},
5198 { "dump", EQUAL
, 1, dump
},
5199 { "dot", EQUAL
, 1, dot
},
5200 { "snapshot", LESS
, 2, snapshot
},
5201 { "delete", MORE
, 2, delete_snapshot
},
5202 { "daemon-reload", EQUAL
, 1, daemon_reload
},
5203 { "daemon-reexec", EQUAL
, 1, daemon_reload
},
5204 { "show-environment", EQUAL
, 1, show_enviroment
},
5205 { "set-environment", MORE
, 2, set_environment
},
5206 { "unset-environment", MORE
, 2, set_environment
},
5207 { "halt", EQUAL
, 1, start_special
},
5208 { "poweroff", EQUAL
, 1, start_special
},
5209 { "reboot", EQUAL
, 1, start_special
},
5210 { "kexec", EQUAL
, 1, start_special
},
5211 { "default", EQUAL
, 1, start_special
},
5212 { "rescue", EQUAL
, 1, start_special
},
5213 { "emergency", EQUAL
, 1, start_special
},
5214 { "exit", EQUAL
, 1, start_special
},
5215 { "reset-failed", MORE
, 1, reset_failed
},
5216 { "enable", MORE
, 2, enable_unit
},
5217 { "disable", MORE
, 2, enable_unit
},
5218 { "is-enabled", MORE
, 2, enable_unit
}
5228 left
= argc
- optind
;
5231 /* Special rule: no arguments means "list-units" */
5234 if (streq(argv
[optind
], "help")) {
5239 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
5240 if (streq(argv
[optind
], verbs
[i
].verb
))
5243 if (i
>= ELEMENTSOF(verbs
)) {
5244 log_error("Unknown operation %s", argv
[optind
]);
5249 switch (verbs
[i
].argc_cmp
) {
5252 if (left
!= verbs
[i
].argc
) {
5253 log_error("Invalid number of arguments.");
5260 if (left
< verbs
[i
].argc
) {
5261 log_error("Too few arguments.");
5268 if (left
> verbs
[i
].argc
) {
5269 log_error("Too many arguments.");
5276 assert_not_reached("Unknown comparison operator.");
5279 /* Require a bus connection for all operations but
5281 if (!streq(verbs
[i
].verb
, "enable") &&
5282 !streq(verbs
[i
].verb
, "disable") &&
5284 log_error("Failed to get D-Bus connection: %s", error
->message
);
5288 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
5291 static int send_shutdownd(usec_t t
, char mode
, bool warn
, const char *message
) {
5293 struct msghdr msghdr
;
5295 union sockaddr_union sockaddr
;
5296 struct shutdownd_command c
;
5304 strncpy(c
.wall_message
, message
, sizeof(c
.wall_message
));
5306 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0)) < 0)
5310 sockaddr
.sa
.sa_family
= AF_UNIX
;
5311 sockaddr
.un
.sun_path
[0] = 0;
5312 strncpy(sockaddr
.un
.sun_path
+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr
.un
.sun_path
)-1);
5315 iovec
.iov_base
= (char*) &c
;
5316 iovec
.iov_len
= sizeof(c
);
5319 msghdr
.msg_name
= &sockaddr
;
5320 msghdr
.msg_namelen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5322 msghdr
.msg_iov
= &iovec
;
5323 msghdr
.msg_iovlen
= 1;
5325 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) < 0) {
5326 close_nointr_nofail(fd
);
5330 close_nointr_nofail(fd
);
5334 static int reload_with_fallback(DBusConnection
*bus
) {
5337 /* First, try systemd via D-Bus. */
5338 if (daemon_reload(bus
, NULL
, 0) > 0)
5342 /* Nothing else worked, so let's try signals */
5343 assert(arg_action
== ACTION_RELOAD
|| arg_action
== ACTION_REEXEC
);
5345 if (kill(1, arg_action
== ACTION_RELOAD
? SIGHUP
: SIGTERM
) < 0) {
5346 log_error("kill() failed: %m");
5353 static int start_with_fallback(DBusConnection
*bus
) {
5356 /* First, try systemd via D-Bus. */
5357 if (start_unit(bus
, NULL
, 0) >= 0)
5361 /* Hmm, talking to systemd via D-Bus didn't work. Then
5362 * let's try to talk to Upstart via D-Bus. */
5363 if (talk_upstart() > 0)
5366 /* Nothing else worked, so let's try
5368 if (talk_initctl() > 0)
5371 log_error("Failed to talk to init daemon.");
5375 warn_wall(arg_action
);
5379 static int halt_main(DBusConnection
*bus
) {
5382 if (geteuid() != 0) {
5383 log_error("Must be root.");
5389 char date
[FORMAT_TIMESTAMP_MAX
];
5391 m
= strv_join(arg_wall
, " ");
5392 r
= send_shutdownd(arg_when
,
5393 arg_action
== ACTION_HALT
? 'H' :
5394 arg_action
== ACTION_POWEROFF
? 'P' :
5401 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r
));
5403 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5404 format_timestamp(date
, sizeof(date
), arg_when
));
5409 if (!arg_dry
&& !arg_immediate
)
5410 return start_with_fallback(bus
);
5413 if (sd_booted() > 0)
5414 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5415 else if ((r
= utmp_put_shutdown(0)) < 0)
5416 log_warning("Failed to write utmp record: %s", strerror(-r
));
5425 /* Make sure C-A-D is handled by the kernel from this
5427 reboot(RB_ENABLE_CAD
);
5429 switch (arg_action
) {
5432 log_info("Halting.");
5433 reboot(RB_HALT_SYSTEM
);
5436 case ACTION_POWEROFF
:
5437 log_info("Powering off.");
5438 reboot(RB_POWER_OFF
);
5442 log_info("Rebooting.");
5443 reboot(RB_AUTOBOOT
);
5447 assert_not_reached("Unknown halt action.");
5450 /* We should never reach this. */
5454 static int runlevel_main(void) {
5455 int r
, runlevel
, previous
;
5457 if ((r
= utmp_get_runlevel(&runlevel
, &previous
)) < 0) {
5458 printf("unknown\n");
5463 previous
<= 0 ? 'N' : previous
,
5464 runlevel
<= 0 ? 'N' : runlevel
);
5469 static void pager_open(void) {
5477 if (!on_tty() || arg_no_pager
)
5480 if ((pager
= getenv("PAGER")))
5481 if (!*pager
|| streq(pager
, "cat"))
5484 /* Determine and cache number of columns before we spawn the
5485 * pager so that we get the value from the actual tty */
5489 log_error("Failed to create pager pipe: %m");
5493 parent_pid
= getpid();
5496 if (pager_pid
< 0) {
5497 log_error("Failed to fork pager: %m");
5502 /* In the child start the pager */
5503 if (pager_pid
== 0) {
5505 dup2(fd
[0], STDIN_FILENO
);
5508 setenv("LESS", "FRSX", 0);
5510 /* Make sure the pager goes away when the parent dies */
5511 if (prctl(PR_SET_PDEATHSIG
, SIGTERM
) < 0)
5512 _exit(EXIT_FAILURE
);
5514 /* Check whether our parent died before we were able
5515 * to set the death signal */
5516 if (getppid() != parent_pid
)
5517 _exit(EXIT_SUCCESS
);
5520 execlp(pager
, pager
, NULL
);
5521 execl("/bin/sh", "sh", "-c", pager
, NULL
);
5523 /* Debian's alternatives command for pagers is
5524 * called 'pager'. Note that we do not call
5525 * sensible-pagers here, since that is just a
5526 * shell script that implements a logic that
5527 * is similar to this one anyway, but is
5528 * Debian-specific. */
5529 execlp("pager", "pager", NULL
);
5531 execlp("less", "less", NULL
);
5532 execlp("more", "more", NULL
);
5535 log_error("Unable to execute pager: %m");
5536 _exit(EXIT_FAILURE
);
5539 /* Return in the parent */
5540 if (dup2(fd
[1], STDOUT_FILENO
) < 0)
5541 log_error("Failed to duplicate pager pipe: %m");
5546 static void pager_close(void) {
5552 /* Inform pager that we are done */
5554 kill(pager_pid
, SIGCONT
);
5555 wait_for_terminate(pager_pid
, &dummy
);
5559 static void agent_close(void) {
5565 /* Inform agent that we are done */
5566 kill(agent_pid
, SIGTERM
);
5567 kill(agent_pid
, SIGCONT
);
5568 wait_for_terminate(agent_pid
, &dummy
);
5572 int main(int argc
, char*argv
[]) {
5573 int r
, retval
= EXIT_FAILURE
;
5574 DBusConnection
*bus
= NULL
;
5577 dbus_error_init(&error
);
5579 log_parse_environment();
5582 if ((r
= parse_argv(argc
, argv
)) < 0)
5585 retval
= EXIT_SUCCESS
;
5589 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5590 * let's shortcut this */
5591 if (arg_action
== ACTION_RUNLEVEL
) {
5592 r
= runlevel_main();
5593 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5597 bus_connect(arg_user
? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &bus
, &private_bus
, &error
);
5599 switch (arg_action
) {
5601 case ACTION_SYSTEMCTL
:
5602 r
= systemctl_main(bus
, argc
, argv
, &error
);
5606 case ACTION_POWEROFF
:
5612 case ACTION_RUNLEVEL2
:
5613 case ACTION_RUNLEVEL3
:
5614 case ACTION_RUNLEVEL4
:
5615 case ACTION_RUNLEVEL5
:
5617 case ACTION_EMERGENCY
:
5618 case ACTION_DEFAULT
:
5619 r
= start_with_fallback(bus
);
5624 r
= reload_with_fallback(bus
);
5627 case ACTION_CANCEL_SHUTDOWN
:
5628 r
= send_shutdownd(0, 0, false, NULL
);
5631 case ACTION_INVALID
:
5632 case ACTION_RUNLEVEL
:
5634 assert_not_reached("Unknown action");
5637 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5642 dbus_connection_flush(bus
);
5643 dbus_connection_close(bus
);
5644 dbus_connection_unref(bus
);
5647 dbus_error_free(&error
);
5651 strv_free(arg_property
);