Mercurial > hg > nginx-vendor-1-0
comparison src/core/nginx.c @ 0:f0b350454894 NGINX_0_1_0
nginx 0.1.0
*) The first public version.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Mon, 04 Oct 2004 00:00:00 +0400 |
parents | |
children | cc9f381affaa |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0b350454894 |
---|---|
1 | |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_event.h> | |
10 #include <nginx.h> | |
11 | |
12 | |
13 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); | |
14 static ngx_int_t ngx_getopt(ngx_master_ctx_t *ctx, ngx_cycle_t *cycle); | |
15 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle); | |
16 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf); | |
17 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
18 | |
19 | |
20 static ngx_command_t ngx_core_commands[] = { | |
21 | |
22 { ngx_string("daemon"), | |
23 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, | |
24 ngx_conf_set_flag_slot, | |
25 0, | |
26 offsetof(ngx_core_conf_t, daemon), | |
27 NULL }, | |
28 | |
29 { ngx_string("master_process"), | |
30 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, | |
31 ngx_conf_set_flag_slot, | |
32 0, | |
33 offsetof(ngx_core_conf_t, master), | |
34 NULL }, | |
35 | |
36 { ngx_string("worker_processes"), | |
37 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, | |
38 ngx_conf_set_num_slot, | |
39 0, | |
40 offsetof(ngx_core_conf_t, worker_processes), | |
41 NULL }, | |
42 | |
43 #if (NGX_THREADS) | |
44 | |
45 { ngx_string("worker_threads"), | |
46 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, | |
47 ngx_conf_set_num_slot, | |
48 0, | |
49 offsetof(ngx_core_conf_t, worker_threads), | |
50 NULL }, | |
51 | |
52 { ngx_string("thread_stack_size"), | |
53 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, | |
54 ngx_conf_set_size_slot, | |
55 0, | |
56 offsetof(ngx_core_conf_t, thread_stack_size), | |
57 NULL }, | |
58 | |
59 #endif | |
60 | |
61 { ngx_string("user"), | |
62 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12, | |
63 ngx_set_user, | |
64 0, | |
65 0, | |
66 NULL }, | |
67 | |
68 { ngx_string("pid"), | |
69 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, | |
70 ngx_conf_set_str_slot, | |
71 0, | |
72 offsetof(ngx_core_conf_t, pid), | |
73 NULL }, | |
74 | |
75 ngx_null_command | |
76 }; | |
77 | |
78 | |
79 static ngx_core_module_t ngx_core_module_ctx = { | |
80 ngx_string("core"), | |
81 ngx_core_module_create_conf, | |
82 ngx_core_module_init_conf | |
83 }; | |
84 | |
85 | |
86 ngx_module_t ngx_core_module = { | |
87 NGX_MODULE, | |
88 &ngx_core_module_ctx, /* module context */ | |
89 ngx_core_commands, /* module directives */ | |
90 NGX_CORE_MODULE, /* module type */ | |
91 NULL, /* init module */ | |
92 NULL /* init process */ | |
93 }; | |
94 | |
95 | |
96 ngx_uint_t ngx_max_module; | |
97 | |
98 | |
99 int main(int argc, char *const *argv) | |
100 { | |
101 ngx_int_t i; | |
102 ngx_log_t *log; | |
103 ngx_cycle_t *cycle, init_cycle; | |
104 ngx_core_conf_t *ccf; | |
105 ngx_master_ctx_t ctx; | |
106 | |
107 #if defined __FreeBSD__ | |
108 ngx_debug_init(); | |
109 #endif | |
110 | |
111 /* TODO */ ngx_max_sockets = -1; | |
112 | |
113 ngx_time_init(); | |
114 | |
115 #if (HAVE_PCRE) | |
116 ngx_regex_init(); | |
117 #endif | |
118 | |
119 ngx_pid = ngx_getpid(); | |
120 | |
121 if (!(log = ngx_log_init_stderr())) { | |
122 return 1; | |
123 } | |
124 | |
125 #if (NGX_OPENSSL) | |
126 ngx_ssl_init(log); | |
127 #endif | |
128 | |
129 /* init_cycle->log is required for signal handlers and ngx_getopt() */ | |
130 | |
131 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); | |
132 init_cycle.log = log; | |
133 ngx_cycle = &init_cycle; | |
134 | |
135 ngx_memzero(&ctx, sizeof(ngx_master_ctx_t)); | |
136 ctx.argc = argc; | |
137 ctx.argv = argv; | |
138 | |
139 if (!(init_cycle.pool = ngx_create_pool(1024, log))) { | |
140 return 1; | |
141 } | |
142 | |
143 if (ngx_getopt(&ctx, &init_cycle) == NGX_ERROR) { | |
144 return 1; | |
145 } | |
146 | |
147 if (ngx_test_config) { | |
148 log->log_level = NGX_LOG_INFO; | |
149 } | |
150 | |
151 if (ngx_os_init(log) == NGX_ERROR) { | |
152 return 1; | |
153 } | |
154 | |
155 if (ngx_add_inherited_sockets(&init_cycle) == NGX_ERROR) { | |
156 return 1; | |
157 } | |
158 | |
159 ngx_max_module = 0; | |
160 for (i = 0; ngx_modules[i]; i++) { | |
161 ngx_modules[i]->index = ngx_max_module++; | |
162 } | |
163 | |
164 cycle = ngx_init_cycle(&init_cycle); | |
165 if (cycle == NULL) { | |
166 if (ngx_test_config) { | |
167 ngx_log_error(NGX_LOG_EMERG, log, 0, | |
168 "the configuration file %s test failed", | |
169 init_cycle.conf_file.data); | |
170 } | |
171 | |
172 return 1; | |
173 } | |
174 | |
175 if (ngx_test_config) { | |
176 ngx_log_error(NGX_LOG_INFO, log, 0, | |
177 "the configuration file %s was tested successfully", | |
178 init_cycle.conf_file.data); | |
179 return 0; | |
180 } | |
181 | |
182 ngx_os_status(cycle->log); | |
183 | |
184 ngx_cycle = cycle; | |
185 | |
186 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | |
187 | |
188 ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE; | |
189 | |
190 #if (WIN32) | |
191 | |
192 #if 0 | |
193 | |
194 TODO: | |
195 | |
196 if (ccf->run_as_service) { | |
197 if (ngx_service(cycle->log) == NGX_ERROR) { | |
198 return 1; | |
199 } | |
200 | |
201 return 0; | |
202 } | |
203 #endif | |
204 | |
205 #else | |
206 | |
207 if (!ngx_inherited && ccf->daemon) { | |
208 if (ngx_daemon(cycle->log) == NGX_ERROR) { | |
209 return 1; | |
210 } | |
211 | |
212 ngx_daemonized = 1; | |
213 } | |
214 | |
215 if (ngx_create_pidfile(cycle, NULL) == NGX_ERROR) { | |
216 return 1; | |
217 } | |
218 | |
219 #endif | |
220 | |
221 if (ngx_process == NGX_PROCESS_MASTER) { | |
222 ngx_master_process_cycle(cycle, &ctx); | |
223 | |
224 } else { | |
225 ngx_single_process_cycle(cycle, &ctx); | |
226 } | |
227 | |
228 return 0; | |
229 } | |
230 | |
231 | |
232 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle) | |
233 { | |
234 u_char *p, *v, *inherited; | |
235 ngx_socket_t s; | |
236 ngx_listening_t *ls; | |
237 | |
238 inherited = (u_char *) getenv(NGINX_VAR); | |
239 | |
240 if (inherited == NULL) { | |
241 return NGX_OK; | |
242 } | |
243 | |
244 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, | |
245 "using inherited sockets from \"%s\"", inherited); | |
246 | |
247 ngx_init_array(cycle->listening, cycle->pool, | |
248 10, sizeof(ngx_listening_t), NGX_ERROR); | |
249 | |
250 for (p = inherited, v = p; *p; p++) { | |
251 if (*p == ':' || *p == ';') { | |
252 s = ngx_atoi(v, p - v); | |
253 if (s == NGX_ERROR) { | |
254 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | |
255 "invalid socket number \"%s\" in " | |
256 NGINX_VAR " enviroment variable, " | |
257 "ignoring the rest of the variable", v); | |
258 break; | |
259 } | |
260 | |
261 v = p + 1; | |
262 | |
263 if (!(ls = ngx_push_array(&cycle->listening))) { | |
264 return NGX_ERROR; | |
265 } | |
266 | |
267 ls->fd = s; | |
268 } | |
269 } | |
270 | |
271 ngx_inherited = 1; | |
272 | |
273 return ngx_set_inherited_sockets(cycle); | |
274 } | |
275 | |
276 | |
277 ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv) | |
278 { | |
279 char *env[2], *var, *p; | |
280 ngx_uint_t i; | |
281 ngx_pid_t pid; | |
282 ngx_exec_ctx_t ctx; | |
283 ngx_listening_t *ls; | |
284 | |
285 ctx.path = argv[0]; | |
286 ctx.name = "new binary process"; | |
287 ctx.argv = argv; | |
288 | |
289 var = ngx_alloc(sizeof(NGINX_VAR) | |
290 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2, | |
291 cycle->log); | |
292 | |
293 p = (char *) ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR)); | |
294 | |
295 ls = cycle->listening.elts; | |
296 for (i = 0; i < cycle->listening.nelts; i++) { | |
297 p += ngx_snprintf(p, NGX_INT32_LEN + 2, "%u;", ls[i].fd); | |
298 } | |
299 | |
300 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var); | |
301 | |
302 env[0] = var; | |
303 env[1] = NULL; | |
304 ctx.envp = (char *const *) &env; | |
305 | |
306 pid = ngx_execute(cycle, &ctx); | |
307 | |
308 ngx_free(var); | |
309 | |
310 return pid; | |
311 } | |
312 | |
313 | |
314 static ngx_int_t ngx_getopt(ngx_master_ctx_t *ctx, ngx_cycle_t *cycle) | |
315 { | |
316 ngx_int_t i; | |
317 | |
318 for (i = 1; i < ctx->argc; i++) { | |
319 if (ctx->argv[i][0] != '-') { | |
320 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | |
321 "invalid option: \"%s\"", ctx->argv[i]); | |
322 return NGX_ERROR; | |
323 } | |
324 | |
325 switch (ctx->argv[i][1]) { | |
326 | |
327 case 't': | |
328 ngx_test_config = 1; | |
329 break; | |
330 | |
331 case 'c': | |
332 if (ctx->argv[i + 1] == NULL) { | |
333 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | |
334 "the option: \"%s\" requires file name", | |
335 ctx->argv[i]); | |
336 return NGX_ERROR; | |
337 } | |
338 | |
339 cycle->conf_file.data = (u_char *) ctx->argv[++i]; | |
340 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data); | |
341 break; | |
342 | |
343 default: | |
344 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, | |
345 "invalid option: \"%s\"", ctx->argv[i]); | |
346 return NGX_ERROR; | |
347 } | |
348 } | |
349 | |
350 if (cycle->conf_file.data == NULL) { | |
351 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1; | |
352 cycle->conf_file.data = (u_char *) NGX_CONF_PATH; | |
353 } | |
354 | |
355 if (ngx_conf_full_name(cycle, &cycle->conf_file) == NGX_ERROR) { | |
356 return NGX_ERROR; | |
357 } | |
358 | |
359 return NGX_OK; | |
360 } | |
361 | |
362 | |
363 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle) | |
364 { | |
365 ngx_core_conf_t *ccf; | |
366 | |
367 if (!(ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)))) { | |
368 return NULL; | |
369 } | |
370 /* set by pcalloc() | |
371 * | |
372 * ccf->pid = NULL; | |
373 * ccf->newpid = NULL; | |
374 */ | |
375 ccf->daemon = NGX_CONF_UNSET; | |
376 ccf->master = NGX_CONF_UNSET; | |
377 ccf->worker_processes = NGX_CONF_UNSET; | |
378 #if (NGX_THREADS) | |
379 ccf->worker_threads = NGX_CONF_UNSET; | |
380 ccf->thread_stack_size = NGX_CONF_UNSET; | |
381 #endif | |
382 ccf->user = (ngx_uid_t) NGX_CONF_UNSET; | |
383 ccf->group = (ngx_gid_t) NGX_CONF_UNSET; | |
384 | |
385 return ccf; | |
386 } | |
387 | |
388 | |
389 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf) | |
390 { | |
391 ngx_core_conf_t *ccf = conf; | |
392 | |
393 #if !(WIN32) | |
394 struct passwd *pwd; | |
395 struct group *grp; | |
396 #endif | |
397 | |
398 ngx_conf_init_value(ccf->daemon, 1); | |
399 ngx_conf_init_value(ccf->master, 1); | |
400 ngx_conf_init_value(ccf->worker_processes, 1); | |
401 | |
402 #if (NGX_THREADS) | |
403 ngx_conf_init_value(ccf->worker_threads, 0); | |
404 ngx_threads_n = ccf->worker_threads; | |
405 ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024); | |
406 #endif | |
407 | |
408 #if !(WIN32) | |
409 | |
410 if (ccf->user == (uid_t) NGX_CONF_UNSET) { | |
411 | |
412 pwd = getpwnam("nobody"); | |
413 if (pwd == NULL) { | |
414 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
415 "getpwnam(\"nobody\") failed"); | |
416 return NGX_CONF_ERROR; | |
417 } | |
418 | |
419 ccf->user = pwd->pw_uid; | |
420 | |
421 grp = getgrnam("nobody"); | |
422 if (grp == NULL) { | |
423 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
424 "getgrnam(\"nobody\") failed"); | |
425 return NGX_CONF_ERROR; | |
426 } | |
427 | |
428 ccf->group = grp->gr_gid; | |
429 } | |
430 | |
431 if (ccf->pid.len == 0) { | |
432 ccf->pid.len = sizeof(NGX_PID_PATH) - 1; | |
433 ccf->pid.data = NGX_PID_PATH; | |
434 } | |
435 | |
436 if (ngx_conf_full_name(cycle, &ccf->pid) == NGX_ERROR) { | |
437 return NGX_CONF_ERROR; | |
438 } | |
439 | |
440 ccf->newpid.len = ccf->pid.len + sizeof(NGX_NEWPID_EXT); | |
441 | |
442 if (!(ccf->newpid.data = ngx_palloc(cycle->pool, ccf->newpid.len))) { | |
443 return NGX_CONF_ERROR; | |
444 } | |
445 | |
446 ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len), | |
447 NGX_NEWPID_EXT, sizeof(NGX_NEWPID_EXT)); | |
448 | |
449 #endif | |
450 | |
451 return NGX_CONF_OK; | |
452 } | |
453 | |
454 | |
455 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
456 { | |
457 #if (WIN32) | |
458 | |
459 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
460 "\"user\" is not supported, ignored"); | |
461 | |
462 return NGX_CONF_OK; | |
463 | |
464 #else | |
465 | |
466 ngx_core_conf_t *ccf = conf; | |
467 | |
468 struct passwd *pwd; | |
469 struct group *grp; | |
470 ngx_str_t *value; | |
471 | |
472 if (ccf->user != (uid_t) NGX_CONF_UNSET) { | |
473 return "is duplicate"; | |
474 } | |
475 | |
476 value = (ngx_str_t *) cf->args->elts; | |
477 | |
478 pwd = getpwnam((const char *) value[1].data); | |
479 if (pwd == NULL) { | |
480 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno, | |
481 "getpwnam(%s) failed", value[1].data); | |
482 return NGX_CONF_ERROR; | |
483 } | |
484 | |
485 ccf->user = pwd->pw_uid; | |
486 | |
487 if (cf->args->nelts == 2) { | |
488 return NGX_CONF_OK; | |
489 } | |
490 | |
491 grp = getgrnam((const char *) value[2].data); | |
492 if (grp == NULL) { | |
493 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno, | |
494 "getgrnam(%s) failed", value[1].data); | |
495 return NGX_CONF_ERROR; | |
496 } | |
497 | |
498 ccf->group = grp->gr_gid; | |
499 | |
500 return NGX_CONF_OK; | |
501 | |
502 #endif | |
503 } |