2 * citserver's main() function lives here.
4 * Copyright (c) 1987-2017 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>
18 #include <libcitadel.h>
20 #include "citserver.h"
21 #include "svn_revision.h"
22 #include "modules_init.h"
25 #include "serv_extensions.h"
26 #include "citadel_dirs.h"
31 const char *CitadelServiceUDS="citadel-UDS";
32 const char *CitadelServiceTCP="citadel-TCP";
36 void go_threading(void);
41 * Create or remove a lock file, so we only have one Citadel Server running at a time.
43 void ctdl_lockfile(int yo) {
44 static char lockfilename[SIZ];
49 syslog(LOG_DEBUG, "Creating lockfile");
50 snprintf(lockfilename, sizeof lockfilename, "%s/citadel.lock", ctdl_run_dir);
51 fp = fopen(lockfilename, "w");
53 syslog(LOG_ERR, "Cannot open or create %s", lockfilename);
56 if (flock(fileno(fp), (LOCK_EX|LOCK_NB)) != 0) {
57 syslog(LOG_ERR, "Cannot lock %s , is another citserver running?", lockfilename);
63 syslog(LOG_DEBUG, "Removing lockfile");
65 flock(fileno(fp), LOCK_UN);
75 * Here's where it all begins.
77 int main(int argc, char **argv)
81 int a; /* General-purpose variables */
82 struct passwd pw, *pwp = NULL;
84 int drop_root_perms = 1;
88 char relhome[PATH_MAX]="";
89 char ctdldir[PATH_MAX]=CTDLDIR;
90 int syslog_facility = LOG_DAEMON;
91 const char *eDebuglist[] = {NULL, NULL};
93 struct passwd *p = NULL;
95 struct stat filestats;
98 eCrashParameters params;
99 // eCrashSymbolTable symbol_table;
102 /* initialize the master context */
103 InitializeMasterCC();
104 InitializeMasterTSD();
106 /* parse command-line arguments */
107 while ((a=getopt(argc, argv, "l:dh:x:t:B:Dru:")) != EOF) switch(a) {
110 safestrncpy(facility, optarg, sizeof(facility));
111 syslog_facility = SyslogFacility(facility);
114 /* run in the background if -d was specified */
116 running_as_daemon = 1;
120 relh = optarg[0] != '/';
122 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
125 safestrncpy(relhome, optarg, sizeof relhome);
131 eDebuglist [0] = optarg;
134 case 't': /* deprecated */
136 case 'B': /* Basesize */
137 basesize = atoi(optarg);
144 /* -r tells the server not to drop root permissions.
145 * Don't use this unless you know what you're doing.
151 /* -u tells the server what uid to run under... */
158 p = getpwnam(optarg);
169 /* any other parameter makes it crash and burn */
170 fprintf(stderr, "citserver: usage: "
180 /* Last ditch effort to determine the user name ... if there's a user called "citadel" then use that */
182 p = getpwnam("citadel");
187 p = getpwnam("guest");
197 if ((ctdluid == 0) && (drop_root_perms == 0)) {
198 fprintf(stderr, "citserver: cannot determine user to run as; please specify -r or -u options\n");
199 exit(CTDLEXIT_UNUSER);
202 StartLibCitadel(basesize);
204 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
208 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
209 /* daemonize, if we were asked to */
210 if (running_as_daemon) {
217 bzero(¶ms, sizeof(params));
218 params.filename = file_pid_paniclog;
219 panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
220 params.filep = fopen(file_pid_paniclog, "a+");
221 params.debugLevel = ECRASH_DEBUG_VERBOSE;
222 params.dumpAllThreads = TRUE;
223 params.useBacktraceSymbols = 1;
224 params.signals[0]=SIGSEGV;
225 params.signals[1]=SIGILL;
226 params.signals[2]=SIGBUS;
227 params.signals[3]=SIGABRT;
228 eCrash_Init(¶ms);
229 eCrash_RegisterThread("MasterThread", 0);
232 /* Tell 'em who's in da house */
233 syslog(LOG_NOTICE, " ");
234 syslog(LOG_NOTICE, " ");
235 syslog(LOG_NOTICE, "*** Citadel server engine ***\n");
236 syslog(LOG_NOTICE, "Version %d (build %s) ***", REV_LEVEL, svn_revision());
237 syslog(LOG_NOTICE, "Copyright (C) 1987-2017 by the Citadel development team.");
238 syslog(LOG_NOTICE, "This program is distributed under the terms of the GNU "
239 "General Public License.");
240 syslog(LOG_NOTICE, " ");
241 syslog(LOG_DEBUG, "Called as: %s", argv[0]);
242 syslog(LOG_INFO, "%s", libcitadel_version_string());
245 /* on some dists rundir gets purged on startup. so we need to recreate it. */
247 if (stat(ctdl_run_dir, &filestats)==-1){
248 #ifdef HAVE_GETPWUID_R
249 #ifdef SOLARIS_GETPWUID
250 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
251 #else // SOLARIS_GETPWUID
252 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
253 #endif // SOLARIS_GETPWUID
254 #else // HAVE_GETPWUID_R
256 #endif // HAVE_GETPWUID_R
258 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST))
260 "unable to create run directory [%s]: %s",
261 ctdl_run_dir, strerror(errno));
263 if (chown(ctdl_run_dir, ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0)
265 "unable to set the access rights for [%s]: %s",
266 ctdl_run_dir, strerror(errno));
278 * Do non system dependent startup functions.
283 * Check that the control record is correct and place sensible values if it isn't
288 * Run any upgrade entry points
290 syslog(LOG_INFO, "Upgrading modules.");
294 * Load the user for the masterCC or create them if they don't exist
296 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
298 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
299 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
300 CtdlPutUser(&masterCC.user);
301 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
305 * Bind the server to a Unix-domain socket (user client access)
307 CtdlRegisterServiceHook(0,
309 citproto_begin_session,
315 * Bind the server to a Unix-domain socket (admin client access)
317 CtdlRegisterServiceHook(0,
318 file_citadel_admin_socket,
319 citproto_begin_admin_session,
323 chmod(file_citadel_admin_socket, S_IRWXU); /* for your eyes only */
326 * Bind the server to our favorite TCP port (usually 504).
328 CtdlRegisterServiceHook(CtdlGetConfigInt("c_port_number"),
330 citproto_begin_session,
339 * Load any server-side extensions available here.
341 syslog(LOG_INFO, "Initializing server extensions");
343 initialise_modules(0);
345 eDebuglist[1] = getenv("CITADEL_LOGDEBUG");
346 CtdlSetDebugLogFacilities(eDebuglist, 2);
349 * If we need host auth, start our chkpwd daemon.
351 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
352 start_chkpwd_daemon();
357 * check, whether we're fired up another time after a crash.
358 * if, post an aide message, so the admin has a chance to react.
364 * Now that we've bound the sockets, change to the Citadel user id and its
365 * corresponding group ids
367 if (drop_root_perms) {
368 cdb_chmod_data(); /* make sure we own our data files */
370 #ifdef HAVE_GETPWUID_R
371 #ifdef SOLARIS_GETPWUID
372 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
373 #else // SOLARIS_GETPWUID
374 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
375 #endif // SOLARIS_GETPWUID
376 #else // HAVE_GETPWUID_R
378 #endif // HAVE_GETPWUID_R
381 syslog(LOG_CRIT, "WARNING: getpwuid(%ld): %s"
382 "Group IDs will be incorrect.\n", (long)CTDLUID,
385 initgroups(pw.pw_name, pw.pw_gid);
386 if (setgid(pw.pw_gid))
387 syslog(LOG_CRIT, "setgid(%ld): %s", (long)pw.pw_gid,
390 syslog(LOG_INFO, "Changing uid to %ld", (long)CTDLUID);
391 if (setuid(CTDLUID) != 0) {
392 syslog(LOG_CRIT, "setuid() failed: %s", strerror(errno));
394 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
395 prctl(PR_SET_DUMPABLE, 1);
399 /* We want to check for idle sessions once per minute */
400 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
404 int exit_code = master_cleanup(exit_signal);