From 9c7caac687d91a85868d129c6879cf8609c26cdb Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 18 Mar 2025 10:48:20 +0000 Subject: [PATCH] jail: Tidy up creating a new jail Signed-off-by: Michael Tremer --- src/pakfire/jail.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/pakfire/jail.c b/src/pakfire/jail.c index 92c22c22..065b2cf4 100644 --- a/src/pakfire/jail.c +++ b/src/pakfire/jail.c @@ -184,67 +184,69 @@ static const char* pakfire_jail_uuid(struct pakfire_jail* jail) { } int pakfire_jail_create(struct pakfire_jail** jail, struct pakfire* pakfire) { + struct pakfire_jail* self = NULL; int r; - const char* arch = pakfire_get_effective_arch(pakfire); - // Allocate a new jail - struct pakfire_jail* j = calloc(1, sizeof(*j)); - if (!j) - return 1; + self = calloc(1, sizeof(*self)); + if (!self) + return -errno; // Reference context - j->ctx = pakfire_ctx(pakfire); + self->ctx = pakfire_ctx(pakfire); // Reference Pakfire - j->pakfire = pakfire_ref(pakfire); + self->pakfire = pakfire_ref(pakfire); // Initialize reference counter - j->nrefs = 1; + self->nrefs = 1; // Generate a random UUID - uuid_generate_random(j->uuid); + uuid_generate_random(self->uuid); // Create environment - r = pakfire_env_create(&j->env, j->ctx); + r = pakfire_env_create(&self->env, self->ctx); if (r < 0) goto ERROR; // Set default environment for (const struct environ* e = ENV; e->key; e++) { - r = pakfire_env_set(j->env, e->key, "%s", e->val); + r = pakfire_env_set(self->env, e->key, "%s", e->val); if (r < 0) goto ERROR; } + // Fetch the architecture + const char* arch = pakfire_get_effective_arch(pakfire); + // Enable all CPU features that CPU has to offer if (!pakfire_arch_is_supported_by_host(arch)) { - r = pakfire_env_set(j->env, "QEMU_CPU", "max"); + r = pakfire_env_set(self->env, "QEMU_CPU", "max"); if (r < 0) goto ERROR; } // Set container UUID - r = pakfire_env_set(j->env, "container_uuid", "%s", pakfire_jail_uuid(j)); + r = pakfire_env_set(self->env, "container_uuid", "%s", pakfire_jail_uuid(self)); if (r < 0) goto ERROR; // Disable systemctl to talk to systemd - if (!pakfire_on_root(j->pakfire)) { - r = pakfire_env_set(j->env, "SYSTEMD_OFFLINE", "1"); + if (!pakfire_on_root(self->pakfire)) { + r = pakfire_env_set(self->env, "SYSTEMD_OFFLINE", "1"); if (r < 0) goto ERROR; } // Log action - DEBUG(j->ctx, "Created new jail %s\n", pakfire_jail_uuid(j)); + DEBUG(self->ctx, "Created new jail %s\n", pakfire_jail_uuid(self)); // Done - *jail = j; + *jail = self; return 0; ERROR: - pakfire_jail_free(j); + pakfire_jail_free(self); return r; } -- 2.39.5