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 void go_threading(void);
77 * Here's where it all begins.
79 int main(int argc, char **argv)
82 int a; /* General-purpose variables */
83 struct passwd pw, *pwp = NULL;
85 int drop_root_perms = 1;
90 char relhome[PATH_MAX]="";
91 char ctdldir[PATH_MAX]=CTDLDIR;
93 struct stat filestats;
96 eCrashParameters params;
97 // eCrashSymbolTable symbol_table;
99 /* initialise semaphores here. Patch by Matt and davew
100 * its called here as they are needed by CtdlLogPrintf for thread safety
102 InitialiseSemaphores();
104 /* initialize the master context */
105 InitializeMasterCC();
107 /* parse command-line arguments */
108 for (a=1; a<argc; ++a) {
110 if (!strncmp(argv[a], "-l", 2)) {
111 safestrncpy(facility, &argv[a][2], sizeof(facility));
112 syslog_facility = SyslogFacility(facility);
116 /* run in the background if -d was specified */
117 else if (!strcmp(argv[a], "-d")) {
118 running_as_daemon = 1;
121 /* -x specifies the desired logging level */
122 else if (!strncmp(argv[a], "-x", 2)) {
123 verbosity = atoi(&argv[a][2]);
126 else if (!strncmp(argv[a], "-h", 2)) {
127 relh=argv[a][2]!='/';
128 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
129 sizeof ctdl_home_directory);
131 safestrncpy(relhome, &argv[a][2],
137 else if (!strncmp(argv[a], "-t", 2)) {
138 freopen(&argv[a][2], "w", stderr);
141 else if (!strncmp(argv[a], "-D", 2)) {
145 /* -r tells the server not to drop root permissions. don't use
146 * this unless you know what you're doing. this should be
147 * removed in the next release if it proves unnecessary. */
148 else if (!strcmp(argv[a], "-r"))
151 /* any other parameter makes it crash and burn */
153 CtdlLogPrintf(CTDL_EMERG, "citserver: usage: "
158 " [-xLogLevel] [-hHomeDir]\n");
164 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
165 /* daemonize, if we were asked to */
166 if (running_as_daemon) {
171 #ifdef HAVE_BACKTRACE
172 bzero(¶ms, sizeof(params));
173 params.filename = file_pid_paniclog;
174 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
175 params.filep = fopen(file_pid_paniclog, "a+");
176 params.debugLevel = ECRASH_DEBUG_VERBOSE;
177 params.dumpAllThreads = TRUE;
178 params.useBacktraceSymbols = 1;
179 params.signals[0]=SIGSEGV;
180 params.signals[1]=SIGILL;
181 params.signals[2]=SIGBUS;
182 params.signals[3]=SIGABRT;
183 eCrash_Init(¶ms);
184 eCrash_RegisterThread("MasterThread", 0);
187 /* Initialize the syslogger. Yes, we are really using 0 as the
188 * facility, because we are going to bitwise-OR the facility to
189 * the severity of each message, allowing us to write to other
190 * facilities when we need to...
193 openlog("citadel", LOG_NDELAY, 0);
194 setlogmask(LOG_UPTO(verbosity));
197 /* Tell 'em who's in da house */
198 CtdlLogPrintf(CTDL_NOTICE, "\n");
199 CtdlLogPrintf(CTDL_NOTICE, "\n");
200 CtdlLogPrintf(CTDL_NOTICE,
201 "*** Citadel server engine v%d.%02d (build %s) ***\n",
202 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
203 CtdlLogPrintf(CTDL_NOTICE, "Copyright (C) 1987-2008 by the Citadel development team.\n");
204 CtdlLogPrintf(CTDL_NOTICE, "This program is distributed under the terms of the GNU "
205 "General Public License.\n");
206 CtdlLogPrintf(CTDL_NOTICE, "\n");
207 CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
208 CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
210 /* Load site-specific parameters, and set the ipgm secret */
211 CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
213 config.c_ipgm_secret = rand();
217 /* on some dists rundir gets purged on startup. so we need to recreate it. */
219 if (stat(ctdl_run_dir, &filestats)==-1){
220 #ifdef HAVE_GETPWUID_R
221 #ifdef SOLARIS_GETPWUID
222 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
223 #else // SOLARIS_GETPWUID
224 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
225 #endif // SOLARIS_GETPWUID
226 #else // HAVE_GETPWUID_R
228 #endif // HAVE_GETPWUID_R
229 mkdir(ctdl_run_dir, 0755);
230 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
240 * Do non system dependent startup functions.
244 CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
248 * Bind the server to a Unix-domain socket.
250 CtdlRegisterServiceHook(0,
252 citproto_begin_session,
258 * Bind the server to our favorite TCP port (usually 504).
260 CtdlRegisterServiceHook(config.c_port_number,
262 citproto_begin_session,
269 * Run any upgrade entry points
271 CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
276 * Load any server-side extensions available here.
278 CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
279 size = strlen(ctdl_home_directory) + 9;
281 initialise_modules(0);
286 * If we need host auth, start our chkpwd daemon.
288 if (config.c_auth_mode == AUTHMODE_HOST) {
289 start_chkpwd_daemon();
293 * Now that we've bound the sockets, change to the Citadel user id and its
294 * corresponding group ids
296 if (drop_root_perms) {
297 cdb_chmod_data(); /* make sure we own our data files */
299 #ifdef HAVE_GETPWUID_R
300 #ifdef SOLARIS_GETPWUID
301 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
302 #else // SOLARIS_GETPWUID
303 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
304 #endif // SOLARIS_GETPWUID
305 #else // HAVE_GETPWUID_R
307 #endif // HAVE_GETPWUID_R
310 CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
311 "Group IDs will be incorrect.\n", (long)CTDLUID,
314 initgroups(pw.pw_name, pw.pw_gid);
315 if (setgid(pw.pw_gid))
316 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
319 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
320 if (setuid(CTDLUID) != 0) {
321 CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
323 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
324 prctl(PR_SET_DUMPABLE, 1);
328 /* We want to check for idle sessions once per minute */
329 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
334 master_cleanup(exit_signal);