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>
43 #ifdef HAVE_SYS_PRCTL_H
44 #include <sys/prctl.h>
48 #include "serv_extensions.h"
49 #include "sysdep_decls.h"
50 #include "citserver.h"
54 #include "housekeeping.h"
56 #include "citadel_dirs.c"
58 #ifdef HAVE_SYS_SELECT_H
59 #include <sys/select.h>
66 int running_as_daemon = 0;
69 * Here's where it all begins.
71 int main(int argc, char **argv)
74 int a, i; /* General-purpose variables */
76 int drop_root_perms = 1;
80 char relhome[PATH_MAX]="";
81 char ctdldir[PATH_MAX]=CTDLDIR;
83 /* initialize the master context */
86 /* parse command-line arguments */
87 for (a=1; a<argc; ++a) {
89 if (!strncmp(argv[a], "-l", 2)) {
90 safestrncpy(facility, &argv[a][2], sizeof(facility));
91 syslog_facility = SyslogFacility(facility);
95 /* run in the background if -d was specified */
96 else if (!strcmp(argv[a], "-d")) {
97 running_as_daemon = 1;
100 /* -x specifies the desired logging level */
101 else if (!strncmp(argv[a], "-x", 2)) {
102 verbosity = atoi(&argv[a][2]);
105 else if (!strncmp(argv[a], "-h", 2)) {
106 relh=argv[a][2]!='/';
107 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
108 sizeof ctdl_home_directory);
110 safestrncpy(relhome, &argv[a][2],
116 else if (!strncmp(argv[a], "-t", 2)) {
117 freopen(&argv[a][2], "w", stderr);
120 else if (!strncmp(argv[a], "-f", 2)) {
124 /* -r tells the server not to drop root permissions. don't use
125 * this unless you know what you're doing. this should be
126 * removed in the next release if it proves unnecessary. */
127 else if (!strcmp(argv[a], "-r"))
130 /* any other parameter makes it crash and burn */
132 lprintf(CTDL_EMERG, "citserver: usage: "
137 " [-xLogLevel] [-hHomeDir]\n");
143 calc_dirs_n_files(relh, home, relhome, ctdldir);
145 /* daemonize, if we were asked to */
146 if (running_as_daemon) {
151 /* Initialize the syslogger. Yes, we are really using 0 as the
152 * facility, because we are going to bitwise-OR the facility to
153 * the severity of each message, allowing us to write to other
154 * facilities when we need to...
157 if (running_as_daemon) {
158 openlog("citadel", LOG_NDELAY, 0);
161 openlog("citadel", LOG_PERROR|LOG_NDELAY, 0);
163 setlogmask(LOG_UPTO(verbosity));
166 /* Tell 'em who's in da house */
167 lprintf(CTDL_NOTICE, "\n");
168 lprintf(CTDL_NOTICE, "\n");
170 "*** Citadel server engine v%d.%02d ***\n",
171 (REV_LEVEL/100), (REV_LEVEL%100));
173 "Copyright (C) 1987-2005 by the Citadel development team.\n");
175 "This program is distributed under the terms of the GNU "
176 "General Public License.\n");
177 lprintf(CTDL_NOTICE, "\n");
178 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
180 /* Load site-specific parameters, and set the ipgm secret */
181 lprintf(CTDL_INFO, "Loading citadel.config\n");
183 config.c_ipgm_secret = rand();
190 * Do non system dependent startup functions.
195 * Bind the server to a Unix-domain socket.
197 CtdlRegisterServiceHook(0,
199 citproto_begin_session,
204 * Bind the server to our favorite TCP port (usually 504).
206 CtdlRegisterServiceHook(config.c_port_number,
208 citproto_begin_session,
213 * Load any server-side extensions available here.
215 lprintf(CTDL_INFO, "Initializing server extensions\n");
216 size = strlen(ctdl_home_directory) + 9;
217 initialize_server_extensions();
220 * Now that we've bound the sockets, change to the Citadel user id and its
221 * corresponding group ids
223 if (drop_root_perms) {
224 if ((pw = getpwuid(CTDLUID)) == NULL)
225 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
226 "Group IDs will be incorrect.\n", (long)CTDLUID,
229 initgroups(pw->pw_name, pw->pw_gid);
230 if (setgid(pw->pw_gid))
231 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
234 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
235 if (setuid(CTDLUID) != 0) {
236 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
238 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
239 prctl(PR_SET_DUMPABLE, 1);
243 /* We want to check for idle sessions once per minute */
244 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
247 * Now create a bunch of worker threads.
249 lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
250 config.c_min_workers-1);
251 begin_critical_section(S_WORKER_LIST);
252 for (i=0; i<(config.c_min_workers-1); ++i) {
255 end_critical_section(S_WORKER_LIST);
257 /* Create the indexer thread. */
258 create_maintenance_threads();
260 /* This thread is now useless. It can't be turned into a worker
261 * thread because its stack is too small, but it can't be killed
262 * either because the whole server process would exit. So we just
263 * join to the first worker thread and exit when it exits.
265 pthread_join(worker_list->tid, NULL);