add more shutdown cleanup: pwbuf, threadpool, logfiles, authdb

8 weeks ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 31 Jan 2025 21:27:54 +0100 (8 weeks ago)
changeset 576
5c31cc844c68
parent 575
bbca8e94fa67
child 577
4f5ccaea4a92

add more shutdown cleanup: pwbuf, threadpool, logfiles, authdb

src/server/config/acl.c file | annotate | diff | comparison | revisions
src/server/config/conf.c file | annotate | diff | comparison | revisions
src/server/config/conf.h file | annotate | diff | comparison | revisions
src/server/config/keyfile.c file | annotate | diff | comparison | revisions
src/server/config/mimeconf.c file | annotate | diff | comparison | revisions
src/server/config/objconf.c file | annotate | diff | comparison | revisions
src/server/daemon/log.h file | annotate | diff | comparison | revisions
src/server/daemon/webserver.c file | annotate | diff | comparison | revisions
src/server/util/thrpool.c file | annotate | diff | comparison | revisions
--- a/src/server/config/acl.c	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/config/acl.c	Fri Jan 31 21:27:54 2025 +0100
@@ -60,13 +60,13 @@
     cxListDestroy(conf->namedACLs);
     cxListDestroy(conf->uriACLs);
     cxListDestroy(conf->pathACLs);
-    cxMempoolDestroy(conf->parser.mp->data); // TODO: is there a better way to access the mempool?
+    cxMempoolDestroy(conf->parser.a->data); // TODO: is there a better way to access the mempool?
     free(conf);
 }
 
 int acl_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
     ACLFile *aclf = p;
-    CxAllocator *mp = aclf->parser.mp;
+    CxAllocator *mp = aclf->parser.a;
     
     if(cx_strprefix(cx_strcast(line), cx_str("ACL "))) {
         cxmutstr param = cx_strsubs_m(line, 4);
@@ -114,7 +114,7 @@
 
 int parse_ace(ACLFile *f, cxmutstr line) {
     ACLConfig *cur = f->cur;
-    CxAllocator *mp = f->parser.mp;
+    CxAllocator *mp = f->parser.a;
     
     cxstring *tk = NULL; 
     ssize_t tkn = cx_strsplit_a(mp, cx_strcast(line), cx_str(":"), ACE_MAX_TOKENS, &tk);
--- a/src/server/config/conf.c	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/config/conf.c	Fri Jan 31 21:27:54 2025 +0100
@@ -36,7 +36,8 @@
     parser->lines_end = NULL;
     CxMempool *mp = cxBasicMempoolCreate(512);
     CxAllocator *a = (CxAllocator*)mp->allocator;
-    parser->mp = a;
+    parser->a = a;
+    parser->mp = mp;
 
     // one logical line over many lines
     cxmutstr mline;
@@ -51,8 +52,8 @@
         void *org_ptr = l.ptr;
         
         // put the line to the list
-        ConfigLine *line = OBJ_NEW(parser->mp, ConfigLine);
-        line->line = cx_strdup_a(parser->mp, cx_strcast(l)); // TODO: check for 0-len str
+        ConfigLine *line = OBJ_NEW(parser->a, ConfigLine);
+        line->line = cx_strdup_a(parser->a, cx_strcast(l)); // TODO: check for 0-len str
         line->object = NULL;
         line->type = LINE_OTHER;
         CFG_LINE_ADD(&parser->lines_begin, &parser->lines_end, line);
@@ -80,7 +81,7 @@
             }
             if(l.ptr[l.length - 1] == '\\') {
                 if(mline.ptr == NULL) {
-                    mline = cx_strdup_a(parser->mp, cx_strcast(l));
+                    mline = cx_strdup_a(parser->a, cx_strcast(l));
                     start_line = line;
                 }
             } else {
--- a/src/server/config/conf.h	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/config/conf.h	Fri Jan 31 21:27:54 2025 +0100
@@ -102,7 +102,8 @@
 };
 
 typedef struct _cfg_parser {
-    CxAllocator    *mp;
+    CxMempool      *mp;
+    CxAllocator    *a;
     ConfigLineList *lines_begin;
     ConfigLineList *lines_end;
     cfg_parse_f    parse;
--- a/src/server/config/keyfile.c	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/config/keyfile.c	Fri Jan 31 21:27:54 2025 +0100
@@ -49,6 +49,8 @@
     int r = cfg_parse_basic_file((ConfigParser*)conf, in);
     if(r != 0) {
         fclose(in);
+        cxMempoolDestroy(conf->parser.mp);
+        free(conf->file);
         free(conf);
         // TODO: free
         return NULL;
@@ -60,19 +62,14 @@
 }
 
 void free_keyfile_config(KeyfileConfig *conf) {
-    /*
-    if(conf->users) {
-        ucx_list_free_a(conf->parser.mp, conf->users);
-    }
-    ucx_mempool_destroy(conf->parser.mp->pool);
-    */
+    cxMempoolDestroy(conf->parser.mp);
     free(conf->file);
     free(conf);
 }
 
 int keyfile_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
     KeyfileConfig *conf = p;
-    CxAllocator *mp = conf->parser.mp;
+    CxAllocator *mp = conf->parser.a;
     
     cxstring *tk = NULL;
     ssize_t tkn = cx_strsplit_a(mp, cx_strcast(line), cx_strn(";", 1), KEYFILE_MAX_TOKENS, &tk);
--- a/src/server/config/mimeconf.c	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/config/mimeconf.c	Fri Jan 31 21:27:54 2025 +0100
@@ -63,13 +63,13 @@
 }
 
 void free_mime_config(MimeConfig *conf) {
-    cxMempoolDestroy(conf->parser.mp->data); // TODO: is there a better way to access the mempool?
+    cxMempoolDestroy(conf->parser.a->data); // TODO: is there a better way to access the mempool?
     free(conf);
 }
 
 int mimeconf_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
     MimeConfig *conf = p;
-    CxAllocator *mp = conf->parser.mp;
+    CxAllocator *mp = conf->parser.a;
     
     // parse mime directive
     MimeDirective *dir = OBJ_NEW_N(mp, MimeDirective);
--- a/src/server/config/objconf.c	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/config/objconf.c	Fri Jan 31 21:27:54 2025 +0100
@@ -209,7 +209,7 @@
     begin->type = cfg_get_line_type(line);
     switch(begin->type) {
         case LINE_BEGIN_TAG: {
-            ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp);
+            ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.a);
             if(tag == NULL) {
                 ws_cfg_log(LOG_FAILURE, "Parse error in %s", conf->file);
                 exit(-1); // TODO: better error handling
@@ -236,7 +236,7 @@
         case LINE_DIRECTIVE: {
             ConfigDirective *dir = cfg_parse_directive(
                     line,
-                    conf->parser.mp);
+                    conf->parser.a);
             dir->begin = begin;
             dir->end = end;
             if(objconf_on_directive(conf, dir) != 0) {
@@ -249,7 +249,7 @@
 }
 
 int objconf_on_begin_tag(ObjectConfig *conf, ConfigTag *tag) {
-    CxAllocator *mp = conf->parser.mp;
+    CxAllocator *mp = conf->parser.a;
     if(tag->type_num != TAG_OBJECT) {
         ConfigParserLevel *l = conf->levels;
         if(l->tag->type_num != TAG_OBJECT) {
@@ -305,7 +305,7 @@
             tag->iftag = last_lvl->iftag;
 
             ConfigParserLevel *lvl = OBJ_NEW(
-                    conf->parser.mp,
+                    conf->parser.a,
                     ConfigParserLevel);
             
             lvl->iftag = last_lvl->tag;
@@ -372,7 +372,7 @@
             dir);
     */
     
-    ConfigDirectiveList *dir_entry = cxMalloc(conf->parser.mp, sizeof(ConfigDirectiveList));
+    ConfigDirectiveList *dir_entry = cxMalloc(conf->parser.a, sizeof(ConfigDirectiveList));
     dir_entry->directive = dir;
     dir_entry->next = NULL;
     CFG_DIRECTIVES_ADD(&conf->obj->directives[dir->type_num], dir_entry);
--- a/src/server/daemon/log.h	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/daemon/log.h	Fri Jan 31 21:27:54 2025 +0100
@@ -47,7 +47,7 @@
 } LogConfig;
 
 typedef struct {
-    FILE     *file;
+    FILE     *file; // TODO: replace with fd
     uint32_t ref;
 } LogFile;
 
--- a/src/server/daemon/webserver.c	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/daemon/webserver.c	Fri Jan 31 21:27:54 2025 +0100
@@ -68,6 +68,8 @@
 
 static RestartCallback *atrestart;
 
+static char *pwbuf;
+
 int webserver_init() { 
     // init NSPR
     systhread_init("webserver");
@@ -126,7 +128,7 @@
     WSBool changeuid = FALSE;
     uid_t ws_uid = geteuid();
     setpwent();
-    char *pwbuf = malloc(DEF_PWBUF);
+    pwbuf = malloc(DEF_PWBUF);
     vars->Vuserpw = malloc(sizeof(struct passwd));
     if(cfg->user.ptr) {
         if(!util_getpwnam(cfg->user.ptr, vars->Vuserpw, pwbuf, DEF_PWBUF)) {
@@ -149,6 +151,7 @@
     if(!vars->Vuserpw) {
         log_ereport(LOG_VERBOSE, "globalvars->Vuserpw is null");
     }
+    endpwent();
     
     // change uid
     if(changeuid && ws_uid == 0) {
@@ -350,7 +353,13 @@
     func_cleanup();
     
     ServerConfiguration *scfg = cfgmgr_get_server_config();
+    CxIterator i = cxListIterator(scfg->logfiles);
+    cx_foreach(AccessLog *, logfile, i) {
+        fclose(logfile->log->file);
+    }
     pool_destroy(scfg->pool);
+    
+    free(pwbuf);
 }
 
 int nsapi_runtime_version() {
--- a/src/server/util/thrpool.c	Fri Jan 31 21:01:48 2025 +0100
+++ b/src/server/util/thrpool.c	Fri Jan 31 21:27:54 2025 +0100
@@ -254,6 +254,12 @@
     while(t < end || i < 2) {
         uint32_t num_threads = pool->num_threads;
         if(num_threads == 0) {
+            // it's possible that we send too many kill jobs, cleanup the queue
+            while(pool->queue) {
+                pool_queue_t *q = pool->queue->next;
+                free(pool->queue);
+                pool->queue = q;
+            }
             break;
         }
         

mercurial