]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: make use of sd_json_parse_fd() where appropriate
authorLennart Poettering <lennart@amutable.com>
Sun, 17 May 2026 06:00:48 +0000 (08:00 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 18 May 2026 09:39:04 +0000 (10:39 +0100)
src/core/luo.c
src/core/main.c
src/home/homework.c
src/network/networkd-serialize.c
src/resolve/resolved-static-records.c
src/shared/elf-util.c
src/shared/luo-util.c
src/sysupdate/sysupdated.c

index 9dc892d5d696eb4e6a476e357a82dab6471e18d7..929a0e3f49575a31b23e92825cf46f6a001c0b3c 100644 (file)
@@ -5,7 +5,6 @@
 #include "errno-util.h"
 #include "fd-util.h"
 #include "fdset.h"
-#include "fileio.h"
 #include "json-util.h"
 #include "log.h"
 #include "luo.h"
 #include "unit-name.h"
 
 static int luo_read_mapping(int session_fd, sd_json_variant **ret) {
-        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
-        _cleanup_close_ int mapping_fd = -EBADF;
-        _cleanup_fclose_ FILE *f = NULL;
         int r;
 
         assert(session_fd >= 0);
         assert(ret);
 
-        mapping_fd = luo_session_retrieve_fd(session_fd, LUO_MAPPING_INDEX);
+        _cleanup_close_ int mapping_fd = luo_session_retrieve_fd(session_fd, LUO_MAPPING_INDEX);
         if (mapping_fd < 0)
                 return log_warning_errno(mapping_fd, "Failed to retrieve LUO mapping fd (fd_index 0): %m");
 
-        r = fdopen_independent(mapping_fd, "r", &f);
-        if (r < 0)
-                return log_warning_errno(r, "Failed to open LUO mapping fd for reading: %m");
-
-        r = sd_json_parse_file(f, "luo-mapping", SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
+        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
+        r = sd_json_parse_fd(
+                        "luo-mapping",
+                        mapping_fd,
+                        SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_REOPEN_FD,
+                        &v,
+                        /* reterr_line= */ NULL,
+                        /* reterr_column= */ NULL);
         if (r < 0)
                 return log_warning_errno(r, "Failed to parse LUO mapping JSON: %m");
 
index f98d0edb2d0049fb6c807e9f23546aec0018fe59..0b88afae6242ee275cc8b02671422b2ea3ad038a 100644 (file)
@@ -3161,10 +3161,6 @@ DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
                 ListenFDsTag, listen_fds_tag_free);
 
 static int parse_listen_fds_mapping(int mapping_fd, Hashmap **ret_index_to_tag) {
-        _cleanup_(sd_json_variant_unrefp) sd_json_variant *root = NULL;
-        _cleanup_hashmap_free_ Hashmap *index_to_tag = NULL;
-        const char *unit_id;
-        sd_json_variant *fds_json;
         int r;
 
         assert(mapping_fd >= 0);
@@ -3175,16 +3171,20 @@ static int parse_listen_fds_mapping(int mapping_fd, Hashmap **ret_index_to_tag)
          * Returns a hashmap keyed by stringified index ("1", "2", ...) with ListenFDsTag* values
          * carrying the resolved (unit_id, original fdname, upstream index). */
 
-        _cleanup_fclose_ FILE *f = NULL;
-        r = fdopen_independent(mapping_fd, "r", &f);
-        if (r < 0)
-                return log_warning_errno(r, "Failed to open fdstore-mapping memfd: %m");
-
-        r = sd_json_parse_file(f, "fdstore-mapping", SD_JSON_PARSE_MUST_BE_OBJECT, &root,
-                               /* reterr_line= */ NULL, /* reterr_column= */ NULL);
+        _cleanup_(sd_json_variant_unrefp) sd_json_variant *root = NULL;
+        r = sd_json_parse_fd(
+                        "fdstore-mapping",
+                        mapping_fd,
+                        SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_REOPEN_FD,
+                        &root,
+                        /* reterr_line= */ NULL,
+                        /* reterr_column= */ NULL);
         if (r < 0)
                 return log_warning_errno(r, "Failed to parse fdstore-mapping JSON: %m");
 
+        _cleanup_hashmap_free_ Hashmap *index_to_tag = NULL;
+        sd_json_variant *fds_json;
+        const char *unit_id;
         JSON_VARIANT_OBJECT_FOREACH(unit_id, fds_json, root) {
                 sd_json_variant *entry;
 
index d63d48144738257e0847014377a3a09ddc13827c..eecb91511c2af75228c1bf71226496bddd466f48 100644 (file)
@@ -19,6 +19,7 @@
 #include "fileio.h"
 #include "filesystems.h"
 #include "format-util.h"
+#include "fs-util.h"
 #include "hashmap.h"
 #include "home-util.h"
 #include "homework-fido2.h"
@@ -551,27 +552,23 @@ int home_sync_and_statfs(int root_fd, struct statfs *ret) {
 }
 
 static int read_identity_file(int root_fd, sd_json_variant **ret) {
-        _cleanup_fclose_ FILE *identity_file = NULL;
-        _cleanup_close_ int identity_fd = -EBADF;
         int r;
 
         assert(root_fd >= 0);
         assert(ret);
 
-        identity_fd = openat(root_fd, ".identity", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK);
+        _cleanup_close_ int identity_fd = xopenat_full(root_fd, ".identity", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_NONBLOCK, XO_REGULAR, MODE_INVALID);
         if (identity_fd < 0)
-                return log_error_errno(errno, "Failed to open .identity file in home directory: %m");
-
-        r = fd_verify_regular(identity_fd);
-        if (r < 0)
-                return log_error_errno(r, "Embedded identity file is not a regular file, refusing: %m");
-
-        identity_file = take_fdopen(&identity_fd, "r");
-        if (!identity_file)
-                return log_oom();
+                return log_error_errno(identity_fd, "Failed to open .identity file in home directory: %m");
 
         unsigned line = 0, column = 0;
-        r = sd_json_parse_file(identity_file, ".identity", SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_SENSITIVE, ret, &line, &column);
+        r = sd_json_parse_fd(
+                        ".identity",
+                        TAKE_FD(identity_fd),
+                        SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_SENSITIVE|SD_JSON_PARSE_DONATE_FD,
+                        ret,
+                        &line,
+                        &column);
         if (r < 0)
                 return log_error_errno(r, "[.identity:%u:%u] Failed to parse JSON data: %m", line, column);
 
index 31f885d87a316cdb07a984617af5d6db032adb6c..394f03ced6bb747150362a040f5de7b70b78f8f8 100644 (file)
@@ -5,7 +5,6 @@
 #include "daemon-util.h"
 #include "errno-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "hashmap.h"
 #include "iovec-util.h"
 #include "json-util.h"
@@ -442,16 +441,12 @@ int manager_deserialize(Manager *manager) {
 
         log_debug("Deserializing...");
 
-        _cleanup_fclose_ FILE *f = take_fdopen(&fd, "r");
-        if (!f)
-                return log_debug_errno(errno, "Failed to fdopen() serialization file descriptor: %m");
-
         _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
         unsigned err_line = 0, err_column = 0;
-        r = sd_json_parse_file(
-                        f,
+        r = sd_json_parse_fd(
                         /* path= */ NULL,
-                        /* flags= */ 0,
+                        TAKE_FD(fd),
+                        SD_JSON_PARSE_DONATE_FD,
                         &v,
                         &err_line,
                         &err_column);
index efcd91c0907d50844ca53367709a973dbf244b51..348422e244ebaa3c0a67b00705f62fff33e472ce 100644 (file)
@@ -10,8 +10,6 @@
 #include "dns-question.h"
 #include "dns-rr.h"
 #include "errno-util.h"
-#include "fd-util.h"
-#include "fileio.h"
 #include "hashmap.h"
 #include "json-util.h"
 #include "log.h"
@@ -96,16 +94,9 @@ static int load_static_record_file(const ConfFile *cf, Hashmap **records, Set **
         if (set_ensure_consume(stats, &inode_unmodified_hash_ops, TAKE_PTR(st_copy)) < 0)
                 return log_oom();
 
-        _cleanup_fclose_ FILE *f = NULL;
-        r = xfopenat(cf->fd, /* path= */ NULL, "re", /* open_flags= */ 0, &f);
-        if (r < 0) {
-                log_warning_errno(r, "Failed to open '%s', skipping: %m", cf->result);
-                return 0;
-        }
-
         _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
         unsigned line = 0, column = 0;
-        r = sd_json_parse_file(f, cf->result, /* flags= */ 0, &j, &line, &column);
+        r = sd_json_parse_fd(cf->result, cf->fd, SD_JSON_PARSE_REOPEN_FD, &j, &line, &column);
         if (r < 0) {
                 if (line > 0)
                         log_syntax(/* unit= */ NULL, LOG_WARNING, cf->result, line, r, "Failed to parse JSON, skipping: %m");
index 02035404a65debd0fb80e42cb55593a62d6ed530..75e1285c2fe806c0e0bbfa3845b33f5d562fb0c2 100644 (file)
@@ -994,25 +994,25 @@ int parse_elf_object(
         }
 
         if (ret_package_metadata) {
-                _cleanup_fclose_ FILE *json_in = NULL;
-
-                json_in = take_fdopen(&package_metadata_pipe[0], "r");
-                if (!json_in)
-                        return -errno;
-
-                r = sd_json_parse_file(json_in, NULL, 0, &package_metadata, NULL, NULL);
+                r = sd_json_parse_fd(
+                                /* path= */ NULL,
+                                TAKE_FD(package_metadata_pipe[0]),
+                                SD_JSON_PARSE_DONATE_FD,
+                                &package_metadata,
+                                /* reterr_line= */ NULL,
+                                /* reterr_column= */ NULL);
                 if (r < 0 && r != -ENODATA) /* ENODATA: json was empty, so we got nothing, but that's ok */
                         log_warning_errno(r, "Failed to read or parse package metadata, ignoring: %m");
         }
 
         if (ret_dlopen_metadata) {
-                _cleanup_fclose_ FILE *json_in = NULL;
-
-                json_in = take_fdopen(&dlopen_metadata_pipe[0], "r");
-                if (!json_in)
-                        return -errno;
-
-                r = sd_json_parse_file(json_in, NULL, 0, &dlopen_metadata, NULL, NULL);
+                r = sd_json_parse_fd(
+                                /* path= */ NULL,
+                                TAKE_FD(dlopen_metadata_pipe[0]),
+                                SD_JSON_PARSE_DONATE_FD,
+                                &dlopen_metadata,
+                                /* reterr_line= */ NULL,
+                                /* reterr_column= */ NULL);
                 if (r < 0 && r != -ENODATA) /* ENODATA: json was empty, so we got nothing, but that's ok */
                         log_warning_errno(r, "Failed to read or parse dlopen metadata, ignoring: %m");
         }
index 898e51efa52c0973a8635586c0bfa5dc43fd2ab1..ab4a4a3235e607a111c4db1140f963531cfb3f4e 100644 (file)
@@ -11,7 +11,6 @@
 #include "alloc-util.h"
 #include "errno-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "json-util.h"
 #include "log.h"
 #include "luo-util.h"
@@ -118,7 +117,6 @@ int luo_session_finish(int session_fd) {
 int luo_parse_serialization(sd_json_variant **ret, int **ret_fds, size_t *ret_n_fds) {
         _cleanup_(sd_json_variant_unrefp) sd_json_variant *root = NULL;
         _cleanup_free_ int *fd_list = NULL;
-        _cleanup_fclose_ FILE *f = NULL;
         size_t n_fds = 0;
         int serialize_fd = -EBADF, r;
 
@@ -139,11 +137,13 @@ int luo_parse_serialization(sd_json_variant **ret, int **ret_fds, size_t *ret_n_
                 return log_warning_errno(serialize_fd,
                                          "Failed to parse SYSTEMD_LUO_SERIALIZE_FD='%s': %m", luo_fd_str);
 
-        r = fdopen_independent(serialize_fd, "r", &f);
-        if (r < 0)
-                return log_warning_errno(r, "Failed to open LUO serialization fd %d: %m", serialize_fd);
-
-        r = sd_json_parse_file(f, /* path= */ NULL, SD_JSON_PARSE_MUST_BE_OBJECT, &root, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
+        r = sd_json_parse_fd(
+                        /* path= */ NULL,
+                        serialize_fd,
+                        SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_REOPEN_FD,
+                        &root,
+                        /* reterr_line= */ NULL,
+                        /* reterr_column= */ NULL);
         if (r < 0)
                 return log_warning_errno(r, "Failed to parse LUO serialization JSON: %m");
 
index 4c7a759a4dadb00bd3eb2e3f0aa7afd8fb77cb4f..3d696baa75dab3423463f32e0ed08fb1e4e0bdc3 100644 (file)
@@ -24,7 +24,6 @@
 #include "escape.h"
 #include "event-util.h"
 #include "fd-util.h"
-#include "fileio.h"
 #include "format-util.h"
 #include "hashmap.h"
 #include "log.h"
@@ -246,8 +245,13 @@ static int job_parse_child_output(int _fd, sd_json_variant **ret) {
                 return 0;
         }
 
-        r = sd_json_parse_file_at(/* f= */ NULL, fd, /* path= */ NULL, /* flags= */ 0,
-                                  &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
+        r = sd_json_parse_fd(
+                        /* path= */ "stdout",
+                        TAKE_FD(fd),
+                        SD_JSON_PARSE_DONATE_FD|SD_JSON_PARSE_SEEK0,
+                        &v,
+                        /* reterr_line= */ NULL,
+                        /* reterr_column= */ NULL);
         if (r < 0)
                 return log_debug_errno(r, "Failed to parse child output as JSON: %m");
 
@@ -771,7 +775,6 @@ static int target_new(Manager *m, TargetClass class, const char *name, const cha
 static int sysupdate_run_simple(sd_json_variant **ret, Target *t, ...) {
         _cleanup_close_pair_ int pipe[2] = EBADF_PAIR;
         _cleanup_(pidref_done_sigkill_wait) PidRef pid = PIDREF_NULL;
-        _cleanup_fclose_ FILE *f = NULL;
         _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
         _cleanup_free_ char *target_arg = NULL;
         int r;
@@ -845,11 +848,14 @@ static int sysupdate_run_simple(sd_json_variant **ret, Target *t, ...) {
         }
 
         pipe[1] = safe_close(pipe[1]);
-        f = take_fdopen(&pipe[0], "r");
-        if (!f)
-                return -errno;
 
-        r = sd_json_parse_file(f, "stdout", 0, &v, NULL, NULL);
+        r = sd_json_parse_fd(
+                        "stdout",
+                        TAKE_FD(pipe[0]),
+                        SD_JSON_PARSE_DONATE_FD,
+                        &v,
+                        /* reterr_line= */ NULL,
+                        /* reterr_column= */ NULL);
         if (r < 0)
                 return log_debug_errno(r, "Failed to parse JSON: %m");