summaryrefslogtreecommitdiff
path: root/src/irmd/proc_table.c
blob: 2d92b700d6cb311c614e3e1f5e8ba106f148247b (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Ouroboros - Copyright (C) 2016 - 2021
 *
 * The IPC Resource Manager - Process Table
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@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., http://www.fsf.org/about/contact/.
 */

#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 200112L
#endif

#include "config.h"

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

#include "proc_table.h"
#include "registry.h"

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

struct proc_entry * proc_entry_create(pid_t  pid,
                                      char * prog)
{
        struct proc_entry * e;
        pthread_condattr_t  cattr;

        assert(prog);

        e = malloc(sizeof(*e));
        if (e == NULL)
                goto fail_malloc;

        if (pthread_condattr_init(&cattr))
                goto fail_condattr;

#ifndef __APPLE__
        pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
#endif

        if (pthread_mutex_init(&e->lock, NULL))
                goto fail_mutex;

        if (pthread_cond_init(&e->cond, &cattr))
                goto fail_cond;

        e->set = shm_flow_set_create(pid);
        if (e->set == NULL)
                goto fail_set;

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

        e->pid      = pid;
        e->prog     = prog;
        e->re       = NULL;
        e->state    = PROC_INIT;

        return e;
 fail_set:
        pthread_cond_destroy(&e->cond);;
 fail_cond:
        pthread_mutex_destroy(&e->lock);
 fail_mutex:
        pthread_condattr_destroy(&cattr);
 fail_condattr:
        free(e);
 fail_malloc:
        return NULL;
}

static void cancel_proc_entry(void * o)
{
        struct proc_entry * e = (struct proc_entry *) o;

        e->state = PROC_NULL;

        pthread_mutex_unlock(&e->lock);
}

void proc_entry_destroy(struct proc_entry * e)
{
        struct list_head * p;
        struct list_head * h;

        assert(e);

        pthread_mutex_lock(&e->lock);

        if (e->state == PROC_DESTROY) {
                pthread_mutex_unlock(&e->lock);
                return;
        }

        if (e->state == PROC_SLEEP)
                e->state = PROC_DESTROY;

        pthread_cond_signal(&e->cond);

        pthread_cleanup_push(cancel_proc_entry, e);

        while (e->state != PROC_INIT)
                pthread_cond_wait(&e->cond, &e->lock);

        pthread_cleanup_pop(false);

        pthread_mutex_unlock(&e->lock);

        shm_flow_set_destroy(e->set);

        pthread_cond_destroy(&e->cond);
        pthread_mutex_destroy(&e->lock);

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

        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 proc_entry_add_name(struct proc_entry * e,
                        char *              name)
{
        struct str_el * s;

        assert(e);
        assert(name);

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

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

        return 0;
}

void proc_entry_del_name(struct proc_entry * e,
                         const char *        name)
{
        struct list_head * p = NULL;
        struct list_head * h = NULL;

        assert(e);
        assert(name);

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

int proc_entry_sleep(struct proc_entry * e,
                     struct timespec *   timeo)
{
        struct timespec dl;

        int ret = 0;

        assert(e);

        if (timeo != NULL) {
                clock_gettime(PTHREAD_COND_CLOCK, &dl);
                ts_add(&dl, timeo, &dl);
        }

        pthread_mutex_lock(&e->lock);

        if (e->state != PROC_WAKE && e->state != PROC_DESTROY)
                e->state = PROC_SLEEP;

        pthread_cleanup_push(cancel_proc_entry, e);

        while (e->state == PROC_SLEEP && ret != -ETIMEDOUT)
                if (timeo)
                        ret = -pthread_cond_timedwait(&e->cond, &e->lock, &dl);
                else
                        ret = -pthread_cond_wait(&e->cond, &e->lock);

        pthread_cleanup_pop(false);

        if (e->state == PROC_DESTROY) {
                if (e->re != NULL)
                        reg_entry_del_pid(e->re, e->pid);
                ret = -1;
        }

        e->state = PROC_INIT;

        pthread_cond_broadcast(&e->cond);
        pthread_mutex_unlock(&e->lock);

        return ret;
}

void proc_entry_wake(struct proc_entry * e,
                     struct reg_entry *  re)
{
        assert(e);
        assert(re);

        pthread_mutex_lock(&e->lock);

        if (e->state != PROC_SLEEP) {
                pthread_mutex_unlock(&e->lock);
                return;
        }

        e->state = PROC_WAKE;
        e->re    = re;

        pthread_cond_broadcast(&e->cond);

        pthread_cleanup_push(cancel_proc_entry, e);

        while (e->state == PROC_WAKE)
                pthread_cond_wait(&e->cond, &e->lock);

        pthread_cleanup_pop(false);

        if (e->state == PROC_DESTROY)
                e->state = PROC_INIT;

        pthread_mutex_unlock(&e->lock);
}

int proc_table_add(struct list_head *  proc_table,
                   struct proc_entry * e)
{

        assert(proc_table);
        assert(e);

        list_add(&e->next, proc_table);

        return 0;
}

void proc_table_del(struct list_head * proc_table,
                    pid_t              pid)
{
        struct list_head * p;
        struct list_head * h;

        assert(proc_table);

        list_for_each_safe(p, h, proc_table) {
                struct proc_entry * e = list_entry(p, struct proc_entry, next);
                if (pid == e->pid) {
                        list_del(&e->next);
                        proc_entry_destroy(e);
                }
        }
}

struct proc_entry * proc_table_get(struct list_head * proc_table,
                                   pid_t              pid)
{
        struct list_head * h;

        assert(proc_table);

        list_for_each(h, proc_table) {
                struct proc_entry * e = list_entry(h, struct proc_entry, next);
                if (pid == e->pid)
                        return e;
        }

        return NULL;
}