diff --git a/src/core/luo.c b/src/core/luo.c index 9dc892d5d6..929a0e3f49 100644 --- a/src/core/luo.c +++ b/src/core/luo.c @@ -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" @@ -17,23 +16,23 @@ #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"); diff --git a/src/core/main.c b/src/core/main.c index f98d0edb2d..0b88afae62 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -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; diff --git a/src/home/homework.c b/src/home/homework.c index d63d481447..eecb91511c 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -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); diff --git a/src/network/networkd-serialize.c b/src/network/networkd-serialize.c index 31f885d87a..394f03ced6 100644 --- a/src/network/networkd-serialize.c +++ b/src/network/networkd-serialize.c @@ -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); diff --git a/src/resolve/resolved-static-records.c b/src/resolve/resolved-static-records.c index efcd91c090..348422e244 100644 --- a/src/resolve/resolved-static-records.c +++ b/src/resolve/resolved-static-records.c @@ -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"); diff --git a/src/shared/elf-util.c b/src/shared/elf-util.c index 02035404a6..75e1285c2f 100644 --- a/src/shared/elf-util.c +++ b/src/shared/elf-util.c @@ -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"); } diff --git a/src/shared/luo-util.c b/src/shared/luo-util.c index 898e51efa5..ab4a4a3235 100644 --- a/src/shared/luo-util.c +++ b/src/shared/luo-util.c @@ -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"); diff --git a/src/sysupdate/sysupdated.c b/src/sysupdate/sysupdated.c index 4c7a759a4d..3d696baa75 100644 --- a/src/sysupdate/sysupdated.c +++ b/src/sysupdate/sysupdated.c @@ -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");