2 * Main source module for the Citadel server
4 * Copyright (c) 1987-2021 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include <libcitadel.h>
23 #include "ctdl_module.h"
24 #include "housekeeping.h"
25 #include "locate_host.h"
26 #include "citserver.h"
31 char *unique_session_numbers;
32 int ScheduledShutdown = 0;
33 time_t server_startup_time;
35 int openid_level_supported = 0;
39 * Various things that need to be initialized at startup
41 void master_startup(void)
46 struct ctdlroom qrbuf;
51 syslog(LOG_DEBUG, "master_startup() started");
52 time(&server_startup_time);
54 syslog(LOG_INFO, "Checking directory access");
55 if ((pw = getpwuid(ctdluid)) == NULL) {
62 if (create_run_directories(CTDLUID, gid) != 0) {
63 syslog(LOG_ERR, "citserver: failed to access and create directories");
66 syslog(LOG_INFO, "Opening databases");
69 /* Load site-specific configuration */
70 syslog(LOG_INFO, "Initializing configuration system");
71 initialize_config_system();
73 migrate_legacy_control_record();
75 // If we have an existing database that is older than version 928, reindex the user records.
76 // Unfortunately we cannot do this in serv_upgrade.c because it needs to happen VERY early during startup.
77 int existing_db = CtdlGetConfigInt("MM_hosted_upgrade_level");
78 if ( (existing_db > 0) && (existing_db < 928) ) {
79 ForEachUser(reindex_user_928, NULL);
82 /* Check floor reference counts */
85 syslog(LOG_INFO, "Creating base rooms (if necessary)");
86 CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
87 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
88 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
89 CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
91 /* The "Local System Configuration" room doesn't need to be visible */
92 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
93 qrbuf.QRflags2 |= QR2_SYSTEM;
94 CtdlPutRoomLock(&qrbuf);
97 /* Aide needs to be public postable, else we're not RFC conformant. */
98 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
99 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
100 CtdlPutRoomLock(&qrbuf);
103 syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
104 urandom = fopen("/dev/urandom", "r");
105 if (urandom != NULL) {
106 rv = fread(&seed, sizeof seed, 1, urandom);
108 syslog(LOG_ERR, "citserver: failed to read random seed: %m");
112 gettimeofday(&tv, NULL);
118 syslog(LOG_DEBUG, "master_startup() finished");
123 * Cleanup routine to be called when the server is shutting down. Returns the needed exit code.
125 int master_cleanup(int exitcode)
127 static int already_cleaning_up = 0;
129 if (already_cleaning_up) {
134 already_cleaning_up = 1;
136 /* Do system-dependent stuff */
137 sysdep_master_cleanup();
139 /* Close the configuration system */
140 shutdown_config_system();
142 /* Close databases */
143 syslog(LOG_INFO, "citserver: closing databases");
146 /* If the operator requested a halt but not an exit, halt here. */
147 if (shutdown_and_halt) {
148 syslog(LOG_ERR, "citserver: Halting server without exiting.");
157 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
161 if (restart_server != 0) {
163 } else if ((running_as_daemon != 0) && ((exitcode == 0))) {
164 exitcode = CTDLEXIT_SHUTDOWN;
171 * returns an asterisk if there are any instant messages waiting,
174 char CtdlCheckExpress(void)
176 if (CC->FirstExpressMessage == NULL) {
184 void citproto_begin_session()
186 if (CC->nologin == 1) {
187 cprintf("%d Too many users are already online (maximum is %d)\n",
188 ERROR + MAX_SESSIONS_EXCEEDED, CtdlGetConfigInt("c_maxsessions")
190 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
192 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
193 CC->can_receive_im = 1;
198 void citproto_begin_admin_session()
200 CC->internal_pgm = 1;
201 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
206 * This loop performs all asynchronous functions.
208 void do_async_loop(void)
210 PerformSessionHooks(EVT_ASYNC);