Merge pull request #15692 from keszybz/preset-cleanup
Make systemctl list-unit-files output more useful
This commit is contained in:
+63
-31
@@ -1456,9 +1456,18 @@ static bool output_show_unit_file(const UnitFileList *u, char **states, char **p
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool show_preset_for_state(UnitFileState state) {
|
||||
/* Don't show preset state in those unit file states, it'll only confuse users. */
|
||||
return !IN_SET(state,
|
||||
UNIT_FILE_ALIAS,
|
||||
UNIT_FILE_STATIC,
|
||||
UNIT_FILE_GENERATED,
|
||||
UNIT_FILE_TRANSIENT);
|
||||
}
|
||||
|
||||
static int output_unit_file_list(const UnitFileList *units, unsigned c) {
|
||||
_cleanup_(table_unrefp) Table *table = NULL;
|
||||
const UnitFileList *u;
|
||||
_cleanup_(unit_file_presets_freep) UnitFilePresets presets = {};
|
||||
int r;
|
||||
|
||||
table = table_new("unit file", "state", "vendor preset");
|
||||
@@ -1469,9 +1478,8 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) {
|
||||
if (arg_full)
|
||||
table_set_width(table, 0);
|
||||
|
||||
for (u = units; u < units + c; u++) {
|
||||
for (const UnitFileList *u = units; u < units + c; u++) {
|
||||
const char *on_underline = NULL, *on_unit_color = NULL, *id;
|
||||
const char *on_preset_color = NULL, *unit_preset_str;
|
||||
bool underline;
|
||||
|
||||
underline = u + 1 < units + c &&
|
||||
@@ -1486,32 +1494,44 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) {
|
||||
UNIT_FILE_DISABLED,
|
||||
UNIT_FILE_BAD))
|
||||
on_unit_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
|
||||
else if (u->state == UNIT_FILE_ENABLED)
|
||||
else if (IN_SET(u->state,
|
||||
UNIT_FILE_ENABLED,
|
||||
UNIT_FILE_ALIAS))
|
||||
on_unit_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green();
|
||||
else
|
||||
on_unit_color = on_underline;
|
||||
|
||||
id = basename(u->path);
|
||||
|
||||
r = unit_file_query_preset(arg_scope, NULL, id);
|
||||
if (r < 0) {
|
||||
unit_preset_str = "n/a";
|
||||
on_preset_color = underline ? on_underline : ansi_normal();
|
||||
} else if (r == 0) {
|
||||
unit_preset_str = "disabled";
|
||||
on_preset_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
|
||||
} else {
|
||||
unit_preset_str = "enabled";
|
||||
on_preset_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green();
|
||||
}
|
||||
|
||||
r = table_add_many(table,
|
||||
TABLE_STRING, id,
|
||||
TABLE_SET_COLOR, strempty(on_underline),
|
||||
TABLE_STRING, unit_file_state_to_string(u->state),
|
||||
TABLE_SET_COLOR, strempty(on_unit_color),
|
||||
TABLE_STRING, unit_preset_str,
|
||||
TABLE_SET_COLOR, strempty(on_preset_color));
|
||||
TABLE_SET_COLOR, strempty(on_unit_color));
|
||||
if (r < 0)
|
||||
return table_log_add_error(r);
|
||||
|
||||
if (show_preset_for_state(u->state)) {
|
||||
const char *unit_preset_str, *on_preset_color;
|
||||
|
||||
r = unit_file_query_preset(arg_scope, arg_root, id, &presets);
|
||||
if (r < 0) {
|
||||
unit_preset_str = "n/a";
|
||||
on_preset_color = underline ? on_underline : ansi_normal();
|
||||
} else if (r == 0) {
|
||||
unit_preset_str = "disabled";
|
||||
on_preset_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
|
||||
} else {
|
||||
unit_preset_str = "enabled";
|
||||
on_preset_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green();
|
||||
}
|
||||
|
||||
r = table_add_many(table,
|
||||
TABLE_STRING, unit_preset_str,
|
||||
TABLE_SET_COLOR, strempty(on_preset_color));
|
||||
} else
|
||||
r = table_add_many(table, TABLE_EMPTY);
|
||||
|
||||
if (r < 0)
|
||||
return table_log_add_error(r);
|
||||
}
|
||||
@@ -4246,14 +4266,18 @@ static void print_status_info(
|
||||
if (!isempty(i->load_error))
|
||||
printf(" Loaded: %s%s%s (Reason: %s)\n",
|
||||
on, strna(i->load_state), off, i->load_error);
|
||||
else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset) &&
|
||||
!STR_IN_SET(i->unit_file_state, "generated", "transient"))
|
||||
printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n",
|
||||
on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset);
|
||||
else if (path && !isempty(i->unit_file_state))
|
||||
printf(" Loaded: %s%s%s (%s; %s)\n",
|
||||
on, strna(i->load_state), off, path, i->unit_file_state);
|
||||
else if (path)
|
||||
else if (path && !isempty(i->unit_file_state)) {
|
||||
bool show_preset = !isempty(i->unit_file_preset) &&
|
||||
show_preset_for_state(unit_file_state_from_string(i->unit_file_state));
|
||||
|
||||
printf(" Loaded: %s%s%s (%s; %s%s%s)\n",
|
||||
on, strna(i->load_state), off,
|
||||
path,
|
||||
i->unit_file_state,
|
||||
show_preset ? "; vendor preset: " : "",
|
||||
show_preset ? i->unit_file_preset : "");
|
||||
|
||||
} else if (path)
|
||||
printf(" Loaded: %s%s%s (%s)\n",
|
||||
on, strna(i->load_state), off, path);
|
||||
else
|
||||
@@ -5892,7 +5916,8 @@ static int show(int argc, char *argv[], void *userdata) {
|
||||
|
||||
if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"This command expects one or more unit names. Did you mean --help?");
|
||||
"'help' command expects one or more unit names.\n"
|
||||
"(Alternatively, help for systemctl itself may be shown with --help)");
|
||||
|
||||
r = acquire_bus(BUS_MANAGER, &bus);
|
||||
if (r < 0)
|
||||
@@ -7367,6 +7392,7 @@ static int unit_is_enabled(int argc, char *argv[], void *userdata) {
|
||||
UNIT_FILE_ENABLED,
|
||||
UNIT_FILE_ENABLED_RUNTIME,
|
||||
UNIT_FILE_STATIC,
|
||||
UNIT_FILE_ALIAS,
|
||||
UNIT_FILE_INDIRECT,
|
||||
UNIT_FILE_GENERATED))
|
||||
enabled = true;
|
||||
@@ -9211,9 +9237,9 @@ static int systemctl_main(int argc, char *argv[]) {
|
||||
{ "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show },
|
||||
{ "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload },
|
||||
{ "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload },
|
||||
{ "log-level", VERB_ANY, 2, 0, log_level },
|
||||
{ "log-target", VERB_ANY, 2, 0, log_target },
|
||||
{ "service-watchdogs", VERB_ANY, 2, 0, service_watchdogs },
|
||||
{ "log-level", VERB_ANY, 2, VERB_ONLINE_ONLY, log_level },
|
||||
{ "log-target", VERB_ANY, 2, VERB_ONLINE_ONLY, log_target },
|
||||
{ "service-watchdogs", VERB_ANY, 2, VERB_ONLINE_ONLY, service_watchdogs },
|
||||
{ "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment },
|
||||
{ "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment },
|
||||
{ "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment },
|
||||
@@ -9253,6 +9279,12 @@ static int systemctl_main(int argc, char *argv[]) {
|
||||
{}
|
||||
};
|
||||
|
||||
const Verb *verb = verbs_find_verb(argv[optind], verbs);
|
||||
if (verb && (verb->flags & VERB_ONLINE_ONLY) && arg_root)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Verb '%s' cannot be used with --root=.",
|
||||
argv[optind] ?: verb->verb);
|
||||
|
||||
return dispatch_verb(argc, argv, verbs, NULL);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user