summaryrefslogtreecommitdiff
path: root/src/irmd/api_table.c
blob: 7619fcf670246a05e4f3ff0b4a77108f77470f6a (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Ouroboros - Copyright (C) 2016 - 2017
 *
 * The IPC Resource Manager - Application Instance Table
 *
 *    Dimitri Staessens <dimitri.staessens@intec.ugent.be>
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <ouroboros/config.h>
#include <ouroboros/list.h>
#include <ouroboros/errno.h>
#include <ouroboros/time_utils.h>

#include "api_table.h"
#include "registry.h"

#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

struct api_entry * api_entry_create(pid_t api, char * apn)
{
        struct api_entry * e;

        if (apn == NULL)
                return NULL;

        e = malloc(sizeof(*e));
        if (e == NULL)
                return NULL;

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

        e->api      = api;
        e->apn      = apn;
        e->daf_name = NULL;

        e->re       = NULL;

        e->state    = API_INIT;

        pthread_mutex_init(&e->state_lock, NULL);
        pthread_cond_init(&e->state_cond, NULL);

        return e;
}

void api_entry_destroy(struct api_entry * e)
{
        struct list_head * p;
        struct list_head * h;

        if (e == NULL)
                return;

        pthread_mutex_lock(&e->state_lock);

        if (e->state == API_DESTROY) {
                pthread_mutex_unlock(&e->state_lock);
                return;
        }

        if (e->state == API_SLEEP)
                e->state = API_DESTROY;

        pthread_cond_signal(&e->state_cond);

        while (e->state != API_INIT)
                pthread_cond_wait(&e->state_cond, &e->state_lock);

        pthread_mutex_unlock(&e->state_lock);

        pthread_cond_destroy(&e->state_cond);
        pthread_mutex_destroy(&e->state_lock);

        if (e->apn != NULL)
                free(e->apn);

        list_for_each_safe(p, h, &e->names) {
                struct str_el * n = list_entry(p, struct str_el, next);
                list_del(&n->next);
                if (n->str != NULL)
                        free(n->str);
                free(n);
        }

        free(e);
}

int api_entry_add_name(struct api_entry * e, char * name)
{
        struct str_el * s;
        if (e == NULL || name == NULL)
                return -EINVAL;

        s = malloc(sizeof(*s));
        if (s == NULL)
                return -ENOMEM;

        s->str = name;
        list_add(&s->next, &e->names);

        return 0;
}

void api_entry_del_name(struct api_entry * e, char * name)
{
        struct list_head * p = NULL;
        struct list_head * h = NULL;

        list_for_each_safe(p, h, &e->names) {
                struct str_el * s = list_entry(p, struct str_el, next);
                if (!wildcard_match(name, s->str)) {
                        list_del(&s->next);
                        if (s->str != NULL)
                                free(s->str);
                        free(s);
                }
        }
}

int api_entry_sleep(struct api_entry * e)
{
        struct timespec timeout = {(IRMD_ACCEPT_TIMEOUT / 1000),
                                   (IRMD_ACCEPT_TIMEOUT % 1000) * MILLION};
        struct timespec now;
        struct timespec dl;

        int ret = 0;

        if (e == NULL)
                return -EINVAL;

        e->re = NULL;

        clock_gettime(CLOCK_REALTIME, &now);

        ts_add(&now, &timeout, &dl);

        pthread_mutex_lock(&e->state_lock);
        if (e->state != API_INIT) {
                pthread_mutex_unlock(&e->state_lock);
                return -EINVAL;
        }

        e->state = API_SLEEP;

        while (e->state == API_SLEEP) {
                if ((ret = -pthread_cond_timedwait(&e->state_cond,
                                                   &e->state_lock,
                                                   &dl)) == -ETIMEDOUT) {
                        break;
                }
        }

        if (e->state == API_DESTROY)
                ret = -1;

        e->state = API_INIT;
        pthread_cond_broadcast(&e->state_cond);
        pthread_mutex_unlock(&e->state_lock);

        return ret;
}

void api_entry_wake(struct api_entry * e, struct reg_entry * re)
{
        if (e == NULL)
                return;

        pthread_mutex_lock(&e->state_lock);

        if (e->state != API_SLEEP) {
                pthread_mutex_unlock(&e->state_lock);
                return;
        }

        e->state = API_WAKE;
        e->re    = re;

        pthread_cond_broadcast(&e->state_cond);

        while (e->state == API_WAKE)
                pthread_cond_wait(&e->state_cond, &e->state_lock);

        pthread_mutex_unlock(&e->state_lock);
}

int api_table_add(struct list_head * api_table, struct api_entry * e)
{
        if (api_table == NULL || e == NULL)
                return -EINVAL;

        list_add(&e->next, api_table);

        return 0;
}

void api_table_del(struct list_head * api_table, pid_t api)
{
        struct list_head * p;
        struct list_head * h;

        list_for_each_safe(p, h, api_table) {
                struct api_entry * e = list_entry(p, struct api_entry, next);
                if (api == e->api) {
                        list_del(&e->next);
                        api_entry_destroy(e);
                }
        }
}

struct api_entry * api_table_get(struct list_head * api_table, pid_t api)
{
        struct list_head * h;

        list_for_each(h, api_table) {
                struct api_entry * e = list_entry(h, struct api_entry, next);
                if (api == e->api)
                        return e;
        }

        return NULL;
}