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 void go_threading(void);
79 * Here's where it all begins.
81 int main(int argc, char **argv)
84 int a; /* General-purpose variables */
85 struct passwd pw, *pwp = NULL;
87 int drop_root_perms = 1;
92 char relhome[PATH_MAX]="";
93 char ctdldir[PATH_MAX]=CTDLDIR;
95 struct stat filestats;
98 eCrashParameters params;
99 // eCrashSymbolTable symbol_table;
101 /* initialise semaphores here. Patch by Matt and davew
102 * its called here as they are needed by CtdlLogPrintf for thread safety
104 CtdlInitBase64Table();
105 InitialiseSemaphores();
107 /* initialize the master context */
108 InitializeMasterCC();
110 /* parse command-line arguments */
111 for (a=1; a<argc; ++a) {
113 if (!strncmp(argv[a], "-l", 2)) {
114 safestrncpy(facility, &argv[a][2], sizeof(facility));
115 syslog_facility = SyslogFacility(facility);
119 /* run in the background if -d was specified */
120 else if (!strcmp(argv[a], "-d")) {
121 running_as_daemon = 1;
124 /* -x specifies the desired logging level */
125 else if (!strncmp(argv[a], "-x", 2)) {
126 verbosity = atoi(&argv[a][2]);
129 else if (!strncmp(argv[a], "-h", 2)) {
130 relh=argv[a][2]!='/';
131 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
132 sizeof ctdl_home_directory);
134 safestrncpy(relhome, &argv[a][2],
140 else if (!strncmp(argv[a], "-t", 2)) {
141 freopen(&argv[a][2], "w", stderr);
144 else if (!strncmp(argv[a], "-f", 2)) {
148 else if (!strncmp(argv[a], "-D", 2)) {
152 /* -r tells the server not to drop root permissions. don't use
153 * this unless you know what you're doing. this should be
154 * removed in the next release if it proves unnecessary. */
155 else if (!strcmp(argv[a], "-r"))
158 /* any other parameter makes it crash and burn */
160 CtdlLogPrintf(CTDL_EMERG, "citserver: usage: "
165 " [-xLogLevel] [-hHomeDir]\n");
171 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
172 /* daemonize, if we were asked to */
173 if (running_as_daemon) {
178 #ifdef HAVE_BACKTRACE
179 bzero(¶ms, sizeof(params));
180 params.filename = file_pid_paniclog;
181 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
182 params.filep = fopen(file_pid_paniclog, "a+");
183 params.debugLevel = ECRASH_DEBUG_VERBOSE;
184 params.dumpAllThreads = TRUE;
185 params.useBacktraceSymbols = 1;
186 /// BuildSymbolTable(&symbol_table);
187 // params.symbolTable = &symbol_table;
188 params.signals[0]=SIGSEGV;
189 params.signals[1]=SIGILL;
190 params.signals[2]=SIGBUS;
191 params.signals[3]=SIGABRT;
193 eCrash_Init(¶ms);
195 eCrash_RegisterThread("MasterThread", 0);
197 /// signal(SIGSEGV, cit_panic_backtrace);
199 /* Initialize the syslogger. Yes, we are really using 0 as the
200 * facility, because we are going to bitwise-OR the facility to
201 * the severity of each message, allowing us to write to other
202 * facilities when we need to...
205 openlog("citadel", LOG_NDELAY, 0);
206 setlogmask(LOG_UPTO(verbosity));
209 /* Tell 'em who's in da house */
210 CtdlLogPrintf(CTDL_NOTICE, "\n");
211 CtdlLogPrintf(CTDL_NOTICE, "\n");
212 CtdlLogPrintf(CTDL_NOTICE,
213 "*** Citadel server engine v%d.%02d ***\n",
214 (REV_LEVEL/100), (REV_LEVEL%100));
215 CtdlLogPrintf(CTDL_NOTICE,
216 "Copyright (C) 1987-2007 by the Citadel development team.\n");
217 CtdlLogPrintf(CTDL_NOTICE,
218 "This program is distributed under the terms of the GNU "
219 "General Public License.\n");
220 CtdlLogPrintf(CTDL_NOTICE, "\n");
221 CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
222 CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
224 /* Load site-specific parameters, and set the ipgm secret */
225 CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
227 config.c_ipgm_secret = rand();
231 /* on some dists rundir gets purged on startup. so we need to recreate it. */
233 if (stat(ctdl_run_dir, &filestats)==-1){
234 #ifdef SOLARIS_GETPWUID
235 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
237 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
239 mkdir(ctdl_run_dir, 0755);
240 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
250 * Do non system dependent startup functions.
254 CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
258 * Bind the server to a Unix-domain socket.
260 CtdlRegisterServiceHook(0,
262 citproto_begin_session,
268 * Bind the server to our favorite TCP port (usually 504).
270 CtdlRegisterServiceHook(config.c_port_number,
272 citproto_begin_session,
279 * Run any upgrade entry points
281 CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
286 * Load any server-side extensions available here.
288 CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
289 size = strlen(ctdl_home_directory) + 9;
291 initialise_modules(0);
296 * If we need host auth, start our chkpwd daemon.
298 if (config.c_auth_mode == 1) {
299 start_chkpwd_daemon();
303 * Now that we've bound the sockets, change to the Citadel user id and its
304 * corresponding group ids
306 if (drop_root_perms) {
307 cdb_chmod_data(); /* make sure we own our data files */
309 #ifdef SOLARIS_GETPWUID
310 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
312 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
315 CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
316 "Group IDs will be incorrect.\n", (long)CTDLUID,
319 initgroups(pw.pw_name, pw.pw_gid);
320 if (setgid(pw.pw_gid))
321 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
324 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
325 if (setuid(CTDLUID) != 0) {
326 CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
328 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
329 prctl(PR_SET_DUMPABLE, 1);
333 /* We want to check for idle sessions once per minute */
334 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
339 master_cleanup(exit_signal);
345 void go_threading(void)
348 struct CtdlThreadNode *last_worker;
351 * Initialise the thread system
353 ctdl_thread_internal_init();
355 * Now create a bunch of worker threads.
357 CtdlLogPrintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers);
358 begin_critical_section(S_THREAD_LIST);
359 i=0; /* Always start at least 1 worker thread */
362 ctdl_internal_create_thread("Worker Thread", CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER, worker_thread, NULL);
363 } while (++i < config.c_min_workers);
364 end_critical_section(S_THREAD_LIST);
366 /* Second call to module init functions now that threading is up */
367 initialise_modules(1);
370 * This thread is now used for garbage collection of other threads in the thread list
372 CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", pthread_self());
375 * We do a lot of locking and unlocking of the thread list in here.
376 * We do this so that we can repeatedly release time for other threads
377 * that may be waiting on the thread list.
378 * We are a low priority thread so we can afford to do this
381 while (CtdlThreadGetCount())
385 check_sched_shutdown();
386 if (CT->state > CTDL_THREAD_STOP_REQ)
388 begin_critical_section(S_THREAD_LIST);
389 ctdl_thread_internal_calc_loadavg();
390 end_critical_section(S_THREAD_LIST);
392 ctdl_thread_internal_check_scheduled(); /* start scheduled threads */
395 /* Reduce the size of the worker thread pool if necessary. */
396 if ((CtdlThreadGetWorkers() > config.c_min_workers) && (CtdlThreadWorkerAvg < 20) && (CT->state > CTDL_THREAD_STOP_REQ))
398 /* Ask a worker thread to stop as we no longer need it */
399 begin_critical_section(S_THREAD_LIST);
400 last_worker = CtdlThreadList;
403 pthread_mutex_lock(&last_worker->ThreadMutex);
404 if (last_worker->flags & CTDLTHREAD_WORKER && last_worker->state > CTDL_THREAD_STOPPING)
406 pthread_mutex_unlock(&last_worker->ThreadMutex);
409 pthread_mutex_unlock(&last_worker->ThreadMutex);
410 last_worker = last_worker->next;
412 end_critical_section(S_THREAD_LIST);
415 #ifdef WITH_THREADLOG
416 CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (%ld).\n",
421 CtdlThreadStop(last_worker);
426 * If all our workers are working hard, start some more to help out
429 /* FIXME: come up with a better way to dynamically alter the number of threads
430 * based on the system load
432 // if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() < num_sessions))
433 // && (CtdlThreadLoadAvg < 90) )
434 if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) && (CT->state > CTDL_THREAD_STOP_REQ))
437 // for (i=0; i< (num_sessions - CtdlThreadGetWorkers()) ; i++)
438 // for (i=0; i< (10 - (55 - CtdlThreadWorkerAvg) / CtdlThreadWorkerAvg / CtdlThreadGetWorkers()) ; i++)
440 // begin_critical_section(S_THREAD_LIST);
441 CtdlThreadCreate("Worker Thread",
442 CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
446 // end_critical_section(S_THREAD_LIST);
452 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
457 if (CtdlThreadGetCount())
461 * If the above loop exits we must be shutting down since we obviously have no threads
463 ctdl_thread_internal_cleanup();