Grabbed another chunk of code out of user_ops.c, purge_user()
authorDave West <davew@uncensored.citadel.org>
Tue, 10 Nov 2009 19:27:46 +0000 (19:27 +0000)
committerDave West <davew@uncensored.citadel.org>
Tue, 10 Nov 2009 19:27:46 +0000 (19:27 +0000)
This chunk checked if a user was logged in by testing the user numbers
It is now a function and exposed to the API as
int CtdlIsUserLoggedInByNum(int num)
Also marked the code with a FIXME since it is unsafe, a user could log in
after the test but before the delete and realy mess things up.

citadel/context.c
citadel/include/ctdl_module.h
citadel/user_ops.c

index 3122e5c06b102dd55cc63fc2ef996e25ab18e0f2..478ed03ad71eb5f5f5aa55b733d88e45ddb77a1a 100644 (file)
@@ -151,6 +151,32 @@ int CtdlIsUserLoggedIn (char *user_name)
        return ret;
 }
 
+
+
+/*
+ * Check to see if a user is currently logged in.
+ * Basically same as CtdlIsUserLoggedIn() but uses the user number instead.
+ * Take care with what you do as a result of this test.
+ * 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)
+{
+       CitContext *cptr;
+       int ret = 0;
+
+       begin_critical_section(S_SESSION_TABLE);
+       for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
+               if (cptr->user.usernum == usernum) {
+                       ret = 1;
+               }
+       }
+       end_critical_section(S_SESSION_TABLE);
+       return ret;
+}
+
+
+
 /*
  * Return a pointer to the CitContext structure bound to the thread which
  * called this function.  If there's no such binding (for example, if it's
index 5e7488794dccbedba8ba833555d7cea5977bced1..5ec05ab08ee91c9ee2f1357bee9a68b6636f0703 100644 (file)
@@ -190,6 +190,7 @@ int CtdlIsSingleUser(void);
 
 
 int CtdlIsUserLoggedIn (char *user_name);
+int CtdlIsUserLoggedInByNum (int usernum);
 
 
 /*
index 965cec625563650f19cd964248c2c8d5bc41eda3..67935a34f20ae7bbfebfe3e25650eaef4df8f9ce 100644 (file)
@@ -1027,8 +1027,6 @@ int purge_user(char pname[])
        char filename[64];
        struct ctdluser usbuf;
        char usernamekey[USERNAME_SIZE];
-       CitContext *ccptr;
-       int user_is_logged_in = 0;
 
        makeuserkey(usernamekey, pname);
 
@@ -1044,15 +1042,7 @@ int purge_user(char pname[])
         * set the access level to 0, and let the account get swept up
         * during the next purge.
         */
-       user_is_logged_in = 0;
-       begin_critical_section(S_SESSION_TABLE);
-       for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
-               if (ccptr->user.usernum == usbuf.usernum) {
-                       user_is_logged_in = 1;
-               }
-       }
-       end_critical_section(S_SESSION_TABLE);
-       if (user_is_logged_in == 1) {
+       if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
                CtdlLogPrintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
                usbuf.axlevel = 0;
                CtdlPutUser(&usbuf);
@@ -1060,6 +1050,16 @@ int purge_user(char pname[])
        }
        CtdlLogPrintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
 
+/*
+ * FIXME:
+ * This should all be wrapped in a S_USERS mutex.
+ * Without the mutex the user could log in before we get to the next function
+ * That would truly mess things up :-(
+ * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
+ * and end after the user has been deleted from the database, below.
+ * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
+ */
+
        /* Perform any purge functions registered by server extensions */
        PerformUserHooks(&usbuf, EVT_PURGEUSER);