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);
90 chmod(tracefile, 0600);
93 /* run in the background if -d was specified */
94 else if (!strcmp(argv[a], "-d")) {
95 start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
98 /* -x specifies the desired logging level */
99 else if (!strncmp(argv[a], "-x", 2)) {
100 verbosity = atoi(&argv[a][2]);
103 else if (!strncmp(argv[a], "-h", 2)) {
104 safestrncpy(bbs_home_directory, &argv[a][2],
105 sizeof bbs_home_directory);
109 else if (!strncmp(argv[a], "-f", 2)) {
113 /* -r tells the server not to drop root permissions. don't use
114 * this unless you know what you're doing. this should be
115 * removed in the next release if it proves unnecessary. */
116 else if (!strcmp(argv[a], "-r"))
119 /* any other parameter makes it crash and burn */
121 lprintf(1, "citserver: usage: "
122 "citserver [-tTraceFile] [-d] [-f]"
123 " [-xLogLevel] [-hHomeDir]\n");
129 /* Tell 'em who's in da house */
131 "\nMultithreaded message server for Citadel/UX\n"
132 "Copyright (C) 1987-2001 by the Citadel/UX development team.\n"
133 "Citadel/UX is free software, covered by the GNU General Public License, and\n"
134 "you are welcome to change it and/or distribute copies of it under certain\n"
135 "conditions. There is absolutely no warranty for this software. Please\n"
136 "read the 'COPYING.txt' file for details.\n\n");
140 openlog("citserver", LOG_PID, LOG_USER);
142 /* Load site-specific parameters */
143 lprintf(7, "Loading citadel.config\n");
148 * Do non system dependent startup functions.
153 * Bind the server to a Unix-domain socket.
155 CtdlRegisterServiceHook(0,
157 citproto_begin_session,
161 * Bind the server to our favorite TCP port (usually 504).
163 CtdlRegisterServiceHook(config.c_port_number,
165 citproto_begin_session,
169 * Load any server-side modules (plugins) available here.
171 lprintf(7, "Initializing loadable modules\n");
172 if ((moddir = malloc(strlen(bbs_home_directory) + 9)) != NULL) {
173 sprintf(moddir, "%s/modules", bbs_home_directory);
174 DLoader_Init(moddir);
179 * The rescan pipe exists so that worker threads can be woken up and
180 * told to re-scan the context list for fd's to listen on. This is
181 * necessary, for example, when a context is about to go idle and needs
182 * to get back on that list.
185 lprintf(1, "Can't create rescan pipe!\n");
192 * Now that we've bound the sockets, change to the BBS user id and its
193 * corresponding group ids
195 if (drop_root_perms) {
196 if ((pw = getpwuid(BBSUID)) == NULL)
197 lprintf(1, "WARNING: getpwuid(%d): %s\n"
198 "Group IDs will be incorrect.\n", BBSUID,
201 initgroups(pw->pw_name, pw->pw_gid);
202 if (setgid(pw->pw_gid))
203 lprintf(3, "setgid(%d): %s\n", pw->pw_gid,
206 lprintf(7, "Changing uid to %d\n", BBSUID);
207 if (setuid(BBSUID) != 0) {
208 lprintf(3, "setuid() failed: %s\n", strerror(errno));
212 /* We want to check for idle sessions once per minute */
213 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
216 * Now create a bunch of worker threads.
218 lprintf(9, "Starting %d worker threads\n", config.c_min_workers-1);
219 begin_critical_section(S_WORKER_LIST);
220 for (i=0; i<(config.c_min_workers-1); ++i) {
223 end_critical_section(S_WORKER_LIST);
225 /* Now this thread can become a worker as well. */
226 initial_thread = pthread_self();
229 /* Server is exiting. Wait for workers to shutdown. */
230 lprintf(7, "Waiting for worker threads to shut down\n");
232 begin_critical_section(S_WORKER_LIST);
233 while (worker_list != NULL) {
235 worker_list = wnp->next;
237 /* avoid deadlock with an exiting thread */
238 end_critical_section(S_WORKER_LIST);
239 if ((i = pthread_join(wnp->tid, NULL)))
240 lprintf(1, "pthread_join: %s\n", strerror(i));
242 begin_critical_section(S_WORKER_LIST);
244 end_critical_section(S_WORKER_LIST);