Updated copyright notice to 2007
[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;
78         int drop_root_perms = 1;
79         size_t size;
80         int relh=0;
81         int home=0;
82         char relhome[PATH_MAX]="";
83         char ctdldir[PATH_MAX]=CTDLDIR;
84 #ifdef HAVE_RUN_DIR
85         struct stat filestats;
86 #endif
87         
88         /* initialize the master context */
89         InitializeMasterCC();
90
91         /* parse command-line arguments */
92         for (a=1; a<argc; ++a) {
93
94                 if (!strncmp(argv[a], "-l", 2)) {
95                         safestrncpy(facility, &argv[a][2], sizeof(facility));
96                         syslog_facility = SyslogFacility(facility);
97                         enable_syslog = 1;
98                 }
99
100                 /* run in the background if -d was specified */
101                 else if (!strcmp(argv[a], "-d")) {
102                         running_as_daemon = 1;
103                 }
104
105                 /* -x specifies the desired logging level */
106                 else if (!strncmp(argv[a], "-x", 2)) {
107                         verbosity = atoi(&argv[a][2]);
108                 }
109
110                 else if (!strncmp(argv[a], "-h", 2)) {
111                         relh=argv[a][2]!='/';
112                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
113                                                                    sizeof ctdl_home_directory);
114                         else
115                                 safestrncpy(relhome, &argv[a][2],
116                                                         sizeof relhome);
117                         home_specified = 1;
118                         home=1;
119                 }
120
121                 else if (!strncmp(argv[a], "-t", 2)) {
122                         freopen(&argv[a][2], "w", stderr);
123                 }
124
125                 else if (!strncmp(argv[a], "-f", 2)) {
126                         do_defrag = 1;
127                 }
128
129                 /* -r tells the server not to drop root permissions. don't use
130                  * this unless you know what you're doing. this should be
131                  * removed in the next release if it proves unnecessary. */
132                 else if (!strcmp(argv[a], "-r"))
133                         drop_root_perms = 0;
134
135                 /* any other parameter makes it crash and burn */
136                 else {
137                         lprintf(CTDL_EMERG,     "citserver: usage: "
138                                         "citserver "
139                                         "[-lLogFacility] "
140                                         "[-d] [-f]"
141                                         " [-tTraceFile]"
142                                         " [-xLogLevel] [-hHomeDir]\n");
143                         exit(1);
144                 }
145
146         }
147
148         calc_dirs_n_files(relh, home, relhome, ctdldir);
149
150         /* daemonize, if we were asked to */
151         if (running_as_daemon) {
152                 start_daemon(0);
153                 drop_root_perms = 1;
154         }
155
156         /* Initialize the syslogger.  Yes, we are really using 0 as the
157          * facility, because we are going to bitwise-OR the facility to
158          * the severity of each message, allowing us to write to other
159          * facilities when we need to...
160          */
161         if (enable_syslog) {
162                 if (running_as_daemon) {
163                         openlog("citadel", LOG_NDELAY, 0);
164                 }
165                 else {
166                         openlog("citadel", LOG_PERROR|LOG_NDELAY, 0);
167                 }
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                 pw=getpwuid(config.c_ctdluid);
199                 mkdir(ctdl_run_dir, 0755);
200                 chown(ctdl_run_dir, config.c_ctdluid, (pw==NULL)?-1:pw->pw_gid);
201
202         }
203                         
204
205 #endif
206
207         /* Initialize... */
208         init_sysdep();
209
210         /*
211          * Do non system dependent startup functions.
212          */
213         master_startup();
214
215         /*
216          * Bind the server to a Unix-domain socket.
217          */
218         CtdlRegisterServiceHook(0,
219                                                         file_citadel_socket,
220                                                         citproto_begin_session,
221                                                         do_command_loop,
222                                                         do_async_loop);
223
224         /*
225          * Bind the server to our favorite TCP port (usually 504).
226          */
227         CtdlRegisterServiceHook(config.c_port_number,
228                                 NULL,
229                                 citproto_begin_session,
230                                 do_command_loop,
231                                 do_async_loop);
232
233         /*
234          * Load any server-side extensions available here.
235          */
236         lprintf(CTDL_INFO, "Initializing server extensions\n");
237         size = strlen(ctdl_home_directory) + 9;
238         initialize_server_extensions();
239
240         /*
241          * Now that we've bound the sockets, change to the Citadel user id and its
242          * corresponding group ids
243          */
244         if (drop_root_perms) {
245                 if ((pw = getpwuid(CTDLUID)) == NULL)
246                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
247                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
248                                 strerror(errno));
249                 else {
250                         initgroups(pw->pw_name, pw->pw_gid);
251                         if (setgid(pw->pw_gid))
252                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
253                                         strerror(errno));
254                 }
255                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
256                 if (setuid(CTDLUID) != 0) {
257                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
258                 }
259 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
260                 prctl(PR_SET_DUMPABLE, 1);
261 #endif
262         }
263
264         /* We want to check for idle sessions once per minute */
265         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
266
267         /*
268          * Now create a bunch of worker threads.
269          */
270         lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
271                 config.c_min_workers-1);
272         begin_critical_section(S_WORKER_LIST);
273         for (i=0; i<(config.c_min_workers-1); ++i) {
274                 create_worker();
275         }
276         end_critical_section(S_WORKER_LIST);
277
278         /* Create the indexer thread. */
279         create_maintenance_threads();
280
281         /* This thread is now useless.  It can't be turned into a worker
282          * thread because its stack is too small, but it can't be killed
283          * either because the whole server process would exit.  So we just
284          * join to the first worker thread and exit when it exits.
285          */
286         pthread_join(worker_list->tid, NULL);
287         master_cleanup(0);
288         return(0);
289 }