1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* Copyright (C) Igor Sysoev
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#if (TEST_BUILD_DEVPOLL)
/* Solaris declarations */
#define POLLREMOVE 0x0800
#define DP_POLL 0xD001
struct dvpoll {
struct pollfd *dp_fds;
int dp_nfds;
int dp_timeout;
};
#endif
typedef struct {
u_int changes;
u_int events;
} ngx_devpoll_conf_t;
static int ngx_devpoll_init(ngx_cycle_t *cycle);
static void ngx_devpoll_done(ngx_cycle_t *cycle);
static int ngx_devpoll_add_event(ngx_event_t *ev, int event, u_int flags);
static int ngx_devpoll_del_event(ngx_event_t *ev, int event, u_int flags);
static int ngx_devpoll_set_event(ngx_event_t *ev, int event, u_int flags);
static int ngx_devpoll_process_events(ngx_cycle_t *cycle);
static void *ngx_devpoll_create_conf(ngx_cycle_t *cycle);
static char *ngx_devpoll_init_conf(ngx_cycle_t *cycle, void *conf);
static int dp = -1;
static struct pollfd *change_list, *event_list;
static u_int nchanges, max_changes, nevents;
static ngx_event_t **change_index;
static ngx_str_t devpoll_name = ngx_string("/dev/poll");
static ngx_command_t ngx_devpoll_commands[] = {
{ngx_string("devpoll_changes"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_devpoll_conf_t, changes),
NULL},
{ngx_string("devpoll_events"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_devpoll_conf_t, events),
NULL},
ngx_null_command
};
ngx_event_module_t ngx_devpoll_module_ctx = {
&devpoll_name,
ngx_devpoll_create_conf, /* create configuration */
ngx_devpoll_init_conf, /* init configuration */
{
ngx_devpoll_add_event, /* add an event */
ngx_devpoll_del_event, /* delete an event */
ngx_devpoll_add_event, /* enable an event */
ngx_devpoll_del_event, /* disable an event */
NULL, /* add an connection */
NULL, /* delete an connection */
NULL, /* process the changes */
ngx_devpoll_process_events, /* process the events */
ngx_devpoll_init, /* init the events */
ngx_devpoll_done, /* done the events */
}
};
ngx_module_t ngx_devpoll_module = {
NGX_MODULE,
&ngx_devpoll_module_ctx, /* module context */
ngx_devpoll_commands, /* module directives */
NGX_EVENT_MODULE, /* module type */
NULL, /* init module */
NULL /* init process */
};
static int ngx_devpoll_init(ngx_cycle_t *cycle)
{
size_t n;
ngx_devpoll_conf_t *dpcf;
dpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_devpoll_module);
if (dp == -1) {
dp = open("/dev/poll", O_RDWR);
if (dp == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"open(/dev/poll) failed");
return NGX_ERROR;
}
}
if (max_changes < dpcf->changes) {
if (nchanges) {
n = nchanges * sizeof(struct pollfd);
if (write(dp, change_list, n) != (ssize_t) n) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"write(/dev/poll) failed");
return NGX_ERROR;
}
nchanges = 0;
}
if (change_list) {
ngx_free(change_list);
}
ngx_test_null(change_list,
ngx_alloc(sizeof(struct pollfd) * dpcf->changes,
cycle->log),
NGX_ERROR);
if (change_index) {
ngx_free(change_index);
}
ngx_test_null(change_index,
ngx_alloc(sizeof(ngx_event_t *) * dpcf->changes,
cycle->log),
NGX_ERROR);
}
max_changes = dpcf->changes;
if (nevents < dpcf->events) {
if (event_list) {
ngx_free(event_list);
}
ngx_test_null(event_list,
ngx_alloc(sizeof(struct pollfd) * dpcf->events,
cycle->log),
NGX_ERROR);
}
nevents = dpcf->events;
ngx_io = ngx_os_io;
ngx_event_actions = ngx_devpoll_module_ctx.actions;
ngx_event_flags = NGX_USE_LEVEL_EVENT;
return NGX_OK;
}
static void ngx_devpoll_done(ngx_cycle_t *cycle)
{
if (close(dp) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"close(/dev/poll) failed");
}
dp = -1;
ngx_free(change_list);
ngx_free(event_list);
ngx_free(change_index);
change_list = NULL;
event_list = NULL;
change_index = NULL;
max_changes = 0;
nchanges = 0;
nevents = 0;
}
static int ngx_devpoll_add_event(ngx_event_t *ev, int event, u_int flags)
{
#if (NGX_DEBUG)
ngx_connection_t *c;
#endif
#if (NGX_READ_EVENT != POLLIN)
if (event == NGX_READ_EVENT) {
event = POLLOUT;
#if (NGX_WRITE_EVENT != POLLOUT)
} else {
event = POLLIN;
#endif
}
#endif
#if (NGX_DEBUG)
c = ev->data;
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"devpoll add event: fd:%d ev:%04X", c->fd, event);
#endif
ev->active = 1;
return ngx_devpoll_set_event(ev, event, 0);
}
static int ngx_devpoll_del_event(ngx_event_t *ev, int event, u_int flags)
{
ngx_event_t *e;
ngx_connection_t *c;
c = ev->data;
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"devpoll del event: fd:%d ev:%04X", c->fd, event);
if (ngx_devpoll_set_event(ev, POLLREMOVE, flags) == NGX_ERROR) {
return NGX_ERROR;
}
ev->active = 0;
if (flags & NGX_CLOSE_EVENT) {
return NGX_OK;
}
/* we need to restore the second event if it exists */
if (event == NGX_READ_EVENT) {
if (ev->accept) {
return NGX_OK;
}
e = c->write;
event = POLLOUT;
} else {
e = c->read;
event = POLLIN;
}
if (e) {
return ngx_devpoll_set_event(e, event, 0);
}
return NGX_OK;
}
static int ngx_devpoll_set_event(ngx_event_t *ev, int event, u_int flags)
{
size_t n;
ngx_connection_t *c;
c = ev->data;
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"devpoll fd:%d ev:%d fl:%d", c->fd, event, flags);
if (nchanges >= max_changes) {
ngx_log_error(NGX_LOG_WARN, ev->log, 0,
"/dev/pool change list is filled up");
n = nchanges * sizeof(struct pollfd);
if (write(dp, change_list, n) != (ssize_t) n) {
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
"write(/dev/poll) failed");
return NGX_ERROR;
}
nchanges = 0;
}
change_list[nchanges].fd = c->fd;
change_list[nchanges].events = event;
change_list[nchanges].revents = 0;
change_index[nchanges] = ev;
ev->index = nchanges;
nchanges++;
if (flags & NGX_CLOSE_EVENT) {
n = nchanges * sizeof(struct pollfd);
if (write(dp, change_list, n) != (ssize_t) n) {
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
"write(/dev/poll) failed");
return NGX_ERROR;
}
nchanges = 0;
}
return NGX_OK;
}
int ngx_devpoll_process_events(ngx_cycle_t *cycle)
{
int events;
ngx_int_t i;
ngx_uint_t j, lock, accept_lock, expire;
size_t n;
ngx_msec_t timer;
ngx_err_t err;
ngx_cycle_t **old_cycle;
ngx_event_t *rev, *wev;
ngx_connection_t *c;
ngx_epoch_msec_t delta;
struct dvpoll dvp;
struct timeval tv;
for ( ;; ) {
timer = ngx_event_find_timer();
if (timer != 0) {
break;
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"devpoll expired timer");
ngx_event_expire_timers((ngx_msec_t)
(ngx_elapsed_msec - ngx_old_elapsed_msec));
}
/* NGX_TIMER_INFINITE == INFTIM */
if (timer == NGX_TIMER_INFINITE) {
expire = 0;
} else {
expire = 1;
}
ngx_old_elapsed_msec = ngx_elapsed_msec;
accept_lock = 0;
if (ngx_accept_mutex) {
if (ngx_accept_disabled > 0) {
ngx_accept_disabled--;
} else {
if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
return NGX_ERROR;
}
if (ngx_accept_mutex_held) {
accept_lock = 1;
} else if (timer == NGX_TIMER_INFINITE
|| timer > ngx_accept_mutex_delay)
{
timer = ngx_accept_mutex_delay;
expire = 0;
}
}
}
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"devpoll timer: %d", timer);
if (nchanges) {
n = nchanges * sizeof(struct pollfd);
if (write(dp, change_list, n) != (ssize_t) n) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"write(/dev/poll) failed");
ngx_accept_mutex_unlock();
return NGX_ERROR;
}
}
dvp.dp_fds = event_list;
dvp.dp_nfds = nevents;
dvp.dp_timeout = timer;
events = ioctl(dp, DP_POLL, &dvp);
if (events == -1) {
err = ngx_errno;
} else {
err = 0;
}
nchanges = 0;
ngx_gettimeofday(&tv);
ngx_time_update(tv.tv_sec);
delta = ngx_elapsed_msec;
ngx_elapsed_msec = (ngx_epoch_msec_t) tv.tv_sec * 1000
+ tv.tv_usec / 1000 - ngx_start_msec;
if (err) {
ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
cycle->log, err, "ioctl(DP_POLL) failed");
ngx_accept_mutex_unlock();
return NGX_ERROR;
}
if (timer != NGX_TIMER_INFINITE) {
delta = ngx_elapsed_msec - delta;
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"devpoll timer: %d, delta: %d", timer, (int) delta);
} else {
if (events == 0) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"ioctl(DP_POLL) returned no events without timeout");
ngx_accept_mutex_unlock();
return NGX_ERROR;
}
}
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
ngx_accept_mutex_unlock();
return NGX_ERROR;
}
lock = 1;
for (i = 0; i < events; i++) {
c = &ngx_cycle->connections[event_list[i].fd];
if (c->fd == -1) {
old_cycle = ngx_old_cycles.elts;
for (j = 0; j < ngx_old_cycles.nelts; j++) {
if (old_cycle[j] == NULL) {
continue;
}
c = &old_cycle[j]->connections[event_list[i].fd];
if (c->fd != -1) {
break;
}
}
}
if (c->fd == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "unknown cycle");
exit(1);
}
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"devpoll: fd:%d, ev:%04X, rev:%04X",
event_list[i].fd,
event_list[i].events, event_list[i].revents);
if (event_list[i].revents & (POLLERR|POLLHUP|POLLNVAL)) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"ioctl(DP_POLL) error fd:%d ev:%04X rev:%04X",
event_list[i].fd,
event_list[i].events, event_list[i].revents);
}
if (event_list[i].revents & ~(POLLIN|POLLOUT|POLLERR|POLLHUP|POLLNVAL))
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"strange ioctl(DP_POLL) events "
"fd:%d ev:%04X rev:%04X",
event_list[i].fd,
event_list[i].events, event_list[i].revents);
}
wev = c->write;
if ((event_list[i].events & (POLLOUT|POLLERR|POLLHUP)) && wev->active) {
wev->ready = 1;
if (!ngx_threaded && !ngx_accept_mutex_held) {
wev->event_handler(wev);
} else {
ngx_post_event(wev);
}
}
/*
* POLLIN must be handled after POLLOUT because we use
* the optimization to avoid the unnecessary mutex locking/unlocking
* if the accept event is the last one.
*/
rev = c->read;
if ((event_list[i].events & (POLLIN|POLLERR|POLLHUP)) && rev->active) {
rev->ready = 1;
if (!ngx_threaded && !ngx_accept_mutex_held) {
rev->event_handler(rev);
} else if (!rev->accept) {
ngx_post_event(rev);
} else if (ngx_accept_disabled <= 0) {
ngx_mutex_unlock(ngx_posted_events_mutex);
c->read->event_handler(rev);
if (ngx_accept_disabled > 0) {
ngx_accept_mutex_unlock();
accept_lock = 0;
}
if (i + 1 == events) {
lock = 0;
break;
}
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
if (accept_lock) {
ngx_accept_mutex_unlock();
}
return NGX_ERROR;
}
}
}
}
if (accept_lock) {
ngx_accept_mutex_unlock();
}
if (lock) {
ngx_mutex_unlock(ngx_posted_events_mutex);
}
if (expire && delta) {
ngx_event_expire_timers((ngx_msec_t) delta);
}
if (!ngx_threaded) {
ngx_event_process_posted(cycle);
}
return NGX_OK;
}
static void *ngx_devpoll_create_conf(ngx_cycle_t *cycle)
{
ngx_devpoll_conf_t *dpcf;
ngx_test_null(dpcf, ngx_palloc(cycle->pool, sizeof(ngx_devpoll_conf_t)),
NGX_CONF_ERROR);
dpcf->changes = NGX_CONF_UNSET;
dpcf->events = NGX_CONF_UNSET;
return dpcf;
}
static char *ngx_devpoll_init_conf(ngx_cycle_t *cycle, void *conf)
{
ngx_devpoll_conf_t *dpcf = conf;
ngx_conf_init_unsigned_value(dpcf->changes, 512);
ngx_conf_init_unsigned_value(dpcf->events, 512);
return NGX_CONF_OK;
}
|