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>
41 #ifdef HAVE_SYS_PRCTL_H
42 #include <sys/prctl.h>
44 #include <libcitadel.h>
47 #include "serv_extensions.h"
48 #include "sysdep_decls.h"
50 #include "citserver.h"
56 #include "housekeeping.h"
57 #include "svn_revision.h"
58 #include "citadel_dirs.h"
62 #include "modules_init.h"
65 #ifdef HAVE_SYS_SELECT_H
66 #include <sys/select.h>
72 const char *CitadelServiceUDS="citadel-UDS";
73 const char *CitadelServiceTCP="citadel-TCP";
77 void go_threading(void);
80 * Here's where it all begins.
82 int main(int argc, char **argv)
85 int a; /* General-purpose variables */
86 struct passwd pw, *pwp = NULL;
88 int drop_root_perms = 1;
93 char relhome[PATH_MAX]="";
94 char ctdldir[PATH_MAX]=CTDLDIR;
96 struct stat filestats;
99 eCrashParameters params;
100 // eCrashSymbolTable symbol_table;
109 /* initialise semaphores here. Patch by Matt and davew
110 * its called here as they are needed by CtdlLogPrintf for thread safety
112 InitialiseSemaphores();
114 /* initialize the master context */
115 InitializeMasterCC();
117 /* parse command-line arguments */
118 for (a=1; a<argc; ++a) {
120 if (!strncmp(argv[a], "-l", 2)) {
121 safestrncpy(facility, &argv[a][2], sizeof(facility));
122 syslog_facility = SyslogFacility(facility);
126 /* run in the background if -d was specified */
127 else if (!strcmp(argv[a], "-d")) {
128 running_as_daemon = 1;
131 /* run a few stats if -s was specified */
132 else if (!strncmp(argv[a], "-s", 2)) {
133 statcount = atoi(&argv[a][2]);
136 /* -x specifies the desired logging level */
137 else if (!strncmp(argv[a], "-x", 2)) {
138 verbosity = atoi(&argv[a][2]);
141 else if (!strncmp(argv[a], "-h", 2)) {
142 relh=argv[a][2]!='/';
143 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
144 sizeof ctdl_home_directory);
146 safestrncpy(relhome, &argv[a][2],
151 else if (!strncmp(argv[a], "-t", 2)) {
152 freopen(&argv[a][2], "w", stderr);
155 else if (!strncmp(argv[a], "-D", 2)) {
159 /* -r tells the server not to drop root permissions. don't use
160 * this unless you know what you're doing. this should be
161 * removed in the next release if it proves unnecessary. */
162 else if (!strcmp(argv[a], "-r"))
165 /* any other parameter makes it crash and burn */
167 CtdlLogPrintf(CTDL_EMERG, "citserver: usage: "
172 " [-xLogLevel] [-hHomeDir]\n");
178 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
179 /* daemonize, if we were asked to */
180 if (running_as_daemon) {
185 #ifdef HAVE_BACKTRACE
186 bzero(¶ms, sizeof(params));
187 params.filename = file_pid_paniclog;
188 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
189 params.filep = fopen(file_pid_paniclog, "a+");
190 params.debugLevel = ECRASH_DEBUG_VERBOSE;
191 params.dumpAllThreads = TRUE;
192 params.useBacktraceSymbols = 1;
193 params.signals[0]=SIGSEGV;
194 params.signals[1]=SIGILL;
195 params.signals[2]=SIGBUS;
196 params.signals[3]=SIGABRT;
197 eCrash_Init(¶ms);
198 eCrash_RegisterThread("MasterThread", 0);
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 openlog("citadel", LOG_NDELAY, 0);
208 setlogmask(LOG_UPTO(verbosity));
211 /* Tell 'em who's in da house */
212 CtdlLogPrintf(CTDL_NOTICE, "\n");
213 CtdlLogPrintf(CTDL_NOTICE, "\n");
214 CtdlLogPrintf(CTDL_NOTICE,
215 "*** Citadel server engine v%d.%02d (build %s) ***\n",
216 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
217 CtdlLogPrintf(CTDL_NOTICE, "Copyright (C) 1987-2008 by the Citadel development team.\n");
218 CtdlLogPrintf(CTDL_NOTICE, "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();
229 /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
230 CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
236 /* on some dists rundir gets purged on startup. so we need to recreate it. */
238 if (stat(ctdl_run_dir, &filestats)==-1){
239 #ifdef HAVE_GETPWUID_R
240 #ifdef SOLARIS_GETPWUID
241 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
242 #else // SOLARIS_GETPWUID
243 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
244 #endif // SOLARIS_GETPWUID
245 #else // HAVE_GETPWUID_R
247 #endif // HAVE_GETPWUID_R
248 mkdir(ctdl_run_dir, 0755);
249 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
259 * Do non system dependent startup functions.
264 * Check that the control record is correct and place sensible values if it isn't
269 * Run any upgrade entry points
271 CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
275 * Load the user for the masterCC or create them if they don't exist
277 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
279 /** User doesn't exist. We can't use create user here as the user number needs to be 0 */
280 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
281 CtdlPutUser(&masterCC.user);
282 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /** Just to be safe */
286 * Bind the server to a Unix-domain socket.
288 CtdlRegisterServiceHook(0,
290 citproto_begin_session,
296 * Bind the server to our favorite TCP port (usually 504).
298 CtdlRegisterServiceHook(config.c_port_number,
300 citproto_begin_session,
309 * Load any server-side extensions available here.
311 CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
312 size = strlen(ctdl_home_directory) + 9;
314 initialise_modules(0);
319 * If we need host auth, start our chkpwd daemon.
321 if (config.c_auth_mode == AUTHMODE_HOST) {
322 start_chkpwd_daemon();
327 * check, whether we're fired up another time after a crash.
328 * if, post an aide message, so the admin has a chance to react.
334 * Now that we've bound the sockets, change to the Citadel user id and its
335 * corresponding group ids
337 if (drop_root_perms) {
338 cdb_chmod_data(); /* make sure we own our data files */
340 #ifdef HAVE_GETPWUID_R
341 #ifdef SOLARIS_GETPWUID
342 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
343 #else // SOLARIS_GETPWUID
344 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
345 #endif // SOLARIS_GETPWUID
346 #else // HAVE_GETPWUID_R
348 #endif // HAVE_GETPWUID_R
351 CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
352 "Group IDs will be incorrect.\n", (long)CTDLUID,
355 initgroups(pw.pw_name, pw.pw_gid);
356 if (setgid(pw.pw_gid))
357 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
360 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
361 if (setuid(CTDLUID) != 0) {
362 CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
364 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
365 prctl(PR_SET_DUMPABLE, 1);
369 /* We want to check for idle sessions once per minute */
370 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
375 master_cleanup(exit_signal);