summaryrefslogtreecommitdiff
path: root/src/irmd/reg/ipcp.c
blob: 62505871e87dc6f23caf813e01b03fef102a1a69 (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
/*
 * Ouroboros - Copyright (C) 2016 - 2023
 *
 * The IPC Resource Manager - Registry - IPCPs
 *
 *    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/.
 */

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

#include "config.h"

#include <ouroboros/errno.h>
#include <ouroboros/hash.h>
#include <ouroboros/ipcp.h>
#include <ouroboros/pthread.h>
#include <ouroboros/time_utils.h>

#include "ipcp.h"

#include <assert.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

struct reg_ipcp * reg_ipcp_create(const char *   name,
                                  enum ipcp_type type)
{
        struct reg_ipcp *  ipcp;
        pthread_condattr_t cattr;

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

        if (pthread_mutex_init(&ipcp->mtx, NULL))
                goto fail_mutex;

        if (pthread_condattr_init(&cattr))
                goto fail_cattr;
#ifndef __APPLE__
        pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
#endif
        if (pthread_cond_init(&ipcp->cond, &cattr))
                goto fail_cond;

        ipcp->name = strdup(name);
        if (ipcp->name == NULL)
                goto fail_name;

        pthread_condattr_destroy(&cattr);

        ipcp->layer = NULL;
        ipcp->type  = type;
        ipcp->state = IPCP_BOOT;

        list_head_init(&ipcp->next);

        return ipcp;

 fail_name:
        pthread_cond_destroy(&ipcp->cond);
 fail_cond:
        pthread_condattr_destroy(&cattr);
 fail_cattr:
        pthread_mutex_destroy(&ipcp->mtx);
 fail_mutex:
        free(ipcp);
 fail_malloc:
        return NULL;
}

void reg_ipcp_destroy(struct reg_ipcp * ipcp)
{
        assert(ipcp);

        pthread_mutex_lock(&ipcp->mtx);

        while (ipcp->state == IPCP_BOOT)
                pthread_cond_wait(&ipcp->cond, &ipcp->mtx);

        free(ipcp->layer);
        free(ipcp->name);

        pthread_mutex_unlock(&ipcp->mtx);

        pthread_cond_destroy(&ipcp->cond);
        pthread_mutex_destroy(&ipcp->mtx);

        free(ipcp);
}

void reg_ipcp_set_state(struct reg_ipcp * ipcp,
                        enum ipcp_state   state)
{
        pthread_mutex_lock(&ipcp->mtx);

        ipcp->state = state;
        pthread_cond_broadcast(&ipcp->cond);

        pthread_mutex_unlock(&ipcp->mtx);
}

int reg_ipcp_wait_boot(struct reg_ipcp * ipcp)
{
        int             ret = 0;
        struct timespec dl;
        struct timespec to = {SOCKET_TIMEOUT / 1000,
                              (SOCKET_TIMEOUT % 1000) * MILLION};

        clock_gettime(PTHREAD_COND_CLOCK, &dl);
        ts_add(&dl, &to, &dl);

        pthread_mutex_lock(&ipcp->mtx);

        while (ipcp->state == IPCP_BOOT && ret != ETIMEDOUT)
                ret = pthread_cond_timedwait(&ipcp->cond, &ipcp->mtx, &dl);

        if (ret == ETIMEDOUT) {
                kill(ipcp->pid, SIGTERM);
                ipcp->state = IPCP_NULL;
                pthread_cond_signal(&ipcp->cond);
        }

        if (ipcp->state != IPCP_LIVE) {
                pthread_mutex_unlock(&ipcp->mtx);
                return -1;
        }

        pthread_mutex_unlock(&ipcp->mtx);

        return 0;
}