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>
65 int running_as_daemon = 0;
68 * Here's where it all begins.
70 int main(int argc, char **argv)
73 int a, i; /* General-purpose variables */
75 int drop_root_perms = 1;
80 char dirbuffer[PATH_MAX]="";
81 char relhome[PATH_MAX]="";
82 char ctdldir[PATH_MAX]=CTDLDIR;
84 /* initialize the master context */
87 /* parse command-line arguments */
88 for (a=1; a<argc; ++a) {
90 if (!strncmp(argv[a], "-l", 2)) {
91 safestrncpy(facility, &argv[a][2], sizeof(facility));
92 syslog_facility = SyslogFacility(facility);
96 /* run in the background if -d was specified */
97 else if (!strcmp(argv[a], "-d")) {
98 running_as_daemon = 1;
101 /* -x specifies the desired logging level */
102 else if (!strncmp(argv[a], "-x", 2)) {
103 verbosity = atoi(&argv[a][2]);
106 else if (!strncmp(argv[a], "-h", 2)) {
107 relh=argv[a][2]!='/';
108 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
109 sizeof ctdl_home_directory);
111 safestrncpy(relhome, &argv[a][2],
117 else if (!strncmp(argv[a], "-t", 2)) {
118 freopen(&argv[a][2], "w", stderr);
121 else if (!strncmp(argv[a], "-f", 2)) {
125 /* -r tells the server not to drop root permissions. don't use
126 * this unless you know what you're doing. this should be
127 * removed in the next release if it proves unnecessary. */
128 else if (!strcmp(argv[a], "-r"))
131 /* any other parameter makes it crash and burn */
133 lprintf(CTDL_EMERG, "citserver: usage: "
138 " [-xLogLevel] [-hHomeDir]\n");
144 /* calculate all our path on a central place */
145 /* where to keep our config */
147 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
148 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
149 (home&!relh)?ctdl_home_directory:basedir, \
150 ((basedir!=ctdldir)&(home&!relh))?basedir:"/", \
151 ((basedir!=ctdldir)&(home&!relh))?"/":"", \
153 (relhome[0]!='\0')?"/":"",\
155 (dirbuffer[0]!='\0')?"/":"");
162 COMPUTE_DIRECTORY(ctdl_etc_dir);
169 COMPUTE_DIRECTORY(ctdl_run_dir);
171 #ifndef HAVE_DATA_DIR
176 COMPUTE_DIRECTORY(ctdl_bio_dir);
177 COMPUTE_DIRECTORY(ctdl_bb_dir);
178 COMPUTE_DIRECTORY(ctdl_data_dir);
179 COMPUTE_DIRECTORY(ctdl_file_dir);
180 COMPUTE_DIRECTORY(ctdl_hlp_dir);
181 COMPUTE_DIRECTORY(ctdl_image_dir);
182 COMPUTE_DIRECTORY(ctdl_info_dir);
183 COMPUTE_DIRECTORY(ctdl_message_dir);
184 COMPUTE_DIRECTORY(ctdl_usrpic_dir);
185 #ifndef HAVE_SPOOL_DIR
190 COMPUTE_DIRECTORY(ctdl_spool_dir);
191 COMPUTE_DIRECTORY(ctdl_netout_dir);
192 COMPUTE_DIRECTORY(ctdl_netin_dir);
195 /* daemonize, if we were asked to */
196 if (running_as_daemon) {
201 /* Initialize the syslogger. Yes, we are really using 0 as the
202 * facility, because we are going to bitwise-OR the facility to
203 * the severity of each message, allowing us to write to other
204 * facilities when we need to...
207 if (running_as_daemon) {
208 openlog("citadel", LOG_NDELAY, 0);
211 openlog("citadel", LOG_PERROR|LOG_NDELAY, 0);
213 setlogmask(LOG_UPTO(verbosity));
216 /* Tell 'em who's in da house */
217 lprintf(CTDL_NOTICE, "\n");
218 lprintf(CTDL_NOTICE, "\n");
220 "*** Citadel server engine v%d.%02d ***\n",
221 (REV_LEVEL/100), (REV_LEVEL%100));
223 "Copyright (C) 1987-2005 by the Citadel development team.\n");
225 "This program is distributed under the terms of the GNU "
226 "General Public License.\n");
227 lprintf(CTDL_NOTICE, "\n");
228 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
230 /* Load site-specific parameters, and set the ipgm secret */
231 lprintf(CTDL_INFO, "Loading citadel.config\n");
233 config.c_ipgm_secret = rand();
240 * Do non system dependent startup functions.
245 * Bind the server to a Unix-domain socket.
247 CtdlRegisterServiceHook(0,
254 citproto_begin_session,
259 * Bind the server to our favorite TCP port (usually 504).
261 CtdlRegisterServiceHook(config.c_port_number,
263 citproto_begin_session,
268 * Load any server-side extensions available here.
270 lprintf(CTDL_INFO, "Initializing server extensions\n");
271 size = strlen(ctdl_home_directory) + 9;
272 initialize_server_extensions();
275 * Now that we've bound the sockets, change to the Citadel user id and its
276 * corresponding group ids
278 if (drop_root_perms) {
279 if ((pw = getpwuid(CTDLUID)) == NULL)
280 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
281 "Group IDs will be incorrect.\n", (long)CTDLUID,
284 initgroups(pw->pw_name, pw->pw_gid);
285 if (setgid(pw->pw_gid))
286 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
289 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
290 if (setuid(CTDLUID) != 0) {
291 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
293 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
294 prctl(PR_SET_DUMPABLE, 1);
298 /* We want to check for idle sessions once per minute */
299 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
302 * Now create a bunch of worker threads.
304 lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
305 config.c_min_workers-1);
306 begin_critical_section(S_WORKER_LIST);
307 for (i=0; i<(config.c_min_workers-1); ++i) {
310 end_critical_section(S_WORKER_LIST);
312 /* Create the indexer thread. */
313 create_maintenance_threads();
315 /* This thread is now useless. It can't be turned into a worker
316 * thread because its stack is too small, but it can't be killed
317 * either because the whole server process would exit. So we just
318 * join to the first worker thread and exit when it exits.
320 pthread_join(worker_list->tid, NULL);