fix dlen
[citadel.git] / citadel / server / citserver.c
index 0361ffac42a66bdd17ad800bf8fe2039e84930ab..a6c9505967fec1c16ea2eb0874c8f83063332a4e 100644 (file)
@@ -1,6 +1,6 @@
 // Main source module for the Citadel server
 //
-// Copyright (c) 1987-2022 by the citadel.org team
+// Copyright (c) 1987-2024 by the citadel.org team
 //
 // This program is open source software.  Use, duplication, or disclosure
 // is subject to the terms of the GNU General Public License, version 3.
@@ -11,8 +11,8 @@
 #include <sys/stat.h>
 #include "sysdep.h"
 #include <time.h>
+#include <sys/time.h>
 #include <libcitadel.h>
-
 #include "ctdl_module.h"
 #include "housekeeping.h"
 #include "locate_host.h"
@@ -25,29 +25,12 @@ char *unique_session_numbers;
 int ScheduledShutdown = 0;
 time_t server_startup_time;
 int panic_fd;
-int openid_level_supported = 0;
 
 
 // We need pseudo-random numbers for a few things.  Seed generously.
 void seed_random_number_generator(void) {
-       FILE *urandom;
-       struct timeval tv;
-       unsigned int seed;
-
-       syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
-       urandom = fopen("/dev/urandom", "r");
-       if (urandom != NULL) {
-               if (fread(&seed, sizeof seed, 1, urandom) == -1) {
-                       syslog(LOG_ERR, "citserver: failed to read random seed: %m");
-               }
-               fclose(urandom);
-       }
-       else {
-               gettimeofday(&tv, NULL);
-               seed = tv.tv_usec;
-       }
-       srand(seed);
-       srandom(seed);
+       syslog(LOG_INFO, "citserver: seeding the pseudo-random number generator");
+       srand(time(NULL) + getpid() + clock());
 }
 
 
@@ -57,10 +40,10 @@ void master_startup(void) {
        struct passwd *pw;
        gid_t gid;
 
-       syslog(LOG_DEBUG, "master_startup() started");
+       syslog(LOG_DEBUG, "citserver: master_startup() started");
        time(&server_startup_time);
 
-       syslog(LOG_INFO, "Checking directory access");
+       syslog(LOG_INFO, "citserver: checking directory access");
        if ((pw = getpwuid(ctdluid)) == NULL) {
                gid = getgid();
        }
@@ -68,7 +51,7 @@ void master_startup(void) {
                gid = pw->pw_gid;
        }
 
-       if (create_run_directories(CTDLUID, gid) != 0) {
+       if (create_run_directories(ctdluid, gid) != 0) {
                syslog(LOG_ERR, "citserver: failed to access and create directories");
                exit(1);
        }
@@ -78,12 +61,13 @@ void master_startup(void) {
        syslog(LOG_DEBUG, "citserver: ctdl_key_dir is %s", ctdl_key_dir);
        syslog(LOG_DEBUG, "citserver: ctdl_run_dir is %s", ctdl_run_dir);
 
-       syslog(LOG_INFO, "Opening databases");
-       open_databases();
+       syslog(LOG_INFO, "citserver: opening databases");
+       cdb_init_backends();
+       cdb_open_databases();
 
        // Load site-specific configuration
        seed_random_number_generator();                                 // must be done before config system
-       syslog(LOG_INFO, "Initializing configuration system");
+       syslog(LOG_INFO, "citserver: initializing configuration system");
        initialize_config_system();
        validate_config();
        migrate_legacy_control_record();
@@ -98,7 +82,7 @@ void master_startup(void) {
        // Check floor reference counts
        check_ref_counts();
 
-       syslog(LOG_INFO, "Creating base rooms (if necessary)");
+       syslog(LOG_INFO, "citserver: creating base rooms (if necessary)");
        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);
@@ -116,12 +100,12 @@ void master_startup(void) {
                CtdlPutRoomLock(&qrbuf);
        }
 
-       syslog(LOG_DEBUG, "master_startup() finished");
+       syslog(LOG_DEBUG, "citserver: master_startup() finished");
 }
 
 
 // Cleanup routine to be called when the server is shutting down.  Returns the needed exit code.
-int master_cleanup(int exitcode) {
+void master_cleanup(int exitcode) {
        static int already_cleaning_up = 0;
 
        if (already_cleaning_up) {
@@ -131,15 +115,15 @@ int master_cleanup(int exitcode) {
        }
        already_cleaning_up = 1;
 
-       // Do system-dependent stuff
-       sysdep_master_cleanup();
+       // Close the sockets
+       context_cleanup();
 
        // Close the configuration system
        shutdown_config_system();
 
        // Close databases
        syslog(LOG_INFO, "citserver: closing databases");
-       close_databases();
+       cdb_close_databases();
 
        // If the operator requested a halt but not an exit, halt here.
        if (shutdown_and_halt) {
@@ -156,19 +140,18 @@ int master_cleanup(int exitcode) {
        fflush(stdout);
        fflush(stderr);
 
-       if (restart_server != 0) {
-               exitcode = 1;
-       }
-       else if ((running_as_daemon != 0) && ((exitcode == 0))) {
+       if ((running_as_daemon != 0) && ((exitcode == 0))) {
                exitcode = CTDLEXIT_SHUTDOWN;
        }
-       return (exitcode);
+
+       ctdl_lockfile(0);
+       exit(exitcode);
 }
 
 
 // returns an asterisk if there are any instant messages waiting, space otherwise.
-char CtdlCheckExpress(void) {
-       if (CC->FirstExpressMessage == NULL) {
+char CtdlCheckExpress(struct CitContext *con) {
+       if (con->FirstExpressMessage == NULL) {
                return (' ');
        }
        else {
@@ -186,6 +169,7 @@ void citproto_begin_session() {
        }
        else {
                cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
+               strcpy(CC->cs_clientname, "Citadel client protocol");
                CC->can_receive_im = 1;
        }
 }