* Reworked shutdown sequence to avoid thread deadlock
[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         size_t size;
75         
76         /* specify default port name and trace file */
77         strcpy(tracefile, "");
78
79         /* initialize the master context */
80         InitializeMasterCC();
81
82         /* parse command-line arguments */
83         for (a=1; a<argc; ++a) {
84
85                 /* -t specifies where to log trace messages to */
86                 if (!strncmp(argv[a], "-t", 2)) {
87                         safestrncpy(tracefile, argv[a], sizeof tracefile);
88                         strcpy(tracefile, &tracefile[2]);
89                         freopen(tracefile, "r", stdin);
90                         freopen(tracefile, "w", stdout);
91                         freopen(tracefile, "w", stderr);
92                         chmod(tracefile, 0600);
93                 }
94
95                 else if (!strncmp(argv[a], "-l", 2)) {
96                         safestrncpy(tracefile, argv[a], sizeof tracefile);
97                         strcpy(tracefile, &tracefile[2]);
98                         syslog_facility = SyslogFacility(tracefile);
99                         if (syslog_facility >= 0) {
100                                 openlog("citadel", LOG_PID, syslog_facility);
101                         }
102                 }
103
104                 /* run in the background if -d was specified */
105                 else if (!strcmp(argv[a], "-d")) {
106                         start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
107                 }
108
109                 /* -x specifies the desired logging level */
110                 else if (!strncmp(argv[a], "-x", 2)) {
111                         verbosity = atoi(&argv[a][2]);
112                 }
113
114                 else if (!strncmp(argv[a], "-h", 2)) {
115                         safestrncpy(bbs_home_directory, &argv[a][2],
116                                     sizeof bbs_home_directory);
117                         home_specified = 1;
118                 }
119
120                 else if (!strncmp(argv[a], "-f", 2)) {
121                         do_defrag = 1;
122                 }
123
124                 /* -r tells the server not to drop root permissions. don't use
125                  * this unless you know what you're doing. this should be
126                  * removed in the next release if it proves unnecessary. */
127                 else if (!strcmp(argv[a], "-r"))
128                         drop_root_perms = 0;
129
130                 /* any other parameter makes it crash and burn */
131                 else {
132                         lprintf(CTDL_EMERG,     "citserver: usage: "
133                                         "citserver [-tTraceFile] "
134                                         "[-lLogFacility] "
135                                         "[-d] [-f]"
136                                         " [-xLogLevel] [-hHomeDir]\n");
137                         exit(1);
138                 }
139
140         }
141
142         /* Tell 'em who's in da house */
143         lprintf(CTDL_NOTICE, "\n");
144         lprintf(CTDL_NOTICE, "\n");
145         lprintf(CTDL_NOTICE,
146                 "*** Citadel server engine v%d.%02d ***\n",
147                 (REV_LEVEL/100), (REV_LEVEL%100));
148         lprintf(CTDL_NOTICE,
149                 "Copyright (C) 1987-2004 by the Citadel development team.\n");
150         lprintf(CTDL_NOTICE,
151                 "This program is distributed under the terms of the GNU "
152                 "General Public License.\n");
153         lprintf(CTDL_NOTICE, "\n");
154
155         /* Load site-specific parameters, and set the ipgm secret */
156         lprintf(CTDL_INFO, "Loading citadel.config\n");
157         get_config();
158         config.c_ipgm_secret = rand();
159         put_config();
160
161         /* Initialize... */
162         init_sysdep();
163
164         /*
165          * Do non system dependent startup functions.
166          */
167         master_startup();
168
169         /*
170          * Bind the server to a Unix-domain socket.
171          */
172         CtdlRegisterServiceHook(0,
173                                 "citadel.socket",
174                                 citproto_begin_session,
175                                 do_command_loop,
176                                 do_async_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                                 do_async_loop);
186
187         /*
188          * Load any server-side extensions available here.
189          */
190         lprintf(CTDL_INFO, "Initializing server extensions\n");
191         size = strlen(bbs_home_directory) + 9;
192         initialize_server_extensions();
193
194         /*
195          * Now that we've bound the sockets, change to the BBS user id and its
196          * corresponding group ids
197          */
198         if (drop_root_perms) {
199                 if ((pw = getpwuid(BBSUID)) == NULL)
200                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
201                                    "Group IDs will be incorrect.\n", (long)BBSUID,
202                                 strerror(errno));
203                 else {
204                         initgroups(pw->pw_name, pw->pw_gid);
205                         if (setgid(pw->pw_gid))
206                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
207                                         strerror(errno));
208                 }
209                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)BBSUID);
210                 if (setuid(BBSUID) != 0) {
211                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
212                 }
213 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
214                 prctl(PR_SET_DUMPABLE, 1);
215 #endif
216         }
217
218         /* We want to check for idle sessions once per minute */
219         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
220
221         /*
222          * Now create a bunch of worker threads.
223          */
224         lprintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers-1);
225         begin_critical_section(S_WORKER_LIST);
226         for (i=0; i<(config.c_min_workers-1); ++i) {
227                 create_worker();
228         }
229         end_critical_section(S_WORKER_LIST);
230
231         /* Now this thread can become a worker as well. */
232         worker_thread(NULL);
233
234         /* Server is exiting. Wait for workers to shutdown. */
235         lprintf(CTDL_INFO, "Server is shutting down.\n");
236         master_cleanup();
237         return(0);
238 }