1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "bus-locator.h"
7 #include "format-table.h"
8 #include "locale-util.h"
10 #include "sort-util.h"
11 #include "systemctl-list-units.h"
12 #include "systemctl-util.h"
13 #include "systemctl.h"
14 #include "terminal-util.h"
16 static void message_set_freep(Set
**set
) {
17 set_free_with_destructor(*set
, sd_bus_message_unref
);
20 static int get_unit_list_recursive(
23 UnitInfo
**ret_unit_infos
,
25 char ***ret_machines
) {
27 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
28 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
29 sd_bus_message
*reply
;
34 assert(ret_unit_infos
);
37 replies
= set_new(NULL
);
41 c
= get_unit_list(bus
, NULL
, patterns
, &unit_infos
, 0, &reply
);
45 r
= set_put(replies
, reply
);
47 sd_bus_message_unref(reply
);
52 _cleanup_strv_free_
char **machines
= NULL
;
54 r
= sd_get_machine_names(&machines
);
56 return log_error_errno(r
, "Failed to get machine names: %m");
58 STRV_FOREACH(i
, machines
) {
59 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*container
= NULL
;
62 r
= sd_bus_open_system_machine(&container
, *i
);
64 log_warning_errno(r
, "Failed to connect to container %s, ignoring: %m", *i
);
68 k
= get_unit_list(container
, *i
, patterns
, &unit_infos
, c
, &reply
);
74 r
= set_put(replies
, reply
);
76 sd_bus_message_unref(reply
);
81 *ret_machines
= TAKE_PTR(machines
);
85 *ret_unit_infos
= TAKE_PTR(unit_infos
);
86 *ret_replies
= TAKE_PTR(replies
);
91 static void output_legend(const char *type
, size_t n_items
) {
96 on
= n_items
> 0 ? ansi_highlight() : ansi_highlight_red();
99 printf("\n%s%zu %ss listed.%s\n", on
, n_items
, type
, off
);
101 printf("Pass --all to see loaded but inactive %ss, too.\n", type
);
104 static int output_units_list(const UnitInfo
*unit_infos
, size_t c
) {
105 _cleanup_(table_unrefp
) Table
*table
= NULL
;
106 size_t job_count
= 0;
109 table
= table_new("", "unit", "load", "active", "sub", "job", "description");
113 table_set_header(table
, arg_legend
!= 0);
115 /* Hide the 'glyph' column when --plain is requested */
116 r
= table_hide_column_from_display(table
, 0);
118 return log_error_errno(r
, "Failed to hide column: %m");
121 table_set_width(table
, 0);
123 (void) table_set_empty_string(table
, "-");
125 for (const UnitInfo
*u
= unit_infos
; unit_infos
&& (size_t) (u
- unit_infos
) < c
; u
++) {
126 _cleanup_free_
char *j
= NULL
;
127 const char *on_underline
= "", *on_loaded
= "", *on_active
= "";
128 const char *on_circle
= "", *id
;
129 bool circle
= false, underline
= false;
131 if (u
+ 1 < unit_infos
+ c
&&
132 !streq(unit_type_suffix(u
->id
), unit_type_suffix((u
+ 1)->id
))) {
133 on_underline
= ansi_underline();
137 if (STR_IN_SET(u
->load_state
, "error", "not-found", "bad-setting", "masked") && !arg_plain
) {
138 on_circle
= underline
? ansi_highlight_yellow_underline() : ansi_highlight_yellow();
140 on_loaded
= underline
? ansi_highlight_red_underline() : ansi_highlight_red();
141 } else if (streq(u
->active_state
, "failed") && !arg_plain
) {
142 on_circle
= underline
? ansi_highlight_red_underline() : ansi_highlight_red();
144 on_active
= underline
? ansi_highlight_red_underline() : ansi_highlight_red();
146 on_circle
= on_underline
;
147 on_active
= on_underline
;
148 on_loaded
= on_underline
;
152 j
= strjoin(u
->machine
, ":", u
->id
);
160 r
= table_add_many(table
,
161 TABLE_STRING
, circle
? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
) : " ",
162 TABLE_SET_BOTH_COLORS
, on_circle
,
164 TABLE_SET_BOTH_COLORS
, on_active
,
165 TABLE_STRING
, u
->load_state
,
166 TABLE_SET_BOTH_COLORS
, on_loaded
,
167 TABLE_STRING
, u
->active_state
,
168 TABLE_SET_BOTH_COLORS
, on_active
,
169 TABLE_STRING
, u
->sub_state
,
170 TABLE_SET_BOTH_COLORS
, on_active
,
171 TABLE_STRING
, u
->job_id
? u
->job_type
: "",
172 TABLE_SET_BOTH_COLORS
, on_underline
,
173 TABLE_STRING
, u
->description
,
174 TABLE_SET_BOTH_COLORS
, on_underline
);
176 return table_log_add_error(r
);
182 if (job_count
== 0) {
183 /* There's no data in the JOB column, so let's hide it */
184 r
= table_hide_column_from_display(table
, 5);
186 return log_error_errno(r
, "Failed to hide column: %m");
189 r
= output_table(table
);
193 if (arg_legend
!= 0) {
194 const char *on
, *off
;
195 size_t records
= table_get_rows(table
) - 1;
199 "LOAD = Reflects whether the unit definition was properly loaded.\n"
200 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
201 "SUB = The low-level unit activation state, values depend on unit type.");
203 puts("JOB = Pending job for the unit.\n");
206 on
= records
> 0 ? ansi_highlight() : ansi_highlight_red();
209 if (arg_all
|| strv_contains(arg_states
, "inactive"))
210 printf("%s%zu loaded units listed.%s\n"
211 "To show all installed unit files use 'systemctl list-unit-files'.\n",
213 else if (!arg_states
)
214 printf("%s%zu loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
215 "To show all installed unit files use 'systemctl list-unit-files'.\n",
218 printf("%zu loaded units listed.\n", records
);
224 int verb_list_units(int argc
, char *argv
[], void *userdata
) {
225 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
226 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
227 _cleanup_strv_free_
char **machines
= NULL
;
231 r
= acquire_bus(BUS_MANAGER
, &bus
);
235 pager_open(arg_pager_flags
);
237 if (arg_with_dependencies
) {
238 _cleanup_strv_free_
char **names
= NULL
;
240 r
= append_unit_dependencies(bus
, strv_skip(argv
, 1), &names
);
244 r
= get_unit_list_recursive(bus
, names
, &unit_infos
, &replies
, &machines
);
248 r
= get_unit_list_recursive(bus
, strv_skip(argv
, 1), &unit_infos
, &replies
, &machines
);
253 typesafe_qsort(unit_infos
, r
, unit_info_compare
);
254 return output_units_list(unit_infos
, r
);
257 static int get_triggered_units(
262 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
269 r
= sd_bus_get_property_strv(
271 "org.freedesktop.systemd1",
273 "org.freedesktop.systemd1.Unit",
278 return log_error_errno(r
, "Failed to determine triggers: %s", bus_error_message(&error
, r
));
283 static int get_listening(
285 const char* unit_path
,
288 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
289 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
290 const char *type
, *path
;
293 r
= sd_bus_get_property(
295 "org.freedesktop.systemd1",
297 "org.freedesktop.systemd1.Socket",
303 return log_error_errno(r
, "Failed to get list of listening sockets: %s", bus_error_message(&error
, r
));
305 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ss)");
307 return bus_log_parse_error(r
);
309 while ((r
= sd_bus_message_read(reply
, "(ss)", &type
, &path
)) > 0) {
311 r
= strv_extend(listening
, type
);
315 r
= strv_extend(listening
, path
);
322 return bus_log_parse_error(r
);
324 r
= sd_bus_message_exit_container(reply
);
326 return bus_log_parse_error(r
);
338 /* Note: triggered is a list here, although it almost certainly will always be one
339 * unit. Nevertheless, dbus API allows for multiple values, so let's follow that. */
342 /* The strv above is shared. free is set only in the first one. */
346 static int socket_info_compare(const struct socket_info
*a
, const struct socket_info
*b
) {
352 r
= strcasecmp_ptr(a
->machine
, b
->machine
);
356 r
= strcmp(a
->path
, b
->path
);
360 return strcmp(a
->type
, b
->type
);
363 static int output_sockets_list(struct socket_info
*socket_infos
, size_t cs
) {
364 _cleanup_(table_unrefp
) Table
*table
= NULL
;
367 assert(socket_infos
|| cs
== 0);
369 table
= table_new("listen", "type", "unit", "activates");
373 if (!arg_show_types
) {
374 /* Hide the second (TYPE) column */
375 r
= table_set_display(table
, (size_t) 0, (size_t) 2, (size_t) 3);
377 return log_error_errno(r
, "Failed to set columns to display: %m");
380 table_set_header(table
, arg_legend
!= 0);
382 table_set_width(table
, 0);
384 (void) table_set_empty_string(table
, "-");
386 for (struct socket_info
*s
= socket_infos
; s
< socket_infos
+ cs
; s
++) {
387 _cleanup_free_
char *j
= NULL
;
391 j
= strjoin(s
->machine
, ":", s
->path
);
398 r
= table_add_many(table
,
400 TABLE_STRING
, s
->type
,
401 TABLE_STRING
, s
->id
);
403 return table_log_add_error(r
);
405 if (strv_isempty(s
->triggered
))
406 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
407 else if (strv_length(s
->triggered
) == 1)
408 r
= table_add_cell(table
, NULL
, TABLE_STRING
, s
->triggered
[0]);
410 /* This should never happen, currently our socket units can only trigger a
411 * single unit. But let's handle this anyway, who knows what the future
413 r
= table_add_cell(table
, NULL
, TABLE_STRV
, s
->triggered
);
415 return table_log_add_error(r
);
418 r
= output_table(table
);
423 output_legend("socket", cs
);
428 int verb_list_sockets(int argc
, char *argv
[], void *userdata
) {
429 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
430 _cleanup_strv_free_
char **machines
= NULL
;
431 _cleanup_strv_free_
char **sockets_with_suffix
= NULL
;
432 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
433 _cleanup_free_
struct socket_info
*socket_infos
= NULL
;
438 r
= acquire_bus(BUS_MANAGER
, &bus
);
442 pager_open(arg_pager_flags
);
444 r
= expand_unit_names(bus
, strv_skip(argv
, 1), ".socket", &sockets_with_suffix
, NULL
);
448 if (argc
== 1 || sockets_with_suffix
) {
449 n
= get_unit_list_recursive(bus
, sockets_with_suffix
, &unit_infos
, &replies
, &machines
);
453 for (const UnitInfo
*u
= unit_infos
; u
< unit_infos
+ n
; u
++) {
454 _cleanup_strv_free_
char **listening
= NULL
, **triggered
= NULL
;
457 if (!endswith(u
->id
, ".socket"))
460 r
= get_triggered_units(bus
, u
->unit_path
, &triggered
);
464 c
= get_listening(bus
, u
->unit_path
, &listening
);
470 if (!GREEDY_REALLOC(socket_infos
, cs
+ c
)) {
475 for (int i
= 0; i
< c
; i
++)
476 socket_infos
[cs
+ i
] = (struct socket_info
) {
477 .machine
= u
->machine
,
479 .type
= listening
[i
*2],
480 .path
= listening
[i
*2 + 1],
481 .triggered
= triggered
,
482 .own_triggered
= i
==0,
485 /* from this point on we will cleanup those socket_infos */
488 listening
= triggered
= NULL
; /* avoid cleanup */
491 typesafe_qsort(socket_infos
, cs
, socket_info_compare
);
494 output_sockets_list(socket_infos
, cs
);
497 assert(cs
== 0 || socket_infos
);
498 for (struct socket_info
*s
= socket_infos
; s
< socket_infos
+ cs
; s
++) {
501 if (s
->own_triggered
)
502 strv_free(s
->triggered
);
508 static int get_next_elapse(
511 dual_timestamp
*next
) {
513 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
521 r
= sd_bus_get_property_trivial(
523 "org.freedesktop.systemd1",
525 "org.freedesktop.systemd1.Timer",
526 "NextElapseUSecMonotonic",
531 return log_error_errno(r
, "Failed to get next elapse time: %s", bus_error_message(&error
, r
));
533 r
= sd_bus_get_property_trivial(
535 "org.freedesktop.systemd1",
537 "org.freedesktop.systemd1.Timer",
538 "NextElapseUSecRealtime",
543 return log_error_errno(r
, "Failed to get next elapse time: %s", bus_error_message(&error
, r
));
549 static int get_last_trigger(
554 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
561 r
= sd_bus_get_property_trivial(
563 "org.freedesktop.systemd1",
565 "org.freedesktop.systemd1.Timer",
571 return log_error_errno(r
, "Failed to get last trigger time: %s", bus_error_message(&error
, r
));
584 static int timer_info_compare(const struct timer_info
*a
, const struct timer_info
*b
) {
590 r
= strcasecmp_ptr(a
->machine
, b
->machine
);
594 r
= CMP(a
->next_elapse
, b
->next_elapse
);
598 return strcmp(a
->id
, b
->id
);
601 static int output_timers_list(struct timer_info
*timer_infos
, size_t n
) {
602 _cleanup_(table_unrefp
) Table
*table
= NULL
;
605 assert(timer_infos
|| n
== 0);
607 table
= table_new("next", "left", "last", "passed", "unit", "activates");
611 table_set_header(table
, arg_legend
!= 0);
613 table_set_width(table
, 0);
615 (void) table_set_empty_string(table
, "-");
617 for (struct timer_info
*t
= timer_infos
; t
< timer_infos
+ n
; t
++) {
618 _cleanup_free_
char *j
= NULL
, *activates
= NULL
;
622 j
= strjoin(t
->machine
, ":", t
->id
);
629 activates
= strv_join(t
->triggered
, ", ");
633 r
= table_add_many(table
,
634 TABLE_TIMESTAMP
, t
->next_elapse
,
635 TABLE_TIMESTAMP_RELATIVE
, t
->next_elapse
,
636 TABLE_TIMESTAMP
, t
->last_trigger
,
637 TABLE_TIMESTAMP_RELATIVE
, t
->last_trigger
,
639 TABLE_STRING
, activates
);
641 return table_log_add_error(r
);
644 r
= output_table(table
);
649 output_legend("timer", n
);
654 usec_t
calc_next_elapse(dual_timestamp
*nw
, dual_timestamp
*next
) {
660 if (timestamp_is_set(next
->monotonic
)) {
663 if (next
->monotonic
> nw
->monotonic
)
664 converted
= nw
->realtime
+ (next
->monotonic
- nw
->monotonic
);
666 converted
= nw
->realtime
- (nw
->monotonic
- next
->monotonic
);
668 if (timestamp_is_set(next
->realtime
))
669 next_elapse
= MIN(converted
, next
->realtime
);
671 next_elapse
= converted
;
674 next_elapse
= next
->realtime
;
679 int verb_list_timers(int argc
, char *argv
[], void *userdata
) {
680 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
681 _cleanup_strv_free_
char **machines
= NULL
;
682 _cleanup_strv_free_
char **timers_with_suffix
= NULL
;
683 _cleanup_free_
struct timer_info
*timer_infos
= NULL
;
684 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
690 r
= acquire_bus(BUS_MANAGER
, &bus
);
694 pager_open(arg_pager_flags
);
696 r
= expand_unit_names(bus
, strv_skip(argv
, 1), ".timer", &timers_with_suffix
, NULL
);
700 if (argc
== 1 || timers_with_suffix
) {
701 n
= get_unit_list_recursive(bus
, timers_with_suffix
, &unit_infos
, &replies
, &machines
);
705 dual_timestamp_get(&nw
);
707 for (const UnitInfo
*u
= unit_infos
; u
< unit_infos
+ n
; u
++) {
708 _cleanup_strv_free_
char **triggered
= NULL
;
709 dual_timestamp next
= DUAL_TIMESTAMP_NULL
;
712 if (!endswith(u
->id
, ".timer"))
715 r
= get_triggered_units(bus
, u
->unit_path
, &triggered
);
719 r
= get_next_elapse(bus
, u
->unit_path
, &next
);
723 get_last_trigger(bus
, u
->unit_path
, &last
);
725 if (!GREEDY_REALLOC(timer_infos
, c
+1)) {
730 m
= calc_next_elapse(&nw
, &next
);
732 timer_infos
[c
++] = (struct timer_info
) {
733 .machine
= u
->machine
,
736 .last_trigger
= last
,
737 .triggered
= TAKE_PTR(triggered
),
741 typesafe_qsort(timer_infos
, c
, timer_info_compare
);
744 output_timers_list(timer_infos
, c
);
747 for (struct timer_info
*t
= timer_infos
; t
< timer_infos
+ c
; t
++)
748 strv_free(t
->triggered
);
753 struct automount_info
{
758 usec_t timeout_idle_usec
;
762 static int automount_info_compare(const struct automount_info
*a
, const struct automount_info
*b
) {
768 r
= strcasecmp_ptr(a
->machine
, b
->machine
);
772 return strcmp(a
->where
, b
->where
);
775 static int collect_automount_info(sd_bus
* bus
, const UnitInfo
* info
, struct automount_info
*ret_info
) {
776 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
777 _cleanup_free_
char *mount
= NULL
, *mount_path
= NULL
, *where
= NULL
, *what
= NULL
, *state
= NULL
;
778 usec_t timeout_idle_usec
;
786 locator
= (BusLocator
) {
787 .destination
= "org.freedesktop.systemd1",
788 .path
= info
->unit_path
,
789 .interface
= "org.freedesktop.systemd1.Automount",
792 r
= bus_get_property_string(bus
, &locator
, "Where", &error
, &where
);
794 return log_error_errno(r
, "Failed to get automount target: %s", bus_error_message(&error
, r
));
796 r
= bus_get_property_trivial(bus
, &locator
, "TimeoutIdleUSec", &error
, 't', &timeout_idle_usec
);
798 return log_error_errno(r
, "Failed to get idle timeout: %s", bus_error_message(&error
, r
));
800 r
= unit_name_from_path(where
, ".mount", &mount
);
802 return log_error_errno(r
, "Failed to generate unit name from path: %m");
804 mount_path
= unit_dbus_path_from_name(mount
);
808 locator
.path
= mount_path
;
809 locator
.interface
= "org.freedesktop.systemd1.Mount";
811 r
= bus_get_property_string(bus
, &locator
, "What", &error
, &what
);
813 return log_error_errno(r
, "Failed to get mount source: %s", bus_error_message(&error
, r
));
815 locator
.interface
= "org.freedesktop.systemd1.Unit";
817 r
= bus_get_property_string(bus
, &locator
, "ActiveState", &error
, &state
);
819 return log_error_errno(r
, "Failed to get mount state: %s", bus_error_message(&error
, r
));
821 *ret_info
= (struct automount_info
) {
822 .machine
= info
->machine
,
824 .what
= TAKE_PTR(what
),
825 .where
= TAKE_PTR(where
),
826 .timeout_idle_usec
= timeout_idle_usec
,
827 .mounted
= streq_ptr(state
, "active"),
833 static int output_automounts_list(struct automount_info
*infos
, size_t n_infos
) {
834 _cleanup_(table_unrefp
) Table
*table
= NULL
;
837 assert(infos
|| n_infos
== 0);
839 table
= table_new("what", "where", "mounted", "idle timeout", "unit");
843 table_set_header(table
, arg_legend
!= 0);
845 table_set_width(table
, 0);
847 (void) table_set_empty_string(table
, "-");
849 for (struct automount_info
*info
= infos
; info
< infos
+ n_infos
; info
++) {
850 _cleanup_free_
char *j
= NULL
;
854 j
= strjoin(info
->machine
, ":", info
->id
);
861 r
= table_add_many(table
,
862 TABLE_STRING
, info
->what
,
863 TABLE_STRING
, info
->where
,
864 TABLE_BOOLEAN
, info
->mounted
,
865 TABLE_TIMESPAN_MSEC
, info
->timeout_idle_usec
,
868 return table_log_add_error(r
);
871 r
= output_table(table
);
876 output_legend("automount", n_infos
);
881 int verb_list_automounts(int argc
, char *argv
[], void *userdata
) {
882 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
883 _cleanup_strv_free_
char **machines
= NULL
, **automounts
= NULL
;
884 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
885 _cleanup_free_
struct automount_info
*automount_infos
= NULL
;
890 r
= acquire_bus(BUS_MANAGER
, &bus
);
894 pager_open(arg_pager_flags
);
896 r
= expand_unit_names(bus
, strv_skip(argv
, 1), ".automount", &automounts
, NULL
);
900 if (argc
== 1 || automounts
) {
901 n
= get_unit_list_recursive(bus
, automounts
, &unit_infos
, &replies
, &machines
);
905 for (const UnitInfo
*u
= unit_infos
; u
< unit_infos
+ n
; u
++) {
906 if (!endswith(u
->id
, ".automount"))
909 if (!GREEDY_REALLOC(automount_infos
, c
+ 1)) {
914 r
= collect_automount_info(bus
, u
, &automount_infos
[c
]);
921 typesafe_qsort(automount_infos
, c
, automount_info_compare
);
924 output_automounts_list(automount_infos
, c
);
927 assert(c
== 0 || automount_infos
);
928 for (struct automount_info
*info
= automount_infos
; info
< automount_infos
+ c
; info
++) {