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
|
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#define DEFAULT_CONNECTIONS 512
extern ngx_module_t ngx_select_module;
extern ngx_event_module_t ngx_select_module_ctx;
#if (HAVE_KQUEUE)
#include <ngx_kqueue_module.h>
#endif
#if (HAVE_DEVPOLL)
extern ngx_module_t ngx_devpoll_module;
#endif
#if (HAVE_AIO)
#include <ngx_aio_module.h>
#endif
static int ngx_event_init(ngx_cycle_t *cycle);
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_event_create_conf(ngx_cycle_t *cycle);
static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf);
int ngx_event_flags;
ngx_event_actions_t ngx_event_actions;
static int ngx_event_max_module;
static ngx_str_t events_name = ngx_string("events");
static ngx_command_t ngx_events_commands[] = {
{ ngx_string("events"),
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_events_block,
0,
0,
NULL },
ngx_null_command
};
ngx_module_t ngx_events_module = {
NGX_MODULE,
&events_name, /* module context */
ngx_events_commands, /* module directives */
NGX_CORE_MODULE, /* module type */
NULL, /* init module */
NULL /* init child */
};
static ngx_str_t event_core_name = ngx_string("event_core");
static ngx_command_t ngx_event_core_commands[] = {
{ ngx_string("connections"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_connections,
0,
0,
NULL },
{ ngx_string("use"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_event_use,
0,
0,
NULL },
ngx_null_command
};
ngx_event_module_t ngx_event_core_module_ctx = {
&event_core_name,
ngx_event_create_conf, /* create configuration */
ngx_event_init_conf, /* init configuration */
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
ngx_module_t ngx_event_core_module = {
NGX_MODULE,
&ngx_event_core_module_ctx, /* module context */
ngx_event_core_commands, /* module directives */
NGX_EVENT_MODULE, /* module type */
NULL, /* init module */
ngx_event_init /* init child */
};
static int ngx_event_init(ngx_cycle_t *cycle)
{
int m, i, fd;
ngx_event_t *rev, *wev;
ngx_listening_t *s;
ngx_connection_t *c;
ngx_event_conf_t *ecf;
ngx_event_module_t *module;
#if (WIN32)
ngx_iocp_conf_t *iocpcf;
#endif
if (cycle->old_cycle == NULL) {
ngx_event_timer_init(cycle);
}
ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
cycle->connection_n = ecf->connections;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
continue;
}
if (ngx_modules[m]->ctx_index == ecf->use) {
module = ngx_modules[m]->ctx;
if (module->actions.init(cycle) == NGX_ERROR) {
return NGX_ERROR;
}
break;
}
}
cycle->connections = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections,
cycle->log);
if (cycle->connections == NULL) {
return NGX_ERROR;
}
c = cycle->connections;
for (i = 0; i < cycle->connection_n; i++) {
c[i].fd = -1;
}
cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
cycle->log);
if (cycle->read_events == NULL) {
return NGX_ERROR;
}
cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
cycle->log);
if (cycle->write_events == NULL) {
return NGX_ERROR;
}
/* for each listening socket */
s = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++) {
fd = s[i].fd;
#if (WIN32)
/*
* Winsock assignes a socket number divisible by 4
* so to find a connection we divide a socket number by 4.
*/
fd /= 4;
#endif
c = &cycle->connections[fd];
rev = &cycle->read_events[fd];
wev = &cycle->write_events[fd];
ngx_memzero(c, sizeof(ngx_connection_t));
ngx_memzero(rev, sizeof(ngx_event_t));
c->fd = s[i].fd;
c->listening = &s[i];
c->ctx = s[i].ctx;
c->servers = s[i].servers;
c->log = s[i].log;
c->read = rev;
/* required by iocp in "c->write->active = 1" */
c->write = wev;
/* required by poll */
wev->index = NGX_INVALID_INDEX;
rev->log = c->log;
rev->data = c;
rev->index = NGX_INVALID_INDEX;
rev->available = 0;
#if (HAVE_DEFERRED_ACCEPT)
rev->deferred_accept = s[i].deferred_accept;
#endif
if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) {
if (s[i].remain) {
/*
* delete the old accept events that were bound to
* the old cycle read events array
*/
if (ngx_del_event(&cycle->old_cycle->read_events[fd],
NGX_READ_EVENT, 0) == NGX_ERROR) {
return NGX_ERROR;
}
cycle->old_cycle->connections[fd].fd = -1;
}
}
#if (WIN32)
if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
rev->event_handler = &ngx_event_acceptex;
if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) {
return NGX_ERROR;
}
iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module);
if (ngx_event_post_acceptex(&s[i], iocpcf->acceptex) == NGX_ERROR) {
return NGX_ERROR;
}
} else {
rev->event_handler = &ngx_event_accept;
ngx_add_event(rev, NGX_READ_EVENT, 0);
}
#else
rev->event_handler = &ngx_event_accept;
ngx_add_event(rev, NGX_READ_EVENT, 0);
#endif
}
return NGX_OK;
}
static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
int m;
char *rv;
void ***ctx;
ngx_conf_t pcf;
ngx_event_module_t *module;
/* count the number of the event modules and set up their indices */
ngx_event_max_module = 0;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
continue;
}
ngx_modules[m]->ctx_index = ngx_event_max_module++;
}
ngx_test_null(ctx, ngx_pcalloc(cf->pool, sizeof(void *)), NGX_CONF_ERROR);
ngx_test_null(*ctx,
ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *)),
NGX_CONF_ERROR);
*(void **) conf = ctx;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->create_conf) {
ngx_test_null((*ctx)[ngx_modules[m]->ctx_index],
module->create_conf(cf->cycle),
NGX_CONF_ERROR);
}
}
pcf = *cf;
cf->ctx = ctx;
cf->module_type = NGX_EVENT_MODULE;
cf->cmd_type = NGX_EVENT_CONF;
rv = ngx_conf_parse(cf, NULL);
*cf = pcf;
if (rv != NGX_CONF_OK)
return rv;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->init_conf) {
rv = module->init_conf(cf->cycle,
(*ctx)[ngx_modules[m]->ctx_index]);
if (rv != NGX_CONF_OK) {
return rv;
}
}
}
return NGX_CONF_OK;
}
static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
ngx_event_conf_t *ecf = conf;
ngx_str_t *value;
if (ecf->connections != NGX_CONF_UNSET) {
return "is duplicate" ;
}
value = cf->args->elts;
ecf->connections = ngx_atoi(value[1].data, value[1].len);
if (ecf->connections == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid number \"%s\"", value[1].data);
return NGX_CONF_ERROR;
}
cf->cycle->connection_n = ecf->connections;
return NGX_CONF_OK;
}
static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_event_conf_t *ecf = conf;
int m;
ngx_str_t *value;
ngx_event_conf_t *old_ecf;
ngx_event_module_t *module;
if (ecf->use != NGX_CONF_UNSET) {
return "is duplicate" ;
}
value = cf->args->elts;
if (cf->cycle->old_cycle) {
old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,
ngx_event_core_module);
} else {
old_ecf = NULL;
}
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_EVENT_MODULE) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->name->len == value[1].len) {
if (ngx_strcmp(module->name->data, value[1].data) == 0) {
ecf->use = ngx_modules[m]->ctx_index;
ecf->name = module->name->data;
if (old_ecf && old_ecf->use != ecf->use) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the \"%s\" event type must be the same as "
"in previous configuration - \"%s\" "
"and it can not be changed on the fly, "
"to change it you need to stop server "
"and start it again",
value[1].data, old_ecf->name);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
}
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid event type \"%s\"", value[1].data);
return NGX_CONF_ERROR;
}
static void *ngx_event_create_conf(ngx_cycle_t *cycle)
{
ngx_event_conf_t *ecf;
ngx_test_null(ecf, ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t)),
NGX_CONF_ERROR);
ecf->connections = NGX_CONF_UNSET;
ecf->timer_queues = NGX_CONF_UNSET;
ecf->use = NGX_CONF_UNSET;
ecf->name = (void *) NGX_CONF_UNSET;
return ecf;
}
static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf)
{
ngx_event_conf_t *ecf = conf;
#if (HAVE_KQUEUE)
ngx_conf_init_value(ecf->connections, DEFAULT_CONNECTIONS);
ngx_conf_init_value(ecf->use, ngx_kqueue_module.ctx_index);
ngx_conf_init_ptr_value(ecf->name, ngx_kqueue_module_ctx.name->data);
#elif (HAVE_DEVPOLL)
ngx_conf_init_value(ecf->connections, DEFAULT_CONNECTIONS);
ngx_conf_init_value(ecf->use, ngx_devpoll_module.ctx_index);
ngx_conf_init_ptr_value(ecf->name, ngx_devpoll_module_ctx.name->data);
#else /* HAVE_SELECT */
ngx_conf_init_value(ecf->connections,
FD_SETSIZE < DEFAULT_CONNECTIONS ? FD_SETSIZE : DEFAULT_CONNECTIONS);
ngx_conf_init_value(ecf->use, ngx_select_module.ctx_index);
ngx_conf_init_ptr_value(ecf->name, ngx_select_module_ctx.name->data);
#endif
cycle->connection_n = ecf->connections;
ngx_conf_init_value(ecf->timer_queues, 10);
return NGX_CONF_OK;
}
|