preset: Add ignore directive

The ignore directive specifies to not do anything with the given
unit and leave existing configuration intact. This allows distributions
to gradually adopt preset files by shipping a ignore * preset file.
This commit is contained in:
Daan De Meyer
2023-04-14 20:27:59 +01:00
committed by Luca Boccassi
parent 6e4ec79a3c
commit e77e07f601
8 changed files with 100 additions and 43 deletions
+33 -12
View File
@@ -52,18 +52,22 @@ typedef struct {
OrderedHashmap *have_processed;
} InstallContext;
typedef enum {
PRESET_UNKNOWN,
PRESET_ENABLE,
PRESET_DISABLE,
} PresetAction;
struct UnitFilePresetRule {
char *pattern;
PresetAction action;
char **instances;
};
/* NB! strings use past tense. */
static const char *const preset_action_past_tense_table[_PRESET_ACTION_MAX] = {
[PRESET_UNKNOWN] = "unknown",
[PRESET_ENABLE] = "enabled",
[PRESET_DISABLE] = "disabled",
[PRESET_IGNORE] = "ignored",
};
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(preset_action_past_tense, PresetAction);
static bool install_info_has_rules(const InstallInfo *i) {
assert(i);
@@ -3297,6 +3301,20 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset
};
}
parameter = first_word(l, "ignore");
if (parameter) {
char *pattern;
pattern = strdup(parameter);
if (!pattern)
return -ENOMEM;
rule = (UnitFilePresetRule) {
.pattern = pattern,
.action = PRESET_IGNORE,
};
}
if (rule.action) {
if (!GREEDY_REALLOC(ps.rules, ps.n_rules + 1))
return -ENOMEM;
@@ -3382,23 +3400,26 @@ static int query_presets(const char *name, const UnitFilePresets *presets, char
switch (action) {
case PRESET_UNKNOWN:
log_debug("Preset files don't specify rule for %s. Enabling.", name);
return 1;
return PRESET_ENABLE;
case PRESET_ENABLE:
if (instance_name_list && *instance_name_list)
STRV_FOREACH(s, *instance_name_list)
log_debug("Preset files say enable %s.", *s);
else
log_debug("Preset files say enable %s.", name);
return 1;
return PRESET_ENABLE;
case PRESET_DISABLE:
log_debug("Preset files say disable %s.", name);
return 0;
return PRESET_DISABLE;
case PRESET_IGNORE:
log_debug("Preset files say ignore %s.", name);
return PRESET_IGNORE;
default:
assert_not_reached();
}
}
int unit_file_query_preset(RuntimeScope scope, const char *root_dir, const char *name, UnitFilePresets *cached) {
PresetAction unit_file_query_preset(RuntimeScope scope, const char *root_dir, const char *name, UnitFilePresets *cached) {
_cleanup_(unit_file_presets_done) UnitFilePresets tmp = {};
int r;
@@ -3492,7 +3513,7 @@ static int preset_prepare_one(
if (r < 0)
return r;
if (r > 0) {
if (r == PRESET_ENABLE) {
if (instance_name_list)
STRV_FOREACH(s, instance_name_list) {
r = install_info_discover_and_check(plus, lp, *s, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
@@ -3507,7 +3528,7 @@ static int preset_prepare_one(
return r;
}
} else
} else if (r == PRESET_DISABLE)
r = install_info_discover(minus, lp, name, SEARCH_FOLLOW_CONFIG_SYMLINKS,
&info, changes, n_changes);
+13 -1
View File
@@ -217,8 +217,20 @@ typedef struct {
bool initialized;
} UnitFilePresets;
typedef enum PresetAction {
PRESET_UNKNOWN,
PRESET_ENABLE,
PRESET_DISABLE,
PRESET_IGNORE,
_PRESET_ACTION_MAX,
_PRESET_ACTION_INVALID = -EINVAL,
_PRESET_ACTION_ERRNO_MAX = -ERRNO_MAX, /* Ensure this type covers the whole negative errno range */
} PresetAction;
const char *preset_action_past_tense_to_string(PresetAction action);
void unit_file_presets_done(UnitFilePresets *p);
int unit_file_query_preset(RuntimeScope scope, const char *root_dir, const char *name, UnitFilePresets *cached);
PresetAction unit_file_query_preset(RuntimeScope scope, const char *root_dir, const char *name, UnitFilePresets *cached);
const char *unit_file_state_to_string(UnitFileState s) _const_;
UnitFileState unit_file_state_from_string(const char *s) _pure_;