]> code.citadel.org Git - citadel.git/blobdiff - citadel/context.c
* start migration to buffered I/O
[citadel.git] / citadel / context.c
index 478ed03ad71eb5f5f5aa55b733d88e45ddb77a1a..c20ec98f7654f873a7387799e617fb3a36fd0fc4 100644 (file)
@@ -129,6 +129,66 @@ int CtdlIsSingleUser(void)
 
 
 
+
+/*
+ * Locate a context by its session number and terminate it if the user is able.
+ * User can NOT terminate their current session.
+ * User CAN terminate any other session that has them logged in.
+ * Aide CAN terminate any session except the current one.
+ */
+int CtdlTerminateOtherSession (int session_num)
+{
+       int ret = 0;
+       CitContext *ccptr;
+
+       if (session_num == CC->cs_pid) {
+               return TERM_NOTALLOWED;
+       }
+
+       CtdlLogPrintf(CTDL_DEBUG, "Locating session to kill\n");
+       begin_critical_section(S_SESSION_TABLE);
+       for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
+               if (session_num == ccptr->cs_pid) {
+                       ret |= TERM_FOUND;
+                       if ((ccptr->user.usernum == CC->user.usernum)
+                          || (CC->user.axlevel >= 6)) {
+                               ret |= TERM_ALLOWED;
+                               ccptr->kill_me = 1;
+                       }
+               }
+       }
+       end_critical_section(S_SESSION_TABLE);
+       return ret;
+}
+
+
+
+/*
+ * Check to see if the user who we just sent mail to is logged in.  If yes,
+ * bump the 'new mail' counter for their session.  That enables them to
+ * receive a new mail notification without having to hit the database.
+ */
+void BumpNewMailCounter(long which_user) 
+{
+       CtdlBumpNewMailCounter(which_user);
+}
+
+void CtdlBumpNewMailCounter(long which_user)
+{
+       CitContext *ptr;
+
+       begin_critical_section(S_SESSION_TABLE);
+
+       for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
+               if (ptr->user.usernum == which_user) {
+                       ptr->newmail += 1;
+               }
+       }
+
+       end_critical_section(S_SESSION_TABLE);
+}
+
+
 /*
  * Check to see if a user is currently logged in
  * Take care with what you do as a result of this test.
@@ -160,7 +220,7 @@ int CtdlIsUserLoggedIn (char *user_name)
  * The user may not have been logged in when this function was called BUT
  * because of threading the user might be logged in before you test the result.
  */
-int CtdlIsUserLoggedInByNum (int usernum)
+int CtdlIsUserLoggedInByNum (long usernum)
 {
        CitContext *cptr;
        int ret = 0;
@@ -196,6 +256,44 @@ CitContext *MyContext(void) {
 
 
 
+/*
+ * Terminate idle sessions.  This function pounds through the session table
+ * comparing the current time to each session's time-of-last-command.  If an
+ * idle session is found it is terminated, then the search restarts at the
+ * beginning because the pointer to our place in the list becomes invalid.
+ */
+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->dont_term) {
+                               ccptr->kill_me = 1;
+                               ++killed;
+                       }
+                       else 
+                               longrunners ++;
+               }
+       }
+       end_critical_section(S_SESSION_TABLE);
+       if (killed > 0)
+               CtdlLogPrintf(CTDL_INFO, "Terminated %d idle sessions\n", killed);
+       if (longrunners > 0)
+               CtdlLogPrintf(CTDL_INFO, "Didn't terminate %d protected idle sessions;\n", killed);
+}
+
+
+
 /*
  * Terminate a session.
  */
@@ -213,7 +311,7 @@ void RemoveContext (CitContext *con)
         *       might make references to "CC" assuming it's the right one.
         */
        become_session(con);
-       logout();
+       CtdlUserLogout();
        PerformSessionHooks(EVT_STOP);
        become_session(NULL);
 
@@ -229,6 +327,8 @@ void RemoveContext (CitContext *con)
                con->ldap_dn = NULL;
        }
 
+       FreeStrBuf(&con->MigrateBuf);
+       FreeStrBuf(&con->ReadBuf);
        CtdlLogPrintf(CTDL_DEBUG, "Done with RemoveContext()\n");
 }
 
@@ -264,6 +364,8 @@ CitContext *CreateNewContext(void) {
         * Generate a unique session number and insert this context into
         * the list.
         */
+       me->MigrateBuf = NewStrBuf();
+       me->ReadBuf = NewStrBuf();
        begin_critical_section(S_SESSION_TABLE);
        me->cs_pid = ++next_pid;
        me->prev = NULL;