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>
49 #include "serv_extensions.h"
50 #include "sysdep_decls.h"
51 #include "citserver.h"
57 #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;
87 char relhome[PATH_MAX]="";
88 char ctdldir[PATH_MAX]=CTDLDIR;
90 struct stat filestats;
92 eCrashParameters params;
93 // eCrashSymbolTable symbol_table;
95 /* initialise semaphores here. Patch by Matt and davew
96 * its called here as they are needed by lprintf for thread safety
98 InitialiseSemaphores();
100 /* initialize the master context */
101 InitializeMasterCC();
103 /* parse command-line arguments */
104 for (a=1; a<argc; ++a) {
106 if (!strncmp(argv[a], "-l", 2)) {
107 safestrncpy(facility, &argv[a][2], sizeof(facility));
108 syslog_facility = SyslogFacility(facility);
112 /* run in the background if -d was specified */
113 else if (!strcmp(argv[a], "-d")) {
114 running_as_daemon = 1;
117 /* -x specifies the desired logging level */
118 else if (!strncmp(argv[a], "-x", 2)) {
119 verbosity = atoi(&argv[a][2]);
122 else if (!strncmp(argv[a], "-h", 2)) {
123 relh=argv[a][2]!='/';
124 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
125 sizeof ctdl_home_directory);
127 safestrncpy(relhome, &argv[a][2],
133 else if (!strncmp(argv[a], "-t", 2)) {
134 freopen(&argv[a][2], "w", stderr);
137 else if (!strncmp(argv[a], "-f", 2)) {
141 /* -r tells the server not to drop root permissions. don't use
142 * this unless you know what you're doing. this should be
143 * removed in the next release if it proves unnecessary. */
144 else if (!strcmp(argv[a], "-r"))
147 /* any other parameter makes it crash and burn */
149 lprintf(CTDL_EMERG, "citserver: usage: "
154 " [-xLogLevel] [-hHomeDir]\n");
160 calc_dirs_n_files(relh, home, relhome, ctdldir);
161 /* daemonize, if we were asked to */
162 if (running_as_daemon) {
167 bzero(¶ms, sizeof(params));
168 params.filename = file_pid_paniclog;
169 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
170 params.filep = fopen(file_pid_paniclog, "a+");
171 params.debugLevel = ECRASH_DEBUG_VERBOSE;
172 params.dumpAllThreads = TRUE;
173 params.useBacktraceSymbols = 1;
174 /// BuildSymbolTable(&symbol_table);
175 // params.symbolTable = &symbol_table;
176 params.signals[0]=SIGSEGV;
177 params.signals[1]=SIGILL;
178 params.signals[2]=SIGBUS;
179 params.signals[3]=SIGABRT;
181 eCrash_Init(¶ms);
183 eCrash_RegisterThread("MasterThread", 0);
185 /// signal(SIGSEGV, cit_panic_backtrace);
186 /* Initialize the syslogger. Yes, we are really using 0 as the
187 * facility, because we are going to bitwise-OR the facility to
188 * the severity of each message, allowing us to write to other
189 * facilities when we need to...
192 openlog("citadel", LOG_NDELAY, 0);
193 setlogmask(LOG_UPTO(verbosity));
196 /* Tell 'em who's in da house */
197 lprintf(CTDL_NOTICE, "\n");
198 lprintf(CTDL_NOTICE, "\n");
200 "*** Citadel server engine v%d.%02d ***\n",
201 (REV_LEVEL/100), (REV_LEVEL%100));
203 "Copyright (C) 1987-2007 by the Citadel development team.\n");
205 "This program is distributed under the terms of the GNU "
206 "General Public License.\n");
207 lprintf(CTDL_NOTICE, "\n");
208 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
210 /* Load site-specific parameters, and set the ipgm secret */
211 lprintf(CTDL_INFO, "Loading citadel.config\n");
213 config.c_ipgm_secret = rand();
216 lprintf(CTDL_INFO, "Acquiring control record\n");
220 /* on some dists rundir gets purged on startup. so we need to recreate it. */
222 if (stat(ctdl_run_dir, &filestats)==-1){
223 #ifdef SOLARIS_GETPWUID
224 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
226 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
228 mkdir(ctdl_run_dir, 0755);
229 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
239 * Do non system dependent startup functions.
244 * Bind the server to a Unix-domain socket.
246 CtdlRegisterServiceHook(0,
248 citproto_begin_session,
254 * Bind the server to our favorite TCP port (usually 504).
256 CtdlRegisterServiceHook(config.c_port_number,
258 citproto_begin_session,
264 * Load any server-side extensions available here.
266 lprintf(CTDL_INFO, "Initializing server extensions\n");
267 size = strlen(ctdl_home_directory) + 9;
270 initialize_server_extensions();
273 initialise_modules();
278 * If we need host auth, start our chkpwd daemon.
280 if (config.c_auth_mode == 1) {
281 start_chkpwd_daemon();
285 * Now that we've bound the sockets, change to the Citadel user id and its
286 * corresponding group ids
288 if (drop_root_perms) {
289 cdb_chmod_data(); /* make sure we own our data files */
291 #ifdef SOLARIS_GETPWUID
292 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
294 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
297 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
298 "Group IDs will be incorrect.\n", (long)CTDLUID,
301 initgroups(pw.pw_name, pw.pw_gid);
302 if (setgid(pw.pw_gid))
303 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
306 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
307 if (setuid(CTDLUID) != 0) {
308 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
310 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
311 prctl(PR_SET_DUMPABLE, 1);
315 /* We want to check for idle sessions once per minute */
316 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
319 * Now create a bunch of worker threads.
321 lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
322 config.c_min_workers-1);
323 begin_critical_section(S_WORKER_LIST);
324 for (i=0; i<(config.c_min_workers-1); ++i) {
327 end_critical_section(S_WORKER_LIST);
329 /* Create the maintenance threads. */
330 create_maintenance_threads();
332 /* This thread is now useless. It can't be turned into a worker
333 * thread because its stack is too small, but it can't be killed
334 * either because the whole server process would exit. So we just
335 * join to the first worker thread and exit when it exits.
337 pthread_join(worker_list->tid, NULL);