Mercurial > hg > nginx
comparison src/core/nginx.c @ 107:b5be4b0448d3
nginx-0.0.1-2003-07-01-19:00:03 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 01 Jul 2003 15:00:03 +0000 |
parents | 9f9de4deda7e |
children | adc093f880c8 |
comparison
equal
deleted
inserted
replaced
106:9f9de4deda7e | 107:b5be4b0448d3 |
---|---|
19 void *ctx_conf; | 19 void *ctx_conf; |
20 | 20 |
21 int ngx_connection_counter; | 21 int ngx_connection_counter; |
22 | 22 |
23 ngx_array_t ngx_listening_sockets; | 23 ngx_array_t ngx_listening_sockets; |
24 | |
25 | |
26 #if 0 | |
27 | |
28 int main(int argc, char *const *argv) | |
29 { | |
30 int i; | |
31 ngx_str_t conf_file; | |
32 ngx_log_t *log; | |
33 ngx_pool_t *pool; | |
34 ngx_conf_t conf; | |
35 | |
36 ngx_max_sockets = -1; | |
37 | |
38 log = ngx_log_init_errlog(); | |
39 | |
40 if (ngx_os_init(log) == NGX_ERROR) { | |
41 return 1; | |
42 } | |
43 | |
44 ngx_max_module = 0; | |
45 for (i = 0; ngx_modules[i]; i++) { | |
46 ngx_modules[i]->index = ngx_max_module++; | |
47 } | |
48 | |
49 ngx_test_null(pool, ngx_create_pool(16 * 1024, log), 1); | |
50 ngx_test_null(cycle, ngx_pcalloc(pool, sizeof(ngx_cycle_t)), 1); | |
51 cycle->pool = pool; | |
52 | |
53 if (ngx_init_conf(cycle) == NGX_ERROR) { | |
54 return 1; | |
55 } | |
56 | |
57 /* daemon */ | |
58 | |
59 /* life cycle */ | |
60 | |
61 for ( ;; ) { | |
62 | |
63 /* forks */ | |
64 | |
65 ngx_init_temp_number(); | |
66 | |
67 /* threads */ | |
68 | |
69 for ( ;; ) { | |
70 | |
71 worker(cycle->log); | |
72 | |
73 pool = ngx_create_pool(16 * 1024, cycle->log); | |
74 | |
75 if (pool == NULL) { | |
76 continue; | |
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 | |
129 cycle = new_cycle; | |
130 break; | |
131 } | |
132 } | |
133 | |
134 return 0; | |
135 } | |
136 | |
137 | |
138 static int ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log) | |
139 { | |
140 int n; | |
141 ngx_conf_t conf; | |
142 ngx_pool_t *pool; | |
143 ngx_cycle_t *cycle; | |
144 | |
145 | |
146 pool = ngx_create_pool(16 * 1024, log); | |
147 if (pool == NULL) { | |
148 return NULL; | |
149 } | |
150 | |
151 cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)); | |
152 if (cycle == NULL) { | |
153 ngx_destroy_pool(pool); | |
154 return NULL; | |
155 } | |
156 cycle->pool = pool; | |
157 | |
158 n = old_cycle ? old_cycle->open_files.nelts : 20; | |
159 cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t)); | |
160 if (cycle->open_files.elts == NULL) { | |
161 ngx_destroy_pool(pool); | |
162 return NULL; | |
163 } | |
164 cycle->open_files.nelts = 0; | |
165 cycle->open_files.size = sizeof(ngx_open_file_t); | |
166 cycle->open_files.nalloc = n; | |
167 cycle->open_files.pool = pool; | |
168 | |
169 n = old_cycle ? old_cycle->listening.nelts : 10; | |
170 cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t)); | |
171 if (cycle->listening.elts == NULL) { | |
172 ngx_destroy_pool(pool); | |
173 return NULL; | |
174 } | |
175 cycle->listening.nelts = 0; | |
176 cycle->listening.size = sizeof(ngx_listening_t); | |
177 cycle->listening.nalloc = n; | |
178 cycle->listening.pool = pool; | |
179 | |
180 cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *)); | |
181 if (cycle->conf_ctx == NULL) { | |
182 ngx_destroy_pool(pool); | |
183 return NULL; | |
184 } | |
185 | |
186 ngx_memzero(&conf, sizeof(ngx_conf_t)); | |
187 /* STUB: init array ? */ | |
188 conf.args = ngx_create_array(pool, 10, sizeof(ngx_str_t)); | |
189 if (conf.args == NULL) { | |
190 ngx_destroy_pool(pool); | |
191 return NULL; | |
192 } | |
193 | |
194 conf.ctx = cycle->conf_ctx; | |
195 conf.cycle = cycle; | |
196 /* STUB */ conf.pool = cycle->pool; | |
197 conf.log = log; | |
198 conf.module_type = NGX_CORE_MODULE; | |
199 conf.cmd_type = NGX_MAIN_CONF; | |
200 | |
201 conf_file.len = sizeof(NGINX_CONF) - 1; | |
202 conf_file.data = NGINX_CONF; | |
203 | |
204 if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) { | |
205 ngx_destroy_pool(pool); | |
206 return NULL; | |
207 } | |
208 | |
209 for (i = 0; ngx_modules[i]; i++) { | |
210 if (ngx_modules[i]->init_module) { | |
211 if (ngx_modules[i]->init_module(cycle, log) == NGX_ERROR) | |
212 { | |
213 failed = 1; | |
214 break; | |
215 } | |
216 } | |
217 } | |
218 | |
219 if (!failed) { | |
220 file = cycle->open_files.elts; | |
221 for (i = 0; i < cycle->open_files.nelts; i++) { | |
222 if (file->name.data = NULL) { | |
223 continue; | |
224 } | |
225 | |
226 file->fd = ngx_open_file(file->name.data, | |
227 NGX_FILE_RDWR, | |
228 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); | |
229 | |
230 if (file->fd == NGX_INVALID_FILE) { | |
231 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
232 ngx_open_file_n " \"%s\" failed", | |
233 file->name.data); | |
234 failed = 1; | |
235 break; | |
236 } | |
237 | |
238 /* TODO: Win32 append */ | |
239 } | |
240 } | |
241 | |
242 if (!failed) { | |
243 if (ngx_open_listening_sockets(new_cycle) == NGX_ERROR) { | |
244 failed = 1; | |
245 } | |
246 } | |
247 | |
248 if (failed) { | |
249 | |
250 /* rollback the new cycle configuration */ | |
251 | |
252 for (i = 0; ngx_modules[i]; i++) { | |
253 if (ngx_modules[i]->rollback_module) { | |
254 ngx_modules[i]->rollback_module(cycle); | |
255 } | |
256 } | |
257 | |
258 file = cycle->open_files.elts; | |
259 for (i = 0; i < cycle->open_files.nelts; i++) { | |
260 if (file->fd != NGX_INVALID_FILE) { | |
261 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | |
262 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
263 ngx_close_file_n " \"%s\" failed", | |
264 file->name.data); | |
265 } | |
266 } | |
267 } | |
268 | |
269 ls[i] = cycle->listening.elts; | |
270 for (i = 0; i < cycle->listening.nelts; i++) { | |
271 if (ls[i].new && ls[i].fd != -1) { | |
272 if (ngx_close_socket(ls[i].fd) == -1) | |
273 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | |
274 ngx_close_socket_n " %s failed", | |
275 ls[i].addr_text.data); | |
276 } | |
277 } | |
278 } | |
279 | |
280 ngx_destroy_pool(pool); | |
281 return NULL; | |
282 | |
283 } else { | |
284 | |
285 /* commit the new cycle configuration */ | |
286 | |
287 for (i = 0; ngx_modules[i]; i++) { | |
288 if (ngx_modules[i]->commit_module) { | |
289 ngx_modules[i]->commit_module(cycle); | |
290 } | |
291 } | |
292 } | |
293 | |
294 new_cycle->log = ???; | |
295 pool->log = ???; | |
296 | |
297 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 } | |
361 | |
362 | |
363 #endif | |
24 | 364 |
25 | 365 |
26 int main(int argc, char *const *argv) | 366 int main(int argc, char *const *argv) |
27 { | 367 { |
28 int i; | 368 int i; |
42 ngx_max_module = 0; | 382 ngx_max_module = 0; |
43 for (i = 0; ngx_modules[i]; i++) { | 383 for (i = 0; ngx_modules[i]; i++) { |
44 ngx_modules[i]->index = ngx_max_module++; | 384 ngx_modules[i]->index = ngx_max_module++; |
45 } | 385 } |
46 | 386 |
47 #if 0 | |
48 | |
49 ngx_test_null(cycle->pool, ngx_create_pool(16 * 1024, log), 1); | |
50 | |
51 if (ngx_init_conf(cycle) == NGX_ERROR) { | |
52 ngx_destroy_pool(cycle->pool); | |
53 return 1; | |
54 } | |
55 | |
56 /* daemon */ | |
57 | |
58 /* life cycle */ | |
59 | |
60 { | |
61 | |
62 /* forks */ | |
63 | |
64 /* threads */ | |
65 | |
66 for ( ;; ) { | |
67 worker; | |
68 | |
69 new_cycle = ngx_calloc(sizeof(ngx_cycle_t), cycle->log); | |
70 | |
71 if (new_cycle == NULL) { | |
72 continue; | |
73 } | |
74 | |
75 new_cycle->pool = ngx_create_pool(16 * 1024, cycle->log); | |
76 | |
77 if (new_cycle->pool == NULL) { | |
78 ngx_free(new_cycle); | |
79 continue; | |
80 } | |
81 | |
82 if (ngx_init_conf(new_cycle) == NGX_ERROR) { | |
83 ngx_destroy_pool(new_cycle->pool); | |
84 ngx_free(new_cycle); | |
85 continue; | |
86 } | |
87 | |
88 /* update bound listening */ | |
89 | |
90 ngx_destroy_pool(cycle->pool); | |
91 ngx_free(cycle); | |
92 | |
93 cycle = new_cycle; | |
94 break; | |
95 } | |
96 } | |
97 | |
98 return 0; | |
99 | |
100 #endif | |
101 | |
102 /* life cycle */ | 387 /* life cycle */ |
103 | 388 |
104 { | 389 { |
105 old_pool = pool; | 390 old_pool = pool; |
106 | 391 |
167 } | 452 } |
168 | 453 |
169 return 0; | 454 return 0; |
170 } | 455 } |
171 | 456 |
172 #if 0 | |
173 | |
174 static int ngx_init_conf(ngx_cycle_t *cycle) | |
175 { | |
176 ngx_conf_t conf; | |
177 | |
178 ngx_init_array(cycle->listening, cycle->pool, 10, sizeof(ngx_listening_t), | |
179 NGX_ERROR); | |
180 | |
181 ngx_memzero(&conf, sizeof(ngx_conf_t)); | |
182 | |
183 ngx_test_null(conf.args, | |
184 ngx_create_array(cycle->pool, 10, sizeof(ngx_str_t)), | |
185 NGX_ERROR); | |
186 | |
187 ngx_test_null(ngx_conf_ctx, | |
188 ngx_pcalloc(cycle->pool, ngx_max_module * sizeof(void *)), | |
189 NGX_ERROR); | |
190 | |
191 conf.ctx = ngx_conf_ctx; | |
192 conf.pool = cycle->pool; | |
193 conf.log = cycle->log; | |
194 conf.module_type = NGX_CORE_MODULE; | |
195 conf.cmd_type = NGX_MAIN_CONF; | |
196 | |
197 conf_file.len = sizeof(NGINX_CONF) - 1; | |
198 conf_file.data = NGINX_CONF; | |
199 | |
200 if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) { | |
201 return NGX_ERROR; | |
202 } | |
203 | |
204 return NGX_OK; | |
205 } | |
206 | |
207 | |
208 #endif | |
209 | 457 |
210 static int ngx_open_listening_sockets(ngx_log_t *log) | 458 static int ngx_open_listening_sockets(ngx_log_t *log) |
211 { | 459 { |
212 int times, failed, reuseaddr, i; | 460 int times, failed, reuseaddr, i; |
213 ngx_err_t err; | 461 ngx_err_t err; |