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"
55 #include "housekeeping.h"
57 #include "citadel_dirs.c"
59 #ifdef HAVE_SYS_SELECT_H
60 #include <sys/select.h>
67 int running_as_daemon = 0;
70 * Here's where it all begins.
72 int main(int argc, char **argv)
75 int a, i; /* General-purpose variables */
77 int drop_root_perms = 1;
81 char relhome[PATH_MAX]="";
82 char ctdldir[PATH_MAX]=CTDLDIR;
84 struct stat filestats;
87 /* initialize the master context */
90 /* parse command-line arguments */
91 for (a=1; a<argc; ++a) {
93 if (!strncmp(argv[a], "-l", 2)) {
94 safestrncpy(facility, &argv[a][2], sizeof(facility));
95 syslog_facility = SyslogFacility(facility);
99 /* run in the background if -d was specified */
100 else if (!strcmp(argv[a], "-d")) {
101 running_as_daemon = 1;
104 /* -x specifies the desired logging level */
105 else if (!strncmp(argv[a], "-x", 2)) {
106 verbosity = atoi(&argv[a][2]);
109 else if (!strncmp(argv[a], "-h", 2)) {
110 relh=argv[a][2]!='/';
111 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
112 sizeof ctdl_home_directory);
114 safestrncpy(relhome, &argv[a][2],
120 else if (!strncmp(argv[a], "-t", 2)) {
121 freopen(&argv[a][2], "w", stderr);
124 else if (!strncmp(argv[a], "-f", 2)) {
128 /* -r tells the server not to drop root permissions. don't use
129 * this unless you know what you're doing. this should be
130 * removed in the next release if it proves unnecessary. */
131 else if (!strcmp(argv[a], "-r"))
134 /* any other parameter makes it crash and burn */
136 lprintf(CTDL_EMERG, "citserver: usage: "
141 " [-xLogLevel] [-hHomeDir]\n");
147 calc_dirs_n_files(relh, home, relhome, ctdldir);
149 /* daemonize, if we were asked to */
150 if (running_as_daemon) {
155 /* Initialize the syslogger. Yes, we are really using 0 as the
156 * facility, because we are going to bitwise-OR the facility to
157 * the severity of each message, allowing us to write to other
158 * facilities when we need to...
161 if (running_as_daemon) {
162 openlog("citadel", LOG_NDELAY, 0);
165 openlog("citadel", LOG_PERROR|LOG_NDELAY, 0);
167 setlogmask(LOG_UPTO(verbosity));
170 /* Tell 'em who's in da house */
171 lprintf(CTDL_NOTICE, "\n");
172 lprintf(CTDL_NOTICE, "\n");
174 "*** Citadel server engine v%d.%02d ***\n",
175 (REV_LEVEL/100), (REV_LEVEL%100));
177 "Copyright (C) 1987-2005 by the Citadel development team.\n");
179 "This program is distributed under the terms of the GNU "
180 "General Public License.\n");
181 lprintf(CTDL_NOTICE, "\n");
182 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
184 /* Load site-specific parameters, and set the ipgm secret */
185 lprintf(CTDL_INFO, "Loading citadel.config\n");
187 config.c_ipgm_secret = rand();
191 /* on some dists rundir gets purged on startup. so we need to recreate it. */
193 if (stat(ctdl_run_dir, &filestats)==-1){
194 pw=getpwuid(config.c_ctdluid);
195 mkdir(ctdl_run_dir, 0755);
196 chown(ctdl_run_dir, config.c_ctdluid, (pw==NULL)?-1:pw->pw_gid);
207 * Do non system dependent startup functions.
212 * Bind the server to a Unix-domain socket.
214 CtdlRegisterServiceHook(0,
216 citproto_begin_session,
221 * Bind the server to our favorite TCP port (usually 504).
223 CtdlRegisterServiceHook(config.c_port_number,
225 citproto_begin_session,
230 * Load any server-side extensions available here.
232 lprintf(CTDL_INFO, "Initializing server extensions\n");
233 size = strlen(ctdl_home_directory) + 9;
234 initialize_server_extensions();
237 * Now that we've bound the sockets, change to the Citadel user id and its
238 * corresponding group ids
240 if (drop_root_perms) {
241 if ((pw = getpwuid(CTDLUID)) == NULL)
242 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
243 "Group IDs will be incorrect.\n", (long)CTDLUID,
246 initgroups(pw->pw_name, pw->pw_gid);
247 if (setgid(pw->pw_gid))
248 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
251 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
252 if (setuid(CTDLUID) != 0) {
253 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
255 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
256 prctl(PR_SET_DUMPABLE, 1);
260 /* We want to check for idle sessions once per minute */
261 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
264 * Now create a bunch of worker threads.
266 lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
267 config.c_min_workers-1);
268 begin_critical_section(S_WORKER_LIST);
269 for (i=0; i<(config.c_min_workers-1); ++i) {
272 end_critical_section(S_WORKER_LIST);
274 /* Create the indexer thread. */
275 create_maintenance_threads();
277 /* This thread is now useless. It can't be turned into a worker
278 * thread because its stack is too small, but it can't be killed
279 * either because the whole server process would exit. So we just
280 * join to the first worker thread and exit when it exits.
282 pthread_join(worker_list->tid, NULL);