Coverage Report

Created: 2025-08-26 06:50

/src/open5gs/lib/core/ogs-poll.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
3
 *
4
 * This file is part of Open5GS.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
#include "core-config-private.h"
21
22
#include "ogs-core.h"
23
24
#include "ogs-poll-private.h"
25
26
extern const ogs_pollset_actions_t ogs_kqueue_actions;
27
extern const ogs_pollset_actions_t ogs_epoll_actions;
28
extern const ogs_pollset_actions_t ogs_select_actions;
29
30
static void *self_handler_data = NULL;
31
32
ogs_pollset_actions_t ogs_pollset_actions;
33
bool ogs_pollset_actions_initialized = false;
34
35
ogs_pollset_t *ogs_pollset_create(unsigned int capacity)
36
0
{
37
0
    ogs_pollset_t *pollset = ogs_calloc(1, sizeof *pollset);
38
0
    if (!pollset) {
39
0
        ogs_error("ogs_calloc() failed");
40
0
        return NULL;
41
0
    }
42
43
0
    pollset->capacity = capacity;
44
45
0
    ogs_pool_init(&pollset->pool, capacity);
46
47
0
    if (ogs_pollset_actions_initialized == false) {
48
#if defined(HAVE_KQUEUE)
49
        ogs_pollset_actions = ogs_kqueue_actions;
50
#elif defined(HAVE_EPOLL)
51
        ogs_pollset_actions = ogs_epoll_actions;
52
#else
53
        ogs_pollset_actions = ogs_select_actions;
54
#endif
55
0
        ogs_pollset_actions_initialized = true;
56
0
    }
57
58
0
    ogs_pollset_actions.init(pollset);
59
60
0
    return pollset;
61
0
}
62
63
void ogs_pollset_destroy(ogs_pollset_t *pollset)
64
0
{
65
0
    ogs_assert(pollset);
66
67
0
    ogs_pollset_actions.cleanup(pollset);
68
69
0
    ogs_pool_final(&pollset->pool);
70
0
    ogs_free(pollset);
71
0
}
72
73
ogs_poll_t *ogs_pollset_add(ogs_pollset_t *pollset, short when,
74
        ogs_socket_t fd, ogs_poll_handler_f handler, void *data)
75
0
{
76
0
    ogs_poll_t *poll = NULL;
77
0
    int rc;
78
79
0
    ogs_assert(pollset);
80
81
0
    ogs_assert(fd != INVALID_SOCKET);
82
0
    ogs_assert(handler);
83
84
0
    ogs_pool_alloc(&pollset->pool, &poll);
85
0
    ogs_assert(poll);
86
87
0
    rc = ogs_nonblocking(fd);
88
0
    ogs_assert(rc == OGS_OK);
89
0
    rc = ogs_closeonexec(fd);
90
0
    ogs_assert(rc == OGS_OK);
91
92
0
    poll->when = when;
93
0
    poll->fd = fd;
94
0
    poll->handler = handler;
95
96
0
    if (data == &self_handler_data)
97
0
        poll->data = poll;
98
0
    else
99
0
        poll->data = data;
100
101
0
    poll->pollset = pollset;
102
103
0
    rc = ogs_pollset_actions.add(poll);
104
0
    if (rc != OGS_OK) {
105
0
        ogs_error("cannot add poll");
106
0
        ogs_pool_free(&pollset->pool, poll);
107
0
        return NULL;
108
0
    }
109
110
0
    return poll;
111
0
}
112
113
void ogs_pollset_remove(ogs_poll_t *poll)
114
0
{
115
0
    int rc;
116
0
    ogs_pollset_t *pollset = NULL;
117
118
0
    ogs_assert(poll);
119
0
    pollset = poll->pollset;
120
0
    ogs_assert(pollset);
121
122
0
    rc = ogs_pollset_actions.remove(poll);
123
0
    if (rc != OGS_OK) {
124
0
        ogs_error("cannot delete poll");
125
0
    }
126
127
0
    ogs_pool_free(&pollset->pool, poll);
128
0
}
129
130
void *ogs_pollset_self_handler_data(void)
131
0
{
132
0
    return &self_handler_data;
133
0
}