* syslog messages are now sent to the desired facility rather than always
[citadel.git] / citadel / server_main.c
1 /*
2  * citserver's main() function lives here.
3  *
4  * $Id$
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <ctype.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <syslog.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <limits.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #include <sys/un.h>
35 #include <string.h>
36 #include <pwd.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <grp.h>
40 #ifdef HAVE_PTHREAD_H
41 #include <pthread.h>
42 #endif
43 #ifdef HAVE_SYS_PRCTL_H
44 #include <sys/prctl.h>
45 #endif
46 #include "citadel.h"
47 #include "server.h"
48 #include "serv_extensions.h"
49 #include "sysdep_decls.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "database.h"
54 #include "housekeeping.h"
55 #include "tools.h"
56
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
59 #endif
60
61 #ifndef HAVE_SNPRINTF
62 #include "snprintf.h"
63 #endif
64
65 int running_as_daemon = 0;
66
67 /*
68  * Here's where it all begins.
69  */
70 int main(int argc, char **argv)
71 {
72         char facility[32];
73         int a, i;                       /* General-purpose variables */
74         struct passwd *pw;
75         int drop_root_perms = 1;
76         size_t size;
77
78         /* initialize the master context */
79         InitializeMasterCC();
80
81         /* parse command-line arguments */
82         for (a=1; a<argc; ++a) {
83
84                 if (!strncmp(argv[a], "-l", 2)) {
85                         safestrncpy(facility, &argv[a][2], sizeof(facility));
86                         syslog_facility = SyslogFacility(facility);
87                         enable_syslog = 1;
88                 }
89
90                 /* run in the background if -d was specified */
91                 else if (!strcmp(argv[a], "-d")) {
92                         running_as_daemon = 1;
93                 }
94
95                 /* -x specifies the desired logging level */
96                 else if (!strncmp(argv[a], "-x", 2)) {
97                         verbosity = atoi(&argv[a][2]);
98                 }
99
100                 else if (!strncmp(argv[a], "-h", 2)) {
101                         safestrncpy(ctdl_home_directory, &argv[a][2],
102                                     sizeof ctdl_home_directory);
103                         home_specified = 1;
104                 }
105
106                 else if (!strncmp(argv[a], "-t", 2)) {
107                         freopen(&argv[a][2], "w", stderr);
108                 }
109
110                 else if (!strncmp(argv[a], "-f", 2)) {
111                         do_defrag = 1;
112                 }
113
114                 /* -r tells the server not to drop root permissions. don't use
115                  * this unless you know what you're doing. this should be
116                  * removed in the next release if it proves unnecessary. */
117                 else if (!strcmp(argv[a], "-r"))
118                         drop_root_perms = 0;
119
120                 /* any other parameter makes it crash and burn */
121                 else {
122                         lprintf(CTDL_EMERG,     "citserver: usage: "
123                                         "citserver "
124                                         "[-lLogFacility] "
125                                         "[-d] [-f]"
126                                         " [-tTraceFile]"
127                                         " [-xLogLevel] [-hHomeDir]\n");
128                         exit(1);
129                 }
130
131         }
132
133         /* daemonize, if we were asked to */
134         if (running_as_daemon) {
135                 start_daemon(0);
136                 drop_root_perms = 1;
137         }
138
139         /* initialize the syslog facility */
140         if (enable_syslog) {
141                 if (running_as_daemon) {
142                         openlog("citadel", LOG_NDELAY, syslog_facility);
143                 }
144                 else {
145                         openlog("citadel", LOG_PERROR|LOG_NDELAY,
146                                 syslog_facility);
147                 }
148                 setlogmask(LOG_UPTO(verbosity));
149         }
150         
151         /* Tell 'em who's in da house */
152         lprintf(CTDL_NOTICE, "\n");
153         lprintf(CTDL_NOTICE, "\n");
154         lprintf(CTDL_NOTICE,
155                 "*** Citadel server engine v%d.%02d ***\n",
156                 (REV_LEVEL/100), (REV_LEVEL%100));
157         lprintf(CTDL_NOTICE,
158                 "Copyright (C) 1987-2005 by the Citadel development team.\n");
159         lprintf(CTDL_NOTICE,
160                 "This program is distributed under the terms of the GNU "
161                 "General Public License.\n");
162         lprintf(CTDL_NOTICE, "\n");
163         lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
164
165         /* Load site-specific parameters, and set the ipgm secret */
166         lprintf(CTDL_INFO, "Loading citadel.config\n");
167         get_config();
168         config.c_ipgm_secret = rand();
169         put_config();
170
171         /* Initialize... */
172         init_sysdep();
173
174         /*
175          * Do non system dependent startup functions.
176          */
177         master_startup();
178
179         /*
180          * Bind the server to a Unix-domain socket.
181          */
182         CtdlRegisterServiceHook(0,
183 #ifndef HAVE_RUN_DIR
184                                          "."
185 #else
186                                          RUN_DIR
187 #endif
188                                 "/citadel.socket",
189                                 citproto_begin_session,
190                                 do_command_loop,
191                                 do_async_loop);
192
193         /*
194          * Bind the server to our favorite TCP port (usually 504).
195          */
196         CtdlRegisterServiceHook(config.c_port_number,
197                                 NULL,
198                                 citproto_begin_session,
199                                 do_command_loop,
200                                 do_async_loop);
201
202         /*
203          * Load any server-side extensions available here.
204          */
205         lprintf(CTDL_INFO, "Initializing server extensions\n");
206         size = strlen(ctdl_home_directory) + 9;
207         initialize_server_extensions();
208
209         /*
210          * Now that we've bound the sockets, change to the Citadel user id and its
211          * corresponding group ids
212          */
213         if (drop_root_perms) {
214                 if ((pw = getpwuid(CTDLUID)) == NULL)
215                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
216                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
217                                 strerror(errno));
218                 else {
219                         initgroups(pw->pw_name, pw->pw_gid);
220                         if (setgid(pw->pw_gid))
221                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
222                                         strerror(errno));
223                 }
224                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
225                 if (setuid(CTDLUID) != 0) {
226                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
227                 }
228 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
229                 prctl(PR_SET_DUMPABLE, 1);
230 #endif
231         }
232
233         /* We want to check for idle sessions once per minute */
234         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
235
236         /*
237          * Now create a bunch of worker threads.
238          */
239         lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
240                 config.c_min_workers-1);
241         begin_critical_section(S_WORKER_LIST);
242         for (i=0; i<(config.c_min_workers-1); ++i) {
243                 create_worker();
244         }
245         end_critical_section(S_WORKER_LIST);
246
247         /* Create the indexer thread. */
248         create_maintenance_threads();
249
250         /* This thread is now useless.  It can't be turned into a worker
251          * thread because its stack is too small, but it can't be killed
252          * either because the whole server process would exit.  So we just
253          * join to the first worker thread and exit when it exits.
254          */
255         pthread_join(worker_list->tid, NULL);
256         master_cleanup(0);
257         return(0);
258 }