This source file includes following definitions.
- fpm_event_epoll_module
- fpm_event_epoll_init
- fpm_event_epoll_clean
- fpm_event_epoll_wait
- fpm_event_epoll_add
- fpm_event_epoll_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include "../fpm_config.h"
22 #include "../fpm_events.h"
23 #include "../fpm.h"
24 #include "../zlog.h"
25
26 #if HAVE_EPOLL
27
28 #include <sys/epoll.h>
29 #include <errno.h>
30
31 static int fpm_event_epoll_init(int max);
32 static int fpm_event_epoll_clean();
33 static int fpm_event_epoll_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
34 static int fpm_event_epoll_add(struct fpm_event_s *ev);
35 static int fpm_event_epoll_remove(struct fpm_event_s *ev);
36
37 static struct fpm_event_module_s epoll_module = {
38 .name = "epoll",
39 .support_edge_trigger = 1,
40 .init = fpm_event_epoll_init,
41 .clean = fpm_event_epoll_clean,
42 .wait = fpm_event_epoll_wait,
43 .add = fpm_event_epoll_add,
44 .remove = fpm_event_epoll_remove,
45 };
46
47 static struct epoll_event *epollfds = NULL;
48 static int nepollfds = 0;
49 static int epollfd = -1;
50
51 #endif
52
53 struct fpm_event_module_s *fpm_event_epoll_module()
54 {
55 #if HAVE_EPOLL
56 return &epoll_module;
57 #else
58 return NULL;
59 #endif
60 }
61
62
63 #if HAVE_EPOLL
64
65
66
67
68 static int fpm_event_epoll_init(int max)
69 {
70 if (max < 1) {
71 return 0;
72 }
73
74
75 epollfd = epoll_create(max + 1);
76 if (epollfd < 0) {
77 zlog(ZLOG_ERROR, "epoll: unable to initialize");
78 return -1;
79 }
80
81
82 epollfds = malloc(sizeof(struct epoll_event) * max);
83 if (!epollfds) {
84 zlog(ZLOG_ERROR, "epoll: unable to allocate %d events", max);
85 return -1;
86 }
87 memset(epollfds, 0, sizeof(struct epoll_event) * max);
88
89
90 nepollfds = max;
91
92 return 0;
93 }
94
95
96
97
98
99 static int fpm_event_epoll_clean()
100 {
101
102 if (epollfds) {
103 free(epollfds);
104 epollfds = NULL;
105 }
106 if (epollfd != -1) {
107 close(epollfd);
108 epollfd = -1;
109 }
110
111 nepollfds = 0;
112
113 return 0;
114 }
115
116
117
118
119
120 static int fpm_event_epoll_wait(struct fpm_event_queue_s *queue, unsigned long int timeout)
121 {
122 int ret, i;
123
124
125 memset(epollfds, 0, sizeof(struct epoll_event) * nepollfds);
126
127
128 ret = epoll_wait(epollfd, epollfds, nepollfds, timeout);
129 if (ret == -1) {
130
131
132 if (errno != EINTR) {
133 zlog(ZLOG_WARNING, "epoll_wait() returns %d", errno);
134 return -1;
135 }
136 }
137
138
139 for (i = 0; i < ret; i++) {
140
141
142 if (!epollfds[i].data.ptr) {
143 continue;
144 }
145
146
147 fpm_event_fire((struct fpm_event_s *)epollfds[i].data.ptr);
148
149
150 if (fpm_globals.parent_pid != getpid()) {
151 return -2;
152 }
153 }
154
155 return ret;
156 }
157
158
159
160
161
162 static int fpm_event_epoll_add(struct fpm_event_s *ev)
163 {
164 struct epoll_event e;
165
166
167 #if SIZEOF_SIZE_T == 4
168
169 e.data.u64 = 0;
170 #endif
171 e.events = EPOLLIN;
172 e.data.fd = ev->fd;
173 e.data.ptr = (void *)ev;
174
175 if (ev->flags & FPM_EV_EDGE) {
176 e.events = e.events | EPOLLET;
177 }
178
179
180 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ev->fd, &e) == -1) {
181 zlog(ZLOG_ERROR, "epoll: unable to add fd %d", ev->fd);
182 return -1;
183 }
184
185
186 ev->index = ev->fd;
187 return 0;
188 }
189
190
191
192
193
194 static int fpm_event_epoll_remove(struct fpm_event_s *ev)
195 {
196 struct epoll_event e;
197
198
199 e.events = EPOLLIN;
200 e.data.fd = ev->fd;
201 e.data.ptr = (void *)ev;
202
203 if (ev->flags & FPM_EV_EDGE) {
204 e.events = e.events | EPOLLET;
205 }
206
207
208 if (epoll_ctl(epollfd, EPOLL_CTL_DEL, ev->fd, &e) == -1) {
209 zlog(ZLOG_ERROR, "epoll: unable to remove fd %d", ev->fd);
210 return -1;
211 }
212
213
214 ev->index = -1;
215 return 0;
216 }
217
218
219 #endif