* Replaced all "Citadel/UX" references with "Citadel"
[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 server engine v%d.%02d ***\n",
148                 (REV_LEVEL/100), (REV_LEVEL%100));
149         lprintf(CTDL_NOTICE,
150                 "Copyright (C) 1987-2004 by the Citadel 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                                 do_async_loop);
178
179         /*
180          * Bind the server to our favorite TCP port (usually 504).
181          */
182         CtdlRegisterServiceHook(config.c_port_number,
183                                 NULL,
184                                 citproto_begin_session,
185                                 do_command_loop,
186                                 do_async_loop);
187
188         /*
189          * Load any server-side extensions available here.
190          */
191         lprintf(CTDL_INFO, "Initializing server extensions\n");
192         size = strlen(bbs_home_directory) + 9;
193         initialize_server_extensions();
194
195         /*
196          * Now that we've bound the sockets, change to the BBS user id and its
197          * corresponding group ids
198          */
199         if (drop_root_perms) {
200                 if ((pw = getpwuid(BBSUID)) == NULL)
201                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
202                                    "Group IDs will be incorrect.\n", (long)BBSUID,
203                                 strerror(errno));
204                 else {
205                         initgroups(pw->pw_name, pw->pw_gid);
206                         if (setgid(pw->pw_gid))
207                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw->pw_gid,
208                                         strerror(errno));
209                 }
210                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)BBSUID);
211                 if (setuid(BBSUID) != 0) {
212                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
213                 }
214 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
215                 prctl(PR_SET_DUMPABLE, 1);
216 #endif
217         }
218
219         /* We want to check for idle sessions once per minute */
220         CtdlRegisterSessionHook(terminate_idle_sessions, EVT_TIMER);
221
222         /*
223          * Now create a bunch of worker threads.
224          */
225         lprintf(CTDL_DEBUG, "Starting %d worker threads\n", config.c_min_workers-1);
226         begin_critical_section(S_WORKER_LIST);
227         for (i=0; i<(config.c_min_workers-1); ++i) {
228                 create_worker();
229         }
230         end_critical_section(S_WORKER_LIST);
231
232         /* Now this thread can become a worker as well. */
233         initial_thread = pthread_self();
234         worker_thread(NULL);
235
236         /* Server is exiting. Wait for workers to shutdown. */
237         lprintf(CTDL_INFO, "Waiting for worker threads to shut down\n");
238
239         begin_critical_section(S_WORKER_LIST);
240         while (worker_list != NULL) {
241                 wnp = worker_list;
242                 worker_list = wnp->next;
243
244                 /* avoid deadlock with an exiting thread */
245                 end_critical_section(S_WORKER_LIST);
246                 if ((i = pthread_join(wnp->tid, NULL)))
247                         lprintf(CTDL_CRIT, "pthread_join: %s\n", strerror(i));
248                 free(wnp);
249                 begin_critical_section(S_WORKER_LIST);
250         }
251         end_critical_section(S_WORKER_LIST);
252
253         master_cleanup();
254         return(0);
255 }