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 "citadel_dirs.c"
59 #include "modules_init.h"
62 #ifdef HAVE_SYS_SELECT_H
63 #include <sys/select.h>
69 const char *CitadelServiceUDS="citadel-UDS";
70 const char *CitadelServiceTCP="citadel-TCP";
73 void go_threading(void);
76 * For the subversion revision number
78 const char *svn_revision(void);
81 * Here's where it all begins.
83 int main(int argc, char **argv)
86 int a; /* General-purpose variables */
87 struct passwd pw, *pwp = NULL;
89 int drop_root_perms = 1;
94 char relhome[PATH_MAX]="";
95 char ctdldir[PATH_MAX]=CTDLDIR;
97 struct stat filestats;
100 eCrashParameters params;
101 // eCrashSymbolTable symbol_table;
103 /* initialise semaphores here. Patch by Matt and davew
104 * its called here as they are needed by CtdlLogPrintf for thread safety
106 InitialiseSemaphores();
108 /* initialize the master context */
109 InitializeMasterCC();
111 /* parse command-line arguments */
112 for (a=1; a<argc; ++a) {
114 if (!strncmp(argv[a], "-l", 2)) {
115 safestrncpy(facility, &argv[a][2], sizeof(facility));
116 syslog_facility = SyslogFacility(facility);
120 /* run in the background if -d was specified */
121 else if (!strcmp(argv[a], "-d")) {
122 running_as_daemon = 1;
125 /* -x specifies the desired logging level */
126 else if (!strncmp(argv[a], "-x", 2)) {
127 verbosity = atoi(&argv[a][2]);
130 else if (!strncmp(argv[a], "-h", 2)) {
131 relh=argv[a][2]!='/';
132 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
133 sizeof ctdl_home_directory);
135 safestrncpy(relhome, &argv[a][2],
141 else if (!strncmp(argv[a], "-t", 2)) {
142 freopen(&argv[a][2], "w", stderr);
145 else if (!strncmp(argv[a], "-D", 2)) {
149 /* -r tells the server not to drop root permissions. don't use
150 * this unless you know what you're doing. this should be
151 * removed in the next release if it proves unnecessary. */
152 else if (!strcmp(argv[a], "-r"))
155 /* any other parameter makes it crash and burn */
157 CtdlLogPrintf(CTDL_EMERG, "citserver: usage: "
162 " [-xLogLevel] [-hHomeDir]\n");
168 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
169 /* daemonize, if we were asked to */
170 if (running_as_daemon) {
175 #ifdef HAVE_BACKTRACE
176 bzero(¶ms, sizeof(params));
177 params.filename = file_pid_paniclog;
178 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
179 params.filep = fopen(file_pid_paniclog, "a+");
180 params.debugLevel = ECRASH_DEBUG_VERBOSE;
181 params.dumpAllThreads = TRUE;
182 params.useBacktraceSymbols = 1;
183 params.signals[0]=SIGSEGV;
184 params.signals[1]=SIGILL;
185 params.signals[2]=SIGBUS;
186 params.signals[3]=SIGABRT;
187 eCrash_Init(¶ms);
188 eCrash_RegisterThread("MasterThread", 0);
191 /* Initialize the syslogger. Yes, we are really using 0 as the
192 * facility, because we are going to bitwise-OR the facility to
193 * the severity of each message, allowing us to write to other
194 * facilities when we need to...
197 openlog("citadel", LOG_NDELAY, 0);
198 setlogmask(LOG_UPTO(verbosity));
201 /* Tell 'em who's in da house */
202 CtdlLogPrintf(CTDL_NOTICE, "\n");
203 CtdlLogPrintf(CTDL_NOTICE, "\n");
204 CtdlLogPrintf(CTDL_NOTICE,
205 "*** Citadel server engine v%d.%02d ***\n",
206 (REV_LEVEL/100), (REV_LEVEL%100));
207 CtdlLogPrintf(CTDL_NOTICE,
208 "*** Build number %s ***\n", svn_revision());
209 CtdlLogPrintf(CTDL_NOTICE,
210 "Copyright (C) 1987-2008 by the Citadel development team.\n");
211 CtdlLogPrintf(CTDL_NOTICE,
212 "This program is distributed under the terms of the GNU "
213 "General Public License.\n");
214 CtdlLogPrintf(CTDL_NOTICE, "\n");
215 CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
216 CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
218 /* Load site-specific parameters, and set the ipgm secret */
219 CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
221 config.c_ipgm_secret = rand();
225 /* on some dists rundir gets purged on startup. so we need to recreate it. */
227 if (stat(ctdl_run_dir, &filestats)==-1){
228 #ifdef HAVE_GETPWUID_R
229 #ifdef SOLARIS_GETPWUID
230 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
231 #else // SOLARIS_GETPWUID
232 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
233 #endif // SOLARIS_GETPWUID
234 #else // HAVE_GETPWUID_R
236 #endif // HAVE_GETPWUID_R
237 mkdir(ctdl_run_dir, 0755);
238 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
248 * Do non system dependent startup functions.
252 CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
256 * Bind the server to a Unix-domain socket.
258 CtdlRegisterServiceHook(0,
260 citproto_begin_session,
266 * Bind the server to our favorite TCP port (usually 504).
268 CtdlRegisterServiceHook(config.c_port_number,
270 citproto_begin_session,
277 * Run any upgrade entry points
279 CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
284 * Load any server-side extensions available here.
286 CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
287 size = strlen(ctdl_home_directory) + 9;
289 initialise_modules(0);
294 * If we need host auth, start our chkpwd daemon.
296 if (config.c_auth_mode == AUTHMODE_HOST) {
297 start_chkpwd_daemon();
301 * Now that we've bound the sockets, change to the Citadel user id and its
302 * corresponding group ids
304 if (drop_root_perms) {
305 cdb_chmod_data(); /* make sure we own our data files */
307 #ifdef HAVE_GETPWUID_R
308 #ifdef SOLARIS_GETPWUID
309 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
310 #else // SOLARIS_GETPWUID
311 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
312 #endif // SOLARIS_GETPWUID
313 #else // HAVE_GETPWUID_R
315 #endif // HAVE_GETPWUID_R
318 CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
319 "Group IDs will be incorrect.\n", (long)CTDLUID,
322 initgroups(pw.pw_name, pw.pw_gid);
323 if (setgid(pw.pw_gid))
324 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
327 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
328 if (setuid(CTDLUID) != 0) {
329 CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
331 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
332 prctl(PR_SET_DUMPABLE, 1);
336 /* We want to check for idle sessions once per minute */
337 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
342 master_cleanup(exit_signal);