changeset 109:a9bc21d63fe4

nginx-0.0.1-2003-07-02-18:41:17 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 02 Jul 2003 14:41:17 +0000
parents adc093f880c8
children 152567c11325
files src/core/nginx.c src/core/ngx_conf_file.h src/core/ngx_connection.h src/core/ngx_core.h src/core/ngx_log.c src/core/ngx_log.h src/event/modules/ngx_devpoll_module.c src/event/ngx_event.c src/event/ngx_event.h src/http/ngx_http_core_module.c src/http/ngx_http_core_module.h
diffstat 11 files changed, 111 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -5,8 +5,8 @@
 #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;
@@ -16,21 +16,25 @@ ngx_os_io_t  ngx_io;
 
 
 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;
 
@@ -64,10 +68,10 @@ int main(int argc, char *const *argv)
 
         for ( ;; ) {
 
-            worker(cycle->log);
+            ngx_worker(cycle);
 
             new_cycle = ngx_init_cycle(cycle, cycle->log);
-            if (new_cycle) == NULL) {
+            if (new_cycle == NULL) {
                 continue;
             }
 
@@ -80,9 +84,10 @@ int main(int argc, char *const *argv)
 }
 
 
-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;
@@ -102,6 +107,12 @@ static int ngx_init_cycle(ngx_cycle_t *o
     }
     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) {
@@ -153,6 +164,8 @@ static int ngx_init_cycle(ngx_cycle_t *o
         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)
@@ -166,7 +179,7 @@ static int ngx_init_cycle(ngx_cycle_t *o
     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;
             }
 
@@ -209,7 +222,7 @@ static int ngx_init_cycle(ngx_cycle_t *o
             }
         }
 
-        if (ngx_open_listening_sockets(new_cycle) == NGX_ERROR) {
+        if (ngx_open_listening_sockets(cycle, log) == NGX_ERROR) {
             failed = 1;
         }
     }
@@ -220,7 +233,7 @@ static int ngx_init_cycle(ngx_cycle_t *o
 
         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);
             }
         }
 
@@ -230,20 +243,20 @@ static int ngx_init_cycle(ngx_cycle_t *o
                 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);
@@ -252,15 +265,13 @@ static int ngx_init_cycle(ngx_cycle_t *o
 
         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);
         }
     }
 
@@ -270,7 +281,7 @@ static int ngx_init_cycle(ngx_cycle_t *o
             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);
@@ -278,20 +289,19 @@ static int ngx_init_cycle(ngx_cycle_t *o
     }
 
     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);
 
@@ -397,7 +407,7 @@ int main(int argc, char *const *argv)
 #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;
@@ -406,16 +416,19 @@ static int ngx_open_listening_sockets(ng
 
     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) {
 
@@ -423,12 +436,12 @@ static int ngx_open_listening_sockets(ng
                 /* 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);
@@ -493,22 +506,20 @@ static int ngx_open_listening_sockets(ng
             /* 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;
     }
 
--- a/src/core/ngx_conf_file.h
+++ b/src/core/ngx_conf_file.h
@@ -56,13 +56,32 @@ struct ngx_command_s {
 #define ngx_null_command   {ngx_null_string, 0, NULL, 0, 0, NULL}
 
 
+struct ngx_open_file_s {
+    ngx_fd_t   fd;
+    ngx_str_t  name;
+};
+
+
+struct ngx_cycle_s {
+    void         ****conf_ctx;
+    ngx_pool_t      *pool;
+    ngx_log_t       *log;
+    ngx_array_t      listening;
+    ngx_array_t      open_files;
+
+    unsigned         one_process:1;
+};
+
+
 struct ngx_module_s {
     int             ctx_index;
     int             index;
     void           *ctx;
     ngx_command_t  *commands;
     int             type;
-    int           (*init_module)(ngx_pool_t *p);
+    int           (*init_module)(ngx_cycle_t *cycle, ngx_log_t *log);
+    int           (*commit_module)(ngx_cycle_t *cycle, ngx_log_t *log);
+    int           (*rollback_module)(ngx_cycle_t *cycle, ngx_log_t *log);
 };
 
 
@@ -73,22 +92,6 @@ typedef struct {
 } ngx_conf_file_t;
 
 
-struct ngx_open_file_s {
-    ngx_fd_t   fd;
-    ngx_str_t  name;
-};
-
-
-typedef struct {
-    ngx_pool_t   *pool;
-    ngx_log_t    *log;
-    ngx_array_t   listening;
-    ngx_array_t   open_files;
-
-    unsigned      one_process:1;
-} ngx_cycle_t;
-
-
 typedef char *(*ngx_conf_handler_pt)(ngx_conf_t *cf,
                                      ngx_command_t *dummy, void *conf);
 
--- a/src/core/ngx_connection.h
+++ b/src/core/ngx_connection.h
@@ -34,6 +34,9 @@ typedef struct {
     time_t            post_accept_timeout;     /* should be here because
                                                   of the deferred accept */
 
+    unsigned          new:1;
+    unsigned          remain:1;
+
     unsigned          bound:1;       /* already bound */
     unsigned          inherited:1;   /* inherited from previous process */
     unsigned          nonblocking_accept:1;
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -10,7 +10,8 @@
 
 typedef struct ngx_module_s      ngx_module_t;
 typedef struct ngx_conf_s        ngx_conf_t;
-typedef struct ngx_open_file_s   ngx_open_file_t;;
+typedef struct ngx_cycle_s       ngx_cycle_t;
+typedef struct ngx_open_file_s   ngx_open_file_t;
 typedef struct ngx_command_s     ngx_command_t;
 
 typedef struct ngx_file_s        ngx_file_t;
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -246,6 +246,17 @@ ngx_log_t *ngx_log_init_errlog()
 }
 
 
+ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle)
+{
+    ngx_log_t  *log;
+
+    ngx_test_null(log, ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)), NULL);
+    ngx_test_null(log->file, ngx_push_array(&cycle->open_files), NULL);
+
+    return log;
+}
+
+
 char *ngx_log_set_errlog(ngx_conf_t *cf, ngx_command_t *cmd, ngx_log_t *log)
 {
     int         len;
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -168,6 +168,8 @@ void ngx_assert_core(ngx_log_t *log, con
 #define ngx_log_copy_log(new, old)    ngx_memcpy(new, old, sizeof(ngx_log_t))
 
 ngx_log_t *ngx_log_init_errlog();
+ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle);
+
 char *ngx_log_set_errlog(ngx_conf_t *cf, ngx_command_t *cmd, ngx_log_t *log);
 
 
--- a/src/event/modules/ngx_devpoll_module.c
+++ b/src/event/modules/ngx_devpoll_module.c
@@ -190,10 +190,10 @@ static int ngx_devpoll_add_event(ngx_eve
 
 static int ngx_devpoll_del_event(ngx_event_t *ev, int event, u_int flags)
 {
-    ngx_event_t  *e;
+    ngx_event_t       *e;
+    ngx_connection_t  *c;
 
 #if (NGX_DEBUG_EVENT)
-    ngx_connection_t *c = ev->data;
     ngx_log_debug(c->log, "del event: %d, %d" _ c->fd _ event);
 #endif
 
@@ -209,6 +209,8 @@ static int ngx_devpoll_del_event(ngx_eve
 
     /* we need to restore second event if it exists */
 
+    c = ev->data;
+
     if (event == NGX_READ_EVENT) {
         e = c->write;
         event = POLLOUT;
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -235,12 +235,12 @@ ngx_log_debug(log, "TYPE: %d" _ ecf->use
 }
 
 
-void ngx_worker(ngx_log_t *log)
+void ngx_worker(ngx_cycle_t *cycle)
 {
     for ( ;; ) {
-        ngx_log_debug(log, "ngx_worker cycle");
+        ngx_log_debug(cycle->log, "ngx_worker cycle");
 
-        ngx_process_events(log);
+        ngx_process_events(cycle->log);
     }
 }
 
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -371,7 +371,7 @@ int ngx_event_close_connection(ngx_event
 
 
 int  ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log);
-void ngx_worker(ngx_log_t *log);
+void ngx_worker(ngx_cycle_t *cycle);
 
 
 /* ***************************** */
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -20,7 +20,7 @@ static void *ngx_http_core_create_loc_co
 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,
@@ -186,17 +186,20 @@ ngx_module_t  ngx_http_core_module = {
     &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;
 
@@ -248,10 +251,12 @@ ngx_log_debug(r->connection->log, "rc: %
         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);
 
@@ -521,7 +526,7 @@ int ngx_http_internal_redirect(ngx_http_
 }
 
 
-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;
 
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -18,7 +18,8 @@ typedef struct {
 
 
 typedef struct {
-    ngx_array_t  servers;      /* array of ngx_http_core_srv_conf_t */
+    ngx_array_t  servers;              /* array of ngx_http_core_srv_conf_t */
+    ngx_array_t  translate_handlers;
 } ngx_http_core_main_conf_t;