changeset 115:be27f922b9a2

nginx-0.0.1-2003-07-10-20:26:57 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 10 Jul 2003 16:26:57 +0000
parents ac69ab96328d
children 571bcbff82c5
files src/core/nginx.c src/core/ngx_core.h src/core/ngx_log.c src/core/ngx_log.h src/core/ngx_os_init.h src/event/modules/ngx_devpoll_module.c src/event/modules/ngx_poll_module.c src/http/ngx_http_core_module.c src/os/unix/ngx_freebsd_config.h src/os/unix/ngx_linux_config.h src/os/unix/ngx_solaris_config.h
diffstat 11 files changed, 138 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -61,7 +61,20 @@ int main(int argc, char *const *argv)
 
     ngx_cycle = cycle;
 
-    /* daemon */
+#if !(WIN32)
+
+    if (0) {
+        if (ngx_daemon(cycle->log) == NGX_ERROR) {
+            return 1;
+        }
+    }
+
+    if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDERR) failed");
+        return 1;
+    }
+
+#endif
 
     /* life cycle */
 
@@ -154,7 +167,7 @@ static ngx_cycle_t *ngx_init_cycle(ngx_c
     cycle->open_files.nalloc = n;
     cycle->open_files.pool = pool;
 
-    cycle->log = ngx_log_create_errlog(cycle);
+    cycle->log = ngx_log_create_errlog(cycle, NULL);
     if (cycle->log == NULL) {
         ngx_destroy_pool(pool);
         return NULL;
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -18,9 +18,9 @@ typedef struct ngx_file_s        ngx_fil
 typedef struct ngx_event_s       ngx_event_t;
 typedef struct ngx_connection_s  ngx_connection_t;
 
+#include <ngx_string.h>
 #include <ngx_log.h>
 #include <ngx_alloc.h>
-#include <ngx_string.h>
 #include <ngx_hunk.h>
 #include <ngx_array.h>
 #include <ngx_table.h>
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -240,13 +240,16 @@ ngx_log_t *ngx_log_init_errlog()
 }
 
 
-ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle)
+ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_str_t *name)
 {
     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);
     log->file->fd = NGX_INVALID_FILE;
+    if (name) {
+        log->file->name = *name;
+    }
 
     return log;
 }
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -168,7 +168,7 @@ 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);
+ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_str_t *name);
 
 
 extern ngx_module_t  ngx_errlog_module;
--- a/src/core/ngx_os_init.h
+++ b/src/core/ngx_os_init.h
@@ -34,6 +34,10 @@ typedef struct {
 
 int ngx_os_init(ngx_log_t *log);
 
+#if !(WIN32)
+int ngx_daemon(ngx_log_t *log);
+#endif
+
 
 extern ngx_os_io_t  ngx_os_io;
 extern int          ngx_max_sockets;
--- a/src/event/modules/ngx_devpoll_module.c
+++ b/src/event/modules/ngx_devpoll_module.c
@@ -6,7 +6,6 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_connection.h>
 #include <ngx_event.h>
 
 
@@ -32,19 +31,15 @@ typedef struct {
 } ngx_devpoll_conf_t;
 
 
-static int ngx_devpoll_init(ngx_log_t *log);
-static void ngx_devpoll_done(ngx_log_t *log);
+static int ngx_devpoll_init(ngx_cycle_t *cycle);
+static void ngx_devpoll_done(ngx_cycle_t *cycle);
 static int ngx_devpoll_add_event(ngx_event_t *ev, int event, u_int flags);
 static int ngx_devpoll_del_event(ngx_event_t *ev, int event, u_int flags);
 static int ngx_devpoll_set_event(ngx_event_t *ev, int event, u_int flags);
 static int ngx_devpoll_process_events(ngx_log_t *log);
 
-static void *ngx_devpoll_create_conf(ngx_pool_t *pool);
-static char *ngx_devpoll_init_conf(ngx_pool_t *pool, void *conf);
-
-/* STUB */
-#define DEVPOLL_NCHANGES  512
-#define DEVPOLL_NEVENTS   512
+static void *ngx_devpoll_create_conf(ngx_cycle_t *cycle);
+static char *ngx_devpoll_init_conf(ngx_cycle_t *cycle, void *conf);
 
 static int              dp;
 static struct pollfd   *change_list, *event_list;
@@ -104,61 +99,107 @@ ngx_module_t  ngx_devpoll_module = {
 };
 
 
-static int ngx_devpoll_init(ngx_log_t *log)
+static int ngx_devpoll_init(ngx_cycle_t *cycle)
 {
+    int                  n;
     ngx_devpoll_conf_t  *dpcf;
 
-    dpcf = ngx_event_get_conf(ngx_devpoll_module);
+    dpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_devpoll_module);
+
+ngx_log_debug(cycle->log, "CH: %d" _ dpcf->changes);
+ngx_log_debug(cycle->log, "EV: %d" _ dpcf->events);
+
+    if (dp == -1) {
+        dp = open("/dev/poll", O_RDWR);
+
+        if (dp == -1) {
+            ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+                          "open(/dev/poll) failed");
+            return NGX_ERROR;
+        }
+    }
 
-ngx_log_debug(log, "CH: %d" _ dpcf->changes);
-ngx_log_debug(log, "EV: %d" _ dpcf->events);
+    if (max_changes < dpcf->changes) {
+        if (nchanges) {
+            n = nchanges * sizeof(struct pollfd);
+            if (write(dp, change_list, n) != n) {
+                ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+                              "write(/dev/poll) failed");
+                return NGX_ERROR;
+            }
+
+            nchanges = 0;
+        }
+
+        if (change_list) {
+            ngx_free(change_list);
+        }
+
+        ngx_test_null(change_list,
+                      ngx_alloc(sizeof(struct pollfd) * dpcf->changes,
+                                cycle->log),
+                      NGX_ERROR);
+
+        if (change_index) {
+            ngx_free(change_index);
+        }
+
+        ngx_test_null(change_index,
+                      ngx_alloc(sizeof(ngx_event_t *) * dpcf->changes,
+                                cycle->log),
+                      NGX_ERROR);
+    }
 
     max_changes = dpcf->changes;
-    nevents = dpcf->events;
-    nchanges = 0;
+
+    if (nevents < dpcf->events) {
+        if (event_list) {
+            ngx_free(event_list);
+        }
 
-    dp = open("/dev/poll", O_RDWR);
+        ngx_test_null(event_list,
+                      ngx_alloc(sizeof(struct pollfd) * dpcf->events,
+                                cycle->log),
+                      NGX_ERROR);
+    }
 
-    if (dp == -1) {
-        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "open(/dev/poll) failed");
+    nevents = dpcf->events;
+
+    if (ngx_event_timer_init(cycle) == NGX_ERROR) {
         return NGX_ERROR;
     }
 
-    ngx_test_null(change_list,
-                  ngx_alloc(sizeof(struct pollfd) * dpcf->changes, log),
-                  NGX_ERROR);
-
-    ngx_test_null(event_list,
-                  ngx_alloc(sizeof(struct pollfd) * dpcf->events, log),
-                  NGX_ERROR);
-
-    ngx_test_null(change_index,
-                  ngx_alloc(sizeof(ngx_event_t *) * dpcf->changes, log),
-                  NGX_ERROR);
-
-    if (ngx_event_timer_init(log) == NGX_ERROR) {
-        return NGX_ERROR;
-    }
+    ngx_io = ngx_os_io;
 
     ngx_event_actions = ngx_devpoll_module_ctx.actions;
+
     ngx_event_flags = NGX_HAVE_LEVEL_EVENT|NGX_USE_LEVEL_EVENT;
 
     return NGX_OK;
 }
 
 
-static void ngx_devpoll_done(ngx_log_t *log)
+static void ngx_devpoll_done(ngx_cycle_t *cycle)
 {
     if (close(dp) == -1) {
-        ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "close(/dev/poll) failed");
+        ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+                      "close(/dev/poll) failed");
     }
 
-    ngx_event_timer_done(log);
+    dp = -1;
+
+    ngx_event_timer_done(cycle);
 
     ngx_free(change_list);
     ngx_free(event_list);
     ngx_free(change_index);
 
+    change_list = NULL;
+    event_list = NULL;
+    change_index = NULL;
+    max_changes = 0;
+    nchanges = 0;
+    nevents = 0;
 }
 
 
@@ -280,12 +321,13 @@ static int ngx_devpoll_set_event(ngx_eve
 
 int ngx_devpoll_process_events(ngx_log_t *log)
 {
-    int                events, n, i;
-    ngx_msec_t         timer, delta;
-    ngx_err_t          err;
-    ngx_connection_t  *c;
-    struct dvpoll      dvp;
-    struct timeval     tv;
+    int                 events, n, i;
+    ngx_msec_t          timer, delta;
+    ngx_err_t           err;
+    ngx_cycle_t       **cycle;
+    ngx_connection_t   *c;
+    struct dvpoll       dvp;
+    struct timeval      tv;
 
     timer = ngx_event_find_timer();
 
@@ -351,6 +393,25 @@ int ngx_devpoll_process_events(ngx_log_t
     }
 
     for (i = 0; i < events; i++) {
+        c = &ngx_cycle->connections[event_list[i].fd];
+
+        if (c->fd == -1) {
+            cycle = ngx_old_cycles.elts;
+            for (i = 0; i < ngx_old_cycles.nelts; i++) {
+                if (cycle[i] == NULL) {
+                    continue;
+                }
+                c = &cycle[i]->connections[event_list[i].fd];
+                if (c->fd != -1) {
+                    break;
+                }
+            }
+        }
+
+        if (c->fd == -1) {
+            ngx_log_error(NGX_LOG_ALERT, log, 0, "unkonwn cycle");
+            exit(1);
+        }
 
 #if (NGX_DEBUG_EVENT)
         ngx_log_debug(log, "devpoll: %d: ev:%d rev:%d" _
@@ -358,8 +419,6 @@ int ngx_devpoll_process_events(ngx_log_t
                       event_list[i].events _ event_list[i].revents);
 #endif
 
-        c = &ngx_connections[event_list[i].fd];
-
         if (event_list[i].revents & POLLIN) {
             if (!c->read->active) {
                 continue;
@@ -394,11 +453,11 @@ int ngx_devpoll_process_events(ngx_log_t
 }
 
 
-static void *ngx_devpoll_create_conf(ngx_pool_t *pool)
+static void *ngx_devpoll_create_conf(ngx_cycle_t *cycle)
 {
     ngx_devpoll_conf_t  *dpcf;
 
-    ngx_test_null(dpcf, ngx_palloc(pool, sizeof(ngx_devpoll_conf_t)),
+    ngx_test_null(dpcf, ngx_palloc(cycle->pool, sizeof(ngx_devpoll_conf_t)),
                   NGX_CONF_ERROR);
 
     dpcf->changes = NGX_CONF_UNSET;
@@ -408,7 +467,7 @@ static void *ngx_devpoll_create_conf(ngx
 }
 
 
-static char *ngx_devpoll_init_conf(ngx_pool_t *pool, void *conf)
+static char *ngx_devpoll_init_conf(ngx_cycle_t *cycle, void *conf)
 {
     ngx_devpoll_conf_t *dpcf = conf;
 
--- a/src/event/modules/ngx_poll_module.c
+++ b/src/event/modules/ngx_poll_module.c
@@ -126,6 +126,8 @@ static void ngx_poll_done(ngx_cycle_t *c
     ngx_free(ready_index);
 
     event_list = NULL;
+    event_index = NULL;
+    ready_index = NULL;
 }
 
 
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1061,10 +1061,9 @@ static char *ngx_set_error_log(ngx_conf_
 
     value = cf->args->elts;
 
-    ngx_test_null(lcf->err_log, ngx_log_create_errlog(cf->cycle),
+    ngx_test_null(lcf->err_log,
+                  ngx_log_create_errlog(cf->cycle, &value[1]),
                   NGX_CONF_ERROR);
 
-    lcf->err_log->file->name = value[1];
-
     return NGX_CONF_OK;
 }
--- a/src/os/unix/ngx_freebsd_config.h
+++ b/src/os/unix/ngx_freebsd_config.h
@@ -5,6 +5,7 @@
 #include <unistd.h>
 #include <stddef.h>             /* offsetof */
 #include <stdlib.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <signal.h>
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -11,6 +11,7 @@
 #include <unistd.h>
 #include <stddef.h>             /* offsetof */
 #include <stdlib.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <signal.h>
--- a/src/os/unix/ngx_solaris_config.h
+++ b/src/os/unix/ngx_solaris_config.h
@@ -11,6 +11,7 @@
 #include <unistd.h>
 #include <stddef.h>             /* offsetof */
 #include <stdlib.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <signal.h>