comparison src/core/nginx.c @ 114:ac69ab96328d

nginx-0.0.1-2003-07-07-10:11:50 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 07 Jul 2003 06:11:50 +0000
parents d7f606e25b99
children be27f922b9a2
comparison
equal deleted inserted replaced
113:d7f606e25b99 114:ac69ab96328d
5 #include <nginx.h> 5 #include <nginx.h>
6 6
7 7
8 static ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log); 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_cycle_t *cycle, ngx_log_t *log); 9 static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log);
10 10 static void ngx_clean_old_cycles(ngx_event_t *ev);
11 11
12 void ****ngx_conf_ctx; 12 #if (NGX_DEBUG) && (__FreeBSD__)
13 13 extern char *malloc_options;
14 14 #endif
15 ngx_os_io_t ngx_io; 15
16 16 int ngx_max_module;
17 17 ngx_os_io_t ngx_io;
18 ngx_cycle_t ngx_cycle; 18
19 19 ngx_cycle_t *ngx_cycle;
20 int ngx_max_module; 20 ngx_pool_t *ngx_temp_pool;
21 ngx_array_t ngx_old_cycles;
22 ngx_event_t ngx_cleaner_event;
23
24 /* STUB NAME */
25 ngx_connection_t dumb;
21 26
22 int ngx_connection_counter; 27 int ngx_connection_counter;
23 28
24 29
25 int restart; 30 int restart;
30 { 35 {
31 int i; 36 int i;
32 ngx_log_t *log; 37 ngx_log_t *log;
33 ngx_cycle_t *cycle; 38 ngx_cycle_t *cycle;
34 39
40 #if (NGX_DEBUG) && (__FreeBSD__)
41 malloc_options = "J";
42 #endif
43
35 /* TODO */ ngx_max_sockets = -1; 44 /* TODO */ ngx_max_sockets = -1;
36 45
37 log = ngx_log_init_errlog(); 46 log = ngx_log_init_errlog();
38 47
39 if (ngx_os_init(log) == NGX_ERROR) { 48 if (ngx_os_init(log) == NGX_ERROR) {
48 cycle = ngx_init_cycle(NULL, log); 57 cycle = ngx_init_cycle(NULL, log);
49 if (cycle == NULL) { 58 if (cycle == NULL) {
50 return 1; 59 return 1;
51 } 60 }
52 61
53 ngx_cycle = *cycle; 62 ngx_cycle = cycle;
54 63
55 /* daemon */ 64 /* daemon */
56 65
57 /* life cycle */ 66 /* life cycle */
58 67
59 for ( ;; ) { 68 for ( ;; ) {
60 /* STUB */ ngx_cycle.log->log_level = NGX_LOG_DEBUG; 69 /* STUB */ ngx_cycle->log->log_level = NGX_LOG_DEBUG;
61 70
62 /* forks */ 71 /* forks */
63 72
64 ngx_init_temp_number(); 73 ngx_init_temp_number();
65 74
66 for (i = 0; ngx_modules[i]; i++) { 75 for (i = 0; ngx_modules[i]; i++) {
67 if (ngx_modules[i]->init_child) { 76 if (ngx_modules[i]->init_child) {
68 if (ngx_modules[i]->init_child(&ngx_cycle) == NGX_ERROR) { 77 if (ngx_modules[i]->init_child(ngx_cycle) == NGX_ERROR) {
69 /* fatal */ 78 /* fatal */
70 exit(1); 79 exit(1);
71 } 80 }
72 } 81 }
73 } 82 }
78 rotate = 0; 87 rotate = 0;
79 88
80 for ( ;; ) { 89 for ( ;; ) {
81 90
82 for ( ;; ) { 91 for ( ;; ) {
83 ngx_log_debug(ngx_cycle.log, "worker cycle"); 92 ngx_log_debug(ngx_cycle->log, "worker cycle");
84 93
85 ngx_process_events(ngx_cycle.log); 94 ngx_process_events(ngx_cycle->log);
86 95
87 if (rotate) { 96 if (rotate) {
88 ngx_log_debug(ngx_cycle.log, "rotate"); 97 ngx_log_debug(ngx_cycle->log, "rotate");
89 } 98 }
90 99
91 if (restart) { 100 if (restart) {
92 ngx_log_debug(ngx_cycle.log, "restart"); 101 ngx_log_debug(ngx_cycle->log, "restart");
93 break; 102 break;
94 } 103 }
95 104
96 } 105 }
97 106
98 cycle = ngx_init_cycle(&ngx_cycle, ngx_cycle.log); 107 cycle = ngx_init_cycle(ngx_cycle, ngx_cycle->log);
99 if (cycle == NULL) { 108 if (cycle == NULL) {
100 continue; 109 continue;
101 } 110 }
102 111
103 ngx_log_debug(ngx_cycle.log, "OPEN: %d" _ cycle->log->file->fd); 112 ngx_cycle = cycle;
104 ngx_cycle = *cycle;
105 ngx_log_debug(ngx_cycle.log, "OPEN: %d" _ ngx_cycle.log->file->fd);
106 break; 113 break;
107 } 114 }
108 } 115 }
109 116
110 return 0; 117 return 0;
115 { 122 {
116 int i, n, failed; 123 int i, n, failed;
117 ngx_str_t conf_file; 124 ngx_str_t conf_file;
118 ngx_conf_t conf; 125 ngx_conf_t conf;
119 ngx_pool_t *pool; 126 ngx_pool_t *pool;
120 ngx_cycle_t *cycle; 127 ngx_cycle_t *cycle, **old;
121 ngx_open_file_t *file; 128 ngx_open_file_t *file;
122 ngx_listening_t *ls, *nls; 129 ngx_listening_t *ls, *nls;
123 130
124 131
125 pool = ngx_create_pool(16 * 1024, log); 132 pool = ngx_create_pool(16 * 1024, log);
131 if (cycle == NULL) { 138 if (cycle == NULL) {
132 ngx_destroy_pool(pool); 139 ngx_destroy_pool(pool);
133 return NULL; 140 return NULL;
134 } 141 }
135 cycle->pool = pool; 142 cycle->pool = pool;
143
144 cycle->old_cycle = old_cycle;
136 145
137 n = old_cycle ? old_cycle->open_files.nelts : 20; 146 n = old_cycle ? old_cycle->open_files.nelts : 20;
138 cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t)); 147 cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t));
139 if (cycle->open_files.elts == NULL) { 148 if (cycle->open_files.elts == NULL) {
140 ngx_destroy_pool(pool); 149 ngx_destroy_pool(pool);
176 return NULL; 185 return NULL;
177 } 186 }
178 187
179 conf.ctx = cycle->conf_ctx; 188 conf.ctx = cycle->conf_ctx;
180 conf.cycle = cycle; 189 conf.cycle = cycle;
181 /* STUB */ conf.pool = cycle->pool; ngx_conf_ctx = cycle->conf_ctx; 190 /* STUB */ conf.pool = cycle->pool;
182 conf.log = log; 191 conf.log = log;
183 conf.module_type = NGX_CORE_MODULE; 192 conf.module_type = NGX_CORE_MODULE;
184 conf.cmd_type = NGX_MAIN_CONF; 193 conf.cmd_type = NGX_MAIN_CONF;
185 194
186 conf_file.len = sizeof(NGINX_CONF) - 1; 195 conf_file.len = sizeof(NGINX_CONF) - 1;
190 ngx_destroy_pool(pool); 199 ngx_destroy_pool(pool);
191 return NULL; 200 return NULL;
192 } 201 }
193 202
194 failed = 0; 203 failed = 0;
195
196 ngx_log_debug(log, "OPEN: %d" _ cycle->log->file->fd);
197 204
198 file = cycle->open_files.elts; 205 file = cycle->open_files.elts;
199 for (i = 0; i < cycle->open_files.nelts; i++) { 206 for (i = 0; i < cycle->open_files.nelts; i++) {
200 if (file->name.data == NULL) { 207 if (file->name.data == NULL) {
201 continue; 208 continue;
202 } 209 }
203 210
204 file->fd = ngx_open_file(file->name.data, 211 file->fd = ngx_open_file(file->name.data,
205 NGX_FILE_RDWR, 212 NGX_FILE_RDWR,
206 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); 213 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);
214
215 ngx_log_debug(log, "OPEN: %d:%s" _ file->fd _ file->name.data);
207 216
208 if (file->fd == NGX_INVALID_FILE) { 217 if (file->fd == NGX_INVALID_FILE) {
209 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, 218 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
210 ngx_open_file_n " \"%s\" failed", 219 ngx_open_file_n " \"%s\" failed",
211 file->name.data); 220 file->name.data);
214 } 223 }
215 224
216 /* TODO: Win32 append */ 225 /* TODO: Win32 append */
217 } 226 }
218 227
219 ngx_log_debug(log, "OPEN: %d" _ cycle->log->file->fd); 228 /* STUB */ cycle->log->log_level = NGX_LOG_DEBUG;
220 /* STUB */ cycle->log->log_level = NGX_LOG_DEBUG;
221 ngx_log_debug(cycle->log, "TEST");
222 229
223 if (!failed) { 230 if (!failed) {
224 if (old_cycle) { 231 if (old_cycle) {
225 ls = old_cycle->listening.elts; 232 ls = old_cycle->listening.elts;
226 for (i = 0; i < old_cycle->listening.nelts; i++) { 233 for (i = 0; i < old_cycle->listening.nelts; i++) {
232 for (i = 0; i < old_cycle->listening.nelts; i++) { 239 for (i = 0; i < old_cycle->listening.nelts; i++) {
233 if (ngx_memcmp(nls[n].sockaddr, 240 if (ngx_memcmp(nls[n].sockaddr,
234 ls[i].sockaddr, ls[i].socklen) == 0) 241 ls[i].sockaddr, ls[i].socklen) == 0)
235 { 242 {
236 nls[n].fd = ls[i].fd; 243 nls[n].fd = ls[i].fd;
244 nls[i].remain = 1;
237 ls[i].remain = 1; 245 ls[i].remain = 1;
238 break; 246 break;
239 } 247 }
240 } 248 }
241 249
242 if (nls[n].fd == -1) { 250 if (nls[n].fd == -1) {
243 nls[n].new = 1; 251 nls[n].new = 1;
244 } 252 }
245 } 253 }
254
255 } else {
256 ls = cycle->listening.elts;
257 for (i = 0; i < cycle->listening.nelts; i++) {
258 ls[i].new = 1;
259 }
246 } 260 }
247 261
248 if (ngx_open_listening_sockets(cycle, log) == NGX_ERROR) { 262 if (ngx_open_listening_sockets(cycle, log) == NGX_ERROR) {
249 failed = 1; 263 failed = 1;
250 } 264 }
285 } 299 }
286 300
287 /* commit the new cycle configuration */ 301 /* commit the new cycle configuration */
288 302
289 pool->log = cycle->log; 303 pool->log = cycle->log;
304
305 #if 1
306 /* STUB */ cycle->one_process = 1;
307 #endif
290 308
291 for (i = 0; ngx_modules[i]; i++) { 309 for (i = 0; ngx_modules[i]; i++) {
292 if (ngx_modules[i]->init_module) { 310 if (ngx_modules[i]->init_module) {
293 if (ngx_modules[i]->init_module(cycle) == NGX_ERROR) { 311 if (ngx_modules[i]->init_module(cycle) == NGX_ERROR) {
294 /* fatal */ 312 /* fatal */
325 ngx_close_file_n " \"%s\" failed", 343 ngx_close_file_n " \"%s\" failed",
326 file->name.data); 344 file->name.data);
327 } 345 }
328 } 346 }
329 347
330 ngx_destroy_pool(old_cycle->pool); 348
349 if (!old_cycle->one_process) {
350 ngx_destroy_pool(old_cycle->pool);
351 return cycle;
352 }
353
354 if (ngx_temp_pool == NULL) {
355 ngx_temp_pool = ngx_create_pool(128, cycle->log);
356 if (ngx_temp_pool == NULL) {
357 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
358 "can not create ngx_temp_pool");
359 exit(1);
360 }
361
362 n = 10;
363 ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool,
364 n * sizeof(ngx_cycle_t *));
365 if (ngx_old_cycles.elts == NULL) {
366 exit(1);
367 }
368 ngx_old_cycles.nelts = 0;
369 ngx_old_cycles.size = sizeof(ngx_cycle_t *);
370 ngx_old_cycles.nalloc = n;
371 ngx_old_cycles.pool = ngx_temp_pool;
372
373 ngx_cleaner_event.event_handler = ngx_clean_old_cycles;
374 ngx_cleaner_event.log = cycle->log;
375 ngx_cleaner_event.data = &dumb;
376 dumb.fd = -1;
377 }
378
379 ngx_temp_pool->log = cycle->log;
380
381 old = ngx_push_array(&ngx_old_cycles);
382 if (old == NULL) {
383 exit(1);
384 }
385 *old = old_cycle;
386
387 if (!ngx_cleaner_event.timer_set) {
388 ngx_add_timer(&ngx_cleaner_event, 30000);
389 ngx_cleaner_event.timer_set = 1;
390 }
331 391
332 return cycle; 392 return cycle;
333 } 393 }
334
335
336
337 #if 0
338
339
340 int main(int argc, char *const *argv)
341 {
342 int i;
343 ngx_str_t conf_file;
344 ngx_log_t *log;
345 ngx_pool_t *pool, *old_pool;
346 ngx_conf_t conf;
347
348 ngx_max_sockets = -1;
349
350 log = ngx_log_init_errlog();
351
352 if (ngx_os_init(log) == NGX_ERROR) {
353 return 1;
354 }
355
356 ngx_max_module = 0;
357 for (i = 0; ngx_modules[i]; i++) {
358 ngx_modules[i]->index = ngx_max_module++;
359 }
360
361 /* life cycle */
362
363 {
364 old_pool = pool;
365
366 pool = ngx_create_pool(16 * 1024, log);
367
368 ngx_init_array(ngx_listening_sockets,
369 pool, 10, sizeof(ngx_listening_t),
370 1);
371
372 ngx_memzero(&conf, sizeof(ngx_conf_t));
373
374 ngx_test_null(conf.args,
375 ngx_create_array(pool, 10, sizeof(ngx_str_t)),
376 1);
377
378 ngx_test_null(ngx_conf_ctx,
379 ngx_pcalloc(pool, ngx_max_module * sizeof(void *)),
380 1);
381
382 conf.ctx = ngx_conf_ctx;
383 conf.pool = pool;
384 conf.log = log;
385 conf.module_type = NGX_CORE_MODULE;
386 conf.cmd_type = NGX_MAIN_CONF;
387
388 conf_file.len = sizeof(NGINX_CONF) - 1;
389 conf_file.data = NGINX_CONF;
390
391 if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) {
392 return 1;
393 }
394
395 #if 0
396 log = (ngx_log_t *) ngx_get_conf(ngx_errlog_module);
397 /* STUB */ log->log_level = NGX_LOG_DEBUG;
398 #endif
399
400 ngx_init_temp_number();
401
402 ngx_io = ngx_os_io;
403
404 for (i = 0; ngx_modules[i]; i++) {
405 if (ngx_modules[i]->init_module) {
406 if (ngx_modules[i]->init_module(pool) == NGX_ERROR) {
407 return 1;
408 }
409 }
410 }
411
412 if (ngx_open_listening_sockets(log) == NGX_ERROR) {
413 return 1;
414 }
415
416 /* TODO: daemon, once only */
417
418 /* TODO: fork */
419
420 ngx_pre_thread(&ngx_listening_sockets, pool, log);
421
422 /* TODO: threads */
423
424 /* STUB */
425 ngx_worker(log);
426 }
427
428 return 0;
429 }
430
431 #endif
432 394
433 395
434 static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log) 396 static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log)
435 { 397 {
436 int times, failed, reuseaddr, i; 398 int times, failed, reuseaddr, i;
547 return NGX_ERROR; 509 return NGX_ERROR;
548 } 510 }
549 511
550 return NGX_OK; 512 return NGX_OK;
551 } 513 }
514
515
516 static void ngx_clean_old_cycles(ngx_event_t *ev)
517 {
518 int i, n, found, live;
519 ngx_cycle_t **cycle;
520
521 ngx_temp_pool->log = ngx_cycle->log;
522
523 ngx_log_debug(ngx_cycle->log, "clean old cycles");
524
525 live = 0;
526
527 cycle = ngx_old_cycles.elts;
528 for (i = 0; i < ngx_old_cycles.nelts; i++) {
529
530 if (cycle[i] == NULL) {
531 continue;
532 }
533
534 found = 0;
535
536 for (n = 0; n < cycle[i]->connection_n; n++) {
537 if (cycle[i]->connections[n].fd != -1) {
538 found = 1;
539 ngx_log_debug(ngx_cycle->log, "live fd: %d" _ n);
540 break;
541 }
542 }
543
544 if (found) {
545 live = 1;
546 continue;
547 }
548
549 ngx_log_debug(ngx_cycle->log, "clean old cycle: %d" _ i);
550 ngx_destroy_pool(cycle[i]->pool);
551 cycle[i] = NULL;
552 }
553
554 ngx_log_debug(ngx_cycle->log, "old cycles status: %d" _ live);
555
556 if (live) {
557 ngx_log_debug(ngx_cycle->log, "TIMER");
558 ngx_add_timer(ev, 30000);
559
560 } else {
561 ngx_cleaner_event.timer_set = 0;
562 ngx_destroy_pool(ngx_temp_pool);
563 ngx_temp_pool = NULL;
564 ngx_old_cycles.nelts = 0;
565 }
566 }