nginx 0.1.9
[nginx-catap.git] / src / core / nginx.c
blob887fc79973498566af2d18b62c8a66796e329016
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10 #include <nginx.h>
13 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
14 static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv);
15 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
16 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
17 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
18 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
21 static ngx_conf_enum_t ngx_debug_points[] = {
22 { ngx_string("stop"), NGX_DEBUG_POINTS_STOP },
23 { ngx_string("abort"), NGX_DEBUG_POINTS_ABORT },
24 { ngx_null_string, 0 }
25 };
28 static ngx_command_t ngx_core_commands[] = {
30 { ngx_string("daemon"),
31 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
32 ngx_conf_set_flag_slot,
34 offsetof(ngx_core_conf_t, daemon),
35 NULL },
37 { ngx_string("master_process"),
38 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
39 ngx_conf_set_flag_slot,
41 offsetof(ngx_core_conf_t, master),
42 NULL },
44 { ngx_string("worker_processes"),
45 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
46 ngx_conf_set_num_slot,
48 offsetof(ngx_core_conf_t, worker_processes),
49 NULL },
51 { ngx_string("debug_points"),
52 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
53 ngx_conf_set_enum_slot,
55 offsetof(ngx_core_conf_t, debug_points),
56 &ngx_debug_points },
58 #if (NGX_THREADS)
60 { ngx_string("worker_threads"),
61 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
62 ngx_conf_set_num_slot,
64 offsetof(ngx_core_conf_t, worker_threads),
65 NULL },
67 { ngx_string("thread_stack_size"),
68 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
69 ngx_conf_set_size_slot,
71 offsetof(ngx_core_conf_t, thread_stack_size),
72 NULL },
74 #endif
76 { ngx_string("user"),
77 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
78 ngx_set_user,
81 NULL },
83 { ngx_string("pid"),
84 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
85 ngx_conf_set_str_slot,
87 offsetof(ngx_core_conf_t, pid),
88 NULL },
90 ngx_null_command
94 static ngx_core_module_t ngx_core_module_ctx = {
95 ngx_string("core"),
96 ngx_core_module_create_conf,
97 ngx_core_module_init_conf
101 ngx_module_t ngx_core_module = {
102 NGX_MODULE,
103 &ngx_core_module_ctx, /* module context */
104 ngx_core_commands, /* module directives */
105 NGX_CORE_MODULE, /* module type */
106 NULL, /* init module */
107 NULL /* init process */
111 ngx_uint_t ngx_max_module;
115 int main(int argc, char *const *argv)
117 ngx_int_t i;
118 ngx_log_t *log;
119 ngx_cycle_t *cycle, init_cycle;
120 ngx_core_conf_t *ccf;
122 #if (NGX_FREEBSD)
123 ngx_debug_init();
124 #endif
126 /* TODO */ ngx_max_sockets = -1;
128 ngx_time_init();
130 #if (NGX_PCRE)
131 ngx_regex_init();
132 #endif
134 ngx_pid = ngx_getpid();
136 if (!(log = ngx_log_init())) {
137 return 1;
140 #if (NGX_OPENSSL)
141 ngx_ssl_init(log);
142 #endif
144 /* init_cycle->log is required for signal handlers and ngx_getopt() */
146 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
147 init_cycle.log = log;
148 ngx_cycle = &init_cycle;
150 if (!(init_cycle.pool = ngx_create_pool(1024, log))) {
151 return 1;
154 if (ngx_save_argv(&init_cycle, argc, argv) == NGX_ERROR) {
155 return 1;
158 if (ngx_getopt(&init_cycle, argc, ngx_argv) == NGX_ERROR) {
159 return 1;
162 if (ngx_test_config) {
163 log->log_level = NGX_LOG_INFO;
166 if (ngx_os_init(log) == NGX_ERROR) {
167 return 1;
170 if (ngx_add_inherited_sockets(&init_cycle) == NGX_ERROR) {
171 return 1;
174 ngx_max_module = 0;
175 for (i = 0; ngx_modules[i]; i++) {
176 ngx_modules[i]->index = ngx_max_module++;
179 cycle = ngx_init_cycle(&init_cycle);
180 if (cycle == NULL) {
181 if (ngx_test_config) {
182 ngx_log_error(NGX_LOG_EMERG, log, 0,
183 "the configuration file \"%s\" test failed",
184 init_cycle.conf_file.data);
187 return 1;
190 if (ngx_test_config) {
191 ngx_log_error(NGX_LOG_INFO, log, 0,
192 "the configuration file \"%s\" was tested successfully",
193 cycle->conf_file.data);
194 return 0;
197 ngx_os_status(cycle->log);
199 ngx_cycle = cycle;
201 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
203 ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
205 #if (NGX_WIN32)
207 #if 0
209 TODO:
211 if (ccf->run_as_service) {
212 if (ngx_service(cycle->log) == NGX_ERROR) {
213 return 1;
216 return 0;
218 #endif
220 #else
222 if (!ngx_inherited && ccf->daemon) {
223 if (ngx_daemon(cycle->log) == NGX_ERROR) {
224 return 1;
227 ngx_daemonized = 1;
230 if (ngx_create_pidfile(cycle, NULL) == NGX_ERROR) {
231 return 1;
234 #endif
236 if (ngx_process == NGX_PROCESS_MASTER) {
237 ngx_master_process_cycle(cycle);
239 } else {
240 ngx_single_process_cycle(cycle);
243 return 0;
247 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle)
249 u_char *p, *v, *inherited;
250 ngx_socket_t s;
251 ngx_listening_t *ls;
253 inherited = (u_char *) getenv(NGINX_VAR);
255 if (inherited == NULL) {
256 return NGX_OK;
259 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
260 "using inherited sockets from \"%s\"", inherited);
262 if (ngx_array_init(&cycle->listening, cycle->pool, 10,
263 sizeof(ngx_listening_t)) == NGX_ERROR)
265 return NGX_ERROR;
268 for (p = inherited, v = p; *p; p++) {
269 if (*p == ':' || *p == ';') {
270 s = ngx_atoi(v, p - v);
271 if (s == NGX_ERROR) {
272 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
273 "invalid socket number \"%s\" in "
274 NGINX_VAR " enviroment variable, "
275 "ignoring the rest of the variable", v);
276 break;
279 v = p + 1;
281 if (!(ls = ngx_array_push(&cycle->listening))) {
282 return NGX_ERROR;
285 ls->fd = s;
289 ngx_inherited = 1;
291 return ngx_set_inherited_sockets(cycle);
295 ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
297 char *env[3], *var;
298 u_char *p;
299 ngx_uint_t i;
300 ngx_pid_t pid;
301 ngx_exec_ctx_t ctx;
302 ngx_listening_t *ls;
304 ctx.path = argv[0];
305 ctx.name = "new binary process";
306 ctx.argv = argv;
308 var = ngx_alloc(sizeof(NGINX_VAR)
309 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
310 cycle->log);
312 p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
314 ls = cycle->listening.elts;
315 for (i = 0; i < cycle->listening.nelts; i++) {
316 p = ngx_sprintf(p, "%ud;", ls[i].fd);
319 *p = '\0';
321 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var);
323 env[0] = var;
325 #if (NGX_SETPROCTITLE_USES_ENV)
327 /* allocate the spare 300 bytes for the new binary process title */
329 env[1] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
330 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
331 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
332 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
333 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
335 env[2] = NULL;
337 #else
339 env[1] = NULL;
341 #endif
343 ctx.envp = (char *const *) &env;
345 pid = ngx_execute(cycle, &ctx);
347 ngx_free(var);
349 return pid;
353 static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)
355 ngx_int_t i;
357 for (i = 1; i < argc; i++) {
358 if (argv[i][0] != '-') {
359 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
360 "invalid option: \"%s\"", argv[i]);
361 return NGX_ERROR;
364 switch (argv[i][1]) {
366 case 't':
367 ngx_test_config = 1;
368 break;
370 case 'c':
371 if (argv[i + 1] == NULL) {
372 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
373 "the option: \"%s\" requires file name",
374 argv[i]);
375 return NGX_ERROR;
378 cycle->conf_file.data = (u_char *) argv[++i];
379 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);
380 break;
382 default:
383 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
384 "invalid option: \"%s\"", argv[i]);
385 return NGX_ERROR;
389 if (cycle->conf_file.data == NULL) {
390 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
391 cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
394 if (ngx_conf_full_name(cycle, &cycle->conf_file) == NGX_ERROR) {
395 return NGX_ERROR;
398 return NGX_OK;
402 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
404 size_t len;
405 ngx_int_t i;
407 ngx_os_argv = (char **) argv;
409 ngx_argc = argc;
411 #if (NGX_FREEBSD)
413 ngx_argv = (char **) argv;
415 #else
417 if (!(ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log))) {
418 return NGX_ERROR;
421 for (i = 0; i < argc; i++) {
422 len = ngx_strlen(argv[i]) + 1;
424 if (!(ngx_argv[i] = ngx_alloc(len, cycle->log))) {
425 return NGX_ERROR;
428 ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len);
431 ngx_argv[i] = NULL;
433 #endif
435 return NGX_OK;
439 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle)
441 ngx_core_conf_t *ccf;
443 if (!(ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)))) {
444 return NULL;
446 /* set by pcalloc()
448 * ccf->pid = NULL;
449 * ccf->newpid = NULL;
451 ccf->daemon = NGX_CONF_UNSET;
452 ccf->master = NGX_CONF_UNSET;
453 ccf->worker_processes = NGX_CONF_UNSET;
454 ccf->debug_points = NGX_CONF_UNSET;
455 ccf->user = (ngx_uid_t) NGX_CONF_UNSET;
456 ccf->group = (ngx_gid_t) NGX_CONF_UNSET;
457 #if (NGX_THREADS)
458 ccf->worker_threads = NGX_CONF_UNSET;
459 ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;
460 #endif
462 return ccf;
466 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
468 ngx_core_conf_t *ccf = conf;
470 #if !(NGX_WIN32)
471 struct passwd *pwd;
472 struct group *grp;
473 #endif
475 ngx_conf_init_value(ccf->daemon, 1);
476 ngx_conf_init_value(ccf->master, 1);
477 ngx_conf_init_value(ccf->worker_processes, 1);
478 ngx_conf_init_value(ccf->debug_points, 0);
480 #if (NGX_THREADS)
481 ngx_conf_init_value(ccf->worker_threads, 0);
482 ngx_threads_n = ccf->worker_threads;
483 ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024);
484 #endif
486 #if !(NGX_WIN32)
488 if (ccf->user == (uid_t) NGX_CONF_UNSET && geteuid() == 0) {
490 pwd = getpwnam(NGX_USER);
491 if (pwd == NULL) {
492 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
493 "getpwnam(\"" NGX_USER "\") failed");
494 return NGX_CONF_ERROR;
497 ccf->user = pwd->pw_uid;
499 grp = getgrnam(NGX_GROUP);
500 if (grp == NULL) {
501 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
502 "getgrnam(\"" NGX_GROUP "\") failed");
503 return NGX_CONF_ERROR;
506 ccf->group = grp->gr_gid;
509 if (ccf->pid.len == 0) {
510 ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
511 ccf->pid.data = (u_char *) NGX_PID_PATH;
514 if (ngx_conf_full_name(cycle, &ccf->pid) == NGX_ERROR) {
515 return NGX_CONF_ERROR;
518 ccf->newpid.len = ccf->pid.len + sizeof(NGX_NEWPID_EXT);
520 if (!(ccf->newpid.data = ngx_palloc(cycle->pool, ccf->newpid.len))) {
521 return NGX_CONF_ERROR;
524 ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len),
525 NGX_NEWPID_EXT, sizeof(NGX_NEWPID_EXT));
527 #endif
529 return NGX_CONF_OK;
533 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
535 #if (NGX_WIN32)
537 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
538 "\"user\" is not supported, ignored");
540 return NGX_CONF_OK;
542 #else
544 ngx_core_conf_t *ccf = conf;
546 char *group;
547 struct passwd *pwd;
548 struct group *grp;
549 ngx_str_t *value;
551 if (ccf->user != (uid_t) NGX_CONF_UNSET) {
552 return "is duplicate";
555 if (geteuid() != 0) {
556 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
557 "the \"user\" directive makes sense only "
558 "if the master process runs "
559 "with super-user privileges, ignored");
560 return NGX_CONF_OK;
563 value = (ngx_str_t *) cf->args->elts;
565 pwd = getpwnam((const char *) value[1].data);
566 if (pwd == NULL) {
567 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
568 "getpwnam(\"%s\") failed", value[1].data);
569 return NGX_CONF_ERROR;
572 ccf->user = pwd->pw_uid;
574 group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
576 grp = getgrnam(group);
577 if (grp == NULL) {
578 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
579 "getgrnam(\"%s\") failed", group);
580 return NGX_CONF_ERROR;
583 ccf->group = grp->gr_gid;
585 return NGX_CONF_OK;
587 #endif