]> code.citadel.org Git - citadel.git/blobdiff - citadel/citserver.c
* Cleaned up some things that generated compiler warnings
[citadel.git] / citadel / citserver.c
index ae36d036b29f91c9459af729f428f6d088de29c5..eda6a754efb9fd26597d5cbb0959fcf9e6dc8780 100644 (file)
@@ -63,6 +63,7 @@
 #endif
 
 struct CitContext *ContextList = NULL;
+struct CitContext* next_session = NULL;
 char *unique_session_numbers;
 int ScheduledShutdown = 0;
 int do_defrag = 0;
@@ -90,10 +91,10 @@ void master_startup(void) {
        check_ref_counts();
 
        lprintf(CTDL_INFO, "Creating base rooms (if necessary)\n");
-       create_room(BASEROOM,           0, "", 0, 1, 0);
-       create_room(AIDEROOM,           3, "", 0, 1, 0);
-       create_room(SYSCONFIGROOM,      3, "", 0, 1, 0);
-       create_room(config.c_twitroom,  0, "", 0, 1, 0);
+       create_room(BASEROOM,           0, "", 0, 1, 0, VIEW_BBS);
+       create_room(AIDEROOM,           3, "", 0, 1, 0, VIEW_BBS);
+       create_room(SYSCONFIGROOM,      3, "", 0, 1, 0, VIEW_BBS);
+       create_room(config.c_twitroom,  0, "", 0, 1, 0, VIEW_BBS);
 
        /* The "Local System Configuration" room doesn't need to be visible */
         if (lgetroom(&qrbuf, SYSCONFIGROOM) == 0) {
@@ -142,6 +143,10 @@ void master_cleanup(void) {
        /* Do system-dependent stuff */
        sysdep_master_cleanup();
 
+#ifdef DEBUG_MEMORY_LEAKS
+       dump_heap();
+#endif
+
        /* Now go away. */
        lprintf(CTDL_NOTICE, "citserver: exiting.\n");
        fflush(stdout); fflush(stderr);
@@ -161,9 +166,9 @@ void deallocate_user_data(struct CitContext *con)
                lprintf(CTDL_DEBUG, "Deallocating user data symbol %ld\n",
                        con->FirstSessData->sym_id);
                if (con->FirstSessData->sym_data != NULL)
-                       phree(con->FirstSessData->sym_data);
+                       free(con->FirstSessData->sym_data);
                ptr = con->FirstSessData->next;
-               phree(con->FirstSessData);
+               free(con->FirstSessData);
                con->FirstSessData = ptr;
        }
        end_critical_section(S_SESSION_TABLE);
@@ -200,6 +205,9 @@ void RemoveContext (struct CitContext *con)
        else {
                for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
                        if (ptr->next == con) {
+                               /* See fair scheduling in sysdep.c */
+                               if (next_session == ptr->next)
+                                       next_session = ptr->next->next;
                                ToFree = ptr->next;
                                ptr->next = ptr->next->next;
                                --num_sessions;
@@ -228,7 +236,7 @@ void RemoveContext (struct CitContext *con)
        logout(con);
 
        unlink(con->temp);
-       lprintf(CTDL_NOTICE, "Session %d: ended.\n", con->cs_pid);
+       lprintf(CTDL_NOTICE, "[%3d] Session ended.\n", con->cs_pid);
 
        /* Deallocate any user-data attached to this session */
        deallocate_user_data(con);
@@ -240,7 +248,7 @@ void RemoveContext (struct CitContext *con)
        /* This is where we used to check for scheduled shutdowns. */
 
        /* Free up the memory used by this context */
-       phree(con);
+       free(con);
 
        lprintf(CTDL_DEBUG, "Done with RemoveContext()\n");
 }
@@ -287,9 +295,9 @@ void CtdlAllocUserData(unsigned long requested_sym, size_t num_bytes)
        }
 
        /* Grab us some memory!  Dem's good eatin' !!  */
-       ptr = mallok(sizeof(struct CtdlSessData));
+       ptr = malloc(sizeof(struct CtdlSessData));
        ptr->sym_id = requested_sym;
-       ptr->sym_data = mallok(num_bytes);
+       ptr->sym_data = malloc(num_bytes);
        memset(ptr->sym_data, 0, num_bytes);
 
        begin_critical_section(S_SESSION_TABLE);
@@ -310,7 +318,7 @@ void CtdlReallocUserData(unsigned long requested_sym, size_t num_bytes)
 
        for (ptr = CC->FirstSessData; ptr != NULL; ptr = ptr->next)  {
                if (ptr->sym_id == requested_sym) {
-                       ptr->sym_data = reallok(ptr->sym_data, num_bytes);
+                       ptr->sym_data = realloc(ptr->sym_data, num_bytes);
                        return;
                }
        }
@@ -353,7 +361,7 @@ void cmd_info(void) {
 
 
 /*
- * returns an asterisk if there are any express messages waiting,
+ * returns an asterisk if there are any instant messages waiting,
  * space otherwise.
  */
 char CtdlCheckExpress(void) {
@@ -368,16 +376,16 @@ char CtdlCheckExpress(void) {
 void cmd_time(void)
 {
    time_t tv;
-   struct tm *tmp;
+   struct tm tmp;
    
    tv = time(NULL);
-   tmp = localtime(&tv);
+   localtime_r(&tv, &tmp);
    
    /* timezone and daylight global variables are not portable. */
 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
-   cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, tmp->tm_gmtoff, tmp->tm_isdst);
+   cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst);
 #else
-   cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, timezone, tmp->tm_isdst);
+   cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst);
 #endif
 }
 
@@ -534,8 +542,8 @@ void cmd_mesg(char *mname)
 
        extract(buf,mname,0);
 
-       dirs[0]=mallok(64);
-       dirs[1]=mallok(64);
+       dirs[0]=malloc(64);
+       dirs[1]=malloc(64);
        strcpy(dirs[0],"messages");
        strcpy(dirs[1],"help");
        snprintf(buf2, sizeof buf2, "%s.%d.%d", buf, CC->cs_clientdev, CC->cs_clienttyp);
@@ -547,8 +555,8 @@ void cmd_mesg(char *mname)
                        mesg_locate(targ,sizeof targ,buf,2,(const char **)dirs);
                }       
        }
-       phree(dirs[0]);
-       phree(dirs[1]);
+       free(dirs[0]);
+       free(dirs[1]);
 
        if (strlen(targ)==0) {
                cprintf("%d '%s' not found.\n",ERROR + FILE_NOT_FOUND, mname);
@@ -585,6 +593,8 @@ void cmd_emsg(char *mname)
        char *dirs[2];
        int a;
 
+       unbuffer_output();
+
        if (CtdlAccessCheck(ac_aide)) return;
 
        extract(buf,mname,0);
@@ -592,13 +602,13 @@ void cmd_emsg(char *mname)
                if (buf[a] == '/') buf[a] = '.';
        }
 
-       dirs[0]=mallok(64);
-       dirs[1]=mallok(64);
+       dirs[0]=malloc(64);
+       dirs[1]=malloc(64);
        strcpy(dirs[0],"messages");
        strcpy(dirs[1],"help");
        mesg_locate(targ,sizeof targ,buf,2,(const char**)dirs);
-       phree(dirs[0]);
-       phree(dirs[1]);
+       free(dirs[0]);
+       free(dirs[1]);
 
        if (strlen(targ)==0) {
                snprintf(targ, sizeof targ, "./help/%s", buf);
@@ -659,6 +669,11 @@ int CtdlAccessCheck(int required_level) {
                return(-1);
        }
 
+       if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
+               cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
+               return(-1);
+       }
+
        if (CC->user.axlevel >= 6) return(0);
        if (required_level >= ac_aide) {
                cprintf("%d This command requires Aide access.\n",
@@ -673,12 +688,6 @@ int CtdlAccessCheck(int required_level) {
                return(-1);
        }
 
-       if (CC->logged_in) return(0);
-       if (required_level >= ac_logged_in) {
-               cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
-               return(-1);
-       }
-
        /* shhh ... succeed quietly */
        return(0);
 }
@@ -856,7 +865,7 @@ void generate_nonce(struct CitContext *con) {
  */
 void begin_session(struct CitContext *con)
 {
-       int len;        /* should be socklen_t but doesn't work on Macintosh */
+       int len;
        struct sockaddr_in sin;
 
        /* 
@@ -885,7 +894,7 @@ void begin_session(struct CitContext *con)
        len = sizeof sin;
        if (!CC->is_local_socket) {
                if (!getpeername(con->client_socket,
-                  (struct sockaddr *) &sin, &len))
+                  (struct sockaddr *) &sin, &len))     /* should be socklen_t but doesn't work on Macintosh */
                        locate_host(con->cs_host, sizeof con->cs_host,
                                con->cs_addr, sizeof con->cs_addr,
                                &sin.sin_addr);
@@ -917,7 +926,7 @@ void citproto_begin_session() {
                        config.c_nodename, config.c_maxsessions);
        }
        else {
-               cprintf("%d %s Citadel/UX server ready.\n",
+               cprintf("%d %s Citadel server ready.\n",
                        CIT_OK, config.c_nodename);
        }
 }
@@ -934,12 +943,14 @@ void do_command_loop(void) {
        time(&CC->lastcmd);
        memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
        if (client_gets(cmdbuf) < 1) {
-               lprintf(CTDL_ERR, "Client socket is broken.  Ending session.\n");
+               lprintf(CTDL_ERR, "Client socket is broken; ending session\n");
                CC->kill_me = 1;
                return;
        }
        lprintf(CTDL_INFO, "Citadel: %s\n", cmdbuf);
 
+       buffer_output();
+
        /*
         * Let other clients see the last command we executed, and
         * update the idle time, but not NOOP, QNOP, PEXP, or GEXP.
@@ -962,7 +973,7 @@ void do_command_loop(void) {
        }
                   
        if (!strncasecmp(cmdbuf,"NOOP",4)) {
-               cprintf("%d%cok\n",CIT_OK,CtdlCheckExpress());
+               cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress() );
        }
        
        else if (!strncasecmp(cmdbuf,"QNOP",4)) {
@@ -970,7 +981,7 @@ void do_command_loop(void) {
        }
 
        else if (!strncasecmp(cmdbuf,"QUIT",4)) {
-               cprintf("%d Goodbye.\n",CIT_OK);
+               cprintf("%d Goodbye.\n", CIT_OK);
                CC->kill_me = 1;
        }
 
@@ -980,7 +991,7 @@ void do_command_loop(void) {
 
        else if (!strncasecmp(cmdbuf,"LOUT",4)) {
                if (CC->logged_in) logout(CC);
-               cprintf("%d logged out.\n",CIT_OK);
+               cprintf("%d logged out.\n", CIT_OK);
        }
 
        else if (!strncasecmp(cmdbuf,"USER",4)) {
@@ -1303,16 +1314,21 @@ void do_command_loop(void) {
                cmd_isme(&cmdbuf[5]);
        }
 
-#ifdef DEBUG_MEMORY_LEAKS
-       else if (!strncasecmp(cmdbuf, "LEAK", 4)) {
-               dump_tracked();
-       }
-#endif
-
        else if (!DLoader_Exec_Cmd(cmdbuf)) {
-               cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
+               cprintf("%d Unrecognized or unsupported command.\n",
+                       ERROR + CMD_NOT_SUPPORTED);
               }
 
-       /* Run any after-each-command outines registered by modules */
+       unbuffer_output();
+
+       /* Run any after-each-command routines registered by modules */
        PerformSessionHooks(EVT_CMD);
 }
+
+
+/*
+ * This loop performs all asynchronous functions.
+ */
+void do_async_loop(void) {
+       PerformSessionHooks(EVT_ASYNC);
+}