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;
83 struct stat filestats;
85 /* initialize the master context */
88 /* parse command-line arguments */
89 for (a=1; a<argc; ++a) {
91 if (!strncmp(argv[a], "-l", 2)) {
92 safestrncpy(facility, &argv[a][2], sizeof(facility));
93 syslog_facility = SyslogFacility(facility);
97 /* run in the background if -d was specified */
98 else if (!strcmp(argv[a], "-d")) {
99 running_as_daemon = 1;
102 /* -x specifies the desired logging level */
103 else if (!strncmp(argv[a], "-x", 2)) {
104 verbosity = atoi(&argv[a][2]);
107 else if (!strncmp(argv[a], "-h", 2)) {
108 relh=argv[a][2]!='/';
109 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
110 sizeof ctdl_home_directory);
112 safestrncpy(relhome, &argv[a][2],
118 else if (!strncmp(argv[a], "-t", 2)) {
119 freopen(&argv[a][2], "w", stderr);
122 else if (!strncmp(argv[a], "-f", 2)) {
126 /* -r tells the server not to drop root permissions. don't use
127 * this unless you know what you're doing. this should be
128 * removed in the next release if it proves unnecessary. */
129 else if (!strcmp(argv[a], "-r"))
132 /* any other parameter makes it crash and burn */
134 lprintf(CTDL_EMERG, "citserver: usage: "
139 " [-xLogLevel] [-hHomeDir]\n");
145 calc_dirs_n_files(relh, home, relhome, ctdldir);
147 /* daemonize, if we were asked to */
148 if (running_as_daemon) {
153 /* Initialize the syslogger. Yes, we are really using 0 as the
154 * facility, because we are going to bitwise-OR the facility to
155 * the severity of each message, allowing us to write to other
156 * facilities when we need to...
159 if (running_as_daemon) {
160 openlog("citadel", LOG_NDELAY, 0);
163 openlog("citadel", LOG_PERROR|LOG_NDELAY, 0);
165 setlogmask(LOG_UPTO(verbosity));
168 /* Tell 'em who's in da house */
169 lprintf(CTDL_NOTICE, "\n");
170 lprintf(CTDL_NOTICE, "\n");
172 "*** Citadel server engine v%d.%02d ***\n",
173 (REV_LEVEL/100), (REV_LEVEL%100));
175 "Copyright (C) 1987-2005 by the Citadel development team.\n");
177 "This program is distributed under the terms of the GNU "
178 "General Public License.\n");
179 lprintf(CTDL_NOTICE, "\n");
180 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
182 /* Load site-specific parameters, and set the ipgm secret */
183 lprintf(CTDL_INFO, "Loading citadel.config\n");
185 config.c_ipgm_secret = rand();
189 /* on some dists rundir gets purged on startup. so we need to recreate it. */
191 if (stat(ctdl_run_dir, &filestats)==-1){
192 pw=getpwuid(config.c_ctdluid);
193 mkdir(ctdl_run_dir, 0755);
194 chown(ctdl_run_dir, config.c_ctdluid, (pw==NULL)?-1:pw->pw_gid);
205 * Do non system dependent startup functions.
210 * Bind the server to a Unix-domain socket.
212 CtdlRegisterServiceHook(0,
214 citproto_begin_session,
219 * Bind the server to our favorite TCP port (usually 504).
221 CtdlRegisterServiceHook(config.c_port_number,
223 citproto_begin_session,
228 * Load any server-side extensions available here.
230 lprintf(CTDL_INFO, "Initializing server extensions\n");
231 size = strlen(ctdl_home_directory) + 9;
232 initialize_server_extensions();
235 * Now that we've bound the sockets, change to the Citadel user id and its
236 * corresponding group ids
238 if (drop_root_perms) {
239 if ((pw = getpwuid(CTDLUID)) == NULL)
240 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
241 "Group IDs will be incorrect.\n", (long)CTDLUID,
244 initgroups(pw->pw_name, pw->pw_gid);
245 if (setgid(pw->pw_gid))
246 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
249 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
250 if (setuid(CTDLUID) != 0) {
251 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
253 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
254 prctl(PR_SET_DUMPABLE, 1);
258 /* We want to check for idle sessions once per minute */
259 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
262 * Now create a bunch of worker threads.
264 lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
265 config.c_min_workers-1);
266 begin_critical_section(S_WORKER_LIST);
267 for (i=0; i<(config.c_min_workers-1); ++i) {
270 end_critical_section(S_WORKER_LIST);
272 /* Create the indexer thread. */
273 create_maintenance_threads();
275 /* This thread is now useless. It can't be turned into a worker
276 * thread because its stack is too small, but it can't be killed
277 * either because the whole server process would exit. So we just
278 * join to the first worker thread and exit when it exits.
280 pthread_join(worker_list->tid, NULL);