Mercurial > hg > nginx-quic
annotate src/event/modules/ngx_eventport_module.c @ 5965:78271500b8de
A bounds check of %N format on Windows.
Thanks to Joe Bialek, Adam Zabrocki and Microsoft Vulnerability Research.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 27 Jan 2015 15:38:15 +0300 |
parents | 3f5f0ab59b35 |
children | d84f0abd4a53 |
rev | line source |
---|---|
719 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
719 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_event.h> | |
11 | |
12 | |
13 #if (NGX_TEST_BUILD_EVENTPORT) | |
14 | |
15 #define ushort_t u_short | |
16 #define uint_t u_int | |
17 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
18 #ifndef CLOCK_REALTIME |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
19 #define CLOCK_REALTIME 0 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
20 typedef int clockid_t; |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
21 typedef void * timer_t; |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
22 #endif |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
23 |
719 | 24 /* Solaris declarations */ |
25 | |
26 #define PORT_SOURCE_AIO 1 | |
27 #define PORT_SOURCE_TIMER 2 | |
28 #define PORT_SOURCE_USER 3 | |
29 #define PORT_SOURCE_FD 4 | |
30 #define PORT_SOURCE_ALERT 5 | |
31 #define PORT_SOURCE_MQ 6 | |
32 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
33 #ifndef ETIME |
719 | 34 #define ETIME 64 |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
35 #endif |
719 | 36 |
37 #define SIGEV_PORT 4 | |
38 | |
39 typedef struct { | |
40 int portev_events; /* event data is source specific */ | |
41 ushort_t portev_source; /* event source */ | |
42 ushort_t portev_pad; /* port internal use */ | |
43 uintptr_t portev_object; /* source specific object */ | |
44 void *portev_user; /* user cookie */ | |
45 } port_event_t; | |
46 | |
47 typedef struct port_notify { | |
48 int portnfy_port; /* bind request(s) to port */ | |
49 void *portnfy_user; /* user defined */ | |
50 } port_notify_t; | |
51 | |
1939
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
52 #if (__FreeBSD_version < 700005) |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
53 |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
54 typedef struct itimerspec { /* definition per POSIX.4 */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
55 struct timespec it_interval;/* timer period */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
56 struct timespec it_value; /* timer expiration */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
57 } itimerspec_t; |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
58 |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
59 #endif |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
60 |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
61 int port_create(void); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
62 |
719 | 63 int port_create(void) |
64 { | |
65 return -1; | |
66 } | |
67 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
68 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
69 int port_associate(int port, int source, uintptr_t object, int events, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
70 void *user); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
71 |
719 | 72 int port_associate(int port, int source, uintptr_t object, int events, |
73 void *user) | |
74 { | |
75 return -1; | |
76 } | |
77 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
78 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
79 int port_dissociate(int port, int source, uintptr_t object); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
80 |
719 | 81 int port_dissociate(int port, int source, uintptr_t object) |
82 { | |
83 return -1; | |
84 } | |
85 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
86 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
87 int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
88 struct timespec *timeout); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
89 |
719 | 90 int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, |
91 struct timespec *timeout) | |
92 { | |
93 return -1; | |
94 } | |
95 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
96 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
97 int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
98 |
719 | 99 int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid) |
100 { | |
101 return -1; | |
102 } | |
103 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
104 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
105 int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
106 struct itimerspec *ovalue); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
107 |
719 | 108 int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, |
109 struct itimerspec *ovalue) | |
110 { | |
111 return -1; | |
112 } | |
113 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
114 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
115 int timer_delete(timer_t timerid); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
116 |
719 | 117 int timer_delete(timer_t timerid) |
118 { | |
119 return -1; | |
120 } | |
121 | |
122 #endif | |
123 | |
124 | |
125 typedef struct { | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
126 ngx_uint_t events; |
719 | 127 } ngx_eventport_conf_t; |
128 | |
129 | |
130 static ngx_int_t ngx_eventport_init(ngx_cycle_t *cycle, ngx_msec_t timer); | |
131 static void ngx_eventport_done(ngx_cycle_t *cycle); | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
132 static ngx_int_t ngx_eventport_add_event(ngx_event_t *ev, ngx_int_t event, |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
133 ngx_uint_t flags); |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
134 static ngx_int_t ngx_eventport_del_event(ngx_event_t *ev, ngx_int_t event, |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
135 ngx_uint_t flags); |
719 | 136 static ngx_int_t ngx_eventport_process_events(ngx_cycle_t *cycle, |
137 ngx_msec_t timer, ngx_uint_t flags); | |
138 | |
139 static void *ngx_eventport_create_conf(ngx_cycle_t *cycle); | |
140 static char *ngx_eventport_init_conf(ngx_cycle_t *cycle, void *conf); | |
141 | |
142 static int ep = -1; | |
143 static port_event_t *event_list; | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
144 static ngx_uint_t nevents; |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
145 static timer_t event_timer = (timer_t) -1; |
719 | 146 |
147 static ngx_str_t eventport_name = ngx_string("eventport"); | |
148 | |
149 | |
150 static ngx_command_t ngx_eventport_commands[] = { | |
151 | |
152 { ngx_string("eventport_events"), | |
153 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
154 ngx_conf_set_num_slot, | |
155 0, | |
156 offsetof(ngx_eventport_conf_t, events), | |
157 NULL }, | |
158 | |
159 ngx_null_command | |
160 }; | |
161 | |
162 | |
163 ngx_event_module_t ngx_eventport_module_ctx = { | |
164 &eventport_name, | |
165 ngx_eventport_create_conf, /* create configuration */ | |
166 ngx_eventport_init_conf, /* init configuration */ | |
167 | |
168 { | |
169 ngx_eventport_add_event, /* add an event */ | |
170 ngx_eventport_del_event, /* delete an event */ | |
171 ngx_eventport_add_event, /* enable an event */ | |
172 ngx_eventport_del_event, /* disable an event */ | |
173 NULL, /* add an connection */ | |
174 NULL, /* delete an connection */ | |
175 NULL, /* process the changes */ | |
176 ngx_eventport_process_events, /* process the events */ | |
177 ngx_eventport_init, /* init the events */ | |
178 ngx_eventport_done, /* done the events */ | |
179 } | |
180 | |
181 }; | |
182 | |
183 ngx_module_t ngx_eventport_module = { | |
184 NGX_MODULE_V1, | |
185 &ngx_eventport_module_ctx, /* module context */ | |
186 ngx_eventport_commands, /* module directives */ | |
187 NGX_EVENT_MODULE, /* module type */ | |
188 NULL, /* init master */ | |
189 NULL, /* init module */ | |
190 NULL, /* init process */ | |
191 NULL, /* init thread */ | |
192 NULL, /* exit thread */ | |
193 NULL, /* exit process */ | |
194 NULL, /* exit master */ | |
195 NGX_MODULE_V1_PADDING | |
196 }; | |
197 | |
198 | |
199 static ngx_int_t | |
200 ngx_eventport_init(ngx_cycle_t *cycle, ngx_msec_t timer) | |
201 { | |
202 port_notify_t pn; | |
203 struct itimerspec its; | |
204 struct sigevent sev; | |
205 ngx_eventport_conf_t *epcf; | |
206 | |
207 epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_eventport_module); | |
208 | |
209 if (ep == -1) { | |
210 ep = port_create(); | |
211 | |
212 if (ep == -1) { | |
213 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
214 "port_create() failed"); | |
215 return NGX_ERROR; | |
216 } | |
217 } | |
218 | |
219 if (nevents < epcf->events) { | |
220 if (event_list) { | |
221 ngx_free(event_list); | |
222 } | |
223 | |
224 event_list = ngx_alloc(sizeof(port_event_t) * epcf->events, | |
225 cycle->log); | |
226 if (event_list == NULL) { | |
227 return NGX_ERROR; | |
228 } | |
229 } | |
230 | |
231 ngx_event_flags = NGX_USE_EVENTPORT_EVENT; | |
232 | |
233 if (timer) { | |
234 ngx_memzero(&pn, sizeof(port_notify_t)); | |
235 pn.portnfy_port = ep; | |
236 | |
237 ngx_memzero(&sev, sizeof(struct sigevent)); | |
238 sev.sigev_notify = SIGEV_PORT; | |
239 #if !(NGX_TEST_BUILD_EVENTPORT) | |
240 sev.sigev_value.sival_ptr = &pn; | |
241 #endif | |
242 | |
243 if (timer_create(CLOCK_REALTIME, &sev, &event_timer) == -1) { | |
244 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
245 "timer_create() failed"); | |
246 return NGX_ERROR; | |
247 } | |
248 | |
249 its.it_interval.tv_sec = timer / 1000; | |
250 its.it_interval.tv_nsec = (timer % 1000) * 1000000; | |
251 its.it_value.tv_sec = timer / 1000; | |
252 its.it_value.tv_nsec = (timer % 1000) * 1000000; | |
253 | |
254 if (timer_settime(event_timer, 0, &its, NULL) == -1) { | |
255 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
256 "timer_settime() failed"); | |
257 return NGX_ERROR; | |
258 } | |
259 | |
260 ngx_event_flags |= NGX_USE_TIMER_EVENT; | |
261 } | |
262 | |
263 nevents = epcf->events; | |
264 | |
265 ngx_io = ngx_os_io; | |
266 | |
267 ngx_event_actions = ngx_eventport_module_ctx.actions; | |
268 | |
269 return NGX_OK; | |
270 } | |
271 | |
272 | |
273 static void | |
274 ngx_eventport_done(ngx_cycle_t *cycle) | |
275 { | |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
276 if (event_timer != (timer_t) -1) { |
719 | 277 if (timer_delete(event_timer) == -1) { |
278 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
279 "timer_delete() failed"); | |
280 } | |
281 | |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
282 event_timer = (timer_t) -1; |
719 | 283 } |
284 | |
285 if (close(ep) == -1) { | |
286 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
287 "close() event port failed"); | |
288 } | |
289 | |
290 ep = -1; | |
291 | |
292 ngx_free(event_list); | |
293 | |
294 event_list = NULL; | |
295 nevents = 0; | |
296 } | |
297 | |
298 | |
299 static ngx_int_t | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
300 ngx_eventport_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags) |
719 | 301 { |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
302 ngx_int_t events, prev; |
719 | 303 ngx_event_t *e; |
304 ngx_connection_t *c; | |
305 | |
306 c = ev->data; | |
307 | |
308 events = event; | |
309 | |
310 if (event == NGX_READ_EVENT) { | |
311 e = c->write; | |
312 prev = POLLOUT; | |
313 #if (NGX_READ_EVENT != POLLIN) | |
314 events = POLLIN; | |
315 #endif | |
316 | |
317 } else { | |
318 e = c->read; | |
319 prev = POLLIN; | |
320 #if (NGX_WRITE_EVENT != POLLOUT) | |
321 events = POLLOUT; | |
322 #endif | |
323 } | |
324 | |
325 if (e->oneshot) { | |
326 events |= prev; | |
327 } | |
328 | |
329 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
330 "eventport add event: fd:%d ev:%04Xi", c->fd, events); |
719 | 331 |
332 if (port_associate(ep, PORT_SOURCE_FD, c->fd, events, | |
333 (void *) ((uintptr_t) ev | ev->instance)) | |
334 == -1) | |
335 { | |
336 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
337 "port_associate() failed"); | |
338 return NGX_ERROR; | |
339 } | |
340 | |
341 ev->active = 1; | |
342 ev->oneshot = 1; | |
343 | |
344 return NGX_OK; | |
345 } | |
346 | |
347 | |
348 static ngx_int_t | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
349 ngx_eventport_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags) |
719 | 350 { |
351 ngx_event_t *e; | |
352 ngx_connection_t *c; | |
353 | |
354 /* | |
355 * when the file descriptor is closed, the event port automatically | |
4572
67653855682e
Fixed spelling in multiline C comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
356 * dissociates it from the port, so we do not need to dissociate explicitly |
719 | 357 * the event before the closing the file descriptor |
358 */ | |
359 | |
360 if (flags & NGX_CLOSE_EVENT) { | |
361 ev->active = 0; | |
362 ev->oneshot = 0; | |
363 return NGX_OK; | |
364 } | |
365 | |
366 c = ev->data; | |
367 | |
368 if (event == NGX_READ_EVENT) { | |
369 e = c->write; | |
370 event = POLLOUT; | |
371 | |
372 } else { | |
373 e = c->read; | |
374 event = POLLIN; | |
375 } | |
376 | |
377 if (e->oneshot) { | |
378 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
379 "eventport change event: fd:%d ev:%04Xi", c->fd, event); |
719 | 380 |
381 if (port_associate(ep, PORT_SOURCE_FD, c->fd, event, | |
382 (void *) ((uintptr_t) ev | ev->instance)) | |
383 == -1) | |
384 { | |
385 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
386 "port_associate() failed"); | |
387 return NGX_ERROR; | |
388 } | |
389 | |
390 } else { | |
391 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
392 "eventport del event: fd:%d", c->fd); | |
393 | |
394 if (port_dissociate(ep, PORT_SOURCE_FD, c->fd) == -1) { | |
395 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
396 "port_dissociate() failed"); | |
397 return NGX_ERROR; | |
398 } | |
399 } | |
400 | |
401 ev->active = 0; | |
402 ev->oneshot = 0; | |
403 | |
404 return NGX_OK; | |
405 } | |
406 | |
407 | |
408 ngx_int_t | |
409 ngx_eventport_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, | |
410 ngx_uint_t flags) | |
411 { | |
412 int n, revents; | |
413 u_int events; | |
414 ngx_err_t err; | |
415 ngx_int_t instance; | |
416 ngx_uint_t i, level; | |
5821
3f5f0ab59b35
Events: processing of posted events changed from LIFO to FIFO.
Valentin Bartenev <vbart@nginx.com>
parents:
5820
diff
changeset
|
417 ngx_event_t *ev, *rev, *wev; |
3f5f0ab59b35
Events: processing of posted events changed from LIFO to FIFO.
Valentin Bartenev <vbart@nginx.com>
parents:
5820
diff
changeset
|
418 ngx_queue_t *queue; |
719 | 419 ngx_connection_t *c; |
420 struct timespec ts, *tp; | |
421 | |
422 if (timer == NGX_TIMER_INFINITE) { | |
423 tp = NULL; | |
424 | |
425 } else { | |
426 ts.tv_sec = timer / 1000; | |
427 ts.tv_nsec = (timer % 1000) * 1000000; | |
428 tp = &ts; | |
429 } | |
430 | |
431 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
432 "eventport timer: %M", timer); | |
433 | |
434 events = 1; | |
435 | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
436 n = port_getn(ep, event_list, (u_int) nevents, &events, tp); |
719 | 437 |
438 err = ngx_errno; | |
439 | |
440 if (flags & NGX_UPDATE_TIME) { | |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
441 ngx_time_update(); |
719 | 442 } |
443 | |
444 if (n == -1) { | |
445 if (err == ETIME) { | |
446 if (timer != NGX_TIMER_INFINITE) { | |
447 return NGX_OK; | |
448 } | |
449 | |
450 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
451 "port_getn() returned no events without timeout"); | |
452 return NGX_ERROR; | |
453 } | |
454 | |
455 level = (err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT; | |
456 ngx_log_error(level, cycle->log, err, "port_getn() failed"); | |
457 return NGX_ERROR; | |
458 } | |
459 | |
460 if (events == 0) { | |
461 if (timer != NGX_TIMER_INFINITE) { | |
462 return NGX_OK; | |
463 } | |
464 | |
465 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
466 "port_getn() returned no events without timeout"); | |
467 return NGX_ERROR; | |
468 } | |
469 | |
470 for (i = 0; i < events; i++) { | |
471 | |
472 if (event_list[i].portev_source == PORT_SOURCE_TIMER) { | |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
473 ngx_time_update(); |
719 | 474 continue; |
475 } | |
476 | |
477 ev = event_list[i].portev_user; | |
478 | |
479 switch (event_list[i].portev_source) { | |
480 | |
481 case PORT_SOURCE_FD: | |
482 | |
483 instance = (uintptr_t) ev & 1; | |
484 ev = (ngx_event_t *) ((uintptr_t) ev & (uintptr_t) ~1); | |
485 | |
486 if (ev->closed || ev->instance != instance) { | |
487 | |
488 /* | |
489 * the stale event from a file descriptor | |
490 * that was just closed in this iteration | |
491 */ | |
492 | |
493 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
494 "eventport: stale event %p", ev); | |
495 continue; | |
496 } | |
497 | |
498 revents = event_list[i].portev_events; | |
499 | |
500 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
501 "eventport: fd:%d, ev:%04Xd", | |
502 event_list[i].portev_object, revents); | |
503 | |
504 if (revents & (POLLERR|POLLHUP|POLLNVAL)) { | |
1128
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
505 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
506 "port_getn() error fd:%d ev:%04Xd", |
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
507 event_list[i].portev_object, revents); |
719 | 508 } |
509 | |
510 if (revents & ~(POLLIN|POLLOUT|POLLERR|POLLHUP|POLLNVAL)) { | |
511 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
512 "strange port_getn() events fd:%d ev:%04Xd", | |
513 event_list[i].portev_object, revents); | |
514 } | |
515 | |
516 if ((revents & (POLLERR|POLLHUP|POLLNVAL)) | |
517 && (revents & (POLLIN|POLLOUT)) == 0) | |
518 { | |
519 /* | |
520 * if the error events were returned without POLLIN or POLLOUT, | |
521 * then add these flags to handle the events at least in one | |
522 * active handler | |
523 */ | |
524 | |
525 revents |= POLLIN|POLLOUT; | |
526 } | |
527 | |
528 c = ev->data; | |
529 rev = c->read; | |
530 wev = c->write; | |
531 | |
532 rev->active = 0; | |
533 wev->active = 0; | |
534 | |
535 if (revents & POLLIN) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
536 rev->ready = 1; |
719 | 537 |
538 if (flags & NGX_POST_EVENTS) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
539 queue = rev->accept ? &ngx_posted_accept_events |
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
540 : &ngx_posted_events; |
719 | 541 |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
542 ngx_post_event(rev, queue); |
719 | 543 |
544 } else { | |
545 rev->handler(rev); | |
1287
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
546 |
5170
6362bd26e4b0
Events: protection from stale events in eventport and devpoll.
Valentin Bartenev <vbart@nginx.com>
parents:
4759
diff
changeset
|
547 if (ev->closed || ev->instance != instance) { |
1287
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
548 continue; |
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
549 } |
719 | 550 } |
551 | |
552 if (rev->accept) { | |
553 if (ngx_use_accept_mutex) { | |
554 ngx_accept_events = 1; | |
555 continue; | |
556 } | |
557 | |
558 if (port_associate(ep, PORT_SOURCE_FD, c->fd, POLLIN, | |
559 (void *) ((uintptr_t) ev | ev->instance)) | |
560 == -1) | |
561 { | |
562 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
563 "port_associate() failed"); | |
564 return NGX_ERROR; | |
565 } | |
566 } | |
567 } | |
568 | |
569 if (revents & POLLOUT) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
570 wev->ready = 1; |
719 | 571 |
572 if (flags & NGX_POST_EVENTS) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
573 ngx_post_event(wev, &ngx_posted_events); |
719 | 574 |
575 } else { | |
576 wev->handler(wev); | |
577 } | |
578 } | |
579 | |
580 continue; | |
581 | |
582 default: | |
583 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
584 "unexpected even_port object %d", | |
585 event_list[i].portev_object); | |
586 continue; | |
587 } | |
588 } | |
589 | |
590 return NGX_OK; | |
591 } | |
592 | |
593 | |
594 static void * | |
595 ngx_eventport_create_conf(ngx_cycle_t *cycle) | |
596 { | |
597 ngx_eventport_conf_t *epcf; | |
598 | |
599 epcf = ngx_palloc(cycle->pool, sizeof(ngx_eventport_conf_t)); | |
600 if (epcf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
1939
diff
changeset
|
601 return NULL; |
719 | 602 } |
603 | |
604 epcf->events = NGX_CONF_UNSET; | |
605 | |
606 return epcf; | |
607 } | |
608 | |
609 | |
610 static char * | |
611 ngx_eventport_init_conf(ngx_cycle_t *cycle, void *conf) | |
612 { | |
613 ngx_eventport_conf_t *epcf = conf; | |
614 | |
615 ngx_conf_init_uint_value(epcf->events, 32); | |
616 | |
617 return NGX_CONF_OK; | |
618 } |