2 * citserver's main() function lives here.
14 #include <sys/types.h>
17 #include <sys/socket.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
32 #include <netinet/in.h>
43 #ifdef HAVE_SYS_PRCTL_H
44 #include <sys/prctl.h>
48 #include "serv_extensions.h"
49 #include "sysdep_decls.h"
50 #include "citserver.h"
54 #include "housekeeping.h"
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
66 * Here's where it all begins.
68 int main(int argc, char **argv)
70 char tracefile[128]; /* Name of file to log traces to */
71 int a, i; /* General-purpose variables */
73 int drop_root_perms = 1;
74 struct worker_node *wnp;
77 /* specify default port name and trace file */
78 strcpy(tracefile, "");
80 /* initialize the master context */
83 /* parse command-line arguments */
84 for (a=1; a<argc; ++a) {
86 /* -t specifies where to log trace messages to */
87 if (!strncmp(argv[a], "-t", 2)) {
88 safestrncpy(tracefile, argv[a], sizeof tracefile);
89 strcpy(tracefile, &tracefile[2]);
90 freopen(tracefile, "r", stdin);
91 freopen(tracefile, "w", stdout);
92 freopen(tracefile, "w", stderr);
93 chmod(tracefile, 0600);
96 else if (!strncmp(argv[a], "-l", 2)) {
97 safestrncpy(tracefile, argv[a], sizeof tracefile);
98 strcpy(tracefile, &tracefile[2]);
99 syslog_facility = SyslogFacility(tracefile);
100 if (syslog_facility >= 0) {
101 openlog("citadel", LOG_PID, syslog_facility);
105 /* run in the background if -d was specified */
106 else if (!strcmp(argv[a], "-d")) {
107 start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
110 /* -x specifies the desired logging level */
111 else if (!strncmp(argv[a], "-x", 2)) {
112 verbosity = atoi(&argv[a][2]);
115 else if (!strncmp(argv[a], "-h", 2)) {
116 safestrncpy(bbs_home_directory, &argv[a][2],
117 sizeof bbs_home_directory);
121 else if (!strncmp(argv[a], "-f", 2)) {
125 /* -r tells the server not to drop root permissions. don't use
126 * this unless you know what you're doing. this should be
127 * removed in the next release if it proves unnecessary. */
128 else if (!strcmp(argv[a], "-r"))
131 /* any other parameter makes it crash and burn */
133 lprintf(CTDL_EMERG, "citserver: usage: "
134 "citserver [-tTraceFile] "
137 " [-xLogLevel] [-hHomeDir]\n");
143 /* Tell 'em who's in da house */
144 lprintf(CTDL_NOTICE, "\n");
145 lprintf(CTDL_NOTICE, "\n");
147 "*** Citadel/UX messaging server engine v%d.%02d ***\n",
148 (REV_LEVEL/100), (REV_LEVEL%100));
150 "Copyright (C) 1987-2003 by the Citadel/UX development team.\n");
152 "This program is distributed under the terms of the GNU "
153 "General Public License.\n");
154 lprintf(CTDL_NOTICE, "\n");
156 /* Load site-specific parameters, and set the ipgm secret */
157 lprintf(CTDL_INFO, "Loading citadel.config\n");
159 config.c_ipgm_secret = rand();
166 * Do non system dependent startup functions.
171 * Bind the server to a Unix-domain socket.
173 CtdlRegisterServiceHook(0,
175 citproto_begin_session,
180 * Bind the server to our favorite TCP port (usually 504).
182 CtdlRegisterServiceHook(config.c_port_number,
184 citproto_begin_session,
189 * Load any server-side extensions available here.
191 lprintf(CTDL_INFO, "Initializing server extensions\n");
192 size = strlen(bbs_home_directory) + 9;
193 initialize_server_extensions();
196 * Now that we've bound the sockets, change to the BBS user id and its
197 * corresponding group ids
199 if (drop_root_perms) {
200 if ((pw = getpwuid(BBSUID)) == NULL)
201 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
202 "Group IDs will be incorrect.\n", (long)BBSUID,
205 initgroups(pw->pw_name, pw->pw_gid);
206 if (setgid(pw->pw_gid))
207 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
210 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)BBSUID);
211 if (setuid(BBSUID) != 0) {
212 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
214 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
215 prctl(PR_SET_DUMPABLE, 1);
219 /* We want to check for idle sessions once per minute */
220 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
223 * Now create a bunch of worker threads.
225 lprintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers-1);
226 begin_critical_section(S_WORKER_LIST);
227 for (i=0; i<(config.c_min_workers-1); ++i) {
230 end_critical_section(S_WORKER_LIST);
232 /* Now this thread can become a worker as well. */
233 initial_thread = pthread_self();
236 /* Server is exiting. Wait for workers to shutdown. */
237 lprintf(CTDL_INFO, "Waiting for worker threads to shut down\n");
239 begin_critical_section(S_WORKER_LIST);
240 while (worker_list != NULL) {
242 worker_list = wnp->next;
244 /* avoid deadlock with an exiting thread */
245 end_critical_section(S_WORKER_LIST);
246 if ((i = pthread_join(wnp->tid, NULL)))
247 lprintf(CTDL_CRIT, "pthread_join: %s\n", strerror(i));
249 begin_critical_section(S_WORKER_LIST);
251 end_critical_section(S_WORKER_LIST);