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 lprintf 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 lprintf(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 lprintf(CTDL_NOTICE, "\n");
211 lprintf(CTDL_NOTICE, "\n");
213 "*** Citadel server engine v%d.%02d ***\n",
214 (REV_LEVEL/100), (REV_LEVEL%100));
216 "Copyright (C) 1987-2007 by the Citadel development team.\n");
218 "This program is distributed under the terms of the GNU "
219 "General Public License.\n");
220 lprintf(CTDL_NOTICE, "\n");
221 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
222 lprintf(CTDL_INFO, "%s\n", libcitadel_version_string());
224 /* Load site-specific parameters, and set the ipgm secret */
225 lprintf(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 lprintf(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,
278 * Load any server-side extensions available here.
280 lprintf(CTDL_INFO, "Initializing server extensions\n");
281 size = strlen(ctdl_home_directory) + 9;
283 initialise_modules(0);
288 * If we need host auth, start our chkpwd daemon.
290 if (config.c_auth_mode == 1) {
291 start_chkpwd_daemon();
295 * Now that we've bound the sockets, change to the Citadel user id and its
296 * corresponding group ids
298 if (drop_root_perms) {
299 cdb_chmod_data(); /* make sure we own our data files */
301 #ifdef SOLARIS_GETPWUID
302 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
304 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
307 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
308 "Group IDs will be incorrect.\n", (long)CTDLUID,
311 initgroups(pw.pw_name, pw.pw_gid);
312 if (setgid(pw.pw_gid))
313 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
316 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
317 if (setuid(CTDLUID) != 0) {
318 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
320 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
321 prctl(PR_SET_DUMPABLE, 1);
325 /* We want to check for idle sessions once per minute */
326 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
331 master_cleanup(exit_signal);
337 void go_threading(void)
340 struct CtdlThreadNode *last_worker;
342 /* We can't use CT_PUSH() here so we do it the long way
343 * So we can still use CT for current thread */
344 struct CtdlThreadNode *_this_cit_thread;
347 * Initialise the thread system
349 ctdl_thread_internal_init();
350 _this_cit_thread = CtdlThreadSelf();
352 * Now create a bunch of worker threads.
354 CtdlLogPrintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers);
355 begin_critical_section(S_THREAD_LIST);
356 i=0; /* Always start at least 1 worker thread */
359 ctdl_internal_create_thread("Worker Thread", CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER, worker_thread, NULL);
360 } while (++i < config.c_min_workers);
361 end_critical_section(S_THREAD_LIST);
363 /* Second call to module init functions now that threading is up */
364 initialise_modules(1);
367 * This thread is now used for garbage collection of other threads in the thread list
369 CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", pthread_self());
372 * We do a lot of locking and unlocking of the thread list in here.
373 * We do this so that we can repeatedly release time for other threads
374 * that may be waiting on the thread list.
375 * We are a low priority thread so we can afford to do this
378 while (CtdlThreadGetCount())
382 check_sched_shutdown();
383 begin_critical_section(S_THREAD_LIST);
384 ctdl_thread_internal_calc_loadavg();
385 end_critical_section(S_THREAD_LIST);
387 /* Reduce the size of the worker thread pool if necessary. */
388 if ((CtdlThreadGetWorkers() > config.c_min_workers) && (CtdlThreadWorkerAvg < 20))
390 /* Ask a worker thread to stop as we no longer need it */
391 begin_critical_section(S_THREAD_LIST);
392 last_worker = CtdlThreadList;
395 pthread_mutex_lock(&last_worker->ThreadMutex);
396 if (last_worker->flags & CTDLTHREAD_WORKER && last_worker->state > CTDL_THREAD_STOPPING)
398 pthread_mutex_unlock(&last_worker->ThreadMutex);
401 pthread_mutex_unlock(&last_worker->ThreadMutex);
402 last_worker = last_worker->next;
404 end_critical_section(S_THREAD_LIST);
407 #ifdef WITH_THREADLOG
408 CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (%ld).\n",
413 CtdlThreadStop(last_worker);
418 * If all our workers are working hard, start some more to help out
421 /* FIXME: come up with a better way to dynamically alter the number of threads
422 * based on the system load
424 // if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() < num_sessions))
425 // && (CtdlThreadLoadAvg < 90) )
426 if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) )
429 // for (i=0; i< (num_sessions - CtdlThreadGetWorkers()) ; i++)
430 // for (i=0; i< (10 - (55 - CtdlThreadWorkerAvg) / CtdlThreadWorkerAvg / CtdlThreadGetWorkers()) ; i++)
432 // begin_critical_section(S_THREAD_LIST);
433 CtdlThreadCreate("Worker Thread",
434 CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
438 // end_critical_section(S_THREAD_LIST);
444 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
449 if (CtdlThreadGetCount())
453 * If the above loop exits we must be shutting down since we obviously have no threads
455 ctdl_thread_internal_cleanup();