Mercurial > hg > nginx
comparison src/core/nginx.c @ 108:adc093f880c8
nginx-0.0.1-2003-07-02-09:01:53 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 02 Jul 2003 05:01:53 +0000 |
parents | b5be4b0448d3 |
children | a9bc21d63fe4 |
comparison
equal
deleted
inserted
replaced
107:b5be4b0448d3 | 108:adc093f880c8 |
---|---|
25 | 25 |
26 #if 0 | 26 #if 0 |
27 | 27 |
28 int main(int argc, char *const *argv) | 28 int main(int argc, char *const *argv) |
29 { | 29 { |
30 int i; | 30 ngx_str_t conf_file; |
31 ngx_str_t conf_file; | 31 ngx_log_t *log; |
32 ngx_log_t *log; | 32 ngx_conf_t conf; |
33 ngx_pool_t *pool; | 33 ngx_cycle_t *cycle; |
34 ngx_conf_t conf; | 34 |
35 | 35 /* TODO */ ngx_max_sockets = -1; |
36 ngx_max_sockets = -1; | |
37 | 36 |
38 log = ngx_log_init_errlog(); | 37 log = ngx_log_init_errlog(); |
39 | 38 |
40 if (ngx_os_init(log) == NGX_ERROR) { | 39 if (ngx_os_init(log) == NGX_ERROR) { |
41 return 1; | 40 return 1; |
44 ngx_max_module = 0; | 43 ngx_max_module = 0; |
45 for (i = 0; ngx_modules[i]; i++) { | 44 for (i = 0; ngx_modules[i]; i++) { |
46 ngx_modules[i]->index = ngx_max_module++; | 45 ngx_modules[i]->index = ngx_max_module++; |
47 } | 46 } |
48 | 47 |
49 ngx_test_null(pool, ngx_create_pool(16 * 1024, log), 1); | 48 cycle = ngx_init_cycle(NULL, log); |
50 ngx_test_null(cycle, ngx_pcalloc(pool, sizeof(ngx_cycle_t)), 1); | 49 if (cycle == NULL) { |
51 cycle->pool = pool; | |
52 | |
53 if (ngx_init_conf(cycle) == NGX_ERROR) { | |
54 return 1; | 50 return 1; |
55 } | 51 } |
56 | 52 |
57 /* daemon */ | 53 /* daemon */ |
58 | 54 |
68 | 64 |
69 for ( ;; ) { | 65 for ( ;; ) { |
70 | 66 |
71 worker(cycle->log); | 67 worker(cycle->log); |
72 | 68 |
73 pool = ngx_create_pool(16 * 1024, cycle->log); | 69 new_cycle = ngx_init_cycle(cycle, cycle->log); |
74 | 70 if (new_cycle) == NULL) { |
75 if (pool == NULL) { | 71 continue; |
76 continue; | 72 } |
77 } | |
78 | |
79 new_cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)); | |
80 | |
81 if (new_cycle == NULL) { | |
82 ngx_destroy_pool(pool); | |
83 continue; | |
84 } | |
85 | |
86 new_cycle->pool = pool; | |
87 | |
88 if (ngx_init_conf(new_cycle, cycle->log) == NGX_ERROR) { | |
89 ngx_destroy_pool(new_cycle->pool); | |
90 continue; | |
91 } | |
92 | |
93 nls = new_cycle->listening.elts; | |
94 for (n = 0; n < new_cycle->listening.nelts; n++) { | |
95 ls = cycle->listening.elts; | |
96 for (i = 0; i < cycle->listening.nelts; i++) { | |
97 if (ngx_memcmp(nls[n].sockaddr, | |
98 ls[i].sockaddr, ls[i].socklen) == 0) | |
99 { | |
100 nls[n].fd = ls[i].fd; | |
101 break; | |
102 } | |
103 } | |
104 | |
105 if (nls[n].fd == -1) { | |
106 nls[n].new = 1; | |
107 } | |
108 } | |
109 | |
110 if (ngx_open_listening_sockets(new_cycle) == NGX_ERROR) { | |
111 for (n = 0; n < new_cycle->listening.nelts; n++) { | |
112 if (nls[n].new && nls[n].fd != -1) { | |
113 if (ngx_close_socket(nls[n].fd) == -1) | |
114 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | |
115 ngx_close_socket_n " %s failed", | |
116 nls[n].addr_text.data); | |
117 } | |
118 } | |
119 } | |
120 | |
121 ngx_destroy_pool(new_cycle->pool); | |
122 continue; | |
123 } | |
124 | |
125 new_cycle->log = new log; | |
126 | |
127 ngx_destroy_pool(cycle->pool); | |
128 | 73 |
129 cycle = new_cycle; | 74 cycle = new_cycle; |
130 break; | 75 break; |
131 } | 76 } |
132 } | 77 } |
135 } | 80 } |
136 | 81 |
137 | 82 |
138 static int ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log) | 83 static int ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log) |
139 { | 84 { |
140 int n; | 85 int i, n; |
141 ngx_conf_t conf; | 86 ngx_conf_t conf; |
142 ngx_pool_t *pool; | 87 ngx_pool_t *pool; |
143 ngx_cycle_t *cycle; | 88 ngx_cycle_t *cycle; |
89 ngx_open_file_t *file; | |
90 ngx_listening_t *ls, *nls; | |
144 | 91 |
145 | 92 |
146 pool = ngx_create_pool(16 * 1024, log); | 93 pool = ngx_create_pool(16 * 1024, log); |
147 if (pool == NULL) { | 94 if (pool == NULL) { |
148 return NULL; | 95 return NULL; |
238 /* TODO: Win32 append */ | 185 /* TODO: Win32 append */ |
239 } | 186 } |
240 } | 187 } |
241 | 188 |
242 if (!failed) { | 189 if (!failed) { |
190 ls = old_cycle->listening.elts; | |
191 for (i = 0; i < old_cycle->listening.nelts; i++) { | |
192 ls[i].remain = 0; | |
193 } | |
194 | |
195 nls = cycle->listening.elts; | |
196 for (n = 0; n < cycle->listening.nelts; n++) { | |
197 for (i = 0; i < old_cycle->listening.nelts; i++) { | |
198 if (ngx_memcmp(nls[n].sockaddr, | |
199 ls[i].sockaddr, ls[i].socklen) == 0) | |
200 { | |
201 nls[n].fd = ls[i].fd; | |
202 ls[i].remain = 1; | |
203 break; | |
204 } | |
205 } | |
206 | |
207 if (nls[n].fd == -1) { | |
208 nls[n].new = 1; | |
209 } | |
210 } | |
211 | |
243 if (ngx_open_listening_sockets(new_cycle) == NGX_ERROR) { | 212 if (ngx_open_listening_sockets(new_cycle) == NGX_ERROR) { |
244 failed = 1; | 213 failed = 1; |
245 } | 214 } |
246 } | 215 } |
247 | 216 |
255 } | 224 } |
256 } | 225 } |
257 | 226 |
258 file = cycle->open_files.elts; | 227 file = cycle->open_files.elts; |
259 for (i = 0; i < cycle->open_files.nelts; i++) { | 228 for (i = 0; i < cycle->open_files.nelts; i++) { |
260 if (file->fd != NGX_INVALID_FILE) { | 229 if (file->fd == NGX_INVALID_FILE) { |
261 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | 230 continue; |
262 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | 231 } |
263 ngx_close_file_n " \"%s\" failed", | 232 |
264 file->name.data); | 233 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { |
265 } | 234 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
235 ngx_close_file_n " \"%s\" failed", | |
236 file->name.data); | |
266 } | 237 } |
267 } | 238 } |
268 | 239 |
269 ls[i] = cycle->listening.elts; | 240 ls[i] = cycle->listening.elts; |
270 for (i = 0; i < cycle->listening.nelts; i++) { | 241 for (i = 0; i < cycle->listening.nelts; i++) { |
271 if (ls[i].new && ls[i].fd != -1) { | 242 if (ls[i].new && ls[i].fd == -1) { |
272 if (ngx_close_socket(ls[i].fd) == -1) | 243 continue; |
273 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | 244 } |
274 ngx_close_socket_n " %s failed", | 245 |
275 ls[i].addr_text.data); | 246 if (ngx_close_socket(ls[i].fd) == -1) |
276 } | 247 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
248 ngx_close_socket_n " %s failed", | |
249 ls[i].addr_text.data); | |
277 } | 250 } |
278 } | 251 } |
279 | 252 |
280 ngx_destroy_pool(pool); | 253 ngx_destroy_pool(pool); |
281 return NULL; | 254 return NULL; |
289 ngx_modules[i]->commit_module(cycle); | 262 ngx_modules[i]->commit_module(cycle); |
290 } | 263 } |
291 } | 264 } |
292 } | 265 } |
293 | 266 |
267 ls = old_cycle->listening.elts; | |
268 for (i = 0; i < old_cycle->listening.nelts; i++) { | |
269 if (ls[i].remain) { | |
270 continue; | |
271 } | |
272 | |
273 if (ngx_close_socket(ls[i].fd) == -1) | |
274 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | |
275 ngx_close_socket_n " %s failed", | |
276 ls[i].addr_text.data); | |
277 } | |
278 } | |
279 | |
280 file = old_cycle->open_files.elts; | |
281 for (i = 0; i < cycle->old_open_files.nelts; i++) { | |
282 if (file->fd == NGX_INVALID_FILE) { | |
283 continue; | |
284 } | |
285 | |
286 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | |
287 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
288 ngx_close_file_n " \"%s\" failed", | |
289 file->name.data); | |
290 } | |
291 } | |
292 | |
294 new_cycle->log = ???; | 293 new_cycle->log = ???; |
295 pool->log = ???; | 294 pool->log = ???; |
296 | 295 |
296 ngx_destroy_pool(old_cycle->pool); | |
297 | |
297 return cycle; | 298 return cycle; |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | |
304 ---------------- | |
305 | |
306 ngx_init_array(cycle->listening, cycle->pool, 10, sizeof(ngx_listening_t), | |
307 NGX_ERROR); | |
308 | |
309 ngx_memzero(&conf, sizeof(ngx_conf_t)); | |
310 | |
311 ngx_test_null(conf.args, | |
312 ngx_create_array(cycle->pool, 10, sizeof(ngx_str_t)), | |
313 NGX_ERROR); | |
314 | |
315 ngx_test_null(cycle->conf_ctx, | |
316 ngx_pcalloc(cycle->pool, ngx_max_module * sizeof(void *)), | |
317 NGX_ERROR); | |
318 | |
319 conf.ctx = cycle->conf_ctx; | |
320 conf.cycle = cycle; | |
321 /* STUB */ conf.pool = cycle->pool; conf.log = cycle->log; | |
322 conf.module_type = NGX_CORE_MODULE; | |
323 conf.cmd_type = NGX_MAIN_CONF; | |
324 | |
325 conf_file.len = sizeof(NGINX_CONF) - 1; | |
326 conf_file.data = NGINX_CONF; | |
327 | |
328 if (ngx_conf_parse(&conf, &conf_file) == NGX_CONF_OK) { | |
329 for (i = 0; ngx_modules[i]; i++) { | |
330 if (ngx_modules[i]->init_module) { | |
331 if (ngx_modules[i]->init_module(pool) == NGX_ERROR) { | |
332 failed = 1; | |
333 break; | |
334 } | |
335 } | |
336 } | |
337 | |
338 } else { | |
339 failed = 1; | |
340 } | |
341 | |
342 if (failed) { | |
343 for (i = 0; ngx_modules[i]; i++) { | |
344 if (ngx_modules[i]->rollback_module) { | |
345 ngx_modules[i]->rollback_module(pool); | |
346 } | |
347 } | |
348 | |
349 return NGX_ERROR; | |
350 | |
351 } else { | |
352 for (i = 0; ngx_modules[i]; i++) { | |
353 if (ngx_modules[i]->commit_module) { | |
354 ngx_modules[i]->commit_module(pool); | |
355 } | |
356 } | |
357 } | |
358 | |
359 return NGX_OK; | |
360 } | 299 } |
361 | 300 |
362 | 301 |
363 #endif | 302 |
303 #else | |
364 | 304 |
365 | 305 |
366 int main(int argc, char *const *argv) | 306 int main(int argc, char *const *argv) |
367 { | 307 { |
368 int i; | 308 int i; |
452 } | 392 } |
453 | 393 |
454 return 0; | 394 return 0; |
455 } | 395 } |
456 | 396 |
397 #endif | |
398 | |
457 | 399 |
458 static int ngx_open_listening_sockets(ngx_log_t *log) | 400 static int ngx_open_listening_sockets(ngx_log_t *log) |
459 { | 401 { |
460 int times, failed, reuseaddr, i; | 402 int times, failed, reuseaddr, i; |
461 ngx_err_t err; | 403 ngx_err_t err; |