CURL: make log output configurable.
[citadel.git] / citadel / context.c
index f49d645dd86c9c51e5fb6ba2c96afe0f0cda9f4e..7993ce08c930b27b3d40f3002eb197b5b56eb5da 100644 (file)
@@ -2,7 +2,7 @@
  * Citadel context management stuff.
  * Here's where we (hopefully) have all the code that manipulates contexts.
  *
- * Copyright (c) 1987-2010 by the citadel.org team
+ * Copyright (c) 1987-2011 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -86,7 +86,7 @@
 
 
 
-citthread_key_t MyConKey;                              /* TSD key for MyContext() */
+pthread_key_t MyConKey;                                /* TSD key for MyContext() */
 
 
 CitContext masterCC;
@@ -259,12 +259,8 @@ int CtdlIsUserLoggedInByNum (long usernum)
  * This function is used *VERY* frequently and must be kept small.
  */
 CitContext *MyContext(void) {
-
        register CitContext *c;
-
-       return ((c = (CitContext *) citthread_getspecific(MyConKey),
-               c == NULL) ? &masterCC : c
-       );
+       return ((c = (CitContext *) pthread_getspecific(MyConKey), c == NULL) ? &masterCC : c);
 }
 
 
@@ -280,46 +276,39 @@ void terminate_idle_sessions(void)
 {
        CitContext *ccptr;
        time_t now;
-       int session_to_kill;
        int killed = 0;
        int longrunners = 0;
 
        now = time(NULL);
-       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);
 }
 
 
 /*
  * During shutdown, close the sockets of any sessions still connected.
  */
-void terminate_stuck_sessions(void)
+void terminate_all_sessions(void)
 {
-
-       abort();
-       
-       /* FIXME this function has been disabled because it is somehow being
-        * called at times other than server shutdown, which is throwing all
-        * the users off.  EPIC FAIL!!!
-        */
-
        CitContext *ccptr;
        int killed = 0;
 
@@ -327,7 +316,7 @@ void terminate_stuck_sessions(void)
        for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
                if (ccptr->client_socket != -1)
                {
-                       syslog(LOG_INFO, "terminate_stuck_sessions() is murdering %s", ccptr->curr_user);
+                       syslog(LOG_INFO, "terminate_all_sessions() is murdering %s", ccptr->curr_user);
                        close(ccptr->client_socket);
                        ccptr->client_socket = -1;
                        killed++;
@@ -347,15 +336,16 @@ void terminate_stuck_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
@@ -364,31 +354,38 @@ 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) {
                free(con->ldap_dn);
                con->ldap_dn = NULL;
        }
-
+       FreeStrBuf(&con->StatusMessage);
        FreeStrBuf(&con->MigrateBuf);
        FreeStrBuf(&con->RecvBuf.Buf);
        if (con->cached_msglist) {
                free(con->cached_msglist);
        }
 
-       syslog(LOG_DEBUG, "Done with RemoveContext()\n");
+       syslog(LOG_DEBUG, "Done with RemoveContext()");
 }
 
 
 
-
 /*
  * Initialize a new context and place it in the list.  The session number
  * used to be the PID (which is why it's called cs_pid), but that was when we
@@ -405,7 +402,8 @@ CitContext *CreateNewContext(void) {
                return NULL;
        }
        memset(me, 0, sizeof(CitContext));
-       /* Give the contaxt a name. Hopefully makes it easier to track */
+
+       /* Give the context a name. Hopefully makes it easier to track */
        strcpy (me->user.fullname, "SYS_notauth");
        
        /* The new context will be created already in the CON_EXECUTING state
@@ -418,12 +416,16 @@ CitContext *CreateNewContext(void) {
         * the list.
         */
        me->MigrateBuf = NewStrBuf();
+
        me->RecvBuf.Buf = NewStrBuf();
+
+       me->lastcmd = time(NULL);       /* set lastcmd to now to prevent idle timer infanticide TODO: if we have a valid IO, use that to set the timer. */
+
+
        begin_critical_section(S_SESSION_TABLE);
        me->cs_pid = ++next_pid;
        me->prev = NULL;
        me->next = ContextList;
-       me->lastcmd = time(NULL);       /* set lastcmd to now to prevent idle timer infanticide */
        ContextList = me;
        if (me->next != NULL) {
                me->next->prev = me;
@@ -557,16 +559,6 @@ void CtdlFillSystemContext(CitContext *context, char *name)
        }
 }
 
-/*
- * flush it again...
- */
-void CtdlClearSystemContext(void)
-{
-       CitContext *CCC = MyContext();
-
-       memset(CCC, 0, sizeof(CitContext));
-       citthread_setspecific(MyConKey, NULL);
-}
 
 /*
  * Cleanup any contexts that are left lying around
@@ -594,7 +586,7 @@ void context_cleanup(void)
                rem = ptr->next;
                --num_sessions;
 
-               syslog(LOG_DEBUG, "context_cleanup(): Purging session #%d %s\n", ptr->cs_pid, ptr->ServiceName);
+               syslog(LOG_DEBUG, "context_cleanup(): purging session %d\n", ptr->cs_pid);
                RemoveContext(ptr);
                free (ptr);
                ptr = rem;
@@ -671,7 +663,7 @@ void dead_session_purge(int force) {
  * function initializes it.
  */
 void InitializeMasterCC(void) {
-       memset(&masterCC, 0, sizeof( CitContext));
+       memset(&masterCC, 0, sizeof(struct CitContext));
        masterCC.internal_pgm = 1;
        masterCC.cs_pid = 0;
 }