Mercurial > hg > nginx
comparison src/core/nginx.c @ 109:a9bc21d63fe4
nginx-0.0.1-2003-07-02-18:41:17 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 02 Jul 2003 14:41:17 +0000 |
parents | adc093f880c8 |
children | 152567c11325 |
comparison
equal
deleted
inserted
replaced
108:adc093f880c8 | 109:a9bc21d63fe4 |
---|---|
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_event.h> | 4 #include <ngx_event.h> |
5 #include <nginx.h> | 5 #include <nginx.h> |
6 | 6 |
7 | 7 |
8 | 8 static ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log); |
9 static int ngx_open_listening_sockets(ngx_log_t *log); | 9 static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log); |
10 | 10 |
11 | 11 |
12 void ****ngx_conf_ctx; | 12 void ****ngx_conf_ctx; |
13 | 13 |
14 | 14 |
15 ngx_os_io_t ngx_io; | 15 ngx_os_io_t ngx_io; |
16 | 16 |
17 | 17 |
18 int ngx_max_module; | 18 int ngx_max_module; |
19 #if 0 | |
19 void *ctx_conf; | 20 void *ctx_conf; |
21 #endif | |
20 | 22 |
21 int ngx_connection_counter; | 23 int ngx_connection_counter; |
22 | 24 |
25 #if 0 | |
23 ngx_array_t ngx_listening_sockets; | 26 ngx_array_t ngx_listening_sockets; |
24 | 27 #endif |
25 | 28 |
26 #if 0 | 29 |
30 #if 1 | |
27 | 31 |
28 int main(int argc, char *const *argv) | 32 int main(int argc, char *const *argv) |
29 { | 33 { |
30 ngx_str_t conf_file; | 34 int i; |
31 ngx_log_t *log; | 35 ngx_log_t *log; |
32 ngx_conf_t conf; | 36 ngx_conf_t conf; |
33 ngx_cycle_t *cycle; | 37 ngx_cycle_t *cycle, *new_cycle; |
34 | 38 |
35 /* TODO */ ngx_max_sockets = -1; | 39 /* TODO */ ngx_max_sockets = -1; |
36 | 40 |
37 log = ngx_log_init_errlog(); | 41 log = ngx_log_init_errlog(); |
38 | 42 |
62 | 66 |
63 /* threads */ | 67 /* threads */ |
64 | 68 |
65 for ( ;; ) { | 69 for ( ;; ) { |
66 | 70 |
67 worker(cycle->log); | 71 ngx_worker(cycle); |
68 | 72 |
69 new_cycle = ngx_init_cycle(cycle, cycle->log); | 73 new_cycle = ngx_init_cycle(cycle, cycle->log); |
70 if (new_cycle) == NULL) { | 74 if (new_cycle == NULL) { |
71 continue; | 75 continue; |
72 } | 76 } |
73 | 77 |
74 cycle = new_cycle; | 78 cycle = new_cycle; |
75 break; | 79 break; |
78 | 82 |
79 return 0; | 83 return 0; |
80 } | 84 } |
81 | 85 |
82 | 86 |
83 static int ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log) | 87 static ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log) |
84 { | 88 { |
85 int i, n; | 89 int i, n, failed; |
90 ngx_str_t conf_file; | |
86 ngx_conf_t conf; | 91 ngx_conf_t conf; |
87 ngx_pool_t *pool; | 92 ngx_pool_t *pool; |
88 ngx_cycle_t *cycle; | 93 ngx_cycle_t *cycle; |
89 ngx_open_file_t *file; | 94 ngx_open_file_t *file; |
90 ngx_listening_t *ls, *nls; | 95 ngx_listening_t *ls, *nls; |
99 if (cycle == NULL) { | 104 if (cycle == NULL) { |
100 ngx_destroy_pool(pool); | 105 ngx_destroy_pool(pool); |
101 return NULL; | 106 return NULL; |
102 } | 107 } |
103 cycle->pool = pool; | 108 cycle->pool = pool; |
109 | |
110 cycle->log = ngx_log_create_errlog(cycle); | |
111 if (cycle->log == NULL) { | |
112 ngx_destroy_pool(pool); | |
113 return NULL; | |
114 } | |
104 | 115 |
105 n = old_cycle ? old_cycle->open_files.nelts : 20; | 116 n = old_cycle ? old_cycle->open_files.nelts : 20; |
106 cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t)); | 117 cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t)); |
107 if (cycle->open_files.elts == NULL) { | 118 if (cycle->open_files.elts == NULL) { |
108 ngx_destroy_pool(pool); | 119 ngx_destroy_pool(pool); |
151 if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) { | 162 if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) { |
152 ngx_destroy_pool(pool); | 163 ngx_destroy_pool(pool); |
153 return NULL; | 164 return NULL; |
154 } | 165 } |
155 | 166 |
167 failed = 0; | |
168 | |
156 for (i = 0; ngx_modules[i]; i++) { | 169 for (i = 0; ngx_modules[i]; i++) { |
157 if (ngx_modules[i]->init_module) { | 170 if (ngx_modules[i]->init_module) { |
158 if (ngx_modules[i]->init_module(cycle, log) == NGX_ERROR) | 171 if (ngx_modules[i]->init_module(cycle, log) == NGX_ERROR) |
159 { | 172 { |
160 failed = 1; | 173 failed = 1; |
164 } | 177 } |
165 | 178 |
166 if (!failed) { | 179 if (!failed) { |
167 file = cycle->open_files.elts; | 180 file = cycle->open_files.elts; |
168 for (i = 0; i < cycle->open_files.nelts; i++) { | 181 for (i = 0; i < cycle->open_files.nelts; i++) { |
169 if (file->name.data = NULL) { | 182 if (file->name.data == NULL) { |
170 continue; | 183 continue; |
171 } | 184 } |
172 | 185 |
173 file->fd = ngx_open_file(file->name.data, | 186 file->fd = ngx_open_file(file->name.data, |
174 NGX_FILE_RDWR, | 187 NGX_FILE_RDWR, |
207 if (nls[n].fd == -1) { | 220 if (nls[n].fd == -1) { |
208 nls[n].new = 1; | 221 nls[n].new = 1; |
209 } | 222 } |
210 } | 223 } |
211 | 224 |
212 if (ngx_open_listening_sockets(new_cycle) == NGX_ERROR) { | 225 if (ngx_open_listening_sockets(cycle, log) == NGX_ERROR) { |
213 failed = 1; | 226 failed = 1; |
214 } | 227 } |
215 } | 228 } |
216 | 229 |
217 if (failed) { | 230 if (failed) { |
218 | 231 |
219 /* rollback the new cycle configuration */ | 232 /* rollback the new cycle configuration */ |
220 | 233 |
221 for (i = 0; ngx_modules[i]; i++) { | 234 for (i = 0; ngx_modules[i]; i++) { |
222 if (ngx_modules[i]->rollback_module) { | 235 if (ngx_modules[i]->rollback_module) { |
223 ngx_modules[i]->rollback_module(cycle); | 236 ngx_modules[i]->rollback_module(cycle, log); |
224 } | 237 } |
225 } | 238 } |
226 | 239 |
227 file = cycle->open_files.elts; | 240 file = cycle->open_files.elts; |
228 for (i = 0; i < cycle->open_files.nelts; i++) { | 241 for (i = 0; i < cycle->open_files.nelts; i++) { |
229 if (file->fd == NGX_INVALID_FILE) { | 242 if (file->fd == NGX_INVALID_FILE) { |
230 continue; | 243 continue; |
231 } | 244 } |
232 | 245 |
233 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | 246 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { |
234 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | 247 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
235 ngx_close_file_n " \"%s\" failed", | 248 ngx_close_file_n " \"%s\" failed", |
236 file->name.data); | 249 file->name.data); |
237 } | 250 } |
238 } | 251 } |
239 | 252 |
240 ls[i] = cycle->listening.elts; | 253 ls = cycle->listening.elts; |
241 for (i = 0; i < cycle->listening.nelts; i++) { | 254 for (i = 0; i < cycle->listening.nelts; i++) { |
242 if (ls[i].new && ls[i].fd == -1) { | 255 if (ls[i].new && ls[i].fd == -1) { |
243 continue; | 256 continue; |
244 } | 257 } |
245 | 258 |
246 if (ngx_close_socket(ls[i].fd) == -1) | 259 if (ngx_close_socket(ls[i].fd) == -1) { |
247 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | 260 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
248 ngx_close_socket_n " %s failed", | 261 ngx_close_socket_n " %s failed", |
249 ls[i].addr_text.data); | 262 ls[i].addr_text.data); |
250 } | 263 } |
251 } | 264 } |
252 | 265 |
253 ngx_destroy_pool(pool); | 266 ngx_destroy_pool(pool); |
254 return NULL; | 267 return NULL; |
255 | 268 } |
256 } else { | 269 |
257 | 270 /* commit the new cycle configuration */ |
258 /* commit the new cycle configuration */ | 271 |
259 | 272 for (i = 0; ngx_modules[i]; i++) { |
260 for (i = 0; ngx_modules[i]; i++) { | 273 if (ngx_modules[i]->commit_module) { |
261 if (ngx_modules[i]->commit_module) { | 274 ngx_modules[i]->commit_module(cycle, log); |
262 ngx_modules[i]->commit_module(cycle); | |
263 } | |
264 } | 275 } |
265 } | 276 } |
266 | 277 |
267 ls = old_cycle->listening.elts; | 278 ls = old_cycle->listening.elts; |
268 for (i = 0; i < old_cycle->listening.nelts; i++) { | 279 for (i = 0; i < old_cycle->listening.nelts; i++) { |
269 if (ls[i].remain) { | 280 if (ls[i].remain) { |
270 continue; | 281 continue; |
271 } | 282 } |
272 | 283 |
273 if (ngx_close_socket(ls[i].fd) == -1) | 284 if (ngx_close_socket(ls[i].fd) == -1) { |
274 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | 285 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
275 ngx_close_socket_n " %s failed", | 286 ngx_close_socket_n " %s failed", |
276 ls[i].addr_text.data); | 287 ls[i].addr_text.data); |
277 } | 288 } |
278 } | 289 } |
279 | 290 |
280 file = old_cycle->open_files.elts; | 291 file = old_cycle->open_files.elts; |
281 for (i = 0; i < cycle->old_open_files.nelts; i++) { | 292 for (i = 0; i < old_cycle->open_files.nelts; i++) { |
282 if (file->fd == NGX_INVALID_FILE) { | 293 if (file->fd == NGX_INVALID_FILE) { |
283 continue; | 294 continue; |
284 } | 295 } |
285 | 296 |
286 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | 297 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { |
287 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | 298 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
288 ngx_close_file_n " \"%s\" failed", | 299 ngx_close_file_n " \"%s\" failed", |
289 file->name.data); | 300 file->name.data); |
290 } | 301 } |
291 } | 302 } |
292 | 303 |
293 new_cycle->log = ???; | 304 pool->log = cycle->log; |
294 pool->log = ???; | |
295 | 305 |
296 ngx_destroy_pool(old_cycle->pool); | 306 ngx_destroy_pool(old_cycle->pool); |
297 | 307 |
298 return cycle; | 308 return cycle; |
299 } | 309 } |
395 } | 405 } |
396 | 406 |
397 #endif | 407 #endif |
398 | 408 |
399 | 409 |
400 static int ngx_open_listening_sockets(ngx_log_t *log) | 410 static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log) |
401 { | 411 { |
402 int times, failed, reuseaddr, i; | 412 int times, failed, reuseaddr, i; |
403 ngx_err_t err; | 413 ngx_err_t err; |
404 ngx_socket_t s; | 414 ngx_socket_t s; |
405 ngx_listening_t *ls; | 415 ngx_listening_t *ls; |
406 | 416 |
407 reuseaddr = 1; | 417 reuseaddr = 1; |
408 | 418 |
419 /* TODO: times configurable */ | |
420 | |
409 for (times = 10; times; times--) { | 421 for (times = 10; times; times--) { |
410 failed = 0; | 422 failed = 0; |
411 | 423 |
412 /* for each listening socket */ | 424 /* for each listening socket */ |
413 | 425 |
414 ls = ngx_listening_sockets.elts; | 426 ls = cycle->listening.elts; |
415 for (i = 0; i < ngx_listening_sockets.nelts; i++) { | 427 for (i = 0; i < cycle->listening.nelts; i++) { |
416 | 428 |
417 if (ls[i].bound) | 429 if (ls[i].fd != -1) { |
418 continue; | 430 continue; |
431 } | |
419 | 432 |
420 if (ls[i].inherited) { | 433 if (ls[i].inherited) { |
421 | 434 |
422 /* TODO: close on exit */ | 435 /* TODO: close on exit */ |
423 /* TODO: nonblocking */ | 436 /* TODO: nonblocking */ |
424 /* TODO: deferred accept */ | 437 /* TODO: deferred accept */ |
425 | 438 |
426 ls[i].bound = 1; | |
427 continue; | 439 continue; |
428 } | 440 } |
429 | 441 |
430 s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol, | 442 s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol, |
431 ls[i].flags); | 443 ls[i].flags); |
444 | |
432 if (s == -1) { | 445 if (s == -1) { |
433 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | 446 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
434 ngx_socket_n " %s falied", ls[i].addr_text.data); | 447 ngx_socket_n " %s falied", ls[i].addr_text.data); |
435 return NGX_ERROR; | 448 return NGX_ERROR; |
436 } | 449 } |
491 } | 504 } |
492 | 505 |
493 /* TODO: deferred accept */ | 506 /* TODO: deferred accept */ |
494 | 507 |
495 ls[i].fd = s; | 508 ls[i].fd = s; |
496 ls[i].bound = 1; | |
497 } | 509 } |
498 | 510 |
499 if (!failed) | 511 if (!failed) |
500 break; | 512 break; |
513 | |
514 /* TODO: delay configurable */ | |
501 | 515 |
502 ngx_log_error(NGX_LOG_NOTICE, log, 0, | 516 ngx_log_error(NGX_LOG_NOTICE, log, 0, |
503 "try again to bind() after 500ms"); | 517 "try again to bind() after 500ms"); |
504 ngx_msleep(500); | 518 ngx_msleep(500); |
505 } | 519 } |
506 | 520 |
507 if (failed) { | 521 if (failed) { |
508 | 522 ngx_log_error(NGX_LOG_EMERG, log, 0, "still can not bind()"); |
509 /* TODO: configurable */ | |
510 | |
511 ngx_log_error(NGX_LOG_EMERG, log, 0, "can not bind(), exiting"); | |
512 return NGX_ERROR; | 523 return NGX_ERROR; |
513 } | 524 } |
514 | 525 |
515 return NGX_OK; | 526 return NGX_OK; |
516 } | 527 } |