New versions of Berkeley DB (I tested with 4.5.20) seem to
[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 #include <pwd.h>
41 #ifdef HAVE_PTHREAD_H
42 #include <pthread.h>
43 #endif
44 #ifdef HAVE_SYS_PRCTL_H
45 #include <sys/prctl.h>
46 #endif
47 #include "citadel.h"
48 #include "server.h"
49 #include "serv_extensions.h"
50 #include "sysdep_decls.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "control.h"
55 #include "database.h"
56 #include "housekeeping.h"
57 #include "tools.h"
58 #include "citadel_dirs.c"
59
60 #ifdef HAVE_SYS_SELECT_H
61 #include <sys/select.h>
62 #endif
63
64 #ifndef HAVE_SNPRINTF
65 #include "snprintf.h"
66 #endif
67
68 int running_as_daemon = 0;
69
70 /*
71  * Here's where it all begins.
72  */
73 int main(int argc, char **argv)
74 {
75         char facility[32];
76         int a, i;                       /* General-purpose variables */
77         struct passwd pw, *pwp = NULL;
78         char pwbuf[SIZ];
79         int drop_root_perms = 1;
80         size_t size;
81         int relh=0;
82         int home=0;
83         char relhome[PATH_MAX]="";
84         char ctdldir[PATH_MAX]=CTDLDIR;
85 #ifdef HAVE_RUN_DIR
86         struct stat filestats;
87 #endif
88         
89         /* initialize the master context */
90         InitializeMasterCC();
91
92         /* parse command-line arguments */
93         for (a=1; a<argc; ++a) {
94
95                 if (!strncmp(argv[a], "-l", 2)) {
96                         safestrncpy(facility, &argv[a][2], sizeof(facility));
97                         syslog_facility = SyslogFacility(facility);
98                         enable_syslog = 1;
99                 }
100
101                 /* run in the background if -d was specified */
102                 else if (!strcmp(argv[a], "-d")) {
103                         running_as_daemon = 1;
104                 }
105
106                 /* -x specifies the desired logging level */
107                 else if (!strncmp(argv[a], "-x", 2)) {
108                         verbosity = atoi(&argv[a][2]);
109                 }
110
111                 else if (!strncmp(argv[a], "-h", 2)) {
112                         relh=argv[a][2]!='/';
113                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
114                                                                    sizeof ctdl_home_directory);
115                         else
116                                 safestrncpy(relhome, &argv[a][2],
117                                                         sizeof relhome);
118                         home_specified = 1;
119                         home=1;
120                 }
121
122                 else if (!strncmp(argv[a], "-t", 2)) {
123                         freopen(&argv[a][2], "w", stderr);
124                 }
125
126                 else if (!strncmp(argv[a], "-f", 2)) {
127                         do_defrag = 1;
128                 }
129
130                 /* -r tells the server not to drop root permissions. don't use
131                  * this unless you know what you're doing. this should be
132                  * removed in the next release if it proves unnecessary. */
133                 else if (!strcmp(argv[a], "-r"))
134                         drop_root_perms = 0;
135
136                 /* any other parameter makes it crash and burn */
137                 else {
138                         lprintf(CTDL_EMERG,     "citserver: usage: "
139                                         "citserver "
140                                         "[-lLogFacility] "
141                                         "[-d] [-f]"
142                                         " [-tTraceFile]"
143                                         " [-xLogLevel] [-hHomeDir]\n");
144                         exit(1);
145                 }
146
147         }
148
149         calc_dirs_n_files(relh, home, relhome, ctdldir);
150
151         /* daemonize, if we were asked to */
152         if (running_as_daemon) {
153                 start_daemon(0);
154                 drop_root_perms = 1;
155         }
156
157         /* Initialize the syslogger.  Yes, we are really using 0 as the
158          * facility, because we are going to bitwise-OR the facility to
159          * the severity of each message, allowing us to write to other
160          * facilities when we need to...
161          */
162         if (enable_syslog) {
163                 openlog("citadel", LOG_NDELAY, 0);
164                 setlogmask(LOG_UPTO(verbosity));
165         }
166         
167         /* Tell 'em who's in da house */
168         lprintf(CTDL_NOTICE, "\n");
169         lprintf(CTDL_NOTICE, "\n");
170         lprintf(CTDL_NOTICE,
171                 "*** Citadel server engine v%d.%02d ***\n",
172                 (REV_LEVEL/100), (REV_LEVEL%100));
173         lprintf(CTDL_NOTICE,
174                 "Copyright (C) 1987-2007 by the Citadel development team.\n");
175         lprintf(CTDL_NOTICE,
176                 "This program is distributed under the terms of the GNU "
177                 "General Public License.\n");
178         lprintf(CTDL_NOTICE, "\n");
179         lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
180
181         /* Load site-specific parameters, and set the ipgm secret */
182         lprintf(CTDL_INFO, "Loading citadel.config\n");
183         get_config();
184         config.c_ipgm_secret = rand();
185         put_config();
186
187         lprintf(CTDL_INFO, "Acquiring control record\n");
188         get_control();
189
190 #ifdef HAVE_RUN_DIR
191         /* on some dists rundir gets purged on startup. so we need to recreate it. */
192
193         if (stat(ctdl_run_dir, &filestats)==-1){
194 #ifdef SOLARIS_GETPWUID
195                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
196 #else
197                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
198 #endif
199                 mkdir(ctdl_run_dir, 0755);
200                 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
201         }
202                         
203
204 #endif
205
206         /* Initialize... */
207         init_sysdep();
208
209         /*
210          * Do non system dependent startup functions.
211          */
212         master_startup();
213
214         /*
215          * Bind the server to a Unix-domain socket.
216          */
217         CtdlRegisterServiceHook(0,
218                                                         file_citadel_socket,
219                                                         citproto_begin_session,
220                                                         do_command_loop,
221                                                         do_async_loop);
222
223         /*
224          * Bind the server to our favorite TCP port (usually 504).
225          */
226         CtdlRegisterServiceHook(config.c_port_number,
227                                 NULL,
228                                 citproto_begin_session,
229                                 do_command_loop,
230                                 do_async_loop);
231
232         /*
233          * Load any server-side extensions available here.
234          */
235         lprintf(CTDL_INFO, "Initializing server extensions\n");
236         size = strlen(ctdl_home_directory) + 9;
237         initialize_server_extensions();
238
239         /*
240          * Now that we've bound the sockets, change to the Citadel user id and its
241          * corresponding group ids
242          */
243         if (drop_root_perms) {
244                 cdb_chmod_data();       /* make sure we own our data files */
245
246 #ifdef SOLARIS_GETPWUID
247                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
248 #else
249                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
250 #endif
251                 if (pwp == NULL)
252                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
253                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
254                                 strerror(errno));
255                 else {
256                         initgroups(pw.pw_name, pw.pw_gid);
257                         if (setgid(pw.pw_gid))
258                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
259                                         strerror(errno));
260                 }
261                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
262                 if (setuid(CTDLUID) != 0) {
263                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
264                 }
265 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
266                 prctl(PR_SET_DUMPABLE, 1);
267 #endif
268         }
269
270         /* We want to check for idle sessions once per minute */
271         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
272
273         /*
274          * Now create a bunch of worker threads.
275          */
276         lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
277                 config.c_min_workers-1);
278         begin_critical_section(S_WORKER_LIST);
279         for (i=0; i<(config.c_min_workers-1); ++i) {
280                 create_worker();
281         }
282         end_critical_section(S_WORKER_LIST);
283
284         /* Create the indexer thread. */
285         create_maintenance_threads();
286
287         /* This thread is now useless.  It can't be turned into a worker
288          * thread because its stack is too small, but it can't be killed
289          * either because the whole server process would exit.  So we just
290          * join to the first worker thread and exit when it exits.
291          */
292         pthread_join(worker_list->tid, NULL);
293         master_cleanup(0);
294         return(0);
295 }