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