2002-08-07 00:39:45 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_config.h>
|
2003-01-10 14:09:20 +08:00
|
|
|
#include <ngx_core.h>
|
2003-06-11 23:28:34 +08:00
|
|
|
#include <ngx_event.h>
|
2003-05-30 22:27:59 +08:00
|
|
|
#include <nginx.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
2004-03-10 03:47:07 +08:00
|
|
|
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
|
2004-10-11 23:07:03 +08:00
|
|
|
static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv);
|
|
|
|
static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
|
2004-04-13 00:38:09 +08:00
|
|
|
static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
|
|
|
|
static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
|
2004-01-21 04:40:08 +08:00
|
|
|
static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
2004-01-09 01:08:10 +08:00
|
|
|
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
static ngx_conf_enum_t ngx_debug_points[] = {
|
|
|
|
{ ngx_string("stop"), NGX_DEBUG_POINTS_STOP },
|
|
|
|
{ ngx_string("abort"), NGX_DEBUG_POINTS_ABORT },
|
|
|
|
{ ngx_null_string, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-07-11 23:17:50 +08:00
|
|
|
static ngx_command_t ngx_core_commands[] = {
|
|
|
|
|
2003-12-09 23:08:11 +08:00
|
|
|
{ ngx_string("daemon"),
|
2004-04-13 00:38:09 +08:00
|
|
|
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_flag_slot,
|
2003-12-09 23:08:11 +08:00
|
|
|
0,
|
|
|
|
offsetof(ngx_core_conf_t, daemon),
|
|
|
|
NULL },
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
{ ngx_string("master_process"),
|
2004-04-13 00:38:09 +08:00
|
|
|
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_flag_slot,
|
2004-01-06 04:55:48 +08:00
|
|
|
0,
|
2004-01-09 01:08:10 +08:00
|
|
|
offsetof(ngx_core_conf_t, master),
|
2004-01-06 04:55:48 +08:00
|
|
|
NULL },
|
|
|
|
|
2004-03-31 04:31:58 +08:00
|
|
|
{ ngx_string("worker_processes"),
|
2004-04-13 00:38:09 +08:00
|
|
|
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_num_slot,
|
2004-02-10 04:47:18 +08:00
|
|
|
0,
|
2004-03-31 04:31:58 +08:00
|
|
|
offsetof(ngx_core_conf_t, worker_processes),
|
2004-02-10 04:47:18 +08:00
|
|
|
NULL },
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
{ ngx_string("debug_points"),
|
|
|
|
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_enum_slot,
|
|
|
|
0,
|
|
|
|
offsetof(ngx_core_conf_t, debug_points),
|
|
|
|
&ngx_debug_points },
|
|
|
|
|
2004-07-05 14:55:54 +08:00
|
|
|
#if (NGX_THREADS)
|
|
|
|
|
|
|
|
{ ngx_string("worker_threads"),
|
|
|
|
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_num_slot,
|
|
|
|
0,
|
|
|
|
offsetof(ngx_core_conf_t, worker_threads),
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("thread_stack_size"),
|
|
|
|
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_size_slot,
|
|
|
|
0,
|
|
|
|
offsetof(ngx_core_conf_t, thread_stack_size),
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
{ ngx_string("user"),
|
|
|
|
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
|
|
|
|
ngx_set_user,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
2004-03-31 04:31:58 +08:00
|
|
|
{ ngx_string("pid"),
|
2004-04-13 00:38:09 +08:00
|
|
|
NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_str_slot,
|
2004-01-22 14:47:28 +08:00
|
|
|
0,
|
2004-03-31 04:31:58 +08:00
|
|
|
offsetof(ngx_core_conf_t, pid),
|
2004-01-22 14:47:28 +08:00
|
|
|
NULL },
|
|
|
|
|
2003-12-09 23:08:11 +08:00
|
|
|
ngx_null_command
|
2003-07-11 23:17:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
static ngx_core_module_t ngx_core_module_ctx = {
|
|
|
|
ngx_string("core"),
|
|
|
|
ngx_core_module_create_conf,
|
|
|
|
ngx_core_module_init_conf
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-07-11 23:17:50 +08:00
|
|
|
ngx_module_t ngx_core_module = {
|
|
|
|
NGX_MODULE,
|
2004-04-13 00:38:09 +08:00
|
|
|
&ngx_core_module_ctx, /* module context */
|
2003-07-11 23:17:50 +08:00
|
|
|
ngx_core_commands, /* module directives */
|
|
|
|
NGX_CORE_MODULE, /* module type */
|
2004-04-13 00:38:09 +08:00
|
|
|
NULL, /* init module */
|
2004-10-04 04:02:06 +08:00
|
|
|
NULL /* init process */
|
2003-07-11 23:17:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-04-26 04:13:21 +08:00
|
|
|
ngx_uint_t ngx_max_module;
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2003-07-01 23:00:03 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
int main(int argc, char *const *argv)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2004-10-11 23:07:03 +08:00
|
|
|
ngx_int_t i;
|
|
|
|
ngx_log_t *log;
|
|
|
|
ngx_cycle_t *cycle, init_cycle;
|
|
|
|
ngx_core_conf_t *ccf;
|
2002-12-23 14:29:22 +08:00
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
#if (NGX_FREEBSD)
|
2003-12-06 01:07:27 +08:00
|
|
|
ngx_debug_init();
|
2003-07-07 14:11:50 +08:00
|
|
|
#endif
|
|
|
|
|
2003-07-02 13:01:53 +08:00
|
|
|
/* TODO */ ngx_max_sockets = -1;
|
2003-05-20 23:37:55 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_time_init();
|
2004-01-09 01:08:10 +08:00
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
#if (NGX_PCRE)
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_regex_init();
|
2003-12-19 16:15:11 +08:00
|
|
|
#endif
|
2003-11-13 14:14:05 +08:00
|
|
|
|
2004-02-04 00:43:54 +08:00
|
|
|
ngx_pid = ngx_getpid();
|
2002-08-20 22:48:28 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
if (!(log = ngx_log_init())) {
|
2004-09-30 00:00:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2004-09-29 04:09:22 +08:00
|
|
|
|
2004-07-16 00:35:51 +08:00
|
|
|
#if (NGX_OPENSSL)
|
|
|
|
ngx_ssl_init(log);
|
|
|
|
#endif
|
|
|
|
|
2004-02-10 04:47:18 +08:00
|
|
|
/* init_cycle->log is required for signal handlers and ngx_getopt() */
|
2003-12-19 16:15:11 +08:00
|
|
|
|
|
|
|
ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
|
|
|
|
init_cycle.log = log;
|
|
|
|
ngx_cycle = &init_cycle;
|
|
|
|
|
2004-09-29 04:09:22 +08:00
|
|
|
if (!(init_cycle.pool = ngx_create_pool(1024, log))) {
|
2004-02-10 04:47:18 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
if (ngx_save_argv(&init_cycle, argc, argv) == NGX_ERROR) {
|
2004-10-11 23:07:03 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
if (ngx_getopt(&init_cycle, argc, ngx_argv) == NGX_ERROR) {
|
2003-05-20 00:39:14 +08:00
|
|
|
return 1;
|
2003-05-15 01:13:13 +08:00
|
|
|
}
|
|
|
|
|
2004-10-01 23:53:53 +08:00
|
|
|
if (ngx_test_config) {
|
|
|
|
log->log_level = NGX_LOG_INFO;
|
|
|
|
}
|
|
|
|
|
2004-09-29 04:09:22 +08:00
|
|
|
if (ngx_os_init(log) == NGX_ERROR) {
|
2004-01-06 04:55:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-10 03:47:07 +08:00
|
|
|
if (ngx_add_inherited_sockets(&init_cycle) == NGX_ERROR) {
|
2004-01-06 04:55:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-04-15 23:34:36 +08:00
|
|
|
ngx_max_module = 0;
|
|
|
|
for (i = 0; ngx_modules[i]; i++) {
|
|
|
|
ngx_modules[i]->index = ngx_max_module++;
|
|
|
|
}
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
cycle = ngx_init_cycle(&init_cycle);
|
2003-07-02 13:01:53 +08:00
|
|
|
if (cycle == NULL) {
|
2004-04-19 03:06:02 +08:00
|
|
|
if (ngx_test_config) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"the configuration file \"%s\" test failed",
|
2004-04-19 03:06:02 +08:00
|
|
|
init_cycle.conf_file.data);
|
|
|
|
}
|
|
|
|
|
2003-06-26 23:35:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-04-15 23:34:36 +08:00
|
|
|
if (ngx_test_config) {
|
2004-04-19 03:06:02 +08:00
|
|
|
ngx_log_error(NGX_LOG_INFO, log, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"the configuration file \"%s\" was tested successfully",
|
2004-10-21 23:34:38 +08:00
|
|
|
cycle->conf_file.data);
|
2004-04-15 23:34:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-04 04:02:06 +08:00
|
|
|
ngx_os_status(cycle->log);
|
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_cycle = cycle;
|
2003-07-04 23:10:33 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
#if (NGX_WIN32)
|
2004-01-08 16:47:17 +08:00
|
|
|
|
|
|
|
#if 0
|
2004-06-23 23:18:17 +08:00
|
|
|
|
|
|
|
TODO:
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
if (ccf->run_as_service) {
|
|
|
|
if (ngx_service(cycle->log) == NGX_ERROR) {
|
2004-01-08 16:47:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
2004-01-08 16:47:17 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
2003-07-11 00:26:57 +08:00
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
if (!ngx_inherited && ccf->daemon) {
|
2003-11-20 00:26:41 +08:00
|
|
|
if (ngx_daemon(cycle->log) == NGX_ERROR) {
|
2003-07-11 00:26:57 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2004-06-23 23:18:17 +08:00
|
|
|
|
|
|
|
ngx_daemonized = 1;
|
2003-07-11 00:26:57 +08:00
|
|
|
}
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
if (ngx_create_pidfile(cycle, NULL) == NGX_ERROR) {
|
2003-12-15 04:10:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-07-11 00:26:57 +08:00
|
|
|
#endif
|
2003-06-26 23:35:36 +08:00
|
|
|
|
2004-04-16 13:14:16 +08:00
|
|
|
if (ngx_process == NGX_PROCESS_MASTER) {
|
2004-10-11 23:07:03 +08:00
|
|
|
ngx_master_process_cycle(cycle);
|
2004-04-16 13:14:16 +08:00
|
|
|
|
|
|
|
} else {
|
2004-10-11 23:07:03 +08:00
|
|
|
ngx_single_process_cycle(cycle);
|
2004-04-16 13:14:16 +08:00
|
|
|
}
|
2004-01-08 16:47:17 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-10 03:47:07 +08:00
|
|
|
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *p, *v, *inherited;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_socket_t s;
|
|
|
|
ngx_listening_t *ls;
|
2003-06-11 23:28:34 +08:00
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
inherited = (u_char *) getenv(NGINX_VAR);
|
2004-03-10 03:47:07 +08:00
|
|
|
|
|
|
|
if (inherited == NULL) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
|
2004-03-10 03:47:07 +08:00
|
|
|
"using inherited sockets from \"%s\"", inherited);
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
if (ngx_array_init(&cycle->listening, cycle->pool, 10,
|
|
|
|
sizeof(ngx_listening_t)) == NGX_ERROR)
|
|
|
|
{
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-03-10 03:47:07 +08:00
|
|
|
|
|
|
|
for (p = inherited, v = p; *p; p++) {
|
|
|
|
if (*p == ':' || *p == ';') {
|
|
|
|
s = ngx_atoi(v, p - v);
|
|
|
|
if (s == NGX_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
|
|
"invalid socket number \"%s\" in "
|
|
|
|
NGINX_VAR " enviroment variable, "
|
|
|
|
"ignoring the rest of the variable", v);
|
|
|
|
break;
|
2002-08-20 22:48:28 +08:00
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
v = p + 1;
|
2004-03-10 03:47:07 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
if (!(ls = ngx_array_push(&cycle->listening))) {
|
2004-03-10 03:47:07 +08:00
|
|
|
return NGX_ERROR;
|
2004-03-16 15:10:12 +08:00
|
|
|
}
|
2004-01-09 05:02:06 +08:00
|
|
|
|
2004-03-10 03:47:07 +08:00
|
|
|
ls->fd = s;
|
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
2004-03-10 03:47:07 +08:00
|
|
|
ngx_inherited = 1;
|
|
|
|
|
|
|
|
return ngx_set_inherited_sockets(cycle);
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-03-04 15:04:55 +08:00
|
|
|
ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
|
2003-07-07 14:11:50 +08:00
|
|
|
{
|
2004-11-11 22:07:14 +08:00
|
|
|
char *env[3], *var;
|
|
|
|
u_char *p;
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_uint_t i;
|
2004-01-14 05:33:59 +08:00
|
|
|
ngx_pid_t pid;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_exec_ctx_t ctx;
|
|
|
|
ngx_listening_t *ls;
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ctx.path = argv[0];
|
|
|
|
ctx.name = "new binary process";
|
|
|
|
ctx.argv = argv;
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-03-10 03:47:07 +08:00
|
|
|
var = ngx_alloc(sizeof(NGINX_VAR)
|
|
|
|
+ cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
|
2004-01-06 04:55:48 +08:00
|
|
|
cycle->log);
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ls = cycle->listening.elts;
|
|
|
|
for (i = 0; i < cycle->listening.nelts; i++) {
|
2004-11-11 22:07:14 +08:00
|
|
|
p = ngx_sprintf(p, "%ud;", ls[i].fd);
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
*p = '\0';
|
|
|
|
|
2004-03-10 03:47:07 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var);
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
env[0] = var;
|
2004-10-11 23:07:03 +08:00
|
|
|
|
|
|
|
#if (NGX_SETPROCTITLE_USES_ENV)
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
/* allocate the spare 300 bytes for the new binary process title */
|
2004-10-11 23:07:03 +08:00
|
|
|
|
|
|
|
env[1] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
|
|
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
|
|
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
|
|
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
|
|
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
|
|
|
|
|
|
|
|
env[2] = NULL;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
env[1] = NULL;
|
2004-10-11 23:07:03 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ctx.envp = (char *const *) &env;
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-03-05 00:34:23 +08:00
|
|
|
pid = ngx_execute(cycle, &ctx);
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_free(var);
|
2004-01-14 05:33:59 +08:00
|
|
|
|
|
|
|
return pid;
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
2003-07-07 14:11:50 +08:00
|
|
|
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)
|
2004-02-10 04:47:18 +08:00
|
|
|
{
|
|
|
|
ngx_int_t i;
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (argv[i][0] != '-') {
|
2004-02-10 04:47:18 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
2004-10-11 23:07:03 +08:00
|
|
|
"invalid option: \"%s\"", argv[i]);
|
2004-02-10 04:47:18 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
switch (argv[i][1]) {
|
2004-02-10 04:47:18 +08:00
|
|
|
|
2004-04-15 23:34:36 +08:00
|
|
|
case 't':
|
|
|
|
ngx_test_config = 1;
|
|
|
|
break;
|
|
|
|
|
2004-02-10 04:47:18 +08:00
|
|
|
case 'c':
|
2004-10-11 23:07:03 +08:00
|
|
|
if (argv[i + 1] == NULL) {
|
2004-04-15 04:34:05 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
|
|
"the option: \"%s\" requires file name",
|
2004-10-11 23:07:03 +08:00
|
|
|
argv[i]);
|
2004-04-15 04:34:05 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
cycle->conf_file.data = (u_char *) argv[++i];
|
2004-02-10 04:47:18 +08:00
|
|
|
cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
2004-10-11 23:07:03 +08:00
|
|
|
"invalid option: \"%s\"", argv[i]);
|
2004-02-10 04:47:18 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-11 00:23:38 +08:00
|
|
|
if (cycle->conf_file.data == NULL) {
|
2004-05-19 04:28:54 +08:00
|
|
|
cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
|
|
|
|
cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
|
2004-02-10 04:47:18 +08:00
|
|
|
}
|
|
|
|
|
2004-09-28 00:03:21 +08:00
|
|
|
if (ngx_conf_full_name(cycle, &cycle->conf_file) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-02-10 04:47:18 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
ngx_int_t i;
|
|
|
|
|
|
|
|
ngx_os_argv = (char **) argv;
|
|
|
|
|
|
|
|
ngx_argc = argc;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
#if (NGX_FREEBSD)
|
2004-10-11 23:07:03 +08:00
|
|
|
|
|
|
|
ngx_argv = (char **) argv;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (!(ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log))) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
len = ngx_strlen(argv[i]) + 1;
|
|
|
|
|
|
|
|
if (!(ngx_argv[i] = ngx_alloc(len, cycle->log))) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_argv[i] = NULL;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
static void *ngx_core_module_create_conf(ngx_cycle_t *cycle)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
|
|
|
ngx_core_conf_t *ccf;
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (!(ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)))) {
|
2004-04-13 00:38:09 +08:00
|
|
|
return NULL;
|
2003-07-07 14:11:50 +08:00
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
/* set by pcalloc()
|
|
|
|
*
|
|
|
|
* ccf->pid = NULL;
|
2004-02-11 00:23:38 +08:00
|
|
|
* ccf->newpid = NULL;
|
2004-01-06 04:55:48 +08:00
|
|
|
*/
|
2004-01-21 04:40:08 +08:00
|
|
|
ccf->daemon = NGX_CONF_UNSET;
|
|
|
|
ccf->master = NGX_CONF_UNSET;
|
2004-03-31 04:31:58 +08:00
|
|
|
ccf->worker_processes = NGX_CONF_UNSET;
|
2004-11-11 22:07:14 +08:00
|
|
|
ccf->debug_points = NGX_CONF_UNSET;
|
|
|
|
ccf->user = (ngx_uid_t) NGX_CONF_UNSET;
|
|
|
|
ccf->group = (ngx_gid_t) NGX_CONF_UNSET;
|
2004-07-05 14:55:54 +08:00
|
|
|
#if (NGX_THREADS)
|
|
|
|
ccf->worker_threads = NGX_CONF_UNSET;
|
2004-11-11 22:07:14 +08:00
|
|
|
ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;
|
2004-07-05 14:55:54 +08:00
|
|
|
#endif
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
return ccf;
|
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
|
|
|
|
static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
|
|
|
|
{
|
|
|
|
ngx_core_conf_t *ccf = conf;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
#if !(NGX_WIN32)
|
2004-09-28 00:03:21 +08:00
|
|
|
struct passwd *pwd;
|
|
|
|
struct group *grp;
|
|
|
|
#endif
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
ngx_conf_init_value(ccf->daemon, 1);
|
|
|
|
ngx_conf_init_value(ccf->master, 1);
|
|
|
|
ngx_conf_init_value(ccf->worker_processes, 1);
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_conf_init_value(ccf->debug_points, 0);
|
2004-04-13 00:38:09 +08:00
|
|
|
|
2004-07-05 14:55:54 +08:00
|
|
|
#if (NGX_THREADS)
|
|
|
|
ngx_conf_init_value(ccf->worker_threads, 0);
|
|
|
|
ngx_threads_n = ccf->worker_threads;
|
|
|
|
ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024);
|
|
|
|
#endif
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
#if !(NGX_WIN32)
|
2004-04-13 00:38:09 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
if (ccf->user == (uid_t) NGX_CONF_UNSET && geteuid() == 0) {
|
2004-09-28 00:03:21 +08:00
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
pwd = getpwnam(NGX_USER);
|
2004-09-28 00:03:21 +08:00
|
|
|
if (pwd == NULL) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
2004-10-21 23:34:38 +08:00
|
|
|
"getpwnam(\"" NGX_USER "\") failed");
|
2004-09-28 00:03:21 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccf->user = pwd->pw_uid;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
grp = getgrnam(NGX_GROUP);
|
2004-09-28 00:03:21 +08:00
|
|
|
if (grp == NULL) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
2004-10-21 23:34:38 +08:00
|
|
|
"getgrnam(\"" NGX_GROUP "\") failed");
|
2004-09-28 00:03:21 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccf->group = grp->gr_gid;
|
|
|
|
}
|
2004-04-13 00:38:09 +08:00
|
|
|
|
|
|
|
if (ccf->pid.len == 0) {
|
2004-05-19 04:28:54 +08:00
|
|
|
ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
|
2004-10-11 23:07:03 +08:00
|
|
|
ccf->pid.data = (u_char *) NGX_PID_PATH;
|
2004-09-28 00:03:21 +08:00
|
|
|
}
|
2004-04-13 00:38:09 +08:00
|
|
|
|
2004-09-28 00:03:21 +08:00
|
|
|
if (ngx_conf_full_name(cycle, &ccf->pid) == NGX_ERROR) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2004-04-13 00:38:09 +08:00
|
|
|
|
2004-09-28 00:03:21 +08:00
|
|
|
ccf->newpid.len = ccf->pid.len + sizeof(NGX_NEWPID_EXT);
|
2004-04-13 00:38:09 +08:00
|
|
|
|
2004-09-28 00:03:21 +08:00
|
|
|
if (!(ccf->newpid.data = ngx_palloc(cycle->pool, ccf->newpid.len))) {
|
|
|
|
return NGX_CONF_ERROR;
|
2004-04-13 00:38:09 +08:00
|
|
|
}
|
2004-09-28 00:03:21 +08:00
|
|
|
|
|
|
|
ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len),
|
|
|
|
NGX_NEWPID_EXT, sizeof(NGX_NEWPID_EXT));
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
2003-07-07 14:11:50 +08:00
|
|
|
}
|
2004-01-21 04:40:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
2004-11-11 22:07:14 +08:00
|
|
|
#if (NGX_WIN32)
|
2004-03-04 15:04:55 +08:00
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"\"user\" is not supported, ignored");
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
ngx_core_conf_t *ccf = conf;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
char *group;
|
2004-01-21 04:40:08 +08:00
|
|
|
struct passwd *pwd;
|
|
|
|
struct group *grp;
|
|
|
|
ngx_str_t *value;
|
|
|
|
|
|
|
|
if (ccf->user != (uid_t) NGX_CONF_UNSET) {
|
|
|
|
return "is duplicate";
|
|
|
|
}
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
if (geteuid() != 0) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
|
|
"the \"user\" directive makes sense only "
|
|
|
|
"if the master process runs "
|
|
|
|
"with super-user privileges, ignored");
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
2004-01-21 04:40:08 +08:00
|
|
|
value = (ngx_str_t *) cf->args->elts;
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
pwd = getpwnam((const char *) value[1].data);
|
2004-01-21 04:40:08 +08:00
|
|
|
if (pwd == NULL) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
|
2004-10-11 23:07:03 +08:00
|
|
|
"getpwnam(\"%s\") failed", value[1].data);
|
2004-01-21 04:40:08 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccf->user = pwd->pw_uid;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
|
2004-01-21 04:40:08 +08:00
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
grp = getgrnam(group);
|
2004-01-21 04:40:08 +08:00
|
|
|
if (grp == NULL) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
|
2004-10-21 23:34:38 +08:00
|
|
|
"getgrnam(\"%s\") failed", group);
|
2004-01-21 04:40:08 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccf->group = grp->gr_gid;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
2004-03-04 15:04:55 +08:00
|
|
|
|
|
|
|
#endif
|
2004-01-21 04:40:08 +08:00
|
|
|
}
|