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