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