]> code.citadel.org Git - citadel.git/blobdiff - citadel/context.c
Fix warnings all over citserver; handle function replies; remove unused code.
[citadel.git] / citadel / context.c
index db2a136f93e5e3bdf415f7c658fecd150f9514b7..695b2a0a7efde70a47fd31e2cd59bf878832b4aa 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, "Terminated %d idle sessions\n", killed);
+               syslog(LOG_INFO, "Terminated %d idle sessions", 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,12 +354,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) {
@@ -383,12 +381,11 @@ void RemoveContext (CitContext *con)
                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
@@ -419,11 +417,12 @@ CitContext *CreateNewContext(void) {
         */
        me->MigrateBuf = NewStrBuf();
        me->ReadBuf = NewStrBuf();
+       me->lastcmd = time(NULL);       /* set lastcmd to now to prevent idle timer infanticide */
+
        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;
@@ -461,52 +460,6 @@ CitContext *CtdlGetContextArray(int *count)
 }
 
 
-
-/*
- * This function fills in a context and its user field correctly
- * Then creates/loads that user
- */
-void CtdlFillSystemContext(CitContext *context, char *name)
-{
-       char sysname[SIZ];
-       long len;
-
-       memset(context, 0, sizeof(CitContext));
-       context->internal_pgm = 1;
-       context->cs_pid = 0;
-       strcpy (sysname, "SYS_");
-       strcat (sysname, name);
-       len = cutuserkey(sysname);
-       memcpy(context->curr_user, sysname, len + 1);
-       context->client_socket = (-1);
-
-       /* internal_create_user has the side effect of loading the user regardless of wether they
-        * already existed or needed to be created
-        */
-       internal_create_user (sysname, len, &(context->user), -1) ;
-       
-       /* Check to see if the system user needs upgrading */
-       if (context->user.usernum == 0)
-       {       /* old system user with number 0, upgrade it */
-               context->user.usernum = get_new_user_number();
-               syslog(LOG_DEBUG, "Upgrading system user \"%s\" from user number 0 to user number %ld\n", context->user.fullname, context->user.usernum);
-               /* add user to the database */
-               CtdlPutUser(&(context->user));
-               cdb_store(CDB_USERSBYNUMBER, &(context->user.usernum), sizeof(long), context->user.fullname, strlen(context->user.fullname)+1);
-       }
-}
-
-/*
- * 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
  */
@@ -610,7 +563,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;
 }