2 * citserver's main() function lives here.
4 * Copyright (c) 1987-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
31 #include <sys/socket.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
46 #include <netinet/in.h>
55 #ifdef HAVE_SYS_PRCTL_H
56 #include <sys/prctl.h>
58 #include <libcitadel.h>
61 #include "serv_extensions.h"
62 #include "sysdep_decls.h"
64 #include "citserver.h"
70 #include "housekeeping.h"
71 #include "svn_revision.h"
72 #include "citadel_dirs.h"
76 #include "modules_init.h"
79 #ifdef HAVE_SYS_SELECT_H
80 #include <sys/select.h>
86 const char *CitadelServiceUDS="citadel-UDS";
87 const char *CitadelServiceTCP="citadel-TCP";
91 void go_threading(void);
94 * Here's where it all begins.
96 int main(int argc, char **argv)
99 int a; /* General-purpose variables */
100 struct passwd pw, *pwp = NULL;
102 int drop_root_perms = 1;
108 char relhome[PATH_MAX]="";
109 char ctdldir[PATH_MAX]=CTDLDIR;
110 int syslog_facility = LOG_DAEMON;
112 struct stat filestats;
114 #ifdef HAVE_BACKTRACE
115 eCrashParameters params;
116 // eCrashSymbolTable symbol_table;
119 /* initialize the master context */
120 InitializeMasterCC();
121 InitializeMasterTSD();
123 /* parse command-line arguments */
124 for (a=1; a<argc; ++a) {
126 if (!strncmp(argv[a], "-l", 2)) {
127 safestrncpy(facility, &argv[a][2], sizeof(facility));
128 syslog_facility = SyslogFacility(facility);
131 /* run in the background if -d was specified */
132 else if (!strcmp(argv[a], "-d")) {
133 running_as_daemon = 1;
136 else if (!strncmp(argv[a], "-h", 2)) {
137 relh=argv[a][2]!='/';
138 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
139 sizeof ctdl_home_directory);
141 safestrncpy(relhome, &argv[a][2],
146 else if (!strncmp(argv[a], "-x", 2)) {
150 else if (!strncmp(argv[a], "-t", 2)) {
151 if (freopen(&argv[a][2], "w", stderr) != stderr)
154 "unable to open your trace log [%s]: %s",
162 else if (!strncmp(argv[a], "-D", 2)) {
166 /* -r tells the server not to drop root permissions. don't use
167 * this unless you know what you're doing. this should be
168 * removed in the next release if it proves unnecessary. */
169 else if (!strcmp(argv[a], "-r"))
172 /* any other parameter makes it crash and burn */
174 fprintf(stderr, "citserver: usage: "
187 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
191 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
192 /* daemonize, if we were asked to */
193 if (running_as_daemon) {
198 #ifdef HAVE_BACKTRACE
199 bzero(¶ms, sizeof(params));
200 params.filename = file_pid_paniclog;
201 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
202 params.filep = fopen(file_pid_paniclog, "a+");
203 params.debugLevel = ECRASH_DEBUG_VERBOSE;
204 params.dumpAllThreads = TRUE;
205 params.useBacktraceSymbols = 1;
206 params.signals[0]=SIGSEGV;
207 params.signals[1]=SIGILL;
208 params.signals[2]=SIGBUS;
209 params.signals[3]=SIGABRT;
210 eCrash_Init(¶ms);
211 eCrash_RegisterThread("MasterThread", 0);
214 /* Tell 'em who's in da house */
215 syslog(LOG_NOTICE, " ");
216 syslog(LOG_NOTICE, " ");
218 "*** Citadel server engine v%d.%02d (build %s) ***",
219 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
220 syslog(LOG_NOTICE, "Copyright (C) 1987-2011 by the Citadel development team.");
221 syslog(LOG_NOTICE, "This program is distributed under the terms of the GNU "
222 "General Public License.");
223 syslog(LOG_NOTICE, " ");
224 syslog(LOG_DEBUG, "Called as: %s", argv[0]);
225 syslog(LOG_INFO, "%s", libcitadel_version_string());
227 /* Load site-specific parameters, and set the ipgm secret */
228 syslog(LOG_INFO, "Loading citadel.config");
230 config.c_ipgm_secret = rand();
232 /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
233 syslog(LOG_INFO, "Acquiring control record");
239 /* on some dists rundir gets purged on startup. so we need to recreate it. */
241 if (stat(ctdl_run_dir, &filestats)==-1){
242 #ifdef HAVE_GETPWUID_R
243 #ifdef SOLARIS_GETPWUID
244 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
245 #else // SOLARIS_GETPWUID
246 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
247 #endif // SOLARIS_GETPWUID
248 #else // HAVE_GETPWUID_R
250 #endif // HAVE_GETPWUID_R
252 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST))
254 "unable to create run directory [%s]: %s",
255 ctdl_run_dir, strerror(errno));
257 if (chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0)
259 "unable to set the access rights for [%s]: %s",
260 ctdl_run_dir, strerror(errno));
270 * Do non system dependent startup functions.
275 * Check that the control record is correct and place sensible values if it isn't
280 * Run any upgrade entry points
282 syslog(LOG_INFO, "Upgrading modules.");
286 * Load the user for the masterCC or create them if they don't exist
288 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
290 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
291 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
292 CtdlPutUser(&masterCC.user);
293 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
297 * Bind the server to a Unix-domain socket.
299 CtdlRegisterServiceHook(0,
301 citproto_begin_session,
307 * Bind the server to our favorite TCP port (usually 504).
309 CtdlRegisterServiceHook(config.c_port_number,
311 citproto_begin_session,
320 * Load any server-side extensions available here.
322 syslog(LOG_INFO, "Initializing server extensions\n");
323 size = strlen(ctdl_home_directory) + 9;
325 initialise_modules(0);
330 * If we need host auth, start our chkpwd daemon.
332 if (config.c_auth_mode == AUTHMODE_HOST) {
333 start_chkpwd_daemon();
338 * check, whether we're fired up another time after a crash.
339 * if, post an aide message, so the admin has a chance to react.
345 * Now that we've bound the sockets, change to the Citadel user id and its
346 * corresponding group ids
348 if (drop_root_perms) {
349 cdb_chmod_data(); /* make sure we own our data files */
351 #ifdef HAVE_GETPWUID_R
352 #ifdef SOLARIS_GETPWUID
353 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
354 #else // SOLARIS_GETPWUID
355 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
356 #endif // SOLARIS_GETPWUID
357 #else // HAVE_GETPWUID_R
359 #endif // HAVE_GETPWUID_R
362 syslog(LOG_CRIT, "WARNING: getpwuid(%ld): %s\n"
363 "Group IDs will be incorrect.\n", (long)CTDLUID,
366 initgroups(pw.pw_name, pw.pw_gid);
367 if (setgid(pw.pw_gid))
368 syslog(LOG_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
371 syslog(LOG_INFO, "Changing uid to %ld\n", (long)CTDLUID);
372 if (setuid(CTDLUID) != 0) {
373 syslog(LOG_CRIT, "setuid() failed: %s\n", strerror(errno));
375 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
376 prctl(PR_SET_DUMPABLE, 1);
380 /* We want to check for idle sessions once per minute */
381 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
385 master_cleanup(exit_signal);