From: Miroslav Spousta <[email protected]>

When worker is required to shut down (by SIGHUP/SIGINT), it waits for all busy
threads to finish. Number of running threads is monitored every 10 seconds.
During the shutdown, control socket is temporary renamed (pid is appended to
the socket name), so that it is still accessible and correct socket is removed
on exit.
---
 config.c |  7 +------
 pound.8  | 10 ----------
 pound.c  | 35 ++++++++++++++++++++++++++++++-----
 pound.h  |  1 -
 4 files changed, 31 insertions(+), 22 deletions(-)
 mode change 100644 => 100755 config.c
 mode change 100644 => 100755 pound.8
 mode change 100644 => 100755 pound.c

diff --git a/config.c b/config.c
old mode 100644
new mode 100755
index ae45991..c0f5437
--- a/config.c
+++ b/config.c
@@ -79,7 +79,7 @@ static regex_t  Err414, Err500, Err501, Err503, MaxRequest, 
HeadRemove, RewriteL
 static regex_t  Service, ServiceName, URL, HeadRequire, HeadDeny, BackEnd, 
Emergency, Priority, HAport, HAportAddr;
 static regex_t  Redirect, RedirectN, TimeOut, Session, Type, TTL, ID, DynScale;
 static regex_t  ClientCert, AddHeader, DisableSSLv2, 
SSLAllowClientRenegotiation, SSLHonorCipherOrder, Ciphers;
-static regex_t  CAlist, VerifyList, CRLlist, NoHTTPS11, Grace, Include, 
ConnTO, IgnoreCase, HTTPS, HTTPSCert;
+static regex_t  CAlist, VerifyList, CRLlist, NoHTTPS11, Include, ConnTO, 
IgnoreCase, HTTPS, HTTPSCert;
 static regex_t  Disabled, Threads, CNName, Anonymise;
 
 static regmatch_t   matches[5];
@@ -1273,8 +1273,6 @@ parse_file(void)
                         def_facility = facilitynames[i].c_val;
                         break;
                     }
-        } else if(!regexec(&Grace, lin, 4, matches, 0)) {
-            grace = atoi(lin + matches[1].rm_so);
         } else if(!regexec(&LogLevel, lin, 4, matches, 0)) {
             log_level = atoi(lin + matches[1].rm_so);
         } else if(!regexec(&Client, lin, 4, matches, 0)) {
@@ -1373,7 +1371,6 @@ config_parse(const int argc, char **const argv)
     || regcomp(&Threads, "^[ \t]*Threads[ \t]+([1-9][0-9]*)[ \t]*$", REG_ICASE 
| REG_NEWLINE | REG_EXTENDED)
     || regcomp(&LogFacility, "^[ \t]*LogFacility[ \t]+([a-z0-9-]+)[ \t]*$", 
REG_ICASE | REG_NEWLINE | REG_EXTENDED)
     || regcomp(&LogLevel, "^[ \t]*LogLevel[ \t]+([0-5])[ \t]*$", REG_ICASE | 
REG_NEWLINE | REG_EXTENDED)
-    || regcomp(&Grace, "^[ \t]*Grace[ \t]+([0-9]+)[ \t]*$", REG_ICASE | 
REG_NEWLINE | REG_EXTENDED)
     || regcomp(&Alive, "^[ \t]*Alive[ \t]+([1-9][0-9]*)[ \t]*$", REG_ICASE | 
REG_NEWLINE | REG_EXTENDED)
     || regcomp(&SSLEngine, "^[ \t]*SSLEngine[ \t]+\"(.+)\"[ \t]*$", REG_ICASE 
| REG_NEWLINE | REG_EXTENDED)
     || regcomp(&Control, "^[ \t]*Control[ \t]+\"(.+)\"[ \t]*$", REG_ICASE | 
REG_NEWLINE | REG_EXTENDED)
@@ -1509,7 +1506,6 @@ config_parse(const int argc, char **const argv)
     numthreads = 128;
     alive_to = 30;
     daemonize = 1;
-    grace = 30;
 
     services = NULL;
     listeners = NULL;
@@ -1535,7 +1531,6 @@ config_parse(const int argc, char **const argv)
     regfree(&Threads);
     regfree(&LogFacility);
     regfree(&LogLevel);
-    regfree(&Grace);
     regfree(&Alive);
     regfree(&SSLEngine);
     regfree(&Control);
diff --git a/pound.8 b/pound.8
old mode 100644
new mode 100755
index bda2fd8..08c6a22
--- a/pound.8
+++ b/pound.8
@@ -296,16 +296,6 @@ wait for a connection to the back-end (in seconds). 
Default: the
 .B TimeOut
 value. This value can be overridden for specific back-ends.
 .TP
-\fBGrace\fR value
-How long should
-.B Pound
-continue to answer existing connections after a receiving and INT or HUP
-signal (default: 30 seconds). The configured listeners are closed
-immediately. You can bypass this behaviour by stopping
-.B Pound
-with a TERM or QUIT signal, in which case the program exits without any
-delay.
-.TP
 \fBSSLEngine\fR "name"
 Use an OpenSSL hardware acceleration card called \fIname\fR. Available
 only if OpenSSL-engine is installed on your system.
diff --git a/pound.c b/pound.c
old mode 100644
new mode 100755
index d008468..a047b96
--- a/pound.c
+++ b/pound.c
@@ -27,6 +27,9 @@
 
 #include    "pound.h"
 
+/* while in shutdown, check number of running threads every 10 seconds */
+#define     RUNNING_CHECK_PERIOD 10
+
 /* common variables */
 char        *user,              /* user to run as */
             *group,             /* group to run as */
@@ -39,7 +42,6 @@ int         alive_to,           /* check interval for 
resurrection */
             daemonize,          /* run as daemon */
             log_facility,       /* log facility to use */
             print_log,          /* print log messages to stdout/stderr */
-            grace,              /* grace period before shutdown */
             control_sock;       /* control socket */
 
 SERVICE     *services;          /* global services (if any) */
@@ -112,6 +114,7 @@ static thr_arg          *first = NULL, *last = NULL;
 static pthread_cond_t   arg_cond;
 static pthread_mutex_t  arg_mut;
 int                     numthreads;
+static int              waiting = 0;
 
 static void
 init_thr_arg(void)
@@ -156,8 +159,10 @@ get_thr_arg(void)
     thr_arg *res;
 
     (void)pthread_mutex_lock(&arg_mut);
+    waiting++;
     while(first == NULL)
         (void)pthread_cond_wait(&arg_cond, &arg_mut);
+    waiting--;
     if((res = first) != NULL)
         if((first = first->next) == NULL)
             last = NULL;
@@ -486,13 +491,33 @@ main(const int argc, char **argv)
             /* and start working */
             for(;;) {
                 if(shut_down) {
-                    logmsg(LOG_NOTICE, "shutting down...");
+                    int finished;
+
+                    logmsg(LOG_NOTICE, "shutting down (%d)...", getpid());
                     for(lstn = listeners; lstn; lstn = lstn->next)
                         close(lstn->sock);
-                    if(grace > 0) {
-                        sleep(grace);
-                        logmsg(LOG_NOTICE, "grace period expired - 
exiting...");
+                    /* rename control file (append pid) */
+                    if(ctrl_name != NULL) {
+                        char *ctrl_tmp = malloc(strlen(ctrl_name)+11);
+                        sprintf(ctrl_tmp, "%s.%d", ctrl_name, getpid());
+                        rename(ctrl_name, ctrl_tmp);
+                        free(ctrl_name);
+                        ctrl_name = ctrl_tmp;
+                    }
+                    /* wait for all threads to be finished */
+                    finished = 0;
+                    while(!finished) {
+                        int running;
+                        (void)pthread_mutex_lock(&arg_mut);
+                        running = numthreads-waiting;
+                        finished = !first && !running;
+                        (void)pthread_mutex_unlock(&arg_mut);
+                        if(!finished) {
+                            logmsg(LOG_INFO, "%d thread(s) still running...", 
running);
+                            sleep(RUNNING_CHECK_PERIOD);
+                        }
                     }
+                    logmsg(LOG_NOTICE, "no threads running - exiting...");
                     if(ctrl_name != NULL)
                         (void)unlink(ctrl_name);
                     exit(0);
diff --git a/pound.h b/pound.h
index 4c18763..fe37080 100644
--- a/pound.h
+++ b/pound.h
@@ -272,7 +272,6 @@ extern int  numthreads,         /* number of worker threads 
*/
             daemonize,          /* run as daemon */
             log_facility,       /* log facility to use */
             print_log,          /* print log messages to stdout/stderr */
-            grace,              /* grace period before shutdown */
             control_sock;       /* control socket */
 
 extern regex_t  HEADER,     /* Allowed header */
-- 
1.8.3.1


--
To unsubscribe send an email with subject unsubscribe to [email protected].
Please contact [email protected] for questions.

Reply via email to