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