]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
env-file: add parse_env_data() helper
authorLennart Poettering <lennart@amutable.com>
Fri, 13 Mar 2026 21:51:42 +0000 (22:51 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 20 Mar 2026 15:34:58 +0000 (16:34 +0100)
src/basic/env-file.c
src/basic/env-file.h
src/portable/portablectl.c
src/shared/bootspec.c
src/shared/kernel-image.c

index 2e15e7eeb7d57188a482a6f66c303b5e46f6c11d..587618614e66df7dbf100701c5d257bd7e6bed5f 100644 (file)
@@ -420,6 +420,40 @@ int parse_env_file_fd_sentinel(
         return r;
 }
 
+int parse_env_datav(
+                const char *data,
+                size_t size,
+                const char *fname, /* only used for logging */
+                va_list ap) {
+
+        assert(data);
+
+        if (size == SIZE_MAX)
+                size = strlen_ptr(data);
+
+        _cleanup_fclose_ FILE *f = fmemopen_unlocked((void*) data, size, "r");
+        if (!f)
+                return -ENOMEM;
+
+        return parse_env_filev(f, fname, ap);
+}
+
+int parse_env_data_sentinel(
+                const char *data,
+                size_t size,
+                const char *fname, /* only used for logging */
+                ...) {
+
+        va_list ap;
+        int r;
+
+        va_start(ap, fname);
+        r = parse_env_datav(data, size, fname, ap);
+        va_end(ap);
+
+        return r;
+}
+
 static int load_env_file_push(
                 const char *filename, unsigned line,
                 const char *key, char *value,
index 78d47f49808574070bccff1cd2aee5bea7395f2a..4d74245915bd73f39ba523ac264a51bb4aa44ae8 100644 (file)
@@ -9,6 +9,11 @@ int parse_env_file_sentinel(FILE *f, const char *fname, ...) _sentinel_;
 #define parse_env_file(f, fname, ...) parse_env_file_sentinel(f, fname, __VA_ARGS__, NULL)
 int parse_env_file_fd_sentinel(int fd, const char *fname, ...) _sentinel_;
 #define parse_env_file_fd(fd, fname, ...) parse_env_file_fd_sentinel(fd, fname, __VA_ARGS__, NULL)
+
+int parse_env_datav(const char *data, size_t size, const char *fname, va_list ap);
+int parse_env_data_sentinel(const char *data, size_t size, const char *fname, ...) _sentinel_;
+#define parse_env_data(text, size, fname, ...) parse_env_data_sentinel(text, size, fname, __VA_ARGS__, NULL)
+
 int load_env_file(FILE *f, const char *fname, char ***ret);
 int load_env_file_pairs(FILE *f, const char *fname, char ***ret);
 int load_env_file_pairs_fd(int fd, const char *fname, char ***ret);
index ea5836fdabdd770fb396fd951eba1f4fe27eb067..98a1657a720a88072e6107a0b65568c1b305935c 100644 (file)
@@ -13,8 +13,6 @@
 #include "bus-wait-for-jobs.h"
 #include "chase.h"
 #include "env-file.h"
-#include "fd-util.h"
-#include "fileio.h"
 #include "format-table.h"
 #include "fs-util.h"
 #include "install.h"
@@ -332,15 +330,12 @@ static int verb_inspect_image(int argc, char *argv[], uintptr_t _data, void *use
                 nl = true;
         } else {
                 _cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL;
-                _cleanup_fclose_ FILE *f = NULL;
 
-                f = fmemopen_unlocked((void*) data, sz, "r");
-                if (!f)
-                        return log_error_errno(errno, "Failed to open /etc/os-release buffer: %m");
-
-                r = parse_env_file(f, "/etc/os-release",
-                                   "PORTABLE_PRETTY_NAME", &pretty_portable,
-                                   "PRETTY_NAME", &pretty_os);
+                r = parse_env_data(
+                                data, sz,
+                                "/etc/os-release",
+                                "PORTABLE_PRETTY_NAME", &pretty_portable,
+                                "PRETTY_NAME", &pretty_os);
                 if (r < 0)
                         return log_error_errno(r, "Failed to parse /etc/os-release: %m");
 
@@ -396,33 +391,30 @@ static int verb_inspect_image(int argc, char *argv[], uintptr_t _data, void *use
                                                     *confext_version_id = NULL, *confext_scope = NULL,
                                                     *confext_image_id = NULL, *confext_image_version = NULL,
                                                     *confext_build_id = NULL;
-                                _cleanup_fclose_ FILE *f = NULL;
-
-                                f = fmemopen_unlocked((void*) data, sz, "r");
-                                if (!f)
-                                        return log_error_errno(errno, "Failed to open extension-release buffer: %m");
-
-                                r = parse_env_file(f, name,
-                                                   "SYSEXT_ID", &sysext_id,
-                                                   "SYSEXT_VERSION_ID", &sysext_version_id,
-                                                   "SYSEXT_BUILD_ID", &sysext_build_id,
-                                                   "SYSEXT_IMAGE_ID", &sysext_image_id,
-                                                   "SYSEXT_IMAGE_VERSION", &sysext_image_version,
-                                                   "SYSEXT_SCOPE", &sysext_scope,
-                                                   "SYSEXT_LEVEL", &sysext_level,
-                                                   "SYSEXT_PRETTY_NAME", &sysext_pretty_os,
-                                                   "CONFEXT_ID", &confext_id,
-                                                   "CONFEXT_VERSION_ID", &confext_version_id,
-                                                   "CONFEXT_BUILD_ID", &confext_build_id,
-                                                   "CONFEXT_IMAGE_ID", &confext_image_id,
-                                                   "CONFEXT_IMAGE_VERSION", &confext_image_version,
-                                                   "CONFEXT_SCOPE", &confext_scope,
-                                                   "CONFEXT_LEVEL", &confext_level,
-                                                   "CONFEXT_PRETTY_NAME", &confext_pretty_os,
-                                                   "ID", &id,
-                                                   "VERSION_ID", &version_id,
-                                                   "PORTABLE_PRETTY_NAME", &pretty_portable,
-                                                   "PORTABLE_PREFIXES", &portable_prefixes);
+
+                                r = parse_env_data(
+                                                data, sz,
+                                                name,
+                                                "SYSEXT_ID", &sysext_id,
+                                                "SYSEXT_VERSION_ID", &sysext_version_id,
+                                                "SYSEXT_BUILD_ID", &sysext_build_id,
+                                                "SYSEXT_IMAGE_ID", &sysext_image_id,
+                                                "SYSEXT_IMAGE_VERSION", &sysext_image_version,
+                                                "SYSEXT_SCOPE", &sysext_scope,
+                                                "SYSEXT_LEVEL", &sysext_level,
+                                                "SYSEXT_PRETTY_NAME", &sysext_pretty_os,
+                                                "CONFEXT_ID", &confext_id,
+                                                "CONFEXT_VERSION_ID", &confext_version_id,
+                                                "CONFEXT_BUILD_ID", &confext_build_id,
+                                                "CONFEXT_IMAGE_ID", &confext_image_id,
+                                                "CONFEXT_IMAGE_VERSION", &confext_image_version,
+                                                "CONFEXT_SCOPE", &confext_scope,
+                                                "CONFEXT_LEVEL", &confext_level,
+                                                "CONFEXT_PRETTY_NAME", &confext_pretty_os,
+                                                "ID", &id,
+                                                "VERSION_ID", &version_id,
+                                                "PORTABLE_PRETTY_NAME", &pretty_portable,
+                                                "PORTABLE_PREFIXES", &portable_prefixes);
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to parse extension release from '%s': %m", name);
 
index a341b0729bd1ee58b44be7a2780443d2117dc2b8..2a898067f81ac40136235cb2975250974f8cfded 100644 (file)
@@ -711,7 +711,6 @@ static int boot_entry_load_unified(
         _cleanup_free_ char *fname = NULL, *os_pretty_name = NULL, *os_image_id = NULL, *os_name = NULL, *os_id = NULL,
                 *os_image_version = NULL, *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL;
         const char *k, *good_name, *good_version, *good_sort_key;
-        _cleanup_fclose_ FILE *f = NULL;
         int r;
 
         assert(root);
@@ -723,11 +722,8 @@ static int boot_entry_load_unified(
         if (!k)
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path);
 
-        f = fmemopen_unlocked((void*) osrelease_text, strlen(osrelease_text), "r");
-        if (!f)
-                return log_oom();
-
-        r = parse_env_file(f, "os-release",
+        r = parse_env_data(osrelease_text, /* size= */ SIZE_MAX,
+                           ".osrel",
                            "PRETTY_NAME", &os_pretty_name,
                            "IMAGE_ID", &os_image_id,
                            "NAME", &os_name,
@@ -755,14 +751,9 @@ static int boot_entry_load_unified(
 
         _cleanup_free_ char *profile_id = NULL, *profile_title = NULL;
         if (profile_text) {
-                fclose(f);
-
-                f = fmemopen_unlocked((void*) profile_text, strlen(profile_text), "r");
-                if (!f)
-                        return log_oom();
-
-                r = parse_env_file(
-                                f, "profile",
+                r = parse_env_data(
+                                profile_text, /* size= */ SIZE_MAX,
+                                ".profile",
                                 "ID", &profile_id,
                                 "TITLE", &profile_title);
                 if (r < 0)
index d3e18d19f41a895b8f36a41e0b36b3cbfa726e58..0f2a646da5eb19df30490de5a675ad7a2b25e602 100644 (file)
@@ -28,7 +28,6 @@ static int uki_read_pretty_name(
                 char **ret) {
 
         _cleanup_free_ char *pname = NULL, *name = NULL;
-        _cleanup_fclose_ FILE *f = NULL;
         _cleanup_free_ void *osrel = NULL;
         size_t osrel_size;
         int r;
@@ -51,12 +50,8 @@ static int uki_read_pretty_name(
                 return 0;
         }
 
-        f = fmemopen(osrel, osrel_size, "r");
-        if (!f)
-                return log_error_errno(errno, "Failed to open embedded os-release file: %m");
-
-        r = parse_env_file(
-                        f, NULL,
+        r = parse_env_data(
+                        osrel, osrel_size, ".osrel",
                         "PRETTY_NAME", &pname,
                         "NAME",        &name);
         if (r < 0)