1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Marc-Antoine Perennou
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <linux/reboot.h>
32 #include <sys/reboot.h>
33 #include <sys/socket.h>
37 #include "sd-daemon.h"
40 #include "alloc-util.h"
41 #include "bus-common-errors.h"
42 #include "bus-error.h"
43 #include "bus-message.h"
45 #include "cgroup-show.h"
46 #include "cgroup-util.h"
51 #include "exit-status.h"
54 #include "formats-util.h"
56 #include "glob-util.h"
57 #include "hostname-util.h"
62 #include "locale-util.h"
64 #include "logs-show.h"
68 #include "parse-util.h"
69 #include "path-lookup.h"
70 #include "path-util.h"
71 #include "process-util.h"
72 #include "rlimit-util.h"
74 #include "signal-util.h"
75 #include "socket-util.h"
76 #include "spawn-ask-password-agent.h"
77 #include "spawn-polkit-agent.h"
79 #include "stat-util.h"
81 #include "terminal-util.h"
82 #include "unit-name.h"
83 #include "user-util.h"
85 #include "utmp-wtmp.h"
89 static char **arg_types
= NULL
;
90 static char **arg_states
= NULL
;
91 static char **arg_properties
= NULL
;
92 static bool arg_all
= false;
93 static enum dependency
{
99 } arg_dependency
= DEPENDENCY_FORWARD
;
100 static const char *arg_job_mode
= "replace";
101 static UnitFileScope arg_scope
= UNIT_FILE_SYSTEM
;
102 static bool arg_no_block
= false;
103 static bool arg_no_legend
= false;
104 static bool arg_no_pager
= false;
105 static bool arg_no_wtmp
= false;
106 static bool arg_no_wall
= false;
107 static bool arg_no_reload
= false;
108 static bool arg_show_types
= false;
109 static bool arg_ignore_inhibitors
= false;
110 static bool arg_dry
= false;
111 static bool arg_quiet
= false;
112 static bool arg_full
= false;
113 static bool arg_recursive
= false;
114 static int arg_force
= 0;
115 static bool arg_ask_password
= false;
116 static bool arg_runtime
= false;
117 static UnitFilePresetMode arg_preset_mode
= UNIT_FILE_PRESET_FULL
;
118 static char **arg_wall
= NULL
;
119 static const char *arg_kill_who
= NULL
;
120 static int arg_signal
= SIGTERM
;
121 static char *arg_root
= NULL
;
122 static usec_t arg_when
= 0;
144 ACTION_CANCEL_SHUTDOWN
,
146 } arg_action
= ACTION_SYSTEMCTL
;
147 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
148 static const char *arg_host
= NULL
;
149 static unsigned arg_lines
= 10;
150 static OutputMode arg_output
= OUTPUT_SHORT
;
151 static bool arg_plain
= false;
152 static bool arg_firmware_setup
= false;
153 static bool arg_now
= false;
155 static int daemon_reload(int argc
, char *argv
[], void* userdata
);
156 static int halt_now(enum action a
);
157 static int check_one_unit(sd_bus
*bus
, const char *name
, const char *good_states
, bool quiet
);
159 static bool original_stdout_is_tty
;
161 typedef enum BusFocus
{
162 BUS_FULL
, /* The full bus indicated via --system or --user */
163 BUS_MANAGER
, /* The manager itself, possibly directly, possibly via the bus */
167 static sd_bus
*busses
[_BUS_FOCUS_MAX
] = {};
169 static int acquire_bus(BusFocus focus
, sd_bus
**ret
) {
172 assert(focus
< _BUS_FOCUS_MAX
);
175 /* We only go directly to the manager, if we are using a local transport */
176 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
179 if (!busses
[focus
]) {
182 user
= arg_scope
!= UNIT_FILE_SYSTEM
;
184 if (focus
== BUS_MANAGER
)
185 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, user
, &busses
[focus
]);
187 r
= bus_connect_transport(arg_transport
, arg_host
, user
, &busses
[focus
]);
189 return log_error_errno(r
, "Failed to connect to bus: %m");
191 (void) sd_bus_set_allow_interactive_authorization(busses
[focus
], arg_ask_password
);
194 *ret
= busses
[focus
];
198 static void release_busses(void) {
201 for (w
= 0; w
< _BUS_FOCUS_MAX
; w
++)
202 busses
[w
] = sd_bus_flush_close_unref(busses
[w
]);
205 static void pager_open_if_enabled(void) {
213 static void ask_password_agent_open_if_enabled(void) {
215 /* Open the password agent as a child process if necessary */
217 if (!arg_ask_password
)
220 if (arg_scope
!= UNIT_FILE_SYSTEM
)
223 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
226 ask_password_agent_open();
229 static void polkit_agent_open_if_enabled(void) {
231 /* Open the polkit agent as a child process if necessary */
233 if (!arg_ask_password
)
236 if (arg_scope
!= UNIT_FILE_SYSTEM
)
239 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
245 static OutputFlags
get_output_flags(void) {
247 arg_all
* OUTPUT_SHOW_ALL
|
248 arg_full
* OUTPUT_FULL_WIDTH
|
249 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
250 on_tty() * OUTPUT_COLOR
|
251 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
254 static int translate_bus_error_to_exit_status(int r
, const sd_bus_error
*error
) {
257 if (!sd_bus_error_is_set(error
))
260 if (sd_bus_error_has_name(error
, SD_BUS_ERROR_ACCESS_DENIED
) ||
261 sd_bus_error_has_name(error
, BUS_ERROR_ONLY_BY_DEPENDENCY
) ||
262 sd_bus_error_has_name(error
, BUS_ERROR_NO_ISOLATION
) ||
263 sd_bus_error_has_name(error
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
))
264 return EXIT_NOPERMISSION
;
266 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
))
267 return EXIT_NOTINSTALLED
;
269 if (sd_bus_error_has_name(error
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
) ||
270 sd_bus_error_has_name(error
, SD_BUS_ERROR_NOT_SUPPORTED
))
271 return EXIT_NOTIMPLEMENTED
;
273 if (sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
))
274 return EXIT_NOTCONFIGURED
;
282 static bool install_client_side(void) {
284 /* Decides when to execute enable/disable/... operations
285 * client-side rather than server-side. */
287 if (running_in_chroot() > 0)
290 if (sd_booted() <= 0)
293 if (!isempty(arg_root
))
296 if (arg_scope
== UNIT_FILE_GLOBAL
)
299 /* Unsupported environment variable, mostly for debugging purposes */
300 if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0)
306 static int compare_unit_info(const void *a
, const void *b
) {
307 const UnitInfo
*u
= a
, *v
= b
;
311 /* First, order by machine */
312 if (!u
->machine
&& v
->machine
)
314 if (u
->machine
&& !v
->machine
)
316 if (u
->machine
&& v
->machine
) {
317 r
= strcasecmp(u
->machine
, v
->machine
);
322 /* Second, order by unit type */
323 d1
= strrchr(u
->id
, '.');
324 d2
= strrchr(v
->id
, '.');
326 r
= strcasecmp(d1
, d2
);
331 /* Third, order by name */
332 return strcasecmp(u
->id
, v
->id
);
335 static bool output_show_unit(const UnitInfo
*u
, char **patterns
) {
336 if (!strv_fnmatch_or_empty(patterns
, u
->id
, FNM_NOESCAPE
))
342 dot
= strrchr(u
->id
, '.');
346 if (!strv_find(arg_types
, dot
+1))
356 if (streq(u
->active_state
, "inactive") || u
->following
[0])
362 static int output_units_list(const UnitInfo
*unit_infos
, unsigned c
) {
363 unsigned circle_len
= 0, id_len
, max_id_len
, load_len
, active_len
, sub_len
, job_len
, desc_len
;
365 unsigned n_shown
= 0;
368 max_id_len
= strlen("UNIT");
369 load_len
= strlen("LOAD");
370 active_len
= strlen("ACTIVE");
371 sub_len
= strlen("SUB");
372 job_len
= strlen("JOB");
375 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
376 max_id_len
= MAX(max_id_len
, strlen(u
->id
) + (u
->machine
? strlen(u
->machine
)+1 : 0));
377 load_len
= MAX(load_len
, strlen(u
->load_state
));
378 active_len
= MAX(active_len
, strlen(u
->active_state
));
379 sub_len
= MAX(sub_len
, strlen(u
->sub_state
));
381 if (u
->job_id
!= 0) {
382 job_len
= MAX(job_len
, strlen(u
->job_type
));
386 if (!arg_no_legend
&&
387 (streq(u
->active_state
, "failed") ||
388 STR_IN_SET(u
->load_state
, "error", "not-found", "masked")))
392 if (!arg_full
&& original_stdout_is_tty
) {
395 id_len
= MIN(max_id_len
, 25u);
396 basic_len
= circle_len
+ 5 + id_len
+ 5 + active_len
+ sub_len
;
399 basic_len
+= job_len
+ 1;
401 if (basic_len
< (unsigned) columns()) {
402 unsigned extra_len
, incr
;
403 extra_len
= columns() - basic_len
;
405 /* Either UNIT already got 25, or is fully satisfied.
406 * Grant up to 25 to DESC now. */
407 incr
= MIN(extra_len
, 25u);
411 /* split the remaining space between UNIT and DESC,
412 * but do not give UNIT more than it needs. */
414 incr
= MIN(extra_len
/ 2, max_id_len
- id_len
);
416 desc_len
+= extra_len
- incr
;
422 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
423 _cleanup_free_
char *e
= NULL
, *j
= NULL
;
424 const char *on_loaded
= "", *off_loaded
= "";
425 const char *on_active
= "", *off_active
= "";
426 const char *on_circle
= "", *off_circle
= "";
430 if (!n_shown
&& !arg_no_legend
) {
435 printf("%-*s %-*s %-*s %-*s ",
438 active_len
, "ACTIVE",
442 printf("%-*s ", job_len
, "JOB");
444 if (!arg_full
&& arg_no_pager
)
445 printf("%.*s\n", desc_len
, "DESCRIPTION");
447 printf("%s\n", "DESCRIPTION");
452 if (STR_IN_SET(u
->load_state
, "error", "not-found", "masked") && !arg_plain
) {
453 on_loaded
= ansi_highlight_red();
454 on_circle
= ansi_highlight_yellow();
455 off_loaded
= off_circle
= ansi_normal();
457 } else if (streq(u
->active_state
, "failed") && !arg_plain
) {
458 on_circle
= on_active
= ansi_highlight_red();
459 off_circle
= off_active
= ansi_normal();
464 j
= strjoin(u
->machine
, ":", u
->id
, NULL
);
473 e
= ellipsize(id
, id_len
, 33);
481 printf("%s%s%s ", on_circle
, circle
? draw_special_char(DRAW_BLACK_CIRCLE
) : " ", off_circle
);
483 printf("%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
484 on_active
, id_len
, id
, off_active
,
485 on_loaded
, load_len
, u
->load_state
, off_loaded
,
486 on_active
, active_len
, u
->active_state
,
487 sub_len
, u
->sub_state
, off_active
,
488 job_count
? job_len
+ 1 : 0, u
->job_id
? u
->job_type
: "");
491 printf("%.*s\n", desc_len
, u
->description
);
493 printf("%s\n", u
->description
);
496 if (!arg_no_legend
) {
497 const char *on
, *off
;
501 "LOAD = Reflects whether the unit definition was properly loaded.\n"
502 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
503 "SUB = The low-level unit activation state, values depend on unit type.");
504 puts(job_count
? "JOB = Pending job for the unit.\n" : "");
505 on
= ansi_highlight();
508 on
= ansi_highlight_red();
513 printf("%s%u loaded units listed.%s\n"
514 "To show all installed unit files use 'systemctl list-unit-files'.\n",
517 printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
518 "To show all installed unit files use 'systemctl list-unit-files'.\n",
525 static int get_unit_list(
529 UnitInfo
**unit_infos
,
531 sd_bus_message
**_reply
) {
533 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
534 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
535 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
544 r
= sd_bus_message_new_method_call(
547 "org.freedesktop.systemd1",
548 "/org/freedesktop/systemd1",
549 "org.freedesktop.systemd1.Manager",
550 "ListUnitsFiltered");
553 return bus_log_create_error(r
);
555 r
= sd_bus_message_append_strv(m
, arg_states
);
557 return bus_log_create_error(r
);
559 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
561 return log_error_errno(r
, "Failed to list units: %s", bus_error_message(&error
, r
));
563 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssssouso)");
565 return bus_log_parse_error(r
);
567 while ((r
= bus_parse_unit_info(reply
, &u
)) > 0) {
570 if (!output_show_unit(&u
, patterns
))
573 if (!GREEDY_REALLOC(*unit_infos
, size
, c
+1))
576 (*unit_infos
)[c
++] = u
;
579 return bus_log_parse_error(r
);
581 r
= sd_bus_message_exit_container(reply
);
583 return bus_log_parse_error(r
);
591 static void message_set_freep(Set
**set
) {
594 while ((m
= set_steal_first(*set
)))
595 sd_bus_message_unref(m
);
600 static int get_unit_list_recursive(
603 UnitInfo
**_unit_infos
,
607 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
608 _cleanup_(message_set_freep
) Set
*replies
;
609 sd_bus_message
*reply
;
617 replies
= set_new(NULL
);
621 c
= get_unit_list(bus
, NULL
, patterns
, &unit_infos
, 0, &reply
);
625 r
= set_put(replies
, reply
);
627 sd_bus_message_unref(reply
);
632 _cleanup_strv_free_
char **machines
= NULL
;
635 r
= sd_get_machine_names(&machines
);
637 return log_error_errno(r
, "Failed to get machine names: %m");
639 STRV_FOREACH(i
, machines
) {
640 _cleanup_bus_flush_close_unref_ sd_bus
*container
= NULL
;
643 r
= sd_bus_open_system_machine(&container
, *i
);
645 log_warning_errno(r
, "Failed to connect to container %s, ignoring: %m", *i
);
649 k
= get_unit_list(container
, *i
, patterns
, &unit_infos
, c
, &reply
);
655 r
= set_put(replies
, reply
);
657 sd_bus_message_unref(reply
);
662 *_machines
= machines
;
667 *_unit_infos
= unit_infos
;
676 static int list_units(int argc
, char *argv
[], void *userdata
) {
677 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
678 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
679 _cleanup_strv_free_
char **machines
= NULL
;
683 pager_open_if_enabled();
685 r
= acquire_bus(BUS_MANAGER
, &bus
);
689 r
= get_unit_list_recursive(bus
, strv_skip(argv
, 1), &unit_infos
, &replies
, &machines
);
693 qsort_safe(unit_infos
, r
, sizeof(UnitInfo
), compare_unit_info
);
694 return output_units_list(unit_infos
, r
);
697 static int get_triggered_units(
702 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
709 r
= sd_bus_get_property_strv(
711 "org.freedesktop.systemd1",
713 "org.freedesktop.systemd1.Unit",
718 return log_error_errno(r
, "Failed to determine triggers: %s", bus_error_message(&error
, r
));
723 static int get_listening(
725 const char* unit_path
,
728 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
729 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
730 const char *type
, *path
;
733 r
= sd_bus_get_property(
735 "org.freedesktop.systemd1",
737 "org.freedesktop.systemd1.Socket",
743 return log_error_errno(r
, "Failed to get list of listening sockets: %s", bus_error_message(&error
, r
));
745 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ss)");
747 return bus_log_parse_error(r
);
749 while ((r
= sd_bus_message_read(reply
, "(ss)", &type
, &path
)) > 0) {
751 r
= strv_extend(listening
, type
);
755 r
= strv_extend(listening
, path
);
762 return bus_log_parse_error(r
);
764 r
= sd_bus_message_exit_container(reply
);
766 return bus_log_parse_error(r
);
778 /* Note: triggered is a list here, although it almost certainly
779 * will always be one unit. Nevertheless, dbus API allows for multiple
780 * values, so let's follow that. */
783 /* The strv above is shared. free is set only in the first one. */
787 static int socket_info_compare(const struct socket_info
*a
, const struct socket_info
*b
) {
793 if (!a
->machine
&& b
->machine
)
795 if (a
->machine
&& !b
->machine
)
797 if (a
->machine
&& b
->machine
) {
798 o
= strcasecmp(a
->machine
, b
->machine
);
803 o
= strcmp(a
->path
, b
->path
);
805 o
= strcmp(a
->type
, b
->type
);
810 static int output_sockets_list(struct socket_info
*socket_infos
, unsigned cs
) {
811 struct socket_info
*s
;
812 unsigned pathlen
= strlen("LISTEN"),
813 typelen
= strlen("TYPE") * arg_show_types
,
814 socklen
= strlen("UNIT"),
815 servlen
= strlen("ACTIVATES");
816 const char *on
, *off
;
818 for (s
= socket_infos
; s
< socket_infos
+ cs
; s
++) {
822 socklen
= MAX(socklen
, strlen(s
->id
));
824 typelen
= MAX(typelen
, strlen(s
->type
));
825 pathlen
= MAX(pathlen
, strlen(s
->path
) + (s
->machine
? strlen(s
->machine
)+1 : 0));
827 STRV_FOREACH(a
, s
->triggered
)
828 tmp
+= strlen(*a
) + 2*(a
!= s
->triggered
);
829 servlen
= MAX(servlen
, tmp
);
834 printf("%-*s %-*.*s%-*s %s\n",
836 typelen
+ arg_show_types
, typelen
+ arg_show_types
, "TYPE ",
840 for (s
= socket_infos
; s
< socket_infos
+ cs
; s
++) {
841 _cleanup_free_
char *j
= NULL
;
846 j
= strjoin(s
->machine
, ":", s
->path
, NULL
);
854 printf("%-*s %-*s %-*s",
855 pathlen
, path
, typelen
, s
->type
, socklen
, s
->id
);
858 pathlen
, path
, socklen
, s
->id
);
859 STRV_FOREACH(a
, s
->triggered
)
861 a
== s
->triggered
? "" : ",", *a
);
865 on
= ansi_highlight();
870 on
= ansi_highlight_red();
874 if (!arg_no_legend
) {
875 printf("%s%u sockets listed.%s\n", on
, cs
, off
);
877 printf("Pass --all to see loaded but inactive sockets, too.\n");
883 static int list_sockets(int argc
, char *argv
[], void *userdata
) {
884 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
885 _cleanup_strv_free_
char **machines
= NULL
;
886 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
887 _cleanup_free_
struct socket_info
*socket_infos
= NULL
;
889 struct socket_info
*s
;
895 pager_open_if_enabled();
897 r
= acquire_bus(BUS_MANAGER
, &bus
);
901 n
= get_unit_list_recursive(bus
, strv_skip(argv
, 1), &unit_infos
, &replies
, &machines
);
905 for (u
= unit_infos
; u
< unit_infos
+ n
; u
++) {
906 _cleanup_strv_free_
char **listening
= NULL
, **triggered
= NULL
;
909 if (!endswith(u
->id
, ".socket"))
912 r
= get_triggered_units(bus
, u
->unit_path
, &triggered
);
916 c
= get_listening(bus
, u
->unit_path
, &listening
);
922 if (!GREEDY_REALLOC(socket_infos
, size
, cs
+ c
)) {
927 for (i
= 0; i
< c
; i
++)
928 socket_infos
[cs
+ i
] = (struct socket_info
) {
929 .machine
= u
->machine
,
931 .type
= listening
[i
*2],
932 .path
= listening
[i
*2 + 1],
933 .triggered
= triggered
,
934 .own_triggered
= i
==0,
937 /* from this point on we will cleanup those socket_infos */
940 listening
= triggered
= NULL
; /* avoid cleanup */
943 qsort_safe(socket_infos
, cs
, sizeof(struct socket_info
),
944 (__compar_fn_t
) socket_info_compare
);
946 output_sockets_list(socket_infos
, cs
);
949 assert(cs
== 0 || socket_infos
);
950 for (s
= socket_infos
; s
< socket_infos
+ cs
; s
++) {
953 if (s
->own_triggered
)
954 strv_free(s
->triggered
);
960 static int get_next_elapse(
963 dual_timestamp
*next
) {
965 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
973 r
= sd_bus_get_property_trivial(
975 "org.freedesktop.systemd1",
977 "org.freedesktop.systemd1.Timer",
978 "NextElapseUSecMonotonic",
983 return log_error_errno(r
, "Failed to get next elapsation time: %s", bus_error_message(&error
, r
));
985 r
= sd_bus_get_property_trivial(
987 "org.freedesktop.systemd1",
989 "org.freedesktop.systemd1.Timer",
990 "NextElapseUSecRealtime",
995 return log_error_errno(r
, "Failed to get next elapsation time: %s", bus_error_message(&error
, r
));
1001 static int get_last_trigger(
1006 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1013 r
= sd_bus_get_property_trivial(
1015 "org.freedesktop.systemd1",
1017 "org.freedesktop.systemd1.Timer",
1023 return log_error_errno(r
, "Failed to get last trigger time: %s", bus_error_message(&error
, r
));
1029 const char* machine
;
1032 usec_t last_trigger
;
1036 static int timer_info_compare(const struct timer_info
*a
, const struct timer_info
*b
) {
1042 if (!a
->machine
&& b
->machine
)
1044 if (a
->machine
&& !b
->machine
)
1046 if (a
->machine
&& b
->machine
) {
1047 o
= strcasecmp(a
->machine
, b
->machine
);
1052 if (a
->next_elapse
< b
->next_elapse
)
1054 if (a
->next_elapse
> b
->next_elapse
)
1057 return strcmp(a
->id
, b
->id
);
1060 static int output_timers_list(struct timer_info
*timer_infos
, unsigned n
) {
1061 struct timer_info
*t
;
1063 nextlen
= strlen("NEXT"),
1064 leftlen
= strlen("LEFT"),
1065 lastlen
= strlen("LAST"),
1066 passedlen
= strlen("PASSED"),
1067 unitlen
= strlen("UNIT"),
1068 activatelen
= strlen("ACTIVATES");
1070 const char *on
, *off
;
1072 assert(timer_infos
|| n
== 0);
1074 for (t
= timer_infos
; t
< timer_infos
+ n
; t
++) {
1078 if (t
->next_elapse
> 0) {
1079 char tstamp
[FORMAT_TIMESTAMP_MAX
] = "", trel
[FORMAT_TIMESTAMP_RELATIVE_MAX
] = "";
1081 format_timestamp(tstamp
, sizeof(tstamp
), t
->next_elapse
);
1082 nextlen
= MAX(nextlen
, strlen(tstamp
) + 1);
1084 format_timestamp_relative(trel
, sizeof(trel
), t
->next_elapse
);
1085 leftlen
= MAX(leftlen
, strlen(trel
));
1088 if (t
->last_trigger
> 0) {
1089 char tstamp
[FORMAT_TIMESTAMP_MAX
] = "", trel
[FORMAT_TIMESTAMP_RELATIVE_MAX
] = "";
1091 format_timestamp(tstamp
, sizeof(tstamp
), t
->last_trigger
);
1092 lastlen
= MAX(lastlen
, strlen(tstamp
) + 1);
1094 format_timestamp_relative(trel
, sizeof(trel
), t
->last_trigger
);
1095 passedlen
= MAX(passedlen
, strlen(trel
));
1098 unitlen
= MAX(unitlen
, strlen(t
->id
) + (t
->machine
? strlen(t
->machine
)+1 : 0));
1100 STRV_FOREACH(a
, t
->triggered
)
1101 ul
+= strlen(*a
) + 2*(a
!= t
->triggered
);
1103 activatelen
= MAX(activatelen
, ul
);
1108 printf("%-*s %-*s %-*s %-*s %-*s %s\n",
1112 passedlen
, "PASSED",
1116 for (t
= timer_infos
; t
< timer_infos
+ n
; t
++) {
1117 _cleanup_free_
char *j
= NULL
;
1119 char tstamp1
[FORMAT_TIMESTAMP_MAX
] = "n/a", trel1
[FORMAT_TIMESTAMP_RELATIVE_MAX
] = "n/a";
1120 char tstamp2
[FORMAT_TIMESTAMP_MAX
] = "n/a", trel2
[FORMAT_TIMESTAMP_RELATIVE_MAX
] = "n/a";
1123 format_timestamp(tstamp1
, sizeof(tstamp1
), t
->next_elapse
);
1124 format_timestamp_relative(trel1
, sizeof(trel1
), t
->next_elapse
);
1126 format_timestamp(tstamp2
, sizeof(tstamp2
), t
->last_trigger
);
1127 format_timestamp_relative(trel2
, sizeof(trel2
), t
->last_trigger
);
1130 j
= strjoin(t
->machine
, ":", t
->id
, NULL
);
1137 printf("%-*s %-*s %-*s %-*s %-*s",
1138 nextlen
, tstamp1
, leftlen
, trel1
, lastlen
, tstamp2
, passedlen
, trel2
, unitlen
, unit
);
1140 STRV_FOREACH(a
, t
->triggered
)
1142 a
== t
->triggered
? "" : ",", *a
);
1146 on
= ansi_highlight();
1147 off
= ansi_normal();
1151 on
= ansi_highlight_red();
1152 off
= ansi_normal();
1155 if (!arg_no_legend
) {
1156 printf("%s%u timers listed.%s\n", on
, n
, off
);
1158 printf("Pass --all to see loaded but inactive timers, too.\n");
1164 static usec_t
calc_next_elapse(dual_timestamp
*nw
, dual_timestamp
*next
) {
1170 if (next
->monotonic
!= USEC_INFINITY
&& next
->monotonic
> 0) {
1173 if (next
->monotonic
> nw
->monotonic
)
1174 converted
= nw
->realtime
+ (next
->monotonic
- nw
->monotonic
);
1176 converted
= nw
->realtime
- (nw
->monotonic
- next
->monotonic
);
1178 if (next
->realtime
!= USEC_INFINITY
&& next
->realtime
> 0)
1179 next_elapse
= MIN(converted
, next
->realtime
);
1181 next_elapse
= converted
;
1184 next_elapse
= next
->realtime
;
1189 static int list_timers(int argc
, char *argv
[], void *userdata
) {
1190 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
1191 _cleanup_strv_free_
char **machines
= NULL
;
1192 _cleanup_free_
struct timer_info
*timer_infos
= NULL
;
1193 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
1194 struct timer_info
*t
;
1202 pager_open_if_enabled();
1204 r
= acquire_bus(BUS_MANAGER
, &bus
);
1208 n
= get_unit_list_recursive(bus
, strv_skip(argv
, 1), &unit_infos
, &replies
, &machines
);
1212 dual_timestamp_get(&nw
);
1214 for (u
= unit_infos
; u
< unit_infos
+ n
; u
++) {
1215 _cleanup_strv_free_
char **triggered
= NULL
;
1216 dual_timestamp next
= DUAL_TIMESTAMP_NULL
;
1219 if (!endswith(u
->id
, ".timer"))
1222 r
= get_triggered_units(bus
, u
->unit_path
, &triggered
);
1226 r
= get_next_elapse(bus
, u
->unit_path
, &next
);
1230 get_last_trigger(bus
, u
->unit_path
, &last
);
1232 if (!GREEDY_REALLOC(timer_infos
, size
, c
+1)) {
1237 m
= calc_next_elapse(&nw
, &next
);
1239 timer_infos
[c
++] = (struct timer_info
) {
1240 .machine
= u
->machine
,
1243 .last_trigger
= last
,
1244 .triggered
= triggered
,
1247 triggered
= NULL
; /* avoid cleanup */
1250 qsort_safe(timer_infos
, c
, sizeof(struct timer_info
),
1251 (__compar_fn_t
) timer_info_compare
);
1253 output_timers_list(timer_infos
, c
);
1256 for (t
= timer_infos
; t
< timer_infos
+ c
; t
++)
1257 strv_free(t
->triggered
);
1262 static int compare_unit_file_list(const void *a
, const void *b
) {
1263 const char *d1
, *d2
;
1264 const UnitFileList
*u
= a
, *v
= b
;
1266 d1
= strrchr(u
->path
, '.');
1267 d2
= strrchr(v
->path
, '.');
1272 r
= strcasecmp(d1
, d2
);
1277 return strcasecmp(basename(u
->path
), basename(v
->path
));
1280 static bool output_show_unit_file(const UnitFileList
*u
, char **patterns
) {
1281 if (!strv_fnmatch_or_empty(patterns
, basename(u
->path
), FNM_NOESCAPE
))
1284 if (!strv_isempty(arg_types
)) {
1287 dot
= strrchr(u
->path
, '.');
1291 if (!strv_find(arg_types
, dot
+1))
1295 if (!strv_isempty(arg_states
) &&
1296 !strv_find(arg_states
, unit_file_state_to_string(u
->state
)))
1302 static void output_unit_file_list(const UnitFileList
*units
, unsigned c
) {
1303 unsigned max_id_len
, id_cols
, state_cols
;
1304 const UnitFileList
*u
;
1306 max_id_len
= strlen("UNIT FILE");
1307 state_cols
= strlen("STATE");
1309 for (u
= units
; u
< units
+ c
; u
++) {
1310 max_id_len
= MAX(max_id_len
, strlen(basename(u
->path
)));
1311 state_cols
= MAX(state_cols
, strlen(unit_file_state_to_string(u
->state
)));
1315 unsigned basic_cols
;
1317 id_cols
= MIN(max_id_len
, 25u);
1318 basic_cols
= 1 + id_cols
+ state_cols
;
1319 if (basic_cols
< (unsigned) columns())
1320 id_cols
+= MIN(columns() - basic_cols
, max_id_len
- id_cols
);
1322 id_cols
= max_id_len
;
1325 printf("%-*s %-*s\n",
1326 id_cols
, "UNIT FILE",
1327 state_cols
, "STATE");
1329 for (u
= units
; u
< units
+ c
; u
++) {
1330 _cleanup_free_
char *e
= NULL
;
1331 const char *on
, *off
;
1334 if (IN_SET(u
->state
,
1336 UNIT_FILE_MASKED_RUNTIME
,
1338 UNIT_FILE_INVALID
)) {
1339 on
= ansi_highlight_red();
1340 off
= ansi_normal();
1341 } else if (u
->state
== UNIT_FILE_ENABLED
) {
1342 on
= ansi_highlight_green();
1343 off
= ansi_normal();
1347 id
= basename(u
->path
);
1349 e
= arg_full
? NULL
: ellipsize(id
, id_cols
, 33);
1351 printf("%-*s %s%-*s%s\n",
1352 id_cols
, e
? e
: id
,
1353 on
, state_cols
, unit_file_state_to_string(u
->state
), off
);
1357 printf("\n%u unit files listed.\n", c
);
1360 static int list_unit_files(int argc
, char *argv
[], void *userdata
) {
1361 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
1362 _cleanup_free_ UnitFileList
*units
= NULL
;
1370 pager_open_if_enabled();
1372 if (install_client_side()) {
1378 h
= hashmap_new(&string_hash_ops
);
1382 r
= unit_file_get_list(arg_scope
, arg_root
, h
);
1384 unit_file_list_free(h
);
1385 return log_error_errno(r
, "Failed to get unit file list: %m");
1388 n_units
= hashmap_size(h
);
1390 units
= new(UnitFileList
, n_units
);
1391 if (!units
&& n_units
> 0) {
1392 unit_file_list_free(h
);
1396 HASHMAP_FOREACH(u
, h
, i
) {
1397 if (!output_show_unit_file(u
, strv_skip(argv
, 1)))
1404 assert(c
<= n_units
);
1407 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1410 r
= acquire_bus(BUS_MANAGER
, &bus
);
1414 r
= sd_bus_call_method(
1416 "org.freedesktop.systemd1",
1417 "/org/freedesktop/systemd1",
1418 "org.freedesktop.systemd1.Manager",
1424 return log_error_errno(r
, "Failed to list unit files: %s", bus_error_message(&error
, r
));
1426 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ss)");
1428 return bus_log_parse_error(r
);
1430 while ((r
= sd_bus_message_read(reply
, "(ss)", &path
, &state
)) > 0) {
1432 if (!GREEDY_REALLOC(units
, size
, c
+ 1))
1435 units
[c
] = (struct UnitFileList
) {
1437 unit_file_state_from_string(state
)
1440 if (output_show_unit_file(&units
[c
], strv_skip(argv
, 1)))
1445 return bus_log_parse_error(r
);
1447 r
= sd_bus_message_exit_container(reply
);
1449 return bus_log_parse_error(r
);
1452 qsort_safe(units
, c
, sizeof(UnitFileList
), compare_unit_file_list
);
1453 output_unit_file_list(units
, c
);
1455 if (install_client_side()) {
1456 for (unit
= units
; unit
< units
+ c
; unit
++)
1463 static int list_dependencies_print(const char *name
, int level
, unsigned int branches
, bool last
) {
1464 _cleanup_free_
char *n
= NULL
;
1465 size_t max_len
= MAX(columns(),20u);
1471 for (i
= level
- 1; i
>= 0; i
--) {
1473 if (len
> max_len
- 3 && !arg_full
) {
1474 printf("%s...\n",max_len
% 2 ? "" : " ");
1477 printf("%s", draw_special_char(branches
& (1 << i
) ? DRAW_TREE_VERTICAL
: DRAW_TREE_SPACE
));
1481 if (len
> max_len
- 3 && !arg_full
) {
1482 printf("%s...\n",max_len
% 2 ? "" : " ");
1486 printf("%s", draw_special_char(last
? DRAW_TREE_RIGHT
: DRAW_TREE_BRANCH
));
1490 printf("%s\n", name
);
1494 n
= ellipsize(name
, max_len
-len
, 100);
1502 static int list_dependencies_get_dependencies(sd_bus
*bus
, const char *name
, char ***deps
) {
1504 static const char *dependencies
[_DEPENDENCY_MAX
] = {
1505 [DEPENDENCY_FORWARD
] = "Requires\0"
1510 [DEPENDENCY_REVERSE
] = "RequiredBy\0"
1515 [DEPENDENCY_AFTER
] = "After\0",
1516 [DEPENDENCY_BEFORE
] = "Before\0",
1519 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1520 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
1521 _cleanup_strv_free_
char **ret
= NULL
;
1522 _cleanup_free_
char *path
= NULL
;
1528 assert_cc(ELEMENTSOF(dependencies
) == _DEPENDENCY_MAX
);
1530 path
= unit_dbus_path_from_name(name
);
1534 r
= sd_bus_call_method(
1536 "org.freedesktop.systemd1",
1538 "org.freedesktop.DBus.Properties",
1542 "s", "org.freedesktop.systemd1.Unit");
1544 return log_error_errno(r
, "Failed to get properties of %s: %s", name
, bus_error_message(&error
, r
));
1546 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "{sv}");
1548 return bus_log_parse_error(r
);
1550 while ((r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
1553 r
= sd_bus_message_read(reply
, "s", &prop
);
1555 return bus_log_parse_error(r
);
1557 if (!nulstr_contains(dependencies
[arg_dependency
], prop
)) {
1558 r
= sd_bus_message_skip(reply
, "v");
1560 return bus_log_parse_error(r
);
1563 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_VARIANT
, "as");
1565 return bus_log_parse_error(r
);
1567 r
= bus_message_read_strv_extend(reply
, &ret
);
1569 return bus_log_parse_error(r
);
1571 r
= sd_bus_message_exit_container(reply
);
1573 return bus_log_parse_error(r
);
1576 r
= sd_bus_message_exit_container(reply
);
1578 return bus_log_parse_error(r
);
1582 return bus_log_parse_error(r
);
1584 r
= sd_bus_message_exit_container(reply
);
1586 return bus_log_parse_error(r
);
1594 static int list_dependencies_compare(const void *_a
, const void *_b
) {
1595 const char **a
= (const char**) _a
, **b
= (const char**) _b
;
1597 if (unit_name_to_type(*a
) == UNIT_TARGET
&& unit_name_to_type(*b
) != UNIT_TARGET
)
1599 if (unit_name_to_type(*a
) != UNIT_TARGET
&& unit_name_to_type(*b
) == UNIT_TARGET
)
1602 return strcasecmp(*a
, *b
);
1605 static int list_dependencies_one(
1610 unsigned int branches
) {
1612 _cleanup_strv_free_
char **deps
= NULL
;
1620 r
= strv_extend(units
, name
);
1624 r
= list_dependencies_get_dependencies(bus
, name
, &deps
);
1628 qsort_safe(deps
, strv_length(deps
), sizeof (char*), list_dependencies_compare
);
1630 STRV_FOREACH(c
, deps
) {
1631 if (strv_contains(*units
, *c
)) {
1633 r
= list_dependencies_print("...", level
+ 1, (branches
<< 1) | (c
[1] == NULL
? 0 : 1), 1);
1646 state
= check_one_unit(bus
, *c
, "activating\0active\0reloading\0", true);
1647 on
= state
> 0 ? ansi_highlight_green() : ansi_highlight_red();
1648 printf("%s%s%s ", on
, draw_special_char(DRAW_BLACK_CIRCLE
), ansi_normal());
1651 r
= list_dependencies_print(*c
, level
, branches
, c
[1] == NULL
);
1655 if (arg_all
|| unit_name_to_type(*c
) == UNIT_TARGET
) {
1656 r
= list_dependencies_one(bus
, *c
, level
+ 1, units
, (branches
<< 1) | (c
[1] == NULL
? 0 : 1));
1663 strv_remove(*units
, name
);
1668 static int list_dependencies(int argc
, char *argv
[], void *userdata
) {
1669 _cleanup_strv_free_
char **units
= NULL
;
1670 _cleanup_free_
char *unit
= NULL
;
1676 r
= unit_name_mangle(argv
[1], UNIT_NAME_NOGLOB
, &unit
);
1678 return log_error_errno(r
, "Failed to mangle unit name: %m");
1682 u
= SPECIAL_DEFAULT_TARGET
;
1684 pager_open_if_enabled();
1686 r
= acquire_bus(BUS_MANAGER
, &bus
);
1692 return list_dependencies_one(bus
, u
, 0, &units
, 0);
1695 struct machine_info
{
1699 char *control_group
;
1700 uint32_t n_failed_units
;
1705 static const struct bus_properties_map machine_info_property_map
[] = {
1706 { "SystemState", "s", NULL
, offsetof(struct machine_info
, state
) },
1707 { "NJobs", "u", NULL
, offsetof(struct machine_info
, n_jobs
) },
1708 { "NFailedUnits", "u", NULL
, offsetof(struct machine_info
, n_failed_units
) },
1709 { "ControlGroup", "s", NULL
, offsetof(struct machine_info
, control_group
) },
1710 { "UserspaceTimestamp", "t", NULL
, offsetof(struct machine_info
, timestamp
) },
1714 static void machine_info_clear(struct machine_info
*info
) {
1718 free(info
->control_group
);
1723 static void free_machines_list(struct machine_info
*machine_infos
, int n
) {
1729 for (i
= 0; i
< n
; i
++)
1730 machine_info_clear(&machine_infos
[i
]);
1732 free(machine_infos
);
1735 static int compare_machine_info(const void *a
, const void *b
) {
1736 const struct machine_info
*u
= a
, *v
= b
;
1738 if (u
->is_host
!= v
->is_host
)
1739 return u
->is_host
> v
->is_host
? -1 : 1;
1741 return strcasecmp(u
->name
, v
->name
);
1744 static int get_machine_properties(sd_bus
*bus
, struct machine_info
*mi
) {
1745 _cleanup_bus_flush_close_unref_ sd_bus
*container
= NULL
;
1751 r
= sd_bus_open_system_machine(&container
, mi
->name
);
1758 r
= bus_map_all_properties(bus
, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map
, mi
);
1765 static bool output_show_machine(const char *name
, char **patterns
) {
1766 return strv_fnmatch_or_empty(patterns
, name
, FNM_NOESCAPE
);
1769 static int get_machine_list(
1771 struct machine_info
**_machine_infos
,
1774 struct machine_info
*machine_infos
= NULL
;
1775 _cleanup_strv_free_
char **m
= NULL
;
1776 _cleanup_free_
char *hn
= NULL
;
1781 hn
= gethostname_malloc();
1785 if (output_show_machine(hn
, patterns
)) {
1786 if (!GREEDY_REALLOC0(machine_infos
, sz
, c
+1))
1789 machine_infos
[c
].is_host
= true;
1790 machine_infos
[c
].name
= hn
;
1793 get_machine_properties(bus
, &machine_infos
[c
]);
1797 r
= sd_get_machine_names(&m
);
1799 return log_error_errno(r
, "Failed to get machine list: %m");
1801 STRV_FOREACH(i
, m
) {
1802 _cleanup_free_
char *class = NULL
;
1804 if (!output_show_machine(*i
, patterns
))
1807 sd_machine_get_class(*i
, &class);
1808 if (!streq_ptr(class, "container"))
1811 if (!GREEDY_REALLOC0(machine_infos
, sz
, c
+1)) {
1812 free_machines_list(machine_infos
, c
);
1816 machine_infos
[c
].is_host
= false;
1817 machine_infos
[c
].name
= strdup(*i
);
1818 if (!machine_infos
[c
].name
) {
1819 free_machines_list(machine_infos
, c
);
1823 get_machine_properties(NULL
, &machine_infos
[c
]);
1827 *_machine_infos
= machine_infos
;
1831 static void output_machines_list(struct machine_info
*machine_infos
, unsigned n
) {
1832 struct machine_info
*m
;
1835 namelen
= sizeof("NAME") - 1,
1836 statelen
= sizeof("STATE") - 1,
1837 failedlen
= sizeof("FAILED") - 1,
1838 jobslen
= sizeof("JOBS") - 1;
1840 assert(machine_infos
|| n
== 0);
1842 for (m
= machine_infos
; m
< machine_infos
+ n
; m
++) {
1843 namelen
= MAX(namelen
, strlen(m
->name
) + (m
->is_host
? sizeof(" (host)") - 1 : 0));
1844 statelen
= MAX(statelen
, m
->state
? strlen(m
->state
) : 0);
1845 failedlen
= MAX(failedlen
, DECIMAL_STR_WIDTH(m
->n_failed_units
));
1846 jobslen
= MAX(jobslen
, DECIMAL_STR_WIDTH(m
->n_jobs
));
1848 if (!arg_plain
&& !streq_ptr(m
->state
, "running"))
1852 if (!arg_no_legend
) {
1856 printf("%-*s %-*s %-*s %-*s\n",
1859 failedlen
, "FAILED",
1863 for (m
= machine_infos
; m
< machine_infos
+ n
; m
++) {
1864 const char *on_state
= "", *off_state
= "";
1865 const char *on_failed
= "", *off_failed
= "";
1866 bool circle
= false;
1868 if (streq_ptr(m
->state
, "degraded")) {
1869 on_state
= ansi_highlight_red();
1870 off_state
= ansi_normal();
1872 } else if (!streq_ptr(m
->state
, "running")) {
1873 on_state
= ansi_highlight_yellow();
1874 off_state
= ansi_normal();
1878 if (m
->n_failed_units
> 0) {
1879 on_failed
= ansi_highlight_red();
1880 off_failed
= ansi_normal();
1882 on_failed
= off_failed
= "";
1885 printf("%s%s%s ", on_state
, circle
? draw_special_char(DRAW_BLACK_CIRCLE
) : " ", off_state
);
1888 printf("%-*s (host) %s%-*s%s %s%*u%s %*u\n",
1889 (int) (namelen
- (sizeof(" (host)")-1)), strna(m
->name
),
1890 on_state
, statelen
, strna(m
->state
), off_state
,
1891 on_failed
, failedlen
, m
->n_failed_units
, off_failed
,
1892 jobslen
, m
->n_jobs
);
1894 printf("%-*s %s%-*s%s %s%*u%s %*u\n",
1895 namelen
, strna(m
->name
),
1896 on_state
, statelen
, strna(m
->state
), off_state
,
1897 on_failed
, failedlen
, m
->n_failed_units
, off_failed
,
1898 jobslen
, m
->n_jobs
);
1902 printf("\n%u machines listed.\n", n
);
1905 static int list_machines(int argc
, char *argv
[], void *userdata
) {
1906 struct machine_info
*machine_infos
= NULL
;
1910 if (geteuid() != 0) {
1911 log_error("Must be root.");
1915 pager_open_if_enabled();
1917 r
= acquire_bus(BUS_MANAGER
, &bus
);
1921 r
= get_machine_list(bus
, &machine_infos
, strv_skip(argv
, 1));
1925 qsort_safe(machine_infos
, r
, sizeof(struct machine_info
), compare_machine_info
);
1926 output_machines_list(machine_infos
, r
);
1927 free_machines_list(machine_infos
, r
);
1932 static int get_default(int argc
, char *argv
[], void *userdata
) {
1933 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
1934 _cleanup_free_
char *_path
= NULL
;
1938 if (install_client_side()) {
1939 r
= unit_file_get_default(arg_scope
, arg_root
, &_path
);
1941 return log_error_errno(r
, "Failed to get default target: %m");
1945 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1948 r
= acquire_bus(BUS_MANAGER
, &bus
);
1952 r
= sd_bus_call_method(
1954 "org.freedesktop.systemd1",
1955 "/org/freedesktop/systemd1",
1956 "org.freedesktop.systemd1.Manager",
1962 return log_error_errno(r
, "Failed to get default target: %s", bus_error_message(&error
, r
));
1964 r
= sd_bus_message_read(reply
, "s", &path
);
1966 return bus_log_parse_error(r
);
1970 printf("%s\n", path
);
1975 static void dump_unit_file_changes(const UnitFileChange
*changes
, unsigned n_changes
) {
1978 assert(changes
|| n_changes
== 0);
1980 for (i
= 0; i
< n_changes
; i
++) {
1981 if (changes
[i
].type
== UNIT_FILE_SYMLINK
)
1982 log_info("Created symlink from %s to %s.", changes
[i
].path
, changes
[i
].source
);
1984 log_info("Removed symlink %s.", changes
[i
].path
);
1988 static int set_default(int argc
, char *argv
[], void *userdata
) {
1989 _cleanup_free_
char *unit
= NULL
;
1995 r
= unit_name_mangle_with_suffix(argv
[1], UNIT_NAME_NOGLOB
, ".target", &unit
);
1997 return log_error_errno(r
, "Failed to mangle unit name: %m");
1999 if (install_client_side()) {
2000 UnitFileChange
*changes
= NULL
;
2001 unsigned n_changes
= 0;
2003 r
= unit_file_set_default(arg_scope
, arg_root
, unit
, true, &changes
, &n_changes
);
2005 return log_error_errno(r
, "Failed to set default target: %m");
2008 dump_unit_file_changes(changes
, n_changes
);
2010 unit_file_changes_free(changes
, n_changes
);
2013 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
2014 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
2017 polkit_agent_open_if_enabled();
2019 r
= acquire_bus(BUS_MANAGER
, &bus
);
2023 r
= sd_bus_call_method(
2025 "org.freedesktop.systemd1",
2026 "/org/freedesktop/systemd1",
2027 "org.freedesktop.systemd1.Manager",
2033 return log_error_errno(r
, "Failed to set default target: %s", bus_error_message(&error
, r
));
2035 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, NULL
, NULL
);
2039 /* Try to reload if enabled */
2041 r
= daemon_reload(argc
, argv
, userdata
);
2051 const char *name
, *type
, *state
;
2054 static void output_jobs_list(const struct job_info
* jobs
, unsigned n
, bool skipped
) {
2055 unsigned id_len
, unit_len
, type_len
, state_len
;
2056 const struct job_info
*j
;
2057 const char *on
, *off
;
2058 bool shorten
= false;
2060 assert(n
== 0 || jobs
);
2063 if (!arg_no_legend
) {
2064 on
= ansi_highlight_green();
2065 off
= ansi_normal();
2067 printf("%sNo jobs %s.%s\n", on
, skipped
? "listed" : "running", off
);
2072 pager_open_if_enabled();
2074 id_len
= strlen("JOB");
2075 unit_len
= strlen("UNIT");
2076 type_len
= strlen("TYPE");
2077 state_len
= strlen("STATE");
2079 for (j
= jobs
; j
< jobs
+ n
; j
++) {
2080 uint32_t id
= j
->id
;
2081 assert(j
->name
&& j
->type
&& j
->state
);
2083 id_len
= MAX(id_len
, DECIMAL_STR_WIDTH(id
));
2084 unit_len
= MAX(unit_len
, strlen(j
->name
));
2085 type_len
= MAX(type_len
, strlen(j
->type
));
2086 state_len
= MAX(state_len
, strlen(j
->state
));
2089 if (!arg_full
&& id_len
+ 1 + unit_len
+ type_len
+ 1 + state_len
> columns()) {
2090 unit_len
= MAX(33u, columns() - id_len
- type_len
- state_len
- 3);
2095 printf("%*s %-*s %-*s %-*s\n",
2099 state_len
, "STATE");
2101 for (j
= jobs
; j
< jobs
+ n
; j
++) {
2102 _cleanup_free_
char *e
= NULL
;
2104 if (streq(j
->state
, "running")) {
2105 on
= ansi_highlight();
2106 off
= ansi_normal();
2110 e
= shorten
? ellipsize(j
->name
, unit_len
, 33) : NULL
;
2111 printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
2113 on
, unit_len
, e
? e
: j
->name
, off
,
2115 on
, state_len
, j
->state
, off
);
2118 if (!arg_no_legend
) {
2119 on
= ansi_highlight();
2120 off
= ansi_normal();
2122 printf("\n%s%u jobs listed%s.\n", on
, n
, off
);
2126 static bool output_show_job(struct job_info
*job
, char **patterns
) {
2127 return strv_fnmatch_or_empty(patterns
, job
->name
, FNM_NOESCAPE
);
2130 static int list_jobs(int argc
, char *argv
[], void *userdata
) {
2131 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
2132 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
2133 const char *name
, *type
, *state
, *job_path
, *unit_path
;
2134 _cleanup_free_
struct job_info
*jobs
= NULL
;
2140 bool skipped
= false;
2142 pager_open_if_enabled();
2144 r
= acquire_bus(BUS_MANAGER
, &bus
);
2148 r
= sd_bus_call_method(
2150 "org.freedesktop.systemd1",
2151 "/org/freedesktop/systemd1",
2152 "org.freedesktop.systemd1.Manager",
2158 return log_error_errno(r
, "Failed to list jobs: %s", bus_error_message(&error
, r
));
2160 r
= sd_bus_message_enter_container(reply
, 'a', "(usssoo)");
2162 return bus_log_parse_error(r
);
2164 while ((r
= sd_bus_message_read(reply
, "(usssoo)", &id
, &name
, &type
, &state
, &job_path
, &unit_path
)) > 0) {
2165 struct job_info job
= { id
, name
, type
, state
};
2167 if (!output_show_job(&job
, strv_skip(argv
, 1))) {
2172 if (!GREEDY_REALLOC(jobs
, size
, c
+ 1))
2178 return bus_log_parse_error(r
);
2180 r
= sd_bus_message_exit_container(reply
);
2182 return bus_log_parse_error(r
);
2184 output_jobs_list(jobs
, c
, skipped
);
2188 static int cancel_job(int argc
, char *argv
[], void *userdata
) {
2194 return daemon_reload(argc
, argv
, userdata
);
2196 polkit_agent_open_if_enabled();
2198 r
= acquire_bus(BUS_MANAGER
, &bus
);
2202 STRV_FOREACH(name
, strv_skip(argv
, 1)) {
2203 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
2207 q
= safe_atou32(*name
, &id
);
2209 return log_error_errno(q
, "Failed to parse job id \"%s\": %m", *name
);
2211 q
= sd_bus_call_method(
2213 "org.freedesktop.systemd1",
2214 "/org/freedesktop/systemd1",
2215 "org.freedesktop.systemd1.Manager",
2221 log_error_errno(q
, "Failed to cancel job %"PRIu32
": %s", id
, bus_error_message(&error
, q
));
2230 static int need_daemon_reload(sd_bus
*bus
, const char *unit
) {
2231 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
2235 /* We ignore all errors here, since this is used to show a
2238 /* We don't use unit_dbus_path_from_name() directly since we
2239 * don't want to load the unit if it isn't loaded. */
2241 r
= sd_bus_call_method(
2243 "org.freedesktop.systemd1",
2244 "/org/freedesktop/systemd1",
2245 "org.freedesktop.systemd1.Manager",
2253 r
= sd_bus_message_read(reply
, "o", &path
);
2257 r
= sd_bus_get_property_trivial(
2259 "org.freedesktop.systemd1",
2261 "org.freedesktop.systemd1.Unit",
2271 static void warn_unit_file_changed(const char *name
) {
2272 log_warning("%sWarning:%s %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.",
2273 ansi_highlight_red(),
2276 arg_scope
== UNIT_FILE_SYSTEM
? "" : " --user");
2279 static int unit_file_find_path(LookupPaths
*lp
, const char *unit_name
, char **unit_path
) {
2286 STRV_FOREACH(p
, lp
->unit_path
) {
2287 _cleanup_free_
char *path
;
2289 path
= path_join(arg_root
, *p
, unit_name
);
2293 if (access(path
, F_OK
) == 0) {
2303 static int unit_find_paths(
2305 const char *unit_name
,
2307 char **fragment_path
,
2308 char ***dropin_paths
) {
2310 _cleanup_free_
char *path
= NULL
;
2311 _cleanup_strv_free_
char **dropins
= NULL
;
2315 * Finds where the unit is defined on disk. Returns 0 if the unit
2316 * is not found. Returns 1 if it is found, and sets
2317 * - the path to the unit in *path, if it exists on disk,
2318 * - and a strv of existing drop-ins in *dropins,
2319 * if the arg is not NULL and any dropins were found.
2323 assert(fragment_path
);
2326 if (!install_client_side() && !unit_name_is_valid(unit_name
, UNIT_NAME_TEMPLATE
)) {
2327 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
2328 _cleanup_bus_message_unref_ sd_bus_message
*unit_load_error
= NULL
;
2329 _cleanup_free_
char *unit
= NULL
;
2330 char *unit_load_error_name
, *unit_load_error_message
;
2332 unit
= unit_dbus_path_from_name(unit_name
);
2336 if (need_daemon_reload(bus
, unit_name
) > 0)
2337 warn_unit_file_changed(unit_name
);
2339 r
= sd_bus_get_property(
2341 "org.freedesktop.systemd1",
2343 "org.freedesktop.systemd1.Unit",
2349 return log_error_errno(r
, "Failed to get LoadError: %s", bus_error_message(&error
, r
));
2351 r
= sd_bus_message_read(
2354 &unit_load_error_name
,
2355 &unit_load_error_message
);
2357 return bus_log_parse_error(r
);
2359 if (!isempty(unit_load_error_name
)) {
2360 log_error("Unit %s is not loaded: %s", unit_name
, unit_load_error_message
);
2364 r
= sd_bus_get_property_string(
2366 "org.freedesktop.systemd1",
2368 "org.freedesktop.systemd1.Unit",
2373 return log_error_errno(r
, "Failed to get FragmentPath: %s", bus_error_message(&error
, r
));
2376 r
= sd_bus_get_property_strv(
2378 "org.freedesktop.systemd1",
2380 "org.freedesktop.systemd1.Unit",
2385 return log_error_errno(r
, "Failed to get DropInPaths: %s", bus_error_message(&error
, r
));
2388 _cleanup_set_free_ Set
*names
;
2390 names
= set_new(NULL
);
2394 r
= set_put(names
, unit_name
);
2396 return log_error_errno(r
, "Failed to add unit name: %m");
2398 r
= unit_file_find_path(lp
, unit_name
, &path
);
2403 _cleanup_free_
char *template = NULL
;
2405 r
= unit_name_template(unit_name
, &template);
2406 if (r
< 0 && r
!= -EINVAL
)
2407 return log_error_errno(r
, "Failed to determine template name: %m");
2409 r
= unit_file_find_path(lp
, template, &path
);
2416 r
= unit_file_find_dropin_paths(lp
->unit_path
, NULL
, names
, &dropins
);
2424 if (!isempty(path
)) {
2425 *fragment_path
= path
;
2430 if (dropin_paths
&& !strv_isempty(dropins
)) {
2431 *dropin_paths
= dropins
;
2437 log_error("No files found for %s.", unit_name
);
2442 static int check_one_unit(sd_bus
*bus
, const char *name
, const char *good_states
, bool quiet
) {
2443 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
2444 _cleanup_free_
char *n
= NULL
, *state
= NULL
;
2450 r
= unit_name_mangle(name
, UNIT_NAME_NOGLOB
, &n
);
2452 return log_error_errno(r
, "Failed to mangle unit name: %m");
2454 /* We don't use unit_dbus_path_from_name() directly since we
2455 * don't want to load the unit if it isn't loaded. */
2457 r
= sd_bus_call_method(
2459 "org.freedesktop.systemd1",
2460 "/org/freedesktop/systemd1",
2461 "org.freedesktop.systemd1.Manager",
2472 r
= sd_bus_message_read(reply
, "o", &path
);
2474 return bus_log_parse_error(r
);
2476 r
= sd_bus_get_property_string(
2478 "org.freedesktop.systemd1",
2480 "org.freedesktop.systemd1.Unit",
2493 return nulstr_contains(good_states
, state
);
2496 static int check_triggering_units(
2500 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
2501 _cleanup_free_
char *path
= NULL
, *n
= NULL
, *state
= NULL
;
2502 _cleanup_strv_free_
char **triggered_by
= NULL
;
2503 bool print_warning_label
= true;
2507 r
= unit_name_mangle(name
, UNIT_NAME_NOGLOB
, &n
);
2509 return log_error_errno(r
, "Failed to mangle unit name: %m");
2511 path
= unit_dbus_path_from_name(n
);
2515 r
= sd_bus_get_property_string(
2517 "org.freedesktop.systemd1",
2519 "org.freedesktop.systemd1.Unit",
2524 return log_error_errno(r
, "Failed to get load state of %s: %s", n
, bus_error_message(&error
, r
));
2526 if (streq(state
, "masked"))
2529 r
= sd_bus_get_property_strv(
2531 "org.freedesktop.systemd1",
2533 "org.freedesktop.systemd1.Unit",
2538 return log_error_errno(r
, "Failed to get triggered by array of %s: %s", n
, bus_error_message(&error
, r
));
2540 STRV_FOREACH(i
, triggered_by
) {
2541 r
= check_one_unit(bus
, *i
, "active\0reloading\0", true);
2543 return log_error_errno(r
, "Failed to check unit: %m");
2548 if (print_warning_label
) {
2549 log_warning("Warning: Stopping %s, but it can still be activated by:", n
);
2550 print_warning_label
= false;
2553 log_warning(" %s", *i
);
2559 static const struct {
2562 } unit_actions
[] = {
2563 { "start", "StartUnit" },
2564 { "stop", "StopUnit" },
2565 { "condstop", "StopUnit" },
2566 { "reload", "ReloadUnit" },
2567 { "restart", "RestartUnit" },
2568 { "try-restart", "TryRestartUnit" },
2569 { "condrestart", "TryRestartUnit" },
2570 { "reload-or-restart", "ReloadOrRestartUnit" },
2571 { "reload-or-try-restart", "ReloadOrTryRestartUnit" },
2572 { "condreload", "ReloadOrTryRestartUnit" },
2573 { "force-reload", "ReloadOrTryRestartUnit" }
2576 static const char *verb_to_method(const char *verb
) {
2579 for (i
= 0; i
< ELEMENTSOF(unit_actions
); i
++)
2580 if (streq_ptr(unit_actions
[i
].verb
, verb
))
2581 return unit_actions
[i
].method
;
2586 static const char *method_to_verb(const char *method
) {
2589 for (i
= 0; i
< ELEMENTSOF(unit_actions
); i
++)
2590 if (streq_ptr(unit_actions
[i
].method
, method
))
2591 return unit_actions
[i
].verb
;
2596 static int start_unit_one(
2601 sd_bus_error
*error
,
2602 BusWaitForJobs
*w
) {
2604 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
2613 log_debug("Calling manager for %s on %s, %s", method
, name
, mode
);
2615 r
= sd_bus_call_method(
2617 "org.freedesktop.systemd1",
2618 "/org/freedesktop/systemd1",
2619 "org.freedesktop.systemd1.Manager",
2627 if (r
== -ENOENT
&& arg_action
!= ACTION_SYSTEMCTL
)
2628 /* There's always a fallback possible for
2629 * legacy actions. */
2630 return -EADDRNOTAVAIL
;
2632 verb
= method_to_verb(method
);
2634 return log_error_errno(r
, "Failed to %s %s: %s", verb
, name
, bus_error_message(error
, r
));
2637 r
= sd_bus_message_read(reply
, "o", &path
);
2639 return bus_log_parse_error(r
);
2641 if (need_daemon_reload(bus
, name
) > 0)
2642 warn_unit_file_changed(name
);
2645 log_debug("Adding %s to the set", path
);
2646 r
= bus_wait_for_jobs_add(w
, path
);
2654 static int expand_names(sd_bus
*bus
, char **names
, const char* suffix
, char ***ret
) {
2655 _cleanup_strv_free_
char **mangled
= NULL
, **globs
= NULL
;
2662 STRV_FOREACH(name
, names
) {
2666 r
= unit_name_mangle_with_suffix(*name
, UNIT_NAME_GLOB
, suffix
, &t
);
2668 r
= unit_name_mangle(*name
, UNIT_NAME_GLOB
, &t
);
2670 return log_error_errno(r
, "Failed to mangle name: %m");
2672 if (string_is_glob(t
))
2673 r
= strv_consume(&globs
, t
);
2675 r
= strv_consume(&mangled
, t
);
2680 /* Query the manager only if any of the names are a glob, since
2681 * this is fairly expensive */
2682 if (!strv_isempty(globs
)) {
2683 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
2684 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
2686 r
= get_unit_list(bus
, NULL
, globs
, &unit_infos
, 0, &reply
);
2690 for (i
= 0; i
< r
; i
++)
2691 if (strv_extend(&mangled
, unit_infos
[i
].id
) < 0)
2696 mangled
= NULL
; /* do not free */
2701 static const struct {
2705 } action_table
[_ACTION_MAX
] = {
2706 [ACTION_HALT
] = { SPECIAL_HALT_TARGET
, "halt", "replace-irreversibly" },
2707 [ACTION_POWEROFF
] = { SPECIAL_POWEROFF_TARGET
, "poweroff", "replace-irreversibly" },
2708 [ACTION_REBOOT
] = { SPECIAL_REBOOT_TARGET
, "reboot", "replace-irreversibly" },
2709 [ACTION_KEXEC
] = { SPECIAL_KEXEC_TARGET
, "kexec", "replace-irreversibly" },
2710 [ACTION_RUNLEVEL2
] = { SPECIAL_MULTI_USER_TARGET
, NULL
, "isolate" },
2711 [ACTION_RUNLEVEL3
] = { SPECIAL_MULTI_USER_TARGET
, NULL
, "isolate" },
2712 [ACTION_RUNLEVEL4
] = { SPECIAL_MULTI_USER_TARGET
, NULL
, "isolate" },
2713 [ACTION_RUNLEVEL5
] = { SPECIAL_GRAPHICAL_TARGET
, NULL
, "isolate" },
2714 [ACTION_RESCUE
] = { SPECIAL_RESCUE_TARGET
, "rescue", "isolate" },
2715 [ACTION_EMERGENCY
] = { SPECIAL_EMERGENCY_TARGET
, "emergency", "isolate" },
2716 [ACTION_DEFAULT
] = { SPECIAL_DEFAULT_TARGET
, "default", "isolate" },
2717 [ACTION_EXIT
] = { SPECIAL_EXIT_TARGET
, "exit", "replace-irreversibly" },
2718 [ACTION_SUSPEND
] = { SPECIAL_SUSPEND_TARGET
, "suspend", "replace-irreversibly" },
2719 [ACTION_HIBERNATE
] = { SPECIAL_HIBERNATE_TARGET
, "hibernate", "replace-irreversibly" },
2720 [ACTION_HYBRID_SLEEP
] = { SPECIAL_HYBRID_SLEEP_TARGET
, "hybrid-sleep", "replace-irreversibly" },
2723 static enum action
verb_to_action(const char *verb
) {
2726 for (i
= _ACTION_INVALID
; i
< _ACTION_MAX
; i
++)
2727 if (streq_ptr(action_table
[i
].verb
, verb
))
2730 return _ACTION_INVALID
;
2733 static int start_unit(int argc
, char *argv
[], void *userdata
) {
2734 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
2735 const char *method
, *mode
, *one_name
, *suffix
= NULL
;
2736 _cleanup_strv_free_
char **names
= NULL
;
2741 ask_password_agent_open_if_enabled();
2742 polkit_agent_open_if_enabled();
2744 r
= acquire_bus(BUS_MANAGER
, &bus
);
2748 if (arg_action
== ACTION_SYSTEMCTL
) {
2751 method
= verb_to_method(argv
[0]);
2752 action
= verb_to_action(argv
[0]);
2754 if (streq(argv
[0], "isolate")) {
2758 mode
= action_table
[action
].mode
?: arg_job_mode
;
2760 one_name
= action_table
[action
].target
;
2762 assert(arg_action
< ELEMENTSOF(action_table
));
2763 assert(action_table
[arg_action
].target
);
2765 method
= "StartUnit";
2767 mode
= action_table
[arg_action
].mode
;
2768 one_name
= action_table
[arg_action
].target
;
2772 names
= strv_new(one_name
, NULL
);
2774 r
= expand_names(bus
, strv_skip(argv
, 1), suffix
, &names
);
2776 return log_error_errno(r
, "Failed to expand names: %m");
2779 if (!arg_no_block
) {
2780 r
= bus_wait_for_jobs_new(bus
, &w
);
2782 return log_error_errno(r
, "Could not watch jobs: %m");
2785 STRV_FOREACH(name
, names
) {
2786 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
2789 q
= start_unit_one(bus
, method
, *name
, mode
, &error
, w
);
2790 if (r
>= 0 && q
< 0)
2791 r
= translate_bus_error_to_exit_status(q
, &error
);
2794 if (!arg_no_block
) {
2797 q
= bus_wait_for_jobs(w
, arg_quiet
);
2801 /* When stopping units, warn if they can still be triggered by
2802 * another active unit (socket, path, timer) */
2803 if (!arg_quiet
&& streq(method
, "StopUnit"))
2804 STRV_FOREACH(name
, names
)
2805 check_triggering_units(bus
, *name
);
2811 static int logind_set_wall_message(void) {
2813 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
2815 _cleanup_free_
char *m
= NULL
;
2818 r
= acquire_bus(BUS_FULL
, &bus
);
2822 m
= strv_join(arg_wall
, " ");
2826 r
= sd_bus_call_method(
2828 "org.freedesktop.login1",
2829 "/org/freedesktop/login1",
2830 "org.freedesktop.login1.Manager",
2839 return log_warning_errno(r
, "Failed to set wall message, ignoring: %s", bus_error_message(&error
, r
));
2845 /* Ask systemd-logind, which might grant access to unprivileged users
2846 * through PolicyKit */
2847 static int logind_reboot(enum action a
) {
2849 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
2850 const char *method
, *description
;
2854 polkit_agent_open_if_enabled();
2855 (void) logind_set_wall_message();
2857 r
= acquire_bus(BUS_FULL
, &bus
);
2865 description
= "reboot system";
2868 case ACTION_POWEROFF
:
2869 method
= "PowerOff";
2870 description
= "power off system";
2873 case ACTION_SUSPEND
:
2875 description
= "suspend system";
2878 case ACTION_HIBERNATE
:
2879 method
= "Hibernate";
2880 description
= "hibernate system";
2883 case ACTION_HYBRID_SLEEP
:
2884 method
= "HybridSleep";
2885 description
= "put system into hybrid sleep";
2892 r
= sd_bus_call_method(
2894 "org.freedesktop.login1",
2895 "/org/freedesktop/login1",
2896 "org.freedesktop.login1.Manager",
2900 "b", arg_ask_password
);
2902 return log_error_errno(r
, "Failed to %s via logind: %s", description
, bus_error_message(&error
, r
));
2910 static int logind_check_inhibitors(enum action a
) {
2912 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
2913 _cleanup_strv_free_
char **sessions
= NULL
;
2914 const char *what
, *who
, *why
, *mode
;
2921 if (arg_ignore_inhibitors
|| arg_force
> 0)
2933 r
= acquire_bus(BUS_FULL
, &bus
);
2937 r
= sd_bus_call_method(
2939 "org.freedesktop.login1",
2940 "/org/freedesktop/login1",
2941 "org.freedesktop.login1.Manager",
2947 /* If logind is not around, then there are no inhibitors... */
2950 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssssuu)");
2952 return bus_log_parse_error(r
);
2954 while ((r
= sd_bus_message_read(reply
, "(ssssuu)", &what
, &who
, &why
, &mode
, &uid
, &pid
)) > 0) {
2955 _cleanup_free_
char *comm
= NULL
, *user
= NULL
;
2956 _cleanup_strv_free_
char **sv
= NULL
;
2958 if (!streq(mode
, "block"))
2961 sv
= strv_split(what
, ":");
2965 if ((pid_t
) pid
< 0)
2966 return log_error_errno(ERANGE
, "Bad PID %"PRIu32
": %m", pid
);
2968 if (!strv_contains(sv
,
2973 ACTION_KEXEC
) ? "shutdown" : "sleep"))
2976 get_process_comm(pid
, &comm
);
2977 user
= uid_to_name(uid
);
2979 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT
" \"%s\", user %s), reason is \"%s\".",
2980 who
, (pid_t
) pid
, strna(comm
), strna(user
), why
);
2985 return bus_log_parse_error(r
);
2987 r
= sd_bus_message_exit_container(reply
);
2989 return bus_log_parse_error(r
);
2991 /* Check for current sessions */
2992 sd_get_sessions(&sessions
);
2993 STRV_FOREACH(s
, sessions
) {
2994 _cleanup_free_
char *type
= NULL
, *tty
= NULL
, *seat
= NULL
, *user
= NULL
, *service
= NULL
, *class = NULL
;
2996 if (sd_session_get_uid(*s
, &uid
) < 0 || uid
== getuid())
2999 if (sd_session_get_class(*s
, &class) < 0 || !streq(class, "user"))
3002 if (sd_session_get_type(*s
, &type
) < 0 || (!streq(type
, "x11") && !streq(type
, "tty")))
3005 sd_session_get_tty(*s
, &tty
);
3006 sd_session_get_seat(*s
, &seat
);
3007 sd_session_get_service(*s
, &service
);
3008 user
= uid_to_name(uid
);
3010 log_warning("User %s is logged in on %s.", strna(user
), isempty(tty
) ? (isempty(seat
) ? strna(service
) : seat
) : tty
);
3017 log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
3018 action_table
[a
].verb
);
3026 static int logind_prepare_firmware_setup(void) {
3028 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
3032 r
= acquire_bus(BUS_FULL
, &bus
);
3036 r
= sd_bus_call_method(
3038 "org.freedesktop.login1",
3039 "/org/freedesktop/login1",
3040 "org.freedesktop.login1.Manager",
3041 "SetRebootToFirmwareSetup",
3046 return log_error_errno(r
, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error
, r
));
3050 log_error("Cannot remotely indicate to EFI to boot into setup mode.");
3055 static int prepare_firmware_setup(void) {
3058 if (!arg_firmware_setup
)
3061 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
3063 r
= efi_set_reboot_to_firmware(true);
3065 log_debug_errno(r
, "Cannot indicate to EFI to boot into setup mode, will retry via logind: %m");
3070 return logind_prepare_firmware_setup();
3073 static int set_exit_code(uint8_t code
) {
3074 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
3078 r
= acquire_bus(BUS_MANAGER
, &bus
);
3082 r
= sd_bus_call_method(
3084 "org.freedesktop.systemd1",
3085 "/org/freedesktop/systemd1",
3086 "org.freedesktop.systemd1.Manager",
3092 return log_error_errno(r
, "Failed to execute operation: %s", bus_error_message(&error
, r
));
3097 static int start_special(int argc
, char *argv
[], void *userdata
) {
3103 a
= verb_to_action(argv
[0]);
3105 r
= logind_check_inhibitors(a
);
3109 if (arg_force
>= 2 && geteuid() != 0) {
3110 log_error("Must be root.");
3114 r
= prepare_firmware_setup();
3118 if (a
== ACTION_REBOOT
&& argc
> 1) {
3119 r
= update_reboot_param_file(argv
[1]);
3123 } else if (a
== ACTION_EXIT
&& argc
> 1) {
3126 /* If the exit code is not given on the command line,
3127 * don't reset it to zero: just keep it as it might
3128 * have been set previously. */
3130 r
= safe_atou8(argv
[1], &code
);
3132 return log_error_errno(r
, "Invalid exit code.");
3134 r
= set_exit_code(code
);
3139 if (arg_force
>= 2 &&
3146 if (arg_force
>= 1 &&
3153 return daemon_reload(argc
, argv
, userdata
);
3155 /* First try logind, to allow authentication with polkit */
3161 ACTION_HYBRID_SLEEP
)) {
3162 r
= logind_reboot(a
);
3165 if (IN_SET(r
, -EOPNOTSUPP
, -EINPROGRESS
))
3166 /* requested operation is not supported or already in progress */
3169 /* On all other errors, try low-level operation */
3172 return start_unit(argc
, argv
, userdata
);
3175 static int check_unit_generic(int code
, const char *good_states
, char **args
) {
3176 _cleanup_strv_free_
char **names
= NULL
;
3181 r
= acquire_bus(BUS_MANAGER
, &bus
);
3185 r
= expand_names(bus
, args
, NULL
, &names
);
3187 return log_error_errno(r
, "Failed to expand names: %m");
3189 STRV_FOREACH(name
, names
) {
3192 state
= check_one_unit(bus
, *name
, good_states
, arg_quiet
);
3202 static int check_unit_active(int argc
, char *argv
[], void *userdata
) {
3203 /* According to LSB: 3, "program is not running" */
3204 return check_unit_generic(3, "active\0reloading\0", strv_skip(argv
, 1));
3207 static int check_unit_failed(int argc
, char *argv
[], void *userdata
) {
3208 return check_unit_generic(1, "failed\0", strv_skip(argv
, 1));
3211 static int kill_unit(int argc
, char *argv
[], void *userdata
) {
3212 _cleanup_strv_free_
char **names
= NULL
;
3213 char *kill_who
= NULL
, **name
;
3217 polkit_agent_open_if_enabled();
3219 r
= acquire_bus(BUS_MANAGER
, &bus
);
3224 arg_kill_who
= "all";
3226 /* --fail was specified */
3227 if (streq(arg_job_mode
, "fail"))
3228 kill_who
= strjoina(arg_kill_who
, "-fail", NULL
);
3230 r
= expand_names(bus
, strv_skip(argv
, 1), NULL
, &names
);
3232 return log_error_errno(r
, "Failed to expand names: %m");
3234 STRV_FOREACH(name
, names
) {
3235 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
3237 q
= sd_bus_call_method(
3239 "org.freedesktop.systemd1",
3240 "/org/freedesktop/systemd1",
3241 "org.freedesktop.systemd1.Manager",
3245 "ssi", *names
, kill_who
? kill_who
: arg_kill_who
, arg_signal
);
3247 log_error_errno(q
, "Failed to kill unit %s: %s", *names
, bus_error_message(&error
, q
));
3256 typedef struct ExecStatusInfo
{
3264 usec_t start_timestamp
;
3265 usec_t exit_timestamp
;
3270 LIST_FIELDS(struct ExecStatusInfo
, exec
);
3273 static void exec_status_info_free(ExecStatusInfo
*i
) {
3282 static int exec_status_info_deserialize(sd_bus_message
*m
, ExecStatusInfo
*i
) {
3283 uint64_t start_timestamp
, exit_timestamp
, start_timestamp_monotonic
, exit_timestamp_monotonic
;
3286 int32_t code
, status
;
3292 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_STRUCT
, "sasbttttuii");
3294 return bus_log_parse_error(r
);
3298 r
= sd_bus_message_read(m
, "s", &path
);
3300 return bus_log_parse_error(r
);
3302 i
->path
= strdup(path
);
3306 r
= sd_bus_message_read_strv(m
, &i
->argv
);
3308 return bus_log_parse_error(r
);
3310 r
= sd_bus_message_read(m
,
3313 &start_timestamp
, &start_timestamp_monotonic
,
3314 &exit_timestamp
, &exit_timestamp_monotonic
,
3318 return bus_log_parse_error(r
);
3321 i
->start_timestamp
= (usec_t
) start_timestamp
;
3322 i
->exit_timestamp
= (usec_t
) exit_timestamp
;
3323 i
->pid
= (pid_t
) pid
;
3327 r
= sd_bus_message_exit_container(m
);
3329 return bus_log_parse_error(r
);
3334 typedef struct UnitStatusInfo
{
3336 const char *load_state
;
3337 const char *active_state
;
3338 const char *sub_state
;
3339 const char *unit_file_state
;
3340 const char *unit_file_preset
;
3342 const char *description
;
3343 const char *following
;
3345 char **documentation
;
3347 const char *fragment_path
;
3348 const char *source_path
;
3349 const char *control_group
;
3351 char **dropin_paths
;
3353 const char *load_error
;
3356 usec_t inactive_exit_timestamp
;
3357 usec_t inactive_exit_timestamp_monotonic
;
3358 usec_t active_enter_timestamp
;
3359 usec_t active_exit_timestamp
;
3360 usec_t inactive_enter_timestamp
;
3362 bool need_daemon_reload
;
3367 const char *status_text
;
3368 const char *pid_file
;
3372 usec_t start_timestamp
;
3373 usec_t exit_timestamp
;
3375 int exit_code
, exit_status
;
3377 usec_t condition_timestamp
;
3378 bool condition_result
;
3379 bool failed_condition_trigger
;
3380 bool failed_condition_negate
;
3381 const char *failed_condition
;
3382 const char *failed_condition_parameter
;
3384 usec_t assert_timestamp
;
3386 bool failed_assert_trigger
;
3387 bool failed_assert_negate
;
3388 const char *failed_assert
;
3389 const char *failed_assert_parameter
;
3392 unsigned n_accepted
;
3393 unsigned n_connections
;
3396 /* Pairs of type, path */
3400 const char *sysfs_path
;
3402 /* Mount, Automount */
3409 uint64_t memory_current
;
3410 uint64_t memory_limit
;
3411 uint64_t cpu_usage_nsec
;
3412 uint64_t tasks_current
;
3415 LIST_HEAD(ExecStatusInfo
, exec
);
3418 static void print_status_info(
3423 const char *active_on
, *active_off
, *on
, *off
, *ss
;
3425 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
3426 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
3432 /* This shows pretty information about a unit. See
3433 * print_property() for a low-level property printer */
3435 if (streq_ptr(i
->active_state
, "failed")) {
3436 active_on
= ansi_highlight_red();
3437 active_off
= ansi_normal();
3438 } else if (streq_ptr(i
->active_state
, "active") || streq_ptr(i
->active_state
, "reloading")) {
3439 active_on
= ansi_highlight_green();
3440 active_off
= ansi_normal();
3442 active_on
= active_off
= "";
3444 printf("%s%s%s %s", active_on
, draw_special_char(DRAW_BLACK_CIRCLE
), active_off
, strna(i
->id
));
3446 if (i
->description
&& !streq_ptr(i
->id
, i
->description
))
3447 printf(" - %s", i
->description
);
3452 printf(" Follow: unit currently follows state of %s\n", i
->following
);
3454 if (streq_ptr(i
->load_state
, "error")) {
3455 on
= ansi_highlight_red();
3456 off
= ansi_normal();
3460 path
= i
->source_path
? i
->source_path
: i
->fragment_path
;
3463 printf(" Loaded: %s%s%s (Reason: %s)\n",
3464 on
, strna(i
->load_state
), off
, i
->load_error
);
3465 else if (path
&& !isempty(i
->unit_file_state
) && !isempty(i
->unit_file_preset
))
3466 printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n",
3467 on
, strna(i
->load_state
), off
, path
, i
->unit_file_state
, i
->unit_file_preset
);
3468 else if (path
&& !isempty(i
->unit_file_state
))
3469 printf(" Loaded: %s%s%s (%s; %s)\n",
3470 on
, strna(i
->load_state
), off
, path
, i
->unit_file_state
);
3472 printf(" Loaded: %s%s%s (%s)\n",
3473 on
, strna(i
->load_state
), off
, path
);
3475 printf(" Loaded: %s%s%s\n",
3476 on
, strna(i
->load_state
), off
);
3478 if (!strv_isempty(i
->dropin_paths
)) {
3479 _cleanup_free_
char *dir
= NULL
;
3483 STRV_FOREACH(dropin
, i
->dropin_paths
) {
3484 if (! dir
|| last
) {
3485 printf(dir
? " " : " Drop-In: ");
3489 dir
= dirname_malloc(*dropin
);
3495 printf("%s\n %s", dir
,
3496 draw_special_char(DRAW_TREE_RIGHT
));
3499 last
= ! (*(dropin
+ 1) && startswith(*(dropin
+ 1), dir
));
3501 printf("%s%s", basename(*dropin
), last
? "\n" : ", ");
3505 ss
= streq_ptr(i
->active_state
, i
->sub_state
) ? NULL
: i
->sub_state
;
3507 printf(" Active: %s%s (%s)%s",
3508 active_on
, strna(i
->active_state
), ss
, active_off
);
3510 printf(" Active: %s%s%s",
3511 active_on
, strna(i
->active_state
), active_off
);
3513 if (!isempty(i
->result
) && !streq(i
->result
, "success"))
3514 printf(" (Result: %s)", i
->result
);
3516 timestamp
= (streq_ptr(i
->active_state
, "active") ||
3517 streq_ptr(i
->active_state
, "reloading")) ? i
->active_enter_timestamp
:
3518 (streq_ptr(i
->active_state
, "inactive") ||
3519 streq_ptr(i
->active_state
, "failed")) ? i
->inactive_enter_timestamp
:
3520 streq_ptr(i
->active_state
, "activating") ? i
->inactive_exit_timestamp
:
3521 i
->active_exit_timestamp
;
3523 s1
= format_timestamp_relative(since1
, sizeof(since1
), timestamp
);
3524 s2
= format_timestamp(since2
, sizeof(since2
), timestamp
);
3527 printf(" since %s; %s\n", s2
, s1
);
3529 printf(" since %s\n", s2
);
3533 if (!i
->condition_result
&& i
->condition_timestamp
> 0) {
3534 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->condition_timestamp
);
3535 s2
= format_timestamp(since2
, sizeof(since2
), i
->condition_timestamp
);
3537 printf("Condition: start %scondition failed%s at %s%s%s\n",
3538 ansi_highlight_yellow(), ansi_normal(),
3539 s2
, s1
? "; " : "", strempty(s1
));
3540 if (i
->failed_condition_trigger
)
3541 printf(" none of the trigger conditions were met\n");
3542 else if (i
->failed_condition
)
3543 printf(" %s=%s%s was not met\n",
3544 i
->failed_condition
,
3545 i
->failed_condition_negate
? "!" : "",
3546 i
->failed_condition_parameter
);
3549 if (!i
->assert_result
&& i
->assert_timestamp
> 0) {
3550 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->assert_timestamp
);
3551 s2
= format_timestamp(since2
, sizeof(since2
), i
->assert_timestamp
);
3553 printf(" Assert: start %sassertion failed%s at %s%s%s\n",
3554 ansi_highlight_red(), ansi_normal(),
3555 s2
, s1
? "; " : "", strempty(s1
));
3556 if (i
->failed_assert_trigger
)
3557 printf(" none of the trigger assertions were met\n");
3558 else if (i
->failed_assert
)
3559 printf(" %s=%s%s was not met\n",
3561 i
->failed_assert_negate
? "!" : "",
3562 i
->failed_assert_parameter
);
3566 printf(" Device: %s\n", i
->sysfs_path
);
3568 printf(" Where: %s\n", i
->where
);
3570 printf(" What: %s\n", i
->what
);
3572 STRV_FOREACH(t
, i
->documentation
)
3573 printf(" %*s %s\n", 9, t
== i
->documentation
? "Docs:" : "", *t
);
3575 STRV_FOREACH_PAIR(t
, t2
, i
->listen
)
3576 printf(" %*s %s (%s)\n", 9, t
== i
->listen
? "Listen:" : "", *t2
, *t
);
3579 printf(" Accepted: %u; Connected: %u\n", i
->n_accepted
, i
->n_connections
);
3581 LIST_FOREACH(exec
, p
, i
->exec
) {
3582 _cleanup_free_
char *argv
= NULL
;
3585 /* Only show exited processes here */
3589 argv
= strv_join(p
->argv
, " ");
3590 printf(" Process: "PID_FMT
" %s=%s ", p
->pid
, p
->name
, strna(argv
));
3592 good
= is_clean_exit_lsb(p
->code
, p
->status
, NULL
);
3594 on
= ansi_highlight_red();
3595 off
= ansi_normal();
3599 printf("%s(code=%s, ", on
, sigchld_code_to_string(p
->code
));
3601 if (p
->code
== CLD_EXITED
) {
3604 printf("status=%i", p
->status
);
3606 c
= exit_status_to_string(p
->status
, EXIT_STATUS_SYSTEMD
);
3611 printf("signal=%s", signal_to_string(p
->status
));
3613 printf(")%s\n", off
);
3615 if (i
->main_pid
== p
->pid
&&
3616 i
->start_timestamp
== p
->start_timestamp
&&
3617 i
->exit_timestamp
== p
->start_timestamp
)
3618 /* Let's not show this twice */
3621 if (p
->pid
== i
->control_pid
)
3625 if (i
->main_pid
> 0 || i
->control_pid
> 0) {
3626 if (i
->main_pid
> 0) {
3627 printf(" Main PID: "PID_FMT
, i
->main_pid
);
3630 _cleanup_free_
char *comm
= NULL
;
3631 get_process_comm(i
->main_pid
, &comm
);
3633 printf(" (%s)", comm
);
3634 } else if (i
->exit_code
> 0) {
3635 printf(" (code=%s, ", sigchld_code_to_string(i
->exit_code
));
3637 if (i
->exit_code
== CLD_EXITED
) {
3640 printf("status=%i", i
->exit_status
);
3642 c
= exit_status_to_string(i
->exit_status
, EXIT_STATUS_SYSTEMD
);
3647 printf("signal=%s", signal_to_string(i
->exit_status
));
3651 if (i
->control_pid
> 0)
3655 if (i
->control_pid
> 0) {
3656 _cleanup_free_
char *c
= NULL
;
3658 printf(" %8s: "PID_FMT
, i
->main_pid
? "" : " Control", i
->control_pid
);
3660 get_process_comm(i
->control_pid
, &c
);
3669 printf(" Status: \"%s\"\n", i
->status_text
);
3670 if (i
->status_errno
> 0)
3671 printf(" Error: %i (%s)\n", i
->status_errno
, strerror(i
->status_errno
));
3673 if (i
->tasks_current
!= (uint64_t) -1) {
3674 printf(" Tasks: %" PRIu64
, i
->tasks_current
);
3676 if (i
->tasks_max
!= (uint64_t) -1)
3677 printf(" (limit: %" PRIi64
")\n", i
->tasks_max
);
3682 if (i
->memory_current
!= (uint64_t) -1) {
3683 char buf
[FORMAT_BYTES_MAX
];
3685 printf(" Memory: %s", format_bytes(buf
, sizeof(buf
), i
->memory_current
));
3687 if (i
->memory_limit
!= (uint64_t) -1)
3688 printf(" (limit: %s)\n", format_bytes(buf
, sizeof(buf
), i
->memory_limit
));
3693 if (i
->cpu_usage_nsec
!= (uint64_t) -1) {
3694 char buf
[FORMAT_TIMESPAN_MAX
];
3695 printf(" CPU: %s\n", format_timespan(buf
, sizeof(buf
), i
->cpu_usage_nsec
/ NSEC_PER_USEC
, USEC_PER_MSEC
));
3698 if (i
->control_group
&&
3699 (i
->main_pid
> 0 || i
->control_pid
> 0 ||
3700 (!IN_SET(arg_transport
, BUS_TRANSPORT_LOCAL
, BUS_TRANSPORT_MACHINE
) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, i
->control_group
) == 0))) {
3703 printf(" CGroup: %s\n", i
->control_group
);
3705 if (IN_SET(arg_transport
,
3706 BUS_TRANSPORT_LOCAL
,
3707 BUS_TRANSPORT_MACHINE
)) {
3710 static const char prefix
[] = " ";
3713 if (c
> sizeof(prefix
) - 1)
3714 c
-= sizeof(prefix
) - 1;
3718 if (i
->main_pid
> 0)
3719 extra
[k
++] = i
->main_pid
;
3721 if (i
->control_pid
> 0)
3722 extra
[k
++] = i
->control_pid
;
3724 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, i
->control_group
, prefix
, c
, false, extra
, k
, get_output_flags());
3728 if (i
->id
&& arg_transport
== BUS_TRANSPORT_LOCAL
)
3729 show_journal_by_unit(
3734 i
->inactive_exit_timestamp_monotonic
,
3737 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
3738 SD_JOURNAL_LOCAL_ONLY
,
3739 arg_scope
== UNIT_FILE_SYSTEM
,
3742 if (i
->need_daemon_reload
)
3743 warn_unit_file_changed(i
->id
);
3746 static void show_unit_help(UnitStatusInfo
*i
) {
3751 if (!i
->documentation
) {
3752 log_info("Documentation for %s not known.", i
->id
);
3756 STRV_FOREACH(p
, i
->documentation
)
3757 if (startswith(*p
, "man:"))
3758 show_man_page(*p
+ 4, false);
3760 log_info("Can't show: %s", *p
);
3763 static int status_property(const char *name
, sd_bus_message
*m
, UnitStatusInfo
*i
, const char *contents
) {
3770 switch (contents
[0]) {
3772 case SD_BUS_TYPE_STRING
: {
3775 r
= sd_bus_message_read(m
, "s", &s
);
3777 return bus_log_parse_error(r
);
3780 if (streq(name
, "Id"))
3782 else if (streq(name
, "LoadState"))
3784 else if (streq(name
, "ActiveState"))
3785 i
->active_state
= s
;
3786 else if (streq(name
, "SubState"))
3788 else if (streq(name
, "Description"))
3790 else if (streq(name
, "FragmentPath"))
3791 i
->fragment_path
= s
;
3792 else if (streq(name
, "SourcePath"))
3795 else if (streq(name
, "DefaultControlGroup")) {
3797 e
= startswith(s
, SYSTEMD_CGROUP_CONTROLLER
":");
3799 i
->control_group
= e
;
3802 else if (streq(name
, "ControlGroup"))
3803 i
->control_group
= s
;
3804 else if (streq(name
, "StatusText"))
3806 else if (streq(name
, "PIDFile"))
3808 else if (streq(name
, "SysFSPath"))
3810 else if (streq(name
, "Where"))
3812 else if (streq(name
, "What"))
3814 else if (streq(name
, "Following"))
3816 else if (streq(name
, "UnitFileState"))
3817 i
->unit_file_state
= s
;
3818 else if (streq(name
, "UnitFilePreset"))
3819 i
->unit_file_preset
= s
;
3820 else if (streq(name
, "Result"))
3827 case SD_BUS_TYPE_BOOLEAN
: {
3830 r
= sd_bus_message_read(m
, "b", &b
);
3832 return bus_log_parse_error(r
);
3834 if (streq(name
, "Accept"))
3836 else if (streq(name
, "NeedDaemonReload"))
3837 i
->need_daemon_reload
= b
;
3838 else if (streq(name
, "ConditionResult"))
3839 i
->condition_result
= b
;
3840 else if (streq(name
, "AssertResult"))
3841 i
->assert_result
= b
;
3846 case SD_BUS_TYPE_UINT32
: {
3849 r
= sd_bus_message_read(m
, "u", &u
);
3851 return bus_log_parse_error(r
);
3853 if (streq(name
, "MainPID")) {
3855 i
->main_pid
= (pid_t
) u
;
3858 } else if (streq(name
, "ControlPID"))
3859 i
->control_pid
= (pid_t
) u
;
3860 else if (streq(name
, "ExecMainPID")) {
3862 i
->main_pid
= (pid_t
) u
;
3863 } else if (streq(name
, "NAccepted"))
3865 else if (streq(name
, "NConnections"))
3866 i
->n_connections
= u
;
3871 case SD_BUS_TYPE_INT32
: {
3874 r
= sd_bus_message_read(m
, "i", &j
);
3876 return bus_log_parse_error(r
);
3878 if (streq(name
, "ExecMainCode"))
3879 i
->exit_code
= (int) j
;
3880 else if (streq(name
, "ExecMainStatus"))
3881 i
->exit_status
= (int) j
;
3882 else if (streq(name
, "StatusErrno"))
3883 i
->status_errno
= (int) j
;
3888 case SD_BUS_TYPE_UINT64
: {
3891 r
= sd_bus_message_read(m
, "t", &u
);
3893 return bus_log_parse_error(r
);
3895 if (streq(name
, "ExecMainStartTimestamp"))
3896 i
->start_timestamp
= (usec_t
) u
;
3897 else if (streq(name
, "ExecMainExitTimestamp"))
3898 i
->exit_timestamp
= (usec_t
) u
;
3899 else if (streq(name
, "ActiveEnterTimestamp"))
3900 i
->active_enter_timestamp
= (usec_t
) u
;
3901 else if (streq(name
, "InactiveEnterTimestamp"))
3902 i
->inactive_enter_timestamp
= (usec_t
) u
;
3903 else if (streq(name
, "InactiveExitTimestamp"))
3904 i
->inactive_exit_timestamp
= (usec_t
) u
;
3905 else if (streq(name
, "InactiveExitTimestampMonotonic"))
3906 i
->inactive_exit_timestamp_monotonic
= (usec_t
) u
;
3907 else if (streq(name
, "ActiveExitTimestamp"))
3908 i
->active_exit_timestamp
= (usec_t
) u
;
3909 else if (streq(name
, "ConditionTimestamp"))
3910 i
->condition_timestamp
= (usec_t
) u
;
3911 else if (streq(name
, "AssertTimestamp"))
3912 i
->assert_timestamp
= (usec_t
) u
;
3913 else if (streq(name
, "MemoryCurrent"))
3914 i
->memory_current
= u
;
3915 else if (streq(name
, "MemoryLimit"))
3916 i
->memory_limit
= u
;
3917 else if (streq(name
, "TasksCurrent"))
3918 i
->tasks_current
= u
;
3919 else if (streq(name
, "TasksMax"))
3921 else if (streq(name
, "CPUUsageNSec"))
3922 i
->cpu_usage_nsec
= u
;
3927 case SD_BUS_TYPE_ARRAY
:
3929 if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& startswith(name
, "Exec")) {
3930 _cleanup_free_ ExecStatusInfo
*info
= NULL
;
3932 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sasbttttuii)");
3934 return bus_log_parse_error(r
);
3936 info
= new0(ExecStatusInfo
, 1);
3940 while ((r
= exec_status_info_deserialize(m
, info
)) > 0) {
3942 info
->name
= strdup(name
);
3946 LIST_PREPEND(exec
, i
->exec
, info
);
3948 info
= new0(ExecStatusInfo
, 1);
3954 return bus_log_parse_error(r
);
3956 r
= sd_bus_message_exit_container(m
);
3958 return bus_log_parse_error(r
);
3962 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Listen")) {
3963 const char *type
, *path
;
3965 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(ss)");
3967 return bus_log_parse_error(r
);
3969 while ((r
= sd_bus_message_read(m
, "(ss)", &type
, &path
)) > 0) {
3971 r
= strv_extend(&i
->listen
, type
);
3975 r
= strv_extend(&i
->listen
, path
);
3980 return bus_log_parse_error(r
);
3982 r
= sd_bus_message_exit_container(m
);
3984 return bus_log_parse_error(r
);
3988 } else if (contents
[1] == SD_BUS_TYPE_STRING
&& streq(name
, "DropInPaths")) {
3990 r
= sd_bus_message_read_strv(m
, &i
->dropin_paths
);
3992 return bus_log_parse_error(r
);
3994 } else if (contents
[1] == SD_BUS_TYPE_STRING
&& streq(name
, "Documentation")) {
3996 r
= sd_bus_message_read_strv(m
, &i
->documentation
);
3998 return bus_log_parse_error(r
);
4000 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Conditions")) {
4001 const char *cond
, *param
;
4002 int trigger
, negate
;
4005 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sbbsi)");
4007 return bus_log_parse_error(r
);
4009 while ((r
= sd_bus_message_read(m
, "(sbbsi)", &cond
, &trigger
, &negate
, ¶m
, &state
)) > 0) {
4010 log_debug("%s %d %d %s %d", cond
, trigger
, negate
, param
, state
);
4011 if (state
< 0 && (!trigger
|| !i
->failed_condition
)) {
4012 i
->failed_condition
= cond
;
4013 i
->failed_condition_trigger
= trigger
;
4014 i
->failed_condition_negate
= negate
;
4015 i
->failed_condition_parameter
= param
;
4019 return bus_log_parse_error(r
);
4021 r
= sd_bus_message_exit_container(m
);
4023 return bus_log_parse_error(r
);
4025 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Asserts")) {
4026 const char *cond
, *param
;
4027 int trigger
, negate
;
4030 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sbbsi)");
4032 return bus_log_parse_error(r
);
4034 while ((r
= sd_bus_message_read(m
, "(sbbsi)", &cond
, &trigger
, &negate
, ¶m
, &state
)) > 0) {
4035 log_debug("%s %d %d %s %d", cond
, trigger
, negate
, param
, state
);
4036 if (state
< 0 && (!trigger
|| !i
->failed_assert
)) {
4037 i
->failed_assert
= cond
;
4038 i
->failed_assert_trigger
= trigger
;
4039 i
->failed_assert_negate
= negate
;
4040 i
->failed_assert_parameter
= param
;
4044 return bus_log_parse_error(r
);
4046 r
= sd_bus_message_exit_container(m
);
4048 return bus_log_parse_error(r
);
4055 case SD_BUS_TYPE_STRUCT_BEGIN
:
4057 if (streq(name
, "LoadError")) {
4058 const char *n
, *message
;
4060 r
= sd_bus_message_read(m
, "(ss)", &n
, &message
);
4062 return bus_log_parse_error(r
);
4064 if (!isempty(message
))
4065 i
->load_error
= message
;
4078 r
= sd_bus_message_skip(m
, contents
);
4080 return bus_log_parse_error(r
);
4085 static int print_property(const char *name
, sd_bus_message
*m
, const char *contents
) {
4091 /* This is a low-level property printer, see
4092 * print_status_info() for the nicer output */
4094 if (arg_properties
&& !strv_find(arg_properties
, name
)) {
4095 /* skip what we didn't read */
4096 r
= sd_bus_message_skip(m
, contents
);
4100 switch (contents
[0]) {
4102 case SD_BUS_TYPE_STRUCT_BEGIN
:
4104 if (contents
[1] == SD_BUS_TYPE_UINT32
&& streq(name
, "Job")) {
4107 r
= sd_bus_message_read(m
, "(uo)", &u
, NULL
);
4109 return bus_log_parse_error(r
);
4112 printf("%s=%"PRIu32
"\n", name
, u
);
4114 printf("%s=\n", name
);
4118 } else if (contents
[1] == SD_BUS_TYPE_STRING
&& streq(name
, "Unit")) {
4121 r
= sd_bus_message_read(m
, "(so)", &s
, NULL
);
4123 return bus_log_parse_error(r
);
4125 if (arg_all
|| !isempty(s
))
4126 printf("%s=%s\n", name
, s
);
4130 } else if (contents
[1] == SD_BUS_TYPE_STRING
&& streq(name
, "LoadError")) {
4131 const char *a
= NULL
, *b
= NULL
;
4133 r
= sd_bus_message_read(m
, "(ss)", &a
, &b
);
4135 return bus_log_parse_error(r
);
4137 if (arg_all
|| !isempty(a
) || !isempty(b
))
4138 printf("%s=%s \"%s\"\n", name
, strempty(a
), strempty(b
));
4141 } else if (streq_ptr(name
, "SystemCallFilter")) {
4142 _cleanup_strv_free_
char **l
= NULL
;
4145 r
= sd_bus_message_enter_container(m
, 'r', "bas");
4147 return bus_log_parse_error(r
);
4149 r
= sd_bus_message_read(m
, "b", &whitelist
);
4151 return bus_log_parse_error(r
);
4153 r
= sd_bus_message_read_strv(m
, &l
);
4155 return bus_log_parse_error(r
);
4157 r
= sd_bus_message_exit_container(m
);
4159 return bus_log_parse_error(r
);
4161 if (arg_all
|| whitelist
|| !strv_isempty(l
)) {
4165 fputs(name
, stdout
);
4171 STRV_FOREACH(i
, l
) {
4179 fputc('\n', stdout
);
4187 case SD_BUS_TYPE_ARRAY
:
4189 if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "EnvironmentFiles")) {
4193 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sb)");
4195 return bus_log_parse_error(r
);
4197 while ((r
= sd_bus_message_read(m
, "(sb)", &path
, &ignore
)) > 0)
4198 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path
, yes_no(ignore
));
4201 return bus_log_parse_error(r
);
4203 r
= sd_bus_message_exit_container(m
);
4205 return bus_log_parse_error(r
);
4209 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Paths")) {
4210 const char *type
, *path
;
4212 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(ss)");
4214 return bus_log_parse_error(r
);
4216 while ((r
= sd_bus_message_read(m
, "(ss)", &type
, &path
)) > 0)
4217 printf("%s=%s\n", type
, path
);
4219 return bus_log_parse_error(r
);
4221 r
= sd_bus_message_exit_container(m
);
4223 return bus_log_parse_error(r
);
4227 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Listen")) {
4228 const char *type
, *path
;
4230 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(ss)");
4232 return bus_log_parse_error(r
);
4234 while ((r
= sd_bus_message_read(m
, "(ss)", &type
, &path
)) > 0)
4235 printf("Listen%s=%s\n", type
, path
);
4237 return bus_log_parse_error(r
);
4239 r
= sd_bus_message_exit_container(m
);
4241 return bus_log_parse_error(r
);
4245 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "Timers")) {
4247 uint64_t value
, next_elapse
;
4249 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(stt)");
4251 return bus_log_parse_error(r
);
4253 while ((r
= sd_bus_message_read(m
, "(stt)", &base
, &value
, &next_elapse
)) > 0) {
4254 char timespan1
[FORMAT_TIMESPAN_MAX
], timespan2
[FORMAT_TIMESPAN_MAX
];
4256 printf("%s={ value=%s ; next_elapse=%s }\n",
4258 format_timespan(timespan1
, sizeof(timespan1
), value
, 0),
4259 format_timespan(timespan2
, sizeof(timespan2
), next_elapse
, 0));
4262 return bus_log_parse_error(r
);
4264 r
= sd_bus_message_exit_container(m
);
4266 return bus_log_parse_error(r
);
4270 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& startswith(name
, "Exec")) {
4271 ExecStatusInfo info
= {};
4273 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sasbttttuii)");
4275 return bus_log_parse_error(r
);
4277 while ((r
= exec_status_info_deserialize(m
, &info
)) > 0) {
4278 char timestamp1
[FORMAT_TIMESTAMP_MAX
], timestamp2
[FORMAT_TIMESTAMP_MAX
];
4279 _cleanup_free_
char *tt
;
4281 tt
= strv_join(info
.argv
, " ");
4283 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT
" ; code=%s ; status=%i%s%s }\n",
4287 yes_no(info
.ignore
),
4288 strna(format_timestamp(timestamp1
, sizeof(timestamp1
), info
.start_timestamp
)),
4289 strna(format_timestamp(timestamp2
, sizeof(timestamp2
), info
.exit_timestamp
)),
4291 sigchld_code_to_string(info
.code
),
4293 info
.code
== CLD_EXITED
? "" : "/",
4294 strempty(info
.code
== CLD_EXITED
? NULL
: signal_to_string(info
.status
)));
4297 strv_free(info
.argv
);
4301 r
= sd_bus_message_exit_container(m
);
4303 return bus_log_parse_error(r
);
4307 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "DeviceAllow")) {
4308 const char *path
, *rwm
;
4310 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(ss)");
4312 return bus_log_parse_error(r
);
4314 while ((r
= sd_bus_message_read(m
, "(ss)", &path
, &rwm
)) > 0)
4315 printf("%s=%s %s\n", name
, strna(path
), strna(rwm
));
4317 return bus_log_parse_error(r
);
4319 r
= sd_bus_message_exit_container(m
);
4321 return bus_log_parse_error(r
);
4325 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& streq(name
, "BlockIODeviceWeight")) {
4329 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(st)");
4331 return bus_log_parse_error(r
);
4333 while ((r
= sd_bus_message_read(m
, "(st)", &path
, &weight
)) > 0)
4334 printf("%s=%s %" PRIu64
"\n", name
, strna(path
), weight
);
4336 return bus_log_parse_error(r
);
4338 r
= sd_bus_message_exit_container(m
);
4340 return bus_log_parse_error(r
);
4344 } else if (contents
[1] == SD_BUS_TYPE_STRUCT_BEGIN
&& (streq(name
, "BlockIOReadBandwidth") || streq(name
, "BlockIOWriteBandwidth"))) {
4348 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(st)");
4350 return bus_log_parse_error(r
);
4352 while ((r
= sd_bus_message_read(m
, "(st)", &path
, &bandwidth
)) > 0)
4353 printf("%s=%s %" PRIu64
"\n", name
, strna(path
), bandwidth
);
4355 return bus_log_parse_error(r
);
4357 r
= sd_bus_message_exit_container(m
);
4359 return bus_log_parse_error(r
);
4367 r
= bus_print_property(name
, m
, arg_all
);
4369 return bus_log_parse_error(r
);
4372 r
= sd_bus_message_skip(m
, contents
);
4374 return bus_log_parse_error(r
);
4377 printf("%s=[unprintable]\n", name
);
4383 static int show_one(
4387 bool show_properties
,
4391 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
4392 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
4393 UnitStatusInfo info
= {
4394 .memory_current
= (uint64_t) -1,
4395 .memory_limit
= (uint64_t) -1,
4396 .cpu_usage_nsec
= (uint64_t) -1,
4397 .tasks_current
= (uint64_t) -1,
4398 .tasks_max
= (uint64_t) -1,
4406 log_debug("Showing one %s", path
);
4408 r
= sd_bus_call_method(
4410 "org.freedesktop.systemd1",
4412 "org.freedesktop.DBus.Properties",
4418 return log_error_errno(r
, "Failed to get properties: %s", bus_error_message(&error
, r
));
4420 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "{sv}");
4422 return bus_log_parse_error(r
);
4429 while ((r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
4430 const char *name
, *contents
;
4432 r
= sd_bus_message_read(reply
, "s", &name
);
4434 return bus_log_parse_error(r
);
4436 r
= sd_bus_message_peek_type(reply
, NULL
, &contents
);
4438 return bus_log_parse_error(r
);
4440 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_VARIANT
, contents
);
4442 return bus_log_parse_error(r
);
4444 if (show_properties
)
4445 r
= print_property(name
, reply
, contents
);
4447 r
= status_property(name
, reply
, &info
, contents
);
4451 r
= sd_bus_message_exit_container(reply
);
4453 return bus_log_parse_error(r
);
4455 r
= sd_bus_message_exit_container(reply
);
4457 return bus_log_parse_error(r
);
4460 return bus_log_parse_error(r
);
4462 r
= sd_bus_message_exit_container(reply
);
4464 return bus_log_parse_error(r
);
4468 if (!show_properties
) {
4469 if (streq(verb
, "help"))
4470 show_unit_help(&info
);
4472 print_status_info(&info
, ellipsized
);
4475 strv_free(info
.documentation
);
4476 strv_free(info
.dropin_paths
);
4477 strv_free(info
.listen
);
4479 if (!streq_ptr(info
.active_state
, "active") &&
4480 !streq_ptr(info
.active_state
, "reloading") &&
4481 streq(verb
, "status")) {
4482 /* According to LSB: "program not running" */
4483 /* 0: program is running or service is OK
4484 * 1: program is dead and /run PID file exists
4485 * 2: program is dead and /run/lock lock file exists
4486 * 3: program is not running
4487 * 4: program or service status is unknown
4489 if (info
.pid_file
&& access(info
.pid_file
, F_OK
) == 0)
4495 while ((p
= info
.exec
)) {
4496 LIST_REMOVE(exec
, info
.exec
, p
);
4497 exec_status_info_free(p
);
4503 static int get_unit_dbus_path_by_pid(
4508 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
4509 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
4513 r
= sd_bus_call_method(
4515 "org.freedesktop.systemd1",
4516 "/org/freedesktop/systemd1",
4517 "org.freedesktop.systemd1.Manager",
4523 return log_error_errno(r
, "Failed to get unit for PID %"PRIu32
": %s", pid
, bus_error_message(&error
, r
));
4525 r
= sd_bus_message_read(reply
, "o", &u
);
4527 return bus_log_parse_error(r
);
4537 static int show_all(
4540 bool show_properties
,
4544 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
4545 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
4550 r
= get_unit_list(bus
, NULL
, NULL
, &unit_infos
, 0, &reply
);
4554 pager_open_if_enabled();
4558 qsort_safe(unit_infos
, c
, sizeof(UnitInfo
), compare_unit_info
);
4560 for (u
= unit_infos
; u
< unit_infos
+ c
; u
++) {
4561 _cleanup_free_
char *p
= NULL
;
4563 p
= unit_dbus_path_from_name(u
->id
);
4567 r
= show_one(verb
, bus
, p
, show_properties
, new_line
, ellipsized
);
4570 else if (r
> 0 && ret
== 0)
4577 static int show_system_status(sd_bus
*bus
) {
4578 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], since2
[FORMAT_TIMESTAMP_MAX
];
4579 _cleanup_free_
char *hn
= NULL
;
4580 _cleanup_(machine_info_clear
) struct machine_info mi
= {};
4581 const char *on
, *off
;
4584 hn
= gethostname_malloc();
4588 r
= bus_map_all_properties(bus
, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map
, &mi
);
4590 return log_error_errno(r
, "Failed to read server status: %m");
4592 if (streq_ptr(mi
.state
, "degraded")) {
4593 on
= ansi_highlight_red();
4594 off
= ansi_normal();
4595 } else if (!streq_ptr(mi
.state
, "running")) {
4596 on
= ansi_highlight_yellow();
4597 off
= ansi_normal();
4601 printf("%s%s%s %s\n", on
, draw_special_char(DRAW_BLACK_CIRCLE
), off
, arg_host
? arg_host
: hn
);
4603 printf(" State: %s%s%s\n",
4604 on
, strna(mi
.state
), off
);
4606 printf(" Jobs: %u queued\n", mi
.n_jobs
);
4607 printf(" Failed: %u units\n", mi
.n_failed_units
);
4609 printf(" Since: %s; %s\n",
4610 format_timestamp(since2
, sizeof(since2
), mi
.timestamp
),
4611 format_timestamp_relative(since1
, sizeof(since1
), mi
.timestamp
));
4613 printf(" CGroup: %s\n", mi
.control_group
?: "/");
4614 if (IN_SET(arg_transport
,
4615 BUS_TRANSPORT_LOCAL
,
4616 BUS_TRANSPORT_MACHINE
)) {
4617 static const char prefix
[] = " ";
4621 if (c
> sizeof(prefix
) - 1)
4622 c
-= sizeof(prefix
) - 1;
4626 show_cgroup(SYSTEMD_CGROUP_CONTROLLER
, strempty(mi
.control_group
), prefix
, c
, false, get_output_flags());
4632 static int show(int argc
, char *argv
[], void *userdata
) {
4633 bool show_properties
, show_status
, show_help
, new_line
= false;
4634 bool ellipsized
= false;
4640 show_properties
= streq(argv
[0], "show");
4641 show_status
= streq(argv
[0], "status");
4642 show_help
= streq(argv
[0], "help");
4644 if (show_help
&& argc
<= 1) {
4645 log_error("This command expects one or more unit names. Did you mean --help?");
4649 if (show_properties
)
4650 pager_open_if_enabled();
4653 /* Increase max number of open files to 16K if we can, we
4654 * might needs this when browsing journal files, which might
4655 * be split up into many files. */
4656 setrlimit_closest(RLIMIT_NOFILE
, &RLIMIT_MAKE_CONST(16384));
4658 r
= acquire_bus(BUS_MANAGER
, &bus
);
4662 /* If no argument is specified inspect the manager itself */
4663 if (show_properties
&& argc
<= 1)
4664 return show_one(argv
[0], bus
, "/org/freedesktop/systemd1", show_properties
, &new_line
, &ellipsized
);
4666 if (show_status
&& argc
<= 1) {
4668 pager_open_if_enabled();
4669 show_system_status(bus
);
4673 ret
= show_all(argv
[0], bus
, false, &new_line
, &ellipsized
);
4675 _cleanup_free_
char **patterns
= NULL
;
4678 STRV_FOREACH(name
, strv_skip(argv
, 1)) {
4679 _cleanup_free_
char *unit
= NULL
;
4682 if (safe_atou32(*name
, &id
) < 0) {
4683 if (strv_push(&patterns
, *name
) < 0)
4687 } else if (show_properties
) {
4688 /* Interpret as job id */
4689 if (asprintf(&unit
, "/org/freedesktop/systemd1/job/%u", id
) < 0)
4693 /* Interpret as PID */
4694 r
= get_unit_dbus_path_by_pid(bus
, id
, &unit
);
4701 r
= show_one(argv
[0], bus
, unit
, show_properties
, &new_line
, &ellipsized
);
4704 else if (r
> 0 && ret
== 0)
4708 if (!strv_isempty(patterns
)) {
4709 _cleanup_strv_free_
char **names
= NULL
;
4711 r
= expand_names(bus
, patterns
, NULL
, &names
);
4713 return log_error_errno(r
, "Failed to expand names: %m");
4715 STRV_FOREACH(name
, names
) {
4716 _cleanup_free_
char *unit
;
4718 unit
= unit_dbus_path_from_name(*name
);
4722 r
= show_one(argv
[0], bus
, unit
, show_properties
, &new_line
, &ellipsized
);
4725 else if (r
> 0 && ret
== 0)
4731 if (ellipsized
&& !arg_quiet
)
4732 printf("Hint: Some lines were ellipsized, use -l to show in full.\n");
4737 static int init_home_and_lookup_paths(char **user_home
, char **user_runtime
, LookupPaths
*lp
) {
4741 assert(user_runtime
);
4744 if (arg_scope
== UNIT_FILE_USER
) {
4745 r
= user_config_home(user_home
);
4747 return log_error_errno(r
, "Failed to query XDG_CONFIG_HOME: %m");
4749 return log_error_errno(ENOTDIR
, "Cannot find units: $XDG_CONFIG_HOME and $HOME are not set.");
4751 r
= user_runtime_dir(user_runtime
);
4753 return log_error_errno(r
, "Failed to query XDG_CONFIG_HOME: %m");
4755 return log_error_errno(ENOTDIR
, "Cannot find units: $XDG_RUNTIME_DIR is not set.");
4758 r
= lookup_paths_init_from_scope(lp
, arg_scope
, arg_root
);
4760 return log_error_errno(r
, "Failed to query unit lookup paths: %m");
4765 static int cat_file(const char *filename
, bool newline
) {
4766 _cleanup_close_
int fd
;
4768 fd
= open(filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
4772 printf("%s%s# %s%s\n",
4773 newline
? "\n" : "",
4774 ansi_highlight_blue(),
4779 return copy_bytes(fd
, STDOUT_FILENO
, (uint64_t) -1, false);
4782 static int cat(int argc
, char *argv
[], void *userdata
) {
4783 _cleanup_free_
char *user_home
= NULL
;
4784 _cleanup_free_
char *user_runtime
= NULL
;
4785 _cleanup_lookup_paths_free_ LookupPaths lp
= {};
4786 _cleanup_strv_free_
char **names
= NULL
;
4792 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
4793 log_error("Cannot remotely cat units.");
4797 r
= init_home_and_lookup_paths(&user_home
, &user_runtime
, &lp
);
4801 r
= acquire_bus(BUS_MANAGER
, &bus
);
4805 r
= expand_names(bus
, strv_skip(argv
, 1), NULL
, &names
);
4807 return log_error_errno(r
, "Failed to expand names: %m");
4809 pager_open_if_enabled();
4811 STRV_FOREACH(name
, names
) {
4812 _cleanup_free_
char *fragment_path
= NULL
;
4813 _cleanup_strv_free_
char **dropin_paths
= NULL
;
4816 r
= unit_find_paths(bus
, *name
, &lp
, &fragment_path
, &dropin_paths
);
4827 if (fragment_path
) {
4828 r
= cat_file(fragment_path
, false);
4830 return log_warning_errno(r
, "Failed to cat %s: %m", fragment_path
);
4833 STRV_FOREACH(path
, dropin_paths
) {
4834 r
= cat_file(*path
, path
== dropin_paths
);
4836 return log_warning_errno(r
, "Failed to cat %s: %m", *path
);
4843 static int set_property(int argc
, char *argv
[], void *userdata
) {
4844 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
4845 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
4846 _cleanup_free_
char *n
= NULL
;
4851 polkit_agent_open_if_enabled();
4853 r
= acquire_bus(BUS_MANAGER
, &bus
);
4857 r
= sd_bus_message_new_method_call(
4860 "org.freedesktop.systemd1",
4861 "/org/freedesktop/systemd1",
4862 "org.freedesktop.systemd1.Manager",
4863 "SetUnitProperties");
4865 return bus_log_create_error(r
);
4867 r
= unit_name_mangle(argv
[1], UNIT_NAME_NOGLOB
, &n
);
4869 return log_error_errno(r
, "Failed to mangle unit name: %m");
4871 r
= sd_bus_message_append(m
, "sb", n
, arg_runtime
);
4873 return bus_log_create_error(r
);
4875 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_ARRAY
, "(sv)");
4877 return bus_log_create_error(r
);
4879 STRV_FOREACH(i
, strv_skip(argv
, 2)) {
4880 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
4882 return bus_log_create_error(r
);
4884 r
= bus_append_unit_property_assignment(m
, *i
);
4888 r
= sd_bus_message_close_container(m
);
4890 return bus_log_create_error(r
);
4893 r
= sd_bus_message_close_container(m
);
4895 return bus_log_create_error(r
);
4897 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
4899 return log_error_errno(r
, "Failed to set unit properties on %s: %s", n
, bus_error_message(&error
, r
));
4904 static int daemon_reload(int argc
, char *argv
[], void *userdata
) {
4905 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
4910 polkit_agent_open_if_enabled();
4912 r
= acquire_bus(BUS_MANAGER
, &bus
);
4916 if (arg_action
== ACTION_RELOAD
)
4918 else if (arg_action
== ACTION_REEXEC
)
4919 method
= "Reexecute";
4921 assert(arg_action
== ACTION_SYSTEMCTL
);
4924 streq(argv
[0], "clear-jobs") ||
4925 streq(argv
[0], "cancel") ? "ClearJobs" :
4926 streq(argv
[0], "daemon-reexec") ? "Reexecute" :
4927 streq(argv
[0], "reset-failed") ? "ResetFailed" :
4928 streq(argv
[0], "halt") ? "Halt" :
4929 streq(argv
[0], "poweroff") ? "PowerOff" :
4930 streq(argv
[0], "reboot") ? "Reboot" :
4931 streq(argv
[0], "kexec") ? "KExec" :
4932 streq(argv
[0], "exit") ? "Exit" :
4933 /* "daemon-reload" */ "Reload";
4936 r
= sd_bus_call_method(
4938 "org.freedesktop.systemd1",
4939 "/org/freedesktop/systemd1",
4940 "org.freedesktop.systemd1.Manager",
4945 if (r
== -ENOENT
&& arg_action
!= ACTION_SYSTEMCTL
)
4946 /* There's always a fallback possible for
4947 * legacy actions. */
4949 else if ((r
== -ETIMEDOUT
|| r
== -ECONNRESET
) && streq(method
, "Reexecute"))
4950 /* On reexecution, we expect a disconnect, not a
4954 return log_error_errno(r
, "Failed to execute operation: %s", bus_error_message(&error
, r
));
4956 return r
< 0 ? r
: 0;
4959 static int reset_failed(int argc
, char *argv
[], void *userdata
) {
4960 _cleanup_strv_free_
char **names
= NULL
;
4966 return daemon_reload(argc
, argv
, userdata
);
4968 polkit_agent_open_if_enabled();
4970 r
= acquire_bus(BUS_MANAGER
, &bus
);
4974 r
= expand_names(bus
, strv_skip(argv
, 1), NULL
, &names
);
4976 return log_error_errno(r
, "Failed to expand names: %m");
4978 STRV_FOREACH(name
, names
) {
4979 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
4981 q
= sd_bus_call_method(
4983 "org.freedesktop.systemd1",
4984 "/org/freedesktop/systemd1",
4985 "org.freedesktop.systemd1.Manager",
4991 log_error_errno(q
, "Failed to reset failed state of unit %s: %s", *name
, bus_error_message(&error
, q
));
5000 static int show_environment(int argc
, char *argv
[], void *userdata
) {
5001 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
5002 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
5007 pager_open_if_enabled();
5009 r
= acquire_bus(BUS_MANAGER
, &bus
);
5013 r
= sd_bus_get_property(
5015 "org.freedesktop.systemd1",
5016 "/org/freedesktop/systemd1",
5017 "org.freedesktop.systemd1.Manager",
5023 return log_error_errno(r
, "Failed to get environment: %s", bus_error_message(&error
, r
));
5025 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "s");
5027 return bus_log_parse_error(r
);
5029 while ((r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_STRING
, &text
)) > 0)
5032 return bus_log_parse_error(r
);
5034 r
= sd_bus_message_exit_container(reply
);
5036 return bus_log_parse_error(r
);
5041 static int switch_root(int argc
, char *argv
[], void *userdata
) {
5042 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
5043 _cleanup_free_
char *cmdline_init
= NULL
;
5044 const char *root
, *init
;
5048 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
5049 log_error("Cannot switch root remotely.");
5053 if (argc
< 2 || argc
> 3) {
5054 log_error("Wrong number of arguments.");
5063 r
= parse_env_file("/proc/cmdline", WHITESPACE
,
5064 "init", &cmdline_init
,
5067 log_debug_errno(r
, "Failed to parse /proc/cmdline: %m");
5069 init
= cmdline_init
;
5076 const char *root_systemd_path
= NULL
, *root_init_path
= NULL
;
5078 root_systemd_path
= strjoina(root
, "/" SYSTEMD_BINARY_PATH
);
5079 root_init_path
= strjoina(root
, "/", init
);
5081 /* If the passed init is actually the same as the
5082 * systemd binary, then let's suppress it. */
5083 if (files_same(root_init_path
, root_systemd_path
) > 0)
5087 r
= acquire_bus(BUS_MANAGER
, &bus
);
5091 log_debug("Switching root - root: %s; init: %s", root
, strna(init
));
5093 r
= sd_bus_call_method(
5095 "org.freedesktop.systemd1",
5096 "/org/freedesktop/systemd1",
5097 "org.freedesktop.systemd1.Manager",
5103 return log_error_errno(r
, "Failed to switch root: %s", bus_error_message(&error
, r
));
5108 static int set_environment(int argc
, char *argv
[], void *userdata
) {
5109 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
5110 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
5118 polkit_agent_open_if_enabled();
5120 r
= acquire_bus(BUS_MANAGER
, &bus
);
5124 method
= streq(argv
[0], "set-environment")
5126 : "UnsetEnvironment";
5128 r
= sd_bus_message_new_method_call(
5131 "org.freedesktop.systemd1",
5132 "/org/freedesktop/systemd1",
5133 "org.freedesktop.systemd1.Manager",
5136 return bus_log_create_error(r
);
5138 r
= sd_bus_message_append_strv(m
, strv_skip(argv
, 1));
5140 return bus_log_create_error(r
);
5142 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
5144 return log_error_errno(r
, "Failed to set environment: %s", bus_error_message(&error
, r
));
5149 static int import_environment(int argc
, char *argv
[], void *userdata
) {
5150 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
5151 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
5155 polkit_agent_open_if_enabled();
5157 r
= acquire_bus(BUS_MANAGER
, &bus
);
5161 r
= sd_bus_message_new_method_call(
5164 "org.freedesktop.systemd1",
5165 "/org/freedesktop/systemd1",
5166 "org.freedesktop.systemd1.Manager",
5169 return bus_log_create_error(r
);
5172 r
= sd_bus_message_append_strv(m
, environ
);
5176 r
= sd_bus_message_open_container(m
, 'a', "s");
5178 return bus_log_create_error(r
);
5180 STRV_FOREACH(a
, strv_skip(argv
, 1)) {
5182 if (!env_name_is_valid(*a
)) {
5183 log_error("Not a valid environment variable name: %s", *a
);
5187 STRV_FOREACH(b
, environ
) {
5190 eq
= startswith(*b
, *a
);
5191 if (eq
&& *eq
== '=') {
5193 r
= sd_bus_message_append(m
, "s", *b
);
5195 return bus_log_create_error(r
);
5202 r
= sd_bus_message_close_container(m
);
5205 return bus_log_create_error(r
);
5207 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
5209 return log_error_errno(r
, "Failed to import environment: %s", bus_error_message(&error
, r
));
5214 static int enable_sysv_units(const char *verb
, char **args
) {
5217 #if defined(HAVE_SYSV_COMPAT)
5219 _cleanup_lookup_paths_free_ LookupPaths paths
= {};
5221 if (arg_scope
!= UNIT_FILE_SYSTEM
)
5224 if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0)
5227 if (!STR_IN_SET(verb
,
5233 /* Processes all SysV units, and reshuffles the array so that
5234 * afterwards only the native units remain */
5236 r
= lookup_paths_init(&paths
, MANAGER_SYSTEM
, false, arg_root
, NULL
, NULL
, NULL
);
5243 _cleanup_free_
char *p
= NULL
, *q
= NULL
, *l
= NULL
;
5244 bool found_native
= false, found_sysv
;
5246 const char *argv
[6] = { ROOTLIBEXECDIR
"/systemd-sysv-install", NULL
, NULL
, NULL
, NULL
};
5254 if (!endswith(name
, ".service"))
5257 if (path_is_absolute(name
))
5260 STRV_FOREACH(k
, paths
.unit_path
) {
5261 _cleanup_free_
char *path
= NULL
;
5263 path
= path_join(arg_root
, *k
, name
);
5267 found_native
= access(path
, F_OK
) >= 0;
5272 /* If we have both a native unit and a SysV script,
5273 * enable/disable them both (below); for is-enabled, prefer the
5275 if (found_native
&& streq(verb
, "is-enabled"))
5278 p
= path_join(arg_root
, SYSTEM_SYSVINIT_PATH
, name
);
5282 p
[strlen(p
) - strlen(".service")] = 0;
5283 found_sysv
= access(p
, F_OK
) >= 0;
5288 log_info("Synchronizing state of %s with SysV init with %s...", name
, argv
[0]);
5290 log_info("%s is not a native service, redirecting to systemd-sysv-install", name
);
5292 if (!isempty(arg_root
))
5293 argv
[c
++] = q
= strappend("--root=", arg_root
);
5296 argv
[c
++] = basename(p
);
5299 l
= strv_join((char**)argv
, " ");
5303 log_info("Executing %s", l
);
5307 return log_error_errno(errno
, "Failed to fork: %m");
5308 else if (pid
== 0) {
5311 (void) reset_all_signal_handlers();
5312 (void) reset_signal_mask();
5314 execv(argv
[0], (char**) argv
);
5315 log_error_errno(r
, "Failed to execute %s: %m", argv
[0]);
5316 _exit(EXIT_FAILURE
);
5319 j
= wait_for_terminate(pid
, &status
);
5321 log_error_errno(j
, "Failed to wait for child: %m");
5325 if (status
.si_code
== CLD_EXITED
) {
5326 if (streq(verb
, "is-enabled")) {
5327 if (status
.si_status
== 0) {
5336 } else if (status
.si_status
!= 0)
5344 /* Remove this entry, so that we don't try enabling it as native unit */
5347 assert(args
[f
] == name
);
5348 strv_remove(args
, name
);
5355 static int mangle_names(char **original_names
, char ***mangled_names
) {
5356 char **i
, **l
, **name
;
5359 l
= i
= new(char*, strv_length(original_names
) + 1);
5363 STRV_FOREACH(name
, original_names
) {
5365 /* When enabling units qualified path names are OK,
5366 * too, hence allow them explicitly. */
5368 if (is_path(*name
)) {
5375 r
= unit_name_mangle(*name
, UNIT_NAME_NOGLOB
, i
);
5378 return log_error_errno(r
, "Failed to mangle unit name: %m");
5391 static int enable_unit(int argc
, char *argv
[], void *userdata
) {
5392 _cleanup_strv_free_
char **names
= NULL
;
5393 const char *verb
= argv
[0];
5394 UnitFileChange
*changes
= NULL
;
5395 unsigned n_changes
= 0;
5396 int carries_install_info
= -1;
5402 r
= mangle_names(strv_skip(argv
, 1), &names
);
5406 r
= enable_sysv_units(verb
, names
);
5410 /* If the operation was fully executed by the SysV compat,
5411 * let's finish early */
5412 if (strv_isempty(names
))
5415 if (install_client_side()) {
5416 if (streq(verb
, "enable")) {
5417 r
= unit_file_enable(arg_scope
, arg_runtime
, arg_root
, names
, arg_force
, &changes
, &n_changes
);
5418 carries_install_info
= r
;
5419 } else if (streq(verb
, "disable"))
5420 r
= unit_file_disable(arg_scope
, arg_runtime
, arg_root
, names
, &changes
, &n_changes
);
5421 else if (streq(verb
, "reenable")) {
5422 r
= unit_file_reenable(arg_scope
, arg_runtime
, arg_root
, names
, arg_force
, &changes
, &n_changes
);
5423 carries_install_info
= r
;
5424 } else if (streq(verb
, "link"))
5425 r
= unit_file_link(arg_scope
, arg_runtime
, arg_root
, names
, arg_force
, &changes
, &n_changes
);
5426 else if (streq(verb
, "preset")) {
5427 r
= unit_file_preset(arg_scope
, arg_runtime
, arg_root
, names
, arg_preset_mode
, arg_force
, &changes
, &n_changes
);
5428 carries_install_info
= r
;
5429 } else if (streq(verb
, "mask"))
5430 r
= unit_file_mask(arg_scope
, arg_runtime
, arg_root
, names
, arg_force
, &changes
, &n_changes
);
5431 else if (streq(verb
, "unmask"))
5432 r
= unit_file_unmask(arg_scope
, arg_runtime
, arg_root
, names
, &changes
, &n_changes
);
5434 assert_not_reached("Unknown verb");
5437 log_error_errno(r
, "Operation failed: %m");
5442 dump_unit_file_changes(changes
, n_changes
);
5446 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
, *m
= NULL
;
5447 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
5448 int expect_carries_install_info
= false;
5449 bool send_force
= true, send_preset_mode
= false;
5453 polkit_agent_open_if_enabled();
5455 r
= acquire_bus(BUS_MANAGER
, &bus
);
5459 if (streq(verb
, "enable")) {
5460 method
= "EnableUnitFiles";
5461 expect_carries_install_info
= true;
5462 } else if (streq(verb
, "disable")) {
5463 method
= "DisableUnitFiles";
5465 } else if (streq(verb
, "reenable")) {
5466 method
= "ReenableUnitFiles";
5467 expect_carries_install_info
= true;
5468 } else if (streq(verb
, "link"))
5469 method
= "LinkUnitFiles";
5470 else if (streq(verb
, "preset")) {
5472 if (arg_preset_mode
!= UNIT_FILE_PRESET_FULL
) {
5473 method
= "PresetUnitFilesWithMode";
5474 send_preset_mode
= true;
5476 method
= "PresetUnitFiles";
5478 expect_carries_install_info
= true;
5479 } else if (streq(verb
, "mask"))
5480 method
= "MaskUnitFiles";
5481 else if (streq(verb
, "unmask")) {
5482 method
= "UnmaskUnitFiles";
5485 assert_not_reached("Unknown verb");
5487 r
= sd_bus_message_new_method_call(
5490 "org.freedesktop.systemd1",
5491 "/org/freedesktop/systemd1",
5492 "org.freedesktop.systemd1.Manager",
5495 return bus_log_create_error(r
);
5497 r
= sd_bus_message_append_strv(m
, names
);
5499 return bus_log_create_error(r
);
5501 if (send_preset_mode
) {
5502 r
= sd_bus_message_append(m
, "s", unit_file_preset_mode_to_string(arg_preset_mode
));
5504 return bus_log_create_error(r
);
5507 r
= sd_bus_message_append(m
, "b", arg_runtime
);
5509 return bus_log_create_error(r
);
5512 r
= sd_bus_message_append(m
, "b", arg_force
);
5514 return bus_log_create_error(r
);
5517 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
5519 return log_error_errno(r
, "Failed to execute operation: %s", bus_error_message(&error
, r
));
5521 if (expect_carries_install_info
) {
5522 r
= sd_bus_message_read(reply
, "b", &carries_install_info
);
5524 return bus_log_parse_error(r
);
5527 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
5531 /* Try to reload if enabled */
5533 r
= daemon_reload(argc
, argv
, userdata
);
5538 if (carries_install_info
== 0)
5539 log_warning("The unit files have no [Install] section. They are not meant to be enabled\n"
5540 "using systemctl.\n"
5541 "Possible reasons for having this kind of units are:\n"
5542 "1) A unit may be statically enabled by being symlinked from another unit's\n"
5543 " .wants/ or .requires/ directory.\n"
5544 "2) A unit's purpose may be to act as a helper for some other unit which has\n"
5545 " a requirement dependency on it.\n"
5546 "3) A unit may be started when needed via activation (socket, path, timer,\n"
5547 " D-Bus, udev, scripted systemctl call, ...).\n");
5549 if (arg_now
&& n_changes
> 0 && STR_IN_SET(argv
[0], "enable", "disable", "mask")) {
5550 char *new_args
[n_changes
+ 2];
5554 r
= acquire_bus(BUS_MANAGER
, &bus
);
5558 new_args
[0] = (char*) (streq(argv
[0], "enable") ? "start" : "stop");
5559 for (i
= 0; i
< n_changes
; i
++)
5560 new_args
[i
+ 1] = basename(changes
[i
].path
);
5561 new_args
[i
+ 1] = NULL
;
5563 r
= start_unit(strv_length(new_args
), new_args
, userdata
);
5567 unit_file_changes_free(changes
, n_changes
);
5572 static int add_dependency(int argc
, char *argv
[], void *userdata
) {
5573 _cleanup_strv_free_
char **names
= NULL
;
5574 _cleanup_free_
char *target
= NULL
;
5575 const char *verb
= argv
[0];
5582 r
= unit_name_mangle_with_suffix(argv
[1], UNIT_NAME_NOGLOB
, ".target", &target
);
5584 return log_error_errno(r
, "Failed to mangle unit name: %m");
5586 r
= mangle_names(strv_skip(argv
, 2), &names
);
5590 if (streq(verb
, "add-wants"))
5592 else if (streq(verb
, "add-requires"))
5593 dep
= UNIT_REQUIRES
;
5595 assert_not_reached("Unknown verb");
5597 if (install_client_side()) {
5598 UnitFileChange
*changes
= NULL
;
5599 unsigned n_changes
= 0;
5601 r
= unit_file_add_dependency(arg_scope
, arg_runtime
, arg_root
, names
, target
, dep
, arg_force
, &changes
, &n_changes
);
5604 return log_error_errno(r
, "Can't add dependency: %m");
5607 dump_unit_file_changes(changes
, n_changes
);
5609 unit_file_changes_free(changes
, n_changes
);
5612 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
, *m
= NULL
;
5613 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
5616 polkit_agent_open_if_enabled();
5618 r
= acquire_bus(BUS_MANAGER
, &bus
);
5622 r
= sd_bus_message_new_method_call(
5625 "org.freedesktop.systemd1",
5626 "/org/freedesktop/systemd1",
5627 "org.freedesktop.systemd1.Manager",
5628 "AddDependencyUnitFiles");
5630 return bus_log_create_error(r
);
5632 r
= sd_bus_message_append_strv(m
, names
);
5634 return bus_log_create_error(r
);
5636 r
= sd_bus_message_append(m
, "ssbb", target
, unit_dependency_to_string(dep
), arg_runtime
, arg_force
);
5638 return bus_log_create_error(r
);
5640 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
5642 return log_error_errno(r
, "Failed to execute operation: %s", bus_error_message(&error
, r
));
5644 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, NULL
, NULL
);
5649 r
= daemon_reload(argc
, argv
, userdata
);
5657 static int preset_all(int argc
, char *argv
[], void *userdata
) {
5658 UnitFileChange
*changes
= NULL
;
5659 unsigned n_changes
= 0;
5662 if (install_client_side()) {
5664 r
= unit_file_preset_all(arg_scope
, arg_runtime
, arg_root
, arg_preset_mode
, arg_force
, &changes
, &n_changes
);
5666 log_error_errno(r
, "Operation failed: %m");
5671 dump_unit_file_changes(changes
, n_changes
);
5676 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
5677 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
5680 polkit_agent_open_if_enabled();
5682 r
= acquire_bus(BUS_MANAGER
, &bus
);
5686 r
= sd_bus_call_method(
5688 "org.freedesktop.systemd1",
5689 "/org/freedesktop/systemd1",
5690 "org.freedesktop.systemd1.Manager",
5691 "PresetAllUnitFiles",
5695 unit_file_preset_mode_to_string(arg_preset_mode
),
5699 return log_error_errno(r
, "Failed to execute operation: %s", bus_error_message(&error
, r
));
5701 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, NULL
, NULL
);
5706 r
= daemon_reload(argc
, argv
, userdata
);
5712 unit_file_changes_free(changes
, n_changes
);
5717 static int unit_is_enabled(int argc
, char *argv
[], void *userdata
) {
5719 _cleanup_strv_free_
char **names
= NULL
;
5724 r
= mangle_names(strv_skip(argv
, 1), &names
);
5728 r
= enable_sysv_units(argv
[0], names
);
5734 if (install_client_side()) {
5736 STRV_FOREACH(name
, names
) {
5737 UnitFileState state
;
5739 state
= unit_file_get_state(arg_scope
, arg_root
, *name
);
5741 return log_error_errno(state
, "Failed to get unit file state for %s: %m", *name
);
5745 UNIT_FILE_ENABLED_RUNTIME
,
5747 UNIT_FILE_INDIRECT
))
5751 puts(unit_file_state_to_string(state
));
5755 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
5758 r
= acquire_bus(BUS_MANAGER
, &bus
);
5762 STRV_FOREACH(name
, names
) {
5763 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
5766 r
= sd_bus_call_method(
5768 "org.freedesktop.systemd1",
5769 "/org/freedesktop/systemd1",
5770 "org.freedesktop.systemd1.Manager",
5776 return log_error_errno(r
, "Failed to get unit file state for %s: %s", *name
, bus_error_message(&error
, r
));
5778 r
= sd_bus_message_read(reply
, "s", &s
);
5780 return bus_log_parse_error(r
);
5782 if (STR_IN_SET(s
, "enabled", "enabled-runtime", "static", "indirect"))
5793 static int is_system_running(int argc
, char *argv
[], void *userdata
) {
5794 _cleanup_free_
char *state
= NULL
;
5798 if (arg_transport
== BUS_TRANSPORT_LOCAL
&& !sd_booted()) {
5801 return EXIT_FAILURE
;
5804 r
= acquire_bus(BUS_MANAGER
, &bus
);
5808 r
= sd_bus_get_property_string(
5810 "org.freedesktop.systemd1",
5811 "/org/freedesktop/systemd1",
5812 "org.freedesktop.systemd1.Manager",
5825 return streq(state
, "running") ? EXIT_SUCCESS
: EXIT_FAILURE
;
5828 static int create_edit_temp_file(const char *new_path
, const char *original_path
, char **ret_tmp_fn
) {
5829 _cleanup_free_
char *t
= NULL
;
5833 assert(original_path
);
5836 r
= tempfn_random(new_path
, NULL
, &t
);
5838 return log_error_errno(r
, "Failed to determine temporary filename for \"%s\": %m", new_path
);
5840 r
= mkdir_parents(new_path
, 0755);
5842 return log_error_errno(r
, "Failed to create directories for \"%s\": %m", new_path
);
5844 r
= copy_file(original_path
, t
, 0, 0644, 0);
5849 return log_error_errno(r
, "Failed to create temporary file \"%s\": %m", t
);
5852 return log_error_errno(r
, "Failed to copy \"%s\" to \"%s\": %m", original_path
, t
);
5860 static int get_file_to_edit(const char *name
, const char *user_home
, const char *user_runtime
, char **ret_path
) {
5861 _cleanup_free_
char *path
= NULL
, *path2
= NULL
, *run
= NULL
;
5866 switch (arg_scope
) {
5867 case UNIT_FILE_SYSTEM
:
5868 path
= path_join(arg_root
, SYSTEM_CONFIG_UNIT_PATH
, name
);
5870 run
= path_join(arg_root
, "/run/systemd/system/", name
);
5872 case UNIT_FILE_GLOBAL
:
5873 path
= path_join(arg_root
, USER_CONFIG_UNIT_PATH
, name
);
5875 run
= path_join(arg_root
, "/run/systemd/user/", name
);
5877 case UNIT_FILE_USER
:
5879 assert(user_runtime
);
5881 path
= path_join(arg_root
, user_home
, name
);
5883 path2
= path_join(arg_root
, USER_CONFIG_UNIT_PATH
, name
);
5886 run
= path_join(arg_root
, user_runtime
, name
);
5890 assert_not_reached("Invalid scope");
5892 if (!path
|| (arg_runtime
&& !run
))
5896 if (access(path
, F_OK
) >= 0) {
5897 log_error("Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", run
, path
);
5901 if (path2
&& access(path2
, F_OK
) >= 0) {
5902 log_error("Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", run
, path2
);
5916 static int unit_file_create_dropin(const char *unit_name
, const char *user_home
, const char *user_runtime
, char **ret_new_path
, char **ret_tmp_path
) {
5917 char *tmp_new_path
, *tmp_tmp_path
, *ending
;
5921 assert(ret_new_path
);
5922 assert(ret_tmp_path
);
5924 ending
= strjoina(unit_name
, ".d/override.conf");
5925 r
= get_file_to_edit(ending
, user_home
, user_runtime
, &tmp_new_path
);
5929 r
= create_edit_temp_file(tmp_new_path
, tmp_new_path
, &tmp_tmp_path
);
5935 *ret_new_path
= tmp_new_path
;
5936 *ret_tmp_path
= tmp_tmp_path
;
5941 static int unit_file_create_copy(
5942 const char *unit_name
,
5943 const char *fragment_path
,
5944 const char *user_home
,
5945 const char *user_runtime
,
5946 char **ret_new_path
,
5947 char **ret_tmp_path
) {
5949 char *tmp_new_path
, *tmp_tmp_path
;
5952 assert(fragment_path
);
5954 assert(ret_new_path
);
5955 assert(ret_tmp_path
);
5957 r
= get_file_to_edit(unit_name
, user_home
, user_runtime
, &tmp_new_path
);
5961 if (!path_equal(fragment_path
, tmp_new_path
) && access(tmp_new_path
, F_OK
) == 0) {
5964 r
= ask_char(&response
, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", tmp_new_path
, fragment_path
);
5969 if (response
!= 'y') {
5970 log_warning("%s ignored", unit_name
);
5976 r
= create_edit_temp_file(tmp_new_path
, fragment_path
, &tmp_tmp_path
);
5978 log_error_errno(r
, "Failed to create temporary file for \"%s\": %m", tmp_new_path
);
5983 *ret_new_path
= tmp_new_path
;
5984 *ret_tmp_path
= tmp_tmp_path
;
5989 static int run_editor(char **paths
) {
5997 return log_error_errno(errno
, "Failed to fork: %m");
6001 char *editor
, **editor_args
= NULL
;
6002 char **tmp_path
, **original_path
, *p
;
6003 unsigned n_editor_args
= 0, i
= 1;
6006 (void) reset_all_signal_handlers();
6007 (void) reset_signal_mask();
6009 argc
= strv_length(paths
)/2 + 1;
6011 /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL
6012 * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present,
6013 * we try to execute well known editors
6015 editor
= getenv("SYSTEMD_EDITOR");
6017 editor
= getenv("EDITOR");
6019 editor
= getenv("VISUAL");
6021 if (!isempty(editor
)) {
6022 editor_args
= strv_split(editor
, WHITESPACE
);
6025 _exit(EXIT_FAILURE
);
6027 n_editor_args
= strv_length(editor_args
);
6028 argc
+= n_editor_args
- 1;
6030 args
= newa(const char*, argc
+ 1);
6032 if (n_editor_args
> 0) {
6033 args
[0] = editor_args
[0];
6034 for (; i
< n_editor_args
; i
++)
6035 args
[i
] = editor_args
[i
];
6038 STRV_FOREACH_PAIR(original_path
, tmp_path
, paths
) {
6039 args
[i
] = *tmp_path
;
6044 if (n_editor_args
> 0)
6045 execvp(args
[0], (char* const*) args
);
6047 FOREACH_STRING(p
, "editor", "nano", "vim", "vi") {
6049 execvp(p
, (char* const*) args
);
6050 /* We do not fail if the editor doesn't exist
6051 * because we want to try each one of them before
6054 if (errno
!= ENOENT
) {
6055 log_error_errno(errno
, "Failed to execute %s: %m", editor
);
6056 _exit(EXIT_FAILURE
);
6060 log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL.");
6061 _exit(EXIT_FAILURE
);
6064 r
= wait_for_terminate_and_warn("editor", pid
, true);
6066 return log_error_errno(r
, "Failed to wait for child: %m");
6071 static int find_paths_to_edit(sd_bus
*bus
, char **names
, char ***paths
) {
6072 _cleanup_free_
char *user_home
= NULL
;
6073 _cleanup_free_
char *user_runtime
= NULL
;
6074 _cleanup_lookup_paths_free_ LookupPaths lp
= {};
6081 r
= init_home_and_lookup_paths(&user_home
, &user_runtime
, &lp
);
6085 STRV_FOREACH(name
, names
) {
6086 _cleanup_free_
char *path
= NULL
;
6087 char *new_path
, *tmp_path
;
6089 r
= unit_find_paths(bus
, *name
, &lp
, &path
, NULL
);
6095 // FIXME: support units with path==NULL (no FragmentPath)
6096 log_error("No fragment exists for %s.", *name
);
6101 r
= unit_file_create_copy(*name
, path
, user_home
, user_runtime
, &new_path
, &tmp_path
);
6103 r
= unit_file_create_dropin(*name
, user_home
, user_runtime
, &new_path
, &tmp_path
);
6107 r
= strv_push_pair(paths
, new_path
, tmp_path
);
6115 static int edit(int argc
, char *argv
[], void *userdata
) {
6116 _cleanup_strv_free_
char **names
= NULL
;
6117 _cleanup_strv_free_
char **paths
= NULL
;
6118 char **original
, **tmp
;
6123 log_error("Cannot edit units if not on a tty.");
6127 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
6128 log_error("Cannot edit units remotely.");
6132 r
= acquire_bus(BUS_MANAGER
, &bus
);
6136 r
= expand_names(bus
, strv_skip(argv
, 1), NULL
, &names
);
6138 return log_error_errno(r
, "Failed to expand names: %m");
6140 r
= find_paths_to_edit(bus
, names
, &paths
);
6144 if (strv_isempty(paths
))
6147 r
= run_editor(paths
);
6151 STRV_FOREACH_PAIR(original
, tmp
, paths
) {
6152 /* If the temporary file is empty we ignore it. It's
6153 * useful if the user wants to cancel its modification
6155 if (null_or_empty_path(*tmp
)) {
6156 log_warning("Editing \"%s\" canceled: temporary file is empty.", *original
);
6160 r
= rename(*tmp
, *original
);
6162 r
= log_error_errno(errno
, "Failed to rename \"%s\" to \"%s\": %m", *tmp
, *original
);
6169 if (!arg_no_reload
&& !install_client_side())
6170 r
= daemon_reload(argc
, argv
, userdata
);
6173 STRV_FOREACH_PAIR(original
, tmp
, paths
)
6174 (void) unlink(*tmp
);
6179 static void systemctl_help(void) {
6181 pager_open_if_enabled();
6183 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
6184 "Query or send control commands to the systemd manager.\n\n"
6185 " -h --help Show this help\n"
6186 " --version Show package version\n"
6187 " --system Connect to system manager\n"
6188 " --user Connect to user service manager\n"
6189 " -H --host=[USER@]HOST\n"
6190 " Operate on remote host\n"
6191 " -M --machine=CONTAINER\n"
6192 " Operate on local container\n"
6193 " -t --type=TYPE List units of a particular type\n"
6194 " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n"
6195 " -p --property=NAME Show only properties by this name\n"
6196 " -a --all Show all loaded units/properties, including dead/empty\n"
6197 " ones. To list all units installed on the system, use\n"
6198 " the 'list-unit-files' command instead.\n"
6199 " -l --full Don't ellipsize unit names on output\n"
6200 " -r --recursive Show unit list of host and local containers\n"
6201 " --reverse Show reverse dependencies with 'list-dependencies'\n"
6202 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
6203 " queueing a new job\n"
6204 " --show-types When showing sockets, explicitly show their type\n"
6205 " -i --ignore-inhibitors\n"
6206 " When shutting down or sleeping, ignore inhibitors\n"
6207 " --kill-who=WHO Who to send signal to\n"
6208 " -s --signal=SIGNAL Which signal to send\n"
6209 " --now Start or stop unit in addition to enabling or disabling it\n"
6210 " -q --quiet Suppress output\n"
6211 " --no-block Do not wait until operation finished\n"
6212 " --no-wall Don't send wall message before halt/power-off/reboot\n"
6213 " --no-reload Don't reload daemon after en-/dis-abling unit files\n"
6214 " --no-legend Do not print a legend (column headers and hints)\n"
6215 " --no-pager Do not pipe output into a pager\n"
6216 " --no-ask-password\n"
6217 " Do not ask for system passwords\n"
6218 " --global Enable/disable unit files globally\n"
6219 " --runtime Enable unit files only temporarily until next reboot\n"
6220 " -f --force When enabling unit files, override existing symlinks\n"
6221 " When shutting down, execute action immediately\n"
6222 " --preset-mode= Apply only enable, only disable, or all presets\n"
6223 " --root=PATH Enable unit files in the specified root directory\n"
6224 " -n --lines=INTEGER Number of journal entries to show\n"
6225 " -o --output=STRING Change journal output mode (short, short-iso,\n"
6226 " short-precise, short-monotonic, verbose,\n"
6227 " export, json, json-pretty, json-sse, cat)\n"
6228 " --firmware-setup Tell the firmware to show the setup menu on next boot\n"
6229 " --plain Print unit dependencies as a list instead of a tree\n\n"
6231 " list-units [PATTERN...] List loaded units\n"
6232 " list-sockets [PATTERN...] List loaded sockets ordered by address\n"
6233 " list-timers [PATTERN...] List loaded timers ordered by next elapse\n"
6234 " start NAME... Start (activate) one or more units\n"
6235 " stop NAME... Stop (deactivate) one or more units\n"
6236 " reload NAME... Reload one or more units\n"
6237 " restart NAME... Start or restart one or more units\n"
6238 " try-restart NAME... Restart one or more units if active\n"
6239 " reload-or-restart NAME... Reload one or more units if possible,\n"
6240 " otherwise start or restart\n"
6241 " reload-or-try-restart NAME... Reload one or more units if possible,\n"
6242 " otherwise restart if active\n"
6243 " isolate NAME Start one unit and stop all others\n"
6244 " kill NAME... Send signal to processes of a unit\n"
6245 " is-active PATTERN... Check whether units are active\n"
6246 " is-failed PATTERN... Check whether units are failed\n"
6247 " status [PATTERN...|PID...] Show runtime status of one or more units\n"
6248 " show [PATTERN...|JOB...] Show properties of one or more\n"
6249 " units/jobs or the manager\n"
6250 " cat PATTERN... Show files and drop-ins of one or more units\n"
6251 " set-property NAME ASSIGNMENT... Sets one or more properties of a unit\n"
6252 " help PATTERN...|PID... Show manual for one or more units\n"
6253 " reset-failed [PATTERN...] Reset failed state for all, one, or more\n"
6255 " list-dependencies [NAME] Recursively show units which are required\n"
6256 " or wanted by this unit or by which this\n"
6257 " unit is required or wanted\n\n"
6258 "Unit File Commands:\n"
6259 " list-unit-files [PATTERN...] List installed unit files\n"
6260 " enable NAME... Enable one or more unit files\n"
6261 " disable NAME... Disable one or more unit files\n"
6262 " reenable NAME... Reenable one or more unit files\n"
6263 " preset NAME... Enable/disable one or more unit files\n"
6264 " based on preset configuration\n"
6265 " preset-all Enable/disable all unit files based on\n"
6266 " preset configuration\n"
6267 " is-enabled NAME... Check whether unit files are enabled\n"
6268 " mask NAME... Mask one or more units\n"
6269 " unmask NAME... Unmask one or more units\n"
6270 " link PATH... Link one or more units files into\n"
6271 " the search path\n"
6272 " add-wants TARGET NAME... Add 'Wants' dependency for the target\n"
6273 " on specified one or more units\n"
6274 " add-requires TARGET NAME... Add 'Requires' dependency for the target\n"
6275 " on specified one or more units\n"
6276 " edit NAME... Edit one or more unit files\n"
6277 " get-default Get the name of the default target\n"
6278 " set-default NAME Set the default target\n\n"
6279 "Machine Commands:\n"
6280 " list-machines [PATTERN...] List local containers and host\n\n"
6282 " list-jobs [PATTERN...] List jobs\n"
6283 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
6284 "Environment Commands:\n"
6285 " show-environment Dump environment\n"
6286 " set-environment NAME=VALUE... Set one or more environment variables\n"
6287 " unset-environment NAME... Unset one or more environment variables\n"
6288 " import-environment [NAME...] Import all or some environment variables\n\n"
6289 "Manager Lifecycle Commands:\n"
6290 " daemon-reload Reload systemd manager configuration\n"
6291 " daemon-reexec Reexecute systemd manager\n\n"
6292 "System Commands:\n"
6293 " is-system-running Check whether system is fully running\n"
6294 " default Enter system default mode\n"
6295 " rescue Enter system rescue mode\n"
6296 " emergency Enter system emergency mode\n"
6297 " halt Shut down and halt the system\n"
6298 " poweroff Shut down and power-off the system\n"
6299 " reboot [ARG] Shut down and reboot the system\n"
6300 " kexec Shut down and reboot the system with kexec\n"
6301 " exit [EXIT_CODE] Request user instance or container exit\n"
6302 " switch-root ROOT [INIT] Change to a different root file system\n"
6303 " suspend Suspend the system\n"
6304 " hibernate Hibernate the system\n"
6305 " hybrid-sleep Hibernate and suspend the system\n",
6306 program_invocation_short_name
);
6309 static void halt_help(void) {
6310 printf("%s [OPTIONS...]%s\n\n"
6311 "%s the system.\n\n"
6312 " --help Show this help\n"
6313 " --halt Halt the machine\n"
6314 " -p --poweroff Switch off the machine\n"
6315 " --reboot Reboot the machine\n"
6316 " -f --force Force immediate halt/power-off/reboot\n"
6317 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
6318 " -d --no-wtmp Don't write wtmp record\n"
6319 " --no-wall Don't send wall message before halt/power-off/reboot\n",
6320 program_invocation_short_name
,
6321 arg_action
== ACTION_REBOOT
? " [ARG]" : "",
6322 arg_action
== ACTION_REBOOT
? "Reboot" :
6323 arg_action
== ACTION_POWEROFF
? "Power off" :
6327 static void shutdown_help(void) {
6328 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
6329 "Shut down the system.\n\n"
6330 " --help Show this help\n"
6331 " -H --halt Halt the machine\n"
6332 " -P --poweroff Power-off the machine\n"
6333 " -r --reboot Reboot the machine\n"
6334 " -h Equivalent to --poweroff, overridden by --halt\n"
6335 " -k Don't halt/power-off/reboot, just send warnings\n"
6336 " --no-wall Don't send wall message before halt/power-off/reboot\n"
6337 " -c Cancel a pending shutdown\n",
6338 program_invocation_short_name
);
6341 static void telinit_help(void) {
6342 printf("%s [OPTIONS...] {COMMAND}\n\n"
6343 "Send control commands to the init daemon.\n\n"
6344 " --help Show this help\n"
6345 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
6347 " 0 Power-off the machine\n"
6348 " 6 Reboot the machine\n"
6349 " 2, 3, 4, 5 Start runlevelX.target unit\n"
6350 " 1, s, S Enter rescue mode\n"
6351 " q, Q Reload init daemon configuration\n"
6352 " u, U Reexecute init daemon\n",
6353 program_invocation_short_name
);
6356 static void runlevel_help(void) {
6357 printf("%s [OPTIONS...]\n\n"
6358 "Prints the previous and current runlevel of the init system.\n\n"
6359 " --help Show this help\n",
6360 program_invocation_short_name
);
6363 static void help_types(void) {
6367 puts("Available unit types:");
6368 for (i
= 0; i
< _UNIT_TYPE_MAX
; i
++)
6369 puts(unit_type_to_string(i
));
6372 static void help_states(void) {
6376 puts("Available unit load states:");
6377 for (i
= 0; i
< _UNIT_LOAD_STATE_MAX
; i
++)
6378 puts(unit_load_state_to_string(i
));
6381 puts("\nAvailable unit active states:");
6382 for (i
= 0; i
< _UNIT_ACTIVE_STATE_MAX
; i
++)
6383 puts(unit_active_state_to_string(i
));
6386 puts("\nAvailable automount unit substates:");
6387 for (i
= 0; i
< _AUTOMOUNT_STATE_MAX
; i
++)
6388 puts(automount_state_to_string(i
));
6391 puts("\nAvailable busname unit substates:");
6392 for (i
= 0; i
< _BUSNAME_STATE_MAX
; i
++)
6393 puts(busname_state_to_string(i
));
6396 puts("\nAvailable device unit substates:");
6397 for (i
= 0; i
< _DEVICE_STATE_MAX
; i
++)
6398 puts(device_state_to_string(i
));
6401 puts("\nAvailable mount unit substates:");
6402 for (i
= 0; i
< _MOUNT_STATE_MAX
; i
++)
6403 puts(mount_state_to_string(i
));
6406 puts("\nAvailable path unit substates:");
6407 for (i
= 0; i
< _PATH_STATE_MAX
; i
++)
6408 puts(path_state_to_string(i
));
6411 puts("\nAvailable scope unit substates:");
6412 for (i
= 0; i
< _SCOPE_STATE_MAX
; i
++)
6413 puts(scope_state_to_string(i
));
6416 puts("\nAvailable service unit substates:");
6417 for (i
= 0; i
< _SERVICE_STATE_MAX
; i
++)
6418 puts(service_state_to_string(i
));
6421 puts("\nAvailable slice unit substates:");
6422 for (i
= 0; i
< _SLICE_STATE_MAX
; i
++)
6423 puts(slice_state_to_string(i
));
6426 puts("\nAvailable socket unit substates:");
6427 for (i
= 0; i
< _SOCKET_STATE_MAX
; i
++)
6428 puts(socket_state_to_string(i
));
6431 puts("\nAvailable swap unit substates:");
6432 for (i
= 0; i
< _SWAP_STATE_MAX
; i
++)
6433 puts(swap_state_to_string(i
));
6436 puts("\nAvailable target unit substates:");
6437 for (i
= 0; i
< _TARGET_STATE_MAX
; i
++)
6438 puts(target_state_to_string(i
));
6441 puts("\nAvailable timer unit substates:");
6442 for (i
= 0; i
< _TIMER_STATE_MAX
; i
++)
6443 puts(timer_state_to_string(i
));
6446 static int systemctl_parse_argv(int argc
, char *argv
[]) {
6455 ARG_IGNORE_DEPENDENCIES
,
6467 ARG_NO_ASK_PASSWORD
,
6480 static const struct option options
[] = {
6481 { "help", no_argument
, NULL
, 'h' },
6482 { "version", no_argument
, NULL
, ARG_VERSION
},
6483 { "type", required_argument
, NULL
, 't' },
6484 { "property", required_argument
, NULL
, 'p' },
6485 { "all", no_argument
, NULL
, 'a' },
6486 { "reverse", no_argument
, NULL
, ARG_REVERSE
},
6487 { "after", no_argument
, NULL
, ARG_AFTER
},
6488 { "before", no_argument
, NULL
, ARG_BEFORE
},
6489 { "show-types", no_argument
, NULL
, ARG_SHOW_TYPES
},
6490 { "failed", no_argument
, NULL
, ARG_FAILED
}, /* compatibility only */
6491 { "full", no_argument
, NULL
, 'l' },
6492 { "job-mode", required_argument
, NULL
, ARG_JOB_MODE
},
6493 { "fail", no_argument
, NULL
, ARG_FAIL
}, /* compatibility only */
6494 { "irreversible", no_argument
, NULL
, ARG_IRREVERSIBLE
}, /* compatibility only */
6495 { "ignore-dependencies", no_argument
, NULL
, ARG_IGNORE_DEPENDENCIES
}, /* compatibility only */
6496 { "ignore-inhibitors", no_argument
, NULL
, 'i' },
6497 { "user", no_argument
, NULL
, ARG_USER
},
6498 { "system", no_argument
, NULL
, ARG_SYSTEM
},
6499 { "global", no_argument
, NULL
, ARG_GLOBAL
},
6500 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
6501 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
6502 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
6503 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
6504 { "quiet", no_argument
, NULL
, 'q' },
6505 { "root", required_argument
, NULL
, ARG_ROOT
},
6506 { "force", no_argument
, NULL
, ARG_FORCE
},
6507 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
6508 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
6509 { "signal", required_argument
, NULL
, 's' },
6510 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
6511 { "host", required_argument
, NULL
, 'H' },
6512 { "machine", required_argument
, NULL
, 'M' },
6513 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
6514 { "lines", required_argument
, NULL
, 'n' },
6515 { "output", required_argument
, NULL
, 'o' },
6516 { "plain", no_argument
, NULL
, ARG_PLAIN
},
6517 { "state", required_argument
, NULL
, ARG_STATE
},
6518 { "recursive", no_argument
, NULL
, 'r' },
6519 { "preset-mode", required_argument
, NULL
, ARG_PRESET_MODE
},
6520 { "firmware-setup", no_argument
, NULL
, ARG_FIRMWARE_SETUP
},
6521 { "now", no_argument
, NULL
, ARG_NOW
},
6522 { "message", required_argument
, NULL
, ARG_MESSAGE
},
6532 /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */
6533 arg_ask_password
= true;
6535 while ((c
= getopt_long(argc
, argv
, "ht:p:alqfs:H:M:n:o:ir", options
, NULL
)) >= 0)
6547 if (isempty(optarg
))
6548 return log_error_errno(r
, "--type requires arguments.");
6552 _cleanup_free_
char *type
= NULL
;
6554 r
= extract_first_word(&p
, &type
, ",", 0);
6556 return log_error_errno(r
, "Failed to parse type: %s", optarg
);
6561 if (streq(type
, "help")) {
6566 if (unit_type_from_string(type
) >= 0) {
6567 if (strv_push(&arg_types
, type
) < 0)
6573 /* It's much nicer to use --state= for
6574 * load states, but let's support this
6575 * in --types= too for compatibility
6576 * with old versions */
6577 if (unit_load_state_from_string(type
) >= 0) {
6578 if (strv_push(&arg_states
, type
) < 0)
6584 log_error("Unknown unit type or load state '%s'.", type
);
6585 log_info("Use -t help to see a list of allowed values.");
6593 /* Make sure that if the empty property list
6594 was specified, we won't show any properties. */
6595 if (isempty(optarg
) && !arg_properties
) {
6596 arg_properties
= new0(char*, 1);
6597 if (!arg_properties
)
6602 _cleanup_free_
char *prop
= NULL
;
6604 r
= extract_first_word(&p
, &prop
, ",", 0);
6606 return log_error_errno(r
, "Failed to parse property: %s", optarg
);
6611 if (strv_push(&arg_properties
, prop
) < 0)
6618 /* If the user asked for a particular
6619 * property, show it to him, even if it is
6631 arg_dependency
= DEPENDENCY_REVERSE
;
6635 arg_dependency
= DEPENDENCY_AFTER
;
6639 arg_dependency
= DEPENDENCY_BEFORE
;
6642 case ARG_SHOW_TYPES
:
6643 arg_show_types
= true;
6647 arg_job_mode
= optarg
;
6651 arg_job_mode
= "fail";
6654 case ARG_IRREVERSIBLE
:
6655 arg_job_mode
= "replace-irreversibly";
6658 case ARG_IGNORE_DEPENDENCIES
:
6659 arg_job_mode
= "ignore-dependencies";
6663 arg_scope
= UNIT_FILE_USER
;
6667 arg_scope
= UNIT_FILE_SYSTEM
;
6671 arg_scope
= UNIT_FILE_GLOBAL
;
6675 arg_no_block
= true;
6679 arg_no_legend
= true;
6683 arg_no_pager
= true;
6691 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
6701 if (strv_extend(&arg_states
, "failed") < 0)
6719 arg_no_reload
= true;
6723 arg_kill_who
= optarg
;
6727 arg_signal
= signal_from_string_try_harder(optarg
);
6728 if (arg_signal
< 0) {
6729 log_error("Failed to parse signal string %s.", optarg
);
6734 case ARG_NO_ASK_PASSWORD
:
6735 arg_ask_password
= false;
6739 arg_transport
= BUS_TRANSPORT_REMOTE
;
6744 arg_transport
= BUS_TRANSPORT_MACHINE
;
6753 if (safe_atou(optarg
, &arg_lines
) < 0) {
6754 log_error("Failed to parse lines '%s'", optarg
);
6760 arg_output
= output_mode_from_string(optarg
);
6761 if (arg_output
< 0) {
6762 log_error("Unknown output '%s'.", optarg
);
6768 arg_ignore_inhibitors
= true;
6775 case ARG_FIRMWARE_SETUP
:
6776 arg_firmware_setup
= true;
6780 if (isempty(optarg
))
6781 return log_error_errno(r
, "--signal requires arguments.");
6785 _cleanup_free_
char *s
= NULL
;
6787 r
= extract_first_word(&p
, &s
, ",", 0);
6789 return log_error_errno(r
, "Failed to parse signal: %s", optarg
);
6794 if (streq(s
, "help")) {
6799 if (strv_push(&arg_states
, s
) < 0)
6808 if (geteuid() != 0) {
6809 log_error("--recursive requires root privileges.");
6813 arg_recursive
= true;
6816 case ARG_PRESET_MODE
:
6818 arg_preset_mode
= unit_file_preset_mode_from_string(optarg
);
6819 if (arg_preset_mode
< 0) {
6820 log_error("Failed to parse preset mode: %s.", optarg
);
6831 if (strv_extend(&arg_wall
, optarg
) < 0)
6839 assert_not_reached("Unhandled option");
6842 if (arg_transport
!= BUS_TRANSPORT_LOCAL
&& arg_scope
!= UNIT_FILE_SYSTEM
) {
6843 log_error("Cannot access user instance remotely.");
6850 static int halt_parse_argv(int argc
, char *argv
[]) {
6859 static const struct option options
[] = {
6860 { "help", no_argument
, NULL
, ARG_HELP
},
6861 { "halt", no_argument
, NULL
, ARG_HALT
},
6862 { "poweroff", no_argument
, NULL
, 'p' },
6863 { "reboot", no_argument
, NULL
, ARG_REBOOT
},
6864 { "force", no_argument
, NULL
, 'f' },
6865 { "wtmp-only", no_argument
, NULL
, 'w' },
6866 { "no-wtmp", no_argument
, NULL
, 'd' },
6867 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
6876 if (utmp_get_runlevel(&runlevel
, NULL
) >= 0)
6877 if (runlevel
== '0' || runlevel
== '6')
6880 while ((c
= getopt_long(argc
, argv
, "pfwdnih", options
, NULL
)) >= 0)
6888 arg_action
= ACTION_HALT
;
6892 if (arg_action
!= ACTION_REBOOT
)
6893 arg_action
= ACTION_POWEROFF
;
6897 arg_action
= ACTION_REBOOT
;
6919 /* Compatibility nops */
6926 assert_not_reached("Unhandled option");
6929 if (arg_action
== ACTION_REBOOT
&& (argc
== optind
|| argc
== optind
+ 1)) {
6930 r
= update_reboot_param_file(argc
== optind
+ 1 ? argv
[optind
] : NULL
);
6933 } else if (optind
< argc
) {
6934 log_error("Too many arguments.");
6941 static int parse_shutdown_time_spec(const char *t
, usec_t
*_u
) {
6945 if (streq(t
, "now"))
6947 else if (!strchr(t
, ':')) {
6950 if (safe_atou64(t
, &u
) < 0)
6953 *_u
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
* u
;
6962 hour
= strtol(t
, &e
, 10);
6963 if (errno
> 0 || *e
!= ':' || hour
< 0 || hour
> 23)
6966 minute
= strtol(e
+1, &e
, 10);
6967 if (errno
> 0 || *e
!= 0 || minute
< 0 || minute
> 59)
6970 n
= now(CLOCK_REALTIME
);
6971 s
= (time_t) (n
/ USEC_PER_SEC
);
6973 assert_se(localtime_r(&s
, &tm
));
6975 tm
.tm_hour
= (int) hour
;
6976 tm
.tm_min
= (int) minute
;
6979 assert_se(s
= mktime(&tm
));
6981 *_u
= (usec_t
) s
* USEC_PER_SEC
;
6984 *_u
+= USEC_PER_DAY
;
6990 static int shutdown_parse_argv(int argc
, char *argv
[]) {
6997 static const struct option options
[] = {
6998 { "help", no_argument
, NULL
, ARG_HELP
},
6999 { "halt", no_argument
, NULL
, 'H' },
7000 { "poweroff", no_argument
, NULL
, 'P' },
7001 { "reboot", no_argument
, NULL
, 'r' },
7002 { "kexec", no_argument
, NULL
, 'K' }, /* not documented extension */
7003 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
7013 while ((c
= getopt_long(argc
, argv
, "HPrhkKtafFc", options
, NULL
)) >= 0)
7021 arg_action
= ACTION_HALT
;
7025 arg_action
= ACTION_POWEROFF
;
7030 arg_action
= ACTION_KEXEC
;
7032 arg_action
= ACTION_REBOOT
;
7036 arg_action
= ACTION_KEXEC
;
7040 if (arg_action
!= ACTION_HALT
)
7041 arg_action
= ACTION_POWEROFF
;
7056 /* Compatibility nops */
7060 arg_action
= ACTION_CANCEL_SHUTDOWN
;
7067 assert_not_reached("Unhandled option");
7070 if (argc
> optind
&& arg_action
!= ACTION_CANCEL_SHUTDOWN
) {
7071 r
= parse_shutdown_time_spec(argv
[optind
], &arg_when
);
7073 log_error("Failed to parse time specification: %s", argv
[optind
]);
7077 arg_when
= now(CLOCK_REALTIME
) + USEC_PER_MINUTE
;
7079 if (argc
> optind
&& arg_action
== ACTION_CANCEL_SHUTDOWN
)
7080 /* No time argument for shutdown cancel */
7081 wall
= argv
+ optind
;
7082 else if (argc
> optind
+ 1)
7083 /* We skip the time argument */
7084 wall
= argv
+ optind
+ 1;
7087 arg_wall
= strv_copy(wall
);
7097 static int telinit_parse_argv(int argc
, char *argv
[]) {
7104 static const struct option options
[] = {
7105 { "help", no_argument
, NULL
, ARG_HELP
},
7106 { "no-wall", no_argument
, NULL
, ARG_NO_WALL
},
7110 static const struct {
7114 { '0', ACTION_POWEROFF
},
7115 { '6', ACTION_REBOOT
},
7116 { '1', ACTION_RESCUE
},
7117 { '2', ACTION_RUNLEVEL2
},
7118 { '3', ACTION_RUNLEVEL3
},
7119 { '4', ACTION_RUNLEVEL4
},
7120 { '5', ACTION_RUNLEVEL5
},
7121 { 's', ACTION_RESCUE
},
7122 { 'S', ACTION_RESCUE
},
7123 { 'q', ACTION_RELOAD
},
7124 { 'Q', ACTION_RELOAD
},
7125 { 'u', ACTION_REEXEC
},
7126 { 'U', ACTION_REEXEC
}
7135 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0)
7150 assert_not_reached("Unhandled option");
7153 if (optind
>= argc
) {
7154 log_error("%s: required argument missing.", program_invocation_short_name
);
7158 if (optind
+ 1 < argc
) {
7159 log_error("Too many arguments.");
7163 if (strlen(argv
[optind
]) != 1) {
7164 log_error("Expected single character argument.");
7168 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
7169 if (table
[i
].from
== argv
[optind
][0])
7172 if (i
>= ELEMENTSOF(table
)) {
7173 log_error("Unknown command '%s'.", argv
[optind
]);
7177 arg_action
= table
[i
].to
;
7184 static int runlevel_parse_argv(int argc
, char *argv
[]) {
7190 static const struct option options
[] = {
7191 { "help", no_argument
, NULL
, ARG_HELP
},
7200 while ((c
= getopt_long(argc
, argv
, "", options
, NULL
)) >= 0)
7211 assert_not_reached("Unhandled option");
7214 if (optind
< argc
) {
7215 log_error("Too many arguments.");
7222 static int parse_argv(int argc
, char *argv
[]) {
7226 if (program_invocation_short_name
) {
7228 if (strstr(program_invocation_short_name
, "halt")) {
7229 arg_action
= ACTION_HALT
;
7230 return halt_parse_argv(argc
, argv
);
7231 } else if (strstr(program_invocation_short_name
, "poweroff")) {
7232 arg_action
= ACTION_POWEROFF
;
7233 return halt_parse_argv(argc
, argv
);
7234 } else if (strstr(program_invocation_short_name
, "reboot")) {
7236 arg_action
= ACTION_KEXEC
;
7238 arg_action
= ACTION_REBOOT
;
7239 return halt_parse_argv(argc
, argv
);
7240 } else if (strstr(program_invocation_short_name
, "shutdown")) {
7241 arg_action
= ACTION_POWEROFF
;
7242 return shutdown_parse_argv(argc
, argv
);
7243 } else if (strstr(program_invocation_short_name
, "init")) {
7245 if (sd_booted() > 0) {
7246 arg_action
= _ACTION_INVALID
;
7247 return telinit_parse_argv(argc
, argv
);
7249 /* Hmm, so some other init system is
7250 * running, we need to forward this
7251 * request to it. For now we simply
7252 * guess that it is Upstart. */
7254 execv(TELINIT
, argv
);
7256 log_error("Couldn't find an alternative telinit implementation to spawn.");
7260 } else if (strstr(program_invocation_short_name
, "runlevel")) {
7261 arg_action
= ACTION_RUNLEVEL
;
7262 return runlevel_parse_argv(argc
, argv
);
7266 arg_action
= ACTION_SYSTEMCTL
;
7267 return systemctl_parse_argv(argc
, argv
);
7270 _pure_
static int action_to_runlevel(void) {
7272 static const char table
[_ACTION_MAX
] = {
7273 [ACTION_HALT
] = '0',
7274 [ACTION_POWEROFF
] = '0',
7275 [ACTION_REBOOT
] = '6',
7276 [ACTION_RUNLEVEL2
] = '2',
7277 [ACTION_RUNLEVEL3
] = '3',
7278 [ACTION_RUNLEVEL4
] = '4',
7279 [ACTION_RUNLEVEL5
] = '5',
7280 [ACTION_RESCUE
] = '1'
7283 assert(arg_action
< _ACTION_MAX
);
7285 return table
[arg_action
];
7288 static int talk_initctl(void) {
7289 #ifdef HAVE_SYSV_COMPAT
7290 struct init_request request
= {
7291 .magic
= INIT_MAGIC
,
7293 .cmd
= INIT_CMD_RUNLVL
7296 _cleanup_close_
int fd
= -1;
7300 rl
= action_to_runlevel();
7304 request
.runlevel
= rl
;
7306 fd
= open(INIT_FIFO
, O_WRONLY
|O_NDELAY
|O_CLOEXEC
|O_NOCTTY
);
7308 if (errno
== ENOENT
)
7311 return log_error_errno(errno
, "Failed to open "INIT_FIFO
": %m");
7314 r
= loop_write(fd
, &request
, sizeof(request
), false);
7316 return log_error_errno(r
, "Failed to write to "INIT_FIFO
": %m");
7324 static int systemctl_main(int argc
, char *argv
[]) {
7326 static const Verb verbs
[] = {
7327 { "list-units", VERB_ANY
, VERB_ANY
, VERB_DEFAULT
, list_units
},
7328 { "list-unit-files", VERB_ANY
, VERB_ANY
, 0, list_unit_files
},
7329 { "list-sockets", VERB_ANY
, VERB_ANY
, 0, list_sockets
},
7330 { "list-timers", VERB_ANY
, VERB_ANY
, 0, list_timers
},
7331 { "list-jobs", VERB_ANY
, VERB_ANY
, 0, list_jobs
},
7332 { "list-machines", VERB_ANY
, VERB_ANY
, 0, list_machines
},
7333 { "clear-jobs", VERB_ANY
, 1, 0, daemon_reload
},
7334 { "cancel", VERB_ANY
, VERB_ANY
, 0, cancel_job
},
7335 { "start", 2, VERB_ANY
, 0, start_unit
},
7336 { "stop", 2, VERB_ANY
, 0, start_unit
},
7337 { "condstop", 2, VERB_ANY
, 0, start_unit
}, /* For compatibility with ALTLinux */
7338 { "reload", 2, VERB_ANY
, 0, start_unit
},
7339 { "restart", 2, VERB_ANY
, 0, start_unit
},
7340 { "try-restart", 2, VERB_ANY
, 0, start_unit
},
7341 { "reload-or-restart", 2, VERB_ANY
, 0, start_unit
},
7342 { "reload-or-try-restart", 2, VERB_ANY
, 0, start_unit
},
7343 { "force-reload", 2, VERB_ANY
, 0, start_unit
}, /* For compatibility with SysV */
7344 { "condreload", 2, VERB_ANY
, 0, start_unit
}, /* For compatibility with ALTLinux */
7345 { "condrestart", 2, VERB_ANY
, 0, start_unit
}, /* For compatibility with RH */
7346 { "isolate", 2, 2, 0, start_unit
},
7347 { "kill", 2, VERB_ANY
, 0, kill_unit
},
7348 { "is-active", 2, VERB_ANY
, 0, check_unit_active
},
7349 { "check", 2, VERB_ANY
, 0, check_unit_active
},
7350 { "is-failed", 2, VERB_ANY
, 0, check_unit_failed
},
7351 { "show", VERB_ANY
, VERB_ANY
, 0, show
},
7352 { "cat", 2, VERB_ANY
, 0, cat
},
7353 { "status", VERB_ANY
, VERB_ANY
, 0, show
},
7354 { "help", VERB_ANY
, VERB_ANY
, 0, show
},
7355 { "daemon-reload", VERB_ANY
, 1, 0, daemon_reload
},
7356 { "daemon-reexec", VERB_ANY
, 1, 0, daemon_reload
},
7357 { "show-environment", VERB_ANY
, 1, 0, show_environment
},
7358 { "set-environment", 2, VERB_ANY
, 0, set_environment
},
7359 { "unset-environment", 2, VERB_ANY
, 0, set_environment
},
7360 { "import-environment", VERB_ANY
, VERB_ANY
, 0, import_environment
},
7361 { "halt", VERB_ANY
, 1, 0, start_special
},
7362 { "poweroff", VERB_ANY
, 1, 0, start_special
},
7363 { "reboot", VERB_ANY
, 2, 0, start_special
},
7364 { "kexec", VERB_ANY
, 1, 0, start_special
},
7365 { "suspend", VERB_ANY
, 1, 0, start_special
},
7366 { "hibernate", VERB_ANY
, 1, 0, start_special
},
7367 { "hybrid-sleep", VERB_ANY
, 1, 0, start_special
},
7368 { "default", VERB_ANY
, 1, 0, start_special
},
7369 { "rescue", VERB_ANY
, 1, 0, start_special
},
7370 { "emergency", VERB_ANY
, 1, 0, start_special
},
7371 { "exit", VERB_ANY
, 2, 0, start_special
},
7372 { "reset-failed", VERB_ANY
, VERB_ANY
, 0, reset_failed
},
7373 { "enable", 2, VERB_ANY
, 0, enable_unit
},
7374 { "disable", 2, VERB_ANY
, 0, enable_unit
},
7375 { "is-enabled", 2, VERB_ANY
, 0, unit_is_enabled
},
7376 { "reenable", 2, VERB_ANY
, 0, enable_unit
},
7377 { "preset", 2, VERB_ANY
, 0, enable_unit
},
7378 { "preset-all", VERB_ANY
, 1, 0, preset_all
},
7379 { "mask", 2, VERB_ANY
, 0, enable_unit
},
7380 { "unmask", 2, VERB_ANY
, 0, enable_unit
},
7381 { "link", 2, VERB_ANY
, 0, enable_unit
},
7382 { "switch-root", 2, VERB_ANY
, 0, switch_root
},
7383 { "list-dependencies", VERB_ANY
, 2, 0, list_dependencies
},
7384 { "set-default", 2, 2, 0, set_default
},
7385 { "get-default", VERB_ANY
, 1, 0, get_default
, },
7386 { "set-property", 3, VERB_ANY
, 0, set_property
},
7387 { "is-system-running", VERB_ANY
, 1, 0, is_system_running
},
7388 { "add-wants", 3, VERB_ANY
, 0, add_dependency
},
7389 { "add-requires", 3, VERB_ANY
, 0, add_dependency
},
7390 { "edit", 2, VERB_ANY
, 0, edit
},
7394 return dispatch_verb(argc
, argv
, verbs
, NULL
);
7397 static int reload_with_fallback(void) {
7399 /* First, try systemd via D-Bus. */
7400 if (daemon_reload(0, NULL
, NULL
) >= 0)
7403 /* Nothing else worked, so let's try signals */
7404 assert(arg_action
== ACTION_RELOAD
|| arg_action
== ACTION_REEXEC
);
7406 if (kill(1, arg_action
== ACTION_RELOAD
? SIGHUP
: SIGTERM
) < 0)
7407 return log_error_errno(errno
, "kill() failed: %m");
7412 static int start_with_fallback(void) {
7414 /* First, try systemd via D-Bus. */
7415 if (start_unit(0, NULL
, NULL
) >= 0)
7418 /* Nothing else worked, so let's try
7420 if (talk_initctl() > 0)
7423 log_error("Failed to talk to init daemon.");
7427 static int halt_now(enum action a
) {
7429 /* The kernel will automaticall flush ATA disks and suchlike
7430 * on reboot(), but the file systems need to be synce'd
7431 * explicitly in advance. */
7434 /* Make sure C-A-D is handled by the kernel from this point
7436 (void) reboot(RB_ENABLE_CAD
);
7441 log_info("Halting.");
7442 (void) reboot(RB_HALT_SYSTEM
);
7445 case ACTION_POWEROFF
:
7446 log_info("Powering off.");
7447 (void) reboot(RB_POWER_OFF
);
7451 case ACTION_REBOOT
: {
7452 _cleanup_free_
char *param
= NULL
;
7454 if (read_one_line_file(REBOOT_PARAM_FILE
, ¶m
) >= 0) {
7455 log_info("Rebooting with argument '%s'.", param
);
7456 (void) syscall(SYS_reboot
, LINUX_REBOOT_MAGIC1
, LINUX_REBOOT_MAGIC2
, LINUX_REBOOT_CMD_RESTART2
, param
);
7459 log_info("Rebooting.");
7460 (void) reboot(RB_AUTOBOOT
);
7465 assert_not_reached("Unknown action.");
7469 static int logind_schedule_shutdown(void) {
7472 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
7473 char date
[FORMAT_TIMESTAMP_MAX
];
7478 (void) logind_set_wall_message();
7480 r
= acquire_bus(BUS_FULL
, &bus
);
7484 switch (arg_action
) {
7488 case ACTION_POWEROFF
:
7489 action
= "poweroff";
7504 action
= strjoina("dry-", action
);
7506 r
= sd_bus_call_method(
7508 "org.freedesktop.login1",
7509 "/org/freedesktop/login1",
7510 "org.freedesktop.login1.Manager",
7518 return log_warning_errno(r
, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error
, r
));
7520 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date
, sizeof(date
), arg_when
));
7523 log_error("Cannot schedule shutdown without logind support, proceeding with immediate shutdown.");
7528 static int halt_main(void) {
7531 r
= logind_check_inhibitors(arg_action
);
7536 return logind_schedule_shutdown();
7538 if (geteuid() != 0) {
7539 if (arg_dry
|| arg_force
> 0) {
7540 log_error("Must be root.");
7544 /* Try logind if we are a normal user and no special
7545 * mode applies. Maybe PolicyKit allows us to shutdown
7547 if (IN_SET(arg_action
, ACTION_POWEROFF
, ACTION_REBOOT
)) {
7548 r
= logind_reboot(arg_action
);
7551 if (IN_SET(r
, -EOPNOTSUPP
, -EINPROGRESS
))
7552 /* requested operation is not
7553 * supported on the local system or
7554 * already in progress */
7556 /* on all other errors, try low-level operation */
7560 if (!arg_dry
&& !arg_force
)
7561 return start_with_fallback();
7563 assert(geteuid() == 0);
7566 if (sd_booted() > 0)
7567 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
7569 r
= utmp_put_shutdown();
7571 log_warning_errno(r
, "Failed to write utmp record: %m");
7578 r
= halt_now(arg_action
);
7579 return log_error_errno(r
, "Failed to reboot: %m");
7582 static int runlevel_main(void) {
7583 int r
, runlevel
, previous
;
7585 r
= utmp_get_runlevel(&runlevel
, &previous
);
7592 previous
<= 0 ? 'N' : previous
,
7593 runlevel
<= 0 ? 'N' : runlevel
);
7598 static int logind_cancel_shutdown(void) {
7600 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
7604 r
= acquire_bus(BUS_FULL
, &bus
);
7608 (void) logind_set_wall_message();
7610 r
= sd_bus_call_method(
7612 "org.freedesktop.login1",
7613 "/org/freedesktop/login1",
7614 "org.freedesktop.login1.Manager",
7615 "CancelScheduledShutdown",
7619 return log_warning_errno(r
, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error
, r
));
7623 log_error("Not compiled with logind support, cannot cancel scheduled shutdowns.");
7628 int main(int argc
, char*argv
[]) {
7631 setlocale(LC_ALL
, "");
7632 log_parse_environment();
7635 /* Explicitly not on_tty() to avoid setting cached value.
7636 * This becomes relevant for piping output which might be
7638 original_stdout_is_tty
= isatty(STDOUT_FILENO
);
7640 r
= parse_argv(argc
, argv
);
7644 if (running_in_chroot() > 0 && arg_action
!= ACTION_SYSTEMCTL
) {
7645 log_info("Running in chroot, ignoring request.");
7650 /* systemctl_main() will print an error message for the bus
7651 * connection, but only if it needs to */
7653 switch (arg_action
) {
7655 case ACTION_SYSTEMCTL
:
7656 r
= systemctl_main(argc
, argv
);
7660 case ACTION_POWEROFF
:
7666 case ACTION_RUNLEVEL2
:
7667 case ACTION_RUNLEVEL3
:
7668 case ACTION_RUNLEVEL4
:
7669 case ACTION_RUNLEVEL5
:
7671 case ACTION_EMERGENCY
:
7672 case ACTION_DEFAULT
:
7673 r
= start_with_fallback();
7678 r
= reload_with_fallback();
7681 case ACTION_CANCEL_SHUTDOWN
:
7682 r
= logind_cancel_shutdown();
7685 case ACTION_RUNLEVEL
:
7686 r
= runlevel_main();
7689 case _ACTION_INVALID
:
7691 assert_not_reached("Unknown action");
7696 ask_password_agent_close();
7697 polkit_agent_close();
7699 strv_free(arg_types
);
7700 strv_free(arg_states
);
7701 strv_free(arg_properties
);
7703 strv_free(arg_wall
);
7708 /* Note that we return r here, not EXIT_SUCCESS, so that we can implement the LSB-like return codes */
7710 return r
< 0 ? EXIT_FAILURE
: r
;