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);
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 CtdlInitBase64Table();
103 InitialiseSemaphores();
105 /* initialize the master context */
106 InitializeMasterCC();
108 /* parse command-line arguments */
109 for (a=1; a<argc; ++a) {
111 if (!strncmp(argv[a], "-l", 2)) {
112 safestrncpy(facility, &argv[a][2], sizeof(facility));
113 syslog_facility = SyslogFacility(facility);
117 /* run in the background if -d was specified */
118 else if (!strcmp(argv[a], "-d")) {
119 running_as_daemon = 1;
122 /* -x specifies the desired logging level */
123 else if (!strncmp(argv[a], "-x", 2)) {
124 verbosity = atoi(&argv[a][2]);
127 else if (!strncmp(argv[a], "-h", 2)) {
128 relh=argv[a][2]!='/';
129 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
130 sizeof ctdl_home_directory);
132 safestrncpy(relhome, &argv[a][2],
138 else if (!strncmp(argv[a], "-t", 2)) {
139 freopen(&argv[a][2], "w", stderr);
142 else if (!strncmp(argv[a], "-f", 2)) {
146 else if (!strncmp(argv[a], "-D", 2)) {
150 /* -r tells the server not to drop root permissions. don't use
151 * this unless you know what you're doing. this should be
152 * removed in the next release if it proves unnecessary. */
153 else if (!strcmp(argv[a], "-r"))
156 /* any other parameter makes it crash and burn */
158 CtdlLogPrintf(CTDL_EMERG, "citserver: usage: "
163 " [-xLogLevel] [-hHomeDir]\n");
169 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
170 /* daemonize, if we were asked to */
171 if (running_as_daemon) {
176 #ifdef HAVE_BACKTRACE
177 bzero(¶ms, sizeof(params));
178 params.filename = file_pid_paniclog;
179 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
180 params.filep = fopen(file_pid_paniclog, "a+");
181 params.debugLevel = ECRASH_DEBUG_VERBOSE;
182 params.dumpAllThreads = TRUE;
183 params.useBacktraceSymbols = 1;
184 /// BuildSymbolTable(&symbol_table);
185 // params.symbolTable = &symbol_table;
186 params.signals[0]=SIGSEGV;
187 params.signals[1]=SIGILL;
188 params.signals[2]=SIGBUS;
189 params.signals[3]=SIGABRT;
191 eCrash_Init(¶ms);
193 eCrash_RegisterThread("MasterThread", 0);
195 /// signal(SIGSEGV, cit_panic_backtrace);
197 /* Initialize the syslogger. Yes, we are really using 0 as the
198 * facility, because we are going to bitwise-OR the facility to
199 * the severity of each message, allowing us to write to other
200 * facilities when we need to...
203 openlog("citadel", LOG_NDELAY, 0);
204 setlogmask(LOG_UPTO(verbosity));
207 /* Tell 'em who's in da house */
208 CtdlLogPrintf(CTDL_NOTICE, "\n");
209 CtdlLogPrintf(CTDL_NOTICE, "\n");
210 CtdlLogPrintf(CTDL_NOTICE,
211 "*** Citadel server engine v%d.%02d ***\n",
212 (REV_LEVEL/100), (REV_LEVEL%100));
213 CtdlLogPrintf(CTDL_NOTICE,
214 "Copyright (C) 1987-2008 by the Citadel development team.\n");
215 CtdlLogPrintf(CTDL_NOTICE,
216 "This program is distributed under the terms of the GNU "
217 "General Public License.\n");
218 CtdlLogPrintf(CTDL_NOTICE, "\n");
219 CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
220 CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
222 /* Load site-specific parameters, and set the ipgm secret */
223 CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
225 config.c_ipgm_secret = rand();
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.
256 CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
260 * Bind the server to a Unix-domain socket.
262 CtdlRegisterServiceHook(0,
264 citproto_begin_session,
270 * Bind the server to our favorite TCP port (usually 504).
272 CtdlRegisterServiceHook(config.c_port_number,
274 citproto_begin_session,
281 * Run any upgrade entry points
283 CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
288 * Load any server-side extensions available here.
290 CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
291 size = strlen(ctdl_home_directory) + 9;
293 initialise_modules(0);
298 * If we need host auth, start our chkpwd daemon.
300 if (config.c_auth_mode == AUTHMODE_HOST) {
301 start_chkpwd_daemon();
305 * Now that we've bound the sockets, change to the Citadel user id and its
306 * corresponding group ids
308 if (drop_root_perms) {
309 cdb_chmod_data(); /* make sure we own our data files */
311 #ifdef HAVE_GETPWUID_R
312 #ifdef SOLARIS_GETPWUID
313 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
314 #else // SOLARIS_GETPWUID
315 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
316 #endif // SOLARIS_GETPWUID
317 #else // HAVE_GETPWUID_R
319 #endif // HAVE_GETPWUID_R
322 CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
323 "Group IDs will be incorrect.\n", (long)CTDLUID,
326 initgroups(pw.pw_name, pw.pw_gid);
327 if (setgid(pw.pw_gid))
328 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
331 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
332 if (setuid(CTDLUID) != 0) {
333 CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
335 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
336 prctl(PR_SET_DUMPABLE, 1);
340 /* We want to check for idle sessions once per minute */
341 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
346 master_cleanup(exit_signal);