summaryrefslogtreecommitdiff
path: root/src/irmd/reg/pool.c
blob: fd983db84ead5811af616d983bed33b0da23a606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Ouroboros - Copyright (C) 2016 - 2026
 *
 * The IPC Resource Manager - Registry - Per-User Pools
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#define _POSIX_C_SOURCE 200809L

#define OUROBOROS_PREFIX "reg/pool"

#include <ouroboros/logs.h>
#include <ouroboros/ssm_pool.h>

#include "pool.h"

#include <assert.h>
#include <stdlib.h>

struct reg_pool * reg_pool_create(uid_t uid,
                                  gid_t gid)
{
        struct reg_pool * pool;

        pool = malloc(sizeof(*pool));
        if (pool == NULL) {
                log_err("Failed to malloc pool.");
                goto fail_malloc;
        }

        pool->ssm = ssm_pool_create(uid, gid);
        if (pool->ssm == NULL) {
                log_err("Failed to create PUP for uid %d.", uid);
                goto fail_ssm;
        }

        list_head_init(&pool->next);
        pool->uid      = uid;
        pool->gid      = gid;
        pool->refcount = 1;

        log_dbg("Created PUP for uid %d gid %d.", uid, gid);

        return pool;

 fail_ssm:
        free(pool);
 fail_malloc:
        return NULL;
}

void reg_pool_destroy(struct reg_pool * pool)
{
        assert(pool != NULL);
        assert(pool->refcount == 0);

        log_dbg("Destroying PUP for uid %d.", pool->uid);

        ssm_pool_destroy(pool->ssm);

        assert(list_is_empty(&pool->next));

        free(pool);
}

void reg_pool_ref(struct reg_pool * pool)
{
        assert(pool != NULL);
        assert(pool->refcount > 0);

        pool->refcount++;

        log_dbg("PUP uid %d refcount++ -> %zu.", pool->uid, pool->refcount);
}

int reg_pool_unref(struct reg_pool * pool)
{
        assert(pool != NULL);
        assert(pool->refcount > 0);

        pool->refcount--;

        log_dbg("PUP uid %d refcount-- -> %zu.", pool->uid, pool->refcount);

        return pool->refcount == 0 ? 0 : 1;
}