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>
44 #ifdef HAVE_SYS_PRCTL_H
45 #include <sys/prctl.h>
47 #include <libcitadel.h>
50 #include "serv_extensions.h"
51 #include "sysdep_decls.h"
52 #include "citserver.h"
58 #include "housekeeping.h"
59 #include "citadel_dirs.c"
61 #include "modules_init.h"
64 #ifdef HAVE_SYS_SELECT_H
65 #include <sys/select.h>
71 const char *CitadelServiceUDS="citadel-UDS";
72 const char *CitadelServiceTCP="citadel-TCP";
75 * Here's where it all begins.
77 int main(int argc, char **argv)
80 int a, i; /* General-purpose variables */
81 struct passwd pw, *pwp = NULL;
83 int drop_root_perms = 1;
88 char relhome[PATH_MAX]="";
89 char ctdldir[PATH_MAX]=CTDLDIR;
91 struct stat filestats;
94 eCrashParameters params;
95 // eCrashSymbolTable symbol_table;
97 /* initialise semaphores here. Patch by Matt and davew
98 * its called here as they are needed by lprintf for thread safety
100 CtdlInitBase64Table();
101 InitialiseSemaphores();
103 /* initialize the master context */
104 InitializeMasterCC();
106 /* parse command-line arguments */
107 for (a=1; a<argc; ++a) {
109 if (!strncmp(argv[a], "-l", 2)) {
110 safestrncpy(facility, &argv[a][2], sizeof(facility));
111 syslog_facility = SyslogFacility(facility);
115 /* run in the background if -d was specified */
116 else if (!strcmp(argv[a], "-d")) {
117 running_as_daemon = 1;
120 /* -x specifies the desired logging level */
121 else if (!strncmp(argv[a], "-x", 2)) {
122 verbosity = atoi(&argv[a][2]);
125 else if (!strncmp(argv[a], "-h", 2)) {
126 relh=argv[a][2]!='/';
127 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
128 sizeof ctdl_home_directory);
130 safestrncpy(relhome, &argv[a][2],
136 else if (!strncmp(argv[a], "-t", 2)) {
137 freopen(&argv[a][2], "w", stderr);
140 else if (!strncmp(argv[a], "-f", 2)) {
144 else if (!strncmp(argv[a], "-D", 2)) {
148 /* -r tells the server not to drop root permissions. don't use
149 * this unless you know what you're doing. this should be
150 * removed in the next release if it proves unnecessary. */
151 else if (!strcmp(argv[a], "-r"))
154 /* any other parameter makes it crash and burn */
156 lprintf(CTDL_EMERG, "citserver: usage: "
161 " [-xLogLevel] [-hHomeDir]\n");
167 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
168 /* daemonize, if we were asked to */
169 if (running_as_daemon) {
174 #ifdef HAVE_BACKTRACE
175 bzero(¶ms, sizeof(params));
176 params.filename = file_pid_paniclog;
177 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
178 params.filep = fopen(file_pid_paniclog, "a+");
179 params.debugLevel = ECRASH_DEBUG_VERBOSE;
180 params.dumpAllThreads = TRUE;
181 params.useBacktraceSymbols = 1;
182 /// BuildSymbolTable(&symbol_table);
183 // params.symbolTable = &symbol_table;
184 params.signals[0]=SIGSEGV;
185 params.signals[1]=SIGILL;
186 params.signals[2]=SIGBUS;
187 params.signals[3]=SIGABRT;
189 eCrash_Init(¶ms);
191 eCrash_RegisterThread("MasterThread", 0);
193 /// signal(SIGSEGV, cit_panic_backtrace);
195 /* Initialize the syslogger. Yes, we are really using 0 as the
196 * facility, because we are going to bitwise-OR the facility to
197 * the severity of each message, allowing us to write to other
198 * facilities when we need to...
201 openlog("citadel", LOG_NDELAY, 0);
202 setlogmask(LOG_UPTO(verbosity));
205 /* Tell 'em who's in da house */
206 lprintf(CTDL_NOTICE, "\n");
207 lprintf(CTDL_NOTICE, "\n");
209 "*** Citadel server engine v%d.%02d ***\n",
210 (REV_LEVEL/100), (REV_LEVEL%100));
212 "Copyright (C) 1987-2007 by the Citadel development team.\n");
214 "This program is distributed under the terms of the GNU "
215 "General Public License.\n");
216 lprintf(CTDL_NOTICE, "\n");
217 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
218 lprintf(CTDL_INFO, "%s\n", libcitadel_version_string());
220 /* Load site-specific parameters, and set the ipgm secret */
221 lprintf(CTDL_INFO, "Loading citadel.config\n");
223 config.c_ipgm_secret = rand();
227 /* on some dists rundir gets purged on startup. so we need to recreate it. */
229 if (stat(ctdl_run_dir, &filestats)==-1){
230 #ifdef SOLARIS_GETPWUID
231 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
233 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
235 mkdir(ctdl_run_dir, 0755);
236 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
246 * Do non system dependent startup functions.
250 lprintf(CTDL_INFO, "Acquiring control record\n");
254 * Bind the server to a Unix-domain socket.
256 CtdlRegisterServiceHook(0,
258 citproto_begin_session,
264 * Bind the server to our favorite TCP port (usually 504).
266 CtdlRegisterServiceHook(config.c_port_number,
268 citproto_begin_session,
274 * Load any server-side extensions available here.
276 lprintf(CTDL_INFO, "Initializing server extensions\n");
277 size = strlen(ctdl_home_directory) + 9;
279 initialise_modules(0);
284 * If we need host auth, start our chkpwd daemon.
286 if (config.c_auth_mode == 1) {
287 start_chkpwd_daemon();
291 * Now that we've bound the sockets, change to the Citadel user id and its
292 * corresponding group ids
294 if (drop_root_perms) {
295 cdb_chmod_data(); /* make sure we own our data files */
297 #ifdef SOLARIS_GETPWUID
298 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
300 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
303 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
304 "Group IDs will be incorrect.\n", (long)CTDLUID,
307 initgroups(pw.pw_name, pw.pw_gid);
308 if (setgid(pw.pw_gid))
309 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
312 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
313 if (setuid(CTDLUID) != 0) {
314 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
316 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
317 prctl(PR_SET_DUMPABLE, 1);
321 /* We want to check for idle sessions once per minute */
322 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
325 * Initialise the thread system
327 ctdl_thread_internal_init();
330 * Now create a bunch of worker threads.
332 CtdlLogPrintf(CTDL_DEBUG, "Starting %d worker threads\n",
333 config.c_min_workers-1);
334 begin_critical_section(S_THREAD_LIST);
335 for (i=0; i<(config.c_min_workers-1); ++i) {
336 ctdl_internal_create_thread("Worker Thread", CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER, worker_thread, NULL);
338 end_critical_section(S_THREAD_LIST);
340 /* Second call to module init functions now that threading is up */
341 initialise_modules(1);
344 * This thread is now used for garbage collection of other threads in the thread list
346 CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", pthread_self());
348 /* Sleep 10 seconds before first garbage collection */
351 while (CtdlThreadGetCount())
353 begin_critical_section(S_THREAD_LIST);
354 ctdl_thread_internal_calc_loadavg();
355 end_critical_section(S_THREAD_LIST);
357 begin_critical_section(S_THREAD_LIST);
358 ctdl_internal_thread_gc();
359 end_critical_section(S_THREAD_LIST);
360 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
362 begin_critical_section(S_THREAD_LIST);
363 ctdl_internal_thread_gc();
364 end_critical_section(S_THREAD_LIST);
368 * If the above loop exits we must be shutting down since we obviously have no threads
370 ctdl_thread_internal_cleanup();
372 master_cleanup(exit_signal);