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