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.c"
60 #include "modules_init.h"
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
70 const char *CitadelServiceUDS="citadel-UDS";
71 const char *CitadelServiceTCP="citadel-TCP";
74 extern struct CitContext masterCC;
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;
102 /* initialise semaphores here. Patch by Matt and davew
103 * its called here as they are needed by CtdlLogPrintf for thread safety
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], "-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 CtdlLogPrintf(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 params.signals[0]=SIGSEGV;
183 params.signals[1]=SIGILL;
184 params.signals[2]=SIGBUS;
185 params.signals[3]=SIGABRT;
186 eCrash_Init(¶ms);
187 eCrash_RegisterThread("MasterThread", 0);
190 /* Initialize the syslogger. Yes, we are really using 0 as the
191 * facility, because we are going to bitwise-OR the facility to
192 * the severity of each message, allowing us to write to other
193 * facilities when we need to...
196 openlog("citadel", LOG_NDELAY, 0);
197 setlogmask(LOG_UPTO(verbosity));
200 /* Tell 'em who's in da house */
201 CtdlLogPrintf(CTDL_NOTICE, "\n");
202 CtdlLogPrintf(CTDL_NOTICE, "\n");
203 CtdlLogPrintf(CTDL_NOTICE,
204 "*** Citadel server engine v%d.%02d (build %s) ***\n",
205 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
206 CtdlLogPrintf(CTDL_NOTICE, "Copyright (C) 1987-2008 by the Citadel development team.\n");
207 CtdlLogPrintf(CTDL_NOTICE, "This program is distributed under the terms of the GNU "
208 "General Public License.\n");
209 CtdlLogPrintf(CTDL_NOTICE, "\n");
210 CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
211 CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
213 /* Load site-specific parameters, and set the ipgm secret */
214 CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
216 config.c_ipgm_secret = rand();
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 HAVE_GETPWUID_R
224 #ifdef SOLARIS_GETPWUID
225 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
226 #else // SOLARIS_GETPWUID
227 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
228 #endif // SOLARIS_GETPWUID
229 #else // HAVE_GETPWUID_R
231 #endif // HAVE_GETPWUID_R
232 mkdir(ctdl_run_dir, 0755);
233 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
243 * Do non system dependent startup functions.
247 CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
252 * Initialise the user 0 to have a name. It would be nice to do it in InitializeMasterCC
253 * since it is contained within the MasterCC but we can't because the DB isn't available
254 * at that time so we do it seperate.
256 /** Give user 0 a name and create them if necessary */
257 if (getuser(&masterCC.user, "Citadel"))
259 getuserbynumber(&masterCC.user, 0);
260 strcpy (masterCC.user.fullname, "Citadel");
261 putuser(&masterCC.user);
265 * Bind the server to a Unix-domain socket.
267 CtdlRegisterServiceHook(0,
269 citproto_begin_session,
275 * Bind the server to our favorite TCP port (usually 504).
277 CtdlRegisterServiceHook(config.c_port_number,
279 citproto_begin_session,
286 * Run any upgrade entry points
288 CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
293 * Load any server-side extensions available here.
295 CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
296 size = strlen(ctdl_home_directory) + 9;
298 initialise_modules(0);
303 * If we need host auth, start our chkpwd daemon.
305 if (config.c_auth_mode == AUTHMODE_HOST) {
306 start_chkpwd_daemon();
310 * Now that we've bound the sockets, change to the Citadel user id and its
311 * corresponding group ids
313 if (drop_root_perms) {
314 cdb_chmod_data(); /* make sure we own our data files */
316 #ifdef HAVE_GETPWUID_R
317 #ifdef SOLARIS_GETPWUID
318 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
319 #else // SOLARIS_GETPWUID
320 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
321 #endif // SOLARIS_GETPWUID
322 #else // HAVE_GETPWUID_R
324 #endif // HAVE_GETPWUID_R
327 CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
328 "Group IDs will be incorrect.\n", (long)CTDLUID,
331 initgroups(pw.pw_name, pw.pw_gid);
332 if (setgid(pw.pw_gid))
333 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
336 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
337 if (setuid(CTDLUID) != 0) {
338 CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
340 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
341 prctl(PR_SET_DUMPABLE, 1);
345 /* We want to check for idle sessions once per minute */
346 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
351 master_cleanup(exit_signal);