sysctl: move hashmap allocation out of main function

This allocation is a low level detail, and it seems nicer to keep it
out of run().
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-01-16 13:05:32 +01:00
parent e76c60bf2a
commit b2ae4d9eb8

View file

@ -122,7 +122,7 @@ static bool test_prefix(const char *p) {
return false;
}
static int parse_file(OrderedHashmap *sysctl_options, const char *path, bool ignore_enoent) {
static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ignore_enoent) {
_cleanup_fclose_ FILE *f = NULL;
unsigned c = 0;
int r;
@ -183,7 +183,10 @@ static int parse_file(OrderedHashmap *sysctl_options, const char *path, bool ign
if (!test_prefix(p))
continue;
existing = ordered_hashmap_get(sysctl_options, p);
if (ordered_hashmap_ensure_allocated(sysctl_options, &option_hash_ops) < 0)
return log_oom();
existing = ordered_hashmap_get(*sysctl_options, p);
if (existing) {
if (streq(value, existing->value)) {
existing->ignore_failure = existing->ignore_failure || ignore_failure;
@ -191,14 +194,14 @@ static int parse_file(OrderedHashmap *sysctl_options, const char *path, bool ign
}
log_debug("Overwriting earlier assignment of %s at '%s:%u'.", p, path, c);
option_free(ordered_hashmap_remove(sysctl_options, p));
option_free(ordered_hashmap_remove(*sysctl_options, p));
}
new_option = option_new(p, value, ignore_failure);
if (!new_option)
return log_oom();
k = ordered_hashmap_put(sysctl_options, new_option->key, new_option);
k = ordered_hashmap_put(*sysctl_options, new_option->key, new_option);
if (k < 0)
return log_error_errno(k, "Failed to add sysctl variable %s to hashmap: %m", p);
@ -320,17 +323,13 @@ static int run(int argc, char *argv[]) {
umask(0022);
sysctl_options = ordered_hashmap_new(&option_hash_ops);
if (!sysctl_options)
return log_oom();
if (argc > optind) {
int i;
r = 0;
for (i = optind; i < argc; i++) {
k = parse_file(sysctl_options, argv[i], false);
k = parse_file(&sysctl_options, argv[i], false);
if (k < 0 && r == 0)
r = k;
}
@ -349,7 +348,7 @@ static int run(int argc, char *argv[]) {
}
STRV_FOREACH(f, files) {
k = parse_file(sysctl_options, *f, true);
k = parse_file(&sysctl_options, *f, true);
if (k < 0 && r == 0)
r = k;
}