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;
76 /* specify default port name and trace file */
77 strcpy(tracefile, "");
79 /* initialize the master context */
82 /* parse command-line arguments */
83 for (a=1; a<argc; ++a) {
85 /* -t specifies where to log trace messages to */
86 if (!strncmp(argv[a], "-t", 2)) {
87 safestrncpy(tracefile, argv[a], sizeof tracefile);
88 strcpy(tracefile, &tracefile[2]);
89 freopen(tracefile, "r", stdin);
90 freopen(tracefile, "w", stdout);
91 freopen(tracefile, "w", stderr);
92 chmod(tracefile, 0600);
95 else if (!strncmp(argv[a], "-l", 2)) {
96 safestrncpy(tracefile, argv[a], sizeof tracefile);
97 strcpy(tracefile, &tracefile[2]);
98 syslog_facility = SyslogFacility(tracefile);
99 if (syslog_facility >= 0) {
100 openlog("citadel", LOG_PID, syslog_facility);
104 /* run in the background if -d was specified */
105 else if (!strcmp(argv[a], "-d")) {
106 start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
109 /* -x specifies the desired logging level */
110 else if (!strncmp(argv[a], "-x", 2)) {
111 verbosity = atoi(&argv[a][2]);
114 else if (!strncmp(argv[a], "-h", 2)) {
115 safestrncpy(bbs_home_directory, &argv[a][2],
116 sizeof bbs_home_directory);
120 else if (!strncmp(argv[a], "-f", 2)) {
124 /* -r tells the server not to drop root permissions. don't use
125 * this unless you know what you're doing. this should be
126 * removed in the next release if it proves unnecessary. */
127 else if (!strcmp(argv[a], "-r"))
130 /* any other parameter makes it crash and burn */
132 lprintf(CTDL_EMERG, "citserver: usage: "
133 "citserver [-tTraceFile] "
136 " [-xLogLevel] [-hHomeDir]\n");
142 /* Tell 'em who's in da house */
143 lprintf(CTDL_NOTICE, "\n");
144 lprintf(CTDL_NOTICE, "\n");
146 "*** Citadel server engine v%d.%02d ***\n",
147 (REV_LEVEL/100), (REV_LEVEL%100));
149 "Copyright (C) 1987-2004 by the Citadel development team.\n");
151 "This program is distributed under the terms of the GNU "
152 "General Public License.\n");
153 lprintf(CTDL_NOTICE, "\n");
155 /* Load site-specific parameters, and set the ipgm secret */
156 lprintf(CTDL_INFO, "Loading citadel.config\n");
158 config.c_ipgm_secret = rand();
165 * Do non system dependent startup functions.
170 * Bind the server to a Unix-domain socket.
172 CtdlRegisterServiceHook(0,
174 citproto_begin_session,
179 * Bind the server to our favorite TCP port (usually 504).
181 CtdlRegisterServiceHook(config.c_port_number,
183 citproto_begin_session,
188 * Load any server-side extensions available here.
190 lprintf(CTDL_INFO, "Initializing server extensions\n");
191 size = strlen(bbs_home_directory) + 9;
192 initialize_server_extensions();
195 * Now that we've bound the sockets, change to the BBS user id and its
196 * corresponding group ids
198 if (drop_root_perms) {
199 if ((pw = getpwuid(BBSUID)) == NULL)
200 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
201 "Group IDs will be incorrect.\n", (long)BBSUID,
204 initgroups(pw->pw_name, pw->pw_gid);
205 if (setgid(pw->pw_gid))
206 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
209 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)BBSUID);
210 if (setuid(BBSUID) != 0) {
211 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
213 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
214 prctl(PR_SET_DUMPABLE, 1);
218 /* We want to check for idle sessions once per minute */
219 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
222 * Now create a bunch of worker threads.
224 lprintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers-1);
225 begin_critical_section(S_WORKER_LIST);
226 for (i=0; i<(config.c_min_workers-1); ++i) {
229 end_critical_section(S_WORKER_LIST);
231 /* Now this thread can become a worker as well. */
234 /* Server is exiting. Wait for workers to shutdown. */
235 lprintf(CTDL_INFO, "Server is shutting down.\n");