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>
45 #include "serv_extensions.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;
71 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 safestrncpy(tracefile, argv[a], sizeof tracefile);
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 else if (!strncmp(argv[a], "-l", 2)) {
94 safestrncpy(tracefile, argv[a], sizeof tracefile);
95 strcpy(tracefile, &tracefile[2]);
96 syslog_facility = SyslogFacility(tracefile);
97 if (syslog_facility >= 0) {
98 openlog("citadel", LOG_PID, syslog_facility);
102 /* run in the background if -d was specified */
103 else if (!strcmp(argv[a], "-d")) {
104 start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
107 /* -x specifies the desired logging level */
108 else if (!strncmp(argv[a], "-x", 2)) {
109 verbosity = atoi(&argv[a][2]);
112 else if (!strncmp(argv[a], "-h", 2)) {
113 safestrncpy(bbs_home_directory, &argv[a][2],
114 sizeof bbs_home_directory);
118 else if (!strncmp(argv[a], "-f", 2)) {
122 /* -r tells the server not to drop root permissions. don't use
123 * this unless you know what you're doing. this should be
124 * removed in the next release if it proves unnecessary. */
125 else if (!strcmp(argv[a], "-r"))
128 /* any other parameter makes it crash and burn */
130 lprintf(CTDL_EMERG, "citserver: usage: "
131 "citserver [-tTraceFile] "
134 " [-xLogLevel] [-hHomeDir]\n");
140 /* Tell 'em who's in da house */
141 lprintf(CTDL_NOTICE, "\n");
142 lprintf(CTDL_NOTICE, "\n");
144 "*** Citadel/UX messaging server engine v%d.%02d ***\n",
145 (REV_LEVEL/100), (REV_LEVEL%100));
147 "Copyright (C) 1987-2003 by the Citadel/UX development team.\n");
149 "This program is distributed under the terms of the GNU "
150 "General Public License.\n");
151 lprintf(CTDL_NOTICE, "\n");
153 /* Load site-specific parameters, and set the ipgm secret */
154 lprintf(CTDL_INFO, "Loading citadel.config\n");
156 config.c_ipgm_secret = rand();
163 * Do non system dependent startup functions.
168 * Bind the server to a Unix-domain socket.
170 CtdlRegisterServiceHook(0,
172 citproto_begin_session,
176 * Bind the server to our favorite TCP port (usually 504).
178 CtdlRegisterServiceHook(config.c_port_number,
180 citproto_begin_session,
184 * Load any server-side extensions available here.
186 lprintf(CTDL_INFO, "Initializing server extensions\n");
187 size = strlen(bbs_home_directory) + 9;
188 initialize_server_extensions();
191 * The rescan pipe exists so that worker threads can be woken up and
192 * told to re-scan the context list for fd's to listen on. This is
193 * necessary, for example, when a context is about to go idle and needs
194 * to get back on that list.
197 lprintf(CTDL_EMERG, "Can't create rescan pipe!\n");
204 * Now that we've bound the sockets, change to the BBS user id and its
205 * corresponding group ids
207 if (drop_root_perms) {
208 if ((pw = getpwuid(BBSUID)) == NULL)
209 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
210 "Group IDs will be incorrect.\n", (long)BBSUID,
213 initgroups(pw->pw_name, pw->pw_gid);
214 if (setgid(pw->pw_gid))
215 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
218 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)BBSUID);
219 if (setuid(BBSUID) != 0) {
220 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
224 /* We want to check for idle sessions once per minute */
225 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
228 * Now create a bunch of worker threads.
230 lprintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers-1);
231 begin_critical_section(S_WORKER_LIST);
232 for (i=0; i<(config.c_min_workers-1); ++i) {
235 end_critical_section(S_WORKER_LIST);
237 /* Now this thread can become a worker as well. */
238 initial_thread = pthread_self();
241 /* Server is exiting. Wait for workers to shutdown. */
242 lprintf(CTDL_INFO, "Waiting for worker threads to shut down\n");
244 begin_critical_section(S_WORKER_LIST);
245 while (worker_list != NULL) {
247 worker_list = wnp->next;
249 /* avoid deadlock with an exiting thread */
250 end_critical_section(S_WORKER_LIST);
251 if ((i = pthread_join(wnp->tid, NULL)))
252 lprintf(CTDL_CRIT, "pthread_join: %s\n", strerror(i));
254 begin_critical_section(S_WORKER_LIST);
256 end_critical_section(S_WORKER_LIST);