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;
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 /* run a few stats if -s was specified */
125 else if (!strncmp(argv[a], "-s", 2)) {
126 statcount = atoi(&argv[a][2]);
129 /* -x specifies the desired logging level */
130 else if (!strncmp(argv[a], "-x", 2)) {
131 verbosity = atoi(&argv[a][2]);
134 else if (!strncmp(argv[a], "-h", 2)) {
135 relh=argv[a][2]!='/';
136 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
137 sizeof ctdl_home_directory);
139 safestrncpy(relhome, &argv[a][2],
144 else if (!strncmp(argv[a], "-t", 2)) {
145 freopen(&argv[a][2], "w", stderr);
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 params.signals[0]=SIGSEGV;
187 params.signals[1]=SIGILL;
188 params.signals[2]=SIGBUS;
189 params.signals[3]=SIGABRT;
190 eCrash_Init(¶ms);
191 eCrash_RegisterThread("MasterThread", 0);
194 /* Initialize the syslogger. Yes, we are really using 0 as the
195 * facility, because we are going to bitwise-OR the facility to
196 * the severity of each message, allowing us to write to other
197 * facilities when we need to...
200 openlog("citadel", LOG_NDELAY, 0);
201 setlogmask(LOG_UPTO(verbosity));
204 /* Tell 'em who's in da house */
205 CtdlLogPrintf(CTDL_NOTICE, "\n");
206 CtdlLogPrintf(CTDL_NOTICE, "\n");
207 CtdlLogPrintf(CTDL_NOTICE,
208 "*** Citadel server engine v%d.%02d (build %s) ***\n",
209 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
210 CtdlLogPrintf(CTDL_NOTICE, "Copyright (C) 1987-2008 by the Citadel development team.\n");
211 CtdlLogPrintf(CTDL_NOTICE, "This program is distributed under the terms of the GNU "
212 "General Public License.\n");
213 CtdlLogPrintf(CTDL_NOTICE, "\n");
214 CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
215 CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
217 /* Load site-specific parameters, and set the ipgm secret */
218 CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
220 config.c_ipgm_secret = rand();
222 /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
223 CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
229 /* on some dists rundir gets purged on startup. so we need to recreate it. */
231 if (stat(ctdl_run_dir, &filestats)==-1){
232 #ifdef HAVE_GETPWUID_R
233 #ifdef SOLARIS_GETPWUID
234 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
235 #else // SOLARIS_GETPWUID
236 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
237 #endif // SOLARIS_GETPWUID
238 #else // HAVE_GETPWUID_R
240 #endif // HAVE_GETPWUID_R
241 mkdir(ctdl_run_dir, 0755);
242 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
252 * Do non system dependent startup functions.
257 * Check that the control record is correct and place sensible values if it isn't
262 * Run any upgrade entry points
264 CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
268 * Load the user for the masterCC or create them if they don't exist
270 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
272 /** User doesn't exist. We can't use create user here as the user number needs to be 0 */
273 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
274 CtdlPutUser(&masterCC.user);
275 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /** Just to be safe */
279 * Bind the server to a Unix-domain socket.
281 CtdlRegisterServiceHook(0,
283 citproto_begin_session,
289 * Bind the server to our favorite TCP port (usually 504).
291 CtdlRegisterServiceHook(config.c_port_number,
293 citproto_begin_session,
302 * Load any server-side extensions available here.
304 CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
305 size = strlen(ctdl_home_directory) + 9;
307 initialise_modules(0);
312 * If we need host auth, start our chkpwd daemon.
314 if (config.c_auth_mode == AUTHMODE_HOST) {
315 start_chkpwd_daemon();
320 * check, whether we're fired up another time after a crash.
321 * if, post an aide message, so the admin has a chance to react.
327 * Now that we've bound the sockets, change to the Citadel user id and its
328 * corresponding group ids
330 if (drop_root_perms) {
331 cdb_chmod_data(); /* make sure we own our data files */
333 #ifdef HAVE_GETPWUID_R
334 #ifdef SOLARIS_GETPWUID
335 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
336 #else // SOLARIS_GETPWUID
337 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
338 #endif // SOLARIS_GETPWUID
339 #else // HAVE_GETPWUID_R
341 #endif // HAVE_GETPWUID_R
344 CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
345 "Group IDs will be incorrect.\n", (long)CTDLUID,
348 initgroups(pw.pw_name, pw.pw_gid);
349 if (setgid(pw.pw_gid))
350 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
353 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
354 if (setuid(CTDLUID) != 0) {
355 CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
357 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
358 prctl(PR_SET_DUMPABLE, 1);
362 /* We want to check for idle sessions once per minute */
363 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
368 master_cleanup(exit_signal);