* use getpwent_r
[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                 if (running_as_daemon) {
164                         openlog("citadel", LOG_NDELAY, 0);
165                 }
166                 else {
167                         openlog("citadel", LOG_PERROR|LOG_NDELAY, 0);
168                 }
169                 setlogmask(LOG_UPTO(verbosity));
170         }
171         
172         /* Tell 'em who's in da house */
173         lprintf(CTDL_NOTICE, "\n");
174         lprintf(CTDL_NOTICE, "\n");
175         lprintf(CTDL_NOTICE,
176                 "*** Citadel server engine v%d.%02d ***\n",
177                 (REV_LEVEL/100), (REV_LEVEL%100));
178         lprintf(CTDL_NOTICE,
179                 "Copyright (C) 1987-2007 by the Citadel development team.\n");
180         lprintf(CTDL_NOTICE,
181                 "This program is distributed under the terms of the GNU "
182                 "General Public License.\n");
183         lprintf(CTDL_NOTICE, "\n");
184         lprintf(CTDL_DEBUG, "Called as: %s\n", argv[0]);
185
186         /* Load site-specific parameters, and set the ipgm secret */
187         lprintf(CTDL_INFO, "Loading citadel.config\n");
188         get_config();
189         config.c_ipgm_secret = rand();
190         put_config();
191
192         lprintf(CTDL_INFO, "Acquiring control record\n");
193         get_control();
194
195 #ifdef HAVE_RUN_DIR
196         /* on some dists rundir gets purged on startup. so we need to recreate it. */
197
198         if (stat(ctdl_run_dir, &filestats)==-1){
199                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
200                 mkdir(ctdl_run_dir, 0755);
201                 chown(ctdl_run_dir, config.c_ctdluid, (pwp==NULL)?-1:pw.pw_gid);
202         }
203                         
204
205 #endif
206
207         /* Initialize... */
208         init_sysdep();
209
210         /*
211          * Do non system dependent startup functions.
212          */
213         master_startup();
214
215         /*
216          * Bind the server to a Unix-domain socket.
217          */
218         CtdlRegisterServiceHook(0,
219                                                         file_citadel_socket,
220                                                         citproto_begin_session,
221                                                         do_command_loop,
222                                                         do_async_loop);
223
224         /*
225          * Bind the server to our favorite TCP port (usually 504).
226          */
227         CtdlRegisterServiceHook(config.c_port_number,
228                                 NULL,
229                                 citproto_begin_session,
230                                 do_command_loop,
231                                 do_async_loop);
232
233         /*
234          * Load any server-side extensions available here.
235          */
236         lprintf(CTDL_INFO, "Initializing server extensions\n");
237         size = strlen(ctdl_home_directory) + 9;
238         initialize_server_extensions();
239
240         /*
241          * Now that we've bound the sockets, change to the Citadel user id and its
242          * corresponding group ids
243          */
244         if (drop_root_perms) {
245                 getpwuid_r(config.c_ctdluid, &pw, pwbuf, sizeof(pwbuf), &pwp);
246
247                 if (pwp == NULL)
248                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
249                                    "Group IDs will be incorrect.\n", (long)CTDLUID,
250                                 strerror(errno));
251                 else {
252                         initgroups(pw.pw_name, pw.pw_gid);
253                         if (setgid(pw.pw_gid))
254                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
255                                         strerror(errno));
256                 }
257                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)CTDLUID);
258                 if (setuid(CTDLUID) != 0) {
259                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
260                 }
261 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
262                 prctl(PR_SET_DUMPABLE, 1);
263 #endif
264         }
265
266         /* We want to check for idle sessions once per minute */
267         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
268
269         /*
270          * Now create a bunch of worker threads.
271          */
272         lprintf(CTDL_DEBUG, "Starting %d worker threads\n",
273                 config.c_min_workers-1);
274         begin_critical_section(S_WORKER_LIST);
275         for (i=0; i<(config.c_min_workers-1); ++i) {
276                 create_worker();
277         }
278         end_critical_section(S_WORKER_LIST);
279
280         /* Create the indexer thread. */
281         create_maintenance_threads();
282
283         /* This thread is now useless.  It can't be turned into a worker
284          * thread because its stack is too small, but it can't be killed
285          * either because the whole server process would exit.  So we just
286          * join to the first worker thread and exit when it exits.
287          */
288         pthread_join(worker_list->tid, NULL);
289         master_cleanup(0);
290         return(0);
291 }