citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

citserver.c
Go to the documentation of this file.
1// Main source module for the Citadel server
2//
3// Copyright (c) 1987-2021 by the citadel.org team
4//
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.
8
9#include <stdlib.h>
10#include <unistd.h>
11#include <stdio.h>
12#include <sys/stat.h>
13#include "sysdep.h"
14#include <time.h>
15#include <libcitadel.h>
16
17#include "ctdl_module.h"
18#include "housekeeping.h"
19#include "locate_host.h"
20#include "citserver.h"
21#include "user_ops.h"
22#include "control.h"
23#include "config.h"
24
30
31
32/*
33 * Various things that need to be initialized at startup
34 */
36{
37 struct timeval tv;
38 unsigned int seed;
39 FILE *urandom;
40 struct ctdlroom qrbuf;
41 int rv;
42 struct passwd *pw;
43 gid_t gid;
44
45 syslog(LOG_DEBUG, "master_startup() started");
47
48 syslog(LOG_INFO, "Checking directory access");
49 if ((pw = getpwuid(ctdluid)) == NULL) {
50 gid = getgid();
51 }
52 else {
53 gid = pw->pw_gid;
54 }
55
56 if (create_run_directories(CTDLUID, gid) != 0) {
57 syslog(LOG_ERR, "citserver: failed to access and create directories");
58 exit(1);
59 }
60 syslog(LOG_INFO, "Opening databases");
62
63 /* Load site-specific configuration */
64 syslog(LOG_INFO, "Initializing configuration system");
68
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) ) {
74 }
75
76 /* Check floor reference counts */
78
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);
84
85 /* The "Local System Configuration" room doesn't need to be visible */
89 }
90
91 /* Aide needs to be public postable, else we're not RFC conformant. */
92 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
95 }
96
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);
101 if (rv == -1) {
102 syslog(LOG_ERR, "citserver: failed to read random seed: %m");
103 }
104 fclose(urandom);
105 } else {
106 gettimeofday(&tv, NULL);
107 seed = tv.tv_usec;
108 }
109 srand(seed);
110 srandom(seed);
111
112 syslog(LOG_DEBUG, "master_startup() finished");
113}
114
115
116/*
117 * Cleanup routine to be called when the server is shutting down. Returns the needed exit code.
118 */
119int master_cleanup(int exitcode)
120{
121 static int already_cleaning_up = 0;
122
123 if (already_cleaning_up) {
124 while (1) {
125 usleep(1000000);
126 }
127 }
128 already_cleaning_up = 1;
129
130 /* Do system-dependent stuff */
132
133 /* Close the configuration system */
135
136 /* Close databases */
137 syslog(LOG_INFO, "citserver: closing databases");
139
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.");
143 fflush(stdout);
144 fflush(stderr);
145 while (1) {
146 sleep(32767);
147 }
148 }
149
150 /* Now go away. */
151 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
152 fflush(stdout);
153 fflush(stderr);
154
155 if (restart_server != 0) {
156 exitcode = 1;
157 } else if ((running_as_daemon != 0) && ((exitcode == 0))) {
158 exitcode = CTDLEXIT_SHUTDOWN;
159 }
160 return (exitcode);
161}
162
163
164/*
165 * returns an asterisk if there are any instant messages waiting,
166 * space otherwise.
167 */
169{
170 if (CC->FirstExpressMessage == NULL) {
171 return (' ');
172 } else {
173 return ('*');
174 }
175}
176
177
179{
180 if (CC->nologin == 1) {
181 cprintf("%d Too many users are already online (maximum is %d)\n",
183 );
185 } else {
186 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
187 CC->can_receive_im = 1;
188 }
189}
190
191
193{
194 CC->internal_pgm = 1;
195 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
196}
197
198
199/*
200 * This loop performs all asynchronous functions.
201 */
203{
205}
int create_run_directories(long UID, long GID)
Definition: citadel_dirs.c:60
char CtdlCheckExpress(void)
Definition: citserver.c:168
time_t server_startup_time
Definition: citserver.c:27
int master_cleanup(int exitcode)
Definition: citserver.c:119
int openid_level_supported
Definition: citserver.c:29
void master_startup(void)
Definition: citserver.c:35
void citproto_begin_session()
Definition: citserver.c:178
int ScheduledShutdown
Definition: citserver.c:26
int panic_fd
Definition: citserver.c:28
char * unique_session_numbers
Definition: citserver.c:25
void do_async_loop(void)
Definition: citserver.c:202
void citproto_begin_admin_session()
Definition: citserver.c:192
char * CtdlGetConfigStr(char *key)
Definition: config.c:384
void shutdown_config_system(void)
Definition: config.c:308
void initialize_config_system(void)
Definition: config.c:217
int CtdlGetConfigInt(char *key)
Definition: config.c:417
void validate_config(void)
Definition: config.c:45
#define CC
Definition: context.h:140
void migrate_legacy_control_record(void)
Definition: control.c:112
int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name)
Definition: room_ops.c:387
#define CTDLUID
Definition: ctdl_module.h:259
unsigned CtdlCreateRoom(char *new_room_name, int new_room_type, char *new_room_pass, int new_room_floor, int really_create, int avoid_access, int new_room_view)
Definition: room_ops.c:1136
void CtdlPutRoomLock(struct ctdlroom *qrbuf)
Definition: room_ops.c:444
void open_databases(void)
Definition: database.c:177
void close_databases(void)
Definition: database.c:310
void check_ref_counts(void)
Definition: housekeeping.c:48
#define CIT_OK
Definition: ipcdef.h:6
#define QR2_SMTP_PUBLIC
Definition: ipcdef.h:59
#define ERROR
Definition: ipcdef.h:9
#define MAX_SESSIONS_EXCEEDED
Definition: ipcdef.h:24
#define QR2_SYSTEM
Definition: ipcdef.h:55
void PerformSessionHooks(int EventType)
struct ctdlroom qrbuf
Definition: serv_migrate.c:497
uid_t ctdluid
Definition: server_main.c:30
#define CTDLEXIT_SHUTDOWN
Definition: server.h:67
#define EVT_ASYNC
Definition: server.h:220
@ KILLME_MAX_SESSIONS_EXCEEDED
Definition: server.h:93
unsigned QRflags2
Definition: citadel.h:121
#define AIDEROOM
Definition: sysconfig.h:63
#define SYSCONFIGROOM
Definition: sysconfig.h:72
void cprintf(const char *format,...)
Definition: sysdep.c:381
void sysdep_master_cleanup(void)
Definition: sysdep.c:632
volatile int restart_server
Definition: sysdep.c:47
volatile int shutdown_and_halt
Definition: sysdep.c:46
volatile int running_as_daemon
Definition: sysdep.c:48
void ForEachUser(void(*CallBack)(char *, void *out_data), void *in_data)
Definition: user_ops.c:1198
void reindex_user_928(char *username, void *out_data)
Definition: user_ops.c:230