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