]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Tidy up creating a new jail
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 18 Mar 2025 10:48:20 +0000 (10:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 18 Mar 2025 10:48:20 +0000 (10:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/jail.c

index 92c22c22d8dc2a036171e9fba73ce66622d253f9..065b2cf41608c1cd61971fd727a8a3d88a93288d 100644 (file)
@@ -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;
 }