Added a new command line option to citserver "-s" takes a numerical parameter.
[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_SYS_PRCTL_H
42 #include <sys/prctl.h>
43 #endif
44 #include <libcitadel.h>
45 #include "citadel.h"
46 #include "server.h"
47 #include "serv_extensions.h"
48 #include "sysdep_decls.h"
49 #include "threads.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "control.h"
54 #include "database.h"
55 #include "user_ops.h"
56 #include "housekeeping.h"
57 #include "svn_revision.h"
58 #include "citadel_dirs.h"
59
60 #include "context.h"
61
62 #include "modules_init.h"
63 #include "ecrash.h"
64
65 #ifdef HAVE_SYS_SELECT_H
66 #include <sys/select.h>
67 #endif
68
69 #ifndef HAVE_SNPRINTF
70 #include "snprintf.h"
71 #endif
72 const char *CitadelServiceUDS="citadel-UDS";
73 const char *CitadelServiceTCP="citadel-TCP";
74
75
76
77 void go_threading(void);
78
79 /*
80  * Here's where it all begins.
81  */
82 int main(int argc, char **argv)
83 {
84         char facility[32];
85         int a;                  /* General-purpose variables */
86         struct passwd pw, *pwp = NULL;
87         char pwbuf[SIZ];
88         int drop_root_perms = 1;
89         size_t size;
90         int relh=0;
91         int home=0;
92         int dbg=0;
93         char relhome[PATH_MAX]="";
94         char ctdldir[PATH_MAX]=CTDLDIR;
95 #ifdef HAVE_RUN_DIR
96         struct stat filestats;
97 #endif
98 #ifdef HAVE_BACKTRACE
99         eCrashParameters params;
100 //      eCrashSymbolTable symbol_table;
101 #endif
102         /* initialise semaphores here. Patch by Matt and davew
103          * its called here as they are needed by CtdlLogPrintf for thread safety
104          */
105         InitialiseSemaphores();
106         
107         /* initialize the master context */
108         InitializeMasterCC();
109
110         /* parse command-line arguments */
111         for (a=1; a<argc; ++a) {
112
113                 if (!strncmp(argv[a], "-l", 2)) {
114                         safestrncpy(facility, &argv[a][2], sizeof(facility));
115                         syslog_facility = SyslogFacility(facility);
116                         enable_syslog = 1;
117                 }
118
119                 /* run in the background if -d was specified */
120                 else if (!strcmp(argv[a], "-d")) {
121                         running_as_daemon = 1;
122                 }
123
124                 /* run a few stats if -s was specified */
125                 else if (!strncmp(argv[a], "-s", 2)) {
126                         statcount = atoi(&argv[a][2]);
127                 }
128
129                 /* -x specifies the desired logging level */
130                 else if (!strncmp(argv[a], "-x", 2)) {
131                         verbosity = atoi(&argv[a][2]);
132                 }
133
134                 else if (!strncmp(argv[a], "-h", 2)) {
135                         relh=argv[a][2]!='/';
136                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
137                                                                    sizeof ctdl_home_directory);
138                         else
139                                 safestrncpy(relhome, &argv[a][2],
140                                                         sizeof relhome);
141                         home=1;
142                 }
143
144                 else if (!strncmp(argv[a], "-t", 2)) {
145                         freopen(&argv[a][2], "w", stderr);
146                 }
147
148                 else if (!strncmp(argv[a], "-D", 2)) {
149                         dbg = 1;
150                 }
151
152                 /* -r tells the server not to drop root permissions. don't use
153                  * this unless you know what you're doing. this should be
154                  * removed in the next release if it proves unnecessary. */
155                 else if (!strcmp(argv[a], "-r"))
156                         drop_root_perms = 0;
157
158                 /* any other parameter makes it crash and burn */
159                 else {
160                         CtdlLogPrintf(CTDL_EMERG,       "citserver: usage: "
161                                         "citserver "
162                                         "[-lLogFacility] "
163                                         "[-d] [-D] [-s]"
164                                         " [-tTraceFile]"
165                                         " [-xLogLevel] [-hHomeDir]\n");
166                         exit(1);
167                 }
168
169         }
170
171         calc_dirs_n_files(relh, home, relhome, ctdldir, dbg);
172         /* daemonize, if we were asked to */
173         if (running_as_daemon) {
174                 start_daemon(0);
175                 drop_root_perms = 1;
176         }
177
178 #ifdef HAVE_BACKTRACE
179         bzero(&params, sizeof(params));
180         params.filename = file_pid_paniclog;
181         panic_fd=open(file_pid_paniclog, O_APPEND|O_CREAT|O_DIRECT);
182         params.filep = fopen(file_pid_paniclog, "a+");
183         params.debugLevel = ECRASH_DEBUG_VERBOSE;
184         params.dumpAllThreads = TRUE;
185         params.useBacktraceSymbols = 1;
186         params.signals[0]=SIGSEGV;
187         params.signals[1]=SIGILL;
188         params.signals[2]=SIGBUS;
189         params.signals[3]=SIGABRT;
190         eCrash_Init(&params);
191         eCrash_RegisterThread("MasterThread", 0);
192 #endif
193
194         /* Initialize the syslogger.  Yes, we are really using 0 as the
195          * facility, because we are going to bitwise-OR the facility to
196          * the severity of each message, allowing us to write to other
197          * facilities when we need to...
198          */
199         if (enable_syslog) {
200                 openlog("citadel", LOG_NDELAY, 0);
201                 setlogmask(LOG_UPTO(verbosity));
202         }
203         
204         /* Tell 'em who's in da house */
205         CtdlLogPrintf(CTDL_NOTICE, "\n");
206         CtdlLogPrintf(CTDL_NOTICE, "\n");
207         CtdlLogPrintf(CTDL_NOTICE,
208                 "*** Citadel server engine v%d.%02d (build %s) ***\n",
209                 (REV_LEVEL/100), (REV_LEVEL%100), svn_revision());
210         CtdlLogPrintf(CTDL_NOTICE, "Copyright (C) 1987-2008 by the Citadel development team.\n");
211         CtdlLogPrintf(CTDL_NOTICE, "This program is distributed under the terms of the GNU "
212                                         "General Public License.\n");
213         CtdlLogPrintf(CTDL_NOTICE, "\n");
214         CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
215         CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
216
217         /* Load site-specific parameters, and set the ipgm secret */
218         CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
219         get_config();
220         config.c_ipgm_secret = rand();
221
222         /* get_control() MUST MUST MUST be called BEFORE the databases are opened!! */
223         CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
224         get_control();
225
226         put_config();
227
228 #ifdef HAVE_RUN_DIR
229         /* on some dists rundir gets purged on startup. so we need to recreate it. */
230
231         if (stat(ctdl_run_dir, &filestats)==-1){
232 #ifdef HAVE_GETPWUID_R
233 #ifdef SOLARIS_GETPWUID
234                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
235 #else // SOLARIS_GETPWUID
236                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
237 #endif // SOLARIS_GETPWUID
238 #else // HAVE_GETPWUID_R
239                 pwp = NULL;
240 #endif // HAVE_GETPWUID_R
241                 mkdir(ctdl_run_dir, 0755);
242                 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
243         }
244                         
245
246 #endif
247
248         /* Initialize... */
249         init_sysdep();
250
251         /*
252          * Do non system dependent startup functions.
253          */
254         master_startup();
255
256         /*
257          * Check that the control record is correct and place sensible values if it isn't
258          */
259         check_control();
260         
261         /*
262          * Run any upgrade entry points
263          */
264         CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
265         upgrade_modules();
266         
267 /**
268  * Load the user for the masterCC or create them if they don't exist
269  */
270         if (CtdlGetUser(&masterCC.user, "SYS_Citadel"))
271         {
272                 /** User doesn't exist. We can't use create user here as the user number needs to be 0 */
273                 strcpy (masterCC.user.fullname, "SYS_Citadel") ;
274                 CtdlPutUser(&masterCC.user);
275                 CtdlGetUser(&masterCC.user, "SYS_Citadel"); /** Just to be safe */
276         }
277         
278         /*
279          * Bind the server to a Unix-domain socket.
280          */
281         CtdlRegisterServiceHook(0,
282                                 file_citadel_socket,
283                                 citproto_begin_session,
284                                 do_command_loop,
285                                 do_async_loop,
286                                 CitadelServiceUDS);
287
288         /*
289          * Bind the server to our favorite TCP port (usually 504).
290          */
291         CtdlRegisterServiceHook(config.c_port_number,
292                                 NULL,
293                                 citproto_begin_session,
294                                 do_command_loop,
295                                 do_async_loop,
296                                 CitadelServiceTCP);
297
298                                 
299         
300         
301         /*
302          * Load any server-side extensions available here.
303          */
304         CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
305         size = strlen(ctdl_home_directory) + 9;
306         
307         initialise_modules(0);
308         
309         
310
311         /*
312          * If we need host auth, start our chkpwd daemon.
313          */
314         if (config.c_auth_mode == AUTHMODE_HOST) {
315                 start_chkpwd_daemon();
316         }
317
318
319         /*
320          * check, whether we're fired up another time after a crash.
321          * if, post an aide message, so the admin has a chance to react.
322          */
323         checkcrash ();
324
325
326         /*
327          * Now that we've bound the sockets, change to the Citadel user id and its
328          * corresponding group ids
329          */
330         if (drop_root_perms) {
331                 cdb_chmod_data();       /* make sure we own our data files */
332
333 #ifdef HAVE_GETPWUID_R
334 #ifdef SOLARIS_GETPWUID
335                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
336 #else // SOLARIS_GETPWUID
337                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
338 #endif // SOLARIS_GETPWUID
339 #else // HAVE_GETPWUID_R
340                 pwp = NULL;
341 #endif // HAVE_GETPWUID_R
342
343                 if (pwp == NULL)
344                         CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
345                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
346                                 strerror(errno));
347                 else {
348                         initgroups(pw.pw_name, pw.pw_gid);
349                         if (setgid(pw.pw_gid))
350                                 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
351                                         strerror(errno));
352                 }
353                 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
354                 if (setuid(CTDLUID) != 0) {
355                         CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
356                 }
357 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
358                 prctl(PR_SET_DUMPABLE, 1);
359 #endif
360         }
361
362         /* We want to check for idle sessions once per minute */
363         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
364
365         go_threading();
366         
367         
368         master_cleanup(exit_signal);
369         return(0);
370 }