sd-json: add sd_json_parse_fd() helper

This commit is contained in:
Lennart Poettering
2026-05-18 08:51:09 +02:00
parent 3d88e78927
commit 321df4885c
3 changed files with 49 additions and 6 deletions
+7 -6
View File
@@ -1093,13 +1093,14 @@ global:
LIBSYSTEMD_261 {
global:
sd_event_add_cpu_pressure;
sd_event_add_io_pressure;
sd_event_source_set_cpu_pressure_period;
sd_event_source_set_cpu_pressure_type;
sd_event_source_set_io_pressure_period;
sd_event_source_set_io_pressure_type;
sd_json_parse_fd;
sd_varlink_call_and_upgrade;
sd_varlink_reply_and_upgrade;
sd_varlink_set_sentinel;
sd_event_add_cpu_pressure;
sd_event_source_set_cpu_pressure_type;
sd_event_source_set_cpu_pressure_period;
sd_event_add_io_pressure;
sd_event_source_set_io_pressure_type;
sd_event_source_set_io_pressure_period;
} LIBSYSTEMD_260;
+39
View File
@@ -12,6 +12,7 @@
#include "errno-util.h"
#include "escape.h"
#include "ether-addr-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "float.h"
#include "hexdecoct.h"
@@ -3526,6 +3527,44 @@ _public_ int sd_json_parse_file(
return sd_json_parse_file_at(f, AT_FDCWD, path, flags, ret, reterr_line, reterr_column);
}
_public_ int sd_json_parse_fd(
const char *path,
int fd,
sd_json_parse_flags_t flags,
sd_json_variant **ret,
unsigned *reterr_line,
unsigned *reterr_column) {
int r;
assert_return(fd >= 0, -EBADF);
_cleanup_close_ int our_fd = -EBADF;
if (FLAGS_SET(flags, SD_JSON_PARSE_REOPEN_FD)) {
assert_return(!FLAGS_SET(flags, SD_JSON_PARSE_DONATE_FD), -EINVAL);
our_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC);
if (our_fd < 0)
return our_fd;
/* If we reopened the thing the file offset will be at the beginning anyway */
flags &= ~SD_JSON_PARSE_SEEK0;
} else if (FLAGS_SET(flags, SD_JSON_PARSE_DONATE_FD))
our_fd = fd;
else {
our_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
if (our_fd < 0)
return -errno;
}
_cleanup_fclose_ FILE *f = NULL;
r = take_fdopen_unlocked(&our_fd, "r", &f);
if (r < 0)
return r;
return sd_json_parse_file(f, path, flags, ret, reterr_line, reterr_column);
}
char *json_underscorify(char *p) {
if (!p)
return NULL;