2 * citserver's main() function lives here.
14 #include <sys/types.h>
17 #include <sys/socket.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
31 #include <netinet/in.h>
45 #include "dynloader.h"
46 #include "sysdep_decls.h"
47 #include "citserver.h"
51 #include "housekeeping.h"
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
63 * Here's where it all begins.
65 int main(int argc, char **argv)
67 char tracefile[128]; /* Name of file to log traces to */
68 int a, i; /* General-purpose variables */
70 int drop_root_perms = 1;
72 struct worker_node *wnp;
74 /* specify default port name and trace file */
75 strcpy(tracefile, "");
77 /* initialize the master context */
80 /* parse command-line arguments */
81 for (a=1; a<argc; ++a) {
83 /* -t specifies where to log trace messages to */
84 if (!strncmp(argv[a], "-t", 2)) {
85 strcpy(tracefile, argv[a]);
86 strcpy(tracefile, &tracefile[2]);
87 freopen(tracefile, "r", stdin);
88 freopen(tracefile, "w", stdout);
89 freopen(tracefile, "w", stderr);
92 /* run in the background if -d was specified */
93 else if (!strcmp(argv[a], "-d")) {
94 start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
97 /* -x specifies the desired logging level */
98 else if (!strncmp(argv[a], "-x", 2)) {
99 verbosity = atoi(&argv[a][2]);
102 else if (!strncmp(argv[a], "-h", 2)) {
103 safestrncpy(bbs_home_directory, &argv[a][2],
104 sizeof bbs_home_directory);
108 else if (!strncmp(argv[a], "-f", 2)) {
112 /* -r tells the server not to drop root permissions. don't use
113 * this unless you know what you're doing. this should be
114 * removed in the next release if it proves unnecessary. */
115 else if (!strcmp(argv[a], "-r"))
118 /* any other parameter makes it crash and burn */
120 lprintf(1, "citserver: usage: "
121 "citserver [-tTraceFile] [-d] [-f]"
122 " [-xLogLevel] [-hHomeDir]\n");
128 /* Tell 'em who's in da house */
130 "\nMultithreaded message server for Citadel/UX\n"
131 "Copyright (C) 1987-2001 by the Citadel/UX development team.\n"
132 "Citadel/UX is free software, covered by the GNU General Public License, and\n"
133 "you are welcome to change it and/or distribute copies of it under certain\n"
134 "conditions. There is absolutely no warranty for this software. Please\n"
135 "read the 'COPYING.txt' file for details.\n\n");
139 openlog("citserver", LOG_PID, LOG_USER);
141 /* Load site-specific parameters */
142 lprintf(7, "Loading citadel.config\n");
147 * Do non system dependent startup functions.
152 * Bind the server to a Unix-domain socket.
154 CtdlRegisterServiceHook(0,
156 citproto_begin_session,
160 * Bind the server to our favorite TCP port (usually 504).
162 CtdlRegisterServiceHook(config.c_port_number,
164 citproto_begin_session,
168 * Load any server-side modules (plugins) available here.
170 lprintf(7, "Initializing loadable modules\n");
171 if ((moddir = malloc(strlen(bbs_home_directory) + 9)) != NULL) {
172 sprintf(moddir, "%s/modules", bbs_home_directory);
173 DLoader_Init(moddir);
178 * The rescan pipe exists so that worker threads can be woken up and
179 * told to re-scan the context list for fd's to listen on. This is
180 * necessary, for example, when a context is about to go idle and needs
181 * to get back on that list.
184 lprintf(1, "Can't create rescan pipe!\n");
191 * Now that we've bound the sockets, change to the BBS user id and its
192 * corresponding group ids
194 if (drop_root_perms) {
195 if ((pw = getpwuid(BBSUID)) == NULL)
196 lprintf(1, "WARNING: getpwuid(%d): %s\n"
197 "Group IDs will be incorrect.\n", BBSUID,
200 initgroups(pw->pw_name, pw->pw_gid);
201 if (setgid(pw->pw_gid))
202 lprintf(3, "setgid(%d): %s\n", pw->pw_gid,
205 lprintf(7, "Changing uid to %d\n", BBSUID);
206 if (setuid(BBSUID) != 0) {
207 lprintf(3, "setuid() failed: %s\n", strerror(errno));
211 /* We want to check for idle sessions once per minute */
212 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
215 * Now create a bunch of worker threads.
217 lprintf(9, "Starting %d worker threads\n", config.c_min_workers-1);
218 begin_critical_section(S_WORKER_LIST);
219 for (i=0; i<(config.c_min_workers-1); ++i) {
222 end_critical_section(S_WORKER_LIST);
224 /* Now this thread can become a worker as well. */
225 initial_thread = pthread_self();
228 /* Server is exiting. Wait for workers to shutdown. */
229 lprintf(7, "Waiting for worker threads to shut down\n");
231 begin_critical_section(S_WORKER_LIST);
232 while (worker_list != NULL) {
234 worker_list = wnp->next;
236 /* avoid deadlock with an exiting thread */
237 end_critical_section(S_WORKER_LIST);
238 if ((i = pthread_join(wnp->tid, NULL)))
239 lprintf(1, "pthread_join: %s\n", strerror(i));
241 begin_critical_section(S_WORKER_LIST);
243 end_critical_section(S_WORKER_LIST);