1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "format-table.h"
7 #include "locale-util.h"
10 #include "systemctl-list-units.h"
11 #include "systemctl-util.h"
12 #include "systemctl.h"
13 #include "terminal-util.h"
15 static void message_set_freep(Set
**set
) {
16 set_free_with_destructor(*set
, sd_bus_message_unref
);
19 static int get_unit_list_recursive(
22 UnitInfo
**ret_unit_infos
,
24 char ***ret_machines
) {
26 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
27 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
28 sd_bus_message
*reply
;
33 assert(ret_unit_infos
);
36 replies
= set_new(NULL
);
40 c
= get_unit_list(bus
, NULL
, patterns
, &unit_infos
, 0, &reply
);
44 r
= set_put(replies
, reply
);
46 sd_bus_message_unref(reply
);
51 _cleanup_strv_free_
char **machines
= NULL
;
53 r
= sd_get_machine_names(&machines
);
55 return log_error_errno(r
, "Failed to get machine names: %m");
57 STRV_FOREACH(i
, machines
) {
58 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*container
= NULL
;
61 r
= sd_bus_open_system_machine(&container
, *i
);
63 log_warning_errno(r
, "Failed to connect to container %s, ignoring: %m", *i
);
67 k
= get_unit_list(container
, *i
, patterns
, &unit_infos
, c
, &reply
);
73 r
= set_put(replies
, reply
);
75 sd_bus_message_unref(reply
);
80 *ret_machines
= TAKE_PTR(machines
);
84 *ret_unit_infos
= TAKE_PTR(unit_infos
);
85 *ret_replies
= TAKE_PTR(replies
);
90 static int output_units_list(const UnitInfo
*unit_infos
, unsigned c
) {
91 _cleanup_(table_unrefp
) Table
*table
= NULL
;
92 unsigned job_count
= 0;
95 table
= table_new("", "unit", "load", "active", "sub", "job", "description");
99 table_set_header(table
, arg_legend
!= 0);
101 /* Hide the 'glyph' column when --plain is requested */
102 r
= table_hide_column_from_display(table
, 0);
104 return log_error_errno(r
, "Failed to hide column: %m");
107 table_set_width(table
, 0);
109 (void) table_set_empty_string(table
, "-");
111 for (const UnitInfo
*u
= unit_infos
; unit_infos
&& (size_t) (u
- unit_infos
) < c
; u
++) {
112 _cleanup_free_
char *j
= NULL
;
113 const char *on_underline
= "", *on_loaded
= "", *on_active
= "";
114 const char *on_circle
= "", *id
;
115 bool circle
= false, underline
= false;
117 if (u
+ 1 < unit_infos
+ c
&&
118 !streq(unit_type_suffix(u
->id
), unit_type_suffix((u
+ 1)->id
))) {
119 on_underline
= ansi_underline();
123 if (STR_IN_SET(u
->load_state
, "error", "not-found", "bad-setting", "masked") && !arg_plain
) {
124 on_circle
= underline
? ansi_highlight_yellow_underline() : ansi_highlight_yellow();
126 on_loaded
= underline
? ansi_highlight_red_underline() : ansi_highlight_red();
127 } else if (streq(u
->active_state
, "failed") && !arg_plain
) {
128 on_circle
= underline
? ansi_highlight_red_underline() : ansi_highlight_red();
130 on_active
= underline
? ansi_highlight_red_underline() : ansi_highlight_red();
132 on_circle
= on_underline
;
133 on_active
= on_underline
;
134 on_loaded
= on_underline
;
138 j
= strjoin(u
->machine
, ":", u
->id
);
146 r
= table_add_many(table
,
147 TABLE_STRING
, circle
? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE
) : " ",
148 TABLE_SET_BOTH_COLORS
, on_circle
,
150 TABLE_SET_BOTH_COLORS
, on_active
,
151 TABLE_STRING
, u
->load_state
,
152 TABLE_SET_BOTH_COLORS
, on_loaded
,
153 TABLE_STRING
, u
->active_state
,
154 TABLE_SET_BOTH_COLORS
, on_active
,
155 TABLE_STRING
, u
->sub_state
,
156 TABLE_SET_BOTH_COLORS
, on_active
,
157 TABLE_STRING
, u
->job_id
? u
->job_type
: "",
158 TABLE_SET_BOTH_COLORS
, on_underline
,
159 TABLE_STRING
, u
->description
,
160 TABLE_SET_BOTH_COLORS
, on_underline
);
162 return table_log_add_error(r
);
168 if (job_count
== 0) {
169 /* There's no data in the JOB column, so let's hide it */
170 r
= table_hide_column_from_display(table
, 5);
172 return log_error_errno(r
, "Failed to hide column: %m");
175 r
= output_table(table
);
179 if (arg_legend
!= 0) {
180 const char *on
, *off
;
181 size_t records
= table_get_rows(table
) - 1;
185 "LOAD = Reflects whether the unit definition was properly loaded.\n"
186 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
187 "SUB = The low-level unit activation state, values depend on unit type.");
189 puts("JOB = Pending job for the unit.\n");
190 on
= ansi_highlight();
193 on
= ansi_highlight_red();
197 if (arg_all
|| strv_contains(arg_states
, "inactive"))
198 printf("%s%zu loaded units listed.%s\n"
199 "To show all installed unit files use 'systemctl list-unit-files'.\n",
201 else if (!arg_states
)
202 printf("%s%zu loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
203 "To show all installed unit files use 'systemctl list-unit-files'.\n",
206 printf("%zu loaded units listed.\n", records
);
212 int verb_list_units(int argc
, char *argv
[], void *userdata
) {
213 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
214 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
215 _cleanup_strv_free_
char **machines
= NULL
;
219 r
= acquire_bus(BUS_MANAGER
, &bus
);
223 pager_open(arg_pager_flags
);
225 if (arg_with_dependencies
) {
226 _cleanup_strv_free_
char **names
= NULL
;
228 r
= append_unit_dependencies(bus
, strv_skip(argv
, 1), &names
);
232 r
= get_unit_list_recursive(bus
, names
, &unit_infos
, &replies
, &machines
);
236 r
= get_unit_list_recursive(bus
, strv_skip(argv
, 1), &unit_infos
, &replies
, &machines
);
241 typesafe_qsort(unit_infos
, r
, unit_info_compare
);
242 return output_units_list(unit_infos
, r
);
245 static int get_triggered_units(
250 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
257 r
= sd_bus_get_property_strv(
259 "org.freedesktop.systemd1",
261 "org.freedesktop.systemd1.Unit",
266 return log_error_errno(r
, "Failed to determine triggers: %s", bus_error_message(&error
, r
));
271 static int get_listening(
273 const char* unit_path
,
276 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
277 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
278 const char *type
, *path
;
281 r
= sd_bus_get_property(
283 "org.freedesktop.systemd1",
285 "org.freedesktop.systemd1.Socket",
291 return log_error_errno(r
, "Failed to get list of listening sockets: %s", bus_error_message(&error
, r
));
293 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ss)");
295 return bus_log_parse_error(r
);
297 while ((r
= sd_bus_message_read(reply
, "(ss)", &type
, &path
)) > 0) {
299 r
= strv_extend(listening
, type
);
303 r
= strv_extend(listening
, path
);
310 return bus_log_parse_error(r
);
312 r
= sd_bus_message_exit_container(reply
);
314 return bus_log_parse_error(r
);
326 /* Note: triggered is a list here, although it almost certainly will always be one
327 * unit. Nevertheless, dbus API allows for multiple values, so let's follow that. */
330 /* The strv above is shared. free is set only in the first one. */
334 static int socket_info_compare(const struct socket_info
*a
, const struct socket_info
*b
) {
340 r
= strcasecmp_ptr(a
->machine
, b
->machine
);
344 r
= strcmp(a
->path
, b
->path
);
348 return strcmp(a
->type
, b
->type
);
351 static int output_sockets_list(struct socket_info
*socket_infos
, unsigned cs
) {
352 _cleanup_(table_unrefp
) Table
*table
= NULL
;
353 const char *on
, *off
;
356 table
= table_new("listen", "type", "unit", "activates");
360 if (!arg_show_types
) {
361 /* Hide the second (TYPE) column */
362 r
= table_set_display(table
, (size_t) 0, (size_t) 2, (size_t) 3);
364 return log_error_errno(r
, "Failed to set columns to display: %m");
367 table_set_header(table
, arg_legend
!= 0);
369 table_set_width(table
, 0);
371 (void) table_set_empty_string(table
, "-");
374 for (struct socket_info
*s
= socket_infos
; s
< socket_infos
+ cs
; s
++) {
375 _cleanup_free_
char *j
= NULL
;
379 j
= strjoin(s
->machine
, ":", s
->path
);
386 r
= table_add_many(table
,
388 TABLE_STRING
, s
->type
,
389 TABLE_STRING
, s
->id
);
391 return table_log_add_error(r
);
393 if (strv_isempty(s
->triggered
))
394 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
395 else if (strv_length(s
->triggered
) == 1)
396 r
= table_add_cell(table
, NULL
, TABLE_STRING
, s
->triggered
[0]);
398 /* This should never happen, currently our socket units can only trigger a
399 * single unit. But let's handle this anyway, who knows what the future
401 r
= table_add_cell(table
, NULL
, TABLE_STRV
, s
->triggered
);
403 return table_log_add_error(r
);
407 on
= ansi_highlight();
410 on
= ansi_highlight_red();
414 r
= output_table(table
);
418 if (arg_legend
!= 0) {
419 printf("\n%s%u sockets listed.%s\n", on
, cs
, off
);
421 printf("Pass --all to see loaded but inactive sockets, too.\n");
427 int verb_list_sockets(int argc
, char *argv
[], void *userdata
) {
428 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
429 _cleanup_strv_free_
char **machines
= NULL
;
430 _cleanup_strv_free_
char **sockets_with_suffix
= NULL
;
431 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
432 _cleanup_free_
struct socket_info
*socket_infos
= NULL
;
437 r
= acquire_bus(BUS_MANAGER
, &bus
);
441 pager_open(arg_pager_flags
);
443 r
= expand_unit_names(bus
, strv_skip(argv
, 1), ".socket", &sockets_with_suffix
, NULL
);
447 if (argc
== 1 || sockets_with_suffix
) {
448 n
= get_unit_list_recursive(bus
, sockets_with_suffix
, &unit_infos
, &replies
, &machines
);
452 for (const UnitInfo
*u
= unit_infos
; u
< unit_infos
+ n
; u
++) {
453 _cleanup_strv_free_
char **listening
= NULL
, **triggered
= NULL
;
456 if (!endswith(u
->id
, ".socket"))
459 r
= get_triggered_units(bus
, u
->unit_path
, &triggered
);
463 c
= get_listening(bus
, u
->unit_path
, &listening
);
469 if (!GREEDY_REALLOC(socket_infos
, cs
+ c
)) {
474 for (int i
= 0; i
< c
; i
++)
475 socket_infos
[cs
+ i
] = (struct socket_info
) {
476 .machine
= u
->machine
,
478 .type
= listening
[i
*2],
479 .path
= listening
[i
*2 + 1],
480 .triggered
= triggered
,
481 .own_triggered
= i
==0,
484 /* from this point on we will cleanup those socket_infos */
487 listening
= triggered
= NULL
; /* avoid cleanup */
490 typesafe_qsort(socket_infos
, cs
, socket_info_compare
);
493 output_sockets_list(socket_infos
, cs
);
496 assert(cs
== 0 || socket_infos
);
497 for (struct socket_info
*s
= socket_infos
; s
< socket_infos
+ cs
; s
++) {
500 if (s
->own_triggered
)
501 strv_free(s
->triggered
);
507 static int get_next_elapse(
510 dual_timestamp
*next
) {
512 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
520 r
= sd_bus_get_property_trivial(
522 "org.freedesktop.systemd1",
524 "org.freedesktop.systemd1.Timer",
525 "NextElapseUSecMonotonic",
530 return log_error_errno(r
, "Failed to get next elapse time: %s", bus_error_message(&error
, r
));
532 r
= sd_bus_get_property_trivial(
534 "org.freedesktop.systemd1",
536 "org.freedesktop.systemd1.Timer",
537 "NextElapseUSecRealtime",
542 return log_error_errno(r
, "Failed to get next elapse time: %s", bus_error_message(&error
, r
));
548 static int get_last_trigger(
553 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
560 r
= sd_bus_get_property_trivial(
562 "org.freedesktop.systemd1",
564 "org.freedesktop.systemd1.Timer",
570 return log_error_errno(r
, "Failed to get last trigger time: %s", bus_error_message(&error
, r
));
583 static int timer_info_compare(const struct timer_info
*a
, const struct timer_info
*b
) {
589 r
= strcasecmp_ptr(a
->machine
, b
->machine
);
593 r
= CMP(a
->next_elapse
, b
->next_elapse
);
597 return strcmp(a
->id
, b
->id
);
600 static int output_timers_list(struct timer_info
*timer_infos
, unsigned n
) {
601 _cleanup_(table_unrefp
) Table
*table
= NULL
;
602 const char *on
, *off
;
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
);
645 on
= ansi_highlight();
648 on
= ansi_highlight_red();
652 r
= output_table(table
);
656 if (arg_legend
!= 0) {
657 printf("\n%s%u timers listed.%s\n", on
, n
, off
);
659 printf("Pass --all to see loaded but inactive timers, too.\n");
665 usec_t
calc_next_elapse(dual_timestamp
*nw
, dual_timestamp
*next
) {
671 if (timestamp_is_set(next
->monotonic
)) {
674 if (next
->monotonic
> nw
->monotonic
)
675 converted
= nw
->realtime
+ (next
->monotonic
- nw
->monotonic
);
677 converted
= nw
->realtime
- (nw
->monotonic
- next
->monotonic
);
679 if (timestamp_is_set(next
->realtime
))
680 next_elapse
= MIN(converted
, next
->realtime
);
682 next_elapse
= converted
;
685 next_elapse
= next
->realtime
;
690 int verb_list_timers(int argc
, char *argv
[], void *userdata
) {
691 _cleanup_(message_set_freep
) Set
*replies
= NULL
;
692 _cleanup_strv_free_
char **machines
= NULL
;
693 _cleanup_strv_free_
char **timers_with_suffix
= NULL
;
694 _cleanup_free_
struct timer_info
*timer_infos
= NULL
;
695 _cleanup_free_ UnitInfo
*unit_infos
= NULL
;
701 r
= acquire_bus(BUS_MANAGER
, &bus
);
705 pager_open(arg_pager_flags
);
707 r
= expand_unit_names(bus
, strv_skip(argv
, 1), ".timer", &timers_with_suffix
, NULL
);
711 if (argc
== 1 || timers_with_suffix
) {
712 n
= get_unit_list_recursive(bus
, timers_with_suffix
, &unit_infos
, &replies
, &machines
);
716 dual_timestamp_get(&nw
);
718 for (const UnitInfo
*u
= unit_infos
; u
< unit_infos
+ n
; u
++) {
719 _cleanup_strv_free_
char **triggered
= NULL
;
720 dual_timestamp next
= DUAL_TIMESTAMP_NULL
;
723 if (!endswith(u
->id
, ".timer"))
726 r
= get_triggered_units(bus
, u
->unit_path
, &triggered
);
730 r
= get_next_elapse(bus
, u
->unit_path
, &next
);
734 get_last_trigger(bus
, u
->unit_path
, &last
);
736 if (!GREEDY_REALLOC(timer_infos
, c
+1)) {
741 m
= calc_next_elapse(&nw
, &next
);
743 timer_infos
[c
++] = (struct timer_info
) {
744 .machine
= u
->machine
,
747 .last_trigger
= last
,
748 .triggered
= TAKE_PTR(triggered
),
752 typesafe_qsort(timer_infos
, c
, timer_info_compare
);
755 output_timers_list(timer_infos
, c
);
758 for (struct timer_info
*t
= timer_infos
; t
< timer_infos
+ c
; t
++)
759 strv_free(t
->triggered
);