From b2ae4d9eb85f80d639733e0ff140c5fa8cae71ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 16 Jan 2020 13:05:32 +0100 Subject: 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(). --- src/sysctl/sysctl.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'src/sysctl') diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 56d86d8eba..1ced48b4db 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -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; } -- cgit v1.2.1