HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/nspawn/nspawn-oci.c

89,199 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 545f80f8ed35d74c09e435c38f45c256346aad9c

Lines 2001–2139 of 2,139

                        n_array = &s->n_oci_hooks_poststart;
                } else {
                        assert(streq(name, "poststop"));
                        array = &s->oci_hooks_poststop;
                        n_array = &s->n_oci_hooks_poststop;
                }
 
                if (!GREEDY_REALLOC(*array, *n_array + 1))
                        return log_oom();
 
                new_item = *array + *n_array;
 
                *new_item = (OciHook) {
                        .timeout = USEC_INFINITY,
                };
 
                r = oci_dispatch(e, table, flags, new_item);
                if (r < 0) {
                        free(new_item->path);
                        strv_free(new_item->args);
                        strv_free(new_item->env);
                        return r;
                }
 
                (*n_array)++;
        }
 
        return 0;
}
 
static int oci_hooks(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
 
        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, 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) {
 
                if (isempty(k))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Annotation with empty key, refusing.");
 
                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|SD_JSON_DEBUG, 0, "Ignoring annotation '%s' with value '%s'.", k, sd_json_variant_string(w));
        }
 
        return 0;
}
 
int oci_load(FILE *f, const char *bundle, Settings **ret) {
 
        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_(sd_json_variant_unrefp) sd_json_variant *oci = NULL;
        _cleanup_(settings_freep) Settings *s = NULL;
        unsigned line = 0, column = 0;
        sd_json_variant *v;
        const char *path;
        int r;
 
        assert_se(bundle);
        assert(ret);
 
        path = strjoina(bundle, "/config.json");
 
        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);
                else
                        return log_error_errno(r, "Failed to parse '%s': %m", path);
        }
 
        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(sd_json_variant_string(v), "1.0.0"))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "OCI bundle version not supported: %s",
                                       strna(sd_json_variant_string(v)));
 
        // {
        //         _cleanup_free_ char *formatted = NULL;
        //         assert_se(json_variant_format(oci, SD_JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, &formatted) >= 0);
        //         fputs(formatted, stdout);
        // }
 
        s = settings_new();
        if (!s)
                return log_oom();
 
        s->start_mode = START_PID1;
        s->resolv_conf = RESOLV_CONF_OFF;
        s->link_journal = LINK_NO;
        s->timezone = TIMEZONE_OFF;
 
        s->bundle = strdup(bundle);
        if (!s->bundle)
                return log_oom();
 
        r = oci_dispatch(oci, table, /* flags= */ 0, s);
        if (r < 0)
                return log_error_errno(r, "Failed to parse OCI bundle configuration file '%s': %m", path);
 
        if (s->properties) {
                r = sd_bus_message_seal(s->properties, 0, 0);
                if (r < 0)
                        return log_error_errno(r, "Cannot seal properties bus message: %m");
        }
 
        *ret = TAKE_PTR(s);
        return 0;
}