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
|
|
|
|
|
|
|
|
2003-07-11 23:17:50 +08:00
|
|
|
typedef struct {
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_str_t user;
|
2003-12-15 04:10:27 +08:00
|
|
|
int daemon;
|
2004-01-09 01:08:10 +08:00
|
|
|
int master;
|
2003-12-15 04:10:27 +08:00
|
|
|
ngx_str_t pid;
|
2003-07-11 23:17:50 +08:00
|
|
|
} ngx_core_conf_t;
|
|
|
|
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
typedef struct {
|
|
|
|
ngx_file_t pid;
|
|
|
|
char *const *argv;
|
|
|
|
} ngx_master_ctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx);
|
|
|
|
static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
|
|
|
|
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle, char **envp);
|
|
|
|
static void ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv);
|
|
|
|
static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle);
|
|
|
|
|
|
|
|
|
2003-07-11 23:17:50 +08:00
|
|
|
static ngx_str_t core_name = ngx_string("core");
|
|
|
|
|
|
|
|
static ngx_command_t ngx_core_commands[] = {
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
{ ngx_string("user"),
|
|
|
|
NGX_MAIN_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_core_str_slot,
|
|
|
|
0,
|
|
|
|
offsetof(ngx_core_conf_t, user),
|
|
|
|
NULL },
|
|
|
|
|
2003-12-09 23:08:11 +08:00
|
|
|
{ ngx_string("daemon"),
|
|
|
|
NGX_MAIN_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_core_flag_slot,
|
|
|
|
0,
|
|
|
|
offsetof(ngx_core_conf_t, daemon),
|
|
|
|
NULL },
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
{ ngx_string("master_process"),
|
2004-01-06 04:55:48 +08:00
|
|
|
NGX_MAIN_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_core_flag_slot,
|
|
|
|
0,
|
2004-01-09 01:08:10 +08:00
|
|
|
offsetof(ngx_core_conf_t, master),
|
2004-01-06 04:55:48 +08:00
|
|
|
NULL },
|
|
|
|
|
2003-12-09 23:08:11 +08:00
|
|
|
ngx_null_command
|
2003-07-11 23:17:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_core_module = {
|
|
|
|
NGX_MODULE,
|
|
|
|
&core_name, /* module context */
|
|
|
|
ngx_core_commands, /* module directives */
|
|
|
|
NGX_CORE_MODULE, /* module type */
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_core_module_init, /* init module */
|
2003-07-11 23:17:50 +08:00
|
|
|
NULL /* init child */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_int_t ngx_max_module;
|
2003-07-03 02:51:41 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
/* STUB */
|
|
|
|
uid_t user;
|
2003-04-08 23:40:10 +08:00
|
|
|
|
2003-11-20 00:26:41 +08:00
|
|
|
u_int ngx_connection_counter;
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-01-08 16:47:17 +08:00
|
|
|
ngx_int_t ngx_process;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2002-08-16 23:27:03 +08:00
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
ngx_int_t ngx_reap;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_int_t ngx_terminate;
|
|
|
|
ngx_int_t ngx_quit;
|
|
|
|
ngx_int_t ngx_reconfigure;
|
|
|
|
ngx_int_t ngx_reopen;
|
|
|
|
ngx_int_t ngx_change_binary;
|
2003-07-04 00:30:22 +08:00
|
|
|
|
2003-07-01 23:00:03 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
int main(int argc, char *const *argv, char **envp)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_fd_t fd;
|
|
|
|
ngx_int_t i;
|
|
|
|
ngx_log_t *log;
|
|
|
|
ngx_cycle_t *cycle, init_cycle;
|
|
|
|
ngx_open_file_t *file;
|
|
|
|
ngx_core_conf_t *ccf;
|
2004-01-09 01:08:10 +08:00
|
|
|
ngx_master_ctx_t ctx;
|
2003-11-13 14:14:05 +08:00
|
|
|
#if !(WIN32)
|
2004-01-06 04:55:48 +08:00
|
|
|
size_t len;
|
|
|
|
char pid[/* STUB */ 10];
|
|
|
|
struct passwd *pwd;
|
2003-11-13 14:14:05 +08:00
|
|
|
#endif
|
2002-12-23 14:29:22 +08:00
|
|
|
|
2003-12-06 01:07:27 +08:00
|
|
|
#if __FreeBSD__
|
|
|
|
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
|
|
|
|
2003-12-19 16:15:11 +08:00
|
|
|
#if (HAVE_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
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
log = ngx_log_init_errlog();
|
2002-08-20 22:48:28 +08:00
|
|
|
|
2003-12-19 16:15:11 +08:00
|
|
|
/* init_cycle->log is required for signal handlers */
|
|
|
|
|
|
|
|
ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
|
|
|
|
init_cycle.log = log;
|
|
|
|
ngx_cycle = &init_cycle;
|
|
|
|
|
2003-06-03 23:42:58 +08:00
|
|
|
if (ngx_os_init(log) == NGX_ERROR) {
|
2003-05-20 00:39:14 +08:00
|
|
|
return 1;
|
2003-05-15 01:13:13 +08:00
|
|
|
}
|
|
|
|
|
2003-04-08 23:40:10 +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
|
|
|
if (!(init_cycle.pool = ngx_create_pool(1024, log))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
if (ngx_add_inherited_sockets(&init_cycle, envp) == NGX_ERROR) {
|
2004-01-06 04:55:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cycle = ngx_init_cycle(&init_cycle);
|
2003-07-02 13:01:53 +08:00
|
|
|
if (cycle == NULL) {
|
2003-06-26 23:35:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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-01-09 01:08:10 +08:00
|
|
|
ngx_process = (ccf->master != 0) ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-01-08 16:47:17 +08:00
|
|
|
#if (WIN32)
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
if (run_as_service) {
|
|
|
|
if (ngx_servie(cycle->log) == NGX_ERROR) {
|
|
|
|
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-01-06 04:55:48 +08:00
|
|
|
/* STUB */
|
|
|
|
if (ccf->user.len) {
|
|
|
|
pwd = getpwnam(ccf->user.data);
|
|
|
|
if (pwd == NULL) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
|
|
"getpwnam(%s) failed", ccf->user);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
user = pwd->pw_uid;
|
|
|
|
}
|
|
|
|
/* */
|
2003-07-11 23:17:50 +08:00
|
|
|
|
|
|
|
if (ccf->daemon != 0) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-20 00:26:41 +08:00
|
|
|
if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
2003-07-11 23:17:50 +08:00
|
|
|
"dup2(STDERR) failed");
|
2003-07-11 00:26:57 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-12-15 04:10:27 +08:00
|
|
|
if (ccf->pid.len == 0) {
|
|
|
|
ccf->pid.len = sizeof(NGINX_PID) - 1;
|
|
|
|
ccf->pid.data = NGINX_PID;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ngx_snprintf(pid, /* STUB */ 10, PID_T_FMT, ngx_getpid());
|
2004-01-09 01:08:10 +08:00
|
|
|
ngx_memzero(&ctx.pid, sizeof(ngx_file_t));
|
|
|
|
ctx.pid.name = ccf->pid;
|
2003-12-15 04:10:27 +08:00
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
ctx.pid.fd = ngx_open_file(ctx.pid.name.data, NGX_FILE_RDWR,
|
2003-12-15 04:10:27 +08:00
|
|
|
NGX_FILE_CREATE_OR_OPEN);
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
if (ctx.pid.fd == NGX_INVALID_FILE) {
|
2003-12-15 04:10:27 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
2004-01-09 01:08:10 +08:00
|
|
|
ngx_open_file_n " \"%s\" failed", ctx.pid.name.data);
|
2003-12-15 04:10:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
if (ngx_write_file(&ctx.pid, pid, len, 0) == NGX_ERROR) {
|
2003-12-15 04:10:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
if (ngx_close_file(ctx.pid.fd) == NGX_FILE_ERROR) {
|
2003-12-15 04:10:27 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
2004-01-09 01:08:10 +08:00
|
|
|
ngx_close_file_n " \"%s\" failed", ctx.pid.name.data);
|
2003-12-15 04:10:27 +08:00
|
|
|
}
|
|
|
|
|
2003-07-11 00:26:57 +08:00
|
|
|
#endif
|
2003-06-26 23:35:36 +08:00
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
ctx.argv = argv;
|
|
|
|
|
|
|
|
ngx_master_process_cycle(cycle, &ctx);
|
2004-01-08 16:47:17 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx)
|
2004-01-08 16:47:17 +08:00
|
|
|
{
|
2004-01-09 01:08:10 +08:00
|
|
|
ngx_msec_t delay;
|
2004-01-08 16:47:17 +08:00
|
|
|
struct timeval tv;
|
|
|
|
ngx_int_t i;
|
2004-01-09 01:08:10 +08:00
|
|
|
sigset_t set, wset;
|
|
|
|
|
|
|
|
delay = 1000;
|
|
|
|
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGCHLD);
|
|
|
|
sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
|
|
|
|
sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
|
|
|
|
sigaddset(&set, ngx_signal_value(NGX_INTERRUPT_SIGNAL));
|
|
|
|
sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL));
|
|
|
|
sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
|
|
|
|
sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
|
|
|
|
|
|
|
|
sigemptyset(&wset);
|
|
|
|
|
|
|
|
if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"sigprocmask() failed");
|
|
|
|
}
|
2003-05-16 23:27:48 +08:00
|
|
|
|
2003-07-01 23:00:03 +08:00
|
|
|
for ( ;; ) {
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle");
|
2003-07-03 02:51:41 +08:00
|
|
|
|
2004-01-08 16:47:17 +08:00
|
|
|
if (ngx_process == NGX_PROCESS_MASTER) {
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
|
|
|
|
"worker process", NGX_PROCESS_RESPAWN);
|
2003-06-26 23:35:36 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
} else {
|
|
|
|
ngx_init_temp_number();
|
2003-07-01 23:00:03 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
for (i = 0; ngx_modules[i]; i++) {
|
|
|
|
if (ngx_modules[i]->init_process) {
|
|
|
|
if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
|
|
|
|
/* fatal */
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-07-04 23:10:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-07-03 02:51:41 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
/* a cycle with the same configuration */
|
2003-07-04 00:30:22 +08:00
|
|
|
|
2003-06-26 23:35:36 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
/* an event loop */
|
|
|
|
|
2003-07-04 00:30:22 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
if (ngx_process == NGX_PROCESS_MASTER) {
|
|
|
|
sigsuspend(&wset);
|
|
|
|
|
|
|
|
ngx_gettimeofday(&tv);
|
|
|
|
ngx_time_update(tv.tv_sec);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
} else if (ngx_process == NGX_PROCESS_SINGLE) {
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"worker cycle");
|
|
|
|
|
|
|
|
ngx_process_events(cycle->log);
|
2003-07-04 00:30:22 +08:00
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
} else if (ngx_process == NGX_PROCESS_MASTER_QUIT) {
|
|
|
|
if (delay < 10000) {
|
|
|
|
delay *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"sigprocmask() failed");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_msleep(delay);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
ngx_gettimeofday(&tv);
|
|
|
|
ngx_time_update(tv.tv_sec);
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"sigprocmask() failed");
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_quit || ngx_terminate) {
|
2003-12-26 04:26:58 +08:00
|
|
|
#if !(WIN32)
|
2004-01-09 01:08:10 +08:00
|
|
|
if (ngx_delete_file(ctx->pid.name.data) == NGX_FILE_ERROR) {
|
2003-12-15 04:10:27 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
ngx_delete_file_n " \"%s\" failed",
|
2004-01-09 01:08:10 +08:00
|
|
|
ctx->pid.name.data);
|
2003-12-15 04:10:27 +08:00
|
|
|
}
|
2003-12-26 04:26:58 +08:00
|
|
|
#endif
|
2003-12-15 04:10:27 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
|
2003-07-21 05:15:59 +08:00
|
|
|
|
2004-01-08 16:47:17 +08:00
|
|
|
if (ngx_process == NGX_PROCESS_MASTER) {
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_signal_processes(cycle,
|
|
|
|
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
|
2003-07-21 05:15:59 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
/* TODO: wait workers */
|
2003-07-21 05:15:59 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_msleep(1000);
|
2003-07-21 05:15:59 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_gettimeofday(&tv);
|
|
|
|
ngx_time_update(tv.tv_sec);
|
|
|
|
}
|
2003-07-21 05:15:59 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit");
|
|
|
|
exit(0);
|
|
|
|
}
|
2003-07-21 05:15:59 +08:00
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
if (ngx_reap) {
|
|
|
|
ngx_reap = 0;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_respawn_processes(cycle);
|
|
|
|
}
|
2003-12-27 03:08:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (ngx_change_binary) {
|
|
|
|
ngx_change_binary = 0;
|
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
|
|
|
|
"changing binary");
|
2004-01-09 01:08:10 +08:00
|
|
|
ngx_exec_new_binary(cycle, ctx->argv);
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
/* TODO: quit workers */
|
2003-07-04 00:30:22 +08:00
|
|
|
}
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (ngx_reconfigure) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring");
|
2003-07-04 00:30:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (ngx_reopen) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
|
|
|
|
"reopening logs");
|
|
|
|
ngx_reopen_files(cycle);
|
|
|
|
ngx_reopen = 0;
|
|
|
|
}
|
|
|
|
|
2003-07-04 00:30:22 +08:00
|
|
|
}
|
2003-07-01 23:00:03 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
cycle = ngx_init_cycle(cycle);
|
2003-07-04 23:10:33 +08:00
|
|
|
if (cycle == NULL) {
|
2003-11-21 14:30:49 +08:00
|
|
|
cycle = (ngx_cycle_t *) ngx_cycle;
|
2003-06-26 23:35:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_cycle = cycle;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_reconfigure = 0;
|
2003-06-26 23:35:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-07-01 23:00:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
|
2003-07-01 23:00:03 +08:00
|
|
|
{
|
2004-01-09 01:08:10 +08:00
|
|
|
sigset_t set;
|
2004-01-07 00:49:34 +08:00
|
|
|
ngx_int_t i;
|
|
|
|
ngx_listening_t *ls;
|
2003-07-02 13:01:53 +08:00
|
|
|
|
2004-01-08 16:47:17 +08:00
|
|
|
ngx_process = NGX_PROCESS_WORKER;
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
if (user) {
|
|
|
|
if (setuid(user) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"setuid() failed");
|
|
|
|
/* fatal */
|
|
|
|
exit(1);
|
2003-07-01 23:00:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-09 01:08:10 +08:00
|
|
|
sigemptyset(&set);
|
|
|
|
|
|
|
|
if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"sigprocmask() failed");
|
|
|
|
}
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ngx_init_temp_number();
|
2003-07-01 23:00:03 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
/*
|
|
|
|
* disable deleting previous events for the listening sockets because
|
|
|
|
* in the worker processes there are no events at all at this point
|
|
|
|
*/
|
|
|
|
ls = cycle->listening.elts;
|
|
|
|
for (i = 0; i < cycle->listening.nelts; i++) {
|
|
|
|
ls[i].remain = 0;
|
2003-07-02 22:41:17 +08:00
|
|
|
}
|
2003-07-01 23:00:03 +08:00
|
|
|
|
2003-07-02 22:41:17 +08:00
|
|
|
for (i = 0; ngx_modules[i]; i++) {
|
2004-01-07 00:49:34 +08:00
|
|
|
if (ngx_modules[i]->init_process) {
|
|
|
|
if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
|
2003-07-04 23:10:33 +08:00
|
|
|
/* fatal */
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-07-01 23:00:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
/* TODO: threads: start ngx_worker_thread_cycle() */
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
|
2003-07-03 02:51:41 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ngx_process_events(cycle->log);
|
2003-07-01 23:00:03 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
if (ngx_terminate) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
|
|
|
|
exit(0);
|
2003-07-02 13:01:53 +08:00
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
if (ngx_quit) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
|
|
|
|
"gracefully shutdowning");
|
|
|
|
break;
|
2003-07-01 23:00:03 +08:00
|
|
|
}
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
if (ngx_reopen) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reopen logs");
|
|
|
|
ngx_reopen_files(cycle);
|
|
|
|
ngx_reopen = 0;
|
2003-07-01 23:00:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ngx_close_listening_sockets(cycle);
|
2003-01-10 14:09:20 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
|
|
|
|
exit(0);
|
2003-05-30 22:27:59 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
ngx_process_events(cycle->log);
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
2002-08-20 22:48:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle, char **envp)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
|
|
|
char *p, *v;
|
|
|
|
ngx_socket_t s;
|
|
|
|
ngx_listening_t *ls;
|
2003-06-11 23:28:34 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
for ( /* void */ ; *envp; envp++) {
|
|
|
|
if (ngx_strncmp(*envp, NGINX_VAR, NGINX_VAR_LEN) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
|
|
|
|
"using inherited sockets from \"%s\"", *envp);
|
2002-08-20 22:48:28 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_init_array(cycle->listening, cycle->pool,
|
|
|
|
10, sizeof(ngx_listening_t), NGX_ERROR);
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
for (p = *envp + NGINX_VAR_LEN, 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 enviroment variable, "
|
|
|
|
"ignoring the rest of the variable", v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
v = p + 1;
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (!(ls = ngx_push_array(&cycle->listening))) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2002-08-26 23:18:19 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ls->fd = s;
|
2002-08-20 22:48:28 +08:00
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
2004-01-07 00:49:34 +08:00
|
|
|
return ngx_set_inherited_sockets(cycle);
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
static void ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
|
2003-07-07 14:11:50 +08:00
|
|
|
{
|
2004-01-06 04:55:48 +08:00
|
|
|
char *env[2], *var, *p;
|
|
|
|
ngx_int_t i;
|
|
|
|
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-01-06 04:55:48 +08:00
|
|
|
var = ngx_alloc(NGINX_VAR_LEN
|
|
|
|
+ cycle->listening.nelts * (NGX_INT32_LEN + 1) + 1,
|
|
|
|
cycle->log);
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
p = ngx_cpymem(var, NGINX_VAR, NGINX_VAR_LEN);
|
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++) {
|
|
|
|
p += ngx_snprintf(p, NGX_INT32_LEN + 2, "%u;", ls[i].fd);
|
|
|
|
}
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
env[0] = var;
|
|
|
|
env[1] = NULL;
|
|
|
|
ctx.envp = (char *const *) &env;
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_exec(cycle, &ctx);
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_free(var);
|
|
|
|
}
|
2003-07-07 14:11:50 +08:00
|
|
|
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle)
|
|
|
|
{
|
|
|
|
ngx_core_conf_t *ccf;
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
/*
|
|
|
|
* ngx_core_module has a special init procedure: it is called by
|
|
|
|
* ngx_init_cycle() before the configuration file parsing to create
|
|
|
|
* ngx_core_module configuration and to set its default parameters
|
|
|
|
*/
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (((void **)(cycle->conf_ctx))[ngx_core_module.index] != NULL) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
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)))) {
|
|
|
|
return NGX_ERROR;
|
2003-07-07 14:11:50 +08:00
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
/* set by pcalloc()
|
|
|
|
*
|
|
|
|
* ccf->pid = NULL;
|
|
|
|
*/
|
|
|
|
ccf->daemon = -1;
|
2004-01-09 01:08:10 +08:00
|
|
|
ccf->master = -1;
|
2004-01-06 04:55:48 +08:00
|
|
|
|
|
|
|
((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf;
|
|
|
|
|
|
|
|
return NGX_OK;
|
2003-07-07 14:11:50 +08:00
|
|
|
}
|