Began working on the 'clean shutdown' code for the new thread architecture.
authorArt Cancro <ajc@citadel.org>
Mon, 21 Mar 2011 03:34:08 +0000 (23:34 -0400)
committerWilfried Goesgens <dothebart@citadel.org>
Sun, 4 Sep 2011 17:13:42 +0000 (17:13 +0000)
14 files changed:
citadel/citserver.c
citadel/housekeeping.c
citadel/locate_host.c
citadel/modules/expire/serv_expire.c
citadel/modules/fulltext/serv_fulltext.c
citadel/modules/migrate/serv_migrate.c
citadel/modules/network/serv_network.c
citadel/modules/pop3client/serv_pop3client.c
citadel/modules/rssclient/serv_rssclient.c
citadel/modules/smtp/serv_smtp.c
citadel/modules/urldeshortener/serv_expand_shorter_urls.c
citadel/sysdep.c
citadel/threads.c
citadel/threads.h

index cff393ba04e239f8f68b1543d65cc6c7e3e40c81..2b70a2667d7249f4a9c85963f50edd4c0951daa0 100644 (file)
@@ -817,7 +817,7 @@ void cmd_down(char *argbuf) {
                cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN); 
        }
        CC->kill_me = KILLME_SERVER_SHUTTING_DOWN;
-       CtdlThreadStopAll();
+       server_shutting_down = 1;
 }
 
 
@@ -829,7 +829,7 @@ void cmd_halt(char *argbuf) {
        if (CtdlAccessCheck(ac_aide)) return;
 
        cprintf("%d Halting server.  Goodbye.\n", CIT_OK);
-       CtdlThreadStopAll();
+       server_shutting_down = 1;
        shutdown_and_halt = 1;
 }
 
index 5773925dd382199bb84f54da25f88d31379a729d..f4d930d64802631a320086be954b50f409255f58 100644 (file)
@@ -63,7 +63,7 @@
 void check_sched_shutdown(void) {
        if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
                syslog(LOG_NOTICE, "Scheduled shutdown initiating.\n");
-               CtdlThreadStopAll();
+               server_shutting_down = 1;
        }
 }
 
index 20704c927c3958f8a419f109fa70bf84018cef20..c3a19636930996eeecebb11db922588bf248829f 100644 (file)
@@ -105,7 +105,7 @@ int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
 
        /* Make our DNS query. */
        answer = fixedans;
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
        {
                if (txtbuf != NULL) {
                        snprintf(txtbuf, txtbufsize, "System shutting down");
@@ -136,7 +136,7 @@ int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
                        return(1);
                }
        }
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
        {
                if (txtbuf != NULL)
                        snprintf(txtbuf, txtbufsize, "System shutting down");
@@ -153,7 +153,7 @@ int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize) {
         * nameserver we're using.
         */
        len = res_query(domain, C_IN, T_TXT, answer, PACKETSZ);
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
        {
                if (txtbuf != NULL) {
                        snprintf(txtbuf, txtbufsize, "System shutting down");
index 70ddb316c4f2ae2e93218af2a3df5ac0ce8c194c..c92ae6d80fcd3a618ff6b8e44d87677ce70e532e 100644 (file)
@@ -876,55 +876,55 @@ void purge_databases(void)
 
        syslog(LOG_INFO, "Auto-purger: starting.\n");
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                retval = PurgeUsers();
                syslog(LOG_NOTICE, "Purged %d users.\n", retval);
        }
                
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                PurgeMessages();
                syslog(LOG_NOTICE, "Expired %d messages.\n", messages_purged);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                        retval = PurgeRooms();
                        syslog(LOG_NOTICE, "Expired %d rooms.\n", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                        retval = PurgeVisits();
                        syslog(LOG_NOTICE, "Purged %d visits.\n", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                retval = PurgeUseTable();
                        syslog(LOG_NOTICE, "Purged %d entries from the use table.\n", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                retval = PurgeEuidIndexTable();
                syslog(LOG_NOTICE, "Purged %d entries from the EUID index.\n", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                retval = PurgeStaleOpenIDassociations();
                syslog(LOG_NOTICE, "Purged %d stale OpenID associations.\n", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                        retval = TDAP_ProcessAdjRefCountQueue();
                syslog(LOG_NOTICE, "Processed %d message reference count adjustments.\n", retval);
        }
 
-       if (!CtdlThreadCheckStop())
+       if (!server_shutting_down)
        {
                syslog(LOG_INFO, "Auto-purger: finished.\n");
                last_purge = now;       /* So we don't do it again soon */
index bac3e90b98bf04fe8fb9af9e134c7acbe992159a..db0edbdd11bc04dc23bf512934f07303f14e8508 100644 (file)
@@ -228,7 +228,7 @@ void ft_index_msg(long msgnum, void *userdata) {
  */
 void ft_index_room(struct ctdlroom *qrbuf, void *data)
 {
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
                
        CtdlGetRoom(&CC->room, qrbuf->QRname);
@@ -321,7 +321,7 @@ void do_fulltext_indexing(void) {
                        ft_index_message(ft_newmsgs[i], 1);
 
                        /* Check to see if we need to quit early */
-                       if (CtdlThreadCheckStop()) {
+                       if (server_shutting_down) {
                                syslog(LOG_DEBUG, "Indexer quitting early\n");
                                ft_newhighest = ft_newmsgs[i];
                                break;
@@ -343,7 +343,7 @@ void do_fulltext_indexing(void) {
        }
        end_time = time(NULL);
 
-       if (CtdlThreadCheckStop()) {
+       if (server_shutting_down) {
                is_running = 0;
                return;
        }
index 10404772b3f2647a0fb676e77a84730d58033984..091822c38a97598fe44d7c6c4a8fa2255bd86ecb 100644 (file)
@@ -831,7 +831,7 @@ void migr_xml_end(void *data, const char *el) {
                        msglist = NULL;
                        msglist_alloc = 0;
                        syslog(LOG_DEBUG, "Imported %d messages.\n", msgcount);
-                       if (CtdlThreadCheckStop()) {
+                       if (server_shutting_down) {
                                return;
                }
        }
@@ -932,7 +932,7 @@ void migr_do_import(void) {
                linelen = strlen(buf);
                strcpy(&buf[linelen++], "\n");
 
-               if (CtdlThreadCheckStop())
+               if (server_shutting_down)
                        break;  // Should we break or return?
                
                if (buf[0] == '\0')
index 99d7f6a28d3f9d86afadc9fc683759ca20cabe19..4518740bf1ac9d00754940659cfbe3070abb6f5b 100644 (file)
@@ -1986,7 +1986,7 @@ void receive_spool(int *sock, char *remote_nodename) {
                 * If shutting down we can exit here and unlink the temp file.
                 * this shouldn't loose us any messages.
                 */
-               if (CtdlThreadCheckStop())
+               if (server_shutting_down)
                {
                        fclose(fp);
                        unlink(tempfilename);
@@ -2028,7 +2028,7 @@ void receive_spool(int *sock, char *remote_nodename) {
        fclose(fp);
 
        /* Last chance for shutdown exit */
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
        {
                unlink(tempfilename);
                return;
@@ -2099,7 +2099,7 @@ void transmit_spool(int *sock, char *remote_nodename)
                bytes_to_write = plen;
                while (bytes_to_write > 0L) {
                        /* Exit if shutting down */
-                       if (CtdlThreadCheckStop())
+                       if (server_shutting_down)
                        {
                                close(fd);
                                return;
@@ -2132,7 +2132,7 @@ ABORTUPL:
        close(fd);
 
        /* Last chance for shutdown exit */
-       if(CtdlThreadCheckStop())
+       if(server_shutting_down)
                return;
                
        if (sock_puts(sock, "UCLS 1") < 0) return;
@@ -2205,9 +2205,9 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
                }
        
                /* At this point we are authenticated. */
-               if (!CtdlThreadCheckStop())
+               if (!server_shutting_down)
                        receive_spool(&sock, node);
-               if (!CtdlThreadCheckStop())
+               if (!server_shutting_down)
                        transmit_spool(&sock, node);
        }
 
@@ -2244,7 +2244,7 @@ void network_poll_other_citadel_nodes(int full_poll) {
 
        /* Use the string tokenizer to grab one line at a time */
        for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
-               if(CtdlThreadCheckStop())
+               if(server_shutting_down)
                        return;
                extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
                extract_token(node, linebuf, 0, '|', sizeof node);
@@ -2354,14 +2354,14 @@ void network_do_queue(void) {
        /* 
         * Go ahead and run the queue
         */
-       if (full_processing && !CtdlThreadCheckStop()) {
+       if (full_processing && !server_shutting_down) {
                syslog(LOG_DEBUG, "network: loading outbound queue\n");
                CtdlForEachRoom(network_queue_room, NULL);
        }
 
        if (rplist != NULL) {
                syslog(LOG_DEBUG, "network: running outbound queue\n");
-               while (rplist != NULL && !CtdlThreadCheckStop()) {
+               while (rplist != NULL && !server_shutting_down) {
                        char spoolroomname[ROOMNAMELEN];
                        safestrncpy(spoolroomname, rplist->name, sizeof spoolroomname);
                        begin_critical_section(S_RPLIST);
@@ -2386,7 +2386,7 @@ void network_do_queue(void) {
        }
 
        /* If there is anything in the inbound queue, process it */
-       if (!CtdlThreadCheckStop()) {
+       if (!server_shutting_down) {
                network_do_spoolin();
        }
 
index 83ddf64cce38b7a33f2791951712a794fb27fcd9..6adaf154981f8c3b65335bb6074ea3a1b3ec9dd6 100644 (file)
@@ -174,6 +174,7 @@ eNextState FailAggregationRun(AsyncIO *IO)
        return eAbort;
 }
 
+
 #define POP3C_DBG_SEND() syslog(LOG_DEBUG, "POP3 client[%ld]: > %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.SendBuf.Buf))
 #define POP3C_DBG_READ() syslog(LOG_DEBUG, "POP3 client[%ld]: < %s\n", RecvMsg->n, ChrPtr(RecvMsg->IO.IOBuf))
 #define POP3C_OK (strncasecmp(ChrPtr(RecvMsg->IO.IOBuf), "+OK", 3) == 0)
@@ -186,7 +187,6 @@ eNextState POP3C_ReadGreeting(pop3aggr *RecvMsg)
        else return eSendReply;
 }
 
-
 eNextState POP3C_SendUser(pop3aggr *RecvMsg)
 {
        /* Identify ourselves.  NOTE: we have to append a CR to each command.  The LF will
@@ -301,7 +301,9 @@ eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
        if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
        {
                struct UseTable ut;
-
+               if (server_shutting_down)
+                       return eAbort;
+                       
                RecvMsg->CurrMsg = (FetchItem*) vData;
                syslog(LOG_DEBUG, "CHECKING: whether %s has already been seen: ", ChrPtr(RecvMsg->CurrMsg->MsgUID));
                /* Find out if we've already seen this item */
@@ -890,13 +892,15 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
                              qrbuf->QRnumber, 
                              qrbuf->QRname);
                pthread_mutex_unlock(&POP3QueueMutex);
-               return;
        }
        pthread_mutex_unlock(&POP3QueueMutex);
 
+       if (server_shutting_down)
+               return;
+
        assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
 
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
                
        /* Only do net processing for rooms that have netconfigs */
@@ -905,14 +909,14 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
                //syslog(LOG_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname);
                return;
        }
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
        if (fstat(fd, &statbuf) == -1) {
                syslog(LOG_DEBUG, "ERROR: could not stat configfile '%s' - %s\n",
                              filename, strerror(errno));
                return;
        }
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
        CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
        if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
@@ -923,7 +927,7 @@ void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
                return;
        }
        close(fd);
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
        
        CfgPtr = NULL;
@@ -1062,15 +1066,25 @@ void pop3client_scan(void) {
        syslog(LOG_DEBUG, "pop3client started\n");
        CtdlForEachRoom(pop3client_scan_room, NULL);
 
-
        pthread_mutex_lock(&POP3QueueMutex);
        it = GetNewHashPos(POP3FetchUrls, 0);
-       while (GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) && 
+       while (!server_shutting_down && 
+              GetNextHashPos(POP3FetchUrls, it, &len, &Key, &vrptr) && 
               (vrptr != NULL)) {
                cptr = (pop3aggr *)vrptr;
                if (cptr->RefCount == 0) 
                        if (!pop3_do_fetching(cptr))
                                DeletePOP3Aggregator(cptr);////TODO
+
+/*
+               if ((palist->interval && time(NULL) > (last_run + palist->interval))
+                       || (time(NULL) > last_run + config.c_pop3_fetch))
+                               pop3_do_fetching(palist->roomname, palist->pop3host,
+                                       palist->pop3user, palist->pop3pass, palist->keep);
+               pptr = palist;
+               palist = palist->next;
+               free(pptr);
+*/
        }
        DeleteHashPos(&it);
        pthread_mutex_unlock(&POP3QueueMutex);
index 7b49ec5a6e5823f2f0de70a6dee697400d45c2d4..67c325a476b0f267333dd54d50ae47ae561ac9f1 100644 (file)
@@ -137,6 +137,9 @@ void UnlinkRooms(rss_aggregator *Cfg)
                        long *lData = (long*) vData;
                        DeleteRoomReference(*lData);
                }
+/*
+               if (server_shutting_down)
+                       break; /* TODO */
 
                DeleteHashPos(&At);
        }
@@ -451,7 +454,6 @@ int rss_do_fetching(rss_aggregator *Cfg)
 }
 
 
-
 void DeleteRssCfg(void *vptr)
 {
        rss_aggregator *rncptr = (rss_aggregator *)vptr;
@@ -545,7 +547,7 @@ void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
 
        assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
 
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
                
        /* Only do net processing for rooms that have netconfigs */
@@ -554,15 +556,10 @@ void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
                //syslog(LOG_DEBUG, "rssclient: %s no config.\n", qrbuf->QRname);
                return;
        }
-       if (CtdlThreadCheckStop())
-               return;
-       if (fstat(fd, &statbuf) == -1) {
-               syslog(LOG_DEBUG, "ERROR: could not stat configfile '%s' - %s\n",
-                       filename, strerror(errno));
-               return;
-       }
-       if (CtdlThreadCheckStop())
-               return;
+
+       if (server_shutting_down)
+               return
+
        CfgData = NewStrBufPlain(NULL, statbuf.st_size + 1);
        if (StrBufReadBLOB(CfgData, &fd, 1, statbuf.st_size, &Err) < 0) {
                close(fd);
@@ -572,7 +569,7 @@ void rssclient_scan_room(struct ctdlroom *qrbuf, void *data)
                return;
        }
        close(fd);
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                return;
        
        CfgPtr = NULL;
@@ -686,7 +683,8 @@ void rssclient_scan(void) {
        pthread_mutex_lock(&RSSQueueMutex);
 
        it = GetNewHashPos(RSSFetchUrls, 0);
-       while (GetNextHashPos(RSSFetchUrls, it, &len, &Key, &vrptr) && 
+       while (!server_shutting_down &&
+              GetNextHashPos(RSSFetchUrls, it, &len, &Key, &vrptr) && 
               (vrptr != NULL)) {
                rptr = (rss_aggregator *)vrptr;
                if (rptr->RefCount == 0) 
index e2d9766a68826c900bb42f0431b26137b9c1c5bf..885bf950f1193111294a3ddaa515868a2910fb93 100644 (file)
@@ -131,7 +131,7 @@ void smtp_greeting(int is_msa)
         */
        if ( (config.c_rbl_at_greeting) && (sSMTP->is_msa == 0) ) {
                if (rbl_check(message_to_spammer)) {
-                       if (CtdlThreadCheckStop())
+                       if (server_shutting_down)
                                cprintf("421 %s\r\n", message_to_spammer);
                        else
                                cprintf("550 %s\r\n", message_to_spammer);
@@ -584,7 +584,7 @@ void smtp_rcpt(char *argbuf) {
           && (!sSMTP->is_lmtp) ) {     /* Don't RBL LMTP clients */
                if (config.c_rbl_at_greeting == 0) {    /* Don't RBL again if we already did it */
                        if (rbl_check(message_to_spammer)) {
-                               if (CtdlThreadCheckStop())
+                               if (server_shutting_down)
                                        cprintf("421 %s\r\n", message_to_spammer);
                                else
                                        cprintf("550 %s\r\n", message_to_spammer);
index 0bc442f57e1fe77bddc54d6dd8f849e904e85948..6b038bec12556e3f42664af651c0a0b2dc4ba344 100644 (file)
@@ -139,7 +139,7 @@ int LookupUrl(StrBuf *ShorterUrlStr)
        OPT(WRITEHEADER, ShorterUrlStr);
 
 
-       if (CtdlThreadCheckStop())
+       if (server_shutting_down)
                goto shutdown ;
 
        evcurl_handle_start(IO);
index ee75bb9cba43be769faad5ded733dffbc7001f46..ea89f5ac5e9e3865be37c6c8111f6a0d1a61030d 100644 (file)
@@ -101,7 +101,7 @@ volatile int running_as_daemon = 0;
 static RETSIGTYPE signal_cleanup(int signum) {
        syslog(LOG_DEBUG, "Caught signal %d; shutting down.", signum);
        exit_signal = signum;
-///    server_shutting_down = 1;
+       server_shutting_down = 1;
 }
 
 static RETSIGTYPE signal_exit(int signum) {
@@ -485,7 +485,7 @@ int client_write(const char *buf, int nbytes)
                                        syslog(LOG_DEBUG, "client_write(%d bytes) select() interrupted.",
                                                nbytes-bytes_written
                                        );
-                                       if (CtdlThreadCheckStop()) {
+                                       if (server_shutting_down) {
                                                CC->kill_me = KILLME_SELECT_INTERRUPTED;
                                                return (-1);
                                        } else {
@@ -1145,7 +1145,7 @@ void *worker_thread(void *blah) {
 
        ++num_workers;
 
-       while (!CtdlThreadCheckStop()) {
+       while (!server_shutting_down) {
 
                /* make doubly sure we're not holding any stale db handles
                 * which might cause a deadlock.
@@ -1194,7 +1194,7 @@ do_select:        force_purge = 0;
                 * ahead and get ready to select().
                 */
 
-               if (!CtdlThreadCheckStop()) {
+               if (!server_shutting_down) {
                        tv.tv_sec = 1;          /* wake up every second if no input */
                        tv.tv_usec = 0;
                        retval = select(highest + 1, &readfds, NULL, NULL, &tv);
@@ -1213,18 +1213,18 @@ do_select:      force_purge = 0;
                        }
                        if (errno != EINTR) {
                                syslog(LOG_EMERG, "Exiting (%s)\n", strerror(errno));
-                               CtdlThreadStopAll();
+                               server_shutting_down = 1;
                                continue;
                        } else {
 #if 0
                                syslog(LOG_DEBUG, "Interrupted select()\n");
 #endif
-                               if (CtdlThreadCheckStop()) return(NULL);
+                               if (server_shutting_down) return(NULL);
                                goto do_select;
                        }
                }
                else if (retval == 0) {
-                       if (CtdlThreadCheckStop()) return(NULL);
+                       if (server_shutting_down) return(NULL);
                }
 
                /* It must be a client socket.  Find a context that has data
@@ -1297,7 +1297,9 @@ SKIP_SELECT:
                do_housekeeping();
                --active_workers;
        }
-       /* If control reaches this point, the server is shutting down */        
+
+       /* If control reaches this point, the server is shutting down */
+       --num_workers;
        return(NULL);
 }
 
@@ -1325,7 +1327,7 @@ void *select_on_master(void *blah)
        CtdlFillSystemContext(&select_on_master_CC, "select_on_master");
        pthread_setspecific(MyConKey, (void *)&select_on_master_CC);
 
-       while (!CtdlThreadCheckStop()) {
+       while (!server_shutting_down) {
                /* Initialize the fdset. */
                FD_ZERO(&master_fds);
                highest = 0;
@@ -1340,7 +1342,7 @@ void *select_on_master(void *blah)
                        }
                }
 
-               if (!CtdlThreadCheckStop()) {
+               if (!server_shutting_down) {
                        tv.tv_sec = 60;         /* wake up every second if no input */
                        tv.tv_usec = 0;
                        retval = select(highest + 1, &master_fds, NULL, NULL, &tv);
@@ -1359,17 +1361,17 @@ void *select_on_master(void *blah)
                        }
                        if (errno != EINTR) {
                                syslog(LOG_EMERG, "Exiting (%s)\n", strerror(errno));
-                               CtdlThreadStopAll();
+                               server_shutting_down = 1;
                        } else {
 #if 0
                                syslog(LOG_DEBUG, "Interrupted CtdlThreadSelect.\n");
 #endif
-                               if (CtdlThreadCheckStop()) return(NULL);
+                               if (server_shutting_down) return(NULL);
                                continue;
                        }
                }
                else if(retval == 0) {
-                       if (CtdlThreadCheckStop()) return(NULL);
+                       if (server_shutting_down) return(NULL);
                        continue;
                }
                /* Next, check to see if it's a new client connecting
index 0d3caed9f24043e5a46bef32238ad9b76e9c4494..965f866da2c31c44000988c8b3caf484c52d4aa3 100644 (file)
@@ -67,6 +67,7 @@ int active_workers = 0;                               /* Number of ACTIVE worker threads */
 pthread_key_t ThreadKey;
 pthread_mutex_t Critters[MAX_SEMAPHORES];      /* Things needing locking */
 struct thread_tsd masterTSD;
+int server_shutting_down = 0;                  /* set to nonzero during shutdown */
 
 
 
@@ -128,27 +129,6 @@ void end_critical_section(int which_one)
 }
 
 
-/*
- * A function to tell all threads to exit
- */
-void CtdlThreadStopAll(void)
-{
-       terminate_all_sessions();               /* close all client sockets */
-       CtdlShutdownServiceHooks();             /* close all listener sockets to prevent new connections */
-       PerformSessionHooks(EVT_SHUTDOWN);      /* run any registered shutdown hooks */
-}
-
-
-/*
- * A function for a thread to check if it has been asked to stop
- */
-int CtdlThreadCheckStop(void)
-{
-
-       /* FIXME this needs to do something useful.  moar code pls ! */
-
-       return 0;
-}
 
 
 /*
@@ -224,16 +204,22 @@ void go_threading(void)
        /* The supervisor thread monitors worker threads and spawns more of them if it finds that
         * they are all in use.  FIXME make the 256 max threads a configurable value.
         */
-       while(!CtdlThreadCheckStop()) {
+       while (!server_shutting_down) {
                if ((active_workers == num_workers) && (num_workers < 256)) {
-                       syslog(LOG_DEBUG, "worker threads: %d, active: %d\n", num_workers, active_workers);
                        CtdlThreadCreate(worker_thread);
-                       syslog(LOG_DEBUG, "worker threads: %d, active: %d\n", num_workers, active_workers);
                }
                sleep(1);
        }
 
-       /* Shut down */
-       CtdlThreadStopAll();
-       exit(0);
+       /* When we get to this point we are getting ready to shut down our Citadel server */
+
+       terminate_all_sessions();               /* close all client sockets */
+       CtdlShutdownServiceHooks();             /* close all listener sockets to prevent new connections */
+       PerformSessionHooks(EVT_SHUTDOWN);      /* run any registered shutdown hooks */
+
+       int countdown = 10;
+       while ( (num_workers > 0) && (countdown-- > 0)) {
+               syslog(LOG_DEBUG, "Waiting for %d worker threads to exit", num_workers);
+               sleep(1);
+       }
 }
index f96ab77d5bbc0c9215a2745aee9e72f8db81c278..2119e02395ce816186c59359f81c169ec44318f7 100644 (file)
@@ -35,14 +35,13 @@ extern struct thread_tsd masterTSD;
 
 extern int num_workers;
 extern int active_workers;
+extern int server_shutting_down;
 
 struct thread_tsd *MyThread(void);
 int try_critical_section (int which_one);
 void begin_critical_section (int which_one);
 void end_critical_section (int which_one);
 void go_threading(void);
-int CtdlThreadCheckStop(void);
-void CtdlThreadStopAll(void);
 void InitializeMasterTSD(void);
 void CtdlThreadCreate(void *(*start_routine)(void*));