* New server command-line option "-l" to send log output to the host
[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,
142                 "\n\n*** Citadel/UX messaging server engine v%d.%02d ***\n"
143                 "Copyright (C) 1987-2003 by the Citadel/UX development team.\n"
144                 "This program is distributed under the terms of the GNU "
145                 "General Public License.\n\n",
146                 (REV_LEVEL/100),
147                 (REV_LEVEL%100)
148         );
149
150         /* Initialize... */
151         init_sysdep();
152
153         /* Load site-specific parameters, and set the ipgm secret */
154         lprintf(7, "Loading citadel.config\n");
155         get_config();
156         config.c_ipgm_secret = rand();
157         put_config();
158
159         /*
160          * Do non system dependent startup functions.
161          */
162         master_startup();
163
164         /*
165          * Bind the server to a Unix-domain socket.
166          */
167         CtdlRegisterServiceHook(0,
168                                 "citadel.socket",
169                                 citproto_begin_session,
170                                 do_command_loop);
171
172         /*
173          * Bind the server to our favorite TCP port (usually 504).
174          */
175         CtdlRegisterServiceHook(config.c_port_number,
176                                 NULL,
177                                 citproto_begin_session,
178                                 do_command_loop);
179
180         /*
181          * Load any server-side extensions available here.
182          */
183         lprintf(7, "Initializing server extensions\n");
184         size = strlen(bbs_home_directory) + 9;
185         initialize_server_extensions();
186
187         /*
188          * The rescan pipe exists so that worker threads can be woken up and
189          * told to re-scan the context list for fd's to listen on.  This is
190          * necessary, for example, when a context is about to go idle and needs
191          * to get back on that list.
192          */
193         if (pipe(rescan)) {
194                 lprintf(1, "Can't create rescan pipe!\n");
195                 exit(errno);
196         }
197
198         init_master_fdset();
199
200         /*
201          * Now that we've bound the sockets, change to the BBS user id and its
202          * corresponding group ids
203          */
204         if (drop_root_perms) {
205                 if ((pw = getpwuid(BBSUID)) == NULL)
206                         lprintf(1, "WARNING: getpwuid(%ld): %s\n"
207                                    "Group IDs will be incorrect.\n", (long)BBSUID,
208                                 strerror(errno));
209                 else {
210                         initgroups(pw->pw_name, pw->pw_gid);
211                         if (setgid(pw->pw_gid))
212                                 lprintf(3, "setgid(%ld): %s\n", (long)pw->pw_gid,
213                                         strerror(errno));
214                 }
215                 lprintf(7, "Changing uid to %ld\n", (long)BBSUID);
216                 if (setuid(BBSUID) != 0) {
217                         lprintf(3, "setuid() failed: %s\n", strerror(errno));
218                 }
219         }
220
221         /* We want to check for idle sessions once per minute */
222         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
223
224         /*
225          * Now create a bunch of worker threads.
226          */
227         lprintf(9, "Starting %d worker threads\n", config.c_min_workers-1);
228         begin_critical_section(S_WORKER_LIST);
229         for (i=0; i<(config.c_min_workers-1); ++i) {
230                 create_worker();
231         }
232         end_critical_section(S_WORKER_LIST);
233
234         /* Now this thread can become a worker as well. */
235         initial_thread = pthread_self();
236         worker_thread(NULL);
237
238         /* Server is exiting. Wait for workers to shutdown. */
239         lprintf(7, "Waiting for worker threads to shut down\n");
240
241         begin_critical_section(S_WORKER_LIST);
242         while (worker_list != NULL) {
243                 wnp = worker_list;
244                 worker_list = wnp->next;
245
246                 /* avoid deadlock with an exiting thread */
247                 end_critical_section(S_WORKER_LIST);
248                 if ((i = pthread_join(wnp->tid, NULL)))
249                         lprintf(1, "pthread_join: %s\n", strerror(i));
250                 phree(wnp);
251                 begin_critical_section(S_WORKER_LIST);
252         }
253         end_critical_section(S_WORKER_LIST);
254
255         master_cleanup();
256
257         return(0);
258 }