Debian Codegraph @

src/common/fdwatch_kqueue.cpp

raw symbols SCIP
1 /*
2 * Abstraction API/layer for the various ways PvPGN can inspect sockets state
3 * 2003 (C)
4 *
5 * Code is based on the ideas found in thttpd project.
6 *
7 * *BSD kqueue(2) based backend
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #include "common/setup_before.h"
25 #ifdef HAVE_KQUEUE
26 #include "fdwatch_kqueue.h"
27
28 #include <cstring>
29 #include <stdint.h>
30
31 #include "common/eventlog.h"
32 #include "fdwatch.h"
33 #include "common/setup_after.h"
34
35 namespace pvpgn
36 {
37
38 FDWKqueueBackend::FDWKqueueBackend(int nfds_)
39 :FDWBackend(nfds_), sr(0), nochanges(0)
40 {
41 if ((kq = kqueue()) == -1)
42 throw InitError("error from kqueue()");
43
44 kqevents.reset(new struct kevent[nfds]);
45 kqchanges.reset(new struct kevent[nfds * 2]);
46 rridx.reset(new int[nfds]);
47 wridx.reset(new int[nfds]);
48
49 std::memset(kqchanges.get(), 0, sizeof(struct kevent) * nfds);
50 for (int i = 0; i < nfds; i++)
51 {
52 rridx[i] = -1;
53 wridx[i] = -1;
54 }
55
56 INFO1("fdwatch kqueue() based layer initialized (max {} sockets)", nfds);
57 }
58
59 FDWKqueueBackend::~FDWKqueueBackend() throw()
60 {}
61
62 int
63 FDWKqueueBackend::add(int idx, unsigned rw)
64 {
65 int idxr;
66 t_fdwatch_fd *cfd = fdw_fds + idx;
67
68 /* eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {} rw: {}", fd, rw); */
69 /* adding read event filter */
70 if (!(fdw_rw(cfd) & fdwatch_type_read) && rw & fdwatch_type_read)
71 {
72 if (rridx[idx] >= 0 && rridx[idx] < nochanges && kqchanges[rridx[idx]].ident == fdw_fd(cfd))
73 {
74 idxr = rridx[idx];
75 /* eventlog(eventlog_level_trace, __FUNCTION__, "updating change event (read) fd on {}", ridx); */
76 }
77 else {
78 idxr = nochanges++;
79 rridx[idx] = idxr;
80 /* eventlog(eventlog_level_trace, __FUNCTION__, "adding new change event (read) fd on {}", ridx); */
81 }
82 EV_SET(kqchanges.get() + idxr, fdw_fd(cfd), EVFILT_READ, EV_ADD, 0, 0, (void*)idx);
83 }
84 else if (fdw_rw(cfd) & fdwatch_type_read && !(rw & fdwatch_type_read))
85 {
86 if (rridx[idx] >= 0 && rridx[idx] < nochanges && kqchanges[rridx[idx]].ident == fdw_fd(cfd))
87 {
88 idxr = rridx[idx];
89 /* eventlog(eventlog_level_trace, __FUNCTION__, "updating change event (read) fd on {}", ridx); */
90 }
91 else {
92 idxr = nochanges++;
93 rridx[idx] = idxr;
94 /* eventlog(eventlog_level_trace, __FUNCTION__, "adding new change event (read) fd on {}", ridx); */
95 }
96 EV_SET(kqchanges.get() + idxr, fdw_fd(cfd), EVFILT_READ, EV_DELETE, 0, 0, (void*)idx);
97 }
98
99 /* adding write event filter */
100 if (!(fdw_rw(cfd) & fdwatch_type_write) && rw & fdwatch_type_write)
101 {
102 if (wridx[idx] >= 0 && wridx[idx] < nochanges && kqchanges[wridx[idx]].ident == fdw_fd(cfd))
103 {
104 idxr = wridx[idx];
105 /* eventlog(eventlog_level_trace, __FUNCTION__, "updating change event (write) fd on {}", ridx); */
106 }
107 else {
108 idxr = nochanges++;
109 wridx[idx] = idxr;
110 /* eventlog(eventlog_level_trace, __FUNCTION__, "adding new change event (write) fd on {}", ridx); */
111 }
112 EV_SET(kqchanges.get() + idxr, fdw_fd(cfd), EVFILT_WRITE, EV_ADD, 0, 0, (void*)idx);
113 }
114 else if (fdw_rw(cfd) & fdwatch_type_write && !(rw & fdwatch_type_write))
115 {
116 if (wridx[idx] >= 0 && wridx[idx] < nochanges && kqchanges[wridx[idx]].ident == fdw_fd(cfd))
117 {
118 idxr = wridx[idx];
119 /* eventlog(eventlog_level_trace, __FUNCTION__, "updating change event (write) fd on {}", ridx); */
120 }
121 else {
122 idxr = nochanges++;
123 wridx[idx] = idxr;
124 /* eventlog(eventlog_level_trace, __FUNCTION__, "adding new change event (write) fd on {}", ridx); */
125 }
126 EV_SET(kqchanges.get() + idxr, fdw_fd(cfd), EVFILT_WRITE, EV_DELETE, 0, 0, (void*)idx);
127 }
128
129 return 0;
130 }
131
132 int
133 FDWKqueueBackend::del(int idx)
134 {
135 /* eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {}", fd); */
136 if (sr > 0)
137 eventlog(eventlog_level_error, __FUNCTION__, "BUG: called while still handling sockets");
138
139 t_fdwatch_fd *cfd = fdw_fds + idx;
140 /* the last event changes about this fd has not yet been sent to kernel */
141 if (fdw_rw(cfd) & fdwatch_type_read &&
142 nochanges && rridx[idx] >= 0 && rridx[idx] < nochanges &&
143 kqchanges[rridx[idx]].ident == fdw_fd(cfd))
144 {
145 nochanges--;
146 if (rridx[idx] < nochanges)
147 {
148 intptr_t oidx;
149
150 oidx = (intptr_t)(kqchanges[nochanges].udata);
151 if (kqchanges[nochanges].filter == EVFILT_READ &&
152 rridx[oidx] == nochanges)
153 {
154 /* eventlog(eventlog_level_trace, __FUNCTION__, "not last, moving {}", kqchanges[rnfds].ident); */
155 rridx[oidx] = rridx[idx];
156 std::memcpy(kqchanges.get() + rridx[idx], kqchanges.get() + nochanges, sizeof(struct kevent));
157 }
158
159 if (kqchanges[nochanges].filter == EVFILT_WRITE &&
160 wridx[oidx] == nochanges)
161 {
162 /* eventlog(eventlog_level_trace, __FUNCTION__, "not last, moving {}", kqchanges[rnfds].ident); */
163 wridx[oidx] = rridx[idx];
164 std::memcpy(kqchanges.get() + rridx[idx], kqchanges.get() + nochanges, sizeof(struct kevent));
165 }
166 }
167 rridx[idx] = -1;
168 }
169
170 if (fdw_rw(cfd) & fdwatch_type_write &&
171 nochanges && wridx[idx] >= 0 && wridx[idx] < nochanges &&
172 kqchanges[wridx[idx]].ident == fdw_fd(cfd))
173 {
174 nochanges--;
175 if (wridx[idx] < nochanges)
176 {
177 intptr_t oidx;
178
179 oidx = (intptr_t)(kqchanges[nochanges].udata);
180 if (kqchanges[nochanges].filter == EVFILT_READ &&
181 rridx[oidx] == nochanges)
182 {
183 /* eventlog(eventlog_level_trace, __FUNCTION__, "not last, moving {}", kqchanges[rnfds].ident); */
184 rridx[oidx] = wridx[idx];
185 std::memcpy(kqchanges.get() + wridx[idx], kqchanges.get() + nochanges, sizeof(struct kevent));
186 }
187
188 if (kqchanges[nochanges].filter == EVFILT_WRITE &&
189 wridx[oidx] == nochanges)
190 {
191 /* eventlog(eventlog_level_trace, __FUNCTION__, "not last, moving {}, kqchanges[rnfds].ident); */
192 wridx[oidx] = wridx[idx];
193 std::memcpy(kqchanges.get() + wridx[idx], kqchanges.get() + nochanges, sizeof(struct kevent));
194 }
195 }
196 wridx[idx] = -1;
197 }
198
199 /* here we presume the calling code does close() on the socket and if so
200 * it is automatically removed from any kernel kqueues */
201
202 return 0;
203 }
204
205 int
206 FDWKqueueBackend::watch(long timeout_msec)
207 {
208 static struct timespec ts;
209
210 ts.tv_sec = timeout_msec / 1000L;
211 ts.tv_nsec = (timeout_msec % 1000L) * 1000000L;
212 sr = kevent(kq, nochanges > 0 ? kqchanges.get() : NULL, nochanges, kqevents.get(), fdw_maxcons, &ts);
213 nochanges = 0;
214 return sr;
215 }
216
217 void
218 FDWKqueueBackend::handle()
219 {
220 /* eventlog(eventlog_level_trace, __FUNCTION__, "called"); */
221 for (unsigned i = 0; i < sr; i++)
222 {
223 /* eventlog(eventlog_level_trace, __FUNCTION__, "checking {} ident: {} read: {} write: {}", i, kqevents[i].ident, kqevents[i].filter & EVFILT_READ, kqevents[i].filter & EVFILT_WRITE); */
224 t_fdwatch_fd *cfd = fdw_fds + (intptr_t)kqevents[i].udata;
225 if (fdw_rw(cfd) & fdwatch_type_read && kqevents[i].filter == EVFILT_READ)
226 if (fdw_hnd(cfd) (fdw_data(cfd), fdwatch_type_read) == -2)
227 continue;
228
229 if (fdw_rw(cfd) & fdwatch_type_write && kqevents[i].filter == EVFILT_WRITE)
230 fdw_hnd(cfd) (fdw_data(cfd), fdwatch_type_write);
231 }
232 sr = 0;
233 }
234
235 }
236
237 #endif /* HAVE_KQUEUE */