2 * citserver's main() function lives here.
4 * Copyright (c) 1987-2013 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.
15 #include <sys/types.h>
17 #include <libcitadel.h>
19 #include "citserver.h"
20 #include "svn_revision.h"
21 #include "modules_init.h"
24 #include "serv_extensions.h"
25 #include "citadel_dirs.h"
29 const char *CitadelServiceUDS="citadel-UDS";
30 const char *CitadelServiceTCP="citadel-TCP";
34 void go_threading(void);
37 * Here's where it all begins.
39 int main(int argc, char **argv)
43 int a; /* General-purpose variables */
44 struct passwd pw, *pwp = NULL;
46 int drop_root_perms = 1;
50 char relhome[PATH_MAX]="";
51 char ctdldir[PATH_MAX]=CTDLDIR;
52 int syslog_facility = LOG_DAEMON;
53 const char *eDebuglist[] = {NULL, NULL};
55 struct stat filestats;
58 eCrashParameters params;
59 // eCrashSymbolTable symbol_table;
62 /* initialize the master context */
64 InitializeMasterTSD();
66 /* parse command-line arguments */
67 while ((a=getopt(argc, argv, "l:dh:x:t:B:Dr")) != EOF) switch(a) {
70 safestrncpy(facility, optarg, sizeof(facility));
71 syslog_facility = SyslogFacility(facility);
74 /* run in the background if -d was specified */
76 running_as_daemon = 1;
80 relh = optarg[0] != '/';
82 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
85 safestrncpy(relhome, optarg, sizeof relhome);
91 eDebuglist [0] = optarg;
94 case 't': /* deprecated */
96 case 'B': /* Basesize */
97 basesize = atoi(optarg);
104 /* -r tells the server not to drop root permissions.
105 * Don't use this unless you know what you're doing.
112 /* any other parameter makes it crash and burn */
113 fprintf(stderr, "citserver: usage: "
121 StartLibCitadel(basesize);
123 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
127 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
128 /* daemonize, if we were asked to */
129 if (running_as_daemon) {
134 #ifdef HAVE_BACKTRACE
135 bzero(¶ms, sizeof(params));
136 params.filename = file_pid_paniclog;
137 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
138 params.filep = fopen(file_pid_paniclog, "a+");
139 params.debugLevel = ECRASH_DEBUG_VERBOSE;
140 params.dumpAllThreads = TRUE;
141 params.useBacktraceSymbols = 1;
142 params.signals[0]=SIGSEGV;
143 params.signals[1]=SIGILL;
144 params.signals[2]=SIGBUS;
145 params.signals[3]=SIGABRT;
146 eCrash_Init(¶ms);
147 eCrash_RegisterThread("MasterThread", 0);
150 /* Tell 'em who's in da house */
151 syslog(LOG_NOTICE, " ");
152 syslog(LOG_NOTICE, " ");
154 "*** Citadel server engine v%d.%02d (build %s) ***",
155 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
156 syslog(LOG_NOTICE, "Copyright (C) 1987-2013 by the Citadel development team.");
157 syslog(LOG_NOTICE, "This program is distributed under the terms of the GNU "
158 "General Public License.");
159 syslog(LOG_NOTICE, " ");
160 syslog(LOG_DEBUG, "Called as: %s", argv[0]);
161 syslog(LOG_INFO, "%s", libcitadel_version_string());
163 /* Load site-specific configuration */
164 syslog(LOG_INFO, "Loading citadel.config");
167 /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
168 syslog(LOG_INFO, "Acquiring control record");
174 /* on some dists rundir gets purged on startup. so we need to recreate it. */
176 if (stat(ctdl_run_dir, &filestats)==-1){
177 #ifdef HAVE_GETPWUID_R
178 #ifdef SOLARIS_GETPWUID
179 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
180 #else // SOLARIS_GETPWUID
181 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
182 #endif // SOLARIS_GETPWUID
183 #else // HAVE_GETPWUID_R
185 #endif // HAVE_GETPWUID_R
187 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST))
189 "unable to create run directory [%s]: %s",
190 ctdl_run_dir, strerror(errno));
192 if (chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0)
194 "unable to set the access rights for [%s]: %s",
195 ctdl_run_dir, strerror(errno));
205 * Do non system dependent startup functions.
210 * Check that the control record is correct and place sensible values if it isn't
215 * Run any upgrade entry points
217 syslog(LOG_INFO, "Upgrading modules.");
221 * Load the user for the masterCC or create them if they don't exist
223 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
225 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
226 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
227 CtdlPutUser(&masterCC.user);
228 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
232 * Bind the server to a Unix-domain socket (user client access)
234 CtdlRegisterServiceHook(0,
236 citproto_begin_session,
242 * Bind the server to a Unix-domain socket (admin client access)
244 CtdlRegisterServiceHook(0,
245 file_citadel_admin_socket,
246 citproto_begin_admin_session,
250 chmod(file_citadel_admin_socket, S_IRWXU); /* for your eyes only */
253 * Bind the server to our favorite TCP port (usually 504).
255 CtdlRegisterServiceHook(config.c_port_number,
257 citproto_begin_session,
266 * Load any server-side extensions available here.
268 syslog(LOG_INFO, "Initializing server extensions");
270 initialise_modules(0);
272 eDebuglist[1] = getenv("CITADEL_LOGDEBUG");
273 CtdlSetDebugLogFacilities(eDebuglist, 2);
276 * If we need host auth, start our chkpwd daemon.
278 if (config.c_auth_mode == AUTHMODE_HOST) {
279 start_chkpwd_daemon();
284 * check, whether we're fired up another time after a crash.
285 * if, post an aide message, so the admin has a chance to react.
291 * Now that we've bound the sockets, change to the Citadel user id and its
292 * corresponding group ids
294 if (drop_root_perms) {
295 cdb_chmod_data(); /* make sure we own our data files */
297 #ifdef HAVE_GETPWUID_R
298 #ifdef SOLARIS_GETPWUID
299 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
300 #else // SOLARIS_GETPWUID
301 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
302 #endif // SOLARIS_GETPWUID
303 #else // HAVE_GETPWUID_R
305 #endif // HAVE_GETPWUID_R
308 syslog(LOG_CRIT, "WARNING: getpwuid(%ld): %s"
309 "Group IDs will be incorrect.\n", (long)CTDLUID,
312 initgroups(pw.pw_name, pw.pw_gid);
313 if (setgid(pw.pw_gid))
314 syslog(LOG_CRIT, "setgid(%ld): %s", (long)pw.pw_gid,
317 syslog(LOG_INFO, "Changing uid to %ld", (long)CTDLUID);
318 if (setuid(CTDLUID) != 0) {
319 syslog(LOG_CRIT, "setuid() failed: %s", strerror(errno));
321 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
322 prctl(PR_SET_DUMPABLE, 1);
326 /* We want to check for idle sessions once per minute */
327 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
331 master_cleanup(exit_signal);