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"
56 #include "housekeeping.h"
58 #include "citadel_dirs.c"
60 #ifdef HAVE_SYS_SELECT_H
61 #include <sys/select.h>
68 int running_as_daemon = 0;
71 * Here's where it all begins.
73 int main(int argc, char **argv)
76 int a, i; /* General-purpose variables */
77 struct passwd pw, *pwp = NULL;
79 int drop_root_perms = 1;
83 char relhome[PATH_MAX]="";
84 char ctdldir[PATH_MAX]=CTDLDIR;
86 struct stat filestats;
89 /* initialize the master context */
92 /* parse command-line arguments */
93 for (a=1; a<argc; ++a) {
95 if (!strncmp(argv[a], "-l", 2)) {
96 safestrncpy(facility, &argv[a][2], sizeof(facility));
97 syslog_facility = SyslogFacility(facility);
101 /* run in the background if -d was specified */
102 else if (!strcmp(argv[a], "-d")) {
103 running_as_daemon = 1;
106 /* -x specifies the desired logging level */
107 else if (!strncmp(argv[a], "-x", 2)) {
108 verbosity = atoi(&argv[a][2]);
111 else if (!strncmp(argv[a], "-h", 2)) {
112 relh=argv[a][2]!='/';
113 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
114 sizeof ctdl_home_directory);
116 safestrncpy(relhome, &argv[a][2],
122 else if (!strncmp(argv[a], "-t", 2)) {
123 freopen(&argv[a][2], "w", stderr);
126 else if (!strncmp(argv[a], "-f", 2)) {
130 /* -r tells the server not to drop root permissions. don't use
131 * this unless you know what you're doing. this should be
132 * removed in the next release if it proves unnecessary. */
133 else if (!strcmp(argv[a], "-r"))
136 /* any other parameter makes it crash and burn */
138 lprintf(CTDL_EMERG, "citserver: usage: "
143 " [-xLogLevel] [-hHomeDir]\n");
149 calc_dirs_n_files(relh, home, relhome, ctdldir);
151 /* daemonize, if we were asked to */
152 if (running_as_daemon) {
157 /* Initialize the syslogger. Yes, we are really using 0 as the
158 * facility, because we are going to bitwise-OR the facility to
159 * the severity of each message, allowing us to write to other
160 * facilities when we need to...
163 openlog("citadel", LOG_NDELAY, 0);
164 setlogmask(LOG_UPTO(verbosity));
167 /* Tell 'em who's in da house */
168 lprintf(CTDL_NOTICE, "\n");
169 lprintf(CTDL_NOTICE, "\n");
171 "*** Citadel server engine v%d.%02d ***\n",
172 (REV_LEVEL/100), (REV_LEVEL%100));
174 "Copyright (C) 1987-2007 by the Citadel development team.\n");
176 "This program is distributed under the terms of the GNU "
177 "General Public License.\n");
178 lprintf(CTDL_NOTICE, "\n");
179 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
181 /* Load site-specific parameters, and set the ipgm secret */
182 lprintf(CTDL_INFO, "Loading citadel.config\n");
184 config.c_ipgm_secret = rand();
187 lprintf(CTDL_INFO, "Acquiring control record\n");
191 /* on some dists rundir gets purged on startup. so we need to recreate it. */
193 if (stat(ctdl_run_dir, &filestats)==-1){
194 #ifdef SOLARIS_GETPWUID
195 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
197 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
199 mkdir(ctdl_run_dir, 0755);
200 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
210 * Do non system dependent startup functions.
215 * Bind the server to a Unix-domain socket.
217 CtdlRegisterServiceHook(0,
219 citproto_begin_session,
224 * Bind the server to our favorite TCP port (usually 504).
226 CtdlRegisterServiceHook(config.c_port_number,
228 citproto_begin_session,
233 * Load any server-side extensions available here.
235 lprintf(CTDL_INFO, "Initializing server extensions\n");
236 size = strlen(ctdl_home_directory) + 9;
237 initialize_server_extensions();
240 * Now that we've bound the sockets, change to the Citadel user id and its
241 * corresponding group ids
243 if (drop_root_perms) {
244 cdb_chmod_data(); /* make sure we own our data files */
246 #ifdef SOLARIS_GETPWUID
247 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
249 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
252 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
253 "Group IDs will be incorrect.\n", (long)CTDLUID,
256 initgroups(pw.pw_name, pw.pw_gid);
257 if (setgid(pw.pw_gid))
258 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
261 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
262 if (setuid(CTDLUID) != 0) {
263 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
265 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
266 prctl(PR_SET_DUMPABLE, 1);
270 /* We want to check for idle sessions once per minute */
271 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
274 * Now create a bunch of worker threads.
276 lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
277 config.c_min_workers-1);
278 begin_critical_section(S_WORKER_LIST);
279 for (i=0; i<(config.c_min_workers-1); ++i) {
282 end_critical_section(S_WORKER_LIST);
284 /* Create the indexer thread. */
285 create_maintenance_threads();
287 /* This thread is now useless. It can't be turned into a worker
288 * thread because its stack is too small, but it can't be killed
289 * either because the whole server process would exit. So we just
290 * join to the first worker thread and exit when it exits.
292 pthread_join(worker_list->tid, NULL);