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(1, "citserver: usage: "
131 "citserver [-tTraceFile] "
134 " [-xLogLevel] [-hHomeDir]\n");
140 /* Tell 'em who's in da house */
142 "\n\n*** Citadel/UX messaging server engine v%d.%02d ***\n"
143 "Copyright (C) 1987-2003 by the Citadel/UX development team.\n"
144 "This program is distributed under the terms of the GNU "
145 "General Public License.\n\n",
153 /* Load site-specific parameters, and set the ipgm secret */
154 lprintf(7, "Loading citadel.config\n");
156 config.c_ipgm_secret = rand();
160 * Do non system dependent startup functions.
165 * Bind the server to a Unix-domain socket.
167 CtdlRegisterServiceHook(0,
169 citproto_begin_session,
173 * Bind the server to our favorite TCP port (usually 504).
175 CtdlRegisterServiceHook(config.c_port_number,
177 citproto_begin_session,
181 * Load any server-side extensions available here.
183 lprintf(7, "Initializing server extensions\n");
184 size = strlen(bbs_home_directory) + 9;
185 initialize_server_extensions();
188 * The rescan pipe exists so that worker threads can be woken up and
189 * told to re-scan the context list for fd's to listen on. This is
190 * necessary, for example, when a context is about to go idle and needs
191 * to get back on that list.
194 lprintf(1, "Can't create rescan pipe!\n");
201 * Now that we've bound the sockets, change to the BBS user id and its
202 * corresponding group ids
204 if (drop_root_perms) {
205 if ((pw = getpwuid(BBSUID)) == NULL)
206 lprintf(1, "WARNING: getpwuid(%ld): %s\n"
207 "Group IDs will be incorrect.\n", (long)BBSUID,
210 initgroups(pw->pw_name, pw->pw_gid);
211 if (setgid(pw->pw_gid))
212 lprintf(3, "setgid(%ld): %s\n", (long)pw->pw_gid,
215 lprintf(7, "Changing uid to %ld\n", (long)BBSUID);
216 if (setuid(BBSUID) != 0) {
217 lprintf(3, "setuid() failed: %s\n", strerror(errno));
221 /* We want to check for idle sessions once per minute */
222 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
225 * Now create a bunch of worker threads.
227 lprintf(9, "Starting %d worker threads\n", config.c_min_workers-1);
228 begin_critical_section(S_WORKER_LIST);
229 for (i=0; i<(config.c_min_workers-1); ++i) {
232 end_critical_section(S_WORKER_LIST);
234 /* Now this thread can become a worker as well. */
235 initial_thread = pthread_self();
238 /* Server is exiting. Wait for workers to shutdown. */
239 lprintf(7, "Waiting for worker threads to shut down\n");
241 begin_critical_section(S_WORKER_LIST);
242 while (worker_list != NULL) {
244 worker_list = wnp->next;
246 /* avoid deadlock with an exiting thread */
247 end_critical_section(S_WORKER_LIST);
248 if ((i = pthread_join(wnp->tid, NULL)))
249 lprintf(1, "pthread_join: %s\n", strerror(i));
251 begin_critical_section(S_WORKER_LIST);
253 end_critical_section(S_WORKER_LIST);