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