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"
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
65 int running_as_daemon = 0;
68 * Here's where it all begins.
70 int main(int argc, char **argv)
73 int a, i; /* General-purpose variables */
75 int drop_root_perms = 1;
78 /* initialize the master context */
81 /* set default syslog facility */
82 syslog_facility = LOG_DAEMON;
84 /* parse command-line arguments */
85 for (a=1; a<argc; ++a) {
87 if (!strncmp(argv[a], "-l", 2)) {
88 safestrncpy(facility, argv[a], sizeof(facility));
89 syslog_facility = SyslogFacility(facility);
93 /* run in the background if -d was specified */
94 else if (!strcmp(argv[a], "-d")) {
95 running_as_daemon = 1;
98 /* -x specifies the desired logging level */
99 else if (!strncmp(argv[a], "-x", 2)) {
100 verbosity = atoi(&argv[a][2]);
103 else if (!strncmp(argv[a], "-h", 2)) {
104 safestrncpy(ctdl_home_directory, &argv[a][2],
105 sizeof ctdl_home_directory);
109 else if (!strncmp(argv[a], "-t", 2)) {
110 freopen(&argv[a][2], "w", stderr);
113 else if (!strncmp(argv[a], "-f", 2)) {
117 /* -r tells the server not to drop root permissions. don't use
118 * this unless you know what you're doing. this should be
119 * removed in the next release if it proves unnecessary. */
120 else if (!strcmp(argv[a], "-r"))
123 /* any other parameter makes it crash and burn */
125 lprintf(CTDL_EMERG, "citserver: usage: "
130 " [-xLogLevel] [-hHomeDir]\n");
136 /* daemonize, if we were asked to */
137 if (running_as_daemon) {
142 /* initialize the syslog facility */
144 if (running_as_daemon) {
145 openlog("citadel", LOG_NDELAY, syslog_facility);
148 openlog("citadel", LOG_PERROR|LOG_NDELAY,
151 setlogmask(LOG_UPTO(verbosity));
154 /* Tell 'em who's in da house */
155 lprintf(CTDL_NOTICE, "\n");
156 lprintf(CTDL_NOTICE, "\n");
158 "*** Citadel server engine v%d.%02d ***\n",
159 (REV_LEVEL/100), (REV_LEVEL%100));
161 "Copyright (C) 1987-2005 by the Citadel development team.\n");
163 "This program is distributed under the terms of the GNU "
164 "General Public License.\n");
165 lprintf(CTDL_NOTICE, "\n");
166 lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
168 /* Load site-specific parameters, and set the ipgm secret */
169 lprintf(CTDL_INFO, "Loading citadel.config\n");
171 config.c_ipgm_secret = rand();
178 * Do non system dependent startup functions.
183 * Bind the server to a Unix-domain socket.
185 CtdlRegisterServiceHook(0,
187 citproto_begin_session,
192 * Bind the server to our favorite TCP port (usually 504).
194 CtdlRegisterServiceHook(config.c_port_number,
196 citproto_begin_session,
201 * Load any server-side extensions available here.
203 lprintf(CTDL_INFO, "Initializing server extensions\n");
204 size = strlen(ctdl_home_directory) + 9;
205 initialize_server_extensions();
208 * Now that we've bound the sockets, change to the Citadel user id and its
209 * corresponding group ids
211 if (drop_root_perms) {
212 if ((pw = getpwuid(CTDLUID)) == NULL)
213 lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
214 "Group IDs will be incorrect.\n", (long)CTDLUID,
217 initgroups(pw->pw_name, pw->pw_gid);
218 if (setgid(pw->pw_gid))
219 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
222 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
223 if (setuid(CTDLUID) != 0) {
224 lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
226 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
227 prctl(PR_SET_DUMPABLE, 1);
231 /* We want to check for idle sessions once per minute */
232 CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
235 * Now create a bunch of worker threads.
237 lprintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers-1);
238 begin_critical_section(S_WORKER_LIST);
239 for (i=0; i<(config.c_min_workers-1); ++i) {
242 end_critical_section(S_WORKER_LIST);
244 /* Create the indexer thread. */
245 create_indexer_thread();
247 /* Now this thread can become a worker as well. */
250 /* Server is exiting. Wait for workers to shutdown. */
251 lprintf(CTDL_INFO, "Server is shutting down.\n");