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