4 * citserver's main() function lives here.
6 * Copyright (c) 1987-2010 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/types.h>
33 #include <sys/socket.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
48 #include <netinet/in.h>
57 #ifdef HAVE_SYS_PRCTL_H
58 #include <sys/prctl.h>
60 #include <libcitadel.h>
63 #include "serv_extensions.h"
64 #include "sysdep_decls.h"
66 #include "citserver.h"
72 #include "housekeeping.h"
73 #include "svn_revision.h"
74 #include "citadel_dirs.h"
78 #include "modules_init.h"
81 #ifdef HAVE_SYS_SELECT_H
82 #include <sys/select.h>
88 const char *CitadelServiceUDS="citadel-UDS";
89 const char *CitadelServiceTCP="citadel-TCP";
93 void go_threading(void);
96 * Here's where it all begins.
98 int main(int argc, char **argv)
101 int a; /* General-purpose variables */
102 struct passwd pw, *pwp = NULL;
104 int drop_root_perms = 1;
109 char relhome[PATH_MAX]="";
110 char ctdldir[PATH_MAX]=CTDLDIR;
112 struct stat filestats;
114 #ifdef HAVE_BACKTRACE
115 eCrashParameters params;
116 // eCrashSymbolTable symbol_table;
125 /* initialise semaphores here. Patch by Matt and davew
126 * its called here as they are needed by CtdlLogPrintf for thread safety
128 InitialiseSemaphores();
130 /* initialize the master context */
131 InitializeMasterCC();
133 /* parse command-line arguments */
134 for (a=1; a<argc; ++a) {
136 if (!strncmp(argv[a], "-l", 2)) {
137 safestrncpy(facility, &argv[a][2], sizeof(facility));
138 syslog_facility = SyslogFacility(facility);
142 /* run in the background if -d was specified */
143 else if (!strcmp(argv[a], "-d")) {
144 running_as_daemon = 1;
147 /* run a few stats if -s was specified */
148 else if (!strncmp(argv[a], "-s", 2)) {
149 statcount = atoi(&argv[a][2]);
152 /* -x specifies the desired logging level */
153 else if (!strncmp(argv[a], "-x", 2)) {
154 verbosity = atoi(&argv[a][2]);
157 else if (!strncmp(argv[a], "-h", 2)) {
158 relh=argv[a][2]!='/';
159 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
160 sizeof ctdl_home_directory);
162 safestrncpy(relhome, &argv[a][2],
167 else if (!strncmp(argv[a], "-t", 2)) {
168 if (freopen(&argv[a][2], "w", stderr) != stderr)
170 CtdlLogPrintf(CTDL_EMERG,
171 "unable to open your trace log [%s]: %s",
178 else if (!strncmp(argv[a], "-D", 2)) {
182 /* -r tells the server not to drop root permissions. don't use
183 * this unless you know what you're doing. this should be
184 * removed in the next release if it proves unnecessary. */
185 else if (!strcmp(argv[a], "-r"))
188 /* any other parameter makes it crash and burn */
190 CtdlLogPrintf(CTDL_EMERG, "citserver: usage: "
195 " [-xLogLevel] [-hHomeDir]\n");
201 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
202 /* daemonize, if we were asked to */
203 if (running_as_daemon) {
208 #ifdef HAVE_BACKTRACE
209 bzero(¶ms, sizeof(params));
210 params.filename = file_pid_paniclog;
211 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
212 params.filep = fopen(file_pid_paniclog, "a+");
213 params.debugLevel = ECRASH_DEBUG_VERBOSE;
214 params.dumpAllThreads = TRUE;
215 params.useBacktraceSymbols = 1;
216 params.signals[0]=SIGSEGV;
217 params.signals[1]=SIGILL;
218 params.signals[2]=SIGBUS;
219 params.signals[3]=SIGABRT;
220 eCrash_Init(¶ms);
221 eCrash_RegisterThread("MasterThread", 0);
224 /* Initialize the syslogger. Yes, we are really using 0 as the
225 * facility, because we are going to bitwise-OR the facility to
226 * the severity of each message, allowing us to write to other
227 * facilities when we need to...
230 openlog("citadel", LOG_NDELAY, 0);
231 setlogmask(LOG_UPTO(verbosity));
234 /* Tell 'em who's in da house */
235 CtdlLogPrintf(CTDL_NOTICE, "\n");
236 CtdlLogPrintf(CTDL_NOTICE, "\n");
237 CtdlLogPrintf(CTDL_NOTICE,
238 "*** Citadel server engine v%d.%02d (build %s) ***\n",
239 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
240 CtdlLogPrintf(CTDL_NOTICE, "Copyright (C) 1987-2010 by the Citadel development team.\n");
241 CtdlLogPrintf(CTDL_NOTICE, "This program is distributed under the terms of the GNU "
242 "General Public License.\n");
243 CtdlLogPrintf(CTDL_NOTICE, "\n");
244 CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
245 CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
247 /* Load site-specific parameters, and set the ipgm secret */
248 CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
250 config.c_ipgm_secret = rand();
252 /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
253 CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
259 /* on some dists rundir gets purged on startup. so we need to recreate it. */
261 if (stat(ctdl_run_dir, &filestats)==-1){
262 #ifdef HAVE_GETPWUID_R
263 #ifdef SOLARIS_GETPWUID
264 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
265 #else // SOLARIS_GETPWUID
266 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
267 #endif // SOLARIS_GETPWUID
268 #else // HAVE_GETPWUID_R
270 #endif // HAVE_GETPWUID_R
272 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST))
273 CtdlLogPrintf(CTDL_EMERG,
274 "unable to create run directory [%s]: %s",
275 ctdl_run_dir, strerror(errno));
277 if (chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0)
278 CtdlLogPrintf(CTDL_EMERG,
279 "unable to set the access rights for [%s]: %s",
280 ctdl_run_dir, strerror(errno));
290 * Do non system dependent startup functions.
295 * Check that the control record is correct and place sensible values if it isn't
300 * Run any upgrade entry points
302 CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
306 * Load the user for the masterCC or create them if they don't exist
308 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
310 /** User doesn't exist. We can't use create user here as the user number needs to be 0 */
311 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
312 CtdlPutUser(&masterCC.user);
313 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /** Just to be safe */
317 * Bind the server to a Unix-domain socket.
319 CtdlRegisterServiceHook(0,
321 citproto_begin_session,
327 * Bind the server to our favorite TCP port (usually 504).
329 CtdlRegisterServiceHook(config.c_port_number,
331 citproto_begin_session,
340 * Load any server-side extensions available here.
342 CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
343 size = strlen(ctdl_home_directory) + 9;
345 initialise_modules(0);
350 * If we need host auth, start our chkpwd daemon.
352 if (config.c_auth_mode == AUTHMODE_HOST) {
353 start_chkpwd_daemon();
358 * check, whether we're fired up another time after a crash.
359 * if, post an aide message, so the admin has a chance to react.
365 * Now that we've bound the sockets, change to the Citadel user id and its
366 * corresponding group ids
368 if (drop_root_perms) {
369 cdb_chmod_data(); /* make sure we own our data files */
371 #ifdef HAVE_GETPWUID_R
372 #ifdef SOLARIS_GETPWUID
373 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
374 #else // SOLARIS_GETPWUID
375 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
376 #endif // SOLARIS_GETPWUID
377 #else // HAVE_GETPWUID_R
379 #endif // HAVE_GETPWUID_R
382 CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
383 "Group IDs will be incorrect.\n", (long)CTDLUID,
386 initgroups(pw.pw_name, pw.pw_gid);
387 if (setgid(pw.pw_gid))
388 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
391 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
392 if (setuid(CTDLUID) != 0) {
393 CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
395 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
396 prctl(PR_SET_DUMPABLE, 1);
400 /* We want to check for idle sessions once per minute */
401 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
406 master_cleanup(exit_signal);