]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
kernel-install,bootctl: unify the config parsing procedure
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 23 May 2024 12:43:50 +0000 (14:43 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 23 May 2024 14:15:24 +0000 (16:15 +0200)
Fixes https://github.com/systemd/systemd/issues/32992.

src/boot/bootctl-install.c
src/kernel-install/kernel-install.c
src/shared/kernel-config.c [new file with mode: 0644]
src/shared/kernel-config.h [new file with mode: 0644]
src/shared/meson.build

index b805fa8f7ad18b7cfc7b6e981a9c29731ea9d003..dc46d30c5bba5c92bb1577338bc339d4415d749f 100644 (file)
@@ -14,6 +14,7 @@
 #include "fs-util.h"
 #include "glyph-util.h"
 #include "id128-util.h"
+#include "kernel-config.h"
 #include "os-util.h"
 #include "path-util.h"
 #include "rm-rf.h"
@@ -81,46 +82,19 @@ static int load_etc_machine_info(void) {
         return 0;
 }
 
-static int load_kernel_install_conf(void) {
+static int load_kernel_install_layout(void) {
         _cleanup_free_ char *layout = NULL;
-        const ConfigTableItem items[] = {
-                { NULL, "layout",           config_parse_string, 0, &layout           },
-                {}
-        };
         int r;
 
-        const char *conf_root = getenv("KERNEL_INSTALL_CONF_ROOT");
-
-        if (conf_root) {
-                _cleanup_free_ char *conf = NULL;
-
-                conf = path_join(conf_root, "install.conf");
-                if (!conf)
-                        return log_oom();
-
-                r = config_parse_many(
-                                STRV_MAKE_CONST(conf),
-                                STRV_MAKE_CONST(conf_root),
-                                "install.conf.d",
-                                /* root= */ NULL, /* $KERNEL_INSTALL_CONF_ROOT and --root are independent */
-                                /* sections= */ NULL,
-                                config_item_table_lookup, items,
-                                CONFIG_PARSE_WARN,
-                                /* userdata = */ NULL,
-                                /* ret_stats_by_path= */ NULL,
-                                /* ret_dropin_files= */ NULL);
-        } else
-                r = config_parse_standard_file_with_dropins_full(
-                                arg_root,
-                                "kernel/install.conf",
-                                /* sections= */ NULL,
-                                config_item_table_lookup, items,
-                                CONFIG_PARSE_WARN,
-                                /* userdata = */ NULL,
-                                /* ret_stats_by_path= */ NULL,
-                                /* ret_dropin_files= */ NULL);
-        if (r < 0)
-                return r == -ENOENT ? 0 : r;
+        r = load_kernel_install_conf(arg_root,
+                                     getenv("KERNEL_INSTALL_CONF_ROOT"),
+                                     /* ret_machine_id= */ NULL,
+                                     /* ret_boot_root= */ NULL,
+                                     &layout,
+                                     /* ret_initrd_generator= */ NULL,
+                                     /* ret_uki_generator= */ NULL);
+        if (r <= 0)
+                return r;
 
         if (!isempty(layout)) {
                 log_debug("layout=%s is specified in config.", layout);
@@ -147,7 +121,7 @@ static int settle_make_entry_directory(void) {
         if (r < 0)
                 return r;
 
-        r = load_kernel_install_conf();
+        r = load_kernel_install_layout();
         if (r < 0)
                 return r;
 
index 6581e80656aa7a35c21032b375a420c4159564e4..5d559a97a8d4516ef6837335533dc073c8662578 100644 (file)
@@ -19,6 +19,7 @@
 #include "fs-util.h"
 #include "id128-util.h"
 #include "image-policy.h"
+#include "kernel-config.h"
 #include "kernel-image.h"
 #include "main-func.h"
 #include "mkdir.h"
@@ -434,48 +435,19 @@ static int context_load_environment(Context *c) {
 static int context_load_install_conf(Context *c) {
         _cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
                             *initrd_generator = NULL, *uki_generator = NULL;
-        const ConfigTableItem items[] = {
-                { NULL, "MACHINE_ID",       config_parse_string, 0, &machine_id       },
-                { NULL, "BOOT_ROOT",        config_parse_string, 0, &boot_root        },
-                { NULL, "layout",           config_parse_string, 0, &layout           },
-                { NULL, "initrd_generator", config_parse_string, 0, &initrd_generator },
-                { NULL, "uki_generator",    config_parse_string, 0, &uki_generator    },
-                {}
-        };
         int r;
 
         assert(c);
 
-        if (c->conf_root) {
-                _cleanup_free_ char *conf = NULL;
-
-                conf = path_join(c->conf_root, "install.conf");
-                if (!conf)
-                        return log_oom();
-
-                r = config_parse_many(
-                                STRV_MAKE_CONST(conf),
-                                STRV_MAKE_CONST(c->conf_root),
-                                "install.conf.d",
-                                /* root= */ NULL, /* $KERNEL_INSTALL_CONF_ROOT and --root are independent */
-                                /* sections= */ NULL,
-                                config_item_table_lookup, items,
-                                CONFIG_PARSE_WARN,
-                                /* userdata = */ NULL,
-                                /* ret_stats_by_path= */ NULL,
-                                /* ret_dropin_files= */ NULL);
-        } else
-                r = config_parse_standard_file_with_dropins_full(
-                                arg_root,
-                                "kernel/install.conf",
-                                /* sections= */ NULL,
-                                config_item_table_lookup, items,
-                                CONFIG_PARSE_WARN,
-                                /* userdata = */ NULL,
-                                /* ret_stats_by_path= */ NULL,
-                                /* ret_dropin_files= */ NULL);
-        if (r < 0)
-                return r == -ENOENT ? 0 : r;
+        r = load_kernel_install_conf(arg_root,
+                                     c->conf_root,
+                                     &machine_id,
+                                     &boot_root,
+                                     &layout,
+                                     &initrd_generator,
+                                     &uki_generator);
+        if (r <= 0)
+                return r;
 
         (void) context_set_machine_id(c, machine_id, "config");
         (void) context_set_boot_root(c, boot_root, "config");
diff --git a/src/shared/kernel-config.c b/src/shared/kernel-config.c
new file mode 100644 (file)
index 0000000..483ca28
--- /dev/null
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <errno.h>
+
+#include "conf-parser.h"
+#include "kernel-config.h"
+#include "macro.h"
+#include "path-util.h"
+#include "strv.h"
+
+int load_kernel_install_conf(
+                const char *root,
+                const char *conf_root,
+                char **ret_machine_id,
+                char **ret_boot_root,
+                char **ret_layout,
+                char **ret_initrd_generator,
+                char **ret_uki_generator) {
+
+        _cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
+                            *initrd_generator = NULL, *uki_generator = NULL;
+        const ConfigTableItem items[] = {
+                { NULL, "MACHINE_ID",       config_parse_string, 0, &machine_id       },
+                { NULL, "BOOT_ROOT",        config_parse_string, 0, &boot_root        },
+                { NULL, "layout",           config_parse_string, 0, &layout           },
+                { NULL, "initrd_generator", config_parse_string, 0, &initrd_generator },
+                { NULL, "uki_generator",    config_parse_string, 0, &uki_generator    },
+                {}
+        };
+        int r;
+
+        if (conf_root) {
+                _cleanup_free_ char *conf = path_join(conf_root, "install.conf");
+                if (!conf)
+                        return log_oom();
+
+                r = config_parse_many(
+                                STRV_MAKE_CONST(conf),
+                                STRV_MAKE_CONST(conf_root),
+                                "install.conf.d",
+                                /* root= */ NULL, /* $KERNEL_INSTALL_CONF_ROOT and --root are independent */
+                                /* sections= */ NULL,
+                                config_item_table_lookup, items,
+                                CONFIG_PARSE_WARN,
+                                /* userdata= */ NULL,
+                                /* ret_stats_by_path= */ NULL,
+                                /* ret_dropin_files= */ NULL);
+        } else
+                r = config_parse_standard_file_with_dropins_full(
+                                root,
+                                "kernel/install.conf",
+                                /* sections= */ NULL,
+                                config_item_table_lookup, items,
+                                CONFIG_PARSE_WARN,
+                                /* userdata= */ NULL,
+                                /* ret_stats_by_path= */ NULL,
+                                /* ret_dropin_files= */ NULL);
+        if (r < 0 && r != -ENOENT)
+                return r;
+
+        if (ret_machine_id)
+                *ret_machine_id = TAKE_PTR(machine_id);
+        if (ret_boot_root)
+                *ret_boot_root = TAKE_PTR(boot_root);
+        if (ret_layout)
+                *ret_layout = TAKE_PTR(layout);
+        if (ret_initrd_generator)
+                *ret_initrd_generator = TAKE_PTR(initrd_generator);
+        if (ret_uki_generator)
+                *ret_uki_generator = TAKE_PTR(uki_generator);
+        return r >= 0;  /* Return 0 if we got -ENOENT above, 1 otherwise. */
+}
diff --git a/src/shared/kernel-config.h b/src/shared/kernel-config.h
new file mode 100644 (file)
index 0000000..5681870
--- /dev/null
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+int load_kernel_install_conf(
+                const char *root,
+                const char *conf_root,
+                char **ret_machine_id,
+                char **ret_boot_root,
+                char **ret_layout,
+                char **ret_initrd_generator,
+                char **ret_uki_generator);
index d01367a159502c42af0b9b0c84e78f985e0eabb4..8fb2b7ec7f6bf125942211a58c103af377098b47 100644 (file)
@@ -98,6 +98,7 @@ shared_sources = files(
         'journal-util.c',
         'json.c',
         'kbd-util.c',
+        'kernel-config.c',
         'kernel-image.c',
         'keyring-util.c',
         'killall.c',