fix configuration referencing

Sun, 27 Nov 2022 15:58:37 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 27 Nov 2022 15:58:37 +0100
changeset 444
96d2ba2f28db
parent 443
ef3c8a0e1fee
child 445
834351da593b

fix configuration referencing

src/server/daemon/config.c file | annotate | diff | comparison | revisions
src/server/daemon/config.h file | annotate | diff | comparison | revisions
src/server/daemon/httplistener.c file | annotate | diff | comparison | revisions
src/server/daemon/httplistener.h file | annotate | diff | comparison | revisions
src/server/daemon/main.c file | annotate | diff | comparison | revisions
src/server/daemon/webserver.c file | annotate | diff | comparison | revisions
src/server/util/atomic.h file | annotate | diff | comparison | revisions
--- a/src/server/daemon/config.c	Sun Nov 27 13:33:30 2022 +0100
+++ b/src/server/daemon/config.c	Sun Nov 27 15:58:37 2022 +0100
@@ -353,6 +353,9 @@
 }
 
 int migrate_server_conf(ServerConfiguration *old_cfg, ServerConfiguration *new_cfg) {
+    old_cfg->next = new_cfg;
+    cfg_ref(new_cfg); // new cfg should not be freed until old cfg is freed
+    
     // compare old/new listeners and set next listener, if they are using
     // the same socket
     CxIterator old_listeners = cxListIterator(old_cfg->listeners, 0);
@@ -360,11 +363,11 @@
         if(oldls->next) {
             continue;
         }
-        
+               
         CxIterator new_listeners = cxListIterator(new_cfg->listeners, 0);
         cx_foreach(HttpListener*, newls, new_listeners) {
             if(http_listener_socket_eq(oldls, newls)) {
-                oldls->next = newls;
+                http_listener_set_next(oldls, newls);
                 break;
             }
         }
@@ -380,6 +383,10 @@
 void cfg_unref(ServerConfiguration *cfg) {
     uint32_t ref = ws_atomic_dec32(&cfg->ref);
     if(ref == 0) {
+        if(cfg->next) {
+            cfg_unref(cfg->next);
+        }
+        log_ereport(LOG_VERBOSE, "destroy configuration %p", cfg);
         pool_destroy(cfg->pool);
     }
 }
--- a/src/server/daemon/config.h	Sun Nov 27 13:33:30 2022 +0100
+++ b/src/server/daemon/config.h	Sun Nov 27 15:58:37 2022 +0100
@@ -67,23 +67,25 @@
 } CfgManager;
 
 struct ServerConfiguration {
-    pool_handle_t  *pool;
-    CxAllocator    *a;
+    pool_handle_t       *pool;
+    CxAllocator         *a;
+    
+    ServerConfiguration *next;
       
-    CxMap          *host_vs;   // map of all vservers. key is the host name
-    CxList         *listeners; // list of all listeners - TODO: remove
+    CxMap               *host_vs;   // map of all vservers. key is the host name
+    CxList              *listeners; // list of all listeners - TODO: remove
     
-    CxMap          *listeners2;
+    CxMap               *listeners2;
     
-    CxList         *logfiles;
-    AccessLog      *default_log;
-    CxMap          *authdbs;
-    MimeMap        *mimetypes;
-    CxMap          *resources;
-    CxMap          *dav;
-    cxmutstr       tmp;
-    cxmutstr       user;
-    uint32_t       ref;        // reference counter
+    CxList              *logfiles;
+    AccessLog           *default_log;
+    CxMap               *authdbs;
+    MimeMap             *mimetypes;
+    CxMap               *resources;
+    CxMap               *dav;
+    cxmutstr            tmp;
+    cxmutstr            user;
+    uint32_t            ref;        // reference counter
 };
 
 struct WebdavRepository {
--- a/src/server/daemon/httplistener.c	Sun Nov 27 13:33:30 2022 +0100
+++ b/src/server/daemon/httplistener.c	Sun Nov 27 15:58:37 2022 +0100
@@ -367,7 +367,6 @@
     }
     listener->nacceptors = conf->nacceptors;
     listener->port = conf->port;
-    listener->ref = 1;
     listener->next = NULL;
     listener->ssl = NULL;
     
@@ -459,21 +458,6 @@
     return 0;
 }
 
-void http_listener_ref(HttpListener *listener) {
-    ws_atomic_inc32(&listener->ref);
-}
-
-void http_listener_unref(HttpListener *listener) {
-    uint32_t ref = ws_atomic_dec32(&listener->ref);
-    if(ref == 0) {
-        log_ereport(LOG_VERBOSE, "HttpListener %s: destroy", listener->name.ptr);
-        free(listener->acceptors);
-        // TODO: unref cfg
-        // TODO: unref session handler
-        free(listener);
-    }
-}
-
 int http_listener_socket_eq(HttpListener *l1, HttpListener *l2) {
     if(l1->server_socket && l2->server_socket && l1->server_socket == l2->server_socket) {
         return TRUE;
@@ -488,8 +472,6 @@
     while(listener->next) {
         listener = listener->next;
     }
-    
-    http_listener_ref(next);
     listener->next = next;
 }
 
@@ -502,6 +484,7 @@
 }
 
 void acceptor_start(Acceptor *a) {
+    cfg_ref(a->listener->cfg);
     if(pthread_create(
             &a->tid,
             NULL,
@@ -509,17 +492,19 @@
             a) != 0)
     {
         log_ereport(LOG_FAILURE, "Listener %s: acceptor_start: %s", a->listener->name.ptr, strerror(errno));
+        cfg_unref(a->listener->cfg);
     }
 }
 
 void* acceptor_thread(Acceptor *acceptor) {
     WS_ASSERT(acceptor);
     WS_ASSERT(acceptor->listener);
+    WS_ASSERT(acceptor->listener->cfg);
     WS_ASSERT(acceptor->listener->session_handler);
     WS_ASSERT(acceptor->listener->session_handler->enqueue_connection);
     
     HttpListener *listener = acceptor->listener;
-
+    
     int server_socket;
     
     ConnectionAddr ca;
@@ -614,10 +599,7 @@
         }
     }
     
-    if(listener->next) {
-        http_listener_unref(listener->next);
-    }
-    http_listener_unref(listener);
+    cfg_unref(acceptor->listener->cfg);
     
     return NULL;
 }
--- a/src/server/daemon/httplistener.h	Sun Nov 27 13:33:30 2022 +0100
+++ b/src/server/daemon/httplistener.h	Sun Nov 27 15:58:37 2022 +0100
@@ -93,7 +93,6 @@
     int                  nacceptors;
     int                  running;
     HttpSSL              *ssl;
-    uint32_t             ref; // reference counter
 };
 
 struct _http_ssl {
@@ -130,8 +129,6 @@
 
 int http_listener_start(HttpListener *listener);
 
-void http_listener_ref(HttpListener *listener);
-void http_listener_unref(HttpListener *listener);
 
 /*
  * returns true of l1 and l2 share the same socket
--- a/src/server/daemon/main.c	Sun Nov 27 13:33:30 2022 +0100
+++ b/src/server/daemon/main.c	Sun Nov 27 15:58:37 2022 +0100
@@ -77,19 +77,7 @@
  */
 void sig_usr1_reload(int sig) {
     log_ereport(LOG_INFORM, "sig reload");
-    
-    CfgManager mgr;
-    if(cfgmgr_load_config(&mgr) != 0) {
-        log_ereport(LOG_FAILURE, "cannot reload server.conf");
-    } else {
-        if(cfgmgr_apply_config(&mgr)) {
-            log_ereport(LOG_FAILURE, "cannot reload config");
-        }
-    }
-    
-    // start newly created listeners
-    start_all_listener();
-
+    (void)webserver_reconfig();
     signal(SIGUSR1, sig_usr1_reload);
 }
 
@@ -269,7 +257,6 @@
         return 1;
     }
     
-    is_daemon = 1;
     if(is_daemon) {
         // initialize startup pipes
         if(pipe(daemon_start_pipe)) {
@@ -324,9 +311,9 @@
         }
         
         dup2(std_out[1], 1);
-        //dup2(std_err[1], 2);
+        dup2(std_err[1], 2);
         close(std_out[1]);
-        //close(std_err[1]);
+        close(std_err[1]);
         
         // set log thread stack size
         pthread_attr_t tattr;
@@ -371,20 +358,6 @@
     if(srvctrl_wait()) {
         return EXIT_FAILURE;
     }
-    
-    /* TODO: join threads (or not?) */
-/*
-    while(1) {
-        if(is_daemon) {
-            fflush(stdout);
-            fflush(stderr);
-        }
-        sleep(10000);
-        if(0) {
-            break;
-        }
-    }
-*/
 
     return EXIT_SUCCESS;
 }
--- a/src/server/daemon/webserver.c	Sun Nov 27 13:33:30 2022 +0100
+++ b/src/server/daemon/webserver.c	Sun Nov 27 15:58:37 2022 +0100
@@ -102,8 +102,6 @@
     if(!init_config) {
         return -1;
     }
-    
-    log_ereport(LOG_INFORM, "test1");
 
     // load server.conf
     // Only the runtime infos are stored in the ServerConfiguration at
--- a/src/server/util/atomic.h	Sun Nov 27 13:33:30 2022 +0100
+++ b/src/server/util/atomic.h	Sun Nov 27 15:58:37 2022 +0100
@@ -36,7 +36,7 @@
 #ifdef __gnu_linux__
 
 #define ws_atomic_inc32(intptr) __sync_fetch_and_add(intptr, 1)
-#define ws_atomic_dec32(intptr) __sync_fetch_and_sub(intptr, 1)
+#define ws_atomic_dec32(intptr) __sync_fetch_and_sub(intptr, 1) - 1
 #define ws_atomic_add32(intptr, val) __sync_fetch_and_add(intptr, val)
 #define ws_atomic_sub32(intptr, val) __sync_fetch_and_sub(intptr, val)
 
@@ -48,7 +48,7 @@
 
 #elif defined(BSD)   
 #define ws_atomic_inc32(intptr) __sync_fetch_and_add(intptr, 1)
-#define ws_atomic_dec32(intptr) __sync_fetch_and_sub(intptr, 1)
+#define ws_atomic_dec32(intptr) __sync_fetch_and_sub(intptr, 1) - 1
 #define ws_atomic_add32(intptr, val) __sync_fetch_and_add(intptr, val)
 #define ws_atomic_sub32(intptr, val) __sync_fetch_and_sub(intptr, va
 #else

mercurial