2 * citserver's main() function lives here.
4 * Copyright (c) 1987-2012 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 version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
22 #include <sys/types.h>
25 #include <sys/socket.h>
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
40 #include <netinet/in.h>
49 #ifdef HAVE_SYS_PRCTL_H
50 #include <sys/prctl.h>
52 #include <libcitadel.h>
55 #include "serv_extensions.h"
56 #include "sysdep_decls.h"
58 #include "citserver.h"
64 #include "housekeeping.h"
65 #include "svn_revision.h"
66 #include "citadel_dirs.h"
70 #include "modules_init.h"
73 #ifdef HAVE_SYS_SELECT_H
74 #include <sys/select.h>
80 const char *CitadelServiceUDS="citadel-UDS";
81 const char *CitadelServiceTCP="citadel-TCP";
85 void go_threading(void);
88 * Here's where it all begins.
90 int main(int argc, char **argv)
93 int a; /* General-purpose variables */
94 struct passwd pw, *pwp = NULL;
96 int drop_root_perms = 1;
100 char relhome[PATH_MAX]="";
101 char ctdldir[PATH_MAX]=CTDLDIR;
102 int syslog_facility = LOG_DAEMON;
104 struct stat filestats;
106 #ifdef HAVE_BACKTRACE
107 eCrashParameters params;
108 // eCrashSymbolTable symbol_table;
111 /* initialize the master context */
112 InitializeMasterCC();
113 InitializeMasterTSD();
115 /* parse command-line arguments */
116 while ((a=getopt(argc, argv, "l:dh:x:t:Dr")) != EOF) switch(a) {
119 safestrncpy(facility, optarg, sizeof(facility));
120 syslog_facility = SyslogFacility(facility);
123 /* run in the background if -d was specified */
125 running_as_daemon = 1;
129 relh = optarg[0] != '/';
131 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
134 safestrncpy(relhome, optarg, sizeof relhome);
139 case 'x': /* deprecated */
142 case 't': /* deprecated */
149 /* -r tells the server not to drop root permissions.
150 * Don't use this unless you know what you're doing.
157 /* any other parameter makes it crash and burn */
158 fprintf(stderr, "citserver: usage: "
168 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
172 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
173 /* daemonize, if we were asked to */
174 if (running_as_daemon) {
179 #ifdef HAVE_BACKTRACE
180 bzero(¶ms, sizeof(params));
181 params.filename = file_pid_paniclog;
182 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
183 params.filep = fopen(file_pid_paniclog, "a+");
184 params.debugLevel = ECRASH_DEBUG_VERBOSE;
185 params.dumpAllThreads = TRUE;
186 params.useBacktraceSymbols = 1;
187 params.signals[0]=SIGSEGV;
188 params.signals[1]=SIGILL;
189 params.signals[2]=SIGBUS;
190 params.signals[3]=SIGABRT;
191 eCrash_Init(¶ms);
192 eCrash_RegisterThread("MasterThread", 0);
195 /* Tell 'em who's in da house */
196 syslog(LOG_NOTICE, " ");
197 syslog(LOG_NOTICE, " ");
199 "*** Citadel server engine v%d.%02d (build %s) ***",
200 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
201 syslog(LOG_NOTICE, "Copyright (C) 1987-2012 by the Citadel development team.");
202 syslog(LOG_NOTICE, "This program is distributed under the terms of the GNU "
203 "General Public License.");
204 syslog(LOG_NOTICE, " ");
205 syslog(LOG_DEBUG, "Called as: %s", argv[0]);
206 syslog(LOG_INFO, "%s", libcitadel_version_string());
208 /* Load site-specific configuration */
209 syslog(LOG_INFO, "Loading citadel.config");
212 /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
213 syslog(LOG_INFO, "Acquiring control record");
219 /* on some dists rundir gets purged on startup. so we need to recreate it. */
221 if (stat(ctdl_run_dir, &filestats)==-1){
222 #ifdef HAVE_GETPWUID_R
223 #ifdef SOLARIS_GETPWUID
224 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
225 #else // SOLARIS_GETPWUID
226 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
227 #endif // SOLARIS_GETPWUID
228 #else // HAVE_GETPWUID_R
230 #endif // HAVE_GETPWUID_R
232 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST))
234 "unable to create run directory [%s]: %s",
235 ctdl_run_dir, strerror(errno));
237 if (chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0)
239 "unable to set the access rights for [%s]: %s",
240 ctdl_run_dir, strerror(errno));
250 * Do non system dependent startup functions.
255 * Check that the control record is correct and place sensible values if it isn't
260 * Run any upgrade entry points
262 syslog(LOG_INFO, "Upgrading modules.");
266 * Load the user for the masterCC or create them if they don't exist
268 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
270 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
271 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
272 CtdlPutUser(&masterCC.user);
273 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
277 * Bind the server to a Unix-domain socket (user client access)
279 CtdlRegisterServiceHook(0,
281 citproto_begin_session,
287 * Bind the server to a Unix-domain socket (admin client access)
289 CtdlRegisterServiceHook(0,
290 file_citadel_admin_socket,
291 citproto_begin_admin_session,
295 chmod(file_citadel_admin_socket, S_IRWXU); /* for your eyes only */
298 * Bind the server to our favorite TCP port (usually 504).
300 CtdlRegisterServiceHook(config.c_port_number,
302 citproto_begin_session,
311 * Load any server-side extensions available here.
313 syslog(LOG_INFO, "Initializing server extensions");
315 initialise_modules(0);
318 * If we need host auth, start our chkpwd daemon.
320 if (config.c_auth_mode == AUTHMODE_HOST) {
321 start_chkpwd_daemon();
326 * check, whether we're fired up another time after a crash.
327 * if, post an aide message, so the admin has a chance to react.
333 * Now that we've bound the sockets, change to the Citadel user id and its
334 * corresponding group ids
336 if (drop_root_perms) {
337 cdb_chmod_data(); /* make sure we own our data files */
339 #ifdef HAVE_GETPWUID_R
340 #ifdef SOLARIS_GETPWUID
341 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
342 #else // SOLARIS_GETPWUID
343 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
344 #endif // SOLARIS_GETPWUID
345 #else // HAVE_GETPWUID_R
347 #endif // HAVE_GETPWUID_R
350 syslog(LOG_CRIT, "WARNING: getpwuid(%ld): %s"
351 "Group IDs will be incorrect.\n", (long)CTDLUID,
354 initgroups(pw.pw_name, pw.pw_gid);
355 if (setgid(pw.pw_gid))
356 syslog(LOG_CRIT, "setgid(%ld): %s", (long)pw.pw_gid,
359 syslog(LOG_INFO, "Changing uid to %ld", (long)CTDLUID);
360 if (setuid(CTDLUID) != 0) {
361 syslog(LOG_CRIT, "setuid() failed: %s", strerror(errno));
363 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
364 prctl(PR_SET_DUMPABLE, 1);
368 /* We want to check for idle sessions once per minute */
369 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
373 master_cleanup(exit_signal);