1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "nstackx_event.h"
16 #include "nstackx_log.h"
17 #include "nstackx_error.h"
18 #include "nstackx_util.h"
19 #include "nstackx_epoll.h"
20 #include "securec.h"
21
22 #define TAG "nStackXEvent"
23
24 typedef struct {
25 EventHandle handle;
26 void *arg;
27 } EventInfo;
28
29 EventNode *SearchEventNode(const List *eventNodeChain, EpollDesc epollfd);
30
CloseNodePipe(const EventNode * node)31 void CloseNodePipe(const EventNode *node)
32 {
33 CloseDesc(node->pipeFd[PIPE_OUT]);
34 CloseDesc(node->pipeFd[PIPE_IN]);
35 }
36
EventProcessHandle(void * arg)37 static void EventProcessHandle(void *arg)
38 {
39 int32_t ret;
40 EventInfo event = {0};
41 EpollTask *task = arg;
42 EventNode *node = container_of(task, EventNode, task);
43
44 ret = (int32_t)read(node->pipeFd[PIPE_OUT], &event, sizeof(event));
45 if (ret != (int32_t)sizeof(event)) {
46 LOGE(TAG, "failed to read from pipe: %d", GetErrno());
47 return;
48 }
49
50 if (event.handle != NULL) {
51 event.handle(event.arg);
52 }
53 }
54
PostEvent(const List * eventNodeChain,EpollDesc epollfd,EventHandle handle,void * arg)55 int32_t PostEvent(const List *eventNodeChain, EpollDesc epollfd, EventHandle handle, void *arg)
56 {
57 int32_t ret;
58 EventNode *node = NULL;
59 EventInfo event = {
60 .handle = handle,
61 .arg = arg,
62 };
63
64 if (eventNodeChain == NULL || handle == NULL) {
65 return NSTACKX_EINVAL;
66 }
67
68 node = SearchEventNode(eventNodeChain, epollfd);
69 if (node == NULL) {
70 LOGE(TAG, "Cannot find event node for %d", epollfd->recvFd);
71 return NSTACKX_EFAILED;
72 }
73
74 ret = (int32_t)write(node->pipeFd[PIPE_IN], &event, sizeof(event));
75 if (ret != (int32_t)sizeof(event)) {
76 LOGE(TAG, "failed to write to pipe: %d", errno);
77 return NSTACKX_EFAILED;
78 }
79
80 return NSTACKX_EOK;
81 }
82
ClearEvent(const List * eventNodeChain,EpollDesc epollfd)83 void ClearEvent(const List *eventNodeChain, EpollDesc epollfd)
84 {
85 EventNode *node = NULL;
86 EventInfo event = {0};
87 int32_t eventLen = (int32_t)sizeof(event);
88 if (eventNodeChain == NULL) {
89 LOGE(TAG, "eventNodeChain is null");
90 return;
91 }
92
93 node = SearchEventNode(eventNodeChain, epollfd);
94 if (node == NULL) {
95 return;
96 }
97
98 int32_t ret = eventLen;
99 while (ret == eventLen) {
100 ret = (int32_t)read(node->pipeFd[PIPE_OUT], &event, sizeof(event));
101 if (ret != eventLen) {
102 break;
103 }
104
105 if (event.handle != NULL) {
106 event.handle(event.arg);
107 }
108 }
109 }
110
CreateNonBlockPipe(EventNode * node)111 static int32_t CreateNonBlockPipe(EventNode *node)
112 {
113 struct EpollDescStr fds;
114
115 if (CreateEpollFdPair(&fds) != NSTACKX_EOK) {
116 LOGE(TAG, "CreateEpollFdPair failed");
117 return NSTACKX_EFAILED;
118 }
119
120 node->pipeFd[PIPE_OUT] = fds.recvFd;
121 node->pipeFd[PIPE_IN] = fds.sendFd;
122
123 return NSTACKX_EOK;
124 }
125
EventModuleInit(List * eventNodeChain,EpollDesc epollfd)126 int32_t EventModuleInit(List *eventNodeChain, EpollDesc epollfd)
127 {
128 List *pos = NULL;
129 EventNode *node = NULL;
130 if (eventNodeChain == NULL) {
131 LOGE(TAG, "eventNodeChain is null");
132 return NSTACKX_EINVAL;
133 }
134 LIST_FOR_EACH(pos, eventNodeChain) {
135 node = (EventNode *)pos;
136 if (IsEpollDescEqual(node->epollfd, epollfd)) {
137 return NSTACKX_EOK;
138 }
139 }
140
141 node = calloc(1, sizeof(EventNode));
142 if (node == NULL) {
143 LOGE(TAG, "calloc failed");
144 return NSTACKX_ENOMEM;
145 }
146
147 if (CreateNonBlockPipe(node) != NSTACKX_EOK) {
148 LOGE(TAG, "create pipe failed");
149 goto L_ERR_FAILED;
150 }
151
152 node->task.taskfd = node->pipeFd[PIPE_OUT];
153 node->task.epollfd = epollfd;
154 node->task.readHandle = EventProcessHandle;
155
156 node->epollfd = epollfd;
157 if (RegisterEpollTask(&node->task, EPOLLIN) != NSTACKX_EOK) {
158 LOGE(TAG, "RegisterEpollTask failed");
159 CloseNodePipe(node);
160 goto L_ERR_FAILED;
161 }
162
163 ListInsertTail(eventNodeChain, &(node->list));
164 return NSTACKX_EOK;
165 L_ERR_FAILED:
166 free(node);
167 return NSTACKX_EFAILED;
168 }
169
DeleteEventNode(EventNode * node)170 void DeleteEventNode(EventNode *node)
171 {
172 ListRemoveNode(&node->list);
173 if (DeRegisterEpollTask(&node->task) != NSTACKX_EOK) {
174 LOGE(TAG, "DeRegisterEpollTask failed");
175 }
176 CloseNodePipe(node);
177 free(node);
178 }
179