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.
17 #include <sys/types.h>
21 #include <libcitadel.h>
23 #include "citserver.h"
24 #include "svn_revision.h"
25 #include "modules_init.h"
28 #include "serv_extensions.h"
29 #include "citadel_dirs.h"
34 const char *CitadelServiceUDS="citadel-UDS";
35 const char *CitadelServiceTCP="citadel-TCP";
36 void go_threading(void);
40 * Create or remove a lock file, so we only have one Citadel Server running at a time.
42 void ctdl_lockfile(int yo) {
43 static char lockfilename[SIZ];
47 syslog(LOG_DEBUG, "main: creating lockfile");
48 snprintf(lockfilename, sizeof lockfilename, "%s/citadel.lock", ctdl_run_dir);
49 fp = fopen(lockfilename, "w");
51 syslog(LOG_ERR, "%s: %s", lockfilename, strerror(errno));
54 if (flock(fileno(fp), (LOCK_EX|LOCK_NB)) != 0) {
55 syslog(LOG_ERR, "main: cannot lock %s , is another citserver running?", lockfilename);
61 syslog(LOG_DEBUG, "main: removing lockfile");
63 flock(fileno(fp), LOCK_UN);
69 * Here's where it all begins.
71 int main(int argc, char **argv)
75 int a; /* General-purpose variables */
76 struct passwd pw, *pwp = NULL;
78 int drop_root_perms = 1;
82 char relhome[PATH_MAX]="";
83 char ctdldir[PATH_MAX]=CTDLDIR;
84 int syslog_facility = LOG_DAEMON;
85 const char *eDebuglist[] = {NULL, NULL};
87 struct passwd *p = NULL;
89 struct stat filestats;
92 eCrashParameters params;
93 // eCrashSymbolTable symbol_table;
96 /* initialize the master context */
98 InitializeMasterTSD();
100 /* parse command-line arguments */
101 while ((a=getopt(argc, argv, "l:dh:x:t:B:Dru:")) != EOF) switch(a) {
104 safestrncpy(facility, optarg, sizeof(facility));
105 syslog_facility = SyslogFacility(facility);
108 /* run in the background if -d was specified */
110 running_as_daemon = 1;
114 relh = optarg[0] != '/';
116 safestrncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
119 safestrncpy(relhome, optarg, sizeof relhome);
125 eDebuglist [0] = optarg;
128 case 't': /* deprecated */
130 case 'B': /* Basesize */
131 basesize = atoi(optarg);
138 /* -r tells the server not to drop root permissions.
139 * Don't use this unless you know what you're doing.
145 /* -u tells the server what uid to run under... */
152 p = getpwnam(optarg);
163 /* any other parameter makes it crash and burn */
164 fprintf(stderr, "citserver: usage: "
174 /* Last ditch effort to determine the user name ... if there's a user called "citadel" then use that */
176 p = getpwnam("citadel");
181 p = getpwnam("guest");
191 if ((ctdluid == 0) && (drop_root_perms == 0)) {
192 fprintf(stderr, "citserver: cannot determine user to run as; please specify -r or -u options\n");
193 exit(CTDLEXIT_UNUSER);
196 StartLibCitadel(basesize);
198 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
202 calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
203 /* daemonize, if we were asked to */
204 if (running_as_daemon) {
209 /* Tell 'em who's in da house */
210 syslog(LOG_INFO, " ");
211 syslog(LOG_INFO, " ");
212 syslog(LOG_INFO, "*** Citadel server engine ***\n");
213 syslog(LOG_INFO, "Version %d (build %s) ***", REV_LEVEL, svn_revision());
214 syslog(LOG_INFO, "Copyright (C) 1987-2017 by the Citadel development team.");
215 syslog(LOG_INFO, "This program is distributed under the terms of the GNU General Public License.");
216 syslog(LOG_INFO, " ");
217 syslog(LOG_INFO, "%s", libcitadel_version_string());
220 /* on some dists rundir gets purged on startup. so we need to recreate it. */
222 if (stat(ctdl_run_dir, &filestats)==-1){
223 #ifdef HAVE_GETPWUID_R
224 #ifdef SOLARIS_GETPWUID
225 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
226 #else // SOLARIS_GETPWUID
227 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
228 #endif // SOLARIS_GETPWUID
229 #else // HAVE_GETPWUID_R
231 #endif // HAVE_GETPWUID_R
233 if ((mkdir(ctdl_run_dir, 0755) != 0) && (errno != EEXIST)) {
234 syslog(LOG_ERR, "main: unable to create run directory [%s]: %s", ctdl_run_dir, strerror(errno));
237 if (chown(ctdl_run_dir, ctdluid, (pwp==NULL)?-1:pw.pw_gid) != 0) {
238 syslog(LOG_ERR, "main: unable to set the access rights for [%s]: %s", ctdl_run_dir, strerror(errno));
249 * Do non system dependent startup functions.
254 * Check that the control record is correct and place sensible values if it isn't
259 * Run any upgrade entry points
261 syslog(LOG_INFO, "main: upgrading modules");
265 * Load the user for the masterCC or create them if they don't exist
267 if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
269 /* User doesn't exist. We can't use create user here as the user number needs to be 0 */
270 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
271 CtdlPutUser(&masterCC.user);
272 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /* Just to be safe */
276 * Bind the server to a Unix-domain socket (user client access)
278 CtdlRegisterServiceHook(0,
280 citproto_begin_session,
286 * Bind the server to a Unix-domain socket (admin client access)
288 CtdlRegisterServiceHook(0,
289 file_citadel_admin_socket,
290 citproto_begin_admin_session,
294 chmod(file_citadel_admin_socket, S_IRWXU); /* for your eyes only */
297 * Bind the server to our favorite TCP port (usually 504).
299 CtdlRegisterServiceHook(CtdlGetConfigInt("c_port_number"),
301 citproto_begin_session,
310 * Load any server-side extensions available here.
312 syslog(LOG_INFO, "main: initializing server extensions");
314 initialise_modules(0);
316 eDebuglist[1] = getenv("CITADEL_LOGDEBUG");
317 CtdlSetDebugLogFacilities(eDebuglist, 2);
320 * If we need host auth, start our chkpwd daemon.
322 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
323 start_chkpwd_daemon();
328 * check, whether we're fired up another time after a crash.
329 * if, post an aide message, so the admin has a chance to react.
335 * Now that we've bound the sockets, change to the Citadel user id and its
336 * corresponding group ids
338 if (drop_root_perms) {
339 cdb_chmod_data(); /* make sure we own our data files */
341 #ifdef HAVE_GETPWUID_R
342 #ifdef SOLARIS_GETPWUID
343 pwp = getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf));
344 #else // SOLARIS_GETPWUID
345 getpwuid_r(ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
346 #endif // SOLARIS_GETPWUID
347 #else // HAVE_GETPWUID_R
349 #endif // HAVE_GETPWUID_R
352 syslog(LOG_ERR, "main: WARNING, getpwuid(%ld): %s" "Group IDs will be incorrect.", (long)CTDLUID, strerror(errno));
354 initgroups(pw.pw_name, pw.pw_gid);
355 if (setgid(pw.pw_gid))
356 syslog(LOG_ERR, "main: setgid(%ld): %s", (long)pw.pw_gid, strerror(errno));
358 syslog(LOG_INFO, "main: changing uid to %ld", (long)CTDLUID);
359 if (setuid(CTDLUID) != 0) {
360 syslog(LOG_ERR, "main: setuid() failed: %s", strerror(errno));
362 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
363 prctl(PR_SET_DUMPABLE, 1);
367 /* We want to check for idle sessions once per minute */
368 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER, PRIO_CLEANUP + 1);
372 int exit_code = master_cleanup(exit_signal);