#include <nginx.h>
-
-static int ngx_open_listening_sockets(ngx_log_t *log);
+static ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log);
+static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log);
void ****ngx_conf_ctx;
int ngx_max_module;
+#if 0
void *ctx_conf;
+#endif
int ngx_connection_counter;
+#if 0
ngx_array_t ngx_listening_sockets;
+#endif
-#if 0
+#if 1
int main(int argc, char *const *argv)
{
- ngx_str_t conf_file;
+ int i;
ngx_log_t *log;
ngx_conf_t conf;
- ngx_cycle_t *cycle;
+ ngx_cycle_t *cycle, *new_cycle;
/* TODO */ ngx_max_sockets = -1;
for ( ;; ) {
- worker(cycle->log);
+ ngx_worker(cycle);
new_cycle = ngx_init_cycle(cycle, cycle->log);
- if (new_cycle) == NULL) {
+ if (new_cycle == NULL) {
continue;
}
}
-static int ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log)
+static ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle, ngx_log_t *log)
{
- int i, n;
+ int i, n, failed;
+ ngx_str_t conf_file;
ngx_conf_t conf;
ngx_pool_t *pool;
ngx_cycle_t *cycle;
}
cycle->pool = pool;
+ cycle->log = ngx_log_create_errlog(cycle);
+ if (cycle->log == NULL) {
+ ngx_destroy_pool(pool);
+ return NULL;
+ }
+
n = old_cycle ? old_cycle->open_files.nelts : 20;
cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t));
if (cycle->open_files.elts == NULL) {
return NULL;
}
+ failed = 0;
+
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_module) {
if (ngx_modules[i]->init_module(cycle, log) == NGX_ERROR)
if (!failed) {
file = cycle->open_files.elts;
for (i = 0; i < cycle->open_files.nelts; i++) {
- if (file->name.data = NULL) {
+ if (file->name.data == NULL) {
continue;
}
}
}
- if (ngx_open_listening_sockets(new_cycle) == NGX_ERROR) {
+ if (ngx_open_listening_sockets(cycle, log) == NGX_ERROR) {
failed = 1;
}
}
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->rollback_module) {
- ngx_modules[i]->rollback_module(cycle);
+ ngx_modules[i]->rollback_module(cycle, log);
}
}
continue;
}
- if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
+ if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
file->name.data);
}
}
- ls[i] = cycle->listening.elts;
+ ls = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++) {
if (ls[i].new && ls[i].fd == -1) {
continue;
}
- if (ngx_close_socket(ls[i].fd) == -1)
+ if (ngx_close_socket(ls[i].fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_close_socket_n " %s failed",
ls[i].addr_text.data);
ngx_destroy_pool(pool);
return NULL;
+ }
- } else {
-
- /* commit the new cycle configuration */
+ /* commit the new cycle configuration */
- for (i = 0; ngx_modules[i]; i++) {
- if (ngx_modules[i]->commit_module) {
- ngx_modules[i]->commit_module(cycle);
- }
+ for (i = 0; ngx_modules[i]; i++) {
+ if (ngx_modules[i]->commit_module) {
+ ngx_modules[i]->commit_module(cycle, log);
}
}
continue;
}
- if (ngx_close_socket(ls[i].fd) == -1)
+ if (ngx_close_socket(ls[i].fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_close_socket_n " %s failed",
ls[i].addr_text.data);
}
file = old_cycle->open_files.elts;
- for (i = 0; i < cycle->old_open_files.nelts; i++) {
+ for (i = 0; i < old_cycle->open_files.nelts; i++) {
if (file->fd == NGX_INVALID_FILE) {
continue;
}
- if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
+ if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
file->name.data);
}
}
- new_cycle->log = ???;
- pool->log = ???;
+ pool->log = cycle->log;
ngx_destroy_pool(old_cycle->pool);
#endif
-static int ngx_open_listening_sockets(ngx_log_t *log)
+static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log)
{
int times, failed, reuseaddr, i;
ngx_err_t err;
reuseaddr = 1;
+ /* TODO: times configurable */
+
for (times = 10; times; times--) {
failed = 0;
/* for each listening socket */
- ls = ngx_listening_sockets.elts;
- for (i = 0; i < ngx_listening_sockets.nelts; i++) {
+ ls = cycle->listening.elts;
+ for (i = 0; i < cycle->listening.nelts; i++) {
- if (ls[i].bound)
+ if (ls[i].fd != -1) {
continue;
+ }
if (ls[i].inherited) {
/* TODO: nonblocking */
/* TODO: deferred accept */
- ls[i].bound = 1;
continue;
}
s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol,
ls[i].flags);
+
if (s == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_socket_n " %s falied", ls[i].addr_text.data);
/* TODO: deferred accept */
ls[i].fd = s;
- ls[i].bound = 1;
}
if (!failed)
break;
+ /* TODO: delay configurable */
+
ngx_log_error(NGX_LOG_NOTICE, log, 0,
"try again to bind() after 500ms");
ngx_msleep(500);
}
if (failed) {
-
- /* TODO: configurable */
-
- ngx_log_error(NGX_LOG_EMERG, log, 0, "can not bind(), exiting");
+ ngx_log_error(NGX_LOG_EMERG, log, 0, "still can not bind()");
return NGX_ERROR;
}
static char *ngx_http_core_merge_loc_conf(ngx_pool_t *pool,
void *parent, void *child);
-static int ngx_http_core_init(ngx_pool_t *pool);
+static int ngx_http_core_init(ngx_cycle_t *cycle, ngx_log_t *log);
static char *ngx_server_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy);
static int ngx_cmp_locations(const void *first, const void *second);
static char *ngx_location_block(ngx_conf_t *cf, ngx_command_t *cmd,
&ngx_http_core_module_ctx, /* module context */
ngx_http_core_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
- ngx_http_core_init /* init module */
+ ngx_http_core_init, /* init module */
+ NULL, /* commit module */
+ NULL /* rollback module */
};
void ngx_http_handler(ngx_http_request_t *r)
{
- int rc, i;
- ngx_http_log_ctx_t *lcx;
- ngx_http_handler_pt *h;
- ngx_http_core_loc_conf_t *clcf, **clcfp;
- ngx_http_core_srv_conf_t *cscf;
+ int rc, i;
+ ngx_http_log_ctx_t *lcx;
+ ngx_http_handler_pt *h;
+ ngx_http_core_loc_conf_t *clcf, **clcfp;
+ ngx_http_core_srv_conf_t *cscf;
+ ngx_http_core_main_conf_t *cmcf;
r->connection->unexpected_eof = 0;
r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY;
}
+ cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
+
/* run translation phase */
- h = ngx_http_translate_handlers.elts;
- for (i = ngx_http_translate_handlers.nelts - 1; i >= 0; i--) {
+ h = cmcf->translate_handlers.elts;
+ for (i = cmcf->translate_handlers.nelts - 1; i >= 0; i--) {
rc = h[i](r);
}
-static int ngx_http_core_init(ngx_pool_t *pool)
+static int ngx_http_core_init(ngx_cycle_t *cycle, ngx_log_t *log)
{
ngx_http_handler_pt *h;