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