summaryrefslogtreecommitdiff
path: root/src/irmd/reg/prog.c
blob: 5429774a96a8911a2291068ec13c7693559ffdc5 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * The IPC Resource Manager - Registry - Programs
 *
 *    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/prog"

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

#include "prog.h"

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

struct name_entry {
        struct list_head next;
        char *           name;
};

static void __free_name_entry(struct name_entry * entry)
{
        assert(entry != NULL);
        assert(entry->name != NULL);

        free(entry->name);
        free(entry);
}

static void __reg_prog_clear_names(struct reg_prog * prog)
{
        struct list_head * p;
        struct list_head * h;

        assert(prog != NULL);

        list_for_each_safe(p, h, &prog->names) {
                struct name_entry * entry;
                entry = list_entry(p, struct name_entry, next);
                list_del(&entry->next);
                __free_name_entry(entry);
                prog->n_names--;
        }
}

struct reg_prog * reg_prog_create(const struct prog_info * info)
{
        struct reg_prog * p;

        assert(info != NULL);

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

        list_head_init(&p->next);
        list_head_init(&p->names);

        p->info    = *info;
        p->n_names = 0;

        return p;

 fail_malloc:
        return NULL;
}

void reg_prog_destroy(struct reg_prog * prog)
{
        assert(prog != NULL);

        __reg_prog_clear_names(prog);

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

        assert(prog->n_names == 0);

        assert(list_is_empty(&prog->names));

        free(prog);
}

static struct name_entry * __reg_prog_get_name(const struct reg_prog * prog,
                                               const char *            name)
{
        struct list_head * p;

        list_for_each(p, &prog->names) {
                struct name_entry * entry;
                entry = list_entry(p, struct name_entry, next);
                if (strcmp(entry->name, name) == 0)
                        return entry;
        }

        return NULL;
}

int reg_prog_add_name(struct reg_prog * prog,
                      const char *      name)
{
        struct name_entry * entry;

        assert(__reg_prog_get_name(prog, name) == NULL);

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

        entry->name = strdup(name);
        if (entry == NULL) {
                log_err("Failed to strdup name.");
                goto fail_name;
        }

        list_add(&entry->next, &prog->names);

        prog->n_names++;

        return 0;

 fail_name:
        free(entry);
 fail_malloc:
        return -1;
}

void reg_prog_del_name(struct reg_prog * prog,
                       const char *      name)
{
        struct name_entry * entry;

        entry = __reg_prog_get_name(prog, name);
        if (entry == NULL)
                return;

        list_del(&entry->next);

        __free_name_entry(entry);

        prog->n_names--;

        assert(__reg_prog_get_name(prog, name) == NULL);
}

bool reg_prog_has_name(const struct reg_prog * prog,
                       const char *            name)
{
        return __reg_prog_get_name(prog, name) != NULL;
}