* configure.ac: check for <sys/prctl.h>
[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 #ifdef HAVE_PTHREAD_H
41 #include <pthread.h>
42 #endif
43 #ifdef HAVE_SYS_PRCTL_H
44 #include <sys/prctl.h>
45 #endif
46 #include "citadel.h"
47 #include "server.h"
48 #include "serv_extensions.h"
49 #include "sysdep_decls.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "database.h"
54 #include "housekeeping.h"
55 #include "tools.h"
56
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
59 #endif
60
61 #ifndef HAVE_SNPRINTF
62 #include "snprintf.h"
63 #endif
64
65 /*
66  * Here's where it all begins.
67  */
68 int main(int argc, char **argv)
69 {
70         char tracefile[128];            /* Name of file to log traces to */
71         int a, i;                       /* General-purpose variables */
72         struct passwd *pw;
73         int drop_root_perms = 1;
74         struct worker_node *wnp;
75         size_t size;
76         
77         /* specify default port name and trace file */
78         strcpy(tracefile, "");
79
80         /* initialize the master context */
81         InitializeMasterCC();
82
83         /* parse command-line arguments */
84         for (a=1; a<argc; ++a) {
85
86                 /* -t specifies where to log trace messages to */
87                 if (!strncmp(argv[a], "-t", 2)) {
88                         safestrncpy(tracefile, argv[a], sizeof tracefile);
89                         strcpy(tracefile, &tracefile[2]);
90                         freopen(tracefile, "r", stdin);
91                         freopen(tracefile, "w", stdout);
92                         freopen(tracefile, "w", stderr);
93                         chmod(tracefile, 0600);
94                 }
95
96                 else if (!strncmp(argv[a], "-l", 2)) {
97                         safestrncpy(tracefile, argv[a], sizeof tracefile);
98                         strcpy(tracefile, &tracefile[2]);
99                         syslog_facility = SyslogFacility(tracefile);
100                         if (syslog_facility >= 0) {
101                                 openlog("citadel", LOG_PID, syslog_facility);
102                         }
103                 }
104
105                 /* run in the background if -d was specified */
106                 else if (!strcmp(argv[a], "-d")) {
107                         start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
108                 }
109
110                 /* -x specifies the desired logging level */
111                 else if (!strncmp(argv[a], "-x", 2)) {
112                         verbosity = atoi(&argv[a][2]);
113                 }
114
115                 else if (!strncmp(argv[a], "-h", 2)) {
116                         safestrncpy(bbs_home_directory, &argv[a][2],
117                                     sizeof bbs_home_directory);
118                         home_specified = 1;
119                 }
120
121                 else if (!strncmp(argv[a], "-f", 2)) {
122                         do_defrag = 1;
123                 }
124
125                 /* -r tells the server not to drop root permissions. don't use
126                  * this unless you know what you're doing. this should be
127                  * removed in the next release if it proves unnecessary. */
128                 else if (!strcmp(argv[a], "-r"))
129                         drop_root_perms = 0;
130
131                 /* any other parameter makes it crash and burn */
132                 else {
133                         lprintf(CTDL_EMERG,     "citserver: usage: "
134                                         "citserver [-tTraceFile] "
135                                         "[-lLogFacility] "
136                                         "[-d] [-f]"
137                                         " [-xLogLevel] [-hHomeDir]\n");
138                         exit(1);
139                 }
140
141         }
142
143         /* Tell 'em who's in da house */
144         lprintf(CTDL_NOTICE, "\n");
145         lprintf(CTDL_NOTICE, "\n");
146         lprintf(CTDL_NOTICE,
147                 "*** Citadel/UX messaging server engine v%d.%02d ***\n",
148                 (REV_LEVEL/100), (REV_LEVEL%100));
149         lprintf(CTDL_NOTICE,
150                 "Copyright (C) 1987-2003 by the Citadel/UX development team.\n");
151         lprintf(CTDL_NOTICE,
152                 "This program is distributed under the terms of the GNU "
153                 "General Public License.\n");
154         lprintf(CTDL_NOTICE, "\n");
155
156         /* Load site-specific parameters, and set the ipgm secret */
157         lprintf(CTDL_INFO, "Loading citadel.config\n");
158         get_config();
159         config.c_ipgm_secret = rand();
160         put_config();
161
162         /* Initialize... */
163         init_sysdep();
164
165         /*
166          * Do non system dependent startup functions.
167          */
168         master_startup();
169
170         /*
171          * Bind the server to a Unix-domain socket.
172          */
173         CtdlRegisterServiceHook(0,
174                                 "citadel.socket",
175                                 citproto_begin_session,
176                                 do_command_loop);
177
178         /*
179          * Bind the server to our favorite TCP port (usually 504).
180          */
181         CtdlRegisterServiceHook(config.c_port_number,
182                                 NULL,
183                                 citproto_begin_session,
184                                 do_command_loop);
185
186         /*
187          * Load any server-side extensions available here.
188          */
189         lprintf(CTDL_INFO, "Initializing server extensions\n");
190         size = strlen(bbs_home_directory) + 9;
191         initialize_server_extensions();
192
193         /*
194          * The rescan pipe exists so that worker threads can be woken up and
195          * told to re-scan the context list for fd's to listen on.  This is
196          * necessary, for example, when a context is about to go idle and needs
197          * to get back on that list.
198          */
199         if (pipe(rescan)) {
200                 lprintf(CTDL_EMERG, "Can't create rescan pipe!\n");
201                 exit(errno);
202         }
203
204         init_master_fdset();
205
206         /*
207          * Now that we've bound the sockets, change to the BBS user id and its
208          * corresponding group ids
209          */
210         if (drop_root_perms) {
211                 if ((pw = getpwuid(BBSUID)) == NULL)
212                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
213                                    "Group IDs will be incorrect.\n", (long)BBSUID,
214                                 strerror(errno));
215                 else {
216                         initgroups(pw->pw_name, pw->pw_gid);
217                         if (setgid(pw->pw_gid))
218                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
219                                         strerror(errno));
220                 }
221                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)BBSUID);
222                 if (setuid(BBSUID) != 0) {
223                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
224                 }
225 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
226                 prctl(PR_SET_DUMPABLE, 1);
227 #endif
228         }
229
230         /* We want to check for idle sessions once per minute */
231         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
232
233         /*
234          * Now create a bunch of worker threads.
235          */
236         lprintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers-1);
237         begin_critical_section(S_WORKER_LIST);
238         for (i=0; i<(config.c_min_workers-1); ++i) {
239                 create_worker();
240         }
241         end_critical_section(S_WORKER_LIST);
242
243         /* Now this thread can become a worker as well. */
244         initial_thread = pthread_self();
245         worker_thread(NULL);
246
247         /* Server is exiting. Wait for workers to shutdown. */
248         lprintf(CTDL_INFO, "Waiting for worker threads to shut down\n");
249
250         begin_critical_section(S_WORKER_LIST);
251         while (worker_list != NULL) {
252                 wnp = worker_list;
253                 worker_list = wnp->next;
254
255                 /* avoid deadlock with an exiting thread */
256                 end_critical_section(S_WORKER_LIST);
257                 if ((i = pthread_join(wnp->tid, NULL)))
258                         lprintf(CTDL_CRIT, "pthread_join: %s\n", strerror(i));
259                 free(wnp);
260                 begin_critical_section(S_WORKER_LIST);
261         }
262         end_critical_section(S_WORKER_LIST);
263
264         master_cleanup();
265         return(0);
266 }