Simple concurrency lock to prevent multiple citservers running at the same time
[citadel.git] / citadel / citserver.c
index 076f5cee9ba0b7010f04d7c918260036dddde6c3..f389ac9f020b303551e62769d12c4d9135ea9b6c 100644 (file)
@@ -138,22 +138,19 @@ void master_startup(void) {
        open_databases();
 
        /* Load site-specific configuration */
-       syslog(LOG_INFO, "Loading citadel.config");
+       syslog(LOG_INFO, "Initializing configuration system");
        initialize_config_system();
        validate_config();
-
-       syslog(LOG_INFO, "Acquiring control record");
-       get_control();
-       put_config();
+       migrate_legacy_control_record();
 
        /* Check floor reference counts */
        check_ref_counts();
 
        syslog(LOG_INFO, "Creating base rooms (if necessary)\n");
-       CtdlCreateRoom(config.c_baseroom,       0, "", 0, 1, 0, VIEW_BBS);
-       CtdlCreateRoom(AIDEROOM,                3, "", 0, 1, 0, VIEW_BBS);
-       CtdlCreateRoom(SYSCONFIGROOM,           3, "", 0, 1, 0, VIEW_BBS);
-       CtdlCreateRoom(config.c_twitroom,       0, "", 0, 1, 0, VIEW_BBS);
+       CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"),  0, "", 0, 1, 0, VIEW_BBS);
+       CtdlCreateRoom(AIDEROOM,                        3, "", 0, 1, 0, VIEW_BBS);
+       CtdlCreateRoom(SYSCONFIGROOM,                   3, "", 0, 1, 0, VIEW_BBS);
+       CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"),  0, "", 0, 1, 0, VIEW_BBS);
 
        /* The "Local System Configuration" room doesn't need to be visible */
         if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
@@ -183,16 +180,14 @@ void master_startup(void) {
        srand(seed);
        srandom(seed);
 
-       put_config();
-
        syslog(LOG_DEBUG, "master_startup() finished\n");
 }
 
 
 /*
- * Cleanup routine to be called when the server is shutting down.
+ * Cleanup routine to be called when the server is shutting down.  Returns the needed exit code.
  */
-void master_cleanup(int exitcode) {
+int master_cleanup(int exitcode) {
        struct CleanupFunctionHook *fcn;
        static int already_cleaning_up = 0;
 
@@ -226,17 +221,17 @@ void master_cleanup(int exitcode) {
                }
        }
        
-       release_control();
-
        /* Now go away. */
        syslog(LOG_NOTICE, "citserver: Exiting with status %d\n", exitcode);
        fflush(stdout); fflush(stderr);
        
-       if (restart_server != 0)
-               exit(1);
-       if ((running_as_daemon != 0) && ((exitcode == 0) ))
+       if (restart_server != 0) {
+               exitcode = 1;
+       }
+       else if ((running_as_daemon != 0) && ((exitcode == 0) )) {
                exitcode = CTDLEXIT_SHUTDOWN;
-       exit(exitcode);
+       }
+       return(exitcode);
 }
 
 
@@ -301,7 +296,7 @@ int CtdlIsPublicClient(void)
                safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
                public_clientspos += sizeof(LOCALHOSTSTR) - 1;
                
-               if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
+               if (hostname_to_dotted_quad(addrbuf, CtdlGetConfigStr("c_fqdn")) == 0) {
                        *(public_clientspos++) = '|';
                        paddr = &addrbuf[0];
                        while (!IsEmptyStr (paddr) && 
@@ -363,12 +358,12 @@ void citproto_begin_session() {
        if (CC->nologin==1) {
                cprintf("%d %s: Too many users are already online (maximum is %d)\n",
                        ERROR + MAX_SESSIONS_EXCEEDED,
-                       config.c_nodename, config.c_maxsessions
+                       CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions")
                );
                CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
        }
        else {
-               cprintf("%d %s Citadel server ready.\n", CIT_OK, config.c_nodename);
+               cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
                CC->can_receive_im = 1;
        }
 }
@@ -376,70 +371,7 @@ void citproto_begin_session() {
 
 void citproto_begin_admin_session() {
        CC->internal_pgm = 1;
-       cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, config.c_nodename);
-}
-
-
-
-
-/*
- * This loop recognizes all server commands.
- */
-void do_command_loop(void) {
-       char cmdbuf[SIZ];
-       
-       time(&CC->lastcmd);
-       memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
-       if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
-               syslog(LOG_ERR, "Citadel client disconnected: ending session.\n");
-               CC->kill_me = KILLME_CLIENT_DISCONNECTED;
-               return;
-       }
-
-       /* Log the server command, but don't show passwords... */
-       if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
-               syslog(LOG_INFO, "[%d][%s(%ld)] %s",
-                       CC->cs_pid, CC->curr_user, CC->user.usernum, cmdbuf
-               );
-       }
-       else {
-               syslog(LOG_INFO, "[%d][%s(%ld)] <password command hidden from log>",
-                       CC->cs_pid, CC->curr_user, CC->user.usernum
-               );
-       }
-
-       buffer_output();
-
-       /*
-        * Let other clients see the last command we executed, and
-        * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
-        */
-       if ( (strncasecmp(cmdbuf, "NOOP", 4))
-          && (strncasecmp(cmdbuf, "QNOP", 4))
-          && (strncasecmp(cmdbuf, "PEXP", 4))
-          && (strncasecmp(cmdbuf, "GEXP", 4))
-          && (strncasecmp(cmdbuf, "RWHO", 4))
-          && (strncasecmp(cmdbuf, "TIME", 4)) ) {
-               strcpy(CC->lastcmdname, "    ");
-               safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
-               time(&CC->lastidle);
-       }
-       
-       if ((strncasecmp(cmdbuf, "ENT0", 4))
-          && (strncasecmp(cmdbuf, "MESG", 4))
-          && (strncasecmp(cmdbuf, "MSGS", 4)))
-       {
-          CC->cs_flags &= ~CS_POSTING;
-       }
-                  
-       if (!DLoader_Exec_Cmd(cmdbuf)) {
-               cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
-       }       
-
-       unbuffer_output();
-
-       /* Run any after-each-command routines registered by modules */
-       PerformSessionHooks(EVT_CMD);
+       cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
 }