1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-09 01:18:19 +03:00

bootspec: split out helper that turns BootEntry into a JSON object

We can use that later for returning the boot loader entry list as JSON
via Varlink.
This commit is contained in:
Lennart Poettering 2024-02-12 17:29:17 +01:00
parent 2cda44c23e
commit f892954ba2
2 changed files with 61 additions and 38 deletions

View File

@ -1668,17 +1668,20 @@ int show_boot_entry(
return -status;
}
int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
int boot_entry_to_json(const BootConfig *c, size_t i, JsonVariant **ret) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
const BootEntry *e;
int r;
assert(config);
assert(c);
assert(ret);
if (!FLAGS_SET(json_format, JSON_FORMAT_OFF)) {
_cleanup_(json_variant_unrefp) JsonVariant *array = NULL;
if (i >= c->n_entries) {
*ret = NULL;
return 0;
}
for (size_t i = 0; i < config->n_entries; i++) {
const BootEntry *e = config->entries + i;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
e = c->entries + i;
r = json_variant_merge_objectb(
&v, JSON_BUILD_OBJECT(
@ -1700,10 +1703,6 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
if (r < 0)
return log_oom();
r = json_cmdline(e, &config->global_addons, &v);
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. */
@ -1712,19 +1711,41 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
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(config->default_entry >= 0, "isDefault", JSON_BUILD_BOOLEAN(i == (size_t) config->default_entry)),
JSON_BUILD_PAIR_CONDITION(config->selected_entry >= 0, "isSelected", JSON_BUILD_BOOLEAN(i == (size_t) config->selected_entry))));
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))));
if (r < 0)
return log_oom();
r = json_cmdline(e, &c->global_addons, &v);
if (r < 0)
return log_oom();
*ret = TAKE_PTR(v);
return 1;
}
int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
int r;
assert(config);
if (!FLAGS_SET(json_format, JSON_FORMAT_OFF)) {
_cleanup_(json_variant_unrefp) JsonVariant *array = NULL;
for (size_t i = 0; i < config->n_entries; i++) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
r = boot_entry_to_json(config, i, &v);
if (r < 0)
return log_oom();
r = json_variant_append_array(&array, v);
if (r < 0)
return log_oom();
}
json_variant_dump(array, json_format | JSON_FORMAT_EMPTY_ARRAY, NULL, NULL);
return json_variant_dump(array, json_format | JSON_FORMAT_EMPTY_ARRAY, NULL, NULL);
} else {
for (size_t n = 0; n < config->n_entries; n++) {
r = show_boot_entry(

View File

@ -143,3 +143,5 @@ int show_boot_entries(
JsonFormatFlags 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);