Added an entry point to the modules init stuff.
[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 <libcitadel.h>
48 #include "citadel.h"
49 #include "server.h"
50 #include "serv_extensions.h"
51 #include "sysdep_decls.h"
52 #include "citserver.h"
53 #include "support.h"
54 #include "config.h"
55 #include "control.h"
56 #include "database.h"
57 #include "user_ops.h"
58 #include "housekeeping.h"
59 #include "citadel_dirs.c"
60
61 #include "modules_init.h"
62 #include "ecrash.h"
63
64 #ifdef HAVE_SYS_SELECT_H
65 #include <sys/select.h>
66 #endif
67
68 #ifndef HAVE_SNPRINTF
69 #include "snprintf.h"
70 #endif
71 const char *CitadelServiceUDS="citadel-UDS";
72 const char *CitadelServiceTCP="citadel-TCP";
73
74
75 void go_threading(void);
76
77
78 /*
79  * Here's where it all begins.
80  */
81 int main(int argc, char **argv)
82 {
83         char facility[32];
84         int a;                  /* General-purpose variables */
85         struct passwd pw, *pwp = NULL;
86         char pwbuf[SIZ];
87         int drop_root_perms = 1;
88         size_t size;
89         int relh=0;
90         int home=0;
91         int dbg=0;
92         char relhome[PATH_MAX]="";
93         char ctdldir[PATH_MAX]=CTDLDIR;
94 #ifdef HAVE_RUN_DIR
95         struct stat filestats;
96 #endif
97 #ifdef HAVE_BACKTRACE
98         eCrashParameters params;
99 //      eCrashSymbolTable symbol_table;
100 #endif
101         /* initialise semaphores here. Patch by Matt and davew
102          * its called here as they are needed by CtdlLogPrintf for thread safety
103          */
104         CtdlInitBase64Table();
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                 /* -x specifies the desired logging level */
125                 else if (!strncmp(argv[a], "-x", 2)) {
126                         verbosity = atoi(&argv[a][2]);
127                 }
128
129                 else if (!strncmp(argv[a], "-h", 2)) {
130                         relh=argv[a][2]!='/';
131                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
132                                                                    sizeof ctdl_home_directory);
133                         else
134                                 safestrncpy(relhome, &argv[a][2],
135                                                         sizeof relhome);
136                         home_specified = 1;
137                         home=1;
138                 }
139
140                 else if (!strncmp(argv[a], "-t", 2)) {
141                         freopen(&argv[a][2], "w", stderr);
142                 }
143
144                 else if (!strncmp(argv[a], "-f", 2)) {
145                         do_defrag = 1;
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] [-f] [-D] "
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 ///     BuildSymbolTable(&symbol_table);
187 //      params.symbolTable = &symbol_table;
188         params.signals[0]=SIGSEGV;
189         params.signals[1]=SIGILL;
190         params.signals[2]=SIGBUS;
191         params.signals[3]=SIGABRT;
192
193         eCrash_Init(&params);
194                 
195         eCrash_RegisterThread("MasterThread", 0);
196
197 ///     signal(SIGSEGV, cit_panic_backtrace);
198 #endif
199         /* Initialize the syslogger.  Yes, we are really using 0 as the
200          * facility, because we are going to bitwise-OR the facility to
201          * the severity of each message, allowing us to write to other
202          * facilities when we need to...
203          */
204         if (enable_syslog) {
205                 openlog("citadel", LOG_NDELAY, 0);
206                 setlogmask(LOG_UPTO(verbosity));
207         }
208         
209         /* Tell 'em who's in da house */
210         CtdlLogPrintf(CTDL_NOTICE, "\n");
211         CtdlLogPrintf(CTDL_NOTICE, "\n");
212         CtdlLogPrintf(CTDL_NOTICE,
213                 "*** Citadel server engine v%d.%02d ***\n",
214                 (REV_LEVEL/100), (REV_LEVEL%100));
215         CtdlLogPrintf(CTDL_NOTICE,
216                 "Copyright (C) 1987-2007 by the Citadel development team.\n");
217         CtdlLogPrintf(CTDL_NOTICE,
218                 "This program is distributed under the terms of the GNU "
219                 "General Public License.\n");
220         CtdlLogPrintf(CTDL_NOTICE, "\n");
221         CtdlLogPrintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
222         CtdlLogPrintf(CTDL_INFO, "%s\n", libcitadel_version_string());
223
224         /* Load site-specific parameters, and set the ipgm secret */
225         CtdlLogPrintf(CTDL_INFO, "Loading citadel.config\n");
226         get_config();
227         config.c_ipgm_secret = rand();
228         put_config();
229
230 #ifdef HAVE_RUN_DIR
231         /* on some dists rundir gets purged on startup. so we need to recreate it. */
232
233         if (stat(ctdl_run_dir, &filestats)==-1){
234 #ifdef SOLARIS_GETPWUID
235                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
236 #else
237                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
238 #endif
239                 mkdir(ctdl_run_dir, 0755);
240                 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
241         }
242                         
243
244 #endif
245
246         /* Initialize... */
247         init_sysdep();
248
249         /*
250          * Do non system dependent startup functions.
251          */
252         master_startup();
253
254         CtdlLogPrintf(CTDL_INFO, "Acquiring control record\n");
255         get_control();
256
257         /*
258          * Bind the server to a Unix-domain socket.
259          */
260         CtdlRegisterServiceHook(0,
261                                 file_citadel_socket,
262                                 citproto_begin_session,
263                                 do_command_loop,
264                                 do_async_loop,
265                                 CitadelServiceUDS);
266
267         /*
268          * Bind the server to our favorite TCP port (usually 504).
269          */
270         CtdlRegisterServiceHook(config.c_port_number,
271                                 NULL,
272                                 citproto_begin_session,
273                                 do_command_loop,
274                                 do_async_loop,
275                                 CitadelServiceTCP);
276
277                                 
278         /*
279          * Run any upgrade entry points
280          */
281         CtdlLogPrintf(CTDL_INFO, "Upgrading modules.\n");
282         upgrade_modules();
283         
284         
285         /*
286          * Load any server-side extensions available here.
287          */
288         CtdlLogPrintf(CTDL_INFO, "Initializing server extensions\n");
289         size = strlen(ctdl_home_directory) + 9;
290         
291         initialise_modules(0);
292         
293         
294
295         /*
296          * If we need host auth, start our chkpwd daemon.
297          */
298         if (config.c_auth_mode == 1) {
299                 start_chkpwd_daemon();
300         }
301
302         /*
303          * Now that we've bound the sockets, change to the Citadel user id and its
304          * corresponding group ids
305          */
306         if (drop_root_perms) {
307                 cdb_chmod_data();       /* make sure we own our data files */
308
309 #ifdef SOLARIS_GETPWUID
310                 pwp = getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf));
311 #else
312                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
313 #endif
314                 if (pwp == NULL)
315                         CtdlLogPrintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
316                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
317                                 strerror(errno));
318                 else {
319                         initgroups(pw.pw_name, pw.pw_gid);
320                         if (setgid(pw.pw_gid))
321                                 CtdlLogPrintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
322                                         strerror(errno));
323                 }
324                 CtdlLogPrintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
325                 if (setuid(CTDLUID) != 0) {
326                         CtdlLogPrintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
327                 }
328 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
329                 prctl(PR_SET_DUMPABLE, 1);
330 #endif
331         }
332
333         /* We want to check for idle sessions once per minute */
334         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
335
336         go_threading();
337         
338         
339         master_cleanup(exit_signal);
340         return(0);
341 }
342
343
344
345 void go_threading(void)
346 {
347         int i;
348         struct CtdlThreadNode *last_worker;
349         
350         /* We can't use CT_PUSH() here so we do it the long way 
351          * So we can still use CT for current thread */
352         struct CtdlThreadNode *_this_cit_thread;
353         
354         /*
355          * Initialise the thread system
356          */
357         ctdl_thread_internal_init();
358         _this_cit_thread = CtdlThreadSelf();
359         /*
360          * Now create a bunch of worker threads.
361          */
362         CtdlLogPrintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers);
363         begin_critical_section(S_THREAD_LIST);
364         i=0;    /* Always start at least 1 worker thread */
365         do
366         {
367                 ctdl_internal_create_thread("Worker Thread", CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER, worker_thread, NULL);
368         } while (++i < config.c_min_workers);
369         end_critical_section(S_THREAD_LIST);
370
371         /* Second call to module init functions now that threading is up */
372         initialise_modules(1);
373
374         /*
375          * This thread is now used for garbage collection of other threads in the thread list
376          */
377         CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", pthread_self());
378
379         /*
380          * We do a lot of locking and unlocking of the thread list in here.
381          * We do this so that we can repeatedly release time for other threads
382          * that may be waiting on the thread list.
383          * We are a low priority thread so we can afford to do this
384          */
385         
386         while (CtdlThreadGetCount())
387         {
388                 if (exit_signal)
389                         CtdlThreadStopAll();
390                 check_sched_shutdown();
391                 begin_critical_section(S_THREAD_LIST);
392                 ctdl_thread_internal_calc_loadavg();
393                 end_critical_section(S_THREAD_LIST);
394         
395                 /* Reduce the size of the worker thread pool if necessary. */
396                 if ((CtdlThreadGetWorkers() > config.c_min_workers) && (CtdlThreadWorkerAvg < 20))
397                 {
398                         /* Ask a worker thread to stop as we no longer need it */
399                         begin_critical_section(S_THREAD_LIST);
400                         last_worker = CtdlThreadList;
401                         while (last_worker)
402                         {
403                                 pthread_mutex_lock(&last_worker->ThreadMutex);
404                                 if (last_worker->flags & CTDLTHREAD_WORKER && last_worker->state > CTDL_THREAD_STOPPING)
405                                 {
406                                         pthread_mutex_unlock(&last_worker->ThreadMutex);
407                                         break;
408                                 }
409                                 pthread_mutex_unlock(&last_worker->ThreadMutex);
410                                 last_worker = last_worker->next;
411                         }
412                         end_critical_section(S_THREAD_LIST);
413                         if (last_worker)
414                         {
415 #ifdef WITH_THREADLOG
416                                 CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (%ld).\n",
417                                         last_worker->name,
418                                         last_worker->tid
419                                         );
420 #endif
421                                 CtdlThreadStop(last_worker);
422                         }
423                 }
424         
425                 /*
426                  * If all our workers are working hard, start some more to help out
427                  * with things
428                  */
429                 /* FIXME: come up with a better way to dynamically alter the number of threads
430                  * based on the system load
431                  */
432 //              if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() < num_sessions))
433                 // && (CtdlThreadLoadAvg < 90) )
434                 if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) )
435                 {
436                         for (i=0; i<5 ; i++)
437 //                      for (i=0; i< (num_sessions - CtdlThreadGetWorkers()) ; i++)
438 //                      for (i=0; i< (10 - (55 - CtdlThreadWorkerAvg) / CtdlThreadWorkerAvg / CtdlThreadGetWorkers()) ; i++)
439                         {
440 //                              begin_critical_section(S_THREAD_LIST);
441                                 CtdlThreadCreate("Worker Thread",
442                                         CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
443                                         worker_thread,
444                                         NULL
445                                         );
446 //                              end_critical_section(S_THREAD_LIST);
447                         }
448                 }
449                 
450                 CtdlThreadGC();
451                 
452                 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
453                 {
454                         CtdlThreadGC();
455                 }
456                 
457                 if (CtdlThreadGetCount())
458                         CtdlThreadSleep(1);
459         }
460         /*
461          * If the above loop exits we must be shutting down since we obviously have no threads
462          */
463         ctdl_thread_internal_cleanup();
464 }