1 // Main source module for the Citadel server
3 // Copyright (c) 1987-2021 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
7 // The program is distributed without any warranty, expressed or implied.
15 #include <libcitadel.h>
17 #include "ctdl_module.h"
18 #include "housekeeping.h"
19 #include "locate_host.h"
20 #include "citserver.h"
25 char *unique_session_numbers;
26 int ScheduledShutdown = 0;
27 time_t server_startup_time;
29 int openid_level_supported = 0;
33 * Various things that need to be initialized at startup
35 void master_startup(void)
40 struct ctdlroom qrbuf;
45 syslog(LOG_DEBUG, "master_startup() started");
46 time(&server_startup_time);
48 syslog(LOG_INFO, "Checking directory access");
49 if ((pw = getpwuid(ctdluid)) == NULL) {
56 if (create_run_directories(CTDLUID, gid) != 0) {
57 syslog(LOG_ERR, "citserver: failed to access and create directories");
60 syslog(LOG_INFO, "Opening databases");
63 /* Load site-specific configuration */
64 syslog(LOG_INFO, "Initializing configuration system");
65 initialize_config_system();
67 migrate_legacy_control_record();
69 // If we have an existing database that is older than version 928, reindex the user records.
70 // Unfortunately we cannot do this in serv_upgrade.c because it needs to happen VERY early during startup.
71 int existing_db = CtdlGetConfigInt("MM_hosted_upgrade_level");
72 if ( (existing_db > 0) && (existing_db < 928) ) {
73 ForEachUser(reindex_user_928, NULL);
76 /* Check floor reference counts */
79 syslog(LOG_INFO, "Creating base rooms (if necessary)");
80 CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
81 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
82 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
83 CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
85 /* The "Local System Configuration" room doesn't need to be visible */
86 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
87 qrbuf.QRflags2 |= QR2_SYSTEM;
88 CtdlPutRoomLock(&qrbuf);
91 /* Aide needs to be public postable, else we're not RFC conformant. */
92 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
93 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
94 CtdlPutRoomLock(&qrbuf);
97 syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
98 urandom = fopen("/dev/urandom", "r");
99 if (urandom != NULL) {
100 rv = fread(&seed, sizeof seed, 1, urandom);
102 syslog(LOG_ERR, "citserver: failed to read random seed: %m");
106 gettimeofday(&tv, NULL);
112 syslog(LOG_DEBUG, "master_startup() finished");
117 * Cleanup routine to be called when the server is shutting down. Returns the needed exit code.
119 int master_cleanup(int exitcode)
121 static int already_cleaning_up = 0;
123 if (already_cleaning_up) {
128 already_cleaning_up = 1;
130 /* Do system-dependent stuff */
131 sysdep_master_cleanup();
133 /* Close the configuration system */
134 shutdown_config_system();
136 /* Close databases */
137 syslog(LOG_INFO, "citserver: closing databases");
140 /* If the operator requested a halt but not an exit, halt here. */
141 if (shutdown_and_halt) {
142 syslog(LOG_ERR, "citserver: Halting server without exiting.");
151 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
155 if (restart_server != 0) {
157 } else if ((running_as_daemon != 0) && ((exitcode == 0))) {
158 exitcode = CTDLEXIT_SHUTDOWN;
165 * returns an asterisk if there are any instant messages waiting,
168 char CtdlCheckExpress(void)
170 if (CC->FirstExpressMessage == NULL) {
178 void citproto_begin_session()
180 if (CC->nologin == 1) {
181 cprintf("%d Too many users are already online (maximum is %d)\n",
182 ERROR + MAX_SESSIONS_EXCEEDED, CtdlGetConfigInt("c_maxsessions")
184 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
186 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
187 CC->can_receive_im = 1;
192 void citproto_begin_admin_session()
194 CC->internal_pgm = 1;
195 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
200 * This loop performs all asynchronous functions.
202 void do_async_loop(void)
204 PerformSessionHooks(EVT_ASYNC);