3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.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 }
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
),
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
),
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
),
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
),
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
),
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
),
77 NGX_MAIN_CONF
|NGX_DIRECT_CONF
|NGX_CONF_TAKE12
,
84 NGX_MAIN_CONF
|NGX_DIRECT_CONF
|NGX_CONF_TAKE1
,
85 ngx_conf_set_str_slot
,
87 offsetof(ngx_core_conf_t
, pid
),
94 static ngx_core_module_t ngx_core_module_ctx
= {
96 ngx_core_module_create_conf
,
97 ngx_core_module_init_conf
101 ngx_module_t ngx_core_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
)
119 ngx_cycle_t
*cycle
, init_cycle
;
120 ngx_core_conf_t
*ccf
;
126 /* TODO */ ngx_max_sockets
= -1;
134 ngx_pid
= ngx_getpid();
136 if (!(log
= ngx_log_init())) {
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
))) {
154 if (ngx_save_argv(&init_cycle
, argc
, argv
) == NGX_ERROR
) {
158 if (ngx_getopt(&init_cycle
, argc
, ngx_argv
) == NGX_ERROR
) {
162 if (ngx_test_config
) {
163 log
->log_level
= NGX_LOG_INFO
;
166 if (ngx_os_init(log
) == NGX_ERROR
) {
170 if (ngx_add_inherited_sockets(&init_cycle
) == NGX_ERROR
) {
175 for (i
= 0; ngx_modules
[i
]; i
++) {
176 ngx_modules
[i
]->index
= ngx_max_module
++;
179 cycle
= ngx_init_cycle(&init_cycle
);
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
);
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
);
197 ngx_os_status(cycle
->log
);
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
;
211 if (ccf
->run_as_service
) {
212 if (ngx_service(cycle
->log
) == NGX_ERROR
) {
222 if (!ngx_inherited
&& ccf
->daemon
) {
223 if (ngx_daemon(cycle
->log
) == NGX_ERROR
) {
230 if (ngx_create_pidfile(cycle
, NULL
) == NGX_ERROR
) {
236 if (ngx_process
== NGX_PROCESS_MASTER
) {
237 ngx_master_process_cycle(cycle
);
240 ngx_single_process_cycle(cycle
);
247 static ngx_int_t
ngx_add_inherited_sockets(ngx_cycle_t
*cycle
)
249 u_char
*p
, *v
, *inherited
;
253 inherited
= (u_char
*) getenv(NGINX_VAR
);
255 if (inherited
== NULL
) {
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
)
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
);
281 if (!(ls
= ngx_array_push(&cycle
->listening
))) {
291 return ngx_set_inherited_sockets(cycle
);
295 ngx_pid_t
ngx_exec_new_binary(ngx_cycle_t
*cycle
, char *const *argv
)
305 ctx
.name
= "new binary process";
308 var
= ngx_alloc(sizeof(NGINX_VAR
)
309 + cycle
->listening
.nelts
* (NGX_INT32_LEN
+ 1) + 2,
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
);
321 ngx_log_debug1(NGX_LOG_DEBUG_CORE
, cycle
->log
, 0, "inherited: %s", 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";
343 ctx
.envp
= (char *const *) &env
;
345 pid
= ngx_execute(cycle
, &ctx
);
353 static ngx_int_t
ngx_getopt(ngx_cycle_t
*cycle
, int argc
, char *const *argv
)
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
]);
364 switch (argv
[i
][1]) {
371 if (argv
[i
+ 1] == NULL
) {
372 ngx_log_error(NGX_LOG_EMERG
, cycle
->log
, 0,
373 "the option: \"%s\" requires file name",
378 cycle
->conf_file
.data
= (u_char
*) argv
[++i
];
379 cycle
->conf_file
.len
= ngx_strlen(cycle
->conf_file
.data
);
383 ngx_log_error(NGX_LOG_EMERG
, cycle
->log
, 0,
384 "invalid option: \"%s\"", argv
[i
]);
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
) {
402 static ngx_int_t
ngx_save_argv(ngx_cycle_t
*cycle
, int argc
, char *const *argv
)
407 ngx_os_argv
= (char **) argv
;
413 ngx_argv
= (char **) argv
;
417 if (!(ngx_argv
= ngx_alloc((argc
+ 1) * sizeof(char *), cycle
->log
))) {
421 for (i
= 0; i
< argc
; i
++) {
422 len
= ngx_strlen(argv
[i
]) + 1;
424 if (!(ngx_argv
[i
] = ngx_alloc(len
, cycle
->log
))) {
428 ngx_cpystrn((u_char
*) ngx_argv
[i
], (u_char
*) argv
[i
], len
);
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
)))) {
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
;
458 ccf
->worker_threads
= NGX_CONF_UNSET
;
459 ccf
->thread_stack_size
= NGX_CONF_UNSET_SIZE
;
466 static char *ngx_core_module_init_conf(ngx_cycle_t
*cycle
, void *conf
)
468 ngx_core_conf_t
*ccf
= conf
;
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);
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);
488 if (ccf
->user
== (uid_t
) NGX_CONF_UNSET
&& geteuid() == 0) {
490 pwd
= getpwnam(NGX_USER
);
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
);
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
));
533 static char *ngx_set_user(ngx_conf_t
*cf
, ngx_command_t
*cmd
, void *conf
)
537 ngx_conf_log_error(NGX_LOG_WARN
, cf
, 0,
538 "\"user\" is not supported, ignored");
544 ngx_core_conf_t
*ccf
= conf
;
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");
563 value
= (ngx_str_t
*) cf
->args
->elts
;
565 pwd
= getpwnam((const char *) value
[1].data
);
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
);
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
;