diff options
179 files changed, 6814 insertions, 6530 deletions
diff --git a/meson.build b/meson.build index e42151998b..82350df5b4 100644 --- a/meson.build +++ b/meson.build @@ -2056,6 +2056,7 @@ libsystemd_includes = [basic_includes, include_directories( 'src/libsystemd/sd-hwdb', 'src/libsystemd/sd-id128', 'src/libsystemd/sd-journal', + 'src/libsystemd/sd-json', 'src/libsystemd/sd-netlink', 'src/libsystemd/sd-network', 'src/libsystemd/sd-resolve')] @@ -2104,6 +2105,7 @@ install_libsystemd_static = static_library( libdl, libgcrypt_cflags, liblz4_cflags, + libm, libmount, libopenssl, librt, diff --git a/src/analyze/analyze-fdstore.c b/src/analyze/analyze-fdstore.c index 8ada6d4e73..dc3d979d84 100644 --- a/src/analyze/analyze-fdstore.c +++ b/src/analyze/analyze-fdstore.c @@ -81,7 +81,7 @@ static int dump_fdstore(sd_bus *bus, const char *arg) { if (r < 0) return r; - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_isempty(table)) + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF) && table_isempty(table)) log_info("No file descriptors in fdstore of '%s'.", unit); else { r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */true); diff --git a/src/analyze/analyze-inspect-elf.c b/src/analyze/analyze-inspect-elf.c index 70226a842b..e949c9049a 100644 --- a/src/analyze/analyze-inspect-elf.c +++ b/src/analyze/analyze-inspect-elf.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "analyze.h" #include "analyze-inspect-elf.h" #include "elf-util.h" @@ -7,15 +9,15 @@ #include "fd-util.h" #include "format-table.h" #include "format-util.h" -#include "json.h" +#include "json-util.h" #include "path-util.h" #include "strv.h" -static int analyze_elf(char **filenames, JsonFormatFlags json_flags) { +static int analyze_elf(char **filenames, sd_json_format_flags_t json_flags) { int r; STRV_FOREACH(filename, filenames) { - _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *package_metadata = NULL; _cleanup_(table_unrefp) Table *t = NULL; _cleanup_free_ char *abspath = NULL; _cleanup_close_ int fd = -EBADF; @@ -46,12 +48,12 @@ static int analyze_elf(char **filenames, JsonFormatFlags json_flags) { return table_log_add_error(r); if (package_metadata) { - JsonVariant *module_json; + sd_json_variant *module_json; const char *module_name; JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, package_metadata) { const char *field_name; - JsonVariant *field; + sd_json_variant *field; /* The ELF type and architecture are added as top-level objects, * since they are only parsed for the file itself, but the packaging @@ -61,7 +63,7 @@ static int analyze_elf(char **filenames, JsonFormatFlags json_flags) { r = table_add_many( t, TABLE_FIELD, module_name, - TABLE_STRING, json_variant_string(module_json)); + TABLE_STRING, sd_json_variant_string(module_json)); if (r < 0) return table_log_add_error(r); @@ -88,22 +90,22 @@ static int analyze_elf(char **filenames, JsonFormatFlags json_flags) { } JSON_VARIANT_OBJECT_FOREACH(field_name, field, module_json) - if (json_variant_is_string(field)) { + if (sd_json_variant_is_string(field)) { r = table_add_many( t, TABLE_FIELD, field_name, - TABLE_STRING, json_variant_string(field)); + TABLE_STRING, sd_json_variant_string(field)); if (r < 0) return table_log_add_error(r); } } } - if (json_flags & JSON_FORMAT_OFF) { + if (json_flags & SD_JSON_FORMAT_OFF) { r = table_print(t, NULL); if (r < 0) return table_log_print_error(r); } else - json_variant_dump(package_metadata, json_flags, stdout, NULL); + sd_json_variant_dump(package_metadata, json_flags, stdout, NULL); } return 0; diff --git a/src/analyze/analyze-plot.c b/src/analyze/analyze-plot.c index e271296b68..0b4725e017 100644 --- a/src/analyze/analyze-plot.c +++ b/src/analyze/analyze-plot.c @@ -413,8 +413,8 @@ static int show_table(Table *table, const char *word) { if (!table_isempty(table)) { table_set_header(table, arg_legend); - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) - r = table_print_json(table, NULL, arg_json_format_flags | JSON_FORMAT_COLOR_AUTO); + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) + r = table_print_json(table, NULL, arg_json_format_flags | SD_JSON_FORMAT_COLOR_AUTO); else r = table_print(table, NULL); if (r < 0) @@ -490,7 +490,7 @@ int verb_plot(int argc, char *argv[], void *userdata) { typesafe_qsort(times, n, compare_unit_start); - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) || arg_table) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF) || arg_table) r = produce_plot_as_text(times, boot); else r = produce_plot_as_svg(times, host, boot, pretty_times); diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index 75508f4d39..6aa67a9339 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -1615,97 +1615,97 @@ static const struct security_assessor security_assessor_table[] = { }, }; -static JsonVariant* security_assessor_find_in_policy(const struct security_assessor *a, JsonVariant *policy, const char *name) { - JsonVariant *item; +static sd_json_variant* security_assessor_find_in_policy(const struct security_assessor *a, sd_json_variant *policy, const char *name) { + sd_json_variant *item; assert(a); if (!policy) return NULL; - if (!json_variant_is_object(policy)) { + if (!sd_json_variant_is_object(policy)) { log_debug("Specified policy is not a JSON object, ignoring."); return NULL; } - item = json_variant_by_key(policy, a->json_field); + item = sd_json_variant_by_key(policy, a->json_field); if (!item) return NULL; - if (!json_variant_is_object(item)) { + if (!sd_json_variant_is_object(item)) { log_debug("Item for '%s' in policy JSON object is not an object, ignoring.", a->id); return NULL; } - return name ? json_variant_by_key(item, name) : item; + return name ? sd_json_variant_by_key(item, name) : item; } -static uint64_t access_weight(const struct security_assessor *a, JsonVariant *policy) { - JsonVariant *val; +static uint64_t access_weight(const struct security_assessor *a, sd_json_variant *policy) { + sd_json_variant *val; assert(a); val = security_assessor_find_in_policy(a, policy, "weight"); if (val) { - if (json_variant_is_unsigned(val)) - return json_variant_unsigned(val); + if (sd_json_variant_is_unsigned(val)) + return sd_json_variant_unsigned(val); log_debug("JSON field 'weight' of policy for %s is not an unsigned integer, ignoring.", a->id); } return a->weight; } -static uint64_t access_range(const struct security_assessor *a, JsonVariant *policy) { - JsonVariant *val; +static uint64_t access_range(const struct security_assessor *a, sd_json_variant *policy) { + sd_json_variant *val; assert(a); val = security_assessor_find_in_policy(a, policy, "range"); if (val) { - if (json_variant_is_unsigned(val)) - return json_variant_unsigned(val); + if (sd_json_variant_is_unsigned(val)) + return sd_json_variant_unsigned(val); log_debug("JSON field 'range' of policy for %s is not an unsigned integer, ignoring.", a->id); } return a->range; } -static const char *access_description_na(const struct security_assessor *a, JsonVariant *policy) { - JsonVariant *val; +static const char *access_description_na(const struct security_assessor *a, sd_json_variant *policy) { + sd_json_variant *val; assert(a); val = security_assessor_find_in_policy(a, policy, "description_na"); if (val) { - if (json_variant_is_string(val)) - return json_variant_string(val); + if (sd_json_variant_is_string(val)) + return sd_json_variant_string(val); log_debug("JSON field 'description_na' of policy for %s is not a string, ignoring.", a->id); } return a->description_na; } -static const char *access_description_good(const struct security_assessor *a, JsonVariant *policy) { - JsonVariant *val; +static const char *access_description_good(const struct security_assessor *a, sd_json_variant *policy) { + sd_json_variant *val; assert(a); val = security_assessor_find_in_policy(a, policy, "description_good"); if (val) { - if (json_variant_is_string(val)) - return json_variant_string(val); + if (sd_json_variant_is_string(val)) + return sd_json_variant_string(val); log_debug("JSON field 'description_good' of policy for %s is not a string, ignoring.", a->id); } return a->description_good; } -static const char *access_description_bad(const struct security_assessor *a, JsonVariant *policy) { - JsonVariant *val; +static const char *access_description_bad(const struct security_assessor *a, sd_json_variant *policy) { + sd_json_variant *val; assert(a); val = security_assessor_find_in_policy(a, policy, "description_bad"); if (val) { - if (json_variant_is_string(val)) - return json_variant_string(val); + if (sd_json_variant_is_string(val)) + return sd_json_variant_string(val); log_debug("JSON field 'description_bad' of policy for %s is not a string, ignoring.", a->id); } @@ -1716,9 +1716,9 @@ static int assess(const SecurityInfo *info, Table *overview_table, AnalyzeSecurityFlags flags, unsigned threshold, - JsonVariant *policy, + sd_json_variant *policy, PagerFlags pager_flags, - JsonFormatFlags json_format_flags) { + sd_json_format_flags_t json_format_flags) { static const struct { uint64_t exposure; @@ -1871,7 +1871,7 @@ static int assess(const SecurityInfo *info, return log_error_errno(r, "Failed to update cell in table: %m"); } - if (json_format_flags & JSON_FORMAT_OFF) { + if (json_format_flags & SD_JSON_FORMAT_OFF) { r = table_hide_column_from_display(details_table, (size_t) 2); if (r < 0) return log_error_errno(r, "Failed to set columns to display: %m"); @@ -1890,7 +1890,7 @@ static int assess(const SecurityInfo *info, assert(i < ELEMENTSOF(badness_table)); - if (details_table && (json_format_flags & JSON_FORMAT_OFF)) { + if (details_table && (json_format_flags & SD_JSON_FORMAT_OFF)) { _cleanup_free_ char *clickable = NULL; const char *name; @@ -2412,9 +2412,9 @@ static int analyze_security_one(sd_bus *bus, Table *overview_table, AnalyzeSecurityFlags flags, unsigned threshold, - JsonVariant *policy, + sd_json_variant *policy, PagerFlags pager_flags, - JsonFormatFlags json_format_flags) { + sd_json_format_flags_t json_format_flags) { _cleanup_(security_info_freep) SecurityInfo *info = security_info_new(); if (!info) @@ -2644,9 +2644,9 @@ static int get_security_info(Unit *u, ExecContext *c, CGroupContext *g, Security static int offline_security_check(Unit *u, unsigned threshold, - JsonVariant *policy, + sd_json_variant *policy, PagerFlags pager_flags, - JsonFormatFlags json_format_flags) { + sd_json_format_flags_t json_format_flags) { _cleanup_(table_unrefp) Table *overview_table = NULL; AnalyzeSecurityFlags flags = 0; @@ -2667,7 +2667,7 @@ static int offline_security_check(Unit *u, static int offline_security_checks( char **filenames, - JsonVariant *policy, + sd_json_variant *policy, RuntimeScope scope, bool check_man, bool run_generators, @@ -2675,7 +2675,7 @@ static int offline_security_checks( const char *root, const char *profile, PagerFlags pager_flags, - JsonFormatFlags json_format_flags) { + sd_json_format_flags_t json_format_flags) { const ManagerTestRunFlags flags = MANAGER_TEST_RUN_MINIMAL | @@ -2771,7 +2771,7 @@ static int offline_security_checks( static int analyze_security(sd_bus *bus, char **units, - JsonVariant *policy, + sd_json_variant *policy, RuntimeScope scope, bool check_man, bool run_generators, @@ -2779,7 +2779,7 @@ static int analyze_security(sd_bus *bus, unsigned threshold, const char *root, const char *profile, - JsonFormatFlags json_format_flags, + sd_json_format_flags_t json_format_flags, PagerFlags pager_flags, AnalyzeSecurityFlags flags) { @@ -2897,7 +2897,7 @@ static int analyze_security(sd_bus *bus, int verb_security(int argc, char *argv[], void *userdata) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *policy = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *policy = NULL; int r; unsigned line, column; @@ -2910,7 +2910,7 @@ int verb_security(int argc, char *argv[], void *userdata) { pager_open(arg_pager_flags); if (arg_security_policy) { - r = json_parse_file(/*f=*/ NULL, arg_security_policy, /*flags=*/ 0, &policy, &line, &column); + r = sd_json_parse_file(/*f=*/ NULL, arg_security_policy, /*flags=*/ 0, &policy, &line, &column); if (r < 0) return log_error_errno(r, "Failed to parse '%s' at %u:%u: %m", arg_security_policy, line, column); } else { @@ -2922,7 +2922,7 @@ int verb_security(int argc, char *argv[], void *userdata) { return r; if (f) { - r = json_parse_file(f, pp, /*flags=*/ 0, &policy, &line, &column); + r = sd_json_parse_file(f, pp, /*flags=*/ 0, &policy, &line, &column); if (r < 0) return log_error_errno(r, "[%s:%u:%u] Failed to parse JSON policy: %m", pp, line, column); } diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index db3996faea..2b4babc5c9 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -109,7 +109,7 @@ unsigned arg_threshold = 100; unsigned arg_iterations = 1; usec_t arg_base_time = USEC_INFINITY; char *arg_unit = NULL; -JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; bool arg_quiet = false; char *arg_profile = NULL; bool arg_legend = true; @@ -566,7 +566,7 @@ static int parse_argv(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --offline= requires one or more units to perform a security review."); - if (arg_json_format_flags != JSON_FORMAT_OFF && !STRPTR_IN_SET(argv[optind], "security", "inspect-elf", "plot", "fdstore", "pcrs", "architectures", "capability", "exit-status")) + if (arg_json_format_flags != SD_JSON_FORMAT_OFF && !STRPTR_IN_SET(argv[optind], "security", "inspect-elf", "plot", "fdstore", "pcrs", "architectures", "capability", "exit-status")) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --json= is only supported for security, inspect-elf, plot, fdstore, pcrs, architectures, capability, exit-status right now."); @@ -605,13 +605,13 @@ static int parse_argv(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No conditions can be passed if --unit= is used."); if ((!arg_legend && !STRPTR_IN_SET(argv[optind], "plot", "architectures")) || - (streq_ptr(argv[optind], "plot") && !arg_legend && !arg_table && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))) + (streq_ptr(argv[optind], "plot") && !arg_legend && !arg_table && FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF))) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --no-legend is only supported for plot with either --table or --json=."); if (arg_table && !streq_ptr(argv[optind], "plot")) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --table is only supported for plot right now."); - if (arg_table && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (arg_table && !FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--table and --json= are mutually exclusive."); return 1; /* work to do */ diff --git a/src/analyze/analyze.h b/src/analyze/analyze.h index 8a9528c0d3..a6920b7653 100644 --- a/src/analyze/analyze.h +++ b/src/analyze/analyze.h @@ -3,9 +3,10 @@ #include <stdbool.h> +#include "sd-json.h" + #include "analyze-verify-util.h" #include "bus-util.h" -#include "json.h" #include "pager.h" #include "pretty-print.h" #include "time-util.h" @@ -35,7 +36,7 @@ extern unsigned arg_threshold; extern unsigned arg_iterations; extern usec_t arg_base_time; extern char *arg_unit; -extern JsonFormatFlags arg_json_format_flags; +extern sd_json_format_flags_t arg_json_format_flags; extern bool arg_quiet; extern char *arg_profile; extern bool arg_legend; diff --git a/src/boot/bootctl-reboot-to-firmware.c b/src/boot/bootctl-reboot-to-firmware.c index cdb04f8045..b2e20ad29a 100644 --- a/src/boot/bootctl-reboot-to-firmware.c +++ b/src/boot/bootctl-reboot-to-firmware.c @@ -38,9 +38,9 @@ int verb_reboot_to_firmware(int argc, char *argv[], void *userdata) { } } -int vl_method_set_reboot_to_firmware(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "state", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, 0, 0 }, +int vl_method_set_reboot_to_firmware(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "state", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, 0, 0 }, {} }; bool b; @@ -59,10 +59,10 @@ int vl_method_set_reboot_to_firmware(Varlink *link, JsonVariant *parameters, Var return varlink_reply(link, NULL); } -int vl_method_get_reboot_to_firmware(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +int vl_method_get_reboot_to_firmware(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { int r; - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); r = efi_get_reboot_to_firmware(); @@ -71,5 +71,5 @@ int vl_method_get_reboot_to_firmware(Varlink *link, JsonVariant *parameters, Var if (r < 0) return r; - return varlink_replyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_BOOLEAN("state", r))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_BOOLEAN("state", r))); } diff --git a/src/boot/bootctl-reboot-to-firmware.h b/src/boot/bootctl-reboot-to-firmware.h index fb8a2485b3..4fc3241343 100644 --- a/src/boot/bootctl-reboot-to-firmware.h +++ b/src/boot/bootctl-reboot-to-firmware.h @@ -4,5 +4,5 @@ int verb_reboot_to_firmware(int argc, char *argv[], void *userdata); -int vl_method_set_reboot_to_firmware(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); -int vl_method_get_reboot_to_firmware(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); +int vl_method_set_reboot_to_firmware(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); +int vl_method_get_reboot_to_firmware(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); diff --git a/src/boot/bootctl-status.c b/src/boot/bootctl-status.c index 224a1bf72d..b2bae93089 100644 --- a/src/boot/bootctl-status.c +++ b/src/boot/bootctl-status.c @@ -808,7 +808,7 @@ int verb_list(int argc, char *argv[], void *userdata) { if (r < 0) return r; - if (config.n_entries == 0 && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (config.n_entries == 0 && FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { log_info("No boot loader entries found."); return 0; } @@ -835,14 +835,14 @@ int verb_unlink(int argc, char *argv[], void *userdata) { return verb_list(argc, argv, userdata); } -int vl_method_list_boot_entries(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +int vl_method_list_boot_entries(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL; dev_t esp_devid = 0, xbootldr_devid = 0; int r; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); r = acquire_esp(/* unprivileged_mode= */ false, @@ -870,15 +870,15 @@ int vl_method_list_boot_entries(Varlink *link, JsonVariant *parameters, VarlinkM if (r < 0) return r; - _cleanup_(json_variant_unrefp) JsonVariant *previous = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *previous = NULL; for (size_t i = 0; i < config.n_entries; i++) { if (previous) { - r = varlink_notifyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("entry", previous))); + r = varlink_notifyb(link, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("entry", previous))); if (r < 0) return r; - previous = json_variant_unref(previous); + previous = sd_json_variant_unref(previous); } r = boot_entry_to_json(&config, i, &previous); @@ -886,6 +886,6 @@ int vl_method_list_boot_entries(Varlink *link, JsonVariant *parameters, VarlinkM return r; } - return varlink_replyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(previous, "entry", JSON_BUILD_VARIANT(previous)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(!!previous, "entry", SD_JSON_BUILD_VARIANT(previous)))); } diff --git a/src/boot/bootctl-status.h b/src/boot/bootctl-status.h index 6fd436513b..e276ba35eb 100644 --- a/src/boot/bootctl-status.h +++ b/src/boot/bootctl-status.h @@ -6,4 +6,4 @@ int verb_status(int argc, char *argv[], void *userdata); int verb_list(int argc, char *argv[], void *userdata); int verb_unlink(int argc, char *argv[], void *userdata); -int vl_method_list_boot_entries(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); +int vl_method_list_boot_entries(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index b883159dcf..10d72df011 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -47,7 +47,7 @@ sd_id128_t arg_machine_id = SD_ID128_NULL; char *arg_install_layout = NULL; BootEntryTokenType arg_entry_token_type = BOOT_ENTRY_TOKEN_AUTO; char *arg_entry_token = NULL; -JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; bool arg_arch_all = false; char *arg_root = NULL; char *arg_image = NULL; diff --git a/src/boot/bootctl.h b/src/boot/bootctl.h index 25cb5166ce..d488eac4e1 100644 --- a/src/boot/bootctl.h +++ b/src/boot/bootctl.h @@ -2,10 +2,10 @@ #pragma once #include "sd-id128.h" +#include "sd-json.h" #include "boot-entry.h" #include "image-policy.h" -#include "json.h" #include "pager.h" typedef enum InstallSource { @@ -28,7 +28,7 @@ extern sd_id128_t arg_machine_id; extern char *arg_install_layout; extern BootEntryTokenType arg_entry_token_type; extern char *arg_entry_token; -extern JsonFormatFlags arg_json_format_flags; +extern sd_json_format_flags_t arg_json_format_flags; extern bool arg_arch_all; extern char *arg_root; extern char *arg_image; diff --git a/src/boot/measure.c b/src/boot/measure.c index a6d27a712a..dca70d74a2 100644 --- a/src/boot/measure.c +++ b/src/boot/measure.c @@ -3,13 +3,14 @@ #include <getopt.h> #include <unistd.h> +#include "sd-json.h" + #include "alloc-util.h" #include "build.h" #include "efi-loader.h" #include "fd-util.h" #include "fileio.h" #include "hexdecoct.h" -#include "json.h" #include "main-func.h" #include "memstream-util.h" #include "openssl-util.h" @@ -17,6 +18,7 @@ #include "parse-util.h" #include "pretty-print.h" #include "sha256.h" +#include "strv.h" #include "terminal-util.h" #include "tpm2-pcr.h" #include "tpm2-util.h" @@ -34,7 +36,7 @@ static KeySourceType arg_private_key_source_type = OPENSSL_KEY_SOURCE_FILE; static char *arg_private_key_source = NULL; static char *arg_public_key = NULL; static char *arg_certificate = NULL; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO|JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO|SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static bool arg_current = false; static char **arg_phase = NULL; @@ -276,7 +278,7 @@ static int parse_argv(int argc, char *argv[]) { } case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; break; case ARG_JSON: @@ -676,7 +678,7 @@ static void pcr_states_restore(PcrState *pcr_states, size_t n) { } static int verb_calculate(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; _cleanup_(pcr_state_free_all) PcrState *pcr_states = NULL; int r; @@ -713,7 +715,7 @@ static int verb_calculate(int argc, char *argv[], void *userdata) { return r; for (size_t i = 0; i < n; i++) { - if (arg_json_format_flags & JSON_FORMAT_OFF) { + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) { _cleanup_free_ char *hd = NULL; if (i == 0) { @@ -732,20 +734,20 @@ static int verb_calculate(int argc, char *argv[], void *userdata) { printf("%i:%s=%s\n", TPM2_PCR_KERNEL_BOOT, pcr_states[i].bank, hd); } else { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; - array = json_variant_ref(json_variant_by_key(w, pcr_states[i].bank)); + array = sd_json_variant_ref(sd_json_variant_by_key(w, pcr_states[i].bank)); - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(!isempty(*phase), "phase", JSON_BUILD_STRING(*phase)), - JSON_BUILD_PAIR("pcr", JSON_BUILD_INTEGER(TPM2_PCR_KERNEL_BOOT)), - JSON_BUILD_PAIR("hash", JSON_BUILD_HEX(pcr_states[i].value, pcr_states[i].value_size)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(!isempty(*phase), "phase", SD_JSON_BUILD_STRING(*phase)), + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_INTEGER(TPM2_PCR_KERNEL_BOOT)), + SD_JSON_BUILD_PAIR("hash", SD_JSON_BUILD_HEX(pcr_states[i].value, pcr_states[i].value_size)))); if (r < 0) return log_error_errno(r, "Failed to append JSON object to array: %m"); - r = json_variant_set_field(&w, pcr_states[i].bank, array); + r = sd_json_variant_set_field(&w, pcr_states[i].bank, array); if (r < 0) return log_error_errno(r, "Failed to add bank info to object: %m"); } @@ -755,19 +757,19 @@ static int verb_calculate(int argc, char *argv[], void *userdata) { pcr_states_restore(pcr_states, n); } - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { - if (arg_json_format_flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (arg_json_format_flags & (SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) pager_open(arg_pager_flags); - json_variant_dump(w, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(w, arg_json_format_flags, stdout, NULL); } return 0; } static int verb_sign(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(pcr_state_free_all) PcrState *pcr_states = NULL; _cleanup_(EVP_PKEY_freep) EVP_PKEY *privkey = NULL, *pubkey = NULL; _cleanup_(X509_freep) X509 *certificate = NULL; @@ -786,17 +788,17 @@ static int verb_sign(int argc, char *argv[], void *userdata) { assert(!strv_isempty(arg_phase)); if (arg_append) { - r = json_parse_file(NULL, arg_append, 0, &v, NULL, NULL); + r = sd_json_parse_file(NULL, arg_append, 0, &v, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse '%s': %m", arg_append); - if (!json_variant_is_object(v)) + if (!sd_json_variant_is_object(v)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File '%s' is not a valid JSON object, refusing.", arg_append); } /* When signing we only support JSON output */ - arg_json_format_flags &= ~JSON_FORMAT_OFF; + arg_json_format_flags &= ~SD_JSON_FORMAT_OFF; /* This must be done before openssl_load_key_from_token() otherwise it will get stuck */ if (arg_certificate) { @@ -939,28 +941,28 @@ static int verb_sign(int argc, char *argv[], void *userdata) { if (r < 0) return r; - _cleanup_(json_variant_unrefp) JsonVariant *a = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL; r = tpm2_make_pcr_json_array(UINT64_C(1) << TPM2_PCR_KERNEL_BOOT, &a); if (r < 0) return log_error_errno(r, "Failed to build JSON PCR mask array: %m"); - _cleanup_(json_variant_unrefp) JsonVariant *bv = NULL; - r = json_build(&bv, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcrs", JSON_BUILD_VARIANT(a)), /* PCR mask */ - JSON_BUILD_PAIR("pkfp", JSON_BUILD_HEX(pubkey_fp, pubkey_fp_size)), /* SHA256 fingerprint of public key (DER) used for the signature */ - JSON_BUILD_PAIR("pol", JSON_BUILD_HEX(pcr_policy_digest.buffer, pcr_policy_digest.size)), /* TPM2 policy hash that is signed */ - JSON_BUILD_PAIR("sig", JSON_BUILD_BASE64(sig, ss)))); /* signature data */ + _cleanup_(sd_json_variant_unrefp) sd_json_variant *bv = NULL; + r = sd_json_build(&bv, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcrs", SD_JSON_BUILD_VARIANT(a)), /* PCR mask */ + SD_JSON_BUILD_PAIR("pkfp", SD_JSON_BUILD_HEX(pubkey_fp, pubkey_fp_size)), /* SHA256 fingerprint of public key (DER) used for the signature */ + SD_JSON_BUILD_PAIR("pol", SD_JSON_BUILD_HEX(pcr_policy_digest.buffer, pcr_policy_digest.size)), /* TPM2 policy hash that is signed */ + SD_JSON_BUILD_PAIR("sig", SD_JSON_BUILD_BASE64(sig, ss)))); /* signature data */ if (r < 0) return log_error_errno(r, "Failed to build JSON object: %m"); - _cleanup_(json_variant_unrefp) JsonVariant *av = NULL; - av = json_variant_ref(json_variant_by_key(v, p->bank)); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *av = NULL; + av = sd_json_variant_ref(sd_json_variant_by_key(v, p->bank)); - r = json_variant_append_array_nodup(&av, bv); + r = sd_json_variant_append_array_nodup(&av, bv); if (r < 0) return log_error_errno(r, "Failed to append JSON object: %m"); - r = json_variant_set_field(&v, p->bank, av); + r = sd_json_variant_set_field(&v, p->bank, av); if (r < 0) return log_error_errno(r, "Failed to add JSON field: %m"); } @@ -969,10 +971,10 @@ static int verb_sign(int argc, char *argv[], void *userdata) { pcr_states_restore(pcr_states, n); } - if (arg_json_format_flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (arg_json_format_flags & (SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) pager_open(arg_pager_flags); - json_variant_dump(v, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(v, arg_json_format_flags, stdout, NULL); return 0; } @@ -1057,7 +1059,7 @@ static int verb_status(int argc, char *argv[], void *userdata) { TPM2_PCR_SYSEXTS, }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; r = validate_stub(); @@ -1088,7 +1090,7 @@ static int verb_status(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to decode PCR value '%s': %m", s); - if (arg_json_format_flags & JSON_FORMAT_OFF) { + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) { _cleanup_free_ char *f = NULL; f = hexmem(h, l); @@ -1111,35 +1113,35 @@ static int verb_status(int argc, char *argv[], void *userdata) { printf("%" PRIu32 ":%s=%s\n", relevant_pcrs[i], b, f); } else { - _cleanup_(json_variant_unrefp) JsonVariant *bv = NULL, *a = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *bv = NULL, *a = NULL; - r = json_build(&bv, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_INTEGER(relevant_pcrs[i])), - JSON_BUILD_PAIR("hash", JSON_BUILD_HEX(h, l)) + r = sd_json_build(&bv, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_INTEGER(relevant_pcrs[i])), + SD_JSON_BUILD_PAIR("hash", SD_JSON_BUILD_HEX(h, l)) ) ); if (r < 0) return log_error_errno(r, "Failed to build JSON object: %m"); - a = json_variant_ref(json_variant_by_key(v, b)); + a = sd_json_variant_ref(sd_json_variant_by_key(v, b)); - r = json_variant_append_array(&a, bv); + r = sd_json_variant_append_array(&a, bv); if (r < 0) return log_error_errno(r, "Failed to append PCR entry to JSON array: %m"); - r = json_variant_set_field(&v, b, a); + r = sd_json_variant_set_field(&v, b, a); if (r < 0) return log_error_errno(r, "Failed to add bank info to object: %m"); } } } - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - if (arg_json_format_flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + if (arg_json_format_flags & (SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) pager_open(arg_pager_flags); - json_variant_dump(v, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(v, arg_json_format_flags, stdout, NULL); } return 0; diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 8db9076997..c06f7ec6a8 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -3,6 +3,7 @@ #include <getopt.h> #include "sd-bus.h" +#include "sd-json.h" #include "alloc-util.h" #include "build.h" @@ -19,7 +20,6 @@ #include "fileio.h" #include "format-table.h" #include "glyph-util.h" -#include "json.h" #include "log.h" #include "main-func.h" #include "memstream-util.h" @@ -38,7 +38,7 @@ #include "verbs.h" #include "version.h" -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; static int arg_full = -1; @@ -69,7 +69,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_matches, strv_freep); #define NAME_IS_ACQUIRED INT_TO_PTR(1) #define NAME_IS_ACTIVATABLE INT_TO_PTR(2) -static int json_transform_message(sd_bus_message *m, JsonVariant **ret); +static int json_transform_message(sd_bus_message *m, sd_json_variant **ret); static int acquire_bus(bool set_monitor, sd_bus **ret) { _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; @@ -1198,7 +1198,7 @@ static int message_pcap(sd_bus_message *m, FILE *f) { } static int message_json(sd_bus_message *m, FILE *f) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; char e[2]; int r; usec_t ts; @@ -1214,28 +1214,28 @@ static int message_json(sd_bus_message *m, FILE *f) { if (ts == 0) ts = now(CLOCK_REALTIME); - r = json_build(&w, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("type", JSON_BUILD_STRING(bus_message_type_to_string(m->header->type))), - JSON_BUILD_PAIR("endian", JSON_BUILD_STRING(e)), - JSON_BUILD_PAIR("flags", JSON_BUILD_INTEGER(m->header->flags)), - JSON_BUILD_PAIR("version", JSON_BUILD_INTEGER(m->header->version)), - JSON_BUILD_PAIR("cookie", JSON_BUILD_INTEGER(BUS_MESSAGE_COOKIE(m))), - JSON_BUILD_PAIR_CONDITION(m->reply_cookie != 0, "reply_cookie", JSON_BUILD_INTEGER(m->reply_cookie)), - JSON_BUILD_PAIR("timestamp-realtime", JSON_BUILD_UNSIGNED(ts)), - JSON_BUILD_PAIR_CONDITION(m->sender, "sender", JSON_BUILD_STRING(m->sender)), - JSON_BUILD_PAIR_CONDITION(m->destination, "destination", JSON_BUILD_STRING(m->destination)), - JSON_BUILD_PAIR_CONDITION(m->path, "path", JSON_BUILD_STRING(m->path)), - JSON_BUILD_PAIR_CONDITION(m->interface, "interface", JSON_BUILD_STRING(m->interface)), - JSON_BUILD_PAIR_CONDITION(m->member, "member", JSON_BUILD_STRING(m->member)), - JSON_BUILD_PAIR_CONDITION(m->monotonic != 0, "monotonic", JSON_BUILD_INTEGER(m->monotonic)), - JSON_BUILD_PAIR_CONDITION(m->realtime != 0, "realtime", JSON_BUILD_INTEGER(m->realtime)), - JSON_BUILD_PAIR_CONDITION(m->seqnum != 0, "seqnum", JSON_BUILD_INTEGER(m->seqnum)), - JSON_BUILD_PAIR_CONDITION(m->error.name, "error_name", JSON_BUILD_STRING(m->error.name)), - JSON_BUILD_PAIR("payload", JSON_BUILD_VARIANT(v)))); + r = sd_json_build(&w, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("type", SD_JSON_BUILD_STRING(bus_message_type_to_string(m->header->type))), + SD_JSON_BUILD_PAIR("endian", SD_JSON_BUILD_STRING(e)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_INTEGER(m->header->flags)), + SD_JSON_BUILD_PAIR("version", SD_JSON_BUILD_INTEGER(m->header->version)), + SD_JSON_BUILD_PAIR("cookie", SD_JSON_BUILD_INTEGER(BUS_MESSAGE_COOKIE(m))), + SD_JSON_BUILD_PAIR_CONDITION(m->reply_cookie != 0, "reply_cookie", SD_JSON_BUILD_INTEGER(m->reply_cookie)), + SD_JSON_BUILD_PAIR("timestamp-realtime", SD_JSON_BUILD_UNSIGNED(ts)), + SD_JSON_BUILD_PAIR_CONDITION(!!m->sender, "sender", SD_JSON_BUILD_STRING(m->sender)), + SD_JSON_BUILD_PAIR_CONDITION(!!m->destination, "destination", SD_JSON_BUILD_STRING(m->destination)), + SD_JSON_BUILD_PAIR_CONDITION(!!m->path, "path", SD_JSON_BUILD_STRING(m->path)), + SD_JSON_BUILD_PAIR_CONDITION(!!m->interface, "interface", SD_JSON_BUILD_STRING(m->interface)), + SD_JSON_BUILD_PAIR_CONDITION(!!m->member, "member", SD_JSON_BUILD_STRING(m->member)), + SD_JSON_BUILD_PAIR_CONDITION(m->monotonic != 0, "monotonic", SD_JSON_BUILD_INTEGER(m->monotonic)), + SD_JSON_BUILD_PAIR_CONDITION(m->realtime != 0, "realtime", SD_JSON_BUILD_INTEGER(m->realtime)), + SD_JSON_BUILD_PAIR_CONDITION(m->seqnum != 0, "seqnum", SD_JSON_BUILD_INTEGER(m->seqnum)), + SD_JSON_BUILD_PAIR_CONDITION(!!m->error.name, "error_name", SD_JSON_BUILD_STRING(m->error.name)), + SD_JSON_BUILD_PAIR("payload", SD_JSON_BUILD_VARIANT(v)))); if (r < 0) return log_error_errno(r, "Failed to build JSON object: %m"); - json_variant_dump(w, arg_json_format_flags, f, NULL); + sd_json_variant_dump(w, arg_json_format_flags, f, NULL); return 0; } @@ -1313,7 +1313,7 @@ static int monitor(int argc, char **argv, int (*dump)(sd_bus_message *m, FILE *f if (r < 0) return log_error_errno(r, "Failed to get unique name: %m"); - if (!arg_quiet && arg_json_format_flags == JSON_FORMAT_OFF) + if (!arg_quiet && arg_json_format_flags == SD_JSON_FORMAT_OFF) log_info("Monitoring bus message stream."); (void) sd_notify(/* unset_environment=false */ false, "READY=1"); @@ -1347,7 +1347,7 @@ static int monitor(int argc, char **argv, int (*dump)(sd_bus_message *m, FILE *f fflush(stdout); if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected") > 0) { - if (!arg_quiet && arg_json_format_flags == JSON_FORMAT_OFF) + if (!arg_quiet && arg_json_format_flags == SD_JSON_FORMAT_OFF) log_info("Connection terminated, exiting."); return 0; } @@ -1365,7 +1365,7 @@ static int monitor(int argc, char **argv, int (*dump)(sd_bus_message *m, FILE *f } static int verb_monitor(int argc, char **argv, void *userdata) { - return monitor(argc, argv, (arg_json_format_flags & JSON_FORMAT_OFF) ? message_dump : message_json); + return monitor(argc, argv, (arg_json_format_flags & SD_JSON_FORMAT_OFF) ? message_dump : message_json); } static int verb_capture(int argc, char **argv, void *userdata) { @@ -1681,10 +1681,10 @@ static int message_append_cmdline(sd_bus_message *m, const char *signature, char return 0; } -static int json_transform_one(sd_bus_message *m, JsonVariant **ret); +static int json_transform_one(sd_bus_message *m, sd_json_variant **ret); -static int json_transform_and_append(sd_bus_message *m, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *element = NULL; +static int json_transform_and_append(sd_bus_message *m, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *element = NULL; int r; assert(m); @@ -1694,11 +1694,11 @@ static int json_transform_and_append(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return r; - return json_variant_append_array(ret, element); + return sd_json_variant_append_array(ret, element); } -static int json_transform_array_or_struct(sd_bus_message *m, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int json_transform_array_or_struct(sd_bus_message *m, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; int r; assert(m); @@ -1717,14 +1717,14 @@ static int json_transform_array_or_struct(sd_bus_message *m, JsonVariant **ret) } if (!array) - return json_variant_new_array(ret, NULL, 0); + return sd_json_variant_new_array(ret, NULL, 0); *ret = TAKE_PTR(array); return 0; } -static int json_transform_variant(sd_bus_message *m, const char *contents, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *value = NULL; +static int json_transform_variant(sd_bus_message *m, const char *contents, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *value = NULL; int r; assert(m); @@ -1735,23 +1735,23 @@ static int json_transform_variant(sd_bus_message *m, const char *contents, JsonV if (r < 0) return r; - r = json_build(ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("type", JSON_BUILD_STRING(contents)), - JSON_BUILD_PAIR("data", JSON_BUILD_VARIANT(value)))); + r = sd_json_build(ret, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("type", SD_JSON_BUILD_STRING(contents)), + SD_JSON_BUILD_PAIR("data", SD_JSON_BUILD_VARIANT(value)))); if (r < 0) return log_oom(); return r; } -static int json_transform_dict_array(sd_bus_message *m, JsonVariant **ret) { - JsonVariant **elements = NULL; +static int json_transform_dict_array(sd_bus_message *m, sd_json_variant **ret) { + sd_json_variant **elements = NULL; size_t n_elements = 0; int r; assert(m); assert(ret); - CLEANUP_ARRAY(elements, n_elements, json_variant_unref_many); + CLEANUP_ARRAY(elements, n_elements, sd_json_variant_unref_many); for (;;) { const char *contents; @@ -1793,11 +1793,11 @@ static int json_transform_dict_array(sd_bus_message *m, JsonVariant **ret) { return bus_log_parse_error(r); } - return json_variant_new_object(ret, elements, n_elements); + return sd_json_variant_new_object(ret, elements, n_elements); } -static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +static int json_transform_one(sd_bus_message *m, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *contents; char type; int r; @@ -1818,7 +1818,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_unsigned(&v, b); + r = sd_json_variant_new_unsigned(&v, b); if (r < 0) return log_error_errno(r, "Failed to transform byte: %m"); @@ -1832,7 +1832,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_boolean(&v, b); + r = sd_json_variant_new_boolean(&v, b); if (r < 0) return log_error_errno(r, "Failed to transform boolean: %m"); @@ -1846,7 +1846,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_integer(&v, b); + r = sd_json_variant_new_integer(&v, b); if (r < 0) return log_error_errno(r, "Failed to transform int16: %m"); @@ -1860,7 +1860,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_unsigned(&v, b); + r = sd_json_variant_new_unsigned(&v, b); if (r < 0) return log_error_errno(r, "Failed to transform uint16: %m"); @@ -1874,7 +1874,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_integer(&v, b); + r = sd_json_variant_new_integer(&v, b); if (r < 0) return log_error_errno(r, "Failed to transform int32: %m"); @@ -1888,7 +1888,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_unsigned(&v, b); + r = sd_json_variant_new_unsigned(&v, b); if (r < 0) return log_error_errno(r, "Failed to transform uint32: %m"); @@ -1902,7 +1902,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_integer(&v, b); + r = sd_json_variant_new_integer(&v, b); if (r < 0) return log_error_errno(r, "Failed to transform int64: %m"); @@ -1916,7 +1916,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_unsigned(&v, b); + r = sd_json_variant_new_unsigned(&v, b); if (r < 0) return log_error_errno(r, "Failed to transform uint64: %m"); @@ -1930,7 +1930,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_real(&v, d); + r = sd_json_variant_new_real(&v, d); if (r < 0) return log_error_errno(r, "Failed to transform double: %m"); @@ -1946,7 +1946,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_string(&v, s); + r = sd_json_variant_new_string(&v, s); if (r < 0) return log_error_errno(r, "Failed to transform double: %m"); @@ -1958,7 +1958,7 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_variant_new_null(&v); + r = sd_json_variant_new_null(&v); if (r < 0) return log_error_errno(r, "Failed to transform fd: %m"); @@ -1994,8 +1994,8 @@ static int json_transform_one(sd_bus_message *m, JsonVariant **ret) { return 0; } -static int json_transform_message(sd_bus_message *m, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +static int json_transform_message(sd_bus_message *m, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *type; int r; @@ -2008,8 +2008,8 @@ static int json_transform_message(sd_bus_message *m, JsonVariant **ret) { if (r < 0) return r; - r = json_build(ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("type", JSON_BUILD_STRING(type)), - JSON_BUILD_PAIR("data", JSON_BUILD_VARIANT(v)))); + r = sd_json_build(ret, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("type", SD_JSON_BUILD_STRING(type)), + SD_JSON_BUILD_PAIR("data", SD_JSON_BUILD_VARIANT(v)))); if (r < 0) return log_oom(); @@ -2083,17 +2083,17 @@ static int call(int argc, char **argv, void *userdata) { if (r == 0 && !arg_quiet) { - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - if (arg_json_format_flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (arg_json_format_flags & (SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) pager_open(arg_pager_flags); r = json_transform_message(reply, &v); if (r < 0) return r; - json_variant_dump(v, arg_json_format_flags, NULL, NULL); + sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); } else if (arg_verbose) { pager_open(arg_pager_flags); @@ -2191,17 +2191,17 @@ static int get_property(int argc, char **argv, void *userdata) { if (r < 0) return bus_log_parse_error(r); - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - if (arg_json_format_flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (arg_json_format_flags & (SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) pager_open(arg_pager_flags); r = json_transform_variant(reply, contents, &v); if (r < 0) return r; - json_variant_dump(v, arg_json_format_flags, NULL, NULL); + sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); } else if (arg_verbose) { pager_open(arg_pager_flags); @@ -2558,7 +2558,7 @@ static int parse_argv(int argc, char *argv[]) { break; case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; break; case ARG_JSON: diff --git a/src/core/core-varlink.c b/src/core/core-varlink.c index 3e6168d912..36d5e5a593 100644 --- a/src/core/core-varlink.c +++ b/src/core/core-varlink.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "core-varlink.h" +#include "json-util.h" #include "mkdir-label.h" #include "strv.h" #include "user-util.h" @@ -23,22 +24,22 @@ static const char* const managed_oom_mode_properties[] = { "ManagedOOMMemoryPressure", }; -static int build_user_json(const char *user_name, uid_t uid, JsonVariant **ret) { +static int build_user_json(const char *user_name, uid_t uid, sd_json_variant **ret) { assert(user_name); assert(uid_is_valid(uid)); assert(ret); - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(user_name)), - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(uid)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(uid)), - JSON_BUILD_PAIR("realName", JSON_BUILD_CONST_STRING("Dynamic User")), - JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/")), - JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN)), - JSON_BUILD_PAIR("locked", JSON_BUILD_BOOLEAN(true)), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.DynamicUser")), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("dynamic")))))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(user_name)), + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(uid)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(uid)), + SD_JSON_BUILD_PAIR("realName", JSON_BUILD_CONST_STRING("Dynamic User")), + SD_JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/")), + SD_JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN)), + SD_JSON_BUILD_PAIR("locked", SD_JSON_BUILD_BOOLEAN(true)), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.DynamicUser")), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("dynamic")))))); } static bool user_match_lookup_parameters(LookupParameters *p, const char *name, uid_t uid) { @@ -53,7 +54,7 @@ static bool user_match_lookup_parameters(LookupParameters *p, const char *name, return true; } -static int build_managed_oom_json_array_element(Unit *u, const char *property, JsonVariant **ret_v) { +static int build_managed_oom_json_array_element(Unit *u, const char *property, sd_json_variant **ret_v) { bool use_limit = false; CGroupContext *c; const char *mode; @@ -85,15 +86,15 @@ static int build_managed_oom_json_array_element(Unit *u, const char *property, J } else return -EINVAL; - return json_build(ret_v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("mode", JSON_BUILD_STRING(mode)), - JSON_BUILD_PAIR("path", JSON_BUILD_STRING(crt->cgroup_path)), - JSON_BUILD_PAIR("property", JSON_BUILD_STRING(property)), - JSON_BUILD_PAIR_CONDITION(use_limit, "limit", JSON_BUILD_UNSIGNED(c->moom_mem_pressure_limit)))); + return sd_json_build(ret_v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("mode", SD_JSON_BUILD_STRING(mode)), + SD_JSON_BUILD_PAIR("path", SD_JSON_BUILD_STRING(crt->cgroup_path)), + SD_JSON_BUILD_PAIR("property", SD_JSON_BUILD_STRING(property)), + SD_JSON_BUILD_PAIR_CONDITION(use_limit, "limit", SD_JSON_BUILD_UNSIGNED(c->moom_mem_pressure_limit)))); } int manager_varlink_send_managed_oom_update(Unit *u) { - _cleanup_(json_variant_unrefp) JsonVariant *arr = NULL, *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *arr = NULL, *v = NULL; CGroupRuntime *crt; CGroupContext *c; int r; @@ -124,23 +125,23 @@ int manager_varlink_send_managed_oom_update(Unit *u) { if (!c) return 0; - r = json_build(&arr, JSON_BUILD_EMPTY_ARRAY); + r = sd_json_build(&arr, SD_JSON_BUILD_EMPTY_ARRAY); if (r < 0) return r; FOREACH_ELEMENT(i, managed_oom_mode_properties) { - _cleanup_(json_variant_unrefp) JsonVariant *e = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL; r = build_managed_oom_json_array_element(u, *i, &e); if (r < 0) return r; - r = json_variant_append_array(&arr, e); + r = sd_json_variant_append_array(&arr, e); if (r < 0) return r; } - r = json_build(&v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("cgroups", JSON_BUILD_VARIANT(arr)))); + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("cgroups", SD_JSON_BUILD_VARIANT(arr)))); if (r < 0) return r; @@ -156,14 +157,14 @@ int manager_varlink_send_managed_oom_update(Unit *u) { return r; } -static int build_managed_oom_cgroups_json(Manager *m, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *arr = NULL; +static int build_managed_oom_cgroups_json(Manager *m, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *arr = NULL; int r; assert(m); assert(ret); - r = json_build(&arr, JSON_BUILD_EMPTY_ARRAY); + r = sd_json_build(&arr, SD_JSON_BUILD_EMPTY_ARRAY); if (r < 0) return r; @@ -183,7 +184,7 @@ static int build_managed_oom_cgroups_json(Manager *m, JsonVariant **ret) { continue; FOREACH_ELEMENT(i, managed_oom_mode_properties) { - _cleanup_(json_variant_unrefp) JsonVariant *e = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL; /* For the initial varlink call we only care about units that enabled (i.e. mode is not * set to "auto") oomd properties. */ @@ -195,14 +196,14 @@ static int build_managed_oom_cgroups_json(Manager *m, JsonVariant **ret) { if (r < 0) return r; - r = json_variant_append_array(&arr, e); + r = sd_json_variant_append_array(&arr, e); if (r < 0) return r; } } } - r = json_build(&v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("cgroups", JSON_BUILD_VARIANT(arr)))); + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("cgroups", SD_JSON_BUILD_VARIANT(arr)))); if (r < 0) return r; @@ -212,11 +213,11 @@ static int build_managed_oom_cgroups_json(Manager *m, JsonVariant **ret) { static int vl_method_subscribe_managed_oom_cgroups( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; Manager *m = ASSERT_PTR(userdata); pid_t pid; Unit *u; @@ -237,7 +238,7 @@ static int vl_method_subscribe_managed_oom_cgroups( if (!streq(u->id, "systemd-oomd.service")) return varlink_error(link, VARLINK_ERROR_PERMISSION_DENIED, NULL); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); /* We only take one subscriber for this method so return an error if there's already an existing one. @@ -258,7 +259,7 @@ static int vl_method_subscribe_managed_oom_cgroups( } static int manager_varlink_send_managed_oom_initial(Manager *m) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(m); @@ -275,16 +276,16 @@ static int manager_varlink_send_managed_oom_initial(Manager *m) { return varlink_send(m->managed_oom_varlink, "io.systemd.oom.ReportManagedOOMCGroups", v); } -static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; LookupParameters p = { .uid = UID_INVALID, }; @@ -325,7 +326,7 @@ static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, Var if (r < 0) return r; - v = json_variant_unref(v); + v = sd_json_variant_unref(v); } r = build_user_json(d->name, uid, &v); @@ -356,18 +357,18 @@ static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, Var return varlink_reply(link, v); } -static int build_group_json(const char *group_name, gid_t gid, JsonVariant **ret) { +static int build_group_json(const char *group_name, gid_t gid, sd_json_variant **ret) { assert(group_name); assert(gid_is_valid(gid)); assert(ret); - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(group_name)), - JSON_BUILD_PAIR("description", JSON_BUILD_CONST_STRING("Dynamic Group")), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(gid)), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.DynamicUser")), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("dynamic")))))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(group_name)), + SD_JSON_BUILD_PAIR("description", JSON_BUILD_CONST_STRING("Dynamic Group")), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(gid)), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.DynamicUser")), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("dynamic")))))); } static bool group_match_lookup_parameters(LookupParameters *p, const char *name, gid_t gid) { @@ -382,16 +383,16 @@ static bool group_match_lookup_parameters(LookupParameters *p, const char *name, return true; } -static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; LookupParameters p = { .gid = GID_INVALID, }; @@ -434,7 +435,7 @@ static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, Va if (r < 0) return r; - v = json_variant_unref(v); + v = sd_json_variant_unref(v); } r = build_group_json(d->name, (gid_t) uid, &v); @@ -465,12 +466,12 @@ static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, Va return varlink_reply(link, v); } -static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), SD_JSON_SAFE }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; @@ -581,7 +582,7 @@ static int manager_varlink_init_system(Manager *m) { return 1; } -static int vl_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { +static int vl_reply(Varlink *link, sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { Manager *m = ASSERT_PTR(userdata); int r; diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index a8ee8e05de..9a1f3c23fe 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -10,6 +10,7 @@ #include "sd-daemon.h" #include "sd-journal.h" +#include "sd-json.h" #include "sd-login.h" #include "sd-messages.h" @@ -32,6 +33,7 @@ #include "iovec-util.h" #include "journal-importer.h" #include "journal-send.h" +#include "json-util.h" #include "log.h" #include "macro.h" #include "main-func.h" @@ -773,14 +775,14 @@ static int submit_coredump( struct iovec_wrapper *iovw, int input_fd) { - _cleanup_(json_variant_unrefp) JsonVariant *json_metadata = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *json_metadata = NULL; _cleanup_close_ int coredump_fd = -EBADF, coredump_node_fd = -EBADF; _cleanup_free_ char *filename = NULL, *coredump_data = NULL; _cleanup_free_ char *stacktrace = NULL; const char *module_name; uint64_t coredump_size = UINT64_MAX, coredump_compressed_size = UINT64_MAX; bool truncated = false, written = false; - JsonVariant *module_json; + sd_json_variant *module_json; int r; assert(context); @@ -870,7 +872,7 @@ static int submit_coredump( if (json_metadata) { _cleanup_free_ char *formatted_json = NULL; - r = json_variant_format(json_metadata, 0, &formatted_json); + r = sd_json_variant_format(json_metadata, 0, &formatted_json); if (r < 0) return log_error_errno(r, "Failed to format JSON package metadata: %m"); @@ -881,19 +883,19 @@ static int submit_coredump( * let's avoid guessing the module name and skip the loop. */ if (context->meta[META_EXE]) JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, json_metadata) { - JsonVariant *t; + sd_json_variant *t; /* We only add structured fields for the 'main' ELF module, and only if we can identify it. */ if (!path_equal_filename(module_name, context->meta[META_EXE])) continue; - t = json_variant_by_key(module_json, "name"); + t = sd_json_variant_by_key(module_json, "name"); if (t) - (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_NAME=", json_variant_string(t)); + (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_NAME=", sd_json_variant_string(t)); - t = json_variant_by_key(module_json, "version"); + t = sd_json_variant_by_key(module_json, "version"); if (t) - (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_VERSION=", json_variant_string(t)); + (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_VERSION=", sd_json_variant_string(t)); } /* Optionally store the entire coredump in the journal */ diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 3a3cd7dca2..8de86a7897 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -8,6 +8,7 @@ #include "sd-bus.h" #include "sd-journal.h" +#include "sd-json.h" #include "sd-messages.h" #include "alloc-util.h" @@ -26,6 +27,7 @@ #include "glob-util.h" #include "journal-internal.h" #include "journal-util.h" +#include "json-util.h" #include "log.h" #include "macro.h" #include "main-func.h" @@ -56,7 +58,7 @@ static const char *arg_directory = NULL; static char *arg_root = NULL; static char *arg_image = NULL; static char **arg_file = NULL; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static int arg_legend = true; static size_t arg_rows_max = SIZE_MAX; @@ -804,26 +806,26 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { /* Print out the build-id of the 'main' ELF module, by matching the JSON key * with the 'exe' field. */ if (exe && pkgmeta_json) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - r = json_parse(pkgmeta_json, 0, &v, NULL, NULL); + r = sd_json_parse(pkgmeta_json, 0, &v, NULL, NULL); if (r < 0) { _cleanup_free_ char *esc = cescape(pkgmeta_json); log_warning_errno(r, "json_parse on \"%s\" failed, ignoring: %m", strnull(esc)); } else { const char *module_name; - JsonVariant *module_json; + sd_json_variant *module_json; JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, v) { - JsonVariant *build_id; + sd_json_variant *build_id; /* We only print the build-id for the 'main' ELF module */ if (!path_equal_filename(module_name, exe)) continue; - build_id = json_variant_by_key(module_json, "buildId"); + build_id = sd_json_variant_by_key(module_json, "buildId"); if (build_id) - fprintf(file, " build-id: %s\n", json_variant_string(build_id)); + fprintf(file, " build-id: %s\n", sd_json_variant_string(build_id)); break; } diff --git a/src/creds/creds.c b/src/creds/creds.c index 0a26429e31..0cff4d8890 100644 --- a/src/creds/creds.c +++ b/src/creds/creds.c @@ -3,6 +3,8 @@ #include <getopt.h> #include <unistd.h> +#include "sd-json.h" + #include "build.h" #include "bus-polkit.h" #include "creds-util.h" @@ -12,7 +14,7 @@ #include "format-table.h" #include "hexdecoct.h" #include "io-util.h" -#include "json.h" +#include "json-util.h" #include "libmount-util.h" #include "main-func.h" #include "memory-util.h" @@ -41,7 +43,7 @@ typedef enum TranscodeMode { _TRANSCODE_INVALID = -EINVAL, } TranscodeMode; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; static bool arg_system = false; @@ -271,7 +273,7 @@ static int verb_list(int argc, char **argv, void *userdata) { return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No credentials passed. (i.e. $CREDENTIALS_DIRECTORY not set.)"); } - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_isempty(t)) { + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF) && table_isempty(t)) { log_info("No credentials"); return 0; } @@ -368,19 +370,19 @@ static int write_blob(FILE *f, const void *data, size_t size) { int r; if (arg_transcode == TRANSCODE_OFF && - arg_json_format_flags != JSON_FORMAT_OFF) { + arg_json_format_flags != SD_JSON_FORMAT_OFF) { _cleanup_(erase_and_freep) char *suffixed = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = make_cstring(data, size, MAKE_CSTRING_REFUSE_TRAILING_NUL, &suffixed); if (r < 0) return log_error_errno(r, "Unable to convert binary string to C string: %m"); - r = json_parse(suffixed, JSON_PARSE_SENSITIVE, &v, NULL, NULL); + r = sd_json_parse(suffixed, SD_JSON_PARSE_SENSITIVE, &v, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse JSON: %m"); - json_variant_dump(v, arg_json_format_flags, f, NULL); + sd_json_variant_dump(v, arg_json_format_flags, f, NULL); return 0; } @@ -1167,16 +1169,16 @@ static int settle_scope( return 0; } -static int vl_method_encrypt(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_encrypt(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "name", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(MethodEncryptParameters, name), 0 }, - { "text", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(MethodEncryptParameters, text), 0 }, - { "data", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(MethodEncryptParameters, data), 0 }, - { "timestamp", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(MethodEncryptParameters, timestamp), 0 }, - { "notAfter", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(MethodEncryptParameters, not_after), 0 }, - { "scope", JSON_VARIANT_STRING, dispatch_credential_scope, offsetof(MethodEncryptParameters, scope), 0 }, - { "uid", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uid_gid, offsetof(MethodEncryptParameters, uid), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodEncryptParameters, name), 0 }, + { "text", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodEncryptParameters, text), 0 }, + { "data", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(MethodEncryptParameters, data), 0 }, + { "timestamp", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(MethodEncryptParameters, timestamp), 0 }, + { "notAfter", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(MethodEncryptParameters, not_after), 0 }, + { "scope", SD_JSON_VARIANT_STRING, dispatch_credential_scope, offsetof(MethodEncryptParameters, scope), 0 }, + { "uid", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid, offsetof(MethodEncryptParameters, uid), 0 }, VARLINK_DISPATCH_POLKIT_FIELD, {} }; @@ -1253,14 +1255,14 @@ static int vl_method_encrypt(Varlink *link, JsonVariant *parameters, VarlinkMeth if (r < 0) return r; - _cleanup_(json_variant_unrefp) JsonVariant *reply = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *reply = NULL; - r = json_build(&reply, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_IOVEC_BASE64("blob", &output))); + r = sd_json_build(&reply, SD_JSON_BUILD_OBJECT(JSON_BUILD_PAIR_IOVEC_BASE64("blob", &output))); if (r < 0) return r; /* Let's also mark the (theoretically encrypted) reply as sensitive, in case the NULL encryption scheme was used. */ - json_variant_sensitive(reply); + sd_json_variant_sensitive(reply); return varlink_reply(link, reply); } @@ -1279,14 +1281,14 @@ static void method_decrypt_parameters_done(MethodDecryptParameters *p) { iovec_done_erase(&p->blob); } -static int vl_method_decrypt(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_decrypt(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "name", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(MethodDecryptParameters, name), 0 }, - { "blob", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(MethodDecryptParameters, blob), JSON_MANDATORY }, - { "timestamp", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(MethodDecryptParameters, timestamp), 0 }, - { "scope", JSON_VARIANT_STRING, dispatch_credential_scope, offsetof(MethodDecryptParameters, scope), 0 }, - { "uid", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uid_gid, offsetof(MethodDecryptParameters, uid), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodDecryptParameters, name), 0 }, + { "blob", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(MethodDecryptParameters, blob), SD_JSON_MANDATORY }, + { "timestamp", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(MethodDecryptParameters, timestamp), 0 }, + { "scope", SD_JSON_VARIANT_STRING, dispatch_credential_scope, offsetof(MethodDecryptParameters, scope), 0 }, + { "uid", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid, offsetof(MethodDecryptParameters, uid), 0 }, VARLINK_DISPATCH_POLKIT_FIELD, {} }; @@ -1373,13 +1375,13 @@ static int vl_method_decrypt(Varlink *link, JsonVariant *parameters, VarlinkMeth if (r < 0) return r; - _cleanup_(json_variant_unrefp) JsonVariant *reply = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *reply = NULL; - r = json_build(&reply, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_IOVEC_BASE64("data", &output))); + r = sd_json_build(&reply, SD_JSON_BUILD_OBJECT(JSON_BUILD_PAIR_IOVEC_BASE64("data", &output))); if (r < 0) return r; - json_variant_sensitive(reply); + sd_json_variant_sensitive(reply); return varlink_reply(link, reply); } diff --git a/src/cryptenroll/cryptenroll-fido2.c b/src/cryptenroll/cryptenroll-fido2.c index baa630a6b3..0baf276878 100644 --- a/src/cryptenroll/cryptenroll-fido2.c +++ b/src/cryptenroll/cryptenroll-fido2.c @@ -4,7 +4,7 @@ #include "cryptenroll-fido2.h" #include "cryptsetup-fido2.h" #include "hexdecoct.h" -#include "json.h" +#include "json-util.h" #include "libfido2-util.h" #include "memory-util.h" #include "random-util.h" @@ -71,7 +71,7 @@ int enroll_fido2( _cleanup_(erase_and_freep) void *salt = NULL, *secret = NULL; _cleanup_(erase_and_freep) char *base64_encoded = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *keyslot_as_string = NULL; size_t cid_size, salt_size, secret_size; _cleanup_free_ void *cid = NULL; @@ -130,16 +130,16 @@ int enroll_fido2( if (asprintf(&keyslot_as_string, "%i", keyslot) < 0) return log_oom(); - r = json_build(&v, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-fido2")), - JSON_BUILD_PAIR("keyslots", JSON_BUILD_ARRAY(JSON_BUILD_STRING(keyslot_as_string))), - JSON_BUILD_PAIR("fido2-credential", JSON_BUILD_BASE64(cid, cid_size)), - JSON_BUILD_PAIR("fido2-salt", JSON_BUILD_BASE64(salt, salt_size)), - JSON_BUILD_PAIR("fido2-rp", JSON_BUILD_CONST_STRING("io.systemd.cryptsetup")), - JSON_BUILD_PAIR("fido2-clientPin-required", JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_PIN))), - JSON_BUILD_PAIR("fido2-up-required", JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_UP))), - JSON_BUILD_PAIR("fido2-uv-required", JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_UV))))); + r = sd_json_build(&v, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-fido2")), + SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING(keyslot_as_string))), + SD_JSON_BUILD_PAIR("fido2-credential", SD_JSON_BUILD_BASE64(cid, cid_size)), + SD_JSON_BUILD_PAIR("fido2-salt", SD_JSON_BUILD_BASE64(salt, salt_size)), + SD_JSON_BUILD_PAIR("fido2-rp", JSON_BUILD_CONST_STRING("io.systemd.cryptsetup")), + SD_JSON_BUILD_PAIR("fido2-clientPin-required", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_PIN))), + SD_JSON_BUILD_PAIR("fido2-up-required", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_UP))), + SD_JSON_BUILD_PAIR("fido2-uv-required", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_UV))))); if (r < 0) return log_error_errno(r, "Failed to prepare FIDO2 JSON token object: %m"); diff --git a/src/cryptenroll/cryptenroll-list.c b/src/cryptenroll/cryptenroll-list.c index 00a1a8e637..ffc1067d2d 100644 --- a/src/cryptenroll/cryptenroll-list.c +++ b/src/cryptenroll/cryptenroll-list.c @@ -1,8 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "cryptenroll-list.h" #include "cryptenroll.h" #include "format-table.h" +#include "json-util.h" #include "parse-util.h" struct keyslot_metadata { @@ -39,9 +42,9 @@ int list_enrolled(struct crypt_device *cd) { /* Second step, enumerate through all tokens, and update the slot table, indicating what kind of * token they are assigned to */ for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *type; - JsonVariant *w, *z; + sd_json_variant *w, *z; EnrollType et; r = cryptsetup_get_token_as_json(cd, token, NULL, &v); @@ -52,20 +55,20 @@ int list_enrolled(struct crypt_device *cd) { continue; } - w = json_variant_by_key(v, "type"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "type"); + if (!w || !sd_json_variant_is_string(w)) { log_warning("Token JSON data lacks type field, ignoring."); continue; } - et = luks2_token_type_from_string(json_variant_string(w)); + et = luks2_token_type_from_string(sd_json_variant_string(w)); if (et < 0) type = "other"; else type = enroll_type_to_string(et); - w = json_variant_by_key(v, "keyslots"); - if (!w || !json_variant_is_array(w)) { + w = sd_json_variant_by_key(v, "keyslots"); + if (!w || !sd_json_variant_is_array(w)) { log_warning("Token JSON data lacks keyslots field, ignoring."); continue; } @@ -73,12 +76,12 @@ int list_enrolled(struct crypt_device *cd) { JSON_VARIANT_ARRAY_FOREACH(z, w) { unsigned u; - if (!json_variant_is_string(z)) { + if (!sd_json_variant_is_string(z)) { log_warning("Token JSON data's keyslot field is not an array of strings, ignoring."); continue; } - r = safe_atou(json_variant_string(z), &u); + r = safe_atou(sd_json_variant_string(z), &u); if (r < 0) { log_warning_errno(r, "Token JSON data's keyslot field is not an integer formatted as string, ignoring."); continue; diff --git a/src/cryptenroll/cryptenroll-pkcs11.c b/src/cryptenroll/cryptenroll-pkcs11.c index 1e4be008e3..f7cf700084 100644 --- a/src/cryptenroll/cryptenroll-pkcs11.c +++ b/src/cryptenroll/cryptenroll-pkcs11.c @@ -2,7 +2,7 @@ #include "cryptenroll-pkcs11.h" #include "hexdecoct.h" -#include "json.h" +#include "json-util.h" #include "memory-util.h" #include "openssl-util.h" #include "pkcs11-util.h" @@ -39,7 +39,7 @@ int enroll_pkcs11( _cleanup_(erase_and_freep) void *decrypted_key = NULL; _cleanup_(erase_and_freep) char *base64_encoded = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *keyslot_as_string = NULL, *private_uri = NULL; size_t decrypted_key_size, saved_key_size; _cleanup_free_ void *saved_key = NULL; @@ -100,12 +100,12 @@ int enroll_pkcs11( if (r < 0) return r; - r = json_build(&v, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-pkcs11")), - JSON_BUILD_PAIR("keyslots", JSON_BUILD_ARRAY(JSON_BUILD_STRING(keyslot_as_string))), - JSON_BUILD_PAIR("pkcs11-uri", JSON_BUILD_STRING(private_uri ?: uri)), - JSON_BUILD_PAIR("pkcs11-key", JSON_BUILD_BASE64(saved_key, saved_key_size)))); + r = sd_json_build(&v, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-pkcs11")), + SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING(keyslot_as_string))), + SD_JSON_BUILD_PAIR("pkcs11-uri", SD_JSON_BUILD_STRING(private_uri ?: uri)), + SD_JSON_BUILD_PAIR("pkcs11-key", SD_JSON_BUILD_BASE64(saved_key, saved_key_size)))); if (r < 0) return log_error_errno(r, "Failed to prepare PKCS#11 JSON token object: %m"); diff --git a/src/cryptenroll/cryptenroll-recovery.c b/src/cryptenroll/cryptenroll-recovery.c index 7c170f2850..6b42a54c8a 100644 --- a/src/cryptenroll/cryptenroll-recovery.c +++ b/src/cryptenroll/cryptenroll-recovery.c @@ -2,7 +2,7 @@ #include "cryptenroll-recovery.h" #include "glyph-util.h" -#include "json.h" +#include "json-util.h" #include "memory-util.h" #include "qrcode-util.h" #include "recovery-key.h" @@ -13,7 +13,7 @@ int enroll_recovery( const void *volume_key, size_t volume_key_size) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(erase_and_freep) char *password = NULL; _cleanup_free_ char *keyslot_as_string = NULL; int keyslot, r, q; @@ -74,10 +74,10 @@ int enroll_recovery( goto rollback; } - r = json_build(&v, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-recovery")), - JSON_BUILD_PAIR("keyslots", JSON_BUILD_ARRAY(JSON_BUILD_STRING(keyslot_as_string))))); + r = sd_json_build(&v, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-recovery")), + SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING(keyslot_as_string))))); if (r < 0) { log_error_errno(r, "Failed to prepare recovery key JSON token object: %m"); goto rollback; diff --git a/src/cryptenroll/cryptenroll-tpm2.c b/src/cryptenroll/cryptenroll-tpm2.c index 1ee3525a81..85eec177ad 100644 --- a/src/cryptenroll/cryptenroll-tpm2.c +++ b/src/cryptenroll/cryptenroll-tpm2.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "alloc-util.h" #include "ask-password-api.h" #include "cryptenroll-tpm2.h" @@ -8,7 +10,6 @@ #include "errno-util.h" #include "fileio.h" #include "hexdecoct.h" -#include "json.h" #include "log.h" #include "memory-util.h" #include "random-util.h" @@ -29,11 +30,11 @@ static int search_policy_hash( return 0; for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ void *thash = NULL; size_t thash_size = 0; int keyslot; - JsonVariant *w; + sd_json_variant *w; r = cryptsetup_get_token_as_json(cd, token, "systemd-tpm2", &v); if (IN_SET(r, -ENOENT, -EINVAL, -EMEDIUMTYPE)) @@ -49,12 +50,12 @@ static int search_policy_hash( continue; } - w = json_variant_by_key(v, "tpm2-policy-hash"); - if (!w || !json_variant_is_string(w)) + w = sd_json_variant_by_key(v, "tpm2-policy-hash"); + if (!w || !sd_json_variant_is_string(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 token data lacks 'tpm2-policy-hash' field."); - r = unhexmem(json_variant_string(w), &thash, &thash_size); + r = unhexmem(sd_json_variant_string(w), &thash, &thash_size); if (r < 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid base64 data in 'tpm2-policy-hash' field."); @@ -257,7 +258,7 @@ int enroll_tpm2(struct crypt_device *cd, const char *pcrlock_path, int *ret_slot_to_wipe) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *signature_json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *signature_json = NULL; _cleanup_(erase_and_freep) char *base64_encoded = NULL; _cleanup_(iovec_done) struct iovec srk = {}, blob = {}, pubkey = {}; _cleanup_(iovec_done_erase) struct iovec secret = {}; diff --git a/src/cryptenroll/cryptenroll-wipe.c b/src/cryptenroll/cryptenroll-wipe.c index 314ebd3113..703342f758 100644 --- a/src/cryptenroll/cryptenroll-wipe.c +++ b/src/cryptenroll/cryptenroll-wipe.c @@ -1,8 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "cryptenroll-wipe.h" #include "cryptenroll.h" -#include "json.h" +#include "json-util.h" #include "memory-util.h" #include "parse-util.h" #include "set.h" @@ -100,8 +102,8 @@ static int find_slots_by_mask( /* Find all slots that are associated with a token of a type in the specified token type mask */ for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - JsonVariant *w, *z; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + sd_json_variant *w, *z; EnrollType t; r = cryptsetup_get_token_as_json(cd, token, NULL, &v); @@ -112,16 +114,16 @@ static int find_slots_by_mask( continue; } - w = json_variant_by_key(v, "type"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "type"); + if (!w || !sd_json_variant_is_string(w)) { log_warning("Token JSON data lacks type field, ignoring."); continue; } - t = luks2_token_type_from_string(json_variant_string(w)); + t = luks2_token_type_from_string(sd_json_variant_string(w)); - w = json_variant_by_key(v, "keyslots"); - if (!w || !json_variant_is_array(w)) { + w = sd_json_variant_by_key(v, "keyslots"); + if (!w || !sd_json_variant_is_array(w)) { log_warning("Token JSON data lacks keyslots field, ignoring."); continue; } @@ -129,12 +131,12 @@ static int find_slots_by_mask( JSON_VARIANT_ARRAY_FOREACH(z, w) { int slot; - if (!json_variant_is_string(z)) { + if (!sd_json_variant_is_string(z)) { log_warning("Token JSON data's keyslot field is not an array of strings, ignoring."); continue; } - r = safe_atoi(json_variant_string(z), &slot); + r = safe_atoi(sd_json_variant_string(z), &slot); if (r < 0) { log_warning_errno(r, "Token JSON data's keyslot filed is not an integer formatted as string, ignoring."); continue; @@ -200,9 +202,9 @@ static int find_slot_tokens(struct crypt_device *cd, Set *wipe_slots, Set *keep_ * the slots sets according to the token data: add any other slots listed in the tokens we act on. */ for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; bool shall_wipe = false; - JsonVariant *w, *z; + sd_json_variant *w, *z; r = cryptsetup_get_token_as_json(cd, token, NULL, &v); if (IN_SET(r, -ENOENT, -EINVAL)) @@ -212,8 +214,8 @@ static int find_slot_tokens(struct crypt_device *cd, Set *wipe_slots, Set *keep_ continue; } - w = json_variant_by_key(v, "keyslots"); - if (!w || !json_variant_is_array(w)) { + w = sd_json_variant_by_key(v, "keyslots"); + if (!w || !sd_json_variant_is_array(w)) { log_warning("Token JSON data lacks keyslots field, ignoring."); continue; } @@ -222,12 +224,12 @@ static int find_slot_tokens(struct crypt_device *cd, Set *wipe_slots, Set *keep_ JSON_VARIANT_ARRAY_FOREACH(z, w) { int slot; - if (!json_variant_is_string(z)) { + if (!sd_json_variant_is_string(z)) { log_warning("Token JSON data's keyslot field is not an array of strings, ignoring."); continue; } - r = safe_atoi(json_variant_string(z), &slot); + r = safe_atoi(sd_json_variant_string(z), &slot); if (r < 0) { log_warning_errno(r, "Token JSON data's keyslot filed is not an integer formatted as string, ignoring."); continue; @@ -249,9 +251,9 @@ static int find_slot_tokens(struct crypt_device *cd, Set *wipe_slots, Set *keep_ JSON_VARIANT_ARRAY_FOREACH(z, w) { int slot; - if (!json_variant_is_string(z)) + if (!sd_json_variant_is_string(z)) continue; - if (safe_atoi(json_variant_string(z), &slot) < 0) + if (safe_atoi(sd_json_variant_string(z), &slot) < 0) continue; if (set_put(shall_wipe ? wipe_slots : keep_slots, INT_TO_PTR(slot)) < 0) diff --git a/src/cryptsetup/cryptsetup-pkcs11.c b/src/cryptsetup/cryptsetup-pkcs11.c index 4b2b5bbf00..c8008533f3 100644 --- a/src/cryptsetup/cryptsetup-pkcs11.c +++ b/src/cryptsetup/cryptsetup-pkcs11.c @@ -3,10 +3,11 @@ #include <fcntl.h> #include <unistd.h> #include <sys/stat.h> - #include <p11-kit/p11-kit.h> #include <p11-kit/uri.h> +#include "sd-json.h" + #include "alloc-util.h" #include "ask-password-api.h" #include "cryptsetup-pkcs11.h" @@ -15,7 +16,6 @@ #include "fileio.h" #include "format-util.h" #include "hexdecoct.h" -#include "json.h" #include "macro.h" #include "memory-util.h" #include "parse-util.h" @@ -109,8 +109,8 @@ int find_pkcs11_auto_data( /* Loads PKCS#11 metadata from LUKS2 JSON token headers. */ for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - JsonVariant *w; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + sd_json_variant *w; int ks; r = cryptsetup_get_token_as_json(cd, token, "systemd-pkcs11", &v); @@ -134,12 +134,12 @@ int find_pkcs11_auto_data( assert(keyslot < 0); keyslot = ks; - w = json_variant_by_key(v, "pkcs11-uri"); - if (!w || !json_variant_is_string(w)) + w = sd_json_variant_by_key(v, "pkcs11-uri"); + if (!w || !sd_json_variant_is_string(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 token data lacks 'pkcs11-uri' field."); - uri = strdup(json_variant_string(w)); + uri = strdup(sd_json_variant_string(w)); if (!uri) return log_oom(); @@ -147,14 +147,14 @@ int find_pkcs11_auto_data( return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 token data contains invalid PKCS#11 URI."); - w = json_variant_by_key(v, "pkcs11-key"); - if (!w || !json_variant_is_string(w)) + w = sd_json_variant_by_key(v, "pkcs11-key"); + if (!w || !sd_json_variant_is_string(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 token data lacks 'pkcs11-key' field."); assert(!key); assert(key_size == 0); - r = unbase64mem(json_variant_string(w), &key, &key_size); + r = unbase64mem(sd_json_variant_string(w), &key, &key_size); if (r < 0) return log_error_errno(r, "Failed to decode base64 encoded key."); } diff --git a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-fido2.c b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-fido2.c index 1efb7c501d..50e2ea755f 100644 --- a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-fido2.c +++ b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-fido2.c @@ -7,7 +7,7 @@ #include "cryptsetup-token.h" #include "cryptsetup-token-util.h" #include "hexdecoct.h" -#include "json.h" +#include "json-util.h" #include "luks2-fido2.h" #include "memory-util.h" #include "version.h" @@ -157,59 +157,59 @@ _public_ int cryptsetup_token_validate( const char *json /* contains valid 'type' and 'keyslots' fields. 'type' is 'systemd-fido2' */) { int r; - JsonVariant *w; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + sd_json_variant *w; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; assert(json); - r = json_parse(json, 0, &v, NULL, NULL); + r = sd_json_parse(json, 0, &v, NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Could not parse " TOKEN_NAME " json object: %m."); - w = json_variant_by_key(v, "fido2-credential"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "fido2-credential"); + if (!w || !sd_json_variant_is_string(w)) { crypt_log_debug(cd, "FIDO2 token data lacks 'fido2-credential' field."); return 1; } - r = unbase64mem(json_variant_string(w), NULL, NULL); + r = unbase64mem(sd_json_variant_string(w), NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Invalid base64 data in 'fido2-credential' field: %m"); - w = json_variant_by_key(v, "fido2-salt"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "fido2-salt"); + if (!w || !sd_json_variant_is_string(w)) { crypt_log_debug(cd, "FIDO2 token data lacks 'fido2-salt' field."); return 1; } - r = unbase64mem(json_variant_string(w), NULL, NULL); + r = unbase64mem(sd_json_variant_string(w), NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Failed to decode base64 encoded salt: %m."); /* The "rp" field is optional. */ - w = json_variant_by_key(v, "fido2-rp"); - if (w && !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "fido2-rp"); + if (w && !sd_json_variant_is_string(w)) { crypt_log_debug(cd, "FIDO2 token data's 'fido2-rp' field is not a string."); return 1; } /* The "fido2-clientPin-required" field is optional. */ - w = json_variant_by_key(v, "fido2-clientPin-required"); - if (w && !json_variant_is_boolean(w)) { + w = sd_json_variant_by_key(v, "fido2-clientPin-required"); + if (w && !sd_json_variant_is_boolean(w)) { crypt_log_debug(cd, "FIDO2 token data's 'fido2-clientPin-required' field is not a boolean."); return 1; } /* The "fido2-up-required" field is optional. */ - w = json_variant_by_key(v, "fido2-up-required"); - if (w && !json_variant_is_boolean(w)) { + w = sd_json_variant_by_key(v, "fido2-up-required"); + if (w && !sd_json_variant_is_boolean(w)) { crypt_log_debug(cd, "FIDO2 token data's 'fido2-up-required' field is not a boolean."); return 1; } /* The "fido2-uv-required" field is optional. */ - w = json_variant_by_key(v, "fido2-uv-required"); - if (w && !json_variant_is_boolean(w)) { + w = sd_json_variant_by_key(v, "fido2-uv-required"); + if (w && !sd_json_variant_is_boolean(w)) { crypt_log_debug(cd, "FIDO2 token data's 'fido2-uv-required' field is not a boolean."); return 1; } diff --git a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-pkcs11.c b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-pkcs11.c index a9898bad9a..1a8dd67673 100644 --- a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-pkcs11.c +++ b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-pkcs11.c @@ -3,10 +3,11 @@ #include <errno.h> #include <libcryptsetup.h> +#include "sd-json.h" + #include "cryptsetup-token.h" #include "cryptsetup-token-util.h" #include "hexdecoct.h" -#include "json.h" #include "luks2-pkcs11.h" #include "memory-util.h" #include "pkcs11-util.h" @@ -112,31 +113,31 @@ _public_ int cryptsetup_token_validate( const char *json /* contains valid 'type' and 'keyslots' fields. 'type' is 'systemd-pkcs11' */) { int r; - JsonVariant *w; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + sd_json_variant *w; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - r = json_parse(json, 0, &v, NULL, NULL); + r = sd_json_parse(json, 0, &v, NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Could not parse " TOKEN_NAME " json object: %m."); - w = json_variant_by_key(v, "pkcs11-uri"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "pkcs11-uri"); + if (!w || !sd_json_variant_is_string(w)) { crypt_log_debug(cd, "PKCS#11 token data lacks 'pkcs11-uri' field."); return 1; } - if (!pkcs11_uri_valid(json_variant_string(w))) { + if (!pkcs11_uri_valid(sd_json_variant_string(w))) { crypt_log_debug(cd, "PKCS#11 token data contains invalid PKCS#11 URI."); return 1; } - w = json_variant_by_key(v, "pkcs11-key"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "pkcs11-key"); + if (!w || !sd_json_variant_is_string(w)) { crypt_log_debug(cd, "PKCS#11 token data lacks 'pkcs11-key' field."); return 1; } - r = unbase64mem(json_variant_string(w), NULL, NULL); + r = unbase64mem(sd_json_variant_string(w), NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Failed to decode base64 encoded key: %m."); diff --git a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c index 8b4754ad77..d7fb08fd58 100644 --- a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c +++ b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c @@ -6,7 +6,7 @@ #include "cryptsetup-token.h" #include "cryptsetup-token-util.h" #include "hexdecoct.h" -#include "json.h" +#include "json-util.h" #include "luks2-tpm2.h" #include "memory-util.h" #include "strv.h" @@ -44,7 +44,7 @@ _public_ int cryptsetup_token_open_pin( _cleanup_(erase_and_freep) char *base64_encoded = NULL, *pin_string = NULL; _cleanup_(iovec_done) struct iovec blob = {}, pubkey = {}, policy_hash = {}, salt = {}, srk = {}, pcrlock_nv = {}; _cleanup_(iovec_done_erase) struct iovec decrypted_key = {}; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; uint32_t hash_pcr_mask, pubkey_pcr_mask; systemd_tpm2_plugin_params params = { .search_pcr_mask = UINT32_MAX @@ -72,7 +72,7 @@ _public_ int cryptsetup_token_open_pin( if (usrptr) params = *(systemd_tpm2_plugin_params *)usrptr; - r = json_parse(json, 0, &v, NULL, NULL); + r = sd_json_parse(json, 0, &v, NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Failed to parse token JSON data: %m"); @@ -169,7 +169,7 @@ _public_ void cryptsetup_token_dump( _cleanup_free_ char *hash_pcrs_str = NULL, *pubkey_pcrs_str = NULL, *blob_str = NULL, *policy_hash_str = NULL, *pubkey_str = NULL; _cleanup_(iovec_done) struct iovec blob = {}, pubkey = {}, policy_hash = {}, salt = {}, srk = {}, pcrlock_nv = {}; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; uint32_t hash_pcr_mask, pubkey_pcr_mask; uint16_t pcr_bank, primary_alg; TPM2Flags flags = 0; @@ -177,7 +177,7 @@ _public_ void cryptsetup_token_dump( assert(json); - r = json_parse(json, 0, &v, NULL, NULL); + r = sd_json_parse(json, 0, &v, NULL, NULL); if (r < 0) return (void) crypt_log_debug_errno(cd, r, "Failed to parse " TOKEN_NAME " JSON object: %m"); @@ -243,17 +243,17 @@ _public_ int cryptsetup_token_validate( const char *json /* contains valid 'type' and 'keyslots' fields. 'type' is 'systemd-tpm2' */) { int r; - JsonVariant *w, *e; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + sd_json_variant *w, *e; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; assert(json); - r = json_parse(json, 0, &v, NULL, NULL); + r = sd_json_parse(json, 0, &v, NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Could not parse " TOKEN_NAME " json object: %m"); - w = json_variant_by_key(v, "tpm2-pcrs"); - if (!w || !json_variant_is_array(w)) { + w = sd_json_variant_by_key(v, "tpm2-pcrs"); + if (!w || !sd_json_variant_is_array(w)) { crypt_log_debug(cd, "TPM2 token data lacks 'tpm2-pcrs' field."); return 1; } @@ -261,12 +261,12 @@ _public_ int cryptsetup_token_validate( JSON_VARIANT_ARRAY_FOREACH(e, w) { uint64_t u; - if (!json_variant_is_number(e)) { + if (!sd_json_variant_is_number(e)) { crypt_log_debug(cd, "TPM2 PCR is not a number."); return 1; } - u = json_variant_unsigned(e); + u = sd_json_variant_unsigned(e); if (!TPM2_PCR_INDEX_VALID(u)) { crypt_log_debug(cd, "TPM2 PCR number out of range."); return 1; @@ -275,61 +275,61 @@ _public_ int cryptsetup_token_validate( /* The bank field is optional, since it was added in systemd 250 only. Before the bank was hardcoded * to SHA256. */ - w = json_variant_by_key(v, "tpm2-pcr-bank"); + w = sd_json_variant_by_key(v, "tpm2-pcr-bank"); if (w) { /* The PCR bank field is optional */ - if (!json_variant_is_string(w)) { + if (!sd_json_variant_is_string(w)) { crypt_log_debug(cd, "TPM2 PCR bank is not a string."); return 1; } - if (tpm2_hash_alg_from_string(json_variant_string(w)) < 0) { - crypt_log_debug(cd, "TPM2 PCR bank invalid or not supported: %s.", json_variant_string(w)); + if (tpm2_hash_alg_from_string(sd_json_variant_string(w)) < 0) { + crypt_log_debug(cd, "TPM2 PCR bank invalid or not supported: %s.", sd_json_variant_string(w)); return 1; } } /* The primary key algorithm field is optional, since it was also added in systemd 250 only. Before * the algorithm was hardcoded to ECC. */ - w = json_variant_by_key(v, "tpm2-primary-alg"); + w = sd_json_variant_by_key(v, "tpm2-primary-alg"); if (w) { /* The primary key algorithm is optional */ - if (!json_variant_is_string(w)) { + if (!sd_json_variant_is_string(w)) { crypt_log_debug(cd, "TPM2 primary key algorithm is not a string."); return 1; } - if (tpm2_asym_alg_from_string(json_variant_string(w)) < 0) { - crypt_log_debug(cd, "TPM2 primary key algorithm invalid or not supported: %s", json_variant_string(w)); + if (tpm2_asym_alg_from_string(sd_json_variant_string(w)) < 0) { + crypt_log_debug(cd, "TPM2 primary key algorithm invalid or not supported: %s", sd_json_variant_string(w)); return 1; } } - w = json_variant_by_key(v, "tpm2-blob"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "tpm2-blob"); + if (!w || !sd_json_variant_is_string(w)) { crypt_log_debug(cd, "TPM2 token data lacks 'tpm2-blob' field."); return 1; } - r = unbase64mem(json_variant_string(w), NULL, NULL); + r = unbase64mem(sd_json_variant_string(w), NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Invalid base64 data in 'tpm2-blob' field: %m"); - w = json_variant_by_key(v, "tpm2-policy-hash"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "tpm2-policy-hash"); + if (!w || !sd_json_variant_is_string(w)) { crypt_log_debug(cd, "TPM2 token data lacks 'tpm2-policy-hash' field."); return 1; } - r = unhexmem(json_variant_string(w), NULL, NULL); + r = unhexmem(sd_json_variant_string(w), NULL, NULL); if (r < 0) return crypt_log_debug_errno(cd, r, "Invalid base64 data in 'tpm2-policy-hash' field: %m"); - w = json_variant_by_key(v, "tpm2-pin"); + w = sd_json_variant_by_key(v, "tpm2-pin"); if (w) { - if (!json_variant_is_boolean(w)) { + if (!sd_json_variant_is_boolean(w)) { crypt_log_debug(cd, "TPM2 PIN policy is not a boolean."); return 1; } diff --git a/src/cryptsetup/cryptsetup-tokens/luks2-fido2.c b/src/cryptsetup/cryptsetup-tokens/luks2-fido2.c index 5b386133a8..c68c3259e2 100644 --- a/src/cryptsetup/cryptsetup-tokens/luks2-fido2.c +++ b/src/cryptsetup/cryptsetup-tokens/luks2-fido2.c @@ -4,7 +4,7 @@ #include "cryptsetup-token-util.h" #include "hexdecoct.h" -#include "json.h" +#include "json-util.h" #include "luks2-fido2.h" #include "memory-util.h" #include "strv.h" @@ -84,8 +84,8 @@ int parse_luks2_fido2_data( size_t cid_size = 0, salt_size = 0; _cleanup_free_ char *rp = NULL; int r; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - JsonVariant *w; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + sd_json_variant *w; Fido2EnrollFlags required = 0; assert(json); @@ -96,54 +96,54 @@ int parse_luks2_fido2_data( assert(ret_cid_size); assert(ret_required); - r = json_parse(json, 0, &v, NULL, NULL); + r = sd_json_parse(json, 0, &v, NULL, NULL); if (r < 0) return crypt_log_error_errno(cd, r, "Failed to parse JSON token data: %m"); - w = json_variant_by_key(v, "fido2-credential"); + w = sd_json_variant_by_key(v, "fido2-credential"); if (!w) return -EINVAL; - r = unbase64mem(json_variant_string(w), &cid, &cid_size); + r = unbase64mem(sd_json_variant_string(w), &cid, &cid_size); if (r < 0) return crypt_log_error_errno(cd, r, "Failed to parse 'fido2-credentials' field: %m"); - w = json_variant_by_key(v, "fido2-salt"); + w = sd_json_variant_by_key(v, "fido2-salt"); if (!w) return -EINVAL; - r = unbase64mem(json_variant_string(w), &salt, &salt_size); + r = unbase64mem(sd_json_variant_string(w), &salt, &salt_size); if (r < 0) return crypt_log_error_errno(cd, r, "Failed to parse 'fido2-salt' field: %m"); - w = json_variant_by_key(v, "fido2-rp"); + w = sd_json_variant_by_key(v, "fido2-rp"); if (w) { /* The "rp" field is optional. */ - rp = strdup(json_variant_string(w)); + rp = strdup(sd_json_variant_string(w)); if (!rp) { crypt_log_error(cd, "Not enough memory."); return -ENOMEM; } } - w = json_variant_by_key(v, "fido2-clientPin-required"); + w = sd_json_variant_by_key(v, "fido2-clientPin-required"); if (w) /* The "fido2-clientPin-required" field is optional. */ - SET_FLAG(required, FIDO2ENROLL_PIN, json_variant_boolean(w)); + SET_FLAG(required, FIDO2ENROLL_PIN, sd_json_variant_boolean(w)); else required |= FIDO2ENROLL_PIN_IF_NEEDED; /* compat with 248, where the field was unset */ - w = json_variant_by_key(v, "fido2-up-required"); + w = sd_json_variant_by_key(v, "fido2-up-required"); if (w) /* The "fido2-up-required" field is optional. */ - SET_FLAG(required, FIDO2ENROLL_UP, json_variant_boolean(w)); + SET_FLAG(required, FIDO2ENROLL_UP, sd_json_variant_boolean(w)); else required |= FIDO2ENROLL_UP_IF_NEEDED; /* compat with 248 */ - w = json_variant_by_key(v, "fido2-uv-required"); + w = sd_json_variant_by_key(v, "fido2-uv-required"); if (w) /* The "fido2-uv-required" field is optional. */ - SET_FLAG(required, FIDO2ENROLL_UV, json_variant_boolean(w)); + SET_FLAG(required, FIDO2ENROLL_UV, sd_json_variant_boolean(w)); else required |= FIDO2ENROLL_UV_OMIT; /* compat with 248 */ diff --git a/src/cryptsetup/cryptsetup-tokens/luks2-pkcs11.c b/src/cryptsetup/cryptsetup-tokens/luks2-pkcs11.c index ac5100f688..c32f1634a0 100644 --- a/src/cryptsetup/cryptsetup-tokens/luks2-pkcs11.c +++ b/src/cryptsetup/cryptsetup-tokens/luks2-pkcs11.c @@ -3,10 +3,11 @@ #include <p11-kit/p11-kit.h> #include <p11-kit/uri.h> +#include "sd-json.h" + #include "cryptsetup-token-util.h" #include "escape.h" #include "hexdecoct.h" -#include "json.h" #include "luks2-pkcs11.h" #include "memory-util.h" #include "pkcs11-util.h" @@ -236,31 +237,31 @@ int parse_luks2_pkcs11_data( size_t key_size; _cleanup_free_ char *uri = NULL; _cleanup_free_ void *key = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - JsonVariant *w; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + sd_json_variant *w; assert(json); assert(ret_uri); assert(ret_encrypted_key); assert(ret_encrypted_key_size); - r = json_parse(json, 0, &v, NULL, NULL); + r = sd_json_parse(json, 0, &v, NULL, NULL); if (r < 0) return r; - w = json_variant_by_key(v, "pkcs11-uri"); + w = sd_json_variant_by_key(v, "pkcs11-uri"); if (!w) return -EINVAL; - uri = strdup(json_variant_string(w)); + uri = strdup(sd_json_variant_string(w)); if (!uri) return -ENOMEM; - w = json_variant_by_key(v, "pkcs11-key"); + w = sd_json_variant_by_key(v, "pkcs11-key"); if (!w) return -EINVAL; - r = unbase64mem(json_variant_string(w), &key, &key_size); + r = unbase64mem(sd_json_variant_string(w), &key, &key_size); if (r < 0) return crypt_log_debug_errno(cd, r, "Failed to decode base64 encoded key: %m."); diff --git a/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c b/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c index 08f901c548..4881012b0f 100644 --- a/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c +++ b/src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c @@ -1,10 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "alloc-util.h" #include "ask-password-api.h" #include "env-util.h" #include "hexdecoct.h" -#include "json.h" #include "log.h" #include "luks2-tpm2.h" #include "parse-util.h" @@ -31,7 +32,7 @@ int acquire_luks2_key( TPM2Flags flags, struct iovec *ret_decrypted_key) { - _cleanup_(json_variant_unrefp) JsonVariant *signature_json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *signature_json = NULL; _cleanup_free_ char *auto_device = NULL; _cleanup_(erase_and_freep) char *b64_salted_pin = NULL; int r; diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 85897aecac..42586a0aee 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -9,6 +9,7 @@ #include <unistd.h> #include "sd-device.h" +#include "sd-json.h" #include "sd-messages.h" #include "alloc-util.h" @@ -28,6 +29,7 @@ #include "fs-util.h" #include "fstab-util.h" #include "hexdecoct.h" +#include "json-util.h" #include "libfido2-util.h" #include "log.h" #include "main-func.h" @@ -720,9 +722,9 @@ static PassphraseType check_registered_passwords(struct crypt_device *cd) { /* Iterate all LUKS2 tokens and keep track of all their slots */ for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *type; - JsonVariant *w, *z; + sd_json_variant *w, *z; int tk; tk = cryptsetup_get_token_as_json(cd, token, NULL, &v); @@ -733,21 +735,21 @@ static PassphraseType check_registered_passwords(struct crypt_device *cd) { continue; } - w = json_variant_by_key(v, "type"); - if (!w || !json_variant_is_string(w)) { + w = sd_json_variant_by_key(v, "type"); + if (!w || !sd_json_variant_is_string(w)) { log_warning("Token JSON data lacks type field, ignoring."); continue; } - type = json_variant_string(w); + type = sd_json_variant_string(w); if (STR_IN_SET(type, "systemd-recovery", "systemd-pkcs11", "systemd-fido2", "systemd-tpm2")) { /* At least exists one recovery key */ if (streq(type, "systemd-recovery")) passphrase_type |= PASSPHRASE_RECOVERY_KEY; - w = json_variant_by_key(v, "keyslots"); - if (!w || !json_variant_is_array(w)) { + w = sd_json_variant_by_key(v, "keyslots"); + if (!w || !sd_json_variant_is_array(w)) { log_warning("Token JSON data lacks keyslots field, ignoring."); continue; } @@ -756,12 +758,12 @@ static PassphraseType check_registered_passwords(struct crypt_device *cd) { unsigned u; int at; - if (!json_variant_is_string(z)) { + if (!sd_json_variant_is_string(z)) { log_warning("Token JSON data's keyslot field is not an array of strings, ignoring."); continue; } - at = safe_atou(json_variant_string(z), &u); + at = safe_atou(sd_json_variant_string(z), &u); if (at < 0) { log_warning_errno(at, "Token JSON data's keyslot field is not an integer formatted as string, ignoring."); continue; diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index d68c29cbf1..7e28b39ea2 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -27,6 +27,7 @@ #include "format-util.h" #include "fs-util.h" #include "hexdecoct.h" +#include "json-util.h" #include "libarchive-util.h" #include "log.h" #include "loop-util.h" @@ -84,7 +85,7 @@ static DissectImageFlags arg_flags = DISSECT_IMAGE_ADD_PARTITION_DEVICES | DISSECT_IMAGE_ALLOW_USERSPACE_VERITY; static VeritySettings arg_verity_settings = VERITY_SETTINGS_DEFAULT; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; static bool arg_rmdir = false; @@ -861,7 +862,7 @@ static int action_dissect( LoopDevice *d, int userns_fd) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(table_unrefp) Table *t = NULL; _cleanup_free_ char *bn = NULL; uint64_t size = UINT64_MAX; @@ -873,10 +874,10 @@ static int action_dissect( if (r < 0) return log_error_errno(r, "Failed to extract file name from image path '%s': %m", arg_image); - if (arg_json_format_flags & (JSON_FORMAT_OFF|JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (arg_json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) pager_open(arg_pager_flags); - if (arg_json_format_flags & JSON_FORMAT_OFF) { + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) { printf(" File Name: %s%s%s\n", ansi_highlight(), bn, ansi_normal()); @@ -906,7 +907,7 @@ static int action_dissect( log_warning_errno(r, "OS image is currently in use, proceeding without showing OS image metadata."); else if (r < 0) return log_error_errno(r, "Failed to acquire image metadata: %m"); - else if (arg_json_format_flags & JSON_FORMAT_OFF) { + else if (arg_json_format_flags & SD_JSON_FORMAT_OFF) { if (m->image_name && !streq(m->image_name, bn)) printf("Image Name: %s\n", m->image_name); @@ -979,29 +980,29 @@ static int action_dissect( Architecture a = dissected_image_architecture(m); - r = json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(bn)), - JSON_BUILD_PAIR_CONDITION(size != UINT64_MAX, "size", JSON_BUILD_INTEGER(size)), - JSON_BUILD_PAIR("sectorSize", JSON_BUILD_INTEGER(m->sector_size)), - JSON_BUILD_PAIR_CONDITION(a >= 0, "architecture", JSON_BUILD_STRING(architecture_to_string(a))), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(m->image_uuid), "imageUuid", JSON_BUILD_UUID(m->image_uuid)), - JSON_BUILD_PAIR_CONDITION(m->hostname, "hostname", JSON_BUILD_STRING(m->hostname)), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(m->machine_id), "machineId", JSON_BUILD_ID128(m->machine_id)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->machine_info), "machineInfo", JSON_BUILD_STRV_ENV_PAIR(m->machine_info)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->os_release), "osRelease", JSON_BUILD_STRV_ENV_PAIR(m->os_release)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->initrd_release), "initrdRelease", JSON_BUILD_STRV_ENV_PAIR(m->initrd_release)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->sysext_release), "sysextRelease", JSON_BUILD_STRV_ENV_PAIR(m->sysext_release)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->confext_release), "confextRelease", JSON_BUILD_STRV_ENV_PAIR(m->confext_release)), - JSON_BUILD_PAIR("useBootableUefi", JSON_BUILD_BOOLEAN(dissected_image_is_bootable_uefi(m))), - JSON_BUILD_PAIR("useBootableContainer", JSON_BUILD_BOOLEAN(dissected_image_is_bootable_os(m))), - JSON_BUILD_PAIR("useInitrd", JSON_BUILD_BOOLEAN(dissected_image_is_initrd(m))), - JSON_BUILD_PAIR("usePortableService", JSON_BUILD_BOOLEAN(dissected_image_is_portable(m))), - JSON_BUILD_PAIR("useSystemExtension", JSON_BUILD_BOOLEAN(strv_contains(sysext_scopes, "system"))), - JSON_BUILD_PAIR("useInitRDSystemExtension", JSON_BUILD_BOOLEAN(strv_contains(sysext_scopes, "initrd"))), - JSON_BUILD_PAIR("usePortableSystemExtension", JSON_BUILD_BOOLEAN(strv_contains(sysext_scopes, "portable"))), - JSON_BUILD_PAIR("useConfigurationExtension", JSON_BUILD_BOOLEAN(strv_contains(confext_scopes, "system"))), - JSON_BUILD_PAIR("useInitRDConfigurationExtension", JSON_BUILD_BOOLEAN(strv_contains(confext_scopes, "initrd"))), - JSON_BUILD_PAIR("usePortableConfigurationExtension", JSON_BUILD_BOOLEAN(strv_contains(confext_scopes, "portable"))))); + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(bn)), + SD_JSON_BUILD_PAIR_CONDITION(size != UINT64_MAX, "size", SD_JSON_BUILD_INTEGER(size)), + SD_JSON_BUILD_PAIR("sectorSize", SD_JSON_BUILD_INTEGER(m->sector_size)), + SD_JSON_BUILD_PAIR_CONDITION(a >= 0, "architecture", SD_JSON_BUILD_STRING(architecture_to_string(a))), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(m->image_uuid), "imageUuid", SD_JSON_BUILD_UUID(m->image_uuid)), + SD_JSON_BUILD_PAIR_CONDITION(!!m->hostname, "hostname", SD_JSON_BUILD_STRING(m->hostname)), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(m->machine_id), "machineId", SD_JSON_BUILD_ID128(m->machine_id)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->machine_info), "machineInfo", JSON_BUILD_STRV_ENV_PAIR(m->machine_info)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->os_release), "osRelease", JSON_BUILD_STRV_ENV_PAIR(m->os_release)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->initrd_release), "initrdRelease", JSON_BUILD_STRV_ENV_PAIR(m->initrd_release)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->sysext_release), "sysextRelease", JSON_BUILD_STRV_ENV_PAIR(m->sysext_release)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(m->confext_release), "confextRelease", JSON_BUILD_STRV_ENV_PAIR(m->confext_release)), + SD_JSON_BUILD_PAIR("useBootableUefi", SD_JSON_BUILD_BOOLEAN(dissected_image_is_bootable_uefi(m))), + SD_JSON_BUILD_PAIR("useBootableContainer", SD_JSON_BUILD_BOOLEAN(dissected_image_is_bootable_os(m))), + SD_JSON_BUILD_PAIR("useInitrd", SD_JSON_BUILD_BOOLEAN(dissected_image_is_initrd(m))), + SD_JSON_BUILD_PAIR("usePortableService", SD_JSON_BUILD_BOOLEAN(dissected_image_is_portable(m))), + SD_JSON_BUILD_PAIR("useSystemExtension", SD_JSON_BUILD_BOOLEAN(strv_contains(sysext_scopes, "system"))), + SD_JSON_BUILD_PAIR("useInitRDSystemExtension", SD_JSON_BUILD_BOOLEAN(strv_contains(sysext_scopes, "initrd"))), + SD_JSON_BUILD_PAIR("usePortableSystemExtension", SD_JSON_BUILD_BOOLEAN(strv_contains(sysext_scopes, "portable"))), + SD_JSON_BUILD_PAIR("useConfigurationExtension", SD_JSON_BUILD_BOOLEAN(strv_contains(confext_scopes, "system"))), + SD_JSON_BUILD_PAIR("useInitRDConfigurationExtension", SD_JSON_BUILD_BOOLEAN(strv_contains(confext_scopes, "initrd"))), + SD_JSON_BUILD_PAIR("usePortableConfigurationExtension", SD_JSON_BUILD_BOOLEAN(strv_contains(confext_scopes, "portable"))))); if (r < 0) return log_oom(); } @@ -1015,7 +1016,7 @@ static int action_dissect( /* Hide the device path if this is a loopback device that is not relinquished, since that means the * device node is not going to be useful the instant our command exits */ - if ((!d || d->created) && (arg_json_format_flags & JSON_FORMAT_OFF)) + if ((!d || d->created) && (arg_json_format_flags & SD_JSON_FORMAT_OFF)) table_hide_column_from_display(t, 8); for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { @@ -1078,24 +1079,24 @@ static int action_dissect( return table_log_add_error(r); } - if (arg_json_format_flags & JSON_FORMAT_OFF) { + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) { (void) table_set_header(t, arg_legend); r = table_print(t, NULL); if (r < 0) return table_log_print_error(r); } else { - _cleanup_(json_variant_unrefp) JsonVariant *jt = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *jt = NULL; r = table_to_json(t, &jt); if (r < 0) return log_error_errno(r, "Failed to convert table to JSON: %m"); - r = json_variant_set_field(&v, "mounts", jt); + r = sd_json_variant_set_field(&v, "mounts", jt); if (r < 0) return log_oom(); - json_variant_dump(v, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(v, arg_json_format_flags, stdout, NULL); } return 0; @@ -1854,7 +1855,7 @@ static int action_discover(void) { return log_error_errno(r, "Failed to discover images: %m"); } - if ((arg_json_format_flags & JSON_FORMAT_OFF) && hashmap_isempty(images)) { + if ((arg_json_format_flags & SD_JSON_FORMAT_OFF) && hashmap_isempty(images)) { log_info("No images found."); return 0; } diff --git a/src/fuzz/fuzz-bootspec.c b/src/fuzz/fuzz-bootspec.c index 0c61cbe39e..e49acafe31 100644 --- a/src/fuzz/fuzz-bootspec.c +++ b/src/fuzz/fuzz-bootspec.c @@ -2,17 +2,20 @@ #include <string.h> +#include "sd-json.h" + #include "bootspec.h" #include "env-util.h" #include "escape.h" -#include "fuzz.h" #include "fd-util.h" -#include "json.h" +#include "fuzz.h" +#include "json-util.h" +#include "strv.h" -static int json_dispatch_config(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_config(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { BootConfig *config = ASSERT_PTR(userdata); - const char *s = json_variant_string(variant); + const char *s = sd_json_variant_string(variant); if (!s) return -EINVAL; @@ -23,29 +26,29 @@ static int json_dispatch_config(const char *name, JsonVariant *variant, JsonDisp return 0; } -static int json_dispatch_entries(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_entries(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { BootConfig *config = ASSERT_PTR(userdata); - JsonVariant *entry; + sd_json_variant *entry; JSON_VARIANT_ARRAY_FOREACH(entry, variant) { - if (!json_variant_is_array(entry) || - json_variant_elements(entry) < 1) + if (!sd_json_variant_is_array(entry) || + sd_json_variant_elements(entry) < 1) return -EINVAL; - JsonVariant *v; + sd_json_variant *v; const char *id = NULL, *raw = NULL; _cleanup_free_ char *data = NULL; ssize_t len = -ENODATA; - v = json_variant_by_index(entry, 0); + v = sd_json_variant_by_index(entry, 0); if (v) - id = json_variant_string(v); + id = sd_json_variant_string(v); if (!id) continue; - v = json_variant_by_index(entry, 1); + v = sd_json_variant_by_index(entry, 1); if (v) - raw = json_variant_string(v); + raw = sd_json_variant_string(v); if (raw) len = cunescape(raw, UNESCAPE_RELAX | UNESCAPE_ACCEPT_NUL, &data); if (len >= 0) { @@ -59,12 +62,12 @@ static int json_dispatch_entries(const char *name, JsonVariant *variant, JsonDis return 0; } -static int json_dispatch_loader(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_loader(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { BootConfig *config = ASSERT_PTR(userdata); _cleanup_strv_free_ char **entries = NULL; int r; - r = json_dispatch_strv(name, variant, flags, &entries); + r = sd_json_dispatch_strv(name, variant, flags, &entries); if (r < 0) return r; @@ -72,10 +75,10 @@ static int json_dispatch_loader(const char *name, JsonVariant *variant, JsonDisp return 0; } -static const JsonDispatch data_dispatch[] = { - { "config", JSON_VARIANT_STRING, json_dispatch_config, 0, 0 }, - { "entries", JSON_VARIANT_ARRAY, json_dispatch_entries, 0, 0 }, - { "loader", JSON_VARIANT_ARRAY, json_dispatch_loader, 0, 0 }, +static const sd_json_dispatch_field data_dispatch[] = { + { "config", SD_JSON_VARIANT_STRING, json_dispatch_config, 0, 0 }, + { "entries", SD_JSON_VARIANT_ARRAY, json_dispatch_entries, 0, 0 }, + { "loader", SD_JSON_VARIANT_ARRAY, json_dispatch_loader, 0, 0 }, {} }; @@ -91,12 +94,12 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { assert_se(datadup = memdup_suffix0(data, size)); - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - r = json_parse(datadup, 0, &v, NULL, NULL); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + r = sd_json_parse(datadup, 0, &v, NULL, NULL); if (r < 0) return 0; - r = json_dispatch(v, data_dispatch, 0, &config); + r = sd_json_dispatch(v, data_dispatch, 0, &config); if (r < 0) return 0; @@ -113,8 +116,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { assert_se(freopen("/dev/null", "w", stdout)); } - (void) show_boot_entries(&config, JSON_FORMAT_OFF); - (void) show_boot_entries(&config, JSON_FORMAT_PRETTY); + (void) show_boot_entries(&config, SD_JSON_FORMAT_OFF); + (void) show_boot_entries(&config, SD_JSON_FORMAT_PRETTY); if (orig_stdout_fd >= 0) assert_se(freopen(FORMAT_PROC_FD_PATH(orig_stdout_fd), "w", stdout)); diff --git a/src/fuzz/fuzz-json.c b/src/fuzz/fuzz-json.c index 3d6d689f28..ea11940e4b 100644 --- a/src/fuzz/fuzz-json.c +++ b/src/fuzz/fuzz-json.c @@ -1,15 +1,17 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "alloc-util.h" #include "env-util.h" #include "fd-util.h" #include "fuzz.h" -#include "json.h" #include "memstream-util.h" +#include "string-util.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { _cleanup_(memstream_done) MemStream m = {}; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_fclose_ FILE *f = NULL; FILE *g = NULL; int r; @@ -19,7 +21,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { f = data_to_file(data, size); assert_se(f); - r = json_parse_file(f, NULL, 0, &v, NULL, NULL); + r = sd_json_parse_file(f, NULL, 0, &v, NULL, NULL); if (r < 0) { log_debug_errno(r, "failed to parse input: %m"); return 0; @@ -28,87 +30,87 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0) assert_se(g = memstream_init(&m)); - json_variant_dump(v, 0, g ?: stdout, NULL); - json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, g ?: stdout, NULL); + sd_json_variant_dump(v, 0, g ?: stdout, NULL); + sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_SOURCE, g ?: stdout, NULL); - bool sorted = json_variant_is_sorted(v); - log_debug("json_variant_is_sorted: %s", yes_no(sorted)); + bool sorted = sd_json_variant_is_sorted(v); + log_debug("sd_json_variant_is_sorted: %s", yes_no(sorted)); - r = json_variant_sort(&v); - log_debug_errno(r, "json_variant_sort: %d/%m", r); + r = sd_json_variant_sort(&v); + log_debug_errno(r, "sd_json_variant_sort: %d/%m", r); - sorted = json_variant_is_sorted(v); + sorted = sd_json_variant_is_sorted(v); log_debug("json_variant_is_sorted: %s", yes_no(sorted)); assert_se(r < 0 || sorted); - bool normalized = json_variant_is_normalized(v); + bool normalized = sd_json_variant_is_normalized(v); log_debug("json_variant_is_normalized: %s", yes_no(normalized)); - r = json_variant_normalize(&v); + r = sd_json_variant_normalize(&v); log_debug_errno(r, "json_variant_normalize: %d/%m", r); - normalized = json_variant_is_normalized(v); + normalized = sd_json_variant_is_normalized(v); log_debug("json_variant_is_normalized: %s", yes_no(normalized)); assert_se(r < 0 || normalized); - double real = json_variant_real(v); + double real = sd_json_variant_real(v); log_debug("json_variant_real: %lf", real); - bool negative = json_variant_is_negative(v); + bool negative = sd_json_variant_is_negative(v); log_debug("json_variant_is_negative: %s", yes_no(negative)); - bool blank = json_variant_is_blank_object(v); + bool blank = sd_json_variant_is_blank_object(v); log_debug("json_variant_is_blank_object: %s", yes_no(blank)); - blank = json_variant_is_blank_array(v); + blank = sd_json_variant_is_blank_array(v); log_debug("json_variant_is_blank_array: %s", yes_no(blank)); - size_t elements = json_variant_elements(v); + size_t elements = sd_json_variant_elements(v); log_debug("json_variant_elements: %zu", elements); for (size_t i = 0; i <= elements + 2; i++) - (void) json_variant_by_index(v, i); + (void) sd_json_variant_by_index(v, i); - assert_se(json_variant_equal(v, v)); - assert_se(!json_variant_equal(v, NULL)); - assert_se(!json_variant_equal(NULL, v)); + assert_se(sd_json_variant_equal(v, v)); + assert_se(!sd_json_variant_equal(v, NULL)); + assert_se(!sd_json_variant_equal(NULL, v)); - bool sensitive = json_variant_is_sensitive(v); + bool sensitive = sd_json_variant_is_sensitive(v); log_debug("json_variant_is_sensitive: %s", yes_no(sensitive)); - json_variant_sensitive(v); + sd_json_variant_sensitive(v); - sensitive = json_variant_is_sensitive(v); + sensitive = sd_json_variant_is_sensitive(v); log_debug("json_variant_is_sensitive: %s", yes_no(sensitive)); const char *source; unsigned line, column; - assert_se(json_variant_get_source(v, &source, &line, &column) == 0); + assert_se(sd_json_variant_get_source(v, &source, &line, &column) == 0); log_debug("json_variant_get_source: %s:%u:%u", source ?: "-", line, column); - r = json_variant_set_field_string(&v, "a", "string-a"); + r = sd_json_variant_set_field_string(&v, "a", "string-a"); log_debug_errno(r, "json_set_field_string: %d/%m", r); - r = json_variant_set_field_integer(&v, "b", -12345); + r = sd_json_variant_set_field_integer(&v, "b", -12345); log_debug_errno(r, "json_set_field_integer: %d/%m", r); - r = json_variant_set_field_unsigned(&v, "c", 12345); + r = sd_json_variant_set_field_unsigned(&v, "c", 12345); log_debug_errno(r, "json_set_field_unsigned: %d/%m", r); - r = json_variant_set_field_boolean(&v, "d", false); + r = sd_json_variant_set_field_boolean(&v, "d", false); log_debug_errno(r, "json_set_field_boolean: %d/%m", r); - r = json_variant_set_field_strv(&v, "e", STRV_MAKE("e-1", "e-2", "e-3")); + r = sd_json_variant_set_field_strv(&v, "e", STRV_MAKE("e-1", "e-2", "e-3")); log_debug_errno(r, "json_set_field_strv: %d/%m", r); - r = json_variant_filter(&v, STRV_MAKE("a", "b", "c", "d", "e")); + r = sd_json_variant_filter(&v, STRV_MAKE("a", "b", "c", "d", "e")); log_debug_errno(r, "json_variant_filter: %d/%m", r); /* I assume we can merge v with itself… */ - r = json_variant_merge_object(&v, v); + r = sd_json_variant_merge_object(&v, v); log_debug_errno(r, "json_variant_merge: %d/%m", r); - r = json_variant_append_array(&v, v); + r = sd_json_variant_append_array(&v, v); log_debug_errno(r, "json_variant_append_array: %d/%m", r); return 0; diff --git a/src/fuzz/fuzz-varlink-idl.c b/src/fuzz/fuzz-varlink-idl.c index 2436342a4d..c5c013d206 100644 --- a/src/fuzz/fuzz-varlink-idl.c +++ b/src/fuzz/fuzz-varlink-idl.c @@ -6,8 +6,9 @@ #include "fd-util.h" #include "fuzz.h" #include "io-util.h" -#include "varlink-idl.h" #include "log.h" +#include "string-util.h" +#include "varlink-idl.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { _cleanup_(varlink_interface_freep) VarlinkInterface *vi = NULL; diff --git a/src/fuzz/fuzz-varlink.c b/src/fuzz/fuzz-varlink.c index dd63419ae1..50139faf3f 100644 --- a/src/fuzz/fuzz-varlink.c +++ b/src/fuzz/fuzz-varlink.c @@ -12,13 +12,13 @@ static FILE *null = NULL; -static int method_something(Varlink *v, JsonVariant *p, VarlinkMethodFlags flags, void *userdata) { - json_variant_dump(p, JSON_FORMAT_NEWLINE|JSON_FORMAT_PRETTY, null, NULL); +static int method_something(Varlink *v, sd_json_variant *p, VarlinkMethodFlags flags, void *userdata) { + sd_json_variant_dump(p, SD_JSON_FORMAT_NEWLINE|SD_JSON_FORMAT_PRETTY, null, NULL); return 0; } -static int reply_callback(Varlink *v, JsonVariant *p, const char *error_id, VarlinkReplyFlags flags, void *userdata) { - json_variant_dump(p, JSON_FORMAT_NEWLINE|JSON_FORMAT_PRETTY, null, NULL); +static int reply_callback(Varlink *v, sd_json_variant *p, const char *error_id, VarlinkReplyFlags flags, void *userdata) { + sd_json_variant_dump(p, SD_JSON_FORMAT_NEWLINE|SD_JSON_FORMAT_PRETTY, null, NULL); return 0; } diff --git a/src/hibernate-resume/hibernate-resume-config.c b/src/hibernate-resume/hibernate-resume-config.c index c7ed1bc8d7..edf7a70e7c 100644 --- a/src/hibernate-resume/hibernate-resume-config.c +++ b/src/hibernate-resume/hibernate-resume-config.c @@ -1,10 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "alloc-util.h" #include "device-nodes.h" #include "fstab-util.h" #include "hibernate-resume-config.h" -#include "json.h" #include "os-util.h" #include "parse-util.h" #include "path-util.h" @@ -141,19 +142,19 @@ static bool validate_efi_hibernate_location(EFIHibernateLocation *e) { int get_efi_hibernate_location(EFIHibernateLocation **ret) { #if ENABLE_EFI - static const JsonDispatch dispatch_table[] = { - { "uuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(EFIHibernateLocation, uuid), JSON_MANDATORY }, - { "offset", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(EFIHibernateLocation, offset), JSON_MANDATORY }, - { "kernelVersion", JSON_VARIANT_STRING, json_dispatch_string, offsetof(EFIHibernateLocation, kernel_version), JSON_PERMISSIVE|JSON_DEBUG }, - { "osReleaseId", JSON_VARIANT_STRING, json_dispatch_string, offsetof(EFIHibernateLocation, id), JSON_PERMISSIVE|JSON_DEBUG }, - { "osReleaseImageId", JSON_VARIANT_STRING, json_dispatch_string, offsetof(EFIHibernateLocation, image_id), JSON_PERMISSIVE|JSON_DEBUG }, - { "osReleaseVersionId", JSON_VARIANT_STRING, json_dispatch_string, offsetof(EFIHibernateLocation, version_id), JSON_PERMISSIVE|JSON_DEBUG }, - { "osReleaseImageVersion", JSON_VARIANT_STRING, json_dispatch_string, offsetof(EFIHibernateLocation, image_version), JSON_PERMISSIVE|JSON_DEBUG }, + static const sd_json_dispatch_field dispatch_table[] = { + { "uuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(EFIHibernateLocation, uuid), SD_JSON_MANDATORY }, + { "offset", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(EFIHibernateLocation, offset), SD_JSON_MANDATORY }, + { "kernelVersion", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(EFIHibernateLocation, kernel_version), SD_JSON_PERMISSIVE|SD_JSON_DEBUG }, + { "osReleaseId", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(EFIHibernateLocation, id), SD_JSON_PERMISSIVE|SD_JSON_DEBUG }, + { "osReleaseImageId", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(EFIHibernateLocation, image_id), SD_JSON_PERMISSIVE|SD_JSON_DEBUG }, + { "osReleaseVersionId", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(EFIHibernateLocation, version_id), SD_JSON_PERMISSIVE|SD_JSON_DEBUG }, + { "osReleaseImageVersion", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(EFIHibernateLocation, image_version), SD_JSON_PERMISSIVE|SD_JSON_DEBUG }, {}, }; _cleanup_(efi_hibernate_location_freep) EFIHibernateLocation *e = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *location_str = NULL; int r; @@ -168,7 +169,7 @@ int get_efi_hibernate_location(EFIHibernateLocation **ret) { if (r < 0) return log_error_errno(r, "Failed to get EFI variable HibernateLocation: %m"); - r = json_parse(location_str, 0, &v, NULL, NULL); + r = sd_json_parse(location_str, 0, &v, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse HibernateLocation JSON object: %m"); @@ -176,7 +177,7 @@ int get_efi_hibernate_location(EFIHibernateLocation **ret) { if (!e) return log_oom(); - r = json_dispatch(v, dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, e); + r = sd_json_dispatch(v, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, e); if (r < 0) return r; diff --git a/src/home/home-util.c b/src/home/home-util.c index 973523652d..e949840152 100644 --- a/src/home/home-util.c +++ b/src/home/home-util.c @@ -115,7 +115,7 @@ int split_user_name_realm(const char *t, char **ret_user_name, char **ret_realm) int bus_message_append_secret(sd_bus_message *m, UserRecord *secret) { _cleanup_(erase_and_freep) char *formatted = NULL; - JsonVariant *v; + sd_json_variant *v; int r; assert(m); @@ -124,11 +124,11 @@ int bus_message_append_secret(sd_bus_message *m, UserRecord *secret) { if (!FLAGS_SET(secret->mask, USER_RECORD_SECRET)) return sd_bus_message_append(m, "s", "{}"); - v = json_variant_by_key(secret->json, "secret"); + v = sd_json_variant_by_key(secret->json, "secret"); if (!v) return -EINVAL; - r = json_variant_format(v, 0, &formatted); + r = sd_json_variant_format(v, 0, &formatted); if (r < 0) return r; diff --git a/src/home/homectl-fido2.c b/src/home/homectl-fido2.c index 384461a1ca..332865c8d9 100644 --- a/src/home/homectl-fido2.c +++ b/src/home/homectl-fido2.c @@ -19,11 +19,11 @@ #if HAVE_LIBFIDO2 static int add_fido2_credential_id( - JsonVariant **v, + sd_json_variant **v, const void *cid, size_t cid_size) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; _cleanup_strv_free_ char **l = NULL; _cleanup_free_ char *escaped = NULL; ssize_t escaped_size; @@ -36,9 +36,9 @@ static int add_fido2_credential_id( if (escaped_size < 0) return log_error_errno(escaped_size, "Failed to base64 encode FIDO2 credential ID: %m"); - w = json_variant_ref(json_variant_by_key(*v, "fido2HmacCredential")); + w = sd_json_variant_ref(sd_json_variant_by_key(*v, "fido2HmacCredential")); if (w) { - r = json_variant_strv(w, &l); + r = sd_json_variant_strv(w, &l); if (r < 0) return log_error_errno(r, "Failed to parse FIDO2 credential ID list: %m"); @@ -50,12 +50,12 @@ static int add_fido2_credential_id( if (r < 0) return log_oom(); - w = json_variant_unref(w); - r = json_variant_new_array_strv(&w, l); + w = sd_json_variant_unref(w); + r = sd_json_variant_new_array_strv(&w, l); if (r < 0) return log_error_errno(r, "Failed to create FIDO2 credential ID JSON: %m"); - r = json_variant_set_field(v, "fido2HmacCredential", w); + r = sd_json_variant_set_field(v, "fido2HmacCredential", w); if (r < 0) return log_error_errno(r, "Failed to update FIDO2 credential ID: %m"); @@ -63,7 +63,7 @@ static int add_fido2_credential_id( } static int add_fido2_salt( - JsonVariant **v, + sd_json_variant **v, const void *cid, size_t cid_size, const void *fido2_salt, @@ -72,7 +72,7 @@ static int add_fido2_salt( size_t secret_size, Fido2EnrollFlags lock_with) { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL, *w = NULL, *e = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL, *w = NULL, *e = NULL; _cleanup_(erase_and_freep) char *base64_encoded = NULL, *hashed = NULL; ssize_t base64_encoded_size; int r; @@ -87,29 +87,29 @@ static int add_fido2_salt( if (r < 0) return log_error_errno(errno_or_else(EINVAL), "Failed to UNIX hash secret key: %m"); - r = json_build(&e, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("credential", JSON_BUILD_BASE64(cid, cid_size)), - JSON_BUILD_PAIR("salt", JSON_BUILD_BASE64(fido2_salt, fido2_salt_size)), - JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRING(hashed)), - JSON_BUILD_PAIR("up", JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_UP))), - JSON_BUILD_PAIR("uv", JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_UV))), - JSON_BUILD_PAIR("clientPin", JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_PIN))))); + r = sd_json_build(&e, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("credential", SD_JSON_BUILD_BASE64(cid, cid_size)), + SD_JSON_BUILD_PAIR("salt", SD_JSON_BUILD_BASE64(fido2_salt, fido2_salt_size)), + SD_JSON_BUILD_PAIR("hashedPassword", SD_JSON_BUILD_STRING(hashed)), + SD_JSON_BUILD_PAIR("up", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_UP))), + SD_JSON_BUILD_PAIR("uv", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_UV))), + SD_JSON_BUILD_PAIR("clientPin", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(lock_with, FIDO2ENROLL_PIN))))); if (r < 0) return log_error_errno(r, "Failed to build FIDO2 salt JSON key object: %m"); - w = json_variant_ref(json_variant_by_key(*v, "privileged")); - l = json_variant_ref(json_variant_by_key(w, "fido2HmacSalt")); + w = sd_json_variant_ref(sd_json_variant_by_key(*v, "privileged")); + l = sd_json_variant_ref(sd_json_variant_by_key(w, "fido2HmacSalt")); - r = json_variant_append_array(&l, e); + r = sd_json_variant_append_array(&l, e); if (r < 0) return log_error_errno(r, "Failed append FIDO2 salt: %m"); - r = json_variant_set_field(&w, "fido2HmacSalt", l); + r = sd_json_variant_set_field(&w, "fido2HmacSalt", l); if (r < 0) return log_error_errno(r, "Failed to set FDO2 salt: %m"); - r = json_variant_set_field(v, "privileged", w); + r = sd_json_variant_set_field(v, "privileged", w); if (r < 0) return log_error_errno(r, "Failed to update privileged field: %m"); @@ -118,13 +118,13 @@ static int add_fido2_salt( #endif int identity_add_fido2_parameters( - JsonVariant **v, + sd_json_variant **v, const char *device, Fido2EnrollFlags lock_with, int cred_alg) { #if HAVE_LIBFIDO2 - JsonVariant *un, *realm, *rn; + sd_json_variant *un, *realm, *rn; _cleanup_(erase_and_freep) void *secret = NULL, *salt = NULL; _cleanup_(erase_and_freep) char *used_pin = NULL; size_t cid_size, salt_size, secret_size; @@ -135,26 +135,26 @@ int identity_add_fido2_parameters( assert(v); assert(device); - un = json_variant_by_key(*v, "userName"); + un = sd_json_variant_by_key(*v, "userName"); if (!un) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "userName field of user record is missing"); - if (!json_variant_is_string(un)) + if (!sd_json_variant_is_string(un)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "userName field of user record is not a string"); - realm = json_variant_by_key(*v, "realm"); + realm = sd_json_variant_by_key(*v, "realm"); if (realm) { - if (!json_variant_is_string(realm)) + if (!sd_json_variant_is_string(realm)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "realm field of user record is not a string"); - fido_un = strjoina(json_variant_string(un), json_variant_string(realm)); + fido_un = strjoina(sd_json_variant_string(un), sd_json_variant_string(realm)); } else - fido_un = json_variant_string(un); + fido_un = sd_json_variant_string(un); - rn = json_variant_by_key(*v, "realName"); - if (rn && !json_variant_is_string(rn)) + rn = sd_json_variant_by_key(*v, "realName"); + if (rn && !sd_json_variant_is_string(rn)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "realName field of user record is not a string"); @@ -164,7 +164,7 @@ int identity_add_fido2_parameters( /* rp_name= */ "Home Directory", /* user_id= */ fido_un, strlen(fido_un), /* We pass the user ID and name as the same */ /* user_name= */ fido_un, - /* user_display_name= */ rn ? json_variant_string(rn) : NULL, + /* user_display_name= */ rn ? sd_json_variant_string(rn) : NULL, /* user_icon_name= */ NULL, /* askpw_icon_name= */ "user-home", /* askpw_credential= */ "home.token-pin", diff --git a/src/home/homectl-fido2.h b/src/home/homectl-fido2.h index 558c6747d9..2ced71c97a 100644 --- a/src/home/homectl-fido2.h +++ b/src/home/homectl-fido2.h @@ -1,7 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "json.h" +#include "sd-json.h" + #include "libfido2-util.h" -int identity_add_fido2_parameters(JsonVariant **v, const char *device, Fido2EnrollFlags lock_with, int cred_alg); +int identity_add_fido2_parameters(sd_json_variant **v, const char *device, Fido2EnrollFlags lock_with, int cred_alg); diff --git a/src/home/homectl-pkcs11.c b/src/home/homectl-pkcs11.c index bb582d7d47..1102feded5 100644 --- a/src/home/homectl-pkcs11.c +++ b/src/home/homectl-pkcs11.c @@ -10,8 +10,8 @@ #include "pkcs11-util.h" #include "strv.h" -int identity_add_token_pin(JsonVariant **v, const char *pin) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL, *l = NULL; +int identity_add_token_pin(sd_json_variant **v, const char *pin) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL, *l = NULL; _cleanup_strv_free_erase_ char **pins = NULL; int r; @@ -20,10 +20,10 @@ int identity_add_token_pin(JsonVariant **v, const char *pin) { if (isempty(pin)) return 0; - w = json_variant_ref(json_variant_by_key(*v, "secret")); - l = json_variant_ref(json_variant_by_key(w, "tokenPin")); + w = sd_json_variant_ref(sd_json_variant_by_key(*v, "secret")); + l = sd_json_variant_ref(sd_json_variant_by_key(w, "tokenPin")); - r = json_variant_strv(l, &pins); + r = sd_json_variant_strv(l, &pins); if (r < 0) return log_error_errno(r, "Failed to convert PIN array: %m"); @@ -36,19 +36,19 @@ int identity_add_token_pin(JsonVariant **v, const char *pin) { strv_uniq(pins); - l = json_variant_unref(l); + l = sd_json_variant_unref(l); - r = json_variant_new_array_strv(&l, pins); + r = sd_json_variant_new_array_strv(&l, pins); if (r < 0) return log_error_errno(r, "Failed to allocate new PIN array JSON: %m"); - json_variant_sensitive(l); + sd_json_variant_sensitive(l); - r = json_variant_set_field(&w, "tokenPin", l); + r = sd_json_variant_set_field(&w, "tokenPin", l); if (r < 0) return log_error_errno(r, "Failed to update PIN field: %m"); - r = json_variant_set_field(v, "secret", w); + r = sd_json_variant_set_field(v, "secret", w); if (r < 0) return log_error_errno(r, "Failed to update secret object: %m"); @@ -57,17 +57,17 @@ int identity_add_token_pin(JsonVariant **v, const char *pin) { #if HAVE_P11KIT -static int add_pkcs11_token_uri(JsonVariant **v, const char *uri) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +static int add_pkcs11_token_uri(sd_json_variant **v, const char *uri) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; _cleanup_strv_free_ char **l = NULL; int r; assert(v); assert(uri); - w = json_variant_ref(json_variant_by_key(*v, "pkcs11TokenUri")); + w = sd_json_variant_ref(sd_json_variant_by_key(*v, "pkcs11TokenUri")); if (w) { - r = json_variant_strv(w, &l); + r = sd_json_variant_strv(w, &l); if (r < 0) return log_error_errno(r, "Failed to parse PKCS#11 token list: %m"); @@ -79,12 +79,12 @@ static int add_pkcs11_token_uri(JsonVariant **v, const char *uri) { if (r < 0) return log_oom(); - w = json_variant_unref(w); - r = json_variant_new_array_strv(&w, l); + w = sd_json_variant_unref(w); + r = sd_json_variant_new_array_strv(&w, l); if (r < 0) return log_error_errno(r, "Failed to create PKCS#11 token URI JSON: %m"); - r = json_variant_set_field(v, "pkcs11TokenUri", w); + r = sd_json_variant_set_field(v, "pkcs11TokenUri", w); if (r < 0) return log_error_errno(r, "Failed to update PKCS#11 token URI list: %m"); @@ -92,12 +92,12 @@ static int add_pkcs11_token_uri(JsonVariant **v, const char *uri) { } static int add_pkcs11_encrypted_key( - JsonVariant **v, + sd_json_variant **v, const char *uri, const void *encrypted_key, size_t encrypted_key_size, const void *decrypted_key, size_t decrypted_key_size) { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL, *w = NULL, *e = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL, *w = NULL, *e = NULL; _cleanup_(erase_and_freep) char *base64_encoded = NULL, *hashed = NULL; ssize_t base64_encoded_size; int r; @@ -119,32 +119,32 @@ static int add_pkcs11_encrypted_key( if (r < 0) return log_error_errno(errno_or_else(EINVAL), "Failed to UNIX hash secret key: %m"); - r = json_build(&e, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("uri", JSON_BUILD_STRING(uri)), - JSON_BUILD_PAIR("data", JSON_BUILD_BASE64(encrypted_key, encrypted_key_size)), - JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRING(hashed)))); + r = sd_json_build(&e, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("uri", SD_JSON_BUILD_STRING(uri)), + SD_JSON_BUILD_PAIR("data", SD_JSON_BUILD_BASE64(encrypted_key, encrypted_key_size)), + SD_JSON_BUILD_PAIR("hashedPassword", SD_JSON_BUILD_STRING(hashed)))); if (r < 0) return log_error_errno(r, "Failed to build encrypted JSON key object: %m"); - w = json_variant_ref(json_variant_by_key(*v, "privileged")); - l = json_variant_ref(json_variant_by_key(w, "pkcs11EncryptedKey")); + w = sd_json_variant_ref(sd_json_variant_by_key(*v, "privileged")); + l = sd_json_variant_ref(sd_json_variant_by_key(w, "pkcs11EncryptedKey")); - r = json_variant_append_array(&l, e); + r = sd_json_variant_append_array(&l, e); if (r < 0) return log_error_errno(r, "Failed append PKCS#11 encrypted key: %m"); - r = json_variant_set_field(&w, "pkcs11EncryptedKey", l); + r = sd_json_variant_set_field(&w, "pkcs11EncryptedKey", l); if (r < 0) return log_error_errno(r, "Failed to set PKCS#11 encrypted key: %m"); - r = json_variant_set_field(v, "privileged", w); + r = sd_json_variant_set_field(v, "privileged", w); if (r < 0) return log_error_errno(r, "Failed to update privileged field: %m"); return 0; } -int identity_add_pkcs11_key_data(JsonVariant **v, const char *uri) { +int identity_add_pkcs11_key_data(sd_json_variant **v, const char *uri) { _cleanup_(erase_and_freep) void *decrypted_key = NULL, *saved_key = NULL; _cleanup_(erase_and_freep) char *pin = NULL; size_t decrypted_key_size, saved_key_size; diff --git a/src/home/homectl-pkcs11.h b/src/home/homectl-pkcs11.h index 424777facd..994198f2ee 100644 --- a/src/home/homectl-pkcs11.h +++ b/src/home/homectl-pkcs11.h @@ -1,14 +1,14 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "json.h" +#include "sd-json.h" -int identity_add_token_pin(JsonVariant **v, const char *pin); +int identity_add_token_pin(sd_json_variant **v, const char *pin); #if HAVE_P11KIT -int identity_add_pkcs11_key_data(JsonVariant **v, const char *token_uri); +int identity_add_pkcs11_key_data(sd_json_variant **v, const char *token_uri); #else -static inline int identity_add_pkcs11_key_data(JsonVariant **v, const char *token_uri) { +static inline int identity_add_pkcs11_key_data(sd_json_variant **v, const char *token_uri) { return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "PKCS#11 tokens not supported on this build."); } #endif diff --git a/src/home/homectl-recovery-key.c b/src/home/homectl-recovery-key.c index bf18ae49e4..c27063009d 100644 --- a/src/home/homectl-recovery-key.c +++ b/src/home/homectl-recovery-key.c @@ -3,6 +3,7 @@ #include "errno-util.h" #include "glyph-util.h" #include "homectl-recovery-key.h" +#include "json-util.h" #include "libcrypt-util.h" #include "memory-util.h" #include "qrcode-util.h" @@ -11,46 +12,46 @@ #include "strv.h" #include "terminal-util.h" -static int add_privileged(JsonVariant **v, const char *hashed) { - _cleanup_(json_variant_unrefp) JsonVariant *e = NULL, *w = NULL, *l = NULL; +static int add_privileged(sd_json_variant **v, const char *hashed) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL, *w = NULL, *l = NULL; int r; assert(v); assert(hashed); - r = json_build(&e, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("modhex64")), - JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRING(hashed)))); + r = sd_json_build(&e, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("modhex64")), + SD_JSON_BUILD_PAIR("hashedPassword", SD_JSON_BUILD_STRING(hashed)))); if (r < 0) return log_error_errno(r, "Failed to build recover key JSON object: %m"); - json_variant_sensitive(e); + sd_json_variant_sensitive(e); - w = json_variant_ref(json_variant_by_key(*v, "privileged")); - l = json_variant_ref(json_variant_by_key(w, "recoveryKey")); + w = sd_json_variant_ref(sd_json_variant_by_key(*v, "privileged")); + l = sd_json_variant_ref(sd_json_variant_by_key(w, "recoveryKey")); - r = json_variant_append_array(&l, e); + r = sd_json_variant_append_array(&l, e); if (r < 0) return log_error_errno(r, "Failed append recovery key: %m"); - r = json_variant_set_field(&w, "recoveryKey", l); + r = sd_json_variant_set_field(&w, "recoveryKey", l); if (r < 0) return log_error_errno(r, "Failed to set recovery key array: %m"); - r = json_variant_set_field(v, "privileged", w); + r = sd_json_variant_set_field(v, "privileged", w); if (r < 0) return log_error_errno(r, "Failed to update privileged field: %m"); return 0; } -static int add_public(JsonVariant **v) { +static int add_public(sd_json_variant **v) { _cleanup_strv_free_ char **types = NULL; int r; assert(v); - r = json_variant_strv(json_variant_by_key(*v, "recoveryKeyType"), &types); + r = sd_json_variant_strv(sd_json_variant_by_key(*v, "recoveryKeyType"), &types); if (r < 0) return log_error_errno(r, "Failed to parse recovery key type list: %m"); @@ -58,25 +59,25 @@ static int add_public(JsonVariant **v) { if (r < 0) return log_oom(); - r = json_variant_set_field_strv(v, "recoveryKeyType", types); + r = sd_json_variant_set_field_strv(v, "recoveryKeyType", types); if (r < 0) return log_error_errno(r, "Failed to update recovery key types: %m"); return 0; } -static int add_secret(JsonVariant **v, const char *password) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL, *l = NULL; +static int add_secret(sd_json_variant **v, const char *password) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL, *l = NULL; _cleanup_strv_free_erase_ char **passwords = NULL; int r; assert(v); assert(password); - w = json_variant_ref(json_variant_by_key(*v, "secret")); - l = json_variant_ref(json_variant_by_key(w, "password")); + w = sd_json_variant_ref(sd_json_variant_by_key(*v, "secret")); + l = sd_json_variant_ref(sd_json_variant_by_key(w, "password")); - r = json_variant_strv(l, &passwords); + r = sd_json_variant_strv(l, &passwords); if (r < 0) return log_error_errno(r, "Failed to convert password array: %m"); @@ -84,24 +85,24 @@ static int add_secret(JsonVariant **v, const char *password) { if (r < 0) return log_oom(); - r = json_variant_new_array_strv(&l, passwords); + r = sd_json_variant_new_array_strv(&l, passwords); if (r < 0) return log_error_errno(r, "Failed to allocate new password array JSON: %m"); - json_variant_sensitive(l); + sd_json_variant_sensitive(l); - r = json_variant_set_field(&w, "password", l); + r = sd_json_variant_set_field(&w, "password", l); if (r < 0) return log_error_errno(r, "Failed to update password field: %m"); - r = json_variant_set_field(v, "secret", w); + r = sd_json_variant_set_field(v, "secret", w); if (r < 0) return log_error_errno(r, "Failed to update secret object: %m"); return 0; } -int identity_add_recovery_key(JsonVariant **v) { +int identity_add_recovery_key(sd_json_variant **v) { _cleanup_(erase_and_freep) char *password = NULL, *hashed = NULL; int r; diff --git a/src/home/homectl-recovery-key.h b/src/home/homectl-recovery-key.h index ab195f94db..387ae64df5 100644 --- a/src/home/homectl-recovery-key.h +++ b/src/home/homectl-recovery-key.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "json.h" +#include "sd-json.h" -int identity_add_recovery_key(JsonVariant **v); +int identity_add_recovery_key(sd_json_variant **v); diff --git a/src/home/homectl.c b/src/home/homectl.c index d9321a2bd9..535422537e 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -27,6 +27,7 @@ #include "homectl-fido2.h" #include "homectl-pkcs11.h" #include "homectl-recovery-key.h" +#include "json-util.h" #include "libfido2-util.h" #include "locale-util.h" #include "main-func.h" @@ -63,10 +64,10 @@ static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; static const char *arg_host = NULL; static bool arg_offline = false; static const char *arg_identity = NULL; -static JsonVariant *arg_identity_extra = NULL; -static JsonVariant *arg_identity_extra_privileged = NULL; -static JsonVariant *arg_identity_extra_this_machine = NULL; -static JsonVariant *arg_identity_extra_rlimits = NULL; +static sd_json_variant *arg_identity_extra = NULL; +static sd_json_variant *arg_identity_extra_privileged = NULL; +static sd_json_variant *arg_identity_extra_this_machine = NULL; +static sd_json_variant *arg_identity_extra_rlimits = NULL; static char **arg_identity_filter = NULL; /* this one is also applied to 'privileged' and 'thisMachine' subobjects */ static char **arg_identity_filter_rlimits = NULL; static uint64_t arg_disk_size = UINT64_MAX; @@ -80,7 +81,7 @@ static int arg_fido2_cred_alg = COSE_ES256; static int arg_fido2_cred_alg = 0; #endif static bool arg_recovery_key = false; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static bool arg_and_resize = false; static bool arg_and_change_password = false; static enum { @@ -95,10 +96,10 @@ static char *arg_blob_dir = NULL; static bool arg_blob_clear = false; static Hashmap *arg_blob_files = NULL; -STATIC_DESTRUCTOR_REGISTER(arg_identity_extra, json_variant_unrefp); -STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine, json_variant_unrefp); -STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged, json_variant_unrefp); -STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits, json_variant_unrefp); +STATIC_DESTRUCTOR_REGISTER(arg_identity_extra, sd_json_variant_unrefp); +STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_this_machine, sd_json_variant_unrefp); +STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_privileged, sd_json_variant_unrefp); +STATIC_DESTRUCTOR_REGISTER(arg_identity_extra_rlimits, sd_json_variant_unrefp); STATIC_DESTRUCTOR_REGISTER(arg_identity_filter, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_identity_filter_rlimits, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri, strv_freep); @@ -111,10 +112,10 @@ static const BusLocator *bus_mgr; static bool identity_properties_specified(void) { return arg_identity || - !json_variant_is_blank_object(arg_identity_extra) || - !json_variant_is_blank_object(arg_identity_extra_privileged) || - !json_variant_is_blank_object(arg_identity_extra_this_machine) || - !json_variant_is_blank_object(arg_identity_extra_rlimits) || + !sd_json_variant_is_blank_object(arg_identity_extra) || + !sd_json_variant_is_blank_object(arg_identity_extra_privileged) || + !sd_json_variant_is_blank_object(arg_identity_extra_this_machine) || + !sd_json_variant_is_blank_object(arg_identity_extra_rlimits) || !strv_isempty(arg_identity_filter) || !strv_isempty(arg_identity_filter_rlimits) || !strv_isempty(arg_pkcs11_token_uri) || @@ -203,7 +204,7 @@ static int list_homes(int argc, char *argv[], void *userdata) { if (r < 0) return bus_log_parse_error(r); - if (!table_isempty(table) || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (!table_isempty(table) || !FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { r = table_set_sort(table, (size_t) 0); if (r < 0) return table_log_sort_error(r); @@ -213,7 +214,7 @@ static int list_homes(int argc, char *argv[], void *userdata) { return r; } - if (arg_legend && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (arg_legend && !FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { if (table_isempty(table)) printf("No home areas.\n"); else @@ -671,7 +672,7 @@ static void dump_home_record(UserRecord *hr) { log_warning("Warning: lacking rights to acquire privileged fields of user record of '%s', output incomplete.", hr->user_name); } - if (arg_json_format_flags & JSON_FORMAT_OFF) + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) user_record_show(hr, true); else { _cleanup_(user_record_unrefp) UserRecord *stripped = NULL; @@ -687,7 +688,7 @@ static void dump_home_record(UserRecord *hr) { if (stripped) hr = stripped; - json_variant_dump(hr->json, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(hr->json, arg_json_format_flags, stdout, NULL); } } @@ -734,7 +735,7 @@ static int inspect_home(int argc, char *argv[], void *userdata) { STRV_FOREACH(i, items) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; const char *json; int incomplete; @@ -770,7 +771,7 @@ static int inspect_home(int argc, char *argv[], void *userdata) { continue; } - r = json_parse(json, JSON_PARSE_SENSITIVE, &v, NULL, NULL); + r = sd_json_parse(json, SD_JSON_PARSE_SENSITIVE, &v, NULL, NULL); if (r < 0) { log_error_errno(r, "Failed to parse JSON identity: %m"); if (ret == 0) @@ -854,8 +855,8 @@ static int authenticate_home(int argc, char *argv[], void *userdata) { return ret; } -static int update_last_change(JsonVariant **v, bool with_password, bool override) { - JsonVariant *c; +static int update_last_change(sd_json_variant **v, bool with_password, bool override) { + sd_json_variant *c; usec_t n; int r; @@ -863,22 +864,22 @@ static int update_last_change(JsonVariant **v, bool with_password, bool override n = now(CLOCK_REALTIME); - c = json_variant_by_key(*v, "lastChangeUSec"); + c = sd_json_variant_by_key(*v, "lastChangeUSec"); if (c) { uint64_t u; if (!override) goto update_password; - if (!json_variant_is_unsigned(c)) + if (!sd_json_variant_is_unsigned(c)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "lastChangeUSec field is not an unsigned integer, refusing."); - u = json_variant_unsigned(c); + u = sd_json_variant_unsigned(c); if (u >= n) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "lastChangeUSec is from the future, can't update."); } - r = json_variant_set_field_unsigned(v, "lastChangeUSec", n); + r = sd_json_variant_set_field_unsigned(v, "lastChangeUSec", n); if (r < 0) return log_error_errno(r, "Failed to update lastChangeUSec: %m"); @@ -886,231 +887,231 @@ update_password: if (!with_password) return 0; - c = json_variant_by_key(*v, "lastPasswordChangeUSec"); + c = sd_json_variant_by_key(*v, "lastPasswordChangeUSec"); if (c) { uint64_t u; if (!override) return 0; - if (!json_variant_is_unsigned(c)) + if (!sd_json_variant_is_unsigned(c)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "lastPasswordChangeUSec field is not an unsigned integer, refusing."); - u = json_variant_unsigned(c); + u = sd_json_variant_unsigned(c); if (u >= n) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "lastPasswordChangeUSec is from the future, can't update."); } - r = json_variant_set_field_unsigned(v, "lastPasswordChangeUSec", n); + r = sd_json_variant_set_field_unsigned(v, "lastPasswordChangeUSec", n); if (r < 0) return log_error_errno(r, "Failed to update lastPasswordChangeUSec: %m"); return 1; } -static int apply_identity_changes(JsonVariant **_v) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +static int apply_identity_changes(sd_json_variant **_v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(_v); - v = json_variant_ref(*_v); + v = sd_json_variant_ref(*_v); - r = json_variant_filter(&v, arg_identity_filter); + r = sd_json_variant_filter(&v, arg_identity_filter); if (r < 0) return log_error_errno(r, "Failed to filter identity: %m"); - r = json_variant_merge_object(&v, arg_identity_extra); + r = sd_json_variant_merge_object(&v, arg_identity_extra); if (r < 0) return log_error_errno(r, "Failed to merge identities: %m"); if (arg_identity_extra_this_machine || !strv_isempty(arg_identity_filter)) { - _cleanup_(json_variant_unrefp) JsonVariant *per_machine = NULL, *mmid = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *per_machine = NULL, *mmid = NULL; sd_id128_t mid; r = sd_id128_get_machine(&mid); if (r < 0) return log_error_errno(r, "Failed to acquire machine ID: %m"); - r = json_variant_new_string(&mmid, SD_ID128_TO_STRING(mid)); + r = sd_json_variant_new_string(&mmid, SD_ID128_TO_STRING(mid)); if (r < 0) return log_error_errno(r, "Failed to allocate matchMachineId object: %m"); - per_machine = json_variant_ref(json_variant_by_key(v, "perMachine")); + per_machine = sd_json_variant_ref(sd_json_variant_by_key(v, "perMachine")); if (per_machine) { - _cleanup_(json_variant_unrefp) JsonVariant *npm = NULL, *add = NULL; - _cleanup_free_ JsonVariant **array = NULL; - JsonVariant *z; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *npm = NULL, *add = NULL; + _cleanup_free_ sd_json_variant **array = NULL; + sd_json_variant *z; size_t i = 0; - if (!json_variant_is_array(per_machine)) + if (!sd_json_variant_is_array(per_machine)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "perMachine field is not an array, refusing."); - array = new(JsonVariant*, json_variant_elements(per_machine) + 1); + array = new(sd_json_variant*, sd_json_variant_elements(per_machine) + 1); if (!array) return log_oom(); JSON_VARIANT_ARRAY_FOREACH(z, per_machine) { - JsonVariant *u; + sd_json_variant *u; - if (!json_variant_is_object(z)) + if (!sd_json_variant_is_object(z)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "perMachine entry is not an object, refusing."); array[i++] = z; - u = json_variant_by_key(z, "matchMachineId"); + u = sd_json_variant_by_key(z, "matchMachineId"); if (!u) continue; - if (!json_variant_equal(u, mmid)) + if (!sd_json_variant_equal(u, mmid)) continue; - r = json_variant_merge_object(&add, z); + r = sd_json_variant_merge_object(&add, z); if (r < 0) return log_error_errno(r, "Failed to merge perMachine entry: %m"); i--; } - r = json_variant_filter(&add, arg_identity_filter); + r = sd_json_variant_filter(&add, arg_identity_filter); if (r < 0) return log_error_errno(r, "Failed to filter perMachine: %m"); - r = json_variant_merge_object(&add, arg_identity_extra_this_machine); + r = sd_json_variant_merge_object(&add, arg_identity_extra_this_machine); if (r < 0) return log_error_errno(r, "Failed to merge in perMachine fields: %m"); if (arg_identity_filter_rlimits || arg_identity_extra_rlimits) { - _cleanup_(json_variant_unrefp) JsonVariant *rlv = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *rlv = NULL; - rlv = json_variant_ref(json_variant_by_key(add, "resourceLimits")); + rlv = sd_json_variant_ref(sd_json_variant_by_key(add, "resourceLimits")); - r = json_variant_filter(&rlv, arg_identity_filter_rlimits); + r = sd_json_variant_filter(&rlv, arg_identity_filter_rlimits); if (r < 0) return log_error_errno(r, "Failed to filter resource limits: %m"); - r = json_variant_merge_object(&rlv, arg_identity_extra_rlimits); + r = sd_json_variant_merge_object(&rlv, arg_identity_extra_rlimits); if (r < 0) return log_error_errno(r, "Failed to set resource limits: %m"); - if (json_variant_is_blank_object(rlv)) { - r = json_variant_filter(&add, STRV_MAKE("resourceLimits")); + if (sd_json_variant_is_blank_object(rlv)) { + r = sd_json_variant_filter(&add, STRV_MAKE("resourceLimits")); if (r < 0) return log_error_errno(r, "Failed to drop resource limits field from identity: %m"); } else { - r = json_variant_set_field(&add, "resourceLimits", rlv); + r = sd_json_variant_set_field(&add, "resourceLimits", rlv); if (r < 0) return log_error_errno(r, "Failed to update resource limits of identity: %m"); } } - if (!json_variant_is_blank_object(add)) { - r = json_variant_set_field(&add, "matchMachineId", mmid); + if (!sd_json_variant_is_blank_object(add)) { + r = sd_json_variant_set_field(&add, "matchMachineId", mmid); if (r < 0) return log_error_errno(r, "Failed to set matchMachineId field: %m"); array[i++] = add; } - r = json_variant_new_array(&npm, array, i); + r = sd_json_variant_new_array(&npm, array, i); if (r < 0) return log_error_errno(r, "Failed to allocate new perMachine array: %m"); - json_variant_unref(per_machine); + sd_json_variant_unref(per_machine); per_machine = TAKE_PTR(npm); } else { - _cleanup_(json_variant_unrefp) JsonVariant *item = json_variant_ref(arg_identity_extra_this_machine); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *item = sd_json_variant_ref(arg_identity_extra_this_machine); if (arg_identity_extra_rlimits) { - r = json_variant_set_field(&item, "resourceLimits", arg_identity_extra_rlimits); + r = sd_json_variant_set_field(&item, "resourceLimits", arg_identity_extra_rlimits); if (r < 0) return log_error_errno(r, "Failed to update resource limits of identity: %m"); } - r = json_variant_set_field(&item, "matchMachineId", mmid); + r = sd_json_variant_set_field(&item, "matchMachineId", mmid); if (r < 0) return log_error_errno(r, "Failed to set matchMachineId field: %m"); - r = json_variant_append_array(&per_machine, item); + r = sd_json_variant_append_array(&per_machine, item); if (r < 0) return log_error_errno(r, "Failed to append to perMachine array: %m"); } - r = json_variant_set_field(&v, "perMachine", per_machine); + r = sd_json_variant_set_field(&v, "perMachine", per_machine); if (r < 0) return log_error_errno(r, "Failed to update per machine record: %m"); } if (arg_identity_extra_privileged || arg_identity_filter) { - _cleanup_(json_variant_unrefp) JsonVariant *privileged = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *privileged = NULL; - privileged = json_variant_ref(json_variant_by_key(v, "privileged")); + privileged = sd_json_variant_ref(sd_json_variant_by_key(v, "privileged")); - r = json_variant_filter(&privileged, arg_identity_filter); + r = sd_json_variant_filter(&privileged, arg_identity_filter); if (r < 0) return log_error_errno(r, "Failed to filter identity (privileged part): %m"); - r = json_variant_merge_object(&privileged, arg_identity_extra_privileged); + r = sd_json_variant_merge_object(&privileged, arg_identity_extra_privileged); if (r < 0) return log_error_errno(r, "Failed to merge identities (privileged part): %m"); - if (json_variant_is_blank_object(privileged)) { - r = json_variant_filter(&v, STRV_MAKE("privileged")); + if (sd_json_variant_is_blank_object(privileged)) { + r = sd_json_variant_filter(&v, STRV_MAKE("privileged")); if (r < 0) return log_error_errno(r, "Failed to drop privileged part from identity: %m"); } else { - r = json_variant_set_field(&v, "privileged", privileged); + r = sd_json_variant_set_field(&v, "privileged", privileged); if (r < 0) return log_error_errno(r, "Failed to update privileged part of identity: %m"); } } if (arg_identity_filter_rlimits) { - _cleanup_(json_variant_unrefp) JsonVariant *rlv = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *rlv = NULL; - rlv = json_variant_ref(json_variant_by_key(v, "resourceLimits")); + rlv = sd_json_variant_ref(sd_json_variant_by_key(v, "resourceLimits")); - r = json_variant_filter(&rlv, arg_identity_filter_rlimits); + r = sd_json_variant_filter(&rlv, arg_identity_filter_rlimits); if (r < 0) return log_error_errno(r, "Failed to filter resource limits: %m"); /* Note that we only filter resource limits here, but don't apply them. We do that in the perMachine section */ - if (json_variant_is_blank_object(rlv)) { - r = json_variant_filter(&v, STRV_MAKE("resourceLimits")); + if (sd_json_variant_is_blank_object(rlv)) { + r = sd_json_variant_filter(&v, STRV_MAKE("resourceLimits")); if (r < 0) return log_error_errno(r, "Failed to drop resource limits field from identity: %m"); } else { - r = json_variant_set_field(&v, "resourceLimits", rlv); + r = sd_json_variant_set_field(&v, "resourceLimits", rlv); if (r < 0) return log_error_errno(r, "Failed to update resource limits of identity: %m"); } } - json_variant_unref(*_v); + sd_json_variant_unref(*_v); *_v = TAKE_PTR(v); return 0; } -static int add_disposition(JsonVariant **v) { +static int add_disposition(sd_json_variant **v) { int r; assert(v); - if (json_variant_by_key(*v, "disposition")) + if (sd_json_variant_by_key(*v, "disposition")) return 0; /* Set the disposition to regular, if not configured explicitly */ - r = json_variant_set_field_string(v, "disposition", "regular"); + r = sd_json_variant_set_field_string(v, "disposition", "regular"); if (r < 0) return log_error_errno(r, "Failed to set disposition field: %m"); return 1; } -static int acquire_new_home_record(JsonVariant *input, UserRecord **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +static int acquire_new_home_record(sd_json_variant *input, UserRecord **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; int r; @@ -1122,13 +1123,13 @@ static int acquire_new_home_record(JsonVariant *input, UserRecord **ret) { if (input) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Two identity records specified, refusing."); - r = json_parse_file( + r = sd_json_parse_file( streq(arg_identity, "-") ? stdin : NULL, - streq(arg_identity, "-") ? "<stdin>" : arg_identity, JSON_PARSE_SENSITIVE, &v, &line, &column); + streq(arg_identity, "-") ? "<stdin>" : arg_identity, SD_JSON_PARSE_SENSITIVE, &v, &line, &column); if (r < 0) return log_error_errno(r, "Failed to parse identity at %u:%u: %m", line, column); } else - v = json_variant_ref(input); + v = sd_json_variant_ref(input); r = apply_identity_changes(&v); if (r < 0) @@ -1161,7 +1162,7 @@ static int acquire_new_home_record(JsonVariant *input, UserRecord **ret) { return r; if (DEBUG_LOGGING) - json_variant_dump(v, JSON_FORMAT_PRETTY, NULL, NULL); + sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY, NULL, NULL); hr = user_record_new(); if (!hr) @@ -1402,7 +1403,7 @@ static int bus_message_append_blobs(sd_bus_message *m, Hashmap *blobs) { return sd_bus_message_close_container(m); } -static int create_home_common(JsonVariant *input) { +static int create_home_common(sd_json_variant *input) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; _cleanup_hashmap_free_ Hashmap *blobs = NULL; @@ -1464,7 +1465,7 @@ static int create_home_common(JsonVariant *input) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; _cleanup_(erase_and_freep) char *formatted = NULL; - r = json_variant_format(hr->json, 0, &formatted); + r = sd_json_variant_format(hr->json, 0, &formatted); if (r < 0) return log_error_errno(r, "Failed to format user record: %m"); @@ -1520,7 +1521,7 @@ static int create_home(int argc, char *argv[], void *userdata) { /* If a username was specified, use it */ if (valid_user_group_name(argv[1], 0)) - r = json_variant_set_field_string(&arg_identity_extra, "userName", argv[1]); + r = sd_json_variant_set_field_string(&arg_identity_extra, "userName", argv[1]); else { _cleanup_free_ char *un = NULL, *rr = NULL; @@ -1530,12 +1531,12 @@ static int create_home(int argc, char *argv[], void *userdata) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "User name '%s' is not valid.", argv[1]); if (rr) { - r = json_variant_set_field_string(&arg_identity_extra, "realm", rr); + r = sd_json_variant_set_field_string(&arg_identity_extra, "realm", rr); if (r < 0) return log_error_errno(r, "Failed to set realm field: %m"); } - r = json_variant_set_field_string(&arg_identity_extra, "userName", un); + r = sd_json_variant_set_field_string(&arg_identity_extra, "userName", un); } if (r < 0) return log_error_errno(r, "Failed to set userName field: %m"); @@ -1586,7 +1587,7 @@ static int acquire_updated_home_record( const char *username, UserRecord **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; int r; @@ -1594,23 +1595,23 @@ static int acquire_updated_home_record( if (arg_identity) { unsigned line, column; - JsonVariant *un; + sd_json_variant *un; - r = json_parse_file( + r = sd_json_parse_file( streq(arg_identity, "-") ? stdin : NULL, - streq(arg_identity, "-") ? "<stdin>" : arg_identity, JSON_PARSE_SENSITIVE, &json, &line, &column); + streq(arg_identity, "-") ? "<stdin>" : arg_identity, SD_JSON_PARSE_SENSITIVE, &json, &line, &column); if (r < 0) return log_error_errno(r, "Failed to parse identity at %u:%u: %m", line, column); - un = json_variant_by_key(json, "userName"); + un = sd_json_variant_by_key(json, "userName"); if (un) { - if (!json_variant_is_string(un) || (username && !streq(json_variant_string(un), username))) + if (!sd_json_variant_is_string(un) || (username && !streq(sd_json_variant_string(un), username))) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "User name specified on command line and in JSON record do not match."); } else { if (!username) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No username specified."); - r = json_variant_set_field_string(&arg_identity_extra, "userName", username); + r = sd_json_variant_set_field_string(&arg_identity_extra, "userName", username); if (r < 0) return log_error_errno(r, "Failed to set userName field: %m"); } @@ -1635,13 +1636,13 @@ static int acquire_updated_home_record( if (incomplete) return log_error_errno(SYNTHETIC_ERRNO(EACCES), "Lacking rights to acquire user record including privileged metadata, can't update record."); - r = json_parse(text, JSON_PARSE_SENSITIVE, &json, NULL, NULL); + r = sd_json_parse(text, SD_JSON_PARSE_SENSITIVE, &json, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse JSON identity: %m"); reply = sd_bus_message_unref(reply); - r = json_variant_filter(&json, STRV_MAKE("binding", "status", "signature", "blobManifest")); + r = sd_json_variant_filter(&json, STRV_MAKE("binding", "status", "signature", "blobManifest")); if (r < 0) return log_error_errno(r, "Failed to strip binding and status from record to update: %m"); } @@ -1669,7 +1670,7 @@ static int acquire_updated_home_record( return r; if (DEBUG_LOGGING) - json_variant_dump(json, JSON_FORMAT_PRETTY, NULL, NULL); + sd_json_variant_dump(json, SD_JSON_FORMAT_PRETTY, NULL, NULL); hr = user_record_new(); if (!hr) @@ -1768,7 +1769,7 @@ static int update_home(int argc, char *argv[], void *userdata) { if (r < 0) return bus_log_create_error(r); - r = json_variant_format(hr->json, 0, &formatted); + r = sd_json_variant_format(hr->json, 0, &formatted); if (r < 0) return log_error_errno(r, "Failed to format user record: %m"); @@ -2338,7 +2339,7 @@ static int create_from_credentials(void) { return log_error_errno(r, "Failed to enumerate credentials: %m"); FOREACH_ARRAY(i, des->entries, des->n_entries) { - _cleanup_(json_variant_unrefp) JsonVariant *identity = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *identity = NULL; struct dirent *de = *i; const char *e; @@ -2354,7 +2355,7 @@ static int create_from_credentials(void) { continue; } - r = json_parse_file_at( + r = sd_json_parse_file_at( /* f= */ NULL, fd, de->d_name, @@ -2367,20 +2368,20 @@ static int create_from_credentials(void) { continue; } - JsonVariant *un; - un = json_variant_by_key(identity, "userName"); + sd_json_variant *un; + un = sd_json_variant_by_key(identity, "userName"); if (un) { - if (!json_variant_is_string(un)) { + if (!sd_json_variant_is_string(un)) { log_warning("User record from credential '%s' contains 'userName' field of invalid type, ignoring.", de->d_name); continue; } - if (!streq(json_variant_string(un), e)) { - log_warning("User record from credential '%s' contains 'userName' field (%s) that doesn't match credential name (%s), ignoring.", de->d_name, json_variant_string(un), e); + if (!streq(sd_json_variant_string(un), e)) { + log_warning("User record from credential '%s' contains 'userName' field (%s) that doesn't match credential name (%s), ignoring.", de->d_name, sd_json_variant_string(un), e); continue; } } else { - r = json_variant_set_field_string(&identity, "userName", e); + r = sd_json_variant_set_field_string(&identity, "userName", e); if (r < 0) return log_warning_errno(r, "Failed to set userName field: %m"); } @@ -2467,7 +2468,7 @@ static int create_interactively(void) { log_notice("Specified user '%s' exists already, try again.", username); } - r = json_variant_set_field_string(&arg_identity_extra, "userName", username); + r = sd_json_variant_set_field_string(&arg_identity_extra, "userName", username); if (r < 0) return log_error_errno(r, "Failed to set userName field: %m"); @@ -2518,15 +2519,15 @@ static int drop_from_identity(const char *field) { return log_oom(); /* Let's also drop the field if it was previously set to a new value on the same command line */ - r = json_variant_filter(&arg_identity_extra, STRV_MAKE(field)); + r = sd_json_variant_filter(&arg_identity_extra, STRV_MAKE(field)); if (r < 0) return log_error_errno(r, "Failed to filter JSON identity data: %m"); - r = json_variant_filter(&arg_identity_extra_this_machine, STRV_MAKE(field)); + r = sd_json_variant_filter(&arg_identity_extra_this_machine, STRV_MAKE(field)); if (r < 0) return log_error_errno(r, "Failed to filter JSON identity data: %m"); - r = json_variant_filter(&arg_identity_extra_privileged, STRV_MAKE(field)); + r = sd_json_variant_filter(&arg_identity_extra_privileged, STRV_MAKE(field)); if (r < 0) return log_error_errno(r, "Failed to filter JSON identity data: %m"); @@ -2971,7 +2972,7 @@ static int parse_argv(int argc, char *argv[]) { if (!valid_gecos(optarg)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Real name '%s' not a valid GECOS field.", optarg); - r = json_variant_set_field_string(&arg_identity_extra, "realName", optarg); + r = sd_json_variant_set_field_string(&arg_identity_extra, "realName", optarg); if (r < 0) return log_error_errno(r, "Failed to set realName field: %m"); @@ -2995,7 +2996,7 @@ static int parse_argv(int argc, char *argv[]) { if (!valid_home(hd)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Home directory '%s' not valid.", hd); - r = json_variant_set_field_string(&arg_identity_extra, "homeDirectory", hd); + r = sd_json_variant_set_field_string(&arg_identity_extra, "homeDirectory", hd); if (r < 0) return log_error_errno(r, "Failed to set homeDirectory field: %m"); @@ -3017,7 +3018,7 @@ static int parse_argv(int argc, char *argv[]) { if (r == 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Realm '%s' is not a valid DNS domain.", optarg); - r = json_variant_set_field_string(&arg_identity_extra, "realm", optarg); + r = sd_json_variant_set_field_string(&arg_identity_extra, "realm", optarg); if (r < 0) return log_error_errno(r, "Failed to set realm field: %m"); break; @@ -3054,7 +3055,7 @@ static int parse_argv(int argc, char *argv[]) { break; } - r = json_variant_set_field_string(&arg_identity_extra, field, optarg); + r = sd_json_variant_set_field_string(&arg_identity_extra, field, optarg); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); @@ -3074,7 +3075,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to validate CIFS service name: %s", optarg); - r = json_variant_set_field_string(&arg_identity_extra, "cifsService", optarg); + r = sd_json_variant_set_field_string(&arg_identity_extra, "cifsService", optarg); if (r < 0) return log_error_errno(r, "Failed to set cifsService field: %m"); @@ -3089,7 +3090,7 @@ static int parse_argv(int argc, char *argv[]) { break; } - r = json_variant_set_field_string(&arg_identity_extra_privileged, "passwordHint", optarg); + r = sd_json_variant_set_field_string(&arg_identity_extra_privileged, "passwordHint", optarg); if (r < 0) return log_error_errno(r, "Failed to set passwordHint field: %m"); @@ -3110,7 +3111,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse nice level: %s", optarg); - r = json_variant_set_field_integer(&arg_identity_extra, "niceLevel", nc); + r = sd_json_variant_set_field_integer(&arg_identity_extra, "niceLevel", nc); if (r < 0) return log_error_errno(r, "Failed to set niceLevel field: %m"); @@ -3118,7 +3119,7 @@ static int parse_argv(int argc, char *argv[]) { } case ARG_RLIMIT: { - _cleanup_(json_variant_unrefp) JsonVariant *jcur = NULL, *jmax = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *jcur = NULL, *jmax = NULL; _cleanup_free_ char *field = NULL, *t = NULL; const char *eq; struct rlimit rl; @@ -3132,7 +3133,7 @@ static int parse_argv(int argc, char *argv[]) { return r; arg_identity_filter_rlimits = strv_free(arg_identity_filter_rlimits); - arg_identity_extra_rlimits = json_variant_unref(arg_identity_extra_rlimits); + arg_identity_extra_rlimits = sd_json_variant_unref(arg_identity_extra_rlimits); break; } @@ -3155,7 +3156,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return r; - r = json_variant_filter(&arg_identity_extra_rlimits, STRV_MAKE(field)); + r = sd_json_variant_filter(&arg_identity_extra_rlimits, STRV_MAKE(field)); if (r < 0) return log_error_errno(r, "Failed to filter JSON identity data: %m"); @@ -3166,11 +3167,11 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse resource limit value: %s", eq + 1); - r = rl.rlim_cur == RLIM_INFINITY ? json_variant_new_null(&jcur) : json_variant_new_unsigned(&jcur, rl.rlim_cur); + r = rl.rlim_cur == RLIM_INFINITY ? sd_json_variant_new_null(&jcur) : sd_json_variant_new_unsigned(&jcur, rl.rlim_cur); if (r < 0) return log_error_errno(r, "Failed to allocate current integer: %m"); - r = rl.rlim_max == RLIM_INFINITY ? json_variant_new_null(&jmax) : json_variant_new_unsigned(&jmax, rl.rlim_max); + r = rl.rlim_max == RLIM_INFINITY ? sd_json_variant_new_null(&jmax) : sd_json_variant_new_unsigned(&jmax, rl.rlim_max); if (r < 0) return log_error_errno(r, "Failed to allocate maximum integer: %m"); @@ -3178,11 +3179,11 @@ static int parse_argv(int argc, char *argv[]) { if (!t) return log_oom(); - r = json_variant_set_fieldb( + r = sd_json_variant_set_fieldb( &arg_identity_extra_rlimits, t, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("cur", JSON_BUILD_VARIANT(jcur)), - JSON_BUILD_PAIR("max", JSON_BUILD_VARIANT(jmax)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("cur", SD_JSON_BUILD_VARIANT(jcur)), + SD_JSON_BUILD_PAIR("max", SD_JSON_BUILD_VARIANT(jmax)))); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", rlimit_to_string(l)); @@ -3211,7 +3212,7 @@ static int parse_argv(int argc, char *argv[]) { if (uid == UID_NOBODY) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "UID " UID_FMT " is nobody UID, refusing.", uid); - r = json_variant_set_field_unsigned(&arg_identity_extra, "uid", uid); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, "uid", uid); if (r < 0) return log_error_errno(r, "Failed to set realm field: %m"); @@ -3235,7 +3236,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return r; - r = json_variant_set_field_string(&arg_identity_extra_this_machine, field, v); + r = sd_json_variant_set_field_string(&arg_identity_extra_this_machine, field, v); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", v); @@ -3254,7 +3255,7 @@ static int parse_argv(int argc, char *argv[]) { if (!valid_shell(optarg)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Shell '%s' not valid.", optarg); - r = json_variant_set_field_string(&arg_identity_extra, "shell", optarg); + r = sd_json_variant_set_field_string(&arg_identity_extra, "shell", optarg); if (r < 0) return log_error_errno(r, "Failed to set shell field: %m"); @@ -3262,8 +3263,8 @@ static int parse_argv(int argc, char *argv[]) { case ARG_SETENV: { _cleanup_free_ char **l = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *ne = NULL; - JsonVariant *e; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ne = NULL; + sd_json_variant *e; if (isempty(optarg)) { r = drop_from_identity("environment"); @@ -3273,9 +3274,9 @@ static int parse_argv(int argc, char *argv[]) { break; } - e = json_variant_by_key(arg_identity_extra, "environment"); + e = sd_json_variant_by_key(arg_identity_extra, "environment"); if (e) { - r = json_variant_strv(e, &l); + r = sd_json_variant_strv(e, &l); if (r < 0) return log_error_errno(r, "Failed to parse JSON environment field: %m"); } @@ -3286,11 +3287,11 @@ static int parse_argv(int argc, char *argv[]) { strv_sort(l); - r = json_variant_new_array_strv(&ne, l); + r = sd_json_variant_new_array_strv(&ne, l); if (r < 0) return log_error_errno(r, "Failed to allocate environment list JSON: %m"); - r = json_variant_set_field(&arg_identity_extra, "environment", ne); + r = sd_json_variant_set_field(&arg_identity_extra, "environment", ne); if (r < 0) return log_error_errno(r, "Failed to set environment list: %m"); @@ -3310,7 +3311,7 @@ static int parse_argv(int argc, char *argv[]) { if (!timezone_is_valid(optarg, LOG_DEBUG)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Timezone '%s' is not valid.", optarg); - r = json_variant_set_field_string(&arg_identity_extra, "timeZone", optarg); + r = sd_json_variant_set_field_string(&arg_identity_extra, "timeZone", optarg); if (r < 0) return log_error_errno(r, "Failed to set timezone field: %m"); @@ -3390,7 +3391,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse %s boolean: %m", field); - r = json_variant_set_field_boolean(&arg_identity_extra, field, r > 0); + r = sd_json_variant_set_field_boolean(&arg_identity_extra, field, r > 0); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); @@ -3398,7 +3399,7 @@ static int parse_argv(int argc, char *argv[]) { } case 'P': - r = json_variant_set_field_boolean(&arg_identity_extra, "enforcePasswordPolicy", false); + r = sd_json_variant_set_field_boolean(&arg_identity_extra, "enforcePasswordPolicy", false); if (r < 0) return log_error_errno(r, "Failed to set enforcePasswordPolicy field: %m"); @@ -3426,7 +3427,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return r; - r = json_variant_set_field_unsigned(&arg_identity_extra_this_machine, "diskSize", arg_disk_size); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra_this_machine, "diskSize", arg_disk_size); if (r < 0) return log_error_errno(r, "Failed to set diskSize field: %m"); @@ -3439,7 +3440,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return r; - r = json_variant_set_field_unsigned(&arg_identity_extra_this_machine, "diskSizeRelative", arg_disk_size_relative); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra_this_machine, "diskSizeRelative", arg_disk_size_relative); if (r < 0) return log_error_errno(r, "Failed to set diskSizeRelative field: %m"); @@ -3447,7 +3448,7 @@ static int parse_argv(int argc, char *argv[]) { } /* Automatically turn off the rebalance logic if user configured a size explicitly */ - r = json_variant_set_field_unsigned(&arg_identity_extra_this_machine, "rebalanceWeight", REBALANCE_WEIGHT_OFF); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra_this_machine, "rebalanceWeight", REBALANCE_WEIGHT_OFF); if (r < 0) return log_error_errno(r, "Failed to set rebalanceWeight field: %m"); @@ -3468,7 +3469,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Access mode '%s' not valid.", optarg); - r = json_variant_set_field_unsigned(&arg_identity_extra, "accessMode", mode); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, "accessMode", mode); if (r < 0) return log_error_errno(r, "Failed to set access mode field: %m"); @@ -3488,7 +3489,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse --luks-discard= parameter: %s", optarg); - r = json_variant_set_field_boolean(&arg_identity_extra, "luksDiscard", r); + r = sd_json_variant_set_field_boolean(&arg_identity_extra, "luksDiscard", r); if (r < 0) return log_error_errno(r, "Failed to set discard field: %m"); @@ -3507,7 +3508,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse --luks-offline-discard= parameter: %s", optarg); - r = json_variant_set_field_boolean(&arg_identity_extra, "luksOfflineDiscard", r); + r = sd_json_variant_set_field_boolean(&arg_identity_extra, "luksOfflineDiscard", r); if (r < 0) return log_error_errno(r, "Failed to set offline discard field: %m"); @@ -3536,7 +3537,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse %s parameter: %s", field, optarg); - r = json_variant_set_field_unsigned(&arg_identity_extra, field, n); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, field, n); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); @@ -3558,7 +3559,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return r; - r = json_variant_set_field_unsigned(&arg_identity_extra, "luksSectorSize", ss); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, "luksSectorSize", ss); if (r < 0) return log_error_errno(r, "Failed to set sector size field: %m"); @@ -3580,7 +3581,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse umask: %m"); - r = json_variant_set_field_integer(&arg_identity_extra, "umask", m); + r = sd_json_variant_set_field_integer(&arg_identity_extra, "umask", m); if (r < 0) return log_error_errno(r, "Failed to set umask field: %m"); @@ -3588,7 +3589,7 @@ static int parse_argv(int argc, char *argv[]) { } case ARG_SSH_AUTHORIZED_KEYS: { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_strv_free_ char **l = NULL, **add = NULL; if (isempty(optarg)) { @@ -3641,9 +3642,9 @@ static int parse_argv(int argc, char *argv[]) { return log_oom(); } - v = json_variant_ref(json_variant_by_key(arg_identity_extra_privileged, "sshAuthorizedKeys")); + v = sd_json_variant_ref(sd_json_variant_by_key(arg_identity_extra_privileged, "sshAuthorizedKeys")); if (v) { - r = json_variant_strv(v, &l); + r = sd_json_variant_strv(v, &l); if (r < 0) return log_error_errno(r, "Failed to parse SSH authorized keys list: %m"); } @@ -3652,13 +3653,13 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_oom(); - v = json_variant_unref(v); + v = sd_json_variant_unref(v); - r = json_variant_new_array_strv(&v, l); + r = sd_json_variant_new_array_strv(&v, l); if (r < 0) return log_oom(); - r = json_variant_set_field(&arg_identity_extra_privileged, "sshAuthorizedKeys", v); + r = sd_json_variant_set_field(&arg_identity_extra_privileged, "sshAuthorizedKeys", v); if (r < 0) return log_error_errno(r, "Failed to set authorized keys: %m"); @@ -3692,7 +3693,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse %s parameter: %m", field); - r = json_variant_set_field_unsigned(&arg_identity_extra, field, n); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, field, n); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); break; @@ -3725,7 +3726,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse %s parameter: %m", field); - r = json_variant_set_field_unsigned(&arg_identity_extra, field, n); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, field, n); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); break; @@ -3759,7 +3760,7 @@ static int parse_argv(int argc, char *argv[]) { if (!string_is_safe(optarg)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Parameter for %s field not valid: %s", field, optarg); - r = json_variant_set_field_string( + r = sd_json_variant_set_field_string( IN_SET(c, ARG_STORAGE, ARG_FS_TYPE) ? &arg_identity_extra_this_machine : &arg_identity_extra, field, optarg); @@ -3793,7 +3794,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse %s field: %s", field, optarg); - r = json_variant_set_field_unsigned(&arg_identity_extra, field, t); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, field, t); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); @@ -3812,7 +3813,7 @@ static int parse_argv(int argc, char *argv[]) { } for (;;) { - _cleanup_(json_variant_unrefp) JsonVariant *mo = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *mo = NULL; _cleanup_strv_free_ char **list = NULL; _cleanup_free_ char *word = NULL; @@ -3825,9 +3826,9 @@ static int parse_argv(int argc, char *argv[]) { if (!valid_user_group_name(word, 0)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid group name %s.", word); - mo = json_variant_ref(json_variant_by_key(arg_identity_extra, "memberOf")); + mo = sd_json_variant_ref(sd_json_variant_by_key(arg_identity_extra, "memberOf")); - r = json_variant_strv(mo, &list); + r = sd_json_variant_strv(mo, &list); if (r < 0) return log_error_errno(r, "Failed to parse group list: %m"); @@ -3838,12 +3839,12 @@ static int parse_argv(int argc, char *argv[]) { strv_sort(list); strv_uniq(list); - mo = json_variant_unref(mo); - r = json_variant_new_array_strv(&mo, list); + mo = sd_json_variant_unref(mo); + r = sd_json_variant_new_array_strv(&mo, list); if (r < 0) return log_error_errno(r, "Failed to create group list JSON: %m"); - r = json_variant_set_field(&arg_identity_extra, "memberOf", mo); + r = sd_json_variant_set_field(&arg_identity_extra, "memberOf", mo); if (r < 0) return log_error_errno(r, "Failed to update group list: %m"); } @@ -3865,7 +3866,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse --tasks-max= parameter: %s", optarg); - r = json_variant_set_field_unsigned(&arg_identity_extra, "tasksMax", u); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, "tasksMax", u); if (r < 0) return log_error_errno(r, "Failed to set tasksMax field: %m"); @@ -3895,7 +3896,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse %s parameter: %s", field, optarg); - r = json_variant_set_field_unsigned(&arg_identity_extra_this_machine, field, u); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra_this_machine, field, u); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); @@ -3924,7 +3925,7 @@ static int parse_argv(int argc, char *argv[]) { if (!CGROUP_WEIGHT_IS_OK(u)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Weight %" PRIu64 " is out of valid weight range.", u); - r = json_variant_set_field_unsigned(&arg_identity_extra, field, u); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, field, u); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); @@ -4055,7 +4056,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse --auto-resize-mode= argument: %s", optarg); - r = json_variant_set_field_string(&arg_identity_extra, "autoResizeMode", auto_resize_mode_to_string(r)); + r = sd_json_variant_set_field_string(&arg_identity_extra, "autoResizeMode", auto_resize_mode_to_string(r)); if (r < 0) return log_error_errno(r, "Failed to set autoResizeMode field: %m"); @@ -4089,7 +4090,7 @@ static int parse_argv(int argc, char *argv[]) { return r; /* Add to main identity */ - r = json_variant_set_field_unsigned(&arg_identity_extra, "rebalanceWeight", u); + r = sd_json_variant_set_field_unsigned(&arg_identity_extra, "rebalanceWeight", u); if (r < 0) return log_error_errno(r, "Failed to set rebalanceWeight field: %m"); @@ -4097,7 +4098,7 @@ static int parse_argv(int argc, char *argv[]) { } case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; break; case ARG_JSON: @@ -4115,9 +4116,9 @@ static int parse_argv(int argc, char *argv[]) { else return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specifying -E more than twice is not supported."); - arg_json_format_flags &= ~JSON_FORMAT_OFF; + arg_json_format_flags &= ~SD_JSON_FORMAT_OFF; if (arg_json_format_flags == 0) - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; break; case ARG_EXPORT_FORMAT: @@ -4156,7 +4157,7 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return r; - r = json_variant_set_field_boolean(&arg_identity_extra, "dropCaches", r); + r = sd_json_variant_set_field_boolean(&arg_identity_extra, "dropCaches", r); if (r < 0) return log_error_errno(r, "Failed to set drop caches field: %m"); @@ -4210,7 +4211,7 @@ static int parse_argv(int argc, char *argv[]) { if (capability_set_to_strv(updated, &l) < 0) return log_oom(); - r = json_variant_set_field_strv(&arg_identity_extra, field, l); + r = sd_json_variant_set_field_strv(&arg_identity_extra, field, l); if (r < 0) return log_error_errno(r, "Failed to set %s field: %m", field); @@ -4311,13 +4312,13 @@ static int parse_argv(int argc, char *argv[]) { if (!strv_isempty(arg_languages)) { char **additional; - r = json_variant_set_field_string(&arg_identity_extra, "preferredLanguage", arg_languages[0]); + r = sd_json_variant_set_field_string(&arg_identity_extra, "preferredLanguage", arg_languages[0]); if (r < 0) return log_error_errno(r, "Failed to update preferred language: %m"); additional = strv_skip(arg_languages, 1); if (!strv_isempty(additional)) { - r = json_variant_set_field_strv(&arg_identity_extra, "additionalLanguages", additional); + r = sd_json_variant_set_field_strv(&arg_identity_extra, "additionalLanguages", additional); if (r < 0) return log_error_errno(r, "Failed to update additional language list: %m"); } else { @@ -4408,7 +4409,7 @@ static int fallback_shell(int argc, char *argv[]) { for (unsigned n_tries = 0;; n_tries++) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; if (n_tries >= 5) return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), @@ -4423,7 +4424,7 @@ static int fallback_shell(int argc, char *argv[]) { if (r < 0) return bus_log_parse_error(r); - r = json_parse(json, JSON_PARSE_SENSITIVE, &v, NULL, NULL); + r = sd_json_parse(json, SD_JSON_PARSE_SENSITIVE, &v, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse JSON identity: %m"); diff --git a/src/home/homed-bus.c b/src/home/homed-bus.c index a6f26fea66..41c4ad65a3 100644 --- a/src/home/homed-bus.c +++ b/src/home/homed-bus.c @@ -7,7 +7,7 @@ #include "strv.h" int bus_message_read_secret(sd_bus_message *m, UserRecord **ret, sd_bus_error *error) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *full = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *full = NULL; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; unsigned line = 0, column = 0; const char *json; @@ -19,11 +19,11 @@ int bus_message_read_secret(sd_bus_message *m, UserRecord **ret, sd_bus_error *e if (r < 0) return r; - r = json_parse(json, JSON_PARSE_SENSITIVE, &v, &line, &column); + r = sd_json_parse(json, SD_JSON_PARSE_SENSITIVE, &v, &line, &column); if (r < 0) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to parse JSON secret record at %u:%u: %m", line, column); - r = json_build(&full, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("secret", JSON_BUILD_VARIANT(v)))); + r = sd_json_build(&full, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("secret", SD_JSON_BUILD_VARIANT(v)))); if (r < 0) return r; @@ -40,7 +40,7 @@ int bus_message_read_secret(sd_bus_message *m, UserRecord **ret, sd_bus_error *e } int bus_message_read_home_record(sd_bus_message *m, UserRecordLoadFlags flags, UserRecord **ret, sd_bus_error *error) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; unsigned line = 0, column = 0; const char *json; @@ -52,7 +52,7 @@ int bus_message_read_home_record(sd_bus_message *m, UserRecordLoadFlags flags, U if (r < 0) return r; - r = json_parse(json, JSON_PARSE_SENSITIVE, &v, &line, &column); + r = sd_json_parse(json, SD_JSON_PARSE_SENSITIVE, &v, &line, &column); if (r < 0) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to parse JSON identity record at %u:%u: %m", line, column); diff --git a/src/home/homed-bus.h b/src/home/homed-bus.h index 0660a5936c..c7e11efdfb 100644 --- a/src/home/homed-bus.h +++ b/src/home/homed-bus.h @@ -2,9 +2,9 @@ #pragma once #include "sd-bus.h" +#include "sd-json.h" #include "hashmap.h" -#include "json.h" #include "user-record.h" int bus_message_read_secret(sd_bus_message *m, UserRecord **ret, sd_bus_error *error); diff --git a/src/home/homed-home-bus.c b/src/home/homed-home-bus.c index 23578fe314..290e05ac6b 100644 --- a/src/home/homed-home-bus.c +++ b/src/home/homed-home-bus.c @@ -133,7 +133,7 @@ int bus_home_get_record_json( if (r < 0) return r; - r = json_variant_format(augmented->json, 0, ret); + r = sd_json_variant_format(augmented->json, 0, ret); if (r < 0) return r; diff --git a/src/home/homed-home.c b/src/home/homed-home.c index 757881c2e6..10c24d1dc5 100644 --- a/src/home/homed-home.c +++ b/src/home/homed-home.c @@ -23,8 +23,9 @@ #include "fs-util.h" #include "glyph-util.h" #include "home-util.h" -#include "homed-home-bus.h" #include "homed-home.h" +#include "homed-home-bus.h" +#include "json-util.h" #include "memfd-util.h" #include "missing_magic.h" #include "missing_mman.h" @@ -266,12 +267,12 @@ int home_set_record(Home *h, UserRecord *hr) { return -EINVAL; if (FLAGS_SET(h->record->mask, USER_RECORD_STATUS)) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; /* Hmm, the existing record has status fields? If so, copy them over */ - v = json_variant_ref(hr->json); - r = json_variant_set_field(&v, "status", json_variant_by_key(h->record->json, "status")); + v = sd_json_variant_ref(hr->json); + r = sd_json_variant_set_field(&v, "status", sd_json_variant_by_key(h->record->json, "status")); if (r < 0) return r; @@ -308,19 +309,19 @@ int home_set_record(Home *h, UserRecord *hr) { } int home_save_record(Home *h) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *text = NULL; const char *fn; int r; assert(h); - v = json_variant_ref(h->record->json); - r = json_variant_normalize(&v); + v = sd_json_variant_ref(h->record->json); + r = sd_json_variant_normalize(&v); if (r < 0) log_warning_errno(r, "User record could not be normalized."); - r = json_variant_format(v, JSON_FORMAT_PRETTY|JSON_FORMAT_NEWLINE, &text); + r = sd_json_variant_format(v, SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_NEWLINE, &text); if (r < 0) return r; @@ -523,7 +524,7 @@ static void home_set_state(Home *h, HomeState state) { } static int home_parse_worker_stdout(int _fd, UserRecord **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_close_ int fd = _fd; /* take possession, even on failure */ _cleanup_(user_record_unrefp) UserRecord *hr = NULL; _cleanup_fclose_ FILE *f = NULL; @@ -559,7 +560,7 @@ static int home_parse_worker_stdout(int _fd, UserRecord **ret) { rewind(f); } - r = json_parse_file(f, "stdout", JSON_PARSE_SENSITIVE, &v, &line, &column); + r = sd_json_parse_file(f, "stdout", SD_JSON_PARSE_SENSITIVE, &v, &line, &column); if (r < 0) return log_error_errno(r, "Failed to parse identity at %u:%u: %m", line, column); @@ -1199,7 +1200,7 @@ static int home_start_work( UserRecord *secret, Hashmap *blobs, uint64_t flags) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *fdmap = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *fdmap = NULL; _cleanup_(erase_and_freep) char *formatted = NULL; _cleanup_close_ int stdin_fd = -EBADF, stdout_fd = -EBADF; _cleanup_free_ int *blob_fds = NULL; @@ -1216,16 +1217,16 @@ static int home_start_work( assert(h->worker_stdout_fd < 0); assert(!h->worker_event_source); - v = json_variant_ref(hr->json); + v = sd_json_variant_ref(hr->json); if (secret) { - JsonVariant *sub = NULL; + sd_json_variant *sub = NULL; - sub = json_variant_by_key(secret->json, "secret"); + sub = sd_json_variant_by_key(secret->json, "secret"); if (!sub) return -ENOKEY; - r = json_variant_set_field(&v, "secret", sub); + r = sd_json_variant_set_field(&v, "secret", sub); if (r < 0) return r; } @@ -1242,26 +1243,26 @@ static int home_start_work( /* homework needs to be able to tell the difference between blobs being null * (the fdmap field is completely missing) and it being empty (the field is an * empty object) */ - r = json_variant_new_object(&fdmap, NULL, 0); + r = sd_json_variant_new_object(&fdmap, NULL, 0); if (r < 0) return r; HASHMAP_FOREACH_KEY(fd_ptr, blob_filename, blobs) { blob_fds[i] = PTR_TO_FD(fd_ptr); - r = json_variant_set_field_integer(&fdmap, blob_filename, i); + r = sd_json_variant_set_field_integer(&fdmap, blob_filename, i); if (r < 0) return r; i++; } - r = json_variant_set_field(&v, HOMEWORK_BLOB_FDMAP_FIELD, fdmap); + r = sd_json_variant_set_field(&v, HOMEWORK_BLOB_FDMAP_FIELD, fdmap); if (r < 0) return r; } - r = json_variant_format(v, 0, &formatted); + r = sd_json_variant_format(v, 0, &formatted); if (r < 0) return r; @@ -1666,20 +1667,20 @@ int home_remove(Home *h, sd_bus_error *error) { } static int user_record_extend_with_binding(UserRecord *hr, UserRecord *with_binding, UserRecordLoadFlags flags, UserRecord **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *nr = NULL; - JsonVariant *binding; + sd_json_variant *binding; int r; assert(hr); assert(with_binding); assert(ret); - assert_se(v = json_variant_ref(hr->json)); + assert_se(v = sd_json_variant_ref(hr->json)); - binding = json_variant_by_key(with_binding->json, "binding"); + binding = sd_json_variant_by_key(with_binding->json, "binding"); if (binding) { - r = json_variant_set_field(&v, "binding", binding); + r = sd_json_variant_set_field(&v, "binding", binding); if (r < 0) return r; } @@ -2611,7 +2612,7 @@ int home_augment_status( UserRecord **ret) { uint64_t disk_size = UINT64_MAX, disk_usage = UINT64_MAX, disk_free = UINT64_MAX, disk_ceiling = UINT64_MAX, disk_floor = UINT64_MAX; - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL, *v = NULL, *m = NULL, *status = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL, *v = NULL, *m = NULL, *status = NULL; _cleanup_(user_record_unrefp) UserRecord *ur = NULL; statfs_f_type_t magic; const char *fstype; @@ -2653,42 +2654,42 @@ int home_augment_status( if (disk_ceiling == UINT64_MAX || disk_ceiling > USER_DISK_SIZE_MAX) disk_ceiling = USER_DISK_SIZE_MAX; - r = json_build(&status, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("state", JSON_BUILD_STRING(home_state_to_string(state))), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Home")), - JSON_BUILD_PAIR("useFallback", JSON_BUILD_BOOLEAN(!HOME_STATE_IS_ACTIVE(state))), - JSON_BUILD_PAIR("fallbackShell", JSON_BUILD_CONST_STRING(BINDIR "/systemd-home-fallback-shell")), - JSON_BUILD_PAIR("fallbackHomeDirectory", JSON_BUILD_CONST_STRING("/")), - JSON_BUILD_PAIR_CONDITION(disk_size != UINT64_MAX, "diskSize", JSON_BUILD_UNSIGNED(disk_size)), - JSON_BUILD_PAIR_CONDITION(disk_usage != UINT64_MAX, "diskUsage", JSON_BUILD_UNSIGNED(disk_usage)), - JSON_BUILD_PAIR_CONDITION(disk_free != UINT64_MAX, "diskFree", JSON_BUILD_UNSIGNED(disk_free)), - JSON_BUILD_PAIR_CONDITION(disk_ceiling != UINT64_MAX, "diskCeiling", JSON_BUILD_UNSIGNED(disk_ceiling)), - JSON_BUILD_PAIR_CONDITION(disk_floor != UINT64_MAX, "diskFloor", JSON_BUILD_UNSIGNED(disk_floor)), - JSON_BUILD_PAIR_CONDITION(h->signed_locally >= 0, "signedLocally", JSON_BUILD_BOOLEAN(h->signed_locally)), - JSON_BUILD_PAIR_CONDITION(fstype, "fileSystemType", JSON_BUILD_STRING(fstype)), - JSON_BUILD_PAIR_CONDITION(access_mode != MODE_INVALID, "accessMode", JSON_BUILD_UNSIGNED(access_mode)) + r = sd_json_build(&status, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("state", SD_JSON_BUILD_STRING(home_state_to_string(state))), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Home")), + SD_JSON_BUILD_PAIR("useFallback", SD_JSON_BUILD_BOOLEAN(!HOME_STATE_IS_ACTIVE(state))), + SD_JSON_BUILD_PAIR("fallbackShell", JSON_BUILD_CONST_STRING(BINDIR "/systemd-home-fallback-shell")), + SD_JSON_BUILD_PAIR("fallbackHomeDirectory", JSON_BUILD_CONST_STRING("/")), + SD_JSON_BUILD_PAIR_CONDITION(disk_size != UINT64_MAX, "diskSize", SD_JSON_BUILD_UNSIGNED(disk_size)), + SD_JSON_BUILD_PAIR_CONDITION(disk_usage != UINT64_MAX, "diskUsage", SD_JSON_BUILD_UNSIGNED(disk_usage)), + SD_JSON_BUILD_PAIR_CONDITION(disk_free != UINT64_MAX, "diskFree", SD_JSON_BUILD_UNSIGNED(disk_free)), + SD_JSON_BUILD_PAIR_CONDITION(disk_ceiling != UINT64_MAX, "diskCeiling", SD_JSON_BUILD_UNSIGNED(disk_ceiling)), + SD_JSON_BUILD_PAIR_CONDITION(disk_floor != UINT64_MAX, "diskFloor", SD_JSON_BUILD_UNSIGNED(disk_floor)), + SD_JSON_BUILD_PAIR_CONDITION(h->signed_locally >= 0, "signedLocally", SD_JSON_BUILD_BOOLEAN(h->signed_locally)), + SD_JSON_BUILD_PAIR_CONDITION(!!fstype, "fileSystemType", SD_JSON_BUILD_STRING(fstype)), + SD_JSON_BUILD_PAIR_CONDITION(access_mode != MODE_INVALID, "accessMode", SD_JSON_BUILD_UNSIGNED(access_mode)) )); if (r < 0) return r; - j = json_variant_ref(h->record->json); - v = json_variant_ref(json_variant_by_key(j, "status")); - m = json_variant_ref(json_variant_by_key(v, SD_ID128_TO_STRING(id))); + j = sd_json_variant_ref(h->record->json); + v = sd_json_variant_ref(sd_json_variant_by_key(j, "status")); + m = sd_json_variant_ref(sd_json_variant_by_key(v, SD_ID128_TO_STRING(id))); - r = json_variant_filter(&m, STRV_MAKE("diskSize", "diskUsage", "diskFree", "diskCeiling", "diskFloor", "signedLocally")); + r = sd_json_variant_filter(&m, STRV_MAKE("diskSize", "diskUsage", "diskFree", "diskCeiling", "diskFloor", "signedLocally")); if (r < 0) return r; - r = json_variant_merge_object(&m, status); + r = sd_json_variant_merge_object(&m, status); if (r < 0) return r; - r = json_variant_set_field(&v, SD_ID128_TO_STRING(id), m); + r = sd_json_variant_set_field(&v, SD_ID128_TO_STRING(id), m); if (r < 0) return r; - r = json_variant_set_field(&j, "status", v); + r = sd_json_variant_set_field(&j, "status", v); if (r < 0) return r; diff --git a/src/home/homed-manager.c b/src/home/homed-manager.c index 7669cbb269..417b5e342e 100644 --- a/src/home/homed-manager.c +++ b/src/home/homed-manager.c @@ -360,7 +360,7 @@ static int manager_add_home_by_record( int dir_fd, const char *fname) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; unsigned line, column; int r, is_signed; @@ -382,11 +382,11 @@ static int manager_add_home_by_record( if (st.st_size == 0) goto unlink_this_file; - r = json_parse_file_at(NULL, dir_fd, fname, JSON_PARSE_SENSITIVE, &v, &line, &column); + r = sd_json_parse_file_at(NULL, dir_fd, fname, SD_JSON_PARSE_SENSITIVE, &v, &line, &column); if (r < 0) return log_error_errno(r, "Failed to parse identity record at %s:%u%u: %m", fname, line, column); - if (json_variant_is_blank_object(v)) + if (sd_json_variant_is_blank_object(v)) goto unlink_this_file; hr = user_record_new(); diff --git a/src/home/homed-varlink.c b/src/home/homed-varlink.c index 1cef25f563..f2f5d48132 100644 --- a/src/home/homed-varlink.c +++ b/src/home/homed-varlink.c @@ -34,7 +34,7 @@ static bool client_is_trusted(Varlink *link, Home *h) { return peer_uid == 0 || peer_uid == h->uid; } -static int build_user_json(Home *h, bool trusted, JsonVariant **ret) { +static int build_user_json(Home *h, bool trusted, sd_json_variant **ret) { _cleanup_(user_record_unrefp) UserRecord *augmented = NULL; UserRecordLoadFlags flags; int r; @@ -52,9 +52,9 @@ static int build_user_json(Home *h, bool trusted, JsonVariant **ret) { if (r < 0) return r; - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("record", JSON_BUILD_VARIANT(augmented->json)), - JSON_BUILD_PAIR("incomplete", JSON_BUILD_BOOLEAN(augmented->incomplete)))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_VARIANT(augmented->json)), + SD_JSON_BUILD_PAIR("incomplete", SD_JSON_BUILD_BOOLEAN(augmented->incomplete)))); } static bool home_user_match_lookup_parameters(LookupParameters *p, Home *h) { @@ -70,16 +70,16 @@ static bool home_user_match_lookup_parameters(LookupParameters *p, Home *h) { return true; } -int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; LookupParameters p = { .uid = UID_INVALID, }; @@ -118,7 +118,7 @@ int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMet if (r < 0) return r; - v = json_variant_unref(v); + v = sd_json_variant_unref(v); } trusted = client_is_trusted(link, h); @@ -149,7 +149,7 @@ int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMet return varlink_reply(link, v); } -static int build_group_json(Home *h, JsonVariant **ret) { +static int build_group_json(Home *h, sd_json_variant **ret) { _cleanup_(group_record_unrefp) GroupRecord *g = NULL; int r; @@ -167,9 +167,9 @@ static int build_group_json(Home *h, JsonVariant **ret) { assert(!FLAGS_SET(g->mask, USER_RECORD_SECRET)); assert(!FLAGS_SET(g->mask, USER_RECORD_PRIVILEGED)); - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("record", JSON_BUILD_VARIANT(g->json)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_VARIANT(g->json)))); } static bool home_group_match_lookup_parameters(LookupParameters *p, Home *h) { @@ -185,16 +185,16 @@ static bool home_group_match_lookup_parameters(LookupParameters *p, Home *h) { return true; } -int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; LookupParameters p = { .gid = GID_INVALID, }; @@ -227,7 +227,7 @@ int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMe if (r < 0) return r; - v = json_variant_unref(v); + v = sd_json_variant_unref(v); } r = build_group_json(h, &v); @@ -254,12 +254,12 @@ int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMe return varlink_reply(link, v); } -int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), SD_JSON_SAFE }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; @@ -288,14 +288,14 @@ int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMet if (!strv_contains(h->record->member_of, p.group_name)) return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); - return varlink_replyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(h->user_name)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(p.group_name)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(h->user_name)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(p.group_name)))); } STRV_FOREACH(i, h->record->member_of) { if (last) { - r = varlink_notifyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(h->user_name)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(last)))); + r = varlink_notifyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(h->user_name)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(last)))); if (r < 0) return r; } @@ -304,8 +304,8 @@ int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMet } if (last) - return varlink_replyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(h->user_name)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(last)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(h->user_name)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(last)))); } else if (p.group_name) { const char *last = NULL; @@ -316,8 +316,8 @@ int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMet continue; if (last) { - r = varlink_notifyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(last)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(p.group_name)))); + r = varlink_notifyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(last)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(p.group_name)))); if (r < 0) return r; } @@ -326,8 +326,8 @@ int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMet } if (last) - return varlink_replyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(last)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(p.group_name)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(last)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(p.group_name)))); } else { const char *last_user_name = NULL, *last_group_name = NULL; @@ -337,8 +337,8 @@ int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMet if (last_user_name) { assert(last_group_name); - r = varlink_notifyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(last_user_name)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(last_group_name)))); + r = varlink_notifyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(last_user_name)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(last_group_name)))); if (r < 0) return r; @@ -350,8 +350,8 @@ int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMet if (last_user_name) { assert(last_group_name); - return varlink_replyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(last_user_name)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(last_group_name)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(last_user_name)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(last_group_name)))); } } diff --git a/src/home/homed-varlink.h b/src/home/homed-varlink.h index 2e404f02fa..bb1d4c0686 100644 --- a/src/home/homed-varlink.h +++ b/src/home/homed-varlink.h @@ -3,6 +3,6 @@ #include "homed-manager.h" -int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); -int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); -int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); +int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); +int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); +int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index 20ff4c31ac..1730674771 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -37,6 +37,7 @@ #include "homework-luks.h" #include "homework-mount.h" #include "io-util.h" +#include "json-util.h" #include "keyring-util.h" #include "memory-util.h" #include "missing_magic.h" @@ -825,7 +826,7 @@ static int luks_validate_home_record( assert(h); for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *rr = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *rr = NULL; _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL; _cleanup_(user_record_unrefp) UserRecord *lhr = NULL; _cleanup_free_ void *encrypted = NULL, *iv = NULL; @@ -834,7 +835,7 @@ static int luks_validate_home_record( _cleanup_free_ char *decrypted = NULL; const char *text, *type; crypt_token_info state; - JsonVariant *jr, *jiv; + sd_json_variant *jr, *jiv; unsigned line, column; const EVP_CIPHER *cc; @@ -853,22 +854,22 @@ static int luks_validate_home_record( if (r < 0) return log_error_errno(r, "Failed to read LUKS token %i: %m", token); - r = json_parse(text, JSON_PARSE_SENSITIVE, &v, &line, &column); + r = sd_json_parse(text, SD_JSON_PARSE_SENSITIVE, &v, &line, &column); if (r < 0) return log_error_errno(r, "Failed to parse LUKS token JSON data %u:%u: %m", line, column); - jr = json_variant_by_key(v, "record"); + jr = sd_json_variant_by_key(v, "record"); if (!jr) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "LUKS token lacks 'record' field."); - jiv = json_variant_by_key(v, "iv"); + jiv = sd_json_variant_by_key(v, "iv"); if (!jiv) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "LUKS token lacks 'iv' field."); - r = json_variant_unbase64(jr, &encrypted, &encrypted_size); + r = sd_json_variant_unbase64(jr, &encrypted, &encrypted_size); if (r < 0) return log_error_errno(r, "Failed to base64 decode record: %m"); - r = json_variant_unbase64(jiv, &iv, &iv_size); + r = sd_json_variant_unbase64(jiv, &iv, &iv_size); if (r < 0) return log_error_errno(r, "Failed to base64 decode IV: %m"); @@ -906,7 +907,7 @@ static int luks_validate_home_record( decrypted[decrypted_size] = 0; - r = json_parse(decrypted, JSON_PARSE_SENSITIVE, &rr, NULL, NULL); + r = sd_json_parse(decrypted, SD_JSON_PARSE_SENSITIVE, &rr, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse decrypted JSON record, refusing."); @@ -941,7 +942,7 @@ static int format_luks_token_text( int r, encrypted_size_out1 = 0, encrypted_size_out2 = 0, iv_size, key_size; _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ void *iv = NULL, *encrypted = NULL; size_t text_length, encrypted_size; _cleanup_free_ char *text = NULL; @@ -976,7 +977,7 @@ static int format_luks_token_text( if (EVP_EncryptInit_ex(context, cc, NULL, volume_key, iv) != 1) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize encryption context."); - r = json_variant_format(hr->json, 0, &text); + r = sd_json_variant_format(hr->json, 0, &text); if (r < 0) return log_error_errno(r, "Failed to format user record for LUKS: %m"); @@ -997,16 +998,16 @@ static int format_luks_token_text( assert((size_t) encrypted_size_out1 + (size_t) encrypted_size_out2 <= encrypted_size); - r = json_build(&v, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-homed")), - JSON_BUILD_PAIR("keyslots", JSON_BUILD_EMPTY_ARRAY), - JSON_BUILD_PAIR("record", JSON_BUILD_BASE64(encrypted, encrypted_size_out1 + encrypted_size_out2)), - JSON_BUILD_PAIR("iv", JSON_BUILD_BASE64(iv, iv_size)))); + r = sd_json_build(&v, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-homed")), + SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_EMPTY_ARRAY), + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_BASE64(encrypted, encrypted_size_out1 + encrypted_size_out2)), + SD_JSON_BUILD_PAIR("iv", SD_JSON_BUILD_BASE64(iv, iv_size)))); if (r < 0) return log_error_errno(r, "Failed to prepare LUKS JSON token object: %m"); - r = json_variant_format(v, 0, ret); + r = sd_json_variant_format(v, 0, ret); if (r < 0) return log_error_errno(r, "Failed to format encrypted user record for LUKS: %m"); diff --git a/src/home/homework.c b/src/home/homework.c index 482db23dbc..2537e8d1dd 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -14,6 +14,7 @@ #include "format-util.h" #include "fs-util.h" #include "home-util.h" +#include "homework.h" #include "homework-blob.h" #include "homework-cifs.h" #include "homework-directory.h" @@ -22,7 +23,7 @@ #include "homework-luks.h" #include "homework-mount.h" #include "homework-pkcs11.h" -#include "homework.h" +#include "json-util.h" #include "libcrypt-util.h" #include "main-func.h" #include "memory-util.h" @@ -80,7 +81,7 @@ int user_record_authenticate( /* First, let's see if we already have a volume key from the keyring */ if (cache->volume_key && - json_variant_is_blank_object(json_variant_by_key(secret->json, "secret"))) { + sd_json_variant_is_blank_object(sd_json_variant_by_key(secret->json, "secret"))) { log_info("LUKS volume key from keyring unlocks user record."); return 1; } @@ -538,7 +539,7 @@ int home_sync_and_statfs(int root_fd, struct statfs *ret) { return 0; } -static int read_identity_file(int root_fd, JsonVariant **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; unsigned line, column; @@ -559,7 +560,7 @@ static int read_identity_file(int root_fd, JsonVariant **ret) { if (!identity_file) return log_oom(); - r = json_parse_file(identity_file, ".identity", JSON_PARSE_SENSITIVE, ret, &line, &column); + r = sd_json_parse_file(identity_file, ".identity", SD_JSON_PARSE_SENSITIVE, ret, &line, &column); if (r < 0) return log_error_errno(r, "[.identity:%u:%u] Failed to parse JSON data: %m", line, column); @@ -568,8 +569,8 @@ static int read_identity_file(int root_fd, JsonVariant **ret) { return 0; } -static int write_identity_file(int root_fd, JsonVariant *v, uid_t uid) { - _cleanup_(json_variant_unrefp) JsonVariant *normalized = NULL; +static int write_identity_file(int root_fd, sd_json_variant *v, uid_t uid) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *normalized = NULL; _cleanup_fclose_ FILE *identity_file = NULL; _cleanup_close_ int identity_fd = -EBADF; _cleanup_free_ char *fn = NULL; @@ -578,9 +579,9 @@ static int write_identity_file(int root_fd, JsonVariant *v, uid_t uid) { assert(root_fd >= 0); assert(v); - normalized = json_variant_ref(v); + normalized = sd_json_variant_ref(v); - r = json_variant_normalize(&normalized); + r = sd_json_variant_normalize(&normalized); if (r < 0) log_warning_errno(r, "Failed to normalize user record, ignoring: %m"); @@ -598,7 +599,7 @@ static int write_identity_file(int root_fd, JsonVariant *v, uid_t uid) { goto fail; } - json_variant_dump(normalized, JSON_FORMAT_PRETTY, identity_file, NULL); + sd_json_variant_dump(normalized, SD_JSON_FORMAT_PRETTY, identity_file, NULL); r = fflush_and_check(identity_file); if (r < 0) { @@ -635,7 +636,7 @@ int home_load_embedded_identity( UserRecord **ret_new_home) { _cleanup_(user_record_unrefp) UserRecord *embedded_home = NULL, *intermediate_home = NULL, *new_home = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(h); @@ -1977,14 +1978,14 @@ static int home_unlock(UserRecord *h) { static int run(int argc, char *argv[]) { _cleanup_(user_record_unrefp) UserRecord *home = NULL, *new_home = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_fclose_ FILE *opened_file = NULL; _cleanup_hashmap_free_ Hashmap *blobs = NULL; unsigned line = 0, column = 0; const char *json_path = NULL, *blob_filename; FILE *json_file; usec_t start; - JsonVariant *fdmap, *blob_fd_variant; + sd_json_variant *fdmap, *blob_fd_variant; int r; start = now(CLOCK_MONOTONIC); @@ -2011,11 +2012,11 @@ static int run(int argc, char *argv[]) { json_file = stdin; } - r = json_parse_file(json_file, json_path, JSON_PARSE_SENSITIVE, &v, &line, &column); + r = sd_json_parse_file(json_file, json_path, SD_JSON_PARSE_SENSITIVE, &v, &line, &column); if (r < 0) return log_error_errno(r, "[%s:%u:%u] Failed to parse JSON data: %m", json_path, line, column); - fdmap = json_variant_by_key(v, HOMEWORK_BLOB_FDMAP_FIELD); + fdmap = sd_json_variant_by_key(v, HOMEWORK_BLOB_FDMAP_FIELD); if (fdmap) { r = hashmap_ensure_allocated(&blobs, &blob_fd_hash_ops); if (r < 0) @@ -2025,10 +2026,10 @@ static int run(int argc, char *argv[]) { _cleanup_free_ char *filename = NULL; _cleanup_close_ int fd = -EBADF; - assert(json_variant_is_integer(blob_fd_variant)); - assert(json_variant_integer(blob_fd_variant) >= 0); - assert(json_variant_integer(blob_fd_variant) <= INT_MAX - SD_LISTEN_FDS_START); - fd = SD_LISTEN_FDS_START + (int) json_variant_integer(blob_fd_variant); + assert(sd_json_variant_is_integer(blob_fd_variant)); + assert(sd_json_variant_integer(blob_fd_variant) >= 0); + assert(sd_json_variant_integer(blob_fd_variant) <= INT_MAX - SD_LISTEN_FDS_START); + fd = SD_LISTEN_FDS_START + (int) sd_json_variant_integer(blob_fd_variant); if (DEBUG_LOGGING) { _cleanup_free_ char *resolved = NULL; @@ -2052,7 +2053,7 @@ static int run(int argc, char *argv[]) { TAKE_FD(fd); } - r = json_variant_filter(&v, STRV_MAKE(HOMEWORK_BLOB_FDMAP_FIELD)); + r = sd_json_variant_filter(&v, STRV_MAKE(HOMEWORK_BLOB_FDMAP_FIELD)); if (r < 0) return log_error_errno(r, "Failed to strip internal fdmap from JSON: %m"); } @@ -2141,7 +2142,7 @@ static int run(int argc, char *argv[]) { * prepare a fresh record, send to us, and only if it works use it without having to keep a local * copy. */ if (new_home) - json_variant_dump(new_home->json, JSON_FORMAT_NEWLINE, stdout, NULL); + sd_json_variant_dump(new_home->json, SD_JSON_FORMAT_NEWLINE, stdout, NULL); return 0; } diff --git a/src/home/pam_systemd_home.c b/src/home/pam_systemd_home.c index 4616f086ed..372d79cc85 100644 --- a/src/home/pam_systemd_home.c +++ b/src/home/pam_systemd_home.c @@ -103,7 +103,7 @@ static int acquire_user_record( PamBusData **bus_data) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *ur = NULL; _cleanup_free_ char *homed_field = NULL; const char *json = NULL; @@ -208,7 +208,7 @@ static int acquire_user_record( TAKE_PTR(json_copy); } - r = json_parse(json, JSON_PARSE_SENSITIVE, &v, NULL, NULL); + r = sd_json_parse(json, SD_JSON_PARSE_SENSITIVE, &v, NULL, NULL); if (r < 0) return pam_syslog_errno(handle, LOG_ERR, r, "Failed to parse JSON user record: %m"); diff --git a/src/home/user-record-sign.c b/src/home/user-record-sign.c index 25618d0343..2230b29caf 100644 --- a/src/home/user-record-sign.c +++ b/src/home/user-record-sign.c @@ -4,13 +4,14 @@ #include "fd-util.h" #include "fileio.h" +#include "json-util.h" #include "memstream-util.h" #include "openssl-util.h" #include "user-record-sign.h" static int user_record_signable_json(UserRecord *ur, char **ret) { _cleanup_(user_record_unrefp) UserRecord *reduced = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; int r; assert(ur); @@ -20,18 +21,18 @@ static int user_record_signable_json(UserRecord *ur, char **ret) { if (r < 0) return r; - j = json_variant_ref(reduced->json); + j = sd_json_variant_ref(reduced->json); - r = json_variant_normalize(&j); + r = sd_json_variant_normalize(&j); if (r < 0) return r; - return json_variant_format(j, 0, ret); + return sd_json_variant_format(j, 0, ret); } int user_record_sign(UserRecord *ur, EVP_PKEY *private_key, UserRecord **ret) { _cleanup_(memstream_done) MemStream m = {}; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *signed_ur = NULL; _cleanup_free_ char *text = NULL, *key = NULL; _cleanup_free_ void *signature = NULL; @@ -62,19 +63,19 @@ int user_record_sign(UserRecord *ur, EVP_PKEY *private_key, UserRecord **ret) { if (r < 0) return r; - v = json_variant_ref(ur->json); + v = sd_json_variant_ref(ur->json); - r = json_variant_set_fieldb( + r = sd_json_variant_set_fieldb( &v, "signature", - JSON_BUILD_ARRAY( - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("data", JSON_BUILD_BASE64(signature, signature_size)), - JSON_BUILD_PAIR("key", JSON_BUILD_STRING(key))))); + SD_JSON_BUILD_ARRAY( + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("data", SD_JSON_BUILD_BASE64(signature, signature_size)), + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_STRING(key))))); if (r < 0) return r; if (DEBUG_LOGGING) - json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR_AUTO, NULL, NULL); + sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO, NULL, NULL); signed_ur = user_record_new(); if (!signed_ur) @@ -91,20 +92,20 @@ int user_record_sign(UserRecord *ur, EVP_PKEY *private_key, UserRecord **ret) { int user_record_verify(UserRecord *ur, EVP_PKEY *public_key) { _cleanup_free_ char *text = NULL; unsigned n_good = 0, n_bad = 0; - JsonVariant *array, *e; + sd_json_variant *array, *e; int r; assert(ur); assert(public_key); - array = json_variant_by_key(ur->json, "signature"); + array = sd_json_variant_by_key(ur->json, "signature"); if (!array) return USER_RECORD_UNSIGNED; - if (!json_variant_is_array(array)) + if (!sd_json_variant_is_array(array)) return -EINVAL; - if (json_variant_elements(array) == 0) + if (sd_json_variant_elements(array) == 0) return USER_RECORD_UNSIGNED; r = user_record_signable_json(ur, &text); @@ -115,16 +116,16 @@ int user_record_verify(UserRecord *ur, EVP_PKEY *public_key) { _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *md_ctx = NULL; _cleanup_free_ void *signature = NULL; size_t signature_size = 0; - JsonVariant *data; + sd_json_variant *data; - if (!json_variant_is_object(e)) + if (!sd_json_variant_is_object(e)) return -EINVAL; - data = json_variant_by_key(e, "data"); + data = sd_json_variant_by_key(e, "data"); if (!data) return -EINVAL; - r = json_variant_unbase64(data, &signature, &signature_size); + r = sd_json_variant_unbase64(data, &signature, &signature_size); if (r < 0) return r; @@ -148,14 +149,14 @@ int user_record_verify(UserRecord *ur, EVP_PKEY *public_key) { } int user_record_has_signature(UserRecord *ur) { - JsonVariant *array; + sd_json_variant *array; - array = json_variant_by_key(ur->json, "signature"); + array = sd_json_variant_by_key(ur->json, "signature"); if (!array) return false; - if (!json_variant_is_array(array)) + if (!sd_json_variant_is_array(array)) return -EINVAL; - return json_variant_elements(array) > 0; + return sd_json_variant_elements(array) > 0; } diff --git a/src/home/user-record-util.c b/src/home/user-record-util.c index 3ae0883cb0..546db9de9b 100644 --- a/src/home/user-record-util.c +++ b/src/home/user-record-util.c @@ -2,15 +2,18 @@ #include <sys/xattr.h> +#include "sd-json.h" + #include "errno-util.h" #include "fd-util.h" #include "home-util.h" #include "id128-util.h" +#include "json-util.h" #include "libcrypt-util.h" #include "memory-util.h" -#include "recovery-key.h" #include "mountpoint-util.h" #include "path-util.h" +#include "recovery-key.h" #include "sha256.h" #include "stat-util.h" #include "user-record-util.h" @@ -81,18 +84,18 @@ int user_record_synthesize( if (!hd) return -ENOMEM; - r = json_build(&h->json, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(user_name)), - JSON_BUILD_PAIR_CONDITION(!!rr, "realm", JSON_BUILD_STRING(realm)), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("regular")), - JSON_BUILD_PAIR("binding", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR(SD_ID128_TO_STRING(mid), JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("imagePath", JSON_BUILD_STRING(image_path)), - JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_STRING(hd)), - JSON_BUILD_PAIR("storage", JSON_BUILD_STRING(user_storage_to_string(storage))), - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(uid)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(gid)))))))); + r = sd_json_build(&h->json, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(user_name)), + SD_JSON_BUILD_PAIR_CONDITION(!!rr, "realm", SD_JSON_BUILD_STRING(realm)), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("regular")), + SD_JSON_BUILD_PAIR("binding", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR(SD_ID128_TO_STRING(mid), SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("imagePath", SD_JSON_BUILD_STRING(image_path)), + SD_JSON_BUILD_PAIR("homeDirectory", SD_JSON_BUILD_STRING(hd)), + SD_JSON_BUILD_PAIR("storage", SD_JSON_BUILD_STRING(user_storage_to_string(storage))), + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(uid)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(gid)))))))); if (r < 0) return r; @@ -141,18 +144,18 @@ int group_record_synthesize(GroupRecord *g, UserRecord *h) { if (!description) return -ENOMEM; - r = json_build(&g->json, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(un)), - JSON_BUILD_PAIR_CONDITION(!!rr, "realm", JSON_BUILD_STRING(rr)), - JSON_BUILD_PAIR("description", JSON_BUILD_STRING(description)), - JSON_BUILD_PAIR("binding", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR(SD_ID128_TO_STRING(mid), JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(user_record_gid(h))))))), - JSON_BUILD_PAIR_CONDITION(h->disposition >= 0, "disposition", JSON_BUILD_STRING(user_disposition_to_string(user_record_disposition(h)))), - JSON_BUILD_PAIR("status", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR(SD_ID128_TO_STRING(mid), JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Home")))))))); + r = sd_json_build(&g->json, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(un)), + SD_JSON_BUILD_PAIR_CONDITION(!!rr, "realm", SD_JSON_BUILD_STRING(rr)), + SD_JSON_BUILD_PAIR("description", SD_JSON_BUILD_STRING(description)), + SD_JSON_BUILD_PAIR("binding", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR(SD_ID128_TO_STRING(mid), SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(user_record_gid(h))))))), + SD_JSON_BUILD_PAIR_CONDITION(h->disposition >= 0, "disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(user_record_disposition(h)))), + SD_JSON_BUILD_PAIR("status", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR(SD_ID128_TO_STRING(mid), SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Home")))))))); if (r < 0) return r; @@ -229,9 +232,9 @@ int user_record_reconcile( } else result = USER_RECONCILE_HOST_WON; } else { - _cleanup_(json_variant_unrefp) JsonVariant *extended = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *extended = NULL; _cleanup_(user_record_unrefp) UserRecord *merged = NULL; - JsonVariant *e; + sd_json_variant *e; /* The embedded version is newer */ @@ -239,11 +242,11 @@ int user_record_reconcile( return -ESTALE; /* Copy in the binding data */ - extended = json_variant_ref(embedded->json); + extended = sd_json_variant_ref(embedded->json); - e = json_variant_by_key(host->json, "binding"); + e = sd_json_variant_by_key(host->json, "binding"); if (e) { - r = json_variant_set_field(&extended, "binding", e); + r = sd_json_variant_set_field(&extended, "binding", e); if (r < 0) return r; } @@ -283,7 +286,7 @@ int user_record_add_binding( uid_t uid, gid_t gid) { - _cleanup_(json_variant_unrefp) JsonVariant *new_binding_entry = NULL, *binding = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *new_binding_entry = NULL, *binding = NULL; _cleanup_free_ char *blob = NULL, *ip = NULL, *hd = NULL, *ip_auto = NULL, *lc = NULL, *lcm = NULL, *fst = NULL; sd_id128_t mid; int r; @@ -335,45 +338,45 @@ int user_record_add_binding( return -ENOMEM; } - r = json_build(&new_binding_entry, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("blobDirectory", JSON_BUILD_STRING(blob)), - JSON_BUILD_PAIR_CONDITION(!!image_path, "imagePath", JSON_BUILD_STRING(image_path)), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(partition_uuid), "partitionUuid", JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(partition_uuid))), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(luks_uuid), "luksUuid", JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(luks_uuid))), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(fs_uuid), "fileSystemUuid", JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(fs_uuid))), - JSON_BUILD_PAIR_CONDITION(!!luks_cipher, "luksCipher", JSON_BUILD_STRING(luks_cipher)), - JSON_BUILD_PAIR_CONDITION(!!luks_cipher_mode, "luksCipherMode", JSON_BUILD_STRING(luks_cipher_mode)), - JSON_BUILD_PAIR_CONDITION(luks_volume_key_size != UINT64_MAX, "luksVolumeKeySize", JSON_BUILD_UNSIGNED(luks_volume_key_size)), - JSON_BUILD_PAIR_CONDITION(!!file_system_type, "fileSystemType", JSON_BUILD_STRING(file_system_type)), - JSON_BUILD_PAIR_CONDITION(!!home_directory, "homeDirectory", JSON_BUILD_STRING(home_directory)), - JSON_BUILD_PAIR_CONDITION(uid_is_valid(uid), "uid", JSON_BUILD_UNSIGNED(uid)), - JSON_BUILD_PAIR_CONDITION(gid_is_valid(gid), "gid", JSON_BUILD_UNSIGNED(gid)), - JSON_BUILD_PAIR_CONDITION(storage >= 0, "storage", JSON_BUILD_STRING(user_storage_to_string(storage))))); + r = sd_json_build(&new_binding_entry, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("blobDirectory", SD_JSON_BUILD_STRING(blob)), + SD_JSON_BUILD_PAIR_CONDITION(!!image_path, "imagePath", SD_JSON_BUILD_STRING(image_path)), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(partition_uuid), "partitionUuid", SD_JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(partition_uuid))), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(luks_uuid), "luksUuid", SD_JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(luks_uuid))), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(fs_uuid), "fileSystemUuid", SD_JSON_BUILD_STRING(SD_ID128_TO_UUID_STRING(fs_uuid))), + SD_JSON_BUILD_PAIR_CONDITION(!!luks_cipher, "luksCipher", SD_JSON_BUILD_STRING(luks_cipher)), + SD_JSON_BUILD_PAIR_CONDITION(!!luks_cipher_mode, "luksCipherMode", SD_JSON_BUILD_STRING(luks_cipher_mode)), + SD_JSON_BUILD_PAIR_CONDITION(luks_volume_key_size != UINT64_MAX, "luksVolumeKeySize", SD_JSON_BUILD_UNSIGNED(luks_volume_key_size)), + SD_JSON_BUILD_PAIR_CONDITION(!!file_system_type, "fileSystemType", SD_JSON_BUILD_STRING(file_system_type)), + SD_JSON_BUILD_PAIR_CONDITION(!!home_directory, "homeDirectory", SD_JSON_BUILD_STRING(home_directory)), + SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(uid), "uid", SD_JSON_BUILD_UNSIGNED(uid)), + SD_JSON_BUILD_PAIR_CONDITION(gid_is_valid(gid), "gid", SD_JSON_BUILD_UNSIGNED(gid)), + SD_JSON_BUILD_PAIR_CONDITION(storage >= 0, "storage", SD_JSON_BUILD_STRING(user_storage_to_string(storage))))); if (r < 0) return r; - binding = json_variant_ref(json_variant_by_key(h->json, "binding")); + binding = sd_json_variant_ref(sd_json_variant_by_key(h->json, "binding")); if (binding) { - _cleanup_(json_variant_unrefp) JsonVariant *be = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *be = NULL; /* Merge the new entry with an old one, if that exists */ - be = json_variant_ref(json_variant_by_key(binding, SD_ID128_TO_STRING(mid))); + be = sd_json_variant_ref(sd_json_variant_by_key(binding, SD_ID128_TO_STRING(mid))); if (be) { - r = json_variant_merge_object(&be, new_binding_entry); + r = sd_json_variant_merge_object(&be, new_binding_entry); if (r < 0) return r; - json_variant_unref(new_binding_entry); + sd_json_variant_unref(new_binding_entry); new_binding_entry = TAKE_PTR(be); } } - r = json_variant_set_field(&binding, SD_ID128_TO_STRING(mid), new_binding_entry); + r = sd_json_variant_set_field(&binding, SD_ID128_TO_STRING(mid), new_binding_entry); if (r < 0) return r; - r = json_variant_set_field(&h->json, "binding", binding); + r = sd_json_variant_set_field(&h->json, "binding", binding); if (r < 0) return r; @@ -634,10 +637,10 @@ int user_record_test_recovery_key(UserRecord *h, UserRecord *secret) { } int user_record_set_disk_size(UserRecord *h, uint64_t disk_size) { - _cleanup_(json_variant_unrefp) JsonVariant *new_per_machine = NULL, *midv = NULL, *midav = NULL, *ne = NULL; - _cleanup_free_ JsonVariant **array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *new_per_machine = NULL, *midv = NULL, *midav = NULL, *ne = NULL; + _cleanup_free_ sd_json_variant **array = NULL; size_t idx = SIZE_MAX, n; - JsonVariant *per_machine; + sd_json_variant *per_machine; sd_id128_t mid; int r; @@ -650,50 +653,50 @@ int user_record_set_disk_size(UserRecord *h, uint64_t disk_size) { if (r < 0) return r; - r = json_variant_new_string(&midv, SD_ID128_TO_STRING(mid)); + r = sd_json_variant_new_string(&midv, SD_ID128_TO_STRING(mid)); if (r < 0) return r; - r = json_variant_new_array(&midav, (JsonVariant*[]) { midv }, 1); + r = sd_json_variant_new_array(&midav, (sd_json_variant*[]) { midv }, 1); if (r < 0) return r; - per_machine = json_variant_by_key(h->json, "perMachine"); + per_machine = sd_json_variant_by_key(h->json, "perMachine"); if (per_machine) { size_t i; - if (!json_variant_is_array(per_machine)) + if (!sd_json_variant_is_array(per_machine)) return -EINVAL; - n = json_variant_elements(per_machine); + n = sd_json_variant_elements(per_machine); - array = new(JsonVariant*, n + 1); + array = new(sd_json_variant*, n + 1); if (!array) return -ENOMEM; for (i = 0; i < n; i++) { - JsonVariant *m; + sd_json_variant *m; - array[i] = json_variant_by_index(per_machine, i); + array[i] = sd_json_variant_by_index(per_machine, i); - if (!json_variant_is_object(array[i])) + if (!sd_json_variant_is_object(array[i])) return -EINVAL; - m = json_variant_by_key(array[i], "matchMachineId"); + m = sd_json_variant_by_key(array[i], "matchMachineId"); if (!m) { /* No machineId field? Let's ignore this, but invalidate what we found so far */ idx = SIZE_MAX; continue; } - if (json_variant_equal(m, midv) || - json_variant_equal(m, midav)) { + if (sd_json_variant_equal(m, midv) || + sd_json_variant_equal(m, midav)) { /* Matches exactly what we are looking for. Let's use this */ idx = i; continue; } - r = per_machine_id_match(m, JSON_PERMISSIVE); + r = per_machine_id_match(m, SD_JSON_PERMISSIVE); if (r < 0) return r; if (r > 0) @@ -705,10 +708,10 @@ int user_record_set_disk_size(UserRecord *h, uint64_t disk_size) { if (idx == SIZE_MAX) idx = n++; /* Nothing suitable found, place new entry at end */ else - ne = json_variant_ref(array[idx]); + ne = sd_json_variant_ref(array[idx]); } else { - array = new(JsonVariant*, 1); + array = new(sd_json_variant*, 1); if (!array) return -ENOMEM; @@ -717,23 +720,23 @@ int user_record_set_disk_size(UserRecord *h, uint64_t disk_size) { } if (!ne) { - r = json_variant_set_field(&ne, "matchMachineId", midav); + r = sd_json_variant_set_field(&ne, "matchMachineId", midav); if (r < 0) return r; } - r = json_variant_set_field_unsigned(&ne, "diskSize", disk_size); + r = sd_json_variant_set_field_unsigned(&ne, "diskSize", disk_size); if (r < 0) return r; assert(idx < n); array[idx] = ne; - r = json_variant_new_array(&new_per_machine, array, n); + r = sd_json_variant_new_array(&new_per_machine, array, n); if (r < 0) return r; - r = json_variant_set_field(&h->json, "perMachine", new_per_machine); + r = sd_json_variant_set_field(&h->json, "perMachine", new_per_machine); if (r < 0) return r; @@ -743,7 +746,7 @@ int user_record_set_disk_size(UserRecord *h, uint64_t disk_size) { } int user_record_update_last_changed(UserRecord *h, bool with_password) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; usec_t n; int r; @@ -760,14 +763,14 @@ int user_record_update_last_changed(UserRecord *h, bool with_password) { if (h->last_password_change_usec != UINT64_MAX && h->last_password_change_usec >= n) return -ECHRNG; - v = json_variant_ref(h->json); + v = sd_json_variant_ref(h->json); - r = json_variant_set_field_unsigned(&v, "lastChangeUSec", n); + r = sd_json_variant_set_field_unsigned(&v, "lastChangeUSec", n); if (r < 0) return r; if (with_password) { - r = json_variant_set_field_unsigned(&v, "lastPasswordChangeUSec", n); + r = sd_json_variant_set_field_unsigned(&v, "lastPasswordChangeUSec", n); if (r < 0) return r; @@ -776,7 +779,7 @@ int user_record_update_last_changed(UserRecord *h, bool with_password) { h->last_change_usec = n; - json_variant_unref(h->json); + sd_json_variant_unref(h->json); h->json = TAKE_PTR(v); h->mask |= USER_RECORD_REGULAR; @@ -784,7 +787,7 @@ int user_record_update_last_changed(UserRecord *h, bool with_password) { } int user_record_make_hashed_password(UserRecord *h, char **secret, bool extend) { - _cleanup_(json_variant_unrefp) JsonVariant *priv = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *priv = NULL; _cleanup_strv_free_ char **np = NULL; int r; @@ -813,45 +816,45 @@ int user_record_make_hashed_password(UserRecord *h, char **secret, bool extend) return r; } - priv = json_variant_ref(json_variant_by_key(h->json, "privileged")); + priv = sd_json_variant_ref(sd_json_variant_by_key(h->json, "privileged")); if (strv_isempty(np)) - r = json_variant_filter(&priv, STRV_MAKE("hashedPassword")); + r = sd_json_variant_filter(&priv, STRV_MAKE("hashedPassword")); else { - _cleanup_(json_variant_unrefp) JsonVariant *new_array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *new_array = NULL; - r = json_variant_new_array_strv(&new_array, np); + r = sd_json_variant_new_array_strv(&new_array, np); if (r < 0) return r; - r = json_variant_set_field(&priv, "hashedPassword", new_array); - if (r < 0) - return r; + r = sd_json_variant_set_field(&priv, "hashedPassword", new_array); } + if (r < 0) + return r; - r = json_variant_set_field(&h->json, "privileged", priv); + r = sd_json_variant_set_field(&h->json, "privileged", priv); if (r < 0) return r; strv_free_and_replace(h->hashed_password, np); - SET_FLAG(h->mask, USER_RECORD_PRIVILEGED, !json_variant_is_blank_object(priv)); + SET_FLAG(h->mask, USER_RECORD_PRIVILEGED, !sd_json_variant_is_blank_object(priv)); return 0; } int user_record_set_hashed_password(UserRecord *h, char **hashed_password) { - _cleanup_(json_variant_unrefp) JsonVariant *priv = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *priv = NULL; _cleanup_strv_free_ char **copy = NULL; int r; assert(h); - priv = json_variant_ref(json_variant_by_key(h->json, "privileged")); + priv = sd_json_variant_ref(sd_json_variant_by_key(h->json, "privileged")); if (strv_isempty(hashed_password)) - r = json_variant_filter(&priv, STRV_MAKE("hashedPassword")); + r = sd_json_variant_filter(&priv, STRV_MAKE("hashedPassword")); else { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; copy = strv_copy(hashed_password); if (!copy) @@ -859,27 +862,27 @@ int user_record_set_hashed_password(UserRecord *h, char **hashed_password) { strv_uniq(copy); - r = json_variant_new_array_strv(&array, copy); + r = sd_json_variant_new_array_strv(&array, copy); if (r < 0) return r; - r = json_variant_set_field(&priv, "hashedPassword", array); + r = sd_json_variant_set_field(&priv, "hashedPassword", array); } if (r < 0) return r; - r = json_variant_set_field(&h->json, "privileged", priv); + r = sd_json_variant_set_field(&h->json, "privileged", priv); if (r < 0) return r; strv_free_and_replace(h->hashed_password, copy); - SET_FLAG(h->mask, USER_RECORD_PRIVILEGED, !json_variant_is_blank_object(priv)); + SET_FLAG(h->mask, USER_RECORD_PRIVILEGED, !sd_json_variant_is_blank_object(priv)); return 0; } int user_record_set_password(UserRecord *h, char **password, bool prepend) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; _cleanup_strv_free_erase_ char **e = NULL; int r; @@ -910,38 +913,38 @@ int user_record_set_password(UserRecord *h, char **password, bool prepend) { strv_uniq(e); } - w = json_variant_ref(json_variant_by_key(h->json, "secret")); + w = sd_json_variant_ref(sd_json_variant_by_key(h->json, "secret")); if (strv_isempty(e)) - r = json_variant_filter(&w, STRV_MAKE("password")); + r = sd_json_variant_filter(&w, STRV_MAKE("password")); else { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL; - r = json_variant_new_array_strv(&l, e); + r = sd_json_variant_new_array_strv(&l, e); if (r < 0) return r; - json_variant_sensitive(l); + sd_json_variant_sensitive(l); - r = json_variant_set_field(&w, "password", l); + r = sd_json_variant_set_field(&w, "password", l); } if (r < 0) return r; - json_variant_sensitive(w); + sd_json_variant_sensitive(w); - r = json_variant_set_field(&h->json, "secret", w); + r = sd_json_variant_set_field(&h->json, "secret", w); if (r < 0) return r; strv_free_and_replace(h->password, e); - SET_FLAG(h->mask, USER_RECORD_SECRET, !json_variant_is_blank_object(w)); + SET_FLAG(h->mask, USER_RECORD_SECRET, !sd_json_variant_is_blank_object(w)); return 0; } int user_record_set_token_pin(UserRecord *h, char **pin, bool prepend) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; _cleanup_strv_free_erase_ char **e = NULL; int r; @@ -972,126 +975,126 @@ int user_record_set_token_pin(UserRecord *h, char **pin, bool prepend) { strv_uniq(e); } - w = json_variant_ref(json_variant_by_key(h->json, "secret")); + w = sd_json_variant_ref(sd_json_variant_by_key(h->json, "secret")); if (strv_isempty(e)) - r = json_variant_filter(&w, STRV_MAKE("tokenPin")); + r = sd_json_variant_filter(&w, STRV_MAKE("tokenPin")); else { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL; - r = json_variant_new_array_strv(&l, e); + r = sd_json_variant_new_array_strv(&l, e); if (r < 0) return r; - json_variant_sensitive(l); + sd_json_variant_sensitive(l); - r = json_variant_set_field(&w, "tokenPin", l); + r = sd_json_variant_set_field(&w, "tokenPin", l); } if (r < 0) return r; - json_variant_sensitive(w); + sd_json_variant_sensitive(w); - r = json_variant_set_field(&h->json, "secret", w); + r = sd_json_variant_set_field(&h->json, "secret", w); if (r < 0) return r; strv_free_and_replace(h->token_pin, e); - SET_FLAG(h->mask, USER_RECORD_SECRET, !json_variant_is_blank_object(w)); + SET_FLAG(h->mask, USER_RECORD_SECRET, !sd_json_variant_is_blank_object(w)); return 0; } int user_record_set_pkcs11_protected_authentication_path_permitted(UserRecord *h, int b) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; int r; assert(h); - w = json_variant_ref(json_variant_by_key(h->json, "secret")); + w = sd_json_variant_ref(sd_json_variant_by_key(h->json, "secret")); if (b < 0) - r = json_variant_filter(&w, STRV_MAKE("pkcs11ProtectedAuthenticationPathPermitted")); + r = sd_json_variant_filter(&w, STRV_MAKE("pkcs11ProtectedAuthenticationPathPermitted")); else - r = json_variant_set_field_boolean(&w, "pkcs11ProtectedAuthenticationPathPermitted", b); + r = sd_json_variant_set_field_boolean(&w, "pkcs11ProtectedAuthenticationPathPermitted", b); if (r < 0) return r; - if (json_variant_is_blank_object(w)) - r = json_variant_filter(&h->json, STRV_MAKE("secret")); + if (sd_json_variant_is_blank_object(w)) + r = sd_json_variant_filter(&h->json, STRV_MAKE("secret")); else { - json_variant_sensitive(w); + sd_json_variant_sensitive(w); - r = json_variant_set_field(&h->json, "secret", w); + r = sd_json_variant_set_field(&h->json, "secret", w); } if (r < 0) return r; h->pkcs11_protected_authentication_path_permitted = b; - SET_FLAG(h->mask, USER_RECORD_SECRET, !json_variant_is_blank_object(w)); + SET_FLAG(h->mask, USER_RECORD_SECRET, !sd_json_variant_is_blank_object(w)); return 0; } int user_record_set_fido2_user_presence_permitted(UserRecord *h, int b) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; int r; assert(h); - w = json_variant_ref(json_variant_by_key(h->json, "secret")); + w = sd_json_variant_ref(sd_json_variant_by_key(h->json, "secret")); if (b < 0) - r = json_variant_filter(&w, STRV_MAKE("fido2UserPresencePermitted")); + r = sd_json_variant_filter(&w, STRV_MAKE("fido2UserPresencePermitted")); else - r = json_variant_set_field_boolean(&w, "fido2UserPresencePermitted", b); + r = sd_json_variant_set_field_boolean(&w, "fido2UserPresencePermitted", b); if (r < 0) return r; - if (json_variant_is_blank_object(w)) - r = json_variant_filter(&h->json, STRV_MAKE("secret")); + if (sd_json_variant_is_blank_object(w)) + r = sd_json_variant_filter(&h->json, STRV_MAKE("secret")); else - r = json_variant_set_field(&h->json, "secret", w); + r = sd_json_variant_set_field(&h->json, "secret", w); if (r < 0) return r; h->fido2_user_presence_permitted = b; - SET_FLAG(h->mask, USER_RECORD_SECRET, !json_variant_is_blank_object(w)); + SET_FLAG(h->mask, USER_RECORD_SECRET, !sd_json_variant_is_blank_object(w)); return 0; } int user_record_set_fido2_user_verification_permitted(UserRecord *h, int b) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; int r; assert(h); - w = json_variant_ref(json_variant_by_key(h->json, "secret")); + w = sd_json_variant_ref(sd_json_variant_by_key(h->json, "secret")); if (b < 0) - r = json_variant_filter(&w, STRV_MAKE("fido2UserVerificationPermitted")); + r = sd_json_variant_filter(&w, STRV_MAKE("fido2UserVerificationPermitted")); else - r = json_variant_set_field_boolean(&w, "fido2UserVerificationPermitted", b); + r = sd_json_variant_set_field_boolean(&w, "fido2UserVerificationPermitted", b); if (r < 0) return r; - if (json_variant_is_blank_object(w)) - r = json_variant_filter(&h->json, STRV_MAKE("secret")); + if (sd_json_variant_is_blank_object(w)) + r = sd_json_variant_filter(&h->json, STRV_MAKE("secret")); else - r = json_variant_set_field(&h->json, "secret", w); + r = sd_json_variant_set_field(&h->json, "secret", w); if (r < 0) return r; h->fido2_user_verification_permitted = b; - SET_FLAG(h->mask, USER_RECORD_SECRET, !json_variant_is_blank_object(w)); + SET_FLAG(h->mask, USER_RECORD_SECRET, !sd_json_variant_is_blank_object(w)); return 0; } -static bool per_machine_entry_empty(JsonVariant *v) { +static bool per_machine_entry_empty(sd_json_variant *v) { const char *k; - _unused_ JsonVariant *e; + _unused_ sd_json_variant *e; JSON_VARIANT_OBJECT_FOREACH(k, e, v) if (!STR_IN_SET(k, "matchMachineId", "matchHostname")) @@ -1101,58 +1104,58 @@ static bool per_machine_entry_empty(JsonVariant *v) { } int user_record_set_password_change_now(UserRecord *h, int b) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; - JsonVariant *per_machine; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; + sd_json_variant *per_machine; int r; assert(h); - w = json_variant_ref(h->json); + w = sd_json_variant_ref(h->json); if (b < 0) - r = json_variant_filter(&w, STRV_MAKE("passwordChangeNow")); + r = sd_json_variant_filter(&w, STRV_MAKE("passwordChangeNow")); else - r = json_variant_set_field_boolean(&w, "passwordChangeNow", b); + r = sd_json_variant_set_field_boolean(&w, "passwordChangeNow", b); if (r < 0) return r; /* Also drop the field from all perMachine entries */ - per_machine = json_variant_by_key(w, "perMachine"); + per_machine = sd_json_variant_by_key(w, "perMachine"); if (per_machine) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; - JsonVariant *e; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; + sd_json_variant *e; JSON_VARIANT_ARRAY_FOREACH(e, per_machine) { - _cleanup_(json_variant_unrefp) JsonVariant *z = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *z = NULL; - if (!json_variant_is_object(e)) + if (!sd_json_variant_is_object(e)) return -EINVAL; - z = json_variant_ref(e); + z = sd_json_variant_ref(e); - r = json_variant_filter(&z, STRV_MAKE("passwordChangeNow")); + r = sd_json_variant_filter(&z, STRV_MAKE("passwordChangeNow")); if (r < 0) return r; if (per_machine_entry_empty(z)) continue; - r = json_variant_append_array(&array, z); + r = sd_json_variant_append_array(&array, z); if (r < 0) return r; } - if (json_variant_is_blank_array(array)) - r = json_variant_filter(&w, STRV_MAKE("perMachine")); + if (sd_json_variant_is_blank_array(array)) + r = sd_json_variant_filter(&w, STRV_MAKE("perMachine")); else - r = json_variant_set_field(&w, "perMachine", array); + r = sd_json_variant_set_field(&w, "perMachine", array); if (r < 0) return r; - SET_FLAG(h->mask, USER_RECORD_PER_MACHINE, !json_variant_is_blank_array(array)); + SET_FLAG(h->mask, USER_RECORD_PER_MACHINE, !sd_json_variant_is_blank_array(array)); } - json_variant_unref(h->json); + sd_json_variant_unref(h->json); h->json = TAKE_PTR(w); h->password_change_now = b; @@ -1204,7 +1207,7 @@ int user_record_merge_secret(UserRecord *h, UserRecord *secret) { } int user_record_good_authentication(UserRecord *h) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL, *z = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL, *z = NULL; uint64_t counter, usec; sd_id128_t mid; int r; @@ -1229,27 +1232,27 @@ int user_record_good_authentication(UserRecord *h) { if (r < 0) return r; - v = json_variant_ref(h->json); - w = json_variant_ref(json_variant_by_key(v, "status")); - z = json_variant_ref(json_variant_by_key(w, SD_ID128_TO_STRING(mid))); + v = sd_json_variant_ref(h->json); + w = sd_json_variant_ref(sd_json_variant_by_key(v, "status")); + z = sd_json_variant_ref(sd_json_variant_by_key(w, SD_ID128_TO_STRING(mid))); - r = json_variant_set_field_unsigned(&z, "goodAuthenticationCounter", counter); + r = sd_json_variant_set_field_unsigned(&z, "goodAuthenticationCounter", counter); if (r < 0) return r; - r = json_variant_set_field_unsigned(&z, "lastGoodAuthenticationUSec", usec); + r = sd_json_variant_set_field_unsigned(&z, "lastGoodAuthenticationUSec", usec); if (r < 0) return r; - r = json_variant_set_field(&w, SD_ID128_TO_STRING(mid), z); + r = sd_json_variant_set_field(&w, SD_ID128_TO_STRING(mid), z); if (r < 0) return r; - r = json_variant_set_field(&v, "status", w); + r = sd_json_variant_set_field(&v, "status", w); if (r < 0) return r; - json_variant_unref(h->json); + sd_json_variant_unref(h->json); h->json = TAKE_PTR(v); h->good_authentication_counter = counter; @@ -1260,7 +1263,7 @@ int user_record_good_authentication(UserRecord *h) { } int user_record_bad_authentication(UserRecord *h) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL, *z = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL, *z = NULL; uint64_t counter, usec; sd_id128_t mid; int r; @@ -1285,27 +1288,27 @@ int user_record_bad_authentication(UserRecord *h) { if (r < 0) return r; - v = json_variant_ref(h->json); - w = json_variant_ref(json_variant_by_key(v, "status")); - z = json_variant_ref(json_variant_by_key(w, SD_ID128_TO_STRING(mid))); + v = sd_json_variant_ref(h->json); + w = sd_json_variant_ref(sd_json_variant_by_key(v, "status")); + z = sd_json_variant_ref(sd_json_variant_by_key(w, SD_ID128_TO_STRING(mid))); - r = json_variant_set_field_unsigned(&z, "badAuthenticationCounter", counter); + r = sd_json_variant_set_field_unsigned(&z, "badAuthenticationCounter", counter); if (r < 0) return r; - r = json_variant_set_field_unsigned(&z, "lastBadAuthenticationUSec", usec); + r = sd_json_variant_set_field_unsigned(&z, "lastBadAuthenticationUSec", usec); if (r < 0) return r; - r = json_variant_set_field(&w, SD_ID128_TO_STRING(mid), z); + r = sd_json_variant_set_field(&w, SD_ID128_TO_STRING(mid), z); if (r < 0) return r; - r = json_variant_set_field(&v, "status", w); + r = sd_json_variant_set_field(&v, "status", w); if (r < 0) return r; - json_variant_unref(h->json); + sd_json_variant_unref(h->json); h->json = TAKE_PTR(v); h->bad_authentication_counter = counter; @@ -1316,7 +1319,7 @@ int user_record_bad_authentication(UserRecord *h) { } int user_record_ratelimit(UserRecord *h) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL, *z = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL, *z = NULL; usec_t usec, new_ratelimit_begin_usec, new_ratelimit_count; sd_id128_t mid; int r; @@ -1347,27 +1350,27 @@ int user_record_ratelimit(UserRecord *h) { if (r < 0) return r; - v = json_variant_ref(h->json); - w = json_variant_ref(json_variant_by_key(v, "status")); - z = json_variant_ref(json_variant_by_key(w, SD_ID128_TO_STRING(mid))); + v = sd_json_variant_ref(h->json); + w = sd_json_variant_ref(sd_json_variant_by_key(v, "status")); + z = sd_json_variant_ref(sd_json_variant_by_key(w, SD_ID128_TO_STRING(mid))); - r = json_variant_set_field_unsigned(&z, "rateLimitBeginUSec", new_ratelimit_begin_usec); + r = sd_json_variant_set_field_unsigned(&z, "rateLimitBeginUSec", new_ratelimit_begin_usec); if (r < 0) return r; - r = json_variant_set_field_unsigned(&z, "rateLimitCount", new_ratelimit_count); + r = sd_json_variant_set_field_unsigned(&z, "rateLimitCount", new_ratelimit_count); if (r < 0) return r; - r = json_variant_set_field(&w, SD_ID128_TO_STRING(mid), z); + r = sd_json_variant_set_field(&w, SD_ID128_TO_STRING(mid), z); if (r < 0) return r; - r = json_variant_set_field(&v, "status", w); + r = sd_json_variant_set_field(&v, "status", w); if (r < 0) return r; - json_variant_unref(h->json); + sd_json_variant_unref(h->json); h->json = TAKE_PTR(v); h->ratelimit_begin_usec = new_ratelimit_begin_usec; @@ -1398,7 +1401,7 @@ int user_record_is_supported(UserRecord *hr, sd_bus_error *error) { return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot manage custom blob directories."); } - if (json_variant_by_key(hr->json, HOMEWORK_BLOB_FDMAP_FIELD)) + if (sd_json_variant_by_key(hr->json, HOMEWORK_BLOB_FDMAP_FIELD)) return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "User record contains unsafe internal fields."); return 0; @@ -1420,11 +1423,11 @@ bool user_record_shall_rebalance(UserRecord *h) { } int user_record_set_rebalance_weight(UserRecord *h, uint64_t weight) { - _cleanup_(json_variant_unrefp) JsonVariant *new_per_machine_array = NULL, *machine_id_variant = NULL, + _cleanup_(sd_json_variant_unrefp) sd_json_variant *new_per_machine_array = NULL, *machine_id_variant = NULL, *machine_id_array = NULL, *per_machine_entry = NULL; - _cleanup_free_ JsonVariant **array = NULL; + _cleanup_free_ sd_json_variant **array = NULL; size_t idx = SIZE_MAX, n; - JsonVariant *per_machine; + sd_json_variant *per_machine; sd_id128_t mid; int r; @@ -1437,48 +1440,48 @@ int user_record_set_rebalance_weight(UserRecord *h, uint64_t weight) { if (r < 0) return r; - r = json_variant_new_id128(&machine_id_variant, mid); + r = sd_json_variant_new_id128(&machine_id_variant, mid); if (r < 0) return r; - r = json_variant_new_array(&machine_id_array, (JsonVariant*[]) { machine_id_variant }, 1); + r = sd_json_variant_new_array(&machine_id_array, (sd_json_variant*[]) { machine_id_variant }, 1); if (r < 0) return r; - per_machine = json_variant_by_key(h->json, "perMachine"); + per_machine = sd_json_variant_by_key(h->json, "perMachine"); if (per_machine) { - if (!json_variant_is_array(per_machine)) + if (!sd_json_variant_is_array(per_machine)) return -EINVAL; - n = json_variant_elements(per_machine); + n = sd_json_variant_elements(per_machine); - array = new(JsonVariant*, n + 1); + array = new(sd_json_variant*, n + 1); if (!array) return -ENOMEM; for (size_t i = 0; i < n; i++) { - JsonVariant *m; + sd_json_variant *m; - array[i] = json_variant_by_index(per_machine, i); + array[i] = sd_json_variant_by_index(per_machine, i); - if (!json_variant_is_object(array[i])) + if (!sd_json_variant_is_object(array[i])) return -EINVAL; - m = json_variant_by_key(array[i], "matchMachineId"); + m = sd_json_variant_by_key(array[i], "matchMachineId"); if (!m) { /* No machineId field? Let's ignore this, but invalidate what we found so far */ idx = SIZE_MAX; continue; } - if (json_variant_equal(m, machine_id_variant) || - json_variant_equal(m, machine_id_array)) { + if (sd_json_variant_equal(m, machine_id_variant) || + sd_json_variant_equal(m, machine_id_array)) { /* Matches exactly what we are looking for. Let's use this */ idx = i; continue; } - r = per_machine_id_match(m, JSON_PERMISSIVE); + r = per_machine_id_match(m, SD_JSON_PERMISSIVE); if (r < 0) return r; if (r > 0) @@ -1490,10 +1493,10 @@ int user_record_set_rebalance_weight(UserRecord *h, uint64_t weight) { if (idx == SIZE_MAX) idx = n++; /* Nothing suitable found, place new entry at end */ else - per_machine_entry = json_variant_ref(array[idx]); + per_machine_entry = sd_json_variant_ref(array[idx]); } else { - array = new(JsonVariant*, 1); + array = new(sd_json_variant*, 1); if (!array) return -ENOMEM; @@ -1502,26 +1505,26 @@ int user_record_set_rebalance_weight(UserRecord *h, uint64_t weight) { } if (!per_machine_entry) { - r = json_variant_set_field(&per_machine_entry, "matchMachineId", machine_id_array); + r = sd_json_variant_set_field(&per_machine_entry, "matchMachineId", machine_id_array); if (r < 0) return r; } if (weight == REBALANCE_WEIGHT_UNSET) - r = json_variant_set_field(&per_machine_entry, "rebalanceWeight", NULL); /* set explicitly to NULL (so that the perMachine setting we are setting here can override the global setting) */ + r = sd_json_variant_set_field(&per_machine_entry, "rebalanceWeight", NULL); /* set explicitly to NULL (so that the perMachine setting we are setting here can override the global setting) */ else - r = json_variant_set_field_unsigned(&per_machine_entry, "rebalanceWeight", weight); + r = sd_json_variant_set_field_unsigned(&per_machine_entry, "rebalanceWeight", weight); if (r < 0) return r; assert(idx < n); array[idx] = per_machine_entry; - r = json_variant_new_array(&new_per_machine_array, array, n); + r = sd_json_variant_new_array(&new_per_machine_array, array, n); if (r < 0) return r; - r = json_variant_set_field(&h->json, "perMachine", new_per_machine_array); + r = sd_json_variant_set_field(&h->json, "perMachine", new_per_machine_array); if (r < 0) return r; @@ -1531,7 +1534,7 @@ int user_record_set_rebalance_weight(UserRecord *h, uint64_t weight) { } int user_record_ensure_blob_manifest(UserRecord *h, Hashmap *blobs, const char **ret_failed) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_hashmap_free_ Hashmap *manifest = NULL; const char *filename; void *key, *value; @@ -1569,7 +1572,7 @@ int user_record_ensure_blob_manifest(UserRecord *h, Hashmap *blobs, const char * HASHMAP_FOREACH_KEY(value, filename, blobs) { _cleanup_free_ char *filename_dup = NULL; _cleanup_free_ uint8_t *hash = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *hash_json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *hash_json = NULL; int fd = PTR_TO_FD(value); off_t initial, size; @@ -1605,7 +1608,7 @@ int user_record_ensure_blob_manifest(UserRecord *h, Hashmap *blobs, const char * if (lseek(fd, initial, SEEK_SET) < 0) return -errno; - r = json_variant_new_hex(&hash_json, hash, SHA256_DIGEST_SIZE); + r = sd_json_variant_new_hex(&hash_json, hash, SHA256_DIGEST_SIZE); if (r < 0) return r; @@ -1615,7 +1618,7 @@ int user_record_ensure_blob_manifest(UserRecord *h, Hashmap *blobs, const char * TAKE_PTR(filename_dup); /* Ownership transfers to hashmap */ TAKE_PTR(hash); - r = json_variant_set_field(&v, filename, hash_json); + r = sd_json_variant_set_field(&v, filename, hash_json); if (r < 0) return r; diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index d1c4d476f6..83e53a4560 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -9,6 +9,7 @@ #include "sd-bus.h" #include "sd-id128.h" +#include "sd-json.h" #include "alloc-util.h" #include "architecture.h" @@ -20,7 +21,6 @@ #include "format-table.h" #include "hostname-setup.h" #include "hostname-util.h" -#include "json.h" #include "main-func.h" #include "parse-argument.h" #include "pretty-print.h" @@ -35,7 +35,7 @@ static char *arg_host = NULL; static bool arg_transient = false; static bool arg_pretty = false; static bool arg_static = false; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; typedef struct StatusInfo { const char *hostname; @@ -491,10 +491,10 @@ static int show_status(int argc, char **argv, void *userdata) { sd_bus *bus = userdata; int r; - if (arg_json_format_flags != JSON_FORMAT_OFF) { + if (arg_json_format_flags != SD_JSON_FORMAT_OFF) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *text = NULL; r = bus_call_method(bus, bus_hostname, "Describe", &error, &reply, NULL); @@ -505,11 +505,11 @@ static int show_status(int argc, char **argv, void *userdata) { if (r < 0) return bus_log_parse_error(r); - r = json_parse(text, 0, &v, NULL, NULL); + r = sd_json_parse(text, 0, &v, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse JSON: %m"); - json_variant_dump(v, arg_json_format_flags, NULL, NULL); + sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); return 0; } @@ -766,7 +766,7 @@ static int parse_argv(int argc, char *argv[]) { break; case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; break; case '?': diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index fe1216fc1c..ea9b3b3843 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -7,6 +7,7 @@ #include <unistd.h> #include "sd-device.h" +#include "sd-json.h" #include "alloc-util.h" #include "bus-common-errors.h" @@ -23,7 +24,7 @@ #include "hostname-setup.h" #include "hostname-util.h" #include "id128-util.h" -#include "json.h" +#include "json-util.h" #include "main-func.h" #include "missing_capability.h" #include "nscd-flush.h" @@ -1343,13 +1344,13 @@ static int method_get_hardware_serial(sd_bus_message *m, void *userdata, sd_bus_ return sd_bus_reply_method_return(m, "s", serial); } -static int build_describe_response(Context *c, bool privileged, JsonVariant **ret) { +static int build_describe_response(Context *c, bool privileged, sd_json_variant **ret) { _cleanup_free_ char *hn = NULL, *dhn = NULL, *in = NULL, *chassis = NULL, *vendor = NULL, *model = NULL, *serial = NULL, *firmware_version = NULL, *firmware_vendor = NULL; _cleanup_strv_free_ char **os_release_pairs = NULL, **machine_info_pairs = NULL; usec_t firmware_date = USEC_INFINITY, eol = USEC_INFINITY; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; sd_id128_t machine_id, boot_id, product_uuid = SD_ID128_NULL; unsigned local_cid = VMADDR_CID_ANY; struct utsname u; @@ -1414,37 +1415,37 @@ static int build_describe_response(Context *c, bool privileged, JsonVariant **re (void) load_os_release_pairs(/* root= */ NULL, &os_release_pairs); (void) load_env_file_pairs(/* f=*/ NULL, "/etc/machine-info", &machine_info_pairs); - r = json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn)), - JSON_BUILD_PAIR("StaticHostname", JSON_BUILD_STRING(c->data[PROP_STATIC_HOSTNAME])), - JSON_BUILD_PAIR("PrettyHostname", JSON_BUILD_STRING(c->data[PROP_PRETTY_HOSTNAME])), - JSON_BUILD_PAIR("DefaultHostname", JSON_BUILD_STRING(dhn)), - JSON_BUILD_PAIR("HostnameSource", JSON_BUILD_STRING(hostname_source_to_string(c->hostname_source))), - JSON_BUILD_PAIR("IconName", JSON_BUILD_STRING(in ?: c->data[PROP_ICON_NAME])), - JSON_BUILD_PAIR("Chassis", JSON_BUILD_STRING(chassis)), - JSON_BUILD_PAIR("Deployment", JSON_BUILD_STRING(c->data[PROP_DEPLOYMENT])), - JSON_BUILD_PAIR("Location", JSON_BUILD_STRING(c->data[PROP_LOCATION])), - JSON_BUILD_PAIR("KernelName", JSON_BUILD_STRING(u.sysname)), - JSON_BUILD_PAIR("KernelRelease", JSON_BUILD_STRING(u.release)), - JSON_BUILD_PAIR("KernelVersion", JSON_BUILD_STRING(u.version)), - JSON_BUILD_PAIR("OperatingSystemPrettyName", JSON_BUILD_STRING(c->data[PROP_OS_PRETTY_NAME])), - JSON_BUILD_PAIR("OperatingSystemCPEName", JSON_BUILD_STRING(c->data[PROP_OS_CPE_NAME])), - JSON_BUILD_PAIR("OperatingSystemHomeURL", JSON_BUILD_STRING(c->data[PROP_OS_HOME_URL])), + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("Hostname", SD_JSON_BUILD_STRING(hn)), + SD_JSON_BUILD_PAIR("StaticHostname", SD_JSON_BUILD_STRING(c->data[PROP_STATIC_HOSTNAME])), + SD_JSON_BUILD_PAIR("PrettyHostname", SD_JSON_BUILD_STRING(c->data[PROP_PRETTY_HOSTNAME])), + SD_JSON_BUILD_PAIR("DefaultHostname", SD_JSON_BUILD_STRING(dhn)), + SD_JSON_BUILD_PAIR("HostnameSource", SD_JSON_BUILD_STRING(hostname_source_to_string(c->hostname_source))), + SD_JSON_BUILD_PAIR("IconName", SD_JSON_BUILD_STRING(in ?: c->data[PROP_ICON_NAME])), + SD_JSON_BUILD_PAIR("Chassis", SD_JSON_BUILD_STRING(chassis)), + SD_JSON_BUILD_PAIR("Deployment", SD_JSON_BUILD_STRING(c->data[PROP_DEPLOYMENT])), + SD_JSON_BUILD_PAIR("Location", SD_JSON_BUILD_STRING(c->data[PROP_LOCATION])), + SD_JSON_BUILD_PAIR("KernelName", SD_JSON_BUILD_STRING(u.sysname)), + SD_JSON_BUILD_PAIR("KernelRelease", SD_JSON_BUILD_STRING(u.release)), + SD_JSON_BUILD_PAIR("KernelVersion", SD_JSON_BUILD_STRING(u.version)), + SD_JSON_BUILD_PAIR("OperatingSystemPrettyName", SD_JSON_BUILD_STRING(c->data[PROP_OS_PRETTY_NAME])), + SD_JSON_BUILD_PAIR("OperatingSystemCPEName", SD_JSON_BUILD_STRING(c->data[PROP_OS_CPE_NAME])), + SD_JSON_BUILD_PAIR("OperatingSystemHomeURL", SD_JSON_BUILD_STRING(c->data[PROP_OS_HOME_URL])), JSON_BUILD_PAIR_FINITE_USEC("OperatingSystemSupportEnd", eol), - JSON_BUILD_PAIR("OperatingSystemReleaseData", JSON_BUILD_STRV_ENV_PAIR(os_release_pairs)), - JSON_BUILD_PAIR("MachineInformationData", JSON_BUILD_STRV_ENV_PAIR(machine_info_pairs)), - JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor ?: c->data[PROP_HARDWARE_VENDOR])), - JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model ?: c->data[PROP_HARDWARE_MODEL])), - JSON_BUILD_PAIR("HardwareSerial", JSON_BUILD_STRING(serial)), - JSON_BUILD_PAIR("FirmwareVersion", JSON_BUILD_STRING(firmware_version)), - JSON_BUILD_PAIR("FirmwareVendor", JSON_BUILD_STRING(firmware_vendor)), + SD_JSON_BUILD_PAIR("OperatingSystemReleaseData", JSON_BUILD_STRV_ENV_PAIR(os_release_pairs)), + SD_JSON_BUILD_PAIR("MachineInformationData", JSON_BUILD_STRV_ENV_PAIR(machine_info_pairs)), + SD_JSON_BUILD_PAIR("HardwareVendor", SD_JSON_BUILD_STRING(vendor ?: c->data[PROP_HARDWARE_VENDOR])), + SD_JSON_BUILD_PAIR("HardwareModel", SD_JSON_BUILD_STRING(model ?: c->data[PROP_HARDWARE_MODEL])), + SD_JSON_BUILD_PAIR("HardwareSerial", SD_JSON_BUILD_STRING(serial)), + SD_JSON_BUILD_PAIR("FirmwareVersion", SD_JSON_BUILD_STRING(firmware_version)), + SD_JSON_BUILD_PAIR("FirmwareVendor", SD_JSON_BUILD_STRING(firmware_vendor)), JSON_BUILD_PAIR_FINITE_USEC("FirmwareDate", firmware_date), - JSON_BUILD_PAIR_ID128("MachineID", machine_id), - JSON_BUILD_PAIR_ID128("BootID", boot_id), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_ID128(product_uuid)), - JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_NULL), - JSON_BUILD_PAIR_CONDITION(local_cid != VMADDR_CID_ANY, "VSockCID", JSON_BUILD_UNSIGNED(local_cid)), - JSON_BUILD_PAIR_CONDITION(local_cid == VMADDR_CID_ANY, "VSockCID", JSON_BUILD_NULL))); + SD_JSON_BUILD_PAIR_ID128("MachineID", machine_id), + SD_JSON_BUILD_PAIR_ID128("BootID", boot_id), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid), "ProductUUID", SD_JSON_BUILD_ID128(product_uuid)), + SD_JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid), "ProductUUID", SD_JSON_BUILD_NULL), + SD_JSON_BUILD_PAIR_CONDITION(local_cid != VMADDR_CID_ANY, "VSockCID", SD_JSON_BUILD_UNSIGNED(local_cid)), + SD_JSON_BUILD_PAIR_CONDITION(local_cid == VMADDR_CID_ANY, "VSockCID", SD_JSON_BUILD_NULL))); if (r < 0) return log_error_errno(r, "Failed to build JSON data: %m"); @@ -1453,7 +1454,7 @@ static int build_describe_response(Context *c, bool privileged, JsonVariant **re } static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *error) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; Context *c = ASSERT_PTR(userdata); _cleanup_free_ char *text = NULL; bool privileged; @@ -1478,7 +1479,7 @@ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *erro if (r < 0) return r; - r = json_variant_format(v, 0, &text); + r = sd_json_variant_format(v, 0, &text); if (r < 0) return log_error_errno(r, "Failed to format JSON data: %m"); @@ -1602,8 +1603,8 @@ static int connect_bus(Context *c) { return 0; } -static int vl_method_describe(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { +static int vl_method_describe(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { VARLINK_DISPATCH_POLKIT_FIELD, {} }; @@ -1632,10 +1633,10 @@ static int vl_method_describe(Varlink *link, JsonVariant *parameters, VarlinkMet * the product ID which we'll check explicitly. */ privileged = r > 0; - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = build_describe_response(c, privileged, &v); if (r < 0) return r; diff --git a/src/id128/id128.c b/src/id128/id128.c index fa86cf666b..a9ea6c5dee 100644 --- a/src/id128/id128.c +++ b/src/id128/id128.c @@ -20,7 +20,7 @@ static sd_id128_t arg_app = {}; static bool arg_value = false; static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static int verb_new(int argc, char **argv, void *userdata) { return id128_print_new(arg_mode); @@ -248,7 +248,7 @@ static int parse_argv(int argc, char *argv[]) { break; case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; break; case ARG_JSON: diff --git a/src/import/importctl.c b/src/import/importctl.c index f939d80815..d81e79e3cd 100644 --- a/src/import/importctl.c +++ b/src/import/importctl.c @@ -42,7 +42,7 @@ static bool arg_quiet = false; static bool arg_ask_password = true; static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE; static const char* arg_format = NULL; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static ImageClass arg_image_class = _IMAGE_CLASS_INVALID; #define PROGRESS_PREFIX "Total: " @@ -932,7 +932,7 @@ static int list_images(int argc, char *argv[], void *userdata) { if (r < 0) return table_log_add_error(r); - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) r = table_add_many( t, TABLE_STRING, read_only ? "ro" : "rw", @@ -1151,7 +1151,7 @@ static int parse_argv(int argc, char *argv[]) { break; case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; arg_legend = false; break; diff --git a/src/journal/journalctl-misc.c b/src/journal/journalctl-misc.c index 8ca6ea2143..2a5d9edfc3 100644 --- a/src/journal/journalctl-misc.c +++ b/src/journal/journalctl-misc.c @@ -285,7 +285,7 @@ int action_list_namespaces(void) { } } - if (table_isempty(table) && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (table_isempty(table) && FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { if (!arg_quiet) log_notice("No namespaces found."); } else { diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 45173a6813..369280a009 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -33,7 +33,7 @@ enum { JournalctlAction arg_action = ACTION_SHOW; OutputMode arg_output = OUTPUT_SHORT; -JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; PagerFlags arg_pager_flags = 0; bool arg_utc = false; bool arg_follow = false; @@ -460,9 +460,9 @@ static int parse_argv(int argc, char *argv[]) { arg_quiet = true; if (OUTPUT_MODE_IS_JSON(arg_output)) - arg_json_format_flags = output_mode_to_json_format_flags(arg_output) | JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = output_mode_to_json_format_flags(arg_output) | SD_JSON_FORMAT_COLOR_AUTO; else - arg_json_format_flags = JSON_FORMAT_OFF; + arg_json_format_flags = SD_JSON_FORMAT_OFF; break; diff --git a/src/journal/journalctl.h b/src/journal/journalctl.h index c6993a201a..2d86c16917 100644 --- a/src/journal/journalctl.h +++ b/src/journal/journalctl.h @@ -5,8 +5,8 @@ #include <stdbool.h> #include "sd-id128.h" +#include "sd-json.h" -#include "json.h" #include "output-mode.h" #include "pager.h" #include "pcre2-util.h" @@ -37,7 +37,7 @@ typedef enum JournalctlAction { extern JournalctlAction arg_action; extern OutputMode arg_output; -extern JsonFormatFlags arg_json_format_flags; +extern sd_json_format_flags_t arg_json_format_flags; extern PagerFlags arg_pager_flags; extern bool arg_utc; extern bool arg_follow; diff --git a/src/journal/journald-client.c b/src/journal/journald-client.c index 25970c13a6..07b903e2b9 100644 --- a/src/journal/journald-client.c +++ b/src/journal/journald-client.c @@ -5,6 +5,7 @@ #include "journald-client.h" #include "nulstr-util.h" #include "pcre2-util.h" +#include "strv.h" /* This consumes both `allow_list` and `deny_list` arguments. Hence, those arguments are not owned by the * caller anymore and should not be freed. */ diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 211f5c3d83..28bd223c9e 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -2212,14 +2212,14 @@ static void synchronize_destroy(void *userdata) { varlink_unref(userdata); } -static int vl_method_synchronize(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_synchronize(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { _cleanup_(sd_event_source_unrefp) sd_event_source *event_source = NULL; Server *s = ASSERT_PTR(userdata); int r; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); log_info("Received client request to sync journal."); @@ -2253,12 +2253,12 @@ static int vl_method_synchronize(Varlink *link, JsonVariant *parameters, Varlink return 0; } -static int vl_method_rotate(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_rotate(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { Server *s = ASSERT_PTR(userdata); assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); log_info("Received client request to rotate journal, rotating."); @@ -2267,12 +2267,12 @@ static int vl_method_rotate(Varlink *link, JsonVariant *parameters, VarlinkMetho return varlink_reply(link, NULL); } -static int vl_method_flush_to_var(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_flush_to_var(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { Server *s = ASSERT_PTR(userdata); assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); if (s->namespace) return varlink_error(link, "io.systemd.Journal.NotSupportedByNamespaces", NULL); @@ -2283,12 +2283,12 @@ static int vl_method_flush_to_var(Varlink *link, JsonVariant *parameters, Varlin return varlink_reply(link, NULL); } -static int vl_method_relinquish_var(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_relinquish_var(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { Server *s = ASSERT_PTR(userdata); assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); if (s->namespace) return varlink_error(link, "io.systemd.Journal.NotSupportedByNamespaces", NULL); diff --git a/src/kernel-install/kernel-install.c b/src/kernel-install/kernel-install.c index 5d559a97a8..fafddab907 100644 --- a/src/kernel-install/kernel-install.c +++ b/src/kernel-install/kernel-install.c @@ -41,7 +41,7 @@ static char *arg_esp_path = NULL; static char *arg_xbootldr_path = NULL; static int arg_make_entry_directory = -1; /* tristate */ static PagerFlags arg_pager_flags = 0; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static char *arg_root = NULL; static char *arg_image = NULL; static ImagePolicy *arg_image_policy = NULL; @@ -1371,7 +1371,7 @@ static int verb_inspect(int argc, char *argv[], void *userdata) { if (r < 0) return table_log_add_error(r); - if (arg_json_format_flags & JSON_FORMAT_OFF) { + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) { r = table_add_many(t, TABLE_FIELD, "Plugin Arguments", TABLE_STRV, strv_skip(c->argv, 1)); diff --git a/src/libsystemd-network/dhcp-client-id-internal.h b/src/libsystemd-network/dhcp-client-id-internal.h index 655f17b2e9..382575e7a2 100644 --- a/src/libsystemd-network/dhcp-client-id-internal.h +++ b/src/libsystemd-network/dhcp-client-id-internal.h @@ -2,9 +2,9 @@ #pragma once #include "sd-dhcp-client-id.h" +#include "sd-json.h" #include "dhcp-duid-internal.h" -#include "json.h" #include "macro.h" #include "siphash24.h" #include "sparse-endian.h" @@ -57,4 +57,4 @@ static inline bool client_id_data_size_is_valid(size_t size) { void client_id_hash_func(const sd_dhcp_client_id *client_id, struct siphash *state); int client_id_compare_func(const sd_dhcp_client_id *a, const sd_dhcp_client_id *b); -int json_dispatch_client_id(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); +int json_dispatch_client_id(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); diff --git a/src/libsystemd-network/dhcp-server-lease-internal.h b/src/libsystemd-network/dhcp-server-lease-internal.h index 7626552811..f206e0118f 100644 --- a/src/libsystemd-network/dhcp-server-lease-internal.h +++ b/src/libsystemd-network/dhcp-server-lease-internal.h @@ -2,10 +2,10 @@ #pragma once #include "sd-dhcp-server-lease.h" +#include "sd-json.h" #include "dhcp-client-id-internal.h" #include "dhcp-server-internal.h" -#include "json.h" #include "time-util.h" typedef struct sd_dhcp_server_lease { @@ -33,8 +33,8 @@ int dhcp_server_cleanup_expired_leases(sd_dhcp_server *server); sd_dhcp_server_lease* dhcp_server_get_static_lease(sd_dhcp_server *server, const DHCPRequest *req); -int dhcp_server_bound_leases_append_json(sd_dhcp_server *server, JsonVariant **v); -int dhcp_server_static_leases_append_json(sd_dhcp_server *server, JsonVariant **v); +int dhcp_server_bound_leases_append_json(sd_dhcp_server *server, sd_json_variant **v); +int dhcp_server_static_leases_append_json(sd_dhcp_server *server, sd_json_variant **v); int dhcp_server_save_leases(sd_dhcp_server *server); int dhcp_server_load_leases(sd_dhcp_server *server); diff --git a/src/libsystemd-network/fuzz-lldp-rx.c b/src/libsystemd-network/fuzz-lldp-rx.c index dad1038ca0..00e6d81809 100644 --- a/src/libsystemd-network/fuzz-lldp-rx.c +++ b/src/libsystemd-network/fuzz-lldp-rx.c @@ -24,7 +24,7 @@ int lldp_network_bind_raw_socket(int ifindex) { int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { _cleanup_(sd_event_unrefp) sd_event *e = NULL; _cleanup_(sd_lldp_rx_unrefp) sd_lldp_rx *lldp_rx = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(memstream_done) MemStream m = {}; FILE *f; @@ -44,7 +44,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { assert_se(lldp_rx_build_neighbors_json(lldp_rx, &v) >= 0); assert_se(f = memstream_init(&m)); - (void) json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, f, NULL); + (void) sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR, f, NULL); assert_se(sd_lldp_rx_stop(lldp_rx) >= 0); assert_se(sd_lldp_rx_detach_event(lldp_rx) >= 0); diff --git a/src/libsystemd-network/lldp-neighbor.c b/src/libsystemd-network/lldp-neighbor.c index a4384ac2e1..77e516c1a8 100644 --- a/src/libsystemd-network/lldp-neighbor.c +++ b/src/libsystemd-network/lldp-neighbor.c @@ -5,6 +5,7 @@ #include "ether-addr-util.h" #include "hexdecoct.h" #include "in-addr-util.h" +#include "json-util.h" #include "lldp-neighbor.h" #include "memory-util.h" #include "missing_network.h" @@ -761,7 +762,7 @@ int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor *n, clockid_t clock, uint64_ return 0; } -int lldp_neighbor_build_json(sd_lldp_neighbor *n, JsonVariant **ret) { +int lldp_neighbor_build_json(sd_lldp_neighbor *n, sd_json_variant **ret) { const char *chassis_id = NULL, *port_id = NULL, *port_description = NULL, *system_name = NULL, *system_description = NULL; uint16_t cc = 0; @@ -778,13 +779,13 @@ int lldp_neighbor_build_json(sd_lldp_neighbor *n, JsonVariant **ret) { valid_cc = sd_lldp_neighbor_get_enabled_capabilities(n, &cc) >= 0; - return json_build(ret, JSON_BUILD_OBJECT( + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( JSON_BUILD_PAIR_STRING_NON_EMPTY("ChassisID", chassis_id), - JSON_BUILD_PAIR_BYTE_ARRAY("RawChassisID", n->id.chassis_id, n->id.chassis_id_size), + SD_JSON_BUILD_PAIR_BYTE_ARRAY("RawChassisID", n->id.chassis_id, n->id.chassis_id_size), JSON_BUILD_PAIR_STRING_NON_EMPTY("PortID", port_id), - JSON_BUILD_PAIR_BYTE_ARRAY("RawPortID", n->id.port_id, n->id.port_id_size), + SD_JSON_BUILD_PAIR_BYTE_ARRAY("RawPortID", n->id.port_id, n->id.port_id_size), JSON_BUILD_PAIR_STRING_NON_EMPTY("PortDescription", port_description), JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemName", system_name), JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemDescription", system_description), - JSON_BUILD_PAIR_CONDITION(valid_cc, "EnabledCapabilities", JSON_BUILD_UNSIGNED(cc)))); + SD_JSON_BUILD_PAIR_CONDITION(valid_cc, "EnabledCapabilities", SD_JSON_BUILD_UNSIGNED(cc)))); } diff --git a/src/libsystemd-network/lldp-neighbor.h b/src/libsystemd-network/lldp-neighbor.h index 06ba4c7460..57ac3a3f20 100644 --- a/src/libsystemd-network/lldp-neighbor.h +++ b/src/libsystemd-network/lldp-neighbor.h @@ -5,10 +5,10 @@ #include <stdbool.h> #include <sys/types.h> +#include "sd-json.h" #include "sd-lldp-rx.h" #include "hash-funcs.h" -#include "json.h" #include "lldp-rx-internal.h" #include "time-util.h" @@ -91,4 +91,4 @@ sd_lldp_neighbor *lldp_neighbor_new(size_t raw_size); int lldp_neighbor_parse(sd_lldp_neighbor *n); void lldp_neighbor_start_ttl(sd_lldp_neighbor *n); bool lldp_neighbor_equal(const sd_lldp_neighbor *a, const sd_lldp_neighbor *b); -int lldp_neighbor_build_json(sd_lldp_neighbor *n, JsonVariant **ret); +int lldp_neighbor_build_json(sd_lldp_neighbor *n, sd_json_variant **ret); diff --git a/src/libsystemd-network/lldp-rx-internal.h b/src/libsystemd-network/lldp-rx-internal.h index e914c6bfc6..33f0add399 100644 --- a/src/libsystemd-network/lldp-rx-internal.h +++ b/src/libsystemd-network/lldp-rx-internal.h @@ -2,10 +2,10 @@ #pragma once #include "sd-event.h" +#include "sd-json.h" #include "sd-lldp-rx.h" #include "hashmap.h" -#include "json.h" #include "network-common.h" #include "prioq.h" @@ -37,7 +37,7 @@ struct sd_lldp_rx { const char* lldp_rx_event_to_string(sd_lldp_rx_event_t e) _const_; sd_lldp_rx_event_t lldp_rx_event_from_string(const char *s) _pure_; -int lldp_rx_build_neighbors_json(sd_lldp_rx *lldp_rx, JsonVariant **ret); +int lldp_rx_build_neighbors_json(sd_lldp_rx *lldp_rx, sd_json_variant **ret); #define log_lldp_rx_errno(lldp_rx, error, fmt, ...) \ log_interface_prefix_full_errno( \ diff --git a/src/libsystemd-network/sd-dhcp-client-id.c b/src/libsystemd-network/sd-dhcp-client-id.c index cab04f05e5..3e06501906 100644 --- a/src/libsystemd-network/sd-dhcp-client-id.c +++ b/src/libsystemd-network/sd-dhcp-client-id.c @@ -3,6 +3,7 @@ #include "alloc-util.h" #include "dhcp-client-id-internal.h" #include "iovec-util.h" +#include "json-util.h" #include "unaligned.h" #include "utf8.h" @@ -179,7 +180,7 @@ int client_id_compare_func(const sd_dhcp_client_id *a, const sd_dhcp_client_id * return memcmp_nn(a->raw, a->size, b->raw, b->size); } -int json_dispatch_client_id(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +int json_dispatch_client_id(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { sd_dhcp_client_id *client_id = ASSERT_PTR(userdata); _cleanup_(iovec_done) struct iovec iov = {}; int r; diff --git a/src/libsystemd-network/sd-dhcp-server-lease.c b/src/libsystemd-network/sd-dhcp-server-lease.c index 2f84d51e65..69a285b44a 100644 --- a/src/libsystemd-network/sd-dhcp-server-lease.c +++ b/src/libsystemd-network/sd-dhcp-server-lease.c @@ -3,6 +3,7 @@ #include "dhcp-server-lease-internal.h" #include "fd-util.h" #include "fs-util.h" +#include "json-util.h" #include "mkdir.h" #include "tmpfile-util.h" @@ -209,19 +210,19 @@ int sd_dhcp_server_set_static_lease( return 0; } -static int dhcp_server_lease_append_json(sd_dhcp_server_lease *lease, JsonVariant **ret) { +static int dhcp_server_lease_append_json(sd_dhcp_server_lease *lease, sd_json_variant **ret) { assert(lease); assert(ret); - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_BYTE_ARRAY("ClientId", lease->client_id.raw, lease->client_id.size), + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_BYTE_ARRAY("ClientId", lease->client_id.raw, lease->client_id.size), JSON_BUILD_PAIR_IN4_ADDR_NON_NULL("Address", &(struct in_addr) { .s_addr = lease->address }), JSON_BUILD_PAIR_STRING_NON_EMPTY("Hostname", lease->hostname))); } -int dhcp_server_bound_leases_append_json(sd_dhcp_server *server, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +int dhcp_server_bound_leases_append_json(sd_dhcp_server *server, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; sd_dhcp_server_lease *lease; usec_t now_b, now_r; int r; @@ -238,7 +239,7 @@ int dhcp_server_bound_leases_append_json(sd_dhcp_server *server, JsonVariant **v return r; HASHMAP_FOREACH(lease, server->bound_leases_by_client_id) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; r = dhcp_server_lease_append_json(lease, &w); if (r < 0) @@ -246,14 +247,14 @@ int dhcp_server_bound_leases_append_json(sd_dhcp_server *server, JsonVariant **v usec_t exp_r = map_clock_usec_raw(lease->expiration, now_b, now_r); - r = json_variant_merge_objectb(&w, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("ExpirationUSec", lease->expiration), - JSON_BUILD_PAIR_UNSIGNED("ExpirationRealtimeUSec", exp_r))); + r = sd_json_variant_merge_objectb(&w, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("ExpirationUSec", lease->expiration), + SD_JSON_BUILD_PAIR_UNSIGNED("ExpirationRealtimeUSec", exp_r))); if (r < 0) return r; - r = json_variant_append_array(&array, w); + r = sd_json_variant_append_array(&array, w); if (r < 0) return r; } @@ -261,8 +262,8 @@ int dhcp_server_bound_leases_append_json(sd_dhcp_server *server, JsonVariant **v return json_variant_set_field_non_null(v, "Leases", array); } -int dhcp_server_static_leases_append_json(sd_dhcp_server *server, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +int dhcp_server_static_leases_append_json(sd_dhcp_server *server, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; sd_dhcp_server_lease *lease; int r; @@ -270,13 +271,13 @@ int dhcp_server_static_leases_append_json(sd_dhcp_server *server, JsonVariant ** assert(v); HASHMAP_FOREACH(lease, server->static_leases_by_client_id) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; r = dhcp_server_lease_append_json(lease, &w); if (r < 0) return r; - r = json_variant_append_array(&array, w); + r = sd_json_variant_append_array(&array, w); if (r < 0) return r; } @@ -285,7 +286,7 @@ int dhcp_server_static_leases_append_json(sd_dhcp_server *server, JsonVariant ** } int dhcp_server_save_leases(sd_dhcp_server *server) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *temp_path = NULL; _cleanup_fclose_ FILE *f = NULL; sd_id128_t boot_id; @@ -307,10 +308,10 @@ int dhcp_server_save_leases(sd_dhcp_server *server) { if (r < 0) return r; - r = json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_ID128("BootID", boot_id), + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_ID128("BootID", boot_id), JSON_BUILD_PAIR_IN4_ADDR("Address", &(struct in_addr) { .s_addr = server->address }), - JSON_BUILD_PAIR_UNSIGNED("PrefixLength", + SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", in4_addr_netmask_to_prefixlen(&(struct in_addr) { .s_addr = server->netmask })))); if (r < 0) return r; @@ -329,7 +330,7 @@ int dhcp_server_save_leases(sd_dhcp_server *server) { (void) fchmod(fileno(f), 0644); - r = json_variant_dump(v, JSON_FORMAT_NEWLINE | JSON_FORMAT_FLUSH, f, /* prefix = */ NULL); + r = sd_json_variant_dump(v, SD_JSON_FORMAT_NEWLINE | SD_JSON_FORMAT_FLUSH, f, /* prefix = */ NULL); if (r < 0) goto failure; @@ -344,20 +345,20 @@ failure: return r; } -static int json_dispatch_dhcp_lease(sd_dhcp_server *server, JsonVariant *v, bool use_boottime) { - static const JsonDispatch dispatch_table_boottime[] = { - { "ClientId", JSON_VARIANT_ARRAY, json_dispatch_client_id, offsetof(sd_dhcp_server_lease, client_id), JSON_MANDATORY }, - { "Address", JSON_VARIANT_ARRAY, json_dispatch_in_addr, offsetof(sd_dhcp_server_lease, address), JSON_MANDATORY }, - { "Hostname", JSON_VARIANT_STRING, json_dispatch_string, offsetof(sd_dhcp_server_lease, hostname), 0 }, - { "ExpirationUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(sd_dhcp_server_lease, expiration), JSON_MANDATORY }, - { "ExpirationRealtimeUSec", _JSON_VARIANT_TYPE_INVALID, NULL, 0, JSON_MANDATORY }, +static int json_dispatch_dhcp_lease(sd_dhcp_server *server, sd_json_variant *v, bool use_boottime) { + static const sd_json_dispatch_field dispatch_table_boottime[] = { + { "ClientId", SD_JSON_VARIANT_ARRAY, json_dispatch_client_id, offsetof(sd_dhcp_server_lease, client_id), SD_JSON_MANDATORY }, + { "Address", SD_JSON_VARIANT_ARRAY, json_dispatch_in_addr, offsetof(sd_dhcp_server_lease, address), SD_JSON_MANDATORY }, + { "Hostname", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(sd_dhcp_server_lease, hostname), 0 }, + { "ExpirationUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(sd_dhcp_server_lease, expiration), SD_JSON_MANDATORY }, + { "ExpirationRealtimeUSec", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, SD_JSON_MANDATORY }, {} }, dispatch_table_realtime[] = { - { "ClientId", JSON_VARIANT_ARRAY, json_dispatch_client_id, offsetof(sd_dhcp_server_lease, client_id), JSON_MANDATORY }, - { "Address", JSON_VARIANT_ARRAY, json_dispatch_in_addr, offsetof(sd_dhcp_server_lease, address), JSON_MANDATORY }, - { "Hostname", JSON_VARIANT_STRING, json_dispatch_string, offsetof(sd_dhcp_server_lease, hostname), 0 }, - { "ExpirationUSec", _JSON_VARIANT_TYPE_INVALID, NULL, 0, JSON_MANDATORY }, - { "ExpirationRealtimeUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(sd_dhcp_server_lease, expiration), JSON_MANDATORY }, + { "ClientId", SD_JSON_VARIANT_ARRAY, json_dispatch_client_id, offsetof(sd_dhcp_server_lease, client_id), SD_JSON_MANDATORY }, + { "Address", SD_JSON_VARIANT_ARRAY, json_dispatch_in_addr, offsetof(sd_dhcp_server_lease, address), SD_JSON_MANDATORY }, + { "Hostname", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(sd_dhcp_server_lease, hostname), 0 }, + { "ExpirationUSec", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, SD_JSON_MANDATORY }, + { "ExpirationRealtimeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(sd_dhcp_server_lease, expiration), SD_JSON_MANDATORY }, {} }; @@ -376,7 +377,7 @@ static int json_dispatch_dhcp_lease(sd_dhcp_server *server, JsonVariant *v, bool .n_ref = 1, }; - r = json_dispatch(v, use_boottime ? dispatch_table_boottime : dispatch_table_realtime, JSON_ALLOW_EXTENSIONS, lease); + r = sd_json_dispatch(v, use_boottime ? dispatch_table_boottime : dispatch_table_realtime, SD_JSON_ALLOW_EXTENSIONS, lease); if (r < 0) return r; @@ -414,25 +415,25 @@ typedef struct SavedInfo { sd_id128_t boot_id; struct in_addr address; uint8_t prefixlen; - JsonVariant *leases; + sd_json_variant *leases; } SavedInfo; static void saved_info_done(SavedInfo *info) { if (!info) return; - json_variant_unref(info->leases); + sd_json_variant_unref(info->leases); } static int load_leases_file(int dir_fd, const char *path, SavedInfo *ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(dir_fd >= 0 || dir_fd == AT_FDCWD); assert(path); assert(ret); - r = json_parse_file_at( + r = sd_json_parse_file_at( /* f = */ NULL, dir_fd, path, @@ -443,15 +444,15 @@ static int load_leases_file(int dir_fd, const char *path, SavedInfo *ret) { if (r < 0) return r; - static const JsonDispatch dispatch_lease_file_table[] = { - { "BootID", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(SavedInfo, boot_id), JSON_MANDATORY }, - { "Address", JSON_VARIANT_ARRAY, json_dispatch_in_addr, offsetof(SavedInfo, address), JSON_MANDATORY }, - { "PrefixLength", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint8, offsetof(SavedInfo, prefixlen), JSON_MANDATORY }, - { "Leases", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(SavedInfo, leases), JSON_MANDATORY }, + static const sd_json_dispatch_field dispatch_lease_file_table[] = { + { "BootID", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(SavedInfo, boot_id), SD_JSON_MANDATORY }, + { "Address", SD_JSON_VARIANT_ARRAY, json_dispatch_in_addr, offsetof(SavedInfo, address), SD_JSON_MANDATORY }, + { "PrefixLength", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint8, offsetof(SavedInfo, prefixlen), SD_JSON_MANDATORY }, + { "Leases", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(SavedInfo, leases), SD_JSON_MANDATORY }, {} }; - return json_dispatch(v, dispatch_lease_file_table, JSON_ALLOW_EXTENSIONS, ret); + return sd_json_dispatch(v, dispatch_lease_file_table, SD_JSON_ALLOW_EXTENSIONS, ret); } int dhcp_server_load_leases(sd_dhcp_server *server) { @@ -478,7 +479,7 @@ int dhcp_server_load_leases(sd_dhcp_server *server) { n = hashmap_size(server->bound_leases_by_client_id); - JsonVariant *i; + sd_json_variant *i; JSON_VARIANT_ARRAY_FOREACH(i, info.leases) RET_GATHER(r, json_dispatch_dhcp_lease(server, i, /* use_boottime = */ sd_id128_equal(info.boot_id, boot_id))); diff --git a/src/libsystemd-network/sd-lldp-rx.c b/src/libsystemd-network/sd-lldp-rx.c index 74000ffb8b..029ee0282c 100644 --- a/src/libsystemd-network/sd-lldp-rx.c +++ b/src/libsystemd-network/sd-lldp-rx.c @@ -4,13 +4,13 @@ #include <linux/sockios.h> #include <sys/ioctl.h> +#include "sd-json.h" #include "sd-lldp-rx.h" #include "alloc-util.h" #include "ether-addr-util.h" #include "event-util.h" #include "fd-util.h" -#include "json.h" #include "lldp-neighbor.h" #include "lldp-network.h" #include "lldp-rx-internal.h" @@ -491,8 +491,8 @@ int sd_lldp_rx_get_neighbors(sd_lldp_rx *lldp_rx, sd_lldp_neighbor ***ret) { return k; } -int lldp_rx_build_neighbors_json(sd_lldp_rx *lldp_rx, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +int lldp_rx_build_neighbors_json(sd_lldp_rx *lldp_rx, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(lldp_rx); @@ -500,13 +500,13 @@ int lldp_rx_build_neighbors_json(sd_lldp_rx *lldp_rx, JsonVariant **ret) { sd_lldp_neighbor *n; HASHMAP_FOREACH(n, lldp_rx->neighbor_by_id) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; r = lldp_neighbor_build_json(n, &w); if (r < 0) return r; - r = json_variant_append_array(&v, w); + r = sd_json_variant_append_array(&v, w); if (r < 0) return r; } diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 6874240b8a..1799706c3c 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -847,4 +847,104 @@ global: LIBSYSTEMD_257 { global: sd_bus_pending_method_calls; + sd_json_build; + sd_json_buildv; + sd_json_dispatch; + sd_json_dispatch_const_string; + sd_json_dispatch_full; + sd_json_dispatch_id128; + sd_json_dispatch_int16; + sd_json_dispatch_int32; + sd_json_dispatch_int64; + sd_json_dispatch_int8; + sd_json_dispatch_intbool; + sd_json_dispatch_stdbool; + sd_json_dispatch_string; + sd_json_dispatch_strv; + sd_json_dispatch_tristate; + sd_json_dispatch_uid_gid; + sd_json_dispatch_uint16; + sd_json_dispatch_uint32; + sd_json_dispatch_uint64; + sd_json_dispatch_uint8; + sd_json_dispatch_unsupported; + sd_json_dispatch_variant; + sd_json_dispatch_variant_noref; + sd_json_parse; + sd_json_parse_continue; + sd_json_parse_file; + sd_json_parse_file_at; + sd_json_parse_with_source; + sd_json_parse_with_source_continue; + sd_json_variant_append_array; + sd_json_variant_append_array_nodup; + sd_json_variant_append_arrayb; + sd_json_variant_boolean; + sd_json_variant_by_index; + sd_json_variant_by_key; + sd_json_variant_by_key_full; + sd_json_variant_dump; + sd_json_variant_elements; + sd_json_variant_equal; + sd_json_variant_filter; + sd_json_variant_find; + sd_json_variant_format; + sd_json_variant_get_source; + sd_json_variant_has_type; + sd_json_variant_integer; + sd_json_variant_is_array; + sd_json_variant_is_blank_array; + sd_json_variant_is_blank_object; + sd_json_variant_is_boolean; + sd_json_variant_is_integer; + sd_json_variant_is_negative; + sd_json_variant_is_normalized; + sd_json_variant_is_null; + sd_json_variant_is_number; + sd_json_variant_is_object; + sd_json_variant_is_real; + sd_json_variant_is_sensitive; + sd_json_variant_is_sensitive_recursive; + sd_json_variant_is_sorted; + sd_json_variant_is_string; + sd_json_variant_is_unsigned; + sd_json_variant_merge_object; + sd_json_variant_merge_objectb; + sd_json_variant_new_array; + sd_json_variant_new_array_bytes; + sd_json_variant_new_array_strv; + sd_json_variant_new_base32hex; + sd_json_variant_new_base64; + sd_json_variant_new_boolean; + sd_json_variant_new_hex; + sd_json_variant_new_id128; + sd_json_variant_new_integer; + sd_json_variant_new_null; + sd_json_variant_new_object; + sd_json_variant_new_octescape; + sd_json_variant_new_real; + sd_json_variant_new_string; + sd_json_variant_new_stringn; + sd_json_variant_new_unsigned; + sd_json_variant_new_uuid; + sd_json_variant_normalize; + sd_json_variant_real; + sd_json_variant_ref; + sd_json_variant_sensitive; + sd_json_variant_set_field; + sd_json_variant_set_field_boolean; + sd_json_variant_set_field_integer; + sd_json_variant_set_field_string; + sd_json_variant_set_field_strv; + sd_json_variant_set_field_unsigned; + sd_json_variant_set_fieldb; + sd_json_variant_sort; + sd_json_variant_string; + sd_json_variant_strv; + sd_json_variant_type; + sd_json_variant_unbase64; + sd_json_variant_unhex; + sd_json_variant_unref; + sd_json_variant_unref_many; + sd_json_variant_unsigned; } LIBSYSTEMD_256; diff --git a/src/libsystemd/meson.build b/src/libsystemd/meson.build index 6d4337d1a7..2b0ad906f8 100644 --- a/src/libsystemd/meson.build +++ b/src/libsystemd/meson.build @@ -66,6 +66,13 @@ sd_login_sources = files('sd-login/sd-login.c') ############################################################ +sd_json_sources = files( + 'sd-json/json-util.c', + 'sd-json/sd-json.c', +) + +############################################################ + libsystemd_sources = files( 'sd-bus/bus-common-errors.c', 'sd-bus/bus-container.c', @@ -109,7 +116,7 @@ libsystemd_sources = files( 'sd-network/sd-network.c', 'sd-path/sd-path.c', 'sd-resolve/sd-resolve.c', -) + sd_journal_sources + id128_sources + sd_daemon_sources + sd_event_sources + sd_login_sources +) + sd_journal_sources + id128_sources + sd_daemon_sources + sd_event_sources + sd_login_sources + sd_json_sources libsystemd_c_args = ['-fvisibility=default'] @@ -120,6 +127,7 @@ libsystemd_static = static_library( c_args : libsystemd_c_args, link_with : [libbasic], dependencies : [threads, + libm, librt, userspace], build_by_default : false) diff --git a/src/shared/json-internal.h b/src/libsystemd/sd-json/json-internal.h index a94befa753..5117b39f45 100644 --- a/src/shared/json-internal.h +++ b/src/libsystemd/sd-json/json-internal.h @@ -2,7 +2,7 @@ #pragma once -#include "json.h" +#include "sd-json.h" /* This header should include all prototypes only the JSON parser itself and * its tests need access to. Normal code consuming the JSON parser should not @@ -23,31 +23,31 @@ assert_cc(sizeof(JsonValue) == 8U); #define JSON_VALUE_NULL ((JsonValue) {}) -/* We use fake JsonVariant objects for some special values, in order to avoid memory allocations for them. Note that +/* We use fake sd_json_variant objects for some special values, in order to avoid memory allocations for them. Note that * effectively this means that there are multiple ways to encode the same objects: via these magic values or as - * properly allocated JsonVariant. We convert between both on-the-fly as necessary. */ + * properly allocated sd_json_variant. We convert between both on-the-fly as necessary. */ enum { _JSON_VARIANT_MAGIC_TRUE = 1, -#define JSON_VARIANT_MAGIC_TRUE ((JsonVariant*) _JSON_VARIANT_MAGIC_TRUE) +#define JSON_VARIANT_MAGIC_TRUE ((sd_json_variant*) _JSON_VARIANT_MAGIC_TRUE) _JSON_VARIANT_MAGIC_FALSE, -#define JSON_VARIANT_MAGIC_FALSE ((JsonVariant*) _JSON_VARIANT_MAGIC_FALSE) +#define JSON_VARIANT_MAGIC_FALSE ((sd_json_variant*) _JSON_VARIANT_MAGIC_FALSE) _JSON_VARIANT_MAGIC_NULL, -#define JSON_VARIANT_MAGIC_NULL ((JsonVariant*) _JSON_VARIANT_MAGIC_NULL) +#define JSON_VARIANT_MAGIC_NULL ((sd_json_variant*) _JSON_VARIANT_MAGIC_NULL) _JSON_VARIANT_MAGIC_ZERO_INTEGER, -#define JSON_VARIANT_MAGIC_ZERO_INTEGER ((JsonVariant*) _JSON_VARIANT_MAGIC_ZERO_INTEGER) +#define JSON_VARIANT_MAGIC_ZERO_INTEGER ((sd_json_variant*) _JSON_VARIANT_MAGIC_ZERO_INTEGER) _JSON_VARIANT_MAGIC_ZERO_UNSIGNED, -#define JSON_VARIANT_MAGIC_ZERO_UNSIGNED ((JsonVariant*) _JSON_VARIANT_MAGIC_ZERO_UNSIGNED) +#define JSON_VARIANT_MAGIC_ZERO_UNSIGNED ((sd_json_variant*) _JSON_VARIANT_MAGIC_ZERO_UNSIGNED) _JSON_VARIANT_MAGIC_ZERO_REAL, -#define JSON_VARIANT_MAGIC_ZERO_REAL ((JsonVariant*) _JSON_VARIANT_MAGIC_ZERO_REAL) +#define JSON_VARIANT_MAGIC_ZERO_REAL ((sd_json_variant*) _JSON_VARIANT_MAGIC_ZERO_REAL) _JSON_VARIANT_MAGIC_EMPTY_STRING, -#define JSON_VARIANT_MAGIC_EMPTY_STRING ((JsonVariant*) _JSON_VARIANT_MAGIC_EMPTY_STRING) +#define JSON_VARIANT_MAGIC_EMPTY_STRING ((sd_json_variant*) _JSON_VARIANT_MAGIC_EMPTY_STRING) _JSON_VARIANT_MAGIC_EMPTY_ARRAY, -#define JSON_VARIANT_MAGIC_EMPTY_ARRAY ((JsonVariant*) _JSON_VARIANT_MAGIC_EMPTY_ARRAY) +#define JSON_VARIANT_MAGIC_EMPTY_ARRAY ((sd_json_variant*) _JSON_VARIANT_MAGIC_EMPTY_ARRAY) _JSON_VARIANT_MAGIC_EMPTY_OBJECT, -#define JSON_VARIANT_MAGIC_EMPTY_OBJECT ((JsonVariant*) _JSON_VARIANT_MAGIC_EMPTY_OBJECT) +#define JSON_VARIANT_MAGIC_EMPTY_OBJECT ((sd_json_variant*) _JSON_VARIANT_MAGIC_EMPTY_OBJECT) __JSON_VARIANT_MAGIC_MAX -#define _JSON_VARIANT_MAGIC_MAX ((JsonVariant*) __JSON_VARIANT_MAGIC_MAX) +#define _JSON_VARIANT_MAGIC_MAX ((sd_json_variant*) __JSON_VARIANT_MAGIC_MAX) }; /* This is only safe as long as we don't define more than 4K magic pointers, i.e. the page size of the simplest diff --git a/src/libsystemd/sd-json/json-util.c b/src/libsystemd/sd-json/json-util.c new file mode 100644 index 0000000000..7c14c03a99 --- /dev/null +++ b/src/libsystemd/sd-json/json-util.c @@ -0,0 +1,135 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "alloc-util.h" +#include "glyph-util.h" +#include "in-addr-util.h" +#include "iovec-util.h" +#include "json-util.h" +#include "path-util.h" +#include "string-util.h" +#include "user-util.h" + +int json_dispatch_unbase64_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + _cleanup_free_ void *buffer = NULL; + struct iovec *iov = ASSERT_PTR(userdata); + size_t sz; + int r; + + if (!sd_json_variant_is_string(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); + + r = sd_json_variant_unbase64(variant, &buffer, &sz); + if (r < 0) + return json_log(variant, flags, r, "JSON field '%s' is not valid Base64 data.", strna(name)); + + free_and_replace(iov->iov_base, buffer); + iov->iov_len = sz; + return 0; +} + +int json_dispatch_byte_array_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + _cleanup_free_ uint8_t *buffer = NULL; + struct iovec *iov = ASSERT_PTR(userdata); + size_t sz, k = 0; + + assert(variant); + + if (!sd_json_variant_is_array(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); + + sz = sd_json_variant_elements(variant); + + buffer = new(uint8_t, sz + 1); + if (!buffer) + return json_log(variant, flags, SYNTHETIC_ERRNO(ENOMEM), "Out of memory."); + + sd_json_variant *i; + JSON_VARIANT_ARRAY_FOREACH(i, variant) { + uint64_t b; + + if (!sd_json_variant_is_unsigned(i)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an unsigned integer.", k, strna(name)); + + b = sd_json_variant_unsigned(i); + if (b > 0xff) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), + "Element %zu of JSON field '%s' is out of range 0%s255.", + k, strna(name), special_glyph(SPECIAL_GLYPH_ELLIPSIS)); + + buffer[k++] = (uint8_t) b; + } + assert(k == sz); + + /* Append a NUL byte for safety, like we do in memdup_suffix0() and others. */ + buffer[sz] = 0; + + free_and_replace(iov->iov_base, buffer); + iov->iov_len = sz; + return 0; +} + +int json_dispatch_user_group_name(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + char **s = userdata; + const char *n; + int r; + + if (sd_json_variant_is_null(variant)) { + *s = mfree(*s); + return 0; + } + + if (!sd_json_variant_is_string(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); + + n = sd_json_variant_string(variant); + if (!valid_user_group_name(n, FLAGS_SET(flags, SD_JSON_RELAX) ? VALID_USER_RELAX : 0)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid user/group name.", strna(name)); + + r = free_and_strdup(s, n); + if (r < 0) + return json_log(variant, flags, r, "Failed to allocate string: %m"); + + return 0; +} + +int json_dispatch_in_addr(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + struct in_addr *address = ASSERT_PTR(userdata); + _cleanup_(iovec_done) struct iovec iov = {}; + int r; + + r = json_dispatch_byte_array_iovec(name, variant, flags, &iov); + if (r < 0) + return r; + + if (iov.iov_len != sizeof(struct in_addr)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name)); + + memcpy(address, iov.iov_base, iov.iov_len); + return 0; +} + +int json_dispatch_path(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + char **p = ASSERT_PTR(userdata); + const char *path; + + assert(variant); + + if (sd_json_variant_is_null(variant)) { + *p = mfree(*p); + return 0; + } + + if (!sd_json_variant_is_string(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); + + path = sd_json_variant_string(variant); + if (!((flags & SD_JSON_SAFE) ? path_is_normalized(path) : path_is_valid(path))) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a normalized file system path.", strna(name)); + if (!path_is_absolute(path)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an absolute file system path.", strna(name)); + + if (free_and_strdup(p, path) < 0) + return json_log_oom(variant, flags); + + return 0; +} diff --git a/src/libsystemd/sd-json/json-util.h b/src/libsystemd/sd-json/json-util.h new file mode 100644 index 0000000000..0b48158a4c --- /dev/null +++ b/src/libsystemd/sd-json/json-util.h @@ -0,0 +1,178 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include <syslog.h> + +#include "sd-json.h" + +#include "macro.h" + +#define JSON_VARIANT_REPLACE(v, q) \ + do { \ + typeof(v)* _v = &(v); \ + typeof(q) _q = (q); \ + sd_json_variant_unref(*_v); \ + *_v = _q; \ + } while(false) + +static inline int json_variant_set_field_non_null(sd_json_variant **v, const char *field, sd_json_variant *value) { + return value && !sd_json_variant_is_null(value) ? sd_json_variant_set_field(v, field, value) : 0; +} + +struct json_variant_foreach_state { + sd_json_variant *variant; + size_t idx; +}; + +#define _JSON_VARIANT_ARRAY_FOREACH(i, v, state) \ + for (struct json_variant_foreach_state state = { (v), 0 }; \ + sd_json_variant_is_array(state.variant) && \ + state.idx < sd_json_variant_elements(state.variant) && \ + ({ i = sd_json_variant_by_index(state.variant, state.idx); \ + true; }); \ + state.idx++) +#define JSON_VARIANT_ARRAY_FOREACH(i, v) \ + _JSON_VARIANT_ARRAY_FOREACH(i, v, UNIQ_T(state, UNIQ)) + +#define _JSON_VARIANT_OBJECT_FOREACH(k, e, v, state) \ + for (struct json_variant_foreach_state state = { (v), 0 }; \ + sd_json_variant_is_object(state.variant) && \ + state.idx < sd_json_variant_elements(state.variant) && \ + ({ k = sd_json_variant_string(sd_json_variant_by_index(state.variant, state.idx)); \ + e = sd_json_variant_by_index(state.variant, state.idx + 1); \ + true; }); \ + state.idx += 2) +#define JSON_VARIANT_OBJECT_FOREACH(k, e, v) \ + _JSON_VARIANT_OBJECT_FOREACH(k, e, v, UNIQ_T(state, UNIQ)) + +#define JSON_DISPATCH_ENUM_DEFINE(name, type, func) \ + int name(const char *n, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { \ + type *c = ASSERT_PTR(userdata); \ + \ + assert(variant); \ + \ + if (sd_json_variant_is_null(variant)) { \ + *c = (type) -EINVAL; \ + return 0; \ + } \ + \ + if (!sd_json_variant_is_string(variant)) \ + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(n)); \ + \ + type cc = func(sd_json_variant_string(variant)); \ + if (cc < 0) \ + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Value of JSON field '%s' not recognized.", strna(n)); \ + \ + *c = cc; \ + return 0; \ + } + +static inline int json_dispatch_level(sd_json_dispatch_flags_t flags) { + + /* Did the user request no logging? If so, then never log higher than LOG_DEBUG. Also, if this is marked as + * debug message, then also log at debug level. */ + + if (!(flags & SD_JSON_LOG) || + (flags & SD_JSON_DEBUG)) + return LOG_DEBUG; + + /* Are we invoked in permissive mode, or is this explicitly marked as warning message? Then this should be + * printed at LOG_WARNING */ + if (flags & (SD_JSON_PERMISSIVE|SD_JSON_WARNING)) + return LOG_WARNING; + + /* Otherwise it's an error. */ + return LOG_ERR; +} + +int json_log_internal(sd_json_variant *variant, int level, int error, const char *file, int line, const char *func, const char *format, ...) _printf_(7, 8); + +#define json_log(variant, flags, error, ...) \ + ({ \ + int _level = json_dispatch_level(flags), _e = (error); \ + (log_get_max_level() >= LOG_PRI(_level)) \ + ? json_log_internal(variant, _level, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \ + : -ERRNO_VALUE(_e); \ + }) + +#define json_log_oom(variant, flags) \ + json_log(variant, flags, SYNTHETIC_ERRNO(ENOMEM), "Out of memory.") + +int json_dispatch_unbase64_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int json_dispatch_byte_array_iovec(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int json_dispatch_user_group_name(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int json_dispatch_in_addr(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int json_dispatch_path(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); + +static inline int json_variant_unbase64_iovec(sd_json_variant *v, struct iovec *ret) { + return sd_json_variant_unbase64(v, ret ? &ret->iov_base : NULL, ret ? &ret->iov_len : NULL); +} + +static inline int json_variant_unhex_iovec(sd_json_variant *v, struct iovec *ret) { + return sd_json_variant_unhex(v, ret ? &ret->iov_base : NULL, ret ? &ret->iov_len : NULL); +} + +#define JSON_VARIANT_STRING_CONST(x) _JSON_VARIANT_STRING_CONST(UNIQ, (x)) + +#define _JSON_VARIANT_STRING_CONST(xq, x) \ + ({ \ + _align_(2) static const char UNIQ_T(json_string_const, xq)[] = (x); \ + assert((((uintptr_t) UNIQ_T(json_string_const, xq)) & 1) == 0); \ + (sd_json_variant*) ((uintptr_t) UNIQ_T(json_string_const, xq) + 1); \ + }) + +enum { + /* This extends the _SD_JSON_BUILD_* enums we define in the public API with some additional values + * that we want to keep private for now. (Mostly because the underlying structures are not public, or + * because we aren't sure yet they are useful for others). */ + _JSON_BUILD_STRV_ENV_PAIR = _SD_JSON_BUILD_MAX, + _JSON_BUILD_IOVEC_BASE64, + _JSON_BUILD_IOVEC_HEX, + _JSON_BUILD_HW_ADDR, + _JSON_BUILD_STRING_SET, + + _JSON_BUILD_PAIR_UNSIGNED_NON_ZERO, + _JSON_BUILD_PAIR_FINITE_USEC, + _JSON_BUILD_PAIR_STRING_NON_EMPTY, + _JSON_BUILD_PAIR_STRV_NON_EMPTY, + _JSON_BUILD_PAIR_VARIANT_NON_NULL, + /* _SD_JSON_BUILD_PAIR_VARIANT_ARRAY_NON_EMPTY, */ + _JSON_BUILD_PAIR_IN4_ADDR_NON_NULL, + _JSON_BUILD_PAIR_IN6_ADDR_NON_NULL, + _JSON_BUILD_PAIR_IN_ADDR_NON_NULL, + _JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL, + _JSON_BUILD_PAIR_HW_ADDR_NON_NULL, + + _SD_JSON_BUILD_REALLYMAX, +}; + +#define JSON_BUILD_STRV_ENV_PAIR(l) _JSON_BUILD_STRV_ENV_PAIR, (char**) { l } +#define JSON_BUILD_IOVEC_BASE64(iov) _JSON_BUILD_IOVEC_BASE64, (const struct iovec*) { iov } +#define JSON_BUILD_IOVEC_HEX(iov) _JSON_BUILD_IOVEC_HEX, (const struct iovec*) { iov } +#define JSON_BUILD_CONST_STRING(s) _SD_JSON_BUILD_VARIANT, JSON_VARIANT_STRING_CONST(s) +#define JSON_BUILD_IN4_ADDR(v) SD_JSON_BUILD_BYTE_ARRAY((const struct in_addr*) { v }, sizeof(struct in_addr)) +#define JSON_BUILD_IN6_ADDR(v) SD_JSON_BUILD_BYTE_ARRAY((const struct in6_addr*) { v }, sizeof(struct in6_addr)) +#define JSON_BUILD_IN_ADDR(v, f) SD_JSON_BUILD_BYTE_ARRAY(((const union in_addr_union*) { v })->bytes, FAMILY_ADDRESS_SIZE_SAFE(f)) +#define JSON_BUILD_ETHER_ADDR(v) SD_JSON_BUILD_BYTE_ARRAY(((const struct ether_addr*) { v })->ether_addr_octet, sizeof(struct ether_addr)) +#define JSON_BUILD_HW_ADDR(v) _JSON_BUILD_HW_ADDR, (const struct hw_addr_data*) { v } +#define JSON_BUILD_STRING_SET(s) _JSON_BUILD_STRING_SET, (Set *) { s } + +#define JSON_BUILD_PAIR_UNSIGNED_NON_ZERO(name, u) _JSON_BUILD_PAIR_UNSIGNED_NON_ZERO, (const char*) { name }, (uint64_t) { u } +#define JSON_BUILD_PAIR_FINITE_USEC(name, u) _JSON_BUILD_PAIR_FINITE_USEC, (const char*) { name }, (usec_t) { u } +#define JSON_BUILD_PAIR_STRING_NON_EMPTY(name, s) _JSON_BUILD_PAIR_STRING_NON_EMPTY, (const char*) { name }, (const char*) { s } +#define JSON_BUILD_PAIR_STRV_NON_EMPTY(name, l) _JSON_BUILD_PAIR_STRV_NON_EMPTY, (const char*) { name }, (char**) { l } +#define JSON_BUILD_PAIR_VARIANT_NON_NULL(name, v) _JSON_BUILD_PAIR_VARIANT_NON_NULL, (const char*) { name }, (sd_json_variant*) { v } +#define JSON_BUILD_PAIR_IN4_ADDR_NON_NULL(name, v) _JSON_BUILD_PAIR_IN4_ADDR_NON_NULL, (const char*) { name }, (const struct in_addr*) { v } +#define JSON_BUILD_PAIR_IN6_ADDR_NON_NULL(name, v) _JSON_BUILD_PAIR_IN6_ADDR_NON_NULL, (const char*) { name }, (const struct in6_addr*) { v } +#define JSON_BUILD_PAIR_IN_ADDR_NON_NULL(name, v, f) _JSON_BUILD_PAIR_IN_ADDR_NON_NULL, (const char*) { name }, (const union in_addr_union*) { v }, (int) { f } +#define JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL(name, v) _JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL, (const char*) { name }, (const struct ether_addr*) { v } +#define JSON_BUILD_PAIR_HW_ADDR_NON_NULL(name, v) _JSON_BUILD_PAIR_HW_ADDR_NON_NULL, (const char*) { name }, (const struct hw_addr_data*) { v } + +#define JSON_BUILD_PAIR_IOVEC_BASE64(name, iov) SD_JSON_BUILD_PAIR(name, JSON_BUILD_IOVEC_BASE64(iov)) +#define JSON_BUILD_PAIR_IOVEC_HEX(name, iov) SD_JSON_BUILD_PAIR(name, JSON_BUILD_IOVEC_HEX(iov)) +#define JSON_BUILD_PAIR_IN4_ADDR(name, v) SD_JSON_BUILD_PAIR(name, JSON_BUILD_IN4_ADDR(v)) +#define JSON_BUILD_PAIR_IN6_ADDR(name, v) SD_JSON_BUILD_PAIR(name, JSON_BUILD_IN6_ADDR(v)) +#define JSON_BUILD_PAIR_IN_ADDR(name, v, f) SD_JSON_BUILD_PAIR(name, JSON_BUILD_IN_ADDR(v, f)) +#define JSON_BUILD_PAIR_ETHER_ADDR(name, v) SD_JSON_BUILD_PAIR(name, JSON_BUILD_ETHER_ADDR(v)) +#define JSON_BUILD_PAIR_HW_ADDR(name, v) SD_JSON_BUILD_PAIR(name, JSON_BUILD_HW_ADDR(v)) +#define JSON_BUILD_PAIR_STRING_SET(name, s) SD_JSON_BUILD_PAIR(name, JSON_BUILD_STRING_SET(s)) diff --git a/src/shared/json.c b/src/libsystemd/sd-json/sd-json.c index 46f6564eed..5414b2257f 100644 --- a/src/shared/json.c +++ b/src/libsystemd/sd-json/sd-json.c @@ -6,19 +6,22 @@ #include <stdlib.h> #include <sys/types.h> +#include "sd-json.h" #include "sd-messages.h" #include "alloc-util.h" #include "errno-util.h" #include "escape.h" +#include "ether-addr-util.h" #include "fd-util.h" #include "fileio.h" #include "float.h" #include "glyph-util.h" #include "hexdecoct.h" +#include "in-addr-util.h" #include "iovec-util.h" #include "json-internal.h" -#include "json.h" +#include "json-util.h" #include "macro.h" #include "math-util.h" #include "memory-util.h" @@ -54,26 +57,26 @@ typedef struct JsonSource { } JsonSource; /* On x86-64 this whole structure should have a size of 6 * 64 bit = 48 bytes */ -struct JsonVariant { +struct sd_json_variant { union { /* We either maintain a reference counter for this variant itself, or we are embedded into an * array/object, in which case only that surrounding object is ref-counted. (If 'embedded' is false, * see below.) */ unsigned n_ref; - /* If this JsonVariant is part of an array/object, then this field points to the surrounding + /* If this sd_json_variant is part of an array/object, then this field points to the surrounding * JSON_VARIANT_ARRAY/JSON_VARIANT_OBJECT object. (If 'embedded' is true, see below.) */ - JsonVariant *parent; + sd_json_variant *parent; }; /* If this was parsed from some file or buffer, this stores where from, as well as the source line/column */ JsonSource *source; unsigned line, column; - /* The current 'depth' of the JsonVariant, i.e. how many levels of member variants this has */ + /* The current 'depth' of the sd_json_variant, i.e. how many levels of member variants this has */ uint16_t depth; - JsonVariantType type:8; + int type:8; /* actually sd_json_variant_type_t, but reduced to 8bit size */ /* A marker whether this variant is embedded into in array/object or not. If true, the 'parent' pointer above * is valid. If false, the 'n_ref' field above is valid instead. */ @@ -107,9 +110,9 @@ struct JsonVariant { size_t n_elements; /* If is_reference as indicated above is set, this is where the reference object is actually stored. */ - JsonVariant *reference; + sd_json_variant *reference; - /* Strings are placed immediately after the structure. Note that when this is a JsonVariant + /* Strings are placed immediately after the structure. Note that when this is a sd_json_variant * embedded into an array we might encode strings up to INLINE_STRING_LENGTH characters * directly inside the element, while longer strings are stored as references. When this * object is not embedded into an array, but stand-alone, we allocate the right size for the @@ -118,15 +121,15 @@ struct JsonVariant { }; }; -/* Inside string arrays we have a series of JsonVariant structures one after the other. In this case, strings longer +/* Inside string arrays we have a series of sd_json_variant structures one after the other. In this case, strings longer * than INLINE_STRING_MAX are stored as references, and all shorter ones inline. (This means — on x86-64 — strings up * to 7 chars are stored within the array elements, and all others in separate allocations) */ -#define INLINE_STRING_MAX (sizeof(JsonVariant) - offsetof(JsonVariant, string) - 1U) +#define INLINE_STRING_MAX (sizeof(sd_json_variant) - offsetof(sd_json_variant, string) - 1U) /* Let's make sure this structure isn't increased in size accidentally. This check is only for our most relevant arch * (x86-64). */ #if defined(__x86_64__) && __SIZEOF_POINTER__ == 8 -assert_cc(sizeof(JsonVariant) == 40U); +assert_cc(sizeof(sd_json_variant) == 40U); assert_cc(INLINE_STRING_MAX == 7U); #endif @@ -161,7 +164,7 @@ static bool json_source_equal(JsonSource *a, JsonSource *b) { DEFINE_TRIVIAL_CLEANUP_FUNC(JsonSource*, json_source_unref); -/* There are four kind of JsonVariant* pointers: +/* There are four kind of sd_json_variant* pointers: * * 1. NULL * 2. A 'regular' one, i.e. pointing to malloc() memory @@ -176,27 +179,27 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(JsonSource*, json_source_unref); * otherwise #4. This makes use of the fact that malloc() will return "maximum aligned" memory, which definitely * means the pointer is even. This means we can use the uneven pointers to reference static strings, as long as we * make sure that all static strings used like this are aligned to 2 (or higher), and that we mask the bit on - * access. The JSON_VARIANT_STRING_CONST() macro encodes strings as JsonVariant* pointers, with the bit set. */ + * access. The JSON_VARIANT_STRING_CONST() macro encodes strings as sd_json_variant* pointers, with the bit set. */ -static bool json_variant_is_magic(const JsonVariant *v) { +static bool json_variant_is_magic(const sd_json_variant *v) { if (!v) return false; return v < _JSON_VARIANT_MAGIC_MAX; } -static bool json_variant_is_const_string(const JsonVariant *v) { +static bool json_variant_is_const_string(const sd_json_variant *v) { if (v < _JSON_VARIANT_MAGIC_MAX) return false; - /* A proper JsonVariant is aligned to whatever malloc() aligns things too, which is definitely not uneven. We + /* A proper sd_json_variant is aligned to whatever malloc() aligns things too, which is definitely not uneven. We * hence use all uneven pointers as indicators for const strings. */ return (((uintptr_t) v) & 1) != 0; } -static bool json_variant_is_regular(const JsonVariant *v) { +static bool json_variant_is_regular(const sd_json_variant *v) { if (v < _JSON_VARIANT_MAGIC_MAX) return false; @@ -204,7 +207,7 @@ static bool json_variant_is_regular(const JsonVariant *v) { return (((uintptr_t) v) & 1) == 0; } -static JsonVariant *json_variant_dereference(JsonVariant *v) { +static sd_json_variant *json_variant_dereference(sd_json_variant *v) { /* Recursively dereference variants that are references to other variants */ @@ -220,7 +223,7 @@ static JsonVariant *json_variant_dereference(JsonVariant *v) { return json_variant_dereference(v->reference); } -static uint16_t json_variant_depth(JsonVariant *v) { +static uint16_t json_variant_depth(sd_json_variant *v) { v = json_variant_dereference(v); if (!v) @@ -232,7 +235,7 @@ static uint16_t json_variant_depth(JsonVariant *v) { return v->depth; } -static JsonVariant *json_variant_formalize(JsonVariant *v) { +static sd_json_variant *json_variant_formalize(sd_json_variant *v) { /* Converts json variant pointers to their normalized form, i.e. fully dereferenced and wherever * possible converted to the "magic" version if there is one */ @@ -242,38 +245,38 @@ static JsonVariant *json_variant_formalize(JsonVariant *v) { v = json_variant_dereference(v); - switch (json_variant_type(v)) { + switch (sd_json_variant_type(v)) { - case JSON_VARIANT_BOOLEAN: - return json_variant_boolean(v) ? JSON_VARIANT_MAGIC_TRUE : JSON_VARIANT_MAGIC_FALSE; + case SD_JSON_VARIANT_BOOLEAN: + return sd_json_variant_boolean(v) ? JSON_VARIANT_MAGIC_TRUE : JSON_VARIANT_MAGIC_FALSE; - case JSON_VARIANT_NULL: + case SD_JSON_VARIANT_NULL: return JSON_VARIANT_MAGIC_NULL; - case JSON_VARIANT_INTEGER: - return json_variant_integer(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_INTEGER : v; + case SD_JSON_VARIANT_INTEGER: + return sd_json_variant_integer(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_INTEGER : v; - case JSON_VARIANT_UNSIGNED: - return json_variant_unsigned(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_UNSIGNED : v; + case SD_JSON_VARIANT_UNSIGNED: + return sd_json_variant_unsigned(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_UNSIGNED : v; - case JSON_VARIANT_REAL: - return iszero_safe(json_variant_real(v)) ? JSON_VARIANT_MAGIC_ZERO_REAL : v; + case SD_JSON_VARIANT_REAL: + return iszero_safe(sd_json_variant_real(v)) ? JSON_VARIANT_MAGIC_ZERO_REAL : v; - case JSON_VARIANT_STRING: - return isempty(json_variant_string(v)) ? JSON_VARIANT_MAGIC_EMPTY_STRING : v; + case SD_JSON_VARIANT_STRING: + return isempty(sd_json_variant_string(v)) ? JSON_VARIANT_MAGIC_EMPTY_STRING : v; - case JSON_VARIANT_ARRAY: - return json_variant_elements(v) == 0 ? JSON_VARIANT_MAGIC_EMPTY_ARRAY : v; + case SD_JSON_VARIANT_ARRAY: + return sd_json_variant_elements(v) == 0 ? JSON_VARIANT_MAGIC_EMPTY_ARRAY : v; - case JSON_VARIANT_OBJECT: - return json_variant_elements(v) == 0 ? JSON_VARIANT_MAGIC_EMPTY_OBJECT : v; + case SD_JSON_VARIANT_OBJECT: + return sd_json_variant_elements(v) == 0 ? JSON_VARIANT_MAGIC_EMPTY_OBJECT : v; default: return v; } } -static JsonVariant *json_variant_conservative_formalize(JsonVariant *v) { +static sd_json_variant *json_variant_conservative_formalize(sd_json_variant *v) { /* Much like json_variant_formalize(), but won't simplify if the variant has a source/line location * attached to it, in order not to lose context */ @@ -290,13 +293,13 @@ static JsonVariant *json_variant_conservative_formalize(JsonVariant *v) { return json_variant_formalize(v); } -static int json_variant_new(JsonVariant **ret, JsonVariantType type, size_t space) { - JsonVariant *v; +static int json_variant_new(sd_json_variant **ret, sd_json_variant_type_t type, size_t space) { + sd_json_variant *v; assert_return(ret, -EINVAL); - v = malloc0(MAX(sizeof(JsonVariant), - offsetof(JsonVariant, value) + space)); + v = malloc0(MAX(sizeof(sd_json_variant), + offsetof(sd_json_variant, value) + space)); if (!v) return -ENOMEM; @@ -307,8 +310,8 @@ static int json_variant_new(JsonVariant **ret, JsonVariantType type, size_t spac return 0; } -int json_variant_new_integer(JsonVariant **ret, int64_t i) { - JsonVariant *v; +_public_ int sd_json_variant_new_integer(sd_json_variant **ret, int64_t i) { + sd_json_variant *v; int r; assert_return(ret, -EINVAL); @@ -318,7 +321,7 @@ int json_variant_new_integer(JsonVariant **ret, int64_t i) { return 0; } - r = json_variant_new(&v, JSON_VARIANT_INTEGER, sizeof(i)); + r = json_variant_new(&v, SD_JSON_VARIANT_INTEGER, sizeof(i)); if (r < 0) return r; @@ -328,8 +331,8 @@ int json_variant_new_integer(JsonVariant **ret, int64_t i) { return 0; } -int json_variant_new_unsigned(JsonVariant **ret, uint64_t u) { - JsonVariant *v; +_public_ int sd_json_variant_new_unsigned(sd_json_variant **ret, uint64_t u) { + sd_json_variant *v; int r; assert_return(ret, -EINVAL); @@ -338,7 +341,7 @@ int json_variant_new_unsigned(JsonVariant **ret, uint64_t u) { return 0; } - r = json_variant_new(&v, JSON_VARIANT_UNSIGNED, sizeof(u)); + r = json_variant_new(&v, SD_JSON_VARIANT_UNSIGNED, sizeof(u)); if (r < 0) return r; @@ -348,8 +351,8 @@ int json_variant_new_unsigned(JsonVariant **ret, uint64_t u) { return 0; } -int json_variant_new_real(JsonVariant **ret, double d) { - JsonVariant *v; +_public_ int sd_json_variant_new_real(sd_json_variant **ret, double d) { + sd_json_variant *v; int r; assert_return(ret, -EINVAL); @@ -367,7 +370,7 @@ int json_variant_new_real(JsonVariant **ret, double d) { return 0; } - r = json_variant_new(&v, JSON_VARIANT_REAL, sizeof(d)); + r = json_variant_new(&v, SD_JSON_VARIANT_REAL, sizeof(d)); if (r < 0) return r; @@ -377,7 +380,7 @@ int json_variant_new_real(JsonVariant **ret, double d) { return 0; } -int json_variant_new_boolean(JsonVariant **ret, bool b) { +_public_ int sd_json_variant_new_boolean(sd_json_variant **ret, int b) { assert_return(ret, -EINVAL); if (b) @@ -388,21 +391,21 @@ int json_variant_new_boolean(JsonVariant **ret, bool b) { return 0; } -int json_variant_new_null(JsonVariant **ret) { +_public_ int sd_json_variant_new_null(sd_json_variant **ret) { assert_return(ret, -EINVAL); *ret = JSON_VARIANT_MAGIC_NULL; return 0; } -int json_variant_new_stringn(JsonVariant **ret, const char *s, size_t n) { - JsonVariant *v; +_public_ int sd_json_variant_new_stringn(sd_json_variant **ret, const char *s, size_t n) { + sd_json_variant *v; int r; assert_return(ret, -EINVAL); if (!s) { assert_return(IN_SET(n, 0, SIZE_MAX), -EINVAL); - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); } if (n == SIZE_MAX) /* determine length automatically */ n = strlen(s); @@ -416,7 +419,7 @@ int json_variant_new_stringn(JsonVariant **ret, const char *s, size_t n) { if (!utf8_is_valid_n(s, n)) /* JSON strings must be valid UTF-8 */ return -EUCLEAN; - r = json_variant_new(&v, JSON_VARIANT_STRING, n + 1); + r = json_variant_new(&v, SD_JSON_VARIANT_STRING, n + 1); if (r < 0) return r; @@ -427,7 +430,11 @@ int json_variant_new_stringn(JsonVariant **ret, const char *s, size_t n) { return 0; } -int json_variant_new_base64(JsonVariant **ret, const void *p, size_t n) { +_public_ int sd_json_variant_new_string(sd_json_variant **ret, const char *s) { + return sd_json_variant_new_stringn(ret, s, SIZE_MAX); +} + +_public_ int sd_json_variant_new_base64(sd_json_variant **ret, const void *p, size_t n) { _cleanup_free_ char *s = NULL; ssize_t k; @@ -438,10 +445,10 @@ int json_variant_new_base64(JsonVariant **ret, const void *p, size_t n) { if (k < 0) return k; - return json_variant_new_stringn(ret, s, k); + return sd_json_variant_new_stringn(ret, s, k); } -int json_variant_new_base32hex(JsonVariant **ret, const void *p, size_t n) { +_public_ int sd_json_variant_new_base32hex(sd_json_variant **ret, const void *p, size_t n) { _cleanup_free_ char *s = NULL; assert_return(ret, -EINVAL); @@ -451,10 +458,10 @@ int json_variant_new_base32hex(JsonVariant **ret, const void *p, size_t n) { if (!s) return -ENOMEM; - return json_variant_new_string(ret, s); + return sd_json_variant_new_string(ret, s); } -int json_variant_new_hex(JsonVariant **ret, const void *p, size_t n) { +_public_ int sd_json_variant_new_hex(sd_json_variant **ret, const void *p, size_t n) { _cleanup_free_ char *s = NULL; assert_return(ret, -EINVAL); @@ -464,10 +471,10 @@ int json_variant_new_hex(JsonVariant **ret, const void *p, size_t n) { if (!s) return -ENOMEM; - return json_variant_new_stringn(ret, s, n*2); + return sd_json_variant_new_stringn(ret, s, n*2); } -int json_variant_new_octescape(JsonVariant **ret, const void *p, size_t n) { +_public_ int sd_json_variant_new_octescape(sd_json_variant **ret, const void *p, size_t n) { _cleanup_free_ char *s = NULL; assert_return(ret, -EINVAL); @@ -477,49 +484,49 @@ int json_variant_new_octescape(JsonVariant **ret, const void *p, size_t n) { if (!s) return -ENOMEM; - return json_variant_new_string(ret, s); + return sd_json_variant_new_string(ret, s); } -int json_variant_new_id128(JsonVariant **ret, sd_id128_t id) { - return json_variant_new_string(ret, SD_ID128_TO_STRING(id)); +_public_ int sd_json_variant_new_id128(sd_json_variant **ret, sd_id128_t id) { + return sd_json_variant_new_string(ret, SD_ID128_TO_STRING(id)); } -int json_variant_new_uuid(JsonVariant **ret, sd_id128_t id) { - return json_variant_new_string(ret, SD_ID128_TO_UUID_STRING(id)); +_public_ int sd_json_variant_new_uuid(sd_json_variant **ret, sd_id128_t id) { + return sd_json_variant_new_string(ret, SD_ID128_TO_UUID_STRING(id)); } -static void json_variant_set(JsonVariant *a, JsonVariant *b) { +static void json_variant_set(sd_json_variant *a, sd_json_variant *b) { assert(a); b = json_variant_dereference(b); if (!b) { - a->type = JSON_VARIANT_NULL; + a->type = SD_JSON_VARIANT_NULL; return; } - a->type = json_variant_type(b); + a->type = sd_json_variant_type(b); switch (a->type) { - case JSON_VARIANT_INTEGER: - a->value.integer = json_variant_integer(b); + case SD_JSON_VARIANT_INTEGER: + a->value.integer = sd_json_variant_integer(b); break; - case JSON_VARIANT_UNSIGNED: - a->value.unsig = json_variant_unsigned(b); + case SD_JSON_VARIANT_UNSIGNED: + a->value.unsig = sd_json_variant_unsigned(b); break; - case JSON_VARIANT_REAL: - a->value.real = json_variant_real(b); + case SD_JSON_VARIANT_REAL: + a->value.real = sd_json_variant_real(b); break; - case JSON_VARIANT_BOOLEAN: - a->value.boolean = json_variant_boolean(b); + case SD_JSON_VARIANT_BOOLEAN: + a->value.boolean = sd_json_variant_boolean(b); break; - case JSON_VARIANT_STRING: { + case SD_JSON_VARIANT_STRING: { const char *s; - assert_se(s = json_variant_string(b)); + assert_se(s = sd_json_variant_string(b)); /* Short strings we can store inline */ if (strnlen(s, INLINE_STRING_MAX+1) <= INLINE_STRING_MAX) { @@ -531,13 +538,13 @@ static void json_variant_set(JsonVariant *a, JsonVariant *b) { _fallthrough_; } - case JSON_VARIANT_ARRAY: - case JSON_VARIANT_OBJECT: + case SD_JSON_VARIANT_ARRAY: + case SD_JSON_VARIANT_OBJECT: a->is_reference = true; - a->reference = json_variant_ref(json_variant_conservative_formalize(b)); + a->reference = sd_json_variant_ref(json_variant_conservative_formalize(b)); break; - case JSON_VARIANT_NULL: + case SD_JSON_VARIANT_NULL: break; default: @@ -545,7 +552,7 @@ static void json_variant_set(JsonVariant *a, JsonVariant *b) { } } -static void json_variant_copy_source(JsonVariant *v, JsonVariant *from) { +static void json_variant_copy_source(sd_json_variant *v, sd_json_variant *from) { assert(v); if (!json_variant_is_regular(from)) @@ -556,9 +563,9 @@ static void json_variant_copy_source(JsonVariant *v, JsonVariant *from) { v->source = json_source_ref(from->source); } -static int _json_variant_array_put_element(JsonVariant *array, JsonVariant *element) { +static int json_variant_array_put_element(sd_json_variant *array, sd_json_variant *element) { assert(array); - JsonVariant *w = array + 1 + array->n_elements; + sd_json_variant *w = array + 1 + array->n_elements; uint16_t d = json_variant_depth(element); if (d >= DEPTH_MAX) /* Refuse too deep nesting */ @@ -567,7 +574,7 @@ static int _json_variant_array_put_element(JsonVariant *array, JsonVariant *elem array->depth = d + 1; array->n_elements++; - *w = (JsonVariant) { + *w = (sd_json_variant) { .is_embedded = true, .parent = array, }; @@ -575,14 +582,14 @@ static int _json_variant_array_put_element(JsonVariant *array, JsonVariant *elem json_variant_set(w, element); json_variant_copy_source(w, element); - if (!json_variant_is_normalized(element)) + if (!sd_json_variant_is_normalized(element)) array->normalized = false; return 0; } -int json_variant_new_array(JsonVariant **ret, JsonVariant **array, size_t n) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +_public_ int sd_json_variant_new_array(sd_json_variant **ret, sd_json_variant **array, size_t n) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert_return(ret, -EINVAL); @@ -592,18 +599,18 @@ int json_variant_new_array(JsonVariant **ret, JsonVariant **array, size_t n) { } assert_return(array, -EINVAL); - v = new(JsonVariant, n + 1); + v = new(sd_json_variant, n + 1); if (!v) return -ENOMEM; - *v = (JsonVariant) { + *v = (sd_json_variant) { .n_ref = 1, - .type = JSON_VARIANT_ARRAY, + .type = SD_JSON_VARIANT_ARRAY, .normalized = true, }; while (v->n_elements < n) { - r = _json_variant_array_put_element(v, array[v->n_elements]); + r = json_variant_array_put_element(v, array[v->n_elements]); if (r < 0) return r; } @@ -612,7 +619,7 @@ int json_variant_new_array(JsonVariant **ret, JsonVariant **array, size_t n) { return 0; } -int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n) { +_public_ int sd_json_variant_new_array_bytes(sd_json_variant **ret, const void *p, size_t n) { assert_return(ret, -EINVAL); if (n == 0) { *ret = JSON_VARIANT_MAGIC_EMPTY_ARRAY; @@ -620,24 +627,24 @@ int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n) { } assert_return(p, -EINVAL); - JsonVariant *v = new(JsonVariant, n + 1); + sd_json_variant *v = new(sd_json_variant, n + 1); if (!v) return -ENOMEM; - *v = (JsonVariant) { + *v = (sd_json_variant) { .n_ref = 1, - .type = JSON_VARIANT_ARRAY, + .type = SD_JSON_VARIANT_ARRAY, .n_elements = n, .depth = 1, }; for (size_t i = 0; i < n; i++) { - JsonVariant *w = v + 1 + i; + sd_json_variant *w = v + 1 + i; - *w = (JsonVariant) { + *w = (sd_json_variant) { .is_embedded = true, .parent = v, - .type = JSON_VARIANT_UNSIGNED, + .type = SD_JSON_VARIANT_UNSIGNED, .value.unsig = ((const uint8_t*) p)[i], }; } @@ -648,8 +655,8 @@ int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n) { return 0; } -int json_variant_new_array_strv(JsonVariant **ret, char **l) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +_public_ int sd_json_variant_new_array_strv(sd_json_variant **ret, char **l) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; size_t n; int r; @@ -661,24 +668,24 @@ int json_variant_new_array_strv(JsonVariant **ret, char **l) { return 0; } - v = new(JsonVariant, n + 1); + v = new(sd_json_variant, n + 1); if (!v) return -ENOMEM; - *v = (JsonVariant) { + *v = (sd_json_variant) { .n_ref = 1, - .type = JSON_VARIANT_ARRAY, + .type = SD_JSON_VARIANT_ARRAY, .depth = 1, }; for (v->n_elements = 0; v->n_elements < n; v->n_elements++) { - JsonVariant *w = v + 1 + v->n_elements; + sd_json_variant *w = v + 1 + v->n_elements; size_t k; - *w = (JsonVariant) { + *w = (sd_json_variant) { .is_embedded = true, .parent = v, - .type = JSON_VARIANT_STRING, + .type = SD_JSON_VARIANT_STRING, }; k = strlen(l[v->n_elements]); @@ -686,7 +693,7 @@ int json_variant_new_array_strv(JsonVariant **ret, char **l) { if (k > INLINE_STRING_MAX) { /* If string is too long, store it as reference. */ - r = json_variant_new_string(&w->reference, l[v->n_elements]); + r = sd_json_variant_new_string(&w->reference, l[v->n_elements]); if (r < 0) return r; @@ -705,8 +712,8 @@ int json_variant_new_array_strv(JsonVariant **ret, char **l) { return 0; } -int json_variant_new_object(JsonVariant **ret, JsonVariant **array, size_t n) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +_public_ int sd_json_variant_new_object(sd_json_variant **ret, sd_json_variant **array, size_t n) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *prev = NULL; bool sorted = true, normalized = true; @@ -718,33 +725,33 @@ int json_variant_new_object(JsonVariant **ret, JsonVariant **array, size_t n) { assert_return(array, -EINVAL); assert_return(n % 2 == 0, -EINVAL); - v = new(JsonVariant, n + 1); + v = new(sd_json_variant, n + 1); if (!v) return -ENOMEM; - *v = (JsonVariant) { + *v = (sd_json_variant) { .n_ref = 1, - .type = JSON_VARIANT_OBJECT, + .type = SD_JSON_VARIANT_OBJECT, }; for (v->n_elements = 0; v->n_elements < n; v->n_elements++) { - JsonVariant *w = v + 1 + v->n_elements, + sd_json_variant *w = v + 1 + v->n_elements, *c = array[v->n_elements]; uint16_t d; if ((v->n_elements & 1) == 0) { const char *k; - if (!json_variant_is_string(c)) + if (!sd_json_variant_is_string(c)) return -EINVAL; /* Every second one needs to be a string, as it is the key name */ - assert_se(k = json_variant_string(c)); + assert_se(k = sd_json_variant_string(c)); if (prev && strcmp(k, prev) <= 0) sorted = normalized = false; prev = k; - } else if (!json_variant_is_normalized(c)) + } else if (!sd_json_variant_is_normalized(c)) normalized = false; d = json_variant_depth(c); @@ -753,7 +760,7 @@ int json_variant_new_object(JsonVariant **ret, JsonVariant **array, size_t n) { if (d >= v->depth) v->depth = d + 1; - *w = (JsonVariant) { + *w = (sd_json_variant) { .is_embedded = true, .parent = v, }; @@ -769,43 +776,43 @@ int json_variant_new_object(JsonVariant **ret, JsonVariant **array, size_t n) { return 0; } -static size_t json_variant_size(JsonVariant* v) { +static size_t json_variant_size(sd_json_variant* v) { if (!json_variant_is_regular(v)) return 0; if (v->is_reference) - return offsetof(JsonVariant, reference) + sizeof(JsonVariant*); + return offsetof(sd_json_variant, reference) + sizeof(sd_json_variant*); switch (v->type) { - case JSON_VARIANT_STRING: - return offsetof(JsonVariant, string) + strlen(v->string) + 1; + case SD_JSON_VARIANT_STRING: + return offsetof(sd_json_variant, string) + strlen(v->string) + 1; - case JSON_VARIANT_REAL: - return offsetof(JsonVariant, value) + sizeof(double); + case SD_JSON_VARIANT_REAL: + return offsetof(sd_json_variant, value) + sizeof(double); - case JSON_VARIANT_UNSIGNED: - return offsetof(JsonVariant, value) + sizeof(uint64_t); + case SD_JSON_VARIANT_UNSIGNED: + return offsetof(sd_json_variant, value) + sizeof(uint64_t); - case JSON_VARIANT_INTEGER: - return offsetof(JsonVariant, value) + sizeof(int64_t); + case SD_JSON_VARIANT_INTEGER: + return offsetof(sd_json_variant, value) + sizeof(int64_t); - case JSON_VARIANT_BOOLEAN: - return offsetof(JsonVariant, value) + sizeof(bool); + case SD_JSON_VARIANT_BOOLEAN: + return offsetof(sd_json_variant, value) + sizeof(bool); - case JSON_VARIANT_ARRAY: - case JSON_VARIANT_OBJECT: - return offsetof(JsonVariant, n_elements) + sizeof(size_t); + case SD_JSON_VARIANT_ARRAY: + case SD_JSON_VARIANT_OBJECT: + return offsetof(sd_json_variant, n_elements) + sizeof(size_t); - case JSON_VARIANT_NULL: - return offsetof(JsonVariant, value); + case SD_JSON_VARIANT_NULL: + return offsetof(sd_json_variant, value); default: assert_not_reached(); } } -static void json_variant_free_inner(JsonVariant *v, bool force_sensitive) { +static void json_variant_free_inner(sd_json_variant *v, bool force_sensitive) { bool sensitive; assert(v); @@ -819,13 +826,13 @@ static void json_variant_free_inner(JsonVariant *v, bool force_sensitive) { if (v->is_reference) { if (sensitive) - json_variant_sensitive(v->reference); + sd_json_variant_sensitive(v->reference); - json_variant_unref(v->reference); + sd_json_variant_unref(v->reference); return; } - if (IN_SET(v->type, JSON_VARIANT_ARRAY, JSON_VARIANT_OBJECT)) + if (IN_SET(v->type, SD_JSON_VARIANT_ARRAY, SD_JSON_VARIANT_OBJECT)) for (size_t i = 0; i < v->n_elements; i++) json_variant_free_inner(v + 1 + i, sensitive); @@ -833,7 +840,7 @@ static void json_variant_free_inner(JsonVariant *v, bool force_sensitive) { explicit_bzero_safe(v, json_variant_size(v)); } -static unsigned json_variant_n_ref(const JsonVariant *v) { +static unsigned json_variant_n_ref(const sd_json_variant *v) { /* Return the number of references to v. * 0 => NULL or not a regular object or embedded. * >0 => number of references @@ -846,14 +853,14 @@ static unsigned json_variant_n_ref(const JsonVariant *v) { return v->n_ref; } -JsonVariant *json_variant_ref(JsonVariant *v) { +_public_ sd_json_variant *sd_json_variant_ref(sd_json_variant *v) { if (!v) return NULL; if (!json_variant_is_regular(v)) return v; if (v->is_embedded) - json_variant_ref(v->parent); /* ref the compounding variant instead */ + sd_json_variant_ref(v->parent); /* ref the compounding variant instead */ else { assert(v->n_ref > 0); v->n_ref++; @@ -862,14 +869,14 @@ JsonVariant *json_variant_ref(JsonVariant *v) { return v; } -JsonVariant *json_variant_unref(JsonVariant *v) { +_public_ sd_json_variant *sd_json_variant_unref(sd_json_variant *v) { if (!v) return NULL; if (!json_variant_is_regular(v)) return NULL; if (v->is_embedded) - json_variant_unref(v->parent); + sd_json_variant_unref(v->parent); else { assert(v->n_ref > 0); v->n_ref--; @@ -883,16 +890,16 @@ JsonVariant *json_variant_unref(JsonVariant *v) { return NULL; } -void json_variant_unref_many(JsonVariant **array, size_t n) { +_public_ void sd_json_variant_unref_many(sd_json_variant **array, size_t n) { assert(array || n == 0); for (size_t i = 0; i < n; i++) - json_variant_unref(array[i]); + sd_json_variant_unref(array[i]); free(array); } -const char *json_variant_string(JsonVariant *v) { +_public_ const char *sd_json_variant_string(sd_json_variant *v) { if (!v) return NULL; if (v == JSON_VARIANT_MAGIC_EMPTY_STRING) @@ -907,8 +914,8 @@ const char *json_variant_string(JsonVariant *v) { } if (v->is_reference) - return json_variant_string(v->reference); - if (v->type != JSON_VARIANT_STRING) + return sd_json_variant_string(v->reference); + if (v->type != SD_JSON_VARIANT_STRING) goto mismatch; return v->string; @@ -918,7 +925,7 @@ mismatch: return NULL; } -bool json_variant_boolean(JsonVariant *v) { +_public_ int sd_json_variant_boolean(sd_json_variant *v) { if (!v) goto mismatch; if (v == JSON_VARIANT_MAGIC_TRUE) @@ -927,10 +934,10 @@ bool json_variant_boolean(JsonVariant *v) { return false; if (!json_variant_is_regular(v)) goto mismatch; - if (v->type != JSON_VARIANT_BOOLEAN) + if (v->type != SD_JSON_VARIANT_BOOLEAN) goto mismatch; if (v->is_reference) - return json_variant_boolean(v->reference); + return sd_json_variant_boolean(v->reference); return v->value.boolean; @@ -939,7 +946,7 @@ mismatch: return false; } -int64_t json_variant_integer(JsonVariant *v) { +_public_ int64_t sd_json_variant_integer(sd_json_variant *v) { if (!v) goto mismatch; if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER || @@ -949,21 +956,21 @@ int64_t json_variant_integer(JsonVariant *v) { if (!json_variant_is_regular(v)) goto mismatch; if (v->is_reference) - return json_variant_integer(v->reference); + return sd_json_variant_integer(v->reference); switch (v->type) { - case JSON_VARIANT_INTEGER: + case SD_JSON_VARIANT_INTEGER: return v->value.integer; - case JSON_VARIANT_UNSIGNED: + case SD_JSON_VARIANT_UNSIGNED: if (v->value.unsig <= INT64_MAX) return (int64_t) v->value.unsig; log_debug("Unsigned integer %" PRIu64 " requested as signed integer and out of range, returning 0.", v->value.unsig); return 0; - case JSON_VARIANT_REAL: { + case SD_JSON_VARIANT_REAL: { int64_t converted; converted = (int64_t) v->value.real; @@ -984,7 +991,7 @@ mismatch: return 0; } -uint64_t json_variant_unsigned(JsonVariant *v) { +_public_ uint64_t sd_json_variant_unsigned(sd_json_variant *v) { if (!v) goto mismatch; if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER || @@ -994,21 +1001,21 @@ uint64_t json_variant_unsigned(JsonVariant *v) { if (!json_variant_is_regular(v)) goto mismatch; if (v->is_reference) - return json_variant_integer(v->reference); + return sd_json_variant_integer(v->reference); switch (v->type) { - case JSON_VARIANT_INTEGER: + case SD_JSON_VARIANT_INTEGER: if (v->value.integer >= 0) return (uint64_t) v->value.integer; log_debug("Signed integer %" PRIi64 " requested as unsigned integer and out of range, returning 0.", v->value.integer); return 0; - case JSON_VARIANT_UNSIGNED: + case SD_JSON_VARIANT_UNSIGNED: return v->value.unsig; - case JSON_VARIANT_REAL: { + case SD_JSON_VARIANT_REAL: { uint64_t converted; converted = (uint64_t) v->value.real; @@ -1029,7 +1036,7 @@ mismatch: return 0; } -double json_variant_real(JsonVariant *v) { +_public_ double sd_json_variant_real(sd_json_variant *v) { if (!v) return 0.0; if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER || @@ -1039,14 +1046,14 @@ double json_variant_real(JsonVariant *v) { if (!json_variant_is_regular(v)) goto mismatch; if (v->is_reference) - return json_variant_real(v->reference); + return sd_json_variant_real(v->reference); switch (v->type) { - case JSON_VARIANT_REAL: + case SD_JSON_VARIANT_REAL: return v->value.real; - case JSON_VARIANT_INTEGER: { + case SD_JSON_VARIANT_INTEGER: { double converted = (double) v->value.integer; if ((int64_t) converted == v->value.integer) @@ -1056,7 +1063,7 @@ double json_variant_real(JsonVariant *v) { return 0.0; } - case JSON_VARIANT_UNSIGNED: { + case SD_JSON_VARIANT_UNSIGNED: { double converted = (double) v->value.unsig; if ((uint64_t) converted == v->value.unsig) @@ -1075,7 +1082,7 @@ mismatch: return 0.0; } -bool json_variant_is_negative(JsonVariant *v) { +_public_ int sd_json_variant_is_negative(sd_json_variant *v) { if (!v) goto mismatch; if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER || @@ -1085,7 +1092,7 @@ bool json_variant_is_negative(JsonVariant *v) { if (!json_variant_is_regular(v)) goto mismatch; if (v->is_reference) - return json_variant_is_negative(v->reference); + return sd_json_variant_is_negative(v->reference); /* This function is useful as checking whether numbers are negative is pretty complex since we have three types * of numbers. And some JSON code (OCI for example) uses negative numbers to mark "not defined" numeric @@ -1093,13 +1100,13 @@ bool json_variant_is_negative(JsonVariant *v) { switch (v->type) { - case JSON_VARIANT_REAL: + case SD_JSON_VARIANT_REAL: return v->value.real < 0; - case JSON_VARIANT_INTEGER: + case SD_JSON_VARIANT_INTEGER: return v->value.integer < 0; - case JSON_VARIANT_UNSIGNED: + case SD_JSON_VARIANT_UNSIGNED: return false; default: @@ -1111,57 +1118,57 @@ mismatch: return false; } -bool json_variant_is_blank_object(JsonVariant *v) { +_public_ int sd_json_variant_is_blank_object(sd_json_variant *v) { /* Returns true if the specified object is null or empty */ return !v || - json_variant_is_null(v) || - (json_variant_is_object(v) && json_variant_elements(v) == 0); + sd_json_variant_is_null(v) || + (sd_json_variant_is_object(v) && sd_json_variant_elements(v) == 0); } -bool json_variant_is_blank_array(JsonVariant *v) { +_public_ int sd_json_variant_is_blank_array(sd_json_variant *v) { return !v || - json_variant_is_null(v) || - (json_variant_is_array(v) && json_variant_elements(v) == 0); + sd_json_variant_is_null(v) || + (sd_json_variant_is_array(v) && sd_json_variant_elements(v) == 0); } -JsonVariantType json_variant_type(JsonVariant *v) { +_public_ sd_json_variant_type_t sd_json_variant_type(sd_json_variant *v) { if (!v) - return _JSON_VARIANT_TYPE_INVALID; + return _SD_JSON_VARIANT_TYPE_INVALID; if (json_variant_is_const_string(v)) - return JSON_VARIANT_STRING; + return SD_JSON_VARIANT_STRING; if (v == JSON_VARIANT_MAGIC_TRUE || v == JSON_VARIANT_MAGIC_FALSE) - return JSON_VARIANT_BOOLEAN; + return SD_JSON_VARIANT_BOOLEAN; if (v == JSON_VARIANT_MAGIC_NULL) - return JSON_VARIANT_NULL; + return SD_JSON_VARIANT_NULL; if (v == JSON_VARIANT_MAGIC_ZERO_INTEGER) - return JSON_VARIANT_INTEGER; + return SD_JSON_VARIANT_INTEGER; if (v == JSON_VARIANT_MAGIC_ZERO_UNSIGNED) - return JSON_VARIANT_UNSIGNED; + return SD_JSON_VARIANT_UNSIGNED; if (v == JSON_VARIANT_MAGIC_ZERO_REAL) - return JSON_VARIANT_REAL; + return SD_JSON_VARIANT_REAL; if (v == JSON_VARIANT_MAGIC_EMPTY_STRING) - return JSON_VARIANT_STRING; + return SD_JSON_VARIANT_STRING; if (v == JSON_VARIANT_MAGIC_EMPTY_ARRAY) - return JSON_VARIANT_ARRAY; + return SD_JSON_VARIANT_ARRAY; if (v == JSON_VARIANT_MAGIC_EMPTY_OBJECT) - return JSON_VARIANT_OBJECT; + return SD_JSON_VARIANT_OBJECT; return v->type; } _function_no_sanitize_float_cast_overflow_ -bool json_variant_has_type(JsonVariant *v, JsonVariantType type) { - JsonVariantType rt; +_public_ int sd_json_variant_has_type(sd_json_variant *v, sd_json_variant_type_t type) { + sd_json_variant_type_t rt; /* Note: we turn off ubsan float cast overflow detection for this function, since it would complain * about our float casts but we do them explicitly to detect conversion errors. */ @@ -1170,7 +1177,7 @@ bool json_variant_has_type(JsonVariant *v, JsonVariantType type) { if (!v) return false; - rt = json_variant_type(v); + rt = sd_json_variant_type(v); if (rt == type) return true; @@ -1180,7 +1187,7 @@ bool json_variant_has_type(JsonVariant *v, JsonVariantType type) { /* All three magic zeroes qualify as integer, unsigned and as real */ if ((v == JSON_VARIANT_MAGIC_ZERO_INTEGER || v == JSON_VARIANT_MAGIC_ZERO_UNSIGNED || v == JSON_VARIANT_MAGIC_ZERO_REAL) && - IN_SET(type, JSON_VARIANT_INTEGER, JSON_VARIANT_UNSIGNED, JSON_VARIANT_REAL, JSON_VARIANT_NUMBER)) + IN_SET(type, SD_JSON_VARIANT_INTEGER, SD_JSON_VARIANT_UNSIGNED, SD_JSON_VARIANT_REAL, SD_JSON_VARIANT_NUMBER)) return true; /* All other magic variant types are only equal to themselves */ @@ -1188,31 +1195,67 @@ bool json_variant_has_type(JsonVariant *v, JsonVariantType type) { return false; /* Handle the "number" pseudo type */ - if (type == JSON_VARIANT_NUMBER) - return IN_SET(rt, JSON_VARIANT_INTEGER, JSON_VARIANT_UNSIGNED, JSON_VARIANT_REAL); + if (type == SD_JSON_VARIANT_NUMBER) + return IN_SET(rt, SD_JSON_VARIANT_INTEGER, SD_JSON_VARIANT_UNSIGNED, SD_JSON_VARIANT_REAL); /* Integer conversions are OK in many cases */ - if (rt == JSON_VARIANT_INTEGER && type == JSON_VARIANT_UNSIGNED) + if (rt == SD_JSON_VARIANT_INTEGER && type == SD_JSON_VARIANT_UNSIGNED) return v->value.integer >= 0; - if (rt == JSON_VARIANT_UNSIGNED && type == JSON_VARIANT_INTEGER) + if (rt == SD_JSON_VARIANT_UNSIGNED && type == SD_JSON_VARIANT_INTEGER) return v->value.unsig <= INT64_MAX; /* Any integer that can be converted lossley to a real and back may also be considered a real */ - if (rt == JSON_VARIANT_INTEGER && type == JSON_VARIANT_REAL) + if (rt == SD_JSON_VARIANT_INTEGER && type == SD_JSON_VARIANT_REAL) return (int64_t) (double) v->value.integer == v->value.integer; - if (rt == JSON_VARIANT_UNSIGNED && type == JSON_VARIANT_REAL) + if (rt == SD_JSON_VARIANT_UNSIGNED && type == SD_JSON_VARIANT_REAL) return (uint64_t) (double) v->value.unsig == v->value.unsig; /* Any real that can be converted losslessly to an integer and back may also be considered an integer */ - if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_INTEGER) + if (rt == SD_JSON_VARIANT_REAL && type == SD_JSON_VARIANT_INTEGER) return fp_equal((double) (int64_t) v->value.real, v->value.real); - if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_UNSIGNED) + if (rt == SD_JSON_VARIANT_REAL && type == SD_JSON_VARIANT_UNSIGNED) return fp_equal((double) (uint64_t) v->value.real, v->value.real); return false; } -size_t json_variant_elements(JsonVariant *v) { +_public_ int sd_json_variant_is_string(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_STRING); +} + +_public_ int sd_json_variant_is_integer(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_INTEGER); +} + +_public_ int sd_json_variant_is_unsigned(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_UNSIGNED); +} + +_public_ int sd_json_variant_is_real(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_REAL); +} + +_public_ int sd_json_variant_is_number(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_NUMBER); +} + +_public_ int sd_json_variant_is_boolean(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_BOOLEAN); +} + +_public_ int sd_json_variant_is_array(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_ARRAY); +} + +_public_ int sd_json_variant_is_object(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_OBJECT); +} + +_public_ int sd_json_variant_is_null(sd_json_variant *v) { + return sd_json_variant_has_type(v, SD_JSON_VARIANT_NULL); +} + +_public_ size_t sd_json_variant_elements(sd_json_variant *v) { if (!v) return 0; if (v == JSON_VARIANT_MAGIC_EMPTY_ARRAY || @@ -1220,10 +1263,10 @@ size_t json_variant_elements(JsonVariant *v) { return 0; if (!json_variant_is_regular(v)) goto mismatch; - if (!IN_SET(v->type, JSON_VARIANT_ARRAY, JSON_VARIANT_OBJECT)) + if (!IN_SET(v->type, SD_JSON_VARIANT_ARRAY, SD_JSON_VARIANT_OBJECT)) goto mismatch; if (v->is_reference) - return json_variant_elements(v->reference); + return sd_json_variant_elements(v->reference); return v->n_elements; @@ -1232,7 +1275,7 @@ mismatch: return 0; } -JsonVariant *json_variant_by_index(JsonVariant *v, size_t idx) { +_public_ sd_json_variant *sd_json_variant_by_index(sd_json_variant *v, size_t idx) { if (!v) return NULL; if (v == JSON_VARIANT_MAGIC_EMPTY_ARRAY || @@ -1240,10 +1283,10 @@ JsonVariant *json_variant_by_index(JsonVariant *v, size_t idx) { return NULL; if (!json_variant_is_regular(v)) goto mismatch; - if (!IN_SET(v->type, JSON_VARIANT_ARRAY, JSON_VARIANT_OBJECT)) + if (!IN_SET(v->type, SD_JSON_VARIANT_ARRAY, SD_JSON_VARIANT_OBJECT)) goto mismatch; if (v->is_reference) - return json_variant_by_index(v->reference, idx); + return sd_json_variant_by_index(v->reference, idx); if (idx >= v->n_elements) return NULL; @@ -1254,7 +1297,7 @@ mismatch: return NULL; } -JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVariant **ret_key) { +_public_ sd_json_variant *sd_json_variant_by_key_full(sd_json_variant *v, const char *key, sd_json_variant **ret_key) { if (!v) goto not_found; if (!key) @@ -1263,10 +1306,10 @@ JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVaria goto not_found; if (!json_variant_is_regular(v)) goto mismatch; - if (v->type != JSON_VARIANT_OBJECT) + if (v->type != SD_JSON_VARIANT_OBJECT) goto mismatch; if (v->is_reference) - return json_variant_by_key(v->reference, key); + return sd_json_variant_by_key(v->reference, key); if (v->sorted) { size_t a = 0, b = v->n_elements/2; @@ -1274,7 +1317,7 @@ JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVaria /* If the variant is sorted we can use bisection to find the entry we need in O(log(n)) time */ while (b > a) { - JsonVariant *p; + sd_json_variant *p; const char *f; size_t i; int c; @@ -1282,7 +1325,7 @@ JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVaria i = (a + b) / 2; p = json_variant_dereference(v + 1 + i*2); - assert_se(f = json_variant_string(p)); + assert_se(f = sd_json_variant_string(p)); c = strcmp(key, f); if (c == 0) { @@ -1301,14 +1344,14 @@ JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVaria /* The variant is not sorted, hence search for the field linearly */ for (size_t i = 0; i < v->n_elements; i += 2) { - JsonVariant *p; + sd_json_variant *p; p = json_variant_dereference(v + 1 + i); - if (!json_variant_has_type(p, JSON_VARIANT_STRING)) + if (!sd_json_variant_has_type(p, SD_JSON_VARIANT_STRING)) continue; - if (streq(json_variant_string(p), key)) { + if (streq(sd_json_variant_string(p), key)) { if (ret_key) *ret_key = json_variant_conservative_formalize(v + 1 + i); @@ -1331,12 +1374,12 @@ mismatch: return NULL; } -JsonVariant *json_variant_by_key(JsonVariant *v, const char *key) { - return json_variant_by_key_full(v, key, NULL); +_public_ sd_json_variant *sd_json_variant_by_key(sd_json_variant *v, const char *key) { + return sd_json_variant_by_key_full(v, key, NULL); } -bool json_variant_equal(JsonVariant *a, JsonVariant *b) { - JsonVariantType t; +_public_ int sd_json_variant_equal(sd_json_variant *a, sd_json_variant *b) { + sd_json_variant_type_t t; a = json_variant_formalize(a); b = json_variant_formalize(b); @@ -1344,45 +1387,45 @@ bool json_variant_equal(JsonVariant *a, JsonVariant *b) { if (a == b) return true; - t = json_variant_type(a); - if (!json_variant_has_type(b, t)) + t = sd_json_variant_type(a); + if (!sd_json_variant_has_type(b, t)) return false; switch (t) { - case JSON_VARIANT_STRING: - return streq(json_variant_string(a), json_variant_string(b)); + case SD_JSON_VARIANT_STRING: + return streq(sd_json_variant_string(a), sd_json_variant_string(b)); - case JSON_VARIANT_INTEGER: - return json_variant_integer(a) == json_variant_integer(b); + case SD_JSON_VARIANT_INTEGER: + return sd_json_variant_integer(a) == sd_json_variant_integer(b); - case JSON_VARIANT_UNSIGNED: - return json_variant_unsigned(a) == json_variant_unsigned(b); + case SD_JSON_VARIANT_UNSIGNED: + return sd_json_variant_unsigned(a) == sd_json_variant_unsigned(b); - case JSON_VARIANT_REAL: - return fp_equal(json_variant_real(a), json_variant_real(b)); + case SD_JSON_VARIANT_REAL: + return fp_equal(sd_json_variant_real(a), sd_json_variant_real(b)); - case JSON_VARIANT_BOOLEAN: - return json_variant_boolean(a) == json_variant_boolean(b); + case SD_JSON_VARIANT_BOOLEAN: + return sd_json_variant_boolean(a) == sd_json_variant_boolean(b); - case JSON_VARIANT_NULL: + case SD_JSON_VARIANT_NULL: return true; - case JSON_VARIANT_ARRAY: { - size_t n = json_variant_elements(a); - if (n != json_variant_elements(b)) + case SD_JSON_VARIANT_ARRAY: { + size_t n = sd_json_variant_elements(a); + if (n != sd_json_variant_elements(b)) return false; for (size_t i = 0; i < n; i++) - if (!json_variant_equal(json_variant_by_index(a, i), json_variant_by_index(b, i))) + if (!sd_json_variant_equal(sd_json_variant_by_index(a, i), sd_json_variant_by_index(b, i))) return false; return true; } - case JSON_VARIANT_OBJECT: { - size_t n = json_variant_elements(a); - if (n != json_variant_elements(b)) + case SD_JSON_VARIANT_OBJECT: { + size_t n = sd_json_variant_elements(a); + if (n != sd_json_variant_elements(b)) return false; /* Iterate through all keys in 'a' */ @@ -1391,9 +1434,9 @@ bool json_variant_equal(JsonVariant *a, JsonVariant *b) { /* Match them against all keys in 'b' */ for (size_t j = 0; j < n; j += 2) { - JsonVariant *key_b; + sd_json_variant *key_b; - key_b = json_variant_by_index(b, j); + key_b = sd_json_variant_by_index(b, j); /* During the first iteration unmark everything */ if (i == 0) @@ -1404,8 +1447,8 @@ bool json_variant_equal(JsonVariant *a, JsonVariant *b) { if (found) continue; - if (json_variant_equal(json_variant_by_index(a, i), key_b) && - json_variant_equal(json_variant_by_index(a, i+1), json_variant_by_index(b, j+1))) { + if (sd_json_variant_equal(sd_json_variant_by_index(a, i), key_b) && + sd_json_variant_equal(sd_json_variant_by_index(a, i+1), sd_json_variant_by_index(b, j+1))) { /* Key and values match! */ key_b->is_marked = found = true; @@ -1429,7 +1472,7 @@ bool json_variant_equal(JsonVariant *a, JsonVariant *b) { } } -void json_variant_sensitive(JsonVariant *v) { +_public_ void sd_json_variant_sensitive(sd_json_variant *v) { assert(v); /* Marks a variant as "sensitive", so that it is erased from memory when it is destroyed. This is a @@ -1449,7 +1492,7 @@ void json_variant_sensitive(JsonVariant *v) { v->sensitive = true; } -bool json_variant_is_sensitive(JsonVariant *v) { +_public_ int sd_json_variant_is_sensitive(sd_json_variant *v) { v = json_variant_formalize(v); if (!json_variant_is_regular(v)) return false; @@ -1457,26 +1500,26 @@ bool json_variant_is_sensitive(JsonVariant *v) { return v->sensitive; } -bool json_variant_is_sensitive_recursive(JsonVariant *v) { +_public_ int sd_json_variant_is_sensitive_recursive(sd_json_variant *v) { if (!v) return false; - if (json_variant_is_sensitive(v)) + if (sd_json_variant_is_sensitive(v)) return true; if (!json_variant_is_regular(v)) return false; if (v->recursive_sensitive) /* Already checked this before */ return true; - if (!IN_SET(v->type, JSON_VARIANT_ARRAY, JSON_VARIANT_OBJECT)) + if (!IN_SET(v->type, SD_JSON_VARIANT_ARRAY, SD_JSON_VARIANT_OBJECT)) return false; if (v->is_reference) { - if (!json_variant_is_sensitive_recursive(v->reference)) + if (!sd_json_variant_is_sensitive_recursive(v->reference)) return false; return (v->recursive_sensitive = true); } - for (size_t i = 0; i < json_variant_elements(v); i++) - if (json_variant_is_sensitive_recursive(json_variant_by_index(v, i))) + for (size_t i = 0; i < sd_json_variant_elements(v); i++) + if (sd_json_variant_is_sensitive_recursive(sd_json_variant_by_index(v, i))) return (v->recursive_sensitive = true); /* Note: we only cache the result here in case true, since we allow all elements down the tree to @@ -1484,12 +1527,12 @@ bool json_variant_is_sensitive_recursive(JsonVariant *v) { return false; } -static void json_variant_propagate_sensitive(JsonVariant *from, JsonVariant *to) { - if (json_variant_is_sensitive(from)) - json_variant_sensitive(to); +static void json_variant_propagate_sensitive(sd_json_variant *from, sd_json_variant *to) { + if (sd_json_variant_is_sensitive(from)) + sd_json_variant_sensitive(to); } -int json_variant_get_source(JsonVariant *v, const char **ret_source, unsigned *ret_line, unsigned *ret_column) { +_public_ int sd_json_variant_get_source(sd_json_variant *v, const char **ret_source, unsigned *ret_line, unsigned *ret_column) { assert_return(v, -EINVAL); if (ret_source) @@ -1504,10 +1547,10 @@ int json_variant_get_source(JsonVariant *v, const char **ret_source, unsigned *r return 0; } -static int print_source(FILE *f, JsonVariant *v, JsonFormatFlags flags, bool whitespace) { +static int print_source(FILE *f, sd_json_variant *v, sd_json_format_flags_t flags, bool whitespace) { size_t w, k; - if (!FLAGS_SET(flags, JSON_FORMAT_SOURCE|JSON_FORMAT_PRETTY)) + if (!FLAGS_SET(flags, SD_JSON_FORMAT_SOURCE|SD_JSON_FORMAT_PRETTY)) return 0; if (!json_variant_is_regular(v)) @@ -1555,12 +1598,12 @@ static int print_source(FILE *f, JsonVariant *v, JsonFormatFlags flags, bool whi return 0; } -static void json_format_string(FILE *f, const char *q, JsonFormatFlags flags) { +static void json_format_string(FILE *f, const char *q, sd_json_format_flags_t flags) { assert(q); fputc('"', f); - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ansi_green(), f); for (; *q; q++) @@ -1601,108 +1644,108 @@ static void json_format_string(FILE *f, const char *q, JsonFormatFlags flags) { break; } - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_NORMAL, f); fputc('"', f); } -static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const char *prefix) { +static int json_format(FILE *f, sd_json_variant *v, sd_json_format_flags_t flags, const char *prefix) { int r; assert(f); assert(v); - if (FLAGS_SET(flags, JSON_FORMAT_CENSOR_SENSITIVE) && json_variant_is_sensitive(v)) { - if (flags & JSON_FORMAT_COLOR) + if (FLAGS_SET(flags, SD_JSON_FORMAT_CENSOR_SENSITIVE) && sd_json_variant_is_sensitive(v)) { + if (flags & SD_JSON_FORMAT_COLOR) fputs(ansi_red(), f); fputs("\"<sensitive data>\"", f); - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_NORMAL, f); return 0; } - switch (json_variant_type(v)) { + switch (sd_json_variant_type(v)) { - case JSON_VARIANT_REAL: { + case SD_JSON_VARIANT_REAL: { locale_t loc, old_loc; loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); if (loc == (locale_t) 0) return -errno; - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ansi_highlight_blue(), f); old_loc = uselocale(loc); - fprintf(f, "%.*e", DECIMAL_DIG, json_variant_real(v)); + fprintf(f, "%.*e", DECIMAL_DIG, sd_json_variant_real(v)); uselocale(old_loc); - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_NORMAL, f); freelocale(loc); break; } - case JSON_VARIANT_INTEGER: - if (flags & JSON_FORMAT_COLOR) + case SD_JSON_VARIANT_INTEGER: + if (flags & SD_JSON_FORMAT_COLOR) fputs(ansi_highlight_blue(), f); - fprintf(f, "%" PRIdMAX, json_variant_integer(v)); + fprintf(f, "%" PRIdMAX, sd_json_variant_integer(v)); - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_NORMAL, f); break; - case JSON_VARIANT_UNSIGNED: - if (flags & JSON_FORMAT_COLOR) + case SD_JSON_VARIANT_UNSIGNED: + if (flags & SD_JSON_FORMAT_COLOR) fputs(ansi_highlight_blue(), f); - fprintf(f, "%" PRIuMAX, json_variant_unsigned(v)); + fprintf(f, "%" PRIuMAX, sd_json_variant_unsigned(v)); - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_NORMAL, f); break; - case JSON_VARIANT_BOOLEAN: + case SD_JSON_VARIANT_BOOLEAN: - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_HIGHLIGHT, f); - if (json_variant_boolean(v)) + if (sd_json_variant_boolean(v)) fputs("true", f); else fputs("false", f); - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_NORMAL, f); break; - case JSON_VARIANT_NULL: - if (flags & JSON_FORMAT_COLOR) + case SD_JSON_VARIANT_NULL: + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_HIGHLIGHT, f); fputs("null", f); - if (flags & JSON_FORMAT_COLOR) + if (flags & SD_JSON_FORMAT_COLOR) fputs(ANSI_NORMAL, f); break; - case JSON_VARIANT_STRING: - json_format_string(f, json_variant_string(v), flags); + case SD_JSON_VARIANT_STRING: + json_format_string(f, sd_json_variant_string(v), flags); break; - case JSON_VARIANT_ARRAY: { - size_t n = json_variant_elements(v); + case SD_JSON_VARIANT_ARRAY: { + size_t n = sd_json_variant_elements(v); if (n == 0) fputs("[]", f); else { _cleanup_free_ char *joined = NULL; const char *prefix2; - if (flags & JSON_FORMAT_PRETTY) { + if (flags & SD_JSON_FORMAT_PRETTY) { joined = strjoin(strempty(prefix), "\t"); if (!joined) return -ENOMEM; @@ -1715,18 +1758,18 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha } for (size_t i = 0; i < n; i++) { - JsonVariant *e; + sd_json_variant *e; - assert_se(e = json_variant_by_index(v, i)); + assert_se(e = sd_json_variant_by_index(v, i)); if (i > 0) { - if (flags & JSON_FORMAT_PRETTY) + if (flags & SD_JSON_FORMAT_PRETTY) fputs(",\n", f); else fputc(',', f); } - if (flags & JSON_FORMAT_PRETTY) { + if (flags & SD_JSON_FORMAT_PRETTY) { print_source(f, e, flags, false); fputs(prefix2, f); } @@ -1736,7 +1779,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha return r; } - if (flags & JSON_FORMAT_PRETTY) { + if (flags & SD_JSON_FORMAT_PRETTY) { fputc('\n', f); print_source(f, v, flags, true); fputs(strempty(prefix), f); @@ -1747,15 +1790,15 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha break; } - case JSON_VARIANT_OBJECT: { - size_t n = json_variant_elements(v); + case SD_JSON_VARIANT_OBJECT: { + size_t n = sd_json_variant_elements(v); if (n == 0) fputs("{}", f); else { _cleanup_free_ char *joined = NULL; const char *prefix2; - if (flags & JSON_FORMAT_PRETTY) { + if (flags & SD_JSON_FORMAT_PRETTY) { joined = strjoin(strempty(prefix), "\t"); if (!joined) return -ENOMEM; @@ -1768,18 +1811,18 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha } for (size_t i = 0; i < n; i += 2) { - JsonVariant *e; + sd_json_variant *e; - e = json_variant_by_index(v, i); + e = sd_json_variant_by_index(v, i); if (i > 0) { - if (flags & JSON_FORMAT_PRETTY) + if (flags & SD_JSON_FORMAT_PRETTY) fputs(",\n", f); else fputc(',', f); } - if (flags & JSON_FORMAT_PRETTY) { + if (flags & SD_JSON_FORMAT_PRETTY) { print_source(f, e, flags, false); fputs(prefix2, f); } @@ -1788,14 +1831,14 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha if (r < 0) return r; - fputs(flags & JSON_FORMAT_PRETTY ? " : " : ":", f); + fputs(flags & SD_JSON_FORMAT_PRETTY ? " : " : ":", f); - r = json_format(f, json_variant_by_index(v, i+1), flags, prefix2); + r = json_format(f, sd_json_variant_by_index(v, i+1), flags, prefix2); if (r < 0) return r; } - if (flags & JSON_FORMAT_PRETTY) { + if (flags & SD_JSON_FORMAT_PRETTY) { fputc('\n', f); print_source(f, v, flags, true); fputs(strempty(prefix), f); @@ -1813,7 +1856,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha return 0; } -int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) { +_public_ int sd_json_variant_format(sd_json_variant *v, sd_json_format_flags_t flags, char **ret) { _cleanup_(memstream_done) MemStream m = {}; size_t sz; FILE *f; @@ -1825,14 +1868,14 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) { assert_return(v, -EINVAL); assert_return(ret, -EINVAL); - if (flags & JSON_FORMAT_OFF) + if (flags & SD_JSON_FORMAT_OFF) return -ENOEXEC; f = memstream_init(&m); if (!f) return -ENOMEM; - r = json_variant_dump(v, flags, f, NULL); + r = sd_json_variant_dump(v, flags, f, NULL); if (r < 0) return r; @@ -1843,9 +1886,9 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) { return sz; } -int json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const char *prefix) { +_public_ int sd_json_variant_dump(sd_json_variant *v, sd_json_format_flags_t flags, FILE *f, const char *prefix) { if (!v) { - if (flags & JSON_FORMAT_EMPTY_ARRAY) + if (flags & SD_JSON_FORMAT_EMPTY_ARRAY) v = JSON_VARIANT_MAGIC_EMPTY_ARRAY; else return 0; @@ -1856,73 +1899,73 @@ int json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const char print_source(f, v, flags, false); - if (((flags & (JSON_FORMAT_COLOR_AUTO|JSON_FORMAT_COLOR)) == JSON_FORMAT_COLOR_AUTO) && colors_enabled()) - flags |= JSON_FORMAT_COLOR; + if (((flags & (SD_JSON_FORMAT_COLOR_AUTO|SD_JSON_FORMAT_COLOR)) == SD_JSON_FORMAT_COLOR_AUTO) && colors_enabled()) + flags |= SD_JSON_FORMAT_COLOR; - if (((flags & (JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_PRETTY)) == JSON_FORMAT_PRETTY_AUTO)) - flags |= on_tty() ? JSON_FORMAT_PRETTY : JSON_FORMAT_NEWLINE; + if (((flags & (SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_PRETTY)) == SD_JSON_FORMAT_PRETTY_AUTO)) + flags |= on_tty() ? SD_JSON_FORMAT_PRETTY : SD_JSON_FORMAT_NEWLINE; - if (flags & JSON_FORMAT_SSE) + if (flags & SD_JSON_FORMAT_SSE) fputs("data: ", f); - if (flags & JSON_FORMAT_SEQ) + if (flags & SD_JSON_FORMAT_SEQ) fputc('\x1e', f); /* ASCII Record Separator */ json_format(f, v, flags, prefix); - if (flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_SEQ|JSON_FORMAT_SSE|JSON_FORMAT_NEWLINE)) + if (flags & (SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_SEQ|SD_JSON_FORMAT_SSE|SD_JSON_FORMAT_NEWLINE)) fputc('\n', f); - if (flags & JSON_FORMAT_SSE) + if (flags & SD_JSON_FORMAT_SSE) fputc('\n', f); /* In case of SSE add a second newline */ - if (flags & JSON_FORMAT_FLUSH) + if (flags & SD_JSON_FORMAT_FLUSH) return fflush_and_check(f); return 0; } -int json_variant_filter(JsonVariant **v, char **to_remove) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; - _cleanup_free_ JsonVariant **array = NULL; +_public_ int sd_json_variant_filter(sd_json_variant **v, char **to_remove) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; + _cleanup_free_ sd_json_variant **array = NULL; size_t n = 0, k = 0; int r; assert(v); - if (json_variant_is_blank_object(*v)) + if (sd_json_variant_is_blank_object(*v)) return 0; - if (!json_variant_is_object(*v)) + if (!sd_json_variant_is_object(*v)) return -EINVAL; if (strv_isempty(to_remove)) return 0; - for (size_t i = 0; i < json_variant_elements(*v); i += 2) { - JsonVariant *p; + for (size_t i = 0; i < sd_json_variant_elements(*v); i += 2) { + sd_json_variant *p; - p = json_variant_by_index(*v, i); - if (!json_variant_has_type(p, JSON_VARIANT_STRING)) + p = sd_json_variant_by_index(*v, i); + if (!sd_json_variant_has_type(p, SD_JSON_VARIANT_STRING)) return -EINVAL; - if (strv_contains(to_remove, json_variant_string(p))) { + if (strv_contains(to_remove, sd_json_variant_string(p))) { if (!array) { - array = new(JsonVariant*, json_variant_elements(*v) - 2); + array = new(sd_json_variant*, sd_json_variant_elements(*v) - 2); if (!array) return -ENOMEM; for (k = 0; k < i; k++) - array[k] = json_variant_by_index(*v, k); + array[k] = sd_json_variant_by_index(*v, k); } n++; } else if (array) { array[k++] = p; - array[k++] = json_variant_by_index(*v, i + 1); + array[k++] = sd_json_variant_by_index(*v, i + 1); } } if (n == 0) return 0; - r = json_variant_new_object(&w, array, k); + r = sd_json_variant_new_object(&w, array, k); if (r < 0) return r; @@ -1932,66 +1975,66 @@ int json_variant_filter(JsonVariant **v, char **to_remove) { return (int) n; } -int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *value) { - _cleanup_(json_variant_unrefp) JsonVariant *field_variant = NULL, *w = NULL; - _cleanup_free_ JsonVariant **array = NULL; +_public_ int sd_json_variant_set_field(sd_json_variant **v, const char *field, sd_json_variant *value) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *field_variant = NULL, *w = NULL; + _cleanup_free_ sd_json_variant **array = NULL; size_t k = 0; int r; assert(v); assert(field); - if (json_variant_is_blank_object(*v)) { - array = new(JsonVariant*, 2); + if (sd_json_variant_is_blank_object(*v)) { + array = new(sd_json_variant*, 2); if (!array) return -ENOMEM; } else { - if (!json_variant_is_object(*v)) + if (!sd_json_variant_is_object(*v)) return -EINVAL; - for (size_t i = 0; i < json_variant_elements(*v); i += 2) { - JsonVariant *p; + for (size_t i = 0; i < sd_json_variant_elements(*v); i += 2) { + sd_json_variant *p; - p = json_variant_by_index(*v, i); - if (!json_variant_is_string(p)) + p = sd_json_variant_by_index(*v, i); + if (!sd_json_variant_is_string(p)) return -EINVAL; - if (streq(json_variant_string(p), field)) { + if (streq(sd_json_variant_string(p), field)) { if (!array) { - array = new(JsonVariant*, json_variant_elements(*v)); + array = new(sd_json_variant*, sd_json_variant_elements(*v)); if (!array) return -ENOMEM; for (k = 0; k < i; k++) - array[k] = json_variant_by_index(*v, k); + array[k] = sd_json_variant_by_index(*v, k); } } else if (array) { array[k++] = p; - array[k++] = json_variant_by_index(*v, i + 1); + array[k++] = sd_json_variant_by_index(*v, i + 1); } } if (!array) { - array = new(JsonVariant*, json_variant_elements(*v) + 2); + array = new(sd_json_variant*, sd_json_variant_elements(*v) + 2); if (!array) return -ENOMEM; - for (k = 0; k < json_variant_elements(*v); k++) - array[k] = json_variant_by_index(*v, k); + for (k = 0; k < sd_json_variant_elements(*v); k++) + array[k] = sd_json_variant_by_index(*v, k); } } - r = json_variant_new_string(&field_variant, field); + r = sd_json_variant_new_string(&field_variant, field); if (r < 0) return r; array[k++] = field_variant; array[k++] = value; - r = json_variant_new_object(&w, array, k); + r = sd_json_variant_new_object(&w, array, k); if (r < 0) return r; @@ -2001,128 +2044,128 @@ int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *valu return 1; } -int json_variant_set_fieldb(JsonVariant **v, const char *field, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +_public_ int sd_json_variant_set_fieldb(sd_json_variant **v, const char *field, ...) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; va_list ap; int r; va_start(ap, field); - r = json_buildv(&w, ap); + r = sd_json_buildv(&w, ap); va_end(ap); if (r < 0) return r; - return json_variant_set_field(v, field, w); + return sd_json_variant_set_field(v, field, w); } -int json_variant_set_field_string(JsonVariant **v, const char *field, const char *value) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +_public_ int sd_json_variant_set_field_string(sd_json_variant **v, const char *field, const char *value) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; - r = json_variant_new_string(&m, value); + r = sd_json_variant_new_string(&m, value); if (r < 0) return r; - return json_variant_set_field(v, field, m); + return sd_json_variant_set_field(v, field, m); } -int json_variant_set_field_integer(JsonVariant **v, const char *field, int64_t i) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +_public_ int sd_json_variant_set_field_integer(sd_json_variant **v, const char *field, int64_t i) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; - r = json_variant_new_integer(&m, i); + r = sd_json_variant_new_integer(&m, i); if (r < 0) return r; - return json_variant_set_field(v, field, m); + return sd_json_variant_set_field(v, field, m); } -int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uint64_t u) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +_public_ int sd_json_variant_set_field_unsigned(sd_json_variant **v, const char *field, uint64_t u) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; - r = json_variant_new_unsigned(&m, u); + r = sd_json_variant_new_unsigned(&m, u); if (r < 0) return r; - return json_variant_set_field(v, field, m); + return sd_json_variant_set_field(v, field, m); } -int json_variant_set_field_boolean(JsonVariant **v, const char *field, bool b) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +_public_ int sd_json_variant_set_field_boolean(sd_json_variant **v, const char *field, int b) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; - r = json_variant_new_boolean(&m, b); + r = sd_json_variant_new_boolean(&m, b); if (r < 0) return r; - return json_variant_set_field(v, field, m); + return sd_json_variant_set_field(v, field, m); } -int json_variant_set_field_strv(JsonVariant **v, const char *field, char **l) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +_public_ int sd_json_variant_set_field_strv(sd_json_variant **v, const char *field, char **l) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; - r = json_variant_new_array_strv(&m, l); + r = sd_json_variant_new_array_strv(&m, l); if (r < 0) return r; - return json_variant_set_field(v, field, m); + return sd_json_variant_set_field(v, field, m); } -int json_variant_merge_object(JsonVariant **v, JsonVariant *m) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; - _cleanup_free_ JsonVariant **array = NULL; +_public_ int sd_json_variant_merge_object(sd_json_variant **v, sd_json_variant *m) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; + _cleanup_free_ sd_json_variant **array = NULL; size_t v_elements, m_elements, k; bool v_blank, m_blank; int r; m = json_variant_dereference(m); - v_blank = json_variant_is_blank_object(*v); - m_blank = json_variant_is_blank_object(m); + v_blank = sd_json_variant_is_blank_object(*v); + m_blank = sd_json_variant_is_blank_object(m); - if (!v_blank && !json_variant_is_object(*v)) + if (!v_blank && !sd_json_variant_is_object(*v)) return -EINVAL; - if (!m_blank && !json_variant_is_object(m)) + if (!m_blank && !sd_json_variant_is_object(m)) return -EINVAL; if (m_blank) return 0; /* nothing to do */ if (v_blank) { - JSON_VARIANT_REPLACE(*v, json_variant_ref(m)); + JSON_VARIANT_REPLACE(*v, sd_json_variant_ref(m)); return 1; } - v_elements = json_variant_elements(*v); - m_elements = json_variant_elements(m); + v_elements = sd_json_variant_elements(*v); + m_elements = sd_json_variant_elements(m); if (v_elements > SIZE_MAX - m_elements) /* overflow check */ return -ENOMEM; - array = new(JsonVariant*, v_elements + m_elements); + array = new(sd_json_variant*, v_elements + m_elements); if (!array) return -ENOMEM; k = 0; for (size_t i = 0; i < v_elements; i += 2) { - JsonVariant *u; + sd_json_variant *u; - u = json_variant_by_index(*v, i); - if (!json_variant_is_string(u)) + u = sd_json_variant_by_index(*v, i); + if (!sd_json_variant_is_string(u)) return -EINVAL; - if (json_variant_by_key(m, json_variant_string(u))) + if (sd_json_variant_by_key(m, sd_json_variant_string(u))) continue; /* skip if exists in second variant */ array[k++] = u; - array[k++] = json_variant_by_index(*v, i + 1); + array[k++] = sd_json_variant_by_index(*v, i + 1); } for (size_t i = 0; i < m_elements; i++) - array[k++] = json_variant_by_index(m, i); + array[k++] = sd_json_variant_by_index(m, i); - r = json_variant_new_object(&w, array, k); + r = sd_json_variant_new_object(&w, array, k); if (r < 0) return r; @@ -2133,37 +2176,37 @@ int json_variant_merge_object(JsonVariant **v, JsonVariant *m) { return 1; } -int json_variant_merge_objectb(JsonVariant **v, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +_public_ int sd_json_variant_merge_objectb(sd_json_variant **v, ...) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; va_list ap; int r; va_start(ap, v); - r = json_buildv(&w, ap); + r = sd_json_buildv(&w, ap); va_end(ap); if (r < 0) return r; - return json_variant_merge_object(v, w); + return sd_json_variant_merge_object(v, w); } -int json_variant_append_array(JsonVariant **v, JsonVariant *element) { - _cleanup_(json_variant_unrefp) JsonVariant *nv = NULL; +_public_ int sd_json_variant_append_array(sd_json_variant **v, sd_json_variant *element) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *nv = NULL; bool blank; int r; assert(v); assert(element); - if (!*v || json_variant_is_null(*v)) + if (!*v || sd_json_variant_is_null(*v)) blank = true; - else if (json_variant_is_array(*v)) - blank = json_variant_elements(*v) == 0; + else if (sd_json_variant_is_array(*v)) + blank = sd_json_variant_elements(*v) == 0; else return -EINVAL; if (blank) { - r = json_variant_new_array(&nv, (JsonVariant*[]) { element }, 1); + r = sd_json_variant_new_array(&nv, (sd_json_variant*[]) { element }, 1); if (r < 0) return r; } else if (json_variant_n_ref(*v) == 1) { @@ -2171,10 +2214,10 @@ int json_variant_append_array(JsonVariant **v, JsonVariant *element) { * to itself, or one of the objects embedded in *v to *v. If the reference count grows, we * need to fall back to the other method below. */ - _unused_ _cleanup_(json_variant_unrefp) JsonVariant *dummy = json_variant_ref(element); + _unused_ _cleanup_(sd_json_variant_unrefp) sd_json_variant *dummy = sd_json_variant_ref(element); if (json_variant_n_ref(*v) == 1) { /* We hold the only reference. Let's mutate the object. */ - size_t size = json_variant_elements(*v); + size_t size = sd_json_variant_elements(*v); void *old = *v; if (!GREEDY_REALLOC(*v, size + 1 + 1)) @@ -2185,23 +2228,23 @@ int json_variant_append_array(JsonVariant **v, JsonVariant *element) { for (size_t i = 1; i < size; i++) (*v)[1 + i].parent = *v; - return _json_variant_array_put_element(*v, element); + return json_variant_array_put_element(*v, element); } } if (!blank) { - size_t size = json_variant_elements(*v); + size_t size = sd_json_variant_elements(*v); - _cleanup_free_ JsonVariant **array = new(JsonVariant*, size + 1); + _cleanup_free_ sd_json_variant **array = new(sd_json_variant*, size + 1); if (!array) return -ENOMEM; for (size_t i = 0; i < size; i++) - array[i] = json_variant_by_index(*v, i); + array[i] = sd_json_variant_by_index(*v, i); array[size] = element; - r = json_variant_new_array(&nv, array, size + 1); + r = sd_json_variant_new_array(&nv, array, size + 1); if (r < 0) return r; } @@ -2212,49 +2255,49 @@ int json_variant_append_array(JsonVariant **v, JsonVariant *element) { return 0; } -int json_variant_append_arrayb(JsonVariant **v, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +_public_ int sd_json_variant_append_arrayb(sd_json_variant **v, ...) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; va_list ap; int r; va_start(ap, v); - r = json_buildv(&w, ap); + r = sd_json_buildv(&w, ap); va_end(ap); if (r < 0) return r; - return json_variant_append_array(v, w); + return sd_json_variant_append_array(v, w); } -JsonVariant *json_variant_find(JsonVariant *haystack, JsonVariant *needle) { - JsonVariant *i; +_public_ sd_json_variant *sd_json_variant_find(sd_json_variant *haystack, sd_json_variant *needle) { + sd_json_variant *i; /* Find a json object in an array. Returns NULL if not found, or if the array is not actually an array. */ JSON_VARIANT_ARRAY_FOREACH(i, haystack) - if (json_variant_equal(i, needle)) + if (sd_json_variant_equal(i, needle)) return i; return NULL; } -int json_variant_append_array_nodup(JsonVariant **v, JsonVariant *element) { +_public_ int sd_json_variant_append_array_nodup(sd_json_variant **v, sd_json_variant *element) { assert(v); - if (json_variant_find(*v, element)) + if (sd_json_variant_find(*v, element)) return 0; - return json_variant_append_array(v, element); + return sd_json_variant_append_array(v, element); } -int json_variant_strv(JsonVariant *v, char ***ret) { +_public_ int sd_json_variant_strv(sd_json_variant *v, char ***ret) { char **l = NULL; bool sensitive; int r; assert(ret); - if (!v || json_variant_is_null(v)) { + if (!v || sd_json_variant_is_null(v)) { l = new0(char*, 1); if (!l) return -ENOMEM; @@ -2263,29 +2306,29 @@ int json_variant_strv(JsonVariant *v, char ***ret) { return 0; } - if (!json_variant_is_array(v)) + if (!sd_json_variant_is_array(v)) return -EINVAL; - sensitive = json_variant_is_sensitive(v); + sensitive = sd_json_variant_is_sensitive(v); - size_t n = json_variant_elements(v); + size_t n = sd_json_variant_elements(v); l = new(char*, n+1); if (!l) return -ENOMEM; for (size_t i = 0; i < n; i++) { - JsonVariant *e; + sd_json_variant *e; - assert_se(e = json_variant_by_index(v, i)); - sensitive = sensitive || json_variant_is_sensitive(e); + assert_se(e = sd_json_variant_by_index(v, i)); + sensitive = sensitive || sd_json_variant_is_sensitive(e); - if (!json_variant_is_string(e)) { + if (!sd_json_variant_is_string(e)) { l[i] = NULL; r = -EINVAL; goto fail; } - l[i] = strdup(json_variant_string(e)); + l[i] = strdup(sd_json_variant_string(e)); if (!l[i]) { r = -ENOMEM; goto fail; @@ -2306,9 +2349,9 @@ fail: return r; } -static int json_variant_copy(JsonVariant **nv, JsonVariant *v) { - JsonVariantType t; - JsonVariant *c; +static int json_variant_copy(sd_json_variant **nv, sd_json_variant *v) { + sd_json_variant_type_t t; + sd_json_variant *c; JsonValue value; const void *source; size_t k; @@ -2317,39 +2360,39 @@ static int json_variant_copy(JsonVariant **nv, JsonVariant *v) { assert(v); /* Let's copy the simple types literally, and the larger types by references */ - t = json_variant_type(v); + t = sd_json_variant_type(v); switch (t) { - case JSON_VARIANT_INTEGER: + case SD_JSON_VARIANT_INTEGER: k = sizeof(int64_t); - value.integer = json_variant_integer(v); + value.integer = sd_json_variant_integer(v); source = &value; break; - case JSON_VARIANT_UNSIGNED: + case SD_JSON_VARIANT_UNSIGNED: k = sizeof(uint64_t); - value.unsig = json_variant_unsigned(v); + value.unsig = sd_json_variant_unsigned(v); source = &value; break; - case JSON_VARIANT_REAL: + case SD_JSON_VARIANT_REAL: k = sizeof(double); - value.real = json_variant_real(v); + value.real = sd_json_variant_real(v); source = &value; break; - case JSON_VARIANT_BOOLEAN: + case SD_JSON_VARIANT_BOOLEAN: k = sizeof(bool); - value.boolean = json_variant_boolean(v); + value.boolean = sd_json_variant_boolean(v); source = &value; break; - case JSON_VARIANT_NULL: + case SD_JSON_VARIANT_NULL: k = 0; source = NULL; break; - case JSON_VARIANT_STRING: - source = json_variant_string(v); + case SD_JSON_VARIANT_STRING: + source = sd_json_variant_string(v); k = strnlen(source, INLINE_STRING_MAX + 1); if (k <= INLINE_STRING_MAX) { k++; @@ -2361,22 +2404,22 @@ static int json_variant_copy(JsonVariant **nv, JsonVariant *v) { default: /* Everything else copy by reference */ - c = malloc0(MAX(sizeof(JsonVariant), - offsetof(JsonVariant, reference) + sizeof(JsonVariant*))); + c = malloc0(MAX(sizeof(sd_json_variant), + offsetof(sd_json_variant, reference) + sizeof(sd_json_variant*))); if (!c) return -ENOMEM; c->n_ref = 1; c->type = t; c->is_reference = true; - c->reference = json_variant_ref(json_variant_formalize(v)); + c->reference = sd_json_variant_ref(json_variant_formalize(v)); *nv = c; return 0; } - c = malloc0(MAX(sizeof(JsonVariant), - offsetof(JsonVariant, value) + k)); + c = malloc0(MAX(sizeof(sd_json_variant), + offsetof(sd_json_variant, value) + k)); if (!c) return -ENOMEM; @@ -2391,7 +2434,7 @@ static int json_variant_copy(JsonVariant **nv, JsonVariant *v) { return 0; } -static bool json_single_ref(JsonVariant *v) { +static bool json_single_ref(sd_json_variant *v) { /* Checks whether the caller is the single owner of the object, i.e. can get away with changing it */ @@ -2405,8 +2448,8 @@ static bool json_single_ref(JsonVariant *v) { return v->n_ref == 1; } -static int json_variant_set_source(JsonVariant **v, JsonSource *source, unsigned line, unsigned column) { - JsonVariant *w; +static int json_variant_set_source(sd_json_variant **v, JsonSource *source, unsigned line, unsigned column) { + sd_json_variant *w; int r; assert(v); @@ -2762,7 +2805,7 @@ int json_tokenize( const char **p, char **ret_string, JsonValue *ret_value, - unsigned *ret_line, /* 'ret_line' returns the line at the beginning of this token */ + unsigned *ret_line, /* 'reterr_line' returns the line at the beginning of this token */ unsigned *ret_column, void **state, unsigned *line, /* 'line' is used as a line state, it always reflect the line we are at after the token was read */ @@ -2937,7 +2980,7 @@ finish: } typedef enum JsonExpect { - /* The following values are used by json_parse() */ + /* The following values are used by sd_json_parse() */ EXPECT_TOPLEVEL, EXPECT_END, EXPECT_OBJECT_FIRST_KEY, @@ -2949,14 +2992,14 @@ typedef enum JsonExpect { EXPECT_ARRAY_NEXT_ELEMENT, EXPECT_ARRAY_COMMA, - /* And these are used by json_build() */ + /* And these are used by sd_json_build() */ EXPECT_ARRAY_ELEMENT, EXPECT_OBJECT_KEY, } JsonExpect; typedef struct JsonStack { JsonExpect expect; - JsonVariant **elements; + sd_json_variant **elements; size_t n_elements; unsigned line_before; unsigned column_before; @@ -2966,14 +3009,14 @@ typedef struct JsonStack { static void json_stack_release(JsonStack *s) { assert(s); - CLEANUP_ARRAY(s->elements, s->n_elements, json_variant_unref_many); + CLEANUP_ARRAY(s->elements, s->n_elements, sd_json_variant_unref_many); } static int json_parse_internal( const char **input, JsonSource *source, - JsonParseFlags flags, - JsonVariant **ret, + sd_json_parse_flags_t flags, + sd_json_variant **ret, unsigned *line, unsigned *column, bool continue_end) { @@ -3003,7 +3046,7 @@ static int json_parse_internal( column = &column_buffer; for (;;) { - _cleanup_(json_variant_unrefp) JsonVariant *add = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *add = NULL; _cleanup_free_ char *string = NULL; unsigned line_token, column_token; JsonStack *current; @@ -3097,7 +3140,7 @@ static int json_parse_internal( assert(n_stack > 1); - r = json_variant_new_object(&add, current->elements, current->n_elements); + r = sd_json_variant_new_object(&add, current->elements, current->n_elements); if (r < 0) goto finish; @@ -3147,7 +3190,7 @@ static int json_parse_internal( assert(n_stack > 1); - r = json_variant_new_array(&add, current->elements, current->n_elements); + r = sd_json_variant_new_array(&add, current->elements, current->n_elements); if (r < 0) goto finish; @@ -3164,7 +3207,7 @@ static int json_parse_internal( goto finish; } - r = json_variant_new_string(&add, string); + r = sd_json_variant_new_string(&add, string); if (r < 0) goto finish; @@ -3187,7 +3230,7 @@ static int json_parse_internal( goto finish; } - r = json_variant_new_real(&add, value.real); + r = sd_json_variant_new_real(&add, value.real); if (r < 0) goto finish; @@ -3208,7 +3251,7 @@ static int json_parse_internal( goto finish; } - r = json_variant_new_integer(&add, value.integer); + r = sd_json_variant_new_integer(&add, value.integer); if (r < 0) goto finish; @@ -3229,7 +3272,7 @@ static int json_parse_internal( goto finish; } - r = json_variant_new_unsigned(&add, value.unsig); + r = sd_json_variant_new_unsigned(&add, value.unsig); if (r < 0) goto finish; @@ -3250,7 +3293,7 @@ static int json_parse_internal( goto finish; } - r = json_variant_new_boolean(&add, value.boolean); + r = sd_json_variant_new_boolean(&add, value.boolean); if (r < 0) goto finish; @@ -3271,7 +3314,7 @@ static int json_parse_internal( goto finish; } - r = json_variant_new_null(&add); + r = sd_json_variant_new_null(&add); if (r < 0) goto finish; @@ -3294,8 +3337,8 @@ static int json_parse_internal( /* If we are asked to make this parsed object sensitive, then let's apply this * immediately after allocating each variant, so that when we abort half-way * everything we already allocated that is then freed is correctly marked. */ - if (FLAGS_SET(flags, JSON_PARSE_SENSITIVE)) - json_variant_sensitive(add); + if (FLAGS_SET(flags, SD_JSON_PARSE_SENSITIVE)) + sd_json_variant_sensitive(add); (void) json_variant_set_source(&add, source, line_token, column_token); @@ -3312,7 +3355,7 @@ done: assert(n_stack == 1); assert(stack[0].n_elements == 1); - *ret = json_variant_ref(stack[0].elements[0]); + *ret = sd_json_variant_ref(stack[0].elements[0]); *input = p; r = 0; @@ -3325,13 +3368,13 @@ finish: return r; } -int json_parse_with_source( +_public_ int sd_json_parse_with_source( const char *input, const char *source, - JsonParseFlags flags, - JsonVariant **ret, - unsigned *ret_line, - unsigned *ret_column) { + sd_json_parse_flags_t flags, + sd_json_variant **ret, + unsigned *reterr_line, + unsigned *reterr_column) { _cleanup_(json_source_unrefp) JsonSource *s = NULL; @@ -3341,16 +3384,16 @@ int json_parse_with_source( return -ENOMEM; } - return json_parse_internal(&input, s, flags, ret, ret_line, ret_column, false); + return json_parse_internal(&input, s, flags, ret, reterr_line, reterr_column, false); } -int json_parse_with_source_continue( +_public_ int sd_json_parse_with_source_continue( const char **p, const char *source, - JsonParseFlags flags, - JsonVariant **ret, - unsigned *ret_line, - unsigned *ret_column) { + sd_json_parse_flags_t flags, + sd_json_variant **ret, + unsigned *reterr_line, + unsigned *reterr_column) { _cleanup_(json_source_unrefp) JsonSource *s = NULL; @@ -3360,17 +3403,37 @@ int json_parse_with_source_continue( return -ENOMEM; } - return json_parse_internal(p, s, flags, ret, ret_line, ret_column, true); + return json_parse_internal(p, s, flags, ret, reterr_line, reterr_column, true); +} + +_public_ int sd_json_parse( + const char *string, + sd_json_parse_flags_t flags, + sd_json_variant **ret, + unsigned *reterr_line, + unsigned *reterr_column) { + + return sd_json_parse_with_source(string, NULL, flags, ret, reterr_line, reterr_column); +} + +_public_ int sd_json_parse_continue( + const char **p, + sd_json_parse_flags_t flags, + sd_json_variant **ret, + unsigned *reterr_line, + unsigned *reterr_column) { + + return sd_json_parse_with_source_continue(p, NULL, flags, ret, reterr_line, reterr_column); } -int json_parse_file_at( +_public_ int sd_json_parse_file_at( FILE *f, int dir_fd, const char *path, - JsonParseFlags flags, - JsonVariant **ret, - unsigned *ret_line, - unsigned *ret_column) { + sd_json_parse_flags_t flags, + sd_json_variant **ret, + unsigned *reterr_line, + unsigned *reterr_column) { _cleanup_free_ char *text = NULL; int r; @@ -3387,10 +3450,21 @@ int json_parse_file_at( if (isempty(text)) return -ENODATA; - return json_parse_with_source(text, path, flags, ret, ret_line, ret_column); + return sd_json_parse_with_source(text, path, flags, ret, reterr_line, reterr_column); } -int json_buildv(JsonVariant **ret, va_list ap) { +_public_ int sd_json_parse_file( + FILE *f, + const char *path, + sd_json_parse_flags_t flags, + sd_json_variant **ret, + unsigned *reterr_line, + unsigned *reterr_column) { + + return sd_json_parse_file_at(f, AT_FDCWD, path, flags, ret, reterr_line, reterr_column); +} + +_public_ int sd_json_buildv(sd_json_variant **ret, va_list ap) { JsonStack *stack = NULL; size_t n_stack = 1; const char *name = NULL; @@ -3406,7 +3480,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { }; for (;;) { - _cleanup_(json_variant_unrefp) JsonVariant *add = NULL, *add_more = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *add = NULL, *add_more = NULL; size_t n_subtract = 0; /* how much to subtract from current->n_suppress, i.e. how many elements would * have been added to the current variant */ JsonStack *current; @@ -3422,7 +3496,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { switch (command) { - case _JSON_BUILD_STRING: { + case _SD_JSON_BUILD_STRING: { const char *p; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3433,7 +3507,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { p = va_arg(ap, const char *); if (current->n_suppress == 0) { - r = json_variant_new_string(&add, p); + r = sd_json_variant_new_string(&add, p); if (r < 0) goto finish; } @@ -3450,7 +3524,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_INTEGER: { + case _SD_JSON_BUILD_INTEGER: { int64_t j; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3461,7 +3535,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { j = va_arg(ap, int64_t); if (current->n_suppress == 0) { - r = json_variant_new_integer(&add, j); + r = sd_json_variant_new_integer(&add, j); if (r < 0) goto finish; } @@ -3478,7 +3552,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_UNSIGNED: { + case _SD_JSON_BUILD_UNSIGNED: { uint64_t j; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3489,7 +3563,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { j = va_arg(ap, uint64_t); if (current->n_suppress == 0) { - r = json_variant_new_unsigned(&add, j); + r = sd_json_variant_new_unsigned(&add, j); if (r < 0) goto finish; } @@ -3506,7 +3580,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_REAL: { + case _SD_JSON_BUILD_REAL: { double d; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3517,7 +3591,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { d = va_arg(ap, double); if (current->n_suppress == 0) { - r = json_variant_new_real(&add, d); + r = sd_json_variant_new_real(&add, d); if (r < 0) goto finish; } @@ -3534,7 +3608,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_BOOLEAN: { + case _SD_JSON_BUILD_BOOLEAN: { bool b; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3545,7 +3619,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { b = va_arg(ap, int); if (current->n_suppress == 0) { - r = json_variant_new_boolean(&add, b); + r = sd_json_variant_new_boolean(&add, b); if (r < 0) goto finish; } @@ -3562,7 +3636,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_NULL: + case _SD_JSON_BUILD_NULL: if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { r = -EINVAL; @@ -3570,7 +3644,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { } if (current->n_suppress == 0) { - r = json_variant_new_null(&add); + r = sd_json_variant_new_null(&add); if (r < 0) goto finish; } @@ -3586,7 +3660,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; - case _JSON_BUILD_VARIANT: + case _SD_JSON_BUILD_VARIANT: if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { r = -EINVAL; @@ -3595,11 +3669,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { /* Note that we don't care for current->n_suppress here, after all the variant is already * allocated anyway... */ - add = va_arg(ap, JsonVariant*); + add = va_arg(ap, sd_json_variant*); if (!add) add = JSON_VARIANT_MAGIC_NULL; else - json_variant_ref(add); + sd_json_variant_ref(add); n_subtract = 1; @@ -3612,8 +3686,8 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; - case _JSON_BUILD_VARIANT_ARRAY: { - JsonVariant **array; + case _SD_JSON_BUILD_VARIANT_ARRAY: { + sd_json_variant **array; size_t n; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3621,11 +3695,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { goto finish; } - array = va_arg(ap, JsonVariant**); + array = va_arg(ap, sd_json_variant**); n = va_arg(ap, size_t); if (current->n_suppress == 0) { - r = json_variant_new_array(&add, array, n); + r = sd_json_variant_new_array(&add, array, n); if (r < 0) goto finish; } @@ -3642,7 +3716,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_LITERAL: { + case _SD_JSON_BUILD_LITERAL: { const char *l; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3656,7 +3730,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { /* Note that we don't care for current->n_suppress here, we should generate parsing * errors even in suppressed object properties */ - r = json_parse(l, 0, &add, NULL, NULL); + r = sd_json_parse(l, 0, &add, NULL, NULL); if (r < 0) goto finish; } else @@ -3674,7 +3748,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_ARRAY_BEGIN: + case _SD_JSON_BUILD_ARRAY_BEGIN: if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { r = -EINVAL; @@ -3704,7 +3778,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; - case _JSON_BUILD_ARRAY_END: + case _SD_JSON_BUILD_ARRAY_END: if (current->expect != EXPECT_ARRAY_ELEMENT) { r = -EINVAL; goto finish; @@ -3713,7 +3787,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { assert(n_stack > 1); if (current->n_suppress == 0) { - r = json_variant_new_array(&add, current->elements, current->n_elements); + r = sd_json_variant_new_array(&add, current->elements, current->n_elements); if (r < 0) goto finish; } @@ -3725,7 +3799,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; - case _JSON_BUILD_STRV: { + case _SD_JSON_BUILD_STRV: { char **l; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3736,7 +3810,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { l = va_arg(ap, char **); if (current->n_suppress == 0) { - r = json_variant_new_array_strv(&add, l); + r = sd_json_variant_new_array_strv(&add, l); if (r < 0) goto finish; } @@ -3779,7 +3853,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { } if (current->n_suppress == 0) { - r = json_variant_new_array_strv(&add, el); + r = sd_json_variant_new_array_strv(&add, el); if (r < 0) goto finish; } @@ -3796,10 +3870,10 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_BASE64: - case _JSON_BUILD_BASE32HEX: - case _JSON_BUILD_HEX: - case _JSON_BUILD_OCTESCAPE: { + case _SD_JSON_BUILD_BASE64: + case _SD_JSON_BUILD_BASE32HEX: + case _SD_JSON_BUILD_HEX: + case _SD_JSON_BUILD_OCTESCAPE: { const void *p; size_t n; @@ -3812,10 +3886,10 @@ int json_buildv(JsonVariant **ret, va_list ap) { n = va_arg(ap, size_t); if (current->n_suppress == 0) { - r = command == _JSON_BUILD_BASE64 ? json_variant_new_base64(&add, p, n) : - command == _JSON_BUILD_BASE32HEX ? json_variant_new_base32hex(&add, p, n) : - command == _JSON_BUILD_HEX ? json_variant_new_hex(&add, p, n) : - json_variant_new_octescape(&add, p, n); + r = command == _SD_JSON_BUILD_BASE64 ? sd_json_variant_new_base64(&add, p, n) : + command == _SD_JSON_BUILD_BASE32HEX ? sd_json_variant_new_base32hex(&add, p, n) : + command == _SD_JSON_BUILD_HEX ? sd_json_variant_new_hex(&add, p, n) : + sd_json_variant_new_octescape(&add, p, n); if (r < 0) goto finish; } @@ -3845,10 +3919,10 @@ int json_buildv(JsonVariant **ret, va_list ap) { if (current->n_suppress == 0) { if (iov) - r = command == _JSON_BUILD_IOVEC_BASE64 ? json_variant_new_base64(&add, iov->iov_base, iov->iov_len) : - json_variant_new_hex(&add, iov->iov_base, iov->iov_len); + r = command == _JSON_BUILD_IOVEC_BASE64 ? sd_json_variant_new_base64(&add, iov->iov_base, iov->iov_len) : + sd_json_variant_new_hex(&add, iov->iov_base, iov->iov_len); else - r = json_variant_new_string(&add, ""); + r = sd_json_variant_new_string(&add, ""); if (r < 0) goto finish; } @@ -3865,8 +3939,8 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_ID128: - case _JSON_BUILD_UUID: { + case _SD_JSON_BUILD_ID128: + case _SD_JSON_BUILD_UUID: { const sd_id128_t *id; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3877,9 +3951,9 @@ int json_buildv(JsonVariant **ret, va_list ap) { assert_se(id = va_arg(ap, sd_id128_t*)); if (current->n_suppress == 0) { - r = command == _JSON_BUILD_ID128 ? - json_variant_new_id128(&add, *id) : - json_variant_new_uuid(&add, *id); + r = command == _SD_JSON_BUILD_ID128 ? + sd_json_variant_new_id128(&add, *id) : + sd_json_variant_new_uuid(&add, *id); if (r < 0) goto finish; } @@ -3896,7 +3970,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_BYTE_ARRAY: { + case _SD_JSON_BUILD_BYTE_ARRAY: { const void *array; size_t n; @@ -3909,7 +3983,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { n = va_arg(ap, size_t); if (current->n_suppress == 0) { - r = json_variant_new_array_bytes(&add, array, n); + r = sd_json_variant_new_array_bytes(&add, array, n); if (r < 0) goto finish; } @@ -3937,7 +4011,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { assert_se(hw_addr = va_arg(ap, struct hw_addr_data*)); if (current->n_suppress == 0) { - r = json_variant_new_array_bytes(&add, hw_addr->bytes, hw_addr->length); + r = sd_json_variant_new_array_bytes(&add, hw_addr->bytes, hw_addr->length); if (r < 0) goto finish; } @@ -3971,7 +4045,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { if (r < 0) goto finish; - r = json_variant_new_array_strv(&add, sorted); + r = sd_json_variant_new_array_strv(&add, sorted); if (r < 0) goto finish; } @@ -3988,8 +4062,8 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_CALLBACK: { - JsonBuildCallback cb; + case _SD_JSON_BUILD_CALLBACK: { + sd_json_build_callback_t cb; void *userdata; if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { @@ -3997,7 +4071,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { goto finish; } - cb = va_arg(ap, JsonBuildCallback); + cb = va_arg(ap, sd_json_build_callback_t); userdata = va_arg(ap, void *); if (current->n_suppress == 0) { @@ -4025,7 +4099,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_OBJECT_BEGIN: + case _SD_JSON_BUILD_OBJECT_BEGIN: if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { r = -EINVAL; @@ -4055,7 +4129,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; - case _JSON_BUILD_OBJECT_END: + case _SD_JSON_BUILD_OBJECT_END: if (current->expect != EXPECT_OBJECT_KEY) { r = -EINVAL; @@ -4065,7 +4139,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { assert(n_stack > 1); if (current->n_suppress == 0) { - r = json_variant_new_object(&add, current->elements, current->n_elements); + r = sd_json_variant_new_object(&add, current->elements, current->n_elements); if (r < 0) goto finish; } @@ -4077,7 +4151,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; - case _JSON_BUILD_PAIR: { + case _SD_JSON_BUILD_PAIR: { if (current->expect != EXPECT_OBJECT_KEY) { r = -EINVAL; @@ -4087,7 +4161,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { name = va_arg(ap, const char *); if (current->n_suppress == 0) { - r = json_variant_new_string(&add, name); + r = sd_json_variant_new_string(&add, name); if (r < 0) goto finish; } @@ -4098,7 +4172,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } - case _JSON_BUILD_PAIR_CONDITION: { + case _SD_JSON_BUILD_PAIR_CONDITION: { bool b; if (current->expect != EXPECT_OBJECT_KEY) { @@ -4110,7 +4184,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { name = va_arg(ap, const char *); if (b && current->n_suppress == 0) { - r = json_variant_new_string(&add, name); + r = sd_json_variant_new_string(&add, name); if (r < 0) goto finish; } @@ -4137,11 +4211,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { u = va_arg(ap, uint64_t); if (u != 0 && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_unsigned(&add_more, u); + r = sd_json_variant_new_unsigned(&add_more, u); if (r < 0) goto finish; } @@ -4165,11 +4239,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { u = va_arg(ap, usec_t); if (u != USEC_INFINITY && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_unsigned(&add_more, u); + r = sd_json_variant_new_unsigned(&add_more, u); if (r < 0) goto finish; } @@ -4192,11 +4266,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { s = va_arg(ap, const char *); if (!isempty(s) && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_string(&add_more, s); + r = sd_json_variant_new_string(&add_more, s); if (r < 0) goto finish; } @@ -4220,11 +4294,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { l = va_arg(ap, char **); if (!strv_isempty(l) && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_array_strv(&add_more, l); + r = sd_json_variant_new_array_strv(&add_more, l); if (r < 0) goto finish; } @@ -4236,7 +4310,7 @@ int json_buildv(JsonVariant **ret, va_list ap) { } case _JSON_BUILD_PAIR_VARIANT_NON_NULL: { - JsonVariant *v; + sd_json_variant *v; const char *n; if (current->expect != EXPECT_OBJECT_KEY) { @@ -4245,14 +4319,14 @@ int json_buildv(JsonVariant **ret, va_list ap) { } n = va_arg(ap, const char *); - v = va_arg(ap, JsonVariant *); + v = va_arg(ap, sd_json_variant *); - if (v && !json_variant_is_null(v) && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + if (v && !sd_json_variant_is_null(v) && current->n_suppress == 0) { + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - add_more = json_variant_ref(v); + add_more = sd_json_variant_ref(v); } n_subtract = 2; /* we generated two item */ @@ -4274,11 +4348,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { a = va_arg(ap, const struct in_addr *); if (a && in4_addr_is_set(a) && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_array_bytes(&add_more, a, sizeof(struct in_addr)); + r = sd_json_variant_new_array_bytes(&add_more, a, sizeof(struct in_addr)); if (r < 0) goto finish; } @@ -4302,11 +4376,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { a = va_arg(ap, const struct in6_addr *); if (a && in6_addr_is_set(a) && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_array_bytes(&add_more, a, sizeof(struct in6_addr)); + r = sd_json_variant_new_array_bytes(&add_more, a, sizeof(struct in6_addr)); if (r < 0) goto finish; } @@ -4332,11 +4406,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { f = va_arg(ap, int); if (a && in_addr_is_set(f, a) && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_array_bytes(&add_more, a->bytes, FAMILY_ADDRESS_SIZE(f)); + r = sd_json_variant_new_array_bytes(&add_more, a->bytes, FAMILY_ADDRESS_SIZE(f)); if (r < 0) goto finish; } @@ -4360,11 +4434,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { a = va_arg(ap, const struct ether_addr *); if (a && !ether_addr_is_null(a) && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_array_bytes(&add_more, a->ether_addr_octet, sizeof(struct ether_addr)); + r = sd_json_variant_new_array_bytes(&add_more, a->ether_addr_octet, sizeof(struct ether_addr)); if (r < 0) goto finish; } @@ -4388,11 +4462,11 @@ int json_buildv(JsonVariant **ret, va_list ap) { a = va_arg(ap, const struct hw_addr_data *); if (a && !hw_addr_is_null(a) && current->n_suppress == 0) { - r = json_variant_new_string(&add, n); + r = sd_json_variant_new_string(&add, n); if (r < 0) goto finish; - r = json_variant_new_array_bytes(&add_more, a->bytes, a->length); + r = sd_json_variant_new_array_bytes(&add_more, a->bytes, a->length); if (r < 0) goto finish; } @@ -4431,7 +4505,7 @@ done: assert(n_stack == 1); assert(stack[0].n_elements == 1); - *ret = json_variant_ref(stack[0].elements[0]); + *ret = sd_json_variant_ref(stack[0].elements[0]); r = 0; finish: @@ -4443,19 +4517,19 @@ finish: return r; } -int json_build(JsonVariant **ret, ...) { +_public_ int sd_json_build(sd_json_variant **ret, ...) { va_list ap; int r; va_start(ap, ret); - r = json_buildv(ret, ap); + r = sd_json_buildv(ret, ap); va_end(ap); return r; } int json_log_internal( - JsonVariant *variant, + sd_json_variant *variant, int level, int error, const char *file, @@ -4478,7 +4552,7 @@ int json_log_internal( va_end(ap); if (variant) { - r = json_variant_get_source(variant, &source, &source_line, &source_column); + r = sd_json_variant_get_source(variant, &source, &source_line, &source_column); if (r < 0) return r; } else { @@ -4518,7 +4592,7 @@ int json_log_internal( NULL); } -static void *dispatch_userdata(const JsonDispatch *p, void *userdata) { +static void *dispatch_userdata(const sd_json_dispatch_field *p, void *userdata) { /* When the userdata pointer is passed in as NULL, then we'll just use the offset as a literal * address, and convert it to a pointer. Note that might as well just add the offset to the NULL @@ -4531,21 +4605,21 @@ static void *dispatch_userdata(const JsonDispatch *p, void *userdata) { return SIZE_TO_PTR(p->offset); } -int json_dispatch_full( - JsonVariant *v, - const JsonDispatch table[], - JsonDispatchCallback bad, - JsonDispatchFlags flags, +_public_ int sd_json_dispatch_full( + sd_json_variant *v, + const sd_json_dispatch_field table[], + sd_json_dispatch_callback_t bad, + sd_json_dispatch_flags_t flags, void *userdata, const char **reterr_bad_field) { size_t m; int r, done = 0; bool *found; - if (!json_variant_is_object(v)) { + if (!sd_json_variant_is_object(v)) { json_log(v, flags, 0, "JSON variant is not an object."); - if (flags & JSON_PERMISSIVE) + if (flags & SD_JSON_PERMISSIVE) return 0; if (reterr_bad_field) @@ -4555,37 +4629,37 @@ int json_dispatch_full( } m = 0; - for (const JsonDispatch *p = table; p->name; p++) + for (const sd_json_dispatch_field *p = table; p->name; p++) m++; found = newa0(bool, m); - size_t n = json_variant_elements(v); + size_t n = sd_json_variant_elements(v); for (size_t i = 0; i < n; i += 2) { - JsonVariant *key, *value; - const JsonDispatch *p; + sd_json_variant *key, *value; + const sd_json_dispatch_field *p; - assert_se(key = json_variant_by_index(v, i)); - assert_se(value = json_variant_by_index(v, i+1)); + assert_se(key = sd_json_variant_by_index(v, i)); + assert_se(value = sd_json_variant_by_index(v, i+1)); for (p = table; p->name; p++) if (p->name == POINTER_MAX || - streq_ptr(json_variant_string(key), p->name)) + streq_ptr(sd_json_variant_string(key), p->name)) break; if (p->name) { /* Found a matching entry! 🙂 */ - JsonDispatchFlags merged_flags; + sd_json_dispatch_flags_t merged_flags; merged_flags = flags | p->flags; - if (p->type != _JSON_VARIANT_TYPE_INVALID && - !json_variant_has_type(value, p->type)) { + if (p->type != _SD_JSON_VARIANT_TYPE_INVALID && + !sd_json_variant_has_type(value, p->type)) { json_log(value, merged_flags, 0, - "Object field '%s' has wrong type %s, expected %s.", json_variant_string(key), - json_variant_type_to_string(json_variant_type(value)), json_variant_type_to_string(p->type)); + "Object field '%s' has wrong type %s, expected %s.", sd_json_variant_string(key), + sd_json_variant_type_to_string(sd_json_variant_type(value)), sd_json_variant_type_to_string(p->type)); - if (merged_flags & JSON_PERMISSIVE) + if (merged_flags & SD_JSON_PERMISSIVE) continue; if (reterr_bad_field) @@ -4595,9 +4669,9 @@ int json_dispatch_full( } if (found[p-table]) { - json_log(value, merged_flags, 0, "Duplicate object field '%s'.", json_variant_string(key)); + json_log(value, merged_flags, 0, "Duplicate object field '%s'.", sd_json_variant_string(key)); - if (merged_flags & JSON_PERMISSIVE) + if (merged_flags & SD_JSON_PERMISSIVE) continue; if (reterr_bad_field) @@ -4609,13 +4683,13 @@ int json_dispatch_full( found[p-table] = true; if (p->callback) { - r = p->callback(json_variant_string(key), value, merged_flags, dispatch_userdata(p, userdata)); + r = p->callback(sd_json_variant_string(key), value, merged_flags, dispatch_userdata(p, userdata)); if (r < 0) { - if (merged_flags & JSON_PERMISSIVE) + if (merged_flags & SD_JSON_PERMISSIVE) continue; if (reterr_bad_field) - *reterr_bad_field = json_variant_string(key); + *reterr_bad_field = sd_json_variant_string(key); return r; } @@ -4626,43 +4700,43 @@ int json_dispatch_full( } else { /* Didn't find a matching entry! ☹️ */ if (bad) { - r = bad(json_variant_string(key), value, flags, userdata); + r = bad(sd_json_variant_string(key), value, flags, userdata); if (r < 0) { - if (flags & JSON_PERMISSIVE) + if (flags & SD_JSON_PERMISSIVE) continue; if (reterr_bad_field) - *reterr_bad_field = json_variant_string(key); + *reterr_bad_field = sd_json_variant_string(key); return r; } else done++; } else { - if (flags & JSON_ALLOW_EXTENSIONS) { - json_log(value, flags|JSON_DEBUG, 0, "Unrecognized object field '%s', assuming extension.", json_variant_string(key)); + if (flags & SD_JSON_ALLOW_EXTENSIONS) { + json_log(value, flags|SD_JSON_DEBUG, 0, "Unrecognized object field '%s', assuming extension.", sd_json_variant_string(key)); continue; } - json_log(value, flags, 0, "Unexpected object field '%s'.", json_variant_string(key)); - if (flags & JSON_PERMISSIVE) + json_log(value, flags, 0, "Unexpected object field '%s'.", sd_json_variant_string(key)); + if (flags & SD_JSON_PERMISSIVE) continue; if (reterr_bad_field) - *reterr_bad_field = json_variant_string(key); + *reterr_bad_field = sd_json_variant_string(key); return -EADDRNOTAVAIL; } } } - for (const JsonDispatch *p = table; p->name; p++) { - JsonDispatchFlags merged_flags = p->flags | flags; + for (const sd_json_dispatch_field *p = table; p->name; p++) { + sd_json_dispatch_flags_t merged_flags = p->flags | flags; - if ((merged_flags & JSON_MANDATORY) && !found[p-table]) { + if ((merged_flags & SD_JSON_MANDATORY) && !found[p-table]) { json_log(v, merged_flags, 0, "Missing object field '%s'.", p->name); - if ((merged_flags & JSON_PERMISSIVE)) + if ((merged_flags & SD_JSON_PERMISSIVE)) continue; if (reterr_bad_field) @@ -4675,80 +4749,101 @@ int json_dispatch_full( return done; } -int json_dispatch_boolean(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch( + sd_json_variant *v, + const sd_json_dispatch_field table[], + sd_json_dispatch_flags_t flags, + void *userdata) { + + return sd_json_dispatch_full(v, table, NULL, flags, userdata, NULL); +} + +_public_ int sd_json_dispatch_stdbool(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { bool *b = ASSERT_PTR(userdata); assert(variant); - if (!json_variant_is_boolean(variant)) + if (!sd_json_variant_is_boolean(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a boolean.", strna(name)); - *b = json_variant_boolean(variant); + *b = sd_json_variant_boolean(variant); return 0; } -int json_dispatch_tristate(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_intbool(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int *b = ASSERT_PTR(userdata); assert(variant); - if (json_variant_is_null(variant)) { + if (!sd_json_variant_is_boolean(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a boolean.", strna(name)); + + *b = sd_json_variant_boolean(variant); + return 0; +} + +_public_ int sd_json_dispatch_tristate(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + int *b = ASSERT_PTR(userdata); + + assert(variant); + + if (sd_json_variant_is_null(variant)) { *b = -1; return 0; } - if (!json_variant_is_boolean(variant)) + if (!sd_json_variant_is_boolean(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a boolean.", strna(name)); - *b = json_variant_boolean(variant); + *b = sd_json_variant_boolean(variant); return 0; } -int json_dispatch_int64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_int64(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int64_t *i = ASSERT_PTR(userdata); assert(variant); /* Also accept numbers formatted as string, to increase compatibility with less capable JSON * implementations that cannot do 64bit integers. */ - if (json_variant_is_string(variant) && safe_atoi64(json_variant_string(variant), i) >= 0) + if (sd_json_variant_is_string(variant) && safe_atoi64(sd_json_variant_string(variant), i) >= 0) return 0; - if (!json_variant_is_integer(variant)) + if (!sd_json_variant_is_integer(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer, nor one formatted as decimal string.", strna(name)); - *i = json_variant_integer(variant); + *i = sd_json_variant_integer(variant); return 0; } -int json_dispatch_uint64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_uint64(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uint64_t *u = ASSERT_PTR(userdata); assert(variant); /* Since 64bit values (in particular unsigned ones) in JSON are problematic, let's also accept them - * formatted as strings. If this is not desired make sure to set the .type field in JsonDispatch to - * JSON_UNSIGNED rather than _JSON_VARIANT_TYPE_INVALID, so that json_dispatch() already filters out - * the non-matching type. */ + * formatted as strings. If this is not desired make sure to set the .type field in + * sd_json_dispatch_field to SD_JSON_UNSIGNED rather than _SD_JSON_VARIANT_TYPE_INVALID, so that + * json_dispatch() already filters out the non-matching type. */ - if (json_variant_is_string(variant) && safe_atou64(json_variant_string(variant), u) >= 0) + if (sd_json_variant_is_string(variant) && safe_atou64(sd_json_variant_string(variant), u) >= 0) return 0; - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an unsigned integer, nor one formatted as decimal string.", strna(name)); - *u = json_variant_unsigned(variant); + *u = sd_json_variant_unsigned(variant); return 0; } -int json_dispatch_uint32(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_uint32(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uint32_t *u = ASSERT_PTR(userdata); uint64_t u64; int r; assert(variant); - r = json_dispatch_uint64(name, variant, flags, &u64); + r = sd_json_dispatch_uint64(name, variant, flags, &u64); if (r < 0) return r; @@ -4759,14 +4854,17 @@ int json_dispatch_uint32(const char *name, JsonVariant *variant, JsonDispatchFla return 0; } -int json_dispatch_int32(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +/* We define sd_json_dispatch_uint() as alias for json_dispatch_uint32(), let's make sure this is safe */ +assert_cc(sizeof(uint32_t) == sizeof(unsigned)); + +_public_ int sd_json_dispatch_int32(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int32_t *i = ASSERT_PTR(userdata); int64_t i64; int r; assert(variant); - r = json_dispatch_int64(name, variant, flags, &i64); + r = sd_json_dispatch_int64(name, variant, flags, &i64); if (r < 0) return r; @@ -4777,14 +4875,17 @@ int json_dispatch_int32(const char *name, JsonVariant *variant, JsonDispatchFlag return 0; } -int json_dispatch_int16(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +/* We define sd_json_dispatch_int() as alias for json_dispatch_int32(), let's make sure this is safe */ +assert_cc(sizeof(int32_t) == sizeof(int)); + +_public_ int sd_json_dispatch_int16(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int16_t *i = ASSERT_PTR(userdata); int64_t i64; int r; assert(variant); - r = json_dispatch_int64(name, variant, flags, &i64); + r = sd_json_dispatch_int64(name, variant, flags, &i64); if (r < 0) return r; @@ -4795,14 +4896,14 @@ int json_dispatch_int16(const char *name, JsonVariant *variant, JsonDispatchFlag return 0; } -int json_dispatch_uint16(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_uint16(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uint16_t *u = ASSERT_PTR(userdata); uint64_t u64; int r; assert(variant); - r = json_dispatch_uint64(name, variant, flags, &u64); + r = sd_json_dispatch_uint64(name, variant, flags, &u64); if (r < 0) return r; @@ -4813,14 +4914,14 @@ int json_dispatch_uint16(const char *name, JsonVariant *variant, JsonDispatchFla return 0; } -int json_dispatch_int8(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_int8(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int8_t *i = ASSERT_PTR(userdata); int64_t i64; int r; assert(variant); - r = json_dispatch_int64(name, variant, flags, &i64); + r = sd_json_dispatch_int64(name, variant, flags, &i64); if (r < 0) return r; @@ -4831,14 +4932,14 @@ int json_dispatch_int8(const char *name, JsonVariant *variant, JsonDispatchFlags return 0; } -int json_dispatch_uint8(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_uint8(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uint8_t *u = ASSERT_PTR(userdata); uint64_t u64; int r; assert(variant); - r = json_dispatch_uint64(name, variant, flags, &u64); + r = sd_json_dispatch_uint64(name, variant, flags, &u64); if (r < 0) return r; @@ -4849,69 +4950,69 @@ int json_dispatch_uint8(const char *name, JsonVariant *variant, JsonDispatchFlag return 0; } -int json_dispatch_string(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_string(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **s = ASSERT_PTR(userdata); int r; assert(variant); - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = mfree(*s); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - if ((flags & JSON_SAFE) && !string_is_safe(json_variant_string(variant))) + if ((flags & SD_JSON_SAFE) && !string_is_safe(sd_json_variant_string(variant))) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' contains unsafe characters, refusing.", strna(name)); - r = free_and_strdup(s, json_variant_string(variant)); + r = free_and_strdup(s, sd_json_variant_string(variant)); if (r < 0) return json_log(variant, flags, r, "Failed to allocate string: %m"); return 0; } -int json_dispatch_const_string(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_const_string(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { const char **s = ASSERT_PTR(userdata); assert(variant); - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = NULL; return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - if ((flags & JSON_SAFE) && !string_is_safe(json_variant_string(variant))) + if ((flags & SD_JSON_SAFE) && !string_is_safe(sd_json_variant_string(variant))) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' contains unsafe characters, refusing.", strna(name)); - *s = json_variant_string(variant); + *s = sd_json_variant_string(variant); return 0; } -int json_dispatch_strv(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_strv(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_strv_free_ char **l = NULL; char ***s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; assert(variant); - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = strv_free(*s); return 0; } /* Let's be flexible here: accept a single string in place of a single-item array */ - if (json_variant_is_string(variant)) { - if ((flags & JSON_SAFE) && !string_is_safe(json_variant_string(variant))) + if (sd_json_variant_is_string(variant)) { + if ((flags & SD_JSON_SAFE) && !string_is_safe(sd_json_variant_string(variant))) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' contains unsafe characters, refusing.", strna(name)); - l = strv_new(json_variant_string(variant)); + l = strv_new(sd_json_variant_string(variant)); if (!l) return log_oom(); @@ -4919,17 +5020,17 @@ int json_dispatch_strv(const char *name, JsonVariant *variant, JsonDispatchFlags return 0; } - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, SYNTHETIC_ERRNO(EINVAL), flags, "JSON field '%s' is not an array.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string."); - if ((flags & JSON_SAFE) && !string_is_safe(json_variant_string(e))) + if ((flags & SD_JSON_SAFE) && !string_is_safe(sd_json_variant_string(e))) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' contains unsafe characters, refusing.", strna(name)); - r = strv_extend(&l, json_variant_string(e)); + r = strv_extend(&l, sd_json_variant_string(e)); if (r < 0) return json_log(e, flags, r, "Failed to append array element: %m"); } @@ -4938,17 +5039,17 @@ int json_dispatch_strv(const char *name, JsonVariant *variant, JsonDispatchFlags return 0; } -int json_dispatch_variant(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - JsonVariant **p = ASSERT_PTR(userdata); +_public_ int sd_json_dispatch_variant(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + sd_json_variant **p = ASSERT_PTR(userdata); assert(variant); /* Takes a reference */ - JSON_VARIANT_REPLACE(*p, json_variant_ref(variant)); + JSON_VARIANT_REPLACE(*p, sd_json_variant_ref(variant)); return 0; } -int json_dispatch_variant_noref(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - JsonVariant **p = ASSERT_PTR(userdata); +_public_ int sd_json_dispatch_variant_noref(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + sd_json_variant **p = ASSERT_PTR(userdata); assert(variant); /* Doesn't take a reference */ @@ -4956,7 +5057,7 @@ int json_dispatch_variant_noref(const char *name, JsonVariant *variant, JsonDisp return 0; } -int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_uid_gid(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uid_t *uid = userdata; uint64_t k; @@ -4967,15 +5068,15 @@ int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFl assert_cc((UID_INVALID < (uid_t) 0) == (GID_INVALID < (gid_t) 0)); REENABLE_WARNING; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *uid = UID_INVALID; return 0; } - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - k = json_variant_unsigned(variant); + k = sd_json_variant_unsigned(variant); if (k > UINT32_MAX || !uid_is_valid(k)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid UID/GID.", strna(name)); @@ -4983,190 +5084,65 @@ int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFl return 0; } -int json_dispatch_user_group_name(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - char **s = userdata; - const char *n; - int r; - - if (json_variant_is_null(variant)) { - *s = mfree(*s); - return 0; - } - - if (!json_variant_is_string(variant)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - - n = json_variant_string(variant); - if (!valid_user_group_name(n, FLAGS_SET(flags, JSON_RELAX) ? VALID_USER_RELAX : 0)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid user/group name.", strna(name)); - - r = free_and_strdup(s, n); - if (r < 0) - return json_log(variant, flags, r, "Failed to allocate string: %m"); - - return 0; -} - -int json_dispatch_path(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - const char *path; - char **p = ASSERT_PTR(userdata); - - assert(variant); - - if (json_variant_is_null(variant)) { - *p = mfree(*p); - return 0; - } - - if (!json_variant_is_string(variant)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - - path = json_variant_string(variant); - if (!(FLAGS_SET(flags, JSON_SAFE) ? path_is_normalized : path_is_valid)(path)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid path.", strna(name)); - if (!path_is_absolute(path)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' must be an absolute path.", strna(name)); - - if (free_and_strdup(p, path) < 0) - return json_log_oom(variant, flags); - - return 0; -} - -int json_dispatch_id128(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_id128(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { sd_id128_t *uuid = userdata; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *uuid = SD_ID128_NULL; return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - r = sd_id128_from_string(json_variant_string(variant), uuid); + r = sd_id128_from_string(sd_json_variant_string(variant), uuid); if (r < 0) return json_log(variant, flags, r, "JSON field '%s' is not a valid UID.", strna(name)); return 0; } -int json_dispatch_unsupported(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +_public_ int sd_json_dispatch_unsupported(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not allowed in this object.", strna(name)); } -int json_dispatch_unbase64_iovec(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - _cleanup_free_ void *buffer = NULL; - struct iovec *iov = ASSERT_PTR(userdata); - size_t sz; - int r; - - if (!json_variant_is_string(variant)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - - r = json_variant_unbase64(variant, &buffer, &sz); - if (r < 0) - return json_log(variant, flags, r, "JSON field '%s' is not valid Base64 data.", strna(name)); - - free_and_replace(iov->iov_base, buffer); - iov->iov_len = sz; - return 0; -} - -int json_dispatch_byte_array_iovec(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - _cleanup_free_ uint8_t *buffer = NULL; - struct iovec *iov = ASSERT_PTR(userdata); - size_t sz, k = 0; - - assert(variant); - - if (!json_variant_is_array(variant)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); - - sz = json_variant_elements(variant); - - buffer = new(uint8_t, sz + 1); - if (!buffer) - return json_log(variant, flags, SYNTHETIC_ERRNO(ENOMEM), "Out of memory."); - - JsonVariant *i; - JSON_VARIANT_ARRAY_FOREACH(i, variant) { - uint64_t b; - - if (!json_variant_is_unsigned(i)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an unsigned integer.", k, strna(name)); - - b = json_variant_unsigned(i); - if (b > 0xff) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), - "Element %zu of JSON field '%s' is out of range 0%s255.", - k, strna(name), special_glyph(SPECIAL_GLYPH_ELLIPSIS)); - - buffer[k++] = (uint8_t) b; - } - assert(k == sz); - - /* Append a NUL byte for safety, like we do in memdup_suffix0() and others. */ - buffer[sz] = 0; - - free_and_replace(iov->iov_base, buffer); - iov->iov_len = sz; - return 0; -} - -int json_dispatch_in_addr(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - struct in_addr *address = ASSERT_PTR(userdata); - _cleanup_(iovec_done) struct iovec iov = {}; - int r; - - r = json_dispatch_byte_array_iovec(name, variant, flags, &iov); - if (r < 0) - return r; - - if (iov.iov_len != sizeof(struct in_addr)) - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name)); - - memcpy(address, iov.iov_base, iov.iov_len); - return 0; -} - static int json_cmp_strings(const void *x, const void *y) { - JsonVariant *const *a = x, *const *b = y; + sd_json_variant *const *a = x, *const *b = y; - if (!json_variant_is_string(*a) || !json_variant_is_string(*b)) + if (!sd_json_variant_is_string(*a) || !sd_json_variant_is_string(*b)) return CMP(*a, *b); - return strcmp(json_variant_string(*a), json_variant_string(*b)); + return strcmp(sd_json_variant_string(*a), sd_json_variant_string(*b)); } -int json_variant_sort(JsonVariant **v) { - _cleanup_free_ JsonVariant **a = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *n = NULL; +_public_ int sd_json_variant_sort(sd_json_variant **v) { + _cleanup_free_ sd_json_variant **a = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *n = NULL; size_t m; int r; assert(v); - if (json_variant_is_sorted(*v)) + if (sd_json_variant_is_sorted(*v)) return 0; - if (!json_variant_is_object(*v)) + if (!sd_json_variant_is_object(*v)) return -EMEDIUMTYPE; /* Sorts they key/value pairs in an object variant */ - m = json_variant_elements(*v); - a = new(JsonVariant*, m); + m = sd_json_variant_elements(*v); + a = new(sd_json_variant*, m); if (!a) return -ENOMEM; for (size_t i = 0; i < m; i++) - a[i] = json_variant_by_index(*v, i); + a[i] = sd_json_variant_by_index(*v, i); - qsort(a, m/2, sizeof(JsonVariant*)*2, json_cmp_strings); + qsort(a, m/2, sizeof(sd_json_variant*)*2, json_cmp_strings); - r = json_variant_new_object(&n, a, m); + r = sd_json_variant_new_object(&n, a, m); if (r < 0) return r; @@ -5180,43 +5156,43 @@ int json_variant_sort(JsonVariant **v) { return 1; } -int json_variant_normalize(JsonVariant **v) { - _cleanup_free_ JsonVariant **a = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *n = NULL; +_public_ int sd_json_variant_normalize(sd_json_variant **v) { + _cleanup_free_ sd_json_variant **a = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *n = NULL; size_t i, m; int r; assert(v); - if (json_variant_is_normalized(*v)) + if (sd_json_variant_is_normalized(*v)) return 0; - if (!json_variant_is_object(*v) && !json_variant_is_array(*v)) + if (!sd_json_variant_is_object(*v) && !sd_json_variant_is_array(*v)) return -EMEDIUMTYPE; /* Sorts the key/value pairs in an object variant anywhere down the tree in the specified variant */ - m = json_variant_elements(*v); - a = new(JsonVariant*, m); + m = sd_json_variant_elements(*v); + a = new(sd_json_variant*, m); if (!a) return -ENOMEM; for (i = 0; i < m; ) { - a[i] = json_variant_ref(json_variant_by_index(*v, i)); + a[i] = sd_json_variant_ref(sd_json_variant_by_index(*v, i)); i++; - r = json_variant_normalize(&a[i-1]); + r = sd_json_variant_normalize(&a[i-1]); if (r < 0) goto finish; } - qsort(a, m/2, sizeof(JsonVariant*)*2, json_cmp_strings); + qsort(a, m/2, sizeof(sd_json_variant*)*2, json_cmp_strings); - if (json_variant_is_object(*v)) - r = json_variant_new_object(&n, a, m); + if (sd_json_variant_is_object(*v)) + r = sd_json_variant_new_object(&n, a, m); else { - assert(json_variant_is_array(*v)); - r = json_variant_new_array(&n, a, m); + assert(sd_json_variant_is_array(*v)); + r = sd_json_variant_new_array(&n, a, m); } if (r < 0) goto finish; @@ -5236,67 +5212,67 @@ int json_variant_normalize(JsonVariant **v) { finish: for (size_t j = 0; j < i; j++) - json_variant_unref(a[j]); + sd_json_variant_unref(a[j]); return r; } -bool json_variant_is_normalized(JsonVariant *v) { +_public_ int sd_json_variant_is_normalized(sd_json_variant *v) { /* For now, let's consider anything containing numbers not expressible as integers as non-normalized. * That's because we cannot sensibly compare them due to accuracy issues, nor even store them if they * are too large. */ - if (json_variant_is_real(v) && !json_variant_is_integer(v) && !json_variant_is_unsigned(v)) + if (sd_json_variant_is_real(v) && !sd_json_variant_is_integer(v) && !sd_json_variant_is_unsigned(v)) return false; /* The concept only applies to variants that include other variants, i.e. objects and arrays. All * others are normalized anyway. */ - if (!json_variant_is_object(v) && !json_variant_is_array(v)) + if (!sd_json_variant_is_object(v) && !sd_json_variant_is_array(v)) return true; /* Empty objects/arrays don't include any other variant, hence are always normalized too */ - if (json_variant_elements(v) == 0) + if (sd_json_variant_elements(v) == 0) return true; return v->normalized; /* For everything else there's an explicit boolean we maintain */ } -bool json_variant_is_sorted(JsonVariant *v) { +_public_ int sd_json_variant_is_sorted(sd_json_variant *v) { /* Returns true if all key/value pairs of an object are properly sorted. Note that this only applies * to objects, not arrays. */ - if (!json_variant_is_object(v)) + if (!sd_json_variant_is_object(v)) return true; - if (json_variant_elements(v) <= 1) + if (sd_json_variant_elements(v) <= 1) return true; return v->sorted; } -int json_variant_unbase64(JsonVariant *v, void **ret, size_t *ret_size) { - if (!json_variant_is_string(v)) +_public_ int sd_json_variant_unbase64(sd_json_variant *v, void **ret, size_t *ret_size) { + if (!sd_json_variant_is_string(v)) return -EINVAL; - return unbase64mem_full(json_variant_string(v), SIZE_MAX, /* secure= */ json_variant_is_sensitive(v), ret, ret_size); + return unbase64mem_full(sd_json_variant_string(v), SIZE_MAX, /* secure= */ sd_json_variant_is_sensitive(v), ret, ret_size); } -int json_variant_unhex(JsonVariant *v, void **ret, size_t *ret_size) { - if (!json_variant_is_string(v)) +_public_ int sd_json_variant_unhex(sd_json_variant *v, void **ret, size_t *ret_size) { + if (!sd_json_variant_is_string(v)) return -EINVAL; - return unhexmem_full(json_variant_string(v), SIZE_MAX, /* secure= */ json_variant_is_sensitive(v), ret, ret_size); + return unhexmem_full(sd_json_variant_string(v), SIZE_MAX, /* secure= */ sd_json_variant_is_sensitive(v), ret, ret_size); } -static const char* const json_variant_type_table[_JSON_VARIANT_TYPE_MAX] = { - [JSON_VARIANT_STRING] = "string", - [JSON_VARIANT_INTEGER] = "integer", - [JSON_VARIANT_UNSIGNED] = "unsigned", - [JSON_VARIANT_REAL] = "real", - [JSON_VARIANT_NUMBER] = "number", - [JSON_VARIANT_BOOLEAN] = "boolean", - [JSON_VARIANT_ARRAY] = "array", - [JSON_VARIANT_OBJECT] = "object", - [JSON_VARIANT_NULL] = "null", +static const char* const sd_json_variant_type_table[_SD_JSON_VARIANT_TYPE_MAX] = { + [SD_JSON_VARIANT_STRING] = "string", + [SD_JSON_VARIANT_INTEGER] = "integer", + [SD_JSON_VARIANT_UNSIGNED] = "unsigned", + [SD_JSON_VARIANT_REAL] = "real", + [SD_JSON_VARIANT_NUMBER] = "number", + [SD_JSON_VARIANT_BOOLEAN] = "boolean", + [SD_JSON_VARIANT_ARRAY] = "array", + [SD_JSON_VARIANT_OBJECT] = "object", + [SD_JSON_VARIANT_NULL] = "null", }; -DEFINE_STRING_TABLE_LOOKUP(json_variant_type, JsonVariantType); +DEFINE_STRING_TABLE_LOOKUP(sd_json_variant_type, sd_json_variant_type_t); diff --git a/src/login/loginctl.c b/src/login/loginctl.c index cf3bff437a..36421da0ad 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -44,7 +44,7 @@ static BusPrintPropertyFlags arg_print_flags = 0; static bool arg_full = false; static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static const char *arg_kill_whom = NULL; static int arg_signal = SIGTERM; static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; @@ -1621,7 +1621,7 @@ static int parse_argv(int argc, char *argv[]) { break; case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; arg_legend = false; break; @@ -1630,7 +1630,7 @@ static int parse_argv(int argc, char *argv[]) { if (r <= 0) return r; - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) arg_legend = false; break; diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c index a711c89b12..40721ebfd9 100644 --- a/src/login/pam_systemd.c +++ b/src/login/pam_systemd.c @@ -196,10 +196,10 @@ static int acquire_user_record( if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM user record data: @PAMERR@"); if (r == PAM_SUCCESS && json) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; /* Parse cached record */ - r = json_parse(json, JSON_PARSE_SENSITIVE, &v, NULL, NULL); + r = sd_json_parse(json, SD_JSON_PARSE_SENSITIVE, &v, NULL, NULL); if (r < 0) return pam_syslog_errno(handle, LOG_ERR, r, "Failed to parse JSON user record: %m"); @@ -225,7 +225,7 @@ static int acquire_user_record( return PAM_USER_UNKNOWN; } - r = json_variant_format(ur->json, 0, &formatted); + r = sd_json_variant_format(ur->json, 0, &formatted); if (r < 0) return pam_syslog_errno(handle, LOG_ERR, r, "Failed to format user JSON: %m"); diff --git a/src/machine/machine-varlink.c b/src/machine/machine-varlink.c index 3f95388abc..a6bf6528c4 100644 --- a/src/machine/machine-varlink.c +++ b/src/machine/machine-varlink.c @@ -3,9 +3,10 @@ #include <limits.h> #include "sd-id128.h" +#include "sd-json.h" #include "hostname-util.h" -#include "json.h" +#include "json-util.h" #include "machine-varlink.h" #include "machine.h" #include "path-util.h" @@ -17,17 +18,17 @@ static JSON_DISPATCH_ENUM_DEFINE(dispatch_machine_class, MachineClass, machine_class_from_string); -static int machine_name(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int machine_name(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **m = ASSERT_PTR(userdata); const char *hostname; int r; assert(variant); - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - hostname = json_variant_string(variant); + hostname = sd_json_variant_string(variant); if (!hostname_is_valid(hostname, /* flags= */ 0)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Invalid machine name"); @@ -38,16 +39,16 @@ static int machine_name(const char *name, JsonVariant *variant, JsonDispatchFlag return 0; } -static int machine_leader(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int machine_leader(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { PidRef *leader = ASSERT_PTR(userdata); _cleanup_(pidref_done) PidRef temp = PIDREF_NULL; uint64_t k; int r; - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - k = json_variant_unsigned(variant); + k = sd_json_variant_unsigned(variant); if (k > PID_T_MAX || !pid_is_valid(k)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid PID.", strna(name)); @@ -65,30 +66,30 @@ static int machine_leader(const char *name, JsonVariant *variant, JsonDispatchFl return 0; } -static int machine_ifindices(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int machine_ifindices(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { Machine *m = ASSERT_PTR(userdata); _cleanup_free_ int *netif = NULL; size_t n_netif, k = 0; assert(variant); - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); - n_netif = json_variant_elements(variant); + n_netif = sd_json_variant_elements(variant); netif = new(int, n_netif); if (!netif) return json_log_oom(variant, flags); - JsonVariant *i; + sd_json_variant *i; JSON_VARIANT_ARRAY_FOREACH(i, variant) { uint64_t b; - if (!json_variant_is_unsigned(i)) + if (!sd_json_variant_is_unsigned(i)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an unsigned integer.", k, strna(name)); - b = json_variant_unsigned(i); + b = sd_json_variant_unsigned(i); if (b > INT_MAX || b <= 0) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Invalid network interface index %"PRIu64, b); @@ -102,15 +103,15 @@ static int machine_ifindices(const char *name, JsonVariant *variant, JsonDispatc return 0; } -static int machine_cid(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int machine_cid(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { unsigned cid, *c = ASSERT_PTR(userdata); assert(variant); - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - cid = json_variant_unsigned(variant); + cid = sd_json_variant_unsigned(variant); if (!VSOCK_CID_IS_REGULAR(cid)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a regular VSOCK CID.", strna(name)); @@ -119,22 +120,22 @@ static int machine_cid(const char *name, JsonVariant *variant, JsonDispatchFlags return 0; } -int vl_method_register(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +int vl_method_register(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { Manager *manager = ASSERT_PTR(userdata); _cleanup_(machine_freep) Machine *machine = NULL; int r; - static const JsonDispatch dispatch_table[] = { - { "name", JSON_VARIANT_STRING, machine_name, offsetof(Machine, name), JSON_MANDATORY }, - { "id", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(Machine, id), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_string, offsetof(Machine, service), 0 }, - { "class", JSON_VARIANT_STRING, dispatch_machine_class, offsetof(Machine, class), JSON_MANDATORY }, - { "leader", JSON_VARIANT_UNSIGNED, machine_leader, offsetof(Machine, leader), 0 }, - { "rootDirectory", JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, root_directory), 0 }, - { "ifIndices", JSON_VARIANT_ARRAY, machine_ifindices, 0, 0 }, - { "vSockCid", JSON_VARIANT_UNSIGNED, machine_cid, offsetof(Machine, vsock_cid), 0 }, - { "sshAddress", JSON_VARIANT_STRING, json_dispatch_string, offsetof(Machine, ssh_address), JSON_SAFE }, - { "sshPrivateKeyPath", JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, ssh_private_key_path), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "name", SD_JSON_VARIANT_STRING, machine_name, offsetof(Machine, name), SD_JSON_MANDATORY }, + { "id", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(Machine, id), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Machine, service), 0 }, + { "class", SD_JSON_VARIANT_STRING, dispatch_machine_class, offsetof(Machine, class), SD_JSON_MANDATORY }, + { "leader", SD_JSON_VARIANT_UNSIGNED, machine_leader, offsetof(Machine, leader), 0 }, + { "rootDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, root_directory), 0 }, + { "ifIndices", SD_JSON_VARIANT_ARRAY, machine_ifindices, 0, 0 }, + { "vSockCid", SD_JSON_VARIANT_UNSIGNED, machine_cid, offsetof(Machine, vsock_cid), 0 }, + { "sshAddress", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Machine, ssh_address), SD_JSON_SAFE }, + { "sshPrivateKeyPath", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, ssh_private_key_path), 0 }, {} }; diff --git a/src/machine/machine-varlink.h b/src/machine/machine-varlink.h index ce4ec54dc1..f9df0ed2a9 100644 --- a/src/machine/machine-varlink.h +++ b/src/machine/machine-varlink.h @@ -3,4 +3,4 @@ #include "varlink.h" -int vl_method_register(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); +int vl_method_register(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 1b63e6d203..5572115a54 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -34,6 +34,7 @@ #include "format-table.h" #include "hostname-util.h" #include "import-util.h" +#include "in-addr-util.h" #include "locale-util.h" #include "log.h" #include "logs-show.h" @@ -261,7 +262,7 @@ static int show_table(Table *table, const char *word) { table_set_header(table, arg_legend); if (OUTPUT_MODE_IS_JSON(arg_output)) - r = table_print_json(table, NULL, output_mode_to_json_format_flags(arg_output) | JSON_FORMAT_COLOR_AUTO); + r = table_print_json(table, NULL, output_mode_to_json_format_flags(arg_output) | SD_JSON_FORMAT_COLOR_AUTO); else r = table_print(table, NULL); if (r < 0) diff --git a/src/machine/machined-varlink.c b/src/machine/machined-varlink.c index 0d3ae627c1..3c1f20eff8 100644 --- a/src/machine/machined-varlink.c +++ b/src/machine/machined-varlink.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "format-util.h" +#include "json-util.h" #include "machine-varlink.h" #include "machined-varlink.h" #include "mkdir.h" @@ -19,22 +20,22 @@ typedef struct LookupParameters { const char *service; } LookupParameters; -static int build_user_json(const char *user_name, uid_t uid, const char *real_name, JsonVariant **ret) { +static int build_user_json(const char *user_name, uid_t uid, const char *real_name, sd_json_variant **ret) { assert(user_name); assert(uid_is_valid(uid)); assert(ret); - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(user_name)), - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(uid)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(GID_NOBODY)), - JSON_BUILD_PAIR_CONDITION(!isempty(real_name), "realName", JSON_BUILD_STRING(real_name)), - JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/")), - JSON_BUILD_PAIR("shell", JSON_BUILD_STRING(NOLOGIN)), - JSON_BUILD_PAIR("locked", JSON_BUILD_BOOLEAN(true)), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Machine")), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("container")))))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(user_name)), + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(uid)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(GID_NOBODY)), + SD_JSON_BUILD_PAIR_CONDITION(!isempty(real_name), "realName", SD_JSON_BUILD_STRING(real_name)), + SD_JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/")), + SD_JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN)), + SD_JSON_BUILD_PAIR("locked", SD_JSON_BUILD_BOOLEAN(true)), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Machine")), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("container")))))); } static bool user_match_lookup_parameters(LookupParameters *p, const char *name, uid_t uid) { @@ -138,16 +139,16 @@ static int user_lookup_name(Manager *m, const char *name, uid_t *ret_uid, char * return 0; } -static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; LookupParameters p = { .uid = UID_INVALID, }; @@ -190,18 +191,18 @@ static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, Var return varlink_reply(link, v); } -static int build_group_json(const char *group_name, gid_t gid, const char *description, JsonVariant **ret) { +static int build_group_json(const char *group_name, gid_t gid, const char *description, sd_json_variant **ret) { assert(group_name); assert(gid_is_valid(gid)); assert(ret); - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("record", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(group_name)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(gid)), - JSON_BUILD_PAIR_CONDITION(!isempty(description), "description", JSON_BUILD_STRING(description)), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Machine")), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("container")))))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(group_name)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(gid)), + SD_JSON_BUILD_PAIR_CONDITION(!isempty(description), "description", SD_JSON_BUILD_STRING(description)), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.Machine")), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("container")))))); } static bool group_match_lookup_parameters(LookupParameters *p, const char *name, gid_t gid) { @@ -303,16 +304,16 @@ static int group_lookup_name(Manager *m, const char *name, gid_t *ret_gid, char return 0; } -static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; LookupParameters p = { .gid = GID_INVALID, }; @@ -355,12 +356,12 @@ static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, Va return varlink_reply(link, v); } -static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), JSON_SAFE }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), SD_JSON_SAFE }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; diff --git a/src/mountfsd/mountwork.c b/src/mountfsd/mountwork.c index 1d218a6b03..27d9cc3d2d 100644 --- a/src/mountfsd/mountwork.c +++ b/src/mountfsd/mountwork.c @@ -11,6 +11,7 @@ #include "errno-util.h" #include "fd-util.h" #include "io-util.h" +#include "json-util.h" #include "main-func.h" #include "missing_loop.h" #include "namespace-util.h" @@ -37,22 +38,22 @@ static const ImagePolicy image_policy_untrusted = { .default_flags = PARTITION_POLICY_IGNORE, }; -static int json_dispatch_image_policy(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_image_policy(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_(image_policy_freep) ImagePolicy *q = NULL; ImagePolicy **p = ASSERT_PTR(userdata); int r; assert(p); - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *p = image_policy_free(*p); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - r = image_policy_from_string(json_variant_string(variant), &q); + r = image_policy_from_string(sd_json_variant_string(variant), &q); if (r < 0) return json_log(variant, flags, r, "JSON field '%s' is not a valid image policy.", strna(name)); @@ -245,17 +246,17 @@ static int validate_userns(Varlink *link, int *userns_fd) { static int vl_method_mount_image( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "imageFileDescriptor", JSON_VARIANT_UNSIGNED, json_dispatch_uint, offsetof(MountImageParameters, image_fd_idx), JSON_MANDATORY }, - { "userNamespaceFileDescriptor", JSON_VARIANT_UNSIGNED, json_dispatch_uint, offsetof(MountImageParameters, userns_fd_idx), 0 }, - { "readOnly", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(MountImageParameters, read_only), 0 }, - { "growFileSystems", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(MountImageParameters, growfs), 0 }, - { "password", JSON_VARIANT_STRING, json_dispatch_string, offsetof(MountImageParameters, password), 0 }, - { "imagePolicy", JSON_VARIANT_STRING, json_dispatch_image_policy, offsetof(MountImageParameters, image_policy), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "imageFileDescriptor", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint, offsetof(MountImageParameters, image_fd_idx), SD_JSON_MANDATORY }, + { "userNamespaceFileDescriptor", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint, offsetof(MountImageParameters, userns_fd_idx), 0 }, + { "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(MountImageParameters, read_only), 0 }, + { "growFileSystems", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(MountImageParameters, growfs), 0 }, + { "password", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(MountImageParameters, password), 0 }, + { "imagePolicy", SD_JSON_VARIANT_STRING, json_dispatch_image_policy, offsetof(MountImageParameters, image_policy), 0 }, VARLINK_DISPATCH_POLKIT_FIELD, {} }; @@ -269,7 +270,7 @@ static int vl_method_mount_image( }; _cleanup_(dissected_image_unrefp) DissectedImage *di = NULL; _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *aj = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *aj = NULL; _cleanup_close_ int image_fd = -EBADF, userns_fd = -EBADF; _cleanup_(image_policy_freep) ImagePolicy *use_policy = NULL; Hashmap **polkit_registry = ASSERT_PTR(userdata); @@ -281,7 +282,7 @@ static int vl_method_mount_image( assert(link); assert(parameters); - json_variant_sensitive(parameters); /* might contain passwords */ + sd_json_variant_sensitive(parameters); /* might contain passwords */ r = varlink_get_peer_uid(link, &peer_uid); if (r < 0) @@ -486,7 +487,7 @@ static int vl_method_mount_image( return r; for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) { - _cleanup_(json_variant_unrefp) JsonVariant *pj = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *pj = NULL; DissectedPartition *pp = di->partitions + d; int fd_idx; @@ -508,35 +509,35 @@ static int vl_method_mount_image( TAKE_FD(pp->fsmount_fd); - r = json_build(&pj, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("designator", JSON_BUILD_STRING(partition_designator_to_string(d))), - JSON_BUILD_PAIR("writable", JSON_BUILD_BOOLEAN(pp->rw)), - JSON_BUILD_PAIR("growFileSystem", JSON_BUILD_BOOLEAN(pp->growfs)), - JSON_BUILD_PAIR_CONDITION(pp->partno > 0, "partitionNumber", JSON_BUILD_INTEGER(pp->partno)), - JSON_BUILD_PAIR_CONDITION(pp->architecture > 0, "architecture", JSON_BUILD_STRING(architecture_to_string(pp->architecture))), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(pp->uuid), "partitionUuid", JSON_BUILD_UUID(pp->uuid)), - JSON_BUILD_PAIR("fileSystemType", JSON_BUILD_STRING(dissected_partition_fstype(pp))), - JSON_BUILD_PAIR_CONDITION(pp->label, "partitionLabel", JSON_BUILD_STRING(pp->label)), - JSON_BUILD_PAIR("size", JSON_BUILD_INTEGER(pp->size)), - JSON_BUILD_PAIR("offset", JSON_BUILD_INTEGER(pp->offset)), - JSON_BUILD_PAIR("mountFileDescriptor", JSON_BUILD_INTEGER(fd_idx)))); + r = sd_json_build(&pj, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("designator", SD_JSON_BUILD_STRING(partition_designator_to_string(d))), + SD_JSON_BUILD_PAIR("writable", SD_JSON_BUILD_BOOLEAN(pp->rw)), + SD_JSON_BUILD_PAIR("growFileSystem", SD_JSON_BUILD_BOOLEAN(pp->growfs)), + SD_JSON_BUILD_PAIR_CONDITION(pp->partno > 0, "partitionNumber", SD_JSON_BUILD_INTEGER(pp->partno)), + SD_JSON_BUILD_PAIR_CONDITION(pp->architecture > 0, "architecture", SD_JSON_BUILD_STRING(architecture_to_string(pp->architecture))), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(pp->uuid), "partitionUuid", SD_JSON_BUILD_UUID(pp->uuid)), + SD_JSON_BUILD_PAIR("fileSystemType", SD_JSON_BUILD_STRING(dissected_partition_fstype(pp))), + SD_JSON_BUILD_PAIR_CONDITION(!!pp->label, "partitionLabel", SD_JSON_BUILD_STRING(pp->label)), + SD_JSON_BUILD_PAIR("size", SD_JSON_BUILD_INTEGER(pp->size)), + SD_JSON_BUILD_PAIR("offset", SD_JSON_BUILD_INTEGER(pp->offset)), + SD_JSON_BUILD_PAIR("mountFileDescriptor", SD_JSON_BUILD_INTEGER(fd_idx)))); if (r < 0) return r; - r = json_variant_append_array(&aj, pj); + r = sd_json_variant_append_array(&aj, pj); if (r < 0) return r; } loop_device_relinquish(loop); - r = varlink_replyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("partitions", JSON_BUILD_VARIANT(aj)), - JSON_BUILD_PAIR("imagePolicy", JSON_BUILD_STRING(ps)), - JSON_BUILD_PAIR("imageSize", JSON_BUILD_INTEGER(di->image_size)), - JSON_BUILD_PAIR("sectorSize", JSON_BUILD_INTEGER(di->sector_size)), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(di->image_uuid), "imageUuid", JSON_BUILD_UUID(di->image_uuid)))); + r = varlink_replyb(link, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("partitions", SD_JSON_BUILD_VARIANT(aj)), + SD_JSON_BUILD_PAIR("imagePolicy", SD_JSON_BUILD_STRING(ps)), + SD_JSON_BUILD_PAIR("imageSize", SD_JSON_BUILD_INTEGER(di->image_size)), + SD_JSON_BUILD_PAIR("sectorSize", SD_JSON_BUILD_INTEGER(di->sector_size)), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(di->image_uuid), "imageUuid", SD_JSON_BUILD_UUID(di->image_uuid)))); if (r < 0) return r; diff --git a/src/network/networkctl.c b/src/network/networkctl.c index a447c39a64..1384a83216 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -16,6 +16,7 @@ #include "sd-device.h" #include "sd-dhcp-client.h" #include "sd-hwdb.h" +#include "sd-json.h" #include "sd-netlink.h" #include "sd-network.h" @@ -39,6 +40,7 @@ #include "hwdb-util.h" #include "ipvlan-util.h" #include "journal-internal.h" +#include "json-util.h" #include "local-addresses.h" #include "locale-util.h" #include "logs-show.h" @@ -90,13 +92,13 @@ bool arg_full = false; bool arg_runtime = false; unsigned arg_lines = 10; char *arg_drop_in = NULL; -JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; STATIC_DESTRUCTOR_REGISTER(arg_drop_in, freep); static int varlink_connect_networkd(Varlink **ret_varlink) { _cleanup_(varlink_flush_close_unrefp) Varlink *vl = NULL; - JsonVariant *reply; + sd_json_variant *reply; uint64_t id; int r; @@ -114,12 +116,12 @@ static int varlink_connect_networkd(Varlink **ret_varlink) { if (r < 0) return r; - static const JsonDispatch dispatch_table[] = { - { "NamespaceId", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, 0, JSON_MANDATORY }, + static const sd_json_dispatch_field dispatch_table[] = { + { "NamespaceId", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, 0, SD_JSON_MANDATORY }, {}, }; - r = json_dispatch(reply, dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &id); + r = sd_json_dispatch(reply, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &id); if (r < 0) return r; @@ -181,7 +183,7 @@ int acquire_bus(sd_bus **ret) { return 0; } -static int get_description(sd_bus *bus, JsonVariant **ret) { +static int get_description(sd_bus *bus, sd_json_variant **ret) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; const char *text; @@ -198,7 +200,7 @@ static int get_description(sd_bus *bus, JsonVariant **ret) { if (r < 0) return bus_log_parse_error(r); - r = json_parse(text, 0, ret, NULL, NULL); + r = sd_json_parse(text, 0, ret, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse JSON: %m"); @@ -206,7 +208,7 @@ static int get_description(sd_bus *bus, JsonVariant **ret) { } static int dump_manager_description(sd_bus *bus) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(bus); @@ -215,14 +217,14 @@ static int dump_manager_description(sd_bus *bus) { if (r < 0) return r; - json_variant_dump(v, arg_json_format_flags, NULL, NULL); + sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); return 0; } static int dump_link_description(sd_bus *bus, char * const *patterns) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ bool *matched_patterns = NULL; - JsonVariant *i; + sd_json_variant *i; size_t c = 0; int r; @@ -237,23 +239,23 @@ static int dump_link_description(sd_bus *bus, char * const *patterns) { if (!matched_patterns) return log_oom(); - JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(v, "Interfaces")) { + JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(v, "Interfaces")) { char ifindex_str[DECIMAL_STR_MAX(int64_t)]; const char *name; int64_t index; size_t pos; - name = json_variant_string(json_variant_by_key(i, "Name")); - index = json_variant_integer(json_variant_by_key(i, "Index")); + name = sd_json_variant_string(sd_json_variant_by_key(i, "Name")); + index = sd_json_variant_integer(sd_json_variant_by_key(i, "Index")); xsprintf(ifindex_str, "%" PRIi64, index); if (!strv_fnmatch_full(patterns, ifindex_str, 0, &pos) && !strv_fnmatch_full(patterns, name, 0, &pos)) { bool match = false; - JsonVariant *a; + sd_json_variant *a; - JSON_VARIANT_ARRAY_FOREACH(a, json_variant_by_key(i, "AlternativeNames")) - if (strv_fnmatch_full(patterns, json_variant_string(a), 0, &pos)) { + JSON_VARIANT_ARRAY_FOREACH(a, sd_json_variant_by_key(i, "AlternativeNames")) + if (strv_fnmatch_full(patterns, sd_json_variant_string(a), 0, &pos)) { match = true; break; } @@ -263,7 +265,7 @@ static int dump_link_description(sd_bus *bus, char * const *patterns) { } matched_patterns[pos] = true; - json_variant_dump(i, arg_json_format_flags, NULL, NULL); + sd_json_variant_dump(i, arg_json_format_flags, NULL, NULL); c++; } @@ -921,7 +923,7 @@ static int list_links(int argc, char *argv[], void *userdata) { if (r < 0) return r; - if (arg_json_format_flags != JSON_FORMAT_OFF) { + if (arg_json_format_flags != SD_JSON_FORMAT_OFF) { if (arg_all || argc <= 1) return dump_manager_description(bus); else @@ -1322,7 +1324,7 @@ typedef struct InterfaceInfo { int ifindex; const char *ifname; char **altnames; - JsonVariant *v; + sd_json_variant *v; } InterfaceInfo; static void interface_info_done(InterfaceInfo *p) { @@ -1330,14 +1332,14 @@ static void interface_info_done(InterfaceInfo *p) { return; strv_free(p->altnames); - json_variant_unref(p->v); + sd_json_variant_unref(p->v); } -static const JsonDispatch interface_info_dispatch_table[] = { - { "InterfaceIndex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(InterfaceInfo, ifindex), JSON_MANDATORY }, - { "InterfaceName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(InterfaceInfo, ifname), JSON_MANDATORY }, - { "InterfaceAlternativeNames", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(InterfaceInfo, altnames), 0 }, - { "Neighbors", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(InterfaceInfo, v), 0 }, +static const sd_json_dispatch_field interface_info_dispatch_table[] = { + { "InterfaceIndex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(InterfaceInfo, ifindex), SD_JSON_MANDATORY }, + { "InterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(InterfaceInfo, ifname), SD_JSON_MANDATORY }, + { "InterfaceAlternativeNames", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(InterfaceInfo, altnames), 0 }, + { "Neighbors", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(InterfaceInfo, v), 0 }, {}, }; @@ -1350,19 +1352,19 @@ typedef struct LLDPNeighborInfo { uint16_t capabilities; } LLDPNeighborInfo; -static const JsonDispatch lldp_neighbor_dispatch_table[] = { - { "ChassisID", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, chassis_id), 0 }, - { "PortID", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, port_id), 0 }, - { "PortDescription", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, port_description), 0 }, - { "SystemName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, system_name), 0 }, - { "SystemDescription", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LLDPNeighborInfo, system_description), 0 }, - { "EnabledCapabilities", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint16, offsetof(LLDPNeighborInfo, capabilities), 0 }, +static const sd_json_dispatch_field lldp_neighbor_dispatch_table[] = { + { "ChassisID", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, chassis_id), 0 }, + { "PortID", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, port_id), 0 }, + { "PortDescription", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, port_description), 0 }, + { "SystemName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, system_name), 0 }, + { "SystemDescription", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, system_description), 0 }, + { "EnabledCapabilities", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(LLDPNeighborInfo, capabilities), 0 }, {}, }; static int dump_lldp_neighbors(Varlink *vl, Table *table, int ifindex) { _cleanup_strv_free_ char **buf = NULL; - JsonVariant *reply; + sd_json_variant *reply; int r; assert(vl); @@ -1370,26 +1372,26 @@ static int dump_lldp_neighbors(Varlink *vl, Table *table, int ifindex) { assert(ifindex > 0); r = varlink_callb_and_log(vl, "io.systemd.Network.GetLLDPNeighbors", &reply, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR_INTEGER("InterfaceIndex", ifindex))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_INTEGER("InterfaceIndex", ifindex))); if (r < 0) return r; - JsonVariant *i; - JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(reply, "Neighbors")) { + sd_json_variant *i; + JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(reply, "Neighbors")) { _cleanup_(interface_info_done) InterfaceInfo info = {}; - r = json_dispatch(i, interface_info_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &info); + r = sd_json_dispatch(i, interface_info_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &info); if (r < 0) return r; if (info.ifindex != ifindex) continue; - JsonVariant *neighbor; + sd_json_variant *neighbor; JSON_VARIANT_ARRAY_FOREACH(neighbor, info.v) { LLDPNeighborInfo neighbor_info = {}; - r = json_dispatch(neighbor, lldp_neighbor_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &neighbor_info); + r = sd_json_dispatch(neighbor, lldp_neighbor_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &neighbor_info); if (r < 0) return r; @@ -2431,7 +2433,7 @@ static int link_status(int argc, char *argv[], void *userdata) { if (r < 0) return r; - if (arg_json_format_flags != JSON_FORMAT_OFF) { + if (arg_json_format_flags != SD_JSON_FORMAT_OFF) { if (arg_all || argc <= 1) return dump_manager_description(bus); else @@ -2547,47 +2549,47 @@ static bool interface_match_pattern(const InterfaceInfo *info, char * const *pat return false; } -static int dump_lldp_neighbors_json(JsonVariant *reply, char * const *patterns) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL, *v = NULL; +static int dump_lldp_neighbors_json(sd_json_variant *reply, char * const *patterns) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL, *v = NULL; int r; assert(reply); if (strv_isempty(patterns)) - return json_variant_dump(reply, arg_json_format_flags, NULL, NULL); + return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL); /* Filter and dump the result. */ - JsonVariant *i; - JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(reply, "Neighbors")) { + sd_json_variant *i; + JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(reply, "Neighbors")) { _cleanup_(interface_info_done) InterfaceInfo info = {}; - r = json_dispatch(i, interface_info_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &info); + r = sd_json_dispatch(i, interface_info_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &info); if (r < 0) return r; if (!interface_match_pattern(&info, patterns)) continue; - r = json_variant_append_array(&array, i); + r = sd_json_variant_append_array(&array, i); if (r < 0) return log_error_errno(r, "Failed to append json variant to array: %m"); } - r = json_build(&v, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(json_variant_is_blank_array(array), "Neighbors", JSON_BUILD_EMPTY_ARRAY), - JSON_BUILD_PAIR_CONDITION(!json_variant_is_blank_array(array), "Neighbors", JSON_BUILD_VARIANT(array)))); + r = sd_json_build(&v, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(sd_json_variant_is_blank_array(array), "Neighbors", SD_JSON_BUILD_EMPTY_ARRAY), + SD_JSON_BUILD_PAIR_CONDITION(!sd_json_variant_is_blank_array(array), "Neighbors", SD_JSON_BUILD_VARIANT(array)))); if (r < 0) return log_error_errno(r, "Failed to build json varinat: %m"); - return json_variant_dump(v, arg_json_format_flags, NULL, NULL); + return sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); } static int link_lldp_status(int argc, char *argv[], void *userdata) { _cleanup_(varlink_flush_close_unrefp) Varlink *vl = NULL; _cleanup_(table_unrefp) Table *table = NULL; - JsonVariant *reply; + sd_json_variant *reply; uint64_t all = 0; TableCell *cell; size_t m = 0; @@ -2601,7 +2603,7 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { if (r < 0) return r; - if (arg_json_format_flags != JSON_FORMAT_OFF) + if (arg_json_format_flags != SD_JSON_FORMAT_OFF) return dump_lldp_neighbors_json(reply, strv_skip(argv, 1)); pager_open(arg_pager_flags); @@ -2629,22 +2631,22 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { assert_se(cell = table_get_cell(table, 0, 7)); table_set_minimum_width(table, cell, 11); - JsonVariant *i; - JSON_VARIANT_ARRAY_FOREACH(i, json_variant_by_key(reply, "Neighbors")) { + sd_json_variant *i; + JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(reply, "Neighbors")) { _cleanup_(interface_info_done) InterfaceInfo info = {}; - r = json_dispatch(i, interface_info_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &info); + r = sd_json_dispatch(i, interface_info_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &info); if (r < 0) return r; if (!interface_match_pattern(&info, strv_skip(argv, 1))) continue; - JsonVariant *neighbor; + sd_json_variant *neighbor; JSON_VARIANT_ARRAY_FOREACH(neighbor, info.v) { LLDPNeighborInfo neighbor_info = {}; - r = json_dispatch(neighbor, lldp_neighbor_dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &neighbor_info); + r = sd_json_dispatch(neighbor, lldp_neighbor_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &neighbor_info); if (r < 0) return r; @@ -2954,7 +2956,7 @@ static int verb_persistent_storage(int argc, char *argv[], void *userdata) { } return varlink_callb_and_log(vl, "io.systemd.Network.SetPersistentStorage", /* reply = */ NULL, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR_BOOLEAN("Ready", ready))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_BOOLEAN("Ready", ready))); } static int help(void) { diff --git a/src/network/networkctl.h b/src/network/networkctl.h index 46b44f7975..1765a8f83d 100644 --- a/src/network/networkctl.h +++ b/src/network/networkctl.h @@ -17,7 +17,7 @@ extern bool arg_full; extern bool arg_runtime; extern unsigned arg_lines; extern char *arg_drop_in; -extern JsonFormatFlags arg_json_format_flags; +extern sd_json_format_flags_t arg_json_format_flags; bool networkd_is_running(void); int acquire_bus(sd_bus **ret); diff --git a/src/network/networkd-json.c b/src/network/networkd-json.c index fb9f492be4..2fddd6796c 100644 --- a/src/network/networkd-json.c +++ b/src/network/networkd-json.c @@ -8,6 +8,7 @@ #include "dhcp6-lease-internal.h" #include "dns-domain.h" #include "ip-protocol-list.h" +#include "json-util.h" #include "netif-util.h" #include "networkd-address.h" #include "networkd-dhcp-common.h" @@ -22,11 +23,12 @@ #include "networkd-route.h" #include "networkd-routing-policy-rule.h" #include "sort-util.h" +#include "strv.h" #include "udev-util.h" #include "user-util.h" #include "wifi-util.h" -static int address_append_json(Address *address, JsonVariant **array) { +static int address_append_json(Address *address, sd_json_variant **array) { _cleanup_free_ char *scope = NULL, *flags = NULL, *state = NULL; int r; @@ -45,30 +47,30 @@ static int address_append_json(Address *address, JsonVariant **array) { if (r < 0) return r; - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_INTEGER("Family", address->family), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_INTEGER("Family", address->family), JSON_BUILD_PAIR_IN_ADDR("Address", &address->in_addr, address->family), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("Peer", &address->in_addr_peer, address->family), JSON_BUILD_PAIR_IN4_ADDR_NON_NULL("Broadcast", &address->broadcast), - JSON_BUILD_PAIR_UNSIGNED("PrefixLength", address->prefixlen), - JSON_BUILD_PAIR_UNSIGNED("Scope", address->scope), - JSON_BUILD_PAIR_STRING("ScopeString", scope), - JSON_BUILD_PAIR_UNSIGNED("Flags", address->flags), - JSON_BUILD_PAIR_STRING("FlagsString", flags), + SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", address->prefixlen), + SD_JSON_BUILD_PAIR_UNSIGNED("Scope", address->scope), + SD_JSON_BUILD_PAIR_STRING("ScopeString", scope), + SD_JSON_BUILD_PAIR_UNSIGNED("Flags", address->flags), + SD_JSON_BUILD_PAIR_STRING("FlagsString", flags), JSON_BUILD_PAIR_STRING_NON_EMPTY("Label", address->label), JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUSec", address->lifetime_preferred_usec), JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUsec", address->lifetime_preferred_usec), /* for backward compat */ JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUSec", address->lifetime_valid_usec), JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUsec", address->lifetime_valid_usec), /* for backward compat */ - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(address->source)), - JSON_BUILD_PAIR_STRING("ConfigState", state), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(address->source)), + SD_JSON_BUILD_PAIR_STRING("ConfigState", state), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", &address->provider, address->family))); } -static int addresses_append_json(Set *addresses, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int addresses_append_json(Set *addresses, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; Address *address; int r; @@ -83,7 +85,7 @@ static int addresses_append_json(Set *addresses, JsonVariant **v) { return json_variant_set_field_non_null(v, "Addresses", array); } -static int neighbor_append_json(Neighbor *n, JsonVariant **array) { +static int neighbor_append_json(Neighbor *n, sd_json_variant **array) { _cleanup_free_ char *state = NULL; int r; @@ -94,18 +96,18 @@ static int neighbor_append_json(Neighbor *n, JsonVariant **array) { if (r < 0) return r; - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_INTEGER("Family", n->family), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_INTEGER("Family", n->family), JSON_BUILD_PAIR_IN_ADDR("Destination", &n->in_addr, n->family), JSON_BUILD_PAIR_HW_ADDR("LinkLayerAddress", &n->ll_addr), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)), - JSON_BUILD_PAIR_STRING("ConfigState", state))); + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)), + SD_JSON_BUILD_PAIR_STRING("ConfigState", state))); } -static int neighbors_append_json(Set *neighbors, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int neighbors_append_json(Set *neighbors, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; Neighbor *neighbor; int r; @@ -120,8 +122,8 @@ static int neighbors_append_json(Set *neighbors, JsonVariant **v) { return json_variant_set_field_non_null(v, "Neighbors", array); } -static int nexthop_group_build_json(NextHop *nexthop, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int nexthop_group_build_json(NextHop *nexthop, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; struct nexthop_grp *g; int r; @@ -129,11 +131,11 @@ static int nexthop_group_build_json(NextHop *nexthop, JsonVariant **ret) { assert(ret); HASHMAP_FOREACH(g, nexthop->group) { - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("ID", g->id), - JSON_BUILD_PAIR_UNSIGNED("Weight", g->weight+1))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("ID", g->id), + SD_JSON_BUILD_PAIR_UNSIGNED("Weight", g->weight+1))); if (r < 0) return r; } @@ -142,8 +144,8 @@ static int nexthop_group_build_json(NextHop *nexthop, JsonVariant **ret) { return 0; } -static int nexthop_append_json(NextHop *n, JsonVariant **array) { - _cleanup_(json_variant_unrefp) JsonVariant *group = NULL; +static int nexthop_append_json(NextHop *n, sd_json_variant **array) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *group = NULL; _cleanup_free_ char *flags = NULL, *protocol = NULL, *state = NULL; int r; @@ -166,23 +168,23 @@ static int nexthop_append_json(NextHop *n, JsonVariant **array) { if (r < 0) return r; - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("ID", n->id), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("ID", n->id), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("Gateway", &n->gw, n->family), - JSON_BUILD_PAIR_UNSIGNED("Flags", n->flags), - JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)), - JSON_BUILD_PAIR_UNSIGNED("Protocol", n->protocol), - JSON_BUILD_PAIR_STRING("ProtocolString", protocol), - JSON_BUILD_PAIR_BOOLEAN("Blackhole", n->blackhole), + SD_JSON_BUILD_PAIR_UNSIGNED("Flags", n->flags), + SD_JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)), + SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", n->protocol), + SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol), + SD_JSON_BUILD_PAIR_BOOLEAN("Blackhole", n->blackhole), JSON_BUILD_PAIR_VARIANT_NON_NULL("Group", group), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)), - JSON_BUILD_PAIR_STRING("ConfigState", state))); + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)), + SD_JSON_BUILD_PAIR_STRING("ConfigState", state))); } -static int nexthops_append_json(Manager *manager, int ifindex, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int nexthops_append_json(Manager *manager, int ifindex, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; NextHop *nexthop; int r; @@ -201,7 +203,7 @@ static int nexthops_append_json(Manager *manager, int ifindex, JsonVariant **v) return json_variant_set_field_non_null(v, "NextHops", array); } -static int route_append_json(Route *route, JsonVariant **array) { +static int route_append_json(Route *route, sd_json_variant **array) { _cleanup_free_ char *scope = NULL, *protocol = NULL, *table = NULL, *flags = NULL, *state = NULL; int r; @@ -228,38 +230,38 @@ static int route_append_json(Route *route, JsonVariant **array) { if (r < 0) return r; - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_INTEGER("Family", route->family), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_INTEGER("Family", route->family), JSON_BUILD_PAIR_IN_ADDR("Destination", &route->dst, route->family), - JSON_BUILD_PAIR_UNSIGNED("DestinationPrefixLength", route->dst_prefixlen), + SD_JSON_BUILD_PAIR_UNSIGNED("DestinationPrefixLength", route->dst_prefixlen), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("Gateway", &route->nexthop.gw, route->nexthop.family), - JSON_BUILD_PAIR_CONDITION(route->src_prefixlen > 0, + SD_JSON_BUILD_PAIR_CONDITION(route->src_prefixlen > 0, "Source", JSON_BUILD_IN_ADDR(&route->src, route->family)), JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("SourcePrefixLength", route->src_prefixlen), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("PreferredSource", &route->prefsrc, route->family), - JSON_BUILD_PAIR_UNSIGNED("Scope", route->scope), - JSON_BUILD_PAIR_STRING("ScopeString", scope), - JSON_BUILD_PAIR_UNSIGNED("Protocol", route->protocol), - JSON_BUILD_PAIR_STRING("ProtocolString", protocol), - JSON_BUILD_PAIR_UNSIGNED("Type", route->type), - JSON_BUILD_PAIR_STRING("TypeString", route_type_to_string(route->type)), - JSON_BUILD_PAIR_UNSIGNED("Priority", route->priority), - JSON_BUILD_PAIR_UNSIGNED("Table", route->table), - JSON_BUILD_PAIR_STRING("TableString", table), + SD_JSON_BUILD_PAIR_UNSIGNED("Scope", route->scope), + SD_JSON_BUILD_PAIR_STRING("ScopeString", scope), + SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", route->protocol), + SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol), + SD_JSON_BUILD_PAIR_UNSIGNED("Type", route->type), + SD_JSON_BUILD_PAIR_STRING("TypeString", route_type_to_string(route->type)), + SD_JSON_BUILD_PAIR_UNSIGNED("Priority", route->priority), + SD_JSON_BUILD_PAIR_UNSIGNED("Table", route->table), + SD_JSON_BUILD_PAIR_STRING("TableString", table), JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("MTU", route_metric_get(&route->metric, RTAX_MTU)), - JSON_BUILD_PAIR_UNSIGNED("Preference", route->pref), - JSON_BUILD_PAIR_UNSIGNED("Flags", route->flags), - JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)), + SD_JSON_BUILD_PAIR_UNSIGNED("Preference", route->pref), + SD_JSON_BUILD_PAIR_UNSIGNED("Flags", route->flags), + SD_JSON_BUILD_PAIR_STRING("FlagsString", strempty(flags)), JSON_BUILD_PAIR_FINITE_USEC("LifetimeUSec", route->lifetime_usec), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(route->source)), - JSON_BUILD_PAIR_STRING("ConfigState", state), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(route->source)), + SD_JSON_BUILD_PAIR_STRING("ConfigState", state), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", &route->provider, route->family))); } -static int routes_append_json(Manager *manager, int ifindex, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int routes_append_json(Manager *manager, int ifindex, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; Route *route; int r; @@ -278,7 +280,7 @@ static int routes_append_json(Manager *manager, int ifindex, JsonVariant **v) { return json_variant_set_field_non_null(v, "Routes", array); } -static int routing_policy_rule_append_json(RoutingPolicyRule *rule, JsonVariant **array) { +static int routing_policy_rule_append_json(RoutingPolicyRule *rule, sd_json_variant **array) { _cleanup_free_ char *table = NULL, *protocol = NULL, *state = NULL; int r; @@ -298,47 +300,47 @@ static int routing_policy_rule_append_json(RoutingPolicyRule *rule, JsonVariant if (r < 0) return r; - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_INTEGER("Family", rule->family), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_INTEGER("Family", rule->family), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("FromPrefix", &rule->from, rule->family), - JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->from), - "FromPrefixLength", JSON_BUILD_UNSIGNED(rule->from_prefixlen)), + SD_JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->from), + "FromPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->from_prefixlen)), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ToPrefix", &rule->to, rule->family), - JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->to), - "ToPrefixLength", JSON_BUILD_UNSIGNED(rule->to_prefixlen)), - JSON_BUILD_PAIR_UNSIGNED("Protocol", rule->protocol), - JSON_BUILD_PAIR_STRING("ProtocolString", protocol), - JSON_BUILD_PAIR_UNSIGNED("TOS", rule->tos), - JSON_BUILD_PAIR_UNSIGNED("Type", rule->type), - JSON_BUILD_PAIR_STRING("TypeString", fr_act_type_full_to_string(rule->type)), - JSON_BUILD_PAIR_UNSIGNED("IPProtocol", rule->ipproto), - JSON_BUILD_PAIR_STRING("IPProtocolString", ip_protocol_to_name(rule->ipproto)), - JSON_BUILD_PAIR_UNSIGNED("Priority", rule->priority), - JSON_BUILD_PAIR_UNSIGNED("FirewallMark", rule->fwmark), - JSON_BUILD_PAIR_UNSIGNED("FirewallMask", rule->fwmask), + SD_JSON_BUILD_PAIR_CONDITION(in_addr_is_set(rule->family, &rule->to), + "ToPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->to_prefixlen)), + SD_JSON_BUILD_PAIR_UNSIGNED("Protocol", rule->protocol), + SD_JSON_BUILD_PAIR_STRING("ProtocolString", protocol), + SD_JSON_BUILD_PAIR_UNSIGNED("TOS", rule->tos), + SD_JSON_BUILD_PAIR_UNSIGNED("Type", rule->type), + SD_JSON_BUILD_PAIR_STRING("TypeString", fr_act_type_full_to_string(rule->type)), + SD_JSON_BUILD_PAIR_UNSIGNED("IPProtocol", rule->ipproto), + SD_JSON_BUILD_PAIR_STRING("IPProtocolString", ip_protocol_to_name(rule->ipproto)), + SD_JSON_BUILD_PAIR_UNSIGNED("Priority", rule->priority), + SD_JSON_BUILD_PAIR_UNSIGNED("FirewallMark", rule->fwmark), + SD_JSON_BUILD_PAIR_UNSIGNED("FirewallMask", rule->fwmask), JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Table", rule->table), JSON_BUILD_PAIR_STRING_NON_EMPTY("TableString", table), - JSON_BUILD_PAIR_BOOLEAN("Invert", rule->invert_rule), - JSON_BUILD_PAIR_CONDITION(rule->suppress_prefixlen >= 0, - "SuppressPrefixLength", JSON_BUILD_UNSIGNED(rule->suppress_prefixlen)), - JSON_BUILD_PAIR_CONDITION(rule->suppress_ifgroup >= 0, - "SuppressInterfaceGroup", JSON_BUILD_UNSIGNED(rule->suppress_ifgroup)), - JSON_BUILD_PAIR_CONDITION(rule->sport.start != 0 || rule->sport.end != 0, "SourcePort", - JSON_BUILD_ARRAY(JSON_BUILD_UNSIGNED(rule->sport.start), JSON_BUILD_UNSIGNED(rule->sport.end))), - JSON_BUILD_PAIR_CONDITION(rule->dport.start != 0 || rule->dport.end != 0, "DestinationPort", - JSON_BUILD_ARRAY(JSON_BUILD_UNSIGNED(rule->dport.start), JSON_BUILD_UNSIGNED(rule->dport.end))), - JSON_BUILD_PAIR_CONDITION(rule->uid_range.start != UID_INVALID && rule->uid_range.end != UID_INVALID, "User", - JSON_BUILD_ARRAY(JSON_BUILD_UNSIGNED(rule->uid_range.start), JSON_BUILD_UNSIGNED(rule->uid_range.end))), + SD_JSON_BUILD_PAIR_BOOLEAN("Invert", rule->invert_rule), + SD_JSON_BUILD_PAIR_CONDITION(rule->suppress_prefixlen >= 0, + "SuppressPrefixLength", SD_JSON_BUILD_UNSIGNED(rule->suppress_prefixlen)), + SD_JSON_BUILD_PAIR_CONDITION(rule->suppress_ifgroup >= 0, + "SuppressInterfaceGroup", SD_JSON_BUILD_UNSIGNED(rule->suppress_ifgroup)), + SD_JSON_BUILD_PAIR_CONDITION(rule->sport.start != 0 || rule->sport.end != 0, "SourcePort", + SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->sport.start), SD_JSON_BUILD_UNSIGNED(rule->sport.end))), + SD_JSON_BUILD_PAIR_CONDITION(rule->dport.start != 0 || rule->dport.end != 0, "DestinationPort", + SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->dport.start), SD_JSON_BUILD_UNSIGNED(rule->dport.end))), + SD_JSON_BUILD_PAIR_CONDITION(rule->uid_range.start != UID_INVALID && rule->uid_range.end != UID_INVALID, "User", + SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(rule->uid_range.start), SD_JSON_BUILD_UNSIGNED(rule->uid_range.end))), JSON_BUILD_PAIR_STRING_NON_EMPTY("IncomingInterface", rule->iif), JSON_BUILD_PAIR_STRING_NON_EMPTY("OutgoingInterface", rule->oif), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(rule->source)), - JSON_BUILD_PAIR_STRING("ConfigState", state))); + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(rule->source)), + SD_JSON_BUILD_PAIR_STRING("ConfigState", state))); } -static int routing_policy_rules_append_json(Set *rules, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int routing_policy_rules_append_json(Set *rules, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; RoutingPolicyRule *rule; int r; @@ -353,27 +355,27 @@ static int routing_policy_rules_append_json(Set *rules, JsonVariant **v) { return json_variant_set_field_non_null(v, "RoutingPolicyRules", array); } -static int network_append_json(Network *network, JsonVariant **v) { +static int network_append_json(Network *network, sd_json_variant **v) { assert(v); if (!network) return 0; - return json_variant_merge_objectb( - v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("NetworkFile", network->filename), - JSON_BUILD_PAIR_STRV("NetworkFileDropins", network->dropins), - JSON_BUILD_PAIR_BOOLEAN("RequiredForOnline", network->required_for_online), - JSON_BUILD_PAIR("RequiredOperationalStateForOnline", - JSON_BUILD_ARRAY(JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.min)), - JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.max)))), - JSON_BUILD_PAIR_STRING("RequiredFamilyForOnline", + return sd_json_variant_merge_objectb( + v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("NetworkFile", network->filename), + SD_JSON_BUILD_PAIR_STRV("NetworkFileDropins", network->dropins), + SD_JSON_BUILD_PAIR_BOOLEAN("RequiredForOnline", network->required_for_online), + SD_JSON_BUILD_PAIR("RequiredOperationalStateForOnline", + SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.min)), + SD_JSON_BUILD_STRING(link_operstate_to_string(network->required_operstate_for_online.max)))), + SD_JSON_BUILD_PAIR_STRING("RequiredFamilyForOnline", link_required_address_family_to_string(network->required_family_for_online)), - JSON_BUILD_PAIR_STRING("ActivationPolicy", + SD_JSON_BUILD_PAIR_STRING("ActivationPolicy", activation_policy_to_string(network->activation_policy)))); } -static int device_append_json(sd_device *device, JsonVariant **v) { +static int device_append_json(sd_device *device, sd_json_variant **v) { _cleanup_strv_free_ char **link_dropins = NULL; const char *link = NULL, *path = NULL, *vendor = NULL, *model = NULL, *joined; int r; @@ -396,9 +398,9 @@ static int device_append_json(sd_device *device, JsonVariant **v) { (void) device_get_vendor_string(device, &vendor); (void) device_get_model_string(device, &model); - return json_variant_merge_objectb( + return sd_json_variant_merge_objectb( v, - JSON_BUILD_OBJECT( + SD_JSON_BUILD_OBJECT( JSON_BUILD_PAIR_STRING_NON_EMPTY("LinkFile", link), JSON_BUILD_PAIR_STRV_NON_EMPTY("LinkFileDropins", link_dropins), JSON_BUILD_PAIR_STRING_NON_EMPTY("Path", path), @@ -406,7 +408,7 @@ static int device_append_json(sd_device *device, JsonVariant **v) { JSON_BUILD_PAIR_STRING_NON_EMPTY("Model", model))); } -static int dns_append_json_one(Link *link, const struct in_addr_full *a, NetworkConfigSource s, const union in_addr_union *p, JsonVariant **array) { +static int dns_append_json_one(Link *link, const struct in_addr_full *a, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) { assert(link); assert(a); assert(array); @@ -414,20 +416,20 @@ static int dns_append_json_one(Link *link, const struct in_addr_full *a, Network if (a->ifindex != 0 && a->ifindex != link->ifindex) return 0; - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_INTEGER("Family", a->family), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_INTEGER("Family", a->family), JSON_BUILD_PAIR_IN_ADDR("Address", &a->address, a->family), JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", a->port), - JSON_BUILD_PAIR_CONDITION(a->ifindex != 0, "InterfaceIndex", JSON_BUILD_INTEGER(a->ifindex)), + SD_JSON_BUILD_PAIR_CONDITION(a->ifindex != 0, "InterfaceIndex", SD_JSON_BUILD_INTEGER(a->ifindex)), JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", a->server_name), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, a->family))); } -static int dns_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int dns_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; int r; assert(link); @@ -509,34 +511,34 @@ static int dns_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "DNS", array); } -static int server_append_json_one_addr(int family, const union in_addr_union *a, NetworkConfigSource s, const union in_addr_union *p, JsonVariant **array) { +static int server_append_json_one_addr(int family, const union in_addr_union *a, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) { assert(IN_SET(family, AF_INET, AF_INET6)); assert(a); assert(array); - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_INTEGER("Family", family), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_INTEGER("Family", family), JSON_BUILD_PAIR_IN_ADDR("Address", a, family), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, family))); } -static int server_append_json_one_fqdn(int family, const char *fqdn, NetworkConfigSource s, const union in_addr_union *p, JsonVariant **array) { +static int server_append_json_one_fqdn(int family, const char *fqdn, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) { assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6)); assert(fqdn); assert(array); - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("Server", fqdn), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("Server", fqdn), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, family))); } -static int server_append_json_one_string(const char *str, NetworkConfigSource s, JsonVariant **array) { +static int server_append_json_one_string(const char *str, NetworkConfigSource s, sd_json_variant **array) { union in_addr_union a; int family; @@ -548,8 +550,8 @@ static int server_append_json_one_string(const char *str, NetworkConfigSource s, return server_append_json_one_fqdn(AF_UNSPEC, str, s, NULL, array); } -static int ntp_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int ntp_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; int r; assert(link); @@ -623,8 +625,8 @@ static int ntp_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "NTP", array); } -static int sip_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int sip_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; const struct in_addr *sip; union in_addr_union s; int n_sip, r; @@ -656,21 +658,21 @@ static int sip_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "SIP", array); } -static int domain_append_json(int family, const char *domain, NetworkConfigSource s, const union in_addr_union *p, JsonVariant **array) { +static int domain_append_json(int family, const char *domain, NetworkConfigSource s, const union in_addr_union *p, sd_json_variant **array) { assert(IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6)); assert(domain); assert(array); - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("Domain", domain), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("Domain", domain), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)), JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, family))); } -static int domains_append_json(Link *link, bool is_route, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int domains_append_json(Link *link, bool is_route, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; OrderedSet *link_domains, *network_domains; UseDomains use_domains; union in_addr_union s; @@ -747,19 +749,19 @@ static int domains_append_json(Link *link, bool is_route, JsonVariant **v) { return json_variant_set_field_non_null(v, is_route ? "RouteDomains" : "SearchDomains", array); } -static int nta_append_json(const char *nta, NetworkConfigSource s, JsonVariant **array) { +static int nta_append_json(const char *nta, NetworkConfigSource s, sd_json_variant **array) { assert(nta); assert(array); - return json_variant_append_arrayb( + return sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("DNSSECNegativeTrustAnchor", nta), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("DNSSECNegativeTrustAnchor", nta), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)))); } -static int ntas_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int ntas_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; const char *nta; int r; @@ -780,8 +782,8 @@ static int ntas_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "DNSSECNegativeTrustAnchors", array); } -static int dns_misc_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int dns_misc_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; ResolveSupport resolve_support; NetworkConfigSource source; DnsOverTlsMode mode; @@ -797,11 +799,11 @@ static int dns_misc_append_json(Link *link, JsonVariant **v) { if (resolve_support >= 0) { source = link->llmnr >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("LLMNR", resolve_support_to_string(resolve_support)), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("LLMNR", resolve_support_to_string(resolve_support)), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)))); if (r < 0) return r; } @@ -810,11 +812,11 @@ static int dns_misc_append_json(Link *link, JsonVariant **v) { if (resolve_support >= 0) { source = link->mdns >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("MDNS", resolve_support_to_string(resolve_support)), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("MDNS", resolve_support_to_string(resolve_support)), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)))); if (r < 0) return r; } @@ -823,11 +825,11 @@ static int dns_misc_append_json(Link *link, JsonVariant **v) { if (t >= 0) { source = link->dns_default_route >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_BOOLEAN("DNSDefaultRoute", t), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_BOOLEAN("DNSDefaultRoute", t), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)))); if (r < 0) return r; } @@ -836,11 +838,11 @@ static int dns_misc_append_json(Link *link, JsonVariant **v) { if (mode >= 0) { source = link->dns_over_tls_mode >= 0 ? NETWORK_CONFIG_SOURCE_RUNTIME : NETWORK_CONFIG_SOURCE_STATIC; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("DNSOverTLS", dns_over_tls_mode_to_string(mode)), - JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("DNSOverTLS", dns_over_tls_mode_to_string(mode)), + SD_JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(source)))); if (r < 0) return r; } @@ -848,7 +850,7 @@ static int dns_misc_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "DNSSettings", array); } -static int captive_portal_append_json(Link *link, JsonVariant **v) { +static int captive_portal_append_json(Link *link, sd_json_variant **v) { const char *captive_portal; int r; @@ -859,11 +861,11 @@ static int captive_portal_append_json(Link *link, JsonVariant **v) { if (r <= 0) return r; - return json_variant_merge_objectb(v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_STRING("CaptivePortal", captive_portal))); + return sd_json_variant_merge_objectb(v, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_STRING("CaptivePortal", captive_portal))); } -static int pref64_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL, *w = NULL; +static int pref64_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL, *w = NULL; NDiscPREF64 *i; int r; @@ -874,10 +876,10 @@ static int pref64_append_json(Link *link, JsonVariant **v) { return 0; SET_FOREACH(i, link->ndisc_pref64) { - r = json_variant_append_arrayb(&array, - JSON_BUILD_OBJECT( + r = sd_json_variant_append_arrayb(&array, + SD_JSON_BUILD_OBJECT( JSON_BUILD_PAIR_IN6_ADDR_NON_NULL("Prefix", &i->prefix), - JSON_BUILD_PAIR_UNSIGNED("PrefixLength", i->prefix_len), + SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", i->prefix_len), JSON_BUILD_PAIR_FINITE_USEC("LifetimeUSec", i->lifetime_usec), JSON_BUILD_PAIR_IN6_ADDR_NON_NULL("ConfigProvider", &i->router))); if (r < 0) @@ -891,8 +893,8 @@ static int pref64_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "NDisc", w); } -static int dhcp_server_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +static int dhcp_server_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; int r; assert(link); @@ -901,10 +903,10 @@ static int dhcp_server_append_json(Link *link, JsonVariant **v) { if (!link->dhcp_server) return 0; - r = json_build(&w, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("PoolOffset", link->dhcp_server->pool_offset), - JSON_BUILD_PAIR_UNSIGNED("PoolSize", link->dhcp_server->pool_size))); + r = sd_json_build(&w, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("PoolOffset", link->dhcp_server->pool_offset), + SD_JSON_BUILD_PAIR_UNSIGNED("PoolSize", link->dhcp_server->pool_size))); if (r < 0) return r; @@ -919,8 +921,8 @@ static int dhcp_server_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "DHCPServer", w); } -static int dhcp6_client_vendor_options_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int dhcp6_client_vendor_options_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; sd_dhcp6_option **options = NULL; int r, n_vendor_options; @@ -933,11 +935,11 @@ static int dhcp6_client_vendor_options_append_json(Link *link, JsonVariant **v) n_vendor_options = sd_dhcp6_lease_get_vendor_options(link->dhcp6_lease, &options); FOREACH_ARRAY(option, options, n_vendor_options) { - r = json_variant_append_arrayb(&array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("EnterpriseId", (*option)->enterprise_identifier), - JSON_BUILD_PAIR_UNSIGNED("SubOptionCode", (*option)->option), - JSON_BUILD_PAIR_HEX("SubOptionData", (*option)->data, (*option)->length))); + r = sd_json_variant_append_arrayb(&array, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("EnterpriseId", (*option)->enterprise_identifier), + SD_JSON_BUILD_PAIR_UNSIGNED("SubOptionCode", (*option)->option), + SD_JSON_BUILD_PAIR_HEX("SubOptionData", (*option)->data, (*option)->length))); if (r < 0) return r; } @@ -945,8 +947,8 @@ static int dhcp6_client_vendor_options_append_json(Link *link, JsonVariant **v) return json_variant_set_field_non_null(v, "VendorSpecificOptions", array); } -static int dhcp6_client_lease_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +static int dhcp6_client_lease_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; usec_t ts = USEC_INFINITY, t1 = USEC_INFINITY, t2 = USEC_INFINITY; int r; @@ -968,7 +970,7 @@ static int dhcp6_client_lease_append_json(Link *link, JsonVariant **v) { if (r < 0 && r != -ENODATA) return r; - r = json_build(&w, JSON_BUILD_OBJECT( + r = sd_json_build(&w, SD_JSON_BUILD_OBJECT( JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", t1), JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", t2), JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", ts))); @@ -978,8 +980,8 @@ static int dhcp6_client_lease_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "Lease", w); } -static int dhcp6_client_pd_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int dhcp6_client_pd_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; int r; assert(link); @@ -1004,9 +1006,9 @@ static int dhcp6_client_pd_append_json(Link *link, JsonVariant **v) { if (r < 0) return r; - r = json_variant_append_arrayb(&array, JSON_BUILD_OBJECT( + r = sd_json_variant_append_arrayb(&array, SD_JSON_BUILD_OBJECT( JSON_BUILD_PAIR_IN6_ADDR("Prefix", &prefix), - JSON_BUILD_PAIR_UNSIGNED("PrefixLength", prefix_len), + SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", prefix_len), JSON_BUILD_PAIR_FINITE_USEC("PreferredLifetimeUSec", lifetime_preferred_usec), JSON_BUILD_PAIR_FINITE_USEC("ValidLifetimeUSec", lifetime_valid_usec))); if (r < 0) @@ -1016,7 +1018,7 @@ static int dhcp6_client_pd_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "Prefixes", array); } -static int dhcp6_client_duid_append_json(Link *link, JsonVariant **v) { +static int dhcp6_client_duid_append_json(Link *link, sd_json_variant **v) { const sd_dhcp_duid *duid; const void *data; size_t data_size; @@ -1036,11 +1038,11 @@ static int dhcp6_client_duid_append_json(Link *link, JsonVariant **v) { if (r < 0) return 0; - return json_variant_merge_objectb(v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_BYTE_ARRAY("DUID", data, data_size))); + return sd_json_variant_merge_objectb(v, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_BYTE_ARRAY("DUID", data, data_size))); } -static int dhcp6_client_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +static int dhcp6_client_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; int r; assert(link); @@ -1068,8 +1070,8 @@ static int dhcp6_client_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "DHCPv6Client", w); } -static int dhcp_client_lease_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +static int dhcp_client_lease_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; usec_t lease_timestamp_usec = USEC_INFINITY, t1 = USEC_INFINITY, t2 = USEC_INFINITY; int r; @@ -1091,7 +1093,7 @@ static int dhcp_client_lease_append_json(Link *link, JsonVariant **v) { if (r < 0 && r != -ENODATA) return r; - r = json_build(&w, JSON_BUILD_OBJECT( + r = sd_json_build(&w, SD_JSON_BUILD_OBJECT( JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", lease_timestamp_usec), JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", t1), JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", t2))); @@ -1101,8 +1103,8 @@ static int dhcp_client_lease_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "Lease", w); } -static int dhcp_client_pd_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *addresses = NULL, *array = NULL; +static int dhcp_client_pd_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *addresses = NULL, *array = NULL; uint8_t ipv4masklen, sixrd_prefixlen; struct in6_addr sixrd_prefix; const struct in_addr *br_addresses; @@ -1121,15 +1123,15 @@ static int dhcp_client_pd_append_json(Link *link, JsonVariant **v) { return r; FOREACH_ARRAY(br_address, br_addresses, n_br_addresses) { - r = json_variant_append_arrayb(&addresses, JSON_BUILD_IN4_ADDR(br_address)); + r = sd_json_variant_append_arrayb(&addresses, JSON_BUILD_IN4_ADDR(br_address)); if (r < 0) return r; } - r = json_build(&array, JSON_BUILD_OBJECT( + r = sd_json_build(&array, SD_JSON_BUILD_OBJECT( JSON_BUILD_PAIR_IN6_ADDR("Prefix", &sixrd_prefix), - JSON_BUILD_PAIR_UNSIGNED("PrefixLength", sixrd_prefixlen), - JSON_BUILD_PAIR_UNSIGNED("IPv4MaskLength", ipv4masklen), + SD_JSON_BUILD_PAIR_UNSIGNED("PrefixLength", sixrd_prefixlen), + SD_JSON_BUILD_PAIR_UNSIGNED("IPv4MaskLength", ipv4masklen), JSON_BUILD_PAIR_VARIANT_NON_NULL("BorderRouters", addresses))); if (r < 0) return r; @@ -1137,8 +1139,8 @@ static int dhcp_client_pd_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "6rdPrefix", array); } -static int dhcp_client_private_options_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int dhcp_client_private_options_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; int r; assert(link); @@ -1149,18 +1151,18 @@ static int dhcp_client_private_options_append_json(Link *link, JsonVariant **v) LIST_FOREACH(options, option, link->dhcp_lease->private_options) { - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("Option", option->tag), - JSON_BUILD_PAIR_HEX("PrivateOptionData", option->data, option->length))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("Option", option->tag), + SD_JSON_BUILD_PAIR_HEX("PrivateOptionData", option->data, option->length))); if (r < 0) return 0; } return json_variant_set_field_non_null(v, "PrivateOptions", array); } -static int dhcp_client_id_append_json(Link *link, JsonVariant **v) { +static int dhcp_client_id_append_json(Link *link, sd_json_variant **v) { const sd_dhcp_client_id *client_id; const void *data; size_t l; @@ -1180,11 +1182,11 @@ static int dhcp_client_id_append_json(Link *link, JsonVariant **v) { if (r < 0) return 0; - return json_variant_merge_objectb(v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_BYTE_ARRAY("ClientIdentifier", data, l))); + return sd_json_variant_merge_objectb(v, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_BYTE_ARRAY("ClientIdentifier", data, l))); } -static int dhcp_client_append_json(Link *link, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +static int dhcp_client_append_json(Link *link, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; int r; assert(link); @@ -1212,8 +1214,8 @@ static int dhcp_client_append_json(Link *link, JsonVariant **v) { return json_variant_set_field_non_null(v, "DHCPv4Client", w); } -int link_build_json(Link *link, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +int link_build_json(Link *link, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *type = NULL, *flags = NULL; int r; @@ -1228,42 +1230,42 @@ int link_build_json(Link *link, JsonVariant **ret) { if (r < 0) return r; - r = json_build(&v, JSON_BUILD_OBJECT( + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( /* basic information */ - JSON_BUILD_PAIR_INTEGER("Index", link->ifindex), - JSON_BUILD_PAIR_STRING("Name", link->ifname), + SD_JSON_BUILD_PAIR_INTEGER("Index", link->ifindex), + SD_JSON_BUILD_PAIR_STRING("Name", link->ifname), JSON_BUILD_PAIR_STRV_NON_EMPTY("AlternativeNames", link->alternative_names), - JSON_BUILD_PAIR_CONDITION(link->master_ifindex > 0, - "MasterInterfaceIndex", JSON_BUILD_INTEGER(link->master_ifindex)), + SD_JSON_BUILD_PAIR_CONDITION(link->master_ifindex > 0, + "MasterInterfaceIndex", SD_JSON_BUILD_INTEGER(link->master_ifindex)), JSON_BUILD_PAIR_STRING_NON_EMPTY("Kind", link->kind), - JSON_BUILD_PAIR_STRING("Type", type), + SD_JSON_BUILD_PAIR_STRING("Type", type), JSON_BUILD_PAIR_STRING_NON_EMPTY("Driver", link->driver), - JSON_BUILD_PAIR_UNSIGNED("Flags", link->flags), - JSON_BUILD_PAIR_STRING("FlagsString", flags), - JSON_BUILD_PAIR_UNSIGNED("KernelOperationalState", link->kernel_operstate), - JSON_BUILD_PAIR_STRING("KernelOperationalStateString", kernel_operstate_to_string(link->kernel_operstate)), - JSON_BUILD_PAIR_UNSIGNED("MTU", link->mtu), - JSON_BUILD_PAIR_UNSIGNED("MinimumMTU", link->min_mtu), - JSON_BUILD_PAIR_UNSIGNED("MaximumMTU", link->max_mtu), + SD_JSON_BUILD_PAIR_UNSIGNED("Flags", link->flags), + SD_JSON_BUILD_PAIR_STRING("FlagsString", flags), + SD_JSON_BUILD_PAIR_UNSIGNED("KernelOperationalState", link->kernel_operstate), + SD_JSON_BUILD_PAIR_STRING("KernelOperationalStateString", kernel_operstate_to_string(link->kernel_operstate)), + SD_JSON_BUILD_PAIR_UNSIGNED("MTU", link->mtu), + SD_JSON_BUILD_PAIR_UNSIGNED("MinimumMTU", link->min_mtu), + SD_JSON_BUILD_PAIR_UNSIGNED("MaximumMTU", link->max_mtu), JSON_BUILD_PAIR_HW_ADDR_NON_NULL("HardwareAddress", &link->hw_addr), JSON_BUILD_PAIR_HW_ADDR_NON_NULL("PermanentHardwareAddress", &link->permanent_hw_addr), JSON_BUILD_PAIR_HW_ADDR_NON_NULL("BroadcastAddress", &link->bcast_addr), JSON_BUILD_PAIR_IN6_ADDR_NON_NULL("IPv6LinkLocalAddress", &link->ipv6ll_address), /* wlan information */ - JSON_BUILD_PAIR_CONDITION(link->wlan_iftype > 0, "WirelessLanInterfaceType", - JSON_BUILD_UNSIGNED(link->wlan_iftype)), - JSON_BUILD_PAIR_CONDITION(link->wlan_iftype > 0, "WirelessLanInterfaceTypeString", - JSON_BUILD_STRING(nl80211_iftype_to_string(link->wlan_iftype))), + SD_JSON_BUILD_PAIR_CONDITION(link->wlan_iftype > 0, "WirelessLanInterfaceType", + SD_JSON_BUILD_UNSIGNED(link->wlan_iftype)), + SD_JSON_BUILD_PAIR_CONDITION(link->wlan_iftype > 0, "WirelessLanInterfaceTypeString", + SD_JSON_BUILD_STRING(nl80211_iftype_to_string(link->wlan_iftype))), JSON_BUILD_PAIR_STRING_NON_EMPTY("SSID", link->ssid), JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL("BSSID", &link->bssid), /* link state */ - JSON_BUILD_PAIR_STRING("AdministrativeState", link_state_to_string(link->state)), - JSON_BUILD_PAIR_STRING("OperationalState", link_operstate_to_string(link->operstate)), - JSON_BUILD_PAIR_STRING("CarrierState", link_carrier_state_to_string(link->carrier_state)), - JSON_BUILD_PAIR_STRING("AddressState", link_address_state_to_string(link->address_state)), - JSON_BUILD_PAIR_STRING("IPv4AddressState", link_address_state_to_string(link->ipv4_address_state)), - JSON_BUILD_PAIR_STRING("IPv6AddressState", link_address_state_to_string(link->ipv6_address_state)), - JSON_BUILD_PAIR_STRING("OnlineState", link_online_state_to_string(link->online_state)))); + SD_JSON_BUILD_PAIR_STRING("AdministrativeState", link_state_to_string(link->state)), + SD_JSON_BUILD_PAIR_STRING("OperationalState", link_operstate_to_string(link->operstate)), + SD_JSON_BUILD_PAIR_STRING("CarrierState", link_carrier_state_to_string(link->carrier_state)), + SD_JSON_BUILD_PAIR_STRING("AddressState", link_address_state_to_string(link->address_state)), + SD_JSON_BUILD_PAIR_STRING("IPv4AddressState", link_address_state_to_string(link->ipv4_address_state)), + SD_JSON_BUILD_PAIR_STRING("IPv6AddressState", link_address_state_to_string(link->ipv6_address_state)), + SD_JSON_BUILD_PAIR_STRING("OnlineState", link_online_state_to_string(link->online_state)))); if (r < 0) return r; @@ -1343,8 +1345,8 @@ int link_build_json(Link *link, JsonVariant **ret) { return 0; } -static int links_append_json(Manager *manager, JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int links_append_json(Manager *manager, sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; _cleanup_free_ Link **links = NULL; size_t n_links = 0; int r; @@ -1357,13 +1359,13 @@ static int links_append_json(Manager *manager, JsonVariant **v) { return r; FOREACH_ARRAY(link, links, n_links) { - _cleanup_(json_variant_unrefp) JsonVariant *e = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL; r = link_build_json(*link, &e); if (r < 0) return r; - r = json_variant_append_array(&array, e); + r = sd_json_variant_append_array(&array, e); if (r < 0) return r; } @@ -1371,8 +1373,8 @@ static int links_append_json(Manager *manager, JsonVariant **v) { return json_variant_set_field_non_null(v, "Interfaces", array); } -int manager_build_json(Manager *manager, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +int manager_build_json(Manager *manager, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(manager); diff --git a/src/network/networkd-json.h b/src/network/networkd-json.h index 25018fa097..b3cdb5cae8 100644 --- a/src/network/networkd-json.h +++ b/src/network/networkd-json.h @@ -1,10 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "json.h" +#include "sd-json.h" typedef struct Link Link; typedef struct Manager Manager; -int link_build_json(Link *link, JsonVariant **ret); -int manager_build_json(Manager *manager, JsonVariant **ret); +int link_build_json(Link *link, sd_json_variant **ret); +int manager_build_json(Manager *manager, sd_json_variant **ret); diff --git a/src/network/networkd-link-bus.c b/src/network/networkd-link-bus.c index 743957d27c..f5b8dc726b 100644 --- a/src/network/networkd-link-bus.c +++ b/src/network/networkd-link-bus.c @@ -680,7 +680,7 @@ int bus_link_method_reconfigure(sd_bus_message *message, void *userdata, sd_bus_ int bus_link_method_describe(sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *text = NULL; Link *link = ASSERT_PTR(userdata); int r; @@ -691,7 +691,7 @@ int bus_link_method_describe(sd_bus_message *message, void *userdata, sd_bus_err if (r < 0) return log_link_error_errno(link, r, "Failed to build JSON data: %m"); - r = json_variant_format(v, 0, &text); + r = sd_json_variant_format(v, 0, &text); if (r < 0) return log_link_error_errno(link, r, "Failed to format JSON data: %m"); diff --git a/src/network/networkd-manager-bus.c b/src/network/networkd-manager-bus.c index 3c3d815a4a..04a986a38d 100644 --- a/src/network/networkd-manager-bus.c +++ b/src/network/networkd-manager-bus.c @@ -231,7 +231,7 @@ static int bus_method_describe_link(sd_bus_message *message, void *userdata, sd_ static int bus_method_describe(sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *text = NULL; Manager *manager = ASSERT_PTR(userdata); int r; @@ -242,7 +242,7 @@ static int bus_method_describe(sd_bus_message *message, void *userdata, sd_bus_e if (r < 0) return log_error_errno(r, "Failed to build JSON data: %m"); - r = json_variant_format(v, 0, &text); + r = sd_json_variant_format(v, 0, &text); if (r < 0) return log_error_errno(r, "Failed to format JSON data: %m"); diff --git a/src/network/networkd-manager-varlink.c b/src/network/networkd-manager-varlink.c index 5eeed95791..8422f124f1 100644 --- a/src/network/networkd-manager-varlink.c +++ b/src/network/networkd-manager-varlink.c @@ -4,6 +4,7 @@ #include "bus-polkit.h" #include "fd-util.h" +#include "json-util.h" #include "lldp-rx-internal.h" #include "networkd-dhcp-server.h" #include "networkd-manager-varlink.h" @@ -11,32 +12,32 @@ #include "varlink.h" #include "varlink-io.systemd.Network.h" -static int vl_method_get_states(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_states(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { Manager *m = ASSERT_PTR(userdata); assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); return varlink_replyb(link, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("AddressState", link_address_state_to_string(m->address_state)), - JSON_BUILD_PAIR_STRING("IPv4AddressState", link_address_state_to_string(m->ipv4_address_state)), - JSON_BUILD_PAIR_STRING("IPv6AddressState", link_address_state_to_string(m->ipv6_address_state)), - JSON_BUILD_PAIR_STRING("CarrierState", link_carrier_state_to_string(m->carrier_state)), - JSON_BUILD_PAIR_CONDITION(m->online_state >= 0, "OnlineState", JSON_BUILD_STRING(link_online_state_to_string(m->online_state))), - JSON_BUILD_PAIR_STRING("OperationalState", link_operstate_to_string(m->operational_state)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("AddressState", link_address_state_to_string(m->address_state)), + SD_JSON_BUILD_PAIR_STRING("IPv4AddressState", link_address_state_to_string(m->ipv4_address_state)), + SD_JSON_BUILD_PAIR_STRING("IPv6AddressState", link_address_state_to_string(m->ipv6_address_state)), + SD_JSON_BUILD_PAIR_STRING("CarrierState", link_carrier_state_to_string(m->carrier_state)), + SD_JSON_BUILD_PAIR_CONDITION(m->online_state >= 0, "OnlineState", SD_JSON_BUILD_STRING(link_online_state_to_string(m->online_state))), + SD_JSON_BUILD_PAIR_STRING("OperationalState", link_operstate_to_string(m->operational_state)))); } -static int vl_method_get_namespace_id(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_namespace_id(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { uint64_t inode = 0; uint32_t nsid = UINT32_MAX; int r; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); /* Network namespaces have two identifiers: the inode number (which all namespace types have), and @@ -55,10 +56,10 @@ static int vl_method_get_namespace_id(Varlink *link, JsonVariant *parameters, Va log_full_errno(r == -ENODATA ? LOG_DEBUG : LOG_WARNING, r, "Failed to query network nsid, ignoring: %m"); return varlink_replyb(link, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("NamespaceId", inode), - JSON_BUILD_PAIR_CONDITION(nsid == UINT32_MAX, "NamespaceNSID", JSON_BUILD_NULL), - JSON_BUILD_PAIR_CONDITION(nsid != UINT32_MAX, "NamespaceNSID", JSON_BUILD_UNSIGNED(nsid)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("NamespaceId", inode), + SD_JSON_BUILD_PAIR_CONDITION(nsid == UINT32_MAX, "NamespaceNSID", SD_JSON_BUILD_NULL), + SD_JSON_BUILD_PAIR_CONDITION(nsid != UINT32_MAX, "NamespaceNSID", SD_JSON_BUILD_UNSIGNED(nsid)))); } typedef struct InterfaceInfo { @@ -66,10 +67,10 @@ typedef struct InterfaceInfo { const char *ifname; } InterfaceInfo; -static int dispatch_interface(Varlink *vlink, JsonVariant *parameters, Manager *manager, Link **ret) { - static const JsonDispatch dispatch_table[] = { - { "InterfaceIndex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(InterfaceInfo, ifindex), 0 }, - { "InterfaceName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(InterfaceInfo, ifname), 0 }, +static int dispatch_interface(Varlink *vlink, sd_json_variant *parameters, Manager *manager, Link **ret) { + static const sd_json_dispatch_field dispatch_table[] = { + { "InterfaceIndex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(InterfaceInfo, ifindex), 0 }, + { "InterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(InterfaceInfo, ifname), 0 }, {} }; @@ -106,21 +107,21 @@ static int dispatch_interface(Varlink *vlink, JsonVariant *parameters, Manager * return 0; } -static int link_append_lldp_neighbors(Link *link, JsonVariant *v, JsonVariant **array) { +static int link_append_lldp_neighbors(Link *link, sd_json_variant *v, sd_json_variant **array) { assert(link); assert(array); - return json_variant_append_arrayb(array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_INTEGER("InterfaceIndex", link->ifindex), - JSON_BUILD_PAIR_STRING("InterfaceName", link->ifname), + return sd_json_variant_append_arrayb(array, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_INTEGER("InterfaceIndex", link->ifindex), + SD_JSON_BUILD_PAIR_STRING("InterfaceName", link->ifname), JSON_BUILD_PAIR_STRV_NON_EMPTY("InterfaceAlternativeNames", link->alternative_names), - JSON_BUILD_PAIR_CONDITION(json_variant_is_blank_array(v), "Neighbors", JSON_BUILD_EMPTY_ARRAY), - JSON_BUILD_PAIR_CONDITION(!json_variant_is_blank_array(v), "Neighbors", JSON_BUILD_VARIANT(v)))); + SD_JSON_BUILD_PAIR_CONDITION(sd_json_variant_is_blank_array(v), "Neighbors", SD_JSON_BUILD_EMPTY_ARRAY), + SD_JSON_BUILD_PAIR_CONDITION(!sd_json_variant_is_blank_array(v), "Neighbors", SD_JSON_BUILD_VARIANT(v)))); } -static int vl_method_get_lldp_neighbors(Varlink *vlink, JsonVariant *parameters, VarlinkMethodFlags flags, Manager *manager) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; +static int vl_method_get_lldp_neighbors(Varlink *vlink, sd_json_variant *parameters, VarlinkMethodFlags flags, Manager *manager) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; Link *link = NULL; int r; @@ -132,7 +133,7 @@ static int vl_method_get_lldp_neighbors(Varlink *vlink, JsonVariant *parameters, return r; if (link) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; if (link->lldp_rx) { r = lldp_rx_build_neighbors_json(link->lldp_rx, &v); @@ -145,7 +146,7 @@ static int vl_method_get_lldp_neighbors(Varlink *vlink, JsonVariant *parameters, return r; } else HASHMAP_FOREACH(link, manager->links_by_index) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; if (!link->lldp_rx) continue; @@ -154,7 +155,7 @@ static int vl_method_get_lldp_neighbors(Varlink *vlink, JsonVariant *parameters, if (r < 0) return r; - if (json_variant_is_blank_array(v)) + if (sd_json_variant_is_blank_array(v)) continue; r = link_append_lldp_neighbors(link, v, &array); @@ -163,14 +164,14 @@ static int vl_method_get_lldp_neighbors(Varlink *vlink, JsonVariant *parameters, } return varlink_replyb(vlink, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(json_variant_is_blank_array(array), "Neighbors", JSON_BUILD_EMPTY_ARRAY), - JSON_BUILD_PAIR_CONDITION(!json_variant_is_blank_array(array), "Neighbors", JSON_BUILD_VARIANT(array)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(sd_json_variant_is_blank_array(array), "Neighbors", SD_JSON_BUILD_EMPTY_ARRAY), + SD_JSON_BUILD_PAIR_CONDITION(!sd_json_variant_is_blank_array(array), "Neighbors", SD_JSON_BUILD_VARIANT(array)))); } -static int vl_method_set_persistent_storage(Varlink *vlink, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "Ready", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, 0, 0 }, +static int vl_method_set_persistent_storage(Varlink *vlink, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "Ready", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, 0, 0 }, {} }; diff --git a/src/nspawn/nspawn-bind-user.c b/src/nspawn/nspawn-bind-user.c index 018e7a37e2..91f90e1d02 100644 --- a/src/nspawn/nspawn-bind-user.c +++ b/src/nspawn/nspawn-bind-user.c @@ -4,8 +4,9 @@ #include "fd-util.h" #include "fileio.h" #include "format-util.h" -#include "nspawn-bind-user.h" +#include "json-util.h" #include "nspawn.h" +#include "nspawn-bind-user.h" #include "path-util.h" #include "user-util.h" #include "userdb.h" @@ -87,7 +88,7 @@ static int convert_user( _cleanup_(group_record_unrefp) GroupRecord *converted_group = NULL; _cleanup_(user_record_unrefp) UserRecord *converted_user = NULL; _cleanup_free_ char *h = NULL; - JsonVariant *p, *hp = NULL; + sd_json_variant *p, *hp = NULL; int r; assert(u); @@ -113,31 +114,31 @@ static int convert_user( return log_oom(); /* Acquire the source hashed password array as-is, so that it retains the JSON_VARIANT_SENSITIVE flag */ - p = json_variant_by_key(u->json, "privileged"); + p = sd_json_variant_by_key(u->json, "privileged"); if (p) - hp = json_variant_by_key(p, "hashedPassword"); + hp = sd_json_variant_by_key(p, "hashedPassword"); r = user_record_build( &converted_user, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(u->user_name)), - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(allocate_uid)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(allocate_uid)), - JSON_BUILD_PAIR_CONDITION(u->disposition >= 0, "disposition", JSON_BUILD_STRING(user_disposition_to_string(u->disposition))), - JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_STRING(h)), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NSpawn")), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(u->hashed_password), "privileged", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_VARIANT(hp)))))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(u->user_name)), + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(allocate_uid)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(allocate_uid)), + SD_JSON_BUILD_PAIR_CONDITION(u->disposition >= 0, "disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(u->disposition))), + SD_JSON_BUILD_PAIR("homeDirectory", SD_JSON_BUILD_STRING(h)), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NSpawn")), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(u->hashed_password), "privileged", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("hashedPassword", SD_JSON_BUILD_VARIANT(hp)))))); if (r < 0) return log_error_errno(r, "Failed to build container user record: %m"); r = group_record_build( &converted_group, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(g->group_name)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(allocate_uid)), - JSON_BUILD_PAIR_CONDITION(g->disposition >= 0, "disposition", JSON_BUILD_STRING(user_disposition_to_string(g->disposition))), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NSpawn")))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(g->group_name)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(allocate_uid)), + SD_JSON_BUILD_PAIR_CONDITION(g->disposition >= 0, "disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(g->disposition))), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NSpawn")))); if (r < 0) return log_error_errno(r, "Failed to build container group record: %m"); @@ -314,7 +315,7 @@ int bind_user_prepare( static int write_and_symlink( const char *root, - JsonVariant *v, + sd_json_variant *v, const char *name, uid_t uid, const char *suffix, @@ -329,7 +330,7 @@ static int write_and_symlink( assert(uid_is_valid(uid)); assert(suffix); - r = json_variant_format(v, JSON_FORMAT_NEWLINE, &j); + r = sd_json_variant_format(v, SD_JSON_FORMAT_NEWLINE, &j); if (r < 0) return log_error_errno(r, "Failed to format user record JSON: %m"); @@ -410,7 +411,7 @@ int bind_user_setup( if (r < 0) return log_error_errno(r, "Failed to extract privileged information from group record: %m"); - if (!json_variant_is_blank_object(shadow_group->json)) { + if (!sd_json_variant_is_blank_object(shadow_group->json)) { r = write_and_symlink( root, shadow_group->json, @@ -442,7 +443,7 @@ int bind_user_setup( if (r < 0) return log_error_errno(r, "Failed to extract privileged information from user record: %m"); - if (!json_variant_is_blank_object(shadow_user->json)) { + if (!sd_json_variant_is_blank_object(shadow_user->json)) { r = write_and_symlink( root, shadow_user->json, diff --git a/src/nspawn/nspawn-oci.c b/src/nspawn/nspawn-oci.c index a00934c9b4..db118699dc 100644 --- a/src/nspawn/nspawn-oci.c +++ b/src/nspawn/nspawn-oci.c @@ -2,6 +2,8 @@ #include <linux/oom.h> +#include "sd-json.h" + #include "bus-util.h" #include "cap-list.h" #include "cpu-set-util.h" @@ -11,7 +13,7 @@ #include "format-util.h" #include "fs-util.h" #include "hostname-util.h" -#include "json.h" +#include "json-util.h" #include "missing_sched.h" #include "nspawn-oci.h" #include "path-util.h" @@ -74,34 +76,34 @@ * */ -static int oci_unexpected(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_unexpected(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), - "Unexpected OCI element '%s' of type '%s'.", name, json_variant_type_to_string(json_variant_type(v))); + "Unexpected OCI element '%s' of type '%s'.", name, sd_json_variant_type_to_string(sd_json_variant_type(v))); } -static int oci_dispatch(JsonVariant *v, const JsonDispatch table[], JsonDispatchFlags flags, void *userdata) { - return json_dispatch_full(v, table, oci_unexpected, flags, userdata, /* reterr_bad_field= */ NULL); +static int oci_dispatch(sd_json_variant *v, const sd_json_dispatch_field table[], sd_json_dispatch_flags_t flags, void *userdata) { + return sd_json_dispatch_full(v, table, oci_unexpected, flags, userdata, /* reterr_bad_field= */ NULL); } -static int oci_unsupported(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_unsupported(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP), - "Unsupported OCI element '%s' of type '%s'.", name, json_variant_type_to_string(json_variant_type(v))); + "Unsupported OCI element '%s' of type '%s'.", name, sd_json_variant_type_to_string(sd_json_variant_type(v))); } -static int oci_terminal(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_terminal(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); /* If not specified, or set to true, we'll default to either an interactive or a read-only * console. If specified as false, we'll forcibly move to "pipe" mode though. */ - s->console_mode = json_variant_boolean(v) ? _CONSOLE_MODE_INVALID : CONSOLE_PIPE; + s->console_mode = sd_json_variant_boolean(v) ? _CONSOLE_MODE_INVALID : CONSOLE_PIPE; return 0; } -static int oci_console_dimension(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int oci_console_dimension(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { unsigned *u = ASSERT_PTR(userdata); uint64_t k; - k = json_variant_unsigned(variant); + k = sd_json_variant_unsigned(variant); if (k == 0) return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "Console size field '%s' is too small.", strna(name)); @@ -113,44 +115,31 @@ static int oci_console_dimension(const char *name, JsonVariant *variant, JsonDis return 0; } -static int oci_console_size(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_console_size(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - static const JsonDispatch table[] = { - { "height", JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_height), JSON_MANDATORY }, - { "width", JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_width), JSON_MANDATORY }, + static const sd_json_dispatch_field table[] = { + { "height", SD_JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_height), SD_JSON_MANDATORY }, + { "width", SD_JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_width), SD_JSON_MANDATORY }, {} }; return oci_dispatch(v, table, flags, s); } -static int oci_absolute_path(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { - char **p = ASSERT_PTR(userdata); - const char *n; - - n = json_variant_string(v); - - if (!path_is_absolute(n)) - return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), - "Path in JSON field '%s' is not absolute: %s", strna(name), n); - - return free_and_strdup_warn(p, n); -} - -static int oci_env(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_env(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { char ***l = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { const char *n; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "Environment array contains non-string."); - assert_se(n = json_variant_string(e)); + assert_se(n = sd_json_variant_string(e)); if (!env_assignment_is_valid(n)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), @@ -164,12 +153,12 @@ static int oci_env(const char *name, JsonVariant *v, JsonDispatchFlags flags, vo return 0; } -static int oci_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_args(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_strv_free_ char **l = NULL; char ***value = ASSERT_PTR(userdata); int r; - r = json_variant_strv(v, &l); + r = sd_json_variant_strv(v, &l); if (r < 0) return json_log(v, flags, r, "Cannot parse arguments as list of strings: %m"); @@ -184,40 +173,40 @@ static int oci_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, v return strv_free_and_replace(*value, l); } -static int oci_rlimit_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_rlimit_type(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { const char *z; int *type = ASSERT_PTR(userdata); int t; - z = startswith(json_variant_string(v), "RLIMIT_"); + z = startswith(sd_json_variant_string(v), "RLIMIT_"); if (!z) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "rlimit entry's name does not begin with 'RLIMIT_', refusing: %s", - json_variant_string(v)); + sd_json_variant_string(v)); t = rlimit_from_string(z); if (t < 0) return json_log(v, flags, t, - "rlimit name unknown: %s", json_variant_string(v)); + "rlimit name unknown: %s", sd_json_variant_string(v)); *type = t; return 0; } -static int oci_rlimit_value(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_rlimit_value(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { rlim_t *value = ASSERT_PTR(userdata); rlim_t z; - if (json_variant_is_negative(v)) + if (sd_json_variant_is_negative(v)) z = RLIM_INFINITY; else { - if (!json_variant_is_unsigned(v)) + if (!sd_json_variant_is_unsigned(v)) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "rlimits limit not unsigned, refusing."); - z = (rlim_t) json_variant_unsigned(v); + z = (rlim_t) sd_json_variant_unsigned(v); - if ((uint64_t) z != json_variant_unsigned(v)) + if ((uint64_t) z != sd_json_variant_unsigned(v)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "rlimits limit out of range, refusing."); } @@ -226,9 +215,9 @@ static int oci_rlimit_value(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_rlimits(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_rlimits(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { @@ -243,10 +232,10 @@ static int oci_rlimits(const char *name, JsonVariant *v, JsonDispatchFlags flags .hard = RLIM_INFINITY, }; - static const JsonDispatch table[] = { - { "soft", JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, soft), JSON_MANDATORY }, - { "hard", JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, hard), JSON_MANDATORY }, - { "type", JSON_VARIANT_STRING, oci_rlimit_type, offsetof(struct rlimit_data, type), JSON_MANDATORY }, + static const sd_json_dispatch_field table[] = { + { "soft", SD_JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, soft), SD_JSON_MANDATORY }, + { "hard", SD_JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, hard), SD_JSON_MANDATORY }, + { "type", SD_JSON_VARIANT_STRING, oci_rlimit_type, offsetof(struct rlimit_data, type), SD_JSON_MANDATORY }, {} }; @@ -274,20 +263,20 @@ static int oci_rlimits(const char *name, JsonVariant *v, JsonDispatchFlags flags return 0; } -static int oci_capability_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_capability_array(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { uint64_t *mask = ASSERT_PTR(userdata); uint64_t m = 0; - JsonVariant *e; + sd_json_variant *e; JSON_VARIANT_ARRAY_FOREACH(e, v) { const char *n; int cap; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "Entry in capabilities array is not a string."); - assert_se(n = json_variant_string(e)); + assert_se(n = sd_json_variant_string(e)); cap = capability_from_name(n); if (cap < 0) @@ -305,14 +294,14 @@ static int oci_capability_array(const char *name, JsonVariant *v, JsonDispatchFl return 0; } -static int oci_capabilities(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_capabilities(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch table[] = { - { "effective", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, effective) }, - { "bounding", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, bounding) }, - { "inheritable", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, inheritable) }, - { "permitted", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, permitted) }, - { "ambient", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, ambient) }, + static const sd_json_dispatch_field table[] = { + { "effective", SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, effective) }, + { "bounding", SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, bounding) }, + { "inheritable", SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, inheritable) }, + { "permitted", SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, permitted) }, + { "ambient", SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, ambient) }, {} }; @@ -331,11 +320,11 @@ static int oci_capabilities(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_oom_score_adj(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); int64_t k; - k = json_variant_integer(v); + k = sd_json_variant_integer(v); if (k < OOM_SCORE_ADJ_MIN || k > OOM_SCORE_ADJ_MAX) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "oomScoreAdj value out of range: %" PRIi64, k); @@ -346,40 +335,19 @@ static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_uid_gid(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { - uid_t *uid = ASSERT_PTR(userdata); - uid_t u; - uint64_t k; - - assert_cc(sizeof(uid_t) == sizeof(gid_t)); - - k = json_variant_unsigned(v); - u = (uid_t) k; - if ((uint64_t) u != k) - return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), - "UID/GID out of range: %" PRIu64, k); - - if (!uid_is_valid(u)) - return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), - "UID/GID is not valid: " UID_FMT, u); - - *uid = u; - return 0; -} - -static int oci_supplementary_gids(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_supplementary_gids(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { gid_t gid, *a; - if (!json_variant_is_unsigned(e)) + if (!sd_json_variant_is_unsigned(e)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "Supplementary GID entry is not a UID."); - r = oci_uid_gid(name, e, flags, &gid); + r = sd_json_dispatch_uid_gid(name, e, flags, &gid); if (r < 0) return r; @@ -394,46 +362,46 @@ static int oci_supplementary_gids(const char *name, JsonVariant *v, JsonDispatch return 0; } -static int oci_user(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_user(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch table[] = { - { "uid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(Settings, uid), JSON_MANDATORY }, - { "gid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(Settings, gid), JSON_MANDATORY }, - { "additionalGids", JSON_VARIANT_ARRAY, oci_supplementary_gids, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(Settings, uid), SD_JSON_MANDATORY }, + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(Settings, gid), SD_JSON_MANDATORY }, + { "additionalGids", SD_JSON_VARIANT_ARRAY, oci_supplementary_gids, 0, 0 }, {} }; return oci_dispatch(v, table, flags, userdata); } -static int oci_process(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { - - static const JsonDispatch table[] = { - { "terminal", JSON_VARIANT_BOOLEAN, oci_terminal, 0, 0 }, - { "consoleSize", JSON_VARIANT_OBJECT, oci_console_size, 0, 0 }, - { "cwd", JSON_VARIANT_STRING, oci_absolute_path, offsetof(Settings, working_directory), 0 }, - { "env", JSON_VARIANT_ARRAY, oci_env, offsetof(Settings, environment), 0 }, - { "args", JSON_VARIANT_ARRAY, oci_args, offsetof(Settings, parameters), 0 }, - { "rlimits", JSON_VARIANT_ARRAY, oci_rlimits, 0, 0 }, - { "apparmorProfile", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE }, - { "capabilities", JSON_VARIANT_OBJECT, oci_capabilities, 0, 0 }, - { "noNewPrivileges", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(Settings, no_new_privileges), 0 }, - { "oomScoreAdj", JSON_VARIANT_INTEGER, oci_oom_score_adj, 0, 0 }, - { "selinuxLabel", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE }, - { "user", JSON_VARIANT_OBJECT, oci_user, 0, 0 }, +static int oci_process(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { + + static const sd_json_dispatch_field table[] = { + { "terminal", SD_JSON_VARIANT_BOOLEAN, oci_terminal, 0, 0 }, + { "consoleSize", SD_JSON_VARIANT_OBJECT, oci_console_size, 0, 0 }, + { "cwd", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(Settings, working_directory), 0 }, + { "env", SD_JSON_VARIANT_ARRAY, oci_env, offsetof(Settings, environment), 0 }, + { "args", SD_JSON_VARIANT_ARRAY, oci_args, offsetof(Settings, parameters), 0 }, + { "rlimits", SD_JSON_VARIANT_ARRAY, oci_rlimits, 0, 0 }, + { "apparmorProfile", SD_JSON_VARIANT_STRING, oci_unsupported, 0, SD_JSON_PERMISSIVE }, + { "capabilities", SD_JSON_VARIANT_OBJECT, oci_capabilities, 0, 0 }, + { "noNewPrivileges", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Settings, no_new_privileges), 0 }, + { "oomScoreAdj", SD_JSON_VARIANT_INTEGER, oci_oom_score_adj, 0, 0 }, + { "selinuxLabel", SD_JSON_VARIANT_STRING, oci_unsupported, 0, SD_JSON_PERMISSIVE }, + { "user", SD_JSON_VARIANT_OBJECT, oci_user, 0, 0 }, {} }; return oci_dispatch(v, table, flags, userdata); } -static int oci_root(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_root(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); int r; - static const JsonDispatch table[] = { - { "path", JSON_VARIANT_STRING, json_dispatch_string, offsetof(Settings, root) }, - { "readonly", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(Settings, read_only) }, + static const sd_json_dispatch_field table[] = { + { "path", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Settings, root) }, + { "readonly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Settings, read_only) }, {} }; @@ -454,11 +422,11 @@ static int oci_root(const char *name, JsonVariant *v, JsonDispatchFlags flags, v return 0; } -static int oci_hostname(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_hostname(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); const char *n; - assert_se(n = json_variant_string(v)); + assert_se(n = sd_json_variant_string(v)); if (!hostname_is_valid(n, 0)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), @@ -521,17 +489,17 @@ static void oci_mount_data_done(oci_mount_data *data) { strv_free(data->options); } -static int oci_mounts(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_mounts(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { - static const JsonDispatch table[] = { - { "destination", JSON_VARIANT_STRING, oci_absolute_path, offsetof(oci_mount_data, destination), JSON_MANDATORY }, - { "source", JSON_VARIANT_STRING, json_dispatch_string, offsetof(oci_mount_data, source), 0 }, - { "options", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(oci_mount_data, options), 0, }, - { "type", JSON_VARIANT_STRING, json_dispatch_string, offsetof(oci_mount_data, type), 0 }, + static const sd_json_dispatch_field table[] = { + { "destination", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(oci_mount_data, destination), SD_JSON_MANDATORY }, + { "source", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(oci_mount_data, source), 0 }, + { "options", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(oci_mount_data, options), 0, }, + { "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(oci_mount_data, type), 0 }, {} }; @@ -584,11 +552,11 @@ static int oci_mounts(const char *name, JsonVariant *v, JsonDispatchFlags flags, return 0; } -static int oci_namespace_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_namespace_type(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { unsigned long *nsflags = ASSERT_PTR(userdata); const char *n; - assert_se(n = json_variant_string(v)); + assert_se(n = sd_json_variant_string(v)); /* We don't use namespace_flags_from_string() here, as the OCI spec uses slightly different names than the * kernel here. */ @@ -624,18 +592,18 @@ static void namespace_data_done(struct namespace_data *data) { free(data->path); } -static int oci_namespaces(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_namespaces(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); unsigned long n = 0; - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { _cleanup_(namespace_data_done) struct namespace_data data = {}; - static const JsonDispatch table[] = { - { "type", JSON_VARIANT_STRING, oci_namespace_type, offsetof(struct namespace_data, type), JSON_MANDATORY }, - { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(struct namespace_data, path), 0 }, + static const sd_json_dispatch_field table[] = { + { "type", SD_JSON_VARIANT_STRING, oci_namespace_type, offsetof(struct namespace_data, type), SD_JSON_MANDATORY }, + { "path", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(struct namespace_data, path), 0 }, {} }; @@ -675,7 +643,7 @@ static int oci_namespaces(const char *name, JsonVariant *v, JsonDispatchFlags fl return 0; } -static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_uid_gid_range(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { uid_t *uid = ASSERT_PTR(userdata); uid_t u; uint64_t k; @@ -686,7 +654,7 @@ static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags * than a specific UID, and hence UID_INVALID has no special significance. OTOH a range of zero makes no * sense. */ - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); u = (uid_t) k; if ((uint64_t) u != k) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), @@ -699,7 +667,7 @@ static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_uid_gid_mappings(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_uid_gid_mappings(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { struct mapping_data { uid_t host_id; uid_t container_id; @@ -710,25 +678,25 @@ static int oci_uid_gid_mappings(const char *name, JsonVariant *v, JsonDispatchFl .range = 0, }; - static const JsonDispatch table[] = { - { "containerID", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(struct mapping_data, container_id), JSON_MANDATORY }, - { "hostID", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(struct mapping_data, host_id), JSON_MANDATORY }, - { "size", JSON_VARIANT_UNSIGNED, oci_uid_gid_range, offsetof(struct mapping_data, range), JSON_MANDATORY }, + static const sd_json_dispatch_field table[] = { + { "containerID", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(struct mapping_data, container_id), SD_JSON_MANDATORY }, + { "hostID", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(struct mapping_data, host_id), SD_JSON_MANDATORY }, + { "size", SD_JSON_VARIANT_UNSIGNED, oci_uid_gid_range, offsetof(struct mapping_data, range), SD_JSON_MANDATORY }, {} }; Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; - if (json_variant_elements(v) == 0) + if (sd_json_variant_elements(v) == 0) return 0; - if (json_variant_elements(v) > 1) + if (sd_json_variant_elements(v) > 1) return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP), "UID/GID mappings with more than one entry are not supported."); - assert_se(e = json_variant_by_index(v, 0)); + assert_se(e = sd_json_variant_by_index(v, 0)); r = oci_dispatch(e, table, flags, &data); if (r < 0) @@ -744,7 +712,7 @@ static int oci_uid_gid_mappings(const char *name, JsonVariant *v, JsonDispatchFl "UID/GID mappings with a non-zero container base are not supported."); if (data.range < 0x10000) - json_log(v, flags|JSON_WARNING, 0, + json_log(v, flags|SD_JSON_WARNING, 0, "UID/GID mapping with less than 65536 UID/GIDS set up, you are looking for trouble."); if (s->uid_range != UID_INVALID && @@ -758,11 +726,11 @@ static int oci_uid_gid_mappings(const char *name, JsonVariant *v, JsonDispatchFl return 0; } -static int oci_device_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_device_type(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { mode_t *mode = ASSERT_PTR(userdata); const char *t; - assert_se(t = json_variant_string(v)); + assert_se(t = sd_json_variant_string(v)); if (STR_IN_SET(t, "c", "u")) *mode = (*mode & ~S_IFMT) | S_IFCHR; @@ -777,11 +745,11 @@ static int oci_device_type(const char *name, JsonVariant *v, JsonDispatchFlags f return 0; } -static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_device_major(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { unsigned *u = ASSERT_PTR(userdata); uint64_t k; - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); if (!DEVICE_MAJOR_VALID(k)) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "Device major %" PRIu64 " out of range.", k); @@ -790,11 +758,11 @@ static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_device_minor(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { unsigned *u = ASSERT_PTR(userdata); uint64_t k; - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); if (!DEVICE_MINOR_VALID(k)) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "Device minor %" PRIu64 " out of range.", k); @@ -803,12 +771,12 @@ static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_device_file_mode(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_device_file_mode(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { mode_t *mode = ASSERT_PTR(userdata); mode_t m; uint64_t k; - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); m = (mode_t) k; if ((m & ~07777) != 0 || (uint64_t) m != k) @@ -819,21 +787,21 @@ static int oci_device_file_mode(const char *name, JsonVariant *v, JsonDispatchFl return 0; } -static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_devices(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { - static const JsonDispatch table[] = { - { "type", JSON_VARIANT_STRING, oci_device_type, offsetof(DeviceNode, mode), JSON_MANDATORY }, - { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(DeviceNode, path), JSON_MANDATORY }, - { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(DeviceNode, major), 0 }, - { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(DeviceNode, minor), 0 }, - { "fileMode", JSON_VARIANT_UNSIGNED, oci_device_file_mode, offsetof(DeviceNode, mode), 0 }, - { "uid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(DeviceNode, uid), 0 }, - { "gid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(DeviceNode, gid), 0 }, + static const sd_json_dispatch_field table[] = { + { "type", SD_JSON_VARIANT_STRING, oci_device_type, offsetof(DeviceNode, mode), SD_JSON_MANDATORY }, + { "path", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(DeviceNode, path), SD_JSON_MANDATORY }, + { "major", SD_JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(DeviceNode, major), 0 }, + { "minor", SD_JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(DeviceNode, minor), 0 }, + { "fileMode", SD_JSON_VARIANT_UNSIGNED, oci_device_file_mode, offsetof(DeviceNode, mode), 0 }, + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(DeviceNode, uid), 0 }, + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(DeviceNode, gid), 0 }, {} }; @@ -870,7 +838,7 @@ static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags /* Suppress a couple of implicit device nodes */ r = devname_from_devnum(node->mode, makedev(node->major, node->minor), &path); if (r < 0) - json_log(e, flags|JSON_DEBUG, r, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor); + json_log(e, flags|SD_JSON_DEBUG, r, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor); else { if (PATH_IN_SET(path, "/dev/null", @@ -884,7 +852,7 @@ static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags "/dev/pts/ptmx", "/dev/console")) { - json_log(e, flags|JSON_DEBUG, 0, "Ignoring devices item for device '%s', as it is implicitly created anyway.", path); + json_log(e, flags|SD_JSON_DEBUG, 0, "Ignoring devices item for device '%s', as it is implicitly created anyway.", path); free(node->path); continue; } @@ -902,13 +870,13 @@ static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags return 0; } -static int oci_cgroups_path(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroups_path(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_free_ char *slice = NULL, *backwards = NULL; Settings *s = ASSERT_PTR(userdata); const char *p; int r; - assert_se(p = json_variant_string(v)); + assert_se(p = sd_json_variant_string(v)); r = cg_path_get_slice(p, &slice); if (r < 0) @@ -926,11 +894,11 @@ static int oci_cgroups_path(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_cgroup_device_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_device_type(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { mode_t *mode = ASSERT_PTR(userdata); const char *n; - assert_se(n = json_variant_string(v)); + assert_se(n = sd_json_variant_string(v)); if (streq(n, "c")) *mode = S_IFCHR; @@ -953,13 +921,13 @@ struct device_data { unsigned minor; }; -static int oci_cgroup_device_access(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_device_access(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { struct device_data *d = ASSERT_PTR(userdata); bool r = false, w = false, m = false; const char *s; size_t i; - assert_se(s = json_variant_string(v)); + assert_se(s = sd_json_variant_string(v)); for (i = 0; s[i]; i++) if (s[i] == 'r') @@ -979,12 +947,12 @@ static int oci_cgroup_device_access(const char *name, JsonVariant *v, JsonDispat return 0; } -static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_devices(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_free_ struct device_data *list = NULL; Settings *s = ASSERT_PTR(userdata); size_t n_list = 0, i; bool noop = false; - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { @@ -994,12 +962,12 @@ static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlag .minor = UINT_MAX, }, *a; - static const JsonDispatch table[] = { - { "allow", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct device_data, allow), JSON_MANDATORY }, - { "type", JSON_VARIANT_STRING, oci_cgroup_device_type, offsetof(struct device_data, type), 0 }, - { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), 0 }, - { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), 0 }, - { "access", JSON_VARIANT_STRING, oci_cgroup_device_access, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "allow", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct device_data, allow), SD_JSON_MANDATORY }, + { "type", SD_JSON_VARIANT_STRING, oci_cgroup_device_type, offsetof(struct device_data, type), 0 }, + { "major", SD_JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), 0 }, + { "minor", SD_JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), 0 }, + { "access", SD_JSON_VARIANT_STRING, oci_cgroup_device_access, 0, 0 }, {} }; @@ -1017,7 +985,7 @@ static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlag * drop the kernel's default we ignore silently */ if (!data.r || !data.w || !data.m || data.type != 0 || data.major != UINT_MAX || data.minor != UINT_MAX) - json_log(v, flags|JSON_WARNING, 0, "Devices cgroup allow list with arbitrary 'allow' entries not supported, ignoring."); + json_log(v, flags|SD_JSON_WARNING, 0, "Devices cgroup allow list with arbitrary 'allow' entries not supported, ignoring."); /* We ignore the 'deny' entry as for us that's implied */ continue; @@ -1137,20 +1105,20 @@ static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlag return 0; } -static int oci_cgroup_memory_limit(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_memory_limit(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { uint64_t *m = ASSERT_PTR(userdata); uint64_t k; - if (json_variant_is_negative(v)) { + if (sd_json_variant_is_negative(v)) { *m = UINT64_MAX; return 0; } - if (!json_variant_is_unsigned(v)) + if (!sd_json_variant_is_unsigned(v)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "Memory limit is not an unsigned integer."); - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); if (k >= UINT64_MAX) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "Memory limit too large: %" PRIu64, k); @@ -1159,7 +1127,7 @@ static int oci_cgroup_memory_limit(const char *name, JsonVariant *v, JsonDispatc return 0; } -static int oci_cgroup_memory(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_memory(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { struct memory_data { uint64_t limit; @@ -1171,14 +1139,14 @@ static int oci_cgroup_memory(const char *name, JsonVariant *v, JsonDispatchFlags .swap = UINT64_MAX, }; - static const JsonDispatch table[] = { - { "limit", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, limit), 0 }, - { "reservation", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, reservation), 0 }, - { "swap", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, swap), 0 }, - { "kernel", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE }, - { "kernelTCP", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE }, - { "swapiness", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE }, - { "disableOOMKiller", JSON_VARIANT_BOOLEAN, oci_unsupported, 0, JSON_PERMISSIVE }, + static const sd_json_dispatch_field table[] = { + { "limit", SD_JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, limit), 0 }, + { "reservation", SD_JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, reservation), 0 }, + { "swap", SD_JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, swap), 0 }, + { "kernel", SD_JSON_VARIANT_NUMBER, oci_unsupported, 0, SD_JSON_PERMISSIVE }, + { "kernelTCP", SD_JSON_VARIANT_NUMBER, oci_unsupported, 0, SD_JSON_PERMISSIVE }, + { "swapiness", SD_JSON_VARIANT_NUMBER, oci_unsupported, 0, SD_JSON_PERMISSIVE }, + { "disableOOMKiller", SD_JSON_VARIANT_BOOLEAN, oci_unsupported, 0, SD_JSON_PERMISSIVE }, {} }; @@ -1235,11 +1203,11 @@ struct cpu_data { CPUSet cpu_set; }; -static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_cpu_shares(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { uint64_t *u = ASSERT_PTR(userdata); uint64_t k; - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); if (k < CGROUP_CPU_SHARES_MIN || k > CGROUP_CPU_SHARES_MAX) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "shares value out of range."); @@ -1248,11 +1216,11 @@ static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchF return 0; } -static int oci_cgroup_cpu_quota(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_cpu_quota(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { uint64_t *u = ASSERT_PTR(userdata); uint64_t k; - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); if (k <= 0 || k >= UINT64_MAX) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "period/quota value out of range."); @@ -1261,13 +1229,13 @@ static int oci_cgroup_cpu_quota(const char *name, JsonVariant *v, JsonDispatchFl return 0; } -static int oci_cgroup_cpu_cpus(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_cpu_cpus(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { struct cpu_data *data = ASSERT_PTR(userdata); CPUSet set; const char *n; int r; - assert_se(n = json_variant_string(v)); + assert_se(n = sd_json_variant_string(v)); r = parse_cpu_set(n, &set); if (r < 0) @@ -1279,16 +1247,16 @@ static int oci_cgroup_cpu_cpus(const char *name, JsonVariant *v, JsonDispatchFla return 0; } -static int oci_cgroup_cpu(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_cpu(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch table[] = { - { "shares", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_shares, offsetof(struct cpu_data, shares), 0 }, - { "quota", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota, offsetof(struct cpu_data, quota), 0 }, - { "period", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota, offsetof(struct cpu_data, period), 0 }, - { "realtimeRuntime", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, 0 }, - { "realtimePeriod", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, 0 }, - { "cpus", JSON_VARIANT_STRING, oci_cgroup_cpu_cpus, 0, 0 }, - { "mems", JSON_VARIANT_STRING, oci_unsupported, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "shares", SD_JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_shares, offsetof(struct cpu_data, shares), 0 }, + { "quota", SD_JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota, offsetof(struct cpu_data, quota), 0 }, + { "period", SD_JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota, offsetof(struct cpu_data, period), 0 }, + { "realtimeRuntime", SD_JSON_VARIANT_UNSIGNED, oci_unsupported, 0, 0 }, + { "realtimePeriod", SD_JSON_VARIANT_UNSIGNED, oci_unsupported, 0, 0 }, + { "cpus", SD_JSON_VARIANT_STRING, oci_cgroup_cpu_cpus, 0, 0 }, + { "mems", SD_JSON_VARIANT_STRING, oci_unsupported, 0, 0 }, {} }; @@ -1336,12 +1304,12 @@ static int oci_cgroup_cpu(const char *name, JsonVariant *v, JsonDispatchFlags fl return 0; } -static int oci_cgroup_block_io_weight(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_block_io_weight(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); uint64_t k; int r; - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); if (k < CGROUP_BLKIO_WEIGHT_MIN || k > CGROUP_BLKIO_WEIGHT_MAX) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "Block I/O weight out of range."); @@ -1357,9 +1325,9 @@ static int oci_cgroup_block_io_weight(const char *name, JsonVariant *v, JsonDisp return 0; } -static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_block_io_weight_device(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { @@ -1373,11 +1341,11 @@ static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, J .weight = UINT64_MAX, }; - static const JsonDispatch table[] = { - { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY }, - { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY }, - { "weight", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct device_data, weight), 0 }, - { "leafWeight", JSON_VARIANT_INTEGER, oci_unsupported, 0, JSON_PERMISSIVE }, + static const sd_json_dispatch_field table[] = { + { "major", SD_JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), SD_JSON_MANDATORY }, + { "minor", SD_JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), SD_JSON_MANDATORY }, + { "weight", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(struct device_data, weight), 0 }, + { "leafWeight", SD_JSON_VARIANT_INTEGER, oci_unsupported, 0, SD_JSON_PERMISSIVE }, {} }; @@ -1410,10 +1378,10 @@ static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, J return 0; } -static int oci_cgroup_block_io_throttle(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_block_io_throttle(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); const char *pname; - JsonVariant *e; + sd_json_variant *e; int r; pname = streq(name, "throttleReadBpsDevice") ? "IOReadBandwidthMax" : @@ -1431,10 +1399,10 @@ static int oci_cgroup_block_io_throttle(const char *name, JsonVariant *v, JsonDi .minor = UINT_MAX, }; - static const JsonDispatch table[] = { - { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY }, - { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY }, - { "rate", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct device_data, rate), JSON_MANDATORY }, + static const sd_json_dispatch_field table[] = { + { "major", SD_JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), SD_JSON_MANDATORY }, + { "minor", SD_JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), SD_JSON_MANDATORY }, + { "rate", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(struct device_data, rate), SD_JSON_MANDATORY }, {} }; @@ -1464,30 +1432,30 @@ static int oci_cgroup_block_io_throttle(const char *name, JsonVariant *v, JsonDi return 0; } -static int oci_cgroup_block_io(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_block_io(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch table[] = { - { "weight", JSON_VARIANT_UNSIGNED, oci_cgroup_block_io_weight, 0, 0 }, - { "leafWeight", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, JSON_PERMISSIVE }, - { "weightDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_weight_device, 0, 0 }, - { "throttleReadBpsDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, - { "throttleWriteBpsDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, - { "throttleReadIOPSDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, - { "throttleWriteIOPSDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "weight", SD_JSON_VARIANT_UNSIGNED, oci_cgroup_block_io_weight, 0, 0 }, + { "leafWeight", SD_JSON_VARIANT_UNSIGNED, oci_unsupported, 0, SD_JSON_PERMISSIVE }, + { "weightDevice", SD_JSON_VARIANT_ARRAY, oci_cgroup_block_io_weight_device, 0, 0 }, + { "throttleReadBpsDevice", SD_JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, + { "throttleWriteBpsDevice", SD_JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, + { "throttleReadIOPSDevice", SD_JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, + { "throttleWriteIOPSDevice", SD_JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, {} }; return oci_dispatch(v, table, flags, userdata); } -static int oci_cgroup_pids(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_cgroup_pids(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch table[] = { - { "limit", JSON_VARIANT_NUMBER, json_dispatch_variant, 0, JSON_MANDATORY }, + static const sd_json_dispatch_field table[] = { + { "limit", SD_JSON_VARIANT_NUMBER, sd_json_dispatch_variant, 0, SD_JSON_MANDATORY }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *k = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *k = NULL; Settings *s = ASSERT_PTR(userdata); uint64_t m; int r; @@ -1496,16 +1464,16 @@ static int oci_cgroup_pids(const char *name, JsonVariant *v, JsonDispatchFlags f if (r < 0) return r; - if (json_variant_is_negative(k)) + if (sd_json_variant_is_negative(k)) m = UINT64_MAX; else { - if (!json_variant_is_unsigned(k)) + if (!sd_json_variant_is_unsigned(k)) return json_log(k, flags, SYNTHETIC_ERRNO(EINVAL), "pids limit not unsigned integer, refusing."); - m = (uint64_t) json_variant_unsigned(k); + m = (uint64_t) sd_json_variant_unsigned(k); - if ((uint64_t) m != json_variant_unsigned(k)) + if ((uint64_t) m != sd_json_variant_unsigned(k)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "pids limit out of range, refusing."); } @@ -1521,17 +1489,17 @@ static int oci_cgroup_pids(const char *name, JsonVariant *v, JsonDispatchFlags f return 0; } -static int oci_resources(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_resources(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch table[] = { - { "devices", JSON_VARIANT_ARRAY, oci_cgroup_devices, 0, 0 }, - { "memory", JSON_VARIANT_OBJECT, oci_cgroup_memory, 0, 0 }, - { "cpu", JSON_VARIANT_OBJECT, oci_cgroup_cpu, 0, 0 }, - { "blockIO", JSON_VARIANT_OBJECT, oci_cgroup_block_io, 0, 0 }, - { "hugepageLimits", JSON_VARIANT_ARRAY, oci_unsupported, 0, 0 }, - { "network", JSON_VARIANT_OBJECT, oci_unsupported, 0, 0 }, - { "pids", JSON_VARIANT_OBJECT, oci_cgroup_pids, 0, 0 }, - { "rdma", JSON_VARIANT_OBJECT, oci_unsupported, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "devices", SD_JSON_VARIANT_ARRAY, oci_cgroup_devices, 0, 0 }, + { "memory", SD_JSON_VARIANT_OBJECT, oci_cgroup_memory, 0, 0 }, + { "cpu", SD_JSON_VARIANT_OBJECT, oci_cgroup_cpu, 0, 0 }, + { "blockIO", SD_JSON_VARIANT_OBJECT, oci_cgroup_block_io, 0, 0 }, + { "hugepageLimits", SD_JSON_VARIANT_ARRAY, oci_unsupported, 0, 0 }, + { "network", SD_JSON_VARIANT_OBJECT, oci_unsupported, 0, 0 }, + { "pids", SD_JSON_VARIANT_OBJECT, oci_cgroup_pids, 0, 0 }, + { "rdma", SD_JSON_VARIANT_OBJECT, oci_unsupported, 0, 0 }, {} }; @@ -1569,20 +1537,20 @@ static bool sysctl_key_valid(const char *s) { return true; } -static int oci_sysctl(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_sysctl(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *w; + sd_json_variant *w; const char *k; int r; JSON_VARIANT_OBJECT_FOREACH(k, w, v) { const char *m; - if (!json_variant_is_string(w)) + if (!sd_json_variant_is_string(w)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "sysctl parameter is not a string, refusing."); - assert_se(m = json_variant_string(w)); + assert_se(m = sd_json_variant_string(w)); if (!sysctl_key_valid(k)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), @@ -1706,21 +1674,21 @@ static int oci_seccomp_compare_from_string(const char *name, enum scmp_compare * return -EINVAL; } -static int oci_seccomp_archs(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_seccomp_archs(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { scmp_filter_ctx *sc = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { uint32_t a; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "Architecture entry is not a string."); - r = oci_seccomp_arch_from_string(json_variant_string(e), &a); + r = oci_seccomp_arch_from_string(sd_json_variant_string(e), &a); if (r < 0) - return json_log(e, flags, r, "Unknown architecture: %s", json_variant_string(e)); + return json_log(e, flags, r, "Unknown architecture: %s", sd_json_variant_string(e)); r = seccomp_arch_add(sc, a); if (r == -EEXIST) @@ -1746,39 +1714,39 @@ static void syscall_rule_done(struct syscall_rule *rule) { free(rule->arguments); }; -static int oci_seccomp_action(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_seccomp_action(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { uint32_t *action = ASSERT_PTR(userdata); int r; - r = oci_seccomp_action_from_string(json_variant_string(v), action); + r = oci_seccomp_action_from_string(sd_json_variant_string(v), action); if (r < 0) - return json_log(v, flags, r, "Unknown system call action '%s': %m", json_variant_string(v)); + return json_log(v, flags, r, "Unknown system call action '%s': %m", sd_json_variant_string(v)); return 0; } -static int oci_seccomp_op(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_seccomp_op(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { enum scmp_compare *op = ASSERT_PTR(userdata); int r; - r = oci_seccomp_compare_from_string(json_variant_string(v), op); + r = oci_seccomp_compare_from_string(sd_json_variant_string(v), op); if (r < 0) - return json_log(v, flags, r, "Unknown seccomp operator '%s': %m", json_variant_string(v)); + return json_log(v, flags, r, "Unknown seccomp operator '%s': %m", sd_json_variant_string(v)); return 0; } -static int oci_seccomp_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_seccomp_args(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { struct syscall_rule *rule = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { - static const struct JsonDispatch table[] = { - { "index", JSON_VARIANT_UNSIGNED, json_dispatch_uint32, offsetof(struct scmp_arg_cmp, arg), JSON_MANDATORY }, - { "value", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_a), JSON_MANDATORY }, - { "valueTwo", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_b), 0 }, - { "op", JSON_VARIANT_STRING, oci_seccomp_op, offsetof(struct scmp_arg_cmp, op), JSON_MANDATORY }, + static const struct sd_json_dispatch_field table[] = { + { "index", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint32, offsetof(struct scmp_arg_cmp, arg), SD_JSON_MANDATORY }, + { "value", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_a), SD_JSON_MANDATORY }, + { "valueTwo", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_b), 0 }, + { "op", SD_JSON_VARIANT_STRING, oci_seccomp_op, offsetof(struct scmp_arg_cmp, op), SD_JSON_MANDATORY }, {}, }; @@ -1805,7 +1773,7 @@ static int oci_seccomp_args(const char *name, JsonVariant *v, JsonDispatchFlags expected = p->op == SCMP_CMP_MASKED_EQ ? 4 : 3; if (r != expected) - json_log(e, flags|JSON_WARNING, 0, "Wrong number of system call arguments for JSON data, ignoring."); + json_log(e, flags|SD_JSON_WARNING, 0, "Wrong number of system call arguments for JSON data, ignoring."); /* Note that we are a bit sloppy here and do not insist that SCMP_CMP_MASKED_EQ gets two datum values, * and the other only one. That's because buildah for example by default calls things with @@ -1817,16 +1785,16 @@ static int oci_seccomp_args(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_seccomp_syscalls(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_seccomp_syscalls(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { scmp_filter_ctx *sc = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { - static const JsonDispatch table[] = { - { "names", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(struct syscall_rule, names), JSON_MANDATORY }, - { "action", JSON_VARIANT_STRING, oci_seccomp_action, offsetof(struct syscall_rule, action), JSON_MANDATORY }, - { "args", JSON_VARIANT_ARRAY, oci_seccomp_args, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "names", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(struct syscall_rule, names), SD_JSON_MANDATORY }, + { "action", SD_JSON_VARIANT_STRING, oci_seccomp_action, offsetof(struct syscall_rule, action), SD_JSON_MANDATORY }, + { "args", SD_JSON_VARIANT_ARRAY, oci_seccomp_args, 0, 0 }, {} }; _cleanup_(syscall_rule_done) struct syscall_rule rule = { @@ -1859,32 +1827,32 @@ static int oci_seccomp_syscalls(const char *name, JsonVariant *v, JsonDispatchFl } #endif -static int oci_seccomp(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_seccomp(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { #if HAVE_SECCOMP - static const JsonDispatch table[] = { - { "defaultAction", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY }, - { "architectures", JSON_VARIANT_ARRAY, oci_seccomp_archs, 0, 0 }, - { "syscalls", JSON_VARIANT_ARRAY, oci_seccomp_syscalls, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "defaultAction", SD_JSON_VARIANT_STRING, NULL, 0, SD_JSON_MANDATORY }, + { "architectures", SD_JSON_VARIANT_ARRAY, oci_seccomp_archs, 0, 0 }, + { "syscalls", SD_JSON_VARIANT_ARRAY, oci_seccomp_syscalls, 0, 0 }, {} }; _cleanup_(seccomp_releasep) scmp_filter_ctx sc = NULL; Settings *s = ASSERT_PTR(userdata); - JsonVariant *def; + sd_json_variant *def; uint32_t d; int r; - def = json_variant_by_key(v, "defaultAction"); + def = sd_json_variant_by_key(v, "defaultAction"); if (!def) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction element missing."); - if (!json_variant_is_string(def)) + if (!sd_json_variant_is_string(def)) return json_log(def, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction is not a string."); - r = oci_seccomp_action_from_string(json_variant_string(def), &d); + r = oci_seccomp_action_from_string(sd_json_variant_string(def), &d); if (r < 0) - return json_log(def, flags, r, "Unknown default action: %s", json_variant_string(def)); + return json_log(def, flags, r, "Unknown default action: %s", sd_json_variant_string(def)); sc = seccomp_init(d); if (!sc) @@ -1902,32 +1870,32 @@ static int oci_seccomp(const char *name, JsonVariant *v, JsonDispatchFlags flags #endif } -static int oci_rootfs_propagation(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_rootfs_propagation(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { const char *s; - s = json_variant_string(v); + s = sd_json_variant_string(v); if (streq(s, "shared")) return 0; - json_log(v, flags|JSON_DEBUG, 0, "Ignoring rootfsPropagation setting '%s'.", s); + json_log(v, flags|SD_JSON_DEBUG, 0, "Ignoring rootfsPropagation setting '%s'.", s); return 0; } -static int oci_masked_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_masked_paths(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; JSON_VARIANT_ARRAY_FOREACH(e, v) { _cleanup_free_ char *destination = NULL; CustomMount *m; const char *p; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "Path is not a string, refusing."); - assert_se(p = json_variant_string(e)); + assert_se(p = sd_json_variant_string(e)); if (!path_is_absolute(p)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), @@ -1954,20 +1922,20 @@ static int oci_masked_paths(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_readonly_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_readonly_paths(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; JSON_VARIANT_ARRAY_FOREACH(e, v) { _cleanup_free_ char *source = NULL, *destination = NULL; CustomMount *m; const char *p; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "Path is not a string, refusing."); - assert_se(p = json_variant_string(e)); + assert_se(p = sd_json_variant_string(e)); if (!path_is_absolute(p)) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), @@ -1996,33 +1964,33 @@ static int oci_readonly_paths(const char *name, JsonVariant *v, JsonDispatchFlag return 0; } -static int oci_linux(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { - - static const JsonDispatch table[] = { - { "namespaces", JSON_VARIANT_ARRAY, oci_namespaces, 0, 0 }, - { "uidMappings", JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 }, - { "gidMappings", JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 }, - { "devices", JSON_VARIANT_ARRAY, oci_devices, 0, 0 }, - { "cgroupsPath", JSON_VARIANT_STRING, oci_cgroups_path, 0, 0 }, - { "resources", JSON_VARIANT_OBJECT, oci_resources, 0, 0 }, - { "intelRdt", JSON_VARIANT_OBJECT, oci_unsupported, 0, JSON_PERMISSIVE }, - { "sysctl", JSON_VARIANT_OBJECT, oci_sysctl, 0, 0 }, - { "seccomp", JSON_VARIANT_OBJECT, oci_seccomp, 0, 0 }, - { "rootfsPropagation", JSON_VARIANT_STRING, oci_rootfs_propagation, 0, 0 }, - { "maskedPaths", JSON_VARIANT_ARRAY, oci_masked_paths, 0, 0 }, - { "readonlyPaths", JSON_VARIANT_ARRAY, oci_readonly_paths, 0, 0 }, - { "mountLabel", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE }, +static int oci_linux(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { + + static const sd_json_dispatch_field table[] = { + { "namespaces", SD_JSON_VARIANT_ARRAY, oci_namespaces, 0, 0 }, + { "uidMappings", SD_JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 }, + { "gidMappings", SD_JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 }, + { "devices", SD_JSON_VARIANT_ARRAY, oci_devices, 0, 0 }, + { "cgroupsPath", SD_JSON_VARIANT_STRING, oci_cgroups_path, 0, 0 }, + { "resources", SD_JSON_VARIANT_OBJECT, oci_resources, 0, 0 }, + { "intelRdt", SD_JSON_VARIANT_OBJECT, oci_unsupported, 0, SD_JSON_PERMISSIVE }, + { "sysctl", SD_JSON_VARIANT_OBJECT, oci_sysctl, 0, 0 }, + { "seccomp", SD_JSON_VARIANT_OBJECT, oci_seccomp, 0, 0 }, + { "rootfsPropagation", SD_JSON_VARIANT_STRING, oci_rootfs_propagation, 0, 0 }, + { "maskedPaths", SD_JSON_VARIANT_ARRAY, oci_masked_paths, 0, 0 }, + { "readonlyPaths", SD_JSON_VARIANT_ARRAY, oci_readonly_paths, 0, 0 }, + { "mountLabel", SD_JSON_VARIANT_STRING, oci_unsupported, 0, SD_JSON_PERMISSIVE }, {} }; return oci_dispatch(v, table, flags, userdata); } -static int oci_hook_timeout(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_hook_timeout(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { usec_t *u = ASSERT_PTR(userdata); uint64_t k; - k = json_variant_unsigned(v); + k = sd_json_variant_unsigned(v); if (k == 0 || k > (UINT64_MAX-1)/USEC_PER_SEC) return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "Hook timeout value out of range."); @@ -2031,18 +1999,18 @@ static int oci_hook_timeout(const char *name, JsonVariant *v, JsonDispatchFlags return 0; } -static int oci_hooks_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_hooks_array(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { Settings *s = ASSERT_PTR(userdata); - JsonVariant *e; + sd_json_variant *e; int r; JSON_VARIANT_ARRAY_FOREACH(e, v) { - static const JsonDispatch table[] = { - { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(OciHook, path), JSON_MANDATORY }, - { "args", JSON_VARIANT_ARRAY, oci_args, offsetof(OciHook, args), 0 }, - { "env", JSON_VARIANT_ARRAY, oci_env, offsetof(OciHook, env), 0 }, - { "timeout", JSON_VARIANT_UNSIGNED, oci_hook_timeout, offsetof(OciHook, timeout), 0 }, + static const sd_json_dispatch_field table[] = { + { "path", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(OciHook, path), SD_JSON_MANDATORY }, + { "args", SD_JSON_VARIANT_ARRAY, oci_args, offsetof(OciHook, args), 0, }, + { "env", SD_JSON_VARIANT_ARRAY, oci_env, offsetof(OciHook, env), 0 }, + { "timeout", SD_JSON_VARIANT_UNSIGNED, oci_hook_timeout, offsetof(OciHook, timeout), 0 }, {} }; @@ -2086,20 +2054,20 @@ static int oci_hooks_array(const char *name, JsonVariant *v, JsonDispatchFlags f return 0; } -static int oci_hooks(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int oci_hooks(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch table[] = { - { "prestart", JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 }, - { "poststart", JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 }, - { "poststop", JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "prestart", SD_JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 }, + { "poststart", SD_JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 }, + { "poststop", SD_JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 }, {} }; return oci_dispatch(v, table, flags, userdata); } -static int oci_annotations(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { - JsonVariant *w; +static int oci_annotations(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { + sd_json_variant *w; const char *k; JSON_VARIANT_OBJECT_FOREACH(k, w, v) { @@ -2108,11 +2076,11 @@ static int oci_annotations(const char *name, JsonVariant *v, JsonDispatchFlags f return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "Annotation with empty key, refusing."); - if (!json_variant_is_string(w)) + if (!sd_json_variant_is_string(w)) return json_log(w, flags, SYNTHETIC_ERRNO(EINVAL), "Annotation has non-string value, refusing."); - json_log(w, flags|JSON_DEBUG, 0, "Ignoring annotation '%s' with value '%s'.", k, json_variant_string(w)); + json_log(w, flags|SD_JSON_DEBUG, 0, "Ignoring annotation '%s' with value '%s'.", k, sd_json_variant_string(w)); } return 0; @@ -2120,22 +2088,22 @@ static int oci_annotations(const char *name, JsonVariant *v, JsonDispatchFlags f int oci_load(FILE *f, const char *bundle, Settings **ret) { - static const JsonDispatch table[] = { - { "ociVersion", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY }, - { "process", JSON_VARIANT_OBJECT, oci_process, 0, 0 }, - { "root", JSON_VARIANT_OBJECT, oci_root, 0, 0 }, - { "hostname", JSON_VARIANT_STRING, oci_hostname, 0, 0 }, - { "mounts", JSON_VARIANT_ARRAY, oci_mounts, 0, 0 }, - { "linux", JSON_VARIANT_OBJECT, oci_linux, 0, 0 }, - { "hooks", JSON_VARIANT_OBJECT, oci_hooks, 0, 0 }, - { "annotations", JSON_VARIANT_OBJECT, oci_annotations, 0, 0 }, + static const sd_json_dispatch_field table[] = { + { "ociVersion", SD_JSON_VARIANT_STRING, NULL, 0, SD_JSON_MANDATORY }, + { "process", SD_JSON_VARIANT_OBJECT, oci_process, 0, 0 }, + { "root", SD_JSON_VARIANT_OBJECT, oci_root, 0, 0 }, + { "hostname", SD_JSON_VARIANT_STRING, oci_hostname, 0, 0 }, + { "mounts", SD_JSON_VARIANT_ARRAY, oci_mounts, 0, 0 }, + { "linux", SD_JSON_VARIANT_OBJECT, oci_linux, 0, 0 }, + { "hooks", SD_JSON_VARIANT_OBJECT, oci_hooks, 0, 0 }, + { "annotations", SD_JSON_VARIANT_OBJECT, oci_annotations, 0, 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *oci = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *oci = NULL; _cleanup_(settings_freep) Settings *s = NULL; unsigned line = 0, column = 0; - JsonVariant *v; + sd_json_variant *v; const char *path; int r; @@ -2143,7 +2111,7 @@ int oci_load(FILE *f, const char *bundle, Settings **ret) { path = strjoina(bundle, "/config.json"); - r = json_parse_file(f, path, 0, &oci, &line, &column); + r = sd_json_parse_file(f, path, 0, &oci, &line, &column); if (r < 0) { if (line != 0 && column != 0) return log_error_errno(r, "Failed to parse '%s' at %u:%u: %m", path, line, column); @@ -2151,19 +2119,19 @@ int oci_load(FILE *f, const char *bundle, Settings **ret) { return log_error_errno(r, "Failed to parse '%s': %m", path); } - v = json_variant_by_key(oci, "ociVersion"); + v = sd_json_variant_by_key(oci, "ociVersion"); if (!v) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "JSON file '%s' is not an OCI bundle configuration file. Refusing.", path); - if (!streq_ptr(json_variant_string(v), "1.0.0")) + if (!streq_ptr(sd_json_variant_string(v), "1.0.0")) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "OCI bundle version not supported: %s", - strna(json_variant_string(v))); + strna(sd_json_variant_string(v))); // { // _cleanup_free_ char *formatted = NULL; - // assert_se(json_variant_format(oci, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, &formatted) >= 0); + // assert_se(json_variant_format(oci, SD_JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, &formatted) >= 0); // fputs(formatted, stdout); // } diff --git a/src/nsresourced/nsresourcework.c b/src/nsresourced/nsresourcework.c index 6bd2fed06f..0d06881839 100644 --- a/src/nsresourced/nsresourcework.c +++ b/src/nsresourced/nsresourcework.c @@ -16,6 +16,7 @@ #include "fs-util.h" #include "group-record.h" #include "io-util.h" +#include "json-util.h" #include "lock-util.h" #include "main-func.h" #include "missing_magic.h" @@ -60,7 +61,7 @@ typedef struct LookupParameters { const char *service; } LookupParameters; -static int build_user_json(UserNamespaceInfo *userns_info, uid_t offset, JsonVariant **ret) { +static int build_user_json(UserNamespaceInfo *userns_info, uid_t offset, sd_json_variant **ret) { _cleanup_free_ char *name = NULL, *realname = NULL; UserDisposition disposition; int r; @@ -81,29 +82,29 @@ static int build_user_json(UserNamespaceInfo *userns_info, uid_t offset, JsonVar if (r < 0) return -ENOMEM; - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(userns_info->start + offset)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(GID_NOBODY)), - JSON_BUILD_PAIR("realName", JSON_BUILD_STRING(realname)), - JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/")), - JSON_BUILD_PAIR("shell", JSON_BUILD_STRING(NOLOGIN)), - JSON_BUILD_PAIR("locked", JSON_BUILD_BOOLEAN(true)), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NamespaceResource")), - JSON_BUILD_PAIR("disposition", JSON_BUILD_STRING(user_disposition_to_string(disposition))))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(userns_info->start + offset)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(GID_NOBODY)), + SD_JSON_BUILD_PAIR("realName", SD_JSON_BUILD_STRING(realname)), + SD_JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/")), + SD_JSON_BUILD_PAIR("shell", SD_JSON_BUILD_STRING(NOLOGIN)), + SD_JSON_BUILD_PAIR("locked", SD_JSON_BUILD_BOOLEAN(true)), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NamespaceResource")), + SD_JSON_BUILD_PAIR("disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(disposition))))); } -static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; _cleanup_(userns_info_freep) UserNamespaceInfo *userns_info = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; LookupParameters p = { .uid = UID_INVALID, }; @@ -184,13 +185,13 @@ static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, Var if (r < 0) return r; - return varlink_replyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("record", JSON_BUILD_VARIANT(v)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_VARIANT(v)))); not_found: return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); } -static int build_group_json(UserNamespaceInfo *userns_info, gid_t offset, JsonVariant **ret) { +static int build_group_json(UserNamespaceInfo *userns_info, gid_t offset, sd_json_variant **ret) { _cleanup_free_ char *name = NULL, *description = NULL; UserDisposition disposition; int r; @@ -211,25 +212,25 @@ static int build_group_json(UserNamespaceInfo *userns_info, gid_t offset, JsonVa if (r < 0) return -ENOMEM; - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(userns_info->start + offset)), - JSON_BUILD_PAIR("description", JSON_BUILD_STRING(description)), - JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NamespaceResource")), - JSON_BUILD_PAIR("disposition", JSON_BUILD_STRING(user_disposition_to_string(disposition))))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(userns_info->start + offset)), + SD_JSON_BUILD_PAIR("description", SD_JSON_BUILD_STRING(description)), + SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.NamespaceResource")), + SD_JSON_BUILD_PAIR("disposition", SD_JSON_BUILD_STRING(user_disposition_to_string(disposition))))); } -static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; _cleanup_(userns_info_freep) UserNamespaceInfo *userns_info = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; LookupParameters p = { .gid = GID_INVALID, }; @@ -310,17 +311,17 @@ static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, Va if (r < 0) return r; - return varlink_replyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("record", JSON_BUILD_VARIANT(v)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_VARIANT(v)))); not_found: return varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); } -static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, +static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; @@ -724,13 +725,13 @@ typedef struct AllocateParameters { unsigned userns_fd_idx; } AllocateParameters; -static int vl_method_allocate_user_range(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_allocate_user_range(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "name", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(AllocateParameters, name), JSON_MANDATORY }, - { "size", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AllocateParameters, size), JSON_MANDATORY }, - { "target", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AllocateParameters, target), 0 }, - { "userNamespaceFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AllocateParameters, userns_fd_idx), JSON_MANDATORY }, + static const sd_json_dispatch_field dispatch_table[] = { + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(AllocateParameters, name), SD_JSON_MANDATORY }, + { "size", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AllocateParameters, size), SD_JSON_MANDATORY }, + { "target", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AllocateParameters, target), 0 }, + { "userNamespaceFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AllocateParameters, userns_fd_idx), SD_JSON_MANDATORY }, {} }; @@ -851,7 +852,7 @@ static int vl_method_allocate_user_range(Varlink *link, JsonVariant *parameters, /* Note, we'll not return UID values from the host, since the child might not run in the same * user namespace as us. If they want to know the ranges they should read them off the userns fd, so * that they are translated into their PoV */ - return varlink_replyb(link, JSON_BUILD_EMPTY_OBJECT); + return varlink_replyb(link, SD_JSON_BUILD_EMPTY_OBJECT); fail: /* Note: we don't have to clean-up the BPF maps in the error path: the bpf map type used will @@ -925,11 +926,11 @@ typedef struct RegisterParameters { unsigned userns_fd_idx; } RegisterParameters; -static int vl_method_register_user_namespace(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_register_user_namespace(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "name", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(RegisterParameters, name), JSON_MANDATORY }, - { "userNamespaceFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(RegisterParameters, userns_fd_idx), JSON_MANDATORY }, + static const sd_json_dispatch_field dispatch_table[] = { + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(RegisterParameters, name), SD_JSON_MANDATORY }, + { "userNamespaceFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(RegisterParameters, userns_fd_idx), SD_JSON_MANDATORY }, {} }; @@ -1039,7 +1040,7 @@ static int vl_method_register_user_namespace(Varlink *link, JsonVariant *paramet if (r < 0) goto fail; - return varlink_replyb(link, JSON_BUILD_EMPTY_OBJECT); + return varlink_replyb(link, SD_JSON_BUILD_EMPTY_OBJECT); fail: userns_registry_remove(registry_dir_fd, userns_info); @@ -1051,11 +1052,11 @@ typedef struct AddMountParameters { unsigned mount_fd_idx; } AddMountParameters; -static int vl_method_add_mount_to_user_namespace(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_add_mount_to_user_namespace(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch parameter_dispatch_table[] = { - { "userNamespaceFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AddMountParameters, userns_fd_idx), JSON_MANDATORY }, - { "mountFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AddMountParameters, mount_fd_idx), JSON_MANDATORY }, + static const sd_json_dispatch_field parameter_dispatch_table[] = { + { "userNamespaceFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AddMountParameters, userns_fd_idx), SD_JSON_MANDATORY }, + { "mountFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AddMountParameters, mount_fd_idx), SD_JSON_MANDATORY }, {} }; @@ -1166,7 +1167,7 @@ static int vl_method_add_mount_to_user_namespace(Varlink *link, JsonVariant *par log_debug("Granting access to mount %i to user namespace " INO_FMT " ('%s')", mnt_id, userns_st.st_ino, userns_info->name); - return varlink_replyb(link, JSON_BUILD_EMPTY_OBJECT); + return varlink_replyb(link, SD_JSON_BUILD_EMPTY_OBJECT); } static int validate_cgroup(Varlink *link, int fd, uint64_t *ret_cgroup_id) { @@ -1202,10 +1203,10 @@ typedef struct AddCGroupParameters { unsigned cgroup_fd_idx; } AddCGroupParameters; -static int vl_method_add_cgroup_to_user_namespace(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch parameter_dispatch_table[] = { - { "userNamespaceFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AddCGroupParameters, userns_fd_idx), JSON_MANDATORY }, - { "controlGroupFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AddCGroupParameters, cgroup_fd_idx), JSON_MANDATORY }, +static int vl_method_add_cgroup_to_user_namespace(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field parameter_dispatch_table[] = { + { "userNamespaceFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AddCGroupParameters, userns_fd_idx), SD_JSON_MANDATORY }, + { "controlGroupFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AddCGroupParameters, cgroup_fd_idx), SD_JSON_MANDATORY }, {} }; @@ -1314,7 +1315,7 @@ static int vl_method_add_cgroup_to_user_namespace(Varlink *link, JsonVariant *pa log_debug("Granting ownership to cgroup %" PRIu64 " to userns " INO_FMT " ('%s' @ UID " UID_FMT ")", cgroup_id, userns_st.st_ino, userns_info->name, userns_info->start); - return varlink_replyb(link, JSON_BUILD_EMPTY_OBJECT); + return varlink_replyb(link, SD_JSON_BUILD_EMPTY_OBJECT); } static uint64_t hash_ifname_id(UserNamespaceInfo *userns_info, const char *ifname) { @@ -1501,12 +1502,12 @@ typedef struct AddNetworkParameters { const char *mode; } AddNetworkParameters; -static int vl_method_add_netif_to_user_namespace(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch parameter_dispatch_table[] = { - { "userNamespaceFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AddNetworkParameters, userns_fd_idx), JSON_MANDATORY }, - { "networkNamespaceFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(AddNetworkParameters, netns_fd_idx), JSON_MANDATORY }, - { "namespaceInterfaceName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(AddNetworkParameters, ifname), 0 }, - { "mode", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(AddNetworkParameters, mode), JSON_MANDATORY }, +static int vl_method_add_netif_to_user_namespace(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field parameter_dispatch_table[] = { + { "userNamespaceFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AddNetworkParameters, userns_fd_idx), SD_JSON_MANDATORY }, + { "networkNamespaceFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(AddNetworkParameters, netns_fd_idx), SD_JSON_MANDATORY }, + { "namespaceInterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(AddNetworkParameters, ifname), 0 }, + { "mode", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(AddNetworkParameters, mode), SD_JSON_MANDATORY }, {} }; @@ -1611,8 +1612,8 @@ static int vl_method_add_netif_to_user_namespace(Varlink *link, JsonVariant *par log_debug("Adding veth tunnel %s from host to userns " INO_FMT " ('%s' @ UID " UID_FMT ", interface %s).", ifname_host, userns_st.st_ino, userns_info->name, userns_info->start, ifname_namespace); - return varlink_replyb(link, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("hostInterfaceName", JSON_BUILD_STRING(ifname_host)), - JSON_BUILD_PAIR("namespaceInterfaceName", JSON_BUILD_STRING(ifname_namespace)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("hostInterfaceName", SD_JSON_BUILD_STRING(ifname_host)), + SD_JSON_BUILD_PAIR("namespaceInterfaceName", SD_JSON_BUILD_STRING(ifname_namespace)))); } static int process_connection(VarlinkServer *server, int _fd) { diff --git a/src/nsresourced/userns-registry.c b/src/nsresourced/userns-registry.c index 2cc1b1f9ef..13f94aa751 100644 --- a/src/nsresourced/userns-registry.c +++ b/src/nsresourced/userns-registry.c @@ -1,11 +1,13 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "chase.h" #include "fd-util.h" #include "fileio.h" #include "format-util.h" #include "fs-util.h" -#include "json.h" +#include "json-util.h" #include "missing_magic.h" #include "path-util.h" #include "recurse-dir.h" @@ -70,43 +72,43 @@ UserNamespaceInfo *userns_info_free(UserNamespaceInfo *userns) { return mfree(userns); } -static int dispatch_cgroups_array(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_cgroups_array(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { UserNamespaceInfo *info = ASSERT_PTR(userdata); _cleanup_free_ uint64_t *cgroups = NULL; size_t n_cgroups = 0; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { info->cgroups = mfree(info->cgroups); info->n_cgroups = 0; return 0; } - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); - cgroups = new(uint64_t, json_variant_elements(variant)); + cgroups = new(uint64_t, sd_json_variant_elements(variant)); if (!cgroups) return json_log_oom(variant, flags); - JsonVariant *e; + sd_json_variant *e; JSON_VARIANT_ARRAY_FOREACH(e, variant) { bool found = false; - if (!json_variant_is_unsigned(e)) + if (!sd_json_variant_is_unsigned(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a number."); FOREACH_ARRAY(cg, cgroups, n_cgroups) - if (*cg == json_variant_unsigned(e)) { + if (*cg == sd_json_variant_unsigned(e)) { found = true; break; } if (found) /* suppress duplicate */ continue; - cgroups[n_cgroups++] = json_variant_unsigned(e); + cgroups[n_cgroups++] = sd_json_variant_unsigned(e); } - assert(n_cgroups <= json_variant_elements(variant)); + assert(n_cgroups <= sd_json_variant_elements(variant)); free_and_replace(info->cgroups, cgroups); info->n_cgroups = n_cgroups; @@ -116,19 +118,19 @@ static int dispatch_cgroups_array(const char *name, JsonVariant *variant, JsonDi static int userns_registry_load(int dir_fd, const char *fn, UserNamespaceInfo **ret) { - static const JsonDispatch dispatch_table[] = { - { "owner", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserNamespaceInfo, owner), JSON_MANDATORY }, - { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserNamespaceInfo, name), JSON_MANDATORY }, - { "userns", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(UserNamespaceInfo, userns_inode), JSON_MANDATORY }, - { "start", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserNamespaceInfo, start), 0 }, - { "size", JSON_VARIANT_UNSIGNED, json_dispatch_uint32, offsetof(UserNamespaceInfo, size), 0 }, - { "target", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserNamespaceInfo, target), 0 }, - { "cgroups", JSON_VARIANT_ARRAY, dispatch_cgroups_array, 0, 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "owner", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserNamespaceInfo, owner), SD_JSON_MANDATORY }, + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserNamespaceInfo, name), SD_JSON_MANDATORY }, + { "userns", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(UserNamespaceInfo, userns_inode), SD_JSON_MANDATORY }, + { "start", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserNamespaceInfo, start), 0 }, + { "size", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint32, offsetof(UserNamespaceInfo, size), 0 }, + { "target", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserNamespaceInfo, target), 0 }, + { "cgroups", SD_JSON_VARIANT_ARRAY, dispatch_cgroups_array, 0, 0 }, {} }; _cleanup_(userns_info_freep) UserNamespaceInfo *userns_info = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_close_ int registry_fd = -EBADF; int r; @@ -140,7 +142,7 @@ static int userns_registry_load(int dir_fd, const char *fn, UserNamespaceInfo ** dir_fd = registry_fd; } - r = json_parse_file_at(NULL, dir_fd, fn, 0, &v, NULL, NULL); + r = sd_json_parse_file_at(NULL, dir_fd, fn, 0, &v, NULL, NULL); if (r < 0) return r; @@ -148,7 +150,7 @@ static int userns_registry_load(int dir_fd, const char *fn, UserNamespaceInfo ** if (!userns_info) return -ENOMEM; - r = json_dispatch(v, dispatch_table, 0, userns_info); + r = sd_json_dispatch(v, dispatch_table, 0, userns_info); if (r < 0) return r; @@ -349,29 +351,29 @@ int userns_registry_store(int dir_fd, UserNamespaceInfo *info) { dir_fd = registry_fd; } - _cleanup_(json_variant_unrefp) JsonVariant *cgroup_array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cgroup_array = NULL; FOREACH_ARRAY(cg, info->cgroups, info->n_cgroups) { - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &cgroup_array, - JSON_BUILD_UNSIGNED(*cg)); + SD_JSON_BUILD_UNSIGNED(*cg)); if (r < 0) return r; } - _cleanup_(json_variant_unrefp) JsonVariant *def = NULL; - r = json_build(&def, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("owner", JSON_BUILD_UNSIGNED(info->owner)), - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(info->name)), - JSON_BUILD_PAIR("userns", JSON_BUILD_UNSIGNED(info->userns_inode)), - JSON_BUILD_PAIR_CONDITION(uid_is_valid(info->start), "start", JSON_BUILD_UNSIGNED(info->start)), - JSON_BUILD_PAIR_CONDITION(uid_is_valid(info->start), "size", JSON_BUILD_UNSIGNED(info->size)), - JSON_BUILD_PAIR_CONDITION(uid_is_valid(info->start), "target", JSON_BUILD_UNSIGNED(info->target)), - JSON_BUILD_PAIR_CONDITION(cgroup_array, "cgroups", JSON_BUILD_VARIANT(cgroup_array)))); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *def = NULL; + r = sd_json_build(&def, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("owner", SD_JSON_BUILD_UNSIGNED(info->owner)), + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(info->name)), + SD_JSON_BUILD_PAIR("userns", SD_JSON_BUILD_UNSIGNED(info->userns_inode)), + SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(info->start), "start", SD_JSON_BUILD_UNSIGNED(info->start)), + SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(info->start), "size", SD_JSON_BUILD_UNSIGNED(info->size)), + SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(info->start), "target", SD_JSON_BUILD_UNSIGNED(info->target)), + SD_JSON_BUILD_PAIR_CONDITION(!!cgroup_array, "cgroups", SD_JSON_BUILD_VARIANT(cgroup_array)))); if (r < 0) return r; _cleanup_free_ char *def_buf = NULL; - r = json_variant_format(def, 0, &def_buf); + r = sd_json_variant_format(def, 0, &def_buf); if (r < 0) return log_debug_errno(r, "Failed to format userns JSON object: %m"); diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c index 3cefb6394c..21e278f5a2 100644 --- a/src/nss-resolve/nss-resolve.c +++ b/src/nss-resolve/nss-resolve.c @@ -12,6 +12,7 @@ #include "errno-util.h" #include "glyph-util.h" #include "in-addr-util.h" +#include "json-util.h" #include "macro.h" #include "nss-util.h" #include "resolved-def.h" @@ -20,13 +21,13 @@ #include "strv.h" #include "varlink.h" -static JsonDispatchFlags json_dispatch_flags = JSON_ALLOW_EXTENSIONS; +static sd_json_dispatch_flags_t json_dispatch_flags = SD_JSON_ALLOW_EXTENSIONS; static void setup_logging(void) { log_parse_environment_variables(); if (DEBUG_LOGGING) - json_dispatch_flags = JSON_LOG; + json_dispatch_flags = SD_JSON_LOG; } static void setup_logging_once(void) { @@ -94,16 +95,16 @@ static uint32_t ifindex_to_scopeid(int family, const void *a, int ifindex) { return in6_addr_is_link_local(&in6) ? ifindex : 0; } -static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_ifindex(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int *ifi = ASSERT_PTR(userdata); int64_t t; assert(variant); - if (!json_variant_is_integer(variant)) + if (!sd_json_variant_is_integer(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - t = json_variant_integer(variant); + t = sd_json_variant_integer(variant); if (t > INT_MAX) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is out of bounds for an interface index.", strna(name)); @@ -111,16 +112,16 @@ static int json_dispatch_ifindex(const char *name, JsonVariant *variant, JsonDis return 0; } -static int json_dispatch_family(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_family(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int *family = ASSERT_PTR(userdata); int64_t t; assert(variant); - if (!json_variant_is_integer(variant)) + if (!sd_json_variant_is_integer(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - t = json_variant_integer(variant); + t = sd_json_variant_integer(variant); if (t < 0 || t > INT_MAX) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid family.", strna(name)); @@ -129,7 +130,7 @@ static int json_dispatch_family(const char *name, JsonVariant *variant, JsonDisp } typedef struct ResolveHostnameReply { - JsonVariant *addresses; + sd_json_variant *addresses; char *name; uint64_t flags; } ResolveHostnameReply; @@ -137,14 +138,14 @@ typedef struct ResolveHostnameReply { static void resolve_hostname_reply_destroy(ResolveHostnameReply *p) { assert(p); - json_variant_unref(p->addresses); + sd_json_variant_unref(p->addresses); free(p->name); } -static const JsonDispatch resolve_hostname_reply_dispatch_table[] = { - { "addresses", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(ResolveHostnameReply, addresses), JSON_MANDATORY }, - { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(ResolveHostnameReply, name), 0 }, - { "flags", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(ResolveHostnameReply, flags), 0 }, +static const sd_json_dispatch_field resolve_hostname_reply_dispatch_table[] = { + { "addresses", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(ResolveHostnameReply, addresses), SD_JSON_MANDATORY }, + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(ResolveHostnameReply, name), 0 }, + { "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(ResolveHostnameReply, flags), 0 }, {} }; @@ -155,28 +156,28 @@ typedef struct AddressParameters { size_t address_size; } AddressParameters; -static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_address(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { AddressParameters *p = ASSERT_PTR(userdata); union in_addr_union buf = {}; - JsonVariant *i; + sd_json_variant *i; size_t n, k = 0; assert(variant); - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); - n = json_variant_elements(variant); + n = sd_json_variant_elements(variant); if (!IN_SET(n, 4, 16)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(i, variant) { int64_t b; - if (!json_variant_is_integer(i)) + if (!sd_json_variant_is_integer(i)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an integer.", k, strna(name)); - b = json_variant_integer(i); + b = sd_json_variant_integer(i); if (b < 0 || b > 0xff) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is out of range 0%s255.", @@ -191,10 +192,10 @@ static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDis return 0; } -static const JsonDispatch address_parameters_dispatch_table[] = { - { "ifindex", JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(AddressParameters, ifindex), 0 }, - { "family", JSON_VARIANT_INTEGER, json_dispatch_family, offsetof(AddressParameters, family), JSON_MANDATORY }, - { "address", JSON_VARIANT_ARRAY, json_dispatch_address, 0, JSON_MANDATORY }, +static const sd_json_dispatch_field address_parameters_dispatch_table[] = { + { "ifindex", SD_JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(AddressParameters, ifindex), 0 }, + { "family", SD_JSON_VARIANT_INTEGER, json_dispatch_family, offsetof(AddressParameters, family), SD_JSON_MANDATORY }, + { "address", SD_JSON_VARIANT_ARRAY, json_dispatch_address, 0, SD_JSON_MANDATORY }, {} }; @@ -232,9 +233,9 @@ enum nss_status _nss_resolve_gethostbyname4_r( int32_t *ttlp) { _cleanup_(varlink_unrefp) Varlink *link = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cparams = NULL; _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; - JsonVariant *rparams, *entry; + sd_json_variant *rparams, *entry; int r; PROTECT_ERRNO; @@ -250,9 +251,9 @@ enum nss_status _nss_resolve_gethostbyname4_r( if (r < 0) goto fail; - r = json_build(&cparams, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); + r = sd_json_build(&cparams, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(query_flags())))); if (r < 0) goto fail; @@ -275,17 +276,17 @@ enum nss_status _nss_resolve_gethostbyname4_r( goto not_found; } - r = json_dispatch(rparams, resolve_hostname_reply_dispatch_table, json_dispatch_flags, &p); + r = sd_json_dispatch(rparams, resolve_hostname_reply_dispatch_table, json_dispatch_flags, &p); if (r < 0) goto fail; - if (json_variant_is_blank_object(p.addresses)) + if (sd_json_variant_is_blank_object(p.addresses)) goto not_found; size_t n_addresses = 0; JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; - r = json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; @@ -323,7 +324,7 @@ enum nss_status _nss_resolve_gethostbyname4_r( JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; - r = json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; @@ -391,9 +392,9 @@ enum nss_status _nss_resolve_gethostbyname3_r( char **canonp) { _cleanup_(varlink_unrefp) Varlink *link = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cparams = NULL; _cleanup_(resolve_hostname_reply_destroy) ResolveHostnameReply p = {}; - JsonVariant *rparams, *entry; + sd_json_variant *rparams, *entry; int r; PROTECT_ERRNO; @@ -417,9 +418,9 @@ enum nss_status _nss_resolve_gethostbyname3_r( if (r < 0) goto fail; - r = json_build(&cparams, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("family", JSON_BUILD_INTEGER(af)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); + r = sd_json_build(&cparams, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("family", SD_JSON_BUILD_INTEGER(af)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(query_flags())))); if (r < 0) goto fail; @@ -437,17 +438,17 @@ enum nss_status _nss_resolve_gethostbyname3_r( goto not_found; } - r = json_dispatch(rparams, resolve_hostname_reply_dispatch_table, json_dispatch_flags, &p); + r = sd_json_dispatch(rparams, resolve_hostname_reply_dispatch_table, json_dispatch_flags, &p); if (r < 0) goto fail; - if (json_variant_is_blank_object(p.addresses)) + if (sd_json_variant_is_blank_object(p.addresses)) goto not_found; size_t n_addresses = 0; JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; - r = json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; @@ -493,7 +494,7 @@ enum nss_status _nss_resolve_gethostbyname3_r( JSON_VARIANT_ARRAY_FOREACH(entry, p.addresses) { AddressParameters q = {}; - r = json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, address_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; @@ -563,19 +564,19 @@ try_again: } typedef struct ResolveAddressReply { - JsonVariant *names; + sd_json_variant *names; uint64_t flags; } ResolveAddressReply; static void resolve_address_reply_destroy(ResolveAddressReply *p) { assert(p); - json_variant_unref(p->names); + sd_json_variant_unref(p->names); } -static const JsonDispatch resolve_address_reply_dispatch_table[] = { - { "names", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(ResolveAddressReply, names), JSON_MANDATORY }, - { "flags", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(ResolveAddressReply, flags), 0 }, +static const sd_json_dispatch_field resolve_address_reply_dispatch_table[] = { + { "names", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(ResolveAddressReply, names), SD_JSON_MANDATORY }, + { "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(ResolveAddressReply, flags), 0 }, {} }; @@ -590,9 +591,9 @@ static void name_parameters_destroy(NameParameters *p) { free(p->name); } -static const JsonDispatch name_parameters_dispatch_table[] = { - { "ifindex", JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(NameParameters, ifindex), 0 }, - { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(NameParameters, name), JSON_MANDATORY }, +static const sd_json_dispatch_field name_parameters_dispatch_table[] = { + { "ifindex", SD_JSON_VARIANT_INTEGER, json_dispatch_ifindex, offsetof(NameParameters, ifindex), 0 }, + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(NameParameters, name), SD_JSON_MANDATORY }, {} }; @@ -605,9 +606,9 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( int32_t *ttlp) { _cleanup_(varlink_unrefp) Varlink *link = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cparams = NULL; _cleanup_(resolve_address_reply_destroy) ResolveAddressReply p = {}; - JsonVariant *rparams, *entry; + sd_json_variant *rparams, *entry; int r; PROTECT_ERRNO; @@ -635,9 +636,9 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( if (r < 0) goto fail; - r = json_build(&cparams, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("address", JSON_BUILD_BYTE_ARRAY(addr, len)), - JSON_BUILD_PAIR("family", JSON_BUILD_INTEGER(af)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(query_flags())))); + r = sd_json_build(&cparams, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("address", SD_JSON_BUILD_BYTE_ARRAY(addr, len)), + SD_JSON_BUILD_PAIR("family", SD_JSON_BUILD_INTEGER(af)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(query_flags())))); if (r < 0) goto fail; @@ -653,10 +654,10 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( goto not_found; } - r = json_dispatch(rparams, resolve_address_reply_dispatch_table, json_dispatch_flags, &p); + r = sd_json_dispatch(rparams, resolve_address_reply_dispatch_table, json_dispatch_flags, &p); if (r < 0) goto fail; - if (json_variant_is_blank_object(p.names)) + if (sd_json_variant_is_blank_object(p.names)) goto not_found; size_t ms = 0, idx; @@ -664,14 +665,14 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( JSON_VARIANT_ARRAY_FOREACH(entry, p.names) { _cleanup_(name_parameters_destroy) NameParameters q = {}; - r = json_dispatch(entry, name_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, name_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; ms += ALIGN(strlen(q.name) + 1); } - size_t n_names = json_variant_elements(p.names); + size_t n_names = sd_json_variant_elements(p.names); ms += ALIGN(len) + /* the address */ 2 * sizeof(char*) + /* pointer to the address, plus trailing NULL */ n_names * sizeof(char*); /* pointers to aliases, plus trailing NULL */ @@ -705,7 +706,7 @@ enum nss_status _nss_resolve_gethostbyaddr2_r( JSON_VARIANT_ARRAY_FOREACH(entry, p.names) { _cleanup_(name_parameters_destroy) NameParameters q = {}; - r = json_dispatch(entry, name_parameters_dispatch_table, json_dispatch_flags, &q); + r = sd_json_dispatch(entry, name_parameters_dispatch_table, json_dispatch_flags, &q); if (r < 0) goto fail; diff --git a/src/oom/oomd-manager.c b/src/oom/oomd-manager.c index 839936ce7c..43d2fcb43b 100644 --- a/src/oom/oomd-manager.c +++ b/src/oom/oomd-manager.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "sd-daemon.h" +#include "sd-json.h" #include "bus-log-control-api.h" #include "bus-util.h" @@ -9,6 +10,7 @@ #include "fd-util.h" #include "fileio.h" #include "format-util.h" +#include "json-util.h" #include "memory-util.h" #include "memstream-util.h" #include "oomd-manager-bus.h" @@ -32,22 +34,22 @@ static void managed_oom_message_destroy(ManagedOOMMessage *message) { static JSON_DISPATCH_ENUM_DEFINE(dispatch_managed_oom_mode, ManagedOOMMode, managed_oom_mode_from_string); -static int process_managed_oom_message(Manager *m, uid_t uid, JsonVariant *parameters) { - JsonVariant *c, *cgroups; +static int process_managed_oom_message(Manager *m, uid_t uid, sd_json_variant *parameters) { + sd_json_variant *c, *cgroups; int r; - static const JsonDispatch dispatch_table[] = { - { "mode", JSON_VARIANT_STRING, dispatch_managed_oom_mode, offsetof(ManagedOOMMessage, mode), JSON_MANDATORY }, - { "path", JSON_VARIANT_STRING, json_dispatch_string, offsetof(ManagedOOMMessage, path), JSON_MANDATORY }, - { "property", JSON_VARIANT_STRING, json_dispatch_string, offsetof(ManagedOOMMessage, property), JSON_MANDATORY }, - { "limit", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint32, offsetof(ManagedOOMMessage, limit), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "mode", SD_JSON_VARIANT_STRING, dispatch_managed_oom_mode, offsetof(ManagedOOMMessage, mode), SD_JSON_MANDATORY }, + { "path", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(ManagedOOMMessage, path), SD_JSON_MANDATORY }, + { "property", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(ManagedOOMMessage, property), SD_JSON_MANDATORY }, + { "limit", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(ManagedOOMMessage, limit), 0 }, {}, }; assert(m); assert(parameters); - cgroups = json_variant_by_key(parameters, "cgroups"); + cgroups = sd_json_variant_by_key(parameters, "cgroups"); if (!cgroups) return -EINVAL; @@ -58,10 +60,10 @@ static int process_managed_oom_message(Manager *m, uid_t uid, JsonVariant *param Hashmap *monitor_hm; loadavg_t limit; - if (!json_variant_is_object(c)) + if (!sd_json_variant_is_object(c)) continue; - r = json_dispatch(c, dispatch_table, 0, &message); + r = sd_json_dispatch(c, dispatch_table, 0, &message); if (r == -ENOMEM) return r; if (r < 0) @@ -126,7 +128,7 @@ static int process_managed_oom_message(Manager *m, uid_t uid, JsonVariant *param static int process_managed_oom_request( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { Manager *m = ASSERT_PTR(userdata); @@ -142,7 +144,7 @@ static int process_managed_oom_request( static int process_managed_oom_reply( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { diff --git a/src/partition/repart.c b/src/partition/repart.c index 78cf60f724..8599d8f85e 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -14,6 +14,7 @@ #include "sd-device.h" #include "sd-id128.h" +#include "sd-json.h" #include "alloc-util.h" #include "blkid-util.h" @@ -43,7 +44,7 @@ #include "id128-util.h" #include "initrd-util.h" #include "io-util.h" -#include "json.h" +#include "json-util.h" #include "list.h" #include "loop-util.h" #include "main-func.h" @@ -140,7 +141,7 @@ static bool arg_randomize = false; static int arg_pretty = -1; static uint64_t arg_size = UINT64_MAX; static bool arg_size_auto = false; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; static void *arg_key = NULL; @@ -2980,7 +2981,7 @@ static int context_dump_partitions(Context *context) { const size_t roothash_col = 14, dropin_files_col = 15, split_path_col = 16; bool has_roothash = false, has_dropin_files = false, has_split_path = false; - if ((arg_json_format_flags & JSON_FORMAT_OFF) && context->n_partitions == 0) { + if ((arg_json_format_flags & SD_JSON_FORMAT_OFF) && context->n_partitions == 0) { log_info("Empty partition table."); return 0; } @@ -3006,7 +3007,7 @@ static int context_dump_partitions(Context *context) { return log_oom(); if (!DEBUG_LOGGING) { - if (arg_json_format_flags & JSON_FORMAT_OFF) + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4, (size_t) 8, (size_t) 9, (size_t) 12, roothash_col, dropin_files_col, split_path_col); @@ -3089,7 +3090,7 @@ static int context_dump_partitions(Context *context) { has_split_path = has_split_path || !isempty(p->split_path); } - if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) { + if ((arg_json_format_flags & SD_JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) { const char *a, *b; a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size)); @@ -3356,17 +3357,17 @@ static int context_dump(Context *context, bool late) { assert(context); - if (arg_pretty == 0 && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (arg_pretty == 0 && FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return 0; /* If we're outputting JSON, only dump after doing all operations so we can include the roothashes * in the output. */ - if (!late && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!late && !FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return 0; /* If we're not outputting JSON, only dump again after doing all operations if there are any * roothashes that we need to communicate to the user. */ - if (late && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !context_has_roothash(context)) + if (late && FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF) && !context_has_roothash(context)) return 0; r = context_dump_partitions(context); @@ -3375,7 +3376,7 @@ static int context_dump(Context *context, bool late) { /* Only write the partition bar once, even if we're writing the partition table twice to communicate * roothashes. */ - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !late) { + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF) && !late) { putc('\n', stdout); r = context_dump_partition_bar(context); @@ -4025,7 +4026,7 @@ static int partition_encrypt(Context *context, Partition *p, PartitionTarget *ta #if HAVE_TPM2 _cleanup_(iovec_done) struct iovec pubkey = {}, blob = {}, srk = {}; _cleanup_(iovec_done_erase) struct iovec secret = {}; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; ssize_t base64_encoded_size; int keyslot; TPM2Flags flags = 0; @@ -4433,7 +4434,7 @@ static int sign_verity_roothash( } static int partition_format_verity_sig(Context *context, Partition *p) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(iovec_done) struct iovec sig = {}; _cleanup_free_ char *text = NULL, *hint = NULL; Partition *hp; @@ -4465,20 +4466,17 @@ static int partition_format_verity_sig(Context *context, Partition *p) { if (r < 0) return log_error_errno(r, "Unable to calculate X509 certificate fingerprint: %m"); - r = json_build(&v, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("rootHash", JSON_BUILD_HEX(hp->roothash.iov_base, hp->roothash.iov_len)), - JSON_BUILD_PAIR( - "certificateFingerprint", - JSON_BUILD_HEX(fp, sizeof(fp)) - ), - JSON_BUILD_PAIR("signature", JSON_BUILD_IOVEC_BASE64(&sig)) + r = sd_json_build(&v, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("rootHash", SD_JSON_BUILD_HEX(hp->roothash.iov_base, hp->roothash.iov_len)), + SD_JSON_BUILD_PAIR("certificateFingerprint", SD_JSON_BUILD_HEX(fp, sizeof(fp))), + SD_JSON_BUILD_PAIR("signature", JSON_BUILD_IOVEC_BASE64(&sig)) ) ); if (r < 0) return log_error_errno(r, "Failed to build verity signature JSON object: %m"); - r = json_variant_format(v, 0, &text); + r = sd_json_variant_format(v, 0, &text); if (r < 0) return log_error_errno(r, "Failed to format verity signature JSON object: %m"); diff --git a/src/pcrextend/pcrextend.c b/src/pcrextend/pcrextend.c index ba2b171250..f1142f445c 100644 --- a/src/pcrextend/pcrextend.c +++ b/src/pcrextend/pcrextend.c @@ -2,11 +2,13 @@ #include <getopt.h> -#include <sd-messages.h> +#include "sd-json.h" +#include "sd-messages.h" #include "build.h" #include "efi-loader.h" #include "escape.h" +#include "json-util.h" #include "main-func.h" #include "openssl-util.h" #include "parse-argument.h" @@ -256,12 +258,12 @@ static void method_extend_parameters_done(MethodExtendParameters *p) { iovec_done(&p->data); } -static int vl_method_extend(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_extend(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "pcr", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(MethodExtendParameters, pcr), JSON_MANDATORY }, - { "text", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(MethodExtendParameters, text), 0 }, - { "data", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(MethodExtendParameters, data), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "pcr", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(MethodExtendParameters, pcr), SD_JSON_MANDATORY }, + { "text", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodExtendParameters, text), 0 }, + { "data", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(MethodExtendParameters, data), 0 }, {} }; _cleanup_(method_extend_parameters_done) MethodExtendParameters p = { diff --git a/src/pcrlock/pcrlock.c b/src/pcrlock/pcrlock.c index 1716fb3219..c07132c135 100644 --- a/src/pcrlock/pcrlock.c +++ b/src/pcrlock/pcrlock.c @@ -6,6 +6,7 @@ #include <sys/file.h> #include "sd-device.h" +#include "sd-json.h" #include "ask-password-api.h" #include "blockdev-util.h" @@ -28,6 +29,7 @@ #include "hash-funcs.h" #include "hexdecoct.h" #include "initrd-util.h" +#include "json-util.h" #include "main-func.h" #include "mkdir-label.h" #include "openssl-util.h" @@ -62,7 +64,7 @@ typedef enum RecoveryPinMode { } RecoveryPinMode; static PagerFlags arg_pager_flags = 0; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF|JSON_FORMAT_NEWLINE; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_NEWLINE; static char **arg_components = NULL; static uint32_t arg_pcr_mask = 0; static char *arg_pcrlock_path = NULL; @@ -157,7 +159,7 @@ struct EventLogRecord { /* Data for userspace events (i.e. those generated by systemd in userspace */ Tpm2UserspaceEventType userspace_event_type; - JsonVariant *userspace_content; + sd_json_variant *userspace_content; /* Validation result for the event payload itself, if the record contains enough information to validate the hash */ EventPayloadValid event_payload_valid; @@ -246,7 +248,7 @@ static EventLogRecord *event_log_record_free(EventLogRecord *record) { free(record->description); free(record->firmware_payload); - json_variant_unref(record->userspace_content); + sd_json_variant_unref(record->userspace_content); while ((bank = LIST_POP(banks, record->banks))) event_log_record_bank_free(bank); @@ -995,58 +997,58 @@ static int event_log_load_firmware(EventLog *el) { return 0; } -static int event_log_record_parse_json(EventLogRecord *record, JsonVariant *j) { +static int event_log_record_parse_json(EventLogRecord *record, sd_json_variant *j) { const char *rectype = NULL; - JsonVariant *x, *k; + sd_json_variant *x, *k; uint64_t u; int r; assert(record); assert(j); - if (!json_variant_is_object(j)) + if (!sd_json_variant_is_object(j)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "record object is not an object."); - x = json_variant_by_key(j, "pcr"); + x = sd_json_variant_by_key(j, "pcr"); if (!x) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'pcr' field missing from TPM measurement log file entry."); - if (!json_variant_is_unsigned(x)) + if (!sd_json_variant_is_unsigned(x)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'pcr' field is not an integer."); - u = json_variant_unsigned(x); + u = sd_json_variant_unsigned(x); if (u >= TPM2_PCRS_MAX) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'pcr' field is out of range."); - record->pcr = json_variant_unsigned(x); + record->pcr = sd_json_variant_unsigned(x); - x = json_variant_by_key(j, "digests"); + x = sd_json_variant_by_key(j, "digests"); if (!x) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'digests' field missing from TPM measurement log file entry."); - if (!json_variant_is_array(x)) + if (!sd_json_variant_is_array(x)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'digests' field is not an array."); JSON_VARIANT_ARRAY_FOREACH(k, x) { _cleanup_free_ void *hash = NULL; size_t hash_size; - JsonVariant *a, *h; + sd_json_variant *a, *h; int na; - a = json_variant_by_key(k, "hashAlg"); + a = sd_json_variant_by_key(k, "hashAlg"); if (!a) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'digests' field element lacks 'hashAlg' field."); - if (!json_variant_is_string(a)) + if (!sd_json_variant_is_string(a)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'hashAlg' field is not a string."); - na = tpm2_hash_alg_from_string(json_variant_string(a)); + na = tpm2_hash_alg_from_string(sd_json_variant_string(a)); if (na < 0) { - log_debug_errno(na, "Unsupported hash '%s' in userspace event log, ignoring: %m", json_variant_string(a)); + log_debug_errno(na, "Unsupported hash '%s' in userspace event log, ignoring: %m", sd_json_variant_string(a)); continue; } - h = json_variant_by_key(k, "digest"); + h = sd_json_variant_by_key(k, "digest"); if (!h) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'digests' field lacks 'digest' field."); - r = json_variant_unhex(h, &hash, &hash_size); + r = sd_json_variant_unhex(h, &hash, &hash_size); if (r < 0) return log_error_errno(r, "Failed to decode digest: %m"); @@ -1060,47 +1062,47 @@ static int event_log_record_parse_json(EventLogRecord *record, JsonVariant *j) { return log_error_errno(r, "Failed to add bank to event log record: %m"); } - x = json_variant_by_key(j, "content_type"); + x = sd_json_variant_by_key(j, "content_type"); if (!x) log_debug("'content_type' missing from TPM measurement log file entry, ignoring."); else { - if (!json_variant_is_string(x)) + if (!sd_json_variant_is_string(x)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'content_type' field is not a string."); - rectype = json_variant_string(x); + rectype = sd_json_variant_string(x); } if (streq_ptr(rectype, "systemd")) { - JsonVariant *y; + sd_json_variant *y; - x = json_variant_by_key(j, "content"); + x = sd_json_variant_by_key(j, "content"); if (!x) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'content' field missing from TPM measurement log file entry."); - if (!json_variant_is_object(x)) + if (!sd_json_variant_is_object(x)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'content' sub-object is not an object."); - y = json_variant_by_key(x, "string"); + y = sd_json_variant_by_key(x, "string"); if (y) { - if (!json_variant_is_string(y)) + if (!sd_json_variant_is_string(y)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'string' field is not a string."); - r = free_and_strdup_warn(&record->description, json_variant_string(y)); + r = free_and_strdup_warn(&record->description, sd_json_variant_string(y)); if (r < 0) return r; } - y = json_variant_by_key(x, "eventType"); + y = sd_json_variant_by_key(x, "eventType"); if (y) { - if (!json_variant_is_string(y)) + if (!sd_json_variant_is_string(y)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "'eventType' field is not a string."); - record->userspace_event_type = tpm2_userspace_event_type_from_string(json_variant_string(y)); + record->userspace_event_type = tpm2_userspace_event_type_from_string(sd_json_variant_string(y)); if (record->userspace_event_type < 0) - log_debug_errno(record->userspace_event_type, "Unknown userspace event type '%s', ignoring.", json_variant_string(y)); + log_debug_errno(record->userspace_event_type, "Unknown userspace event type '%s', ignoring.", sd_json_variant_string(y)); } - json_variant_unref(record->userspace_content); - record->userspace_content = json_variant_ref(x); + sd_json_variant_unref(record->userspace_content); + record->userspace_content = sd_json_variant_ref(x); } return 0; @@ -1130,7 +1132,7 @@ static int event_log_load_userspace(EventLog *el) { return log_error_errno(errno, "Failed to lock userspace TPM measurement log file: %m"); for (;;) { - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; EventLogRecord *record; int ch; @@ -1164,7 +1166,7 @@ static int event_log_load_userspace(EventLog *el) { continue; } - r = json_parse(b, 0, &j, NULL, NULL); + r = sd_json_parse(b, 0, &j, NULL, NULL); if (r < 0) return log_error_errno(r, "Failed to parse local TPM measurement log file: %m"); @@ -1476,7 +1478,7 @@ static int event_log_record_validate_hash_userspace( _cleanup_free_ unsigned char *payload_hash = NULL; unsigned payload_hash_size; - JsonVariant *js; + sd_json_variant *js; const char *s; int mdsz; @@ -1490,12 +1492,12 @@ static int event_log_record_validate_hash_userspace( if (!record->userspace_content) return 0; - js = json_variant_by_key(record->userspace_content, "string"); + js = sd_json_variant_by_key(record->userspace_content, "string"); if (!js) return 0; - assert(json_variant_is_string(js)); - s = json_variant_string(js); + assert(sd_json_variant_is_string(js)); + s = sd_json_variant_string(js); mdsz = EVP_MD_size(md); assert(mdsz > 0); @@ -1634,8 +1636,8 @@ static int event_log_record_equal(const EventLogRecord *a, const EventLogRecord static int event_log_add_component_file(EventLog *el, EventLogComponent *component, const char *path) { _cleanup_(event_log_component_variant_freep) EventLogComponentVariant *variant = NULL; _cleanup_free_ char *fname = NULL, *id = NULL, *path_copy = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; - JsonVariant *records; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; + sd_json_variant *records; const char *e; int r; @@ -1662,7 +1664,7 @@ static int event_log_add_component_file(EventLog *el, EventLogComponent *compone if (!GREEDY_REALLOC(component->variants, component->n_variants+1)) return log_oom(); - r = json_parse_file( + r = sd_json_parse_file( /* f= */ NULL, path, /* flags= */ 0, @@ -1674,7 +1676,7 @@ static int event_log_add_component_file(EventLog *el, EventLogComponent *compone return 0; } - if (!json_variant_is_object(j)) { + if (!sd_json_variant_is_object(j)) { log_warning_errno(r, "Component file %s does not contain JSON object, ignoring.", path); return 0; } @@ -1693,11 +1695,11 @@ static int event_log_add_component_file(EventLog *el, EventLogComponent *compone .id = TAKE_PTR(id), }; - records = json_variant_by_key(j, "records"); + records = sd_json_variant_by_key(j, "records"); if (records) { - JsonVariant *rj; + sd_json_variant *rj; - if (!json_variant_is_array(records)) { + if (!sd_json_variant_is_array(records)) { log_warning_errno(r, "Component records field of file %s is not an array, ignoring.", path); return 0; } @@ -2017,7 +2019,7 @@ static int add_algorithm_columns( if (r < 0) return table_log_add_error(r); - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF) && el->primary_algorithm != UINT16_MAX && *alg != el->primary_algorithm) (void) table_hide_column_from_display(table, c); @@ -2039,7 +2041,7 @@ static int add_algorithm_columns( return 0; } -static int show_log_table(EventLog *el, JsonVariant **ret_variant) { +static int show_log_table(EventLog *el, sd_json_variant **ret_variant) { _cleanup_(table_unrefp) Table *table = NULL; int r; @@ -2078,7 +2080,7 @@ static int show_log_table(EventLog *el, JsonVariant **ret_variant) { (void) table_hide_column_from_display(table, table_get_columns(table) - 3); /* hide source */ - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) (void) table_hide_column_from_display(table, (size_t) 1); /* hide color block column */ (void) table_set_json_field_name(table, phase_column, "phase"); @@ -2177,7 +2179,7 @@ static bool event_log_pcr_checks_out(const EventLog *el, const EventLogRegister return true; } -static int show_pcr_table(EventLog *el, JsonVariant **ret_variant) { +static int show_pcr_table(EventLog *el, sd_json_variant **ret_variant) { _cleanup_(table_unrefp) Table *table = NULL; int r; @@ -2214,7 +2216,7 @@ static int show_pcr_table(EventLog *el, JsonVariant **ret_variant) { if (r < 0) return r; - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) (void) table_hide_column_from_display(table, (size_t) 1, (size_t) 2); /* hide color block and emoji column */ else if (!emoji_enabled()) (void) table_hide_column_from_display(table, (size_t) 2); @@ -2319,7 +2321,7 @@ static int show_pcr_table(EventLog *el, JsonVariant **ret_variant) { if (r < 0) return log_error_errno(r, "Failed to output table: %m"); - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) printf("\n" "%sLegend: H → PCR hash value matches event log%s\n" "%s R → All event log records for this PCR have a matching component%s\n" @@ -2399,9 +2401,9 @@ static int event_log_load_and_process(EventLog **ret) { } static int verb_show_log(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *log_table = NULL, *pcr_table = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *log_table = NULL, *pcr_table = NULL; _cleanup_(event_log_freep) EventLog *el = NULL; - bool want_json = !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF); + bool want_json = !FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF); int r; r = event_log_load_and_process(&el); @@ -2423,15 +2425,15 @@ static int verb_show_log(int argc, char *argv[], void *userdata) { return r; if (want_json) { - _cleanup_(json_variant_unrefp) JsonVariant *object = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *object = NULL; - r = json_build(&object, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("log", log_table), - JSON_BUILD_PAIR_VARIANT("pcrs", pcr_table))); + r = sd_json_build(&object, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("log", log_table), + SD_JSON_BUILD_PAIR_VARIANT("pcrs", pcr_table))); if (r < 0) return log_error_errno(r, "Failed to generate combined object: %m"); - r = json_variant_dump(object, arg_json_format_flags, stdout, /* prefix= */ NULL); + r = sd_json_variant_dump(object, arg_json_format_flags, stdout, /* prefix= */ NULL); if (r < 0) return log_error_errno(r, "Failed to dump JSON object: %m"); } @@ -2439,9 +2441,9 @@ static int verb_show_log(int argc, char *argv[], void *userdata) { return 0; } -static int event_log_record_to_cel(EventLogRecord *record, uint64_t *recnum, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *ja = NULL, *fj = NULL; - JsonVariant *cd = NULL; +static int event_log_record_to_cel(EventLogRecord *record, uint64_t *recnum, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ja = NULL, *fj = NULL; + sd_json_variant *cd = NULL; const char *ct = NULL; int r; @@ -2450,16 +2452,16 @@ static int event_log_record_to_cel(EventLogRecord *record, uint64_t *recnum, Jso assert(ret); LIST_FOREACH(banks, bank, record->banks) { - r = json_variant_append_arrayb( - &ja, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("hashAlg", tpm2_hash_alg_to_string(bank->algorithm)), - JSON_BUILD_PAIR_HEX("digest", bank->hash.buffer, bank->hash.size))); + r = sd_json_variant_append_arrayb( + &ja, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("hashAlg", tpm2_hash_alg_to_string(bank->algorithm)), + SD_JSON_BUILD_PAIR_HEX("digest", bank->hash.buffer, bank->hash.size))); if (r < 0) return log_error_errno(r, "Failed to append CEL digest entry: %m"); } if (!ja) { - r = json_variant_new_array(&ja, NULL, 0); + r = sd_json_variant_new_array(&ja, NULL, 0); if (r < 0) return log_error_errno(r, "Failed to allocate JSON array: %m"); } @@ -2482,9 +2484,9 @@ static int event_log_record_to_cel(EventLogRecord *record, uint64_t *recnum, Jso } else if (asprintf(&et, "%" PRIu32, record->firmware_event_type) < 0) return log_oom(); - r = json_build(&fj, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("event_type", et), - JSON_BUILD_PAIR_HEX("event_data", record->firmware_payload, record->firmware_payload_size))); + r = sd_json_build(&fj, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("event_type", et), + SD_JSON_BUILD_PAIR_HEX("event_data", record->firmware_payload, record->firmware_payload_size))); if (r < 0) return log_error_errno(r, "Failed to build firmware event data: %m"); @@ -2495,13 +2497,13 @@ static int event_log_record_to_cel(EventLogRecord *record, uint64_t *recnum, Jso ct = "systemd"; } - r = json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("pcr", record->pcr), - JSON_BUILD_PAIR_UNSIGNED("recnum", ++(*recnum)), - JSON_BUILD_PAIR_VARIANT("digests", ja), - JSON_BUILD_PAIR_CONDITION(ct, "content_type", JSON_BUILD_STRING(ct)), - JSON_BUILD_PAIR_CONDITION(cd, "content", JSON_BUILD_VARIANT(cd)))); + r = sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("pcr", record->pcr), + SD_JSON_BUILD_PAIR_UNSIGNED("recnum", ++(*recnum)), + SD_JSON_BUILD_PAIR_VARIANT("digests", ja), + SD_JSON_BUILD_PAIR_CONDITION(!!ct, "content_type", SD_JSON_BUILD_STRING(ct)), + SD_JSON_BUILD_PAIR_CONDITION(!!cd, "content", SD_JSON_BUILD_VARIANT(cd)))); if (r < 0) return log_error_errno(r, "Failed to make CEL record: %m"); @@ -2509,7 +2511,7 @@ static int event_log_record_to_cel(EventLogRecord *record, uint64_t *recnum, Jso } static int verb_show_cel(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; _cleanup_(event_log_freep) EventLog *el = NULL; uint64_t recnum = 0; int r; @@ -2525,21 +2527,21 @@ static int verb_show_cel(int argc, char *argv[], void *userdata) { /* Output the event log in TCG CEL-JSON. */ FOREACH_ARRAY(rr, el->records, el->n_records) { - _cleanup_(json_variant_unrefp) JsonVariant *cel = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cel = NULL; r = event_log_record_to_cel(*rr, &recnum, &cel); if (r < 0) return r; - r = json_variant_append_array(&array, cel); + r = sd_json_variant_append_array(&array, cel); if (r < 0) return log_error_errno(r, "Failed to append CEL record: %m"); } - if (arg_json_format_flags & (JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (arg_json_format_flags & (SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) pager_open(arg_pager_flags); - json_variant_dump(array, arg_json_format_flags|JSON_FORMAT_EMPTY_ARRAY, stdout, NULL); + sd_json_variant_dump(array, arg_json_format_flags|SD_JSON_FORMAT_EMPTY_ARRAY, stdout, NULL); return 0; } @@ -2575,7 +2577,7 @@ static int verb_list_components(int argc, char *argv[], void *userdata) { FOREACH_ARRAY(c, el->components, el->n_components) { - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { _cleanup_free_ char *marker = NULL; switch (loc) { @@ -2621,13 +2623,13 @@ static int verb_list_components(int argc, char *argv[], void *userdata) { } } - if (!table_isempty(table) || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (!table_isempty(table) || !FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */ true); if (r < 0) return log_error_errno(r, "Failed to output table: %m"); } - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { if (table_isempty(table)) printf("No components defined.\n"); else @@ -2656,9 +2658,9 @@ static int make_pcrlock_record( uint32_t pcr, const void *data, size_t data_size, - JsonVariant **ret_record) { + sd_json_variant **ret_record) { - _cleanup_(json_variant_unrefp) JsonVariant *digests = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *digests = NULL; int r; assert(data || data_size == 0); @@ -2689,19 +2691,19 @@ static int make_pcrlock_record( if (EVP_Digest(data, data_size, hash, &hash_usize, md, NULL) != 1) return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to hash data with algorithm '%s'.", a); - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &digests, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("hashAlg", JSON_BUILD_STRING(a)), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(hash, hash_usize)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("hashAlg", SD_JSON_BUILD_STRING(a)), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(hash, hash_usize)))); if (r < 0) return log_error_errno(r, "Failed to build JSON digest object: %m"); } - r = json_build(ret_record, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_UNSIGNED(pcr)), - JSON_BUILD_PAIR("digests", JSON_BUILD_VARIANT(digests)))); + r = sd_json_build(ret_record, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_UNSIGNED(pcr)), + SD_JSON_BUILD_PAIR("digests", SD_JSON_BUILD_VARIANT(digests)))); if (r < 0) return log_error_errno(r, "Failed to build record object: %m"); @@ -2718,9 +2720,9 @@ static void evp_md_ctx_free_all(EVP_MD_CTX *(*md)[TPM2_N_HASH_ALGORITHMS]) { static int make_pcrlock_record_from_stream( uint32_t pcr_mask, FILE *f, - JsonVariant **ret_records) { + sd_json_variant **ret_records) { - _cleanup_(json_variant_unrefp) JsonVariant *digests = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *digests = NULL; _cleanup_(evp_md_ctx_free_all) EVP_MD_CTX *mdctx[TPM2_N_HASH_ALGORITHMS] = {}; int r; @@ -2773,29 +2775,29 @@ static int make_pcrlock_record_from_stream( return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to finalize hash context for algorithn '%s'.", a); - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &digests, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("hashAlg", JSON_BUILD_STRING(a)), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(hash, hash_usize)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("hashAlg", SD_JSON_BUILD_STRING(a)), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(hash, hash_usize)))); if (r < 0) return log_error_errno(r, "Failed to build JSON digest object: %m"); } for (uint32_t i = 0; i < TPM2_PCRS_MAX; i++) { - _cleanup_(json_variant_unrefp) JsonVariant *record = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *record = NULL; if (!FLAGS_SET(pcr_mask, UINT32_C(1) << i)) continue; - r = json_build(&record, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_UNSIGNED(i)), - JSON_BUILD_PAIR("digests", JSON_BUILD_VARIANT(digests)))); + r = sd_json_build(&record, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_UNSIGNED(i)), + SD_JSON_BUILD_PAIR("digests", SD_JSON_BUILD_VARIANT(digests)))); if (r < 0) return log_error_errno(r, "Failed to build record object: %m"); - r = json_variant_append_array(ret_records, record); + r = sd_json_variant_append_array(ret_records, record); if (r < 0) return log_error_errno(r, "Failed to append to JSON array: %m"); } @@ -2807,22 +2809,22 @@ static const char *pcrlock_path(const char *default_pcrlock_path) { return arg_pcrlock_path ?: arg_pcrlock_auto ? default_pcrlock_path : NULL; } -static int write_pcrlock(JsonVariant *array, const char *default_pcrlock_path) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *a = NULL; +static int write_pcrlock(sd_json_variant *array, const char *default_pcrlock_path) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *a = NULL; _cleanup_fclose_ FILE *f = NULL; const char *p; int r; if (!array) { - r = json_variant_new_array(&a, NULL, 0); + r = sd_json_variant_new_array(&a, NULL, 0); if (r < 0) return log_error_errno(r, "Failed to allocate empty array: %m"); array = a; } - r = json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("records", JSON_BUILD_VARIANT(array)))); + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("records", SD_JSON_BUILD_VARIANT(array)))); if (r < 0) return log_error_errno(r, "Failed to build JSON object: %m"); @@ -2835,7 +2837,7 @@ static int write_pcrlock(JsonVariant *array, const char *default_pcrlock_path) { return log_error_errno(errno, "Failed to open %s for writing: %m", p); } - r = json_variant_dump(v, arg_json_format_flags, f ?: stdout, /* prefix= */ NULL); + r = sd_json_variant_dump(v, arg_json_format_flags, f ?: stdout, /* prefix= */ NULL); if (r < 0) return log_error_errno(r, "Failed to output JSON object: %m"); @@ -2866,7 +2868,7 @@ static int unlink_pcrlock(const char *default_pcrlock_path) { } static int verb_lock_raw(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *records = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *records = NULL; _cleanup_fclose_ FILE *f = NULL; int r; @@ -2905,14 +2907,14 @@ static int verb_lock_secureboot_policy(int argc, char *argv[], void *userdata) { { EFI_VENDOR_DATABASE, "dbr", -1 }, }; - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; int r; /* Generates expected records from the current SecureBoot state, as readable in the EFI variables * right now. */ FOREACH_ELEMENT(vv, variables) { - _cleanup_(json_variant_unrefp) JsonVariant *record = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *record = NULL; _cleanup_free_ char *name = NULL; if (asprintf(&name, "%s-" SD_ID128_UUID_FORMAT_STR, vv->name, SD_ID128_FORMAT_VAL(vv->id)) < 0) @@ -2955,7 +2957,7 @@ static int verb_lock_secureboot_policy(int argc, char *argv[], void *userdata) { if (r < 0) return r; - r = json_variant_append_array(&array, record); + r = sd_json_variant_append_array(&array, record); if (r < 0) return log_error_errno(r, "Failed to append to JSON array: %m"); } @@ -3085,7 +3087,7 @@ static int event_log_ensure_secureboot_consistency(EventLog *el) { } static int verb_lock_secureboot_authority(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; _cleanup_(event_log_freep) EventLog *el = NULL; int r; @@ -3135,7 +3137,7 @@ static int verb_lock_secureboot_authority(int argc, char *argv[], void *userdata return r; FOREACH_ARRAY(rr, el->records, el->n_records) { - _cleanup_(json_variant_unrefp) JsonVariant *digests = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *digests = NULL; EventLogRecord *rec = *rr; if (!event_log_record_is_secureboot_authority(rec)) @@ -3144,20 +3146,20 @@ static int verb_lock_secureboot_authority(int argc, char *argv[], void *userdata log_debug("Locking down authority '%s'.", strna(rec->description)); LIST_FOREACH(banks, bank, rec->banks) { - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &digests, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("hashAlg", JSON_BUILD_STRING(tpm2_hash_alg_to_string(bank->algorithm))), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(bank->hash.buffer, bank->hash.size)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("hashAlg", SD_JSON_BUILD_STRING(tpm2_hash_alg_to_string(bank->algorithm))), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(bank->hash.buffer, bank->hash.size)))); if (r < 0) return log_error_errno(r, "Failed to build digests array: %m"); } - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_UNSIGNED(rec->pcr)), - JSON_BUILD_PAIR("digests", JSON_BUILD_VARIANT(digests)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_UNSIGNED(rec->pcr)), + SD_JSON_BUILD_PAIR("digests", SD_JSON_BUILD_VARIANT(digests)))); if (r < 0) return log_error_errno(r, "Failed to build record array: %m"); } @@ -3170,7 +3172,7 @@ static int verb_unlock_secureboot_authority(int argc, char *argv[], void *userda } static int verb_lock_gpt(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL, *record = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL, *record = NULL; _cleanup_(sd_device_unrefp) sd_device *d = NULL; uint8_t h[2 * 4096]; /* space for at least two 4K sectors. GPT header should definitely be in here */ uint64_t start, n_members, member_size; @@ -3275,7 +3277,7 @@ static int verb_lock_gpt(int argc, char *argv[], void *userdata) { if (r < 0) return r; - r = json_variant_new_array(&array, &record, 1); + r = sd_json_variant_new_array(&array, &record, 1); if (r < 0) return log_error_errno(r, "Failed to append to JSON array: %m"); @@ -3334,15 +3336,15 @@ static void enable_json_sse(void) { if (!arg_pcrlock_path && arg_pcrlock_auto) return; - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_SSE)) + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_SSE)) return; log_notice("Enabling JSON_SEQ mode, since writing two .pcrlock files to single output."); - arg_json_format_flags |= JSON_FORMAT_SSE; + arg_json_format_flags |= SD_JSON_FORMAT_SSE; } static int verb_lock_firmware(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *array_early = NULL, *array_late = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array_early = NULL, *array_late = NULL; _cleanup_(event_log_freep) EventLog *el = NULL; uint32_t always_mask, separator_mask, separator_seen_mask = 0, action_seen_mask = 0; const char *default_pcrlock_early_path, *default_pcrlock_late_path; @@ -3408,7 +3410,7 @@ static int verb_lock_firmware(int argc, char *argv[], void *userdata) { // and exactly once FOREACH_ARRAY(rr, el->records, el->n_records) { - _cleanup_(json_variant_unrefp) JsonVariant *digests = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *digests = NULL; EventLogRecord *rec = *rr; uint32_t bit = UINT32_C(1) << rec->pcr; @@ -3437,20 +3439,20 @@ static int verb_lock_firmware(int argc, char *argv[], void *userdata) { } LIST_FOREACH(banks, bank, rec->banks) { - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &digests, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("hashAlg", JSON_BUILD_STRING(tpm2_hash_alg_to_string(bank->algorithm))), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(bank->hash.buffer, bank->hash.size)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("hashAlg", SD_JSON_BUILD_STRING(tpm2_hash_alg_to_string(bank->algorithm))), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(bank->hash.buffer, bank->hash.size)))); if (r < 0) return log_error_errno(r, "Failed to build digests array: %m"); } - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( FLAGS_SET(separator_seen_mask, bit) ? &array_late : &array_early, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_UNSIGNED(rec->pcr)), - JSON_BUILD_PAIR("digests", JSON_BUILD_VARIANT(digests)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_UNSIGNED(rec->pcr)), + SD_JSON_BUILD_PAIR("digests", SD_JSON_BUILD_VARIANT(digests)))); if (r < 0) return log_error_errno(r, "Failed to build record array: %m"); } @@ -3489,7 +3491,7 @@ static int verb_unlock_firmware(int argc, char *argv[], void *userdata) { } static int verb_lock_machine_id(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *record = NULL, *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *record = NULL, *array = NULL; _cleanup_free_ char *word = NULL; int r; @@ -3501,7 +3503,7 @@ static int verb_lock_machine_id(int argc, char *argv[], void *userdata) { if (r < 0) return r; - r = json_variant_new_array(&array, &record, 1); + r = sd_json_variant_new_array(&array, &record, 1); if (r < 0) return log_error_errno(r, "Failed to create record array: %m"); @@ -3566,7 +3568,7 @@ static int verb_lock_file_system(int argc, char *argv[], void *userdata) { STRV_FOREACH(p, paths) { _cleanup_free_ char *word = NULL, *normalized_path = NULL, *pcrlock_file = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *record = NULL, *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *record = NULL, *array = NULL; r = pcrextend_file_system_word(*p, &word, &normalized_path); if (r < 0) @@ -3580,7 +3582,7 @@ static int verb_lock_file_system(int argc, char *argv[], void *userdata) { if (r < 0) return r; - r = json_variant_new_array(&array, &record, 1); + r = sd_json_variant_new_array(&array, &record, 1); if (r < 0) return log_error_errno(r, "Failed to create record array: %m"); @@ -3623,7 +3625,7 @@ static int verb_unlock_file_system(int argc, char *argv[], void *userdata) { } static int verb_lock_pe(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; _cleanup_close_ int fd = -EBADF; int r; @@ -3640,7 +3642,7 @@ static int verb_lock_pe(int argc, char *argv[], void *userdata) { arg_pcr_mask = UINT32_C(1) << TPM2_PCR_BOOT_LOADER_CODE; for (uint32_t i = 0; i < TPM2_PCRS_MAX; i++) { - _cleanup_(json_variant_unrefp) JsonVariant *digests = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *digests = NULL; if (!FLAGS_SET(arg_pcr_mask, UINT32_C(1) << i)) continue; @@ -3658,19 +3660,19 @@ static int verb_lock_pe(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to hash PE binary: %m"); - r = json_variant_append_arrayb(&digests, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("hashAlg", JSON_BUILD_STRING(a)), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(hash, hash_size)))); + r = sd_json_variant_append_arrayb(&digests, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("hashAlg", SD_JSON_BUILD_STRING(a)), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(hash, hash_size)))); if (r < 0) return log_error_errno(r, "Failed to build JSON digest object: %m"); } - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_UNSIGNED(i)), - JSON_BUILD_PAIR("digests", JSON_BUILD_VARIANT(digests)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_UNSIGNED(i)), + SD_JSON_BUILD_PAIR("digests", SD_JSON_BUILD_VARIANT(digests)))); if (r < 0) return log_error_errno(r, "Failed to append record object: %m"); } @@ -3688,7 +3690,7 @@ static void section_hashes_array_done(SectionHashArray *array) { } static int verb_lock_uki(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL, *pe_digests = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL, *pe_digests = NULL; _cleanup_(section_hashes_array_done) SectionHashArray section_hashes = {}; size_t hash_sizes[TPM2_N_HASH_ALGORITHMS]; _cleanup_close_ int fd = -EBADF; @@ -3715,11 +3717,11 @@ static int verb_lock_uki(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to hash PE binary: %m"); - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &pe_digests, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("hashAlg", JSON_BUILD_STRING(a)), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(peh, hash_sizes[i])))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("hashAlg", SD_JSON_BUILD_STRING(a)), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(peh, hash_sizes[i])))); if (r < 0) return log_error_errno(r, "Failed to build JSON digest object: %m"); @@ -3728,16 +3730,16 @@ static int verb_lock_uki(int argc, char *argv[], void *userdata) { return log_error_errno(r, "Failed to UKI hash PE binary: %m"); } - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_UNSIGNED(TPM2_PCR_BOOT_LOADER_CODE)), - JSON_BUILD_PAIR("digests", JSON_BUILD_VARIANT(pe_digests)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_UNSIGNED(TPM2_PCR_BOOT_LOADER_CODE)), + SD_JSON_BUILD_PAIR("digests", SD_JSON_BUILD_VARIANT(pe_digests)))); if (r < 0) return log_error_errno(r, "Failed to append record object: %m"); for (UnifiedSection section = 0; section < _UNIFIED_SECTION_MAX; section++) { - _cleanup_(json_variant_unrefp) JsonVariant *section_digests = NULL, *record = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *section_digests = NULL, *record = NULL; if (!unified_section_measure(section)) continue; @@ -3752,11 +3754,11 @@ static int verb_lock_uki(int argc, char *argv[], void *userdata) { assert_se(a = tpm2_hash_alg_to_string(tpm2_hash_algorithms[i])); - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( §ion_digests, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("hashAlg", JSON_BUILD_STRING(a)), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(hash, hash_sizes[i])))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("hashAlg", SD_JSON_BUILD_STRING(a)), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(hash, hash_sizes[i])))); if (r < 0) return log_error_errno(r, "Failed to build JSON digest object: %m"); } @@ -3769,16 +3771,16 @@ static int verb_lock_uki(int argc, char *argv[], void *userdata) { if (r < 0) return r; - r = json_variant_append_array(&array, record); + r = sd_json_variant_append_array(&array, record); if (r < 0) return log_error_errno(r, "Failed to append JSON record array: %m"); /* And then append a record for the section contents digests as well */ - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_UNSIGNED(TPM2_PCR_KERNEL_BOOT /* =11 */)), - JSON_BUILD_PAIR("digests", JSON_BUILD_VARIANT(section_digests)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_UNSIGNED(TPM2_PCR_KERNEL_BOOT /* =11 */)), + SD_JSON_BUILD_PAIR("digests", SD_JSON_BUILD_VARIANT(section_digests)))); if (r < 0) return log_error_errno(r, "Failed to append record object: %m"); } @@ -3850,7 +3852,7 @@ static int event_log_reduce_to_safe_pcrs(EventLog *el, uint32_t *pcrs) { } static int verb_lock_kernel_cmdline(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *record = NULL, *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *record = NULL, *array = NULL; _cleanup_free_ char *cmdline = NULL; int r; @@ -3875,7 +3877,7 @@ static int verb_lock_kernel_cmdline(int argc, char *argv[], void *userdata) { if (r < 0) return r; - r = json_variant_new_array(&array, &record, 1); + r = sd_json_variant_new_array(&array, &record, 1); if (r < 0) return log_error_errno(r, "Failed to create record array: %m"); @@ -3891,7 +3893,7 @@ static int verb_unlock_kernel_cmdline(int argc, char *argv[], void *userdata) { } static int verb_lock_kernel_initrd(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *records = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *records = NULL; _cleanup_fclose_ FILE *f = NULL; uint32_t pcr_mask = UINT32_C(1) << TPM2_PCR_KERNEL_INITRD; int r; @@ -4132,11 +4134,11 @@ static int event_log_show_predictions(Tpm2PCRPrediction *context, uint16_t alg) pager_open(arg_pager_flags); - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; for (size_t i = 0; i < TPM2_N_HASH_ALGORITHMS; i++) { - _cleanup_(json_variant_unrefp) JsonVariant *aj = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *aj = NULL; r = tpm2_pcr_prediction_to_json( context, @@ -4145,10 +4147,10 @@ static int event_log_show_predictions(Tpm2PCRPrediction *context, uint16_t alg) if (r < 0) return r; - if (json_variant_elements(aj) == 0) + if (sd_json_variant_elements(aj) == 0) continue; - r = json_variant_set_field( + r = sd_json_variant_set_field( &j, tpm2_hash_alg_to_string(tpm2_hash_algorithms[i]), aj); @@ -4157,12 +4159,12 @@ static int event_log_show_predictions(Tpm2PCRPrediction *context, uint16_t alg) } if (!j) { - r = json_variant_new_object(&j, NULL, 0); + r = sd_json_variant_new_object(&j, NULL, 0); if (r < 0) return log_error_errno(r, "Failed to allocated empty object: %m"); } - json_variant_dump(j, arg_json_format_flags, /* f= */ NULL, /* prefix= */ NULL); + sd_json_variant_dump(j, arg_json_format_flags, /* f= */ NULL, /* prefix= */ NULL); return 0; } @@ -4453,13 +4455,13 @@ static int make_policy(bool force, RecoveryPinMode recovery_pin_mode) { log_info("Predicted future PCRs in %s.", FORMAT_TIMESPAN(usec_sub_unsigned(now(CLOCK_MONOTONIC), predict_start_usec), 1)); - _cleanup_(json_variant_unrefp) JsonVariant *new_prediction_json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *new_prediction_json = NULL; r = tpm2_pcr_prediction_to_json(&new_prediction, el->primary_algorithm, &new_prediction_json); if (r < 0) return r; if (DEBUG_LOGGING) - (void) json_variant_dump(new_prediction_json, JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO, stderr, NULL); + (void) sd_json_variant_dump(new_prediction_json, SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO, stderr, NULL); _cleanup_(tpm2_pcrlock_policy_done) Tpm2PCRLockPolicy old_policy = {}; @@ -4783,12 +4785,12 @@ static int make_policy(bool force, RecoveryPinMode recovery_pin_mode) { return log_error_errno(r, "Failed to marshal NV public area: %m"); } - _cleanup_(json_variant_unrefp) JsonVariant *new_configuration_json = NULL; - r = json_build(&new_configuration_json, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("pcrBank", tpm2_hash_alg_to_string(el->primary_algorithm)), - JSON_BUILD_PAIR_VARIANT("pcrValues", new_prediction_json), - JSON_BUILD_PAIR_INTEGER("nvIndex", nv_index), + _cleanup_(sd_json_variant_unrefp) sd_json_variant *new_configuration_json = NULL; + r = sd_json_build(&new_configuration_json, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("pcrBank", tpm2_hash_alg_to_string(el->primary_algorithm)), + SD_JSON_BUILD_PAIR_VARIANT("pcrValues", new_prediction_json), + SD_JSON_BUILD_PAIR_INTEGER("nvIndex", nv_index), JSON_BUILD_PAIR_IOVEC_BASE64("nvHandle", &nv_blob), JSON_BUILD_PAIR_IOVEC_BASE64("nvPublic", &nv_public_blob), JSON_BUILD_PAIR_IOVEC_BASE64("srkHandle", &srk_blob), @@ -4798,7 +4800,7 @@ static int make_policy(bool force, RecoveryPinMode recovery_pin_mode) { return log_error_errno(r, "Failed to generate JSON: %m"); _cleanup_free_ char *text = NULL; - r = json_variant_format(new_configuration_json, 0, &text); + r = sd_json_variant_format(new_configuration_json, 0, &text); if (r < 0) return log_error_errno(r, "Failed to format new configuration to JSON: %m"); @@ -5247,14 +5249,14 @@ static int pcrlock_main(int argc, char *argv[]) { return dispatch_verb(argc, argv, verbs, NULL); } -static int vl_method_read_event_log(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_read_event_log(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { _cleanup_(event_log_freep) EventLog *el = NULL; uint64_t recnum = 0; int r; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); el = event_log_new(); @@ -5265,17 +5267,17 @@ static int vl_method_read_event_log(Varlink *link, JsonVariant *parameters, Varl if (r < 0) return r; - _cleanup_(json_variant_unrefp) JsonVariant *rec_cel = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *rec_cel = NULL; FOREACH_ARRAY(rr, el->records, el->n_records) { if (rec_cel) { r = varlink_notifyb(link, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR_VARIANT("record", rec_cel))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_VARIANT("record", rec_cel))); if (r < 0) return r; - rec_cel = json_variant_unref(rec_cel); + rec_cel = sd_json_variant_unref(rec_cel); } r = event_log_record_to_cel(*rr, &recnum, &rec_cel); @@ -5284,16 +5286,16 @@ static int vl_method_read_event_log(Varlink *link, JsonVariant *parameters, Varl } return varlink_replyb(link, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR_CONDITION(rec_cel, "record", JSON_BUILD_VARIANT(rec_cel)))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_CONDITION(!!rec_cel, "record", SD_JSON_BUILD_VARIANT(rec_cel)))); } typedef struct MethodMakePolicyParameters { bool force; } MethodMakePolicyParameters; -static int vl_method_make_policy(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "force", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(MethodMakePolicyParameters, force), 0 }, +static int vl_method_make_policy(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "force", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(MethodMakePolicyParameters, force), 0 }, {} }; MethodMakePolicyParameters p = {}; @@ -5314,12 +5316,12 @@ static int vl_method_make_policy(Varlink *link, JsonVariant *parameters, Varlink return varlink_reply(link, NULL); } -static int vl_method_remove_policy(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_remove_policy(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { int r; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); r = remove_policy(); diff --git a/src/resolve/fuzz-resource-record.c b/src/resolve/fuzz-resource-record.c index f792167aff..b1ff0d87e9 100644 --- a/src/resolve/fuzz-resource-record.c +++ b/src/resolve/fuzz-resource-record.c @@ -8,7 +8,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *copy = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(memstream_done) MemStream m = {}; FILE *f; @@ -27,7 +27,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { (void) fprintf(f, "%s", strna(dns_resource_record_to_string(rr))); assert_se(dns_resource_record_to_json(rr, &v) >= 0); - assert_se(json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, f, NULL) >= 0); + assert_se(sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_SOURCE, f, NULL) >= 0); assert_se(dns_resource_record_to_wire_format(rr, false) >= 0); assert_se(dns_resource_record_to_wire_format(rr, true) >= 0); diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index f2e9e7a96b..63fb7d6d39 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -5,6 +5,7 @@ #include <net/if.h> #include "sd-bus.h" +#include "sd-json.h" #include "sd-netlink.h" #include "af-list.h" @@ -22,7 +23,7 @@ #include "format-util.h" #include "gcrypt-util.h" #include "hostname-util.h" -#include "json.h" +#include "json-util.h" #include "main-func.h" #include "missing_network.h" #include "netlink-util.h" @@ -56,7 +57,7 @@ static uint16_t arg_type = 0; static uint16_t arg_class = 0; static bool arg_legend = true; static uint64_t arg_flags = 0; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; bool arg_ifindex_permissive = false; /* If true, don't generate an error if the specified interface index doesn't exist */ static const char *arg_service_family = NULL; @@ -184,7 +185,7 @@ static void print_source(uint64_t flags, usec_t rtt) { if (!arg_legend) return; - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return; if (flags == 0) @@ -253,7 +254,7 @@ static int resolve_host(sd_bus *bus, const char *name) { assert(name); - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type=A or --type=AAAA to acquire address record information in JSON format."); log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); @@ -354,7 +355,7 @@ static int resolve_address(sd_bus *bus, int family, const union in_addr_union *a assert(IN_SET(family, AF_INET, AF_INET6)); assert(address); - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); if (ifindex <= 0) @@ -448,8 +449,8 @@ static int output_rr_packet(const void *d, size_t l, int ifindex) { if (r < 0) return log_error_errno(r, "Failed to parse RR: %m"); - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; r = dns_resource_record_to_json(rr, &j); if (r < 0) return log_error_errno(r, "Failed to convert RR to JSON: %m"); @@ -457,7 +458,7 @@ static int output_rr_packet(const void *d, size_t l, int ifindex) { if (!j) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "JSON formatting for records of type %s (%u) not available.", dns_type_to_string(rr->key->type), rr->key->type); - r = json_variant_dump(j, arg_json_format_flags, NULL, NULL); + r = sd_json_variant_dump(j, arg_json_format_flags, NULL, NULL); if (r < 0) return r; @@ -970,7 +971,7 @@ static int resolve_service(sd_bus *bus, const char *name, const char *type, cons static int verb_service(int argc, char **argv, void *userdata) { sd_bus *bus = userdata; - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); if (argc == 2) @@ -1032,7 +1033,7 @@ static int verb_openpgp(int argc, char **argv, void *userdata) { sd_bus *bus = userdata; int q, r = 0; - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); STRV_FOREACH(p, argv + 1) { @@ -1086,7 +1087,7 @@ static int verb_tlsa(int argc, char **argv, void *userdata) { const char *family = "tcp"; int q, r = 0; - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); if (service_family_is_valid(argv[1])) { @@ -1105,7 +1106,7 @@ static int verb_tlsa(int argc, char **argv, void *userdata) { static int show_statistics(int argc, char **argv, void *userdata) { _cleanup_(table_unrefp) Table *table = NULL; - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; _cleanup_(varlink_unrefp) Varlink *vl = NULL; int r; @@ -1117,23 +1118,23 @@ static int show_statistics(int argc, char **argv, void *userdata) { if (r < 0) return r; - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) - return json_variant_dump(reply, arg_json_format_flags, NULL, NULL); + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) + return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL); struct statistics { - JsonVariant *transactions; - JsonVariant *cache; - JsonVariant *dnssec; + sd_json_variant *transactions; + sd_json_variant *cache; + sd_json_variant *dnssec; } statistics; - static const JsonDispatch statistics_dispatch_table[] = { - { "transactions", JSON_VARIANT_OBJECT, json_dispatch_variant_noref, offsetof(struct statistics, transactions), JSON_MANDATORY }, - { "cache", JSON_VARIANT_OBJECT, json_dispatch_variant_noref, offsetof(struct statistics, cache), JSON_MANDATORY }, - { "dnssec", JSON_VARIANT_OBJECT, json_dispatch_variant_noref, offsetof(struct statistics, dnssec), JSON_MANDATORY }, + static const sd_json_dispatch_field statistics_dispatch_table[] = { + { "transactions", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, transactions), SD_JSON_MANDATORY }, + { "cache", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, cache), SD_JSON_MANDATORY }, + { "dnssec", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, dnssec), SD_JSON_MANDATORY }, {}, }; - r = json_dispatch(reply, statistics_dispatch_table, JSON_LOG, &statistics); + r = sd_json_dispatch(reply, statistics_dispatch_table, SD_JSON_LOG, &statistics); if (r < 0) return r; @@ -1146,17 +1147,17 @@ static int show_statistics(int argc, char **argv, void *userdata) { uint64_t n_failure_responses_served_stale_total; } transactions; - static const JsonDispatch transactions_dispatch_table[] = { - { "currentTransactions", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct transactions, n_current_transactions), JSON_MANDATORY }, - { "totalTransactions", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct transactions, n_transactions_total), JSON_MANDATORY }, - { "totalTimeouts", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct transactions, n_timeouts_total), JSON_MANDATORY }, - { "totalTimeoutsServedStale", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct transactions, n_timeouts_served_stale_total), JSON_MANDATORY }, - { "totalFailedResponses", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_total), JSON_MANDATORY }, - { "totalFailedResponsesServedStale", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_served_stale_total), JSON_MANDATORY }, + static const sd_json_dispatch_field transactions_dispatch_table[] = { + { "currentTransactions", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_current_transactions), SD_JSON_MANDATORY }, + { "totalTransactions", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_transactions_total), SD_JSON_MANDATORY }, + { "totalTimeouts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_timeouts_total), SD_JSON_MANDATORY }, + { "totalTimeoutsServedStale", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_timeouts_served_stale_total), SD_JSON_MANDATORY }, + { "totalFailedResponses", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_total), SD_JSON_MANDATORY }, + { "totalFailedResponsesServedStale", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_served_stale_total), SD_JSON_MANDATORY }, {}, }; - r = json_dispatch(statistics.transactions, transactions_dispatch_table, JSON_LOG, &transactions); + r = sd_json_dispatch(statistics.transactions, transactions_dispatch_table, SD_JSON_LOG, &transactions); if (r < 0) return r; @@ -1166,14 +1167,14 @@ static int show_statistics(int argc, char **argv, void *userdata) { uint64_t n_cache_miss; } cache; - static const JsonDispatch cache_dispatch_table[] = { - { "size", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct cache, cache_size), JSON_MANDATORY }, - { "hits", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct cache, n_cache_hit), JSON_MANDATORY }, - { "misses", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct cache, n_cache_miss), JSON_MANDATORY }, + static const sd_json_dispatch_field cache_dispatch_table[] = { + { "size", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, cache_size), SD_JSON_MANDATORY }, + { "hits", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, n_cache_hit), SD_JSON_MANDATORY }, + { "misses", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, n_cache_miss), SD_JSON_MANDATORY }, {}, }; - r = json_dispatch(statistics.cache, cache_dispatch_table, JSON_LOG, &cache); + r = sd_json_dispatch(statistics.cache, cache_dispatch_table, SD_JSON_LOG, &cache); if (r < 0) return r; @@ -1184,15 +1185,15 @@ static int show_statistics(int argc, char **argv, void *userdata) { uint64_t n_dnssec_indeterminate; } dnsssec; - static const JsonDispatch dnssec_dispatch_table[] = { - { "secure", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_secure), JSON_MANDATORY }, - { "insecure", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_insecure), JSON_MANDATORY }, - { "bogus", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_bogus), JSON_MANDATORY }, - { "indeterminate", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_indeterminate), JSON_MANDATORY }, + static const sd_json_dispatch_field dnssec_dispatch_table[] = { + { "secure", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_secure), SD_JSON_MANDATORY }, + { "insecure", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_insecure), SD_JSON_MANDATORY }, + { "bogus", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_bogus), SD_JSON_MANDATORY }, + { "indeterminate", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_indeterminate), SD_JSON_MANDATORY }, {}, }; - r = json_dispatch(statistics.dnssec, dnssec_dispatch_table, JSON_LOG, &dnsssec); + r = sd_json_dispatch(statistics.dnssec, dnssec_dispatch_table, SD_JSON_LOG, &dnsssec); if (r < 0) return r; @@ -1263,7 +1264,7 @@ static int show_statistics(int argc, char **argv, void *userdata) { } static int reset_statistics(int argc, char **argv, void *userdata) { - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; _cleanup_(varlink_unrefp) Varlink *vl = NULL; int r; @@ -1275,8 +1276,8 @@ static int reset_statistics(int argc, char **argv, void *userdata) { if (r < 0) return r; - if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) - return json_variant_dump(reply, arg_json_format_flags, NULL, NULL); + if (!FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) + return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL); return 0; } @@ -2676,8 +2677,8 @@ static int verb_log_level(int argc, char *argv[], void *userdata) { return verb_log_control_common(bus, "org.freedesktop.resolve1", argv[0], argc == 2 ? argv[1] : NULL); } -static int print_question(char prefix, const char *color, JsonVariant *question) { - JsonVariant *q = NULL; +static int print_question(char prefix, const char *color, sd_json_variant *question) { + sd_json_variant *q = NULL; int r; assert(color); @@ -2703,24 +2704,24 @@ static int print_question(char prefix, const char *color, JsonVariant *question) return 0; } -static int print_answer(JsonVariant *answer) { - JsonVariant *a; +static int print_answer(sd_json_variant *answer) { + sd_json_variant *a; int r; JSON_VARIANT_ARRAY_FOREACH(a, answer) { _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; _cleanup_free_ void *d = NULL; - JsonVariant *jraw; + sd_json_variant *jraw; const char *s; size_t l; - jraw = json_variant_by_key(a, "raw"); + jraw = sd_json_variant_by_key(a, "raw"); if (!jraw) { log_warning("Received monitor answer lacking valid raw data, ignoring."); continue; } - r = json_variant_unbase64(jraw, &d, &l); + r = sd_json_variant_unbase64(jraw, &d, &l); if (r < 0) { log_warning_errno(r, "Failed to undo base64 encoding of monitor answer raw data, ignoring."); continue; @@ -2746,27 +2747,27 @@ static int print_answer(JsonVariant *answer) { return 0; } -static void monitor_query_dump(JsonVariant *v) { - _cleanup_(json_variant_unrefp) JsonVariant *question = NULL, *answer = NULL, *collected_questions = NULL; +static void monitor_query_dump(sd_json_variant *v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *question = NULL, *answer = NULL, *collected_questions = NULL; int rcode = -1, error = 0, ede_code = -1; const char *state = NULL, *result = NULL, *ede_msg = NULL; assert(v); - JsonDispatch dispatch_table[] = { - { "question", JSON_VARIANT_ARRAY, json_dispatch_variant, PTR_TO_SIZE(&question), JSON_MANDATORY }, - { "answer", JSON_VARIANT_ARRAY, json_dispatch_variant, PTR_TO_SIZE(&answer), 0 }, - { "collectedQuestions", JSON_VARIANT_ARRAY, json_dispatch_variant, PTR_TO_SIZE(&collected_questions), 0 }, - { "state", JSON_VARIANT_STRING, json_dispatch_const_string, PTR_TO_SIZE(&state), JSON_MANDATORY }, - { "result", JSON_VARIANT_STRING, json_dispatch_const_string, PTR_TO_SIZE(&result), 0 }, - { "rcode", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, PTR_TO_SIZE(&rcode), 0 }, - { "errno", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, PTR_TO_SIZE(&error), 0 }, - { "extendedDNSErrorCode", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, PTR_TO_SIZE(&ede_code), 0 }, - { "extendedDNSErrorMessage", JSON_VARIANT_STRING, json_dispatch_const_string, PTR_TO_SIZE(&ede_msg), 0 }, + sd_json_dispatch_field dispatch_table[] = { + { "question", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, PTR_TO_SIZE(&question), SD_JSON_MANDATORY }, + { "answer", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, PTR_TO_SIZE(&answer), 0 }, + { "collectedQuestions", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, PTR_TO_SIZE(&collected_questions), 0 }, + { "state", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, PTR_TO_SIZE(&state), SD_JSON_MANDATORY }, + { "result", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, PTR_TO_SIZE(&result), 0 }, + { "rcode", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, PTR_TO_SIZE(&rcode), 0 }, + { "errno", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, PTR_TO_SIZE(&error), 0 }, + { "extendedDNSErrorCode", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, PTR_TO_SIZE(&ede_code), 0 }, + { "extendedDNSErrorMessage", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, PTR_TO_SIZE(&ede_msg), 0 }, {} }; - if (json_dispatch(v, dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, NULL) < 0) + if (sd_json_dispatch(v, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, NULL) < 0) return; /* First show the current question */ @@ -2799,7 +2800,7 @@ static void monitor_query_dump(JsonVariant *v) { static int monitor_reply( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { @@ -2819,7 +2820,7 @@ static int monitor_reply( return 0; } - if (json_variant_by_key(parameters, "ready")) { + if (sd_json_variant_by_key(parameters, "ready")) { /* The first message coming in will just indicate that we are now subscribed. We let our * caller know if they asked for it. Once the caller sees this they should know that we are * not going to miss any queries anymore. */ @@ -2827,11 +2828,11 @@ static int monitor_reply( return 0; } - if (arg_json_format_flags & JSON_FORMAT_OFF) { + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) { monitor_query_dump(parameters); printf("\n"); } else - json_variant_dump(parameters, arg_json_format_flags, NULL, NULL); + sd_json_variant_dump(parameters, arg_json_format_flags, NULL, NULL); fflush(stdout); @@ -2882,27 +2883,27 @@ static int verb_monitor(int argc, char *argv[], void *userdata) { return c; } -static int dump_cache_item(JsonVariant *item) { +static int dump_cache_item(sd_json_variant *item) { struct item_info { - JsonVariant *key; - JsonVariant *rrs; + sd_json_variant *key; + sd_json_variant *rrs; const char *type; uint64_t until; } item_info = {}; - static const JsonDispatch dispatch_table[] = { - { "key", JSON_VARIANT_OBJECT, json_dispatch_variant_noref, offsetof(struct item_info, key), JSON_MANDATORY }, - { "rrs", JSON_VARIANT_ARRAY, json_dispatch_variant_noref, offsetof(struct item_info, rrs), 0 }, - { "type", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct item_info, type), 0 }, - { "until", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct item_info, until), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "key", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct item_info, key), SD_JSON_MANDATORY }, + { "rrs", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant_noref, offsetof(struct item_info, rrs), 0 }, + { "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct item_info, type), 0 }, + { "until", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct item_info, until), 0 }, {}, }; _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL; int r, c = 0; - r = json_dispatch(item, dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &item_info); + r = sd_json_dispatch(item, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &item_info); if (r < 0) return r; @@ -2913,19 +2914,19 @@ static int dump_cache_item(JsonVariant *item) { if (item_info.type) printf("%s %s%s%s\n", DNS_RESOURCE_KEY_TO_STRING(k), ansi_highlight_red(), item_info.type, ansi_normal()); else { - JsonVariant *i; + sd_json_variant *i; JSON_VARIANT_ARRAY_FOREACH(i, item_info.rrs) { _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; _cleanup_free_ void *data = NULL; - JsonVariant *raw; + sd_json_variant *raw; size_t size; - raw = json_variant_by_key(i, "raw"); + raw = sd_json_variant_by_key(i, "raw"); if (!raw) return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "raw field missing from RR JSON data."); - r = json_variant_unbase64(raw, &data, &size); + r = sd_json_variant_unbase64(raw, &data, &size); if (r < 0) return log_error_errno(r, "Unable to decode raw RR JSON data: %m"); @@ -2941,30 +2942,30 @@ static int dump_cache_item(JsonVariant *item) { return c; } -static int dump_cache_scope(JsonVariant *scope) { +static int dump_cache_scope(sd_json_variant *scope) { struct scope_info { const char *protocol; int family; int ifindex; const char *ifname; - JsonVariant *cache; + sd_json_variant *cache; } scope_info = { .family = AF_UNSPEC, }; - JsonVariant *i; + sd_json_variant *i; int r, c = 0; - static const JsonDispatch dispatch_table[] = { - { "protocol", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct scope_info, protocol), JSON_MANDATORY }, - { "family", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(struct scope_info, family), 0 }, - { "ifindex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(struct scope_info, ifindex), 0 }, - { "ifname", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct scope_info, ifname), 0 }, - { "cache", JSON_VARIANT_ARRAY, json_dispatch_variant_noref, offsetof(struct scope_info, cache), JSON_MANDATORY }, + static const sd_json_dispatch_field dispatch_table[] = { + { "protocol", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct scope_info, protocol), SD_JSON_MANDATORY }, + { "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(struct scope_info, family), 0 }, + { "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(struct scope_info, ifindex), 0 }, + { "ifname", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct scope_info, ifname), 0 }, + { "cache", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant_noref, offsetof(struct scope_info, cache), SD_JSON_MANDATORY }, {}, }; - r = json_dispatch(scope, dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &scope_info); + r = sd_json_dispatch(scope, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &scope_info); if (r < 0) return r; @@ -2997,7 +2998,7 @@ static int dump_cache_scope(JsonVariant *scope) { } static int verb_show_cache(int argc, char *argv[], void *userdata) { - JsonVariant *reply = NULL, *d = NULL; + sd_json_variant *reply = NULL, *d = NULL; _cleanup_(varlink_unrefp) Varlink *vl = NULL; int r; @@ -3009,17 +3010,17 @@ static int verb_show_cache(int argc, char *argv[], void *userdata) { if (r < 0) return r; - d = json_variant_by_key(reply, "dump"); + d = sd_json_variant_by_key(reply, "dump"); if (!d) return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "DumpCache() response is missing 'dump' key."); - if (!json_variant_is_array(d)) + if (!sd_json_variant_is_array(d)) return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "DumpCache() response 'dump' field not an array"); - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - JsonVariant *i; + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + sd_json_variant *i; JSON_VARIANT_ARRAY_FOREACH(i, d) { r = dump_cache_scope(i); @@ -3030,10 +3031,10 @@ static int verb_show_cache(int argc, char *argv[], void *userdata) { return 0; } - return json_variant_dump(d, arg_json_format_flags, NULL, NULL); + return sd_json_variant_dump(d, arg_json_format_flags, NULL, NULL); } -static int dump_server_state(JsonVariant *server) { +static int dump_server_state(sd_json_variant *server) { _cleanup_(table_unrefp) Table *table = NULL; TableCell *cell; @@ -3060,27 +3061,27 @@ static int dump_server_state(JsonVariant *server) { int r; - static const JsonDispatch dispatch_table[] = { - { "Server", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, server_name), JSON_MANDATORY }, - { "Type", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, type), JSON_MANDATORY }, - { "Interface", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, ifname), 0 }, - { "InterfaceIndex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(struct server_state, ifindex), 0 }, - { "VerifiedFeatureLevel", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, verified_feature_level), 0 }, - { "PossibleFeatureLevel", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, possible_feature_level), 0 }, - { "DNSSECMode", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(struct server_state, dnssec_mode), JSON_MANDATORY }, - { "DNSSECSupported", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, dnssec_supported), JSON_MANDATORY }, - { "ReceivedUDPFragmentMax", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct server_state, received_udp_fragment_max), JSON_MANDATORY }, - { "FailedUDPAttempts", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct server_state, n_failed_udp), JSON_MANDATORY }, - { "FailedTCPAttempts", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct server_state, n_failed_tcp), JSON_MANDATORY }, - { "PacketTruncated", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_truncated), JSON_MANDATORY }, - { "PacketBadOpt", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_bad_opt), JSON_MANDATORY }, - { "PacketRRSIGMissing", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_rrsig_missing), JSON_MANDATORY }, - { "PacketInvalid", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_invalid), JSON_MANDATORY }, - { "PacketDoOff", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct server_state, packet_do_off), JSON_MANDATORY }, + static const sd_json_dispatch_field dispatch_table[] = { + { "Server", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, server_name), SD_JSON_MANDATORY }, + { "Type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, type), SD_JSON_MANDATORY }, + { "Interface", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, ifname), 0 }, + { "InterfaceIndex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(struct server_state, ifindex), 0 }, + { "VerifiedFeatureLevel", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, verified_feature_level), 0 }, + { "PossibleFeatureLevel", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, possible_feature_level), 0 }, + { "DNSSECMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, dnssec_mode), SD_JSON_MANDATORY }, + { "DNSSECSupported", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, dnssec_supported), SD_JSON_MANDATORY }, + { "ReceivedUDPFragmentMax", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, received_udp_fragment_max), SD_JSON_MANDATORY }, + { "FailedUDPAttempts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, n_failed_udp), SD_JSON_MANDATORY }, + { "FailedTCPAttempts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, n_failed_tcp), SD_JSON_MANDATORY }, + { "PacketTruncated", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_truncated), SD_JSON_MANDATORY }, + { "PacketBadOpt", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_bad_opt), SD_JSON_MANDATORY }, + { "PacketRRSIGMissing", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_rrsig_missing), SD_JSON_MANDATORY }, + { "PacketInvalid", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_invalid), SD_JSON_MANDATORY }, + { "PacketDoOff", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_do_off), SD_JSON_MANDATORY }, {}, }; - r = json_dispatch(server, dispatch_table, JSON_LOG|JSON_ALLOW_EXTENSIONS, &server_state); + r = sd_json_dispatch(server, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &server_state); if (r < 0) return r; @@ -3171,7 +3172,7 @@ static int dump_server_state(JsonVariant *server) { } static int verb_show_server_state(int argc, char *argv[], void *userdata) { - JsonVariant *reply = NULL, *d = NULL; + sd_json_variant *reply = NULL, *d = NULL; _cleanup_(varlink_unrefp) Varlink *vl = NULL; int r; @@ -3183,17 +3184,17 @@ static int verb_show_server_state(int argc, char *argv[], void *userdata) { if (r < 0) return r; - d = json_variant_by_key(reply, "dump"); + d = sd_json_variant_by_key(reply, "dump"); if (!d) return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "DumpCache() response is missing 'dump' key."); - if (!json_variant_is_array(d)) + if (!sd_json_variant_is_array(d)) return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "DumpCache() response 'dump' field not an array"); - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - JsonVariant *i; + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + sd_json_variant *i; JSON_VARIANT_ARRAY_FOREACH(i, d) { r = dump_server_state(i); @@ -3204,7 +3205,7 @@ static int verb_show_server_state(int argc, char *argv[], void *userdata) { return 0; } - return json_variant_dump(d, arg_json_format_flags, NULL, NULL); + return sd_json_variant_dump(d, arg_json_format_flags, NULL, NULL); } static void help_protocol_types(void) { @@ -3934,7 +3935,7 @@ static int native_parse_argv(int argc, char *argv[]) { break; case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; break; case '?': diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index 90619085c4..afda571ed5 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -1425,8 +1425,8 @@ void dns_cache_dump(DnsCache *cache, FILE *f) { } } -int dns_cache_dump_to_json(DnsCache *cache, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *c = NULL; +int dns_cache_dump_to_json(DnsCache *cache, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *c = NULL; DnsCacheItem *i; int r; @@ -1434,17 +1434,17 @@ int dns_cache_dump_to_json(DnsCache *cache, JsonVariant **ret) { assert(ret); HASHMAP_FOREACH(i, cache->by_key) { - _cleanup_(json_variant_unrefp) JsonVariant *d = NULL, *k = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *d = NULL, *k = NULL; r = dns_resource_key_to_json(i->key, &k); if (r < 0) return r; if (i->rr) { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL; LIST_FOREACH(by_key, j, i) { - _cleanup_(json_variant_unrefp) JsonVariant *rj = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *rj = NULL; assert(j->rr); @@ -1456,48 +1456,48 @@ int dns_cache_dump_to_json(DnsCache *cache, JsonVariant **ret) { if (r < 0) return r; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &l, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("rr", rj), - JSON_BUILD_PAIR_BASE64("raw", j->rr->wire_format, j->rr->wire_format_size))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("rr", rj), + SD_JSON_BUILD_PAIR_BASE64("raw", j->rr->wire_format, j->rr->wire_format_size))); if (r < 0) return r; } if (!l) { - r = json_variant_new_array(&l, NULL, 0); + r = sd_json_variant_new_array(&l, NULL, 0); if (r < 0) return r; } - r = json_build(&d, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("key", k), - JSON_BUILD_PAIR_VARIANT("rrs", l), - JSON_BUILD_PAIR_UNSIGNED("until", i->until))); + r = sd_json_build(&d, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("key", k), + SD_JSON_BUILD_PAIR_VARIANT("rrs", l), + SD_JSON_BUILD_PAIR_UNSIGNED("until", i->until))); } else if (i->type == DNS_CACHE_NODATA) { - r = json_build(&d, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("key", k), - JSON_BUILD_PAIR_EMPTY_ARRAY("rrs"), - JSON_BUILD_PAIR_UNSIGNED("until", i->until))); + r = sd_json_build(&d, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("key", k), + SD_JSON_BUILD_PAIR_EMPTY_ARRAY("rrs"), + SD_JSON_BUILD_PAIR_UNSIGNED("until", i->until))); } else - r = json_build(&d, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("key", k), - JSON_BUILD_PAIR_STRING("type", dns_cache_item_type_to_string(i)), - JSON_BUILD_PAIR_UNSIGNED("until", i->until))); + r = sd_json_build(&d, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("key", k), + SD_JSON_BUILD_PAIR_STRING("type", dns_cache_item_type_to_string(i)), + SD_JSON_BUILD_PAIR_UNSIGNED("until", i->until))); if (r < 0) return r; - r = json_variant_append_array(&c, d); + r = sd_json_variant_append_array(&c, d); if (r < 0) return r; } if (!c) - return json_variant_new_array(ret, NULL, 0); + return sd_json_variant_new_array(ret, NULL, 0); *ret = TAKE_PTR(c); return 0; diff --git a/src/resolve/resolved-dns-cache.h b/src/resolve/resolved-dns-cache.h index 6a45b95a60..d8b71a3c2a 100644 --- a/src/resolve/resolved-dns-cache.h +++ b/src/resolve/resolved-dns-cache.h @@ -51,7 +51,7 @@ int dns_cache_lookup( int dns_cache_check_conflicts(DnsCache *cache, DnsResourceRecord *rr, int owner_family, const union in_addr_union *owner_address); void dns_cache_dump(DnsCache *cache, FILE *f); -int dns_cache_dump_to_json(DnsCache *cache, JsonVariant **ret); +int dns_cache_dump_to_json(DnsCache *cache, sd_json_variant **ret); bool dns_cache_is_empty(DnsCache *cache); diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c index 204d4a625e..a2e9196261 100644 --- a/src/resolve/resolved-dns-rr.c +++ b/src/resolve/resolved-dns-rr.c @@ -7,6 +7,7 @@ #include "dns-type.h" #include "escape.h" #include "hexdecoct.h" +#include "json-util.h" #include "memory-util.h" #include "resolved-dns-dnssec.h" #include "resolved-dns-packet.h" @@ -2130,34 +2131,34 @@ int dns_resource_record_new_from_raw(DnsResourceRecord **ret, const void *data, return dns_packet_read_rr(p, ret, NULL, NULL); } -int dns_resource_key_to_json(DnsResourceKey *key, JsonVariant **ret) { +int dns_resource_key_to_json(DnsResourceKey *key, sd_json_variant **ret) { assert(key); assert(ret); - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("class", JSON_BUILD_INTEGER(key->class)), - JSON_BUILD_PAIR("type", JSON_BUILD_INTEGER(key->type)), - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(dns_resource_key_name(key))))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("class", SD_JSON_BUILD_INTEGER(key->class)), + SD_JSON_BUILD_PAIR("type", SD_JSON_BUILD_INTEGER(key->type)), + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(dns_resource_key_name(key))))); } -int dns_resource_key_from_json(JsonVariant *v, DnsResourceKey **ret) { +int dns_resource_key_from_json(sd_json_variant *v, DnsResourceKey **ret) { _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; uint16_t type = 0, class = 0; const char *name = NULL; int r; - JsonDispatch dispatch_table[] = { - { "class", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint16, PTR_TO_SIZE(&class), JSON_MANDATORY }, - { "type", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint16, PTR_TO_SIZE(&type), JSON_MANDATORY }, - { "name", JSON_VARIANT_STRING, json_dispatch_const_string, PTR_TO_SIZE(&name), JSON_MANDATORY }, + sd_json_dispatch_field dispatch_table[] = { + { "class", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, PTR_TO_SIZE(&class), SD_JSON_MANDATORY }, + { "type", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, PTR_TO_SIZE(&type), SD_JSON_MANDATORY }, + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, PTR_TO_SIZE(&name), SD_JSON_MANDATORY }, {} }; assert(v); assert(ret); - r = json_dispatch(v, dispatch_table, 0, NULL); + r = sd_json_dispatch(v, dispatch_table, 0, NULL); if (r < 0) return r; @@ -2169,34 +2170,34 @@ int dns_resource_key_from_json(JsonVariant *v, DnsResourceKey **ret) { return 0; } -static int type_bitmap_to_json(Bitmap *b, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL; +static int type_bitmap_to_json(Bitmap *b, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL; unsigned t; int r; assert(ret); BITMAP_FOREACH(t, b) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - r = json_variant_new_unsigned(&v, t); + r = sd_json_variant_new_unsigned(&v, t); if (r < 0) return r; - r = json_variant_append_array(&l, v); + r = sd_json_variant_append_array(&l, v); if (r < 0) return r; } if (!l) - return json_variant_new_array(ret, NULL, 0); + return sd_json_variant_new_array(ret, NULL, 0); *ret = TAKE_PTR(l); return 0; } -static int txt_to_json(DnsTxtItem *items, JsonVariant **ret) { - JsonVariant **elements = NULL; +static int txt_to_json(DnsTxtItem *items, sd_json_variant **ret) { + sd_json_variant **elements = NULL; size_t n = 0; int r; @@ -2208,22 +2209,22 @@ static int txt_to_json(DnsTxtItem *items, JsonVariant **ret) { goto finalize; } - r = json_variant_new_octescape(elements + n, i->data, i->length); + r = sd_json_variant_new_octescape(elements + n, i->data, i->length); if (r < 0) goto finalize; n++; } - r = json_variant_new_array(ret, elements, n); + r = sd_json_variant_new_array(ret, elements, n); finalize: - json_variant_unref_many(elements, n); + sd_json_variant_unref_many(elements, n); return r; } -static int svc_params_to_json(DnsSvcParam *params, JsonVariant **ret) { - JsonVariant **elements = NULL; +static int svc_params_to_json(DnsSvcParam *params, sd_json_variant **ret) { + sd_json_variant **elements = NULL; size_t n = 0; int r; @@ -2235,21 +2236,21 @@ static int svc_params_to_json(DnsSvcParam *params, JsonVariant **ret) { goto finalize; } - r = json_variant_new_base64(elements + n, i->value, i->length); + r = sd_json_variant_new_base64(elements + n, i->value, i->length); if (r < 0) goto finalize; n++; } - r = json_variant_new_array(ret, elements, n); + r = sd_json_variant_new_array(ret, elements, n); finalize: - json_variant_unref_many(elements, n); + sd_json_variant_unref_many(elements, n); return r; } -int dns_resource_record_to_json(DnsResourceRecord *rr, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *k = NULL; +int dns_resource_record_to_json(DnsResourceRecord *rr, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *k = NULL; int r; assert(rr); @@ -2262,201 +2263,201 @@ int dns_resource_record_to_json(DnsResourceRecord *rr, JsonVariant **ret) { switch (rr->unparsable ? _DNS_TYPE_INVALID : rr->key->type) { case DNS_TYPE_SRV: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("priority", JSON_BUILD_UNSIGNED(rr->srv.priority)), - JSON_BUILD_PAIR("weight", JSON_BUILD_UNSIGNED(rr->srv.weight)), - JSON_BUILD_PAIR("port", JSON_BUILD_UNSIGNED(rr->srv.port)), - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(rr->srv.name)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("priority", SD_JSON_BUILD_UNSIGNED(rr->srv.priority)), + SD_JSON_BUILD_PAIR("weight", SD_JSON_BUILD_UNSIGNED(rr->srv.weight)), + SD_JSON_BUILD_PAIR("port", SD_JSON_BUILD_UNSIGNED(rr->srv.port)), + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(rr->srv.name)))); case DNS_TYPE_PTR: case DNS_TYPE_NS: case DNS_TYPE_CNAME: case DNS_TYPE_DNAME: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(rr->ptr.name)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(rr->ptr.name)))); case DNS_TYPE_HINFO: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("cpu", JSON_BUILD_STRING(rr->hinfo.cpu)), - JSON_BUILD_PAIR("os", JSON_BUILD_STRING(rr->hinfo.os)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("cpu", SD_JSON_BUILD_STRING(rr->hinfo.cpu)), + SD_JSON_BUILD_PAIR("os", SD_JSON_BUILD_STRING(rr->hinfo.os)))); case DNS_TYPE_SPF: case DNS_TYPE_TXT: { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL; r = txt_to_json(rr->txt.items, &l); if (r < 0) return r; - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("items", JSON_BUILD_VARIANT(l)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("items", SD_JSON_BUILD_VARIANT(l)))); } case DNS_TYPE_A: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("address", JSON_BUILD_IN4_ADDR(&rr->a.in_addr)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("address", JSON_BUILD_IN4_ADDR(&rr->a.in_addr)))); case DNS_TYPE_AAAA: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("address", JSON_BUILD_IN6_ADDR(&rr->aaaa.in6_addr)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("address", JSON_BUILD_IN6_ADDR(&rr->aaaa.in6_addr)))); case DNS_TYPE_SOA: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("mname", JSON_BUILD_STRING(rr->soa.mname)), - JSON_BUILD_PAIR("rname", JSON_BUILD_STRING(rr->soa.rname)), - JSON_BUILD_PAIR("serial", JSON_BUILD_UNSIGNED(rr->soa.serial)), - JSON_BUILD_PAIR("refresh", JSON_BUILD_UNSIGNED(rr->soa.refresh)), - JSON_BUILD_PAIR("expire", JSON_BUILD_UNSIGNED(rr->soa.retry)), - JSON_BUILD_PAIR("minimum", JSON_BUILD_UNSIGNED(rr->soa.minimum)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("mname", SD_JSON_BUILD_STRING(rr->soa.mname)), + SD_JSON_BUILD_PAIR("rname", SD_JSON_BUILD_STRING(rr->soa.rname)), + SD_JSON_BUILD_PAIR("serial", SD_JSON_BUILD_UNSIGNED(rr->soa.serial)), + SD_JSON_BUILD_PAIR("refresh", SD_JSON_BUILD_UNSIGNED(rr->soa.refresh)), + SD_JSON_BUILD_PAIR("expire", SD_JSON_BUILD_UNSIGNED(rr->soa.retry)), + SD_JSON_BUILD_PAIR("minimum", SD_JSON_BUILD_UNSIGNED(rr->soa.minimum)))); case DNS_TYPE_MX: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("priority", JSON_BUILD_UNSIGNED(rr->mx.priority)), - JSON_BUILD_PAIR("exchange", JSON_BUILD_STRING(rr->mx.exchange)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("priority", SD_JSON_BUILD_UNSIGNED(rr->mx.priority)), + SD_JSON_BUILD_PAIR("exchange", SD_JSON_BUILD_STRING(rr->mx.exchange)))); case DNS_TYPE_LOC: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("version", JSON_BUILD_UNSIGNED(rr->loc.version)), - JSON_BUILD_PAIR("size", JSON_BUILD_UNSIGNED(rr->loc.size)), - JSON_BUILD_PAIR("horiz_pre", JSON_BUILD_UNSIGNED(rr->loc.horiz_pre)), - JSON_BUILD_PAIR("vert_pre", JSON_BUILD_UNSIGNED(rr->loc.vert_pre)), - JSON_BUILD_PAIR("latitude", JSON_BUILD_UNSIGNED(rr->loc.latitude)), - JSON_BUILD_PAIR("longitude", JSON_BUILD_UNSIGNED(rr->loc.longitude)), - JSON_BUILD_PAIR("altitude", JSON_BUILD_UNSIGNED(rr->loc.altitude)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("version", SD_JSON_BUILD_UNSIGNED(rr->loc.version)), + SD_JSON_BUILD_PAIR("size", SD_JSON_BUILD_UNSIGNED(rr->loc.size)), + SD_JSON_BUILD_PAIR("horiz_pre", SD_JSON_BUILD_UNSIGNED(rr->loc.horiz_pre)), + SD_JSON_BUILD_PAIR("vert_pre", SD_JSON_BUILD_UNSIGNED(rr->loc.vert_pre)), + SD_JSON_BUILD_PAIR("latitude", SD_JSON_BUILD_UNSIGNED(rr->loc.latitude)), + SD_JSON_BUILD_PAIR("longitude", SD_JSON_BUILD_UNSIGNED(rr->loc.longitude)), + SD_JSON_BUILD_PAIR("altitude", SD_JSON_BUILD_UNSIGNED(rr->loc.altitude)))); case DNS_TYPE_DS: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("keyTag", JSON_BUILD_UNSIGNED(rr->ds.key_tag)), - JSON_BUILD_PAIR("algorithm", JSON_BUILD_UNSIGNED(rr->ds.algorithm)), - JSON_BUILD_PAIR("digestType", JSON_BUILD_UNSIGNED(rr->ds.digest_type)), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(rr->ds.digest, rr->ds.digest_size)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("keyTag", SD_JSON_BUILD_UNSIGNED(rr->ds.key_tag)), + SD_JSON_BUILD_PAIR("algorithm", SD_JSON_BUILD_UNSIGNED(rr->ds.algorithm)), + SD_JSON_BUILD_PAIR("digestType", SD_JSON_BUILD_UNSIGNED(rr->ds.digest_type)), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(rr->ds.digest, rr->ds.digest_size)))); case DNS_TYPE_SSHFP: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("algorithm", JSON_BUILD_UNSIGNED(rr->sshfp.algorithm)), - JSON_BUILD_PAIR("fptype", JSON_BUILD_UNSIGNED(rr->sshfp.fptype)), - JSON_BUILD_PAIR("fingerprint", JSON_BUILD_HEX(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("algorithm", SD_JSON_BUILD_UNSIGNED(rr->sshfp.algorithm)), + SD_JSON_BUILD_PAIR("fptype", SD_JSON_BUILD_UNSIGNED(rr->sshfp.fptype)), + SD_JSON_BUILD_PAIR("fingerprint", SD_JSON_BUILD_HEX(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size)))); case DNS_TYPE_DNSKEY: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(rr->dnskey.flags)), - JSON_BUILD_PAIR("protocol", JSON_BUILD_UNSIGNED(rr->dnskey.protocol)), - JSON_BUILD_PAIR("algorithm", JSON_BUILD_UNSIGNED(rr->dnskey.algorithm)), - JSON_BUILD_PAIR("dnskey", JSON_BUILD_BASE64(rr->dnskey.key, rr->dnskey.key_size)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(rr->dnskey.flags)), + SD_JSON_BUILD_PAIR("protocol", SD_JSON_BUILD_UNSIGNED(rr->dnskey.protocol)), + SD_JSON_BUILD_PAIR("algorithm", SD_JSON_BUILD_UNSIGNED(rr->dnskey.algorithm)), + SD_JSON_BUILD_PAIR("dnskey", SD_JSON_BUILD_BASE64(rr->dnskey.key, rr->dnskey.key_size)))); case DNS_TYPE_RRSIG: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("signer", JSON_BUILD_STRING(rr->rrsig.signer)), - JSON_BUILD_PAIR("typeCovered", JSON_BUILD_UNSIGNED(rr->rrsig.type_covered)), - JSON_BUILD_PAIR("algorithm", JSON_BUILD_UNSIGNED(rr->rrsig.algorithm)), - JSON_BUILD_PAIR("labels", JSON_BUILD_UNSIGNED(rr->rrsig.labels)), - JSON_BUILD_PAIR("originalTtl", JSON_BUILD_UNSIGNED(rr->rrsig.original_ttl)), - JSON_BUILD_PAIR("expiration", JSON_BUILD_UNSIGNED(rr->rrsig.expiration)), - JSON_BUILD_PAIR("inception", JSON_BUILD_UNSIGNED(rr->rrsig.inception)), - JSON_BUILD_PAIR("keyTag", JSON_BUILD_UNSIGNED(rr->rrsig.key_tag)), - JSON_BUILD_PAIR("signature", JSON_BUILD_BASE64(rr->rrsig.signature, rr->rrsig.signature_size)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("signer", SD_JSON_BUILD_STRING(rr->rrsig.signer)), + SD_JSON_BUILD_PAIR("typeCovered", SD_JSON_BUILD_UNSIGNED(rr->rrsig.type_covered)), + SD_JSON_BUILD_PAIR("algorithm", SD_JSON_BUILD_UNSIGNED(rr->rrsig.algorithm)), + SD_JSON_BUILD_PAIR("labels", SD_JSON_BUILD_UNSIGNED(rr->rrsig.labels)), + SD_JSON_BUILD_PAIR("originalTtl", SD_JSON_BUILD_UNSIGNED(rr->rrsig.original_ttl)), + SD_JSON_BUILD_PAIR("expiration", SD_JSON_BUILD_UNSIGNED(rr->rrsig.expiration)), + SD_JSON_BUILD_PAIR("inception", SD_JSON_BUILD_UNSIGNED(rr->rrsig.inception)), + SD_JSON_BUILD_PAIR("keyTag", SD_JSON_BUILD_UNSIGNED(rr->rrsig.key_tag)), + SD_JSON_BUILD_PAIR("signature", SD_JSON_BUILD_BASE64(rr->rrsig.signature, rr->rrsig.signature_size)))); case DNS_TYPE_NSEC: { - _cleanup_(json_variant_unrefp) JsonVariant *bm = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *bm = NULL; r = type_bitmap_to_json(rr->nsec.types, &bm); if (r < 0) return r; - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("nextDomain", JSON_BUILD_STRING(rr->nsec.next_domain_name)), - JSON_BUILD_PAIR("types", JSON_BUILD_VARIANT(bm)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("nextDomain", SD_JSON_BUILD_STRING(rr->nsec.next_domain_name)), + SD_JSON_BUILD_PAIR("types", SD_JSON_BUILD_VARIANT(bm)))); } case DNS_TYPE_NSEC3: { - _cleanup_(json_variant_unrefp) JsonVariant *bm = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *bm = NULL; r = type_bitmap_to_json(rr->nsec3.types, &bm); if (r < 0) return r; - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("algorithm", JSON_BUILD_UNSIGNED(rr->nsec3.algorithm)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(rr->nsec3.flags)), - JSON_BUILD_PAIR("iterations", JSON_BUILD_UNSIGNED(rr->nsec3.iterations)), - JSON_BUILD_PAIR("salt", JSON_BUILD_HEX(rr->nsec3.salt, rr->nsec3.salt_size)), - JSON_BUILD_PAIR("hash", JSON_BUILD_BASE32HEX(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size)), - JSON_BUILD_PAIR("types", JSON_BUILD_VARIANT(bm)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("algorithm", SD_JSON_BUILD_UNSIGNED(rr->nsec3.algorithm)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(rr->nsec3.flags)), + SD_JSON_BUILD_PAIR("iterations", SD_JSON_BUILD_UNSIGNED(rr->nsec3.iterations)), + SD_JSON_BUILD_PAIR("salt", SD_JSON_BUILD_HEX(rr->nsec3.salt, rr->nsec3.salt_size)), + SD_JSON_BUILD_PAIR("hash", SD_JSON_BUILD_BASE32HEX(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size)), + SD_JSON_BUILD_PAIR("types", SD_JSON_BUILD_VARIANT(bm)))); } case DNS_TYPE_TLSA: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("certUsage", JSON_BUILD_UNSIGNED(rr->tlsa.cert_usage)), - JSON_BUILD_PAIR("selector", JSON_BUILD_UNSIGNED(rr->tlsa.selector)), - JSON_BUILD_PAIR("matchingType", JSON_BUILD_UNSIGNED(rr->tlsa.matching_type)), - JSON_BUILD_PAIR("data", JSON_BUILD_HEX(rr->tlsa.data, rr->tlsa.data_size)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("certUsage", SD_JSON_BUILD_UNSIGNED(rr->tlsa.cert_usage)), + SD_JSON_BUILD_PAIR("selector", SD_JSON_BUILD_UNSIGNED(rr->tlsa.selector)), + SD_JSON_BUILD_PAIR("matchingType", SD_JSON_BUILD_UNSIGNED(rr->tlsa.matching_type)), + SD_JSON_BUILD_PAIR("data", SD_JSON_BUILD_HEX(rr->tlsa.data, rr->tlsa.data_size)))); case DNS_TYPE_SVCB: case DNS_TYPE_HTTPS: { - _cleanup_(json_variant_unrefp) JsonVariant *p = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *p = NULL; r = svc_params_to_json(rr->svcb.params, &p); if (r < 0) return r; - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("priority", JSON_BUILD_UNSIGNED(rr->svcb.priority)), - JSON_BUILD_PAIR("target", JSON_BUILD_STRING(rr->svcb.target_name)), - JSON_BUILD_PAIR("params", JSON_BUILD_VARIANT(p)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("priority", SD_JSON_BUILD_UNSIGNED(rr->svcb.priority)), + SD_JSON_BUILD_PAIR("target", SD_JSON_BUILD_STRING(rr->svcb.target_name)), + SD_JSON_BUILD_PAIR("params", SD_JSON_BUILD_VARIANT(p)))); } case DNS_TYPE_CAA: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(rr->caa.flags)), - JSON_BUILD_PAIR("tag", JSON_BUILD_STRING(rr->caa.tag)), - JSON_BUILD_PAIR("value", JSON_BUILD_OCTESCAPE(rr->caa.value, rr->caa.value_size)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(rr->caa.flags)), + SD_JSON_BUILD_PAIR("tag", SD_JSON_BUILD_STRING(rr->caa.tag)), + SD_JSON_BUILD_PAIR("value", SD_JSON_BUILD_OCTESCAPE(rr->caa.value, rr->caa.value_size)))); case DNS_TYPE_NAPTR: - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("key", JSON_BUILD_VARIANT(k)), - JSON_BUILD_PAIR("order", JSON_BUILD_UNSIGNED(rr->naptr.order)), - JSON_BUILD_PAIR("preference", JSON_BUILD_UNSIGNED(rr->naptr.preference)), + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("key", SD_JSON_BUILD_VARIANT(k)), + SD_JSON_BUILD_PAIR("order", SD_JSON_BUILD_UNSIGNED(rr->naptr.order)), + SD_JSON_BUILD_PAIR("preference", SD_JSON_BUILD_UNSIGNED(rr->naptr.preference)), /* NB: we name this flags field here naptrFlags, because there's already another "flags" field (for example in CAA) which has a different type */ - JSON_BUILD_PAIR("naptrFlags", JSON_BUILD_STRING(rr->naptr.flags)), - JSON_BUILD_PAIR("services", JSON_BUILD_STRING(rr->naptr.services)), - JSON_BUILD_PAIR("regexp", JSON_BUILD_STRING(rr->naptr.regexp)), - JSON_BUILD_PAIR("replacement", JSON_BUILD_STRING(rr->naptr.replacement)))); + SD_JSON_BUILD_PAIR("naptrFlags", SD_JSON_BUILD_STRING(rr->naptr.flags)), + SD_JSON_BUILD_PAIR("services", SD_JSON_BUILD_STRING(rr->naptr.services)), + SD_JSON_BUILD_PAIR("regexp", SD_JSON_BUILD_STRING(rr->naptr.regexp)), + SD_JSON_BUILD_PAIR("replacement", SD_JSON_BUILD_STRING(rr->naptr.replacement)))); default: /* Can't provide broken-down format */ diff --git a/src/resolve/resolved-dns-rr.h b/src/resolve/resolved-dns-rr.h index 156fa01873..23bf2f66d4 100644 --- a/src/resolve/resolved-dns-rr.h +++ b/src/resolve/resolved-dns-rr.h @@ -3,12 +3,13 @@ #include <netinet/in.h> +#include "sd-json.h" + #include "bitmap.h" #include "dns-def.h" #include "dns-type.h" #include "hashmap.h" #include "in-addr-util.h" -#include "json.h" #include "list.h" #include "string-util.h" #include "time-util.h" @@ -404,9 +405,9 @@ DnsSvcParam *dns_svc_params_copy(DnsSvcParam *first); int dns_resource_record_new_from_raw(DnsResourceRecord **ret, const void *data, size_t size); -int dns_resource_key_to_json(DnsResourceKey *key, JsonVariant **ret); -int dns_resource_key_from_json(JsonVariant *v, DnsResourceKey **ret); -int dns_resource_record_to_json(DnsResourceRecord *rr, JsonVariant **ret); +int dns_resource_key_to_json(DnsResourceKey *key, sd_json_variant **ret); +int dns_resource_key_from_json(sd_json_variant *v, DnsResourceKey **ret); +int dns_resource_record_to_json(DnsResourceRecord *rr, sd_json_variant **ret); void dns_resource_record_hash_func(const DnsResourceRecord *i, struct siphash *state); int dns_resource_record_compare_func(const DnsResourceRecord *x, const DnsResourceRecord *y); diff --git a/src/resolve/resolved-dns-scope.c b/src/resolve/resolved-dns-scope.c index 17bc823196..a2756c1162 100644 --- a/src/resolve/resolved-dns-scope.c +++ b/src/resolve/resolved-dns-scope.c @@ -1715,8 +1715,8 @@ bool dns_scope_is_default_route(DnsScope *scope) { return !dns_scope_has_route_only_domains(scope); } -int dns_scope_dump_cache_to_json(DnsScope *scope, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *cache = NULL; +int dns_scope_dump_cache_to_json(DnsScope *scope, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *cache = NULL; int r; assert(scope); @@ -1726,13 +1726,13 @@ int dns_scope_dump_cache_to_json(DnsScope *scope, JsonVariant **ret) { if (r < 0) return r; - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("protocol", dns_protocol_to_string(scope->protocol)), - JSON_BUILD_PAIR_CONDITION(scope->family != AF_UNSPEC, "family", JSON_BUILD_INTEGER(scope->family)), - JSON_BUILD_PAIR_CONDITION(scope->link, "ifindex", JSON_BUILD_INTEGER(scope->link ? scope->link->ifindex : 0)), - JSON_BUILD_PAIR_CONDITION(scope->link, "ifname", JSON_BUILD_STRING(scope->link ? scope->link->ifname : NULL)), - JSON_BUILD_PAIR_VARIANT("cache", cache))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("protocol", dns_protocol_to_string(scope->protocol)), + SD_JSON_BUILD_PAIR_CONDITION(scope->family != AF_UNSPEC, "family", SD_JSON_BUILD_INTEGER(scope->family)), + SD_JSON_BUILD_PAIR_CONDITION(!!scope->link, "ifindex", SD_JSON_BUILD_INTEGER(scope->link ? scope->link->ifindex : 0)), + SD_JSON_BUILD_PAIR_CONDITION(!!scope->link, "ifname", SD_JSON_BUILD_STRING(scope->link ? scope->link->ifname : NULL)), + SD_JSON_BUILD_PAIR_VARIANT("cache", cache))); } int dns_type_suitable_for_protocol(uint16_t type, DnsProtocol protocol) { diff --git a/src/resolve/resolved-dns-scope.h b/src/resolve/resolved-dns-scope.h index 23f147b8bd..872bc2abe7 100644 --- a/src/resolve/resolved-dns-scope.h +++ b/src/resolve/resolved-dns-scope.h @@ -114,7 +114,7 @@ int dns_scope_remove_dnssd_services(DnsScope *scope); bool dns_scope_is_default_route(DnsScope *scope); -int dns_scope_dump_cache_to_json(DnsScope *scope, JsonVariant **ret); +int dns_scope_dump_cache_to_json(DnsScope *scope, sd_json_variant **ret); int dns_type_suitable_for_protocol(uint16_t type, DnsProtocol protocol); int dns_question_types_suitable_for_protocol(DnsQuestion *q, DnsProtocol protocol); diff --git a/src/resolve/resolved-dns-server.c b/src/resolve/resolved-dns-server.c index b37f541c7f..c092bb1189 100644 --- a/src/resolve/resolved-dns-server.c +++ b/src/resolve/resolved-dns-server.c @@ -1106,27 +1106,27 @@ static const char* const dns_server_feature_level_table[_DNS_SERVER_FEATURE_LEVE }; DEFINE_STRING_TABLE_LOOKUP(dns_server_feature_level, DnsServerFeatureLevel); -int dns_server_dump_state_to_json(DnsServer *server, JsonVariant **ret) { +int dns_server_dump_state_to_json(DnsServer *server, sd_json_variant **ret) { assert(server); assert(ret); - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("Server", strna(dns_server_string_full(server))), - JSON_BUILD_PAIR_STRING("Type", strna(dns_server_type_to_string(server->type))), - JSON_BUILD_PAIR_CONDITION(server->type == DNS_SERVER_LINK, "Interface", JSON_BUILD_STRING(server->link ? server->link->ifname : NULL)), - JSON_BUILD_PAIR_CONDITION(server->type == DNS_SERVER_LINK, "InterfaceIndex", JSON_BUILD_UNSIGNED(server->link ? server->link->ifindex : 0)), - JSON_BUILD_PAIR_STRING("VerifiedFeatureLevel", strna(dns_server_feature_level_to_string(server->verified_feature_level))), - JSON_BUILD_PAIR_STRING("PossibleFeatureLevel", strna(dns_server_feature_level_to_string(server->possible_feature_level))), - JSON_BUILD_PAIR_STRING("DNSSECMode", strna(dnssec_mode_to_string(dns_server_get_dnssec_mode(server)))), - JSON_BUILD_PAIR_BOOLEAN("DNSSECSupported", dns_server_dnssec_supported(server)), - JSON_BUILD_PAIR_UNSIGNED("ReceivedUDPFragmentMax", server->received_udp_fragment_max), - JSON_BUILD_PAIR_UNSIGNED("FailedUDPAttempts", server->n_failed_udp), - JSON_BUILD_PAIR_UNSIGNED("FailedTCPAttempts", server->n_failed_tcp), - JSON_BUILD_PAIR_BOOLEAN("PacketTruncated", server->packet_truncated), - JSON_BUILD_PAIR_BOOLEAN("PacketBadOpt", server->packet_bad_opt), - JSON_BUILD_PAIR_BOOLEAN("PacketRRSIGMissing", server->packet_rrsig_missing), - JSON_BUILD_PAIR_BOOLEAN("PacketInvalid", server->packet_invalid), - JSON_BUILD_PAIR_BOOLEAN("PacketDoOff", server->packet_do_off))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("Server", strna(dns_server_string_full(server))), + SD_JSON_BUILD_PAIR_STRING("Type", strna(dns_server_type_to_string(server->type))), + SD_JSON_BUILD_PAIR_CONDITION(server->type == DNS_SERVER_LINK, "Interface", SD_JSON_BUILD_STRING(server->link ? server->link->ifname : NULL)), + SD_JSON_BUILD_PAIR_CONDITION(server->type == DNS_SERVER_LINK, "InterfaceIndex", SD_JSON_BUILD_UNSIGNED(server->link ? server->link->ifindex : 0)), + SD_JSON_BUILD_PAIR_STRING("VerifiedFeatureLevel", strna(dns_server_feature_level_to_string(server->verified_feature_level))), + SD_JSON_BUILD_PAIR_STRING("PossibleFeatureLevel", strna(dns_server_feature_level_to_string(server->possible_feature_level))), + SD_JSON_BUILD_PAIR_STRING("DNSSECMode", strna(dnssec_mode_to_string(dns_server_get_dnssec_mode(server)))), + SD_JSON_BUILD_PAIR_BOOLEAN("DNSSECSupported", dns_server_dnssec_supported(server)), + SD_JSON_BUILD_PAIR_UNSIGNED("ReceivedUDPFragmentMax", server->received_udp_fragment_max), + SD_JSON_BUILD_PAIR_UNSIGNED("FailedUDPAttempts", server->n_failed_udp), + SD_JSON_BUILD_PAIR_UNSIGNED("FailedTCPAttempts", server->n_failed_tcp), + SD_JSON_BUILD_PAIR_BOOLEAN("PacketTruncated", server->packet_truncated), + SD_JSON_BUILD_PAIR_BOOLEAN("PacketBadOpt", server->packet_bad_opt), + SD_JSON_BUILD_PAIR_BOOLEAN("PacketRRSIGMissing", server->packet_rrsig_missing), + SD_JSON_BUILD_PAIR_BOOLEAN("PacketInvalid", server->packet_invalid), + SD_JSON_BUILD_PAIR_BOOLEAN("PacketDoOff", server->packet_do_off))); } diff --git a/src/resolve/resolved-dns-server.h b/src/resolve/resolved-dns-server.h index ef76bbc878..157c908548 100644 --- a/src/resolve/resolved-dns-server.h +++ b/src/resolve/resolved-dns-server.h @@ -1,8 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include "sd-json.h" + #include "in-addr-util.h" -#include "json.h" #include "list.h" #include "resolve-util.h" #include "time-util.h" @@ -181,4 +182,4 @@ void dns_server_unref_stream(DnsServer *s); DnsScope *dns_server_scope(DnsServer *s); -int dns_server_dump_state_to_json(DnsServer *server, JsonVariant **ret); +int dns_server_dump_state_to_json(DnsServer *server, sd_json_variant **ret); diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 99787f7822..5959546dbb 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -1139,21 +1139,21 @@ static int manager_ipv6_send( return sendmsg_loop(fd, &mh, 0); } -static int dns_question_to_json(DnsQuestion *q, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL; +static int dns_question_to_json(DnsQuestion *q, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL; DnsResourceKey *key; int r; assert(ret); DNS_QUESTION_FOREACH(key, q) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = dns_resource_key_to_json(key, &v); if (r < 0) return r; - r = json_variant_append_array(&l, v); + r = sd_json_variant_append_array(&l, v); if (r < 0) return r; } @@ -1163,7 +1163,7 @@ static int dns_question_to_json(DnsQuestion *q, JsonVariant **ret) { } int manager_monitor_send(Manager *m, DnsQuery *q) { - _cleanup_(json_variant_unrefp) JsonVariant *jquestion = NULL, *jcollected_questions = NULL, *janswer = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *jquestion = NULL, *jcollected_questions = NULL, *janswer = NULL; _cleanup_(dns_question_unrefp) DnsQuestion *merged = NULL; Varlink *connection; DnsAnswerItem *rri; @@ -1201,7 +1201,7 @@ int manager_monitor_send(Manager *m, DnsQuery *q) { return log_error_errno(r, "Failed to convert question to JSON: %m"); DNS_ANSWER_FOREACH_ITEM(rri, q->answer) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = dns_resource_record_to_json(rri->rr, &v); if (r < 0) @@ -1211,40 +1211,40 @@ int manager_monitor_send(Manager *m, DnsQuery *q) { if (r < 0) return log_error_errno(r, "Failed to generate RR wire format: %m"); - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &janswer, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(v, "rr", JSON_BUILD_VARIANT(v)), - JSON_BUILD_PAIR("raw", JSON_BUILD_BASE64(rri->rr->wire_format, rri->rr->wire_format_size)), - JSON_BUILD_PAIR_CONDITION(rri->ifindex > 0, "ifindex", JSON_BUILD_INTEGER(rri->ifindex)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(!!v, "rr", SD_JSON_BUILD_VARIANT(v)), + SD_JSON_BUILD_PAIR("raw", SD_JSON_BUILD_BASE64(rri->rr->wire_format, rri->rr->wire_format_size)), + SD_JSON_BUILD_PAIR_CONDITION(rri->ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(rri->ifindex)))); if (r < 0) return log_debug_errno(r, "Failed to append notification entry to array: %m"); } SET_FOREACH(connection, m->varlink_subscription) { r = varlink_notifyb(connection, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("state", JSON_BUILD_STRING(dns_transaction_state_to_string(q->state))), - JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_DNSSEC_FAILED, - "result", JSON_BUILD_STRING(dnssec_result_to_string(q->answer_dnssec_result))), - JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_RCODE_FAILURE, - "rcode", JSON_BUILD_INTEGER(q->answer_rcode)), - JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_ERRNO, - "errno", JSON_BUILD_INTEGER(q->answer_errno)), - JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("state", SD_JSON_BUILD_STRING(dns_transaction_state_to_string(q->state))), + SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_DNSSEC_FAILED, + "result", SD_JSON_BUILD_STRING(dnssec_result_to_string(q->answer_dnssec_result))), + SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_RCODE_FAILURE, + "rcode", SD_JSON_BUILD_INTEGER(q->answer_rcode)), + SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_ERRNO, + "errno", SD_JSON_BUILD_INTEGER(q->answer_errno)), + SD_JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, DNS_TRANSACTION_DNSSEC_FAILED, DNS_TRANSACTION_RCODE_FAILURE) && q->answer_ede_rcode >= 0, - "extendedDNSErrorCode", JSON_BUILD_INTEGER(q->answer_ede_rcode)), - JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, + "extendedDNSErrorCode", SD_JSON_BUILD_INTEGER(q->answer_ede_rcode)), + SD_JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, DNS_TRANSACTION_DNSSEC_FAILED, DNS_TRANSACTION_RCODE_FAILURE) && q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), - "extendedDNSErrorMessage", JSON_BUILD_STRING(q->answer_ede_msg)), - JSON_BUILD_PAIR("question", JSON_BUILD_VARIANT(jquestion)), - JSON_BUILD_PAIR_CONDITION(jcollected_questions, - "collectedQuestions", JSON_BUILD_VARIANT(jcollected_questions)), - JSON_BUILD_PAIR_CONDITION(janswer, - "answer", JSON_BUILD_VARIANT(janswer)))); + "extendedDNSErrorMessage", SD_JSON_BUILD_STRING(q->answer_ede_msg)), + SD_JSON_BUILD_PAIR("question", SD_JSON_BUILD_VARIANT(jquestion)), + SD_JSON_BUILD_PAIR_CONDITION(!!jcollected_questions, + "collectedQuestions", SD_JSON_BUILD_VARIANT(jcollected_questions)), + SD_JSON_BUILD_PAIR_CONDITION(!!janswer, + "answer", SD_JSON_BUILD_VARIANT(janswer)))); if (r < 0) log_debug_errno(r, "Failed to send monitor event, ignoring: %m"); } @@ -1879,7 +1879,7 @@ int socket_disable_pmtud(int fd, int af) { } } -int dns_manager_dump_statistics_json(Manager *m, JsonVariant **ret) { +int dns_manager_dump_statistics_json(Manager *m, sd_json_variant **ret) { uint64_t size = 0, hit = 0, miss = 0; assert(m); @@ -1891,26 +1891,26 @@ int dns_manager_dump_statistics_json(Manager *m, JsonVariant **ret) { miss += s->cache.n_miss; } - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("transactions", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("currentTransactions", hashmap_size(m->dns_transactions)), - JSON_BUILD_PAIR_UNSIGNED("totalTransactions", m->n_transactions_total), - JSON_BUILD_PAIR_UNSIGNED("totalTimeouts", m->n_timeouts_total), - JSON_BUILD_PAIR_UNSIGNED("totalTimeoutsServedStale", m->n_timeouts_served_stale_total), - JSON_BUILD_PAIR_UNSIGNED("totalFailedResponses", m->n_failure_responses_total), - JSON_BUILD_PAIR_UNSIGNED("totalFailedResponsesServedStale", m->n_failure_responses_served_stale_total) + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("transactions", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("currentTransactions", hashmap_size(m->dns_transactions)), + SD_JSON_BUILD_PAIR_UNSIGNED("totalTransactions", m->n_transactions_total), + SD_JSON_BUILD_PAIR_UNSIGNED("totalTimeouts", m->n_timeouts_total), + SD_JSON_BUILD_PAIR_UNSIGNED("totalTimeoutsServedStale", m->n_timeouts_served_stale_total), + SD_JSON_BUILD_PAIR_UNSIGNED("totalFailedResponses", m->n_failure_responses_total), + SD_JSON_BUILD_PAIR_UNSIGNED("totalFailedResponsesServedStale", m->n_failure_responses_served_stale_total) )), - JSON_BUILD_PAIR("cache", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("size", size), - JSON_BUILD_PAIR_UNSIGNED("hits", hit), - JSON_BUILD_PAIR_UNSIGNED("misses", miss) + SD_JSON_BUILD_PAIR("cache", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("size", size), + SD_JSON_BUILD_PAIR_UNSIGNED("hits", hit), + SD_JSON_BUILD_PAIR_UNSIGNED("misses", miss) )), - JSON_BUILD_PAIR("dnssec", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("secure", m->n_dnssec_verdict[DNSSEC_SECURE]), - JSON_BUILD_PAIR_UNSIGNED("insecure", m->n_dnssec_verdict[DNSSEC_INSECURE]), - JSON_BUILD_PAIR_UNSIGNED("bogus", m->n_dnssec_verdict[DNSSEC_BOGUS]), - JSON_BUILD_PAIR_UNSIGNED("indeterminate", m->n_dnssec_verdict[DNSSEC_INDETERMINATE]) + SD_JSON_BUILD_PAIR("dnssec", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("secure", m->n_dnssec_verdict[DNSSEC_SECURE]), + SD_JSON_BUILD_PAIR_UNSIGNED("insecure", m->n_dnssec_verdict[DNSSEC_INSECURE]), + SD_JSON_BUILD_PAIR_UNSIGNED("bogus", m->n_dnssec_verdict[DNSSEC_BOGUS]), + SD_JSON_BUILD_PAIR_UNSIGNED("indeterminate", m->n_dnssec_verdict[DNSSEC_INDETERMINATE]) )))); } diff --git a/src/resolve/resolved-manager.h b/src/resolve/resolved-manager.h index bd0e0532e7..b4a2954b03 100644 --- a/src/resolve/resolved-manager.h +++ b/src/resolve/resolved-manager.h @@ -226,6 +226,6 @@ bool manager_server_is_stub(Manager *m, DnsServer *s); int socket_disable_pmtud(int fd, int af); -int dns_manager_dump_statistics_json(Manager *m, JsonVariant **ret); +int dns_manager_dump_statistics_json(Manager *m, sd_json_variant **ret); void dns_manager_reset_statistics(Manager *m); diff --git a/src/resolve/resolved-varlink.c b/src/resolve/resolved-varlink.c index 25f85d8233..d0da67c78b 100644 --- a/src/resolve/resolved-varlink.c +++ b/src/resolve/resolved-varlink.c @@ -2,6 +2,7 @@ #include "glyph-util.h" #include "in-addr-util.h" +#include "json-util.h" #include "resolved-dns-synthesize.h" #include "resolved-varlink.h" #include "socket-netlink.h" @@ -60,11 +61,11 @@ static int reply_query_state(DnsQuery *q) { case DNS_TRANSACTION_DNSSEC_FAILED: return varlink_errorb(q->varlink_request, "io.systemd.Resolve.DNSSECValidationFailed", - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("result", JSON_BUILD_STRING(dnssec_result_to_string(q->answer_dnssec_result))), - JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0, - "extendedDNSErrorCode", JSON_BUILD_INTEGER(q->answer_ede_rcode)), - JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), - "extendedDNSErrorMessage", JSON_BUILD_STRING(q->answer_ede_msg)))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("result", SD_JSON_BUILD_STRING(dnssec_result_to_string(q->answer_dnssec_result))), + SD_JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0, + "extendedDNSErrorCode", SD_JSON_BUILD_INTEGER(q->answer_ede_rcode)), + SD_JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), + "extendedDNSErrorMessage", SD_JSON_BUILD_STRING(q->answer_ede_msg)))); case DNS_TRANSACTION_NO_TRUST_ANCHOR: return varlink_error(q->varlink_request, "io.systemd.Resolve.NoTrustAnchor", NULL); @@ -85,15 +86,15 @@ static int reply_query_state(DnsQuery *q) { /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */ return varlink_errorb(q->varlink_request, "io.systemd.Resolve.DNSError", - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("rcode", JSON_BUILD_INTEGER(DNS_RCODE_NXDOMAIN)))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("rcode", SD_JSON_BUILD_INTEGER(DNS_RCODE_NXDOMAIN)))); case DNS_TRANSACTION_RCODE_FAILURE: return varlink_errorb(q->varlink_request, "io.systemd.Resolve.DNSError", - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("rcode", JSON_BUILD_INTEGER(q->answer_rcode)), - JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0, - "extendedDNSErrorCode", JSON_BUILD_INTEGER(q->answer_ede_rcode)), - JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), - "extendedDNSErrorMessage", JSON_BUILD_STRING(q->answer_ede_msg)))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("rcode", SD_JSON_BUILD_INTEGER(q->answer_rcode)), + SD_JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0, + "extendedDNSErrorCode", SD_JSON_BUILD_INTEGER(q->answer_ede_rcode)), + SD_JSON_BUILD_PAIR_CONDITION(q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), + "extendedDNSErrorMessage", SD_JSON_BUILD_STRING(q->answer_ede_msg)))); case DNS_TRANSACTION_NULL: case DNS_TRANSACTION_PENDING: @@ -181,7 +182,7 @@ static bool validate_and_mangle_flags( } static int find_addr_records( - JsonVariant **array, + sd_json_variant **array, DnsQuestion *question, DnsQuery *q, DnsResourceRecord **canonical, @@ -190,7 +191,7 @@ static int find_addr_records( int ifindex, r; DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) { - _cleanup_(json_variant_unrefp) JsonVariant *entry = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *entry = NULL; int family; const void *p; @@ -210,15 +211,15 @@ static int find_addr_records( return -EAFNOSUPPORT; } - r = json_build(&entry, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", JSON_BUILD_INTEGER(ifindex)), - JSON_BUILD_PAIR("family", JSON_BUILD_INTEGER(family)), - JSON_BUILD_PAIR("address", JSON_BUILD_BYTE_ARRAY(p, FAMILY_ADDRESS_SIZE(family))))); + r = sd_json_build(&entry, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(ifindex)), + SD_JSON_BUILD_PAIR("family", SD_JSON_BUILD_INTEGER(family)), + SD_JSON_BUILD_PAIR("address", SD_JSON_BUILD_BYTE_ARRAY(p, FAMILY_ADDRESS_SIZE(family))))); if (r < 0) return r; - r = json_variant_append_array(array, entry); + r = sd_json_variant_append_array(array, entry); if (r < 0) return r; @@ -231,7 +232,7 @@ static int find_addr_records( static void vl_method_resolve_hostname_complete(DnsQuery *query) { _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; _cleanup_(dns_query_freep) DnsQuery *q = query; _cleanup_free_ char *normalized = NULL; DnsQuestion *question; @@ -263,7 +264,7 @@ static void vl_method_resolve_hostname_complete(DnsQuery *query) { if (r < 0) goto finish; - if (json_variant_is_blank_object(array)) { + if (sd_json_variant_is_blank_object(array)) { r = varlink_error(q->varlink_request, "io.systemd.Resolve.NoSuchResourceRecord", NULL); goto finish; } @@ -274,10 +275,10 @@ static void vl_method_resolve_hostname_complete(DnsQuery *query) { goto finish; r = varlink_replyb(q->varlink_request, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("addresses", JSON_BUILD_VARIANT(array)), - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(normalized)), - JSON_BUILD_PAIR("flags", JSON_BUILD_INTEGER(dns_query_reply_flags_make(q))))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("addresses", SD_JSON_BUILD_VARIANT(array)), + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(normalized)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_INTEGER(dns_query_reply_flags_make(q))))); finish: if (r < 0) { log_full_errno(ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_ERR, r, "Failed to send hostname reply: %m"); @@ -312,24 +313,24 @@ static int parse_as_address(Varlink *link, LookupParameters *p) { return varlink_replyb( link, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("addresses", - JSON_BUILD_ARRAY( - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", JSON_BUILD_INTEGER(ifindex)), - JSON_BUILD_PAIR("family", JSON_BUILD_INTEGER(ff)), - JSON_BUILD_PAIR("address", JSON_BUILD_BYTE_ARRAY(&parsed, FAMILY_ADDRESS_SIZE(ff)))))), - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(canonical)), - JSON_BUILD_PAIR("flags", JSON_BUILD_INTEGER(SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(p->flags), ff, true, true)| + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("addresses", + SD_JSON_BUILD_ARRAY( + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(ifindex)), + SD_JSON_BUILD_PAIR("family", SD_JSON_BUILD_INTEGER(ff)), + SD_JSON_BUILD_PAIR("address", SD_JSON_BUILD_BYTE_ARRAY(&parsed, FAMILY_ADDRESS_SIZE(ff)))))), + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(canonical)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_INTEGER(SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(p->flags), ff, true, true)| SD_RESOLVED_SYNTHETIC)))); } -static int vl_method_resolve_hostname(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "ifindex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(LookupParameters, ifindex), 0 }, - { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(LookupParameters, name), JSON_MANDATORY }, - { "family", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(LookupParameters, family), 0 }, - { "flags", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, +static int vl_method_resolve_hostname(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParameters, ifindex), 0 }, + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(LookupParameters, name), SD_JSON_MANDATORY }, + { "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParameters, family), 0 }, + { "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, {} }; @@ -397,28 +398,28 @@ static int vl_method_resolve_hostname(Varlink *link, JsonVariant *parameters, Va return 1; } -static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_address(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { LookupParameters *p = ASSERT_PTR(userdata); union in_addr_union buf = {}; - JsonVariant *i; + sd_json_variant *i; size_t n, k = 0; assert(variant); - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); - n = json_variant_elements(variant); + n = sd_json_variant_elements(variant); if (!IN_SET(n, 4, 16)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is array of unexpected size.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(i, variant) { int64_t b; - if (!json_variant_is_integer(i)) + if (!sd_json_variant_is_integer(i)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an integer.", k, strna(name)); - b = json_variant_integer(i); + b = sd_json_variant_integer(i); if (b < 0 || b > 0xff) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is out of range 0%s255.", @@ -434,7 +435,7 @@ static int json_dispatch_address(const char *name, JsonVariant *variant, JsonDis } static void vl_method_resolve_address_complete(DnsQuery *query) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; _cleanup_(dns_query_freep) DnsQuery *q = query; DnsQuestion *question; DnsResourceRecord *rr; @@ -475,24 +476,24 @@ static void vl_method_resolve_address_complete(DnsQuery *query) { if (r < 0) goto finish; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", JSON_BUILD_INTEGER(ifindex)), - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(normalized)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(ifindex)), + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(normalized)))); if (r < 0) goto finish; } - if (json_variant_is_blank_object(array)) { + if (sd_json_variant_is_blank_object(array)) { r = varlink_error(q->varlink_request, "io.systemd.Resolve.NoSuchResourceRecord", NULL); goto finish; } r = varlink_replyb(q->varlink_request, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("names", JSON_BUILD_VARIANT(array)), - JSON_BUILD_PAIR("flags", JSON_BUILD_INTEGER(dns_query_reply_flags_make(q))))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("names", SD_JSON_BUILD_VARIANT(array)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_INTEGER(dns_query_reply_flags_make(q))))); finish: if (r < 0) { log_full_errno(ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_ERR, r, "Failed to send address reply: %m"); @@ -500,12 +501,12 @@ finish: } } -static int vl_method_resolve_address(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "ifindex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(LookupParameters, ifindex), 0 }, - { "family", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(LookupParameters, family), JSON_MANDATORY }, - { "address", JSON_VARIANT_ARRAY, json_dispatch_address, 0, JSON_MANDATORY }, - { "flags", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, +static int vl_method_resolve_address(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParameters, ifindex), 0 }, + { "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParameters, family), SD_JSON_MANDATORY }, + { "address", SD_JSON_VARIANT_ARRAY, json_dispatch_address, 0, SD_JSON_MANDATORY }, + { "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, {} }; @@ -564,7 +565,7 @@ static int vl_method_resolve_address(Varlink *link, JsonVariant *parameters, Var return 1; } -static int append_txt(JsonVariant **txt, DnsResourceRecord *rr) { +static int append_txt(sd_json_variant **txt, DnsResourceRecord *rr) { int r; assert(txt); @@ -575,16 +576,16 @@ static int append_txt(JsonVariant **txt, DnsResourceRecord *rr) { return 0; LIST_FOREACH(items, i, rr->txt.items) { - _cleanup_(json_variant_unrefp) JsonVariant *entry = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *entry = NULL; if (i->length <= 0) continue; - r = json_variant_new_octescape(&entry, i->data, i->length); + r = sd_json_variant_new_octescape(&entry, i->data, i->length); if (r < 0) return r; - r = json_variant_append_array(txt, entry); + r = sd_json_variant_append_array(txt, entry); if (r < 0) return r; } @@ -595,10 +596,10 @@ static int append_txt(JsonVariant **txt, DnsResourceRecord *rr) { static int append_srv( DnsQuery *q, DnsResourceRecord *rr, - JsonVariant **array) { + sd_json_variant **array) { _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *normalized = NULL; int r; @@ -654,12 +655,12 @@ static int append_srv( if (r < 0) return r; - r = json_build(&v, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("priority", JSON_BUILD_UNSIGNED(rr->srv.priority)), - JSON_BUILD_PAIR("weight", JSON_BUILD_UNSIGNED(rr->srv.weight)), - JSON_BUILD_PAIR("port", JSON_BUILD_UNSIGNED(rr->srv.port)), - JSON_BUILD_PAIR("hostname", JSON_BUILD_STRING(normalized)))); + r = sd_json_build(&v, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("priority", SD_JSON_BUILD_UNSIGNED(rr->srv.priority)), + SD_JSON_BUILD_PAIR("weight", SD_JSON_BUILD_UNSIGNED(rr->srv.weight)), + SD_JSON_BUILD_PAIR("port", SD_JSON_BUILD_UNSIGNED(rr->srv.port)), + SD_JSON_BUILD_PAIR("hostname", SD_JSON_BUILD_STRING(normalized)))); if (r < 0) return r; @@ -670,13 +671,13 @@ static int append_srv( if (r < 0) return r; - r = json_variant_set_field_string(&v, "canonicalName", normalized); + r = sd_json_variant_set_field_string(&v, "canonicalName", normalized); if (r < 0) return r; } if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) { - _cleanup_(json_variant_unrefp) JsonVariant *addresses = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *addresses = NULL; LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) { DnsQuestion *question; @@ -699,12 +700,12 @@ static int append_srv( return r; } - r = json_variant_set_field(&v, "addresses", addresses); + r = sd_json_variant_set_field(&v, "addresses", addresses); if (r < 0) return r; } - r = json_variant_append_array(array, v); + r = sd_json_variant_append_array(array, v); if (r < 0) return r; @@ -723,7 +724,7 @@ static Varlink *get_vl_link_aux_query(DnsQuery *aux) { static void resolve_service_all_complete(DnsQuery *query) { _cleanup_(dns_query_freep) DnsQuery *q = query; - _cleanup_(json_variant_unrefp) JsonVariant *srv = NULL, *txt = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *srv = NULL, *txt = NULL; _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL; _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL; DnsQuestion *question; @@ -804,7 +805,7 @@ static void resolve_service_all_complete(DnsQuery *query) { canonical = dns_resource_record_ref(rr); } - if (json_variant_is_blank_object(srv)) { + if (sd_json_variant_is_blank_object(srv)) { r = varlink_error(query->varlink_request, "io.systemd.Resolve.NoSuchResourceRecord", NULL); goto finish; } @@ -829,14 +830,14 @@ static void resolve_service_all_complete(DnsQuery *query) { if (r < 0) goto finish; - r = varlink_replyb(query->varlink_request, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("services", JSON_BUILD_VARIANT(srv)), - JSON_BUILD_PAIR_CONDITION(!json_variant_is_blank_object(txt), "txt", JSON_BUILD_VARIANT(txt)), - JSON_BUILD_PAIR("canonical", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("type", JSON_BUILD_STRING(type)), - JSON_BUILD_PAIR("domain", JSON_BUILD_STRING(domain)))), - JSON_BUILD_PAIR("flags", JSON_BUILD_UNSIGNED(dns_query_reply_flags_make(query))))); + r = varlink_replyb(query->varlink_request, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("services", SD_JSON_BUILD_VARIANT(srv)), + SD_JSON_BUILD_PAIR_CONDITION(!sd_json_variant_is_blank_object(txt), "txt", SD_JSON_BUILD_VARIANT(txt)), + SD_JSON_BUILD_PAIR("canonical", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("type", SD_JSON_BUILD_STRING(type)), + SD_JSON_BUILD_PAIR("domain", SD_JSON_BUILD_STRING(domain)))), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_UNSIGNED(dns_query_reply_flags_make(query))))); finish: if (r < 0) { @@ -990,14 +991,14 @@ finish: } } -static int vl_method_resolve_service(Varlink* link, JsonVariant* parameters, VarlinkMethodFlags flags, void* userdata) { - static const JsonDispatch dispatch_table[] = { - { "name", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParametersResolveService, name), 0 }, - { "type", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParametersResolveService, type), 0 }, - { "domain", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParametersResolveService, domain), JSON_MANDATORY }, - { "ifindex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(LookupParametersResolveService, ifindex), 0 }, - { "family", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(LookupParametersResolveService, family), 0 }, - { "flags", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(LookupParametersResolveService, flags), 0 }, +static int vl_method_resolve_service(Varlink* link, sd_json_variant* parameters, VarlinkMethodFlags flags, void* userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParametersResolveService, name), 0 }, + { "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParametersResolveService, type), 0 }, + { "domain", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParametersResolveService, domain), SD_JSON_MANDATORY }, + { "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParametersResolveService, ifindex), 0 }, + { "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParametersResolveService, family), 0 }, + { "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParametersResolveService, flags), 0 }, {} }; @@ -1077,7 +1078,7 @@ static int vl_method_resolve_service(Varlink* link, JsonVariant* parameters, Var } static void vl_method_resolve_record_complete(DnsQuery *query) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; _cleanup_(dns_query_freep) DnsQuery *q = query; DnsQuestion *question; int r; @@ -1108,7 +1109,7 @@ static void vl_method_resolve_record_complete(DnsQuery *query) { int ifindex; DnsResourceRecord *rr; DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = dns_question_matches_rr(question, rr, NULL); if (r < 0) @@ -1124,11 +1125,11 @@ static void vl_method_resolve_record_complete(DnsQuery *query) { if (r < 0) goto finish; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &array, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", JSON_BUILD_INTEGER(ifindex)), - JSON_BUILD_PAIR_CONDITION(v, "rr", JSON_BUILD_VARIANT(v)), - JSON_BUILD_PAIR("raw", JSON_BUILD_BASE64(rr->wire_format, rr->wire_format_size)))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(ifindex)), + SD_JSON_BUILD_PAIR_CONDITION(!!v, "rr", SD_JSON_BUILD_VARIANT(v)), + SD_JSON_BUILD_PAIR("raw", SD_JSON_BUILD_BASE64(rr->wire_format, rr->wire_format_size)))); if (r < 0) goto finish; @@ -1141,9 +1142,9 @@ static void vl_method_resolve_record_complete(DnsQuery *query) { } r = varlink_replyb(q->varlink_request, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("rrs", JSON_BUILD_VARIANT(array)), - JSON_BUILD_PAIR("flags", JSON_BUILD_INTEGER(dns_query_reply_flags_make(q))))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("rrs", SD_JSON_BUILD_VARIANT(array)), + SD_JSON_BUILD_PAIR("flags", SD_JSON_BUILD_INTEGER(dns_query_reply_flags_make(q))))); finish: if (r < 0) { log_full_errno(ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_ERR, r, "Failed to send record reply: %m"); @@ -1151,13 +1152,13 @@ finish: } } -static int vl_method_resolve_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "ifindex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int, offsetof(LookupParameters, ifindex), 0 }, - { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(LookupParameters, name), JSON_MANDATORY }, - { "class", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint16, offsetof(LookupParameters, class), 0 }, - { "type", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint16, offsetof(LookupParameters, type), JSON_MANDATORY }, - { "flags", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, +static int vl_method_resolve_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(LookupParameters, ifindex), 0 }, + { "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(LookupParameters, name), SD_JSON_MANDATORY }, + { "class", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(LookupParameters, class), 0 }, + { "type", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(LookupParameters, type), SD_JSON_MANDATORY }, + { "flags", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(LookupParameters, flags), 0 }, {} }; @@ -1228,7 +1229,7 @@ static int vl_method_resolve_record(Varlink *link, JsonVariant *parameters, Varl return 1; } -static int vl_method_subscribe_query_results(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_subscribe_query_results(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { Manager *m; int r; @@ -1240,13 +1241,13 @@ static int vl_method_subscribe_query_results(Varlink *link, JsonVariant *paramet if (!FLAGS_SET(flags, VARLINK_METHOD_MORE)) return varlink_error(link, VARLINK_ERROR_EXPECTED_MORE, NULL); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); /* Send a ready message to the connecting client, to indicate that we are now listinening, and all * queries issued after the point the client sees this will also be reported to the client. */ r = varlink_notifyb(link, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("ready", JSON_BUILD_BOOLEAN(true)))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("ready", SD_JSON_BUILD_BOOLEAN(true)))); if (r < 0) return log_error_errno(r, "Failed to report monitor to be established: %m"); @@ -1260,42 +1261,42 @@ static int vl_method_subscribe_query_results(Varlink *link, JsonVariant *paramet return 1; } -static int vl_method_dump_cache(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *list = NULL; +static int vl_method_dump_cache(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *list = NULL; Manager *m; int r; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); m = ASSERT_PTR(varlink_server_get_userdata(varlink_get_server(link))); LIST_FOREACH(scopes, s, m->dns_scopes) { - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; r = dns_scope_dump_cache_to_json(s, &j); if (r < 0) return r; - r = json_variant_append_array(&list, j); + r = sd_json_variant_append_array(&list, j); if (r < 0) return r; } if (!list) { - r = json_variant_new_array(&list, NULL, 0); + r = sd_json_variant_new_array(&list, NULL, 0); if (r < 0) return r; } - return varlink_replyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("dump", JSON_BUILD_VARIANT(list)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("dump", SD_JSON_BUILD_VARIANT(list)))); } -static int dns_server_dump_state_to_json_list(DnsServer *server, JsonVariant **list) { - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; +static int dns_server_dump_state_to_json_list(DnsServer *server, sd_json_variant **list) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; int r; assert(list); @@ -1305,18 +1306,18 @@ static int dns_server_dump_state_to_json_list(DnsServer *server, JsonVariant **l if (r < 0) return r; - return json_variant_append_array(list, j); + return sd_json_variant_append_array(list, j); } -static int vl_method_dump_server_state(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *list = NULL; +static int vl_method_dump_server_state(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *list = NULL; Manager *m; int r; Link *l; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); m = ASSERT_PTR(varlink_server_get_userdata(varlink_get_server(link))); @@ -1341,23 +1342,23 @@ static int vl_method_dump_server_state(Varlink *link, JsonVariant *parameters, V } if (!list) { - r = json_variant_new_array(&list, NULL, 0); + r = sd_json_variant_new_array(&list, NULL, 0); if (r < 0) return r; } - return varlink_replyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("dump", JSON_BUILD_VARIANT(list)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("dump", SD_JSON_BUILD_VARIANT(list)))); } -static int vl_method_dump_statistics(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; +static int vl_method_dump_statistics(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; Manager *m; int r; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); m = ASSERT_PTR(varlink_server_get_userdata(varlink_get_server(link))); @@ -1366,22 +1367,22 @@ static int vl_method_dump_statistics(Varlink *link, JsonVariant *parameters, Var if (r < 0) return r; - return varlink_replyb(link, JSON_BUILD_VARIANT(j)); + return varlink_replyb(link, SD_JSON_BUILD_VARIANT(j)); } -static int vl_method_reset_statistics(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_reset_statistics(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { Manager *m; assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); m = ASSERT_PTR(varlink_server_get_userdata(varlink_get_server(link))); dns_manager_reset_statistics(m); - return varlink_replyb(link, JSON_BUILD_EMPTY_OBJECT); + return varlink_replyb(link, SD_JSON_BUILD_EMPTY_OBJECT); } static int varlink_monitor_server_init(Manager *m) { diff --git a/src/resolve/test-resolved-packet.c b/src/resolve/test-resolved-packet.c index 8a65ea0f5e..cccbf54d46 100644 --- a/src/resolve/test-resolved-packet.c +++ b/src/resolve/test-resolved-packet.c @@ -185,7 +185,7 @@ TEST(naptr) { assert_se(dns_packet_extract(p) >= 0); - _cleanup_(json_variant_unrefp) JsonVariant *a = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL; _cleanup_free_ char *joined = NULL; DnsResourceRecord *rr; DNS_ANSWER_FOREACH(rr, p->answer) { @@ -196,18 +196,18 @@ TEST(naptr) { assert_se(strextend(&joined, s, "\n")); - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; assert_se(dns_resource_record_to_json(rr, &v) >= 0); - assert_se(json_variant_append_array(&a, v) >= 0); + assert_se(sd_json_variant_append_array(&a, v) >= 0); } assert(streq(joined, twilio_reply_string)); - _cleanup_(json_variant_unrefp) JsonVariant *parsed = NULL; - assert_se(json_parse(twilio_reply_json, /* flags= */ 0, &parsed, /* ret_line= */ NULL, /* ret_column= */ NULL) >= 0); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parsed = NULL; + assert_se(sd_json_parse(twilio_reply_json, /* flags= */ 0, &parsed, /* ret_line= */ NULL, /* ret_column= */ NULL) >= 0); - assert_se(json_variant_equal(parsed, a)); + assert_se(sd_json_variant_equal(parsed, a)); } DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 4bc3ae73ab..0ae74a3e49 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -1532,18 +1532,18 @@ static int print_cmdline( static int json_addon( BootEntryAddon *addon, const char *addon_str, - JsonVariant **array) { + sd_json_variant **array) { int r; assert(addon); assert(addon_str); - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( array, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR(addon_str, JSON_BUILD_STRING(addon->location)), - JSON_BUILD_PAIR("options", JSON_BUILD_STRING(addon->cmdline)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR(addon_str, SD_JSON_BUILD_STRING(addon->location)), + SD_JSON_BUILD_PAIR("options", SD_JSON_BUILD_STRING(addon->cmdline)))); if (r < 0) return log_oom(); @@ -1554,10 +1554,10 @@ static int json_cmdline( const BootEntry *e, const BootEntryAddons *global_arr, const char *def_cmdline, - JsonVariant **v) { + sd_json_variant **v) { _cleanup_free_ char *combined_cmdline = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *addons_array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *addons_array = NULL; int r; assert(e); @@ -1584,10 +1584,10 @@ static int json_cmdline( return log_oom(); } - r = json_variant_merge_objectb( - v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("addons", JSON_BUILD_VARIANT(addons_array)), - JSON_BUILD_PAIR_CONDITION(combined_cmdline, "cmdline", JSON_BUILD_STRING(combined_cmdline)))); + r = sd_json_variant_merge_objectb( + v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("addons", SD_JSON_BUILD_VARIANT(addons_array)), + SD_JSON_BUILD_PAIR_CONDITION(!!combined_cmdline, "cmdline", SD_JSON_BUILD_STRING(combined_cmdline)))); if (r < 0) return log_oom(); return 0; @@ -1695,8 +1695,8 @@ int show_boot_entry( return -status; } -int boot_entry_to_json(const BootConfig *c, size_t i, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +int boot_entry_to_json(const BootConfig *c, size_t i, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *opts = NULL; const BootEntry *e; int r; @@ -1717,37 +1717,37 @@ int boot_entry_to_json(const BootConfig *c, size_t i, JsonVariant **ret) { return log_oom(); } - r = json_variant_merge_objectb( - &v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("type", JSON_BUILD_STRING(boot_entry_type_json_to_string(e->type))), - JSON_BUILD_PAIR_CONDITION(e->id, "id", JSON_BUILD_STRING(e->id)), - JSON_BUILD_PAIR_CONDITION(e->path, "path", JSON_BUILD_STRING(e->path)), - JSON_BUILD_PAIR_CONDITION(e->root, "root", JSON_BUILD_STRING(e->root)), - JSON_BUILD_PAIR_CONDITION(e->title, "title", JSON_BUILD_STRING(e->title)), - JSON_BUILD_PAIR_CONDITION(boot_entry_title(e), "showTitle", JSON_BUILD_STRING(boot_entry_title(e))), - JSON_BUILD_PAIR_CONDITION(e->sort_key, "sortKey", JSON_BUILD_STRING(e->sort_key)), - JSON_BUILD_PAIR_CONDITION(e->version, "version", JSON_BUILD_STRING(e->version)), - JSON_BUILD_PAIR_CONDITION(e->machine_id, "machineId", JSON_BUILD_STRING(e->machine_id)), - JSON_BUILD_PAIR_CONDITION(e->architecture, "architecture", JSON_BUILD_STRING(e->architecture)), - JSON_BUILD_PAIR_CONDITION(opts, "options", JSON_BUILD_STRING(opts)), - JSON_BUILD_PAIR_CONDITION(e->kernel, "linux", JSON_BUILD_STRING(e->kernel)), - JSON_BUILD_PAIR_CONDITION(e->efi, "efi", JSON_BUILD_STRING(e->efi)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->initrd), "initrd", JSON_BUILD_STRV(e->initrd)), - JSON_BUILD_PAIR_CONDITION(e->device_tree, "devicetree", JSON_BUILD_STRING(e->device_tree)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->device_tree_overlay), "devicetreeOverlay", JSON_BUILD_STRV(e->device_tree_overlay)))); + r = sd_json_variant_merge_objectb( + &v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("type", SD_JSON_BUILD_STRING(boot_entry_type_json_to_string(e->type))), + SD_JSON_BUILD_PAIR_CONDITION(!!e->id, "id", SD_JSON_BUILD_STRING(e->id)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->path, "path", SD_JSON_BUILD_STRING(e->path)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->root, "root", SD_JSON_BUILD_STRING(e->root)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->title, "title", SD_JSON_BUILD_STRING(e->title)), + SD_JSON_BUILD_PAIR_CONDITION(!!boot_entry_title(e), "showTitle", SD_JSON_BUILD_STRING(boot_entry_title(e))), + SD_JSON_BUILD_PAIR_CONDITION(!!e->sort_key, "sortKey", SD_JSON_BUILD_STRING(e->sort_key)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->version, "version", SD_JSON_BUILD_STRING(e->version)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->machine_id, "machineId", SD_JSON_BUILD_STRING(e->machine_id)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->architecture, "architecture", SD_JSON_BUILD_STRING(e->architecture)), + SD_JSON_BUILD_PAIR_CONDITION(!!opts, "options", SD_JSON_BUILD_STRING(opts)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->kernel, "linux", SD_JSON_BUILD_STRING(e->kernel)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->efi, "efi", SD_JSON_BUILD_STRING(e->efi)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->initrd), "initrd", SD_JSON_BUILD_STRV(e->initrd)), + SD_JSON_BUILD_PAIR_CONDITION(!!e->device_tree, "devicetree", SD_JSON_BUILD_STRING(e->device_tree)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->device_tree_overlay), "devicetreeOverlay", SD_JSON_BUILD_STRV(e->device_tree_overlay)))); if (r < 0) return log_oom(); /* Sanitizers (only memory sanitizer?) do not like function call with too many * arguments and trigger false positive warnings. Let's not add too many json objects * at once. */ - r = json_variant_merge_objectb( - &v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("isReported", JSON_BUILD_BOOLEAN(e->reported_by_loader)), - JSON_BUILD_PAIR_CONDITION(e->tries_left != UINT_MAX, "triesLeft", JSON_BUILD_UNSIGNED(e->tries_left)), - JSON_BUILD_PAIR_CONDITION(e->tries_done != UINT_MAX, "triesDone", JSON_BUILD_UNSIGNED(e->tries_done)), - JSON_BUILD_PAIR_CONDITION(c->default_entry >= 0, "isDefault", JSON_BUILD_BOOLEAN(i == (size_t) c->default_entry)), - JSON_BUILD_PAIR_CONDITION(c->selected_entry >= 0, "isSelected", JSON_BUILD_BOOLEAN(i == (size_t) c->selected_entry)))); + r = sd_json_variant_merge_objectb( + &v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("isReported", SD_JSON_BUILD_BOOLEAN(e->reported_by_loader)), + SD_JSON_BUILD_PAIR_CONDITION(e->tries_left != UINT_MAX, "triesLeft", SD_JSON_BUILD_UNSIGNED(e->tries_left)), + SD_JSON_BUILD_PAIR_CONDITION(e->tries_done != UINT_MAX, "triesDone", SD_JSON_BUILD_UNSIGNED(e->tries_done)), + SD_JSON_BUILD_PAIR_CONDITION(c->default_entry >= 0, "isDefault", SD_JSON_BUILD_BOOLEAN(i == (size_t) c->default_entry)), + SD_JSON_BUILD_PAIR_CONDITION(c->selected_entry >= 0, "isSelected", SD_JSON_BUILD_BOOLEAN(i == (size_t) c->selected_entry)))); if (r < 0) return log_oom(); @@ -1760,27 +1760,27 @@ int boot_entry_to_json(const BootConfig *c, size_t i, JsonVariant **ret) { return 1; } -int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) { +int show_boot_entries(const BootConfig *config, sd_json_format_flags_t json_format) { int r; assert(config); - if (!FLAGS_SET(json_format, JSON_FORMAT_OFF)) { - _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; + if (!FLAGS_SET(json_format, SD_JSON_FORMAT_OFF)) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; for (size_t i = 0; i < config->n_entries; i++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = boot_entry_to_json(config, i, &v); if (r < 0) return log_oom(); - r = json_variant_append_array(&array, v); + r = sd_json_variant_append_array(&array, v); if (r < 0) return log_oom(); } - return json_variant_dump(array, json_format | JSON_FORMAT_EMPTY_ARRAY, NULL, NULL); + return sd_json_variant_dump(array, json_format | SD_JSON_FORMAT_EMPTY_ARRAY, NULL, NULL); } else for (size_t n = 0; n < config->n_entries; n++) { r = show_boot_entry( diff --git a/src/shared/bootspec.h b/src/shared/bootspec.h index 1885a88a50..0be254e106 100644 --- a/src/shared/bootspec.h +++ b/src/shared/bootspec.h @@ -7,7 +7,8 @@ #include <stdbool.h> #include <sys/types.h> -#include "json.h" +#include "sd-json.h" + #include "set.h" #include "string-util.h" @@ -134,8 +135,8 @@ int show_boot_entry( bool show_reported); int show_boot_entries( const BootConfig *config, - JsonFormatFlags json_format); + sd_json_format_flags_t json_format); int boot_filename_extract_tries(const char *fname, char **ret_stripped, unsigned *ret_tries_left, unsigned *ret_tries_done); -int boot_entry_to_json(const BootConfig *c, size_t i, JsonVariant **ret); +int boot_entry_to_json(const BootConfig *c, size_t i, sd_json_variant **ret); diff --git a/src/shared/bus-polkit.c b/src/shared/bus-polkit.c index 0382d0b798..aefc84a00c 100644 --- a/src/shared/bus-polkit.c +++ b/src/shared/bus-polkit.c @@ -718,7 +718,7 @@ static int bus_message_new_polkit_auth_call_for_varlink( } static bool varlink_allow_interactive_authentication(Varlink *link) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(link); @@ -732,11 +732,11 @@ static bool varlink_allow_interactive_authentication(Varlink *link) { return false; } - JsonVariant *b; - b = json_variant_by_key(v, "allowInteractiveAuthentication"); + sd_json_variant *b; + b = sd_json_variant_by_key(v, "allowInteractiveAuthentication"); if (b) { - if (json_variant_is_boolean(b)) - return json_variant_boolean(b); + if (sd_json_variant_is_boolean(b)) + return sd_json_variant_boolean(b); log_debug("Incoming 'allowInteractiveAuthentication' field is not a boolean, ignoring."); } diff --git a/src/shared/bus-polkit.h b/src/shared/bus-polkit.h index f3741b2822..25616a0a45 100644 --- a/src/shared/bus-polkit.h +++ b/src/shared/bus-polkit.h @@ -25,12 +25,12 @@ static inline int varlink_verify_polkit_async(Varlink *link, sd_bus *bus, const return varlink_verify_polkit_async_full(link, bus, action, details, UID_INVALID, 0, registry); } -/* A JsonDispatch initializer that makes sure the allowInteractiveAuthentication boolean field we want for +/* A sd_json_dispatch_field initializer that makes sure the allowInteractiveAuthentication boolean field we want for * polkit support in Varlink calls is ignored while regular dispatching (and does not result in errors * regarding unexpected fields) */ #define VARLINK_DISPATCH_POLKIT_FIELD { \ .name = "allowInteractiveAuthentication", \ - .type = JSON_VARIANT_BOOLEAN, \ + .type = SD_JSON_VARIANT_BOOLEAN, \ } bool varlink_has_polkit_action(Varlink *link, const char *action, const char **details, Hashmap **registry); diff --git a/src/shared/creds-util.c b/src/shared/creds-util.c index 1d8bd91d8c..986b53bdd6 100644 --- a/src/shared/creds-util.c +++ b/src/shared/creds-util.c @@ -7,6 +7,7 @@ #endif #include "sd-id128.h" +#include "sd-json.h" #include "blockdev-util.h" #include "capability-util.h" @@ -21,6 +22,7 @@ #include "format-util.h" #include "fs-util.h" #include "io-util.h" +#include "json-util.h" #include "memory-util.h" #include "mkdir-label.h" #include "openssl-util.h" @@ -1184,7 +1186,7 @@ int decrypt_credential_and_warn( struct iovec *ret) { _cleanup_(iovec_done_erase) struct iovec host_key = {}, plaintext = {}, tpm2_key = {}; - _cleanup_(json_variant_unrefp) JsonVariant *signature_json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *signature_json = NULL; _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL; struct encrypted_credential_header *h; struct metadata_credential_header *m; @@ -1545,26 +1547,26 @@ int ipc_encrypt_credential(const char *name, usec_t timestamp, usec_t not_after, return log_error_errno(r, "Failed to enable sensitive Varlink input: %m"); /* Create the input data blob object separately, so that we can mark it as sensitive */ - _cleanup_(json_variant_unrefp) JsonVariant *jinput = NULL; - r = json_build(&jinput, JSON_BUILD_IOVEC_BASE64(input)); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *jinput = NULL; + r = sd_json_build(&jinput, JSON_BUILD_IOVEC_BASE64(input)); if (r < 0) return log_error_errno(r, "Failed to create input object: %m"); - json_variant_sensitive(jinput); + sd_json_variant_sensitive(jinput); - _cleanup_(json_variant_unrefp) JsonVariant *reply = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *reply = NULL; const char *error_id = NULL; r = varlink_callb(vl, "io.systemd.Credentials.Encrypt", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(name, "name", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("data", JSON_BUILD_VARIANT(jinput)), - JSON_BUILD_PAIR_CONDITION(timestamp != USEC_INFINITY, "timestamp", JSON_BUILD_UNSIGNED(timestamp)), - JSON_BUILD_PAIR_CONDITION(not_after != USEC_INFINITY, "notAfter", JSON_BUILD_UNSIGNED(not_after)), - JSON_BUILD_PAIR_CONDITION(!FLAGS_SET(flags, CREDENTIAL_ANY_SCOPE), "scope", JSON_BUILD_STRING(uid_is_valid(uid) ? "user" : "system")), - JSON_BUILD_PAIR_CONDITION(uid_is_valid(uid), "uid", JSON_BUILD_UNSIGNED(uid)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(!!name, "name", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("data", SD_JSON_BUILD_VARIANT(jinput)), + SD_JSON_BUILD_PAIR_CONDITION(timestamp != USEC_INFINITY, "timestamp", SD_JSON_BUILD_UNSIGNED(timestamp)), + SD_JSON_BUILD_PAIR_CONDITION(not_after != USEC_INFINITY, "notAfter", SD_JSON_BUILD_UNSIGNED(not_after)), + SD_JSON_BUILD_PAIR_CONDITION(!FLAGS_SET(flags, CREDENTIAL_ANY_SCOPE), "scope", SD_JSON_BUILD_STRING(uid_is_valid(uid) ? "user" : "system")), + SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(uid), "uid", SD_JSON_BUILD_UNSIGNED(uid)))); if (r < 0) return log_error_errno(r, "Failed to call Encrypt() varlink call."); if (!isempty(error_id)) { @@ -1574,13 +1576,13 @@ int ipc_encrypt_credential(const char *name, usec_t timestamp, usec_t not_after, return log_error_errno(varlink_error_to_errno(error_id, reply), "Failed to encrypt: %s", error_id); } - r = json_dispatch( + r = sd_json_dispatch( reply, - (const JsonDispatch[]) { - { "blob", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, PTR_TO_SIZE(ret), JSON_MANDATORY }, + (const sd_json_dispatch_field[]) { + { "blob", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, PTR_TO_SIZE(ret), SD_JSON_MANDATORY }, {}, }, - JSON_LOG|JSON_ALLOW_EXTENSIONS, + SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, /* userdata= */ NULL); if (r < 0) return r; @@ -1605,25 +1607,25 @@ int ipc_decrypt_credential(const char *validate_name, usec_t validate_timestamp, /* Create the input data blob object separately, so that we can mark it as sensitive (it's supposed * to be encrypted, but who knows maybe it uses the NULL cypher). */ - _cleanup_(json_variant_unrefp) JsonVariant *jinput = NULL; - r = json_build(&jinput, JSON_BUILD_IOVEC_BASE64(input)); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *jinput = NULL; + r = sd_json_build(&jinput, JSON_BUILD_IOVEC_BASE64(input)); if (r < 0) return log_error_errno(r, "Failed to create input object: %m"); - json_variant_sensitive(jinput); + sd_json_variant_sensitive(jinput); - _cleanup_(json_variant_unrefp) JsonVariant *reply = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *reply = NULL; const char *error_id = NULL; r = varlink_callb(vl, "io.systemd.Credentials.Decrypt", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(validate_name, "name", JSON_BUILD_STRING(validate_name)), - JSON_BUILD_PAIR("blob", JSON_BUILD_VARIANT(jinput)), - JSON_BUILD_PAIR_CONDITION(validate_timestamp != USEC_INFINITY, "timestamp", JSON_BUILD_UNSIGNED(validate_timestamp)), - JSON_BUILD_PAIR_CONDITION(!FLAGS_SET(flags, CREDENTIAL_ANY_SCOPE), "scope", JSON_BUILD_STRING(uid_is_valid(uid) ? "user" : "system")), - JSON_BUILD_PAIR_CONDITION(uid_is_valid(uid), "uid", JSON_BUILD_UNSIGNED(uid)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(!!validate_name, "name", SD_JSON_BUILD_STRING(validate_name)), + SD_JSON_BUILD_PAIR("blob", SD_JSON_BUILD_VARIANT(jinput)), + SD_JSON_BUILD_PAIR_CONDITION(validate_timestamp != USEC_INFINITY, "timestamp", SD_JSON_BUILD_UNSIGNED(validate_timestamp)), + SD_JSON_BUILD_PAIR_CONDITION(!FLAGS_SET(flags, CREDENTIAL_ANY_SCOPE), "scope", SD_JSON_BUILD_STRING(uid_is_valid(uid) ? "user" : "system")), + SD_JSON_BUILD_PAIR_CONDITION(uid_is_valid(uid), "uid", SD_JSON_BUILD_UNSIGNED(uid)))); if (r < 0) return log_error_errno(r, "Failed to call Decrypt() varlink call."); if (!isempty(error_id)) { @@ -1641,13 +1643,13 @@ int ipc_decrypt_credential(const char *validate_name, usec_t validate_timestamp, return log_error_errno(varlink_error_to_errno(error_id, reply), "Failed to decrypt: %s", error_id); } - r = json_dispatch( + r = sd_json_dispatch( reply, - (const JsonDispatch[]) { - { "data", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, PTR_TO_SIZE(ret), JSON_MANDATORY }, + (const sd_json_dispatch_field[]) { + { "data", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, PTR_TO_SIZE(ret), SD_JSON_MANDATORY }, {}, }, - JSON_LOG|JSON_ALLOW_EXTENSIONS, + SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, /* userdata= */ NULL); if (r < 0) return r; diff --git a/src/shared/cryptsetup-fido2.c b/src/shared/cryptsetup-fido2.c index 5ab5cefe8f..ebb1c65216 100644 --- a/src/shared/cryptsetup-fido2.c +++ b/src/shared/cryptsetup-fido2.c @@ -1,11 +1,12 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "ask-password-api.h" #include "cryptsetup-fido2.h" #include "env-util.h" #include "fileio.h" #include "hexdecoct.h" -#include "json.h" #include "libfido2-util.h" #include "parse-util.h" #include "random-util.h" @@ -158,8 +159,8 @@ int acquire_fido2_key_auto( /* Loads FIDO2 metadata from LUKS2 JSON token headers. */ for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - JsonVariant *w; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + sd_json_variant *w; _cleanup_free_ void *salt = NULL; _cleanup_free_ char *rp = NULL; size_t salt_size = 0; @@ -179,74 +180,74 @@ int acquire_fido2_key_auto( continue; } - w = json_variant_by_key(v, "fido2-credential"); - if (!w || !json_variant_is_string(w)) + w = sd_json_variant_by_key(v, "fido2-credential"); + if (!w || !sd_json_variant_is_string(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "FIDO2 token data lacks 'fido2-credential' field."); - r = unbase64mem(json_variant_string(w), &cid, &cid_size); + r = unbase64mem(sd_json_variant_string(w), &cid, &cid_size); if (r < 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid base64 data in 'fido2-credential' field."); - w = json_variant_by_key(v, "fido2-salt"); - if (!w || !json_variant_is_string(w)) + w = sd_json_variant_by_key(v, "fido2-salt"); + if (!w || !sd_json_variant_is_string(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "FIDO2 token data lacks 'fido2-salt' field."); assert(!salt); assert(salt_size == 0); - r = unbase64mem(json_variant_string(w), &salt, &salt_size); + r = unbase64mem(sd_json_variant_string(w), &salt, &salt_size); if (r < 0) return log_error_errno(r, "Failed to decode base64 encoded salt."); - w = json_variant_by_key(v, "fido2-rp"); + w = sd_json_variant_by_key(v, "fido2-rp"); if (w) { /* The "rp" field is optional. */ - if (!json_variant_is_string(w)) + if (!sd_json_variant_is_string(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "FIDO2 token data's 'fido2-rp' field is not a string."); assert(!rp); - rp = strdup(json_variant_string(w)); + rp = strdup(sd_json_variant_string(w)); if (!rp) return log_oom(); } - w = json_variant_by_key(v, "fido2-clientPin-required"); + w = sd_json_variant_by_key(v, "fido2-clientPin-required"); if (w) { /* The "fido2-clientPin-required" field is optional. */ - if (!json_variant_is_boolean(w)) + if (!sd_json_variant_is_boolean(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "FIDO2 token data's 'fido2-clientPin-required' field is not a boolean."); - SET_FLAG(required, FIDO2ENROLL_PIN, json_variant_boolean(w)); + SET_FLAG(required, FIDO2ENROLL_PIN, sd_json_variant_boolean(w)); } else required |= FIDO2ENROLL_PIN_IF_NEEDED; /* compat with 248, where the field was unset */ - w = json_variant_by_key(v, "fido2-up-required"); + w = sd_json_variant_by_key(v, "fido2-up-required"); if (w) { /* The "fido2-up-required" field is optional. */ - if (!json_variant_is_boolean(w)) + if (!sd_json_variant_is_boolean(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "FIDO2 token data's 'fido2-up-required' field is not a boolean."); - SET_FLAG(required, FIDO2ENROLL_UP, json_variant_boolean(w)); + SET_FLAG(required, FIDO2ENROLL_UP, sd_json_variant_boolean(w)); } else required |= FIDO2ENROLL_UP_IF_NEEDED; /* compat with 248 */ - w = json_variant_by_key(v, "fido2-uv-required"); + w = sd_json_variant_by_key(v, "fido2-uv-required"); if (w) { /* The "fido2-uv-required" field is optional. */ - if (!json_variant_is_boolean(w)) + if (!sd_json_variant_is_boolean(w)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "FIDO2 token data's 'fido2-uv-required' field is not a boolean."); - SET_FLAG(required, FIDO2ENROLL_UV, json_variant_boolean(w)); + SET_FLAG(required, FIDO2ENROLL_UV, sd_json_variant_boolean(w)); } else required |= FIDO2ENROLL_UV_OMIT; /* compat with 248 */ diff --git a/src/shared/cryptsetup-tpm2.c b/src/shared/cryptsetup-tpm2.c index bfd7d3ab09..d029f101ad 100644 --- a/src/shared/cryptsetup-tpm2.c +++ b/src/shared/cryptsetup-tpm2.c @@ -1,12 +1,13 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-json.h" + #include "alloc-util.h" #include "ask-password-api.h" #include "cryptsetup-tpm2.h" #include "env-util.h" #include "fileio.h" #include "hexdecoct.h" -#include "json.h" #include "parse-util.h" #include "random-util.h" #include "sha256.h" @@ -80,7 +81,7 @@ int acquire_tpm2_key( AskPasswordFlags askpw_flags, struct iovec *ret_decrypted_key) { - _cleanup_(json_variant_unrefp) JsonVariant *signature_json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *signature_json = NULL; _cleanup_free_ void *loaded_blob = NULL; _cleanup_free_ char *auto_device = NULL; struct iovec blob; @@ -242,7 +243,7 @@ int find_tpm2_auto_data( for (token = start_token; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { _cleanup_(iovec_done) struct iovec blob = {}, policy_hash = {}, pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {}; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; uint32_t hash_pcr_mask, pubkey_pcr_mask; uint16_t pcr_bank, primary_alg; TPM2Flags flags; diff --git a/src/shared/cryptsetup-util.c b/src/shared/cryptsetup-util.c index d0dd434df8..01f2e203ab 100644 --- a/src/shared/cryptsetup-util.c +++ b/src/shared/cryptsetup-util.c @@ -173,9 +173,9 @@ int cryptsetup_get_token_as_json( struct crypt_device *cd, int idx, const char *verify_type, - JsonVariant **ret) { + sd_json_variant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *text; int r; @@ -197,18 +197,18 @@ int cryptsetup_get_token_as_json( if (r < 0) return r; - r = json_parse(text, 0, &v, NULL, NULL); + r = sd_json_parse(text, 0, &v, NULL, NULL); if (r < 0) return r; if (verify_type) { - JsonVariant *w; + sd_json_variant *w; - w = json_variant_by_key(v, "type"); + w = sd_json_variant_by_key(v, "type"); if (!w) return -EINVAL; - if (!streq_ptr(json_variant_string(w), verify_type)) + if (!streq_ptr(sd_json_variant_string(w), verify_type)) return -EMEDIUMTYPE; } @@ -218,7 +218,7 @@ int cryptsetup_get_token_as_json( return 0; } -int cryptsetup_add_token_json(struct crypt_device *cd, JsonVariant *v) { +int cryptsetup_add_token_json(struct crypt_device *cd, sd_json_variant *v) { _cleanup_free_ char *text = NULL; int r; @@ -226,7 +226,7 @@ int cryptsetup_add_token_json(struct crypt_device *cd, JsonVariant *v) { if (r < 0) return r; - r = json_variant_format(v, 0, &text); + r = sd_json_variant_format(v, 0, &text); if (r < 0) return log_debug_errno(r, "Failed to format token data for LUKS: %m"); @@ -327,27 +327,27 @@ int dlopen_cryptsetup(void) { #endif } -int cryptsetup_get_keyslot_from_token(JsonVariant *v) { +int cryptsetup_get_keyslot_from_token(sd_json_variant *v) { int keyslot, r; - JsonVariant *w; + sd_json_variant *w; /* Parses the "keyslots" field of a LUKS2 token object. The field can be an array, but here we assume * that it contains a single element only, since that's the only way we ever generate it * ourselves. */ - w = json_variant_by_key(v, "keyslots"); + w = sd_json_variant_by_key(v, "keyslots"); if (!w) return -ENOENT; - if (!json_variant_is_array(w) || json_variant_elements(w) != 1) + if (!sd_json_variant_is_array(w) || sd_json_variant_elements(w) != 1) return -EMEDIUMTYPE; - w = json_variant_by_index(w, 0); + w = sd_json_variant_by_index(w, 0); if (!w) return -ENOENT; - if (!json_variant_is_string(w)) + if (!sd_json_variant_is_string(w)) return -EMEDIUMTYPE; - r = safe_atoi(json_variant_string(w), &keyslot); + r = safe_atoi(sd_json_variant_string(w), &keyslot); if (r < 0) return r; if (keyslot < 0) diff --git a/src/shared/cryptsetup-util.h b/src/shared/cryptsetup-util.h index d255e59004..4767e06b5e 100644 --- a/src/shared/cryptsetup-util.h +++ b/src/shared/cryptsetup-util.h @@ -1,10 +1,13 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include "sd-json.h" + #include "alloc-util.h" #include "dlfcn-util.h" -#include "json.h" #include "macro.h" +#include "string-util.h" +#include "strv.h" #if HAVE_LIBCRYPTSETUP #include <libcryptsetup.h> @@ -93,8 +96,8 @@ void cryptsetup_enable_logging(struct crypt_device *cd); int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd); -int cryptsetup_get_token_as_json(struct crypt_device *cd, int idx, const char *verify_type, JsonVariant **ret); -int cryptsetup_add_token_json(struct crypt_device *cd, JsonVariant *v); +int cryptsetup_get_token_as_json(struct crypt_device *cd, int idx, const char *verify_type, sd_json_variant **ret); +int cryptsetup_add_token_json(struct crypt_device *cd, sd_json_variant *v); #else @@ -108,7 +111,7 @@ static inline void sym_crypt_freep(struct crypt_device** cd) {} int dlopen_cryptsetup(void); -int cryptsetup_get_keyslot_from_token(JsonVariant *v); +int cryptsetup_get_keyslot_from_token(sd_json_variant *v); static inline const char *mangle_none(const char *s) { /* A helper that turns cryptsetup/integritysetup/veritysetup "options" strings into NULL if they are effectively empty */ diff --git a/src/shared/discover-image.c b/src/shared/discover-image.c index 1079d28200..4813973d17 100644 --- a/src/shared/discover-image.c +++ b/src/shared/discover-image.c @@ -1644,22 +1644,22 @@ bool image_in_search_path( return false; } -int image_to_json(const struct Image *img, JsonVariant **ret) { +int image_to_json(const struct Image *img, sd_json_variant **ret) { assert(img); - return json_build(ret, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("Type", image_type_to_string(img->type)), - JSON_BUILD_PAIR_STRING("Class", image_class_to_string(img->class)), - JSON_BUILD_PAIR_STRING("Name", img->name), - JSON_BUILD_PAIR_CONDITION(img->path, "Path", JSON_BUILD_STRING(img->path)), - JSON_BUILD_PAIR_BOOLEAN("ReadOnly", img->read_only), - JSON_BUILD_PAIR_CONDITION(img->crtime != 0, "CreationTimestamp", JSON_BUILD_UNSIGNED(img->crtime)), - JSON_BUILD_PAIR_CONDITION(img->mtime != 0, "ModificationTimestamp", JSON_BUILD_UNSIGNED(img->mtime)), - JSON_BUILD_PAIR_CONDITION(img->usage != UINT64_MAX, "Usage", JSON_BUILD_UNSIGNED(img->usage)), - JSON_BUILD_PAIR_CONDITION(img->usage_exclusive != UINT64_MAX, "UsageExclusive", JSON_BUILD_UNSIGNED(img->usage_exclusive)), - JSON_BUILD_PAIR_CONDITION(img->limit != UINT64_MAX, "Limit", JSON_BUILD_UNSIGNED(img->limit)), - JSON_BUILD_PAIR_CONDITION(img->limit_exclusive != UINT64_MAX, "LimitExclusive", JSON_BUILD_UNSIGNED(img->limit_exclusive)))); + return sd_json_build(ret, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("Type", image_type_to_string(img->type)), + SD_JSON_BUILD_PAIR_STRING("Class", image_class_to_string(img->class)), + SD_JSON_BUILD_PAIR_STRING("Name", img->name), + SD_JSON_BUILD_PAIR_CONDITION(!!img->path, "Path", SD_JSON_BUILD_STRING(img->path)), + SD_JSON_BUILD_PAIR_BOOLEAN("ReadOnly", img->read_only), + SD_JSON_BUILD_PAIR_CONDITION(img->crtime != 0, "CreationTimestamp", SD_JSON_BUILD_UNSIGNED(img->crtime)), + SD_JSON_BUILD_PAIR_CONDITION(img->mtime != 0, "ModificationTimestamp", SD_JSON_BUILD_UNSIGNED(img->mtime)), + SD_JSON_BUILD_PAIR_CONDITION(img->usage != UINT64_MAX, "Usage", SD_JSON_BUILD_UNSIGNED(img->usage)), + SD_JSON_BUILD_PAIR_CONDITION(img->usage_exclusive != UINT64_MAX, "UsageExclusive", SD_JSON_BUILD_UNSIGNED(img->usage_exclusive)), + SD_JSON_BUILD_PAIR_CONDITION(img->limit != UINT64_MAX, "Limit", SD_JSON_BUILD_UNSIGNED(img->limit)), + SD_JSON_BUILD_PAIR_CONDITION(img->limit_exclusive != UINT64_MAX, "LimitExclusive", SD_JSON_BUILD_UNSIGNED(img->limit_exclusive)))); } static const char* const image_type_table[_IMAGE_TYPE_MAX] = { diff --git a/src/shared/discover-image.h b/src/shared/discover-image.h index 6491cec886..bd4bb6f034 100644 --- a/src/shared/discover-image.h +++ b/src/shared/discover-image.h @@ -5,10 +5,10 @@ #include <stdint.h> #include "sd-id128.h" +#include "sd-json.h" #include "hashmap.h" #include "image-policy.h" -#include "json.h" #include "lock-util.h" #include "macro.h" #include "os-util.h" @@ -117,7 +117,7 @@ static inline bool IMAGE_IS_HOST(const struct Image *i) { return false; } -int image_to_json(const struct Image *i, JsonVariant **ret); +int image_to_json(const struct Image *i, sd_json_variant **ret); const char *image_root_to_string(ImageClass c) _const_; diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index a9e211f246..fa8965efcb 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -20,6 +20,7 @@ #include "sd-device.h" #include "sd-id128.h" +#include "sd-json.h" #include "architecture.h" #include "ask-password-api.h" @@ -51,6 +52,7 @@ #include "id128-util.h" #include "import-util.h" #include "io-util.h" +#include "json-util.h" #include "missing_mount.h" #include "missing_syscall.h" #include "mkdir-label.h" @@ -3343,12 +3345,12 @@ int dissected_image_load_verity_sig_partition( VeritySettings *verity) { _cleanup_free_ void *root_hash = NULL, *root_hash_sig = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; size_t root_hash_size, root_hash_sig_size; _cleanup_free_ char *buf = NULL; PartitionDesignator d; DissectedPartition *p; - JsonVariant *rh, *sig; + sd_json_variant *rh, *sig; ssize_t n; char *e; int r; @@ -3396,17 +3398,17 @@ int dissected_image_load_verity_sig_partition( } else buf[p->size] = 0; - r = json_parse(buf, 0, &v, NULL, NULL); + r = sd_json_parse(buf, 0, &v, NULL, NULL); if (r < 0) return log_debug_errno(r, "Failed to parse signature JSON data: %m"); - rh = json_variant_by_key(v, "rootHash"); + rh = sd_json_variant_by_key(v, "rootHash"); if (!rh) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature JSON object lacks 'rootHash' field."); - if (!json_variant_is_string(rh)) + if (!sd_json_variant_is_string(rh)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'rootHash' field of signature JSON object is not a string."); - r = unhexmem(json_variant_string(rh), &root_hash, &root_hash_size); + r = unhexmem(sd_json_variant_string(rh), &root_hash, &root_hash_size); if (r < 0) return log_debug_errno(r, "Failed to parse root hash field: %m"); @@ -3421,13 +3423,13 @@ int dissected_image_load_verity_sig_partition( return log_debug_errno(r, "Root hash in signature JSON data (%s) doesn't match configured hash (%s).", strna(a), strna(b)); } - sig = json_variant_by_key(v, "signature"); + sig = sd_json_variant_by_key(v, "signature"); if (!sig) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature JSON object lacks 'signature' field."); - if (!json_variant_is_string(sig)) + if (!sd_json_variant_is_string(sig)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "'signature' field of signature JSON object is not a string."); - r = unbase64mem(json_variant_string(sig), &root_hash_sig, &root_hash_sig_size); + r = unbase64mem(sd_json_variant_string(sig), &root_hash_sig, &root_hash_sig_size); if (r < 0) return log_debug_errno(r, "Failed to parse signature field: %m"); @@ -4223,7 +4225,7 @@ static void partition_fields_done(PartitionFields *f) { } typedef struct ReplyParameters { - JsonVariant *partitions; + sd_json_variant *partitions; char *image_policy; uint64_t image_size; uint32_t sector_size; @@ -4234,7 +4236,7 @@ static void reply_parameters_done(ReplyParameters *p) { assert(p); p->image_policy = mfree(p->image_policy); - p->partitions = json_variant_unref(p->partitions); + p->partitions = sd_json_variant_unref(p->partitions); } #endif @@ -4249,12 +4251,12 @@ int mountfsd_mount_image( #if HAVE_BLKID _cleanup_(reply_parameters_done) ReplyParameters p = {}; - static const JsonDispatch dispatch_table[] = { - { "partitions", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(struct ReplyParameters, partitions), JSON_MANDATORY }, - { "imagePolicy", JSON_VARIANT_STRING, json_dispatch_string, offsetof(struct ReplyParameters, image_policy), 0 }, - { "imageSize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct ReplyParameters, image_size), JSON_MANDATORY }, - { "sectorSize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint32, offsetof(struct ReplyParameters, sector_size), JSON_MANDATORY }, - { "imageUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(struct ReplyParameters, image_uuid), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "partitions", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(struct ReplyParameters, partitions), SD_JSON_MANDATORY }, + { "imagePolicy", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(struct ReplyParameters, image_policy), 0 }, + { "imageSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct ReplyParameters, image_size), SD_JSON_MANDATORY }, + { "sectorSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(struct ReplyParameters, sector_size), SD_JSON_MANDATORY }, + { "imageUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(struct ReplyParameters, image_uuid), 0 }, {} }; @@ -4301,31 +4303,31 @@ int mountfsd_mount_image( return log_error_errno(r, "Failed format image policy to string: %m"); } - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; r = varlink_callb( vl, "io.systemd.MountFileSystem.MountImage", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("imageFileDescriptor", JSON_BUILD_UNSIGNED(0)), - JSON_BUILD_PAIR_CONDITION(userns_fd >= 0, "userNamespaceFileDescriptor", JSON_BUILD_UNSIGNED(1)), - JSON_BUILD_PAIR("readOnly", JSON_BUILD_BOOLEAN(FLAGS_SET(flags, DISSECT_IMAGE_MOUNT_READ_ONLY))), - JSON_BUILD_PAIR("growFileSystems", JSON_BUILD_BOOLEAN(FLAGS_SET(flags, DISSECT_IMAGE_GROWFS))), - JSON_BUILD_PAIR_CONDITION(ps, "imagePolicy", JSON_BUILD_STRING(ps)), - JSON_BUILD_PAIR("allowInteractiveAuthentication", JSON_BUILD_BOOLEAN(FLAGS_SET(flags, DISSECT_IMAGE_ALLOW_INTERACTIVE_AUTH))))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("imageFileDescriptor", SD_JSON_BUILD_UNSIGNED(0)), + SD_JSON_BUILD_PAIR_CONDITION(userns_fd >= 0, "userNamespaceFileDescriptor", SD_JSON_BUILD_UNSIGNED(1)), + SD_JSON_BUILD_PAIR("readOnly", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(flags, DISSECT_IMAGE_MOUNT_READ_ONLY))), + SD_JSON_BUILD_PAIR("growFileSystems", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(flags, DISSECT_IMAGE_GROWFS))), + SD_JSON_BUILD_PAIR_CONDITION(!!ps, "imagePolicy", SD_JSON_BUILD_STRING(ps)), + SD_JSON_BUILD_PAIR("allowInteractiveAuthentication", SD_JSON_BUILD_BOOLEAN(FLAGS_SET(flags, DISSECT_IMAGE_ALLOW_INTERACTIVE_AUTH))))); if (r < 0) return log_error_errno(r, "Failed to call MountImage() varlink call: %m"); if (!isempty(error_id)) return log_error_errno(varlink_error_to_errno(error_id, reply), "Failed to call MountImage() varlink call: %s", error_id); - r = json_dispatch(reply, dispatch_table, JSON_ALLOW_EXTENSIONS, &p); + r = sd_json_dispatch(reply, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p); if (r < 0) return log_error_errno(r, "Failed to parse MountImage() reply: %m"); log_debug("Effective image policy: %s", p.image_policy); - JsonVariant *i; + sd_json_variant *i; JSON_VARIANT_ARRAY_FOREACH(i, p.partitions) { _cleanup_close_ int fsmount_fd = -EBADF; @@ -4337,22 +4339,22 @@ int mountfsd_mount_image( .fsmount_fd_idx = UINT_MAX, }; - static const JsonDispatch partition_dispatch_table[] = { - { "designator", JSON_VARIANT_STRING, dispatch_partition_designator, offsetof(struct PartitionFields, designator), JSON_MANDATORY }, - { "writable", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct PartitionFields, rw), JSON_MANDATORY }, - { "growFileSystem", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct PartitionFields, growfs), JSON_MANDATORY }, - { "partitionNumber", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(struct PartitionFields, partno), 0 }, - { "architecture", JSON_VARIANT_STRING, dispatch_architecture, offsetof(struct PartitionFields, architecture), 0 }, - { "partitionUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(struct PartitionFields, uuid), 0 }, - { "fileSystemType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(struct PartitionFields, fstype), JSON_MANDATORY }, - { "partitionLabel", JSON_VARIANT_STRING, json_dispatch_string, offsetof(struct PartitionFields, label), 0 }, - { "size", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct PartitionFields, size), JSON_MANDATORY }, - { "offset", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct PartitionFields, offset), JSON_MANDATORY }, - { "mountFileDescriptor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint, offsetof(struct PartitionFields, fsmount_fd_idx), JSON_MANDATORY }, + static const sd_json_dispatch_field partition_dispatch_table[] = { + { "designator", SD_JSON_VARIANT_STRING, dispatch_partition_designator, offsetof(struct PartitionFields, designator), SD_JSON_MANDATORY }, + { "writable", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct PartitionFields, rw), SD_JSON_MANDATORY }, + { "growFileSystem", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct PartitionFields, growfs), SD_JSON_MANDATORY }, + { "partitionNumber", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(struct PartitionFields, partno), 0 }, + { "architecture", SD_JSON_VARIANT_STRING, dispatch_architecture, offsetof(struct PartitionFields, architecture), 0 }, + { "partitionUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(struct PartitionFields, uuid), 0 }, + { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(struct PartitionFields, fstype), SD_JSON_MANDATORY }, + { "partitionLabel", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(struct PartitionFields, label), 0 }, + { "size", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct PartitionFields, size), SD_JSON_MANDATORY }, + { "offset", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct PartitionFields, offset), SD_JSON_MANDATORY }, + { "mountFileDescriptor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(struct PartitionFields, fsmount_fd_idx), SD_JSON_MANDATORY }, {} }; - r = json_dispatch(i, partition_dispatch_table, JSON_ALLOW_EXTENSIONS, &pp); + r = sd_json_dispatch(i, partition_dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &pp); if (r < 0) return log_error_errno(r, "Failed to parse partition data: %m"); diff --git a/src/shared/elf-util.c b/src/shared/elf-util.c index 9d1f494671..7826828642 100644 --- a/src/shared/elf-util.c +++ b/src/shared/elf-util.c @@ -165,7 +165,7 @@ typedef struct StackContext { Elf *elf; unsigned n_thread; unsigned n_frame; - JsonVariant **package_metadata; + sd_json_variant **package_metadata; Set **modules; } StackContext; @@ -301,7 +301,7 @@ static char* build_package_reference( strempty(arch)); } -static void report_module_metadata(StackContext *c, const char *name, JsonVariant *metadata) { +static void report_module_metadata(StackContext *c, const char *name, sd_json_variant *metadata) { assert(c); assert(name); @@ -312,11 +312,11 @@ static void report_module_metadata(StackContext *c, const char *name, JsonVarian if (metadata) { const char - *build_id = json_variant_string(json_variant_by_key(metadata, "buildId")), - *type = json_variant_string(json_variant_by_key(metadata, "type")), - *package = json_variant_string(json_variant_by_key(metadata, "name")), - *version = json_variant_string(json_variant_by_key(metadata, "version")), - *arch = json_variant_string(json_variant_by_key(metadata, "architecture")); + *build_id = sd_json_variant_string(sd_json_variant_by_key(metadata, "buildId")), + *type = sd_json_variant_string(sd_json_variant_by_key(metadata, "type")), + *package = sd_json_variant_string(sd_json_variant_by_key(metadata, "name")), + *version = sd_json_variant_string(sd_json_variant_by_key(metadata, "version")), + *arch = sd_json_variant_string(sd_json_variant_by_key(metadata, "architecture")); if (package) { /* Version/architecture is only meaningful with a package name. @@ -332,7 +332,7 @@ static void report_module_metadata(StackContext *c, const char *name, JsonVarian fputs("\n", c->m.f); } -static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *elf, bool *ret_interpreter_found, StackContext *c) { +static int parse_package_metadata(const char *name, sd_json_variant *id_json, Elf *elf, bool *ret_interpreter_found, StackContext *c) { bool interpreter_found = false; size_t n_program_headers; int r; @@ -380,7 +380,7 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e note_offset < data->d_size && (note_offset = sym_gelf_getnote(data, note_offset, ¬e_header, &name_offset, &desc_offset)) > 0;) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; const char *payload = (const char *)data->d_buf + desc_offset; if (note_header.n_namesz == 0 || note_header.n_descsz == 0) @@ -404,7 +404,7 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e payload = payload_0suffixed; } - r = json_parse(payload, 0, &v, NULL, NULL); + r = sd_json_parse(payload, 0, &v, NULL, NULL); if (r < 0) { _cleanup_free_ char *esc = cescape(payload); return log_error_errno(r, "json_parse on \"%s\" failed: %m", strnull(esc)); @@ -413,9 +413,9 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e /* If we have a build-id, merge it in the same JSON object so that it appears all * nicely together in the logs/metadata. */ if (id_json) { - r = json_variant_merge_object(&v, id_json); + r = sd_json_variant_merge_object(&v, id_json); if (r < 0) - return log_error_errno(r, "json_variant_merge of package meta with buildId failed: %m"); + return log_error_errno(r, "sd_json_variant_merge of package meta with buildId failed: %m"); } /* Pretty-print to the buffer, so that the metadata goes as plaintext in the @@ -425,13 +425,13 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e /* Then we build a new object using the module name as the key, and merge it * with the previous parses, so that in the end it all fits together in a single * JSON blob. */ - r = json_build(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR(name, JSON_BUILD_VARIANT(v)))); + r = sd_json_build(&w, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_VARIANT(v)))); if (r < 0) return log_error_errno(r, "Failed to build JSON object: %m"); - r = json_variant_merge_object(c->package_metadata, w); + r = sd_json_variant_merge_object(c->package_metadata, w); if (r < 0) - return log_error_errno(r, "json_variant_merge of package meta with buildId failed: %m"); + return log_error_errno(r, "sd_json_variant_merge of package meta with buildId failed: %m"); /* Finally stash the name, so we avoid double visits. */ r = set_put_strdup(c->modules, name); @@ -453,8 +453,8 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e } /* Get the build-id out of an ELF object or a dwarf core module. */ -static int parse_buildid(Dwfl_Module *mod, Elf *elf, const char *name, StackContext *c, JsonVariant **ret_id_json) { - _cleanup_(json_variant_unrefp) JsonVariant *id_json = NULL; +static int parse_buildid(Dwfl_Module *mod, Elf *elf, const char *name, StackContext *c, sd_json_variant **ret_id_json) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *id_json = NULL; const unsigned char *id; GElf_Addr id_vaddr; ssize_t id_len; @@ -478,7 +478,7 @@ static int parse_buildid(Dwfl_Module *mod, Elf *elf, const char *name, StackCont /* We will later parse package metadata json and pass it to our caller. Prepare the * build-id in json format too, so that it can be appended and parsed cleanly. It * will then be added as metadata to the journal message with the stack trace. */ - r = json_build(&id_json, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("buildId", JSON_BUILD_HEX(id, id_len)))); + r = sd_json_build(&id_json, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("buildId", SD_JSON_BUILD_HEX(id, id_len)))); if (r < 0) return log_error_errno(r, "json_build on buildId failed: %m"); } @@ -490,7 +490,7 @@ static int parse_buildid(Dwfl_Module *mod, Elf *elf, const char *name, StackCont } static int module_callback(Dwfl_Module *mod, void **userdata, const char *name, Dwarf_Addr start, void *arg) { - _cleanup_(json_variant_unrefp) JsonVariant *id_json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *id_json = NULL; StackContext *c = ASSERT_PTR(arg); size_t n_program_headers; GElf_Addr bias; @@ -580,7 +580,7 @@ static int module_callback(Dwfl_Module *mod, void **userdata, const char *name, return DWARF_CB_OK; } -static int parse_core(int fd, const char *executable, char **ret, JsonVariant **ret_package_metadata) { +static int parse_core(int fd, const char *executable, char **ret, sd_json_variant **ret_package_metadata) { const Dwfl_Callbacks callbacks = { .find_elf = sym_dwfl_build_id_find_elf, @@ -588,7 +588,7 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** .find_debuginfo = sym_dwfl_standard_find_debuginfo, }; - _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *package_metadata = NULL; _cleanup_set_free_ Set *modules = NULL; _cleanup_(stack_context_done) StackContext c = { .package_metadata = &package_metadata, @@ -641,8 +641,8 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant ** return 0; } -static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **ret_package_metadata) { - _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL, *elf_metadata = NULL; +static int parse_elf(int fd, const char *executable, char **ret, sd_json_variant **ret_package_metadata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *package_metadata = NULL, *elf_metadata = NULL; _cleanup_set_free_ Set *modules = NULL; _cleanup_(stack_context_done) StackContext c = { .package_metadata = &package_metadata, @@ -681,7 +681,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r elf_type = "coredump"; } else { - _cleanup_(json_variant_unrefp) JsonVariant *id_json = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *id_json = NULL; const char *e = executable ?: "(unnamed)"; bool interpreter_found = false; @@ -695,7 +695,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r /* If we found a build-id and nothing else, return at least that. */ if (!package_metadata && id_json) { - r = json_build(&package_metadata, JSON_BUILD_OBJECT(JSON_BUILD_PAIR(e, JSON_BUILD_VARIANT(id_json)))); + r = sd_json_build(&package_metadata, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR(e, SD_JSON_BUILD_VARIANT(id_json)))); if (r < 0) return log_warning_errno(r, "Failed to build JSON object: %m"); } @@ -708,21 +708,21 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r /* Note that e_type is always DYN for both executables and libraries, so we can't tell them apart from the header, * but we will search for the PT_INTERP section when parsing the metadata. */ - r = json_build(&elf_metadata, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("elfType", JSON_BUILD_STRING(elf_type)))); + r = sd_json_build(&elf_metadata, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("elfType", SD_JSON_BUILD_STRING(elf_type)))); if (r < 0) return log_warning_errno(r, "Failed to build JSON object: %m"); #if HAVE_DWELF_ELF_E_MACHINE_STRING const char *elf_architecture = sym_dwelf_elf_e_machine_string(elf_header.e_machine); if (elf_architecture) { - _cleanup_(json_variant_unrefp) JsonVariant *json_architecture = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *json_architecture = NULL; - r = json_build(&json_architecture, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("elfArchitecture", JSON_BUILD_STRING(elf_architecture)))); + r = sd_json_build(&json_architecture, + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("elfArchitecture", SD_JSON_BUILD_STRING(elf_architecture)))); if (r < 0) return log_warning_errno(r, "Failed to build JSON object: %m"); - r = json_variant_merge_object(&elf_metadata, json_architecture); + r = sd_json_variant_merge_object(&elf_metadata, json_architecture); if (r < 0) return log_warning_errno(r, "Failed to merge JSON objects: %m"); @@ -732,7 +732,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r #endif /* We always at least have the ELF type, so merge that (and possibly the arch). */ - r = json_variant_merge_object(&elf_metadata, package_metadata); + r = sd_json_variant_merge_object(&elf_metadata, package_metadata); if (r < 0) return log_warning_errno(r, "Failed to merge JSON objects: %m"); @@ -748,11 +748,11 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r return 0; } -int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, char **ret, JsonVariant **ret_package_metadata) { +int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, char **ret, sd_json_variant **ret_package_metadata) { _cleanup_close_pair_ int error_pipe[2] = EBADF_PAIR, return_pipe[2] = EBADF_PAIR, json_pipe[2] = EBADF_PAIR; - _cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *package_metadata = NULL; _cleanup_free_ char *buf = NULL; int r; @@ -858,7 +858,7 @@ int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, cha goto child_fail; } - r = json_variant_dump(package_metadata, JSON_FORMAT_FLUSH, json_out, NULL); + r = sd_json_variant_dump(package_metadata, SD_JSON_FORMAT_FLUSH, json_out, NULL); if (r < 0) log_warning_errno(r, "Failed to write JSON package metadata, ignoring: %m"); } @@ -893,7 +893,7 @@ int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, cha if (!json_in) return -errno; - r = json_parse_file(json_in, NULL, 0, &package_metadata, NULL, NULL); + r = sd_json_parse_file(json_in, NULL, 0, &package_metadata, NULL, 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 json metadata, ignoring: %m"); } diff --git a/src/shared/elf-util.h b/src/shared/elf-util.h index b28e64cea6..13f531df9f 100644 --- a/src/shared/elf-util.h +++ b/src/shared/elf-util.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "json.h" +#include "sd-json.h" #if HAVE_ELFUTILS int dlopen_dw(void); @@ -10,9 +10,9 @@ int dlopen_elf(void); /* Parse an ELF object in a forked process, so that errors while iterating over * untrusted and potentially malicious data do not propagate to the main caller's process. * If fork_disable_dump, the child process will not dump core if it crashes. */ -int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, char **ret, JsonVariant **ret_package_metadata); +int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, char **ret, sd_json_variant **ret_package_metadata); #else -static inline int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, char **ret, JsonVariant **ret_package_metadata) { +static inline int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, char **ret, sd_json_variant **ret_package_metadata) { return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "elfutils disabled, parsing ELF objects not supported"); } #endif diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 9146444d9c..14eeb4647f 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -2736,27 +2736,27 @@ const void* table_get_at(Table *t, size_t row, size_t column) { return table_get(t, cell); } -static int table_data_to_json(TableData *d, JsonVariant **ret) { +static int table_data_to_json(TableData *d, sd_json_variant **ret) { switch (d->type) { case TABLE_EMPTY: - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); case TABLE_STRING: case TABLE_PATH: case TABLE_PATH_BASENAME: case TABLE_FIELD: case TABLE_HEADER: - return json_variant_new_string(ret, d->string); + return sd_json_variant_new_string(ret, d->string); case TABLE_STRV: case TABLE_STRV_WRAPPED: - return json_variant_new_array_strv(ret, d->strv); + return sd_json_variant_new_array_strv(ret, d->strv); case TABLE_BOOLEAN_CHECKMARK: case TABLE_BOOLEAN: - return json_variant_new_boolean(ret, d->boolean); + return sd_json_variant_new_boolean(ret, d->boolean); case TABLE_TIMESTAMP: case TABLE_TIMESTAMP_UTC: @@ -2765,116 +2765,116 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) { case TABLE_TIMESTAMP_LEFT: case TABLE_TIMESTAMP_DATE: if (d->timestamp == USEC_INFINITY) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_unsigned(ret, d->timestamp); + return sd_json_variant_new_unsigned(ret, d->timestamp); case TABLE_TIMESPAN: case TABLE_TIMESPAN_MSEC: case TABLE_TIMESPAN_DAY: if (d->timespan == USEC_INFINITY) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_unsigned(ret, d->timespan); + return sd_json_variant_new_unsigned(ret, d->timespan); case TABLE_SIZE: case TABLE_BPS: if (d->size == UINT64_MAX) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_unsigned(ret, d->size); + return sd_json_variant_new_unsigned(ret, d->size); case TABLE_INT: - return json_variant_new_integer(ret, d->int_val); + return sd_json_variant_new_integer(ret, d->int_val); case TABLE_INT8: - return json_variant_new_integer(ret, d->int8); + return sd_json_variant_new_integer(ret, d->int8); case TABLE_INT16: - return json_variant_new_integer(ret, d->int16); + return sd_json_variant_new_integer(ret, d->int16); case TABLE_INT32: - return json_variant_new_integer(ret, d->int32); + return sd_json_variant_new_integer(ret, d->int32); case TABLE_INT64: - return json_variant_new_integer(ret, d->int64); + return sd_json_variant_new_integer(ret, d->int64); case TABLE_UINT: - return json_variant_new_unsigned(ret, d->uint_val); + return sd_json_variant_new_unsigned(ret, d->uint_val); case TABLE_UINT8: - return json_variant_new_unsigned(ret, d->uint8); + return sd_json_variant_new_unsigned(ret, d->uint8); case TABLE_UINT16: - return json_variant_new_unsigned(ret, d->uint16); + return sd_json_variant_new_unsigned(ret, d->uint16); case TABLE_UINT32: case TABLE_UINT32_HEX: - return json_variant_new_unsigned(ret, d->uint32); + return sd_json_variant_new_unsigned(ret, d->uint32); case TABLE_UINT64: case TABLE_UINT64_HEX: - return json_variant_new_unsigned(ret, d->uint64); + return sd_json_variant_new_unsigned(ret, d->uint64); case TABLE_PERCENT: - return json_variant_new_integer(ret, d->percent); + return sd_json_variant_new_integer(ret, d->percent); case TABLE_IFINDEX: if (d->ifindex <= 0) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_integer(ret, d->ifindex); + return sd_json_variant_new_integer(ret, d->ifindex); case TABLE_IN_ADDR: - return json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET)); + return sd_json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET)); case TABLE_IN6_ADDR: - return json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET6)); + return sd_json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET6)); case TABLE_ID128: - return json_variant_new_id128(ret, d->id128); + return sd_json_variant_new_id128(ret, d->id128); case TABLE_UUID: - return json_variant_new_uuid(ret, d->id128); + return sd_json_variant_new_uuid(ret, d->id128); case TABLE_UID: if (!uid_is_valid(d->uid)) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_integer(ret, d->uid); + return sd_json_variant_new_integer(ret, d->uid); case TABLE_GID: if (!gid_is_valid(d->gid)) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_integer(ret, d->gid); + return sd_json_variant_new_integer(ret, d->gid); case TABLE_PID: if (!pid_is_valid(d->pid)) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_integer(ret, d->pid); + return sd_json_variant_new_integer(ret, d->pid); case TABLE_SIGNAL: if (!SIGNAL_VALID(d->int_val)) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_integer(ret, d->int_val); + return sd_json_variant_new_integer(ret, d->int_val); case TABLE_MODE: case TABLE_MODE_INODE_TYPE: if (d->mode == MODE_INVALID) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_variant_new_unsigned(ret, d->mode); + return sd_json_variant_new_unsigned(ret, d->mode); case TABLE_DEVNUM: if (devnum_is_zero(d->devnum)) - return json_variant_new_null(ret); + return sd_json_variant_new_null(ret); - return json_build(ret, JSON_BUILD_ARRAY( - JSON_BUILD_UNSIGNED(major(d->devnum)), - JSON_BUILD_UNSIGNED(minor(d->devnum)))); + return sd_json_build(ret, SD_JSON_BUILD_ARRAY( + SD_JSON_BUILD_UNSIGNED(major(d->devnum)), + SD_JSON_BUILD_UNSIGNED(minor(d->devnum)))); default: return -EINVAL; @@ -2927,8 +2927,8 @@ static const char *table_get_json_field_name(Table *t, size_t idx) { return idx < t->n_json_fields ? t->json_fields[idx] : NULL; } -static int table_to_json_regular(Table *t, JsonVariant **ret) { - JsonVariant **rows = NULL, **elements = NULL; +static int table_to_json_regular(Table *t, sd_json_variant **ret) { + sd_json_variant **rows = NULL, **elements = NULL; _cleanup_free_ size_t *sorted = NULL; size_t n_rows, display_columns; int r; @@ -2962,11 +2962,11 @@ static int table_to_json_regular(Table *t, JsonVariant **ret) { display_columns = t->n_columns; assert(display_columns > 0); - elements = new0(JsonVariant*, display_columns * 2); + elements = new0(sd_json_variant*, display_columns * 2); if (!elements) return -ENOMEM; - CLEANUP_ARRAY(elements, (size_t) { display_columns * 2 }, json_variant_unref_many); + CLEANUP_ARRAY(elements, (size_t) { display_columns * 2 }, sd_json_variant_unref_many); for (size_t j = 0; j < display_columns; j++) { _cleanup_free_ char *mangled = NULL; @@ -2985,16 +2985,16 @@ static int table_to_json_regular(Table *t, JsonVariant **ret) { n = mangled; } - r = json_variant_new_string(elements + j*2, n); + r = sd_json_variant_new_string(elements + j*2, n); if (r < 0) return r; } - rows = new0(JsonVariant*, n_rows-1); + rows = new0(sd_json_variant*, n_rows-1); if (!rows) return -ENOMEM; - CLEANUP_ARRAY(rows, (size_t) { n_rows - 1 }, json_variant_unref_many); + CLEANUP_ARRAY(rows, (size_t) { n_rows - 1 }, sd_json_variant_unref_many); for (size_t i = 1; i < n_rows; i++) { TableData **row; @@ -3011,23 +3011,23 @@ static int table_to_json_regular(Table *t, JsonVariant **ret) { assert_se(d = row[t->display_map ? t->display_map[j] : j]); k = j*2+1; - elements[k] = json_variant_unref(elements[k]); + elements[k] = sd_json_variant_unref(elements[k]); r = table_data_to_json(d, elements + k); if (r < 0) return r; } - r = json_variant_new_object(rows + i - 1, elements, display_columns * 2); + r = sd_json_variant_new_object(rows + i - 1, elements, display_columns * 2); if (r < 0) return r; } - return json_variant_new_array(ret, rows, n_rows - 1); + return sd_json_variant_new_array(ret, rows, n_rows - 1); } -static int table_to_json_vertical(Table *t, JsonVariant **ret) { - JsonVariant **elements = NULL; +static int table_to_json_vertical(Table *t, sd_json_variant **ret) { + sd_json_variant **elements = NULL; size_t n_elements = 0; int r; @@ -3040,11 +3040,11 @@ static int table_to_json_vertical(Table *t, JsonVariant **ret) { /* Ensure we have no incomplete rows */ assert(t->n_cells % t->n_columns == 0); - elements = new0(JsonVariant *, t->n_cells); + elements = new0(sd_json_variant *, t->n_cells); if (!elements) return -ENOMEM; - CLEANUP_ARRAY(elements, n_elements, json_variant_unref_many); + CLEANUP_ARRAY(elements, n_elements, sd_json_variant_unref_many); for (size_t i = t->n_columns; i < t->n_cells; i++) { @@ -3061,7 +3061,7 @@ static int table_to_json_vertical(Table *t, JsonVariant **ret) { n = mangled; } - r = json_variant_new_string(elements + n_elements, n); + r = sd_json_variant_new_string(elements + n_elements, n); } else r = table_data_to_json(t->data[i], elements + n_elements); if (r < 0) @@ -3070,10 +3070,10 @@ static int table_to_json_vertical(Table *t, JsonVariant **ret) { n_elements++; } - return json_variant_new_object(ret, elements, n_elements); + return sd_json_variant_new_object(ret, elements, n_elements); } -int table_to_json(Table *t, JsonVariant **ret) { +int table_to_json(Table *t, sd_json_variant **ret) { assert(t); if (t->vertical) @@ -3082,13 +3082,13 @@ int table_to_json(Table *t, JsonVariant **ret) { return table_to_json_regular(t, ret); } -int table_print_json(Table *t, FILE *f, JsonFormatFlags flags) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +int table_print_json(Table *t, FILE *f, sd_json_format_flags_t flags) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; assert(t); - if (flags & JSON_FORMAT_OFF) /* If JSON output is turned off, use regular output */ + if (flags & SD_JSON_FORMAT_OFF) /* If JSON output is turned off, use regular output */ return table_print(t, f); if (!f) @@ -3098,14 +3098,14 @@ int table_print_json(Table *t, FILE *f, JsonFormatFlags flags) { if (r < 0) return r; - json_variant_dump(v, flags, f, NULL); + sd_json_variant_dump(v, flags, f, NULL); return fflush_and_check(f); } int table_print_with_pager( Table *t, - JsonFormatFlags json_format_flags, + sd_json_format_flags_t json_format_flags, PagerFlags pager_flags, bool show_header) { @@ -3117,7 +3117,7 @@ int table_print_with_pager( /* An all-in-one solution for showing tables, and turning on a pager first. Also optionally suppresses * the table header and logs about any error. */ - if (json_format_flags & (JSON_FORMAT_OFF|JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) pager_open(pager_flags); saved_header = t->header; diff --git a/src/shared/format-table.h b/src/shared/format-table.h index b169eb0ba5..dcd339727f 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -5,7 +5,8 @@ #include <stdio.h> #include <sys/types.h> -#include "json.h" +#include "sd-json.h" + #include "macro.h" #include "pager.h" @@ -159,10 +160,10 @@ TableCell *table_get_cell(Table *t, size_t row, size_t column); const void *table_get(Table *t, TableCell *cell); const void *table_get_at(Table *t, size_t row, size_t column); -int table_to_json(Table *t, JsonVariant **ret); -int table_print_json(Table *t, FILE *f, JsonFormatFlags json_flags); +int table_to_json(Table *t, sd_json_variant **ret); +int table_print_json(Table *t, FILE *f, sd_json_format_flags_t json_flags); -int table_print_with_pager(Table *t, JsonFormatFlags json_format_flags, PagerFlags pager_flags, bool show_header); +int table_print_with_pager(Table *t, sd_json_format_flags_t json_format_flags, PagerFlags pager_flags, bool show_header); int table_set_json_field_name(Table *t, size_t idx, const char *name); diff --git a/src/shared/group-record.c b/src/shared/group-record.c index 6c1e41a1fe..f0ea1781d4 100644 --- a/src/shared/group-record.c +++ b/src/shared/group-record.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "group-record.h" +#include "json-util.h" #include "strv.h" #include "uid-classification.h" #include "user-util.h" @@ -36,73 +37,73 @@ static GroupRecord *group_record_free(GroupRecord *g) { strv_free(g->administrators); strv_free_erase(g->hashed_password); - json_variant_unref(g->json); + sd_json_variant_unref(g->json); return mfree(g); } DEFINE_TRIVIAL_REF_UNREF_FUNC(GroupRecord, group_record, group_record_free); -static int dispatch_privileged(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_privileged(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch privileged_dispatch_table[] = { - { "hashedPassword", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(GroupRecord, hashed_password), JSON_SAFE }, + static const sd_json_dispatch_field privileged_dispatch_table[] = { + { "hashedPassword", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(GroupRecord, hashed_password), SD_JSON_SAFE }, {}, }; - return json_dispatch(variant, privileged_dispatch_table, flags, userdata); + return sd_json_dispatch(variant, privileged_dispatch_table, flags, userdata); } -static int dispatch_binding(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_binding(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch binding_dispatch_table[] = { - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 }, + static const sd_json_dispatch_field binding_dispatch_table[] = { + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 }, {}, }; - JsonVariant *m; + sd_json_variant *m; sd_id128_t mid; int r; if (!variant) return 0; - if (!json_variant_is_object(variant)) + if (!sd_json_variant_is_object(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name)); r = sd_id128_get_machine(&mid); if (r < 0) return json_log(variant, flags, r, "Failed to determine machine ID: %m"); - m = json_variant_by_key(variant, SD_ID128_TO_STRING(mid)); + m = sd_json_variant_by_key(variant, SD_ID128_TO_STRING(mid)); if (!m) return 0; - return json_dispatch(m, binding_dispatch_table, flags, userdata); + return sd_json_dispatch(m, binding_dispatch_table, flags, userdata); } -static int dispatch_per_machine(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_per_machine(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch per_machine_dispatch_table[] = { - { "matchMachineId", _JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, - { "matchHostname", _JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 }, - { "members", JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, members), JSON_RELAX}, - { "administrators", JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, administrators), JSON_RELAX}, + static const sd_json_dispatch_field per_machine_dispatch_table[] = { + { "matchMachineId", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, + { "matchHostname", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 }, + { "members", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, members), SD_JSON_RELAX }, + { "administrators", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, administrators), SD_JSON_RELAX }, {}, }; - JsonVariant *e; + sd_json_variant *e; int r; if (!variant) return 0; - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { - if (!json_variant_is_object(e)) + if (!sd_json_variant_is_object(e)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name)); r = per_machine_match(e, flags); @@ -111,7 +112,7 @@ static int dispatch_per_machine(const char *name, JsonVariant *variant, JsonDisp if (r == 0) continue; - r = json_dispatch(e, per_machine_dispatch_table, flags, userdata); + r = sd_json_dispatch(e, per_machine_dispatch_table, flags, userdata); if (r < 0) return r; } @@ -119,35 +120,35 @@ static int dispatch_per_machine(const char *name, JsonVariant *variant, JsonDisp return 0; } -static int dispatch_status(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_status(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch status_dispatch_table[] = { - { "service", JSON_VARIANT_STRING, json_dispatch_string, offsetof(GroupRecord, service), JSON_SAFE }, + static const sd_json_dispatch_field status_dispatch_table[] = { + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(GroupRecord, service), SD_JSON_SAFE }, {}, }; - JsonVariant *m; + sd_json_variant *m; sd_id128_t mid; int r; if (!variant) return 0; - if (!json_variant_is_object(variant)) + if (!sd_json_variant_is_object(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name)); r = sd_id128_get_machine(&mid); if (r < 0) return json_log(variant, flags, r, "Failed to determine machine ID: %m"); - m = json_variant_by_key(variant, SD_ID128_TO_STRING(mid)); + m = sd_json_variant_by_key(variant, SD_ID128_TO_STRING(mid)); if (!m) return 0; - return json_dispatch(m, status_dispatch_table, flags, userdata); + return sd_json_dispatch(m, status_dispatch_table, flags, userdata); } -static int group_record_augment(GroupRecord *h, JsonDispatchFlags json_flags) { +static int group_record_augment(GroupRecord *h, sd_json_dispatch_flags_t json_flags) { assert(h); if (!FLAGS_SET(h->mask, USER_RECORD_REGULAR)) @@ -166,36 +167,36 @@ static int group_record_augment(GroupRecord *h, JsonDispatchFlags json_flags) { int group_record_load( GroupRecord *h, - JsonVariant *v, + sd_json_variant *v, UserRecordLoadFlags load_flags) { - static const JsonDispatch group_dispatch_table[] = { - { "groupName", JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(GroupRecord, group_name), JSON_RELAX}, - { "realm", JSON_VARIANT_STRING, json_dispatch_realm, offsetof(GroupRecord, realm), 0 }, - { "description", JSON_VARIANT_STRING, json_dispatch_gecos, offsetof(GroupRecord, description), 0 }, - { "disposition", JSON_VARIANT_STRING, json_dispatch_user_disposition, offsetof(GroupRecord, disposition), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_string, offsetof(GroupRecord, service), JSON_SAFE }, - { "lastChangeUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(GroupRecord, last_change_usec), 0 }, - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 }, - { "members", JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, members), JSON_RELAX}, - { "administrators", JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, administrators), JSON_RELAX}, + static const sd_json_dispatch_field group_dispatch_table[] = { + { "groupName", SD_JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(GroupRecord, group_name), SD_JSON_RELAX }, + { "realm", SD_JSON_VARIANT_STRING, json_dispatch_realm, offsetof(GroupRecord, realm), 0 }, + { "description", SD_JSON_VARIANT_STRING, json_dispatch_gecos, offsetof(GroupRecord, description), 0 }, + { "disposition", SD_JSON_VARIANT_STRING, json_dispatch_user_disposition, offsetof(GroupRecord, disposition), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(GroupRecord, service), SD_JSON_SAFE }, + { "lastChangeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(GroupRecord, last_change_usec), 0 }, + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(GroupRecord, gid), 0 }, + { "members", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, members), SD_JSON_RELAX }, + { "administrators", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(GroupRecord, administrators), SD_JSON_RELAX }, - { "privileged", JSON_VARIANT_OBJECT, dispatch_privileged, 0, 0 }, + { "privileged", SD_JSON_VARIANT_OBJECT, dispatch_privileged, 0, 0 }, /* Not defined for now, for groups, but let's at least generate sensible errors about it */ - { "secret", JSON_VARIANT_OBJECT, json_dispatch_unsupported, 0, 0 }, + { "secret", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_unsupported, 0, 0 }, /* Ignore the perMachine, binding and status stuff here, and process it later, so that it overrides whatever is set above */ - { "perMachine", JSON_VARIANT_ARRAY, NULL, 0, 0 }, - { "binding", JSON_VARIANT_OBJECT, NULL, 0, 0 }, - { "status", JSON_VARIANT_OBJECT, NULL, 0, 0 }, + { "perMachine", SD_JSON_VARIANT_ARRAY, NULL, 0, 0 }, + { "binding", SD_JSON_VARIANT_OBJECT, NULL, 0, 0 }, + { "status", SD_JSON_VARIANT_OBJECT, NULL, 0, 0 }, /* Ignore 'signature', we check it with explicit accessors instead */ - { "signature", JSON_VARIANT_ARRAY, NULL, 0, 0 }, + { "signature", SD_JSON_VARIANT_ARRAY, NULL, 0, 0 }, {}, }; - JsonDispatchFlags json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags); + sd_json_dispatch_flags_t json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags); int r; assert(h); @@ -210,22 +211,22 @@ int group_record_load( if (r < 0) return r; - r = json_dispatch(h->json, group_dispatch_table, json_flags | JSON_ALLOW_EXTENSIONS, h); + r = sd_json_dispatch(h->json, group_dispatch_table, json_flags | SD_JSON_ALLOW_EXTENSIONS, h); if (r < 0) return r; /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields, since we want * them to override the global options. Let's process them now. */ - r = dispatch_per_machine("perMachine", json_variant_by_key(h->json, "perMachine"), json_flags, h); + r = dispatch_per_machine("perMachine", sd_json_variant_by_key(h->json, "perMachine"), json_flags, h); if (r < 0) return r; - r = dispatch_binding("binding", json_variant_by_key(h->json, "binding"), json_flags, h); + r = dispatch_binding("binding", sd_json_variant_by_key(h->json, "binding"), json_flags, h); if (r < 0) return r; - r = dispatch_status("status", json_variant_by_key(h->json, "status"), json_flags, h); + r = dispatch_status("status", sd_json_variant_by_key(h->json, "status"), json_flags, h); if (r < 0) return r; @@ -240,7 +241,7 @@ int group_record_load( } int group_record_build(GroupRecord **ret, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(group_record_unrefp) GroupRecord *g = NULL; va_list ap; int r; @@ -248,7 +249,7 @@ int group_record_build(GroupRecord **ret, ...) { assert(ret); va_start(ap, ret); - r = json_buildv(&v, ap); + r = sd_json_buildv(&v, ap); va_end(ap); if (r < 0) diff --git a/src/shared/group-record.h b/src/shared/group-record.h index f81020437c..5b33abac21 100644 --- a/src/shared/group-record.h +++ b/src/shared/group-record.h @@ -1,7 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "json.h" +#include "sd-json.h" + #include "user-record.h" typedef struct GroupRecord { @@ -29,7 +30,7 @@ typedef struct GroupRecord { char **administrators; /* maps to 'struct sgrp' .sg_adm field */ char **hashed_password; /* maps to 'struct sgrp' .sg_passwd field */ - JsonVariant *json; + sd_json_variant *json; } GroupRecord; GroupRecord* group_record_new(void); @@ -38,7 +39,7 @@ GroupRecord* group_record_unref(GroupRecord *g); DEFINE_TRIVIAL_CLEANUP_FUNC(GroupRecord*, group_record_unref); -int group_record_load(GroupRecord *h, JsonVariant *v, UserRecordLoadFlags flags); +int group_record_load(GroupRecord *h, sd_json_variant *v, UserRecordLoadFlags flags); int group_record_build(GroupRecord **ret, ...); int group_record_clone(GroupRecord *g, UserRecordLoadFlags flags, GroupRecord **ret); diff --git a/src/shared/json.h b/src/shared/json.h deleted file mode 100644 index ba4470d2e7..0000000000 --- a/src/shared/json.h +++ /dev/null @@ -1,515 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -#pragma once - -#include <fcntl.h> -#include <stdbool.h> -#include <stddef.h> -#include <stdint.h> -#include <stdio.h> - -#include "sd-id128.h" - -#include "ether-addr-util.h" -#include "in-addr-util.h" -#include "log.h" -#include "macro.h" -#include "string-util.h" -#include "strv.h" -#include "time-util.h" - -/* - In case you wonder why we have our own JSON implementation, here are a couple of reasons why this implementation has - benefits over various other implementations: - - - We need support for 64-bit signed and unsigned integers, i.e. the full 64,5bit range of -9223372036854775808…18446744073709551615 - - All our variants are immutable after creation - - Special values such as true, false, zero, null, empty strings, empty array, empty objects require zero dynamic memory - - Progressive parsing - - Our integer/real type implicitly converts, but only if that's safe and loss-lessly possible - - There's a "builder" for putting together objects easily in varargs function calls - - There's a "dispatcher" for mapping objects to C data structures - - Every variant optionally carries parsing location information, which simplifies debugging and parse log error generation - - Formatter has color, line, column support - - Limitations: - - Doesn't allow embedded NUL in strings - - Can't store integers outside of the -9223372036854775808…18446744073709551615 range (it will use 'double' for - values outside this range, which is lossy) - - Can't store negative zero (will be treated identical to positive zero, and not retained across serialization) - - Can't store non-integer numbers that can't be stored in "double" losslessly - - Allows creation and parsing of objects with duplicate keys. The "dispatcher" will refuse them however. This means - we can parse and pass around such objects, but will carefully refuse them when we convert them into our own data. - - (These limitations should be pretty much in line with those of other JSON implementations, in fact might be less - limiting in most cases even.) -*/ - -typedef struct JsonVariant JsonVariant; - -typedef enum JsonVariantType { - JSON_VARIANT_STRING, - JSON_VARIANT_INTEGER, - JSON_VARIANT_UNSIGNED, - JSON_VARIANT_REAL, - JSON_VARIANT_NUMBER, /* This a pseudo-type: we can never create variants of this type, but we use it as wildcard check for the above three types */ - JSON_VARIANT_BOOLEAN, - JSON_VARIANT_ARRAY, - JSON_VARIANT_OBJECT, - JSON_VARIANT_NULL, - _JSON_VARIANT_TYPE_MAX, - _JSON_VARIANT_TYPE_INVALID = -EINVAL, -} JsonVariantType; - -int json_variant_new_stringn(JsonVariant **ret, const char *s, size_t n); -int json_variant_new_base64(JsonVariant **ret, const void *p, size_t n); -int json_variant_new_base32hex(JsonVariant **ret, const void *p, size_t n); -int json_variant_new_hex(JsonVariant **ret, const void *p, size_t n); -int json_variant_new_octescape(JsonVariant **ret, const void *p, size_t n); -int json_variant_new_integer(JsonVariant **ret, int64_t i); -int json_variant_new_unsigned(JsonVariant **ret, uint64_t u); -int json_variant_new_real(JsonVariant **ret, double d); -int json_variant_new_boolean(JsonVariant **ret, bool b); -int json_variant_new_array(JsonVariant **ret, JsonVariant **array, size_t n); -int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n); -int json_variant_new_array_strv(JsonVariant **ret, char **l); -int json_variant_new_object(JsonVariant **ret, JsonVariant **array, size_t n); -int json_variant_new_null(JsonVariant **ret); -int json_variant_new_id128(JsonVariant **ret, sd_id128_t id); -int json_variant_new_uuid(JsonVariant **ret, sd_id128_t id); - -static inline int json_variant_new_string(JsonVariant **ret, const char *s) { - return json_variant_new_stringn(ret, s, SIZE_MAX); -} - -JsonVariant *json_variant_ref(JsonVariant *v); -JsonVariant *json_variant_unref(JsonVariant *v); -void json_variant_unref_many(JsonVariant **array, size_t n); - -#define JSON_VARIANT_REPLACE(v, q) \ - do { \ - typeof(v)* _v = &(v); \ - typeof(q) _q = (q); \ - json_variant_unref(*_v); \ - *_v = _q; \ - } while(0) - -DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref); - -const char *json_variant_string(JsonVariant *v); -int64_t json_variant_integer(JsonVariant *v); -uint64_t json_variant_unsigned(JsonVariant *v); -double json_variant_real(JsonVariant *v); -bool json_variant_boolean(JsonVariant *v); - -JsonVariantType json_variant_type(JsonVariant *v); -bool json_variant_has_type(JsonVariant *v, JsonVariantType type); - -static inline bool json_variant_is_string(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_STRING); -} - -static inline bool json_variant_is_integer(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_INTEGER); -} - -static inline bool json_variant_is_unsigned(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_UNSIGNED); -} - -static inline bool json_variant_is_real(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_REAL); -} - -static inline bool json_variant_is_number(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_NUMBER); -} - -static inline bool json_variant_is_boolean(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_BOOLEAN); -} - -static inline bool json_variant_is_array(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_ARRAY); -} - -static inline bool json_variant_is_object(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_OBJECT); -} - -static inline bool json_variant_is_null(JsonVariant *v) { - return json_variant_has_type(v, JSON_VARIANT_NULL); -} - -bool json_variant_is_negative(JsonVariant *v); -bool json_variant_is_blank_object(JsonVariant *v); -bool json_variant_is_blank_array(JsonVariant *v); -bool json_variant_is_normalized(JsonVariant *v); -bool json_variant_is_sorted(JsonVariant *v); - -size_t json_variant_elements(JsonVariant *v); -JsonVariant *json_variant_by_index(JsonVariant *v, size_t index); -JsonVariant *json_variant_by_key(JsonVariant *v, const char *key); -JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVariant **ret_key); - -bool json_variant_equal(JsonVariant *a, JsonVariant *b); - -void json_variant_sensitive(JsonVariant *v); -bool json_variant_is_sensitive(JsonVariant *v); -bool json_variant_is_sensitive_recursive(JsonVariant *v); - -struct json_variant_foreach_state { - JsonVariant *variant; - size_t idx; -}; - -#define _JSON_VARIANT_ARRAY_FOREACH(i, v, state) \ - for (struct json_variant_foreach_state state = { (v), 0 }; \ - json_variant_is_array(state.variant) && \ - state.idx < json_variant_elements(state.variant) && \ - ({ i = json_variant_by_index(state.variant, state.idx); \ - true; }); \ - state.idx++) -#define JSON_VARIANT_ARRAY_FOREACH(i, v) \ - _JSON_VARIANT_ARRAY_FOREACH(i, v, UNIQ_T(state, UNIQ)) - -#define _JSON_VARIANT_OBJECT_FOREACH(k, e, v, state) \ - for (struct json_variant_foreach_state state = { (v), 0 }; \ - json_variant_is_object(state.variant) && \ - state.idx < json_variant_elements(state.variant) && \ - ({ k = json_variant_string(json_variant_by_index(state.variant, state.idx)); \ - e = json_variant_by_index(state.variant, state.idx + 1); \ - true; }); \ - state.idx += 2) -#define JSON_VARIANT_OBJECT_FOREACH(k, e, v) \ - _JSON_VARIANT_OBJECT_FOREACH(k, e, v, UNIQ_T(state, UNIQ)) - -int json_variant_get_source(JsonVariant *v, const char **ret_source, unsigned *ret_line, unsigned *ret_column); - -typedef enum JsonFormatFlags { - JSON_FORMAT_NEWLINE = 1 << 0, /* suffix with newline */ - JSON_FORMAT_PRETTY = 1 << 1, /* add internal whitespace to appeal to human readers */ - JSON_FORMAT_PRETTY_AUTO = 1 << 2, /* same, but only if connected to a tty (and JSON_FORMAT_NEWLINE otherwise) */ - JSON_FORMAT_COLOR = 1 << 3, /* insert ANSI color sequences */ - JSON_FORMAT_COLOR_AUTO = 1 << 4, /* insert ANSI color sequences if colors_enabled() says so */ - JSON_FORMAT_SOURCE = 1 << 5, /* prefix with source filename/line/column */ - JSON_FORMAT_SSE = 1 << 6, /* prefix/suffix with W3C server-sent events */ - JSON_FORMAT_SEQ = 1 << 7, /* prefix/suffix with RFC 7464 application/json-seq */ - JSON_FORMAT_FLUSH = 1 << 8, /* call fflush() after dumping JSON */ - JSON_FORMAT_EMPTY_ARRAY = 1 << 9, /* output "[]" for empty input */ - JSON_FORMAT_OFF = 1 << 10, /* make json_variant_format() fail with -ENOEXEC */ - JSON_FORMAT_CENSOR_SENSITIVE = 1 << 11, /* Replace all sensitive elements with the string "<sensitive data>" */ -} JsonFormatFlags; - -int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret); -int json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const char *prefix); - -int json_variant_filter(JsonVariant **v, char **to_remove); - -int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *value); -int json_variant_set_fieldb(JsonVariant **v, const char *field, ...); -int json_variant_set_field_string(JsonVariant **v, const char *field, const char *value); -int json_variant_set_field_integer(JsonVariant **v, const char *field, int64_t value); -int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uint64_t value); -int json_variant_set_field_boolean(JsonVariant **v, const char *field, bool b); -int json_variant_set_field_strv(JsonVariant **v, const char *field, char **l); - -static inline int json_variant_set_field_non_null(JsonVariant **v, const char *field, JsonVariant *value) { - return value && !json_variant_is_null(value) ? json_variant_set_field(v, field, value) : 0; -} - -JsonVariant *json_variant_find(JsonVariant *haystack, JsonVariant *needle); - -int json_variant_append_array(JsonVariant **v, JsonVariant *element); -int json_variant_append_arrayb(JsonVariant **v, ...); -int json_variant_append_array_nodup(JsonVariant **v, JsonVariant *element); - -int json_variant_merge_object(JsonVariant **v, JsonVariant *m); -int json_variant_merge_objectb(JsonVariant **v, ...); - -int json_variant_strv(JsonVariant *v, char ***ret); - -int json_variant_sort(JsonVariant **v); -int json_variant_normalize(JsonVariant **v); - -typedef enum JsonParseFlags { - JSON_PARSE_SENSITIVE = 1 << 0, /* mark variant as "sensitive", i.e. something containing secret key material or such */ -} JsonParseFlags; - -int json_parse_with_source(const char *string, const char *source, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column); -int json_parse_with_source_continue(const char **p, const char *source, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column); - -static inline int json_parse(const char *string, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { - return json_parse_with_source(string, NULL, flags, ret, ret_line, ret_column); -} -static inline int json_parse_continue(const char **p, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { - return json_parse_with_source_continue(p, NULL, flags, ret, ret_line, ret_column); -} - -int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column); - -static inline int json_parse_file(FILE *f, const char *path, JsonParseFlags flags, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { - return json_parse_file_at(f, AT_FDCWD, path, flags, ret, ret_line, ret_column); -} - -enum { - _JSON_BUILD_STRING, - _JSON_BUILD_INTEGER, - _JSON_BUILD_UNSIGNED, - _JSON_BUILD_REAL, - _JSON_BUILD_BOOLEAN, - _JSON_BUILD_ARRAY_BEGIN, - _JSON_BUILD_ARRAY_END, - _JSON_BUILD_OBJECT_BEGIN, - _JSON_BUILD_OBJECT_END, - _JSON_BUILD_PAIR, - _JSON_BUILD_PAIR_CONDITION, - _JSON_BUILD_NULL, - _JSON_BUILD_VARIANT, - _JSON_BUILD_VARIANT_ARRAY, - _JSON_BUILD_LITERAL, - _JSON_BUILD_STRV, - _JSON_BUILD_STRV_ENV_PAIR, - _JSON_BUILD_BASE64, - _JSON_BUILD_IOVEC_BASE64, - _JSON_BUILD_BASE32HEX, - _JSON_BUILD_HEX, - _JSON_BUILD_IOVEC_HEX, - _JSON_BUILD_OCTESCAPE, - _JSON_BUILD_ID128, - _JSON_BUILD_UUID, - _JSON_BUILD_BYTE_ARRAY, - _JSON_BUILD_HW_ADDR, - _JSON_BUILD_STRING_SET, - _JSON_BUILD_CALLBACK, - _JSON_BUILD_PAIR_UNSIGNED_NON_ZERO, - _JSON_BUILD_PAIR_FINITE_USEC, - _JSON_BUILD_PAIR_STRING_NON_EMPTY, - _JSON_BUILD_PAIR_STRV_NON_EMPTY, - _JSON_BUILD_PAIR_VARIANT_NON_NULL, - _JSON_BUILD_PAIR_VARIANT_ARRAY_NON_EMPTY, - _JSON_BUILD_PAIR_IN4_ADDR_NON_NULL, - _JSON_BUILD_PAIR_IN6_ADDR_NON_NULL, - _JSON_BUILD_PAIR_IN_ADDR_NON_NULL, - _JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL, - _JSON_BUILD_PAIR_HW_ADDR_NON_NULL, - _JSON_BUILD_MAX, -}; - -typedef int (*JsonBuildCallback)(JsonVariant **ret, const char *name, void *userdata); - -#define JSON_BUILD_STRING(s) _JSON_BUILD_STRING, (const char*) { s } -#define JSON_BUILD_INTEGER(i) _JSON_BUILD_INTEGER, (int64_t) { i } -#define JSON_BUILD_UNSIGNED(u) _JSON_BUILD_UNSIGNED, (uint64_t) { u } -#define JSON_BUILD_REAL(d) _JSON_BUILD_REAL, (double) { d } -#define JSON_BUILD_BOOLEAN(b) _JSON_BUILD_BOOLEAN, (bool) { b } -#define JSON_BUILD_ARRAY(...) _JSON_BUILD_ARRAY_BEGIN, __VA_ARGS__, _JSON_BUILD_ARRAY_END -#define JSON_BUILD_EMPTY_ARRAY _JSON_BUILD_ARRAY_BEGIN, _JSON_BUILD_ARRAY_END -#define JSON_BUILD_OBJECT(...) _JSON_BUILD_OBJECT_BEGIN, __VA_ARGS__, _JSON_BUILD_OBJECT_END -#define JSON_BUILD_EMPTY_OBJECT _JSON_BUILD_OBJECT_BEGIN, _JSON_BUILD_OBJECT_END -#define JSON_BUILD_PAIR(n, ...) _JSON_BUILD_PAIR, (const char*) { n }, __VA_ARGS__ -#define JSON_BUILD_PAIR_CONDITION(c, n, ...) _JSON_BUILD_PAIR_CONDITION, (bool) { c }, (const char*) { n }, __VA_ARGS__ -#define JSON_BUILD_NULL _JSON_BUILD_NULL -#define JSON_BUILD_VARIANT(v) _JSON_BUILD_VARIANT, (JsonVariant*) { v } -#define JSON_BUILD_VARIANT_ARRAY(v, n) _JSON_BUILD_VARIANT_ARRAY, (JsonVariant **) { v }, (size_t) { n } -#define JSON_BUILD_LITERAL(l) _JSON_BUILD_LITERAL, (const char*) { l } -#define JSON_BUILD_STRV(l) _JSON_BUILD_STRV, (char**) { l } -#define JSON_BUILD_STRV_ENV_PAIR(l) _JSON_BUILD_STRV_ENV_PAIR, (char**) { l } -#define JSON_BUILD_BASE64(p, n) _JSON_BUILD_BASE64, (const void*) { p }, (size_t) { n } -#define JSON_BUILD_IOVEC_BASE64(iov) _JSON_BUILD_IOVEC_BASE64, (const struct iovec*) { iov } -#define JSON_BUILD_BASE32HEX(p, n) _JSON_BUILD_BASE32HEX, (const void*) { p }, (size_t) { n } -#define JSON_BUILD_HEX(p, n) _JSON_BUILD_HEX, (const void*) { p }, (size_t) { n } -#define JSON_BUILD_IOVEC_HEX(iov) _JSON_BUILD_IOVEC_HEX, (const struct iovec*) { iov } -#define JSON_BUILD_OCTESCAPE(p, n) _JSON_BUILD_OCTESCAPE, (const void*) { p }, (size_t) { n } -#define JSON_BUILD_ID128(id) _JSON_BUILD_ID128, (const sd_id128_t*) { &(id) } -#define JSON_BUILD_UUID(id) _JSON_BUILD_UUID, (const sd_id128_t*) { &(id) } -#define JSON_BUILD_BYTE_ARRAY(v, n) _JSON_BUILD_BYTE_ARRAY, (const void*) { v }, (size_t) { n } -#define JSON_BUILD_CONST_STRING(s) _JSON_BUILD_VARIANT, JSON_VARIANT_STRING_CONST(s) -#define JSON_BUILD_IN4_ADDR(v) JSON_BUILD_BYTE_ARRAY((const struct in_addr*) { v }, sizeof(struct in_addr)) -#define JSON_BUILD_IN6_ADDR(v) JSON_BUILD_BYTE_ARRAY((const struct in6_addr*) { v }, sizeof(struct in6_addr)) -#define JSON_BUILD_IN_ADDR(v, f) JSON_BUILD_BYTE_ARRAY(((const union in_addr_union*) { v })->bytes, FAMILY_ADDRESS_SIZE_SAFE(f)) -#define JSON_BUILD_ETHER_ADDR(v) JSON_BUILD_BYTE_ARRAY(((const struct ether_addr*) { v })->ether_addr_octet, sizeof(struct ether_addr)) -#define JSON_BUILD_HW_ADDR(v) _JSON_BUILD_HW_ADDR, (const struct hw_addr_data*) { v } -#define JSON_BUILD_STRING_SET(s) _JSON_BUILD_STRING_SET, (Set *) { s } -#define JSON_BUILD_CALLBACK(c, u) _JSON_BUILD_CALLBACK, (JsonBuildCallback) { c }, (void*) { u } - -#define JSON_BUILD_PAIR_STRING(name, s) JSON_BUILD_PAIR(name, JSON_BUILD_STRING(s)) -#define JSON_BUILD_PAIR_INTEGER(name, i) JSON_BUILD_PAIR(name, JSON_BUILD_INTEGER(i)) -#define JSON_BUILD_PAIR_UNSIGNED(name, u) JSON_BUILD_PAIR(name, JSON_BUILD_UNSIGNED(u)) -#define JSON_BUILD_PAIR_REAL(name, d) JSON_BUILD_PAIR(name, JSON_BUILD_REAL(d)) -#define JSON_BUILD_PAIR_BOOLEAN(name, b) JSON_BUILD_PAIR(name, JSON_BUILD_BOOLEAN(b)) -#define JSON_BUILD_PAIR_ARRAY(name, ...) JSON_BUILD_PAIR(name, JSON_BUILD_ARRAY(__VA_ARGS__)) -#define JSON_BUILD_PAIR_EMPTY_ARRAY(name) JSON_BUILD_PAIR(name, JSON_BUILD_EMPTY_ARRAY) -#define JSON_BUILD_PAIR_OBJECT(name, ...) JSON_BUILD_PAIR(name, JSON_BUILD_OBJECT(__VA_ARGS__)) -#define JSON_BUILD_PAIR_EMPTY_OBJECT(name) JSON_BUILD_PAIR(name, JSON_BUILD_EMPTY_OBJECT) -#define JSON_BUILD_PAIR_NULL(name) JSON_BUILD_PAIR(name, JSON_BUILD_NULL) -#define JSON_BUILD_PAIR_VARIANT(name, v) JSON_BUILD_PAIR(name, JSON_BUILD_VARIANT(v)) -#define JSON_BUILD_PAIR_VARIANT_ARRAY(name, v, n) JSON_BUILD_PAIR(name, JSON_BUILD_VARIANT_ARRAY(v, n)) -#define JSON_BUILD_PAIR_LITERAL(name, l) JSON_BUILD_PAIR(name, JSON_BUILD_LITERAL(l)) -#define JSON_BUILD_PAIR_STRV(name, l) JSON_BUILD_PAIR(name, JSON_BUILD_STRV(l)) -#define JSON_BUILD_PAIR_BASE64(name, p, n) JSON_BUILD_PAIR(name, JSON_BUILD_BASE64(p, n)) -#define JSON_BUILD_PAIR_IOVEC_BASE64(name, iov) JSON_BUILD_PAIR(name, JSON_BUILD_IOVEC_BASE64(iov)) -#define JSON_BUILD_PAIR_HEX(name, p, n) JSON_BUILD_PAIR(name, JSON_BUILD_HEX(p, n)) -#define JSON_BUILD_PAIR_IOVEC_HEX(name, iov) JSON_BUILD_PAIR(name, JSON_BUILD_IOVEC_HEX(iov)) -#define JSON_BUILD_PAIR_ID128(name, id) JSON_BUILD_PAIR(name, JSON_BUILD_ID128(id)) -#define JSON_BUILD_PAIR_UUID(name, id) JSON_BUILD_PAIR(name, JSON_BUILD_UUID(id)) -#define JSON_BUILD_PAIR_BYTE_ARRAY(name, v, n) JSON_BUILD_PAIR(name, JSON_BUILD_BYTE_ARRAY(v, n)) -#define JSON_BUILD_PAIR_IN4_ADDR(name, v) JSON_BUILD_PAIR(name, JSON_BUILD_IN4_ADDR(v)) -#define JSON_BUILD_PAIR_IN6_ADDR(name, v) JSON_BUILD_PAIR(name, JSON_BUILD_IN6_ADDR(v)) -#define JSON_BUILD_PAIR_IN_ADDR(name, v, f) JSON_BUILD_PAIR(name, JSON_BUILD_IN_ADDR(v, f)) -#define JSON_BUILD_PAIR_ETHER_ADDR(name, v) JSON_BUILD_PAIR(name, JSON_BUILD_ETHER_ADDR(v)) -#define JSON_BUILD_PAIR_HW_ADDR(name, v) JSON_BUILD_PAIR(name, JSON_BUILD_HW_ADDR(v)) -#define JSON_BUILD_PAIR_STRING_SET(name, s) JSON_BUILD_PAIR(name, JSON_BUILD_STRING_SET(s)) -#define JSON_BUILD_PAIR_CALLBACK(name, c, u) JSON_BUILD_PAIR(name, JSON_BUILD_CALLBACK(c, u)) - -#define JSON_BUILD_PAIR_UNSIGNED_NON_ZERO(name, u) _JSON_BUILD_PAIR_UNSIGNED_NON_ZERO, (const char*) { name }, (uint64_t) { u } -#define JSON_BUILD_PAIR_FINITE_USEC(name, u) _JSON_BUILD_PAIR_FINITE_USEC, (const char*) { name }, (usec_t) { u } -#define JSON_BUILD_PAIR_STRING_NON_EMPTY(name, s) _JSON_BUILD_PAIR_STRING_NON_EMPTY, (const char*) { name }, (const char*) { s } -#define JSON_BUILD_PAIR_STRV_NON_EMPTY(name, l) _JSON_BUILD_PAIR_STRV_NON_EMPTY, (const char*) { name }, (char**) { l } -#define JSON_BUILD_PAIR_VARIANT_NON_NULL(name, v) _JSON_BUILD_PAIR_VARIANT_NON_NULL, (const char*) { name }, (JsonVariant*) { v } -#define JSON_BUILD_PAIR_IN4_ADDR_NON_NULL(name, v) _JSON_BUILD_PAIR_IN4_ADDR_NON_NULL, (const char*) { name }, (const struct in_addr*) { v } -#define JSON_BUILD_PAIR_IN6_ADDR_NON_NULL(name, v) _JSON_BUILD_PAIR_IN6_ADDR_NON_NULL, (const char*) { name }, (const struct in6_addr*) { v } -#define JSON_BUILD_PAIR_IN_ADDR_NON_NULL(name, v, f) _JSON_BUILD_PAIR_IN_ADDR_NON_NULL, (const char*) { name }, (const union in_addr_union*) { v }, (int) { f } -#define JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL(name, v) _JSON_BUILD_PAIR_ETHER_ADDR_NON_NULL, (const char*) { name }, (const struct ether_addr*) { v } -#define JSON_BUILD_PAIR_HW_ADDR_NON_NULL(name, v) _JSON_BUILD_PAIR_HW_ADDR_NON_NULL, (const char*) { name }, (const struct hw_addr_data*) { v } - -int json_build(JsonVariant **ret, ...); -int json_buildv(JsonVariant **ret, va_list ap); - -/* A bitmask of flags used by the dispatch logic. Note that this is a combined bit mask, that is generated from the bit - * mask originally passed into json_dispatch(), the individual bitmask associated with the static JsonDispatch callout - * entry, as well the bitmask specified for json_log() calls */ -typedef enum JsonDispatchFlags { - /* The following three may be set in JsonDispatch's .flags field or the json_dispatch() flags parameter */ - JSON_PERMISSIVE = 1 << 0, /* Shall parsing errors be considered fatal for this field or object? */ - JSON_MANDATORY = 1 << 1, /* Should existence of this property be mandatory? */ - JSON_LOG = 1 << 2, /* Should the parser log about errors? */ - JSON_SAFE = 1 << 3, /* Don't accept "unsafe" strings in json_dispatch_string() + json_dispatch_string() */ - JSON_RELAX = 1 << 4, /* Use relaxed user name checking in json_dispatch_user_group_name */ - JSON_ALLOW_EXTENSIONS = 1 << 5, /* Subset of JSON_PERMISSIVE: allow additional fields, but no other permissive handling */ - - /* The following two may be passed into log_json() in addition to those above */ - JSON_DEBUG = 1 << 6, /* Indicates that this log message is a debug message */ - JSON_WARNING = 1 << 7, /* Indicates that this log message is a warning message */ -} JsonDispatchFlags; - -typedef int (*JsonDispatchCallback)(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); - -typedef struct JsonDispatch { - const char *name; - JsonVariantType type; - JsonDispatchCallback callback; - size_t offset; - JsonDispatchFlags flags; -} JsonDispatch; - -int json_dispatch_full(JsonVariant *v, const JsonDispatch table[], JsonDispatchCallback bad, JsonDispatchFlags flags, void *userdata, const char **reterr_bad_field); - -static inline int json_dispatch(JsonVariant *v, const JsonDispatch table[], JsonDispatchFlags flags, void *userdata) { - return json_dispatch_full(v, table, NULL, flags, userdata, NULL); -} - -int json_dispatch_string(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_const_string(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_strv(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_boolean(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_tristate(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_variant(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_variant_noref(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_int64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_uint64(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_uint32(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_int32(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_uint16(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_int16(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_int8(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_uint8(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_uid_gid(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_user_group_name(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_path(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_id128(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_unsupported(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_unbase64_iovec(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_byte_array_iovec(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_in_addr(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); - -assert_cc(sizeof(uint32_t) == sizeof(unsigned)); -#define json_dispatch_uint json_dispatch_uint32 - -assert_cc(sizeof(int32_t) == sizeof(int)); -#define json_dispatch_int json_dispatch_int32 - -#define JSON_DISPATCH_ENUM_DEFINE(name, type, func) \ - int name(const char *n, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { \ - type *c = ASSERT_PTR(userdata); \ - \ - assert(variant); \ - \ - if (json_variant_is_null(variant)) { \ - *c = (type) -EINVAL; \ - return 0; \ - } \ - \ - if (!json_variant_is_string(variant)) \ - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(n)); \ - \ - type cc = func(json_variant_string(variant)); \ - if (cc < 0) \ - return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Value of JSON field '%s' not recognized.", strna(n)); \ - \ - *c = cc; \ - return 0; \ - } - -static inline int json_dispatch_level(JsonDispatchFlags flags) { - - /* Did the user request no logging? If so, then never log higher than LOG_DEBUG. Also, if this is marked as - * debug message, then also log at debug level. */ - - if (!(flags & JSON_LOG) || - (flags & JSON_DEBUG)) - return LOG_DEBUG; - - /* Are we invoked in permissive mode, or is this explicitly marked as warning message? Then this should be - * printed at LOG_WARNING */ - if (flags & (JSON_PERMISSIVE|JSON_WARNING)) - return LOG_WARNING; - - /* Otherwise it's an error. */ - return LOG_ERR; -} - -int json_log_internal(JsonVariant *variant, int level, int error, const char *file, int line, const char *func, const char *format, ...) _printf_(7, 8); - -#define json_log(variant, flags, error, ...) \ - ({ \ - int _level = json_dispatch_level(flags), _e = (error); \ - (log_get_max_level() >= LOG_PRI(_level)) \ - ? json_log_internal(variant, _level, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \ - : -ERRNO_VALUE(_e); \ - }) - -#define json_log_oom(variant, flags) \ - json_log(variant, flags, SYNTHETIC_ERRNO(ENOMEM), "Out of memory.") - -#define JSON_VARIANT_STRING_CONST(x) _JSON_VARIANT_STRING_CONST(UNIQ, (x)) - -#define _JSON_VARIANT_STRING_CONST(xq, x) \ - ({ \ - _align_(2) static const char UNIQ_T(json_string_const, xq)[] = (x); \ - assert((((uintptr_t) UNIQ_T(json_string_const, xq)) & 1) == 0); \ - (JsonVariant*) ((uintptr_t) UNIQ_T(json_string_const, xq) + 1); \ - }) - -int json_variant_unbase64(JsonVariant *v, void **ret, size_t *ret_size); -int json_variant_unhex(JsonVariant *v, void **ret, size_t *ret_size); - -static inline int json_variant_unbase64_iovec(JsonVariant *v, struct iovec *ret) { - return json_variant_unbase64(v, ret ? &ret->iov_base : NULL, ret ? &ret->iov_len : NULL); -} - -static inline int json_variant_unhex_iovec(JsonVariant *v, struct iovec *ret) { - return json_variant_unhex(v, ret ? &ret->iov_base : NULL, ret ? &ret->iov_len : NULL); -} - -const char *json_variant_type_to_string(JsonVariantType t); -JsonVariantType json_variant_type_from_string(const char *s); diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index c71c868889..e122a4b111 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -10,6 +10,7 @@ #include "sd-id128.h" #include "sd-journal.h" +#include "sd-json.h" #include "alloc-util.h" #include "fd-util.h" @@ -21,7 +22,6 @@ #include "io-util.h" #include "journal-internal.h" #include "journal-util.h" -#include "json.h" #include "locale-util.h" #include "log.h" #include "logs-show.h" @@ -1040,16 +1040,16 @@ void json_escape( } typedef struct JsonData { - JsonVariant* name; - JsonVariant* values; + sd_json_variant* name; + sd_json_variant* values; } JsonData; static JsonData* json_data_free(JsonData *d) { if (!d) return NULL; - json_variant_unref(d->name); - json_variant_unref(d->values); + sd_json_variant_unref(d->name); + sd_json_variant_unref(d->values); return mfree(d); } @@ -1067,7 +1067,7 @@ static int update_json_data( const void *value, size_t size) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; JsonData *d; int r; @@ -1078,17 +1078,17 @@ static int update_json_data( size = strlen(value); if (!(flags & OUTPUT_SHOW_ALL) && strlen(name) + 1 + size >= JSON_THRESHOLD) - r = json_variant_new_null(&v); + r = sd_json_variant_new_null(&v); else if (utf8_is_printable(value, size)) - r = json_variant_new_stringn(&v, value, size); + r = sd_json_variant_new_stringn(&v, value, size); else - r = json_variant_new_array_bytes(&v, value, size); + r = sd_json_variant_new_array_bytes(&v, value, size); if (r < 0) return log_error_errno(r, "Failed to allocate JSON data: %m"); d = hashmap_get(h, name); if (d) { - r = json_variant_append_array(&d->values, v); + r = sd_json_variant_append_array(&d->values, v); if (r < 0) return log_error_errno(r, "Failed to append JSON value into array: %m"); } else { @@ -1098,15 +1098,15 @@ static int update_json_data( if (!e) return log_oom(); - r = json_variant_new_string(&e->name, name); + r = sd_json_variant_new_string(&e->name, name); if (r < 0) return log_error_errno(r, "Failed to allocate JSON name variant: %m"); - r = json_variant_append_array(&e->values, v); + r = sd_json_variant_append_array(&e->values, v); if (r < 0) return log_error_errno(r, "Failed to create JSON value array: %m"); - r = hashmap_put(h, json_variant_string(e->name), e); + r = hashmap_put(h, sd_json_variant_string(e->name), e); if (r < 0) return log_error_errno(r, "Failed to insert JSON data into hashmap: %m"); @@ -1160,12 +1160,12 @@ static int output_json( sd_id128_t *previous_boot_id) { /* unused */ char usecbuf[CONST_MAX(DECIMAL_STR_MAX(usec_t), DECIMAL_STR_MAX(uint64_t))]; - _cleanup_(json_variant_unrefp) JsonVariant *object = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *object = NULL; _cleanup_hashmap_free_ Hashmap *h = NULL; sd_id128_t journal_boot_id, seqnum_id; _cleanup_free_ char *cursor = NULL; usec_t realtime, monotonic; - JsonVariant **array = NULL; + sd_json_variant **array = NULL; JsonData *d; uint64_t seqnum; size_t n = 0; @@ -1241,30 +1241,30 @@ static int output_json( return r; } - array = new(JsonVariant*, hashmap_size(h)*2); + array = new(sd_json_variant*, hashmap_size(h)*2); if (!array) return log_oom(); - CLEANUP_ARRAY(array, n, json_variant_unref_many); + CLEANUP_ARRAY(array, n, sd_json_variant_unref_many); HASHMAP_FOREACH(d, h) { - assert(json_variant_elements(d->values) > 0); + assert(sd_json_variant_elements(d->values) > 0); - array[n++] = json_variant_ref(d->name); + array[n++] = sd_json_variant_ref(d->name); - if (json_variant_elements(d->values) == 1) - array[n++] = json_variant_ref(json_variant_by_index(d->values, 0)); + if (sd_json_variant_elements(d->values) == 1) + array[n++] = sd_json_variant_ref(sd_json_variant_by_index(d->values, 0)); else - array[n++] = json_variant_ref(d->values); + array[n++] = sd_json_variant_ref(d->values); } - r = json_variant_new_object(&object, array, n); + r = sd_json_variant_new_object(&object, array, n); if (r < 0) return log_error_errno(r, "Failed to allocate JSON object: %m"); - return json_variant_dump(object, + return sd_json_variant_dump(object, output_mode_to_json_format_flags(mode) | - (FLAGS_SET(flags, OUTPUT_COLOR) ? JSON_FORMAT_COLOR : 0), + (FLAGS_SET(flags, OUTPUT_COLOR) ? SD_JSON_FORMAT_COLOR : 0), f, NULL); } diff --git a/src/shared/logs-show.h b/src/shared/logs-show.h index 7e7b2af901..f775feb316 100644 --- a/src/shared/logs-show.h +++ b/src/shared/logs-show.h @@ -11,6 +11,7 @@ #include "macro.h" #include "output-mode.h" +#include "set.h" #include "time-util.h" typedef struct BootId { diff --git a/src/shared/meson.build b/src/shared/meson.build index a1c3b421cc..46679c1ada 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -96,7 +96,6 @@ shared_sources = files( 'journal-file-util.c', 'journal-importer.c', 'journal-util.c', - 'json.c', 'kbd-util.c', 'kernel-config.c', 'kernel-image.c', diff --git a/src/shared/nsresource.c b/src/shared/nsresource.c index bc0a352581..d1368c22e3 100644 --- a/src/shared/nsresource.c +++ b/src/shared/nsresource.c @@ -4,6 +4,7 @@ #include "fd-util.h" #include "format-util.h" +#include "json-util.h" #include "missing_sched.h" #include "namespace-util.h" #include "nsresource.h" @@ -74,15 +75,15 @@ int nsresource_allocate_userns(const char *name, uint64_t size) { if (userns_fd_idx < 0) return log_debug_errno(userns_fd_idx, "Failed to push userns fd into varlink connection: %m"); - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; r = varlink_callb(vl, "io.systemd.NamespaceResource.AllocateUserRange", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("size", JSON_BUILD_UNSIGNED(size)), - JSON_BUILD_PAIR("userNamespaceFileDescriptor", JSON_BUILD_UNSIGNED(userns_fd_idx)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("size", SD_JSON_BUILD_UNSIGNED(size)), + SD_JSON_BUILD_PAIR("userNamespaceFileDescriptor", SD_JSON_BUILD_UNSIGNED(userns_fd_idx)))); if (r < 0) return log_debug_errno(r, "Failed to call AllocateUserRange() varlink call: %m"); if (error_id) @@ -128,14 +129,14 @@ int nsresource_register_userns(const char *name, int userns_fd) { if (userns_fd_idx < 0) return log_debug_errno(userns_fd_idx, "Failed to push userns fd into varlink connection: %m"); - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; r = varlink_callb(vl, "io.systemd.NamespaceResource.RegisterUserNamespace", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("name", JSON_BUILD_STRING(name)), - JSON_BUILD_PAIR("userNamespaceFileDescriptor", JSON_BUILD_UNSIGNED(userns_fd_idx)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING(name)), + SD_JSON_BUILD_PAIR("userNamespaceFileDescriptor", SD_JSON_BUILD_UNSIGNED(userns_fd_idx)))); if (r < 0) return log_debug_errno(r, "Failed to call RegisterUserNamespace() varlink call: %m"); if (error_id) @@ -176,14 +177,14 @@ int nsresource_add_mount(int userns_fd, int mount_fd) { if (mount_fd_idx < 0) return log_error_errno(mount_fd_idx, "Failed to push mount fd into varlink connection: %m"); - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; r = varlink_callb(vl, "io.systemd.NamespaceResource.AddMountToUserNamespace", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userNamespaceFileDescriptor", JSON_BUILD_UNSIGNED(userns_fd_idx)), - JSON_BUILD_PAIR("mountFileDescriptor", JSON_BUILD_UNSIGNED(mount_fd_idx)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userNamespaceFileDescriptor", SD_JSON_BUILD_UNSIGNED(userns_fd_idx)), + SD_JSON_BUILD_PAIR("mountFileDescriptor", SD_JSON_BUILD_UNSIGNED(mount_fd_idx)))); if (r < 0) return log_error_errno(r, "Failed to call AddMountToUserNamespace() varlink call: %m"); if (streq_ptr(error_id, "io.systemd.NamespaceResource.UserNamespaceNotRegistered")) { @@ -228,14 +229,14 @@ int nsresource_add_cgroup(int userns_fd, int cgroup_fd) { if (cgroup_fd_idx < 0) return log_debug_errno(userns_fd_idx, "Failed to push cgroup fd into varlink connection: %m"); - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; r = varlink_callb(vl, "io.systemd.NamespaceResource.AddControlGroupToUserNamespace", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userNamespaceFileDescriptor", JSON_BUILD_UNSIGNED(userns_fd_idx)), - JSON_BUILD_PAIR("controlGroupFileDescriptor", JSON_BUILD_UNSIGNED(cgroup_fd_idx)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userNamespaceFileDescriptor", SD_JSON_BUILD_UNSIGNED(userns_fd_idx)), + SD_JSON_BUILD_PAIR("controlGroupFileDescriptor", SD_JSON_BUILD_UNSIGNED(cgroup_fd_idx)))); if (r < 0) return log_debug_errno(r, "Failed to call AddControlGroupToUserNamespace() varlink call: %m"); if (streq_ptr(error_id, "io.systemd.NamespaceResource.UserNamespaceNotRegistered")) { @@ -292,16 +293,16 @@ int nsresource_add_netif( if (netns_fd_idx < 0) return log_debug_errno(netns_fd_idx, "Failed to push netns fd into varlink connection: %m"); - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; r = varlink_callb(vl, "io.systemd.NamespaceResource.AddNetworkToUserNamespace", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userNamespaceFileDescriptor", JSON_BUILD_UNSIGNED(userns_fd_idx)), - JSON_BUILD_PAIR("networkNamespaceFileDescriptor", JSON_BUILD_UNSIGNED(netns_fd_idx)), - JSON_BUILD_PAIR("mode", JSON_BUILD_CONST_STRING("veth")), - JSON_BUILD_PAIR_CONDITION(namespace_ifname, "namespaceInterfaceName", JSON_BUILD_STRING(namespace_ifname)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userNamespaceFileDescriptor", SD_JSON_BUILD_UNSIGNED(userns_fd_idx)), + SD_JSON_BUILD_PAIR("networkNamespaceFileDescriptor", SD_JSON_BUILD_UNSIGNED(netns_fd_idx)), + SD_JSON_BUILD_PAIR("mode", JSON_BUILD_CONST_STRING("veth")), + SD_JSON_BUILD_PAIR_CONDITION(!!namespace_ifname, "namespaceInterfaceName", SD_JSON_BUILD_STRING(namespace_ifname)))); if (r < 0) return log_debug_errno(r, "Failed to call AddNetworkToUserNamespace() varlink call: %m"); if (streq_ptr(error_id, "io.systemd.NamespaceResource.UserNamespaceNotRegistered")) { @@ -312,13 +313,13 @@ int nsresource_add_netif( return log_debug_errno(varlink_error_to_errno(error_id, reply), "Failed to add network to user namespace: %s", error_id); _cleanup_free_ char *host_interface_name = NULL, *namespace_interface_name = NULL; - r = json_dispatch( + r = sd_json_dispatch( reply, - (const JsonDispatch[]) { - { "hostInterfaceName", JSON_VARIANT_STRING, json_dispatch_string, PTR_TO_SIZE(&host_interface_name) }, - { "namespaceInterfaceName", JSON_VARIANT_STRING, json_dispatch_string, PTR_TO_SIZE(&namespace_interface_name) }, + (const sd_json_dispatch_field[]) { + { "hostInterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, PTR_TO_SIZE(&host_interface_name) }, + { "namespaceInterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, PTR_TO_SIZE(&namespace_interface_name) }, }, - JSON_ALLOW_EXTENSIONS, + SD_JSON_ALLOW_EXTENSIONS, /* userdata= */ NULL); if (ret_host_ifname) diff --git a/src/shared/output-mode.c b/src/shared/output-mode.c index 026bf19a59..c8c6a27a59 100644 --- a/src/shared/output-mode.c +++ b/src/shared/output-mode.c @@ -3,21 +3,21 @@ #include "output-mode.h" #include "string-table.h" -JsonFormatFlags output_mode_to_json_format_flags(OutputMode m) { +sd_json_format_flags_t output_mode_to_json_format_flags(OutputMode m) { switch (m) { case OUTPUT_JSON_SSE: - return JSON_FORMAT_SSE; + return SD_JSON_FORMAT_SSE; case OUTPUT_JSON_SEQ: - return JSON_FORMAT_SEQ; + return SD_JSON_FORMAT_SEQ; case OUTPUT_JSON_PRETTY: - return JSON_FORMAT_PRETTY; + return SD_JSON_FORMAT_PRETTY; default: - return JSON_FORMAT_NEWLINE; + return SD_JSON_FORMAT_NEWLINE; } } diff --git a/src/shared/output-mode.h b/src/shared/output-mode.h index 8683f573af..5079b84658 100644 --- a/src/shared/output-mode.h +++ b/src/shared/output-mode.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "json.h" +#include "sd-json.h" #include "macro.h" typedef enum OutputMode { @@ -51,7 +51,7 @@ typedef enum OutputFlags { OUTPUT_CGROUP_ID = 1 << 11, } OutputFlags; -JsonFormatFlags output_mode_to_json_format_flags(OutputMode m); +sd_json_format_flags_t output_mode_to_json_format_flags(OutputMode m); const char* output_mode_to_string(OutputMode m) _const_; OutputMode output_mode_from_string(const char *s) _pure_; diff --git a/src/shared/parse-argument.c b/src/shared/parse-argument.c index 145bd119ac..e6159c1482 100644 --- a/src/shared/parse-argument.c +++ b/src/shared/parse-argument.c @@ -31,16 +31,16 @@ int parse_boolean_argument(const char *optname, const char *s, bool *ret) { } } -int parse_json_argument(const char *s, JsonFormatFlags *ret) { +int parse_json_argument(const char *s, sd_json_format_flags_t *ret) { assert(s); assert(ret); if (streq(s, "pretty")) - *ret = JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR_AUTO; + *ret = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO; else if (streq(s, "short")) - *ret = JSON_FORMAT_NEWLINE; + *ret = SD_JSON_FORMAT_NEWLINE; else if (streq(s, "off")) - *ret = JSON_FORMAT_OFF; + *ret = SD_JSON_FORMAT_OFF; else if (streq(s, "help")) { puts("pretty\n" "short\n" diff --git a/src/shared/parse-argument.h b/src/shared/parse-argument.h index adad65e902..6a1a67aef7 100644 --- a/src/shared/parse-argument.h +++ b/src/shared/parse-argument.h @@ -1,9 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once -#include "json.h" +#include <stdbool.h> + +#include "sd-json.h" int parse_boolean_argument(const char *optname, const char *s, bool *ret); -int parse_json_argument(const char *s, JsonFormatFlags *ret); +int parse_json_argument(const char *s, sd_json_format_flags_t *ret); int parse_path_argument(const char *path, bool suppress_root, char **arg); int parse_signal_argument(const char *s, int *ret); diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 9603f1837e..421bb051f1 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -18,6 +18,7 @@ #include "hmac.h" #include "initrd-util.h" #include "io-util.h" +#include "json-util.h" #include "lock-util.h" #include "log.h" #include "logarithm.h" @@ -3271,7 +3272,7 @@ int tpm2_make_policy_session( } static int find_signature( - JsonVariant *v, + sd_json_variant *v, const TPML_PCR_SELECTION *pcr_selection, const void *fp, size_t fp_size, @@ -3281,14 +3282,14 @@ static int find_signature( size_t *ret_signature_size) { #if HAVE_OPENSSL - JsonVariant *b, *i; + sd_json_variant *b, *i; const char *k; int r; /* Searches for a signature blob in the specified JSON object. Search keys are PCR bank, PCR mask, * public key, and policy digest. */ - if (!json_variant_is_object(v)) + if (!sd_json_variant_is_object(v)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Signature is not a JSON object."); uint16_t pcr_bank = pcr_selection->pcrSelections[0].hash; @@ -3299,25 +3300,25 @@ static int find_signature( return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Don't know PCR bank %" PRIu16, pcr_bank); /* First, find field by bank */ - b = json_variant_by_key(v, k); + b = sd_json_variant_by_key(v, k); if (!b) return log_debug_errno(SYNTHETIC_ERRNO(ENXIO), "Signature lacks data for PCR bank '%s'.", k); - if (!json_variant_is_array(b)) + if (!sd_json_variant_is_array(b)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Bank data is not a JSON array."); /* Now iterate through all signatures known for this bank */ JSON_VARIANT_ARRAY_FOREACH(i, b) { _cleanup_free_ void *fpj_data = NULL, *polj_data = NULL; - JsonVariant *maskj, *fpj, *sigj, *polj; + sd_json_variant *maskj, *fpj, *sigj, *polj; size_t fpj_size, polj_size; uint32_t parsed_mask; - if (!json_variant_is_object(i)) + if (!sd_json_variant_is_object(i)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Bank data element is not a JSON object"); /* Check if the PCR mask matches our expectations */ - maskj = json_variant_by_key(i, "pcrs"); + maskj = sd_json_variant_by_key(i, "pcrs"); if (!maskj) continue; @@ -3329,11 +3330,11 @@ static int find_signature( continue; /* Not for this PCR mask */ /* Then check if this is for the public key we operate with */ - fpj = json_variant_by_key(i, "pkfp"); + fpj = sd_json_variant_by_key(i, "pkfp"); if (!fpj) continue; - r = json_variant_unhex(fpj, &fpj_data, &fpj_size); + r = sd_json_variant_unhex(fpj, &fpj_data, &fpj_size); if (r < 0) return log_debug_errno(r, "Failed to decode fingerprint in JSON data: %m"); @@ -3341,11 +3342,11 @@ static int find_signature( continue; /* Not for this public key */ /* Finally, check if this is for the PCR policy we expect this to be */ - polj = json_variant_by_key(i, "pol"); + polj = sd_json_variant_by_key(i, "pol"); if (!polj) continue; - r = json_variant_unhex(polj, &polj_data, &polj_size); + r = sd_json_variant_unhex(polj, &polj_data, &polj_size); if (r < 0) return log_debug_errno(r, "Failed to decode policy hash JSON data: %m"); @@ -3353,11 +3354,11 @@ static int find_signature( continue; /* This entry matches all our expectations, now return the signature included in it */ - sigj = json_variant_by_key(i, "sig"); + sigj = sd_json_variant_by_key(i, "sig"); if (!sigj) continue; - return json_variant_unbase64(sigj, ret_signature, ret_signature_size); + return sd_json_variant_unbase64(sigj, ret_signature, ret_signature_size); } return log_debug_errno(SYNTHETIC_ERRNO(ENXIO), "Couldn't find signature for this PCR bank, PCR index and public key."); @@ -4052,7 +4053,7 @@ static int tpm2_policy_authorize( const TPM2B_PUBLIC *public, const void *fp, size_t fp_size, - JsonVariant *signature_json, + sd_json_variant *signature_json, TPM2B_DIGEST **ret_policy_digest) { TSS2_RC rc; @@ -4226,7 +4227,7 @@ static int tpm2_build_sealing_policy( const void *fp, size_t fp_size, uint32_t pubkey_pcr_mask, - JsonVariant *signature_json, + sd_json_variant *signature_json, bool use_pin, const Tpm2PCRLockPolicy *pcrlock_policy, TPM2B_DIGEST **ret_policy_digest) { @@ -5590,7 +5591,7 @@ int tpm2_unseal(Tpm2Context *c, uint16_t pcr_bank, const struct iovec *pubkey, uint32_t pubkey_pcr_mask, - JsonVariant *signature, + sd_json_variant *signature, const char *pin, const Tpm2PCRLockPolicy *pcrlock_policy, uint16_t primary_alg, @@ -6282,7 +6283,7 @@ static int tpm2_userspace_log( Tpm2UserspaceEventType event_type, const char *description) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *array = NULL; _cleanup_free_ char *f = NULL; sd_id128_t boot_id; int r; @@ -6325,10 +6326,10 @@ static int tpm2_userspace_log( assert_se(a = tpm2_hash_alg_to_string(values->digests[i].hashAlg)); assert_se(implementation = EVP_get_digestbyname(a)); - r = json_variant_append_arrayb( - &array, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("hashAlg", a), - JSON_BUILD_PAIR("digest", JSON_BUILD_HEX(&values->digests[i].digest, EVP_MD_size(implementation))))); + r = sd_json_variant_append_arrayb( + &array, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("hashAlg", a), + SD_JSON_BUILD_PAIR("digest", SD_JSON_BUILD_HEX(&values->digests[i].digest, EVP_MD_size(implementation))))); if (r < 0) return log_debug_errno(r, "Failed to append digest object to JSON array: %m"); } @@ -6339,19 +6340,19 @@ static int tpm2_userspace_log( if (r < 0) return log_debug_errno(r, "Failed to acquire boot ID: %m"); - r = json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pcr", JSON_BUILD_UNSIGNED(pcr_index)), - JSON_BUILD_PAIR("digests", JSON_BUILD_VARIANT(array)), - JSON_BUILD_PAIR("content_type", JSON_BUILD_STRING("systemd")), - JSON_BUILD_PAIR("content", JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_CONDITION(description, "string", JSON_BUILD_STRING(description)), - JSON_BUILD_PAIR("bootId", JSON_BUILD_ID128(boot_id)), - JSON_BUILD_PAIR("timestamp", JSON_BUILD_UNSIGNED(now(CLOCK_BOOTTIME))), - JSON_BUILD_PAIR_CONDITION(event_type >= 0, "eventType", JSON_BUILD_STRING(tpm2_userspace_event_type_to_string(event_type))))))); + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pcr", SD_JSON_BUILD_UNSIGNED(pcr_index)), + SD_JSON_BUILD_PAIR("digests", SD_JSON_BUILD_VARIANT(array)), + SD_JSON_BUILD_PAIR("content_type", SD_JSON_BUILD_STRING("systemd")), + SD_JSON_BUILD_PAIR("content", SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_CONDITION(!!description, "string", SD_JSON_BUILD_STRING(description)), + SD_JSON_BUILD_PAIR("bootId", SD_JSON_BUILD_ID128(boot_id)), + SD_JSON_BUILD_PAIR("timestamp", SD_JSON_BUILD_UNSIGNED(now(CLOCK_BOOTTIME))), + SD_JSON_BUILD_PAIR_CONDITION(event_type >= 0, "eventType", SD_JSON_BUILD_STRING(tpm2_userspace_event_type_to_string(event_type))))))); if (r < 0) return log_debug_errno(r, "Failed to build log record JSON: %m"); - r = json_variant_format(v, JSON_FORMAT_SEQ, &f); + r = sd_json_variant_format(v, SD_JSON_FORMAT_SEQ, &f); if (r < 0) return log_debug_errno(r, "Failed to format JSON: %m"); @@ -6595,16 +6596,16 @@ bool tpm2_pcr_prediction_equal( int tpm2_pcr_prediction_to_json( const Tpm2PCRPrediction *prediction, uint16_t algorithm, - JsonVariant **ret) { + sd_json_variant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *aj = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *aj = NULL; int r; assert(prediction); assert(ret); for (uint32_t pcr = 0; pcr < TPM2_PCRS_MAX; pcr++) { - _cleanup_(json_variant_unrefp) JsonVariant *vj = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *vj = NULL; Tpm2PCRPredictionResult *banks; if (!FLAGS_SET(prediction->pcrs, UINT32_C(1) << pcr)) @@ -6616,9 +6617,9 @@ int tpm2_pcr_prediction_to_json( if (!hash) continue; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &vj, - JSON_BUILD_HEX(hash->buffer, hash->size)); + SD_JSON_BUILD_HEX(hash->buffer, hash->size)); if (r < 0) return log_error_errno(r, "Failed to append hash variant to JSON array: %m"); } @@ -6626,17 +6627,17 @@ int tpm2_pcr_prediction_to_json( if (!vj) continue; - r = json_variant_append_arrayb( + r = sd_json_variant_append_arrayb( &aj, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_INTEGER("pcr", pcr), - JSON_BUILD_PAIR_VARIANT("values", vj))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_INTEGER("pcr", pcr), + SD_JSON_BUILD_PAIR_VARIANT("values", vj))); if (r < 0) return log_error_errno(r, "Failed to append PCR variants to JSON array: %m"); } if (!aj) { - r = json_variant_new_array(&aj, NULL, 0); + r = sd_json_variant_new_array(&aj, NULL, 0); if (r < 0) return r; } @@ -6648,7 +6649,7 @@ int tpm2_pcr_prediction_to_json( int tpm2_pcr_prediction_from_json( Tpm2PCRPrediction *prediction, uint16_t algorithm, - JsonVariant *aj) { + sd_json_variant *aj) { int r; @@ -6657,36 +6658,36 @@ int tpm2_pcr_prediction_from_json( size_t alg_index = tpm2_hash_algorithm_index(algorithm); assert(alg_index < TPM2_N_HASH_ALGORITHMS); - if (!json_variant_is_array(aj)) + if (!sd_json_variant_is_array(aj)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PCR variant array is not an array."); - JsonVariant *pcr; + sd_json_variant *pcr; JSON_VARIANT_ARRAY_FOREACH(pcr, aj) { - JsonVariant *nr, *values; + sd_json_variant *nr, *values; - nr = json_variant_by_key(pcr, "pcr"); + nr = sd_json_variant_by_key(pcr, "pcr"); if (!nr) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PCR array entry lacks PCR index field"); - if (!json_variant_is_unsigned(nr) || - json_variant_unsigned(nr) >= TPM2_PCRS_MAX) + if (!sd_json_variant_is_unsigned(nr) || + sd_json_variant_unsigned(nr) >= TPM2_PCRS_MAX) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PCR array entry PCR index is not an integer in the range 0…23"); - values = json_variant_by_key(pcr, "values"); + values = sd_json_variant_by_key(pcr, "values"); if (!values) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PCR array entry lacks values field"); - if (!json_variant_is_array(values)) + if (!sd_json_variant_is_array(values)) return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PCR array entry values field is not an array"); - prediction->pcrs |= UINT32_C(1) << json_variant_unsigned(nr); + prediction->pcrs |= UINT32_C(1) << sd_json_variant_unsigned(nr); - JsonVariant *v; + sd_json_variant *v; JSON_VARIANT_ARRAY_FOREACH(v, values) { _cleanup_free_ void *buffer = NULL; size_t size; - r = json_variant_unhex(v, &buffer, &size); + r = sd_json_variant_unhex(v, &buffer, &size); if (r < 0) return log_error_errno(r, "Failed to decode PCR policy array hash value"); @@ -6703,7 +6704,7 @@ int tpm2_pcr_prediction_from_json( memcpy(banks->hash[alg_index].buffer, buffer, size); banks->hash[alg_index].size = size; - r = ordered_set_ensure_put(prediction->results + json_variant_unsigned(nr), &tpm2_pcr_prediction_result_hash_ops, banks); + r = ordered_set_ensure_put(prediction->results + sd_json_variant_unsigned(nr), &tpm2_pcr_prediction_result_hash_ops, banks); if (r == -EEXIST) /* Let's allow duplicates */ continue; if (r < 0) @@ -6945,7 +6946,7 @@ int tpm2_policy_super_pcr( void tpm2_pcrlock_policy_done(Tpm2PCRLockPolicy *data) { assert(data); - data->prediction_json = json_variant_unref(data->prediction_json); + data->prediction_json = sd_json_variant_unref(data->prediction_json); tpm2_pcr_prediction_done(&data->prediction); iovec_done(&data->nv_handle); iovec_done(&data->nv_public); @@ -6954,13 +6955,13 @@ void tpm2_pcrlock_policy_done(Tpm2PCRLockPolicy *data) { iovec_done(&data->pin_private); } -static int json_dispatch_tpm2_algorithm(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_tpm2_algorithm(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uint16_t *algorithm = ASSERT_PTR(userdata); int r; - r = tpm2_hash_alg_from_string(json_variant_string(variant)); + r = tpm2_hash_alg_from_string(sd_json_variant_string(variant)); if (r < 0 || tpm2_hash_algorithm_index(r) == SIZE_MAX) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid hash algorithm: %s", json_variant_string(variant)); + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid hash algorithm: %s", sd_json_variant_string(variant)); *algorithm = r; return 0; @@ -6984,21 +6985,21 @@ int tpm2_pcrlock_search_file(const char *path, FILE **ret_file, char **ret_path) } int tpm2_pcrlock_policy_from_json( - JsonVariant *v, + sd_json_variant *v, Tpm2PCRLockPolicy *ret_policy) { /* We use a type check of _JSON_VARIANT_TYPE_INVALID for the integer fields to allow * json_dispatch_uint32() to parse strings as integers to work around the integer type weakness of * JSON's design. */ - JsonDispatch policy_dispatch[] = { - { "pcrBank", JSON_VARIANT_STRING, json_dispatch_tpm2_algorithm, offsetof(Tpm2PCRLockPolicy, algorithm), JSON_MANDATORY }, - { "pcrValues", JSON_VARIANT_ARRAY, json_dispatch_variant, offsetof(Tpm2PCRLockPolicy, prediction_json), JSON_MANDATORY }, - { "nvIndex", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint32, offsetof(Tpm2PCRLockPolicy, nv_index), JSON_MANDATORY }, - { "nvHandle", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, nv_handle), JSON_MANDATORY }, - { "nvPublic", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, nv_public), JSON_MANDATORY }, - { "srkHandle", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, srk_handle), JSON_MANDATORY }, - { "pinPublic", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, pin_public), JSON_MANDATORY }, - { "pinPrivate", JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, pin_private), JSON_MANDATORY }, + sd_json_dispatch_field policy_dispatch[] = { + { "pcrBank", SD_JSON_VARIANT_STRING, json_dispatch_tpm2_algorithm, offsetof(Tpm2PCRLockPolicy, algorithm), SD_JSON_MANDATORY }, + { "pcrValues", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(Tpm2PCRLockPolicy, prediction_json), SD_JSON_MANDATORY }, + { "nvIndex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(Tpm2PCRLockPolicy, nv_index), SD_JSON_MANDATORY }, + { "nvHandle", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, nv_handle), SD_JSON_MANDATORY }, + { "nvPublic", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, nv_public), SD_JSON_MANDATORY }, + { "srkHandle", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, srk_handle), SD_JSON_MANDATORY }, + { "pinPublic", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, pin_public), SD_JSON_MANDATORY }, + { "pinPrivate", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Tpm2PCRLockPolicy, pin_private), SD_JSON_MANDATORY }, {} }; @@ -7008,7 +7009,7 @@ int tpm2_pcrlock_policy_from_json( assert(v); assert(ret_policy); - r = json_dispatch(v, policy_dispatch, JSON_LOG, &policy); + r = sd_json_dispatch(v, policy_dispatch, SD_JSON_LOG, &policy); if (r < 0) return r; @@ -7036,8 +7037,8 @@ int tpm2_pcrlock_policy_load( if (r < 0) return log_error_errno(r, "Failed to load TPM2 pcrlock policy file: %m"); - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - r = json_parse_file( + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + r = sd_json_parse_file( f, discovered_path, /* flags = */ 0, @@ -7082,8 +7083,8 @@ static int pcrlock_policy_load_credential( if (memchr(decoded.iov_base, 0, decoded.iov_len)) return log_error_errno(r, "Credential '%s' contains embedded NUL byte, refusing.", name); - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - r = json_parse(decoded.iov_base, + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + r = sd_json_parse(decoded.iov_base, /* flags= */ 0, &v, /* ret_line= */ NULL, @@ -7294,48 +7295,48 @@ char *tpm2_pcr_mask_to_string(uint32_t mask) { return TAKE_PTR(s); } -int tpm2_make_pcr_json_array(uint32_t pcr_mask, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *a = NULL; +int tpm2_make_pcr_json_array(uint32_t pcr_mask, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL; int r; assert(ret); for (size_t i = 0; i < TPM2_PCRS_MAX; i++) { - _cleanup_(json_variant_unrefp) JsonVariant *e = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *e = NULL; if ((pcr_mask & (UINT32_C(1) << i)) == 0) continue; - r = json_variant_new_integer(&e, i); + r = sd_json_variant_new_integer(&e, i); if (r < 0) return r; - r = json_variant_append_array(&a, e); + r = sd_json_variant_append_array(&a, e); if (r < 0) return r; } if (!a) - return json_variant_new_array(ret, NULL, 0); + return sd_json_variant_new_array(ret, NULL, 0); *ret = TAKE_PTR(a); return 0; } -int tpm2_parse_pcr_json_array(JsonVariant *v, uint32_t *ret) { - JsonVariant *e; +int tpm2_parse_pcr_json_array(sd_json_variant *v, uint32_t *ret) { + sd_json_variant *e; uint32_t mask = 0; - if (!json_variant_is_array(v)) + if (!sd_json_variant_is_array(v)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 PCR array is not a JSON array."); JSON_VARIANT_ARRAY_FOREACH(e, v) { uint64_t u; - if (!json_variant_is_unsigned(e)) + if (!sd_json_variant_is_unsigned(e)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 PCR is not an unsigned integer."); - u = json_variant_unsigned(e); + u = sd_json_variant_unsigned(e); if (u >= TPM2_PCRS_MAX) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 PCR number out of range: %" PRIu64, u); @@ -7361,9 +7362,9 @@ int tpm2_make_luks2_json( const struct iovec *srk, const struct iovec *pcrlock_nv, TPM2Flags flags, - JsonVariant **ret) { + sd_json_variant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *hmj = NULL, *pkmj = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *hmj = NULL, *pkmj = NULL; _cleanup_free_ char *keyslot_as_string = NULL; int r; @@ -7388,22 +7389,22 @@ int tpm2_make_luks2_json( * other programming languages. Let's not make things worse though, i.e. future additions to the JSON * object should use "_" rather than "-" in field names. */ - r = json_build(&v, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-tpm2")), - JSON_BUILD_PAIR("keyslots", JSON_BUILD_ARRAY(JSON_BUILD_STRING(keyslot_as_string))), - JSON_BUILD_PAIR("tpm2-blob", JSON_BUILD_IOVEC_BASE64(blob)), - JSON_BUILD_PAIR("tpm2-pcrs", JSON_BUILD_VARIANT(hmj)), - JSON_BUILD_PAIR_CONDITION(pcr_bank != 0 && tpm2_hash_alg_to_string(pcr_bank), "tpm2-pcr-bank", JSON_BUILD_STRING(tpm2_hash_alg_to_string(pcr_bank))), - JSON_BUILD_PAIR_CONDITION(primary_alg != 0 && tpm2_asym_alg_to_string(primary_alg), "tpm2-primary-alg", JSON_BUILD_STRING(tpm2_asym_alg_to_string(primary_alg))), - JSON_BUILD_PAIR("tpm2-policy-hash", JSON_BUILD_IOVEC_HEX(policy_hash)), - JSON_BUILD_PAIR_CONDITION(FLAGS_SET(flags, TPM2_FLAGS_USE_PIN), "tpm2-pin", JSON_BUILD_BOOLEAN(true)), - JSON_BUILD_PAIR_CONDITION(FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK), "tpm2_pcrlock", JSON_BUILD_BOOLEAN(true)), - JSON_BUILD_PAIR_CONDITION(pubkey_pcr_mask != 0, "tpm2_pubkey_pcrs", JSON_BUILD_VARIANT(pkmj)), - JSON_BUILD_PAIR_CONDITION(iovec_is_set(pubkey), "tpm2_pubkey", JSON_BUILD_IOVEC_BASE64(pubkey)), - JSON_BUILD_PAIR_CONDITION(iovec_is_set(salt), "tpm2_salt", JSON_BUILD_IOVEC_BASE64(salt)), - JSON_BUILD_PAIR_CONDITION(iovec_is_set(srk), "tpm2_srk", JSON_BUILD_IOVEC_BASE64(srk)), - JSON_BUILD_PAIR_CONDITION(iovec_is_set(pcrlock_nv), "tpm2_pcrlock_nv", JSON_BUILD_IOVEC_BASE64(pcrlock_nv)))); + r = sd_json_build(&v, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("type", JSON_BUILD_CONST_STRING("systemd-tpm2")), + SD_JSON_BUILD_PAIR("keyslots", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING(keyslot_as_string))), + SD_JSON_BUILD_PAIR("tpm2-blob", JSON_BUILD_IOVEC_BASE64(blob)), + SD_JSON_BUILD_PAIR("tpm2-pcrs", SD_JSON_BUILD_VARIANT(hmj)), + SD_JSON_BUILD_PAIR_CONDITION(pcr_bank != 0 && tpm2_hash_alg_to_string(pcr_bank), "tpm2-pcr-bank", SD_JSON_BUILD_STRING(tpm2_hash_alg_to_string(pcr_bank))), + SD_JSON_BUILD_PAIR_CONDITION(primary_alg != 0 && tpm2_asym_alg_to_string(primary_alg), "tpm2-primary-alg", SD_JSON_BUILD_STRING(tpm2_asym_alg_to_string(primary_alg))), + SD_JSON_BUILD_PAIR("tpm2-policy-hash", JSON_BUILD_IOVEC_HEX(policy_hash)), + SD_JSON_BUILD_PAIR_CONDITION(FLAGS_SET(flags, TPM2_FLAGS_USE_PIN), "tpm2-pin", SD_JSON_BUILD_BOOLEAN(true)), + SD_JSON_BUILD_PAIR_CONDITION(FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK), "tpm2_pcrlock", SD_JSON_BUILD_BOOLEAN(true)), + SD_JSON_BUILD_PAIR_CONDITION(pubkey_pcr_mask != 0, "tpm2_pubkey_pcrs", SD_JSON_BUILD_VARIANT(pkmj)), + SD_JSON_BUILD_PAIR_CONDITION(iovec_is_set(pubkey), "tpm2_pubkey", JSON_BUILD_IOVEC_BASE64(pubkey)), + SD_JSON_BUILD_PAIR_CONDITION(iovec_is_set(salt), "tpm2_salt", JSON_BUILD_IOVEC_BASE64(salt)), + SD_JSON_BUILD_PAIR_CONDITION(iovec_is_set(srk), "tpm2_srk", JSON_BUILD_IOVEC_BASE64(srk)), + SD_JSON_BUILD_PAIR_CONDITION(iovec_is_set(pcrlock_nv), "tpm2_pcrlock_nv", JSON_BUILD_IOVEC_BASE64(pcrlock_nv)))); if (r < 0) return r; @@ -7414,7 +7415,7 @@ int tpm2_make_luks2_json( } int tpm2_parse_luks2_json( - JsonVariant *v, + sd_json_variant *v, int *ret_keyslot, uint32_t *ret_hash_pcr_mask, uint16_t *ret_pcr_bank, @@ -7434,7 +7435,7 @@ int tpm2_parse_luks2_json( uint16_t pcr_bank = UINT16_MAX; /* default: pick automatically */ int r, keyslot = -1; TPM2Flags flags = 0; - JsonVariant *w; + sd_json_variant *w; assert(v); @@ -7449,7 +7450,7 @@ int tpm2_parse_luks2_json( } } - w = json_variant_by_key(v, "tpm2-pcrs"); + w = sd_json_variant_by_key(v, "tpm2-pcrs"); if (!w) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 token data lacks 'tpm2-pcrs' field."); @@ -7459,37 +7460,37 @@ int tpm2_parse_luks2_json( /* The bank field is optional, since it was added in systemd 250 only. Before the bank was hardcoded * to SHA256. */ - w = json_variant_by_key(v, "tpm2-pcr-bank"); + w = sd_json_variant_by_key(v, "tpm2-pcr-bank"); if (w) { /* The PCR bank field is optional */ - if (!json_variant_is_string(w)) + if (!sd_json_variant_is_string(w)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 PCR bank is not a string."); - r = tpm2_hash_alg_from_string(json_variant_string(w)); + r = tpm2_hash_alg_from_string(sd_json_variant_string(w)); if (r < 0) - return log_debug_errno(r, "TPM2 PCR bank invalid or not supported: %s", json_variant_string(w)); + return log_debug_errno(r, "TPM2 PCR bank invalid or not supported: %s", sd_json_variant_string(w)); pcr_bank = r; } /* The primary key algorithm field is optional, since it was also added in systemd 250 only. Before * the algorithm was hardcoded to ECC. */ - w = json_variant_by_key(v, "tpm2-primary-alg"); + w = sd_json_variant_by_key(v, "tpm2-primary-alg"); if (w) { /* The primary key algorithm is optional */ - if (!json_variant_is_string(w)) + if (!sd_json_variant_is_string(w)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 primary key algorithm is not a string."); - r = tpm2_asym_alg_from_string(json_variant_string(w)); + r = tpm2_asym_alg_from_string(sd_json_variant_string(w)); if (r < 0) - return log_debug_errno(r, "TPM2 asymmetric algorithm invalid or not supported: %s", json_variant_string(w)); + return log_debug_errno(r, "TPM2 asymmetric algorithm invalid or not supported: %s", sd_json_variant_string(w)); primary_alg = r; } - w = json_variant_by_key(v, "tpm2-blob"); + w = sd_json_variant_by_key(v, "tpm2-blob"); if (!w) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 token data lacks 'tpm2-blob' field."); @@ -7497,7 +7498,7 @@ int tpm2_parse_luks2_json( if (r < 0) return log_debug_errno(r, "Invalid base64 data in 'tpm2-blob' field."); - w = json_variant_by_key(v, "tpm2-policy-hash"); + w = sd_json_variant_by_key(v, "tpm2-policy-hash"); if (!w) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 token data lacks 'tpm2-policy-hash' field."); @@ -7505,37 +7506,37 @@ int tpm2_parse_luks2_json( if (r < 0) return log_debug_errno(r, "Invalid base64 data in 'tpm2-policy-hash' field."); - w = json_variant_by_key(v, "tpm2-pin"); + w = sd_json_variant_by_key(v, "tpm2-pin"); if (w) { - if (!json_variant_is_boolean(w)) + if (!sd_json_variant_is_boolean(w)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 PIN policy is not a boolean."); - SET_FLAG(flags, TPM2_FLAGS_USE_PIN, json_variant_boolean(w)); + SET_FLAG(flags, TPM2_FLAGS_USE_PIN, sd_json_variant_boolean(w)); } - w = json_variant_by_key(v, "tpm2_pcrlock"); + w = sd_json_variant_by_key(v, "tpm2_pcrlock"); if (w) { - if (!json_variant_is_boolean(w)) + if (!sd_json_variant_is_boolean(w)) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "TPM2 pclock policy is not a boolean."); - SET_FLAG(flags, TPM2_FLAGS_USE_PCRLOCK, json_variant_boolean(w)); + SET_FLAG(flags, TPM2_FLAGS_USE_PCRLOCK, sd_json_variant_boolean(w)); } - w = json_variant_by_key(v, "tpm2_salt"); + w = sd_json_variant_by_key(v, "tpm2_salt"); if (w) { r = json_variant_unbase64_iovec(w, &salt); if (r < 0) return log_debug_errno(r, "Invalid base64 data in 'tpm2_salt' field."); } - w = json_variant_by_key(v, "tpm2_pubkey_pcrs"); + w = sd_json_variant_by_key(v, "tpm2_pubkey_pcrs"); if (w) { r = tpm2_parse_pcr_json_array(w, &pubkey_pcr_mask); if (r < 0) return r; } - w = json_variant_by_key(v, "tpm2_pubkey"); + w = sd_json_variant_by_key(v, "tpm2_pubkey"); if (w) { r = json_variant_unbase64_iovec(w, &pubkey); if (r < 0) @@ -7543,14 +7544,14 @@ int tpm2_parse_luks2_json( } else if (pubkey_pcr_mask != 0) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Public key PCR mask set, but not public key included in JSON data, refusing."); - w = json_variant_by_key(v, "tpm2_srk"); + w = sd_json_variant_by_key(v, "tpm2_srk"); if (w) { r = json_variant_unbase64_iovec(w, &srk); if (r < 0) return log_debug_errno(r, "Invalid base64 data in 'tpm2_srk' field."); } - w = json_variant_by_key(v, "tpm2_pcrlock_nv"); + w = sd_json_variant_by_key(v, "tpm2_pcrlock_nv"); if (w) { r = json_variant_unbase64_iovec(w, &pcrlock_nv); if (r < 0) @@ -7884,7 +7885,7 @@ int tpm2_parse_pcr_argument_to_mask(const char *arg, uint32_t *ret_mask) { #endif } -int tpm2_load_pcr_signature(const char *path, JsonVariant **ret) { +int tpm2_load_pcr_signature(const char *path, sd_json_variant **ret) { _cleanup_strv_free_ char **search = NULL; _cleanup_free_ char *discovered_path = NULL; _cleanup_fclose_ FILE *f = NULL; @@ -7913,7 +7914,7 @@ int tpm2_load_pcr_signature(const char *path, JsonVariant **ret) { if (r < 0) return log_debug_errno(r, "Failed to find TPM PCR signature file '%s': %m", path); - r = json_parse_file(f, discovered_path, 0, ret, NULL, NULL); + r = sd_json_parse_file(f, discovered_path, 0, ret, NULL, NULL); if (r < 0) return log_debug_errno(r, "Failed to parse TPM PCR signature JSON object '%s': %m", discovered_path); diff --git a/src/shared/tpm2-util.h b/src/shared/tpm2-util.h index ed306d4113..8d5aac09b2 100644 --- a/src/shared/tpm2-util.h +++ b/src/shared/tpm2-util.h @@ -5,7 +5,7 @@ #include "bitfield.h" #include "io-util.h" -#include "json.h" +#include "sd-json.h" #include "macro.h" #include "openssl-util.h" #include "ordered-set.h" @@ -223,8 +223,8 @@ extern const struct hash_ops tpm2_pcr_prediction_result_hash_ops; bool tpm2_pcr_prediction_equal(Tpm2PCRPrediction *a, Tpm2PCRPrediction *b, uint16_t algorithm); -int tpm2_pcr_prediction_to_json(const Tpm2PCRPrediction *prediction, uint16_t algorithm, JsonVariant **ret); -int tpm2_pcr_prediction_from_json(Tpm2PCRPrediction *prediction, uint16_t algorithm, JsonVariant *aj); +int tpm2_pcr_prediction_to_json(const Tpm2PCRPrediction *prediction, uint16_t algorithm, sd_json_variant **ret); +int tpm2_pcr_prediction_from_json(Tpm2PCRPrediction *prediction, uint16_t algorithm, sd_json_variant *aj); /* As structure encapsulating all metadata stored for a pcrlock policy on disk */ typedef struct Tpm2PCRLockPolicy { @@ -242,12 +242,12 @@ typedef struct Tpm2PCRLockPolicy { /* The below contains the current prediction whose resulting policy is stored in the NV * index. Once in JSON and once in parsed form. When the policy is updated the fields below are * changed, the fields above remain fixed. */ - JsonVariant *prediction_json; + sd_json_variant *prediction_json; Tpm2PCRPrediction prediction; } Tpm2PCRLockPolicy; void tpm2_pcrlock_policy_done(Tpm2PCRLockPolicy *data); -int tpm2_pcrlock_policy_from_json(JsonVariant *v, Tpm2PCRLockPolicy *ret_policy); +int tpm2_pcrlock_policy_from_json(sd_json_variant *v, Tpm2PCRLockPolicy *ret_policy); int tpm2_pcrlock_search_file(const char *path, FILE **ret_file, char **ret_path); int tpm2_pcrlock_policy_load(const char *path, Tpm2PCRLockPolicy *ret_policy); int tpm2_pcrlock_policy_from_credentials(const struct iovec *srk, const struct iovec *nv, Tpm2PCRLockPolicy *ret); @@ -292,7 +292,7 @@ int tpm2_get_srk(Tpm2Context *c, const Tpm2Handle *session, TPM2B_PUBLIC **ret_p int tpm2_get_or_create_srk(Tpm2Context *c, const Tpm2Handle *session, TPM2B_PUBLIC **ret_public, TPM2B_NAME **ret_name, TPM2B_NAME **ret_qname, Tpm2Handle **ret_handle); int tpm2_seal(Tpm2Context *c, uint32_t seal_key_handle, const TPM2B_DIGEST *policy, const char *pin, struct iovec *ret_secret, struct iovec *ret_blob, uint16_t *ret_primary_alg, struct iovec *ret_srk); -int tpm2_unseal(Tpm2Context *c, uint32_t hash_pcr_mask, uint16_t pcr_bank, const struct iovec *pubkey, uint32_t pubkey_pcr_mask, JsonVariant *signature, const char *pin, const Tpm2PCRLockPolicy *pcrlock_policy, uint16_t primary_alg, const struct iovec *blob, const struct iovec *policy_hash, const struct iovec *srk, struct iovec *ret_secret); +int tpm2_unseal(Tpm2Context *c, uint32_t hash_pcr_mask, uint16_t pcr_bank, const struct iovec *pubkey, uint32_t pubkey_pcr_mask, sd_json_variant *signature, const char *pin, const Tpm2PCRLockPolicy *pcrlock_policy, uint16_t primary_alg, const struct iovec *blob, const struct iovec *policy_hash, const struct iovec *srk, struct iovec *ret_secret); #if HAVE_OPENSSL int tpm2_tpm2b_public_to_openssl_pkey(const TPM2B_PUBLIC *public, EVP_PKEY **ret); @@ -388,11 +388,11 @@ static inline int tpm2_pcrlock_search_file(const char *path, FILE **ret_file, ch int tpm2_list_devices(void); int tpm2_find_device_auto(char **ret); -int tpm2_make_pcr_json_array(uint32_t pcr_mask, JsonVariant **ret); -int tpm2_parse_pcr_json_array(JsonVariant *v, uint32_t *ret); +int tpm2_make_pcr_json_array(uint32_t pcr_mask, sd_json_variant **ret); +int tpm2_parse_pcr_json_array(sd_json_variant *v, uint32_t *ret); -int tpm2_make_luks2_json(int keyslot, uint32_t hash_pcr_mask, uint16_t pcr_bank, const struct iovec *pubkey, uint32_t pubkey_pcr_mask, uint16_t primary_alg, const struct iovec *blob, const struct iovec *policy_hash, const struct iovec *salt, const struct iovec *srk, const struct iovec *pcrlock_nv, TPM2Flags flags, JsonVariant **ret); -int tpm2_parse_luks2_json(JsonVariant *v, int *ret_keyslot, uint32_t *ret_hash_pcr_mask, uint16_t *ret_pcr_bank, struct iovec *ret_pubkey, uint32_t *ret_pubkey_pcr_mask, uint16_t *ret_primary_alg, struct iovec *ret_blob, struct iovec *ret_policy_hash, struct iovec *ret_salt, struct iovec *ret_srk, struct iovec *pcrlock_nv, TPM2Flags *ret_flags); +int tpm2_make_luks2_json(int keyslot, uint32_t hash_pcr_mask, uint16_t pcr_bank, const struct iovec *pubkey, uint32_t pubkey_pcr_mask, uint16_t primary_alg, const struct iovec *blob, const struct iovec *policy_hash, const struct iovec *salt, const struct iovec *srk, const struct iovec *pcrlock_nv, TPM2Flags flags, sd_json_variant **ret); +int tpm2_parse_luks2_json(sd_json_variant *v, int *ret_keyslot, uint32_t *ret_hash_pcr_mask, uint16_t *ret_pcr_bank, struct iovec *ret_pubkey, uint32_t *ret_pubkey_pcr_mask, uint16_t *ret_primary_alg, struct iovec *ret_blob, struct iovec *ret_policy_hash, struct iovec *ret_salt, struct iovec *ret_srk, struct iovec *pcrlock_nv, TPM2Flags *ret_flags); /* Default to PCR 7 only */ #define TPM2_PCR_INDEX_DEFAULT UINT32_C(7) @@ -467,7 +467,7 @@ int tpm2_parse_pcr_argument(const char *arg, Tpm2PCRValue **ret_pcr_values, size int tpm2_parse_pcr_argument_append(const char *arg, Tpm2PCRValue **ret_pcr_values, size_t *ret_n_pcr_values); int tpm2_parse_pcr_argument_to_mask(const char *arg, uint32_t *mask); -int tpm2_load_pcr_signature(const char *path, JsonVariant **ret); +int tpm2_load_pcr_signature(const char *path, sd_json_variant **ret); int tpm2_load_pcr_public_key(const char *path, void **ret_pubkey, size_t *ret_pubkey_size); int tpm2_util_pbkdf2_hmac_sha256(const void *pass, diff --git a/src/shared/user-record-nss.c b/src/shared/user-record-nss.c index ffb5721466..fa99694ed7 100644 --- a/src/shared/user-record-nss.c +++ b/src/shared/user-record-nss.c @@ -136,23 +136,23 @@ int nss_passwd_to_user_record( spwd && spwd->sp_inact > 0 && (uint64_t) spwd->sp_inact <= (UINT64_MAX-1)/USEC_PER_DAY, spwd->sp_inact * USEC_PER_DAY, UINT64_MAX); - hr->json = json_variant_unref(hr->json); - r = json_build(&hr->json, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(hr->user_name)), - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(hr->uid)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(hr->gid)), - JSON_BUILD_PAIR_CONDITION(hr->real_name, "realName", JSON_BUILD_STRING(hr->real_name)), - JSON_BUILD_PAIR_CONDITION(hr->home_directory, "homeDirectory", JSON_BUILD_STRING(hr->home_directory)), - JSON_BUILD_PAIR_CONDITION(hr->shell, "shell", JSON_BUILD_STRING(hr->shell)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(hr->hashed_password), "privileged", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRV(hr->hashed_password)))), - JSON_BUILD_PAIR_CONDITION(hr->locked >= 0, "locked", JSON_BUILD_BOOLEAN(hr->locked)), - JSON_BUILD_PAIR_CONDITION(hr->not_after_usec != UINT64_MAX, "notAfterUSec", JSON_BUILD_UNSIGNED(hr->not_after_usec)), - JSON_BUILD_PAIR_CONDITION(hr->password_change_now >= 0, "passwordChangeNow", JSON_BUILD_BOOLEAN(hr->password_change_now)), - JSON_BUILD_PAIR_CONDITION(hr->last_password_change_usec != UINT64_MAX, "lastPasswordChangeUSec", JSON_BUILD_UNSIGNED(hr->last_password_change_usec)), - JSON_BUILD_PAIR_CONDITION(hr->password_change_min_usec != UINT64_MAX, "passwordChangeMinUSec", JSON_BUILD_UNSIGNED(hr->password_change_min_usec)), - JSON_BUILD_PAIR_CONDITION(hr->password_change_max_usec != UINT64_MAX, "passwordChangeMaxUSec", JSON_BUILD_UNSIGNED(hr->password_change_max_usec)), - JSON_BUILD_PAIR_CONDITION(hr->password_change_warn_usec != UINT64_MAX, "passwordChangeWarnUSec", JSON_BUILD_UNSIGNED(hr->password_change_warn_usec)), - JSON_BUILD_PAIR_CONDITION(hr->password_change_inactive_usec != UINT64_MAX, "passwordChangeInactiveUSec", JSON_BUILD_UNSIGNED(hr->password_change_inactive_usec)))); + hr->json = sd_json_variant_unref(hr->json); + r = sd_json_build(&hr->json, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(hr->user_name)), + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(hr->uid)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(hr->gid)), + SD_JSON_BUILD_PAIR_CONDITION(!!hr->real_name, "realName", SD_JSON_BUILD_STRING(hr->real_name)), + SD_JSON_BUILD_PAIR_CONDITION(!!hr->home_directory, "homeDirectory", SD_JSON_BUILD_STRING(hr->home_directory)), + SD_JSON_BUILD_PAIR_CONDITION(!!hr->shell, "shell", SD_JSON_BUILD_STRING(hr->shell)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(hr->hashed_password), "privileged", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("hashedPassword", SD_JSON_BUILD_STRV(hr->hashed_password)))), + SD_JSON_BUILD_PAIR_CONDITION(hr->locked >= 0, "locked", SD_JSON_BUILD_BOOLEAN(hr->locked)), + SD_JSON_BUILD_PAIR_CONDITION(hr->not_after_usec != UINT64_MAX, "notAfterUSec", SD_JSON_BUILD_UNSIGNED(hr->not_after_usec)), + SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_now >= 0, "passwordChangeNow", SD_JSON_BUILD_BOOLEAN(hr->password_change_now)), + SD_JSON_BUILD_PAIR_CONDITION(hr->last_password_change_usec != UINT64_MAX, "lastPasswordChangeUSec", SD_JSON_BUILD_UNSIGNED(hr->last_password_change_usec)), + SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_min_usec != UINT64_MAX, "passwordChangeMinUSec", SD_JSON_BUILD_UNSIGNED(hr->password_change_min_usec)), + SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_max_usec != UINT64_MAX, "passwordChangeMaxUSec", SD_JSON_BUILD_UNSIGNED(hr->password_change_max_usec)), + SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_warn_usec != UINT64_MAX, "passwordChangeWarnUSec", SD_JSON_BUILD_UNSIGNED(hr->password_change_warn_usec)), + SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_inactive_usec != UINT64_MAX, "passwordChangeInactiveUSec", SD_JSON_BUILD_UNSIGNED(hr->password_change_inactive_usec)))); if (r < 0) return r; @@ -319,12 +319,12 @@ int nss_group_to_group_record( return r; } - r = json_build(&g->json, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(g->group_name)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(g->gid)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->members), "members", JSON_BUILD_STRV(g->members)), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->hashed_password), "privileged", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("hashedPassword", JSON_BUILD_STRV(g->hashed_password)))), - JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->administrators), "administrators", JSON_BUILD_STRV(g->administrators)))); + r = sd_json_build(&g->json, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(g->group_name)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(g->gid)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->members), "members", SD_JSON_BUILD_STRV(g->members)), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->hashed_password), "privileged", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("hashedPassword", SD_JSON_BUILD_STRV(g->hashed_password)))), + SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->administrators), "administrators", SD_JSON_BUILD_STRV(g->administrators)))); if (r < 0) return r; diff --git a/src/shared/user-record.c b/src/shared/user-record.c index d0fdf6c1b5..daf9927b27 100644 --- a/src/shared/user-record.c +++ b/src/shared/user-record.c @@ -10,6 +10,7 @@ #include "glyph-util.h" #include "hexdecoct.h" #include "hostname-util.h" +#include "json-util.h" #include "locale-util.h" #include "memory-util.h" #include "path-util.h" @@ -206,27 +207,27 @@ static UserRecord* user_record_free(UserRecord *h) { for (size_t i = 0; i < h->n_recovery_key; i++) recovery_key_done(h->recovery_key + i); - json_variant_unref(h->json); + sd_json_variant_unref(h->json); return mfree(h); } DEFINE_TRIVIAL_REF_UNREF_FUNC(UserRecord, user_record, user_record_free); -int json_dispatch_realm(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +int json_dispatch_realm(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **s = userdata; const char *n; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = mfree(*s); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - n = json_variant_string(variant); + n = sd_json_variant_string(variant); r = dns_name_is_valid(n); if (r < 0) return json_log(variant, flags, r, "Failed to check if JSON field '%s' is a valid DNS domain.", strna(name)); @@ -240,26 +241,26 @@ int json_dispatch_realm(const char *name, JsonVariant *variant, JsonDispatchFlag return 0; } -int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +int json_dispatch_gecos(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **s = userdata; const char *n; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = mfree(*s); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - n = json_variant_string(variant); + n = sd_json_variant_string(variant); if (valid_gecos(n)) { if (free_and_strdup(s, n) < 0) return json_log_oom(variant, flags); } else { _cleanup_free_ char *m = NULL; - json_log(variant, flags|JSON_DEBUG, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid GECOS compatible string, mangling.", strna(name)); + json_log(variant, flags|SD_JSON_DEBUG, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid GECOS compatible string, mangling.", strna(name)); m = mangle_gecos(n); if (!m) @@ -271,19 +272,19 @@ int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlag return 0; } -static int json_dispatch_nice(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_nice(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { int *nl = userdata; int64_t m; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *nl = INT_MAX; return 0; } - if (!json_variant_is_integer(variant)) + if (!sd_json_variant_is_integer(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - m = json_variant_integer(variant); + m = sd_json_variant_integer(variant); if (m < PRIO_MIN || m >= PRIO_MAX) return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' is not a valid nice level.", strna(name)); @@ -291,16 +292,16 @@ static int json_dispatch_nice(const char *name, JsonVariant *variant, JsonDispat return 0; } -static int json_dispatch_rlimit_value(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_rlimit_value(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { rlim_t *ret = userdata; - if (json_variant_is_null(variant)) + if (sd_json_variant_is_null(variant)) *ret = RLIM_INFINITY; - else if (json_variant_is_unsigned(variant)) { + else if (sd_json_variant_is_unsigned(variant)) { uint64_t w; - w = json_variant_unsigned(variant); - if (w == RLIM_INFINITY || (uint64_t) w != json_variant_unsigned(variant)) + w = sd_json_variant_unsigned(variant); + if (w == RLIM_INFINITY || (uint64_t) w != sd_json_variant_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "Resource limit value '%s' is out of range.", name); *ret = (rlim_t) w; @@ -310,24 +311,24 @@ static int json_dispatch_rlimit_value(const char *name, JsonVariant *variant, Js return 0; } -static int json_dispatch_rlimits(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_rlimits(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { struct rlimit** limits = userdata; - JsonVariant *value; + sd_json_variant *value; const char *key; int r; assert_se(limits); - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { rlimit_free_all(limits); return 0; } - if (!json_variant_is_object(variant)) + if (!sd_json_variant_is_object(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name)); JSON_VARIANT_OBJECT_FOREACH(key, value, variant) { - JsonVariant *jcur, *jmax; + sd_json_variant *jcur, *jmax; struct rlimit rl; const char *p; int l; @@ -340,20 +341,20 @@ static int json_dispatch_rlimits(const char *name, JsonVariant *variant, JsonDis if (l < 0) return json_log(variant, flags, l, "Resource limit '%s' not known.", key); - if (!json_variant_is_object(value)) + if (!sd_json_variant_is_object(value)) return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' has invalid value.", key); - if (json_variant_elements(value) != 4) + if (sd_json_variant_elements(value) != 4) return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' value is does not have two fields as expected.", key); - jcur = json_variant_by_key(value, "cur"); + jcur = sd_json_variant_by_key(value, "cur"); if (!jcur) return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' lacks 'cur' field.", key); r = json_dispatch_rlimit_value("cur", jcur, flags, &rl.rlim_cur); if (r < 0) return r; - jmax = json_variant_by_key(value, "max"); + jmax = sd_json_variant_by_key(value, "max"); if (!jmax) return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Resource limit '%s' lacks 'max' field.", key); r = json_dispatch_rlimit_value("max", jmax, flags, &rl.rlim_max); @@ -372,20 +373,20 @@ static int json_dispatch_rlimits(const char *name, JsonVariant *variant, JsonDis return 0; } -static int json_dispatch_filename_or_path(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_filename_or_path(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **s = ASSERT_PTR(userdata); const char *n; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = mfree(*s); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - n = json_variant_string(variant); + n = sd_json_variant_string(variant); if (!filename_is_valid(n) && !path_is_normalized(n)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid file name or normalized path.", strna(name)); @@ -396,20 +397,20 @@ static int json_dispatch_filename_or_path(const char *name, JsonVariant *variant return 0; } -static int json_dispatch_home_directory(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_home_directory(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **s = userdata; const char *n; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = mfree(*s); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - n = json_variant_string(variant); + n = sd_json_variant_string(variant); if (!valid_home(n)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid home directory path.", strna(name)); @@ -420,20 +421,20 @@ static int json_dispatch_home_directory(const char *name, JsonVariant *variant, return 0; } -static int json_dispatch_image_path(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_image_path(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **s = userdata; const char *n; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = mfree(*s); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - n = json_variant_string(variant); + n = sd_json_variant_string(variant); if (empty_or_root(n) || !path_is_valid(n) || !path_is_absolute(n)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid image path.", strna(name)); @@ -444,19 +445,19 @@ static int json_dispatch_image_path(const char *name, JsonVariant *variant, Json return 0; } -static int json_dispatch_umask(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_umask(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { mode_t *m = userdata; uint64_t k; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *m = MODE_INVALID; return 0; } - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a number.", strna(name)); - k = json_variant_unsigned(variant); + k = sd_json_variant_unsigned(variant); if (k > 0777) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' outside of valid range 0%s0777.", @@ -466,19 +467,19 @@ static int json_dispatch_umask(const char *name, JsonVariant *variant, JsonDispa return 0; } -static int json_dispatch_access_mode(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_access_mode(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { mode_t *m = userdata; uint64_t k; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *m = MODE_INVALID; return 0; } - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a number.", strna(name)); - k = json_variant_unsigned(variant); + k = sd_json_variant_unsigned(variant); if (k > 07777) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' outside of valid range 0%s07777.", @@ -488,28 +489,28 @@ static int json_dispatch_access_mode(const char *name, JsonVariant *variant, Jso return 0; } -static int json_dispatch_environment(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_environment(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_strv_free_ char **n = NULL; char ***l = userdata; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *l = strv_free(*l); return 0; } - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); - for (size_t i = 0; i < json_variant_elements(variant); i++) { - JsonVariant *e; + for (size_t i = 0; i < sd_json_variant_elements(variant); i++) { + sd_json_variant *e; const char *a; - e = json_variant_by_index(variant, i); - if (!json_variant_is_string(e)) + e = sd_json_variant_by_index(variant, i); + if (!sd_json_variant_is_string(e)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name)); - assert_se(a = json_variant_string(e)); + assert_se(a = sd_json_variant_string(e)); if (!env_assignment_is_valid(a)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of environment variables.", strna(name)); @@ -522,20 +523,20 @@ static int json_dispatch_environment(const char *name, JsonVariant *variant, Jso return strv_free_and_replace(*l, n); } -static int json_dispatch_locale(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_locale(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **s = userdata; const char *n; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = mfree(*s); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - n = json_variant_string(variant); + n = sd_json_variant_string(variant); if (!locale_is_valid(n)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid locale.", strna(name)); @@ -547,26 +548,26 @@ static int json_dispatch_locale(const char *name, JsonVariant *variant, JsonDisp return 0; } -static int json_dispatch_locales(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_locales(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_strv_free_ char **n = NULL; char ***l = userdata; const char *locale; - JsonVariant *e; + sd_json_variant *e; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *l = strv_free(*l); return 0; } - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name)); - locale = json_variant_string(e); + locale = sd_json_variant_string(e); if (!locale_is_valid(locale)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of valid locales.", strna(name)); @@ -581,18 +582,18 @@ static int json_dispatch_locales(const char *name, JsonVariant *variant, JsonDis JSON_DISPATCH_ENUM_DEFINE(json_dispatch_user_disposition, UserDisposition, user_disposition_from_string); static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_user_storage, UserStorage, user_storage_from_string); -static int json_dispatch_tasks_or_memory_max(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_tasks_or_memory_max(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uint64_t *limit = userdata, k; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *limit = UINT64_MAX; return 0; } - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - k = json_variant_unsigned(variant); + k = sd_json_variant_unsigned(variant); if (k <= 0 || k >= UINT64_MAX) return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' is not in valid range %" PRIu64 "%s%" PRIu64 ".", @@ -602,18 +603,18 @@ static int json_dispatch_tasks_or_memory_max(const char *name, JsonVariant *vari return 0; } -static int json_dispatch_weight(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int json_dispatch_weight(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uint64_t *weight = userdata, k; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *weight = UINT64_MAX; return 0; } - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - k = json_variant_unsigned(variant); + k = sd_json_variant_unsigned(variant); if (k <= CGROUP_WEIGHT_MIN || k >= CGROUP_WEIGHT_MAX) return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' is not in valid range %" PRIu64 "%s%" PRIu64 ".", @@ -624,24 +625,24 @@ static int json_dispatch_weight(const char *name, JsonVariant *variant, JsonDisp return 0; } -int json_dispatch_user_group_list(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +int json_dispatch_user_group_list(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_strv_free_ char **l = NULL; char ***list = userdata; - JsonVariant *e; + sd_json_variant *e; int r; - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string."); - if (!valid_user_group_name(json_variant_string(e), FLAGS_SET(flags, JSON_RELAX) ? VALID_USER_RELAX : 0)) - return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a valid user/group name: %s", json_variant_string(e)); + if (!valid_user_group_name(sd_json_variant_string(e), FLAGS_SET(flags, SD_JSON_RELAX) ? VALID_USER_RELAX : 0)) + return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a valid user/group name: %s", sd_json_variant_string(e)); - r = strv_extend(&l, json_variant_string(e)); + r = strv_extend(&l, sd_json_variant_string(e)); if (r < 0) return json_log(e, flags, r, "Failed to append array element: %m"); } @@ -653,35 +654,35 @@ int json_dispatch_user_group_list(const char *name, JsonVariant *variant, JsonDi return 0; } -static int dispatch_secret(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_secret(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch secret_dispatch_table[] = { - { "password", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, password), 0 }, - { "tokenPin", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, token_pin), 0 }, - { "pkcs11Pin", /* legacy alias */ _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, token_pin), 0 }, - { "pkcs11ProtectedAuthenticationPathPermitted", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, pkcs11_protected_authentication_path_permitted), 0 }, - { "fido2UserPresencePermitted", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, fido2_user_presence_permitted), 0 }, - { "fido2UserVerificationPermitted", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, fido2_user_verification_permitted), 0 }, + static const sd_json_dispatch_field secret_dispatch_table[] = { + { "password", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, password), 0 }, + { "tokenPin", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, token_pin), 0 }, + { "pkcs11Pin", /* legacy alias */ _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, token_pin), 0 }, + { "pkcs11ProtectedAuthenticationPathPermitted", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, pkcs11_protected_authentication_path_permitted), 0 }, + { "fido2UserPresencePermitted", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, fido2_user_presence_permitted), 0 }, + { "fido2UserVerificationPermitted", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, fido2_user_verification_permitted), 0 }, {}, }; - return json_dispatch(variant, secret_dispatch_table, flags, userdata); + return sd_json_dispatch(variant, secret_dispatch_table, flags, userdata); } -static int dispatch_pkcs11_uri(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_pkcs11_uri(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { char **s = userdata; const char *n; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *s = mfree(*s); return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - n = json_variant_string(variant); + n = sd_json_variant_string(variant); if (!pkcs11_uri_valid(n)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name)); @@ -692,21 +693,21 @@ static int dispatch_pkcs11_uri(const char *name, JsonVariant *variant, JsonDispa return 0; } -static int dispatch_pkcs11_uri_array(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_pkcs11_uri_array(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_strv_free_ char **z = NULL; char ***l = userdata; - JsonVariant *e; + sd_json_variant *e; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *l = strv_free(*l); return 0; } - if (json_variant_is_string(variant)) { + if (sd_json_variant_is_string(variant)) { const char *n; - n = json_variant_string(variant); + n = sd_json_variant_string(variant); if (!pkcs11_uri_valid(n)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid RFC7512 PKCS#11 URI.", strna(name)); @@ -716,16 +717,16 @@ static int dispatch_pkcs11_uri_array(const char *name, JsonVariant *variant, Jso } else { - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string or array of strings.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { const char *n; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string."); - n = json_variant_string(e); + n = sd_json_variant_string(e); if (!pkcs11_uri_valid(n)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element in '%s' is not a valid RFC7512 PKCS#11 URI: %s", strna(name), n); @@ -739,22 +740,22 @@ static int dispatch_pkcs11_uri_array(const char *name, JsonVariant *variant, Jso return 0; } -static int dispatch_pkcs11_key_data(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_pkcs11_key_data(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { Pkcs11EncryptedKey *k = userdata; size_t l; void *b; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { k->data = erase_and_free(k->data); k->size = 0; return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - r = unbase64mem(json_variant_string(variant), &b, &l); + r = unbase64mem(sd_json_variant_string(variant), &b, &l); if (r < 0) return json_log(variant, flags, r, "Failed to decode encrypted PKCS#11 key: %m"); @@ -765,25 +766,25 @@ static int dispatch_pkcs11_key_data(const char *name, JsonVariant *variant, Json return 0; } -static int dispatch_pkcs11_key(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_pkcs11_key(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { UserRecord *h = userdata; - JsonVariant *e; + sd_json_variant *e; int r; - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { Pkcs11EncryptedKey *array, *k; - static const JsonDispatch pkcs11_key_dispatch_table[] = { - { "uri", JSON_VARIANT_STRING, dispatch_pkcs11_uri, offsetof(Pkcs11EncryptedKey, uri), JSON_MANDATORY }, - { "data", JSON_VARIANT_STRING, dispatch_pkcs11_key_data, 0, JSON_MANDATORY }, - { "hashedPassword", JSON_VARIANT_STRING, json_dispatch_string, offsetof(Pkcs11EncryptedKey, hashed_password), JSON_MANDATORY }, + static const sd_json_dispatch_field pkcs11_key_dispatch_table[] = { + { "uri", SD_JSON_VARIANT_STRING, dispatch_pkcs11_uri, offsetof(Pkcs11EncryptedKey, uri), SD_JSON_MANDATORY }, + { "data", SD_JSON_VARIANT_STRING, dispatch_pkcs11_key_data, 0, SD_JSON_MANDATORY }, + { "hashedPassword", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Pkcs11EncryptedKey, hashed_password), SD_JSON_MANDATORY }, {}, }; - if (!json_variant_is_object(e)) + if (!sd_json_variant_is_object(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object."); array = reallocarray(h->pkcs11_encrypted_key, h->n_pkcs11_encrypted_key + 1, sizeof(Pkcs11EncryptedKey)); @@ -794,7 +795,7 @@ static int dispatch_pkcs11_key(const char *name, JsonVariant *variant, JsonDispa k = h->pkcs11_encrypted_key + h->n_pkcs11_encrypted_key; *k = (Pkcs11EncryptedKey) {}; - r = json_dispatch(e, pkcs11_key_dispatch_table, flags, k); + r = sd_json_dispatch(e, pkcs11_key_dispatch_table, flags, k); if (r < 0) { pkcs11_encrypted_key_done(k); return r; @@ -806,22 +807,22 @@ static int dispatch_pkcs11_key(const char *name, JsonVariant *variant, JsonDispa return 0; } -static int dispatch_fido2_hmac_credential(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_fido2_hmac_credential(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { Fido2HmacCredential *k = userdata; size_t l; void *b; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { k->id = mfree(k->id); k->size = 0; return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - r = unbase64mem(json_variant_string(variant), &b, &l); + r = unbase64mem(sd_json_variant_string(variant), &b, &l); if (r < 0) return json_log(variant, flags, r, "Failed to decode FIDO2 credential ID: %m"); @@ -831,12 +832,12 @@ static int dispatch_fido2_hmac_credential(const char *name, JsonVariant *variant return 0; } -static int dispatch_fido2_hmac_credential_array(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_fido2_hmac_credential_array(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { UserRecord *h = userdata; - JsonVariant *e; + sd_json_variant *e; int r; - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of strings.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { @@ -844,14 +845,14 @@ static int dispatch_fido2_hmac_credential_array(const char *name, JsonVariant *v size_t l; void *b; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string."); array = reallocarray(h->fido2_hmac_credential, h->n_fido2_hmac_credential + 1, sizeof(Fido2HmacCredential)); if (!array) return log_oom(); - r = unbase64mem(json_variant_string(e), &b, &l); + r = unbase64mem(sd_json_variant_string(e), &b, &l); if (r < 0) return json_log(variant, flags, r, "Failed to decode FIDO2 credential ID: %m"); @@ -866,22 +867,22 @@ static int dispatch_fido2_hmac_credential_array(const char *name, JsonVariant *v return 0; } -static int dispatch_fido2_hmac_salt_value(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_fido2_hmac_salt_value(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { Fido2HmacSalt *k = userdata; size_t l; void *b; int r; - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { k->salt = erase_and_free(k->salt); k->salt_size = 0; return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); - r = unbase64mem(json_variant_string(variant), &b, &l); + r = unbase64mem(sd_json_variant_string(variant), &b, &l); if (r < 0) return json_log(variant, flags, r, "Failed to decode FIDO2 salt: %m"); @@ -892,28 +893,28 @@ static int dispatch_fido2_hmac_salt_value(const char *name, JsonVariant *variant return 0; } -static int dispatch_fido2_hmac_salt(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_fido2_hmac_salt(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { UserRecord *h = userdata; - JsonVariant *e; + sd_json_variant *e; int r; - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { Fido2HmacSalt *array, *k; - static const JsonDispatch fido2_hmac_salt_dispatch_table[] = { - { "credential", JSON_VARIANT_STRING, dispatch_fido2_hmac_credential, offsetof(Fido2HmacSalt, credential), JSON_MANDATORY }, - { "salt", JSON_VARIANT_STRING, dispatch_fido2_hmac_salt_value, 0, JSON_MANDATORY }, - { "hashedPassword", JSON_VARIANT_STRING, json_dispatch_string, offsetof(Fido2HmacSalt, hashed_password), JSON_MANDATORY }, - { "up", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(Fido2HmacSalt, up), 0 }, - { "uv", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(Fido2HmacSalt, uv), 0 }, - { "clientPin", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(Fido2HmacSalt, client_pin), 0 }, + static const sd_json_dispatch_field fido2_hmac_salt_dispatch_table[] = { + { "credential", SD_JSON_VARIANT_STRING, dispatch_fido2_hmac_credential, offsetof(Fido2HmacSalt, credential), SD_JSON_MANDATORY }, + { "salt", SD_JSON_VARIANT_STRING, dispatch_fido2_hmac_salt_value, 0, SD_JSON_MANDATORY }, + { "hashedPassword", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Fido2HmacSalt, hashed_password), SD_JSON_MANDATORY }, + { "up", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Fido2HmacSalt, up), 0 }, + { "uv", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Fido2HmacSalt, uv), 0 }, + { "clientPin", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Fido2HmacSalt, client_pin), 0 }, {}, }; - if (!json_variant_is_object(e)) + if (!sd_json_variant_is_object(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object."); array = reallocarray(h->fido2_hmac_salt, h->n_fido2_hmac_salt + 1, sizeof(Fido2HmacSalt)); @@ -928,7 +929,7 @@ static int dispatch_fido2_hmac_salt(const char *name, JsonVariant *variant, Json .client_pin = -1, }; - r = json_dispatch(e, fido2_hmac_salt_dispatch_table, flags, k); + r = sd_json_dispatch(e, fido2_hmac_salt_dispatch_table, flags, k); if (r < 0) { fido2_hmac_salt_done(k); return r; @@ -940,24 +941,24 @@ static int dispatch_fido2_hmac_salt(const char *name, JsonVariant *variant, Json return 0; } -static int dispatch_recovery_key(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_recovery_key(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { UserRecord *h = userdata; - JsonVariant *e; + sd_json_variant *e; int r; - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { RecoveryKey *array, *k; - static const JsonDispatch recovery_key_dispatch_table[] = { - { "type", JSON_VARIANT_STRING, json_dispatch_string, 0, JSON_MANDATORY }, - { "hashedPassword", JSON_VARIANT_STRING, json_dispatch_string, offsetof(RecoveryKey, hashed_password), JSON_MANDATORY }, + static const sd_json_dispatch_field recovery_key_dispatch_table[] = { + { "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, 0, SD_JSON_MANDATORY }, + { "hashedPassword", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(RecoveryKey, hashed_password), SD_JSON_MANDATORY }, {}, }; - if (!json_variant_is_object(e)) + if (!sd_json_variant_is_object(e)) return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not an object."); array = reallocarray(h->recovery_key, h->n_recovery_key + 1, sizeof(RecoveryKey)); @@ -968,7 +969,7 @@ static int dispatch_recovery_key(const char *name, JsonVariant *variant, JsonDis k = h->recovery_key + h->n_recovery_key; *k = (RecoveryKey) {}; - r = json_dispatch(e, recovery_key_dispatch_table, flags, k); + r = sd_json_dispatch(e, recovery_key_dispatch_table, flags, k); if (r < 0) { recovery_key_done(k); return r; @@ -980,25 +981,25 @@ static int dispatch_recovery_key(const char *name, JsonVariant *variant, JsonDis return 0; } -static int dispatch_auto_resize_mode(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_auto_resize_mode(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { AutoResizeMode *mode = userdata, m; assert_se(mode); - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *mode = _AUTO_RESIZE_MODE_INVALID; return 0; } - if (json_variant_is_boolean(variant)) { - *mode = json_variant_boolean(variant) ? AUTO_RESIZE_SHRINK_AND_GROW : AUTO_RESIZE_OFF; + if (sd_json_variant_is_boolean(variant)) { + *mode = sd_json_variant_boolean(variant) ? AUTO_RESIZE_SHRINK_AND_GROW : AUTO_RESIZE_OFF; return 0; } - if (!json_variant_is_string(variant)) + if (!sd_json_variant_is_string(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string, boolean or null.", strna(name)); - m = auto_resize_mode_from_string(json_variant_string(variant)); + m = auto_resize_mode_from_string(sd_json_variant_string(variant)); if (m < 0) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid automatic resize mode.", strna(name)); @@ -1006,26 +1007,26 @@ static int dispatch_auto_resize_mode(const char *name, JsonVariant *variant, Jso return 0; } -static int dispatch_rebalance_weight(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_rebalance_weight(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { uint64_t *rebalance_weight = userdata; uintmax_t u; assert_se(rebalance_weight); - if (json_variant_is_null(variant)) { + if (sd_json_variant_is_null(variant)) { *rebalance_weight = REBALANCE_WEIGHT_UNSET; return 0; } - if (json_variant_is_boolean(variant)) { - *rebalance_weight = json_variant_boolean(variant) ? REBALANCE_WEIGHT_DEFAULT : REBALANCE_WEIGHT_OFF; + if (sd_json_variant_is_boolean(variant)) { + *rebalance_weight = sd_json_variant_boolean(variant) ? REBALANCE_WEIGHT_DEFAULT : REBALANCE_WEIGHT_OFF; return 0; } - if (!json_variant_is_unsigned(variant)) + if (!sd_json_variant_is_unsigned(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an unsigned integer, boolean or null.", strna(name)); - u = json_variant_unsigned(variant); + u = sd_json_variant_unsigned(variant); if (u >= REBALANCE_WEIGHT_MIN && u <= REBALANCE_WEIGHT_MAX) *rebalance_weight = (uint64_t) u; else if (u == 0) @@ -1038,79 +1039,79 @@ static int dispatch_rebalance_weight(const char *name, JsonVariant *variant, Jso return 0; } -static int dispatch_privileged(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_privileged(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { - static const JsonDispatch privileged_dispatch_table[] = { - { "passwordHint", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, password_hint), 0 }, - { "hashedPassword", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, hashed_password), JSON_SAFE }, - { "sshAuthorizedKeys", _JSON_VARIANT_TYPE_INVALID, json_dispatch_strv, offsetof(UserRecord, ssh_authorized_keys), 0 }, - { "pkcs11EncryptedKey", JSON_VARIANT_ARRAY, dispatch_pkcs11_key, 0, 0 }, - { "fido2HmacSalt", JSON_VARIANT_ARRAY, dispatch_fido2_hmac_salt, 0, 0 }, - { "recoveryKey", JSON_VARIANT_ARRAY, dispatch_recovery_key, 0, 0 }, + static const sd_json_dispatch_field privileged_dispatch_table[] = { + { "passwordHint", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, password_hint), 0 }, + { "hashedPassword", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, hashed_password), SD_JSON_SAFE }, + { "sshAuthorizedKeys", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(UserRecord, ssh_authorized_keys), 0 }, + { "pkcs11EncryptedKey", SD_JSON_VARIANT_ARRAY, dispatch_pkcs11_key, 0, 0 }, + { "fido2HmacSalt", SD_JSON_VARIANT_ARRAY, dispatch_fido2_hmac_salt, 0, 0 }, + { "recoveryKey", SD_JSON_VARIANT_ARRAY, dispatch_recovery_key, 0, 0 }, {}, }; - return json_dispatch(variant, privileged_dispatch_table, flags, userdata); -} - -static int dispatch_binding(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - - static const JsonDispatch binding_dispatch_table[] = { - { "blobDirectory", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), JSON_SAFE }, - { "imagePath", JSON_VARIANT_STRING, json_dispatch_image_path, offsetof(UserRecord, image_path), 0 }, - { "homeDirectory", JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, home_directory), 0 }, - { "partitionUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 }, - { "luksUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 }, - { "fileSystemUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 }, - { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 }, - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 }, - { "storage", JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 }, - { "fileSystemType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, file_system_type), JSON_SAFE }, - { "luksCipher", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher), JSON_SAFE }, - { "luksCipherMode", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), JSON_SAFE }, - { "luksVolumeKeySize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 }, + return sd_json_dispatch(variant, privileged_dispatch_table, flags, userdata); +} + +static int dispatch_binding(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + + static const sd_json_dispatch_field binding_dispatch_table[] = { + { "blobDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), SD_JSON_SAFE }, + { "imagePath", SD_JSON_VARIANT_STRING, json_dispatch_image_path, offsetof(UserRecord, image_path), 0 }, + { "homeDirectory", SD_JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, home_directory), 0 }, + { "partitionUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 }, + { "luksUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 }, + { "fileSystemUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 }, + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 }, + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 }, + { "storage", SD_JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 }, + { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, file_system_type), SD_JSON_SAFE }, + { "luksCipher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher), SD_JSON_SAFE }, + { "luksCipherMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), SD_JSON_SAFE }, + { "luksVolumeKeySize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 }, {}, }; - JsonVariant *m; + sd_json_variant *m; sd_id128_t mid; int r; if (!variant) return 0; - if (!json_variant_is_object(variant)) + if (!sd_json_variant_is_object(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name)); r = sd_id128_get_machine(&mid); if (r < 0) return json_log(variant, flags, r, "Failed to determine machine ID: %m"); - m = json_variant_by_key(variant, SD_ID128_TO_STRING(mid)); + m = sd_json_variant_by_key(variant, SD_ID128_TO_STRING(mid)); if (!m) return 0; - return json_dispatch(m, binding_dispatch_table, flags, userdata); + return sd_json_dispatch(m, binding_dispatch_table, flags, userdata); } -static int dispatch_blob_manifest(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { +static int dispatch_blob_manifest(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { _cleanup_hashmap_free_ Hashmap *manifest = NULL; Hashmap **ret = ASSERT_PTR(userdata); - JsonVariant *value; + sd_json_variant *value; const char *key; int r; if (!variant) return 0; - if (!json_variant_is_object(variant)) + if (!sd_json_variant_is_object(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name)); JSON_VARIANT_OBJECT_FOREACH(key, value, variant) { _cleanup_free_ char *filename = NULL; _cleanup_free_ uint8_t *hash = NULL; - if (!json_variant_is_string(value)) + if (!sd_json_variant_is_string(value)) return json_log(value, flags, SYNTHETIC_ERRNO(EINVAL), "Blob entry '%s' has invalid hash.", key); if (!suitable_blob_filename(key)) @@ -1124,9 +1125,9 @@ static int dispatch_blob_manifest(const char *name, JsonVariant *variant, JsonDi if (!hash) return json_log_oom(value, flags); - r = parse_sha256(json_variant_string(value), hash); + r = parse_sha256(sd_json_variant_string(value), hash); if (r < 0) - return json_log(value, flags, r, "Blob entry '%s' has invalid hash: %s", filename, json_variant_string(value)); + return json_log(value, flags, r, "Blob entry '%s' has invalid hash: %s", filename, sd_json_variant_string(value)); r = hashmap_ensure_put(&manifest, &path_hash_ops_free_free, filename, hash); if (r < 0) @@ -1139,7 +1140,7 @@ static int dispatch_blob_manifest(const char *name, JsonVariant *variant, JsonDi return 0; } -int per_machine_id_match(JsonVariant *ids, JsonDispatchFlags flags) { +int per_machine_id_match(sd_json_variant *ids, sd_json_dispatch_flags_t flags) { sd_id128_t mid; int r; @@ -1147,32 +1148,32 @@ int per_machine_id_match(JsonVariant *ids, JsonDispatchFlags flags) { if (r < 0) return json_log(ids, flags, r, "Failed to acquire machine ID: %m"); - if (json_variant_is_string(ids)) { + if (sd_json_variant_is_string(ids)) { sd_id128_t k; - r = sd_id128_from_string(json_variant_string(ids), &k); + r = sd_id128_from_string(sd_json_variant_string(ids), &k); if (r < 0) { - json_log(ids, flags, r, "%s is not a valid machine ID, ignoring: %m", json_variant_string(ids)); + json_log(ids, flags, r, "%s is not a valid machine ID, ignoring: %m", sd_json_variant_string(ids)); return 0; } return sd_id128_equal(mid, k); } - if (json_variant_is_array(ids)) { - JsonVariant *e; + if (sd_json_variant_is_array(ids)) { + sd_json_variant *e; JSON_VARIANT_ARRAY_FOREACH(e, ids) { sd_id128_t k; - if (!json_variant_is_string(e)) { + if (!sd_json_variant_is_string(e)) { json_log(e, flags, 0, "Machine ID is not a string, ignoring: %m"); continue; } - r = sd_id128_from_string(json_variant_string(e), &k); + r = sd_id128_from_string(sd_json_variant_string(e), &k); if (r < 0) { - json_log(e, flags, r, "%s is not a valid machine ID, ignoring: %m", json_variant_string(e)); + json_log(e, flags, r, "%s is not a valid machine ID, ignoring: %m", sd_json_variant_string(e)); continue; } @@ -1187,7 +1188,7 @@ int per_machine_id_match(JsonVariant *ids, JsonDispatchFlags flags) { return false; } -int per_machine_hostname_match(JsonVariant *hns, JsonDispatchFlags flags) { +int per_machine_hostname_match(sd_json_variant *hns, sd_json_dispatch_flags_t flags) { _cleanup_free_ char *hn = NULL; int r; @@ -1199,20 +1200,20 @@ int per_machine_hostname_match(JsonVariant *hns, JsonDispatchFlags flags) { if (r < 0) return json_log(hns, flags, r, "Failed to acquire hostname: %m"); - if (json_variant_is_string(hns)) - return streq(json_variant_string(hns), hn); + if (sd_json_variant_is_string(hns)) + return streq(sd_json_variant_string(hns), hn); - if (json_variant_is_array(hns)) { - JsonVariant *e; + if (sd_json_variant_is_array(hns)) { + sd_json_variant *e; JSON_VARIANT_ARRAY_FOREACH(e, hns) { - if (!json_variant_is_string(e)) { + if (!sd_json_variant_is_string(e)) { json_log(e, flags, 0, "Hostname is not a string, ignoring: %m"); continue; } - if (streq(json_variant_string(hns), hn)) + if (streq(sd_json_variant_string(hns), hn)) return true; } @@ -1223,13 +1224,13 @@ int per_machine_hostname_match(JsonVariant *hns, JsonDispatchFlags flags) { return false; } -int per_machine_match(JsonVariant *entry, JsonDispatchFlags flags) { - JsonVariant *m; +int per_machine_match(sd_json_variant *entry, sd_json_dispatch_flags_t flags) { + sd_json_variant *m; int r; - assert(json_variant_is_object(entry)); + assert(sd_json_variant_is_object(entry)); - m = json_variant_by_key(entry, "matchMachineId"); + m = sd_json_variant_by_key(entry, "matchMachineId"); if (m) { r = per_machine_id_match(m, flags); if (r < 0) @@ -1238,7 +1239,7 @@ int per_machine_match(JsonVariant *entry, JsonDispatchFlags flags) { return true; } - m = json_variant_by_key(entry, "matchHostname"); + m = sd_json_variant_by_key(entry, "matchHostname"); if (m) { r = per_machine_hostname_match(m, flags); if (r < 0) @@ -1250,98 +1251,98 @@ int per_machine_match(JsonVariant *entry, JsonDispatchFlags flags) { return false; } -static int dispatch_per_machine(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - - static const JsonDispatch per_machine_dispatch_table[] = { - { "matchMachineId", _JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, - { "matchHostname", _JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, - { "blobDirectory", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), JSON_SAFE }, - { "blobManifest", JSON_VARIANT_OBJECT, dispatch_blob_manifest, offsetof(UserRecord, blob_manifest), 0 }, - { "iconName", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, icon_name), JSON_SAFE }, - { "location", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, location), 0 }, - { "shell", JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, shell), 0 }, - { "umask", JSON_VARIANT_UNSIGNED, json_dispatch_umask, offsetof(UserRecord, umask), 0 }, - { "environment", JSON_VARIANT_ARRAY, json_dispatch_environment, offsetof(UserRecord, environment), 0 }, - { "timeZone", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, time_zone), JSON_SAFE }, - { "preferredLanguage", JSON_VARIANT_STRING, json_dispatch_locale, offsetof(UserRecord, preferred_language), 0 }, - { "additionalLanguages", JSON_VARIANT_ARRAY, json_dispatch_locales, offsetof(UserRecord, additional_languages), 0 }, - { "niceLevel", _JSON_VARIANT_TYPE_INVALID, json_dispatch_nice, offsetof(UserRecord, nice_level), 0 }, - { "resourceLimits", _JSON_VARIANT_TYPE_INVALID, json_dispatch_rlimits, offsetof(UserRecord, rlimits), 0 }, - { "locked", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, locked), 0 }, - { "notBeforeUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, not_before_usec), 0 }, - { "notAfterUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, not_after_usec), 0 }, - { "storage", JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 }, - { "diskSize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 }, - { "diskSizeRelative", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_size_relative), 0 }, - { "skeletonDirectory", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, skeleton_directory), JSON_SAFE }, - { "accessMode", JSON_VARIANT_UNSIGNED, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 }, - { "tasksMax", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, tasks_max), 0 }, - { "memoryHigh", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_high), 0 }, - { "memoryMax", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_max), 0 }, - { "cpuWeight", JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, cpu_weight), 0 }, - { "ioWeight", JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, io_weight), 0 }, - { "mountNoDevices", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, nodev), 0 }, - { "mountNoSuid", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, nosuid), 0 }, - { "mountNoExecute", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, noexec), 0 }, - { "cifsDomain", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_domain), JSON_SAFE }, - { "cifsUserName", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_user_name), JSON_SAFE }, - { "cifsService", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_service), JSON_SAFE }, - { "cifsExtraMountOptions", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_extra_mount_options), 0 }, - { "imagePath", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, image_path), JSON_SAFE }, - { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 }, - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 }, - { "memberOf", JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, member_of), JSON_RELAX}, - { "capabilityBoundingSet", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(UserRecord, capability_bounding_set), JSON_SAFE }, - { "capabilityAmbientSet", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(UserRecord, capability_ambient_set), JSON_SAFE }, - { "fileSystemType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, file_system_type), JSON_SAFE }, - { "partitionUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 }, - { "luksUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 }, - { "fileSystemUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 }, - { "luksDiscard", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, luks_discard), 0, }, - { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, luks_offline_discard), 0, }, - { "luksCipher", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher), JSON_SAFE }, - { "luksCipherMode", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), JSON_SAFE }, - { "luksVolumeKeySize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 }, - { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_pbkdf_hash_algorithm), JSON_SAFE }, - { "luksPbkdfType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_pbkdf_type), JSON_SAFE }, - { "luksPbkdfForceIterations", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_force_iterations), 0 }, - { "luksPbkdfTimeCostUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_time_cost_usec), 0 }, - { "luksPbkdfMemoryCost", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_memory_cost), 0 }, - { "luksPbkdfParallelThreads", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_parallel_threads), 0 }, - { "luksSectorSize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_sector_size), 0 }, - { "luksExtraMountOptions", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_extra_mount_options), 0 }, - { "dropCaches", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, drop_caches), 0 }, - { "autoResizeMode", _JSON_VARIANT_TYPE_INVALID, dispatch_auto_resize_mode, offsetof(UserRecord, auto_resize_mode), 0 }, - { "rebalanceWeight", _JSON_VARIANT_TYPE_INVALID, dispatch_rebalance_weight, offsetof(UserRecord, rebalance_weight), 0 }, - { "rateLimitIntervalUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, ratelimit_interval_usec), 0 }, - { "rateLimitBurst", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, ratelimit_burst), 0 }, - { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, enforce_password_policy), 0 }, - { "autoLogin", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, auto_login), 0 }, - { "preferredSessionType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, preferred_session_type), JSON_SAFE }, - { "preferredSessionLauncher", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, preferred_session_launcher), JSON_SAFE }, - { "stopDelayUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, stop_delay_usec), 0 }, - { "killProcesses", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, kill_processes), 0 }, - { "passwordChangeMinUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, password_change_min_usec), 0 }, - { "passwordChangeMaxUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, password_change_max_usec), 0 }, - { "passwordChangeWarnUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, password_change_warn_usec), 0 }, - { "passwordChangeInactiveUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, password_change_inactive_usec), 0 }, - { "passwordChangeNow", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, password_change_now), 0 }, - { "pkcs11TokenUri", JSON_VARIANT_ARRAY, dispatch_pkcs11_uri_array, offsetof(UserRecord, pkcs11_token_uri), 0 }, - { "fido2HmacCredential", JSON_VARIANT_ARRAY, dispatch_fido2_hmac_credential_array, 0, 0 }, +static int dispatch_per_machine(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + + static const sd_json_dispatch_field per_machine_dispatch_table[] = { + { "matchMachineId", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, + { "matchHostname", _SD_JSON_VARIANT_TYPE_INVALID, NULL, 0, 0 }, + { "blobDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), SD_JSON_SAFE }, + { "blobManifest", SD_JSON_VARIANT_OBJECT, dispatch_blob_manifest, offsetof(UserRecord, blob_manifest), 0 }, + { "iconName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, icon_name), SD_JSON_SAFE }, + { "location", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, location), 0 }, + { "shell", SD_JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, shell), 0 }, + { "umask", SD_JSON_VARIANT_UNSIGNED, json_dispatch_umask, offsetof(UserRecord, umask), 0 }, + { "environment", SD_JSON_VARIANT_ARRAY, json_dispatch_environment, offsetof(UserRecord, environment), 0 }, + { "timeZone", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, time_zone), SD_JSON_SAFE }, + { "preferredLanguage", SD_JSON_VARIANT_STRING, json_dispatch_locale, offsetof(UserRecord, preferred_language), 0 }, + { "additionalLanguages", SD_JSON_VARIANT_ARRAY, json_dispatch_locales, offsetof(UserRecord, additional_languages), 0 }, + { "niceLevel", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_nice, offsetof(UserRecord, nice_level), 0 }, + { "resourceLimits", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_rlimits, offsetof(UserRecord, rlimits), 0 }, + { "locked", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, locked), 0 }, + { "notBeforeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, not_before_usec), 0 }, + { "notAfterUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, not_after_usec), 0 }, + { "storage", SD_JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 }, + { "diskSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 }, + { "diskSizeRelative", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size_relative), 0 }, + { "skeletonDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, skeleton_directory), SD_JSON_SAFE }, + { "accessMode", SD_JSON_VARIANT_UNSIGNED, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 }, + { "tasksMax", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, tasks_max), 0 }, + { "memoryHigh", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_high), 0 }, + { "memoryMax", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_max), 0 }, + { "cpuWeight", SD_JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, cpu_weight), 0 }, + { "ioWeight", SD_JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, io_weight), 0 }, + { "mountNoDevices", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, nodev), 0 }, + { "mountNoSuid", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, nosuid), 0 }, + { "mountNoExecute", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, noexec), 0 }, + { "cifsDomain", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_domain), SD_JSON_SAFE }, + { "cifsUserName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_user_name), SD_JSON_SAFE }, + { "cifsService", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_service), SD_JSON_SAFE }, + { "cifsExtraMountOptions", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_extra_mount_options), 0 }, + { "imagePath", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, image_path), SD_JSON_SAFE }, + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 }, + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 }, + { "memberOf", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, member_of), SD_JSON_RELAX }, + { "capabilityBoundingSet", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, capability_bounding_set), SD_JSON_SAFE }, + { "capabilityAmbientSet", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, capability_ambient_set), SD_JSON_SAFE }, + { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, file_system_type), SD_JSON_SAFE }, + { "partitionUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 }, + { "luksUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 }, + { "fileSystemUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 }, + { "luksDiscard", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, luks_discard), 0, }, + { "luksOfflineDiscard", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, luks_offline_discard), 0, }, + { "luksCipher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher), SD_JSON_SAFE }, + { "luksCipherMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), SD_JSON_SAFE }, + { "luksVolumeKeySize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 }, + { "luksPbkdfHashAlgorithm", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_pbkdf_hash_algorithm), SD_JSON_SAFE }, + { "luksPbkdfType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_pbkdf_type), SD_JSON_SAFE }, + { "luksPbkdfForceIterations", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_force_iterations), 0 }, + { "luksPbkdfTimeCostUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_time_cost_usec), 0 }, + { "luksPbkdfMemoryCost", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_memory_cost), 0 }, + { "luksPbkdfParallelThreads", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_parallel_threads), 0 }, + { "luksSectorSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_sector_size), 0 }, + { "luksExtraMountOptions", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_extra_mount_options), 0 }, + { "dropCaches", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, drop_caches), 0 }, + { "autoResizeMode", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_auto_resize_mode, offsetof(UserRecord, auto_resize_mode), 0 }, + { "rebalanceWeight", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_rebalance_weight, offsetof(UserRecord, rebalance_weight), 0 }, + { "rateLimitIntervalUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_interval_usec), 0 }, + { "rateLimitBurst", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_burst), 0 }, + { "enforcePasswordPolicy", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, enforce_password_policy), 0 }, + { "autoLogin", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, auto_login), 0 }, + { "preferredSessionType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, preferred_session_type), SD_JSON_SAFE }, + { "preferredSessionLauncher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, preferred_session_launcher), SD_JSON_SAFE }, + { "stopDelayUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, stop_delay_usec), 0 }, + { "killProcesses", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, kill_processes), 0 }, + { "passwordChangeMinUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_min_usec), 0 }, + { "passwordChangeMaxUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_max_usec), 0 }, + { "passwordChangeWarnUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_warn_usec), 0 }, + { "passwordChangeInactiveUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_inactive_usec), 0 }, + { "passwordChangeNow", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, password_change_now), 0 }, + { "pkcs11TokenUri", SD_JSON_VARIANT_ARRAY, dispatch_pkcs11_uri_array, offsetof(UserRecord, pkcs11_token_uri), 0 }, + { "fido2HmacCredential", SD_JSON_VARIANT_ARRAY, dispatch_fido2_hmac_credential_array, 0, 0 }, {}, }; - JsonVariant *e; + sd_json_variant *e; int r; if (!variant) return 0; - if (!json_variant_is_array(variant)) + if (!sd_json_variant_is_array(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); JSON_VARIANT_ARRAY_FOREACH(e, variant) { - if (!json_variant_is_object(e)) + if (!sd_json_variant_is_object(e)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array of objects.", strna(name)); r = per_machine_match(e, flags); @@ -1350,7 +1351,7 @@ static int dispatch_per_machine(const char *name, JsonVariant *variant, JsonDisp if (r == 0) continue; - r = json_dispatch(e, per_machine_dispatch_table, flags, userdata); + r = sd_json_dispatch(e, per_machine_dispatch_table, flags, userdata); if (r < 0) return r; } @@ -1358,51 +1359,51 @@ static int dispatch_per_machine(const char *name, JsonVariant *variant, JsonDisp return 0; } -static int dispatch_status(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { - - static const JsonDispatch status_dispatch_table[] = { - { "diskUsage", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_usage), 0 }, - { "diskFree", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_free), 0 }, - { "diskSize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 }, - { "diskCeiling", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_ceiling), 0 }, - { "diskFloor", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_floor), 0 }, - { "state", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, state), JSON_SAFE }, - { "service", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, service), JSON_SAFE }, - { "signedLocally", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, signed_locally), 0 }, - { "goodAuthenticationCounter", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, good_authentication_counter), 0 }, - { "badAuthenticationCounter", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, bad_authentication_counter), 0 }, - { "lastGoodAuthenticationUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, last_good_authentication_usec), 0 }, - { "lastBadAuthenticationUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, last_bad_authentication_usec), 0 }, - { "rateLimitBeginUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, ratelimit_begin_usec), 0 }, - { "rateLimitCount", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, ratelimit_count), 0 }, - { "removable", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, removable), 0 }, - { "accessMode", JSON_VARIANT_UNSIGNED, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 }, - { "fileSystemType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, file_system_type), JSON_SAFE }, - { "fallbackShell", JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, fallback_shell), 0 }, - { "fallbackHomeDirectory", JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, fallback_home_directory), 0 }, - { "useFallback", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, use_fallback), 0 }, +static int dispatch_status(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { + + static const sd_json_dispatch_field status_dispatch_table[] = { + { "diskUsage", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_usage), 0 }, + { "diskFree", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_free), 0 }, + { "diskSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 }, + { "diskCeiling", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_ceiling), 0 }, + { "diskFloor", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_floor), 0 }, + { "state", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, state), SD_JSON_SAFE }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, service), SD_JSON_SAFE }, + { "signedLocally", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, signed_locally), 0 }, + { "goodAuthenticationCounter", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, good_authentication_counter), 0 }, + { "badAuthenticationCounter", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, bad_authentication_counter), 0 }, + { "lastGoodAuthenticationUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, last_good_authentication_usec), 0 }, + { "lastBadAuthenticationUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, last_bad_authentication_usec), 0 }, + { "rateLimitBeginUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_begin_usec), 0 }, + { "rateLimitCount", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_count), 0 }, + { "removable", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, removable), 0 }, + { "accessMode", SD_JSON_VARIANT_UNSIGNED, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 }, + { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, file_system_type), SD_JSON_SAFE }, + { "fallbackShell", SD_JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, fallback_shell), 0 }, + { "fallbackHomeDirectory", SD_JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, fallback_home_directory), 0 }, + { "useFallback", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, use_fallback), 0 }, {}, }; - JsonVariant *m; + sd_json_variant *m; sd_id128_t mid; int r; if (!variant) return 0; - if (!json_variant_is_object(variant)) + if (!sd_json_variant_is_object(variant)) return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an object.", strna(name)); r = sd_id128_get_machine(&mid); if (r < 0) return json_log(variant, flags, r, "Failed to determine machine ID: %m"); - m = json_variant_by_key(variant, SD_ID128_TO_STRING(mid)); + m = sd_json_variant_by_key(variant, SD_ID128_TO_STRING(mid)); if (!m) return 0; - return json_dispatch(m, status_dispatch_table, flags, userdata); + return sd_json_dispatch(m, status_dispatch_table, flags, userdata); } int user_record_build_image_path(UserStorage storage, const char *user_name_and_realm, char **ret) { @@ -1430,7 +1431,7 @@ int user_record_build_image_path(UserStorage storage, const char *user_name_and_ return 1; } -static int user_record_augment(UserRecord *h, JsonDispatchFlags json_flags) { +static int user_record_augment(UserRecord *h, sd_json_dispatch_flags_t json_flags) { int r; assert(h); @@ -1466,9 +1467,9 @@ static int user_record_augment(UserRecord *h, JsonDispatchFlags json_flags) { } int user_group_record_mangle( - JsonVariant *v, + sd_json_variant *v, UserRecordLoadFlags load_flags, - JsonVariant **ret_variant, + sd_json_variant **ret_variant, UserRecordMask *ret_mask) { static const struct { @@ -1483,9 +1484,9 @@ int user_group_record_mangle( { USER_RECORD_SIGNATURE, "signature" }, }; - JsonDispatchFlags json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags); - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; - JsonVariant *array[ELEMENTSOF(mask_field) * 2]; + sd_json_dispatch_flags_t json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; + sd_json_variant *array[ELEMENTSOF(mask_field) * 2]; size_t n_retain = 0; UserRecordMask m = 0; int r; @@ -1501,7 +1502,7 @@ int user_group_record_mangle( /* Note that this function is shared with the group record parser, hence we try to be generic in our * log message wording here, to cover both cases. */ - if (!json_variant_is_object(v)) + if (!sd_json_variant_is_object(v)) return json_log(v, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record is not a JSON object, refusing."); if (USER_RECORD_ALLOW_MASK(load_flags) == 0) /* allow nothing? */ @@ -1512,20 +1513,20 @@ int user_group_record_mangle( /* Check if we have the special sections and if they match our flags set */ for (size_t i = 0; i < ELEMENTSOF(mask_field); i++) { - JsonVariant *e, *k; + sd_json_variant *e, *k; if (FLAGS_SET(USER_RECORD_STRIP_MASK(load_flags), mask_field[i].mask)) { if (!w) - w = json_variant_ref(v); + w = sd_json_variant_ref(v); - r = json_variant_filter(&w, STRV_MAKE(mask_field[i].name)); + r = sd_json_variant_filter(&w, STRV_MAKE(mask_field[i].name)); if (r < 0) return json_log(w, json_flags, r, "Failed to remove field from variant: %m"); continue; } - e = json_variant_by_key_full(v, mask_field[i].name, &k); + e = sd_json_variant_by_key_full(v, mask_field[i].name, &k); if (e) { if (!FLAGS_SET(USER_RECORD_ALLOW_MASK(load_flags), mask_field[i].mask)) return json_log(e, json_flags, SYNTHETIC_ERRNO(EBADMSG), "Record contains '%s' field, which is not allowed.", mask_field[i].name); @@ -1546,17 +1547,17 @@ int user_group_record_mangle( /* If we are supposed to strip regular items, then let's instead just allocate a new object * with just the stuff we need. */ - w = json_variant_unref(w); - r = json_variant_new_object(&w, array, n_retain); + w = sd_json_variant_unref(w); + r = sd_json_variant_new_object(&w, array, n_retain); if (r < 0) return json_log(v, json_flags, r, "Failed to allocate new object: %m"); } else /* And now check if there's anything else in the record */ - for (size_t i = 0; i < json_variant_elements(v); i += 2) { + for (size_t i = 0; i < sd_json_variant_elements(v); i += 2) { const char *f; bool special = false; - assert_se(f = json_variant_string(json_variant_by_index(v, i))); + assert_se(f = sd_json_variant_string(sd_json_variant_by_index(v, i))); for (size_t j = 0; j < ELEMENTSOF(mask_field); j++) if (streq(f, mask_field[j].name)) { /* already covered in the loop above */ @@ -1582,113 +1583,113 @@ int user_group_record_mangle( if (w) *ret_variant = TAKE_PTR(w); else - *ret_variant = json_variant_ref(v); + *ret_variant = sd_json_variant_ref(v); *ret_mask = m; return 0; } -int user_record_load(UserRecord *h, JsonVariant *v, UserRecordLoadFlags load_flags) { - - static const JsonDispatch user_dispatch_table[] = { - { "userName", JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(UserRecord, user_name), JSON_RELAX}, - { "realm", JSON_VARIANT_STRING, json_dispatch_realm, offsetof(UserRecord, realm), 0 }, - { "blobDirectory", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), JSON_SAFE }, - { "blobManifest", JSON_VARIANT_OBJECT, dispatch_blob_manifest, offsetof(UserRecord, blob_manifest), 0 }, - { "realName", JSON_VARIANT_STRING, json_dispatch_gecos, offsetof(UserRecord, real_name), 0 }, - { "emailAddress", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, email_address), JSON_SAFE }, - { "iconName", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, icon_name), JSON_SAFE }, - { "location", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, location), 0 }, - { "disposition", JSON_VARIANT_STRING, json_dispatch_user_disposition, offsetof(UserRecord, disposition), 0 }, - { "lastChangeUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, last_change_usec), 0 }, - { "lastPasswordChangeUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, last_password_change_usec), 0 }, - { "shell", JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, shell), 0 }, - { "umask", JSON_VARIANT_UNSIGNED, json_dispatch_umask, offsetof(UserRecord, umask), 0 }, - { "environment", JSON_VARIANT_ARRAY, json_dispatch_environment, offsetof(UserRecord, environment), 0 }, - { "timeZone", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, time_zone), JSON_SAFE }, - { "preferredLanguage", JSON_VARIANT_STRING, json_dispatch_locale, offsetof(UserRecord, preferred_language), 0 }, - { "additionalLanguages", JSON_VARIANT_ARRAY, json_dispatch_locales, offsetof(UserRecord, additional_languages), 0 }, - { "niceLevel", _JSON_VARIANT_TYPE_INVALID, json_dispatch_nice, offsetof(UserRecord, nice_level), 0 }, - { "resourceLimits", _JSON_VARIANT_TYPE_INVALID, json_dispatch_rlimits, offsetof(UserRecord, rlimits), 0 }, - { "locked", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, locked), 0 }, - { "notBeforeUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, not_before_usec), 0 }, - { "notAfterUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, not_after_usec), 0 }, - { "storage", JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 }, - { "diskSize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 }, - { "diskSizeRelative", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, disk_size_relative), 0 }, - { "skeletonDirectory", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, skeleton_directory), JSON_SAFE }, - { "accessMode", JSON_VARIANT_UNSIGNED, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 }, - { "tasksMax", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, tasks_max), 0 }, - { "memoryHigh", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_high), 0 }, - { "memoryMax", JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_max), 0 }, - { "cpuWeight", JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, cpu_weight), 0 }, - { "ioWeight", JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, io_weight), 0 }, - { "mountNoDevices", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, nodev), 0 }, - { "mountNoSuid", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, nosuid), 0 }, - { "mountNoExecute", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(UserRecord, noexec), 0 }, - { "cifsDomain", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_domain), JSON_SAFE }, - { "cifsUserName", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_user_name), JSON_SAFE }, - { "cifsService", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_service), JSON_SAFE }, - { "cifsExtraMountOptions", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, cifs_extra_mount_options), 0 }, - { "imagePath", JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, image_path), JSON_SAFE }, - { "homeDirectory", JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, home_directory), 0 }, - { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 }, - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 }, - { "memberOf", JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, member_of), JSON_RELAX}, - { "capabilityBoundingSet", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(UserRecord, capability_bounding_set), JSON_SAFE }, - { "capabilityAmbientSet", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(UserRecord, capability_ambient_set), JSON_SAFE }, - { "fileSystemType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, file_system_type), JSON_SAFE }, - { "partitionUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 }, - { "luksUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 }, - { "fileSystemUuid", JSON_VARIANT_STRING, json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 }, - { "luksDiscard", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, luks_discard), 0 }, - { "luksOfflineDiscard", _JSON_VARIANT_TYPE_INVALID, json_dispatch_tristate, offsetof(UserRecord, luks_offline_discard), 0 }, - { "luksCipher", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher), JSON_SAFE }, - { "luksCipherMode", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), JSON_SAFE }, - { "luksVolumeKeySize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 }, - { "luksPbkdfHashAlgorithm", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_pbkdf_hash_algorithm), JSON_SAFE }, - { "luksPbkdfType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_pbkdf_type), JSON_SAFE }, - { "luksPbkdfForceIterations", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_force_iterations), 0 }, - { "luksPbkdfTimeCostUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_time_cost_usec), 0 }, - { "luksPbkdfMemoryCost", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_memory_cost), 0 }, - { "luksPbkdfParallelThreads", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_parallel_threads), 0 }, - { "luksSectorSize", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, luks_sector_size), 0 }, - { "luksExtraMountOptions", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, luks_extra_mount_options), 0 }, - { "dropCaches", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, drop_caches), 0 }, - { "autoResizeMode", _JSON_VARIANT_TYPE_INVALID, dispatch_auto_resize_mode, offsetof(UserRecord, auto_resize_mode), 0 }, - { "rebalanceWeight", _JSON_VARIANT_TYPE_INVALID, dispatch_rebalance_weight, offsetof(UserRecord, rebalance_weight), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, service), JSON_SAFE }, - { "rateLimitIntervalUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, ratelimit_interval_usec), 0 }, - { "rateLimitBurst", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, ratelimit_burst), 0 }, - { "enforcePasswordPolicy", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, enforce_password_policy), 0 }, - { "autoLogin", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, auto_login), 0 }, - { "preferredSessionType", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, preferred_session_type), JSON_SAFE }, - { "preferredSessionLauncher", JSON_VARIANT_STRING, json_dispatch_string, offsetof(UserRecord, preferred_session_launcher), JSON_SAFE }, - { "stopDelayUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, stop_delay_usec), 0 }, - { "killProcesses", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, kill_processes), 0 }, - { "passwordChangeMinUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, password_change_min_usec), 0 }, - { "passwordChangeMaxUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, password_change_max_usec), 0 }, - { "passwordChangeWarnUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, password_change_warn_usec), 0 }, - { "passwordChangeInactiveUSec", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(UserRecord, password_change_inactive_usec), 0 }, - { "passwordChangeNow", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(UserRecord, password_change_now), 0 }, - { "pkcs11TokenUri", JSON_VARIANT_ARRAY, dispatch_pkcs11_uri_array, offsetof(UserRecord, pkcs11_token_uri), 0 }, - { "fido2HmacCredential", JSON_VARIANT_ARRAY, dispatch_fido2_hmac_credential_array, 0, 0 }, - { "recoveryKeyType", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(UserRecord, recovery_key_type), 0 }, - - { "secret", JSON_VARIANT_OBJECT, dispatch_secret, 0, 0 }, - { "privileged", JSON_VARIANT_OBJECT, dispatch_privileged, 0, 0 }, +int user_record_load(UserRecord *h, sd_json_variant *v, UserRecordLoadFlags load_flags) { + + static const sd_json_dispatch_field user_dispatch_table[] = { + { "userName", SD_JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(UserRecord, user_name), SD_JSON_RELAX }, + { "realm", SD_JSON_VARIANT_STRING, json_dispatch_realm, offsetof(UserRecord, realm), 0 }, + { "blobDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, blob_directory), SD_JSON_SAFE }, + { "blobManifest", SD_JSON_VARIANT_OBJECT, dispatch_blob_manifest, offsetof(UserRecord, blob_manifest), 0 }, + { "realName", SD_JSON_VARIANT_STRING, json_dispatch_gecos, offsetof(UserRecord, real_name), 0 }, + { "emailAddress", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, email_address), SD_JSON_SAFE }, + { "iconName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, icon_name), SD_JSON_SAFE }, + { "location", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, location), 0 }, + { "disposition", SD_JSON_VARIANT_STRING, json_dispatch_user_disposition, offsetof(UserRecord, disposition), 0 }, + { "lastChangeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, last_change_usec), 0 }, + { "lastPasswordChangeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, last_password_change_usec), 0 }, + { "shell", SD_JSON_VARIANT_STRING, json_dispatch_filename_or_path, offsetof(UserRecord, shell), 0 }, + { "umask", SD_JSON_VARIANT_UNSIGNED, json_dispatch_umask, offsetof(UserRecord, umask), 0 }, + { "environment", SD_JSON_VARIANT_ARRAY, json_dispatch_environment, offsetof(UserRecord, environment), 0 }, + { "timeZone", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, time_zone), SD_JSON_SAFE }, + { "preferredLanguage", SD_JSON_VARIANT_STRING, json_dispatch_locale, offsetof(UserRecord, preferred_language), 0 }, + { "additionalLanguages", SD_JSON_VARIANT_ARRAY, json_dispatch_locales, offsetof(UserRecord, additional_languages), 0 }, + { "niceLevel", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_nice, offsetof(UserRecord, nice_level), 0 }, + { "resourceLimits", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_rlimits, offsetof(UserRecord, rlimits), 0 }, + { "locked", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, locked), 0 }, + { "notBeforeUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, not_before_usec), 0 }, + { "notAfterUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, not_after_usec), 0 }, + { "storage", SD_JSON_VARIANT_STRING, json_dispatch_user_storage, offsetof(UserRecord, storage), 0 }, + { "diskSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size), 0 }, + { "diskSizeRelative", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, disk_size_relative), 0 }, + { "skeletonDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, skeleton_directory), SD_JSON_SAFE }, + { "accessMode", SD_JSON_VARIANT_UNSIGNED, json_dispatch_access_mode, offsetof(UserRecord, access_mode), 0 }, + { "tasksMax", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, tasks_max), 0 }, + { "memoryHigh", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_high), 0 }, + { "memoryMax", SD_JSON_VARIANT_UNSIGNED, json_dispatch_tasks_or_memory_max, offsetof(UserRecord, memory_max), 0 }, + { "cpuWeight", SD_JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, cpu_weight), 0 }, + { "ioWeight", SD_JSON_VARIANT_UNSIGNED, json_dispatch_weight, offsetof(UserRecord, io_weight), 0 }, + { "mountNoDevices", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, nodev), 0 }, + { "mountNoSuid", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, nosuid), 0 }, + { "mountNoExecute", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(UserRecord, noexec), 0 }, + { "cifsDomain", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_domain), SD_JSON_SAFE }, + { "cifsUserName", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_user_name), SD_JSON_SAFE }, + { "cifsService", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_service), SD_JSON_SAFE }, + { "cifsExtraMountOptions", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, cifs_extra_mount_options), 0 }, + { "imagePath", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(UserRecord, image_path), SD_JSON_SAFE }, + { "homeDirectory", SD_JSON_VARIANT_STRING, json_dispatch_home_directory, offsetof(UserRecord, home_directory), 0 }, + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, uid), 0 }, + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(UserRecord, gid), 0 }, + { "memberOf", SD_JSON_VARIANT_ARRAY, json_dispatch_user_group_list, offsetof(UserRecord, member_of), SD_JSON_RELAX }, + { "capabilityBoundingSet", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, capability_bounding_set), SD_JSON_SAFE }, + { "capabilityAmbientSet", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, capability_ambient_set), SD_JSON_SAFE }, + { "fileSystemType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, file_system_type), SD_JSON_SAFE }, + { "partitionUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, partition_uuid), 0 }, + { "luksUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, luks_uuid), 0 }, + { "fileSystemUuid", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(UserRecord, file_system_uuid), 0 }, + { "luksDiscard", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, luks_discard), 0 }, + { "luksOfflineDiscard", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_tristate, offsetof(UserRecord, luks_offline_discard), 0 }, + { "luksCipher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher), SD_JSON_SAFE }, + { "luksCipherMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_cipher_mode), SD_JSON_SAFE }, + { "luksVolumeKeySize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_volume_key_size), 0 }, + { "luksPbkdfHashAlgorithm", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_pbkdf_hash_algorithm), SD_JSON_SAFE }, + { "luksPbkdfType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_pbkdf_type), SD_JSON_SAFE }, + { "luksPbkdfForceIterations", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_force_iterations), 0 }, + { "luksPbkdfTimeCostUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_time_cost_usec), 0 }, + { "luksPbkdfMemoryCost", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_memory_cost), 0 }, + { "luksPbkdfParallelThreads", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_pbkdf_parallel_threads), 0 }, + { "luksSectorSize", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, luks_sector_size), 0 }, + { "luksExtraMountOptions", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, luks_extra_mount_options), 0 }, + { "dropCaches", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, drop_caches), 0 }, + { "autoResizeMode", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_auto_resize_mode, offsetof(UserRecord, auto_resize_mode), 0 }, + { "rebalanceWeight", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_rebalance_weight, offsetof(UserRecord, rebalance_weight), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, service), SD_JSON_SAFE }, + { "rateLimitIntervalUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_interval_usec), 0 }, + { "rateLimitBurst", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, ratelimit_burst), 0 }, + { "enforcePasswordPolicy", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, enforce_password_policy), 0 }, + { "autoLogin", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, auto_login), 0 }, + { "preferredSessionType", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, preferred_session_type), SD_JSON_SAFE }, + { "preferredSessionLauncher", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(UserRecord, preferred_session_launcher), SD_JSON_SAFE }, + { "stopDelayUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, stop_delay_usec), 0 }, + { "killProcesses", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, kill_processes), 0 }, + { "passwordChangeMinUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_min_usec), 0 }, + { "passwordChangeMaxUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_max_usec), 0 }, + { "passwordChangeWarnUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_warn_usec), 0 }, + { "passwordChangeInactiveUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(UserRecord, password_change_inactive_usec), 0 }, + { "passwordChangeNow", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(UserRecord, password_change_now), 0 }, + { "pkcs11TokenUri", SD_JSON_VARIANT_ARRAY, dispatch_pkcs11_uri_array, offsetof(UserRecord, pkcs11_token_uri), 0 }, + { "fido2HmacCredential", SD_JSON_VARIANT_ARRAY, dispatch_fido2_hmac_credential_array, 0, 0 }, + { "recoveryKeyType", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(UserRecord, recovery_key_type), 0 }, + + { "secret", SD_JSON_VARIANT_OBJECT, dispatch_secret, 0, 0 }, + { "privileged", SD_JSON_VARIANT_OBJECT, dispatch_privileged, 0, 0 }, /* Ignore the perMachine, binding, status stuff here, and process it later, so that it overrides whatever is set above */ - { "perMachine", JSON_VARIANT_ARRAY, NULL, 0, 0 }, - { "binding", JSON_VARIANT_OBJECT, NULL, 0, 0 }, - { "status", JSON_VARIANT_OBJECT, NULL, 0, 0 }, + { "perMachine", SD_JSON_VARIANT_ARRAY, NULL, 0, 0 }, + { "binding", SD_JSON_VARIANT_OBJECT, NULL, 0, 0 }, + { "status", SD_JSON_VARIANT_OBJECT, NULL, 0, 0 }, /* Ignore 'signature', we check it with explicit accessors instead */ - { "signature", JSON_VARIANT_ARRAY, NULL, 0, 0 }, + { "signature", SD_JSON_VARIANT_ARRAY, NULL, 0, 0 }, {}, }; - JsonDispatchFlags json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags); + sd_json_dispatch_flags_t json_flags = USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(load_flags); int r; assert(h); @@ -1700,22 +1701,22 @@ int user_record_load(UserRecord *h, JsonVariant *v, UserRecordLoadFlags load_fla if (r < 0) return r; - r = json_dispatch(h->json, user_dispatch_table, json_flags | JSON_ALLOW_EXTENSIONS, h); + r = sd_json_dispatch(h->json, user_dispatch_table, json_flags | SD_JSON_ALLOW_EXTENSIONS, h); if (r < 0) return r; /* During the parsing operation above we ignored the 'perMachine', 'binding' and 'status' fields, * since we want them to override the global options. Let's process them now. */ - r = dispatch_per_machine("perMachine", json_variant_by_key(h->json, "perMachine"), json_flags, h); + r = dispatch_per_machine("perMachine", sd_json_variant_by_key(h->json, "perMachine"), json_flags, h); if (r < 0) return r; - r = dispatch_binding("binding", json_variant_by_key(h->json, "binding"), json_flags, h); + r = dispatch_binding("binding", sd_json_variant_by_key(h->json, "binding"), json_flags, h); if (r < 0) return r; - r = dispatch_status("status", json_variant_by_key(h->json, "status"), json_flags, h); + r = dispatch_status("status", sd_json_variant_by_key(h->json, "status"), json_flags, h); if (r < 0) return r; @@ -1730,7 +1731,7 @@ int user_record_load(UserRecord *h, JsonVariant *v, UserRecordLoadFlags load_fla } int user_record_build(UserRecord **ret, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *u = NULL; va_list ap; int r; @@ -1738,7 +1739,7 @@ int user_record_build(UserRecord **ret, ...) { assert(ret); va_start(ap, ret); - r = json_buildv(&v, ap); + r = sd_json_buildv(&v, ap); va_end(ap); if (r < 0) @@ -2216,7 +2217,7 @@ bool user_record_equal(UserRecord *a, UserRecord *b) { /* We assume that when a record is modified its JSON data is updated at the same time, hence it's * sufficient to compare the JSON data. */ - return json_variant_equal(a->json, b->json); + return sd_json_variant_equal(a->json, b->json); } bool user_record_compatible(UserRecord *a, UserRecord *b) { diff --git a/src/shared/user-record.h b/src/shared/user-record.h index cca112f5fe..14a71e009a 100644 --- a/src/shared/user-record.h +++ b/src/shared/user-record.h @@ -5,9 +5,9 @@ #include <sys/types.h> #include "sd-id128.h" +#include "sd-json.h" #include "hashmap.h" -#include "json.h" #include "missing_resource.h" #include "time-util.h" @@ -166,9 +166,9 @@ static inline UserRecordMask USER_RECORD_STRIP_MASK(UserRecordLoadFlags f) { return (f >> 21) & _USER_RECORD_MASK_MAX; } -static inline JsonDispatchFlags USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(UserRecordLoadFlags flags) { - return (FLAGS_SET(flags, USER_RECORD_LOG) ? JSON_LOG : 0) | - (FLAGS_SET(flags, USER_RECORD_PERMISSIVE) ? JSON_PERMISSIVE : 0); +static inline sd_json_dispatch_flags_t USER_RECORD_LOAD_FLAGS_TO_JSON_DISPATCH_FLAGS(UserRecordLoadFlags flags) { + return (FLAGS_SET(flags, USER_RECORD_LOG) ? SD_JSON_LOG : 0) | + (FLAGS_SET(flags, USER_RECORD_PERMISSIVE) ? SD_JSON_PERMISSIVE : 0); } typedef struct Pkcs11EncryptedKey { @@ -383,7 +383,7 @@ typedef struct UserRecord { char **capability_bounding_set; char **capability_ambient_set; - JsonVariant *json; + sd_json_variant *json; } UserRecord; UserRecord* user_record_new(void); @@ -392,7 +392,7 @@ UserRecord* user_record_unref(UserRecord *h); DEFINE_TRIVIAL_CLEANUP_FUNC(UserRecord*, user_record_unref); -int user_record_load(UserRecord *h, JsonVariant *v, UserRecordLoadFlags flags); +int user_record_load(UserRecord *h, sd_json_variant *v, UserRecordLoadFlags flags); int user_record_build(UserRecord **ret, ...); const char *user_record_user_name_and_realm(UserRecord *h); @@ -446,15 +446,15 @@ int user_record_test_blocked(UserRecord *h); int user_record_test_password_change_required(UserRecord *h); /* The following six are user by group-record.c, that's why we export them here */ -int json_dispatch_realm(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_gecos(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_user_group_list(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); -int json_dispatch_user_disposition(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata); - -int per_machine_id_match(JsonVariant *ids, JsonDispatchFlags flags); -int per_machine_hostname_match(JsonVariant *hns, JsonDispatchFlags flags); -int per_machine_match(JsonVariant *entry, JsonDispatchFlags flags); -int user_group_record_mangle(JsonVariant *v, UserRecordLoadFlags load_flags, JsonVariant **ret_variant, UserRecordMask *ret_mask); +int json_dispatch_realm(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int json_dispatch_gecos(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int json_dispatch_user_group_list(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int json_dispatch_user_disposition(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); + +int per_machine_id_match(sd_json_variant *ids, sd_json_dispatch_flags_t flags); +int per_machine_hostname_match(sd_json_variant *hns, sd_json_dispatch_flags_t flags); +int per_machine_match(sd_json_variant *entry, sd_json_dispatch_flags_t flags); +int user_group_record_mangle(sd_json_variant *v, UserRecordLoadFlags load_flags, sd_json_variant **ret_variant, UserRecordMask *ret_mask); #define BLOB_DIR_MAX_SIZE (UINT64_C(64) * U64_MB) int suitable_blob_filename(const char *name); diff --git a/src/shared/userdb-dropin.c b/src/shared/userdb-dropin.c index a2d48fa036..9f027d7783 100644 --- a/src/shared/userdb-dropin.c +++ b/src/shared/userdb-dropin.c @@ -17,21 +17,21 @@ static int load_user( UserDBFlags flags, UserRecord **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *u = NULL; bool have_privileged; int r; assert(f); - r = json_parse_file(f, path, 0, &v, NULL, NULL); + r = sd_json_parse_file(f, path, 0, &v, NULL, NULL); if (r < 0) return r; if (FLAGS_SET(flags, USERDB_SUPPRESS_SHADOW) || !path || !(name || uid_is_valid(uid))) have_privileged = false; else { - _cleanup_(json_variant_unrefp) JsonVariant *privileged_v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *privileged_v = NULL; _cleanup_free_ char *d = NULL, *j = NULL; /* Let's load the "privileged" section from a companion file. But only if USERDB_AVOID_SHADOW @@ -56,7 +56,7 @@ static int load_user( return -ENOMEM; } - r = json_parse_file(NULL, j, JSON_PARSE_SENSITIVE, &privileged_v, NULL, NULL); + r = sd_json_parse_file(NULL, j, SD_JSON_PARSE_SENSITIVE, &privileged_v, NULL, NULL); if (ERRNO_IS_NEG_PRIVILEGE(r)) have_privileged = false; else if (r == -ENOENT) @@ -64,7 +64,7 @@ static int load_user( else if (r < 0) return r; else { - r = json_variant_merge_object(&v, privileged_v); + r = sd_json_variant_merge_object(&v, privileged_v); if (r < 0) return r; @@ -169,21 +169,21 @@ static int load_group( UserDBFlags flags, GroupRecord **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(group_record_unrefp) GroupRecord *g = NULL; bool have_privileged; int r; assert(f); - r = json_parse_file(f, path, 0, &v, NULL, NULL); + r = sd_json_parse_file(f, path, 0, &v, NULL, NULL); if (r < 0) return r; if (FLAGS_SET(flags, USERDB_SUPPRESS_SHADOW) || !path || !(name || gid_is_valid(gid))) have_privileged = false; else { - _cleanup_(json_variant_unrefp) JsonVariant *privileged_v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *privileged_v = NULL; _cleanup_free_ char *d = NULL, *j = NULL; r = path_extract_directory(path, &d); @@ -200,7 +200,7 @@ static int load_group( return -ENOMEM; } - r = json_parse_file(NULL, j, JSON_PARSE_SENSITIVE, &privileged_v, NULL, NULL); + r = sd_json_parse_file(NULL, j, SD_JSON_PARSE_SENSITIVE, &privileged_v, NULL, NULL); if (ERRNO_IS_NEG_PRIVILEGE(r)) have_privileged = false; else if (r == -ENOENT) @@ -208,7 +208,7 @@ static int load_group( else if (r < 0) return r; else { - r = json_variant_merge_object(&v, privileged_v); + r = sd_json_variant_merge_object(&v, privileged_v); if (r < 0) return r; diff --git a/src/shared/userdb.c b/src/shared/userdb.c index 75dece3442..caf05302d9 100644 --- a/src/shared/userdb.c +++ b/src/shared/userdb.c @@ -8,6 +8,7 @@ #include "errno-util.h" #include "fd-util.h" #include "format-util.h" +#include "json-util.h" #include "missing_syscall.h" #include "parse-util.h" #include "set.h" @@ -138,12 +139,12 @@ static int userdb_iterator_block_nss_systemd(UserDBIterator *iterator) { } struct user_group_data { - JsonVariant *record; + sd_json_variant *record; bool incomplete; }; static void user_group_data_done(struct user_group_data *d) { - json_variant_unref(d->record); + sd_json_variant_unref(d->record); } struct membership_data { @@ -158,7 +159,7 @@ static void membership_data_done(struct membership_data *d) { static int userdb_on_query_reply( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { @@ -190,16 +191,16 @@ static int userdb_on_query_reply( case LOOKUP_USER: { _cleanup_(user_group_data_done) struct user_group_data user_data = {}; - static const JsonDispatch dispatch_table[] = { - { "record", _JSON_VARIANT_TYPE_INVALID, json_dispatch_variant, offsetof(struct user_group_data, record), 0 }, - { "incomplete", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct user_group_data, incomplete), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "record", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_variant, offsetof(struct user_group_data, record), 0 }, + { "incomplete", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct user_group_data, incomplete), 0 }, {} }; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; assert_se(!iterator->found_user); - r = json_dispatch(parameters, dispatch_table, JSON_ALLOW_EXTENSIONS, &user_data); + r = sd_json_dispatch(parameters, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &user_data); if (r < 0) goto finish; @@ -247,16 +248,16 @@ static int userdb_on_query_reply( case LOOKUP_GROUP: { _cleanup_(user_group_data_done) struct user_group_data group_data = {}; - static const JsonDispatch dispatch_table[] = { - { "record", _JSON_VARIANT_TYPE_INVALID, json_dispatch_variant, offsetof(struct user_group_data, record), 0 }, - { "incomplete", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct user_group_data, incomplete), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "record", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_variant, offsetof(struct user_group_data, record), 0 }, + { "incomplete", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct user_group_data, incomplete), 0 }, {} }; _cleanup_(group_record_unrefp) GroupRecord *g = NULL; assert_se(!iterator->found_group); - r = json_dispatch(parameters, dispatch_table, JSON_ALLOW_EXTENSIONS, &group_data); + r = sd_json_dispatch(parameters, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &group_data); if (r < 0) goto finish; @@ -300,16 +301,16 @@ static int userdb_on_query_reply( case LOOKUP_MEMBERSHIP: { _cleanup_(membership_data_done) struct membership_data membership_data = {}; - static const JsonDispatch dispatch_table[] = { - { "userName", JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(struct membership_data, user_name), JSON_RELAX }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(struct membership_data, group_name), JSON_RELAX }, + static const sd_json_dispatch_field dispatch_table[] = { + { "userName", SD_JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(struct membership_data, user_name), SD_JSON_RELAX }, + { "groupName", SD_JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(struct membership_data, group_name), SD_JSON_RELAX }, {} }; assert(!iterator->found_user_name); assert(!iterator->found_group_name); - r = json_dispatch(parameters, dispatch_table, JSON_ALLOW_EXTENSIONS, &membership_data); + r = sd_json_dispatch(parameters, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &membership_data); if (r < 0) goto finish; @@ -344,7 +345,7 @@ static int userdb_connect( const char *path, const char *method, bool more, - JsonVariant *query) { + sd_json_variant *query) { _cleanup_(varlink_unrefp) Varlink *vl = NULL; int r; @@ -392,7 +393,7 @@ static int userdb_start_query( UserDBIterator *iterator, const char *method, bool more, - JsonVariant *query, + sd_json_variant *query, UserDBFlags flags) { _cleanup_strv_free_ char **except = NULL, **only = NULL; @@ -429,9 +430,9 @@ static int userdb_start_query( if ((flags & (USERDB_AVOID_MULTIPLEXER|USERDB_EXCLUDE_DYNAMIC_USER|USERDB_EXCLUDE_NSS|USERDB_EXCLUDE_DROPIN|USERDB_DONT_SYNTHESIZE)) == 0 && !strv_contains(except, "io.systemd.Multiplexer") && (!only || strv_contains(only, "io.systemd.Multiplexer"))) { - _cleanup_(json_variant_unrefp) JsonVariant *patched_query = json_variant_ref(query); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *patched_query = sd_json_variant_ref(query); - r = json_variant_set_field_string(&patched_query, "service", "io.systemd.Multiplexer"); + r = sd_json_variant_set_field_string(&patched_query, "service", "io.systemd.Multiplexer"); if (r < 0) return log_debug_errno(r, "Unable to set service JSON field: %m"); @@ -452,7 +453,7 @@ static int userdb_start_query( } FOREACH_DIRENT(de, d, return -errno) { - _cleanup_(json_variant_unrefp) JsonVariant *patched_query = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *patched_query = NULL; _cleanup_free_ char *p = NULL; bool is_nss, is_dropin; @@ -486,8 +487,8 @@ static int userdb_start_query( if (!p) return -ENOMEM; - patched_query = json_variant_ref(query); - r = json_variant_set_field_string(&patched_query, "service", de->d_name); + patched_query = sd_json_variant_ref(query); + r = sd_json_variant_set_field_string(&patched_query, "service", de->d_name); if (r < 0) return log_debug_errno(r, "Unable to set service JSON field: %m"); @@ -591,34 +592,34 @@ static int userdb_process( static int synthetic_root_user_build(UserRecord **ret) { return user_record_build( ret, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_CONST_STRING("root")), - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(0)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(0)), - JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/root")), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", JSON_BUILD_CONST_STRING("root")), + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(0)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(0)), + SD_JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/root")), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")))); } static int synthetic_nobody_user_build(UserRecord **ret) { return user_record_build( ret, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("userName", JSON_BUILD_CONST_STRING(NOBODY_USER_NAME)), - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(UID_NOBODY)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(GID_NOBODY)), - JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN)), - JSON_BUILD_PAIR("locked", JSON_BUILD_BOOLEAN(true)), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("userName", JSON_BUILD_CONST_STRING(NOBODY_USER_NAME)), + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(UID_NOBODY)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(GID_NOBODY)), + SD_JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN)), + SD_JSON_BUILD_PAIR("locked", SD_JSON_BUILD_BOOLEAN(true)), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")))); } int userdb_by_name(const char *name, UserDBFlags flags, UserRecord **ret) { _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *query = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *query = NULL; int r; if (!valid_user_group_name(name, VALID_USER_RELAX)) return -EINVAL; - r = json_build(&query, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(name)))); + r = sd_json_build(&query, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(name)))); if (r < 0) return r; @@ -664,14 +665,14 @@ int userdb_by_name(const char *name, UserDBFlags flags, UserRecord **ret) { int userdb_by_uid(uid_t uid, UserDBFlags flags, UserRecord **ret) { _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *query = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *query = NULL; int r; if (!uid_is_valid(uid)) return -EINVAL; - r = json_build(&query, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("uid", JSON_BUILD_UNSIGNED(uid)))); + r = sd_json_build(&query, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("uid", SD_JSON_BUILD_UNSIGNED(uid)))); if (r < 0) return r; @@ -869,29 +870,29 @@ int userdb_iterator_get(UserDBIterator *iterator, UserRecord **ret) { static int synthetic_root_group_build(GroupRecord **ret) { return group_record_build( ret, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("groupName", JSON_BUILD_CONST_STRING("root")), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(0)), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("groupName", JSON_BUILD_CONST_STRING("root")), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(0)), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")))); } static int synthetic_nobody_group_build(GroupRecord **ret) { return group_record_build( ret, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("groupName", JSON_BUILD_CONST_STRING(NOBODY_GROUP_NAME)), - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(GID_NOBODY)), - JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("groupName", JSON_BUILD_CONST_STRING(NOBODY_GROUP_NAME)), + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(GID_NOBODY)), + SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")))); } int groupdb_by_name(const char *name, UserDBFlags flags, GroupRecord **ret) { _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *query = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *query = NULL; int r; if (!valid_user_group_name(name, VALID_USER_RELAX)) return -EINVAL; - r = json_build(&query, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(name)))); + r = sd_json_build(&query, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(name)))); if (r < 0) return r; @@ -935,14 +936,14 @@ int groupdb_by_name(const char *name, UserDBFlags flags, GroupRecord **ret) { int groupdb_by_gid(gid_t gid, UserDBFlags flags, GroupRecord **ret) { _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *query = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *query = NULL; int r; if (!gid_is_valid(gid)) return -EINVAL; - r = json_build(&query, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("gid", JSON_BUILD_UNSIGNED(gid)))); + r = sd_json_build(&query, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("gid", SD_JSON_BUILD_UNSIGNED(gid)))); if (r < 0) return r; @@ -1141,7 +1142,7 @@ static void discover_membership_dropins(UserDBIterator *i, UserDBFlags flags) { int membershipdb_by_user(const char *name, UserDBFlags flags, UserDBIterator **ret) { _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *query = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *query = NULL; int r, qr; assert(ret); @@ -1149,8 +1150,8 @@ int membershipdb_by_user(const char *name, UserDBFlags flags, UserDBIterator **r if (!valid_user_group_name(name, VALID_USER_RELAX)) return -EINVAL; - r = json_build(&query, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(name)))); + r = sd_json_build(&query, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(name)))); if (r < 0) return r; @@ -1187,7 +1188,7 @@ int membershipdb_by_user(const char *name, UserDBFlags flags, UserDBIterator **r int membershipdb_by_group(const char *name, UserDBFlags flags, UserDBIterator **ret) { _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *query = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *query = NULL; int r, qr; assert(ret); @@ -1195,8 +1196,8 @@ int membershipdb_by_group(const char *name, UserDBFlags flags, UserDBIterator ** if (!valid_user_group_name(name, VALID_USER_RELAX)) return -EINVAL; - r = json_build(&query, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(name)))); + r = sd_json_build(&query, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(name)))); if (r < 0) return r; diff --git a/src/shared/varlink-idl.c b/src/shared/varlink-idl.c index 6748343798..42151316cc 100644 --- a/src/shared/varlink-idl.c +++ b/src/shared/varlink-idl.c @@ -1,10 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "json-util.h" #include "memstream-util.h" +#include "set.h" #include "strv.h" #include "terminal-util.h" #include "varlink-idl.h" -#include "set.h" #define DEPTH_MAX 64U @@ -1360,9 +1361,9 @@ int varlink_idl_consistent(const VarlinkInterface *interface, int level) { return 0; } -static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, JsonVariant *v, VarlinkFieldDirection direction, const char **bad_field); +static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, sd_json_variant *v, VarlinkFieldDirection direction, const char **bad_field); -static int varlink_idl_validate_field_element_type(const VarlinkField *field, JsonVariant *v) { +static int varlink_idl_validate_field_element_type(const VarlinkField *field, sd_json_variant *v) { assert(field); switch (field->field_type) { @@ -1373,32 +1374,32 @@ static int varlink_idl_validate_field_element_type(const VarlinkField *field, Js return varlink_idl_validate_symbol(field->symbol, v, VARLINK_REGULAR, NULL); case VARLINK_BOOL: - if (!json_variant_is_boolean(v)) + if (!sd_json_variant_is_boolean(v)) return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Field '%s' should be a bool, but it is not, refusing.", strna(field->name)); break; case VARLINK_INT: /* Allow strings here too, since integers with > 53 bits are often passed in as strings */ - if (!json_variant_is_integer(v) && !json_variant_is_unsigned(v) && !json_variant_is_string(v)) + if (!sd_json_variant_is_integer(v) && !sd_json_variant_is_unsigned(v) && !sd_json_variant_is_string(v)) return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Field '%s' should be an int, but it is not, refusing.", strna(field->name)); break; case VARLINK_FLOAT: - if (!json_variant_is_number(v)) + if (!sd_json_variant_is_number(v)) return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Field '%s' should be a float, but it is not, refusing.", strna(field->name)); break; case VARLINK_STRING: - if (!json_variant_is_string(v)) + if (!sd_json_variant_is_string(v)) return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Field '%s' should be a string, but it is not, refusing.", strna(field->name)); break; case VARLINK_OBJECT: - if (!json_variant_is_object(v)) + if (!sd_json_variant_is_object(v)) return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Field '%s' should be an object, but it is not, refusing.", strna(field->name)); break; @@ -1410,20 +1411,20 @@ static int varlink_idl_validate_field_element_type(const VarlinkField *field, Js return 0; } -static int varlink_idl_validate_field(const VarlinkField *field, JsonVariant *v) { +static int varlink_idl_validate_field(const VarlinkField *field, sd_json_variant *v) { int r; assert(field); - if (!v || json_variant_is_null(v)) { + if (!v || sd_json_variant_is_null(v)) { if (!FLAGS_SET(field->field_flags, VARLINK_NULLABLE)) return varlink_idl_log(SYNTHETIC_ERRNO(ENOANO), "Mandatory field '%s' is null or missing on object, refusing.", strna(field->name)); } else if (FLAGS_SET(field->field_flags, VARLINK_ARRAY)) { - JsonVariant *i; + sd_json_variant *i; - if (!json_variant_is_array(v)) + if (!sd_json_variant_is_array(v)) return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Field '%s' should be an array, but it is not, refusing.", strna(field->name)); JSON_VARIANT_ARRAY_FOREACH(i, v) { @@ -1434,9 +1435,9 @@ static int varlink_idl_validate_field(const VarlinkField *field, JsonVariant *v) } else if (FLAGS_SET(field->field_flags, VARLINK_MAP)) { _unused_ const char *k; - JsonVariant *e; + sd_json_variant *e; - if (!json_variant_is_object(v)) + if (!sd_json_variant_is_object(v)) return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Field '%s' should be an object, but it is not, refusing.", strna(field->name)); JSON_VARIANT_OBJECT_FOREACH(k, e, v) { @@ -1454,7 +1455,7 @@ static int varlink_idl_validate_field(const VarlinkField *field, JsonVariant *v) return 0; } -static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, JsonVariant *v, VarlinkFieldDirection direction, const char **bad_field) { +static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, sd_json_variant *v, VarlinkFieldDirection direction, const char **bad_field) { int r; assert(symbol); @@ -1471,13 +1472,13 @@ static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, JsonVariant bool found = false; const char *s; - if (!json_variant_is_string(v)) { + if (!sd_json_variant_is_string(v)) { if (bad_field) *bad_field = symbol->name; return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Passed non-string to enum field '%s', refusing.", strna(symbol->name)); } - assert_se(s = json_variant_string(v)); + assert_se(s = sd_json_variant_string(v)); for (const VarlinkField *field = symbol->fields; field->field_type != _VARLINK_FIELD_TYPE_END_MARKER; field++) { @@ -1501,7 +1502,7 @@ static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, JsonVariant case VARLINK_STRUCT_TYPE: case VARLINK_METHOD: case VARLINK_ERROR: { - if (!json_variant_is_object(v)) { + if (!sd_json_variant_is_object(v)) { if (bad_field) *bad_field = symbol->name; return varlink_idl_log(SYNTHETIC_ERRNO(EMEDIUMTYPE), "Passed non-object to field '%s', refusing.", strna(symbol->name)); @@ -1512,7 +1513,7 @@ static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, JsonVariant if (field->field_direction != direction) continue; - r = varlink_idl_validate_field(field, json_variant_by_key(v, field->name)); + r = varlink_idl_validate_field(field, sd_json_variant_by_key(v, field->name)); if (r < 0) { if (bad_field) *bad_field = field->name; @@ -1520,7 +1521,7 @@ static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, JsonVariant } } - _unused_ JsonVariant *e; + _unused_ sd_json_variant *e; const char *name; JSON_VARIANT_OBJECT_FOREACH(name, e, v) { if (!varlink_idl_find_field(symbol, name)) { @@ -1540,7 +1541,7 @@ static int varlink_idl_validate_symbol(const VarlinkSymbol *symbol, JsonVariant return 1; /* validated */ } -static int varlink_idl_validate_method(const VarlinkSymbol *method, JsonVariant *v, VarlinkFieldDirection direction, const char **bad_field) { +static int varlink_idl_validate_method(const VarlinkSymbol *method, sd_json_variant *v, VarlinkFieldDirection direction, const char **bad_field) { assert(IN_SET(direction, VARLINK_INPUT, VARLINK_OUTPUT)); if (!method) @@ -1551,15 +1552,15 @@ static int varlink_idl_validate_method(const VarlinkSymbol *method, JsonVariant return varlink_idl_validate_symbol(method, v, direction, bad_field); } -int varlink_idl_validate_method_call(const VarlinkSymbol *method, JsonVariant *v, const char **bad_field) { +int varlink_idl_validate_method_call(const VarlinkSymbol *method, sd_json_variant *v, const char **bad_field) { return varlink_idl_validate_method(method, v, VARLINK_INPUT, bad_field); } -int varlink_idl_validate_method_reply(const VarlinkSymbol *method, JsonVariant *v, const char **bad_field) { +int varlink_idl_validate_method_reply(const VarlinkSymbol *method, sd_json_variant *v, const char **bad_field) { return varlink_idl_validate_method(method, v, VARLINK_OUTPUT, bad_field); } -int varlink_idl_validate_error(const VarlinkSymbol *error, JsonVariant *v, const char **bad_field) { +int varlink_idl_validate_error(const VarlinkSymbol *error, sd_json_variant *v, const char **bad_field) { if (!error) return 0; /* Can't validate */ if (error->symbol_type != VARLINK_ERROR) diff --git a/src/shared/varlink-idl.h b/src/shared/varlink-idl.h index 140b937c0d..f2d4892eee 100644 --- a/src/shared/varlink-idl.h +++ b/src/shared/varlink-idl.h @@ -5,7 +5,7 @@ #include <stdbool.h> #include <stdio.h> -#include "json.h" +#include "sd-json.h" #include "macro.h" /* This implements the Varlink Interface Definition Language ("Varlink IDL"), @@ -153,6 +153,6 @@ int varlink_idl_consistent(const VarlinkInterface *interface, int level); const VarlinkSymbol* varlink_idl_find_symbol(const VarlinkInterface *interface, VarlinkSymbolType type, const char *name); const VarlinkField* varlink_idl_find_field(const VarlinkSymbol *symbol, const char *name); -int varlink_idl_validate_method_call(const VarlinkSymbol *method, JsonVariant *v, const char **bad_field); -int varlink_idl_validate_method_reply(const VarlinkSymbol *method, JsonVariant *v, const char **bad_field); -int varlink_idl_validate_error(const VarlinkSymbol *error, JsonVariant *v, const char **bad_field); +int varlink_idl_validate_method_call(const VarlinkSymbol *method, sd_json_variant *v, const char **bad_field); +int varlink_idl_validate_method_reply(const VarlinkSymbol *method, sd_json_variant *v, const char **bad_field); +int varlink_idl_validate_error(const VarlinkSymbol *error, sd_json_variant *v, const char **bad_field); diff --git a/src/shared/varlink-io.systemd.service.c b/src/shared/varlink-io.systemd.service.c index e9df5de814..7c22c33ea8 100644 --- a/src/shared/varlink-io.systemd.service.c +++ b/src/shared/varlink-io.systemd.service.c @@ -19,10 +19,10 @@ VARLINK_DEFINE_INTERFACE( &vl_method_Reload, &vl_method_SetLogLevel); -int varlink_method_ping(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +int varlink_method_ping(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { assert(link); - if (json_variant_elements(parameters) > 0) + if (sd_json_variant_elements(parameters) > 0) return varlink_error_invalid_parameter(link, parameters); log_debug("Received io.systemd.service.Ping"); @@ -30,9 +30,9 @@ int varlink_method_ping(Varlink *link, JsonVariant *parameters, VarlinkMethodFla return varlink_reply(link, NULL); } -int varlink_method_set_log_level(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "level", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int64, 0, JSON_MANDATORY }, +int varlink_method_set_log_level(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "level", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int64, 0, SD_JSON_MANDATORY }, {} }; @@ -44,8 +44,8 @@ int varlink_method_set_log_level(Varlink *link, JsonVariant *parameters, Varlink assert(parameters); /* NOTE: The method does have 1 parameter, but we must compare to 2 here, because - * json_variant_elements() breaks abstraction and exposes internal structure of JsonObject. */ - if (json_variant_elements(parameters) != 2) + * sd_json_variant_elements() breaks abstraction and exposes internal structure of JsonObject. */ + if (sd_json_variant_elements(parameters) != 2) return varlink_error_invalid_parameter(link, parameters); r = varlink_dispatch(link, parameters, dispatch_table, &level); diff --git a/src/shared/varlink-io.systemd.service.h b/src/shared/varlink-io.systemd.service.h index bc90ff0986..286140b75a 100644 --- a/src/shared/varlink-io.systemd.service.h +++ b/src/shared/varlink-io.systemd.service.h @@ -6,5 +6,5 @@ extern const VarlinkInterface vl_interface_io_systemd_service; -int varlink_method_ping(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); -int varlink_method_set_log_level(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); +int varlink_method_ping(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); +int varlink_method_set_log_level(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); diff --git a/src/shared/varlink.c b/src/shared/varlink.c index 034e72b1db..74053d6cec 100644 --- a/src/shared/varlink.c +++ b/src/shared/varlink.c @@ -12,6 +12,7 @@ #include "hashmap.h" #include "io-util.h" #include "iovec-util.h" +#include "json-util.h" #include "list.h" #include "path-util.h" #include "process-util.h" @@ -27,8 +28,8 @@ #include "user-util.h" #include "varlink.h" #include "varlink-internal.h" -#include "varlink-org.varlink.service.h" #include "varlink-io.systemd.h" +#include "varlink-org.varlink.service.h" #include "version.h" #define VARLINK_DEFAULT_CONNECTIONS_MAX 4096U @@ -100,7 +101,7 @@ typedef struct VarlinkJsonQueueItem VarlinkJsonQueueItem; * whole pipeline. */ struct VarlinkJsonQueueItem { LIST_FIELDS(VarlinkJsonQueueItem, queue); - JsonVariant *data; + sd_json_variant *data; size_t n_fds; int fds[]; }; @@ -173,8 +174,8 @@ struct Varlink { VarlinkReply reply_callback; - JsonVariant *current; - JsonVariant *current_collected; + sd_json_variant *current; + sd_json_variant *current_collected; VarlinkReplyFlags current_reply_flags; VarlinkSymbol *current_method; @@ -303,13 +304,13 @@ static VarlinkJsonQueueItem *varlink_json_queue_item_free(VarlinkJsonQueueItem * if (!q) return NULL; - json_variant_unref(q->data); + sd_json_variant_unref(q->data); close_many(q->fds, q->n_fds); return mfree(q); } -static VarlinkJsonQueueItem *varlink_json_queue_item_new(JsonVariant *m, const int fds[], size_t n_fds) { +static VarlinkJsonQueueItem *varlink_json_queue_item_new(sd_json_variant *m, const int fds[], size_t n_fds) { VarlinkJsonQueueItem *q; assert(m); @@ -320,7 +321,7 @@ static VarlinkJsonQueueItem *varlink_json_queue_item_new(JsonVariant *m, const i return NULL; *q = (VarlinkJsonQueueItem) { - .data = json_variant_ref(m), + .data = sd_json_variant_ref(m), .n_fds = n_fds, }; @@ -690,8 +691,8 @@ static void varlink_clear_current(Varlink *v) { assert(v); /* Clears the currently processed incoming message */ - v->current = json_variant_unref(v->current); - v->current_collected = json_variant_unref(v->current_collected); + v->current = sd_json_variant_unref(v->current); + v->current_collected = sd_json_variant_unref(v->current_collected); v->current_method = NULL; v->current_reply_flags = 0; @@ -1053,7 +1054,7 @@ static int varlink_parse_message(Varlink *v) { sz = e - begin + 1; - r = json_parse(begin, 0, &v->current, NULL, NULL); + r = sd_json_parse(begin, 0, &v->current, NULL, NULL); if (v->input_sensitive) explicit_bzero_safe(begin, sz); if (r < 0) { @@ -1065,16 +1066,16 @@ static int varlink_parse_message(Varlink *v) { if (v->input_sensitive) { /* Mark the parameters subfield as sensitive right-away, if that's requested */ - JsonVariant *parameters = json_variant_by_key(v->current, "parameters"); + sd_json_variant *parameters = sd_json_variant_by_key(v->current, "parameters"); if (parameters) - json_variant_sensitive(parameters); + sd_json_variant_sensitive(parameters); } if (DEBUG_LOGGING) { _cleanup_(erase_and_freep) char *censored_text = NULL; /* Suppress sensitive fields in the debug output */ - r = json_variant_format(v->current, /* flags= */ JSON_FORMAT_CENSOR_SENSITIVE, &censored_text); + r = sd_json_variant_format(v->current, /* flags= */ SD_JSON_FORMAT_CENSOR_SENSITIVE, &censored_text); if (r < 0) return r; @@ -1150,36 +1151,36 @@ static int varlink_dispatch_disconnect(Varlink *v) { return 1; } -static int varlink_sanitize_parameters(JsonVariant **v) { +static int varlink_sanitize_parameters(sd_json_variant **v) { int r; assert(v); /* Varlink always wants a parameters list, hence make one if the caller doesn't want any */ if (!*v) - return json_variant_new_object(v, NULL, 0); - if (json_variant_is_null(*v)) { - JsonVariant *empty; + return sd_json_variant_new_object(v, NULL, 0); + if (sd_json_variant_is_null(*v)) { + sd_json_variant *empty; - r = json_variant_new_object(&empty, NULL, 0); + r = sd_json_variant_new_object(&empty, NULL, 0); if (r < 0) return r; - json_variant_unref(*v); + sd_json_variant_unref(*v); *v = empty; return 0; } - if (!json_variant_is_object(*v)) + if (!sd_json_variant_is_object(*v)) return -EINVAL; return 0; } static int varlink_dispatch_reply(Varlink *v) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; VarlinkReplyFlags flags = 0; const char *error = NULL; - JsonVariant *e; + sd_json_variant *e; const char *k; int r; @@ -1192,7 +1193,7 @@ static int varlink_dispatch_reply(Varlink *v) { assert(v->n_pending > 0); - if (!json_variant_is_object(v->current)) + if (!sd_json_variant_is_object(v->current)) goto invalid; JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { @@ -1200,28 +1201,28 @@ static int varlink_dispatch_reply(Varlink *v) { if (streq(k, "error")) { if (error) goto invalid; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) goto invalid; - error = json_variant_string(e); + error = sd_json_variant_string(e); flags |= VARLINK_REPLY_ERROR; } else if (streq(k, "parameters")) { if (parameters) goto invalid; - if (!json_variant_is_object(e) && !json_variant_is_null(e)) + if (!sd_json_variant_is_object(e) && !sd_json_variant_is_null(e)) goto invalid; - parameters = json_variant_ref(e); + parameters = sd_json_variant_ref(e); } else if (streq(k, "continues")) { if (FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) goto invalid; - if (!json_variant_is_boolean(e)) + if (!sd_json_variant_is_boolean(e)) goto invalid; - if (json_variant_boolean(e)) + if (sd_json_variant_boolean(e)) flags |= VARLINK_REPLY_CONTINUES; } else goto invalid; @@ -1281,7 +1282,7 @@ invalid: static int generic_method_get_info( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { @@ -1291,7 +1292,7 @@ static int generic_method_get_info( assert(link); - if (json_variant_elements(parameters) != 0) + if (sd_json_variant_elements(parameters) != 0) return varlink_error_invalid_parameter(link, parameters); product = strjoin("systemd (", program_invocation_short_name, ")"); @@ -1307,22 +1308,22 @@ static int generic_method_get_info( strv_sort(interfaces); - return varlink_replyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("vendor", "The systemd Project"), - JSON_BUILD_PAIR_STRING("product", product), - JSON_BUILD_PAIR_STRING("version", PROJECT_VERSION_FULL " (" GIT_VERSION ")"), - JSON_BUILD_PAIR_STRING("url", "https://systemd.io/"), - JSON_BUILD_PAIR_STRV("interfaces", interfaces))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("vendor", "The systemd Project"), + SD_JSON_BUILD_PAIR_STRING("product", product), + SD_JSON_BUILD_PAIR_STRING("version", PROJECT_VERSION_FULL " (" GIT_VERSION ")"), + SD_JSON_BUILD_PAIR_STRING("url", "https://systemd.io/"), + SD_JSON_BUILD_PAIR_STRV("interfaces", interfaces))); } static int generic_method_get_interface_description( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const struct JsonDispatch dispatch_table[] = { - { "interface", JSON_VARIANT_STRING, json_dispatch_const_string, 0, JSON_MANDATORY }, + static const struct sd_json_dispatch_field dispatch_table[] = { + { "interface", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, 0, SD_JSON_MANDATORY }, {} }; _cleanup_free_ char *text = NULL; @@ -1332,30 +1333,30 @@ static int generic_method_get_interface_description( assert(link); - r = json_dispatch(parameters, dispatch_table, 0, &name); + r = sd_json_dispatch(parameters, dispatch_table, 0, &name); if (r < 0) return r; interface = hashmap_get(ASSERT_PTR(link->server)->interfaces, name); if (!interface) return varlink_errorb(link, VARLINK_ERROR_INTERFACE_NOT_FOUND, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("interface", name))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("interface", name))); r = varlink_idl_format(interface, &text); if (r < 0) return r; return varlink_replyb(link, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("description", text))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("description", text))); } static int varlink_dispatch_method(Varlink *v) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; VarlinkMethodFlags flags = 0; const char *method = NULL; - JsonVariant *e; + sd_json_variant *e; VarlinkMethod callback; const char *k; int r; @@ -1367,7 +1368,7 @@ static int varlink_dispatch_method(Varlink *v) { if (!v->current) return 0; - if (!json_variant_is_object(v->current)) + if (!sd_json_variant_is_object(v->current)) goto invalid; JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { @@ -1375,28 +1376,28 @@ static int varlink_dispatch_method(Varlink *v) { if (streq(k, "method")) { if (method) goto invalid; - if (!json_variant_is_string(e)) + if (!sd_json_variant_is_string(e)) goto invalid; - method = json_variant_string(e); + method = sd_json_variant_string(e); } else if (streq(k, "parameters")) { if (parameters) goto invalid; - if (!json_variant_is_object(e) && !json_variant_is_null(e)) + if (!sd_json_variant_is_object(e) && !sd_json_variant_is_null(e)) goto invalid; - parameters = json_variant_ref(e); + parameters = sd_json_variant_ref(e); } else if (streq(k, "oneway")) { if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) goto invalid; - if (!json_variant_is_boolean(e)) + if (!sd_json_variant_is_boolean(e)) goto invalid; - if (json_variant_boolean(e)) + if (sd_json_variant_boolean(e)) flags |= VARLINK_METHOD_ONEWAY; } else if (streq(k, "more")) { @@ -1404,10 +1405,10 @@ static int varlink_dispatch_method(Varlink *v) { if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) goto invalid; - if (!json_variant_is_boolean(e)) + if (!sd_json_variant_is_boolean(e)) goto invalid; - if (json_variant_boolean(e)) + if (sd_json_variant_boolean(e)) flags |= VARLINK_METHOD_MORE; } else @@ -1474,7 +1475,7 @@ static int varlink_dispatch_method(Varlink *v) { } } } else if (IN_SET(v->state, VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE)) { - r = varlink_errorb(v, VARLINK_ERROR_METHOD_NOT_FOUND, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)))); + r = varlink_errorb(v, VARLINK_ERROR_METHOD_NOT_FOUND, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("method", SD_JSON_BUILD_STRING(method)))); if (r < 0) return r; } @@ -1619,20 +1620,20 @@ int varlink_dispatch_again(Varlink *v) { return 0; } -int varlink_get_current_parameters(Varlink *v, JsonVariant **ret) { - JsonVariant *p; +int varlink_get_current_parameters(Varlink *v, sd_json_variant **ret) { + sd_json_variant *p; assert_return(v, -EINVAL); if (!v->current) return -ENODATA; - p = json_variant_by_key(v->current, "parameters"); + p = sd_json_variant_by_key(v->current, "parameters"); if (!p) return -ENODATA; if (ret) - *ret = json_variant_ref(p); + *ret = sd_json_variant_ref(p); return 0; } @@ -1879,14 +1880,14 @@ Varlink* varlink_flush_close_unref(Varlink *v) { return varlink_close_unref(v); } -static int varlink_format_json(Varlink *v, JsonVariant *m) { +static int varlink_format_json(Varlink *v, sd_json_variant *m) { _cleanup_(erase_and_freep) char *text = NULL; int sz, r; assert(v); assert(m); - sz = json_variant_format(m, /* flags= */ 0, &text); + sz = sd_json_variant_format(m, /* flags= */ 0, &text); if (sz < 0) return sz; assert(text[sz] == '\0'); @@ -1898,7 +1899,7 @@ static int varlink_format_json(Varlink *v, JsonVariant *m) { _cleanup_(erase_and_freep) char *censored_text = NULL; /* Suppress sensitive fields in the debug output */ - r = json_variant_format(m, /* flags= */ JSON_FORMAT_CENSOR_SENSITIVE, &censored_text); + r = sd_json_variant_format(m, SD_JSON_FORMAT_CENSOR_SENSITIVE, &censored_text); if (r < 0) return r; @@ -1934,7 +1935,7 @@ static int varlink_format_json(Varlink *v, JsonVariant *m) { v->output_buffer_index = 0; } - if (json_variant_is_sensitive_recursive(m)) + if (sd_json_variant_is_sensitive_recursive(m)) v->output_buffer_sensitive = true; /* Propagate sensitive flag */ else text = mfree(text); /* No point in the erase_and_free() destructor declared above */ @@ -1942,7 +1943,7 @@ static int varlink_format_json(Varlink *v, JsonVariant *m) { return 0; } -static int varlink_enqueue_json(Varlink *v, JsonVariant *m) { +static int varlink_enqueue_json(Varlink *v, sd_json_variant *m) { VarlinkJsonQueueItem *q; assert(v); @@ -2006,8 +2007,8 @@ static int varlink_format_queue(Varlink *v) { return 0; } -int varlink_send(Varlink *v, const char *method, JsonVariant *parameters) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +int varlink_send(Varlink *v, const char *method, sd_json_variant *parameters) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; assert_return(v, -EINVAL); @@ -2024,10 +2025,10 @@ int varlink_send(Varlink *v, const char *method, JsonVariant *parameters) { if (r < 0) return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); - r = json_build(&m, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), - JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), - JSON_BUILD_PAIR("oneway", JSON_BUILD_BOOLEAN(true)))); + r = sd_json_build(&m, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("method", SD_JSON_BUILD_STRING(method)), + SD_JSON_BUILD_PAIR("parameters", SD_JSON_BUILD_VARIANT(parameters)), + SD_JSON_BUILD_PAIR("oneway", SD_JSON_BUILD_BOOLEAN(true)))); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2041,14 +2042,14 @@ int varlink_send(Varlink *v, const char *method, JsonVariant *parameters) { } int varlink_sendb(Varlink *v, const char *method, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; va_list ap; int r; assert_return(v, -EINVAL); va_start(ap, method); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); va_end(ap); if (r < 0) @@ -2057,8 +2058,8 @@ int varlink_sendb(Varlink *v, const char *method, ...) { return varlink_send(v, method, parameters); } -int varlink_invoke(Varlink *v, const char *method, JsonVariant *parameters) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +int varlink_invoke(Varlink *v, const char *method, sd_json_variant *parameters) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; assert_return(v, -EINVAL); @@ -2075,9 +2076,9 @@ int varlink_invoke(Varlink *v, const char *method, JsonVariant *parameters) { if (r < 0) return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); - r = json_build(&m, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), - JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); + r = sd_json_build(&m, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("method", SD_JSON_BUILD_STRING(method)), + SD_JSON_BUILD_PAIR("parameters", SD_JSON_BUILD_VARIANT(parameters)))); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2093,14 +2094,14 @@ int varlink_invoke(Varlink *v, const char *method, JsonVariant *parameters) { } int varlink_invokeb(Varlink *v, const char *method, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; va_list ap; int r; assert_return(v, -EINVAL); va_start(ap, method); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); va_end(ap); if (r < 0) @@ -2109,8 +2110,8 @@ int varlink_invokeb(Varlink *v, const char *method, ...) { return varlink_invoke(v, method, parameters); } -int varlink_observe(Varlink *v, const char *method, JsonVariant *parameters) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +int varlink_observe(Varlink *v, const char *method, sd_json_variant *parameters) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; assert_return(v, -EINVAL); @@ -2128,10 +2129,10 @@ int varlink_observe(Varlink *v, const char *method, JsonVariant *parameters) { if (r < 0) return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); - r = json_build(&m, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), - JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), - JSON_BUILD_PAIR("more", JSON_BUILD_BOOLEAN(true)))); + r = sd_json_build(&m, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("method", SD_JSON_BUILD_STRING(method)), + SD_JSON_BUILD_PAIR("parameters", SD_JSON_BUILD_VARIANT(parameters)), + SD_JSON_BUILD_PAIR("more", SD_JSON_BUILD_BOOLEAN(true)))); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2147,14 +2148,14 @@ int varlink_observe(Varlink *v, const char *method, JsonVariant *parameters) { } int varlink_observeb(Varlink *v, const char *method, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; va_list ap; int r; assert_return(v, -EINVAL); va_start(ap, method); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); va_end(ap); if (r < 0) @@ -2166,12 +2167,12 @@ int varlink_observeb(Varlink *v, const char *method, ...) { int varlink_call_full( Varlink *v, const char *method, - JsonVariant *parameters, - JsonVariant **ret_parameters, + sd_json_variant *parameters, + sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; assert_return(v, -EINVAL); @@ -2192,9 +2193,9 @@ int varlink_call_full( if (r < 0) return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); - r = json_build(&m, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), - JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); + r = sd_json_build(&m, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("method", SD_JSON_BUILD_STRING(method)), + SD_JSON_BUILD_PAIR("parameters", SD_JSON_BUILD_VARIANT(parameters)))); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2227,17 +2228,17 @@ int varlink_call_full( assert(v->n_pending == 1); v->n_pending--; - JsonVariant *e = json_variant_by_key(v->current, "error"), - *p = json_variant_by_key(v->current, "parameters"); + sd_json_variant *e = sd_json_variant_by_key(v->current, "error"), + *p = sd_json_variant_by_key(v->current, "parameters"); /* If caller doesn't ask for the error string, then let's return an error code in case of failure */ if (!ret_error_id && e) - return varlink_error_to_errno(json_variant_string(e), p); + return varlink_error_to_errno(sd_json_variant_string(e), p); if (ret_parameters) *ret_parameters = p; if (ret_error_id) - *ret_error_id = e ? json_variant_string(e) : NULL; + *ret_error_id = e ? sd_json_variant_string(e) : NULL; if (ret_flags) *ret_flags = v->current_reply_flags; @@ -2259,18 +2260,18 @@ int varlink_call_full( int varlink_callb_ap( Varlink *v, const char *method, - JsonVariant **ret_parameters, + sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags, va_list ap) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; int r; assert_return(v, -EINVAL); assert_return(method, -EINVAL); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2280,10 +2281,10 @@ int varlink_callb_ap( int varlink_call_and_log( Varlink *v, const char *method, - JsonVariant *parameters, - JsonVariant **ret_parameters) { + sd_json_variant *parameters, + sd_json_variant **ret_parameters) { - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; const char *error_id = NULL; int r; @@ -2306,10 +2307,10 @@ int varlink_call_and_log( int varlink_callb_and_log( Varlink *v, const char *method, - JsonVariant **ret_parameters, + sd_json_variant **ret_parameters, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; va_list ap; int r; @@ -2317,7 +2318,7 @@ int varlink_callb_and_log( assert_return(method, -EINVAL); va_start(ap, ret_parameters); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); va_end(ap); if (r < 0) return log_error_errno(r, "Failed to build JSON message: %m"); @@ -2328,12 +2329,12 @@ int varlink_callb_and_log( int varlink_collect_full( Varlink *v, const char *method, - JsonVariant *parameters, - JsonVariant **ret_parameters, + sd_json_variant *parameters, + sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL, *collected = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL, *collected = NULL; int r; assert_return(v, -EINVAL); @@ -2354,10 +2355,10 @@ int varlink_collect_full( if (r < 0) return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); - r = json_build(&m, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), - JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), - JSON_BUILD_PAIR("more", JSON_BUILD_BOOLEAN(true)))); + r = sd_json_build(&m, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("method", SD_JSON_BUILD_STRING(method)), + SD_JSON_BUILD_PAIR("parameters", SD_JSON_BUILD_VARIANT(parameters)), + SD_JSON_BUILD_PAIR("more", SD_JSON_BUILD_BOOLEAN(true)))); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2387,8 +2388,8 @@ int varlink_collect_full( case VARLINK_COLLECTING_REPLY: { assert(v->current); - JsonVariant *e = json_variant_by_key(v->current, "error"), - *p = json_variant_by_key(v->current, "parameters"); + sd_json_variant *e = sd_json_variant_by_key(v->current, "error"), + *p = sd_json_variant_by_key(v->current, "parameters"); /* Unless there is more to collect we reset state to idle */ if (!FLAGS_SET(v->current_reply_flags, VARLINK_REPLY_CONTINUES)) { @@ -2399,22 +2400,22 @@ int varlink_collect_full( if (e) { if (!ret_error_id) - return varlink_error_to_errno(json_variant_string(e), p); + return varlink_error_to_errno(sd_json_variant_string(e), p); if (ret_parameters) *ret_parameters = p; if (ret_error_id) - *ret_error_id = json_variant_string(e); + *ret_error_id = sd_json_variant_string(e); if (ret_flags) *ret_flags = v->current_reply_flags; return 1; } - if (json_variant_elements(collected) >= VARLINK_COLLECT_MAX) - return varlink_log_errno(v, SYNTHETIC_ERRNO(E2BIG), "Number of reply messages grew too large (%zu) while collecting.", json_variant_elements(collected)); + if (sd_json_variant_elements(collected) >= VARLINK_COLLECT_MAX) + return varlink_log_errno(v, SYNTHETIC_ERRNO(E2BIG), "Number of reply messages grew too large (%zu) while collecting.", sd_json_variant_elements(collected)); - r = json_variant_append_array(&collected, p); + r = sd_json_variant_append_array(&collected, p); if (r < 0) return varlink_log_errno(v, r, "Failed to append JSON object to array: %m"); @@ -2454,18 +2455,18 @@ int varlink_collect_full( int varlink_collectb( Varlink *v, const char *method, - JsonVariant **ret_parameters, + sd_json_variant **ret_parameters, const char **ret_error_id, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; va_list ap; int r; assert_return(v, -EINVAL); va_start(ap, ret_error_id); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); va_end(ap); if (r < 0) @@ -2474,8 +2475,8 @@ int varlink_collectb( return varlink_collect_full(v, method, parameters, ret_parameters, ret_error_id, NULL); } -int varlink_reply(Varlink *v, JsonVariant *parameters) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +int varlink_reply(Varlink *v, sd_json_variant *parameters) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; assert_return(v, -EINVAL); @@ -2491,7 +2492,7 @@ int varlink_reply(Varlink *v, JsonVariant *parameters) { if (r < 0) return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); - r = json_build(&m, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); + r = sd_json_build(&m, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("parameters", SD_JSON_BUILD_VARIANT(parameters)))); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2524,14 +2525,14 @@ int varlink_reply(Varlink *v, JsonVariant *parameters) { } int varlink_replyb(Varlink *v, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; va_list ap; int r; assert_return(v, -EINVAL); va_start(ap, v); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); va_end(ap); if (r < 0) @@ -2540,8 +2541,8 @@ int varlink_replyb(Varlink *v, ...) { return varlink_reply(v, parameters); } -int varlink_error(Varlink *v, const char *error_id, JsonVariant *parameters) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +int varlink_error(Varlink *v, const char *error_id, sd_json_variant *parameters) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; assert_return(v, -EINVAL); @@ -2565,9 +2566,9 @@ int varlink_error(Varlink *v, const char *error_id, JsonVariant *parameters) { if (r < 0) return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); - r = json_build(&m, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("error", JSON_BUILD_STRING(error_id)), - JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); + r = sd_json_build(&m, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("error", SD_JSON_BUILD_STRING(error_id)), + SD_JSON_BUILD_PAIR("parameters", SD_JSON_BUILD_VARIANT(parameters)))); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2598,7 +2599,7 @@ int varlink_error(Varlink *v, const char *error_id, JsonVariant *parameters) { } int varlink_errorb(Varlink *v, const char *error_id, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; va_list ap; int r; @@ -2606,7 +2607,7 @@ int varlink_errorb(Varlink *v, const char *error_id, ...) { assert_return(error_id, -EINVAL); va_start(ap, error_id); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); va_end(ap); if (r < 0) @@ -2615,7 +2616,7 @@ int varlink_errorb(Varlink *v, const char *error_id, ...) { return varlink_error(v, error_id, parameters); } -int varlink_error_invalid_parameter(Varlink *v, JsonVariant *parameters) { +int varlink_error_invalid_parameter(Varlink *v, sd_json_variant *parameters) { int r; assert_return(v, -EINVAL); @@ -2629,25 +2630,25 @@ int varlink_error_invalid_parameter(Varlink *v, JsonVariant *parameters) { /* varlink_error(...) expects a json object as the third parameter. Passing a string variant causes * parameter sanitization to fail, and it returns -EINVAL. */ - if (json_variant_is_string(parameters)) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters_obj = NULL; + if (sd_json_variant_is_string(parameters)) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters_obj = NULL; - r = json_build(¶meters_obj, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("parameter", JSON_BUILD_VARIANT(parameters)))); + r = sd_json_build(¶meters_obj, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("parameter", SD_JSON_BUILD_VARIANT(parameters)))); if (r < 0) return r; return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, parameters_obj); } - if (json_variant_is_object(parameters) && - json_variant_elements(parameters) > 0) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters_obj = NULL; + if (sd_json_variant_is_object(parameters) && + sd_json_variant_elements(parameters) > 0) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters_obj = NULL; - r = json_build(¶meters_obj, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("parameter", JSON_BUILD_VARIANT(json_variant_by_index(parameters, 0))))); + r = sd_json_build(¶meters_obj, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("parameter", SD_JSON_BUILD_VARIANT(sd_json_variant_by_index(parameters, 0))))); if (r < 0) return r; @@ -2661,18 +2662,18 @@ int varlink_error_invalid_parameter_name(Varlink *v, const char *name) { return varlink_errorb( v, VARLINK_ERROR_INVALID_PARAMETER, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("parameter", JSON_BUILD_STRING(name)))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("parameter", SD_JSON_BUILD_STRING(name)))); } int varlink_error_errno(Varlink *v, int error) { return varlink_errorb( v, VARLINK_ERROR_SYSTEM, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR("errno", JSON_BUILD_INTEGER(abs(error))))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("errno", SD_JSON_BUILD_INTEGER(abs(error))))); } -int varlink_notify(Varlink *v, JsonVariant *parameters) { - _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; +int varlink_notify(Varlink *v, sd_json_variant *parameters) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL; int r; assert_return(v, -EINVAL); @@ -2692,9 +2693,9 @@ int varlink_notify(Varlink *v, JsonVariant *parameters) { if (r < 0) return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); - r = json_build(&m, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), - JSON_BUILD_PAIR("continues", JSON_BUILD_BOOLEAN(true)))); + r = sd_json_build(&m, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("parameters", SD_JSON_BUILD_VARIANT(parameters)), + SD_JSON_BUILD_PAIR("continues", SD_JSON_BUILD_BOOLEAN(true)))); if (r < 0) return varlink_log_errno(v, r, "Failed to build json message: %m"); @@ -2717,14 +2718,14 @@ int varlink_notify(Varlink *v, JsonVariant *parameters) { } int varlink_notifyb(Varlink *v, ...) { - _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; va_list ap; int r; assert_return(v, -EINVAL); va_start(ap, v); - r = json_buildv(¶meters, ap); + r = sd_json_buildv(¶meters, ap); va_end(ap); if (r < 0) @@ -2733,7 +2734,7 @@ int varlink_notifyb(Varlink *v, ...) { return varlink_notify(v, parameters); } -int varlink_dispatch(Varlink *v, JsonVariant *parameters, const JsonDispatch table[], void *userdata) { +int varlink_dispatch(Varlink *v, sd_json_variant *parameters, const sd_json_dispatch_field table[], void *userdata) { const char *bad_field = NULL; int r; @@ -2742,7 +2743,7 @@ int varlink_dispatch(Varlink *v, JsonVariant *parameters, const JsonDispatch tab /* A wrapper around json_dispatch_full() that returns a nice InvalidParameter error if we hit a problem with some field. */ - r = json_dispatch_full(parameters, table, /* bad= */ NULL, /* flags= */ 0, userdata, &bad_field); + r = sd_json_dispatch_full(parameters, table, /* bad= */ NULL, /* flags= */ 0, userdata, &bad_field); if (r < 0) { if (bad_field) return varlink_error_invalid_parameter_name(v, bad_field); @@ -4116,7 +4117,7 @@ int varlink_invocation(VarlinkInvocationFlags flags) { return true; } -int varlink_error_to_errno(const char *error, JsonVariant *parameters) { +int varlink_error_to_errno(const char *error, sd_json_variant *parameters) { static const struct { const char *error; int value; @@ -4140,13 +4141,13 @@ int varlink_error_to_errno(const char *error, JsonVariant *parameters) { return t->value; if (streq(error, VARLINK_ERROR_SYSTEM) && parameters) { - JsonVariant *e; + sd_json_variant *e; - e = json_variant_by_key(parameters, "errno"); - if (json_variant_is_integer(e)) { + e = sd_json_variant_by_key(parameters, "errno"); + if (sd_json_variant_is_integer(e)) { int64_t i; - i = json_variant_integer(e); + i = sd_json_variant_integer(e); if (i > 0 && i < ERRNO_MAX) return -i; } diff --git a/src/shared/varlink.h b/src/shared/varlink.h index f6dda63549..6999f16380 100644 --- a/src/shared/varlink.h +++ b/src/shared/varlink.h @@ -3,7 +3,7 @@ #include "sd-event.h" -#include "json.h" +#include "sd-json.h" #include "pidref.h" #include "time-util.h" #include "varlink-idl.h" @@ -51,8 +51,8 @@ typedef enum VarlinkServerFlags { _VARLINK_SERVER_FLAGS_ALL = (1 << 5) - 1, } VarlinkServerFlags; -typedef int (*VarlinkMethod)(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata); -typedef int (*VarlinkReply)(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata); +typedef int (*VarlinkMethod)(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata); +typedef int (*VarlinkReply)(Varlink *link, sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata); typedef int (*VarlinkConnect)(VarlinkServer *server, Varlink *link, void *userdata); typedef void (*VarlinkDisconnect)(VarlinkServer *server, Varlink *link, void *userdata); @@ -84,18 +84,18 @@ Varlink* varlink_flush_close_unref(Varlink *v); Varlink* varlink_close_unref(Varlink *v); /* Enqueue method call, not expecting a reply */ -int varlink_send(Varlink *v, const char *method, JsonVariant *parameters); +int varlink_send(Varlink *v, const char *method, sd_json_variant *parameters); int varlink_sendb(Varlink *v, const char *method, ...); /* Send method call and wait for reply */ -int varlink_call_full(Varlink *v, const char *method, JsonVariant *parameters, JsonVariant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags); -static inline int varlink_call(Varlink *v, const char *method, JsonVariant *parameters, JsonVariant **ret_parameters, const char **ret_error_id) { +int varlink_call_full(Varlink *v, const char *method, sd_json_variant *parameters, sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags); +static inline int varlink_call(Varlink *v, const char *method, sd_json_variant *parameters, sd_json_variant **ret_parameters, const char **ret_error_id) { return varlink_call_full(v, method, parameters, ret_parameters, ret_error_id, NULL); } -int varlink_call_and_log(Varlink *v, const char *method, JsonVariant *parameters, JsonVariant **ret_parameters); +int varlink_call_and_log(Varlink *v, const char *method, sd_json_variant *parameters, sd_json_variant **ret_parameters); -int varlink_callb_ap(Varlink *v, const char *method, JsonVariant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags, va_list ap); -static inline int varlink_callb_full(Varlink *v, const char *method, JsonVariant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags, ...) { +int varlink_callb_ap(Varlink *v, const char *method, sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags, va_list ap); +static inline int varlink_callb_full(Varlink *v, const char *method, sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags, ...) { va_list ap; int r; @@ -104,7 +104,7 @@ static inline int varlink_callb_full(Varlink *v, const char *method, JsonVariant va_end(ap); return r; } -static inline int varlink_callb(Varlink *v, const char *method, JsonVariant **ret_parameters, const char **ret_error_id, ...) { +static inline int varlink_callb(Varlink *v, const char *method, sd_json_variant **ret_parameters, const char **ret_error_id, ...) { va_list ap; int r; @@ -113,46 +113,46 @@ static inline int varlink_callb(Varlink *v, const char *method, JsonVariant **re va_end(ap); return r; } -int varlink_callb_and_log(Varlink *v, const char *method, JsonVariant **ret_parameters, ...); +int varlink_callb_and_log(Varlink *v, const char *method, sd_json_variant **ret_parameters, ...); /* Send method call and begin collecting all 'more' replies into an array, finishing when a final reply is sent */ -int varlink_collect_full(Varlink *v, const char *method, JsonVariant *parameters, JsonVariant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags); -static inline int varlink_collect(Varlink *v, const char *method, JsonVariant *parameters, JsonVariant **ret_parameters, const char **ret_error_id) { +int varlink_collect_full(Varlink *v, const char *method, sd_json_variant *parameters, sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags); +static inline int varlink_collect(Varlink *v, const char *method, sd_json_variant *parameters, sd_json_variant **ret_parameters, const char **ret_error_id) { return varlink_collect_full(v, method, parameters, ret_parameters, ret_error_id, NULL); } -int varlink_collectb(Varlink *v, const char *method, JsonVariant **ret_parameters, const char **ret_error_id, ...); +int varlink_collectb(Varlink *v, const char *method, sd_json_variant **ret_parameters, const char **ret_error_id, ...); /* Enqueue method call, expect a reply, which is eventually delivered to the reply callback */ -int varlink_invoke(Varlink *v, const char *method, JsonVariant *parameters); +int varlink_invoke(Varlink *v, const char *method, sd_json_variant *parameters); int varlink_invokeb(Varlink *v, const char *method, ...); /* Enqueue method call, expect a reply now, and possibly more later, which are all delivered to the reply callback */ -int varlink_observe(Varlink *v, const char *method, JsonVariant *parameters); +int varlink_observe(Varlink *v, const char *method, sd_json_variant *parameters); int varlink_observeb(Varlink *v, const char *method, ...); /* Enqueue a final reply */ -int varlink_reply(Varlink *v, JsonVariant *parameters); +int varlink_reply(Varlink *v, sd_json_variant *parameters); int varlink_replyb(Varlink *v, ...); /* Enqueue a (final) error */ -int varlink_error(Varlink *v, const char *error_id, JsonVariant *parameters); +int varlink_error(Varlink *v, const char *error_id, sd_json_variant *parameters); int varlink_errorb(Varlink *v, const char *error_id, ...); -int varlink_error_invalid_parameter(Varlink *v, JsonVariant *parameters); +int varlink_error_invalid_parameter(Varlink *v, sd_json_variant *parameters); int varlink_error_invalid_parameter_name(Varlink *v, const char *name); int varlink_error_errno(Varlink *v, int error); /* Enqueue a "more" reply */ -int varlink_notify(Varlink *v, JsonVariant *parameters); +int varlink_notify(Varlink *v, sd_json_variant *parameters); int varlink_notifyb(Varlink *v, ...); /* Ask for the current message to be dispatched again */ int varlink_dispatch_again(Varlink *v); /* Get the currently processed incoming message */ -int varlink_get_current_parameters(Varlink *v, JsonVariant **ret); +int varlink_get_current_parameters(Varlink *v, sd_json_variant **ret); /* Parsing incoming data via json_dispatch() and generate a nice error on parse errors */ -int varlink_dispatch(Varlink *v, JsonVariant *parameters, const JsonDispatch table[], void *userdata); +int varlink_dispatch(Varlink *v, sd_json_variant *parameters, const sd_json_dispatch_field table[], void *userdata); /* Write outgoing fds into the socket (to be associated with the next enqueued message) */ int varlink_push_fd(Varlink *v, int fd); @@ -242,7 +242,7 @@ typedef enum VarlinkInvocationFlags { int varlink_invocation(VarlinkInvocationFlags flags); -int varlink_error_to_errno(const char *error, JsonVariant *parameters); +int varlink_error_to_errno(const char *error, sd_json_variant *parameters); DEFINE_TRIVIAL_CLEANUP_FUNC(Varlink *, varlink_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(Varlink *, varlink_close_unref); diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 0402bb07f3..1bdb3a5f79 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -16,6 +16,7 @@ #include "sd-bus.h" #include "sd-device.h" #include "sd-id128.h" +#include "sd-json.h" #include "sd-messages.h" #include "battery-capacity.h" @@ -36,7 +37,6 @@ #include "hibernate-util.h" #include "id128-util.h" #include "io-util.h" -#include "json.h" #include "log.h" #include "main-func.h" #include "os-util.h" @@ -57,7 +57,7 @@ static int write_efi_hibernate_location(const HibernationDevice *hibernation_dev int log_level = required ? LOG_ERR : LOG_DEBUG; #if ENABLE_EFI - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_free_ char *formatted = NULL, *id = NULL, *image_id = NULL, *version_id = NULL, *image_version = NULL; _cleanup_(sd_device_unrefp) sd_device *device = NULL; @@ -98,18 +98,18 @@ static int write_efi_hibernate_location(const HibernationDevice *hibernation_dev if (r < 0) log_full_errno(log_level_ignore, r, "Failed to parse os-release, ignoring: %m"); - r = json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UUID("uuid", uuid), - JSON_BUILD_PAIR_UNSIGNED("offset", hibernation_device->offset), - JSON_BUILD_PAIR_CONDITION(!isempty(uts.release), "kernelVersion", JSON_BUILD_STRING(uts.release)), - JSON_BUILD_PAIR_CONDITION(id, "osReleaseId", JSON_BUILD_STRING(id)), - JSON_BUILD_PAIR_CONDITION(image_id, "osReleaseImageId", JSON_BUILD_STRING(image_id)), - JSON_BUILD_PAIR_CONDITION(version_id, "osReleaseVersionId", JSON_BUILD_STRING(version_id)), - JSON_BUILD_PAIR_CONDITION(image_version, "osReleaseImageVersion", JSON_BUILD_STRING(image_version)))); + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UUID("uuid", uuid), + SD_JSON_BUILD_PAIR_UNSIGNED("offset", hibernation_device->offset), + SD_JSON_BUILD_PAIR_CONDITION(!isempty(uts.release), "kernelVersion", SD_JSON_BUILD_STRING(uts.release)), + SD_JSON_BUILD_PAIR_CONDITION(!!id, "osReleaseId", SD_JSON_BUILD_STRING(id)), + SD_JSON_BUILD_PAIR_CONDITION(!!image_id, "osReleaseImageId", SD_JSON_BUILD_STRING(image_id)), + SD_JSON_BUILD_PAIR_CONDITION(!!version_id, "osReleaseVersionId", SD_JSON_BUILD_STRING(version_id)), + SD_JSON_BUILD_PAIR_CONDITION(!!image_version, "osReleaseImageVersion", SD_JSON_BUILD_STRING(image_version)))); if (r < 0) return log_full_errno(log_level, r, "Failed to build JSON object: %m"); - r = json_variant_format(v, 0, &formatted); + r = sd_json_variant_format(v, 0, &formatted); if (r < 0) return log_full_errno(log_level, r, "Failed to format JSON object: %m"); diff --git a/src/sysext/sysext.c b/src/sysext/sysext.c index c55c24fdf3..e5aa86c88c 100644 --- a/src/sysext/sysext.c +++ b/src/sysext/sysext.c @@ -76,7 +76,7 @@ DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(mutable_mode, Mutabl static char **arg_hierarchies = NULL; /* "/usr" + "/opt" by default for sysext and /etc by default for confext */ static char *arg_root = NULL; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; static bool arg_force = false; @@ -443,11 +443,11 @@ typedef struct MethodUnmergeParameters { int no_reload; } MethodUnmergeParameters; -static int vl_method_unmerge(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_unmerge(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "class", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(MethodUnmergeParameters, class), 0 }, - { "noReload", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(MethodUnmergeParameters, no_reload), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "class", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodUnmergeParameters, class), 0 }, + { "noReload", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(MethodUnmergeParameters, no_reload), 0 }, {} }; MethodUnmergeParameters p = { @@ -1918,13 +1918,13 @@ typedef struct MethodMergeParameters { int noexec; } MethodMergeParameters; -static int parse_merge_parameters(Varlink *link, JsonVariant *parameters, MethodMergeParameters *p) { +static int parse_merge_parameters(Varlink *link, sd_json_variant *parameters, MethodMergeParameters *p) { - static const JsonDispatch dispatch_table[] = { - { "class", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(MethodMergeParameters, class), 0 }, - { "force", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(MethodMergeParameters, force), 0 }, - { "noReload", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(MethodMergeParameters, no_reload), 0 }, - { "noexec", JSON_VARIANT_BOOLEAN, json_dispatch_tristate, offsetof(MethodMergeParameters, noexec), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "class", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodMergeParameters, class), 0 }, + { "force", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(MethodMergeParameters, force), 0 }, + { "noReload", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(MethodMergeParameters, no_reload), 0 }, + { "noexec", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(MethodMergeParameters, noexec), 0 }, {} }; @@ -1935,7 +1935,7 @@ static int parse_merge_parameters(Varlink *link, JsonVariant *parameters, Method return varlink_dispatch(link, parameters, dispatch_table, p); } -static int vl_method_merge(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_merge(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { _cleanup_hashmap_free_ Hashmap *images = NULL; MethodMergeParameters p = { .force = -1, @@ -1968,7 +1968,7 @@ static int vl_method_merge(Varlink *link, JsonVariant *parameters, VarlinkMethod if (r < 0) return r; if (r > 0) - return varlink_errorb(link, "io.systemd.sysext.AlreadyMerged", JSON_BUILD_OBJECT(JSON_BUILD_PAIR_STRING("hierarchy", which))); + return varlink_errorb(link, "io.systemd.sysext.AlreadyMerged", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_STRING("hierarchy", which))); r = merge(image_class, hierarchies ?: arg_hierarchies, @@ -2038,7 +2038,7 @@ static int verb_refresh(int argc, char **argv, void *userdata) { arg_noexec); } -static int vl_method_refresh(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_refresh(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { MethodMergeParameters p = { .force = -1, @@ -2084,7 +2084,7 @@ static int verb_list(int argc, char **argv, void *userdata) { if (r < 0) return log_error_errno(r, "Failed to discover images: %m"); - if ((arg_json_format_flags & JSON_FORMAT_OFF) && hashmap_isempty(images)) { + if ((arg_json_format_flags & SD_JSON_FORMAT_OFF) && hashmap_isempty(images)) { log_info("No OS extensions found."); return 0; } @@ -2113,15 +2113,15 @@ typedef struct MethodListParameters { const char *class; } MethodListParameters; -static int vl_method_list(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_list(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "class", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(MethodListParameters, class), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "class", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodListParameters, class), 0 }, {} }; MethodListParameters p = { }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_hashmap_free_ Hashmap *images = NULL; ImageClass image_class = arg_image_class; Image *img; @@ -2153,7 +2153,7 @@ static int vl_method_list(Varlink *link, JsonVariant *parameters, VarlinkMethodF return r; } - v = json_variant_unref(v); + v = sd_json_variant_unref(v); r = image_to_json(img, &v); if (r < 0) diff --git a/src/systemctl/systemctl-set-environment.c b/src/systemctl/systemctl-set-environment.c index 55d1160510..409985ebda 100644 --- a/src/systemctl/systemctl-set-environment.c +++ b/src/systemctl/systemctl-set-environment.c @@ -8,8 +8,8 @@ #include "systemctl-util.h" #include "systemctl.h" -static int json_transform_message(sd_bus_message *m, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +static int json_transform_message(sd_bus_message *m, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *text; int r; @@ -31,7 +31,7 @@ static int json_transform_message(sd_bus_message *m, JsonVariant **ret) { sep++; - r = json_variant_set_field_string(&v, n, sep); + r = sd_json_variant_set_field_string(&v, n, sep); if (r < 0) return log_error_errno(r, "Failed to set JSON field '%s' to '%s': %m", n, sep); } @@ -81,13 +81,13 @@ int verb_show_environment(int argc, char *argv[], void *userdata) { return bus_log_parse_error(r); if (OUTPUT_MODE_IS_JSON(arg_output)) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = json_transform_message(reply, &v); if (r < 0) return r; - json_variant_dump(v, output_mode_to_json_format_flags(arg_output), stdout, NULL); + sd_json_variant_dump(v, output_mode_to_json_format_flags(arg_output), stdout, NULL); } else { while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { r = print_variable(text); diff --git a/src/systemctl/systemctl-util.c b/src/systemctl/systemctl-util.c index 2482b7ccb2..be3b35e6f9 100644 --- a/src/systemctl/systemctl-util.c +++ b/src/systemctl/systemctl-util.c @@ -897,7 +897,7 @@ int output_table(Table *table) { assert(table); if (OUTPUT_MODE_IS_JSON(arg_output)) - r = table_print_json(table, NULL, output_mode_to_json_format_flags(arg_output) | JSON_FORMAT_COLOR_AUTO); + r = table_print_json(table, NULL, output_mode_to_json_format_flags(arg_output) | SD_JSON_FORMAT_COLOR_AUTO); else r = table_print(table, NULL); if (r < 0) diff --git a/src/systemd/meson.build b/src/systemd/meson.build index de58bff334..4065ac4f30 100644 --- a/src/systemd/meson.build +++ b/src/systemd/meson.build @@ -11,6 +11,7 @@ _systemd_headers = [ 'sd-hwdb.h', 'sd-id128.h', 'sd-journal.h', + 'sd-json.h', 'sd-login.h', 'sd-messages.h', 'sd-path.h', diff --git a/src/systemd/sd-json.h b/src/systemd/sd-json.h new file mode 100644 index 0000000000..c707322177 --- /dev/null +++ b/src/systemd/sd-json.h @@ -0,0 +1,334 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#ifndef foosdjsonhfoo +#define foosdjsonhfoo + +/*** + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <https://www.gnu.org/licenses/>. +***/ + +#include <errno.h> +#include <fcntl.h> +#include <inttypes.h> +#include <stddef.h> +#include <stdio.h> + +#include "sd-id128.h" + +#include "_sd-common.h" + +_SD_BEGIN_DECLARATIONS; + +/* + In case you wonder why we have our own JSON implementation, here are a couple of reasons why this implementation has + benefits over various other implementations: + + - We need support for 64-bit signed and unsigned integers, i.e. the full 64,5bit range of -9223372036854775808…18446744073709551615 + - All our variants are immutable after creation + - Special values such as true, false, zero, null, empty strings, empty array, empty objects require zero dynamic memory + - Progressive parsing + - Our integer/real type implicitly converts, but only if that's safe and loss-lessly possible + - There's a "builder" for putting together objects easily in varargs function calls + - There's a "dispatcher" for mapping objects to C data structures + - Every variant optionally carries parsing location information, which simplifies debugging and parse log error generation + - Formatter has color, line, column support + + Limitations: + - Doesn't allow embedded NUL in strings + - Can't store integers outside of the -9223372036854775808…18446744073709551615 range (it will use 'double' for + values outside this range, which is lossy) + - Can't store negative zero (will be treated identical to positive zero, and not retained across serialization) + - Can't store non-integer numbers that can't be stored in "double" losslessly + - Allows creation and parsing of objects with duplicate keys. The "dispatcher" will refuse them however. This means + we can parse and pass around such objects, but will carefully refuse them when we convert them into our own data. + + (These limitations should be pretty much in line with those of other JSON implementations, in fact might be less + limiting in most cases even.) +*/ + +typedef struct sd_json_variant sd_json_variant; + +__extension__ typedef enum sd_json_variant_type_t { + SD_JSON_VARIANT_STRING, + SD_JSON_VARIANT_INTEGER, + SD_JSON_VARIANT_UNSIGNED, + SD_JSON_VARIANT_REAL, + SD_JSON_VARIANT_NUMBER, /* This a pseudo-type: we can never create variants of this type, but we use it as wildcard check for the above three types */ + SD_JSON_VARIANT_BOOLEAN, + SD_JSON_VARIANT_ARRAY, + SD_JSON_VARIANT_OBJECT, + SD_JSON_VARIANT_NULL, + _SD_JSON_VARIANT_TYPE_MAX, + _SD_JSON_VARIANT_TYPE_INVALID = -EINVAL, + _SD_ENUM_FORCE_S64(JSON_VARIANT_TYPE) +} sd_json_variant_type_t; + +int sd_json_variant_new_string(sd_json_variant **ret, const char *s); +int sd_json_variant_new_stringn(sd_json_variant **ret, const char *s, size_t n); +int sd_json_variant_new_base64(sd_json_variant **ret, const void *p, size_t n); +int sd_json_variant_new_base32hex(sd_json_variant **ret, const void *p, size_t n); +int sd_json_variant_new_hex(sd_json_variant **ret, const void *p, size_t n); +int sd_json_variant_new_octescape(sd_json_variant **ret, const void *p, size_t n); +int sd_json_variant_new_integer(sd_json_variant **ret, int64_t i); +int sd_json_variant_new_unsigned(sd_json_variant **ret, uint64_t u); +int sd_json_variant_new_real(sd_json_variant **ret, double d); +int sd_json_variant_new_boolean(sd_json_variant **ret, int b); +int sd_json_variant_new_array(sd_json_variant **ret, sd_json_variant **array, size_t n); +int sd_json_variant_new_array_bytes(sd_json_variant **ret, const void *p, size_t n); +int sd_json_variant_new_array_strv(sd_json_variant **ret, char **l); +int sd_json_variant_new_object(sd_json_variant **ret, sd_json_variant **array, size_t n); +int sd_json_variant_new_null(sd_json_variant **ret); +int sd_json_variant_new_id128(sd_json_variant **ret, sd_id128_t id); +int sd_json_variant_new_uuid(sd_json_variant **ret, sd_id128_t id); + +sd_json_variant* sd_json_variant_ref(sd_json_variant *v); +sd_json_variant* sd_json_variant_unref(sd_json_variant *v); +void sd_json_variant_unref_many(sd_json_variant **array, size_t n); + +_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_json_variant, sd_json_variant_unref); + +const char *sd_json_variant_string(sd_json_variant *v); +int64_t sd_json_variant_integer(sd_json_variant *v); +uint64_t sd_json_variant_unsigned(sd_json_variant *v); +double sd_json_variant_real(sd_json_variant *v); +int sd_json_variant_boolean(sd_json_variant *v); + +sd_json_variant_type_t sd_json_variant_type(sd_json_variant *v); +int sd_json_variant_has_type(sd_json_variant *v, sd_json_variant_type_t type); + +int sd_json_variant_is_string(sd_json_variant *v); +int sd_json_variant_is_integer(sd_json_variant *v); +int sd_json_variant_is_unsigned(sd_json_variant *v); +int sd_json_variant_is_real(sd_json_variant *v); +int sd_json_variant_is_number(sd_json_variant *v); +int sd_json_variant_is_boolean(sd_json_variant *v); +int sd_json_variant_is_array(sd_json_variant *v); +int sd_json_variant_is_object(sd_json_variant *v); +int sd_json_variant_is_null(sd_json_variant *v); + +int sd_json_variant_is_negative(sd_json_variant *v); +int sd_json_variant_is_blank_object(sd_json_variant *v); +int sd_json_variant_is_blank_array(sd_json_variant *v); +int sd_json_variant_is_normalized(sd_json_variant *v); +int sd_json_variant_is_sorted(sd_json_variant *v); + +size_t sd_json_variant_elements(sd_json_variant *v); +sd_json_variant* sd_json_variant_by_index(sd_json_variant *v, size_t index); +sd_json_variant* sd_json_variant_by_key(sd_json_variant *v, const char *key); +sd_json_variant* sd_json_variant_by_key_full(sd_json_variant *v, const char *key, sd_json_variant **ret_key); + +int sd_json_variant_equal(sd_json_variant *a, sd_json_variant *b); + +void sd_json_variant_sensitive(sd_json_variant *v); +int sd_json_variant_is_sensitive(sd_json_variant *v); +int sd_json_variant_is_sensitive_recursive(sd_json_variant *v); + +int sd_json_variant_get_source(sd_json_variant *v, const char **ret_source, unsigned *ret_line, unsigned *reterr_column); + +__extension__ typedef enum sd_json_format_flags_t { + SD_JSON_FORMAT_NEWLINE = 1 << 0, /* suffix with newline */ + SD_JSON_FORMAT_PRETTY = 1 << 1, /* add internal whitespace to appeal to human readers */ + SD_JSON_FORMAT_PRETTY_AUTO = 1 << 2, /* same, but only if connected to a tty (and JSON_FORMAT_NEWLINE otherwise) */ + SD_JSON_FORMAT_COLOR = 1 << 3, /* insert ANSI color sequences */ + SD_JSON_FORMAT_COLOR_AUTO = 1 << 4, /* insert ANSI color sequences if colors_enabled() says so */ + SD_JSON_FORMAT_SOURCE = 1 << 5, /* prefix with source filename/line/column */ + SD_JSON_FORMAT_SSE = 1 << 6, /* prefix/suffix with W3C server-sent events */ + SD_JSON_FORMAT_SEQ = 1 << 7, /* prefix/suffix with RFC 7464 application/json-seq */ + SD_JSON_FORMAT_FLUSH = 1 << 8, /* call fflush() after dumping JSON */ + SD_JSON_FORMAT_EMPTY_ARRAY = 1 << 9, /* output "[]" for empty input */ + SD_JSON_FORMAT_OFF = 1 << 10, /* make json_variant_format() fail with -ENOEXEC */ + SD_JSON_FORMAT_CENSOR_SENSITIVE = 1 << 11, /* Replace all sensitive elements with the string "<sensitive data>" */ + _SD_ENUM_FORCE_S64(JSON_FORMAT_FLAGS) +} sd_json_format_flags_t; + +int sd_json_variant_format(sd_json_variant *v, sd_json_format_flags_t flags, char **ret); +int sd_json_variant_dump(sd_json_variant *v, sd_json_format_flags_t flags, FILE *f, const char *prefix); + +int sd_json_variant_filter(sd_json_variant **v, char **to_remove); + +int sd_json_variant_set_field(sd_json_variant **v, const char *field, sd_json_variant *value); +int sd_json_variant_set_fieldb(sd_json_variant **v, const char *field, ...); +int sd_json_variant_set_field_string(sd_json_variant **v, const char *field, const char *value); +int sd_json_variant_set_field_integer(sd_json_variant **v, const char *field, int64_t value); +int sd_json_variant_set_field_unsigned(sd_json_variant **v, const char *field, uint64_t value); +int sd_json_variant_set_field_boolean(sd_json_variant **v, const char *field, int b); +int sd_json_variant_set_field_strv(sd_json_variant **v, const char *field, char **l); + +sd_json_variant* sd_json_variant_find(sd_json_variant *haystack, sd_json_variant *needle); + +int sd_json_variant_append_array(sd_json_variant **v, sd_json_variant *element); +int sd_json_variant_append_arrayb(sd_json_variant **v, ...); +int sd_json_variant_append_array_nodup(sd_json_variant **v, sd_json_variant *element); + +int sd_json_variant_merge_object(sd_json_variant **v, sd_json_variant *m); +int sd_json_variant_merge_objectb(sd_json_variant **v, ...); + +int sd_json_variant_sort(sd_json_variant **v); +int sd_json_variant_normalize(sd_json_variant **v); + +__extension__ typedef enum sd_json_parse_flags_t { + SD_JSON_PARSE_SENSITIVE = 1 << 0, /* mark variant as "sensitive", i.e. something containing secret key material or such */ + _SD_ENUM_FORCE_S64(JSON_PARSE_FLAGS) +} sd_json_parse_flags_t; + +int sd_json_parse_with_source(const char *string, const char *source, sd_json_parse_flags_t flags, sd_json_variant **ret, unsigned *reterr_line, unsigned *reterr_column); +int sd_json_parse_with_source_continue(const char **p, const char *source, sd_json_parse_flags_t flags, sd_json_variant **ret, unsigned *reterr_line, unsigned *reterr_column); +int sd_json_parse(const char *string, sd_json_parse_flags_t flags, sd_json_variant **ret, unsigned *reterr_line, unsigned *reterr_column); +int sd_json_parse_continue(const char **p, sd_json_parse_flags_t flags, sd_json_variant **ret, unsigned *reterr_line, unsigned *reterr_column); +int sd_json_parse_file_at(FILE *f, int dir_fd, const char *path, sd_json_parse_flags_t flags, sd_json_variant **ret, unsigned *reterr_line, unsigned *reterr_column); +int sd_json_parse_file(FILE *f, const char *path, sd_json_parse_flags_t flags, sd_json_variant **ret, unsigned *reterr_line, unsigned *reterr_column); + +enum { + _SD_JSON_BUILD_STRING, + _SD_JSON_BUILD_INTEGER, + _SD_JSON_BUILD_UNSIGNED, + _SD_JSON_BUILD_REAL, + _SD_JSON_BUILD_BOOLEAN, + _SD_JSON_BUILD_ARRAY_BEGIN, + _SD_JSON_BUILD_ARRAY_END, + _SD_JSON_BUILD_OBJECT_BEGIN, + _SD_JSON_BUILD_OBJECT_END, + _SD_JSON_BUILD_PAIR, + _SD_JSON_BUILD_PAIR_CONDITION, + _SD_JSON_BUILD_NULL, + _SD_JSON_BUILD_VARIANT, + _SD_JSON_BUILD_VARIANT_ARRAY, + _SD_JSON_BUILD_LITERAL, + _SD_JSON_BUILD_STRV, + _SD_JSON_BUILD_BASE64, + _SD_JSON_BUILD_BASE32HEX, + _SD_JSON_BUILD_HEX, + _SD_JSON_BUILD_OCTESCAPE, + _SD_JSON_BUILD_BYTE_ARRAY, + _SD_JSON_BUILD_ID128, + _SD_JSON_BUILD_UUID, + _SD_JSON_BUILD_CALLBACK, + _SD_JSON_BUILD_MAX +}; + +typedef int (*sd_json_build_callback_t)(sd_json_variant **ret, const char *name, void *userdata); + +#define SD_JSON_BUILD_STRING(s) _SD_JSON_BUILD_STRING, (const char*) { s } +#define SD_JSON_BUILD_INTEGER(i) _SD_JSON_BUILD_INTEGER, (int64_t) { i } +#define SD_JSON_BUILD_UNSIGNED(u) _SD_JSON_BUILD_UNSIGNED, (uint64_t) { u } +#define SD_JSON_BUILD_REAL(d) _SD_JSON_BUILD_REAL, (double) { d } +#define SD_JSON_BUILD_BOOLEAN(b) _SD_JSON_BUILD_BOOLEAN, (int) { b } +#define SD_JSON_BUILD_ARRAY(...) _SD_JSON_BUILD_ARRAY_BEGIN, __VA_ARGS__, _SD_JSON_BUILD_ARRAY_END +#define SD_JSON_BUILD_EMPTY_ARRAY _SD_JSON_BUILD_ARRAY_BEGIN, _SD_JSON_BUILD_ARRAY_END +#define SD_JSON_BUILD_OBJECT(...) _SD_JSON_BUILD_OBJECT_BEGIN, __VA_ARGS__, _SD_JSON_BUILD_OBJECT_END +#define SD_JSON_BUILD_EMPTY_OBJECT _SD_JSON_BUILD_OBJECT_BEGIN, _SD_JSON_BUILD_OBJECT_END +#define SD_JSON_BUILD_PAIR(n, ...) _SD_JSON_BUILD_PAIR, (const char*) { n }, __VA_ARGS__ +#define SD_JSON_BUILD_PAIR_CONDITION(c, n, ...) _SD_JSON_BUILD_PAIR_CONDITION, (int) { c }, (const char*) { n }, __VA_ARGS__ +#define SD_JSON_BUILD_NULL _SD_JSON_BUILD_NULL +#define SD_JSON_BUILD_VARIANT(v) _SD_JSON_BUILD_VARIANT, (sd_json_variant*) { v } +#define SD_JSON_BUILD_VARIANT_ARRAY(v, n) _SD_JSON_BUILD_VARIANT_ARRAY, (sd_json_variant **) { v }, (size_t) { n } +#define SD_JSON_BUILD_LITERAL(l) _SD_JSON_BUILD_LITERAL, (const char*) { l } +#define SD_JSON_BUILD_STRV(l) _SD_JSON_BUILD_STRV, (char**) { l } +#define SD_JSON_BUILD_BASE64(p, n) _SD_JSON_BUILD_BASE64, (const void*) { p }, (size_t) { n } +#define SD_JSON_BUILD_BASE32HEX(p, n) _SD_JSON_BUILD_BASE32HEX, (const void*) { p }, (size_t) { n } +#define SD_JSON_BUILD_HEX(p, n) _SD_JSON_BUILD_HEX, (const void*) { p }, (size_t) { n } +#define SD_JSON_BUILD_OCTESCAPE(p, n) _SD_JSON_BUILD_OCTESCAPE, (const void*) { p }, (size_t) { n } +#define SD_JSON_BUILD_BYTE_ARRAY(v, n) _SD_JSON_BUILD_BYTE_ARRAY, (const void*) { v }, (size_t) { n } +#define SD_JSON_BUILD_ID128(id) _SD_JSON_BUILD_ID128, (const sd_id128_t*) { &(id) } +#define SD_JSON_BUILD_UUID(id) _SD_JSON_BUILD_UUID, (const sd_id128_t*) { &(id) } +#define SD_JSON_BUILD_CALLBACK(c, u) _SD_JSON_BUILD_CALLBACK, (sd_json_build_callback_t) { c }, (void*) { u } + +#define SD_JSON_BUILD_PAIR_STRING(name, s) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_STRING(s)) +#define SD_JSON_BUILD_PAIR_INTEGER(name, i) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_INTEGER(i)) +#define SD_JSON_BUILD_PAIR_UNSIGNED(name, u) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_UNSIGNED(u)) +#define SD_JSON_BUILD_PAIR_REAL(name, d) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_REAL(d)) +#define SD_JSON_BUILD_PAIR_BOOLEAN(name, b) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_BOOLEAN(b)) +#define SD_JSON_BUILD_PAIR_ARRAY(name, ...) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_ARRAY(__VA_ARGS__)) +#define SD_JSON_BUILD_PAIR_EMPTY_ARRAY(name) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_EMPTY_ARRAY) +#define SD_JSON_BUILD_PAIR_OBJECT(name, ...) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_OBJECT(__VA_ARGS__)) +#define SD_JSON_BUILD_PAIR_EMPTY_OBJECT(name) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_EMPTY_OBJECT) +#define SD_JSON_BUILD_PAIR_NULL(name) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_NULL) +#define SD_JSON_BUILD_PAIR_VARIANT(name, v) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_VARIANT(v)) +#define SD_JSON_BUILD_PAIR_VARIANT_ARRAY(name, v, n) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_VARIANT_ARRAY(v, n)) +#define SD_JSON_BUILD_PAIR_LITERAL(name, l) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_LITERAL(l)) +#define SD_JSON_BUILD_PAIR_STRV(name, l) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_STRV(l)) +#define SD_JSON_BUILD_PAIR_BASE64(name, p, n) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_BASE64(p, n)) +#define SD_JSON_BUILD_PAIR_BASE32HEX(name, p, n) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_BASE32HEX(p, n)) +#define SD_JSON_BUILD_PAIR_HEX(name, p, n) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_HEX(p, n)) +#define SD_JSON_BUILD_PAIR_OCTESCAPE(name, p, n) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_OCTESCAPE(p, n)) +#define SD_JSON_BUILD_PAIR_BYTE_ARRAY(name, v, n) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_BYTE_ARRAY(v, n)) +#define SD_JSON_BUILD_PAIR_ID128(name, id) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_ID128(id)) +#define SD_JSON_BUILD_PAIR_UUID(name, id) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_UUID(id)) +#define SD_JSON_BUILD_PAIR_CALLBACK(name, c, u) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_CALLBACK(c, u)) + +int sd_json_build(sd_json_variant **ret, ...); +int sd_json_buildv(sd_json_variant **ret, va_list ap); + +/* A bitmask of flags used by the dispatch logic. Note that this is a combined bit mask, that is generated from the bit + * mask originally passed into json_dispatch(), the individual bitmask associated with the static sd_json_dispatch callout + * entry, as well the bitmask specified for json_log() calls */ +typedef enum sd_json_dispatch_flags_t { + /* The following three may be set in sd_json_dispatch's .flags field or the json_dispatch() flags parameter */ + SD_JSON_PERMISSIVE = 1 << 0, /* Shall parsing errors be considered fatal for this field or object? */ + SD_JSON_MANDATORY = 1 << 1, /* Should existence of this property be mandatory? */ + SD_JSON_LOG = 1 << 2, /* Should the parser log about errors? */ + SD_JSON_SAFE = 1 << 3, /* Don't accept "unsafe" strings in json_dispatch_string() + json_dispatch_string() */ + SD_JSON_RELAX = 1 << 4, /* Use relaxed user name checking in json_dispatch_user_group_name */ + SD_JSON_ALLOW_EXTENSIONS = 1 << 5, /* Subset of JSON_PERMISSIVE: allow additional fields, but no other permissive handling */ + + /* The following two may be passed into log_json() in addition to those above */ + SD_JSON_DEBUG = 1 << 6, /* Indicates that this log message is a debug message */ + SD_JSON_WARNING = 1 << 7 /* Indicates that this log message is a warning message */ +} sd_json_dispatch_flags_t; + +typedef int (*sd_json_dispatch_callback_t)(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); + +typedef struct sd_json_dispatch_field { + const char *name; + sd_json_variant_type_t type; + sd_json_dispatch_callback_t callback; + size_t offset; + sd_json_dispatch_flags_t flags; +} sd_json_dispatch_field; + +int sd_json_dispatch_full(sd_json_variant *v, const sd_json_dispatch_field table[], sd_json_dispatch_callback_t bad, sd_json_dispatch_flags_t flags, void *userdata, const char **reterr_bad_field); +int sd_json_dispatch(sd_json_variant *v, const sd_json_dispatch_field table[], sd_json_dispatch_flags_t flags, void *userdata); + +int sd_json_dispatch_string(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_const_string(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_strv(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_stdbool(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_intbool(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_tristate(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_variant(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_variant_noref(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_int64(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_uint64(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_uint32(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_int32(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_uint16(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_int16(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_int8(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_uint8(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_uid_gid(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_id128(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); +int sd_json_dispatch_unsupported(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata); + +#define sd_json_dispatch_uint sd_json_dispatch_uint32 +#define sd_json_dispatch_int sd_json_dispatch_int32 + +int sd_json_variant_strv(sd_json_variant *v, char ***ret); +int sd_json_variant_unbase64(sd_json_variant *v, void **ret, size_t *ret_size); +int sd_json_variant_unhex(sd_json_variant *v, void **ret, size_t *ret_size); + +const char *sd_json_variant_type_to_string(sd_json_variant_type_t t); +sd_json_variant_type_t sd_json_variant_type_from_string(const char *s); + +_SD_END_DECLARATIONS; + +#endif diff --git a/src/sysupdate/sysupdate.c b/src/sysupdate/sysupdate.c index 9565b68e05..9a8915c3fe 100644 --- a/src/sysupdate/sysupdate.c +++ b/src/sysupdate/sysupdate.c @@ -38,7 +38,7 @@ static char *arg_definitions = NULL; bool arg_sync = true; uint64_t arg_instances_max = UINT64_MAX; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; char *arg_root = NULL; @@ -485,10 +485,10 @@ static int context_show_version(Context *c, const char *version) { if (!us) return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version); - if (arg_json_format_flags & (JSON_FORMAT_OFF|JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO)) + if (arg_json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) (void) pager_open(arg_pager_flags); - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) printf("%s%s%s Version: %s\n" " State: %s%s%s\n" "Installed: %s%s\n" diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index 3dbfda7f96..85cd31ecee 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -4,6 +4,7 @@ #include "alloc-util.h" #include "format-table.h" +#include "json-util.h" #include "string-util.h" #include "strv.h" #include "tests.h" @@ -360,7 +361,7 @@ TEST(strv_wrapped) { } TEST(json) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; _cleanup_(table_unrefp) Table *t = NULL; assert_se(t = table_new("foo bar", "quux", "piep miau")); @@ -378,18 +379,18 @@ TEST(json) { assert_se(table_to_json(t, &v) >= 0); - assert_se(json_build(&w, - JSON_BUILD_ARRAY( - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("foo_bar", JSON_BUILD_CONST_STRING("v1")), - JSON_BUILD_PAIR("quux", JSON_BUILD_UNSIGNED(4711)), - JSON_BUILD_PAIR("zzz", JSON_BUILD_BOOLEAN(true))), - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("foo_bar", JSON_BUILD_STRV(STRV_MAKE("a", "b", "c"))), - JSON_BUILD_PAIR("quux", JSON_BUILD_NULL), - JSON_BUILD_PAIR("zzz", JSON_BUILD_UNSIGNED(0755))))) >= 0); - - assert_se(json_variant_equal(v, w)); + assert_se(sd_json_build(&w, + SD_JSON_BUILD_ARRAY( + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("foo_bar", JSON_BUILD_CONST_STRING("v1")), + SD_JSON_BUILD_PAIR("quux", SD_JSON_BUILD_UNSIGNED(4711)), + SD_JSON_BUILD_PAIR("zzz", SD_JSON_BUILD_BOOLEAN(true))), + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("foo_bar", SD_JSON_BUILD_STRV(STRV_MAKE("a", "b", "c"))), + SD_JSON_BUILD_PAIR("quux", SD_JSON_BUILD_NULL), + SD_JSON_BUILD_PAIR("zzz", SD_JSON_BUILD_UNSIGNED(0755))))) >= 0); + + assert_se(sd_json_variant_equal(v, w)); } TEST(table) { @@ -554,15 +555,15 @@ TEST(vertical) { " uuu o: 1K\n" "lllllllllllo: jjjjjjjjjjjjjjjjj\n")); - _cleanup_(json_variant_unrefp) JsonVariant *a = NULL, *b = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL; assert_se(table_to_json(t, &a) >= 0); - assert_se(json_build(&b, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("pfft_aa", JSON_BUILD_STRING("foo")), - JSON_BUILD_PAIR("dimpfelmoser", JSON_BUILD_UNSIGNED(1024)), - JSON_BUILD_PAIR("lllllllllllo", JSON_BUILD_STRING("jjjjjjjjjjjjjjjjj")))) >= 0); + assert_se(sd_json_build(&b, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("pfft_aa", SD_JSON_BUILD_STRING("foo")), + SD_JSON_BUILD_PAIR("dimpfelmoser", SD_JSON_BUILD_UNSIGNED(1024)), + SD_JSON_BUILD_PAIR("lllllllllllo", SD_JSON_BUILD_STRING("jjjjjjjjjjjjjjjjj")))) >= 0); - assert_se(json_variant_equal(a, b)); + assert_se(sd_json_variant_equal(a, b)); } TEST(path_basename) { diff --git a/src/test/test-json.c b/src/test/test-json.c index 7d9a7852f0..e40fd0a354 100644 --- a/src/test/test-json.c +++ b/src/test/test-json.c @@ -2,13 +2,15 @@ #include <float.h> +#include "sd-json.h" + #include "alloc-util.h" #include "escape.h" #include "fd-util.h" #include "fileio.h" #include "iovec-util.h" #include "json-internal.h" -#include "json.h" +#include "json-util.h" #include "math-util.h" #include "string-table.h" #include "string-util.h" @@ -77,10 +79,10 @@ static void test_tokenizer_one(const char *data, ...) { va_end(ap); } -typedef void (*Test)(JsonVariant *); +typedef void (*Test)(sd_json_variant *); static void test_variant_one(const char *data, Test test) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; _cleanup_free_ char *s = NULL; int r; @@ -88,62 +90,62 @@ static void test_variant_one(const char *data, Test test) { assert_se(cdata = cescape(data)); log_info("/* %s data=\"%s\" */", __func__, cdata); - r = json_parse(data, 0, &v, NULL, NULL); + r = sd_json_parse(data, 0, &v, NULL, NULL); assert_se(r == 0); assert_se(v); - r = json_variant_format(v, 0, &s); + r = sd_json_variant_format(v, 0, &s); assert_se(r >= 0); assert_se(s); assert_se((size_t) r == strlen(s)); log_info("formatted normally: %s", s); - r = json_parse(data, JSON_PARSE_SENSITIVE, &w, NULL, NULL); + r = sd_json_parse(data, SD_JSON_PARSE_SENSITIVE, &w, NULL, NULL); assert_se(r == 0); assert_se(w); - assert_se(json_variant_has_type(v, json_variant_type(w))); - assert_se(json_variant_has_type(w, json_variant_type(v))); - assert_se(json_variant_equal(v, w)); + assert_se(sd_json_variant_has_type(v, sd_json_variant_type(w))); + assert_se(sd_json_variant_has_type(w, sd_json_variant_type(v))); + assert_se(sd_json_variant_equal(v, w)); s = mfree(s); - r = json_variant_format(w, JSON_FORMAT_CENSOR_SENSITIVE, &s); + r = sd_json_variant_format(w, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s); assert_se(s); ASSERT_STREQ(s, "\"<sensitive data>\""); s = mfree(s); - r = json_variant_format(w, JSON_FORMAT_PRETTY, &s); + r = sd_json_variant_format(w, SD_JSON_FORMAT_PRETTY, &s); assert_se(r >= 0); assert_se(s); assert_se((size_t) r == strlen(s)); s = mfree(s); - w = json_variant_unref(w); + w = sd_json_variant_unref(w); - r = json_variant_format(v, JSON_FORMAT_PRETTY, &s); + r = sd_json_variant_format(v, SD_JSON_FORMAT_PRETTY, &s); assert_se(r >= 0); assert_se(s); assert_se((size_t) r == strlen(s)); log_info("formatted prettily:\n%s", s); - r = json_parse(data, 0, &w, NULL, NULL); + r = sd_json_parse(data, 0, &w, NULL, NULL); assert_se(r == 0); assert_se(w); - assert_se(json_variant_has_type(v, json_variant_type(w))); - assert_se(json_variant_has_type(w, json_variant_type(v))); - assert_se(json_variant_equal(v, w)); + assert_se(sd_json_variant_has_type(v, sd_json_variant_type(w))); + assert_se(sd_json_variant_has_type(w, sd_json_variant_type(v))); + assert_se(sd_json_variant_equal(v, w)); s = mfree(s); - r = json_variant_format(v, JSON_FORMAT_COLOR, &s); + r = sd_json_variant_format(v, SD_JSON_FORMAT_COLOR, &s); assert_se(r >= 0); assert_se(s); assert_se((size_t) r == strlen(s)); printf("Normal with color: %s\n", s); s = mfree(s); - r = json_variant_format(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, &s); + r = sd_json_variant_format(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, &s); assert_se(r >= 0); assert_se(s); assert_se((size_t) r == strlen(s)); @@ -153,224 +155,224 @@ static void test_variant_one(const char *data, Test test) { test(v); } -static void test_1(JsonVariant *v) { - JsonVariant *p, *q; +static void test_1(sd_json_variant *v) { + sd_json_variant *p, *q; unsigned i; log_info("/* %s */", __func__); /* 3 keys + 3 values */ - assert_se(json_variant_elements(v) == 6); + assert_se(sd_json_variant_elements(v) == 6); /* has k */ - p = json_variant_by_key(v, "k"); - assert_se(p && json_variant_type(p) == JSON_VARIANT_STRING); + p = sd_json_variant_by_key(v, "k"); + assert_se(p && sd_json_variant_type(p) == SD_JSON_VARIANT_STRING); /* k equals v */ - ASSERT_STREQ(json_variant_string(p), "v"); + ASSERT_STREQ(sd_json_variant_string(p), "v"); /* has foo */ - p = json_variant_by_key(v, "foo"); - assert_se(p && json_variant_type(p) == JSON_VARIANT_ARRAY && json_variant_elements(p) == 3); + p = sd_json_variant_by_key(v, "foo"); + assert_se(p && sd_json_variant_type(p) == SD_JSON_VARIANT_ARRAY && sd_json_variant_elements(p) == 3); /* check foo[0] = 1, foo[1] = 2, foo[2] = 3 */ for (i = 0; i < 3; ++i) { - q = json_variant_by_index(p, i); - assert_se(q && json_variant_type(q) == JSON_VARIANT_UNSIGNED && json_variant_unsigned(q) == (i+1)); - assert_se(q && json_variant_has_type(q, JSON_VARIANT_INTEGER) && json_variant_integer(q) == (i+1)); + q = sd_json_variant_by_index(p, i); + assert_se(q && sd_json_variant_type(q) == SD_JSON_VARIANT_UNSIGNED && sd_json_variant_unsigned(q) == (i+1)); + assert_se(q && sd_json_variant_has_type(q, SD_JSON_VARIANT_INTEGER) && sd_json_variant_integer(q) == (i+1)); } /* has bar */ - p = json_variant_by_key(v, "bar"); - assert_se(p && json_variant_type(p) == JSON_VARIANT_OBJECT && json_variant_elements(p) == 2); + p = sd_json_variant_by_key(v, "bar"); + assert_se(p && sd_json_variant_type(p) == SD_JSON_VARIANT_OBJECT && sd_json_variant_elements(p) == 2); /* zap is null */ - q = json_variant_by_key(p, "zap"); - assert_se(q && json_variant_type(q) == JSON_VARIANT_NULL); + q = sd_json_variant_by_key(p, "zap"); + assert_se(q && sd_json_variant_type(q) == SD_JSON_VARIANT_NULL); } -static void test_2(JsonVariant *v) { - JsonVariant *p, *q; +static void test_2(sd_json_variant *v) { + sd_json_variant *p, *q; log_info("/* %s */", __func__); /* 2 keys + 2 values */ - assert_se(json_variant_elements(v) == 4); + assert_se(sd_json_variant_elements(v) == 4); /* has mutant */ - p = json_variant_by_key(v, "mutant"); - assert_se(p && json_variant_type(p) == JSON_VARIANT_ARRAY && json_variant_elements(p) == 4); + p = sd_json_variant_by_key(v, "mutant"); + assert_se(p && sd_json_variant_type(p) == SD_JSON_VARIANT_ARRAY && sd_json_variant_elements(p) == 4); /* mutant[0] == 1 */ - q = json_variant_by_index(p, 0); - assert_se(q && json_variant_type(q) == JSON_VARIANT_UNSIGNED && json_variant_unsigned(q) == 1); - assert_se(q && json_variant_has_type(q, JSON_VARIANT_INTEGER) && json_variant_integer(q) == 1); + q = sd_json_variant_by_index(p, 0); + assert_se(q && sd_json_variant_type(q) == SD_JSON_VARIANT_UNSIGNED && sd_json_variant_unsigned(q) == 1); + assert_se(q && sd_json_variant_has_type(q, SD_JSON_VARIANT_INTEGER) && sd_json_variant_integer(q) == 1); /* mutant[1] == null */ - q = json_variant_by_index(p, 1); - assert_se(q && json_variant_type(q) == JSON_VARIANT_NULL); + q = sd_json_variant_by_index(p, 1); + assert_se(q && sd_json_variant_type(q) == SD_JSON_VARIANT_NULL); /* mutant[2] == "1" */ - q = json_variant_by_index(p, 2); - assert_se(q && json_variant_type(q) == JSON_VARIANT_STRING && streq(json_variant_string(q), "1")); + q = sd_json_variant_by_index(p, 2); + assert_se(q && sd_json_variant_type(q) == SD_JSON_VARIANT_STRING && streq(sd_json_variant_string(q), "1")); - /* mutant[3] == JSON_VARIANT_OBJECT */ - q = json_variant_by_index(p, 3); - assert_se(q && json_variant_type(q) == JSON_VARIANT_OBJECT && json_variant_elements(q) == 2); + /* mutant[3] == SD_JSON_VARIANT_OBJECT */ + q = sd_json_variant_by_index(p, 3); + assert_se(q && sd_json_variant_type(q) == SD_JSON_VARIANT_OBJECT && sd_json_variant_elements(q) == 2); /* has 1 */ - p = json_variant_by_key(q, "1"); - assert_se(p && json_variant_type(p) == JSON_VARIANT_ARRAY && json_variant_elements(p) == 2); + p = sd_json_variant_by_key(q, "1"); + assert_se(p && sd_json_variant_type(p) == SD_JSON_VARIANT_ARRAY && sd_json_variant_elements(p) == 2); /* "1"[0] == 1 */ - q = json_variant_by_index(p, 0); - assert_se(q && json_variant_type(q) == JSON_VARIANT_UNSIGNED && json_variant_unsigned(q) == 1); - assert_se(q && json_variant_has_type(q, JSON_VARIANT_INTEGER) && json_variant_integer(q) == 1); + q = sd_json_variant_by_index(p, 0); + assert_se(q && sd_json_variant_type(q) == SD_JSON_VARIANT_UNSIGNED && sd_json_variant_unsigned(q) == 1); + assert_se(q && sd_json_variant_has_type(q, SD_JSON_VARIANT_INTEGER) && sd_json_variant_integer(q) == 1); /* "1"[1] == "1" */ - q = json_variant_by_index(p, 1); - assert_se(q && json_variant_type(q) == JSON_VARIANT_STRING && streq(json_variant_string(q), "1")); + q = sd_json_variant_by_index(p, 1); + assert_se(q && sd_json_variant_type(q) == SD_JSON_VARIANT_STRING && streq(sd_json_variant_string(q), "1")); /* has thisisaverylongproperty */ - p = json_variant_by_key(v, "thisisaverylongproperty"); - assert_se(p && json_variant_type(p) == JSON_VARIANT_REAL && fabs(json_variant_real(p) - 1.27) < 0.001); + p = sd_json_variant_by_key(v, "thisisaverylongproperty"); + assert_se(p && sd_json_variant_type(p) == SD_JSON_VARIANT_REAL && fabs(sd_json_variant_real(p) - 1.27) < 0.001); } -static void test_zeroes(JsonVariant *v) { +static void test_zeroes(sd_json_variant *v) { /* Make sure zero is how we expect it. */ log_info("/* %s */", __func__); - assert_se(json_variant_elements(v) == 13); + assert_se(sd_json_variant_elements(v) == 13); - for (size_t i = 0; i < json_variant_elements(v); i++) { - JsonVariant *w; + for (size_t i = 0; i < sd_json_variant_elements(v); i++) { + sd_json_variant *w; size_t j; - assert_se(w = json_variant_by_index(v, i)); + assert_se(w = sd_json_variant_by_index(v, i)); - assert_se(json_variant_integer(w) == 0); - assert_se(json_variant_unsigned(w) == 0U); + assert_se(sd_json_variant_integer(w) == 0); + assert_se(sd_json_variant_unsigned(w) == 0U); - assert_se(iszero_safe(json_variant_real(w))); + assert_se(iszero_safe(sd_json_variant_real(w))); - assert_se(json_variant_is_integer(w)); - assert_se(json_variant_is_unsigned(w)); - assert_se(json_variant_is_real(w)); - assert_se(json_variant_is_number(w)); + assert_se(sd_json_variant_is_integer(w)); + assert_se(sd_json_variant_is_unsigned(w)); + assert_se(sd_json_variant_is_real(w)); + assert_se(sd_json_variant_is_number(w)); - assert_se(!json_variant_is_negative(w)); + assert_se(!sd_json_variant_is_negative(w)); - assert_se(IN_SET(json_variant_type(w), JSON_VARIANT_INTEGER, JSON_VARIANT_UNSIGNED, JSON_VARIANT_REAL)); + assert_se(IN_SET(sd_json_variant_type(w), SD_JSON_VARIANT_INTEGER, SD_JSON_VARIANT_UNSIGNED, SD_JSON_VARIANT_REAL)); - for (j = 0; j < json_variant_elements(v); j++) { - JsonVariant *q; + for (j = 0; j < sd_json_variant_elements(v); j++) { + sd_json_variant *q; - assert_se(q = json_variant_by_index(v, j)); + assert_se(q = sd_json_variant_by_index(v, j)); - assert_se(json_variant_equal(w, q)); + assert_se(sd_json_variant_equal(w, q)); } } } TEST(build) { - _cleanup_(json_variant_unrefp) JsonVariant *a = NULL, *b = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL; _cleanup_free_ char *s = NULL, *t = NULL; - assert_se(json_build(&a, JSON_BUILD_STRING("hallo")) >= 0); - assert_se(json_build(&b, JSON_BUILD_LITERAL(" \"hallo\" ")) >= 0); - assert_se(json_variant_equal(a, b)); + assert_se(sd_json_build(&a, SD_JSON_BUILD_STRING("hallo")) >= 0); + assert_se(sd_json_build(&b, SD_JSON_BUILD_LITERAL(" \"hallo\" ")) >= 0); + assert_se(sd_json_variant_equal(a, b)); - b = json_variant_unref(b); + b = sd_json_variant_unref(b); - assert_se(json_build(&b, JSON_BUILD_VARIANT(a)) >= 0); - assert_se(json_variant_equal(a, b)); + assert_se(sd_json_build(&b, SD_JSON_BUILD_VARIANT(a)) >= 0); + assert_se(sd_json_variant_equal(a, b)); - b = json_variant_unref(b); - assert_se(json_build(&b, JSON_BUILD_STRING("pief")) >= 0); - assert_se(!json_variant_equal(a, b)); + b = sd_json_variant_unref(b); + assert_se(sd_json_build(&b, SD_JSON_BUILD_STRING("pief")) >= 0); + assert_se(!sd_json_variant_equal(a, b)); - a = json_variant_unref(a); - b = json_variant_unref(b); + a = sd_json_variant_unref(a); + b = sd_json_variant_unref(b); - assert_se(json_build(&a, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("one", JSON_BUILD_INTEGER(7)), - JSON_BUILD_PAIR("two", JSON_BUILD_REAL(2.0)), - JSON_BUILD_PAIR("three", JSON_BUILD_INTEGER(0)))) >= 0); + assert_se(sd_json_build(&a, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("one", SD_JSON_BUILD_INTEGER(7)), + SD_JSON_BUILD_PAIR("two", SD_JSON_BUILD_REAL(2.0)), + SD_JSON_BUILD_PAIR("three", SD_JSON_BUILD_INTEGER(0)))) >= 0); - assert_se(json_build(&b, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("two", JSON_BUILD_INTEGER(2)), - JSON_BUILD_PAIR("three", JSON_BUILD_REAL(0)), - JSON_BUILD_PAIR("one", JSON_BUILD_REAL(7)))) >= 0); + assert_se(sd_json_build(&b, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("two", SD_JSON_BUILD_INTEGER(2)), + SD_JSON_BUILD_PAIR("three", SD_JSON_BUILD_REAL(0)), + SD_JSON_BUILD_PAIR("one", SD_JSON_BUILD_REAL(7)))) >= 0); - assert_se(json_variant_equal(a, b)); + assert_se(sd_json_variant_equal(a, b)); - a = json_variant_unref(a); - b = json_variant_unref(b); + a = sd_json_variant_unref(a); + b = sd_json_variant_unref(b); const char* arr_1234[] = {"one", "two", "three", "four", NULL}; - assert_se(json_build(&a, JSON_BUILD_ARRAY(JSON_BUILD_OBJECT(JSON_BUILD_PAIR("x", JSON_BUILD_BOOLEAN(true)), - JSON_BUILD_PAIR("y", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("this", JSON_BUILD_NULL)))), - JSON_BUILD_VARIANT(NULL), - JSON_BUILD_LITERAL(NULL), - JSON_BUILD_STRING(NULL), - JSON_BUILD_NULL, - JSON_BUILD_INTEGER(77), - JSON_BUILD_ARRAY(JSON_BUILD_VARIANT(JSON_VARIANT_STRING_CONST("foobar")), - JSON_BUILD_VARIANT(JSON_VARIANT_STRING_CONST("zzz"))), - JSON_BUILD_STRV((char**) arr_1234))) >= 0); - - assert_se(json_variant_format(a, 0, &s) >= 0); + assert_se(sd_json_build(&a, SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("x", SD_JSON_BUILD_BOOLEAN(true)), + SD_JSON_BUILD_PAIR("y", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("this", SD_JSON_BUILD_NULL)))), + SD_JSON_BUILD_VARIANT(NULL), + SD_JSON_BUILD_LITERAL(NULL), + SD_JSON_BUILD_STRING(NULL), + SD_JSON_BUILD_NULL, + SD_JSON_BUILD_INTEGER(77), + SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_VARIANT(JSON_VARIANT_STRING_CONST("foobar")), + SD_JSON_BUILD_VARIANT(JSON_VARIANT_STRING_CONST("zzz"))), + SD_JSON_BUILD_STRV((char**) arr_1234))) >= 0); + + assert_se(sd_json_variant_format(a, 0, &s) >= 0); log_info("GOT: %s", s); - assert_se(json_parse(s, 0, &b, NULL, NULL) >= 0); - assert_se(json_variant_equal(a, b)); + assert_se(sd_json_parse(s, 0, &b, NULL, NULL) >= 0); + assert_se(sd_json_variant_equal(a, b)); - a = json_variant_unref(a); - b = json_variant_unref(b); + a = sd_json_variant_unref(a); + b = sd_json_variant_unref(b); - assert_se(json_build(&a, JSON_BUILD_REAL(M_PI)) >= 0); + assert_se(sd_json_build(&a, SD_JSON_BUILD_REAL(M_PI)) >= 0); s = mfree(s); - assert_se(json_variant_format(a, 0, &s) >= 0); + assert_se(sd_json_variant_format(a, 0, &s) >= 0); log_info("GOT: %s", s); - assert_se(json_parse(s, 0, &b, NULL, NULL) >= 0); - assert_se(json_variant_format(b, 0, &t) >= 0); + assert_se(sd_json_parse(s, 0, &b, NULL, NULL) >= 0); + assert_se(sd_json_variant_format(b, 0, &t) >= 0); log_info("GOT: %s", t); ASSERT_STREQ(s, t); - a = json_variant_unref(a); - b = json_variant_unref(b); + a = sd_json_variant_unref(a); + b = sd_json_variant_unref(b); - assert_se(json_build(&a, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("x", JSON_BUILD_STRING("y")), - JSON_BUILD_PAIR("z", JSON_BUILD_CONST_STRING("a")), - JSON_BUILD_PAIR("b", JSON_BUILD_CONST_STRING("c")) + assert_se(sd_json_build(&a, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("x", SD_JSON_BUILD_STRING("y")), + SD_JSON_BUILD_PAIR("z", JSON_BUILD_CONST_STRING("a")), + SD_JSON_BUILD_PAIR("b", JSON_BUILD_CONST_STRING("c")) )) >= 0); - assert_se(json_build(&b, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("x", JSON_BUILD_STRING("y")), - JSON_BUILD_PAIR_CONDITION(false, "p", JSON_BUILD_STRING("q")), - JSON_BUILD_PAIR_CONDITION(true, "z", JSON_BUILD_CONST_STRING("a")), - JSON_BUILD_PAIR_CONDITION(false, "j", JSON_BUILD_ARRAY(JSON_BUILD_STRING("k"), JSON_BUILD_CONST_STRING("u"), JSON_BUILD_CONST_STRING("i"))), - JSON_BUILD_PAIR("b", JSON_BUILD_CONST_STRING("c")) + assert_se(sd_json_build(&b, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("x", SD_JSON_BUILD_STRING("y")), + SD_JSON_BUILD_PAIR_CONDITION(false, "p", SD_JSON_BUILD_STRING("q")), + SD_JSON_BUILD_PAIR_CONDITION(true, "z", JSON_BUILD_CONST_STRING("a")), + SD_JSON_BUILD_PAIR_CONDITION(false, "j", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_STRING("k"), JSON_BUILD_CONST_STRING("u"), JSON_BUILD_CONST_STRING("i"))), + SD_JSON_BUILD_PAIR("b", JSON_BUILD_CONST_STRING("c")) )) >= 0); - assert_se(json_variant_equal(a, b)); + assert_se(sd_json_variant_equal(a, b)); } TEST(json_parse_file_empty) { _cleanup_fclose_ FILE *f = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; assert_se(fopen_unlocked("/dev/null", "re", &f) >= 0); - assert_se(json_parse_file(f, "waldo", 0, &v, NULL, NULL) == -ENODATA); + assert_se(sd_json_parse_file(f, "waldo", 0, &v, NULL, NULL) == -ENODATA); ASSERT_NULL(v); } TEST(json_parse_file_invalid) { _cleanup_fclose_ FILE *f = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; assert_se(f = fmemopen_unlocked((void*) "kookoo", 6, "r")); - assert_se(json_parse_file(f, "waldo", 0, &v, NULL, NULL) == -EINVAL); + assert_se(sd_json_parse_file(f, "waldo", 0, &v, NULL, NULL) == -EINVAL); ASSERT_NULL(v); } @@ -392,7 +394,7 @@ TEST(source) { "}\n"; _cleanup_fclose_ FILE *f = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; printf("--- original begin ---\n" "%s" @@ -400,19 +402,19 @@ TEST(source) { assert_se(f = fmemopen_unlocked((void*) data, strlen(data), "r")); - assert_se(json_parse_file(f, "waldo", 0, &v, NULL, NULL) >= 0); + assert_se(sd_json_parse_file(f, "waldo", 0, &v, NULL, NULL) >= 0); printf("--- non-pretty begin ---\n"); - json_variant_dump(v, 0, stdout, NULL); + sd_json_variant_dump(v, 0, stdout, NULL); printf("\n--- non-pretty end ---\n"); printf("--- pretty begin ---\n"); - json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, stdout, NULL); + sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_SOURCE, stdout, NULL); printf("--- pretty end ---\n"); } TEST(depth) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; int r; v = JSON_VARIANT_STRING_CONST("start"); @@ -420,13 +422,13 @@ TEST(depth) { /* Let's verify that the maximum depth checks work */ for (unsigned i = 0;; i++) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; assert_se(i <= UINT16_MAX); if (i & 1) - r = json_variant_new_array(&w, &v, 1); + r = sd_json_variant_new_array(&w, &v, 1); else - r = json_variant_new_object(&w, (JsonVariant*[]) { JSON_VARIANT_STRING_CONST("key"), v }, 2); + r = sd_json_variant_new_object(&w, (sd_json_variant*[]) { JSON_VARIANT_STRING_CONST("key"), v }, 2); if (r == -ELNRNG) { log_info("max depth at %u", i); break; @@ -441,176 +443,176 @@ TEST(depth) { assert_se(r >= 0); - json_variant_unref(v); + sd_json_variant_unref(v); v = TAKE_PTR(w); } - json_variant_dump(v, 0, stdout, NULL); + sd_json_variant_dump(v, 0, stdout, NULL); fputs("\n", stdout); } TEST(normalize) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; _cleanup_free_ char *t = NULL; - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("b", JSON_BUILD_STRING("x")), - JSON_BUILD_PAIR("c", JSON_BUILD_CONST_STRING("y")), - JSON_BUILD_PAIR("a", JSON_BUILD_CONST_STRING("z")))) >= 0); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_STRING("x")), + SD_JSON_BUILD_PAIR("c", JSON_BUILD_CONST_STRING("y")), + SD_JSON_BUILD_PAIR("a", JSON_BUILD_CONST_STRING("z")))) >= 0); - assert_se(!json_variant_is_sorted(v)); - assert_se(!json_variant_is_normalized(v)); + assert_se(!sd_json_variant_is_sorted(v)); + assert_se(!sd_json_variant_is_normalized(v)); - assert_se(json_variant_format(v, 0, &t) >= 0); + assert_se(sd_json_variant_format(v, 0, &t) >= 0); ASSERT_STREQ(t, "{\"b\":\"x\",\"c\":\"y\",\"a\":\"z\"}"); t = mfree(t); - assert_se(json_build(&w, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("bar", JSON_BUILD_STRING("zzz")), - JSON_BUILD_PAIR("foo", JSON_BUILD_VARIANT(v)))) >= 0); + assert_se(sd_json_build(&w, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("bar", SD_JSON_BUILD_STRING("zzz")), + SD_JSON_BUILD_PAIR("foo", SD_JSON_BUILD_VARIANT(v)))) >= 0); - assert_se(json_variant_is_sorted(w)); - assert_se(!json_variant_is_normalized(w)); + assert_se(sd_json_variant_is_sorted(w)); + assert_se(!sd_json_variant_is_normalized(w)); - assert_se(json_variant_format(w, 0, &t) >= 0); + assert_se(sd_json_variant_format(w, 0, &t) >= 0); ASSERT_STREQ(t, "{\"bar\":\"zzz\",\"foo\":{\"b\":\"x\",\"c\":\"y\",\"a\":\"z\"}}"); t = mfree(t); - assert_se(json_variant_sort(&v) >= 0); - assert_se(json_variant_is_sorted(v)); - assert_se(json_variant_is_normalized(v)); + assert_se(sd_json_variant_sort(&v) >= 0); + assert_se(sd_json_variant_is_sorted(v)); + assert_se(sd_json_variant_is_normalized(v)); - assert_se(json_variant_format(v, 0, &t) >= 0); + assert_se(sd_json_variant_format(v, 0, &t) >= 0); ASSERT_STREQ(t, "{\"a\":\"z\",\"b\":\"x\",\"c\":\"y\"}"); t = mfree(t); - assert_se(json_variant_normalize(&w) >= 0); - assert_se(json_variant_is_sorted(w)); - assert_se(json_variant_is_normalized(w)); + assert_se(sd_json_variant_normalize(&w) >= 0); + assert_se(sd_json_variant_is_sorted(w)); + assert_se(sd_json_variant_is_normalized(w)); - assert_se(json_variant_format(w, 0, &t) >= 0); + assert_se(sd_json_variant_format(w, 0, &t) >= 0); ASSERT_STREQ(t, "{\"bar\":\"zzz\",\"foo\":{\"a\":\"z\",\"b\":\"x\",\"c\":\"y\"}}"); t = mfree(t); } TEST(bisect) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - /* Tests the bisection logic in json_variant_by_key() */ + /* Tests the bisection logic in sd_json_variant_by_key() */ for (char c = 'z'; c >= 'a'; c--) { if ((c % 3) == 0) continue; - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; - assert_se(json_variant_new_stringn(&w, (char[4]) { '<', c, c, '>' }, 4) >= 0); - assert_se(json_variant_set_field(&v, (char[2]) { c, 0 }, w) >= 0); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; + assert_se(sd_json_variant_new_stringn(&w, (char[4]) { '<', c, c, '>' }, 4) >= 0); + assert_se(sd_json_variant_set_field(&v, (char[2]) { c, 0 }, w) >= 0); } - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); - assert_se(!json_variant_is_sorted(v)); - assert_se(!json_variant_is_normalized(v)); - assert_se(json_variant_normalize(&v) >= 0); - assert_se(json_variant_is_sorted(v)); - assert_se(json_variant_is_normalized(v)); + assert_se(!sd_json_variant_is_sorted(v)); + assert_se(!sd_json_variant_is_normalized(v)); + assert_se(sd_json_variant_normalize(&v) >= 0); + assert_se(sd_json_variant_is_sorted(v)); + assert_se(sd_json_variant_is_normalized(v)); - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); for (char c = 'a'; c <= 'z'; c++) { - JsonVariant *k; + sd_json_variant *k; const char *z; - k = json_variant_by_key(v, (char[2]) { c, 0 }); + k = sd_json_variant_by_key(v, (char[2]) { c, 0 }); assert_se(!k == ((c % 3) == 0)); if (!k) continue; - assert_se(json_variant_is_string(k)); + assert_se(sd_json_variant_is_string(k)); z = (char[5]){ '<', c, c, '>', 0}; - ASSERT_STREQ(json_variant_string(k), z); + ASSERT_STREQ(sd_json_variant_string(k), z); } } -static void test_float_match(JsonVariant *v) { +static void test_float_match(sd_json_variant *v) { const double delta = 0.0001; - assert_se(json_variant_is_array(v)); - assert_se(json_variant_elements(v) == 11); - assert_se(fabs(1.0 - (DBL_MIN / json_variant_real(json_variant_by_index(v, 0)))) <= delta); - assert_se(fabs(1.0 - (DBL_MAX / json_variant_real(json_variant_by_index(v, 1)))) <= delta); - assert_se(json_variant_is_null(json_variant_by_index(v, 2))); /* nan is not supported by json → null */ - assert_se(json_variant_is_null(json_variant_by_index(v, 3))); /* +inf is not supported by json → null */ - assert_se(json_variant_is_null(json_variant_by_index(v, 4))); /* -inf is not supported by json → null */ - assert_se(json_variant_is_null(json_variant_by_index(v, 5)) || - fabs(1.0 - (HUGE_VAL / json_variant_real(json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */ - assert_se(json_variant_is_real(json_variant_by_index(v, 6)) && - json_variant_is_integer(json_variant_by_index(v, 6)) && - json_variant_integer(json_variant_by_index(v, 6)) == 0); - assert_se(json_variant_is_real(json_variant_by_index(v, 7)) && - json_variant_is_integer(json_variant_by_index(v, 7)) && - json_variant_integer(json_variant_by_index(v, 7)) == 10); - assert_se(json_variant_is_real(json_variant_by_index(v, 8)) && - json_variant_is_integer(json_variant_by_index(v, 8)) && - json_variant_integer(json_variant_by_index(v, 8)) == -10); - assert_se(json_variant_is_real(json_variant_by_index(v, 9)) && - !json_variant_is_integer(json_variant_by_index(v, 9))); - assert_se(fabs(1.0 - (DBL_MIN / 2 / json_variant_real(json_variant_by_index(v, 9)))) <= delta); - assert_se(json_variant_is_real(json_variant_by_index(v, 10)) && - !json_variant_is_integer(json_variant_by_index(v, 10))); - assert_se(fabs(1.0 - (-DBL_MIN / 2 / json_variant_real(json_variant_by_index(v, 10)))) <= delta); + assert_se(sd_json_variant_is_array(v)); + assert_se(sd_json_variant_elements(v) == 11); + assert_se(fabs(1.0 - (DBL_MIN / sd_json_variant_real(sd_json_variant_by_index(v, 0)))) <= delta); + assert_se(fabs(1.0 - (DBL_MAX / sd_json_variant_real(sd_json_variant_by_index(v, 1)))) <= delta); + assert_se(sd_json_variant_is_null(sd_json_variant_by_index(v, 2))); /* nan is not supported by json → null */ + assert_se(sd_json_variant_is_null(sd_json_variant_by_index(v, 3))); /* +inf is not supported by json → null */ + assert_se(sd_json_variant_is_null(sd_json_variant_by_index(v, 4))); /* -inf is not supported by json → null */ + assert_se(sd_json_variant_is_null(sd_json_variant_by_index(v, 5)) || + fabs(1.0 - (HUGE_VAL / sd_json_variant_real(sd_json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */ + assert_se(sd_json_variant_is_real(sd_json_variant_by_index(v, 6)) && + sd_json_variant_is_integer(sd_json_variant_by_index(v, 6)) && + sd_json_variant_integer(sd_json_variant_by_index(v, 6)) == 0); + assert_se(sd_json_variant_is_real(sd_json_variant_by_index(v, 7)) && + sd_json_variant_is_integer(sd_json_variant_by_index(v, 7)) && + sd_json_variant_integer(sd_json_variant_by_index(v, 7)) == 10); + assert_se(sd_json_variant_is_real(sd_json_variant_by_index(v, 8)) && + sd_json_variant_is_integer(sd_json_variant_by_index(v, 8)) && + sd_json_variant_integer(sd_json_variant_by_index(v, 8)) == -10); + assert_se(sd_json_variant_is_real(sd_json_variant_by_index(v, 9)) && + !sd_json_variant_is_integer(sd_json_variant_by_index(v, 9))); + assert_se(fabs(1.0 - (DBL_MIN / 2 / sd_json_variant_real(sd_json_variant_by_index(v, 9)))) <= delta); + assert_se(sd_json_variant_is_real(sd_json_variant_by_index(v, 10)) && + !sd_json_variant_is_integer(sd_json_variant_by_index(v, 10))); + assert_se(fabs(1.0 - (-DBL_MIN / 2 / sd_json_variant_real(sd_json_variant_by_index(v, 10)))) <= delta); } TEST(float) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; _cleanup_free_ char *text = NULL; - assert_se(json_build(&v, JSON_BUILD_ARRAY( - JSON_BUILD_REAL(DBL_MIN), - JSON_BUILD_REAL(DBL_MAX), - JSON_BUILD_REAL(NAN), - JSON_BUILD_REAL(INFINITY), - JSON_BUILD_REAL(-INFINITY), - JSON_BUILD_REAL(HUGE_VAL), - JSON_BUILD_REAL(0), - JSON_BUILD_REAL(10), - JSON_BUILD_REAL(-10), - JSON_BUILD_REAL(DBL_MIN / 2), - JSON_BUILD_REAL(-DBL_MIN / 2))) >= 0); - - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + assert_se(sd_json_build(&v, SD_JSON_BUILD_ARRAY( + SD_JSON_BUILD_REAL(DBL_MIN), + SD_JSON_BUILD_REAL(DBL_MAX), + SD_JSON_BUILD_REAL(NAN), + SD_JSON_BUILD_REAL(INFINITY), + SD_JSON_BUILD_REAL(-INFINITY), + SD_JSON_BUILD_REAL(HUGE_VAL), + SD_JSON_BUILD_REAL(0), + SD_JSON_BUILD_REAL(10), + SD_JSON_BUILD_REAL(-10), + SD_JSON_BUILD_REAL(DBL_MIN / 2), + SD_JSON_BUILD_REAL(-DBL_MIN / 2))) >= 0); + + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); test_float_match(v); - assert_se(json_variant_format(v, 0, &text) >= 0); - assert_se(json_parse(text, 0, &w, NULL, NULL) >= 0); + assert_se(sd_json_variant_format(v, 0, &text) >= 0); + assert_se(sd_json_parse(text, 0, &w, NULL, NULL) >= 0); - json_variant_dump(w, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + sd_json_variant_dump(w, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); test_float_match(w); } -static void test_equal_text(JsonVariant *v, const char *text) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; +static void test_equal_text(sd_json_variant *v, const char *text) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; - assert_se(json_parse(text, 0, &w, NULL, NULL) >= 0); - assert_se(json_variant_equal(v, w) || (!v && json_variant_is_null(w))); + assert_se(sd_json_parse(text, 0, &w, NULL, NULL) >= 0); + assert_se(sd_json_variant_equal(v, w) || (!v && sd_json_variant_is_null(w))); } TEST(set_field) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; test_equal_text(v, "null"); - assert_se(json_variant_set_field(&v, "foo", NULL) >= 0); + assert_se(sd_json_variant_set_field(&v, "foo", NULL) >= 0); test_equal_text(v, "{\"foo\" : null}"); - assert_se(json_variant_set_field(&v, "bar", JSON_VARIANT_STRING_CONST("quux")) >= 0); + assert_se(sd_json_variant_set_field(&v, "bar", JSON_VARIANT_STRING_CONST("quux")) >= 0); test_equal_text(v, "{\"foo\" : null, \"bar\" : \"quux\"}"); - assert_se(json_variant_set_field(&v, "foo", JSON_VARIANT_STRING_CONST("quux2")) >= 0); + assert_se(sd_json_variant_set_field(&v, "foo", JSON_VARIANT_STRING_CONST("quux2")) >= 0); test_equal_text(v, "{\"foo\" : \"quux2\", \"bar\" : \"quux\"}"); - assert_se(json_variant_set_field(&v, "bar", NULL) >= 0); + assert_se(sd_json_variant_set_field(&v, "bar", NULL) >= 0); test_equal_text(v, "{\"foo\" : \"quux2\", \"bar\" : null}"); } @@ -662,41 +664,41 @@ TEST(variant) { } TEST(json_variant_merge_objectb) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL; - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("b", JSON_BUILD_STRING("x")), - JSON_BUILD_PAIR("c", JSON_BUILD_CONST_STRING("y")), - JSON_BUILD_PAIR("a", JSON_BUILD_CONST_STRING("z")))) >= 0); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_STRING("x")), + SD_JSON_BUILD_PAIR("c", JSON_BUILD_CONST_STRING("y")), + SD_JSON_BUILD_PAIR("a", JSON_BUILD_CONST_STRING("z")))) >= 0); - assert_se(json_variant_merge_objectb(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("b", JSON_BUILD_STRING("x")))) >= 0); - assert_se(json_variant_merge_objectb(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("c", JSON_BUILD_STRING("y")))) >= 0); - assert_se(json_variant_merge_objectb(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_STRING("z")))) >= 0); + assert_se(sd_json_variant_merge_objectb(&w, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_STRING("x")))) >= 0); + assert_se(sd_json_variant_merge_objectb(&w, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_STRING("y")))) >= 0); + assert_se(sd_json_variant_merge_objectb(&w, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_STRING("z")))) >= 0); - assert_se(json_variant_equal(v, w)); + assert_se(sd_json_variant_equal(v, w)); } static void json_array_append_with_source_one(bool source) { - _cleanup_(json_variant_unrefp) JsonVariant *a, *b; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *a, *b; /* Parse two sources, each with a different name and line/column numbers */ - assert_se(json_parse_with_source(" [41]", source ? "string 1" : NULL, 0, + assert_se(sd_json_parse_with_source(" [41]", source ? "string 1" : NULL, 0, &a, NULL, NULL) >= 0); - assert_se(json_parse_with_source("\n\n [42]", source ? "string 2" : NULL, 0, + assert_se(sd_json_parse_with_source("\n\n [42]", source ? "string 2" : NULL, 0, &b, NULL, NULL) >= 0); - assert_se(json_variant_is_array(a)); - assert_se(json_variant_elements(a) == 1); - assert_se(json_variant_is_array(b)); - assert_se(json_variant_elements(b) == 1); + assert_se(sd_json_variant_is_array(a)); + assert_se(sd_json_variant_elements(a) == 1); + assert_se(sd_json_variant_is_array(b)); + assert_se(sd_json_variant_elements(b) == 1); /* Verify source information */ const char *s1, *s2; unsigned line1, col1, line2, col2; - assert_se(json_variant_get_source(a, &s1, &line1, &col1) >= 0); - assert_se(json_variant_get_source(b, &s2, &line2, &col2) >= 0); + assert_se(sd_json_variant_get_source(a, &s1, &line1, &col1) >= 0); + assert_se(sd_json_variant_get_source(b, &s2, &line2, &col2) >= 0); ASSERT_STREQ(s1, source ? "string 1" : NULL); ASSERT_STREQ(s2, source ? "string 2" : NULL); @@ -707,21 +709,21 @@ static void json_array_append_with_source_one(bool source) { /* Append one elem from the second array (and source) to the first. */ - JsonVariant *elem; - assert_se(elem = json_variant_by_index(b, 0)); - assert_se(json_variant_is_integer(elem)); - assert_se(json_variant_elements(elem) == 0); + sd_json_variant *elem; + assert_se(elem = sd_json_variant_by_index(b, 0)); + assert_se(sd_json_variant_is_integer(elem)); + assert_se(sd_json_variant_elements(elem) == 0); - assert_se(json_variant_append_array(&a, elem) >= 0); + assert_se(sd_json_variant_append_array(&a, elem) >= 0); - assert_se(json_variant_is_array(a)); - assert_se(json_variant_elements(a) == 2); + assert_se(sd_json_variant_is_array(a)); + assert_se(sd_json_variant_elements(a) == 2); /* Verify that source information was propagated correctly */ - assert_se(json_variant_get_source(elem, &s1, &line1, &col1) >= 0); - assert_se(elem = json_variant_by_index(a, 1)); - assert_se(json_variant_get_source(elem, &s2, &line2, &col2) >= 0); + assert_se(sd_json_variant_get_source(elem, &s1, &line1, &col1) >= 0); + assert_se(elem = sd_json_variant_by_index(a, 1)); + assert_se(sd_json_variant_get_source(elem, &s2, &line2, &col2) >= 0); ASSERT_STREQ(s1, source ? "string 2" : NULL); ASSERT_STREQ(s2, source ? "string 2" : NULL); @@ -740,28 +742,28 @@ TEST(json_array_append_without_source) { } TEST(json_array_append_nodup) { - _cleanup_(json_variant_unrefp) JsonVariant *l = NULL, *s = NULL, *wd = NULL, *nd = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL, *s = NULL, *wd = NULL, *nd = NULL; - assert_se(json_build(&l, JSON_BUILD_STRV(STRV_MAKE("foo", "bar", "baz", "bar", "baz", "foo", "qux", "baz"))) >= 0); - assert_se(json_build(&s, JSON_BUILD_STRV(STRV_MAKE("foo", "bar", "baz", "qux"))) >= 0); + assert_se(sd_json_build(&l, SD_JSON_BUILD_STRV(STRV_MAKE("foo", "bar", "baz", "bar", "baz", "foo", "qux", "baz"))) >= 0); + assert_se(sd_json_build(&s, SD_JSON_BUILD_STRV(STRV_MAKE("foo", "bar", "baz", "qux"))) >= 0); - assert_se(!json_variant_equal(l, s)); - assert_se(json_variant_elements(l) == 8); - assert_se(json_variant_elements(s) == 4); + assert_se(!sd_json_variant_equal(l, s)); + assert_se(sd_json_variant_elements(l) == 8); + assert_se(sd_json_variant_elements(s) == 4); - JsonVariant *i; + sd_json_variant *i; JSON_VARIANT_ARRAY_FOREACH(i, l) { - assert_se(json_variant_append_array(&wd, i) >= 0); - assert_se(json_variant_append_array_nodup(&nd, i) >= 0); + assert_se(sd_json_variant_append_array(&wd, i) >= 0); + assert_se(sd_json_variant_append_array_nodup(&nd, i) >= 0); } - assert_se(json_variant_elements(wd) == 8); - assert_se(json_variant_equal(l, wd)); - assert_se(!json_variant_equal(s, wd)); + assert_se(sd_json_variant_elements(wd) == 8); + assert_se(sd_json_variant_equal(l, wd)); + assert_se(!sd_json_variant_equal(s, wd)); - assert_se(json_variant_elements(nd) == 4); - assert_se(!json_variant_equal(l, nd)); - assert_se(json_variant_equal(s, nd)); + assert_se(sd_json_variant_elements(nd) == 4); + assert_se(!sd_json_variant_equal(l, nd)); + assert_se(sd_json_variant_equal(s, nd)); } TEST(json_dispatch) { @@ -774,41 +776,41 @@ TEST(json_dispatch) { int16_t k, l; } foobar = {}; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; - - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("a", JSON_BUILD_UNSIGNED(UINT64_MAX)), - JSON_BUILD_PAIR("b", JSON_BUILD_STRING("18446744073709551615")), - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(INT64_MIN)), - JSON_BUILD_PAIR("d", JSON_BUILD_STRING("-9223372036854775808")), - JSON_BUILD_PAIR("e", JSON_BUILD_UNSIGNED(UINT32_MAX)), - JSON_BUILD_PAIR("f", JSON_BUILD_STRING("4294967295")), - JSON_BUILD_PAIR("g", JSON_BUILD_INTEGER(INT32_MIN)), - JSON_BUILD_PAIR("h", JSON_BUILD_STRING("-2147483648")), - JSON_BUILD_PAIR("i", JSON_BUILD_UNSIGNED(UINT16_MAX)), - JSON_BUILD_PAIR("j", JSON_BUILD_STRING("65535")), - JSON_BUILD_PAIR("k", JSON_BUILD_INTEGER(INT16_MIN)), - JSON_BUILD_PAIR("l", JSON_BUILD_STRING("-32768")))) >= 0); - - assert_se(json_variant_dump(v, JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO, stdout, /* prefix= */ NULL) >= 0); - - JsonDispatch table[] = { - { "a", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct foobar, a) }, - { "b", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint64, offsetof(struct foobar, b) }, - { "c", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int64, offsetof(struct foobar, c) }, - { "d", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int64, offsetof(struct foobar, d) }, - { "e", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint32, offsetof(struct foobar, e) }, - { "f", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint32, offsetof(struct foobar, f) }, - { "g", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int32, offsetof(struct foobar, g) }, - { "h", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int32, offsetof(struct foobar, h) }, - { "i", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint16, offsetof(struct foobar, i) }, - { "j", _JSON_VARIANT_TYPE_INVALID, json_dispatch_uint16, offsetof(struct foobar, j) }, - { "k", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int16, offsetof(struct foobar, k) }, - { "l", _JSON_VARIANT_TYPE_INVALID, json_dispatch_int16, offsetof(struct foobar, l) }, + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; + + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_UNSIGNED(UINT64_MAX)), + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_STRING("18446744073709551615")), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(INT64_MIN)), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_STRING("-9223372036854775808")), + SD_JSON_BUILD_PAIR("e", SD_JSON_BUILD_UNSIGNED(UINT32_MAX)), + SD_JSON_BUILD_PAIR("f", SD_JSON_BUILD_STRING("4294967295")), + SD_JSON_BUILD_PAIR("g", SD_JSON_BUILD_INTEGER(INT32_MIN)), + SD_JSON_BUILD_PAIR("h", SD_JSON_BUILD_STRING("-2147483648")), + SD_JSON_BUILD_PAIR("i", SD_JSON_BUILD_UNSIGNED(UINT16_MAX)), + SD_JSON_BUILD_PAIR("j", SD_JSON_BUILD_STRING("65535")), + SD_JSON_BUILD_PAIR("k", SD_JSON_BUILD_INTEGER(INT16_MIN)), + SD_JSON_BUILD_PAIR("l", SD_JSON_BUILD_STRING("-32768")))) >= 0); + + assert_se(sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO, stdout, /* prefix= */ NULL) >= 0); + + sd_json_dispatch_field table[] = { + { "a", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct foobar, a) }, + { "b", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct foobar, b) }, + { "c", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int64, offsetof(struct foobar, c) }, + { "d", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int64, offsetof(struct foobar, d) }, + { "e", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(struct foobar, e) }, + { "f", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(struct foobar, f) }, + { "g", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int32, offsetof(struct foobar, g) }, + { "h", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int32, offsetof(struct foobar, h) }, + { "i", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(struct foobar, i) }, + { "j", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(struct foobar, j) }, + { "k", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int16, offsetof(struct foobar, k) }, + { "l", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int16, offsetof(struct foobar, l) }, {} }; - assert_se(json_dispatch(v, table, JSON_LOG, &foobar) >= 0); + assert_se(sd_json_dispatch(v, table, SD_JSON_LOG, &foobar) >= 0); assert_se(foobar.a == UINT64_MAX); assert_se(foobar.b == UINT64_MAX); @@ -851,20 +853,20 @@ TEST(json_dispatch_enum_define) { .d = mybar, }; - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; - assert_se(json_build(&j, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("a", JSON_BUILD_STRING("mybaz")), - JSON_BUILD_PAIR("b", JSON_BUILD_STRING("mybar")), - JSON_BUILD_PAIR("c", JSON_BUILD_STRING("myfoo")), - JSON_BUILD_PAIR("d", JSON_BUILD_NULL))) >= 0); + assert_se(sd_json_build(&j, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_STRING("mybaz")), + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_STRING("mybar")), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_STRING("myfoo")), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_NULL))) >= 0); - assert_se(json_dispatch(j, - (const JsonDispatch[]) { - { "a", _JSON_VARIANT_TYPE_INVALID, dispatch_mytestenum, offsetof(struct data, a), 0 }, - { "b", _JSON_VARIANT_TYPE_INVALID, dispatch_mytestenum, offsetof(struct data, b), 0 }, - { "c", _JSON_VARIANT_TYPE_INVALID, dispatch_mytestenum, offsetof(struct data, c), 0 }, - { "d", _JSON_VARIANT_TYPE_INVALID, dispatch_mytestenum, offsetof(struct data, d), 0 }, + assert_se(sd_json_dispatch(j, + (const sd_json_dispatch_field[]) { + { "a", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_mytestenum, offsetof(struct data, a), 0 }, + { "b", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_mytestenum, offsetof(struct data, b), 0 }, + { "c", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_mytestenum, offsetof(struct data, c), 0 }, + { "d", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_mytestenum, offsetof(struct data, d), 0 }, {}, }, /* flags= */ 0, @@ -877,116 +879,116 @@ TEST(json_dispatch_enum_define) { } TEST(json_sensitive) { - _cleanup_(json_variant_unrefp) JsonVariant *a = NULL, *b = NULL, *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL, *v = NULL; _cleanup_free_ char *s = NULL; int r; - assert_se(json_build(&a, JSON_BUILD_STRV(STRV_MAKE("foo", "bar", "baz", "bar", "baz", "foo", "qux", "baz"))) >= 0); - assert_se(json_build(&b, JSON_BUILD_STRV(STRV_MAKE("foo", "bar", "baz", "qux"))) >= 0); + assert_se(sd_json_build(&a, SD_JSON_BUILD_STRV(STRV_MAKE("foo", "bar", "baz", "bar", "baz", "foo", "qux", "baz"))) >= 0); + assert_se(sd_json_build(&b, SD_JSON_BUILD_STRV(STRV_MAKE("foo", "bar", "baz", "qux"))) >= 0); - json_variant_sensitive(a); + sd_json_variant_sensitive(a); - assert_se(json_variant_format(a, JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); + assert_se(sd_json_variant_format(a, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); ASSERT_STREQ(s, "\"<sensitive data>\""); s = mfree(s); - r = json_variant_format(b, JSON_FORMAT_CENSOR_SENSITIVE, &s); + r = sd_json_variant_format(b, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s); assert_se(r >= 0); assert_se(s); assert_se((size_t) r == strlen(s)); s = mfree(s); - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(INT64_MIN)), - JSON_BUILD_PAIR("d", JSON_BUILD_STRING("-9223372036854775808")), - JSON_BUILD_PAIR("e", JSON_BUILD_EMPTY_OBJECT))) >= 0); - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(INT64_MIN)), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_STRING("-9223372036854775808")), + SD_JSON_BUILD_PAIR("e", SD_JSON_BUILD_EMPTY_OBJECT))) >= 0); + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); - r = json_variant_format(v, JSON_FORMAT_CENSOR_SENSITIVE, &s); + r = sd_json_variant_format(v, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s); assert_se(r >= 0); assert_se(s); assert_se((size_t) r == strlen(s)); s = mfree(s); - v = json_variant_unref(v); + v = sd_json_variant_unref(v); - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("b", b), - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(INT64_MIN)), - JSON_BUILD_PAIR("d", JSON_BUILD_STRING("-9223372036854775808")), - JSON_BUILD_PAIR("e", JSON_BUILD_EMPTY_OBJECT))) >= 0); - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("b", b), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(INT64_MIN)), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_STRING("-9223372036854775808")), + SD_JSON_BUILD_PAIR("e", SD_JSON_BUILD_EMPTY_OBJECT))) >= 0); + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); - r = json_variant_format(v, JSON_FORMAT_CENSOR_SENSITIVE, &s); + r = sd_json_variant_format(v, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s); assert_se(r >= 0); assert_se(s); assert_se((size_t) r == strlen(s)); s = mfree(s); - v = json_variant_unref(v); + v = sd_json_variant_unref(v); - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("b", b), - JSON_BUILD_PAIR_VARIANT("a", a), - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(INT64_MIN)), - JSON_BUILD_PAIR("d", JSON_BUILD_STRING("-9223372036854775808")), - JSON_BUILD_PAIR("e", JSON_BUILD_EMPTY_OBJECT))) >= 0); - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("b", b), + SD_JSON_BUILD_PAIR_VARIANT("a", a), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(INT64_MIN)), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_STRING("-9223372036854775808")), + SD_JSON_BUILD_PAIR("e", SD_JSON_BUILD_EMPTY_OBJECT))) >= 0); + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); - assert_se(json_variant_format(v, JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); + assert_se(sd_json_variant_format(v, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); ASSERT_STREQ(s, "{\"b\":[\"foo\",\"bar\",\"baz\",\"qux\"],\"a\":\"<sensitive data>\",\"c\":-9223372036854775808,\"d\":\"-9223372036854775808\",\"e\":{}}"); s = mfree(s); - v = json_variant_unref(v); + v = sd_json_variant_unref(v); - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("b", b), - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(INT64_MIN)), - JSON_BUILD_PAIR_VARIANT("a", a), - JSON_BUILD_PAIR("d", JSON_BUILD_STRING("-9223372036854775808")), - JSON_BUILD_PAIR("e", JSON_BUILD_EMPTY_OBJECT))) >= 0); - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("b", b), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(INT64_MIN)), + SD_JSON_BUILD_PAIR_VARIANT("a", a), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_STRING("-9223372036854775808")), + SD_JSON_BUILD_PAIR("e", SD_JSON_BUILD_EMPTY_OBJECT))) >= 0); + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); - assert_se(json_variant_format(v, JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); + assert_se(sd_json_variant_format(v, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); ASSERT_STREQ(s, "{\"b\":[\"foo\",\"bar\",\"baz\",\"qux\"],\"c\":-9223372036854775808,\"a\":\"<sensitive data>\",\"d\":\"-9223372036854775808\",\"e\":{}}"); s = mfree(s); - v = json_variant_unref(v); + v = sd_json_variant_unref(v); - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("b", b), - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(INT64_MIN)), - JSON_BUILD_PAIR("d", JSON_BUILD_STRING("-9223372036854775808")), - JSON_BUILD_PAIR_VARIANT("a", a), - JSON_BUILD_PAIR("e", JSON_BUILD_EMPTY_OBJECT))) >= 0); - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("b", b), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(INT64_MIN)), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_STRING("-9223372036854775808")), + SD_JSON_BUILD_PAIR_VARIANT("a", a), + SD_JSON_BUILD_PAIR("e", SD_JSON_BUILD_EMPTY_OBJECT))) >= 0); + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); - assert_se(json_variant_format(v, JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); + assert_se(sd_json_variant_format(v, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); ASSERT_STREQ(s, "{\"b\":[\"foo\",\"bar\",\"baz\",\"qux\"],\"c\":-9223372036854775808,\"d\":\"-9223372036854775808\",\"a\":\"<sensitive data>\",\"e\":{}}"); s = mfree(s); - v = json_variant_unref(v); + v = sd_json_variant_unref(v); - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_VARIANT("b", b), - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(INT64_MIN)), - JSON_BUILD_PAIR("d", JSON_BUILD_STRING("-9223372036854775808")), - JSON_BUILD_PAIR("e", JSON_BUILD_EMPTY_OBJECT), - JSON_BUILD_PAIR_VARIANT("a", a))) >= 0); - json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_VARIANT("b", b), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(INT64_MIN)), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_STRING("-9223372036854775808")), + SD_JSON_BUILD_PAIR("e", SD_JSON_BUILD_EMPTY_OBJECT), + SD_JSON_BUILD_PAIR_VARIANT("a", a))) >= 0); + sd_json_variant_dump(v, SD_JSON_FORMAT_COLOR|SD_JSON_FORMAT_PRETTY, NULL, NULL); - assert_se(json_variant_format(v, JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); + assert_se(sd_json_variant_format(v, SD_JSON_FORMAT_CENSOR_SENSITIVE, &s) >= 0); ASSERT_STREQ(s, "{\"b\":[\"foo\",\"bar\",\"baz\",\"qux\"],\"c\":-9223372036854775808,\"d\":\"-9223372036854775808\",\"e\":{},\"a\":\"<sensitive data>\"}"); } TEST(json_iovec) { struct iovec iov1 = CONST_IOVEC_MAKE_STRING("üxknürz"), iov2 = CONST_IOVEC_MAKE_STRING("wuffwuffmiau"); - _cleanup_(json_variant_unrefp) JsonVariant *j = NULL; - assert_se(json_build(&j, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("nr1", JSON_BUILD_IOVEC_BASE64(&iov1)), - JSON_BUILD_PAIR("nr2", JSON_BUILD_IOVEC_HEX(&iov2)))) >= 0); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; + assert_se(sd_json_build(&j, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("nr1", JSON_BUILD_IOVEC_BASE64(&iov1)), + SD_JSON_BUILD_PAIR("nr2", JSON_BUILD_IOVEC_HEX(&iov2)))) >= 0); - json_variant_dump(j, JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO, /* f= */ NULL, /* prefix= */ NULL); + sd_json_variant_dump(j, SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO, /* f= */ NULL, /* prefix= */ NULL); _cleanup_(iovec_done) struct iovec a = {}, b = {}; - assert_se(json_variant_unbase64_iovec(json_variant_by_key(j, "nr1"), &a) >= 0); - assert_se(json_variant_unhex_iovec(json_variant_by_key(j, "nr2"), &b) >= 0); + assert_se(json_variant_unbase64_iovec(sd_json_variant_by_key(j, "nr1"), &a) >= 0); + assert_se(json_variant_unhex_iovec(sd_json_variant_by_key(j, "nr2"), &b) >= 0); assert_se(iovec_memcmp(&iov1, &a) == 0); assert_se(iovec_memcmp(&iov2, &b) == 0); diff --git a/src/test/test-parse-argument.c b/src/test/test-parse-argument.c index c07b2d9afe..9fb1a17b24 100644 --- a/src/test/test-parse-argument.c +++ b/src/test/test-parse-argument.c @@ -7,13 +7,13 @@ #include "tests.h" TEST(parse_json_argument) { - JsonFormatFlags flags = JSON_FORMAT_PRETTY; + sd_json_format_flags_t flags = SD_JSON_FORMAT_PRETTY; assert_se(parse_json_argument("help", &flags) == 0); - assert_se(flags == JSON_FORMAT_PRETTY); + assert_se(flags == SD_JSON_FORMAT_PRETTY); assert_se(parse_json_argument("off", &flags) == 1); - assert_se(flags == JSON_FORMAT_OFF); + assert_se(flags == SD_JSON_FORMAT_OFF); } TEST(parse_path_argument) { diff --git a/src/test/test-varlink-idl.c b/src/test/test-varlink-idl.c index 86dbcc963c..34d84145fc 100644 --- a/src/test/test-varlink-idl.c +++ b/src/test/test-varlink-idl.c @@ -252,15 +252,15 @@ TEST(validate_json) { assert_se(varlink_idl_parse(text, NULL, NULL, &parsed) >= 0); test_parse_format_one(parsed); - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - assert_se(json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("a", JSON_BUILD_STRING("x")), - JSON_BUILD_PAIR("b", JSON_BUILD_UNSIGNED(44)), - JSON_BUILD_PAIR("d", JSON_BUILD_ARRAY(JSON_BUILD_UNSIGNED(5), JSON_BUILD_UNSIGNED(7), JSON_BUILD_UNSIGNED(107))), - JSON_BUILD_PAIR("g", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("f", JSON_BUILD_REAL(0.5f)))))) >= 0); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_STRING("x")), + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_UNSIGNED(44)), + SD_JSON_BUILD_PAIR("d", SD_JSON_BUILD_ARRAY(SD_JSON_BUILD_UNSIGNED(5), SD_JSON_BUILD_UNSIGNED(7), SD_JSON_BUILD_UNSIGNED(107))), + SD_JSON_BUILD_PAIR("g", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("f", SD_JSON_BUILD_REAL(0.5f)))))) >= 0); - json_variant_dump(v, JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO, stdout, NULL); + sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO, stdout, NULL); const VarlinkSymbol* symbol = ASSERT_PTR(varlink_idl_find_symbol(parsed, VARLINK_METHOD, "Mymethod")); @@ -296,16 +296,16 @@ TEST(recursive) { assert_se(test_recursive_one(20000) < 0 ); } -static int test_method(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - JsonVariant *foo = json_variant_by_key(parameters, "foo"), *bar = json_variant_by_key(parameters, "bar"); +static int test_method(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + sd_json_variant *foo = sd_json_variant_by_key(parameters, "foo"), *bar = sd_json_variant_by_key(parameters, "bar"); return varlink_replyb(link, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("waldo", json_variant_unsigned(foo) * json_variant_unsigned(bar)), - JSON_BUILD_PAIR_UNSIGNED("quux", json_variant_unsigned(foo) + json_variant_unsigned(bar)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("waldo", sd_json_variant_unsigned(foo) * sd_json_variant_unsigned(bar)), + SD_JSON_BUILD_PAIR_UNSIGNED("quux", sd_json_variant_unsigned(foo) + sd_json_variant_unsigned(bar)))); } -static int done_method(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int done_method(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { assert_se(sd_event_exit(varlink_get_event(link), 0) >= 0); return 0; } @@ -354,45 +354,45 @@ TEST(validate_method_call) { assert_se(pthread_create(&t, NULL, server_thread, FD_TO_PTR(TAKE_FD(fd[1]))) == 0); assert_se(varlink_connect_fd(&v, TAKE_FD(fd[0])) >= 0); - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; const char *error_id = NULL; assert_se(varlink_callb(v, "xyz.TestMethod", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("foo", 8), - JSON_BUILD_PAIR_UNSIGNED("bar", 9))) >= 0); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("foo", 8), + SD_JSON_BUILD_PAIR_UNSIGNED("bar", 9))) >= 0); - _cleanup_(json_variant_unrefp) JsonVariant *expected_reply = NULL; - assert_se(json_build(&expected_reply, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("waldo", 8*9), - JSON_BUILD_PAIR_UNSIGNED("quux", 8+9))) >= 0); + _cleanup_(sd_json_variant_unrefp) sd_json_variant *expected_reply = NULL; + assert_se(sd_json_build(&expected_reply, + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("waldo", 8*9), + SD_JSON_BUILD_PAIR_UNSIGNED("quux", 8+9))) >= 0); assert_se(!error_id); - json_variant_dump(reply, JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO, NULL, NULL); - json_variant_dump(expected_reply, JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO, NULL, NULL); - assert_se(json_variant_equal(reply, expected_reply)); + sd_json_variant_dump(reply, SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO, NULL, NULL); + sd_json_variant_dump(expected_reply, SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO, NULL, NULL); + assert_se(sd_json_variant_equal(reply, expected_reply)); assert_se(varlink_callb(v, "xyz.TestMethod", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("foo", 9), - JSON_BUILD_PAIR_UNSIGNED("bar", 8), - JSON_BUILD_PAIR_STRING("optional", "pfft"))) >= 0); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("foo", 9), + SD_JSON_BUILD_PAIR_UNSIGNED("bar", 8), + SD_JSON_BUILD_PAIR_STRING("optional", "pfft"))) >= 0); assert_se(!error_id); - assert_se(json_variant_equal(reply, expected_reply)); + assert_se(sd_json_variant_equal(reply, expected_reply)); assert_se(varlink_callb(v, "xyz.TestMethod", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_UNSIGNED("foo", 8), - JSON_BUILD_PAIR_UNSIGNED("bar", 9), - JSON_BUILD_PAIR_STRING("zzz", "pfft"))) >= 0); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_UNSIGNED("foo", 8), + SD_JSON_BUILD_PAIR_UNSIGNED("bar", 9), + SD_JSON_BUILD_PAIR_STRING("zzz", "pfft"))) >= 0); ASSERT_STREQ(error_id, VARLINK_ERROR_INVALID_PARAMETER); assert_se(varlink_callb(v, "xyz.TestMethod", &reply, &error_id, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_BOOLEAN("foo", true), - JSON_BUILD_PAIR_UNSIGNED("bar", 9))) >= 0); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_BOOLEAN("foo", true), + SD_JSON_BUILD_PAIR_UNSIGNED("bar", 9))) >= 0); ASSERT_STREQ(error_id, VARLINK_ERROR_INVALID_PARAMETER); assert_se(varlink_send(v, "xyz.Done", NULL) >= 0); diff --git a/src/test/test-varlink.c b/src/test/test-varlink.c index 8ad5757b9d..48fc4a7e7f 100644 --- a/src/test/test-varlink.c +++ b/src/test/test-varlink.c @@ -5,10 +5,11 @@ #include <pthread.h> #include "sd-event.h" +#include "sd-json.h" #include "data-fd-util.h" #include "fd-util.h" -#include "json.h" +#include "json-util.h" #include "rm-rf.h" #include "strv.h" #include "tests.h" @@ -25,33 +26,33 @@ static int n_done = 0; static int block_write_fd = -EBADF; -static int method_something(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *ret = NULL; - JsonVariant *a, *b; +static int method_something(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; + sd_json_variant *a, *b; int64_t x, y; int r; - a = json_variant_by_key(parameters, "a"); + a = sd_json_variant_by_key(parameters, "a"); if (!a) return varlink_error(link, "io.test.BadParameters", NULL); - x = json_variant_integer(a); + x = sd_json_variant_integer(a); - b = json_variant_by_key(parameters, "b"); + b = sd_json_variant_by_key(parameters, "b"); if (!b) return varlink_error(link, "io.test.BadParameters", NULL); - y = json_variant_integer(b); + y = sd_json_variant_integer(b); - r = json_build(&ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("sum", JSON_BUILD_INTEGER(x + y)))); + r = sd_json_build(&ret, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("sum", SD_JSON_BUILD_INTEGER(x + y)))); if (r < 0) return r; return varlink_reply(link, ret); } -static int method_something_more(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *ret = NULL; +static int method_something_more(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; int r; struct Something { @@ -59,9 +60,9 @@ static int method_something_more(Varlink *link, JsonVariant *parameters, Varlink int y; }; - static const JsonDispatch dispatch_table[] = { - { "a", JSON_VARIANT_INTEGER, json_dispatch_int, offsetof(struct Something, x), JSON_MANDATORY }, - { "b", JSON_VARIANT_INTEGER, json_dispatch_int, offsetof(struct Something, y), JSON_MANDATORY}, + static const sd_json_dispatch_field dispatch_table[] = { + { "a", SD_JSON_VARIANT_INTEGER, sd_json_dispatch_int, offsetof(struct Something, x), SD_JSON_MANDATORY }, + { "b", SD_JSON_VARIANT_INTEGER, sd_json_dispatch_int, offsetof(struct Something, y), SD_JSON_MANDATORY}, {} }; struct Something s = {}; @@ -71,9 +72,9 @@ static int method_something_more(Varlink *link, JsonVariant *parameters, Varlink return r; for (int i = 0; i < 5; i++) { - _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *w = NULL; - r = json_build(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("sum", JSON_BUILD_INTEGER(s.x + (s.y * i))))); + r = sd_json_build(&w, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("sum", SD_JSON_BUILD_INTEGER(s.x + (s.y * i))))); if (r < 0) return r; @@ -82,7 +83,7 @@ static int method_something_more(Varlink *link, JsonVariant *parameters, Varlink return r; } - r = json_build(&ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("sum", JSON_BUILD_INTEGER(s.x + (s.y * 5))))); + r = sd_json_build(&ret, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("sum", SD_JSON_BUILD_INTEGER(s.x + (s.y * 5))))); if (r < 0) return r; @@ -98,16 +99,16 @@ static void test_fd(int fd, const void *buf, size_t n) { assert_se(memcmp_nn(buf, n, rbuf, m) == 0); } -static int method_passfd(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *ret = NULL; - JsonVariant *a; +static int method_passfd(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *ret = NULL; + sd_json_variant *a; int r; - a = json_variant_by_key(parameters, "fd"); + a = sd_json_variant_by_key(parameters, "fd"); if (!a) return varlink_error(link, "io.test.BadParameters", NULL); - ASSERT_STREQ(json_variant_string(a), "whoop"); + ASSERT_STREQ(sd_json_variant_string(a), "whoop"); int xx = varlink_peek_fd(link, 0), yy = varlink_peek_fd(link, 1), @@ -129,7 +130,7 @@ static int method_passfd(Varlink *link, JsonVariant *parameters, VarlinkMethodFl assert_se(vv >= 0); assert_se(ww >= 0); - r = json_build(&ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("yo", JSON_BUILD_INTEGER(88)))); + r = sd_json_build(&ret, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("yo", SD_JSON_BUILD_INTEGER(88)))); if (r < 0) return r; @@ -142,7 +143,7 @@ static int method_passfd(Varlink *link, JsonVariant *parameters, VarlinkMethodFl return varlink_reply(link, ret); } -static int method_done(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int method_done(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { if (++n_done == 2) sd_event_exit(varlink_get_event(link), EXIT_FAILURE); @@ -150,12 +151,12 @@ static int method_done(Varlink *link, JsonVariant *parameters, VarlinkMethodFlag return 0; } -static int reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { - JsonVariant *sum; +static int reply(Varlink *link, sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { + sd_json_variant *sum; - sum = json_variant_by_key(parameters, "sum"); + sum = sd_json_variant_by_key(parameters, "sum"); - assert_se(json_variant_integer(sum) == 7+22); + assert_se(sd_json_variant_integer(sum) == 7+22); if (++n_done == 2) sd_event_exit(varlink_get_event(link), EXIT_FAILURE); @@ -177,7 +178,7 @@ static int on_connect(VarlinkServer *s, Varlink *link, void *userdata) { return 0; } -static int overload_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { +static int overload_reply(Varlink *link, sd_json_variant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { /* This method call reply should always be called with a disconnection, since the method call should * be talking to an overloaded server */ @@ -213,7 +214,7 @@ static void flood_test(const char *address) { assert_se(asprintf(&t, "flood-%zu", k) >= 0); assert_se(varlink_set_description(connections[k], t) >= 0); assert_se(varlink_attach_event(connections[k], e, k) >= 0); - assert_se(varlink_sendb(connections[k], "io.test.Rubbish", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("id", JSON_BUILD_INTEGER(k)))) >= 0); + assert_se(varlink_sendb(connections[k], "io.test.Rubbish", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("id", SD_JSON_BUILD_INTEGER(k)))) >= 0); } /* Then, create one more, which should fail */ @@ -222,7 +223,7 @@ static void flood_test(const char *address) { assert_se(varlink_set_description(c, "overload-client") >= 0); assert_se(varlink_attach_event(c, e, k) >= 0); assert_se(varlink_bind_reply(c, overload_reply) >= 0); - assert_se(varlink_invokeb(c, "io.test.Overload", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("foo", JSON_BUILD_CONST_STRING("bar")))) >= 0); + assert_se(varlink_invokeb(c, "io.test.Overload", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("foo", JSON_BUILD_CONST_STRING("bar")))) >= 0); /* Unblock it */ log_debug("Unblocking server..."); @@ -238,15 +239,15 @@ static void flood_test(const char *address) { static void *thread(void *arg) { _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *i = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *wrong = NULL; - JsonVariant *o = NULL, *k = NULL, *j = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *i = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *wrong = NULL; + sd_json_variant *o = NULL, *k = NULL, *j = NULL; const char *error_id; const char *e; int x = 0; - assert_se(json_build(&i, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(88)), - JSON_BUILD_PAIR("b", JSON_BUILD_INTEGER(99)))) >= 0); + assert_se(sd_json_build(&i, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_INTEGER(88)), + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_INTEGER(99)))) >= 0); assert_se(varlink_connect_address(&c, arg) >= 0); assert_se(varlink_set_description(c, "thread-client") >= 0); @@ -254,8 +255,8 @@ static void *thread(void *arg) { assert_se(varlink_set_allow_fd_passing_output(c, true) >= 0); /* Test that client is able to perform two sequential varlink_collect calls if first resulted in an error */ - assert_se(json_build(&wrong, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(88)), - JSON_BUILD_PAIR("c", JSON_BUILD_INTEGER(99)))) >= 0); + assert_se(sd_json_build(&wrong, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_INTEGER(88)), + SD_JSON_BUILD_PAIR("c", SD_JSON_BUILD_INTEGER(99)))) >= 0); assert_se(varlink_collect(c, "io.test.DoSomethingMore", wrong, &j, &error_id) >= 0); assert_se(strcmp_ptr(error_id, "org.varlink.service.InvalidParameter") == 0); @@ -263,16 +264,16 @@ static void *thread(void *arg) { assert_se(varlink_collect(c, "io.test.DoSomethingMore", i, &j, &error_id) >= 0); assert_se(!error_id); - assert_se(json_variant_is_array(j) && !json_variant_is_blank_array(j)); + assert_se(sd_json_variant_is_array(j) && !sd_json_variant_is_blank_array(j)); JSON_VARIANT_ARRAY_FOREACH(k, j) { - assert_se(json_variant_integer(json_variant_by_key(k, "sum")) == 88 + (99 * x)); + assert_se(sd_json_variant_integer(sd_json_variant_by_key(k, "sum")) == 88 + (99 * x)); x++; } assert_se(x == 6); assert_se(varlink_call(c, "io.test.DoSomething", i, &o, &e) >= 0); - assert_se(json_variant_integer(json_variant_by_key(o, "sum")) == 88 + 99); + assert_se(sd_json_variant_integer(sd_json_variant_by_key(o, "sum")) == 88 + 99); assert_se(!e); int fd1 = acquire_data_fd("foo"); @@ -287,7 +288,7 @@ static void *thread(void *arg) { assert_se(varlink_push_fd(c, fd2) == 1); assert_se(varlink_push_fd(c, fd3) == 2); - assert_se(varlink_callb(c, "io.test.PassFD", &o, &e, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("fd", JSON_BUILD_STRING("whoop")))) >= 0); + assert_se(varlink_callb(c, "io.test.PassFD", &o, &e, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("fd", SD_JSON_BUILD_STRING("whoop")))) >= 0); int fd4 = varlink_peek_fd(c, 0); int fd5 = varlink_peek_fd(c, 1); @@ -298,8 +299,8 @@ static void *thread(void *arg) { test_fd(fd4, "miau", 4); test_fd(fd5, "wuff", 4); - assert_se(varlink_callb(c, "io.test.IDontExist", &o, &e, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("x", JSON_BUILD_REAL(5.5)))) >= 0); - ASSERT_STREQ(json_variant_string(json_variant_by_key(o, "method")), "io.test.IDontExist"); + assert_se(varlink_callb(c, "io.test.IDontExist", &o, &e, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("x", SD_JSON_BUILD_REAL(5.5)))) >= 0); + ASSERT_STREQ(sd_json_variant_string(sd_json_variant_by_key(o, "method")), "io.test.IDontExist"); ASSERT_STREQ(e, VARLINK_ERROR_METHOD_NOT_FOUND); flood_test(arg); @@ -331,7 +332,7 @@ int main(int argc, char *argv[]) { _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL; _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL; _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(sd_event_unrefp) sd_event *e = NULL; _cleanup_close_pair_ int block_fds[2] = EBADF_PAIR; pthread_t t; @@ -361,8 +362,8 @@ int main(int argc, char *argv[]) { assert_se(varlink_server_attach_event(s, e, 0) >= 0); assert_se(varlink_server_set_connections_max(s, OVERLOAD_CONNECTIONS) >= 0); - assert_se(json_build(&v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(7)), - JSON_BUILD_PAIR("b", JSON_BUILD_INTEGER(22)))) >= 0); + assert_se(sd_json_build(&v, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("a", SD_JSON_BUILD_INTEGER(7)), + SD_JSON_BUILD_PAIR("b", SD_JSON_BUILD_INTEGER(22)))) >= 0); assert_se(varlink_connect_address(&c, sp) >= 0); assert_se(varlink_set_description(c, "main-client") >= 0); diff --git a/src/udev/udevadm-info.c b/src/udev/udevadm-info.c index 7c3c0cd202..1d105047c9 100644 --- a/src/udev/udevadm-info.c +++ b/src/udev/udevadm-info.c @@ -10,6 +10,7 @@ #include <unistd.h> #include "sd-device.h" +#include "sd-json.h" #include "alloc-util.h" #include "device-enumerator-private.h" @@ -21,7 +22,6 @@ #include "fd-util.h" #include "fileio.h" #include "glyph-util.h" -#include "json.h" #include "pager.h" #include "parse-argument.h" #include "sort-util.h" @@ -56,7 +56,7 @@ static bool arg_value = false; static const char *arg_export_prefix = NULL; static usec_t arg_wait_for_initialization_timeout = 0; PagerFlags arg_pager_flags = 0; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; /* Put a limit on --tree descent level to not exhaust our stack */ #define TREE_DEPTH_MAX 64 @@ -265,8 +265,8 @@ static int print_record(sd_device *device, const char *prefix) { return 0; } -static int record_to_json(sd_device *device, JsonVariant **ret) { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; +static int record_to_json(sd_device *device, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; const char *str; int r; @@ -277,19 +277,19 @@ static int record_to_json(sd_device *device, JsonVariant **ret) { * all the other ones have a matching property which will already be included. */ if (sd_device_get_sysname(device, &str) >= 0) { - r = json_variant_set_field_string(&v, "SYSNAME", str); + r = sd_json_variant_set_field_string(&v, "SYSNAME", str); if (r < 0) return r; } if (sd_device_get_sysnum(device, &str) >= 0) { - r = json_variant_set_field_string(&v, "SYSNUM", str); + r = sd_json_variant_set_field_string(&v, "SYSNUM", str); if (r < 0) return r; } FOREACH_DEVICE_PROPERTY(device, key, val) { - r = json_variant_set_field_string(&v, key, val); + r = sd_json_variant_set_field_string(&v, key, val); if (r < 0) return r; } @@ -331,16 +331,16 @@ static int export_devices(sd_device_enumerator *e) { pager_open(arg_pager_flags); FOREACH_DEVICE_AND_SUBSYSTEM(e, d) - if (arg_json_format_flags & JSON_FORMAT_OFF) + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) (void) print_record(d, NULL); else { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = record_to_json(d, &v); if (r < 0) return r; - (void) json_variant_dump(v, arg_json_format_flags, stdout, NULL); + (void) sd_json_variant_dump(v, arg_json_format_flags, stdout, NULL); } return 0; @@ -507,16 +507,16 @@ static int query_device(QueryType query, sd_device* device) { return 0; case QUERY_ALL: - if (arg_json_format_flags & JSON_FORMAT_OFF) + if (arg_json_format_flags & SD_JSON_FORMAT_OFF) return print_record(device, NULL); else { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; r = record_to_json(device, &v); if (r < 0) return r; - (void) json_variant_dump(v, arg_json_format_flags, stdout, NULL); + (void) sd_json_variant_dump(v, arg_json_format_flags, stdout, NULL); } return 0; diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index 1718419407..b2013bf80d 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -36,7 +36,7 @@ static PagerFlags arg_pager_flags = 0; static bool arg_legend = true; static char** arg_services = NULL; static UserDBFlags arg_userdb_flags = 0; -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static bool arg_chain = false; STATIC_DESTRUCTOR_REGISTER(arg_services, strv_freep); @@ -86,7 +86,7 @@ static int show_user(UserRecord *ur, Table *table) { break; case OUTPUT_JSON: - json_variant_dump(ur->json, arg_json_format_flags, NULL, 0); + sd_json_variant_dump(ur->json, arg_json_format_flags, NULL, 0); break; case OUTPUT_FRIENDLY: @@ -486,7 +486,7 @@ static int show_group(GroupRecord *gr, Table *table) { } case OUTPUT_JSON: - json_variant_dump(gr->json, arg_json_format_flags, NULL, 0); + sd_json_variant_dump(gr->json, arg_json_format_flags, NULL, 0); break; case OUTPUT_FRIENDLY: @@ -780,15 +780,15 @@ static int show_membership(const char *user, const char *group, Table *table) { break; case OUTPUT_JSON: { - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - r = json_build(&v, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("user", JSON_BUILD_STRING(user)), - JSON_BUILD_PAIR("group", JSON_BUILD_STRING(group)))); + r = sd_json_build(&v, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("user", SD_JSON_BUILD_STRING(user)), + SD_JSON_BUILD_PAIR("group", SD_JSON_BUILD_STRING(group)))); if (r < 0) return log_error_errno(r, "Failed to build JSON object: %m"); - json_variant_dump(v, arg_json_format_flags, NULL, NULL); + sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); break; } @@ -1200,7 +1200,7 @@ static int parse_argv(int argc, char *argv[]) { } else return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --output= mode: %s", optarg); - arg_json_format_flags = arg_output == OUTPUT_JSON ? JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR_AUTO : JSON_FORMAT_OFF; + arg_json_format_flags = arg_output == OUTPUT_JSON ? SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO : SD_JSON_FORMAT_OFF; break; case ARG_JSON: @@ -1208,11 +1208,11 @@ static int parse_argv(int argc, char *argv[]) { if (r <= 0) return r; - arg_output = FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) ? _OUTPUT_INVALID : OUTPUT_JSON; + arg_output = FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF) ? _OUTPUT_INVALID : OUTPUT_JSON; break; case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO; arg_output = OUTPUT_JSON; break; diff --git a/src/userdb/userwork.c b/src/userdb/userwork.c index 729a9a1bb6..d8f4e0f634 100644 --- a/src/userdb/userwork.c +++ b/src/userdb/userwork.c @@ -36,8 +36,8 @@ typedef struct LookupParameters { const char *service; } LookupParameters; -static int add_nss_service(JsonVariant **v) { - _cleanup_(json_variant_unrefp) JsonVariant *status = NULL, *z = NULL; +static int add_nss_service(sd_json_variant **v) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *status = NULL, *z = NULL; sd_id128_t mid; int r; @@ -46,33 +46,33 @@ static int add_nss_service(JsonVariant **v) { /* Patch in service field if it's missing. The assumption here is that this field is unset only for * NSS records */ - if (json_variant_by_key(*v, "service")) + if (sd_json_variant_by_key(*v, "service")) return 0; r = sd_id128_get_machine(&mid); if (r < 0) return r; - status = json_variant_ref(json_variant_by_key(*v, "status")); - z = json_variant_ref(json_variant_by_key(status, SD_ID128_TO_STRING(mid))); + status = sd_json_variant_ref(sd_json_variant_by_key(*v, "status")); + z = sd_json_variant_ref(sd_json_variant_by_key(status, SD_ID128_TO_STRING(mid))); - if (json_variant_by_key(z, "service")) + if (sd_json_variant_by_key(z, "service")) return 0; - r = json_variant_set_field_string(&z, "service", "io.systemd.NameServiceSwitch"); + r = sd_json_variant_set_field_string(&z, "service", "io.systemd.NameServiceSwitch"); if (r < 0) return r; - r = json_variant_set_field(&status, SD_ID128_TO_STRING(mid), z); + r = sd_json_variant_set_field(&status, SD_ID128_TO_STRING(mid), z); if (r < 0) return r; - return json_variant_set_field(v, "status", status); + return sd_json_variant_set_field(v, "status", status); } -static int build_user_json(Varlink *link, UserRecord *ur, JsonVariant **ret) { +static int build_user_json(Varlink *link, UserRecord *ur, sd_json_variant **ret) { _cleanup_(user_record_unrefp) UserRecord *stripped = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; UserRecordLoadFlags flags; uid_t peer_uid; bool trusted; @@ -103,14 +103,14 @@ static int build_user_json(Varlink *link, UserRecord *ur, JsonVariant **ret) { (FLAGS_SET(ur->mask, USER_RECORD_PRIVILEGED) && !FLAGS_SET(stripped->mask, USER_RECORD_PRIVILEGED)); - v = json_variant_ref(stripped->json); + v = sd_json_variant_ref(stripped->json); r = add_nss_service(&v); if (r < 0) return r; - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("record", JSON_BUILD_VARIANT(v)), - JSON_BUILD_PAIR("incomplete", JSON_BUILD_BOOLEAN(stripped->incomplete)))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_VARIANT(v)), + SD_JSON_BUILD_PAIR("incomplete", SD_JSON_BUILD_BOOLEAN(stripped->incomplete)))); } static int userdb_flags_from_service(Varlink *link, const char *service, UserDBFlags *ret) { @@ -129,16 +129,16 @@ static int userdb_flags_from_service(Varlink *link, const char *service, UserDBF return 0; } -static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_user_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "uid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(user_record_unrefp) UserRecord *hr = NULL; LookupParameters p = { .uid = UID_INVALID, @@ -163,7 +163,7 @@ static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, Var r = userdb_by_name(p.user_name, userdb_flags, &hr); else { _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *last = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *last = NULL; r = userdb_all(userdb_flags, &iterator); if (IN_SET(r, -ESRCH, -ENOLINK)) @@ -191,7 +191,7 @@ static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, Var if (r < 0) return r; - last = json_variant_unref(last); + last = sd_json_variant_unref(last); } r = build_user_json(link, z, &last); @@ -222,9 +222,9 @@ static int vl_method_get_user_record(Varlink *link, JsonVariant *parameters, Var return varlink_reply(link, v); } -static int build_group_json(Varlink *link, GroupRecord *gr, JsonVariant **ret) { +static int build_group_json(Varlink *link, GroupRecord *gr, sd_json_variant **ret) { _cleanup_(group_record_unrefp) GroupRecord *stripped = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; UserRecordLoadFlags flags; uid_t peer_uid; bool trusted; @@ -255,26 +255,26 @@ static int build_group_json(Varlink *link, GroupRecord *gr, JsonVariant **ret) { (FLAGS_SET(gr->mask, USER_RECORD_PRIVILEGED) && !FLAGS_SET(stripped->mask, USER_RECORD_PRIVILEGED)); - v = json_variant_ref(gr->json); + v = sd_json_variant_ref(gr->json); r = add_nss_service(&v); if (r < 0) return r; - return json_build(ret, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("record", JSON_BUILD_VARIANT(v)), - JSON_BUILD_PAIR("incomplete", JSON_BUILD_BOOLEAN(stripped->incomplete)))); + return sd_json_build(ret, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_VARIANT(v)), + SD_JSON_BUILD_PAIR("incomplete", SD_JSON_BUILD_BOOLEAN(stripped->incomplete)))); } -static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { +static int vl_method_get_group_record(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "gid", JSON_VARIANT_UNSIGNED, json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, + static const sd_json_dispatch_field dispatch_table[] = { + { "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; - _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; _cleanup_(group_record_unrefp) GroupRecord *g = NULL; LookupParameters p = { .gid = GID_INVALID, @@ -298,7 +298,7 @@ static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, Va r = groupdb_by_name(p.group_name, userdb_flags, &g); else { _cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL; - _cleanup_(json_variant_unrefp) JsonVariant *last = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *last = NULL; r = groupdb_all(userdb_flags, &iterator); if (IN_SET(r, -ESRCH, -ENOLINK)) @@ -320,7 +320,7 @@ static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, Va if (r < 0) return r; - last = json_variant_unref(last); + last = sd_json_variant_unref(last); } r = build_group_json(link, z, &last); @@ -351,11 +351,11 @@ static int vl_method_get_group_record(Varlink *link, JsonVariant *parameters, Va return varlink_reply(link, v); } -static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) { - static const JsonDispatch dispatch_table[] = { - { "userName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, - { "groupName", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, - { "service", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, +static int vl_method_get_memberships(Varlink *link, sd_json_variant *parameters, VarlinkMethodFlags flags, void *userdata) { + static const sd_json_dispatch_field dispatch_table[] = { + { "userName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, user_name), 0 }, + { "groupName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, group_name), 0 }, + { "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, {} }; @@ -402,9 +402,9 @@ static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, Var if (last_user_name) { assert(last_group_name); - r = varlink_notifyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(last_user_name)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(last_group_name)))); + r = varlink_notifyb(link, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(last_user_name)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(last_group_name)))); if (r < 0) return r; } @@ -420,9 +420,9 @@ static int vl_method_get_memberships(Varlink *link, JsonVariant *parameters, Var assert(last_group_name); - return varlink_replyb(link, JSON_BUILD_OBJECT( - JSON_BUILD_PAIR("userName", JSON_BUILD_STRING(last_user_name)), - JSON_BUILD_PAIR("groupName", JSON_BUILD_STRING(last_group_name)))); + return varlink_replyb(link, SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR("userName", SD_JSON_BUILD_STRING(last_user_name)), + SD_JSON_BUILD_PAIR("groupName", SD_JSON_BUILD_STRING(last_group_name)))); } static int process_connection(VarlinkServer *server, int _fd) { diff --git a/src/varlinkctl/varlinkctl.c b/src/varlinkctl/varlinkctl.c index 9da484d3b8..474ec9def8 100644 --- a/src/varlinkctl/varlinkctl.c +++ b/src/varlinkctl/varlinkctl.c @@ -16,7 +16,7 @@ #include "verbs.h" #include "version.h" -static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; +static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; static PagerFlags arg_pager_flags = 0; static VarlinkMethodFlags arg_method_flags = 0; static bool arg_collect = false; @@ -128,7 +128,7 @@ static int parse_argv(int argc, char *argv[]) { break; case 'j': - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; break; case '?': @@ -140,7 +140,7 @@ static int parse_argv(int argc, char *argv[]) { /* If more than one reply is expected, imply JSON-SEQ output */ if (FLAGS_SET(arg_method_flags, VARLINK_METHOD_MORE)) - arg_json_format_flags |= JSON_FORMAT_SEQ; + arg_json_format_flags |= SD_JSON_FORMAT_SEQ; return 1; } @@ -217,25 +217,25 @@ static int verb_info(int argc, char *argv[], void *userdata) { if (r < 0) return r; - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; r = varlink_call_and_log(vl, "org.varlink.service.GetInfo", /* parameters= */ NULL, &reply); if (r < 0) return r; pager_open(arg_pager_flags); - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - static const struct JsonDispatch dispatch_table[] = { - { "vendor", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(GetInfoData, vendor), JSON_MANDATORY }, - { "product", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(GetInfoData, product), JSON_MANDATORY }, - { "version", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(GetInfoData, version), JSON_MANDATORY }, - { "url", JSON_VARIANT_STRING, json_dispatch_const_string, offsetof(GetInfoData, url), JSON_MANDATORY }, - { "interfaces", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(GetInfoData, interfaces), JSON_MANDATORY }, + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + static const struct sd_json_dispatch_field dispatch_table[] = { + { "vendor", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(GetInfoData, vendor), SD_JSON_MANDATORY }, + { "product", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(GetInfoData, product), SD_JSON_MANDATORY }, + { "version", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(GetInfoData, version), SD_JSON_MANDATORY }, + { "url", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(GetInfoData, url), SD_JSON_MANDATORY }, + { "interfaces", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(GetInfoData, interfaces), SD_JSON_MANDATORY }, {} }; _cleanup_(get_info_data_done) GetInfoData data = {}; - r = json_dispatch(reply, dispatch_table, JSON_LOG, &data); + r = sd_json_dispatch(reply, dispatch_table, SD_JSON_LOG, &data); if (r < 0) return r; @@ -272,12 +272,12 @@ static int verb_info(int argc, char *argv[], void *userdata) { return table_log_print_error(r); } } else { - JsonVariant *v; + sd_json_variant *v; v = streq_ptr(argv[0], "list-interfaces") ? - json_variant_by_key(reply, "interfaces") : reply; + sd_json_variant_by_key(reply, "interfaces") : reply; - json_variant_dump(v, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(v, arg_json_format_flags, stdout, NULL); } return 0; @@ -300,27 +300,27 @@ static int verb_introspect(int argc, char *argv[], void *userdata) { if (r < 0) return r; - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; r = varlink_callb_and_log( vl, "org.varlink.service.GetInterfaceDescription", &reply, - JSON_BUILD_OBJECT(JSON_BUILD_PAIR_STRING("interface", interface))); + SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_STRING("interface", interface))); if (r < 0) return r; pager_open(arg_pager_flags); - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) { - static const struct JsonDispatch dispatch_table[] = { - { "description", JSON_VARIANT_STRING, json_dispatch_const_string, 0, JSON_MANDATORY }, + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) { + static const struct sd_json_dispatch_field dispatch_table[] = { + { "description", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, 0, SD_JSON_MANDATORY }, {} }; _cleanup_(varlink_interface_freep) VarlinkInterface *vi = NULL; const char *description = NULL; unsigned line = 0, column = 0; - r = json_dispatch(reply, dispatch_table, JSON_LOG, &description); + r = sd_json_dispatch(reply, dispatch_table, SD_JSON_LOG, &description); if (r < 0) return r; @@ -338,14 +338,14 @@ static int verb_introspect(int argc, char *argv[], void *userdata) { return log_error_errno(r, "Failed to format parsed interface description: %m"); } } else - json_variant_dump(reply, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(reply, arg_json_format_flags, stdout, NULL); return 0; } static int reply_callback( Varlink *link, - JsonVariant *parameters, + sd_json_variant *parameters, const char *error, VarlinkReplyFlags flags, void *userdata) { @@ -362,12 +362,12 @@ static int reply_callback( } else r = 0; - json_variant_dump(parameters, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(parameters, arg_json_format_flags, stdout, NULL); return r; } static int verb_call(int argc, char *argv[], void *userdata) { - _cleanup_(json_variant_unrefp) JsonVariant *jp = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *jp = NULL; _cleanup_(varlink_unrefp) Varlink *vl = NULL; const char *url, *method, *parameter; unsigned line = 0, column = 0; @@ -380,20 +380,20 @@ static int verb_call(int argc, char *argv[], void *userdata) { parameter = argc > 3 && !streq(argv[3], "-") ? argv[3] : NULL; /* No JSON mode explicitly configured? Then default to the same as -j */ - if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) - arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO; + if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) + arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; /* For pipeable text tools it's kinda customary to finish output off in a newline character, and not * leave incomplete lines hanging around. */ - arg_json_format_flags |= JSON_FORMAT_NEWLINE; + arg_json_format_flags |= SD_JSON_FORMAT_NEWLINE; if (parameter) { /* <argv[4]> is correct, as dispatch_verb() shifts arguments by one for the verb. */ - r = json_parse_with_source(parameter, "<argv[4]>", 0, &jp, &line, &column); + r = sd_json_parse_with_source(parameter, "<argv[4]>", 0, &jp, &line, &column); if (r < 0) return log_error_errno(r, "Failed to parse parameters at <argv[4]>:%u:%u: %m", line, column); } else { - r = json_parse_file_at(stdin, AT_FDCWD, "<stdin>", 0, &jp, &line, &column); + r = sd_json_parse_file_at(stdin, AT_FDCWD, "<stdin>", 0, &jp, &line, &column); if (r < 0) return log_error_errno(r, "Failed to parse parameters at <stdin>:%u:%u: %m", line, column); } @@ -403,7 +403,7 @@ static int verb_call(int argc, char *argv[], void *userdata) { return r; if (arg_collect) { - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; const char *error = NULL; r = varlink_collect(vl, method, jp, &reply, &error); @@ -418,7 +418,7 @@ static int verb_call(int argc, char *argv[], void *userdata) { r = 0; pager_open(arg_pager_flags); - json_variant_dump(reply, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(reply, arg_json_format_flags, stdout, NULL); return r; } else if (arg_method_flags & VARLINK_METHOD_ONEWAY) { @@ -463,7 +463,7 @@ static int verb_call(int argc, char *argv[], void *userdata) { return ret; } else { - JsonVariant *reply = NULL; + sd_json_variant *reply = NULL; const char *error = NULL; r = varlink_call(vl, method, jp, &reply, &error); @@ -481,7 +481,7 @@ static int verb_call(int argc, char *argv[], void *userdata) { pager_open(arg_pager_flags); - json_variant_dump(reply, arg_json_format_flags, stdout, NULL); + sd_json_variant_dump(reply, arg_json_format_flags, stdout, NULL); return r; } diff --git a/src/vmspawn/vmspawn-register.c b/src/vmspawn/vmspawn-register.c index 42650b82c0..b0b1c60a44 100644 --- a/src/vmspawn/vmspawn-register.c +++ b/src/vmspawn/vmspawn-register.c @@ -2,10 +2,11 @@ #include "sd-bus.h" #include "sd-id128.h" +#include "sd-json.h" #include "bus-error.h" #include "bus-locator.h" -#include "json.h" +#include "json-util.h" #include "macro.h" #include "process-util.h" #include "socket-util.h" @@ -61,15 +62,15 @@ int register_machine( return varlink_callb_and_log(vl, "io.systemd.Machine.Register", NULL, - JSON_BUILD_OBJECT( - JSON_BUILD_PAIR_STRING("name", machine_name), - JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(uuid), "id", JSON_BUILD_ID128(uuid)), - JSON_BUILD_PAIR_STRING("service", service), - JSON_BUILD_PAIR_STRING("class", "vm"), - JSON_BUILD_PAIR_CONDITION(VSOCK_CID_IS_REGULAR(cid), "vSockCid", JSON_BUILD_UNSIGNED(cid)), - JSON_BUILD_PAIR_CONDITION(directory, "rootDirectory", JSON_BUILD_STRING(directory)), - JSON_BUILD_PAIR_CONDITION(address, "sshAddress", JSON_BUILD_STRING(address)), - JSON_BUILD_PAIR_CONDITION(key_path, "sshPrivateKeyPath", JSON_BUILD_STRING(key_path)))); + SD_JSON_BUILD_OBJECT( + SD_JSON_BUILD_PAIR_STRING("name", machine_name), + SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(uuid), "id", SD_JSON_BUILD_ID128(uuid)), + SD_JSON_BUILD_PAIR_STRING("service", service), + SD_JSON_BUILD_PAIR_STRING("class", "vm"), + SD_JSON_BUILD_PAIR_CONDITION(VSOCK_CID_IS_REGULAR(cid), "vSockCid", SD_JSON_BUILD_UNSIGNED(cid)), + SD_JSON_BUILD_PAIR_CONDITION(!!directory, "rootDirectory", SD_JSON_BUILD_STRING(directory)), + SD_JSON_BUILD_PAIR_CONDITION(!!address, "sshAddress", SD_JSON_BUILD_STRING(address)), + SD_JSON_BUILD_PAIR_CONDITION(!!key_path, "sshPrivateKeyPath", SD_JSON_BUILD_STRING(key_path)))); } int unregister_machine(sd_bus *bus, const char *machine_name) { diff --git a/src/vmspawn/vmspawn-util.c b/src/vmspawn/vmspawn-util.c index 472dd92126..8a6122dd9d 100644 --- a/src/vmspawn/vmspawn-util.c +++ b/src/vmspawn/vmspawn-util.c @@ -4,13 +4,15 @@ #include <linux/vhost.h> #include <sys/ioctl.h> +#include "sd-json.h" + #include "architecture.h" #include "conf-files.h" #include "errno-util.h" #include "escape.h" #include "fd-util.h" #include "fileio.h" -#include "json.h" +#include "json-util.h" #include "log.h" #include "macro.h" #include "memory-util.h" @@ -140,52 +142,52 @@ static FirmwareData* firmware_data_free(FirmwareData *fwd) { } DEFINE_TRIVIAL_CLEANUP_FUNC(FirmwareData*, firmware_data_free); -static int firmware_executable(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { - static const JsonDispatch table[] = { - { "filename", JSON_VARIANT_STRING, json_dispatch_string, offsetof(FirmwareData, firmware), JSON_MANDATORY }, - { "format", JSON_VARIANT_STRING, json_dispatch_string, offsetof(FirmwareData, firmware_format), JSON_MANDATORY }, +static int firmware_executable(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { + static const sd_json_dispatch_field table[] = { + { "filename", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(FirmwareData, firmware), SD_JSON_MANDATORY }, + { "format", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(FirmwareData, firmware_format), SD_JSON_MANDATORY }, {} }; - return json_dispatch(v, table, flags, userdata); + return sd_json_dispatch(v, table, flags, userdata); } -static int firmware_nvram_template(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { - static const JsonDispatch table[] = { - { "filename", JSON_VARIANT_STRING, json_dispatch_string, offsetof(FirmwareData, vars), JSON_MANDATORY }, - { "format", JSON_VARIANT_STRING, json_dispatch_string, offsetof(FirmwareData, vars_format), JSON_MANDATORY }, +static int firmware_nvram_template(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { + static const sd_json_dispatch_field table[] = { + { "filename", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(FirmwareData, vars), SD_JSON_MANDATORY }, + { "format", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(FirmwareData, vars_format), SD_JSON_MANDATORY }, {} }; - return json_dispatch(v, table, flags, userdata); + return sd_json_dispatch(v, table, flags, userdata); } -static int firmware_mapping(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { - static const JsonDispatch table[] = { - { "device", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY }, - { "executable", JSON_VARIANT_OBJECT, firmware_executable, 0, JSON_MANDATORY }, - { "nvram-template", JSON_VARIANT_OBJECT, firmware_nvram_template, 0, JSON_MANDATORY }, +static int firmware_mapping(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { + static const sd_json_dispatch_field table[] = { + { "device", SD_JSON_VARIANT_STRING, NULL, 0, SD_JSON_MANDATORY }, + { "executable", SD_JSON_VARIANT_OBJECT, firmware_executable, 0, SD_JSON_MANDATORY }, + { "nvram-template", SD_JSON_VARIANT_OBJECT, firmware_nvram_template, 0, SD_JSON_MANDATORY }, {} }; - return json_dispatch(v, table, flags, userdata); + return sd_json_dispatch(v, table, flags, userdata); } -static int target_architecture(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { +static int target_architecture(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) { int r; - JsonVariant *e; + sd_json_variant *e; char ***supported_architectures = ASSERT_PTR(userdata); - static const JsonDispatch table[] = { - { "architecture", JSON_VARIANT_STRING, json_dispatch_string, 0, JSON_MANDATORY }, - { "machines", JSON_VARIANT_ARRAY, NULL, 0, JSON_MANDATORY }, + static const sd_json_dispatch_field table[] = { + { "architecture", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, 0, SD_JSON_MANDATORY }, + { "machines", SD_JSON_VARIANT_ARRAY, NULL, 0, SD_JSON_MANDATORY }, {} }; JSON_VARIANT_ARRAY_FOREACH(e, v) { _cleanup_free_ char *arch = NULL; - r = json_dispatch(e, table, flags, &arch); + r = sd_json_dispatch(e, table, flags, &arch); if (r < 0) return r; @@ -251,8 +253,8 @@ static int load_firmware_data(const char *path, FirmwareData **ret) { assert(path); assert(ret); - _cleanup_(json_variant_unrefp) JsonVariant *json = NULL; - r = json_parse_file( + _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL; + r = sd_json_parse_file( /* f= */ NULL, path, /* flags= */ 0, @@ -262,13 +264,13 @@ static int load_firmware_data(const char *path, FirmwareData **ret) { if (r < 0) return r; - static const JsonDispatch table[] = { - { "description", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY }, - { "interface-types", JSON_VARIANT_ARRAY, NULL, 0, JSON_MANDATORY }, - { "mapping", JSON_VARIANT_OBJECT, firmware_mapping, 0, JSON_MANDATORY }, - { "targets", JSON_VARIANT_ARRAY, target_architecture, offsetof(FirmwareData, architectures), JSON_MANDATORY }, - { "features", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(FirmwareData, features), JSON_MANDATORY }, - { "tags", JSON_VARIANT_ARRAY, NULL, 0, JSON_MANDATORY }, + static const sd_json_dispatch_field table[] = { + { "description", SD_JSON_VARIANT_STRING, NULL, 0, SD_JSON_MANDATORY }, + { "interface-types", SD_JSON_VARIANT_ARRAY, NULL, 0, SD_JSON_MANDATORY }, + { "mapping", SD_JSON_VARIANT_OBJECT, firmware_mapping, 0, SD_JSON_MANDATORY }, + { "targets", SD_JSON_VARIANT_ARRAY, target_architecture, offsetof(FirmwareData, architectures), SD_JSON_MANDATORY }, + { "features", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(FirmwareData, features), SD_JSON_MANDATORY }, + { "tags", SD_JSON_VARIANT_ARRAY, NULL, 0, SD_JSON_MANDATORY }, {} }; @@ -277,7 +279,7 @@ static int load_firmware_data(const char *path, FirmwareData **ret) { if (!fwd) return -ENOMEM; - r = json_dispatch(json, table, JSON_ALLOW_EXTENSIONS, fwd); + r = sd_json_dispatch(json, table, SD_JSON_ALLOW_EXTENSIONS, fwd); if (r < 0) return r; |