/src/open5gs/lib/core/ogs-notify.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 | | #if HAVE_UNISTD_H |
23 | | #include <unistd.h> |
24 | | #endif |
25 | | |
26 | | #if HAVE_EVENTFD |
27 | | #include <sys/eventfd.h> |
28 | | #endif |
29 | | |
30 | | #include "ogs-core.h" |
31 | | #include "ogs-poll-private.h" |
32 | | |
33 | | static void ogs_drain_pollset(short when, ogs_socket_t fd, void *data); |
34 | | |
35 | | void ogs_notify_init(ogs_pollset_t *pollset) |
36 | 0 | { |
37 | | #if !defined(HAVE_EVENTFD) |
38 | | int rc; |
39 | | #endif |
40 | 0 | ogs_assert(pollset); |
41 | | |
42 | 0 | #if defined(HAVE_EVENTFD) |
43 | 0 | pollset->notify.fd[0] = eventfd(0, 0); |
44 | 0 | ogs_assert(pollset->notify.fd[0] != INVALID_SOCKET); |
45 | | #else |
46 | | rc = ogs_socketpair(AF_SOCKPAIR, SOCK_STREAM, 0, pollset->notify.fd); |
47 | | ogs_assert(rc == OGS_OK); |
48 | | #endif |
49 | | |
50 | 0 | pollset->notify.poll = ogs_pollset_add(pollset, OGS_POLLIN, |
51 | 0 | pollset->notify.fd[0], ogs_drain_pollset, NULL); |
52 | 0 | ogs_assert(pollset->notify.poll); |
53 | 0 | } |
54 | | |
55 | | void ogs_notify_final(ogs_pollset_t *pollset) |
56 | 0 | { |
57 | 0 | ogs_assert(pollset); |
58 | | |
59 | 0 | ogs_pollset_remove(pollset->notify.poll); |
60 | |
|
61 | 0 | ogs_closesocket(pollset->notify.fd[0]); |
62 | | #if !defined(HAVE_EVENTFD) |
63 | | ogs_closesocket(pollset->notify.fd[1]); |
64 | | #endif |
65 | 0 | } |
66 | | |
67 | | int ogs_notify_pollset(ogs_pollset_t *pollset) |
68 | 0 | { |
69 | 0 | ssize_t r; |
70 | 0 | #if defined(HAVE_EVENTFD) |
71 | 0 | uint64_t msg = 1; |
72 | | #else |
73 | | char buf[1]; |
74 | | buf[0] = 0; |
75 | | #endif |
76 | |
|
77 | 0 | ogs_assert(pollset); |
78 | | |
79 | 0 | #if defined(HAVE_EVENTFD) |
80 | 0 | r = write(pollset->notify.fd[0], (void*)&msg, sizeof(msg)); |
81 | | #else |
82 | | r = send(pollset->notify.fd[1], buf, 1, 0); |
83 | | #endif |
84 | |
|
85 | 0 | if (r < 0) { |
86 | 0 | ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, "notify failed"); |
87 | 0 | return OGS_ERROR; |
88 | 0 | } |
89 | | |
90 | 0 | return OGS_OK; |
91 | 0 | } |
92 | | |
93 | | static void ogs_drain_pollset(short when, ogs_socket_t fd, void *data) |
94 | 0 | { |
95 | 0 | ssize_t r; |
96 | 0 | #if defined(HAVE_EVENTFD) |
97 | 0 | uint64_t msg; |
98 | | #else |
99 | | unsigned char buf[1024]; |
100 | | #endif |
101 | |
|
102 | 0 | ogs_assert(when == OGS_POLLIN); |
103 | | |
104 | 0 | #if defined(HAVE_EVENTFD) |
105 | 0 | r = read(fd, (char *)&msg, sizeof(msg)); |
106 | | #else |
107 | | r = recv(fd, (char *)buf, sizeof(buf), 0); |
108 | | #endif |
109 | 0 | if (r < 0) { |
110 | 0 | ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, "drain failed"); |
111 | 0 | } |
112 | 0 | } |