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