]> granicus.if.org Git - apache/blob - server/main.c
Fix default Timeout & KeepaliveTimeout. Still need to limit directive
[apache] / server / main.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 #define CORE_PRIVATE
60 #include "ap_config.h"
61 #include "httpd.h" 
62 #include "http_main.h" 
63 #include "http_log.h" 
64 #include "http_config.h"
65 #include "http_vhost.h"
66 #include "util_uri.h" 
67 #include "util_ebcdic.h"
68 #include "apr_strings.h"
69 #include "apr_getopt.h"
70 #include "ap_mpm.h"
71
72 /* WARNING: Win32 binds http_main.c dynamically to the server. Please place 
73  *          extern functions and global data in another appropriate module.
74  *
75  * Most significant main() global data can be found in http_config.c
76  */
77
78 /* XXX - We should be able to grab the per-MPM settings here too */
79 static void show_compile_settings(void)
80 {
81     printf("Server version: %s\n", ap_get_server_version());
82     printf("Server built:   %s\n", ap_get_server_built());
83     printf("Server's Module Magic Number: %u:%u\n",
84            MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
85     printf("Server compiled with....\n");
86 #ifdef BIG_SECURITY_HOLE
87     printf(" -D BIG_SECURITY_HOLE\n");
88 #endif
89 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
90     printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
91 #endif
92 #ifdef APR_HAS_MMAP
93     printf(" -D HAVE_MMAP\n");
94 #endif
95 #ifdef HAVE_SHMGET
96     printf(" -D HAVE_SHMGET\n");
97 #endif
98 #ifdef USE_FILE_BASED_SCOREBOARD
99     printf(" -D USE_FILE_BASED_SCOREBOARD\n");
100 #endif
101 #ifdef USE_MEM_BASED_SCOREBOARD
102     printf(" -D USE_MEM_BASED_SCOREBOARD\n");
103 #endif
104 #ifdef USE_MMAP_FILES
105     printf(" -D USE_MMAP_FILES\n");
106 #ifdef MMAP_SEGMENT_SIZE
107         printf(" -D MMAP_SEGMENT_SIZE=%ld\n",(long)MMAP_SEGMENT_SIZE);
108 #endif
109 #endif /*USE_MMAP_FILES*/
110 #ifdef NO_WRITEV
111     printf(" -D NO_WRITEV\n");
112 #endif
113 #ifdef NO_LINGCLOSE
114     printf(" -D NO_LINGCLOSE\n");
115 #endif
116 #if APR_USE_FLOCK_SERIALIZE
117     printf(" -D APR_USE_FLOCK_SERIALIZE\n");
118 #endif
119 #if APR_USE_SYSVSEM_SERIALIZE
120     printf(" -D APR_USE_SYSVSEM_SERIALIZE\n");
121 #endif
122 #if APR_USE_FCNTL_SERIALIZE
123     printf(" -D APR_USE_FCNTL_SERIALIZE\n");
124 #endif
125 #if APR_USE_PROC_PTHREAD_SERIALIZE
126     printf(" -D APR_USE_PROC_PTHREAD_SERIALIZE\n");
127 #endif
128 #if APR_USE_PTHREAD_SERIALIZE
129     printf(" -D APR_USE_PTHREAD_SERIALIZE\n");
130 #endif
131 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
132     printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
133 #endif
134 #if APR_HAS_OTHER_CHILD
135     printf(" -D APR_HAS_OTHER_CHILD\n");
136 #endif
137 #ifdef HAVE_RELIABLE_PIPED_LOGS
138     printf(" -D HAVE_RELIABLE_PIPED_LOGS\n");
139 #endif
140 #ifdef BUFFERED_LOGS
141     printf(" -D BUFFERED_LOGS\n");
142 #ifdef PIPE_BUF
143         printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
144 #endif
145 #endif
146 #ifdef CHARSET_EBCDIC
147     printf(" -D CHARSET_EBCDIC\n");
148 #endif
149 #ifdef APACHE_XLATE
150     printf(" -D APACHE_XLATE\n");
151 #endif
152 #ifdef NEED_HASHBANG_EMUL
153     printf(" -D NEED_HASHBANG_EMUL\n");
154 #endif
155 #ifdef SHARED_CORE
156     printf(" -D SHARED_CORE\n");
157 #endif
158
159 /* This list displays the compiled in default paths: */
160 #ifdef HTTPD_ROOT
161     printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
162 #endif
163 #ifdef SUEXEC_BIN
164     printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
165 #endif
166 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
167     printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
168 #endif
169 #ifdef DEFAULT_PIDLOG
170     printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
171 #endif
172 #ifdef DEFAULT_SCOREBOARD
173     printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
174 #endif
175 #ifdef DEFAULT_LOCKFILE
176     printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
177 #endif
178 #ifdef DEFAULT_XFERLOG
179     printf(" -D DEFAULT_XFERLOG=\"" DEFAULT_XFERLOG "\"\n");
180 #endif
181 #ifdef DEFAULT_ERRORLOG
182     printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
183 #endif
184 #ifdef TYPES_CONFIG_FILE
185     printf(" -D TYPES_CONFIG_FILE=\"" TYPES_CONFIG_FILE "\"\n");
186 #endif
187 #ifdef SERVER_CONFIG_FILE
188     printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
189 #endif
190 }
191
192 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
193 {
194     apr_destroy_pool(process->pool); /* and destroy all descendent pools */
195     apr_terminate();
196     exit(process_exit_value);
197 }
198
199 static process_rec *create_process(int argc, char *const *argv)
200 {
201     process_rec *process;
202     
203     {
204         apr_pool_t *cntx;
205         apr_status_t stat;
206
207         stat = apr_create_pool(&cntx, NULL);
208         if (stat != APR_SUCCESS) {
209             ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
210                          "apr_create_pool() failed to create "
211                          "initial context");
212             apr_terminate();
213             exit(1);
214         }
215
216         process = apr_palloc(cntx, sizeof(process_rec));
217         process->pool = cntx;
218     }
219     apr_create_pool(&process->pconf, process->pool);
220     process->argc = argc;
221     process->argv = argv;
222     process->short_name = apr_filename_of_pathname(argv[0]);
223     return process;
224 }
225
226 static void usage(process_rec *process)
227 {
228     const char *bin = process->argv[0];
229     char pad[MAX_STRING_LEN];
230     unsigned i;
231
232     for (i = 0; i < strlen(bin); i++)
233         pad[i] = ' ';
234     pad[i] = '\0';
235 #ifdef SHARED_CORE
236     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
237 #else
238     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
239 #endif
240     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-C \"directive\"] [-c \"directive\"]", pad);
241 #ifdef WIN32
242     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-k restart|shutdown|start]", pad);
243     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-n service_name]", pad);
244     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-i] [-u]", pad);
245 #endif
246     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-v] [-V] [-h] [-l] [-L] [-S] [-t] [-T]", pad);
247     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:");
248 #ifdef SHARED_CORE
249     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -R directory     : specify an alternate location for shared object files");
250 #endif
251     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -D name          : define a name for use in <IfDefine name> directives");
252     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -d directory     : specify an alternate initial ServerRoot");
253     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -f file          : specify an alternate ServerConfigFile");
254     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -C \"directive\"   : process directive before reading config files");
255     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -c \"directive\"   : process directive after  reading config files");
256 #ifdef WIN32
257     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -n name          : set service name and use its ServerConfigFile");
258     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k shutdown      : tell running Apache to shutdown");
259     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k restart       : tell running Apache to do a graceful restart");
260     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k start         : tell Apache to start");
261     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -i               : install an Apache service");
262     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -u               : uninstall an Apache service");
263 #endif
264     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -v               : show version number");
265     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -V               : show compile settings");
266     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -h               : list available command line options (this page)");
267     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -l               : list compiled in modules");
268     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -L               : list available configuration directives");
269     /* TODOC: -S has been replaced by '-t -D DUMP_VHOSTS' */
270     /* ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -S               : show parsed settings (currently only vhost settings)"); */
271     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t               : run syntax check for config files (with docroot check)");
272     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -T               : run syntax check for config files (without docroot check)");
273     /* TODOC: -X goes away, expect MPMs to use -D options */
274     destroy_and_exit_process(process, 1);
275 }
276
277 int main(int argc, char *argv[])
278 {
279     char c;
280     int configtestonly = 0;
281     const char *confname = SERVER_CONFIG_FILE;
282     const char *def_server_root = HTTPD_ROOT;
283     process_rec *process;
284     server_rec *server_conf;
285     apr_pool_t *pglobal;
286     apr_pool_t *pconf;
287     apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
288     apr_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
289     apr_pool_t *pcommands; /* Pool for -D, -C and -c switches */
290     apr_getopt_t *opt;
291     module **mod;
292     ap_directive_t *conftree = NULL;
293     const char *optarg;
294
295     apr_initialize();
296     process = create_process(argc, argv);
297     pglobal = process->pool;
298     pconf = process->pconf;
299     ap_server_argv0 = process->short_name;
300
301 #ifdef CHARSET_EBCDIC
302     if (ap_init_ebcdic(pglobal) != APR_SUCCESS) {
303         destroy_and_exit_process(process, 1);
304     }
305 #endif
306
307     ap_util_uri_init();
308
309     ap_setup_prelinked_modules(process);
310
311     apr_create_pool(&pcommands, pglobal);
312     ap_server_pre_read_config  = apr_make_array(pcommands, 1, sizeof(char *));
313     ap_server_post_read_config = apr_make_array(pcommands, 1, sizeof(char *));
314     ap_server_config_defines   = apr_make_array(pcommands, 1, sizeof(char *));
315
316     ap_run_rewrite_args(process);
317
318     /* Maintain AP_SERVER_BASEARGS list in http_main.h to allow the MPM 
319      * to safely pass on our args from its rewrite_args() handler.
320      */
321     apr_initopt(&opt, pcommands, process->argc, process->argv);
322
323     while (apr_getopt(opt, AP_SERVER_BASEARGS, &c, &optarg) 
324             == APR_SUCCESS) {
325         char **new;
326         switch (c) {
327         case 'c':
328             new = (char **)apr_push_array(ap_server_post_read_config);
329             *new = apr_pstrdup(pcommands, optarg);
330             break;
331         case 'C':
332             new = (char **)apr_push_array(ap_server_pre_read_config);
333             *new = apr_pstrdup(pcommands, optarg);
334             break;
335         case 'd':
336             def_server_root = optarg;
337             break;
338         case 'D':
339             new = (char **)apr_push_array(ap_server_config_defines);
340             *new = apr_pstrdup(pcommands, optarg);
341             break;
342         case 'f':
343             confname = optarg;
344             break;
345         case 'v':
346             printf("Server version: %s\n", ap_get_server_version());
347             printf("Server built:   %s\n", ap_get_server_built());
348             destroy_and_exit_process(process, 0);
349         case 'V':
350             show_compile_settings();
351             destroy_and_exit_process(process, 0);
352         case 'l':
353             ap_show_modules();
354             destroy_and_exit_process(process, 0);
355         case 'L':
356             ap_show_directives();
357             destroy_and_exit_process(process, 0);
358         case 't':
359             configtestonly = 1;
360             break;
361         case '?':
362         case 'h':
363             usage(process);
364         }
365     }
366
367     apr_create_pool(&plog, pglobal);
368     apr_create_pool(&ptemp, pconf);
369
370     /* Note that we preflight the config file once
371        before reading it _again_ in the main loop.
372        This allows things, log files configuration 
373        for example, to settle down. */
374
375     ap_server_root = def_server_root;
376     server_conf = ap_read_config(process, ptemp, confname, &conftree);
377     ap_run_pre_config(pconf, plog, ptemp);
378     ap_process_config_tree(server_conf, conftree, process->pconf, ptemp); 
379     ap_fixup_virtual_hosts(pconf, server_conf);
380     ap_fini_vhost_config(pconf, server_conf);
381     ap_sort_hooks();
382     if (configtestonly) {
383         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n");
384         destroy_and_exit_process(process, 0);
385     }
386     apr_clear_pool(plog);
387     ap_run_open_logs(pconf, plog, ptemp, server_conf);
388     ap_post_config_hook(pconf, plog, ptemp, server_conf);
389     apr_destroy_pool(ptemp);
390
391     for (;;) {
392         ap_hook_deregister_all();
393         apr_clear_pool(pconf);
394         for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
395                 ap_register_hooks(*mod);
396         }
397         /* This is a hack until we finish the code so that it only reads
398          * the config file once and just operates on the tree already in
399          * memory.  rbb
400          */
401         conftree = NULL;
402         apr_create_pool(&ptemp, pconf);
403         ap_server_root = def_server_root;
404         server_conf = ap_read_config(process, ptemp, confname, &conftree);
405         ap_run_pre_config(pconf, plog, ptemp);
406         ap_process_config_tree(server_conf, conftree, process->pconf, ptemp); 
407         ap_fixup_virtual_hosts(pconf, server_conf);
408         ap_fini_vhost_config(pconf, server_conf);
409         ap_sort_hooks();
410         apr_clear_pool(plog);
411         ap_run_open_logs(pconf, plog, ptemp, server_conf);
412         ap_post_config_hook(pconf, plog, ptemp, server_conf);
413         apr_destroy_pool(ptemp);
414
415         if (ap_mpm_run(pconf, plog, server_conf)) break;
416     }
417     destroy_and_exit_process(process, 0);
418     return 0; /* Supress compiler warning. */
419 }
420
421 /* force Expat to be linked into the server executable */
422 #if defined(USE_EXPAT) && !defined(SHARED_CORE_BOOTSTRAP)
423 #include "xmlparse.h"
424 const XML_LChar *suck_in_expat(void);
425 const XML_LChar *suck_in_expat(void)
426 {
427     return XML_ErrorString(XML_ERROR_NONE);
428 }
429 #endif /* USE_EXPAT */
430
431 #ifndef SHARED_CORE_BOOTSTRAP
432 /*
433  * Force apr_validate_password() into the image so that modules like
434  * mod_auth can use it even if they're dynamically loaded.
435  */
436 void suck_in_apr_validate_password(void);
437 void suck_in_apr_validate_password(void)
438 {
439     apr_validate_password("a", "b");
440 }
441 #endif
442