Mercurial > hg > nginx-quic
annotate src/mail/ngx_mail.c @ 1207:103988cef757
use memory optimization for i386 and amd64 only:
this fixes SIGBUS in PCRE on sparc64
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 21 May 2007 13:49:35 +0000 |
parents | 68f30ab68bb7 |
children | c6c33f81fd79 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
5 |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_event.h> |
1136 | 10 #include <ngx_mail.h> |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
11 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
12 |
1136 | 13 static char *ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
14 static int ngx_libc_cdecl ngx_mail_cmp_conf_in_addrs(const void *one, | |
641 | 15 const void *two); |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
16 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
17 |
1136 | 18 ngx_uint_t ngx_mail_max_module; |
521 | 19 |
20 | |
1136 | 21 static ngx_command_t ngx_mail_commands[] = { |
22 | |
23 { ngx_string("mail"), | |
24 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, | |
25 ngx_mail_block, | |
26 0, | |
27 0, | |
28 NULL }, | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
29 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
30 { ngx_string("imap"), |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
31 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, |
1136 | 32 ngx_mail_block, |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
33 0, |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
34 0, |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
35 NULL }, |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
36 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
37 ngx_null_command |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
38 }; |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
39 |
577 | 40 |
1136 | 41 static ngx_core_module_t ngx_mail_module_ctx = { |
42 ngx_string("mail"), | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
43 NULL, |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
44 NULL |
577 | 45 }; |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
46 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
47 |
1136 | 48 ngx_module_t ngx_mail_module = { |
509 | 49 NGX_MODULE_V1, |
1136 | 50 &ngx_mail_module_ctx, /* module context */ |
51 ngx_mail_commands, /* module directives */ | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
52 NGX_CORE_MODULE, /* module type */ |
541 | 53 NULL, /* init master */ |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
54 NULL, /* init module */ |
541 | 55 NULL, /* init process */ |
56 NULL, /* init thread */ | |
57 NULL, /* exit thread */ | |
58 NULL, /* exit process */ | |
59 NULL, /* exit master */ | |
60 NGX_MODULE_V1_PADDING | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
61 }; |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
62 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
63 |
521 | 64 static char * |
1136 | 65 ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
66 { |
521 | 67 char *rv; |
641 | 68 u_char *text; |
69 size_t len; | |
70 ngx_uint_t i, a, l, m, mi, s, p, last, bind_all, done; | |
521 | 71 ngx_conf_t pcf; |
641 | 72 ngx_array_t in_ports; |
73 ngx_listening_t *ls; | |
1136 | 74 ngx_mail_listen_t *imls; |
75 ngx_mail_module_t *module; | |
76 ngx_mail_in_port_t *imip; | |
77 ngx_mail_conf_ctx_t *ctx; | |
78 ngx_mail_conf_in_port_t *in_port; | |
79 ngx_mail_conf_in_addr_t *in_addr; | |
80 ngx_mail_core_srv_conf_t **cscfp; | |
81 ngx_mail_core_main_conf_t *cmcf; | |
521 | 82 |
1136 | 83 if (cmd->name.data[0] == 'i') { |
84 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
85 "the \"imap\" directive is deprecated, " | |
86 "use the \"mail\" directive instead"); | |
87 } | |
521 | 88 |
1136 | 89 /* the main mail context */ |
90 | |
91 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_mail_conf_ctx_t)); | |
521 | 92 if (ctx == NULL) { |
93 return NGX_CONF_ERROR; | |
94 } | |
95 | |
1136 | 96 *(ngx_mail_conf_ctx_t **) conf = ctx; |
521 | 97 |
98 /* count the number of the http modules and set up their indices */ | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
99 |
1136 | 100 ngx_mail_max_module = 0; |
521 | 101 for (m = 0; ngx_modules[m]; m++) { |
1136 | 102 if (ngx_modules[m]->type != NGX_MAIL_MODULE) { |
521 | 103 continue; |
104 } | |
105 | |
1136 | 106 ngx_modules[m]->ctx_index = ngx_mail_max_module++; |
521 | 107 } |
108 | |
109 | |
1136 | 110 /* the mail main_conf context, it is the same in the all mail contexts */ |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
111 |
521 | 112 ctx->main_conf = ngx_pcalloc(cf->pool, |
1136 | 113 sizeof(void *) * ngx_mail_max_module); |
521 | 114 if (ctx->main_conf == NULL) { |
115 return NGX_CONF_ERROR; | |
116 } | |
117 | |
118 | |
119 /* | |
1136 | 120 * the mail null srv_conf context, it is used to merge |
521 | 121 * the server{}s' srv_conf's |
122 */ | |
123 | |
1136 | 124 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_mail_max_module); |
521 | 125 if (ctx->srv_conf == NULL) { |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
126 return NGX_CONF_ERROR; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
127 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
128 |
521 | 129 |
130 /* | |
131 * create the main_conf's, the null srv_conf's, and the null loc_conf's | |
1136 | 132 * of the all mail modules |
521 | 133 */ |
134 | |
135 for (m = 0; ngx_modules[m]; m++) { | |
1136 | 136 if (ngx_modules[m]->type != NGX_MAIL_MODULE) { |
521 | 137 continue; |
138 } | |
139 | |
140 module = ngx_modules[m]->ctx; | |
141 mi = ngx_modules[m]->ctx_index; | |
142 | |
143 if (module->create_main_conf) { | |
144 ctx->main_conf[mi] = module->create_main_conf(cf); | |
145 if (ctx->main_conf[mi] == NULL) { | |
146 return NGX_CONF_ERROR; | |
147 } | |
148 } | |
149 | |
150 if (module->create_srv_conf) { | |
151 ctx->srv_conf[mi] = module->create_srv_conf(cf); | |
152 if (ctx->srv_conf[mi] == NULL) { | |
153 return NGX_CONF_ERROR; | |
154 } | |
155 } | |
156 } | |
157 | |
158 | |
1136 | 159 /* parse inside the mail{} block */ |
521 | 160 |
161 pcf = *cf; | |
162 cf->ctx = ctx; | |
163 | |
1136 | 164 cf->module_type = NGX_MAIL_MODULE; |
165 cf->cmd_type = NGX_MAIL_MAIN_CONF; | |
521 | 166 rv = ngx_conf_parse(cf, NULL); |
167 | |
168 if (rv != NGX_CONF_OK) { | |
169 *cf = pcf; | |
170 return rv; | |
171 } | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
172 |
521 | 173 |
1136 | 174 /* init mail{} main_conf's, merge the server{}s' srv_conf's */ |
521 | 175 |
1136 | 176 cmcf = ctx->main_conf[ngx_mail_core_module.ctx_index]; |
521 | 177 cscfp = cmcf->servers.elts; |
178 | |
179 for (m = 0; ngx_modules[m]; m++) { | |
1136 | 180 if (ngx_modules[m]->type != NGX_MAIL_MODULE) { |
521 | 181 continue; |
182 } | |
183 | |
184 module = ngx_modules[m]->ctx; | |
185 mi = ngx_modules[m]->ctx_index; | |
186 | |
1136 | 187 /* init mail{} main_conf's */ |
521 | 188 |
189 if (module->init_main_conf) { | |
577 | 190 rv = module->init_main_conf(cf, ctx->main_conf[mi]); |
521 | 191 if (rv != NGX_CONF_OK) { |
192 *cf = pcf; | |
193 return rv; | |
194 } | |
195 } | |
196 | |
197 for (s = 0; s < cmcf->servers.nelts; s++) { | |
577 | 198 |
521 | 199 /* merge the server{}s' srv_conf's */ |
577 | 200 |
521 | 201 if (module->merge_srv_conf) { |
202 rv = module->merge_srv_conf(cf, | |
203 ctx->srv_conf[mi], | |
204 cscfp[s]->ctx->srv_conf[mi]); | |
205 if (rv != NGX_CONF_OK) { | |
206 *cf = pcf; | |
207 return rv; | |
208 } | |
209 } | |
210 } | |
211 } | |
212 | |
1136 | 213 /* mail{}'s cf->ctx was needed while the configuration merging */ |
577 | 214 |
215 *cf = pcf; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
216 |
641 | 217 |
218 if (ngx_array_init(&in_ports, cf->temp_pool, 4, | |
1136 | 219 sizeof(ngx_mail_conf_in_port_t)) |
641 | 220 != NGX_OK) |
221 { | |
222 return NGX_CONF_ERROR; | |
223 } | |
224 | |
225 imls = cmcf->listen.elts; | |
226 | |
227 for (l = 0; l < cmcf->listen.nelts; l++) { | |
228 | |
229 /* AF_INET only */ | |
230 | |
231 in_port = in_ports.elts; | |
232 for (p = 0; p < in_ports.nelts; p++) { | |
233 if (in_port[p].port == imls[l].port) { | |
234 in_port = &in_port[p]; | |
235 goto found; | |
236 } | |
237 } | |
238 | |
239 in_port = ngx_array_push(&in_ports); | |
240 if (in_port == NULL) { | |
241 return NGX_CONF_ERROR; | |
242 } | |
243 | |
244 in_port->port = imls[l].port; | |
245 | |
246 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 2, | |
1136 | 247 sizeof(ngx_mail_conf_in_addr_t)) |
641 | 248 != NGX_OK) |
249 { | |
250 return NGX_CONF_ERROR; | |
251 } | |
252 | |
253 found: | |
254 | |
255 in_addr = ngx_array_push(&in_port->addrs); | |
256 if (in_addr == NULL) { | |
257 return NGX_CONF_ERROR; | |
258 } | |
259 | |
260 in_addr->addr = imls[l].addr; | |
261 in_addr->ctx = imls[l].ctx; | |
262 in_addr->bind = imls[l].bind; | |
263 } | |
264 | |
265 /* optimize the lists of ports and addresses */ | |
266 | |
267 /* AF_INET only */ | |
268 | |
269 in_port = in_ports.elts; | |
270 for (p = 0; p < in_ports.nelts; p++) { | |
271 | |
272 ngx_qsort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts, | |
1136 | 273 sizeof(ngx_mail_conf_in_addr_t), ngx_mail_cmp_conf_in_addrs); |
641 | 274 |
275 in_addr = in_port[p].addrs.elts; | |
276 last = in_port[p].addrs.nelts; | |
277 | |
278 /* | |
279 * if there is the binding to the "*:port" then we need to bind() | |
280 * to the "*:port" only and ignore the other bindings | |
281 */ | |
282 | |
283 if (in_addr[last - 1].addr == INADDR_ANY) { | |
284 in_addr[last - 1].bind = 1; | |
285 bind_all = 0; | |
286 | |
287 } else { | |
288 bind_all = 1; | |
289 } | |
290 | |
291 for (a = 0; a < last; /* void */ ) { | |
292 | |
293 if (!bind_all && !in_addr[a].bind) { | |
294 a++; | |
295 continue; | |
296 } | |
297 | |
298 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr, | |
299 in_port[p].port); | |
300 if (ls == NULL) { | |
301 return NGX_CONF_ERROR; | |
302 } | |
303 | |
304 ls->backlog = -1; | |
305 ls->rcvbuf = -1; | |
306 ls->sndbuf = -1; | |
307 | |
308 ls->addr_ntop = 1; | |
1136 | 309 ls->handler = ngx_mail_init_connection; |
641 | 310 ls->pool_size = 256; |
311 | |
312 /* STUB */ | |
313 ls->log = *cf->cycle->new_log; | |
314 ls->log.data = &ls->addr_text; | |
315 ls->log.handler = ngx_accept_log_error; | |
316 /**/ | |
317 | |
1136 | 318 imip = ngx_palloc(cf->pool, sizeof(ngx_mail_in_port_t)); |
641 | 319 if (imip == NULL) { |
320 return NGX_CONF_ERROR; | |
321 } | |
322 | |
323 ls->servers = imip; | |
324 | |
325 in_addr = in_port[p].addrs.elts; | |
326 | |
327 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) { | |
328 imip->naddrs = 1; | |
329 done = 0; | |
330 | |
331 } else if (in_port[p].addrs.nelts > 1 | |
332 && in_addr[last - 1].addr == INADDR_ANY) | |
333 { | |
334 imip->naddrs = last; | |
335 done = 1; | |
336 | |
337 } else { | |
338 imip->naddrs = 1; | |
339 done = 0; | |
340 } | |
341 | |
342 #if 0 | |
343 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, | |
344 "%ui: %V %d %ui %ui", | |
345 a, &ls->addr_text, in_addr[a].bind, | |
346 imip->naddrs, last); | |
347 #endif | |
348 | |
349 imip->addrs = ngx_pcalloc(cf->pool, | |
1136 | 350 imip->naddrs * sizeof(ngx_mail_in_addr_t)); |
641 | 351 if (imip->addrs == NULL) { |
352 return NGX_CONF_ERROR; | |
353 } | |
354 | |
355 for (i = 0; i < imip->naddrs; i++) { | |
356 imip->addrs[i].addr = in_addr[i].addr; | |
357 imip->addrs[i].ctx = in_addr[i].ctx; | |
358 | |
359 text = ngx_palloc(cf->pool, | |
360 INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1); | |
361 if (text == NULL) { | |
362 return NGX_CONF_ERROR; | |
363 } | |
364 | |
365 len = ngx_inet_ntop(AF_INET, &in_addr[i].addr, text, | |
366 INET_ADDRSTRLEN); | |
367 | |
368 len = ngx_sprintf(text + len, ":%d", in_port[p].port) - text; | |
369 | |
370 imip->addrs[i].addr_text.len = len; | |
371 imip->addrs[i].addr_text.data = text; | |
372 } | |
373 | |
374 if (done) { | |
375 break; | |
376 } | |
377 | |
378 in_addr++; | |
379 in_port[p].addrs.elts = in_addr; | |
380 last--; | |
381 | |
382 a = 0; | |
383 } | |
384 } | |
385 | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
386 return NGX_CONF_OK; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
387 } |
641 | 388 |
389 | |
390 static int ngx_libc_cdecl | |
1136 | 391 ngx_mail_cmp_conf_in_addrs(const void *one, const void *two) |
641 | 392 { |
1136 | 393 ngx_mail_conf_in_addr_t *first, *second; |
641 | 394 |
1136 | 395 first = (ngx_mail_conf_in_addr_t *) one; |
396 second = (ngx_mail_conf_in_addr_t *) two; | |
641 | 397 |
398 if (first->addr == INADDR_ANY) { | |
399 /* the INADDR_ANY must be the last resort, shift it to the end */ | |
400 return 1; | |
401 } | |
402 | |
403 if (first->bind && !second->bind) { | |
404 /* shift explicit bind()ed addresses to the start */ | |
405 return -1; | |
406 } | |
407 | |
408 if (!first->bind && second->bind) { | |
409 /* shift explicit bind()ed addresses to the start */ | |
410 return 1; | |
411 } | |
412 | |
413 /* do not sort by default */ | |
414 | |
415 return 0; | |
416 } |