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