nginx-quic/src/core/nginx.c

909 lines
24 KiB
C
Raw Normal View History

#include <ngx_config.h>
2003-01-10 03:09:20 -03:00
#include <ngx_core.h>
2003-06-11 11:28:34 -04:00
#include <ngx_event.h>
2003-05-30 10:27:59 -04:00
#include <nginx.h>
2004-01-05 17:55:48 -03:00
static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
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 11:17:50 -04:00
typedef struct {
2004-01-05 17:55:48 -03:00
ngx_str_t user;
2003-12-14 17:10:27 -03:00
int daemon;
2004-01-05 17:55:48 -03:00
int single;
2003-12-14 17:10:27 -03:00
ngx_str_t pid;
2003-07-11 11:17:50 -04:00
} ngx_core_conf_t;
static ngx_str_t core_name = ngx_string("core");
static ngx_command_t ngx_core_commands[] = {
2004-01-05 17:55:48 -03: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 12:08:11 -03: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-05 17:55:48 -03:00
{ ngx_string("single_process"),
NGX_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_core_flag_slot,
0,
offsetof(ngx_core_conf_t, single),
NULL },
2003-12-09 12:08:11 -03:00
ngx_null_command
2003-07-11 11:17:50 -04: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-05 17:55:48 -03:00
ngx_core_module_init, /* init module */
2003-07-11 11:17:50 -04:00
NULL /* init child */
};
2004-01-05 17:55:48 -03:00
ngx_int_t ngx_max_module;
2003-07-02 14:51:41 -04:00
2003-11-25 17:44:56 -03:00
2004-01-05 17:55:48 -03:00
/* STUB */
uid_t user;
2003-04-08 11:40:10 -04:00
2003-11-19 13:26:41 -03:00
u_int ngx_connection_counter;
2002-12-15 03:25:09 -03:00
2004-01-05 17:55:48 -03:00
ngx_int_t ngx_master;
ngx_int_t ngx_single;
2002-08-16 11:27:03 -04:00
2004-01-05 17:55:48 -03:00
ngx_int_t ngx_respawn;
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-03 12:30:22 -04:00
2003-07-01 11:00:03 -04:00
2004-01-05 17:55:48 -03:00
int main(int argc, char *const *argv, char **envp)
{
2004-01-05 17:55:48 -03:00
struct timeval tv;
ngx_fd_t fd;
ngx_int_t i;
ngx_err_t err;
ngx_log_t *log;
ngx_cycle_t *cycle, init_cycle;
ngx_open_file_t *file;
ngx_core_conf_t *ccf;
2003-11-13 03:14:05 -03:00
#if !(WIN32)
2004-01-05 17:55:48 -03:00
size_t len;
char pid[/* STUB */ 10];
ngx_file_t pidfile;
struct passwd *pwd;
2003-11-13 03:14:05 -03:00
#endif
2002-12-23 03:29:22 -03:00
2003-12-05 14:07:27 -03:00
#if __FreeBSD__
ngx_debug_init();
2003-07-07 02:11:50 -04:00
#endif
2003-07-02 01:01:53 -04:00
/* TODO */ ngx_max_sockets = -1;
2003-05-20 11:37:55 -04:00
2003-11-25 17:44:56 -03:00
ngx_time_init();
2003-12-19 05:15:11 -03:00
#if (HAVE_PCRE)
2003-11-25 17:44:56 -03:00
ngx_regex_init();
2003-12-19 05:15:11 -03:00
#endif
2003-11-13 03:14:05 -03:00
2003-06-03 11:42:58 -04:00
log = ngx_log_init_errlog();
2002-08-20 10:48:28 -04:00
2003-12-19 05:15:11 -03: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 11:42:58 -04:00
if (ngx_os_init(log) == NGX_ERROR) {
2003-05-19 12:39:14 -04:00
return 1;
2003-05-14 13:13:13 -04:00
}
2003-04-08 11:40:10 -04:00
ngx_max_module = 0;
for (i = 0; ngx_modules[i]; i++) {
ngx_modules[i]->index = ngx_max_module++;
}
2004-01-05 17:55:48 -03:00
if (!(init_cycle.pool = ngx_create_pool(1024, log))) {
return 1;
}
if (ngx_set_inherited_sockets(&init_cycle, envp) == NGX_ERROR) {
return 1;
}
cycle = ngx_init_cycle(&init_cycle);
2003-07-02 01:01:53 -04:00
if (cycle == NULL) {
2003-06-26 11:35:36 -04:00
return 1;
}
2003-07-07 02:11:50 -04:00
ngx_cycle = cycle;
2003-07-04 11:10:33 -04:00
2004-01-05 17:55:48 -03:00
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
if (ccf->single == 1) {
ngx_master = 0;
ngx_single = 1;
} else {
ngx_master = 1;
ngx_single = 0;
}
2003-07-10 12:26:57 -04:00
#if !(WIN32)
2004-01-05 17:55:48 -03: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 11:17:50 -04:00
if (ccf->daemon != 0) {
2003-11-19 13:26:41 -03:00
if (ngx_daemon(cycle->log) == NGX_ERROR) {
2003-07-10 12:26:57 -04:00
return 1;
}
}
2003-11-19 13:26:41 -03:00
if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
2003-07-11 11:17:50 -04:00
"dup2(STDERR) failed");
2003-07-10 12:26:57 -04:00
return 1;
}
2003-12-14 17:10:27 -03: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());
ngx_memzero(&pidfile, sizeof(ngx_file_t));
pidfile.name = ccf->pid;
pidfile.fd = ngx_open_file(pidfile.name.data, NGX_FILE_RDWR,
NGX_FILE_CREATE_OR_OPEN);
if (pidfile.fd == NGX_INVALID_FILE) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
ngx_open_file_n " \"%s\" failed", pidfile.name.data);
return 1;
}
if (ngx_write_file(&pidfile, pid, len, 0) == NGX_ERROR) {
return 1;
}
if (ngx_close_file(pidfile.fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
ngx_close_file_n " \"%s\" failed", pidfile.name.data);
}
2003-07-10 12:26:57 -04:00
#endif
2003-06-26 11:35:36 -04:00
2004-01-05 17:55:48 -03:00
/* a life cycle */
2003-05-16 11:27:48 -04:00
2003-07-01 11:00:03 -04:00
for ( ;; ) {
2004-01-05 17:55:48 -03:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle");
2003-07-02 14:51:41 -04:00
2004-01-05 17:55:48 -03:00
if (ngx_master) {
ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
"worker process", NGX_PROCESS_RESPAWN);
2003-06-26 11:35:36 -04:00
2004-01-05 17:55:48 -03:00
} else {
ngx_init_temp_number();
2003-07-01 11:00:03 -04:00
2004-01-05 17:55:48 -03: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 11:10:33 -04:00
}
}
}
2003-07-02 14:51:41 -04:00
2004-01-05 17:55:48 -03:00
#if 0
reconfigure = 0;
reopen = 0;
#endif
2003-06-26 11:35:36 -04:00
2004-01-05 17:55:48 -03:00
/* a cycle with the same configuration */
2003-07-03 12:30:22 -04:00
2003-06-26 11:35:36 -04:00
for ( ;; ) {
2004-01-05 17:55:48 -03:00
/* an event loop */
2003-07-03 12:30:22 -04:00
for ( ;; ) {
2004-01-05 17:55:48 -03:00
err = 0;
if (ngx_single) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"worker cycle");
ngx_process_events(cycle->log);
2003-07-03 12:30:22 -04:00
2004-01-05 17:55:48 -03:00
} else {
ngx_set_errno(0);
ngx_msleep(1000);
err = ngx_errno;
ngx_gettimeofday(&tv);
ngx_time_update(tv.tv_sec);
if (err) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, err,
"sleep() exited");
}
}
if (ngx_quit || ngx_terminate) {
2003-12-25 17:26:58 -03:00
#if !(WIN32)
2003-12-14 17:10:27 -03:00
if (ngx_delete_file(pidfile.name.data) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
ngx_delete_file_n " \"%s\" failed",
pidfile.name.data);
}
2003-12-25 17:26:58 -03:00
#endif
2003-12-14 17:10:27 -03:00
2004-01-05 17:55:48 -03:00
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
2003-07-20 17:15:59 -04:00
2004-01-05 17:55:48 -03:00
if (ngx_master) {
ngx_signal_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
2003-07-20 17:15:59 -04:00
2004-01-05 17:55:48 -03:00
/* TODO: wait workers */
2003-07-20 17:15:59 -04:00
2004-01-05 17:55:48 -03:00
ngx_msleep(1000);
2003-07-20 17:15:59 -04:00
2004-01-05 17:55:48 -03:00
ngx_gettimeofday(&tv);
ngx_time_update(tv.tv_sec);
}
2003-07-20 17:15:59 -04:00
2004-01-05 17:55:48 -03:00
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit");
exit(0);
}
2003-07-20 17:15:59 -04:00
2004-01-05 17:55:48 -03:00
if (err == NGX_EINTR) {
ngx_respawn_processes(cycle);
}
2003-12-26 16:08:50 -03:00
2004-01-05 17:55:48 -03:00
if (ngx_change_binary) {
ngx_change_binary = 0;
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
"changing binary");
ngx_exec_new_binary(cycle, argv);
/* TODO: quit workers */
2003-07-03 12:30:22 -04:00
}
2004-01-05 17:55:48 -03:00
if (ngx_reconfigure) {
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring");
2003-07-03 12:30:22 -04:00
break;
}
2004-01-05 17:55:48 -03:00
if (ngx_reopen) {
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
"reopening logs");
ngx_reopen_files(cycle);
ngx_reopen = 0;
}
2003-07-03 12:30:22 -04:00
}
2003-07-01 11:00:03 -04:00
2004-01-05 17:55:48 -03:00
cycle = ngx_init_cycle(cycle);
2003-07-04 11:10:33 -04:00
if (cycle == NULL) {
2003-11-21 03:30:49 -03:00
cycle = (ngx_cycle_t *) ngx_cycle;
2003-06-26 11:35:36 -04:00
continue;
}
2003-07-07 02:11:50 -04:00
ngx_cycle = cycle;
2004-01-05 17:55:48 -03:00
ngx_reconfigure = 0;
2003-06-26 11:35:36 -04:00
break;
}
}
2003-07-01 11:00:03 -04:00
}
2004-01-05 17:55:48 -03:00
#if 0
2003-07-01 11:00:03 -04:00
2004-01-05 17:55:48 -03:00
static ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle)
2003-07-01 11:00:03 -04:00
{
2004-01-05 17:55:48 -03:00
ngx_int_t i, n, failed;
2003-07-02 10:41:17 -04:00
ngx_str_t conf_file;
2004-01-05 17:55:48 -03:00
ngx_log_t *log;
2003-07-02 01:01:53 -04:00
ngx_conf_t conf;
ngx_pool_t *pool;
2003-07-07 02:11:50 -04:00
ngx_cycle_t *cycle, **old;
2003-12-25 17:26:58 -03:00
ngx_socket_t fd;
2003-07-11 11:17:50 -04:00
ngx_core_conf_t *ccf;
2003-07-02 01:01:53 -04:00
ngx_open_file_t *file;
ngx_listening_t *ls, *nls;
2003-07-01 11:00:03 -04:00
2004-01-05 17:55:48 -03:00
log = old_cycle->log;
2003-07-01 11:00:03 -04:00
2003-12-05 14:07:27 -03:00
if (!(pool = ngx_create_pool(16 * 1024, log))) {
2003-07-01 11:00:03 -04:00
return NULL;
}
2003-12-05 14:07:27 -03:00
if (!(cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)))) {
2003-07-01 11:00:03 -04:00
ngx_destroy_pool(pool);
return NULL;
}
cycle->pool = pool;
2003-07-07 02:11:50 -04:00
cycle->old_cycle = old_cycle;
2003-07-11 11:17:50 -04:00
2004-01-05 17:55:48 -03:00
n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
2003-12-05 14:07:27 -03:00
if (!(cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *)))) {
2003-11-18 18:34:08 -03:00
ngx_destroy_pool(pool);
return NULL;
}
cycle->pathes.nelts = 0;
cycle->pathes.size = sizeof(ngx_path_t *);
cycle->pathes.nalloc = n;
cycle->pathes.pool = pool;
2004-01-05 17:55:48 -03:00
n = old_cycle->open_files.nelts ? old_cycle->open_files.nelts : 20;
2003-07-01 11:00:03 -04:00
cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t));
if (cycle->open_files.elts == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
cycle->open_files.nelts = 0;
cycle->open_files.size = sizeof(ngx_open_file_t);
cycle->open_files.nalloc = n;
cycle->open_files.pool = pool;
2003-07-11 11:17:50 -04:00
2003-12-05 14:07:27 -03:00
if (!(cycle->log = ngx_log_create_errlog(cycle, NULL))) {
2003-07-02 14:51:41 -04:00
ngx_destroy_pool(pool);
return NULL;
}
2003-07-11 11:17:50 -04:00
2004-01-05 17:55:48 -03:00
n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;
2003-07-01 11:00:03 -04:00
cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));
if (cycle->listening.elts == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
cycle->listening.nelts = 0;
cycle->listening.size = sizeof(ngx_listening_t);
cycle->listening.nalloc = n;
cycle->listening.pool = pool;
2003-07-11 11:17:50 -04:00
2003-07-01 11:00:03 -04:00
cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
if (cycle->conf_ctx == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
2003-07-11 11:17:50 -04:00
2003-12-05 14:07:27 -03:00
if (!(ccf = ngx_pcalloc(pool, sizeof(ngx_core_conf_t)))) {
2003-07-11 11:17:50 -04:00
ngx_destroy_pool(pool);
return NULL;
}
2003-12-14 17:10:27 -03:00
/* set by pcalloc()
*
* ccf->pid = NULL;
*/
2003-07-11 11:17:50 -04:00
ccf->daemon = -1;
2004-01-05 17:55:48 -03:00
ccf->single = -1;
2003-07-11 11:17:50 -04:00
((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf;
2003-07-01 11:00:03 -04:00
ngx_memzero(&conf, sizeof(ngx_conf_t));
/* STUB: init array ? */
conf.args = ngx_create_array(pool, 10, sizeof(ngx_str_t));
if (conf.args == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
conf.ctx = cycle->conf_ctx;
conf.cycle = cycle;
2003-07-07 02:11:50 -04:00
/* STUB */ conf.pool = cycle->pool;
2003-07-01 11:00:03 -04:00
conf.log = log;
conf.module_type = NGX_CORE_MODULE;
conf.cmd_type = NGX_MAIN_CONF;
conf_file.len = sizeof(NGINX_CONF) - 1;
conf_file.data = NGINX_CONF;
if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) {
ngx_destroy_pool(pool);
return NULL;
}
2003-07-11 11:17:50 -04:00
2003-07-02 10:41:17 -04:00
failed = 0;
2003-07-04 11:10:33 -04:00
file = cycle->open_files.elts;
for (i = 0; i < cycle->open_files.nelts; i++) {
2003-07-20 17:15:59 -04:00
if (file[i].name.data == NULL) {
2003-07-04 11:10:33 -04:00
continue;
}
2003-07-01 11:00:03 -04:00
2003-07-20 17:15:59 -04:00
file[i].fd = ngx_open_file(file[i].name.data,
NGX_FILE_RDWR,
NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);
2003-07-01 11:00:03 -04:00
2003-07-20 17:15:59 -04:00
ngx_log_debug(log, "OPEN: %d:%s" _ file[i].fd _ file[i].name.data);
2003-07-07 02:11:50 -04:00
2003-07-20 17:15:59 -04:00
if (file[i].fd == NGX_INVALID_FILE) {
2003-07-04 11:10:33 -04:00
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
ngx_open_file_n " \"%s\" failed",
2003-07-20 17:15:59 -04:00
file[i].name.data);
2003-07-04 11:10:33 -04:00
failed = 1;
break;
2003-07-01 11:00:03 -04:00
}
2003-07-04 11:10:33 -04:00
2003-07-20 17:15:59 -04:00
#if (WIN32)
if (ngx_file_append_mode(file[i].fd) == NGX_ERROR) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
ngx_file_append_mode_n " \"%s\" failed",
file[i].name.data);
failed = 1;
break;
}
#endif
2003-07-01 11:00:03 -04:00
}
if (!failed) {
2004-01-05 17:55:48 -03:00
if (old_cycle->listening.nelts) {
2003-07-02 14:51:41 -04:00
ls = old_cycle->listening.elts;
2003-07-02 01:01:53 -04:00
for (i = 0; i < old_cycle->listening.nelts; i++) {
2003-07-02 14:51:41 -04:00
ls[i].remain = 0;
2003-07-02 01:01:53 -04:00
}
2003-07-02 14:51:41 -04:00
nls = cycle->listening.elts;
for (n = 0; n < cycle->listening.nelts; n++) {
for (i = 0; i < old_cycle->listening.nelts; i++) {
2004-01-05 17:55:48 -03:00
if (ls[i].ignore) {
continue;
}
ngx_log_error(NGX_LOG_INFO, log, 0,
"%X, %X",
*(int *) ls[i].sockaddr,
*(int *) nls[n].sockaddr);
2003-07-02 14:51:41 -04:00
if (ngx_memcmp(nls[n].sockaddr,
ls[i].sockaddr, ls[i].socklen) == 0)
{
2003-12-09 12:08:11 -03:00
fd = ls[i].fd;
#if (WIN32)
/*
* Winsock assignes a socket number divisible by 4 so
* to find a connection we divide a socket number by 4.
*/
fd /= 4;
#endif
2003-12-25 17:26:58 -03:00
if (fd >= (ngx_socket_t) cycle->connection_n) {
2003-12-09 12:08:11 -03:00
ngx_log_error(NGX_LOG_EMERG, log, 0,
"%d connections is not enough to hold "
"an open listening socket on %s, "
"required at least %d connections",
cycle->connection_n,
ls[i].addr_text.data, fd);
failed = 1;
break;
}
2003-07-02 14:51:41 -04:00
nls[n].fd = ls[i].fd;
2003-07-07 02:11:50 -04:00
nls[i].remain = 1;
2003-07-02 14:51:41 -04:00
ls[i].remain = 1;
break;
}
}
if (nls[n].fd == -1) {
nls[n].new = 1;
}
2003-07-02 01:01:53 -04:00
}
2003-07-07 02:11:50 -04:00
} else {
ls = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++) {
ls[i].new = 1;
}
2003-07-02 01:01:53 -04:00
}
2003-12-09 12:08:11 -03:00
if (!failed) {
2004-01-05 17:55:48 -03:00
if (ngx_open_listening_sockets(cycle) == NGX_ERROR) {
2003-12-09 12:08:11 -03:00
failed = 1;
}
2003-07-01 11:00:03 -04:00
}
}
if (failed) {
/* rollback the new cycle configuration */
file = cycle->open_files.elts;
for (i = 0; i < cycle->open_files.nelts; i++) {
2003-07-20 17:15:59 -04:00
if (file[i].fd == NGX_INVALID_FILE) {
2003-07-02 01:01:53 -04:00
continue;
}
2003-07-20 17:15:59 -04:00
if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
2003-07-02 01:01:53 -04:00
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
2003-07-20 17:15:59 -04:00
file[i].name.data);
2003-07-01 11:00:03 -04:00
}
}
2003-07-02 10:41:17 -04:00
ls = cycle->listening.elts;
2003-07-01 11:00:03 -04:00
for (i = 0; i < cycle->listening.nelts; i++) {
2003-07-02 01:01:53 -04:00
if (ls[i].new && ls[i].fd == -1) {
continue;
}
2003-07-02 10:41:17 -04:00
if (ngx_close_socket(ls[i].fd) == -1) {
2003-07-02 01:01:53 -04:00
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_close_socket_n " %s failed",
ls[i].addr_text.data);
2003-07-01 11:00:03 -04:00
}
}
ngx_destroy_pool(pool);
return NULL;
2003-07-02 10:41:17 -04:00
}
2003-07-01 11:00:03 -04:00
2003-07-02 10:41:17 -04:00
/* commit the new cycle configuration */
2003-07-01 11:00:03 -04:00
2003-07-04 11:10:33 -04:00
pool->log = cycle->log;
2003-07-07 02:11:50 -04:00
2003-07-02 10:41:17 -04:00
for (i = 0; ngx_modules[i]; i++) {
2003-07-04 11:10:33 -04:00
if (ngx_modules[i]->init_module) {
if (ngx_modules[i]->init_module(cycle) == NGX_ERROR) {
/* fatal */
exit(1);
}
2003-07-01 11:00:03 -04:00
}
}
2004-01-05 17:55:48 -03:00
/* close and delete stuff that lefts from an old cycle */
/* close the unneeded listening sockets */
2003-07-02 14:51:41 -04:00
2003-07-02 01:01:53 -04:00
ls = old_cycle->listening.elts;
for (i = 0; i < old_cycle->listening.nelts; i++) {
if (ls[i].remain) {
continue;
2003-07-01 11:00:03 -04:00
}
2003-07-02 10:41:17 -04:00
if (ngx_close_socket(ls[i].fd) == -1) {
2003-07-02 01:01:53 -04:00
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_close_socket_n " %s failed",
ls[i].addr_text.data);
}
2003-07-01 11:00:03 -04:00
}
2004-01-05 17:55:48 -03:00
/* close the unneeded open files */
2003-07-02 01:01:53 -04:00
file = old_cycle->open_files.elts;
2003-07-02 10:41:17 -04:00
for (i = 0; i < old_cycle->open_files.nelts; i++) {
2003-07-20 17:15:59 -04:00
if (file[i].fd == NGX_INVALID_FILE) {
2003-07-02 01:01:53 -04:00
continue;
2003-07-01 11:00:03 -04:00
}
2003-07-20 17:15:59 -04:00
if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
2003-07-02 01:01:53 -04:00
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
2003-07-20 17:15:59 -04:00
file[i].name.data);
2003-07-01 11:00:03 -04:00
}
}
2004-01-05 17:55:48 -03:00
if (old_cycle->connections == NULL) {
/* an old cycle is an init cycle */
ngx_destroy_pool(old_cycle->pool);
return cycle;
}
2003-06-26 11:35:36 -04:00
2004-01-05 17:55:48 -03:00
if (master) {
2003-07-07 02:11:50 -04:00
ngx_destroy_pool(old_cycle->pool);
return cycle;
2003-07-01 11:00:03 -04:00
}
2003-07-07 02:11:50 -04:00
if (ngx_temp_pool == NULL) {
ngx_temp_pool = ngx_create_pool(128, cycle->log);
if (ngx_temp_pool == NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
"can not create ngx_temp_pool");
exit(1);
2003-01-10 03:09:20 -03:00
}
2003-07-07 02:11:50 -04:00
n = 10;
ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool,
n * sizeof(ngx_cycle_t *));
if (ngx_old_cycles.elts == NULL) {
exit(1);
2003-05-30 10:27:59 -04:00
}
2003-07-07 02:11:50 -04:00
ngx_old_cycles.nelts = 0;
ngx_old_cycles.size = sizeof(ngx_cycle_t *);
ngx_old_cycles.nalloc = n;
ngx_old_cycles.pool = ngx_temp_pool;
ngx_cleaner_event.event_handler = ngx_clean_old_cycles;
ngx_cleaner_event.log = cycle->log;
ngx_cleaner_event.data = &dumb;
2003-11-16 18:49:42 -03:00
dumb.fd = (ngx_socket_t) -1;
2003-07-07 02:11:50 -04:00
}
2003-07-07 02:11:50 -04:00
ngx_temp_pool->log = cycle->log;
2003-07-07 02:11:50 -04:00
old = ngx_push_array(&ngx_old_cycles);
if (old == NULL) {
exit(1);
}
*old = old_cycle;
2003-07-07 02:11:50 -04:00
if (!ngx_cleaner_event.timer_set) {
ngx_add_timer(&ngx_cleaner_event, 30000);
ngx_cleaner_event.timer_set = 1;
2003-05-27 08:18:54 -04:00
}
2002-09-02 10:48:24 -04:00
2003-07-07 02:11:50 -04:00
return cycle;
2002-08-20 10:48:28 -04:00
}
2003-11-16 18:49:42 -03:00
#endif
2002-08-20 10:48:28 -04:00
2003-07-02 10:41:17 -04:00
2004-01-05 17:55:48 -03:00
#if 0
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
static ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle, char **envp)
{
char *p, *v;
ngx_socket_t s;
ngx_listening_t *ls;
struct sockaddr_in *addr_in;
2003-06-11 11:28:34 -04:00
2004-01-05 17:55:48 -03:00
for ( /* void */ ; *envp; envp++) {
if (ngx_strncmp(*envp, NGINX_VAR, NGINX_VAR_LEN) != 0) {
continue;
}
2002-08-26 11:18:19 -04:00
2004-01-05 17:55:48 -03:00
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
"using inherited sockets from \"%s\"", *envp);
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
ngx_init_array(cycle->listening, cycle->pool,
10, sizeof(ngx_listening_t), NGX_ERROR);
2002-08-26 11:18:19 -04:00
2004-01-05 17:55:48 -03: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 11:18:19 -04:00
2004-01-05 17:55:48 -03:00
if (!(ls = ngx_push_array(&cycle->listening))) {
return NGX_ERROR;
}
2002-08-26 11:18:19 -04:00
2004-01-05 17:55:48 -03:00
ls->fd = s;
2003-07-02 10:41:17 -04:00
2004-01-05 17:55:48 -03:00
/* AF_INET only */
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
ls->sockaddr = ngx_palloc(cycle->pool,
sizeof(struct sockaddr_in));
if (ls->sockaddr == NULL) {
return NGX_ERROR;
}
2003-06-11 11:28:34 -04:00
2004-01-05 17:55:48 -03:00
ls->socklen = sizeof(struct sockaddr_in);
if (getsockname(s, ls->sockaddr, &ls->socklen) == -1) {
ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
"getsockname() of the inherited "
"socket #%d failed", s);
ls->ignore = 1;
continue;
}
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
addr_in = (struct sockaddr_in *) ls->sockaddr;
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
if (addr_in->sin_family != AF_INET) {
ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno,
"the inherited socket #%d has "
"unsupported family", s);
ls->ignore = 1;
continue;
}
ls->addr_text_max_len = INET_ADDRSTRLEN;
ls->addr_text.data = ngx_palloc(cycle->pool,
ls->addr_text_max_len);
if (ls->addr_text.data == NULL) {
2003-11-16 18:49:42 -03:00
return NGX_ERROR;
}
2004-01-05 17:55:48 -03:00
addr_in->sin_len = 0;
ls->family = addr_in->sin_family;
ls->addr_text.len = ngx_sock_ntop(ls->family, ls->sockaddr,
ls->addr_text.data,
ls->addr_text_max_len);
if (ls->addr_text.len == 0) {
2003-05-30 10:27:59 -04:00
return NGX_ERROR;
2002-08-26 11:18:19 -04:00
}
2002-08-20 10:48:28 -04:00
}
2004-01-05 17:55:48 -03:00
}
break;
}
return NGX_OK;
}
2003-11-16 18:49:42 -03:00
#endif
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
{
ngx_int_t i;
ngx_listening_t *ls;
if (user) {
if (setuid(user) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"setuid() failed");
/* fatal */
exit(1);
}
}
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
ngx_init_temp_number();
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03: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;
}
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03: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);
2002-08-26 11:18:19 -04:00
}
2004-01-05 17:55:48 -03:00
}
}
/* TODO: threads: start ngx_worker_thread_cycle() */
for ( ;; ) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
ngx_process_events(cycle->log);
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
if (ngx_terminate) {
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
exit(0);
2002-08-20 10:48:28 -04:00
}
2004-01-05 17:55:48 -03:00
if (ngx_quit) {
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
"gracefully shutdowning");
2002-08-20 10:48:28 -04:00
break;
2004-01-05 17:55:48 -03:00
}
2002-08-20 10:48:28 -04:00
2004-01-05 17:55:48 -03:00
if (ngx_reopen) {
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reopen logs");
ngx_reopen_files(cycle);
ngx_reopen = 0;
}
2002-08-20 10:48:28 -04:00
}
2004-01-05 17:55:48 -03:00
ngx_close_listening_sockets(cycle);
2003-05-30 10:27:59 -04:00
2004-01-05 17:55:48 -03:00
for ( ;; ) {
if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
exit(0);
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
ngx_process_events(cycle->log);
}
}
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
static void ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
2003-07-07 02:11:50 -04:00
{
2004-01-05 17:55:48 -03:00
char *env[2], *var, *p;
ngx_int_t i;
ngx_exec_ctx_t ctx;
ngx_listening_t *ls;
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
ctx.path = argv[0];
ctx.name = "new binary process";
ctx.argv = argv;
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
var = ngx_alloc(NGINX_VAR_LEN
+ cycle->listening.nelts * (NGX_INT32_LEN + 1) + 1,
cycle->log);
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
p = ngx_cpymem(var, NGINX_VAR, NGINX_VAR_LEN);
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03: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 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
env[0] = var;
env[1] = NULL;
ctx.envp = (char *const *) &env;
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
ngx_exec(cycle, &ctx);
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
ngx_free(var);
}
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle)
{
ngx_core_conf_t *ccf;
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03: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 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
if (((void **)(cycle->conf_ctx))[ngx_core_module.index] != NULL) {
return NGX_OK;
}
2003-07-07 02:11:50 -04:00
2004-01-05 17:55:48 -03:00
if (!(ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)))) {
return NGX_ERROR;
2003-07-07 02:11:50 -04:00
}
2004-01-05 17:55:48 -03:00
/* set by pcalloc()
*
* ccf->pid = NULL;
*/
ccf->daemon = -1;
ccf->single = -1;
((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf;
return NGX_OK;
2003-07-07 02:11:50 -04:00
}