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