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 bool arg_fail
= false;
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 char **arg_wall
= NULL
;
80 static const char *arg_kill_who
= NULL
;
81 static const char *arg_kill_mode
= NULL
;
82 static int arg_signal
= SIGTERM
;
83 static usec_t arg_when
= 0;
102 ACTION_CANCEL_SHUTDOWN
,
104 } arg_action
= ACTION_SYSTEMCTL
;
111 static bool private_bus
= false;
113 static pid_t pager_pid
= 0;
115 static int daemon_reload(DBusConnection
*bus
, char **args
, unsigned n
);
116 static void pager_open(void);
118 static bool on_tty(void) {
121 /* Note that this is invoked relatively early, before we start
122 * the pager. That means the value we return reflects whether
123 * we originally were started on a tty, not if we currently
124 * are. But this is intended, since we want color, and so on
125 * when run in our own pager. */
127 if (_unlikely_(t
< 0))
128 t
= isatty(STDOUT_FILENO
) > 0;
133 static void spawn_ask_password_agent(void) {
136 /* We check STDIN here, not STDOUT, since this is about input,
138 if (!isatty(STDIN_FILENO
))
141 if (!arg_ask_password
)
146 /* Spawns a temporary TTY agent, making sure it goes away when
149 if ((child
= fork()) < 0)
154 const char * const args
[] = {
155 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH
,
162 /* Make sure the agent goes away when the parent dies */
163 if (prctl(PR_SET_PDEATHSIG
, SIGTERM
) < 0)
166 /* Check whether our parent died before we were able
167 * to set the death signal */
168 if (getppid() != parent
)
171 /* Don't leak fds to the agent */
172 close_all_fds(NULL
, 0);
174 /* Detach from stdin/stdout/stderr. and reopen
175 * /dev/tty for them. This is important to ensure that
176 * when systemctl is started via popen() or a similar
177 * call that expects to read EOF we actually do
178 * generate EOF and not delay this indefinitely by
179 * because we keep an unused copy of stdin around. */
180 if ((fd
= open("/dev/tty", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
)) < 0) {
181 log_error("Failed to open /dev/tty: %m");
186 close(STDOUT_FILENO
);
187 close(STDERR_FILENO
);
189 dup2(fd
, STDIN_FILENO
);
190 dup2(fd
, STDOUT_FILENO
);
191 dup2(fd
, STDERR_FILENO
);
196 execv(args
[0], (char **) args
);
201 static const char *ansi_highlight(bool b
) {
206 return b
? ANSI_HIGHLIGHT_ON
: ANSI_HIGHLIGHT_OFF
;
209 static const char *ansi_highlight_green(bool b
) {
214 return b
? ANSI_HIGHLIGHT_GREEN_ON
: ANSI_HIGHLIGHT_OFF
;
217 static bool error_is_no_service(const DBusError
*error
) {
220 if (!dbus_error_is_set(error
))
223 if (dbus_error_has_name(error
, DBUS_ERROR_NAME_HAS_NO_OWNER
))
226 if (dbus_error_has_name(error
, DBUS_ERROR_SERVICE_UNKNOWN
))
229 return startswith(error
->name
, "org.freedesktop.DBus.Error.Spawn.");
232 static int translate_bus_error_to_exit_status(int r
, const DBusError
*error
) {
235 if (!dbus_error_is_set(error
))
238 if (dbus_error_has_name(error
, DBUS_ERROR_ACCESS_DENIED
) ||
239 dbus_error_has_name(error
, BUS_ERROR_ONLY_BY_DEPENDENCY
) ||
240 dbus_error_has_name(error
, BUS_ERROR_NO_ISOLATION
) ||
241 dbus_error_has_name(error
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
))
242 return EXIT_NOPERMISSION
;
244 if (dbus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
))
245 return EXIT_NOTINSTALLED
;
247 if (dbus_error_has_name(error
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
) ||
248 dbus_error_has_name(error
, BUS_ERROR_NOT_SUPPORTED
))
249 return EXIT_NOTIMPLEMENTED
;
251 if (dbus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
))
252 return EXIT_NOTCONFIGURED
;
260 static int bus_iter_get_basic_and_next(DBusMessageIter
*iter
, int type
, void *data
, bool next
) {
265 if (dbus_message_iter_get_arg_type(iter
) != type
)
268 dbus_message_iter_get_basic(iter
, data
);
270 if (!dbus_message_iter_next(iter
) != !next
)
276 static void warn_wall(enum action action
) {
277 static const char *table
[_ACTION_MAX
] = {
278 [ACTION_HALT
] = "The system is going down for system halt NOW!",
279 [ACTION_REBOOT
] = "The system is going down for reboot NOW!",
280 [ACTION_POWEROFF
] = "The system is going down for power-off NOW!",
281 [ACTION_KEXEC
] = "The system is going down for kexec reboot NOW!",
282 [ACTION_RESCUE
] = "The system is going down to rescue mode NOW!",
283 [ACTION_EMERGENCY
] = "The system is going down to emergency mode NOW!"
292 if (!(p
= strv_join(arg_wall
, " "))) {
293 log_error("Failed to join strings.");
309 utmp_wall(table
[action
], NULL
);
314 const char *description
;
315 const char *load_state
;
316 const char *active_state
;
317 const char *sub_state
;
318 const char *following
;
319 const char *unit_path
;
321 const char *job_type
;
322 const char *job_path
;
325 static int compare_unit_info(const void *a
, const void *b
) {
327 const struct unit_info
*u
= a
, *v
= b
;
329 d1
= strrchr(u
->id
, '.');
330 d2
= strrchr(v
->id
, '.');
335 if ((r
= strcasecmp(d1
, d2
)) != 0)
339 return strcasecmp(u
->id
, v
->id
);
342 static bool output_show_job(const struct unit_info
*u
) {
345 return (!arg_type
|| ((dot
= strrchr(u
->id
, '.')) &&
346 streq(dot
+1, arg_type
))) &&
347 (arg_all
|| !(streq(u
->active_state
, "inactive") || u
->following
[0]) || u
->job_id
> 0);
350 static void output_units_list(const struct unit_info
*unit_infos
, unsigned c
) {
351 unsigned active_len
, sub_len
, job_len
, n_shown
= 0;
352 const struct unit_info
*u
;
354 active_len
= sizeof("ACTIVE")-1;
355 sub_len
= sizeof("SUB")-1;
356 job_len
= sizeof("JOB")-1;
358 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
359 if (!output_show_job(u
))
362 active_len
= MAX(active_len
, strlen(u
->active_state
));
363 sub_len
= MAX(sub_len
, strlen(u
->sub_state
));
365 job_len
= MAX(job_len
, strlen(u
->job_type
));
369 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
370 active_len
, "ACTIVE", sub_len
, "SUB", job_len
, "JOB");
371 if (columns() >= 80+12 || arg_full
)
372 printf(" %s\n", "DESCRIPTION");
377 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
380 const char *on_loaded
, *off_loaded
;
381 const char *on_active
, *off_active
;
383 if (!output_show_job(u
))
388 if (!streq(u
->load_state
, "loaded") &&
389 !streq(u
->load_state
, "banned")) {
390 on_loaded
= ansi_highlight(true);
391 off_loaded
= ansi_highlight(false);
393 on_loaded
= off_loaded
= "";
395 if (streq(u
->active_state
, "failed")) {
396 on_active
= ansi_highlight(true);
397 off_active
= ansi_highlight(false);
399 on_active
= off_active
= "";
401 e
= arg_full
? NULL
: ellipsize(u
->id
, 25, 33);
403 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
405 on_loaded
, u
->load_state
, off_loaded
,
406 on_active
, active_len
, u
->active_state
,
407 sub_len
, u
->sub_state
, off_active
,
412 a
-= strlen(on_loaded
) + strlen(off_loaded
);
413 a
-= strlen(on_active
) + strlen(off_active
);
416 printf(" %-*s", job_len
, u
->job_type
);
420 if (a
+ b
+ 1 < columns()) {
422 printf(" %-*s", job_len
, "");
425 printf(" %s", u
->description
);
427 printf(" %.*s", columns() - a
- b
- 1, u
->description
);
434 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
435 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
436 "SUB = The low-level unit activation state, values depend on unit type.\n"
437 "JOB = Pending job for the unit.\n");
440 printf("\n%u units listed.\n", n_shown
);
442 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown
);
446 static int list_units(DBusConnection
*bus
, char **args
, unsigned n
) {
447 DBusMessage
*m
= NULL
, *reply
= NULL
;
450 DBusMessageIter iter
, sub
, sub2
;
451 unsigned c
= 0, n_units
= 0;
452 struct unit_info
*unit_infos
= NULL
;
454 dbus_error_init(&error
);
460 if (!(m
= dbus_message_new_method_call(
461 "org.freedesktop.systemd1",
462 "/org/freedesktop/systemd1",
463 "org.freedesktop.systemd1.Manager",
465 log_error("Could not allocate message.");
469 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
470 log_error("Failed to issue method call: %s", bus_error_message(&error
));
475 if (!dbus_message_iter_init(reply
, &iter
) ||
476 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
477 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
478 log_error("Failed to parse reply.");
483 dbus_message_iter_recurse(&iter
, &sub
);
485 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
488 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
489 log_error("Failed to parse reply.");
497 n_units
= MAX(2*c
, 16);
498 w
= realloc(unit_infos
, sizeof(struct unit_info
) * n_units
);
501 log_error("Failed to allocate unit array.");
511 dbus_message_iter_recurse(&sub
, &sub2
);
513 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->id
, true) < 0 ||
514 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->description
, true) < 0 ||
515 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->load_state
, true) < 0 ||
516 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->active_state
, true) < 0 ||
517 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->sub_state
, true) < 0 ||
518 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->following
, true) < 0 ||
519 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->unit_path
, true) < 0 ||
520 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &u
->job_id
, true) < 0 ||
521 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &u
->job_type
, true) < 0 ||
522 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u
->job_path
, false) < 0) {
523 log_error("Failed to parse reply.");
528 dbus_message_iter_next(&sub
);
533 qsort(unit_infos
, c
, sizeof(struct unit_info
), compare_unit_info
);
534 output_units_list(unit_infos
, c
);
541 dbus_message_unref(m
);
544 dbus_message_unref(reply
);
548 dbus_error_free(&error
);
553 static int dot_one_property(const char *name
, const char *prop
, DBusMessageIter
*iter
) {
554 static const char * const colors
[] = {
555 "Requires", "[color=\"black\"]",
556 "RequiresOverridable", "[color=\"black\"]",
557 "Requisite", "[color=\"darkblue\"]",
558 "RequisiteOverridable", "[color=\"darkblue\"]",
559 "Wants", "[color=\"darkgrey\"]",
560 "Conflicts", "[color=\"red\"]",
561 "ConflictedBy", "[color=\"red\"]",
562 "After", "[color=\"green\"]"
565 const char *c
= NULL
;
572 for (i
= 0; i
< ELEMENTSOF(colors
); i
+= 2)
573 if (streq(colors
[i
], prop
)) {
581 if (arg_dot
!= DOT_ALL
)
582 if ((arg_dot
== DOT_ORDER
) != streq(prop
, "After"))
585 switch (dbus_message_iter_get_arg_type(iter
)) {
587 case DBUS_TYPE_ARRAY
:
589 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
592 dbus_message_iter_recurse(iter
, &sub
);
594 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
597 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
598 dbus_message_iter_get_basic(&sub
, &s
);
599 printf("\t\"%s\"->\"%s\" %s;\n", name
, s
, c
);
601 dbus_message_iter_next(&sub
);
611 static int dot_one(DBusConnection
*bus
, const char *name
, const char *path
) {
612 DBusMessage
*m
= NULL
, *reply
= NULL
;
613 const char *interface
= "org.freedesktop.systemd1.Unit";
616 DBusMessageIter iter
, sub
, sub2
, sub3
;
621 dbus_error_init(&error
);
623 if (!(m
= dbus_message_new_method_call(
624 "org.freedesktop.systemd1",
626 "org.freedesktop.DBus.Properties",
628 log_error("Could not allocate message.");
633 if (!dbus_message_append_args(m
,
634 DBUS_TYPE_STRING
, &interface
,
635 DBUS_TYPE_INVALID
)) {
636 log_error("Could not append arguments to message.");
641 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
642 log_error("Failed to issue method call: %s", bus_error_message(&error
));
647 if (!dbus_message_iter_init(reply
, &iter
) ||
648 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
649 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
650 log_error("Failed to parse reply.");
655 dbus_message_iter_recurse(&iter
, &sub
);
657 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
660 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
661 log_error("Failed to parse reply.");
666 dbus_message_iter_recurse(&sub
, &sub2
);
668 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &prop
, true) < 0) {
669 log_error("Failed to parse reply.");
674 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
675 log_error("Failed to parse reply.");
680 dbus_message_iter_recurse(&sub2
, &sub3
);
682 if (dot_one_property(name
, prop
, &sub3
)) {
683 log_error("Failed to parse reply.");
688 dbus_message_iter_next(&sub
);
695 dbus_message_unref(m
);
698 dbus_message_unref(reply
);
700 dbus_error_free(&error
);
705 static int dot(DBusConnection
*bus
, char **args
, unsigned n
) {
706 DBusMessage
*m
= NULL
, *reply
= NULL
;
709 DBusMessageIter iter
, sub
, sub2
;
711 dbus_error_init(&error
);
715 if (!(m
= dbus_message_new_method_call(
716 "org.freedesktop.systemd1",
717 "/org/freedesktop/systemd1",
718 "org.freedesktop.systemd1.Manager",
720 log_error("Could not allocate message.");
724 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
725 log_error("Failed to issue method call: %s", bus_error_message(&error
));
730 if (!dbus_message_iter_init(reply
, &iter
) ||
731 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
732 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
733 log_error("Failed to parse reply.");
738 printf("digraph systemd {\n");
740 dbus_message_iter_recurse(&iter
, &sub
);
741 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
742 const char *id
, *description
, *load_state
, *active_state
, *sub_state
, *following
, *unit_path
;
744 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
745 log_error("Failed to parse reply.");
750 dbus_message_iter_recurse(&sub
, &sub2
);
752 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &id
, true) < 0 ||
753 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &description
, true) < 0 ||
754 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &load_state
, true) < 0 ||
755 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &active_state
, true) < 0 ||
756 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &sub_state
, true) < 0 ||
757 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &following
, true) < 0 ||
758 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, true) < 0) {
759 log_error("Failed to parse reply.");
764 if ((r
= dot_one(bus
, id
, unit_path
)) < 0)
767 /* printf("\t\"%s\";\n", id); */
768 dbus_message_iter_next(&sub
);
773 log_info(" Color legend: black = Requires\n"
774 " dark blue = Requisite\n"
775 " dark grey = Wants\n"
779 if (isatty(fileno(stdout
)))
780 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
781 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
787 dbus_message_unref(m
);
790 dbus_message_unref(reply
);
792 dbus_error_free(&error
);
797 static int list_jobs(DBusConnection
*bus
, char **args
, unsigned n
) {
798 DBusMessage
*m
= NULL
, *reply
= NULL
;
801 DBusMessageIter iter
, sub
, sub2
;
804 dbus_error_init(&error
);
810 if (!(m
= dbus_message_new_method_call(
811 "org.freedesktop.systemd1",
812 "/org/freedesktop/systemd1",
813 "org.freedesktop.systemd1.Manager",
815 log_error("Could not allocate message.");
819 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
820 log_error("Failed to issue method call: %s", bus_error_message(&error
));
825 if (!dbus_message_iter_init(reply
, &iter
) ||
826 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
827 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRUCT
) {
828 log_error("Failed to parse reply.");
833 dbus_message_iter_recurse(&iter
, &sub
);
835 if (isatty(STDOUT_FILENO
))
836 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
838 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
839 const char *name
, *type
, *state
, *job_path
, *unit_path
;
843 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRUCT
) {
844 log_error("Failed to parse reply.");
849 dbus_message_iter_recurse(&sub
, &sub2
);
851 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &id
, true) < 0 ||
852 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0 ||
853 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) < 0 ||
854 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &state
, true) < 0 ||
855 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &job_path
, true) < 0 ||
856 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
, false) < 0) {
857 log_error("Failed to parse reply.");
862 e
= arg_full
? NULL
: ellipsize(name
, 25, 33);
863 printf("%4u %-25s %-15s %-7s\n", id
, e
? e
: name
, type
, state
);
868 dbus_message_iter_next(&sub
);
871 if (isatty(STDOUT_FILENO
))
872 printf("\n%u jobs listed.\n", k
);
878 dbus_message_unref(m
);
881 dbus_message_unref(reply
);
883 dbus_error_free(&error
);
888 static int load_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
889 DBusMessage
*m
= NULL
, *reply
= NULL
;
894 dbus_error_init(&error
);
899 for (i
= 1; i
< n
; i
++) {
901 if (!(m
= dbus_message_new_method_call(
902 "org.freedesktop.systemd1",
903 "/org/freedesktop/systemd1",
904 "org.freedesktop.systemd1.Manager",
906 log_error("Could not allocate message.");
911 if (!dbus_message_append_args(m
,
912 DBUS_TYPE_STRING
, &args
[i
],
913 DBUS_TYPE_INVALID
)) {
914 log_error("Could not append arguments to message.");
919 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
920 log_error("Failed to issue method call: %s", bus_error_message(&error
));
925 dbus_message_unref(m
);
926 dbus_message_unref(reply
);
935 dbus_message_unref(m
);
938 dbus_message_unref(reply
);
940 dbus_error_free(&error
);
945 static int cancel_job(DBusConnection
*bus
, char **args
, unsigned n
) {
946 DBusMessage
*m
= NULL
, *reply
= NULL
;
951 dbus_error_init(&error
);
957 return daemon_reload(bus
, args
, n
);
959 for (i
= 1; i
< n
; i
++) {
963 if (!(m
= dbus_message_new_method_call(
964 "org.freedesktop.systemd1",
965 "/org/freedesktop/systemd1",
966 "org.freedesktop.systemd1.Manager",
968 log_error("Could not allocate message.");
973 if ((r
= safe_atou(args
[i
], &id
)) < 0) {
974 log_error("Failed to parse job id: %s", strerror(-r
));
978 assert_cc(sizeof(uint32_t) == sizeof(id
));
979 if (!dbus_message_append_args(m
,
980 DBUS_TYPE_UINT32
, &id
,
981 DBUS_TYPE_INVALID
)) {
982 log_error("Could not append arguments to message.");
987 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
988 log_error("Failed to issue method call: %s", bus_error_message(&error
));
993 if (!dbus_message_get_args(reply
, &error
,
994 DBUS_TYPE_OBJECT_PATH
, &path
,
995 DBUS_TYPE_INVALID
)) {
996 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1001 dbus_message_unref(m
);
1002 if (!(m
= dbus_message_new_method_call(
1003 "org.freedesktop.systemd1",
1005 "org.freedesktop.systemd1.Job",
1007 log_error("Could not allocate message.");
1012 dbus_message_unref(reply
);
1013 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1014 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1019 dbus_message_unref(m
);
1020 dbus_message_unref(reply
);
1028 dbus_message_unref(m
);
1031 dbus_message_unref(reply
);
1033 dbus_error_free(&error
);
1038 static bool need_daemon_reload(DBusConnection
*bus
, const char *unit
) {
1039 DBusMessage
*m
= NULL
, *reply
= NULL
;
1040 dbus_bool_t b
= FALSE
;
1041 DBusMessageIter iter
, sub
;
1043 *interface
= "org.freedesktop.systemd1.Unit",
1044 *property
= "NeedDaemonReload",
1047 /* We ignore all errors here, since this is used to show a warning only */
1049 if (!(m
= dbus_message_new_method_call(
1050 "org.freedesktop.systemd1",
1051 "/org/freedesktop/systemd1",
1052 "org.freedesktop.systemd1.Manager",
1056 if (!dbus_message_append_args(m
,
1057 DBUS_TYPE_STRING
, &unit
,
1061 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
)))
1064 if (!dbus_message_get_args(reply
, NULL
,
1065 DBUS_TYPE_OBJECT_PATH
, &path
,
1069 dbus_message_unref(m
);
1070 if (!(m
= dbus_message_new_method_call(
1071 "org.freedesktop.systemd1",
1073 "org.freedesktop.DBus.Properties",
1077 if (!dbus_message_append_args(m
,
1078 DBUS_TYPE_STRING
, &interface
,
1079 DBUS_TYPE_STRING
, &property
,
1080 DBUS_TYPE_INVALID
)) {
1084 dbus_message_unref(reply
);
1085 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, NULL
)))
1088 if (!dbus_message_iter_init(reply
, &iter
) ||
1089 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
)
1092 dbus_message_iter_recurse(&iter
, &sub
);
1094 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_BOOLEAN
)
1097 dbus_message_iter_get_basic(&sub
, &b
);
1101 dbus_message_unref(m
);
1104 dbus_message_unref(reply
);
1109 typedef struct WaitData
{
1114 static DBusHandlerResult
wait_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
1122 dbus_error_init(&error
);
1124 log_debug("Got D-Bus request: %s.%s() on %s",
1125 dbus_message_get_interface(message
),
1126 dbus_message_get_member(message
),
1127 dbus_message_get_path(message
));
1129 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
1130 log_error("Warning! D-Bus connection terminated.");
1131 dbus_connection_close(connection
);
1133 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1136 dbus_bool_t success
= true;
1138 if (!dbus_message_get_args(message
, &error
,
1139 DBUS_TYPE_UINT32
, &id
,
1140 DBUS_TYPE_OBJECT_PATH
, &path
,
1141 DBUS_TYPE_BOOLEAN
, &success
,
1143 log_error("Failed to parse message: %s", bus_error_message(&error
));
1147 if ((p
= set_remove(d
->set
, (char*) path
)))
1155 dbus_error_free(&error
);
1156 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1159 static int enable_wait_for_jobs(DBusConnection
*bus
) {
1167 dbus_error_init(&error
);
1168 dbus_bus_add_match(bus
,
1170 "sender='org.freedesktop.systemd1',"
1171 "interface='org.freedesktop.systemd1.Manager',"
1172 "member='JobRemoved',"
1173 "path='/org/freedesktop/systemd1'",
1176 if (dbus_error_is_set(&error
)) {
1177 log_error("Failed to add match: %s", bus_error_message(&error
));
1178 dbus_error_free(&error
);
1182 /* This is slightly dirty, since we don't undo the match registrations. */
1186 static int wait_for_jobs(DBusConnection
*bus
, Set
*s
) {
1197 if (!dbus_connection_add_filter(bus
, wait_filter
, &d
, NULL
)) {
1198 log_error("Failed to add filter.");
1203 while (!set_isempty(s
) &&
1204 dbus_connection_read_write_dispatch(bus
, -1))
1207 if (!arg_quiet
&& d
.failed
)
1208 log_error("Job failed. See system logs and 'systemctl status' for details.");
1210 r
= d
.failed
? -EIO
: 0;
1213 /* This is slightly dirty, since we don't undo the filter registration. */
1218 static int start_unit_one(
1219 DBusConnection
*bus
,
1226 DBusMessage
*m
= NULL
, *reply
= NULL
;
1235 assert(arg_no_block
|| s
);
1237 if (!(m
= dbus_message_new_method_call(
1238 "org.freedesktop.systemd1",
1239 "/org/freedesktop/systemd1",
1240 "org.freedesktop.systemd1.Manager",
1242 log_error("Could not allocate message.");
1247 if (!dbus_message_append_args(m
,
1248 DBUS_TYPE_STRING
, &name
,
1249 DBUS_TYPE_STRING
, &mode
,
1250 DBUS_TYPE_INVALID
)) {
1251 log_error("Could not append arguments to message.");
1256 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, error
))) {
1258 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(error
)) {
1259 /* There's always a fallback possible for
1260 * legacy actions. */
1265 log_error("Failed to issue method call: %s", bus_error_message(error
));
1270 if (!dbus_message_get_args(reply
, error
,
1271 DBUS_TYPE_OBJECT_PATH
, &path
,
1272 DBUS_TYPE_INVALID
)) {
1273 log_error("Failed to parse reply: %s", bus_error_message(error
));
1278 if (need_daemon_reload(bus
, name
))
1279 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1280 arg_user
? "--user" : "--system");
1282 if (!arg_no_block
) {
1285 if (!(p
= strdup(path
))) {
1286 log_error("Failed to duplicate path.");
1291 if ((r
= set_put(s
, p
)) < 0) {
1293 log_error("Failed to add path to set.");
1302 dbus_message_unref(m
);
1305 dbus_message_unref(reply
);
1310 static enum action
verb_to_action(const char *verb
) {
1311 if (streq(verb
, "halt"))
1313 else if (streq(verb
, "poweroff"))
1314 return ACTION_POWEROFF
;
1315 else if (streq(verb
, "reboot"))
1316 return ACTION_REBOOT
;
1317 else if (streq(verb
, "kexec"))
1318 return ACTION_KEXEC
;
1319 else if (streq(verb
, "rescue"))
1320 return ACTION_RESCUE
;
1321 else if (streq(verb
, "emergency"))
1322 return ACTION_EMERGENCY
;
1323 else if (streq(verb
, "default"))
1324 return ACTION_DEFAULT
;
1325 else if (streq(verb
, "exit"))
1328 return ACTION_INVALID
;
1331 static int start_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1333 static const char * const table
[_ACTION_MAX
] = {
1334 [ACTION_HALT
] = SPECIAL_HALT_TARGET
,
1335 [ACTION_POWEROFF
] = SPECIAL_POWEROFF_TARGET
,
1336 [ACTION_REBOOT
] = SPECIAL_REBOOT_TARGET
,
1337 [ACTION_KEXEC
] = SPECIAL_KEXEC_TARGET
,
1338 [ACTION_RUNLEVEL2
] = SPECIAL_RUNLEVEL2_TARGET
,
1339 [ACTION_RUNLEVEL3
] = SPECIAL_RUNLEVEL3_TARGET
,
1340 [ACTION_RUNLEVEL4
] = SPECIAL_RUNLEVEL4_TARGET
,
1341 [ACTION_RUNLEVEL5
] = SPECIAL_RUNLEVEL5_TARGET
,
1342 [ACTION_RESCUE
] = SPECIAL_RESCUE_TARGET
,
1343 [ACTION_EMERGENCY
] = SPECIAL_EMERGENCY_TARGET
,
1344 [ACTION_DEFAULT
] = SPECIAL_DEFAULT_TARGET
,
1345 [ACTION_EXIT
] = SPECIAL_EXIT_TARGET
1350 const char *method
, *mode
, *one_name
;
1354 dbus_error_init(&error
);
1358 spawn_ask_password_agent();
1360 if (arg_action
== ACTION_SYSTEMCTL
) {
1362 streq(args
[0], "stop") ? "StopUnit" :
1363 streq(args
[0], "reload") ? "ReloadUnit" :
1364 streq(args
[0], "restart") ? "RestartUnit" :
1365 streq(args
[0], "try-restart") ||
1366 streq(args
[0], "condrestart") ? "TryRestartUnit" :
1367 streq(args
[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1368 streq(args
[0], "reload-or-try-restart") ||
1369 streq(args
[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1373 (streq(args
[0], "isolate") ||
1374 streq(args
[0], "rescue") ||
1375 streq(args
[0], "emergency")) ? "isolate" :
1379 one_name
= table
[verb_to_action(args
[0])];
1382 assert(arg_action
< ELEMENTSOF(table
));
1383 assert(table
[arg_action
]);
1385 method
= "StartUnit";
1387 mode
= (arg_action
== ACTION_EMERGENCY
||
1388 arg_action
== ACTION_RESCUE
||
1389 arg_action
== ACTION_RUNLEVEL2
||
1390 arg_action
== ACTION_RUNLEVEL3
||
1391 arg_action
== ACTION_RUNLEVEL4
||
1392 arg_action
== ACTION_RUNLEVEL5
) ? "isolate" : "replace";
1394 one_name
= table
[arg_action
];
1397 if (!arg_no_block
) {
1398 if ((ret
= enable_wait_for_jobs(bus
)) < 0) {
1399 log_error("Could not watch jobs: %s", strerror(-ret
));
1403 if (!(s
= set_new(string_hash_func
, string_compare_func
))) {
1404 log_error("Failed to allocate set.");
1411 if ((ret
= start_unit_one(bus
, method
, one_name
, mode
, &error
, s
)) <= 0)
1414 for (i
= 1; i
< n
; i
++)
1415 if ((r
= start_unit_one(bus
, method
, args
[i
], mode
, &error
, s
)) != 0) {
1416 ret
= translate_bus_error_to_exit_status(r
, &error
);
1417 dbus_error_free(&error
);
1422 if ((r
= wait_for_jobs(bus
, s
)) < 0) {
1431 dbus_error_free(&error
);
1436 static int start_special(DBusConnection
*bus
, char **args
, unsigned n
) {
1443 (streq(args
[0], "halt") ||
1444 streq(args
[0], "poweroff") ||
1445 streq(args
[0], "reboot") ||
1446 streq(args
[0], "kexec") ||
1447 streq(args
[0], "exit")))
1448 return daemon_reload(bus
, args
, n
);
1450 r
= start_unit(bus
, args
, n
);
1453 warn_wall(verb_to_action(args
[0]));
1458 static int check_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1459 DBusMessage
*m
= NULL
, *reply
= NULL
;
1461 *interface
= "org.freedesktop.systemd1.Unit",
1462 *property
= "ActiveState";
1463 int r
= 3; /* According to LSB: "program is not running" */
1470 dbus_error_init(&error
);
1472 for (i
= 1; i
< n
; i
++) {
1473 const char *path
= NULL
;
1475 DBusMessageIter iter
, sub
;
1477 if (!(m
= dbus_message_new_method_call(
1478 "org.freedesktop.systemd1",
1479 "/org/freedesktop/systemd1",
1480 "org.freedesktop.systemd1.Manager",
1482 log_error("Could not allocate message.");
1487 if (!dbus_message_append_args(m
,
1488 DBUS_TYPE_STRING
, &args
[i
],
1489 DBUS_TYPE_INVALID
)) {
1490 log_error("Could not append arguments to message.");
1495 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1497 /* Hmm, cannot figure out anything about this unit... */
1501 dbus_error_free(&error
);
1502 dbus_message_unref(m
);
1506 if (!dbus_message_get_args(reply
, &error
,
1507 DBUS_TYPE_OBJECT_PATH
, &path
,
1508 DBUS_TYPE_INVALID
)) {
1509 log_error("Failed to parse reply: %s", bus_error_message(&error
));
1514 dbus_message_unref(m
);
1515 if (!(m
= dbus_message_new_method_call(
1516 "org.freedesktop.systemd1",
1518 "org.freedesktop.DBus.Properties",
1520 log_error("Could not allocate message.");
1525 if (!dbus_message_append_args(m
,
1526 DBUS_TYPE_STRING
, &interface
,
1527 DBUS_TYPE_STRING
, &property
,
1528 DBUS_TYPE_INVALID
)) {
1529 log_error("Could not append arguments to message.");
1534 dbus_message_unref(reply
);
1535 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1536 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1541 if (!dbus_message_iter_init(reply
, &iter
) ||
1542 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
1543 log_error("Failed to parse reply.");
1548 dbus_message_iter_recurse(&iter
, &sub
);
1550 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
1551 log_error("Failed to parse reply.");
1556 dbus_message_iter_get_basic(&sub
, &state
);
1561 if (streq(state
, "active") || streq(state
, "reloading"))
1564 dbus_message_unref(m
);
1565 dbus_message_unref(reply
);
1571 dbus_message_unref(m
);
1574 dbus_message_unref(reply
);
1576 dbus_error_free(&error
);
1581 static int kill_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
1582 DBusMessage
*m
= NULL
, *reply
= NULL
;
1590 dbus_error_init(&error
);
1593 arg_kill_who
= "all";
1596 arg_kill_mode
= streq(arg_kill_who
, "all") ? "control-group" : "process";
1598 for (i
= 1; i
< n
; i
++) {
1600 if (!(m
= dbus_message_new_method_call(
1601 "org.freedesktop.systemd1",
1602 "/org/freedesktop/systemd1",
1603 "org.freedesktop.systemd1.Manager",
1605 log_error("Could not allocate message.");
1610 if (!dbus_message_append_args(m
,
1611 DBUS_TYPE_STRING
, &args
[i
],
1612 DBUS_TYPE_STRING
, &arg_kill_who
,
1613 DBUS_TYPE_STRING
, &arg_kill_mode
,
1614 DBUS_TYPE_INT32
, &arg_signal
,
1615 DBUS_TYPE_INVALID
)) {
1616 log_error("Could not append arguments to message.");
1621 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
1622 log_error("Failed to issue method call: %s", bus_error_message(&error
));
1623 dbus_error_free(&error
);
1627 dbus_message_unref(m
);
1630 dbus_message_unref(reply
);
1636 dbus_message_unref(m
);
1639 dbus_message_unref(reply
);
1641 dbus_error_free(&error
);
1646 typedef struct ExecStatusInfo
{
1654 usec_t start_timestamp
;
1655 usec_t exit_timestamp
;
1660 LIST_FIELDS(struct ExecStatusInfo
, exec
);
1663 static void exec_status_info_free(ExecStatusInfo
*i
) {
1672 static int exec_status_info_deserialize(DBusMessageIter
*sub
, ExecStatusInfo
*i
) {
1673 uint64_t start_timestamp
, exit_timestamp
;
1674 DBusMessageIter sub2
, sub3
;
1678 int32_t code
, status
;
1684 if (dbus_message_iter_get_arg_type(sub
) != DBUS_TYPE_STRUCT
)
1687 dbus_message_iter_recurse(sub
, &sub2
);
1689 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, true) < 0)
1692 if (!(i
->path
= strdup(path
)))
1695 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_ARRAY
||
1696 dbus_message_iter_get_element_type(&sub2
) != DBUS_TYPE_STRING
)
1700 dbus_message_iter_recurse(&sub2
, &sub3
);
1701 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
1702 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
1703 dbus_message_iter_next(&sub3
);
1708 if (!(i
->argv
= new0(char*, n
+1)))
1712 dbus_message_iter_recurse(&sub2
, &sub3
);
1713 while (dbus_message_iter_get_arg_type(&sub3
) != DBUS_TYPE_INVALID
) {
1716 assert(dbus_message_iter_get_arg_type(&sub3
) == DBUS_TYPE_STRING
);
1717 dbus_message_iter_get_basic(&sub3
, &s
);
1718 dbus_message_iter_next(&sub3
);
1720 if (!(i
->argv
[n
++] = strdup(s
)))
1724 if (!dbus_message_iter_next(&sub2
) ||
1725 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_BOOLEAN
, &ignore
, true) < 0 ||
1726 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &start_timestamp
, true) < 0 ||
1727 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &exit_timestamp
, true) < 0 ||
1728 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT32
, &pid
, true) < 0 ||
1729 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &code
, true) < 0 ||
1730 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_INT32
, &status
, false) < 0)
1734 i
->start_timestamp
= (usec_t
) start_timestamp
;
1735 i
->exit_timestamp
= (usec_t
) exit_timestamp
;
1736 i
->pid
= (pid_t
) pid
;
1743 typedef struct UnitStatusInfo
{
1745 const char *load_state
;
1746 const char *active_state
;
1747 const char *sub_state
;
1749 const char *description
;
1750 const char *following
;
1753 const char *default_control_group
;
1755 usec_t inactive_exit_timestamp
;
1756 usec_t active_enter_timestamp
;
1757 usec_t active_exit_timestamp
;
1758 usec_t inactive_enter_timestamp
;
1760 bool need_daemon_reload
;
1765 const char *status_text
;
1767 #ifdef HAVE_SYSV_COMPAT
1771 usec_t start_timestamp
;
1772 usec_t exit_timestamp
;
1774 int exit_code
, exit_status
;
1777 unsigned n_accepted
;
1778 unsigned n_connections
;
1782 const char *sysfs_path
;
1784 /* Mount, Automount */
1790 LIST_HEAD(ExecStatusInfo
, exec
);
1793 static void print_status_info(UnitStatusInfo
*i
) {
1795 const char *on
, *off
, *ss
;
1797 char since1
[FORMAT_TIMESTAMP_PRETTY_MAX
], *s1
;
1798 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
1802 /* This shows pretty information about a unit. See
1803 * print_property() for a low-level property printer */
1805 printf("%s", strna(i
->id
));
1807 if (i
->description
&& !streq_ptr(i
->id
, i
->description
))
1808 printf(" - %s", i
->description
);
1813 printf("\t Follow: unit currently follows state of %s\n", i
->following
);
1815 if (streq_ptr(i
->load_state
, "failed") ||
1816 streq_ptr(i
->load_state
, "banned")) {
1817 on
= ansi_highlight(true);
1818 off
= ansi_highlight(false);
1823 printf("\t Loaded: %s%s%s (%s)\n", on
, strna(i
->load_state
), off
, i
->path
);
1825 printf("\t Loaded: %s%s%s\n", on
, strna(i
->load_state
), off
);
1827 ss
= streq_ptr(i
->active_state
, i
->sub_state
) ? NULL
: i
->sub_state
;
1829 if (streq_ptr(i
->active_state
, "failed")) {
1830 on
= ansi_highlight(true);
1831 off
= ansi_highlight(false);
1832 } else if (streq_ptr(i
->active_state
, "active") || streq_ptr(i
->active_state
, "reloading")) {
1833 on
= ansi_highlight_green(true);
1834 off
= ansi_highlight_green(false);
1839 printf("\t Active: %s%s (%s)%s",
1841 strna(i
->active_state
),
1845 printf("\t Active: %s%s%s",
1847 strna(i
->active_state
),
1850 timestamp
= (streq_ptr(i
->active_state
, "active") ||
1851 streq_ptr(i
->active_state
, "reloading")) ? i
->active_enter_timestamp
:
1852 (streq_ptr(i
->active_state
, "inactive") ||
1853 streq_ptr(i
->active_state
, "failed")) ? i
->inactive_enter_timestamp
:
1854 streq_ptr(i
->active_state
, "activating") ? i
->inactive_exit_timestamp
:
1855 i
->active_exit_timestamp
;
1857 s1
= format_timestamp_pretty(since1
, sizeof(since1
), timestamp
);
1858 s2
= format_timestamp(since2
, sizeof(since2
), timestamp
);
1861 printf(" since %s; %s\n", s2
, s1
);
1863 printf(" since %s\n", s2
);
1868 printf("\t Device: %s\n", i
->sysfs_path
);
1870 printf("\t Where: %s\n", i
->where
);
1872 printf("\t What: %s\n", i
->what
);
1875 printf("\tAccepted: %u; Connected: %u\n", i
->n_accepted
, i
->n_connections
);
1877 LIST_FOREACH(exec
, p
, i
->exec
) {
1881 /* Only show exited processes here */
1885 t
= strv_join(p
->argv
, " ");
1886 printf("\t Process: %u %s=%s ", p
->pid
, p
->name
, strna(t
));
1889 #ifdef HAVE_SYSV_COMPAT
1891 good
= is_clean_exit_lsb(p
->code
, p
->status
);
1894 good
= is_clean_exit(p
->code
, p
->status
);
1897 on
= ansi_highlight(true);
1898 off
= ansi_highlight(false);
1902 printf("%s(code=%s, ", on
, sigchld_code_to_string(p
->code
));
1904 if (p
->code
== CLD_EXITED
) {
1907 printf("status=%i", p
->status
);
1909 #ifdef HAVE_SYSV_COMPAT
1910 if ((c
= exit_status_to_string(p
->status
, i
->is_sysv
? EXIT_STATUS_LSB
: EXIT_STATUS_SYSTEMD
)))
1912 if ((c
= exit_status_to_string(p
->status
, EXIT_STATUS_SYSTEMD
)))
1917 printf("signal=%s", signal_to_string(p
->status
));
1919 printf(")%s\n", off
);
1923 if (i
->main_pid
== p
->pid
&&
1924 i
->start_timestamp
== p
->start_timestamp
&&
1925 i
->exit_timestamp
== p
->start_timestamp
)
1926 /* Let's not show this twice */
1929 if (p
->pid
== i
->control_pid
)
1933 if (i
->main_pid
> 0 || i
->control_pid
> 0) {
1936 if (i
->main_pid
> 0) {
1937 printf("Main PID: %u", (unsigned) i
->main_pid
);
1941 get_process_name(i
->main_pid
, &t
);
1946 } else if (i
->exit_code
> 0) {
1947 printf(" (code=%s, ", sigchld_code_to_string(i
->exit_code
));
1949 if (i
->exit_code
== CLD_EXITED
) {
1952 printf("status=%i", i
->exit_status
);
1954 #ifdef HAVE_SYSV_COMPAT
1955 if ((c
= exit_status_to_string(i
->exit_status
, i
->is_sysv
? EXIT_STATUS_LSB
: EXIT_STATUS_SYSTEMD
)))
1957 if ((c
= exit_status_to_string(i
->exit_status
, EXIT_STATUS_SYSTEMD
)))
1962 printf("signal=%s", signal_to_string(i
->exit_status
));
1967 if (i
->main_pid
> 0 && i
->control_pid
> 0)
1970 if (i
->control_pid
> 0) {
1973 printf(" Control: %u", (unsigned) i
->control_pid
);
1975 get_process_name(i
->control_pid
, &t
);
1986 printf("\t Status: \"%s\"\n", i
->status_text
);
1988 if (i
->default_control_group
) {
1991 printf("\t CGroup: %s\n", i
->default_control_group
);
1993 if ((c
= columns()) > 18)
1998 show_cgroup_by_path(i
->default_control_group
, "\t\t ", c
);
2001 if (i
->need_daemon_reload
)
2002 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2003 ansi_highlight(true),
2004 ansi_highlight(false),
2005 arg_user
? "--user" : "--system");
2008 static int status_property(const char *name
, DBusMessageIter
*iter
, UnitStatusInfo
*i
) {
2010 switch (dbus_message_iter_get_arg_type(iter
)) {
2012 case DBUS_TYPE_STRING
: {
2015 dbus_message_iter_get_basic(iter
, &s
);
2018 if (streq(name
, "Id"))
2020 else if (streq(name
, "LoadState"))
2022 else if (streq(name
, "ActiveState"))
2023 i
->active_state
= s
;
2024 else if (streq(name
, "SubState"))
2026 else if (streq(name
, "Description"))
2028 else if (streq(name
, "FragmentPath"))
2030 #ifdef HAVE_SYSV_COMPAT
2031 else if (streq(name
, "SysVPath")) {
2036 else if (streq(name
, "DefaultControlGroup"))
2037 i
->default_control_group
= s
;
2038 else if (streq(name
, "StatusText"))
2040 else if (streq(name
, "SysFSPath"))
2042 else if (streq(name
, "Where"))
2044 else if (streq(name
, "What"))
2046 else if (streq(name
, "Following"))
2053 case DBUS_TYPE_BOOLEAN
: {
2056 dbus_message_iter_get_basic(iter
, &b
);
2058 if (streq(name
, "Accept"))
2060 else if (streq(name
, "NeedDaemonReload"))
2061 i
->need_daemon_reload
= b
;
2066 case DBUS_TYPE_UINT32
: {
2069 dbus_message_iter_get_basic(iter
, &u
);
2071 if (streq(name
, "MainPID")) {
2073 i
->main_pid
= (pid_t
) u
;
2076 } else if (streq(name
, "ControlPID"))
2077 i
->control_pid
= (pid_t
) u
;
2078 else if (streq(name
, "ExecMainPID")) {
2080 i
->main_pid
= (pid_t
) u
;
2081 } else if (streq(name
, "NAccepted"))
2083 else if (streq(name
, "NConnections"))
2084 i
->n_connections
= u
;
2089 case DBUS_TYPE_INT32
: {
2092 dbus_message_iter_get_basic(iter
, &j
);
2094 if (streq(name
, "ExecMainCode"))
2095 i
->exit_code
= (int) j
;
2096 else if (streq(name
, "ExecMainStatus"))
2097 i
->exit_status
= (int) j
;
2102 case DBUS_TYPE_UINT64
: {
2105 dbus_message_iter_get_basic(iter
, &u
);
2107 if (streq(name
, "ExecMainStartTimestamp"))
2108 i
->start_timestamp
= (usec_t
) u
;
2109 else if (streq(name
, "ExecMainExitTimestamp"))
2110 i
->exit_timestamp
= (usec_t
) u
;
2111 else if (streq(name
, "ActiveEnterTimestamp"))
2112 i
->active_enter_timestamp
= (usec_t
) u
;
2113 else if (streq(name
, "InactiveEnterTimestamp"))
2114 i
->inactive_enter_timestamp
= (usec_t
) u
;
2115 else if (streq(name
, "InactiveExitTimestamp"))
2116 i
->inactive_exit_timestamp
= (usec_t
) u
;
2117 else if (streq(name
, "ActiveExitTimestamp"))
2118 i
->active_exit_timestamp
= (usec_t
) u
;
2123 case DBUS_TYPE_ARRAY
: {
2125 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&&
2126 startswith(name
, "Exec")) {
2127 DBusMessageIter sub
;
2129 dbus_message_iter_recurse(iter
, &sub
);
2130 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2131 ExecStatusInfo
*info
;
2134 if (!(info
= new0(ExecStatusInfo
, 1)))
2137 if (!(info
->name
= strdup(name
))) {
2142 if ((r
= exec_status_info_deserialize(&sub
, info
)) < 0) {
2147 LIST_PREPEND(ExecStatusInfo
, exec
, i
->exec
, info
);
2149 dbus_message_iter_next(&sub
);
2160 static int print_property(const char *name
, DBusMessageIter
*iter
) {
2164 /* This is a low-level property printer, see
2165 * print_status_info() for the nicer output */
2167 if (arg_property
&& !strv_find(arg_property
, name
))
2170 switch (dbus_message_iter_get_arg_type(iter
)) {
2172 case DBUS_TYPE_STRING
: {
2174 dbus_message_iter_get_basic(iter
, &s
);
2176 if (arg_all
|| s
[0])
2177 printf("%s=%s\n", name
, s
);
2182 case DBUS_TYPE_BOOLEAN
: {
2184 dbus_message_iter_get_basic(iter
, &b
);
2185 printf("%s=%s\n", name
, yes_no(b
));
2190 case DBUS_TYPE_UINT64
: {
2192 dbus_message_iter_get_basic(iter
, &u
);
2194 /* Yes, heuristics! But we can change this check
2195 * should it turn out to not be sufficient */
2197 if (strstr(name
, "Timestamp")) {
2198 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
2200 if ((t
= format_timestamp(timestamp
, sizeof(timestamp
), u
)) || arg_all
)
2201 printf("%s=%s\n", name
, strempty(t
));
2202 } else if (strstr(name
, "USec")) {
2203 char timespan
[FORMAT_TIMESPAN_MAX
];
2205 printf("%s=%s\n", name
, format_timespan(timespan
, sizeof(timespan
), u
));
2207 printf("%s=%llu\n", name
, (unsigned long long) u
);
2212 case DBUS_TYPE_UINT32
: {
2214 dbus_message_iter_get_basic(iter
, &u
);
2216 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
2217 printf("%s=%04o\n", name
, u
);
2219 printf("%s=%u\n", name
, (unsigned) u
);
2224 case DBUS_TYPE_INT32
: {
2226 dbus_message_iter_get_basic(iter
, &i
);
2228 printf("%s=%i\n", name
, (int) i
);
2232 case DBUS_TYPE_DOUBLE
: {
2234 dbus_message_iter_get_basic(iter
, &d
);
2236 printf("%s=%g\n", name
, d
);
2240 case DBUS_TYPE_STRUCT
: {
2241 DBusMessageIter sub
;
2242 dbus_message_iter_recurse(iter
, &sub
);
2244 if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_UINT32
&& streq(name
, "Job")) {
2247 dbus_message_iter_get_basic(&sub
, &u
);
2250 printf("%s=%u\n", name
, (unsigned) u
);
2252 printf("%s=\n", name
);
2255 } else if (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
&& streq(name
, "Unit")) {
2258 dbus_message_iter_get_basic(&sub
, &s
);
2260 if (arg_all
|| s
[0])
2261 printf("%s=%s\n", name
, s
);
2269 case DBUS_TYPE_ARRAY
:
2271 if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRING
) {
2272 DBusMessageIter sub
;
2275 dbus_message_iter_recurse(iter
, &sub
);
2277 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2278 printf("%s=", name
);
2280 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2283 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
2284 dbus_message_iter_get_basic(&sub
, &s
);
2285 printf("%s%s", space
? " " : "", s
);
2288 dbus_message_iter_next(&sub
);
2296 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_BYTE
) {
2297 DBusMessageIter sub
;
2299 dbus_message_iter_recurse(iter
, &sub
);
2301 dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2302 printf("%s=", name
);
2304 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2307 assert(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_BYTE
);
2308 dbus_message_iter_get_basic(&sub
, &u
);
2311 dbus_message_iter_next(&sub
);
2319 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Paths")) {
2320 DBusMessageIter sub
, sub2
;
2322 dbus_message_iter_recurse(iter
, &sub
);
2323 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2324 const char *type
, *path
;
2326 dbus_message_iter_recurse(&sub
, &sub2
);
2328 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &type
, true) >= 0 &&
2329 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &path
, false) >= 0)
2330 printf("%s=%s\n", type
, path
);
2332 dbus_message_iter_next(&sub
);
2337 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& streq(name
, "Timers")) {
2338 DBusMessageIter sub
, sub2
;
2340 dbus_message_iter_recurse(iter
, &sub
);
2341 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2343 uint64_t value
, next_elapse
;
2345 dbus_message_iter_recurse(&sub
, &sub2
);
2347 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &base
, true) >= 0 &&
2348 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &value
, true) >= 0 &&
2349 bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_UINT64
, &next_elapse
, false) >= 0) {
2350 char timespan1
[FORMAT_TIMESPAN_MAX
], timespan2
[FORMAT_TIMESPAN_MAX
];
2352 printf("%s={ value=%s ; next_elapse=%s }\n",
2354 format_timespan(timespan1
, sizeof(timespan1
), value
),
2355 format_timespan(timespan2
, sizeof(timespan2
), next_elapse
));
2358 dbus_message_iter_next(&sub
);
2363 } else if (dbus_message_iter_get_element_type(iter
) == DBUS_TYPE_STRUCT
&& startswith(name
, "Exec")) {
2364 DBusMessageIter sub
;
2366 dbus_message_iter_recurse(iter
, &sub
);
2367 while (dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRUCT
) {
2368 ExecStatusInfo info
;
2371 if (exec_status_info_deserialize(&sub
, &info
) >= 0) {
2372 char timestamp1
[FORMAT_TIMESTAMP_MAX
], timestamp2
[FORMAT_TIMESTAMP_MAX
];
2375 t
= strv_join(info
.argv
, " ");
2377 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2381 yes_no(info
.ignore
),
2382 strna(format_timestamp(timestamp1
, sizeof(timestamp1
), info
.start_timestamp
)),
2383 strna(format_timestamp(timestamp2
, sizeof(timestamp2
), info
.exit_timestamp
)),
2384 (unsigned) info
. pid
,
2385 sigchld_code_to_string(info
.code
),
2387 info
.code
== CLD_EXITED
? "" : "/",
2388 strempty(info
.code
== CLD_EXITED
? NULL
: signal_to_string(info
.status
)));
2394 strv_free(info
.argv
);
2396 dbus_message_iter_next(&sub
);
2406 printf("%s=[unprintable]\n", name
);
2411 static int show_one(const char *verb
, DBusConnection
*bus
, const char *path
, bool show_properties
, bool *new_line
) {
2412 DBusMessage
*m
= NULL
, *reply
= NULL
;
2413 const char *interface
= "";
2416 DBusMessageIter iter
, sub
, sub2
, sub3
;
2417 UnitStatusInfo info
;
2425 dbus_error_init(&error
);
2427 if (!(m
= dbus_message_new_method_call(
2428 "org.freedesktop.systemd1",
2430 "org.freedesktop.DBus.Properties",
2432 log_error("Could not allocate message.");
2437 if (!dbus_message_append_args(m
,
2438 DBUS_TYPE_STRING
, &interface
,
2439 DBUS_TYPE_INVALID
)) {
2440 log_error("Could not append arguments to message.");
2445 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2446 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2451 if (!dbus_message_iter_init(reply
, &iter
) ||
2452 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
2453 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_DICT_ENTRY
) {
2454 log_error("Failed to parse reply.");
2459 dbus_message_iter_recurse(&iter
, &sub
);
2466 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
2469 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_DICT_ENTRY
) {
2470 log_error("Failed to parse reply.");
2475 dbus_message_iter_recurse(&sub
, &sub2
);
2477 if (bus_iter_get_basic_and_next(&sub2
, DBUS_TYPE_STRING
, &name
, true) < 0) {
2478 log_error("Failed to parse reply.");
2483 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_VARIANT
) {
2484 log_error("Failed to parse reply.");
2489 dbus_message_iter_recurse(&sub2
, &sub3
);
2491 if (show_properties
)
2492 r
= print_property(name
, &sub3
);
2494 r
= status_property(name
, &sub3
, &info
);
2497 log_error("Failed to parse reply.");
2502 dbus_message_iter_next(&sub
);
2507 if (!show_properties
)
2508 print_status_info(&info
);
2510 if (!streq_ptr(info
.active_state
, "active") &&
2511 !streq_ptr(info
.active_state
, "reloading") &&
2512 streq(verb
, "status"))
2513 /* According to LSB: "program not running" */
2516 while ((p
= info
.exec
)) {
2517 LIST_REMOVE(ExecStatusInfo
, exec
, info
.exec
, p
);
2518 exec_status_info_free(p
);
2523 dbus_message_unref(m
);
2526 dbus_message_unref(reply
);
2528 dbus_error_free(&error
);
2533 static int show(DBusConnection
*bus
, char **args
, unsigned n
) {
2534 DBusMessage
*m
= NULL
, *reply
= NULL
;
2538 bool show_properties
, new_line
= false;
2543 dbus_error_init(&error
);
2545 show_properties
= !streq(args
[0], "status");
2547 if (show_properties
)
2550 if (show_properties
&& n
<= 1) {
2551 /* If not argument is specified inspect the manager
2554 ret
= show_one(args
[0], bus
, "/org/freedesktop/systemd1", show_properties
, &new_line
);
2558 for (i
= 1; i
< n
; i
++) {
2559 const char *path
= NULL
;
2562 if (safe_atou32(args
[i
], &id
) < 0) {
2564 /* Interpret as unit name */
2566 if (!(m
= dbus_message_new_method_call(
2567 "org.freedesktop.systemd1",
2568 "/org/freedesktop/systemd1",
2569 "org.freedesktop.systemd1.Manager",
2571 log_error("Could not allocate message.");
2576 if (!dbus_message_append_args(m
,
2577 DBUS_TYPE_STRING
, &args
[i
],
2578 DBUS_TYPE_INVALID
)) {
2579 log_error("Could not append arguments to message.");
2584 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2586 if (!dbus_error_has_name(&error
, DBUS_ERROR_ACCESS_DENIED
)) {
2587 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2592 dbus_error_free(&error
);
2594 dbus_message_unref(m
);
2595 if (!(m
= dbus_message_new_method_call(
2596 "org.freedesktop.systemd1",
2597 "/org/freedesktop/systemd1",
2598 "org.freedesktop.systemd1.Manager",
2600 log_error("Could not allocate message.");
2605 if (!dbus_message_append_args(m
,
2606 DBUS_TYPE_STRING
, &args
[i
],
2607 DBUS_TYPE_INVALID
)) {
2608 log_error("Could not append arguments to message.");
2613 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2614 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2616 if (dbus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
))
2617 ret
= 4; /* According to LSB: "program or service status is unknown" */
2624 } else if (show_properties
) {
2626 /* Interpret as job id */
2628 if (!(m
= dbus_message_new_method_call(
2629 "org.freedesktop.systemd1",
2630 "/org/freedesktop/systemd1",
2631 "org.freedesktop.systemd1.Manager",
2633 log_error("Could not allocate message.");
2638 if (!dbus_message_append_args(m
,
2639 DBUS_TYPE_UINT32
, &id
,
2640 DBUS_TYPE_INVALID
)) {
2641 log_error("Could not append arguments to message.");
2646 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2647 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2653 /* Interpret as PID */
2655 if (!(m
= dbus_message_new_method_call(
2656 "org.freedesktop.systemd1",
2657 "/org/freedesktop/systemd1",
2658 "org.freedesktop.systemd1.Manager",
2660 log_error("Could not allocate message.");
2665 if (!dbus_message_append_args(m
,
2666 DBUS_TYPE_UINT32
, &id
,
2667 DBUS_TYPE_INVALID
)) {
2668 log_error("Could not append arguments to message.");
2673 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2674 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2680 if (!dbus_message_get_args(reply
, &error
,
2681 DBUS_TYPE_OBJECT_PATH
, &path
,
2682 DBUS_TYPE_INVALID
)) {
2683 log_error("Failed to parse reply: %s", bus_error_message(&error
));
2688 if ((r
= show_one(args
[0], bus
, path
, show_properties
, &new_line
)) != 0)
2691 dbus_message_unref(m
);
2692 dbus_message_unref(reply
);
2698 dbus_message_unref(m
);
2701 dbus_message_unref(reply
);
2703 dbus_error_free(&error
);
2708 static DBusHandlerResult
monitor_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
2710 DBusMessage
*m
= NULL
, *reply
= NULL
;
2715 dbus_error_init(&error
);
2717 log_debug("Got D-Bus request: %s.%s() on %s",
2718 dbus_message_get_interface(message
),
2719 dbus_message_get_member(message
),
2720 dbus_message_get_path(message
));
2722 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
2723 log_error("Warning! D-Bus connection terminated.");
2724 dbus_connection_close(connection
);
2726 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2727 dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2728 const char *id
, *path
;
2730 if (!dbus_message_get_args(message
, &error
,
2731 DBUS_TYPE_STRING
, &id
,
2732 DBUS_TYPE_OBJECT_PATH
, &path
,
2734 log_error("Failed to parse message: %s", bus_error_message(&error
));
2735 else if (streq(dbus_message_get_member(message
), "UnitNew"))
2736 printf("Unit %s added.\n", id
);
2738 printf("Unit %s removed.\n", id
);
2740 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobNew") ||
2741 dbus_message_is_signal(message
, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2745 if (!dbus_message_get_args(message
, &error
,
2746 DBUS_TYPE_UINT32
, &id
,
2747 DBUS_TYPE_OBJECT_PATH
, &path
,
2749 log_error("Failed to parse message: %s", bus_error_message(&error
));
2750 else if (streq(dbus_message_get_member(message
), "JobNew"))
2751 printf("Job %u added.\n", id
);
2753 printf("Job %u removed.\n", id
);
2756 } else if (dbus_message_is_signal(message
, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2758 const char *path
, *interface
, *property
= "Id";
2759 DBusMessageIter iter
, sub
;
2761 path
= dbus_message_get_path(message
);
2763 if (!dbus_message_get_args(message
, &error
,
2764 DBUS_TYPE_STRING
, &interface
,
2765 DBUS_TYPE_INVALID
)) {
2766 log_error("Failed to parse message: %s", bus_error_message(&error
));
2770 if (!streq(interface
, "org.freedesktop.systemd1.Job") &&
2771 !streq(interface
, "org.freedesktop.systemd1.Unit"))
2774 if (!(m
= dbus_message_new_method_call(
2775 "org.freedesktop.systemd1",
2777 "org.freedesktop.DBus.Properties",
2779 log_error("Could not allocate message.");
2783 if (!dbus_message_append_args(m
,
2784 DBUS_TYPE_STRING
, &interface
,
2785 DBUS_TYPE_STRING
, &property
,
2786 DBUS_TYPE_INVALID
)) {
2787 log_error("Could not append arguments to message.");
2791 if (!(reply
= dbus_connection_send_with_reply_and_block(connection
, m
, -1, &error
))) {
2792 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2796 if (!dbus_message_iter_init(reply
, &iter
) ||
2797 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
2798 log_error("Failed to parse reply.");
2802 dbus_message_iter_recurse(&iter
, &sub
);
2804 if (streq(interface
, "org.freedesktop.systemd1.Unit")) {
2807 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
2808 log_error("Failed to parse reply.");
2812 dbus_message_iter_get_basic(&sub
, &id
);
2813 printf("Unit %s changed.\n", id
);
2817 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_UINT32
) {
2818 log_error("Failed to parse reply.");
2822 dbus_message_iter_get_basic(&sub
, &id
);
2823 printf("Job %u changed.\n", id
);
2829 dbus_message_unref(m
);
2832 dbus_message_unref(reply
);
2834 dbus_error_free(&error
);
2835 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
2839 dbus_message_unref(m
);
2842 dbus_message_unref(reply
);
2844 dbus_error_free(&error
);
2845 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
2848 static int monitor(DBusConnection
*bus
, char **args
, unsigned n
) {
2849 DBusMessage
*m
= NULL
, *reply
= NULL
;
2853 dbus_error_init(&error
);
2856 dbus_bus_add_match(bus
,
2858 "sender='org.freedesktop.systemd1',"
2859 "interface='org.freedesktop.systemd1.Manager',"
2860 "path='/org/freedesktop/systemd1'",
2863 if (dbus_error_is_set(&error
)) {
2864 log_error("Failed to add match: %s", bus_error_message(&error
));
2869 dbus_bus_add_match(bus
,
2871 "sender='org.freedesktop.systemd1',"
2872 "interface='org.freedesktop.DBus.Properties',"
2873 "member='PropertiesChanged'",
2876 if (dbus_error_is_set(&error
)) {
2877 log_error("Failed to add match: %s", bus_error_message(&error
));
2883 if (!dbus_connection_add_filter(bus
, monitor_filter
, NULL
, NULL
)) {
2884 log_error("Failed to add filter.");
2889 if (!(m
= dbus_message_new_method_call(
2890 "org.freedesktop.systemd1",
2891 "/org/freedesktop/systemd1",
2892 "org.freedesktop.systemd1.Manager",
2894 log_error("Could not allocate message.");
2899 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2900 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2905 while (dbus_connection_read_write_dispatch(bus
, -1))
2912 /* This is slightly dirty, since we don't undo the filter or the matches. */
2915 dbus_message_unref(m
);
2918 dbus_message_unref(reply
);
2920 dbus_error_free(&error
);
2925 static int dump(DBusConnection
*bus
, char **args
, unsigned n
) {
2926 DBusMessage
*m
= NULL
, *reply
= NULL
;
2931 dbus_error_init(&error
);
2935 if (!(m
= dbus_message_new_method_call(
2936 "org.freedesktop.systemd1",
2937 "/org/freedesktop/systemd1",
2938 "org.freedesktop.systemd1.Manager",
2940 log_error("Could not allocate message.");
2944 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
2945 log_error("Failed to issue method call: %s", bus_error_message(&error
));
2950 if (!dbus_message_get_args(reply
, &error
,
2951 DBUS_TYPE_STRING
, &text
,
2952 DBUS_TYPE_INVALID
)) {
2953 log_error("Failed to parse reply: %s", bus_error_message(&error
));
2958 fputs(text
, stdout
);
2964 dbus_message_unref(m
);
2967 dbus_message_unref(reply
);
2969 dbus_error_free(&error
);
2974 static int snapshot(DBusConnection
*bus
, char **args
, unsigned n
) {
2975 DBusMessage
*m
= NULL
, *reply
= NULL
;
2978 const char *name
= "", *path
, *id
;
2979 dbus_bool_t cleanup
= FALSE
;
2980 DBusMessageIter iter
, sub
;
2982 *interface
= "org.freedesktop.systemd1.Unit",
2985 dbus_error_init(&error
);
2987 if (!(m
= dbus_message_new_method_call(
2988 "org.freedesktop.systemd1",
2989 "/org/freedesktop/systemd1",
2990 "org.freedesktop.systemd1.Manager",
2991 "CreateSnapshot"))) {
2992 log_error("Could not allocate message.");
2999 if (!dbus_message_append_args(m
,
3000 DBUS_TYPE_STRING
, &name
,
3001 DBUS_TYPE_BOOLEAN
, &cleanup
,
3002 DBUS_TYPE_INVALID
)) {
3003 log_error("Could not append arguments to message.");
3008 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3009 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3014 if (!dbus_message_get_args(reply
, &error
,
3015 DBUS_TYPE_OBJECT_PATH
, &path
,
3016 DBUS_TYPE_INVALID
)) {
3017 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3022 dbus_message_unref(m
);
3023 if (!(m
= dbus_message_new_method_call(
3024 "org.freedesktop.systemd1",
3026 "org.freedesktop.DBus.Properties",
3028 log_error("Could not allocate message.");
3032 if (!dbus_message_append_args(m
,
3033 DBUS_TYPE_STRING
, &interface
,
3034 DBUS_TYPE_STRING
, &property
,
3035 DBUS_TYPE_INVALID
)) {
3036 log_error("Could not append arguments to message.");
3041 dbus_message_unref(reply
);
3042 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3043 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3048 if (!dbus_message_iter_init(reply
, &iter
) ||
3049 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3050 log_error("Failed to parse reply.");
3055 dbus_message_iter_recurse(&iter
, &sub
);
3057 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_STRING
) {
3058 log_error("Failed to parse reply.");
3063 dbus_message_iter_get_basic(&sub
, &id
);
3071 dbus_message_unref(m
);
3074 dbus_message_unref(reply
);
3076 dbus_error_free(&error
);
3081 static int delete_snapshot(DBusConnection
*bus
, char **args
, unsigned n
) {
3082 DBusMessage
*m
= NULL
, *reply
= NULL
;
3090 dbus_error_init(&error
);
3092 for (i
= 1; i
< n
; i
++) {
3093 const char *path
= NULL
;
3095 if (!(m
= dbus_message_new_method_call(
3096 "org.freedesktop.systemd1",
3097 "/org/freedesktop/systemd1",
3098 "org.freedesktop.systemd1.Manager",
3100 log_error("Could not allocate message.");
3105 if (!dbus_message_append_args(m
,
3106 DBUS_TYPE_STRING
, &args
[i
],
3107 DBUS_TYPE_INVALID
)) {
3108 log_error("Could not append arguments to message.");
3113 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3114 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3119 if (!dbus_message_get_args(reply
, &error
,
3120 DBUS_TYPE_OBJECT_PATH
, &path
,
3121 DBUS_TYPE_INVALID
)) {
3122 log_error("Failed to parse reply: %s", bus_error_message(&error
));
3127 dbus_message_unref(m
);
3128 if (!(m
= dbus_message_new_method_call(
3129 "org.freedesktop.systemd1",
3131 "org.freedesktop.systemd1.Snapshot",
3133 log_error("Could not allocate message.");
3138 dbus_message_unref(reply
);
3139 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3140 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3145 dbus_message_unref(m
);
3146 dbus_message_unref(reply
);
3154 dbus_message_unref(m
);
3157 dbus_message_unref(reply
);
3159 dbus_error_free(&error
);
3164 static int daemon_reload(DBusConnection
*bus
, char **args
, unsigned n
) {
3165 DBusMessage
*m
= NULL
, *reply
= NULL
;
3170 dbus_error_init(&error
);
3172 if (arg_action
== ACTION_RELOAD
)
3174 else if (arg_action
== ACTION_REEXEC
)
3175 method
= "Reexecute";
3177 assert(arg_action
== ACTION_SYSTEMCTL
);
3180 streq(args
[0], "clear-jobs") ||
3181 streq(args
[0], "cancel") ? "ClearJobs" :
3182 streq(args
[0], "daemon-reexec") ? "Reexecute" :
3183 streq(args
[0], "reset-failed") ? "ResetFailed" :
3184 streq(args
[0], "halt") ? "Halt" :
3185 streq(args
[0], "poweroff") ? "PowerOff" :
3186 streq(args
[0], "reboot") ? "Reboot" :
3187 streq(args
[0], "kexec") ? "KExec" :
3188 streq(args
[0], "exit") ? "Exit" :
3189 /* "daemon-reload" */ "Reload";
3192 if (!(m
= dbus_message_new_method_call(
3193 "org.freedesktop.systemd1",
3194 "/org/freedesktop/systemd1",
3195 "org.freedesktop.systemd1.Manager",
3197 log_error("Could not allocate message.");
3201 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3203 if (arg_action
!= ACTION_SYSTEMCTL
&& error_is_no_service(&error
)) {
3204 /* There's always a fallback possible for
3205 * legacy actions. */
3210 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3219 dbus_message_unref(m
);
3222 dbus_message_unref(reply
);
3224 dbus_error_free(&error
);
3229 static int reset_failed(DBusConnection
*bus
, char **args
, unsigned n
) {
3230 DBusMessage
*m
= NULL
, *reply
= NULL
;
3236 dbus_error_init(&error
);
3239 return daemon_reload(bus
, args
, n
);
3241 for (i
= 1; i
< n
; i
++) {
3243 if (!(m
= dbus_message_new_method_call(
3244 "org.freedesktop.systemd1",
3245 "/org/freedesktop/systemd1",
3246 "org.freedesktop.systemd1.Manager",
3247 "ResetFailedUnit"))) {
3248 log_error("Could not allocate message.");
3253 if (!dbus_message_append_args(m
,
3254 DBUS_TYPE_STRING
, args
+ i
,
3255 DBUS_TYPE_INVALID
)) {
3256 log_error("Could not append arguments to message.");
3261 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3262 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3267 dbus_message_unref(m
);
3268 dbus_message_unref(reply
);
3276 dbus_message_unref(m
);
3279 dbus_message_unref(reply
);
3281 dbus_error_free(&error
);
3286 static int show_enviroment(DBusConnection
*bus
, char **args
, unsigned n
) {
3287 DBusMessage
*m
= NULL
, *reply
= NULL
;
3289 DBusMessageIter iter
, sub
, sub2
;
3292 *interface
= "org.freedesktop.systemd1.Manager",
3293 *property
= "Environment";
3295 dbus_error_init(&error
);
3299 if (!(m
= dbus_message_new_method_call(
3300 "org.freedesktop.systemd1",
3301 "/org/freedesktop/systemd1",
3302 "org.freedesktop.DBus.Properties",
3304 log_error("Could not allocate message.");
3308 if (!dbus_message_append_args(m
,
3309 DBUS_TYPE_STRING
, &interface
,
3310 DBUS_TYPE_STRING
, &property
,
3311 DBUS_TYPE_INVALID
)) {
3312 log_error("Could not append arguments to message.");
3317 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3318 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3323 if (!dbus_message_iter_init(reply
, &iter
) ||
3324 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_VARIANT
) {
3325 log_error("Failed to parse reply.");
3330 dbus_message_iter_recurse(&iter
, &sub
);
3332 if (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_ARRAY
||
3333 dbus_message_iter_get_element_type(&sub
) != DBUS_TYPE_STRING
) {
3334 log_error("Failed to parse reply.");
3339 dbus_message_iter_recurse(&sub
, &sub2
);
3341 while (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_INVALID
) {
3344 if (dbus_message_iter_get_arg_type(&sub2
) != DBUS_TYPE_STRING
) {
3345 log_error("Failed to parse reply.");
3350 dbus_message_iter_get_basic(&sub2
, &text
);
3351 printf("%s\n", text
);
3353 dbus_message_iter_next(&sub2
);
3360 dbus_message_unref(m
);
3363 dbus_message_unref(reply
);
3365 dbus_error_free(&error
);
3370 static int set_environment(DBusConnection
*bus
, char **args
, unsigned n
) {
3371 DBusMessage
*m
= NULL
, *reply
= NULL
;
3375 DBusMessageIter iter
, sub
;
3378 dbus_error_init(&error
);
3380 method
= streq(args
[0], "set-environment")
3382 : "UnsetEnvironment";
3384 if (!(m
= dbus_message_new_method_call(
3385 "org.freedesktop.systemd1",
3386 "/org/freedesktop/systemd1",
3387 "org.freedesktop.systemd1.Manager",
3390 log_error("Could not allocate message.");
3394 dbus_message_iter_init_append(m
, &iter
);
3396 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
)) {
3397 log_error("Could not append arguments to message.");
3402 for (i
= 1; i
< n
; i
++)
3403 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &args
[i
])) {
3404 log_error("Could not append arguments to message.");
3409 if (!dbus_message_iter_close_container(&iter
, &sub
)) {
3410 log_error("Could not append arguments to message.");
3415 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
3416 log_error("Failed to issue method call: %s", bus_error_message(&error
));
3425 dbus_message_unref(m
);
3428 dbus_message_unref(reply
);
3430 dbus_error_free(&error
);
3443 static Hashmap
*will_install
= NULL
, *have_installed
= NULL
;
3444 static Set
*remove_symlinks_to
= NULL
;
3445 static unsigned n_symlinks
= 0;
3447 static void install_info_free(InstallInfo
*i
) {
3452 strv_free(i
->aliases
);
3453 strv_free(i
->wanted_by
);
3457 static void install_info_hashmap_free(Hashmap
*m
) {
3460 while ((i
= hashmap_steal_first(m
)))
3461 install_info_free(i
);
3466 static int install_info_add(const char *name
) {
3470 assert(will_install
);
3472 if (!unit_name_is_valid_no_type(name
, true)) {
3473 log_warning("Unit name %s is not a valid unit name.", name
);
3477 if (hashmap_get(have_installed
, name
) ||
3478 hashmap_get(will_install
, name
))
3481 if (!(i
= new0(InstallInfo
, 1))) {
3486 if (!(i
->name
= strdup(name
))) {
3491 if ((r
= hashmap_put(will_install
, i
->name
, i
)) < 0)
3498 install_info_free(i
);
3503 static int config_parse_also(
3504 const char *filename
,
3506 const char *section
,
3520 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
3524 if (!(n
= strndup(w
, l
)))
3527 if ((r
= install_info_add(n
)) < 0) {
3528 log_warning("Cannot install unit %s: %s", n
, strerror(-r
));
3539 static int mark_symlink_for_removal(const char *p
) {
3544 assert(path_is_absolute(p
));
3546 if (!remove_symlinks_to
)
3549 if (!(n
= strdup(p
)))
3552 path_kill_slashes(n
);
3554 if ((r
= set_put(remove_symlinks_to
, n
)) < 0) {
3556 return r
== -EEXIST
? 0 : r
;
3562 static int remove_marked_symlinks_fd(int fd
, const char *config_path
, const char *root
, bool *deleted
) {
3571 if (!(d
= fdopendir(fd
))) {
3572 close_nointr_nofail(fd
);
3578 while ((de
= readdir(d
))) {
3579 bool is_dir
= false, is_link
= false;
3581 if (ignore_file(de
->d_name
))
3584 if (de
->d_type
== DT_LNK
)
3586 else if (de
->d_type
== DT_DIR
)
3588 else if (de
->d_type
== DT_UNKNOWN
) {
3591 if (fstatat(fd
, de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
3592 log_error("Failed to stat %s/%s: %m", root
, de
->d_name
);
3599 is_link
= S_ISLNK(st
.st_mode
);
3600 is_dir
= S_ISDIR(st
.st_mode
);
3608 if ((nfd
= openat(fd
, de
->d_name
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
)) < 0) {
3609 log_error("Failed to open %s/%s: %m", root
, de
->d_name
);
3616 if (asprintf(&p
, "%s/%s", root
, de
->d_name
) < 0) {
3617 log_error("Failed to allocate directory string.");
3618 close_nointr_nofail(nfd
);
3623 /* This will close nfd, regardless whether it succeeds or not */
3624 q
= remove_marked_symlinks_fd(nfd
, config_path
, p
, deleted
);
3630 } else if (is_link
) {
3634 if (asprintf(&p
, "%s/%s", root
, de
->d_name
) < 0) {
3635 log_error("Failed to allocate symlink string.");
3640 if ((q
= readlink_and_make_absolute(p
, &dest
)) < 0) {
3641 log_error("Cannot read symlink %s: %s", p
, strerror(-q
));
3649 if ((c
= canonicalize_file_name(dest
))) {
3650 /* This might fail if the destination
3651 * is already removed */
3657 path_kill_slashes(dest
);
3658 if (set_get(remove_symlinks_to
, dest
)) {
3661 log_info("rm '%s'", p
);
3663 if (unlink(p
) < 0) {
3664 log_error("Cannot unlink symlink %s: %m", p
);
3669 rmdir_parents(p
, config_path
);
3670 path_kill_slashes(p
);
3672 if (!set_get(remove_symlinks_to
, p
)) {
3674 if ((r
= mark_symlink_for_removal(p
)) < 0) {
3693 static int remove_marked_symlinks(const char *config_path
) {
3697 assert(config_path
);
3699 if (set_size(remove_symlinks_to
) <= 0)
3702 if ((fd
= open(config_path
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
)) < 0)
3709 if ((cfd
= dup(fd
)) < 0) {
3714 /* This takes possession of cfd and closes it */
3715 if ((q
= remove_marked_symlinks_fd(cfd
, config_path
, config_path
, &deleted
)) < 0) {
3721 close_nointr_nofail(fd
);
3726 static int create_symlink(const char *verb
, const char *old_path
, const char *new_path
) {
3733 if (streq(verb
, "enable")) {
3736 mkdir_parents(new_path
, 0755);
3738 if (symlink(old_path
, new_path
) >= 0) {
3741 log_info("ln -s '%s' '%s'", old_path
, new_path
);
3746 if (errno
!= EEXIST
) {
3747 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
3751 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3753 if (errno
== EINVAL
) {
3754 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path
, new_path
);
3758 log_error("readlink() failed: %s", strerror(-r
));
3762 if (streq(dest
, old_path
)) {
3768 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path
, new_path
, dest
);
3777 log_info("ln -s '%s' '%s'", old_path
, new_path
);
3779 if (symlink(old_path
, new_path
) >= 0)
3782 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
3785 } else if (streq(verb
, "disable")) {
3788 if ((r
= mark_symlink_for_removal(old_path
)) < 0)
3791 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3792 if (errno
== ENOENT
)
3795 if (errno
== EINVAL
) {
3796 log_warning("File %s not a symlink, ignoring.", old_path
);
3800 log_error("readlink() failed: %s", strerror(-r
));
3804 if (!streq(dest
, old_path
)) {
3805 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path
, old_path
, dest
);
3812 if ((r
= mark_symlink_for_removal(new_path
)) < 0)
3816 log_info("rm '%s'", new_path
);
3818 if (unlink(new_path
) >= 0)
3821 log_error("Cannot unlink %s: %m", new_path
);
3824 } else if (streq(verb
, "is-enabled")) {
3827 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
3829 if (errno
== ENOENT
|| errno
== EINVAL
)
3832 log_error("readlink() failed: %s", strerror(-r
));
3836 if (streq(dest
, old_path
)) {
3844 assert_not_reached("Unknown action.");
3847 static int install_info_symlink_alias(const char *verb
, InstallInfo
*i
, const char *config_path
) {
3849 char *alias_path
= NULL
;
3854 assert(config_path
);
3856 STRV_FOREACH(s
, i
->aliases
) {
3859 if (!(alias_path
= path_make_absolute(*s
, config_path
))) {
3860 log_error("Out of memory");
3865 if ((r
= create_symlink(verb
, i
->path
, alias_path
)) != 0)
3868 if (streq(verb
, "disable"))
3869 rmdir_parents(alias_path
, config_path
);
3879 static int install_info_symlink_wants(const char *verb
, InstallInfo
*i
, const char *config_path
) {
3881 char *alias_path
= NULL
;
3886 assert(config_path
);
3888 STRV_FOREACH(s
, i
->wanted_by
) {
3889 if (!unit_name_is_valid_no_type(*s
, true)) {
3890 log_error("Invalid name %s.", *s
);
3898 if (asprintf(&alias_path
, "%s/%s.wants/%s", config_path
, *s
, i
->name
) < 0) {
3899 log_error("Out of memory");
3904 if ((r
= create_symlink(verb
, i
->path
, alias_path
)) != 0)
3907 if (streq(verb
, "disable"))
3908 rmdir_parents(alias_path
, config_path
);
3919 static int install_info_apply(const char *verb
, LookupPaths
*paths
, InstallInfo
*i
, const char *config_path
) {
3921 const ConfigItem items
[] = {
3922 { "Alias", config_parse_strv
, &i
->aliases
, "Install" },
3923 { "WantedBy", config_parse_strv
, &i
->wanted_by
, "Install" },
3924 { "Also", config_parse_also
, NULL
, "Install" },
3926 { NULL
, NULL
, NULL
, NULL
}
3930 char *filename
= NULL
;
3937 STRV_FOREACH(p
, paths
->unit_path
) {
3940 if (!(filename
= path_make_absolute(i
->name
, *p
))) {
3941 log_error("Out of memory");
3945 /* Ensure that we don't follow symlinks */
3946 if ((fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NOCTTY
)) >= 0)
3947 if ((f
= fdopen(fd
, "re")))
3950 if (errno
== ELOOP
) {
3951 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3956 if (errno
!= ENOENT
) {
3957 log_error("Failed to open %s: %m", filename
);
3967 #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT)
3969 if (endswith(i
->name
, ".service")) {
3973 if (asprintf(&sysv
, SYSTEM_SYSVINIT_PATH
"/%s", i
->name
) < 0) {
3974 log_error("Out of memory");
3978 sysv
[strlen(sysv
) - sizeof(".service") + 1] = 0;
3979 exists
= access(sysv
, F_OK
) >= 0;
3985 const char *argv
[] = {
3992 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i
->name
);
3994 argv
[1] = file_name_from_path(sysv
);
3996 streq(verb
, "enable") ? "on" :
3997 streq(verb
, "disable") ? "off" : NULL
;
3999 log_info("Executing %s %s %s", argv
[0], argv
[1], strempty(argv
[2]));
4001 if ((pid
= fork()) < 0) {
4002 log_error("Failed to fork: %m");
4005 } else if (pid
== 0) {
4006 execv(argv
[0], (char**) argv
);
4007 _exit(EXIT_FAILURE
);
4012 if ((r
= wait_for_terminate(pid
, &status
)) < 0)
4015 if (status
.si_code
== CLD_EXITED
) {
4016 if (status
.si_status
== 0 && (streq(verb
, "enable") || streq(verb
, "disable")))
4019 return status
.si_status
== 0 ? 0 : -EINVAL
;
4029 log_error("Couldn't find %s.", i
->name
);
4035 if ((r
= config_parse(filename
, f
, NULL
, items
, true, i
)) < 0) {
4040 n_symlinks
+= strv_length(i
->aliases
);
4041 n_symlinks
+= strv_length(i
->wanted_by
);
4045 if ((r
= install_info_symlink_alias(verb
, i
, config_path
)) != 0)
4048 if ((r
= install_info_symlink_wants(verb
, i
, config_path
)) != 0)
4051 if ((r
= mark_symlink_for_removal(filename
)) < 0)
4054 if ((r
= remove_marked_symlinks(config_path
)) < 0)
4060 static char *get_config_path(void) {
4062 if (arg_user
&& arg_global
)
4063 return strdup(USER_CONFIG_UNIT_PATH
);
4068 if (user_config_home(&p
) < 0)
4074 return strdup(SYSTEM_CONFIG_UNIT_PATH
);
4077 static int enable_unit(DBusConnection
*bus
, char **args
, unsigned n
) {
4081 char *config_path
= NULL
;
4084 const char *verb
= args
[0];
4086 dbus_error_init(&error
);
4089 if ((r
= lookup_paths_init(&paths
, arg_user
? MANAGER_USER
: MANAGER_SYSTEM
)) < 0) {
4090 log_error("Failed to determine lookup paths: %s", strerror(-r
));
4094 if (!(config_path
= get_config_path())) {
4095 log_error("Failed to determine config path");
4100 will_install
= hashmap_new(string_hash_func
, string_compare_func
);
4101 have_installed
= hashmap_new(string_hash_func
, string_compare_func
);
4103 if (!will_install
|| !have_installed
) {
4104 log_error("Failed to allocate unit sets.");
4109 if (!arg_defaults
&& streq(verb
, "disable"))
4110 if (!(remove_symlinks_to
= set_new(string_hash_func
, string_compare_func
))) {
4111 log_error("Failed to allocate symlink sets.");
4116 for (j
= 1; j
< n
; j
++)
4117 if ((r
= install_info_add(args
[j
])) < 0) {
4118 log_warning("Cannot install unit %s: %s", args
[j
], strerror(-r
));
4122 while ((i
= hashmap_first(will_install
))) {
4125 assert_se(hashmap_move_one(have_installed
, will_install
, i
->name
) == 0);
4127 if ((q
= install_info_apply(verb
, &paths
, i
, config_path
)) != 0) {
4135 /* In test mode and found something */
4141 if (streq(verb
, "is-enabled"))
4142 r
= r
> 0 ? 0 : -ENOENT
;
4144 if (n_symlinks
<= 0)
4145 log_warning("Unit files contain no applicable installation information. Ignoring.");
4148 /* Don't try to reload anything if the user asked us to not do this */
4150 /* Don't try to reload anything when updating a unit globally */
4152 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4153 (arg_user
|| sd_booted() > 0) &&
4154 /* Don't try to reload anything if we are running in a chroot environment */
4155 (arg_user
|| running_in_chroot() <= 0) ) {
4158 if ((q
= daemon_reload(bus
, args
, n
)) < 0)
4164 install_info_hashmap_free(will_install
);
4165 install_info_hashmap_free(have_installed
);
4167 set_free_free(remove_symlinks_to
);
4169 lookup_paths_free(&paths
);
4176 static int systemctl_help(void) {
4178 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4179 "Send control commands to or query the systemd manager.\n\n"
4180 " -h --help Show this help\n"
4181 " --version Show package version\n"
4182 " -t --type=TYPE List only units of a particular type\n"
4183 " -p --property=NAME Show only properties by this name\n"
4184 " -a --all Show all units/properties, including dead/empty ones\n"
4185 " --full Don't ellipsize unit names on output\n"
4186 " --fail When queueing a new job, fail if conflicting jobs are\n"
4188 " -q --quiet Suppress output\n"
4189 " --no-block Do not wait until operation finished\n"
4190 " --no-pager Do not pipe output into a pager.\n"
4191 " --system Connect to system manager\n"
4192 " --user Connect to user service manager\n"
4193 " --order When generating graph for dot, show only order\n"
4194 " --require When generating graph for dot, show only requirement\n"
4195 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4196 " --global Enable/disable unit files globally\n"
4197 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4199 " --no-ask-password\n"
4200 " Do not ask for system passwords\n"
4201 " --kill-mode=MODE How to send signal\n"
4202 " --kill-who=WHO Who to send signal to\n"
4203 " -s --signal=SIGNAL Which signal to send\n"
4204 " -f --force When enabling unit files, override existing symlinks\n"
4205 " When shutting down, execute action immediately\n"
4206 " --defaults When disabling unit files, remove default symlinks only\n\n"
4208 " list-units List units\n"
4209 " start [NAME...] Start (activate) one or more units\n"
4210 " stop [NAME...] Stop (deactivate) one or more units\n"
4211 " reload [NAME...] Reload one or more units\n"
4212 " restart [NAME...] Start or restart one or more units\n"
4213 " try-restart [NAME...] Restart one or more units if active\n"
4214 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4215 " otherwise start or restart\n"
4216 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4217 " otherwise restart if active\n"
4218 " isolate [NAME] Start one unit and stop all others\n"
4219 " kill [NAME...] Send signal to processes of a unit\n"
4220 " is-active [NAME...] Check whether units are active\n"
4221 " status [NAME...|PID...] Show runtime status of one or more units\n"
4222 " show [NAME...|JOB...] Show properties of one or more\n"
4223 " units/jobs or the manager\n"
4224 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4226 " enable [NAME...] Enable one or more unit files\n"
4227 " disable [NAME...] Disable one or more unit files\n"
4228 " is-enabled [NAME...] Check whether unit files are enabled\n"
4229 " load [NAME...] Load one or more units\n"
4230 " list-jobs List jobs\n"
4231 " cancel [JOB...] Cancel all, one, or more jobs\n"
4232 " monitor Monitor unit/job changes\n"
4233 " dump Dump server status\n"
4234 " dot Dump dependency graph for dot(1)\n"
4235 " snapshot [NAME] Create a snapshot\n"
4236 " delete [NAME...] Remove one or more snapshots\n"
4237 " daemon-reload Reload systemd manager configuration\n"
4238 " daemon-reexec Reexecute systemd manager\n"
4239 " show-environment Dump environment\n"
4240 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4241 " unset-environment [NAME...] Unset one or more environment variables\n"
4242 " default Enter system default mode\n"
4243 " rescue Enter system rescue mode\n"
4244 " emergency Enter system emergency mode\n"
4245 " halt Shut down and halt the system\n"
4246 " poweroff Shut down and power-off the system\n"
4247 " reboot Shut down and reboot the system\n"
4248 " kexec Shut down and reboot the system with kexec\n"
4249 " exit Ask for user instance termination\n",
4250 program_invocation_short_name
);
4255 static int halt_help(void) {
4257 printf("%s [OPTIONS...]\n\n"
4258 "%s the system.\n\n"
4259 " --help Show this help\n"
4260 " --halt Halt the machine\n"
4261 " -p --poweroff Switch off the machine\n"
4262 " --reboot Reboot the machine\n"
4263 " -f --force Force immediate halt/power-off/reboot\n"
4264 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4265 " -d --no-wtmp Don't write wtmp record\n"
4266 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4267 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4268 program_invocation_short_name
,
4269 arg_action
== ACTION_REBOOT
? "Reboot" :
4270 arg_action
== ACTION_POWEROFF
? "Power off" :
4276 static int shutdown_help(void) {
4278 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4279 "Shut down the system.\n\n"
4280 " --help Show this help\n"
4281 " -H --halt Halt the machine\n"
4282 " -P --poweroff Power-off the machine\n"
4283 " -r --reboot Reboot the machine\n"
4284 " -h Equivalent to --poweroff, overriden by --halt\n"
4285 " -k Don't halt/power-off/reboot, just send warnings\n"
4286 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4287 " -c Cancel a pending shutdown\n",
4288 program_invocation_short_name
);
4293 static int telinit_help(void) {
4295 printf("%s [OPTIONS...] {COMMAND}\n\n"
4296 "Send control commands to the init daemon.\n\n"
4297 " --help Show this help\n"
4298 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4300 " 0 Power-off the machine\n"
4301 " 6 Reboot the machine\n"
4302 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4303 " 1, s, S Enter rescue mode\n"
4304 " q, Q Reload init daemon configuration\n"
4305 " u, U Reexecute init daemon\n",
4306 program_invocation_short_name
);
4311 static int runlevel_help(void) {
4313 printf("%s [OPTIONS...]\n\n"
4314 "Prints the previous and current runlevel of the init system.\n\n"
4315 " --help Show this help\n",
4316 program_invocation_short_name
);
4321 static int systemctl_parse_argv(int argc
, char *argv
[]) {
4342 static const struct option options
[] = {
4343 { "help", no_argument
, NULL
, 'h' },
4344 { "version", no_argument
, NULL
, ARG_VERSION
},
4345 { "type", required_argument
, NULL
, 't' },
4346 { "property", required_argument
, NULL
, 'p' },
4347 { "all", no_argument
, NULL
, 'a' },
4348 { "full", no_argument
, NULL
, ARG_FULL
},
4349 { "fail", no_argument
, NULL
, ARG_FAIL
},
4350 { "user", no_argument
, NULL
, ARG_USER
},
4351 { "system", no_argument
, NULL
, ARG_SYSTEM
},
4352 { "global", no_argument
, NULL
, ARG_GLOBAL
},
4353 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
4354 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
4355 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4356 { "quiet", no_argument
, NULL
, 'q' },
4357 { "order", no_argument
, NULL
, ARG_ORDER
},
4358 { "require", no_argument
, NULL
, ARG_REQUIRE
},
4359 { "force", no_argument
, NULL
, 'f' },
4360 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
4361 { "defaults", no_argument
, NULL
, ARG_DEFAULTS
},
4362 { "kill-mode", required_argument
, NULL
, ARG_KILL_MODE
},
4363 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
4364 { "signal", required_argument
, NULL
, 's' },
4365 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
4366 { NULL
, 0, NULL
, 0 }
4374 /* Only when running as systemctl we ask for passwords */
4375 arg_ask_password
= true;
4377 while ((c
= getopt_long(argc
, argv
, "ht:p:aqfs:", options
, NULL
)) >= 0) {
4386 puts(PACKAGE_STRING
);
4388 puts(SYSTEMD_FEATURES
);
4398 if (!(l
= strv_append(arg_property
, optarg
)))
4401 strv_free(arg_property
);
4404 /* If the user asked for a particular
4405 * property, show it to him, even if it is
4428 arg_no_block
= true;
4432 arg_no_pager
= true;
4440 arg_dot
= DOT_ORDER
;
4444 arg_dot
= DOT_REQUIRE
;
4460 arg_no_reload
= true;
4469 arg_defaults
= true;
4473 arg_kill_who
= optarg
;
4477 arg_kill_mode
= optarg
;
4481 if ((arg_signal
= signal_from_string_try_harder(optarg
)) < 0) {
4482 log_error("Failed to parse signal string %s.", optarg
);
4487 case ARG_NO_ASK_PASSWORD
:
4488 arg_ask_password
= false;
4495 log_error("Unknown option code %c", c
);
4503 static int halt_parse_argv(int argc
, char *argv
[]) {
4512 static const struct option options
[] = {
4513 { "help", no_argument
, NULL
, ARG_HELP
},
4514 { "halt", no_argument
, NULL
, ARG_HALT
},
4515 { "poweroff", no_argument
, NULL
, 'p' },
4516 { "reboot", no_argument
, NULL
, ARG_REBOOT
},
4517 { "force", no_argument
, NULL
, 'f' },
4518 { "wtmp-only", no_argument
, NULL
, 'w' },
4519 { "no-wtmp", no_argument
, NULL
, 'd' },
4520 { "no-sync", no_argument
, NULL
, 'n' },
4521 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4522 { NULL
, 0, NULL
, 0 }
4530 if (utmp_get_runlevel(&runlevel
, NULL
) >= 0)
4531 if (runlevel
== '0' || runlevel
== '6')
4532 arg_immediate
= true;
4534 while ((c
= getopt_long(argc
, argv
, "pfwdnih", options
, NULL
)) >= 0) {
4542 arg_action
= ACTION_HALT
;
4546 if (arg_action
!= ACTION_REBOOT
)
4547 arg_action
= ACTION_POWEROFF
;
4551 arg_action
= ACTION_REBOOT
;
4555 arg_immediate
= true;
4576 /* Compatibility nops */
4583 log_error("Unknown option code %c", c
);
4588 if (optind
< argc
) {
4589 log_error("Too many arguments.");
4596 static int parse_time_spec(const char *t
, usec_t
*_u
) {
4600 if (streq(t
, "now"))
4602 else if (t
[0] == '+') {
4605 if (safe_atou64(t
+ 1, &u
) < 0)
4608 *_u
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
* u
;
4617 hour
= strtol(t
, &e
, 10);
4618 if (errno
!= 0 || *e
!= ':' || hour
< 0 || hour
> 23)
4621 minute
= strtol(e
+1, &e
, 10);
4622 if (errno
!= 0 || *e
!= 0 || minute
< 0 || minute
> 59)
4625 n
= now(CLOCK_REALTIME
);
4626 s
= (time_t) (n
/ USEC_PER_SEC
);
4629 assert_se(localtime_r(&s
, &tm
));
4631 tm
.tm_hour
= (int) hour
;
4632 tm
.tm_min
= (int) minute
;
4635 assert_se(s
= mktime(&tm
));
4637 *_u
= (usec_t
) s
* USEC_PER_SEC
;
4640 *_u
+= USEC_PER_DAY
;
4646 static int shutdown_parse_argv(int argc
, char *argv
[]) {
4653 static const struct option options
[] = {
4654 { "help", no_argument
, NULL
, ARG_HELP
},
4655 { "halt", no_argument
, NULL
, 'H' },
4656 { "poweroff", no_argument
, NULL
, 'P' },
4657 { "reboot", no_argument
, NULL
, 'r' },
4658 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4659 { NULL
, 0, NULL
, 0 }
4667 while ((c
= getopt_long(argc
, argv
, "HPrhkt:afFc", options
, NULL
)) >= 0) {
4675 arg_action
= ACTION_HALT
;
4679 arg_action
= ACTION_POWEROFF
;
4683 arg_action
= ACTION_REBOOT
;
4687 if (arg_action
!= ACTION_HALT
)
4688 arg_action
= ACTION_POWEROFF
;
4701 /* Compatibility nops */
4705 arg_action
= ACTION_CANCEL_SHUTDOWN
;
4712 log_error("Unknown option code %c", c
);
4717 if (argc
> optind
) {
4718 if ((r
= parse_time_spec(argv
[optind
], &arg_when
)) < 0) {
4719 log_error("Failed to parse time specification: %s", argv
[optind
]);
4723 arg_when
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
;
4725 /* We skip the time argument */
4726 if (argc
> optind
+ 1)
4727 arg_wall
= argv
+ optind
+ 1;
4734 static int telinit_parse_argv(int argc
, char *argv
[]) {
4741 static const struct option options
[] = {
4742 { "help", no_argument
, NULL
, ARG_HELP
},
4743 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
4744 { NULL
, 0, NULL
, 0 }
4747 static const struct {
4751 { '0', ACTION_POWEROFF
},
4752 { '6', ACTION_REBOOT
},
4753 { '1', ACTION_RESCUE
},
4754 { '2', ACTION_RUNLEVEL2
},
4755 { '3', ACTION_RUNLEVEL3
},
4756 { '4', ACTION_RUNLEVEL4
},
4757 { '5', ACTION_RUNLEVEL5
},
4758 { 's', ACTION_RESCUE
},
4759 { 'S', ACTION_RESCUE
},
4760 { 'q', ACTION_RELOAD
},
4761 { 'Q', ACTION_RELOAD
},
4762 { 'u', ACTION_REEXEC
},
4763 { 'U', ACTION_REEXEC
}
4772 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
4787 log_error("Unknown option code %c", c
);
4792 if (optind
>= argc
) {
4797 if (optind
+ 1 < argc
) {
4798 log_error("Too many arguments.");
4802 if (strlen(argv
[optind
]) != 1) {
4803 log_error("Expected single character argument.");
4807 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
4808 if (table
[i
].from
== argv
[optind
][0])
4811 if (i
>= ELEMENTSOF(table
)) {
4812 log_error("Unknown command %s.", argv
[optind
]);
4816 arg_action
= table
[i
].to
;
4823 static int runlevel_parse_argv(int argc
, char *argv
[]) {
4829 static const struct option options
[] = {
4830 { "help", no_argument
, NULL
, ARG_HELP
},
4831 { NULL
, 0, NULL
, 0 }
4839 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0) {
4850 log_error("Unknown option code %c", c
);
4855 if (optind
< argc
) {
4856 log_error("Too many arguments.");
4863 static int parse_argv(int argc
, char *argv
[]) {
4867 if (program_invocation_short_name
) {
4869 if (strstr(program_invocation_short_name
, "halt")) {
4870 arg_action
= ACTION_HALT
;
4871 return halt_parse_argv(argc
, argv
);
4872 } else if (strstr(program_invocation_short_name
, "poweroff")) {
4873 arg_action
= ACTION_POWEROFF
;
4874 return halt_parse_argv(argc
, argv
);
4875 } else if (strstr(program_invocation_short_name
, "reboot")) {
4876 arg_action
= ACTION_REBOOT
;
4877 return halt_parse_argv(argc
, argv
);
4878 } else if (strstr(program_invocation_short_name
, "shutdown")) {
4879 arg_action
= ACTION_POWEROFF
;
4880 return shutdown_parse_argv(argc
, argv
);
4881 } else if (strstr(program_invocation_short_name
, "init")) {
4883 if (sd_booted() > 0) {
4884 arg_action
= ACTION_INVALID
;
4885 return telinit_parse_argv(argc
, argv
);
4887 /* Hmm, so some other init system is
4888 * running, we need to forward this
4889 * request to it. For now we simply
4890 * guess that it is Upstart. */
4892 execv("/lib/upstart/telinit", argv
);
4894 log_error("Couldn't find an alternative telinit implementation to spawn.");
4898 } else if (strstr(program_invocation_short_name
, "runlevel")) {
4899 arg_action
= ACTION_RUNLEVEL
;
4900 return runlevel_parse_argv(argc
, argv
);
4904 arg_action
= ACTION_SYSTEMCTL
;
4905 return systemctl_parse_argv(argc
, argv
);
4908 static int action_to_runlevel(void) {
4910 static const char table
[_ACTION_MAX
] = {
4911 [ACTION_HALT
] = '0',
4912 [ACTION_POWEROFF
] = '0',
4913 [ACTION_REBOOT
] = '6',
4914 [ACTION_RUNLEVEL2
] = '2',
4915 [ACTION_RUNLEVEL3
] = '3',
4916 [ACTION_RUNLEVEL4
] = '4',
4917 [ACTION_RUNLEVEL5
] = '5',
4918 [ACTION_RESCUE
] = '1'
4921 assert(arg_action
< _ACTION_MAX
);
4923 return table
[arg_action
];
4926 static int talk_upstart(void) {
4927 DBusMessage
*m
= NULL
, *reply
= NULL
;
4929 int previous
, rl
, r
;
4931 env1_buf
[] = "RUNLEVEL=X",
4932 env2_buf
[] = "PREVLEVEL=X";
4933 char *env1
= env1_buf
, *env2
= env2_buf
;
4934 const char *emit
= "runlevel";
4935 dbus_bool_t b_false
= FALSE
;
4936 DBusMessageIter iter
, sub
;
4937 DBusConnection
*bus
;
4939 dbus_error_init(&error
);
4941 if (!(rl
= action_to_runlevel()))
4944 if (utmp_get_runlevel(&previous
, NULL
) < 0)
4947 if (!(bus
= dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error
))) {
4948 if (dbus_error_has_name(&error
, DBUS_ERROR_NO_SERVER
)) {
4953 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error
));
4958 if ((r
= bus_check_peercred(bus
)) < 0) {
4959 log_error("Failed to verify owner of bus.");
4963 if (!(m
= dbus_message_new_method_call(
4964 "com.ubuntu.Upstart",
4965 "/com/ubuntu/Upstart",
4966 "com.ubuntu.Upstart0_6",
4969 log_error("Could not allocate message.");
4974 dbus_message_iter_init_append(m
, &iter
);
4976 env1_buf
[sizeof(env1_buf
)-2] = rl
;
4977 env2_buf
[sizeof(env2_buf
)-2] = previous
;
4979 if (!dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &emit
) ||
4980 !dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "s", &sub
) ||
4981 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env1
) ||
4982 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &env2
) ||
4983 !dbus_message_iter_close_container(&iter
, &sub
) ||
4984 !dbus_message_iter_append_basic(&iter
, DBUS_TYPE_BOOLEAN
, &b_false
)) {
4985 log_error("Could not append arguments to message.");
4990 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
4992 if (error_is_no_service(&error
)) {
4997 log_error("Failed to issue method call: %s", bus_error_message(&error
));
5006 dbus_message_unref(m
);
5009 dbus_message_unref(reply
);
5012 dbus_connection_flush(bus
);
5013 dbus_connection_close(bus
);
5014 dbus_connection_unref(bus
);
5017 dbus_error_free(&error
);
5022 static int talk_initctl(void) {
5023 struct init_request request
;
5027 if (!(rl
= action_to_runlevel()))
5031 request
.magic
= INIT_MAGIC
;
5032 request
.sleeptime
= 0;
5033 request
.cmd
= INIT_CMD_RUNLVL
;
5034 request
.runlevel
= rl
;
5036 if ((fd
= open(INIT_FIFO
, O_WRONLY
|O_NDELAY
|O_CLOEXEC
|O_NOCTTY
)) < 0) {
5038 if (errno
== ENOENT
)
5041 log_error("Failed to open "INIT_FIFO
": %m");
5046 r
= loop_write(fd
, &request
, sizeof(request
), false) != sizeof(request
);
5047 close_nointr_nofail(fd
);
5050 log_error("Failed to write to "INIT_FIFO
": %m");
5051 return errno
? -errno
: -EIO
;
5057 static int systemctl_main(DBusConnection
*bus
, int argc
, char *argv
[], DBusError
*error
) {
5059 static const struct {
5067 int (* const dispatch
)(DBusConnection
*bus
, char **args
, unsigned n
);
5069 { "list-units", LESS
, 1, list_units
},
5070 { "list-jobs", EQUAL
, 1, list_jobs
},
5071 { "clear-jobs", EQUAL
, 1, daemon_reload
},
5072 { "load", MORE
, 2, load_unit
},
5073 { "cancel", MORE
, 2, cancel_job
},
5074 { "start", MORE
, 2, start_unit
},
5075 { "stop", MORE
, 2, start_unit
},
5076 { "reload", MORE
, 2, start_unit
},
5077 { "restart", MORE
, 2, start_unit
},
5078 { "try-restart", MORE
, 2, start_unit
},
5079 { "reload-or-restart", MORE
, 2, start_unit
},
5080 { "reload-or-try-restart", MORE
, 2, start_unit
},
5081 { "force-reload", MORE
, 2, start_unit
}, /* For compatibility with SysV */
5082 { "condrestart", MORE
, 2, start_unit
}, /* For compatibility with RH */
5083 { "isolate", EQUAL
, 2, start_unit
},
5084 { "kill", MORE
, 2, kill_unit
},
5085 { "is-active", MORE
, 2, check_unit
},
5086 { "check", MORE
, 2, check_unit
},
5087 { "show", MORE
, 1, show
},
5088 { "status", MORE
, 2, show
},
5089 { "monitor", EQUAL
, 1, monitor
},
5090 { "dump", EQUAL
, 1, dump
},
5091 { "dot", EQUAL
, 1, dot
},
5092 { "snapshot", LESS
, 2, snapshot
},
5093 { "delete", MORE
, 2, delete_snapshot
},
5094 { "daemon-reload", EQUAL
, 1, daemon_reload
},
5095 { "daemon-reexec", EQUAL
, 1, daemon_reload
},
5096 { "show-environment", EQUAL
, 1, show_enviroment
},
5097 { "set-environment", MORE
, 2, set_environment
},
5098 { "unset-environment", MORE
, 2, set_environment
},
5099 { "halt", EQUAL
, 1, start_special
},
5100 { "poweroff", EQUAL
, 1, start_special
},
5101 { "reboot", EQUAL
, 1, start_special
},
5102 { "kexec", EQUAL
, 1, start_special
},
5103 { "default", EQUAL
, 1, start_special
},
5104 { "rescue", EQUAL
, 1, start_special
},
5105 { "emergency", EQUAL
, 1, start_special
},
5106 { "exit", EQUAL
, 1, start_special
},
5107 { "reset-failed", MORE
, 1, reset_failed
},
5108 { "enable", MORE
, 2, enable_unit
},
5109 { "disable", MORE
, 2, enable_unit
},
5110 { "is-enabled", MORE
, 2, enable_unit
}
5120 left
= argc
- optind
;
5123 /* Special rule: no arguments means "list-units" */
5126 if (streq(argv
[optind
], "help")) {
5131 for (i
= 0; i
< ELEMENTSOF(verbs
); i
++)
5132 if (streq(argv
[optind
], verbs
[i
].verb
))
5135 if (i
>= ELEMENTSOF(verbs
)) {
5136 log_error("Unknown operation %s", argv
[optind
]);
5141 switch (verbs
[i
].argc_cmp
) {
5144 if (left
!= verbs
[i
].argc
) {
5145 log_error("Invalid number of arguments.");
5152 if (left
< verbs
[i
].argc
) {
5153 log_error("Too few arguments.");
5160 if (left
> verbs
[i
].argc
) {
5161 log_error("Too many arguments.");
5168 assert_not_reached("Unknown comparison operator.");
5171 /* Require a bus connection for all operations but
5173 if (!streq(verbs
[i
].verb
, "enable") &&
5174 !streq(verbs
[i
].verb
, "disable") &&
5176 log_error("Failed to get D-Bus connection: %s", error
->message
);
5180 return verbs
[i
].dispatch(bus
, argv
+ optind
, left
);
5183 static int send_shutdownd(usec_t t
, char mode
, bool warn
, const char *message
) {
5185 struct msghdr msghdr
;
5187 union sockaddr_union sockaddr
;
5188 struct shutdownd_command c
;
5196 strncpy(c
.wall_message
, message
, sizeof(c
.wall_message
));
5198 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0)) < 0)
5202 sockaddr
.sa
.sa_family
= AF_UNIX
;
5203 sockaddr
.un
.sun_path
[0] = 0;
5204 strncpy(sockaddr
.un
.sun_path
+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr
.un
.sun_path
)-1);
5207 iovec
.iov_base
= (char*) &c
;
5208 iovec
.iov_len
= sizeof(c
);
5211 msghdr
.msg_name
= &sockaddr
;
5212 msghdr
.msg_namelen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5214 msghdr
.msg_iov
= &iovec
;
5215 msghdr
.msg_iovlen
= 1;
5217 if (sendmsg(fd
, &msghdr
, MSG_NOSIGNAL
) < 0) {
5218 close_nointr_nofail(fd
);
5222 close_nointr_nofail(fd
);
5226 static int reload_with_fallback(DBusConnection
*bus
) {
5229 /* First, try systemd via D-Bus. */
5230 if (daemon_reload(bus
, NULL
, 0) > 0)
5234 /* Nothing else worked, so let's try signals */
5235 assert(arg_action
== ACTION_RELOAD
|| arg_action
== ACTION_REEXEC
);
5237 if (kill(1, arg_action
== ACTION_RELOAD
? SIGHUP
: SIGTERM
) < 0) {
5238 log_error("kill() failed: %m");
5245 static int start_with_fallback(DBusConnection
*bus
) {
5248 /* First, try systemd via D-Bus. */
5249 if (start_unit(bus
, NULL
, 0) >= 0)
5253 /* Hmm, talking to systemd via D-Bus didn't work. Then
5254 * let's try to talk to Upstart via D-Bus. */
5255 if (talk_upstart() > 0)
5258 /* Nothing else worked, so let's try
5260 if (talk_initctl() > 0)
5263 log_error("Failed to talk to init daemon.");
5267 warn_wall(arg_action
);
5271 static int halt_main(DBusConnection
*bus
) {
5274 if (geteuid() != 0) {
5275 log_error("Must be root.");
5281 char date
[FORMAT_TIMESTAMP_MAX
];
5283 m
= strv_join(arg_wall
, " ");
5284 r
= send_shutdownd(arg_when
,
5285 arg_action
== ACTION_HALT
? 'H' :
5286 arg_action
== ACTION_POWEROFF
? 'P' :
5293 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r
));
5295 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5296 format_timestamp(date
, sizeof(date
), arg_when
));
5301 if (!arg_dry
&& !arg_immediate
)
5302 return start_with_fallback(bus
);
5305 if (sd_booted() > 0)
5306 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5307 else if ((r
= utmp_put_shutdown(0)) < 0)
5308 log_warning("Failed to write utmp record: %s", strerror(-r
));
5317 /* Make sure C-A-D is handled by the kernel from this
5319 reboot(RB_ENABLE_CAD
);
5321 switch (arg_action
) {
5324 log_info("Halting.");
5325 reboot(RB_HALT_SYSTEM
);
5328 case ACTION_POWEROFF
:
5329 log_info("Powering off.");
5330 reboot(RB_POWER_OFF
);
5334 log_info("Rebooting.");
5335 reboot(RB_AUTOBOOT
);
5339 assert_not_reached("Unknown halt action.");
5342 /* We should never reach this. */
5346 static int runlevel_main(void) {
5347 int r
, runlevel
, previous
;
5349 if ((r
= utmp_get_runlevel(&runlevel
, &previous
)) < 0) {
5350 printf("unknown\n");
5355 previous
<= 0 ? 'N' : previous
,
5356 runlevel
<= 0 ? 'N' : runlevel
);
5361 static void pager_open(void) {
5368 if (!on_tty() || arg_no_pager
)
5371 if ((pager
= getenv("PAGER")))
5372 if (!*pager
|| streq(pager
, "cat"))
5375 /* Determine and cache number of columns before we spawn the
5376 * pager so that we get the value from the actual tty */
5380 log_error("Failed to create pager pipe: %m");
5385 if (pager_pid
< 0) {
5386 log_error("Failed to fork pager: %m");
5391 /* In the child start the pager */
5392 if (pager_pid
== 0) {
5394 dup2(fd
[0], STDIN_FILENO
);
5397 setenv("LESS", "FRSX", 0);
5399 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
5402 execlp(pager
, pager
, NULL
);
5403 execl("/bin/sh", "sh", "-c", pager
, NULL
);
5405 /* Debian's alternatives command for pagers is
5406 * called 'pager'. Note that we do not call
5407 * sensible-pagers here, since that is just a
5408 * shell script that implements a logic that
5409 * is similar to this one anyway, but is
5410 * Debian-specific. */
5411 execlp("pager", "pager", NULL
);
5413 execlp("less", "less", NULL
);
5414 execlp("more", "more", NULL
);
5417 log_error("Unable to execute pager: %m");
5418 _exit(EXIT_FAILURE
);
5421 /* Return in the parent */
5422 if (dup2(fd
[1], STDOUT_FILENO
) < 0)
5423 log_error("Failed to duplicate pager pipe: %m");
5428 static void pager_close(void) {
5434 /* Inform pager that we are done */
5436 wait_for_terminate(pager_pid
, &dummy
);
5440 int main(int argc
, char*argv
[]) {
5441 int r
, retval
= EXIT_FAILURE
;
5442 DBusConnection
*bus
= NULL
;
5445 dbus_error_init(&error
);
5447 log_parse_environment();
5450 if ((r
= parse_argv(argc
, argv
)) < 0)
5453 retval
= EXIT_SUCCESS
;
5457 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5458 * let's shortcut this */
5459 if (arg_action
== ACTION_RUNLEVEL
) {
5460 r
= runlevel_main();
5461 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5465 bus_connect(arg_user
? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &bus
, &private_bus
, &error
);
5467 switch (arg_action
) {
5469 case ACTION_SYSTEMCTL
:
5470 r
= systemctl_main(bus
, argc
, argv
, &error
);
5474 case ACTION_POWEROFF
:
5479 case ACTION_RUNLEVEL2
:
5480 case ACTION_RUNLEVEL3
:
5481 case ACTION_RUNLEVEL4
:
5482 case ACTION_RUNLEVEL5
:
5484 case ACTION_EMERGENCY
:
5485 case ACTION_DEFAULT
:
5486 r
= start_with_fallback(bus
);
5491 r
= reload_with_fallback(bus
);
5494 case ACTION_CANCEL_SHUTDOWN
:
5495 r
= send_shutdownd(0, 0, false, NULL
);
5498 case ACTION_INVALID
:
5499 case ACTION_RUNLEVEL
:
5501 assert_not_reached("Unknown action");
5504 retval
= r
< 0 ? EXIT_FAILURE
: r
;
5509 dbus_connection_flush(bus
);
5510 dbus_connection_close(bus
);
5511 dbus_connection_unref(bus
);
5514 dbus_error_free(&error
);
5518 strv_free(arg_property
);