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>
44 #ifdef HAVE_SYS_PRCTL_H
45 #include <sys/prctl.h>
49 #include "serv_extensions.h"
50 #include "sysdep_decls.h"
51 #include "citserver.h"
57 #include "housekeeping.h"
59 #include "citadel_dirs.c"
61 #include "modules_init.h"
64 #ifdef HAVE_SYS_SELECT_H
65 #include <sys/select.h>
73 * Here's where it all begins.
75 int main(int argc, char **argv)
78 int a, i; /* General-purpose variables */
79 struct passwd pw, *pwp = NULL;
81 int drop_root_perms = 1;
85 char relhome[PATH_MAX]="";
86 char ctdldir[PATH_MAX]=CTDLDIR;
88 struct stat filestats;
91 /* initialise semaphores here. Patch by Matt and davew
92 * its called here as they are needed by lprintf for thread safety
94 InitialiseSemaphores();
96 /* initialize the master context */
99 /* parse command-line arguments */
100 for (a=1; a<argc; ++a) {
102 if (!strncmp(argv[a], "-l", 2)) {
103 safestrncpy(facility, &argv[a][2], sizeof(facility));
104 syslog_facility = SyslogFacility(facility);
108 /* run in the background if -d was specified */
109 else if (!strcmp(argv[a], "-d")) {
110 running_as_daemon = 1;
113 /* -x specifies the desired logging level */
114 else if (!strncmp(argv[a], "-x", 2)) {
115 verbosity = atoi(&argv[a][2]);
118 else if (!strncmp(argv[a], "-h", 2)) {
119 relh=argv[a][2]!='/';
120 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
121 sizeof ctdl_home_directory);
123 safestrncpy(relhome, &argv[a][2],
129 else if (!strncmp(argv[a], "-t", 2)) {
130 freopen(&argv[a][2], "w", stderr);
133 else if (!strncmp(argv[a], "-f", 2)) {
137 /* -r tells the server not to drop root permissions. don't use
138 * this unless you know what you're doing. this should be
139 * removed in the next release if it proves unnecessary. */
140 else if (!strcmp(argv[a], "-r"))
143 /* any other parameter makes it crash and burn */
145 lprintf(CTDL_EMERG, "citserver: usage: "
150 " [-xLogLevel] [-hHomeDir]\n");
156 calc_dirs_n_files(relh, home, relhome, ctdldir);
158 /* daemonize, if we were asked to */
159 if (running_as_daemon) {
164 /* Initialize the syslogger. Yes, we are really using 0 as the
165 * facility, because we are going to bitwise-OR the facility to
166 * the severity of each message, allowing us to write to other
167 * facilities when we need to...
170 openlog("citadel", LOG_NDELAY, 0);
171 setlogmask(LOG_UPTO(verbosity));
174 /* Tell 'em who's in da house */
175 lprintf(CTDL_NOTICE, "\n");
176 lprintf(CTDL_NOTICE, "\n");
178 "*** Citadel server engine v%d.%02d ***\n",
179 (REV_LEVEL/100), (REV_LEVEL%100));
181 "Copyright (C) 1987-2007 by the Citadel development team.\n");
183 "This program is distributed under the terms of the GNU "
184 "General Public License.\n");
185 lprintf(CTDL_NOTICE, "\n");
186 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
188 /* Load site-specific parameters, and set the ipgm secret */
189 lprintf(CTDL_INFO, "Loading citadel.config\n");
191 config.c_ipgm_secret = rand();
194 lprintf(CTDL_INFO, "Acquiring control record\n");
198 /* on some dists rundir gets purged on startup. so we need to recreate it. */
200 if (stat(ctdl_run_dir, &filestats)==-1){
201 #ifdef SOLARIS_GETPWUID
202 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
204 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
206 mkdir(ctdl_run_dir, 0755);
207 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
217 * Do non system dependent startup functions.
222 * Bind the server to a Unix-domain socket.
224 CtdlRegisterServiceHook(0,
226 citproto_begin_session,
231 * Bind the server to our favorite TCP port (usually 504).
233 CtdlRegisterServiceHook(config.c_port_number,
235 citproto_begin_session,
240 * Load any server-side extensions available here.
242 lprintf(CTDL_INFO, "Initializing server extensions\n");
243 size = strlen(ctdl_home_directory) + 9;
246 initialize_server_extensions();
249 initialise_modules();
254 * If we need host auth, start our chkpwd daemon.
256 if (config.c_auth_mode == 1) {
257 start_chkpwd_daemon();
261 * Now that we've bound the sockets, change to the Citadel user id and its
262 * corresponding group ids
264 if (drop_root_perms) {
265 cdb_chmod_data(); /* make sure we own our data files */
267 #ifdef SOLARIS_GETPWUID
268 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
270 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
273 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
274 "Group IDs will be incorrect.\n", (long)CTDLUID,
277 initgroups(pw.pw_name, pw.pw_gid);
278 if (setgid(pw.pw_gid))
279 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
282 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
283 if (setuid(CTDLUID) != 0) {
284 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
286 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
287 prctl(PR_SET_DUMPABLE, 1);
291 /* We want to check for idle sessions once per minute */
292 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
295 * Now create a bunch of worker threads.
297 lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
298 config.c_min_workers-1);
299 begin_critical_section(S_WORKER_LIST);
300 for (i=0; i<(config.c_min_workers-1); ++i) {
303 end_critical_section(S_WORKER_LIST);
305 /* Create the maintenance threads. */
306 create_maintenance_threads();
308 /* This thread is now useless. It can't be turned into a worker
309 * thread because its stack is too small, but it can't be killed
310 * either because the whole server process would exit. So we just
311 * join to the first worker thread and exit when it exits.
313 pthread_join(worker_list->tid, NULL);