]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
remove one more systemcontext
[citadel.git] / citadel / sysdep.c
index c3bbd7ad653860b993e487b620e3471511edccd1..31a9c7ba150b732ffd594a8ae037aacd7dde9960 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 {
@@ -583,7 +583,7 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
 #endif
                retval = client_read_sslblob(Target, bytes, timeout);
                if (retval < 0) {
-                       syslog(LOG_CRIT, "%s failed", __FUNCTION__);
+                       syslog(LOG_CRIT, "client_read_blob() failed");
                }
 #ifdef BIGBAD_IODBG
                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
@@ -626,7 +626,7 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
                                                O_TERM,
                                                &Error);
                if (retval < 0) {
-                       syslog(LOG_CRIT, "%s failed: %s\n", __FUNCTION__, Error);
+                       syslog(LOG_CRIT, "client_read_blob() failed: %s", Error);
                        client_close();
                        return retval;
                }
@@ -806,10 +806,9 @@ int CtdlClientGetLine(StrBuf *Target)
                         StrLength(Target), ChrPtr(Target));
                 fclose(fd);
 
-               if (rc < 0)
-                       syslog(LOG_CRIT, 
-                                     "%s failed\n",
-                                     __FUNCTION__);
+               if (rc < 0) {
+                       syslog(LOG_CRIT, "CtdlClientGetLine() failed");
+               }
 #endif
                return rc;
        }
@@ -864,11 +863,9 @@ int CtdlClientGetLine(StrBuf *Target)
                         StrLength(Target), ChrPtr(Target));
                 fclose(fd);
 
-               if ((rc < 0) && (Error != NULL))
-                       syslog(LOG_CRIT, 
-                                     "%s failed: %s\n",
-                                     __FUNCTION__,
-                                     Error);
+               if ((rc < 0) && (Error != NULL)) {
+                       syslog(LOG_CRIT, "CtdlClientGetLine() failed: %s", Error);
+               }
 #endif
                return rc;
        }
@@ -1143,7 +1140,9 @@ void *worker_thread(void *blah) {
        struct timeval tv;
        int force_purge = 0;
 
-       while (!CtdlThreadCheckStop()) {
+       ++num_workers;
+
+       while (!server_shutting_down) {
 
                /* make doubly sure we're not holding any stale db handles
                 * which might cause a deadlock.
@@ -1192,12 +1191,13 @@ 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);
                }
                else {
+                       --num_workers;
                        return NULL;
                }
 
@@ -1211,18 +1211,24 @@ 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) {
+                                       --num_workers;
+                                       return(NULL);
+                               }
                                goto do_select;
                        }
                }
                else if (retval == 0) {
-                       if (CtdlThreadCheckStop()) return(NULL);
+                       if (server_shutting_down) {
+                               --num_workers;
+                               return(NULL);
+                       }
                }
 
                /* It must be a client socket.  Find a context that has data
@@ -1259,6 +1265,7 @@ do_select:        force_purge = 0;
 
 SKIP_SELECT:
                /* We're bound to a session */
+               ++active_workers;
                if (bind_me != NULL) {
                        become_session(bind_me);
 
@@ -1292,8 +1299,11 @@ SKIP_SELECT:
 
                dead_session_purge(force_purge);
                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);
 }
 
@@ -1316,12 +1326,8 @@ void *select_on_master(void *blah)
        int m;
        int i;
        int retval;
-       struct CitContext select_on_master_CC;
-
-       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;
@@ -1336,13 +1342,14 @@ 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);
                }
-               else
-                       return NULL;
+               else {
+                       retval = -1 ;
+               }
 
                /* Now figure out who made this select() unblock.
                 * First, check for an error or exit condition.
@@ -1355,24 +1362,20 @@ 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);
-                       continue;
-               }
+
                /* Next, check to see if it's a new client connecting
                 * on a master socket.
                 */
-               else for (serviceptr = ServiceHookTable; serviceptr != NULL;
-                    serviceptr = serviceptr->next ) {
+               else if ((retval > 0) && (!server_shutting_down)) for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next) {
 
                        if (FD_ISSET(serviceptr->msock, &master_fds)) {
                                ssock = accept(serviceptr->msock, NULL, 0);
@@ -1417,8 +1420,6 @@ void *select_on_master(void *blah)
                        }
                }
        }
-       CtdlClearSystemContext();
-
        return NULL;
 }