Attempting to fix a segfault generated in Removecontext()
[citadel.git] / citadel / context.c
index c6575d3dba1e0194be7b21e6045a355be98823fc..181d0347c87db600a9862d947130a4511fca7b13 100644 (file)
@@ -284,22 +284,25 @@ void terminate_idle_sessions(void)
        session_to_kill = 0;
        begin_critical_section(S_SESSION_TABLE);
        for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
-               if (  (ccptr!=CC)
-               && (config.c_sleeping > 0)
-               && (now - (ccptr->lastcmd) > config.c_sleeping) ) {
+               if (
+                       (ccptr != CC)
+                       && (config.c_sleeping > 0)
+                       && (now - (ccptr->lastcmd) > config.c_sleeping)
+               ) {
                        if (!ccptr->dont_term) {
                                ccptr->kill_me = KILLME_IDLE;
                                ++killed;
                        }
-                       else 
-                               longrunners ++;
+                       else {
+                               ++longrunners;
+                       }
                }
        }
        end_critical_section(S_SESSION_TABLE);
        if (killed > 0)
                syslog(LOG_INFO, "Scheduled %d idle sessions for termination\n", killed);
        if (longrunners > 0)
-               syslog(LOG_INFO, "Didn't terminate %d protected idle sessions;\n", killed);
+               syslog(LOG_INFO, "Didn't terminate %d protected idle sessions", longrunners);
 }
 
 
@@ -335,15 +338,16 @@ void terminate_all_sessions(void)
 void RemoveContext (CitContext *con)
 {
        const char *c;
-       if (con==NULL) {
-               syslog(LOG_ERR, "WARNING: RemoveContext() called with NULL!\n");
+       if (con == NULL) {
+               syslog(LOG_ERR, "WARNING: RemoveContext() called with NULL!");
                return;
        }
        c = con->ServiceName;
-       if (c == NULL)
+       if (c == NULL) {
                c = "WTF?";
-       syslog(LOG_DEBUG, "RemoveContext(%s) session %d\n", c, con->cs_pid);
-       cit_backtrace ();
+       }
+       syslog(LOG_DEBUG, "RemoveContext(%s) session %d", c, con->cs_pid);
+       cit_backtrace();
 
        /* Run any cleanup routines registered by loadable modules.
         * Note: We have to "become_session()" because the cleanup functions
@@ -352,12 +356,20 @@ void RemoveContext (CitContext *con)
        become_session(con);
        CtdlUserLogout();
        PerformSessionHooks(EVT_STOP);
+       client_close();                         /* If the client is still connected, blow 'em away. */
        become_session(NULL);
 
-       syslog(LOG_NOTICE, "[%3d] Session ended.\n", con->cs_pid);
+       syslog(LOG_NOTICE, "[%3d] Session ended.", con->cs_pid);
 
-       /* If the client is still connected, blow 'em away. */
-       client_close();
+       /* 
+        * If the client is still connected, blow 'em away. 
+        * if the socket is 0 or -1, its already gone or was never there.
+        */
+       if (con->client_socket > 0)
+       {
+               syslog(LOG_NOTICE, "Closing socket %d", con->client_socket);
+               close(con->client_socket);
+       }
 
        /* If using AUTHMODE_LDAP, free the DN */
        if (con->ldap_dn) {
@@ -371,7 +383,7 @@ void RemoveContext (CitContext *con)
                free(con->cached_msglist);
        }
 
-       syslog(LOG_DEBUG, "Done with RemoveContext()\n");
+       syslog(LOG_DEBUG, "Done with RemoveContext()");
 }
 
 
@@ -549,16 +561,6 @@ void CtdlFillSystemContext(CitContext *context, char *name)
        }
 }
 
-/*
- * flush it again...
- */
-void CtdlClearSystemContext(void)
-{
-       CitContext *CCC = MyContext();
-
-       memset(CCC, 0, sizeof(CitContext));
-       pthread_setspecific(MyConKey, NULL);
-}
 
 /*
  * Cleanup any contexts that are left lying around