]> granicus.if.org Git - apache/blob - server/main.c
tag a few of the key httpd pools
[apache] / server / main.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 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 #include "apr.h"
60 #include "apr_strings.h"
61 #include "apr_getopt.h"
62 #include "apr_general.h"
63 #include "apr_lib.h"
64
65 #define APR_WANT_STDIO
66 #include "apr_want.h"
67
68 #define CORE_PRIVATE
69 #include "ap_config.h"
70 #include "httpd.h" 
71 #include "http_main.h" 
72 #include "http_log.h" 
73 #include "http_config.h"
74 #include "http_vhost.h"
75 #include "apr_uri.h" 
76 #include "util_ebcdic.h"
77 #include "ap_mpm.h"
78
79 /* WARNING: Win32 binds http_main.c dynamically to the server. Please place 
80  *          extern functions and global data in another appropriate module.
81  *
82  * Most significant main() global data can be found in http_config.c
83  */
84
85 /* XXX - We should be able to grab the per-MPM settings here too */
86 static void show_compile_settings(void)
87 {
88     printf("Server version: %s\n", ap_get_server_version());
89     printf("Server built:   %s\n", ap_get_server_built());
90     printf("Server's Module Magic Number: %u:%u\n",
91            MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
92     /* sizeof(foo) is long on some platforms so we might as well
93      * make it long everywhere to keep the printf format
94      * consistent
95      */
96     printf("Architecture:   %ld-bit\n", 8 * (long)sizeof(void *));
97     printf("Server compiled with....\n");
98 #ifdef BIG_SECURITY_HOLE
99     printf(" -D BIG_SECURITY_HOLE\n");
100 #endif
101 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
102     printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
103 #endif
104 #ifdef APACHE_MPM_DIR
105     printf(" -D APACHE_MPM_DIR=\"%s\"\n", APACHE_MPM_DIR);
106 #endif
107 #ifdef HAVE_SHMGET
108     printf(" -D HAVE_SHMGET\n");
109 #endif
110 #if APR_FILE_BASED_SHM
111     printf(" -D APR_FILE_BASED_SHM\n");
112 #endif
113 #if APR_HAS_SENDFILE
114     printf(" -D APR_HAS_SENDFILE\n");
115 #endif
116 #if APR_HAS_MMAP
117     printf(" -D APR_HAS_MMAP\n");
118 #endif
119 #ifdef NO_WRITEV
120     printf(" -D NO_WRITEV\n");
121 #endif
122 #ifdef NO_LINGCLOSE
123     printf(" -D NO_LINGCLOSE\n");
124 #endif
125 #if APR_HAVE_IPV6
126     printf(" -D APR_HAVE_IPV6\n");
127 #endif
128 #if APR_USE_FLOCK_SERIALIZE
129     printf(" -D APR_USE_FLOCK_SERIALIZE\n");
130 #endif
131 #if APR_USE_SYSVSEM_SERIALIZE
132     printf(" -D APR_USE_SYSVSEM_SERIALIZE\n");
133 #endif
134 #if APR_USE_FCNTL_SERIALIZE
135     printf(" -D APR_USE_FCNTL_SERIALIZE\n");
136 #endif
137 #if APR_USE_PROC_PTHREAD_SERIALIZE
138     printf(" -D APR_USE_PROC_PTHREAD_SERIALIZE\n");
139 #endif
140 #if APR_USE_PTHREAD_SERIALIZE
141     printf(" -D APR_USE_PTHREAD_SERIALIZE\n");
142 #endif
143 #if APR_PROCESS_LOCK_IS_GLOBAL
144     printf(" -D APR_PROCESS_LOCK_IS_GLOBAL\n");
145 #endif
146 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
147     printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
148 #endif
149 #if APR_HAS_OTHER_CHILD
150     printf(" -D APR_HAS_OTHER_CHILD\n");
151 #endif
152 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
153     printf(" -D AP_HAVE_RELIABLE_PIPED_LOGS\n");
154 #endif
155 #ifdef BUFFERED_LOGS
156     printf(" -D BUFFERED_LOGS\n");
157 #ifdef PIPE_BUF
158         printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
159 #endif
160 #endif
161 #if APR_CHARSET_EBCDIC
162     printf(" -D APR_CHARSET_EBCDIC\n");
163 #endif
164 #ifdef APACHE_XLATE
165     printf(" -D APACHE_XLATE\n");
166 #endif
167 #ifdef NEED_HASHBANG_EMUL
168     printf(" -D NEED_HASHBANG_EMUL\n");
169 #endif
170 #ifdef SHARED_CORE
171     printf(" -D SHARED_CORE\n");
172 #endif
173
174 /* This list displays the compiled in default paths: */
175 #ifdef HTTPD_ROOT
176     printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
177 #endif
178 #ifdef SUEXEC_BIN
179     printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
180 #endif
181 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
182     printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
183 #endif
184 #ifdef DEFAULT_PIDLOG
185     printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
186 #endif
187 #ifdef DEFAULT_SCOREBOARD
188     printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
189 #endif
190 #ifdef DEFAULT_LOCKFILE
191     printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
192 #endif
193 #ifdef DEFAULT_ERRORLOG
194     printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
195 #endif
196 #ifdef TYPES_CONFIG_FILE
197     printf(" -D TYPES_CONFIG_FILE=\"" TYPES_CONFIG_FILE "\"\n");
198 #endif
199 #ifdef SERVER_CONFIG_FILE
200     printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
201 #endif
202 }
203
204 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
205 {
206     apr_pool_destroy(process->pool); /* and destroy all descendent pools */
207     apr_terminate();
208     exit(process_exit_value);
209 }
210
211 static process_rec *create_process(int argc, const char * const *argv)
212 {
213     process_rec *process;
214     apr_pool_t *cntx;
215     apr_status_t stat;
216
217     stat = apr_pool_create(&cntx, NULL);
218     if (stat != APR_SUCCESS) {
219         /* XXX From the time that we took away the NULL pool->malloc mapping
220          *     we have been unable to log here without segfaulting.
221          */
222         ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
223                      "apr_pool_create() failed to create "
224                      "initial context");
225         apr_terminate();
226         exit(1);
227     }
228
229     apr_pool_tag(cntx, "process");
230     ap_open_stderr_log(cntx);
231
232     process = apr_palloc(cntx, sizeof(process_rec));
233     process->pool = cntx;
234
235     apr_pool_create(&process->pconf, process->pool);
236     apr_pool_tag(process->pconf, "pconf");
237     process->argc = argc;
238     process->argv = argv;
239     process->short_name = apr_filename_of_pathname(argv[0]);
240     return process;
241 }
242
243 static void usage(process_rec *process)
244 {
245     const char *bin = process->argv[0];
246     char pad[MAX_STRING_LEN];
247     unsigned i;
248
249     for (i = 0; i < strlen(bin); i++)
250         pad[i] = ' ';
251     pad[i] = '\0';
252 #ifdef SHARED_CORE
253     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
254 #else
255     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
256 #endif
257     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-C \"directive\"] [-c \"directive\"]", pad);
258 #ifdef WIN32
259     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-k restart|shutdown|start]", pad);
260     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-n service_name]", pad);
261     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-i] [-u]", pad);
262 #endif
263     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-v] [-V] [-h] [-l] [-L] [-t] [-T]", pad);
264     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:");
265 #ifdef SHARED_CORE
266     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -R directory      : specify an alternate location for shared object files");
267 #endif
268     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -D name           : define a name for use in <IfDefine name> directives");
269     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -d directory      : specify an alternate initial ServerRoot");
270     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -f file           : specify an alternate ServerConfigFile");
271     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -C \"directive\"    : process directive before reading config files");
272     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -c \"directive\"    : process directive after  reading config files");
273 #ifdef WIN32
274     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -n name           : set service name and use its ServerConfigFile");
275     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k shutdown       : tell running Apache to shutdown");
276     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k restart        : tell running Apache to do a graceful restart");
277     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k start          : tell Apache to start");
278     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -i                : install an Apache service");
279     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -u                : uninstall an Apache service");
280 #endif
281     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -v                : show version number");
282     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -V                : show compile settings");
283     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -h                : list available command line options (this page)");
284     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -l                : list compiled in modules");
285     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -L                : list available configuration directives");
286     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings)"); 
287     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t                : run syntax check for config files (with docroot check)");
288     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -T                : run syntax check for config files (without docroot check)");
289     /* TODOC: -X goes away, expect MPMs to use -D options */
290     destroy_and_exit_process(process, 1);
291 }
292
293 int main(int argc, const char * const argv[])
294 {
295     char c;
296     int configtestonly = 0;
297     const char *confname = SERVER_CONFIG_FILE;
298     const char *def_server_root = HTTPD_ROOT;
299     process_rec *process;
300     server_rec *server_conf;
301     apr_pool_t *pglobal;
302     apr_pool_t *pconf;
303     apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
304     apr_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
305     apr_pool_t *pcommands; /* Pool for -D, -C and -c switches */
306     apr_getopt_t *opt;
307     apr_status_t rv;
308     module **mod;
309     const char *optarg;
310
311     AP_MONCONTROL(0); /* turn of profiling of startup */
312
313     apr_initialize();
314
315     process = create_process(argc, argv);
316     pglobal = process->pool;
317     pconf = process->pconf;
318     ap_server_argv0 = process->short_name;
319
320 #if APR_CHARSET_EBCDIC
321     if (ap_init_ebcdic(pglobal) != APR_SUCCESS) {
322         destroy_and_exit_process(process, 1);
323     }
324 #endif
325
326     ap_setup_prelinked_modules(process);
327
328     apr_pool_create(&pcommands, pglobal);
329     apr_pool_tag(pcommands, "pcommands");
330     ap_server_pre_read_config  = apr_array_make(pcommands, 1, sizeof(char *));
331     ap_server_post_read_config = apr_array_make(pcommands, 1, sizeof(char *));
332     ap_server_config_defines   = apr_array_make(pcommands, 1, sizeof(char *));
333
334     ap_run_rewrite_args(process);
335
336     /* Maintain AP_SERVER_BASEARGS list in http_main.h to allow the MPM 
337      * to safely pass on our args from its rewrite_args() handler.
338      */
339     apr_getopt_init(&opt, pcommands, process->argc, process->argv);
340
341     while ((rv = apr_getopt(opt, AP_SERVER_BASEARGS, &c, &optarg))
342             == APR_SUCCESS) {
343         char **new;
344         switch (c) {
345         case 'c':
346             new = (char **)apr_array_push(ap_server_post_read_config);
347             *new = apr_pstrdup(pcommands, optarg);
348             break;
349         case 'C':
350             new = (char **)apr_array_push(ap_server_pre_read_config);
351             *new = apr_pstrdup(pcommands, optarg);
352             break;
353         case 'd':
354             def_server_root = optarg;
355             break;
356         case 'D':
357             new = (char **)apr_array_push(ap_server_config_defines);
358             *new = apr_pstrdup(pcommands, optarg);
359             break;
360         case 'X':
361             new = (char **)apr_array_push(ap_server_config_defines);
362             *new = "DEBUG";
363             break;
364         case 'f':
365             confname = optarg;
366             break;
367         case 'v':
368             printf("Server version: %s\n", ap_get_server_version());
369             printf("Server built:   %s\n", ap_get_server_built());
370             destroy_and_exit_process(process, 0);
371         case 'V':
372             show_compile_settings();
373             destroy_and_exit_process(process, 0);
374         case 'l':
375             ap_show_modules();
376             destroy_and_exit_process(process, 0);
377         case 'L':
378             ap_show_directives();
379             destroy_and_exit_process(process, 0);
380         case 't':
381             configtestonly = 1;
382             break;
383         case 'h':
384         case '?':
385             usage(process);
386         }
387     }
388
389     /* bad cmdline option?  then we die */
390     if (rv != APR_EOF) { 
391         usage(process);
392     }
393
394     apr_pool_create(&plog, pglobal);
395     apr_pool_tag(plog, "plog");
396     apr_pool_create(&ptemp, pconf);
397
398     /* Note that we preflight the config file once
399        before reading it _again_ in the main loop.
400        This allows things, log files configuration 
401        for example, to settle down. */
402
403     ap_server_root = def_server_root;
404     server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
405     ap_run_pre_config(pconf, plog, ptemp);
406     ap_process_config_tree(server_conf, ap_conftree, process->pconf, ptemp); 
407     ap_fixup_virtual_hosts(pconf, server_conf);
408     ap_fini_vhost_config(pconf, server_conf);
409     apr_sort_hooks();
410     if (configtestonly) {
411         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n");
412         destroy_and_exit_process(process, 0);
413     }
414     apr_pool_clear(plog);
415     if ( ap_run_open_logs(pconf, plog, ptemp, server_conf) != OK) {
416         ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR| APLOG_NOERRNO, 0, NULL, "Unable to open logs\n");
417         destroy_and_exit_process(process, 1);
418     }
419     if ( ap_run_post_config(pconf, plog, ptemp, server_conf) != OK) {
420         ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR| APLOG_NOERRNO, 0, NULL, "Configuration Failed\n");
421         destroy_and_exit_process(process, 1);
422     }
423     apr_pool_destroy(ptemp);
424
425     for (;;) {
426         apr_hook_deregister_all();
427         apr_pool_clear(pconf);
428         for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
429             ap_register_hooks(*mod, pconf);
430         }
431
432
433         /* This is a hack until we finish the code so that it only reads
434          * the config file once and just operates on the tree already in
435          * memory.  rbb
436          */
437         ap_conftree = NULL;
438         apr_pool_create(&ptemp, pconf);
439         ap_server_root = def_server_root;
440         server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
441         ap_run_pre_config(pconf, plog, ptemp);
442         ap_process_config_tree(server_conf, ap_conftree, process->pconf, ptemp);
443         ap_fixup_virtual_hosts(pconf, server_conf);
444         ap_fini_vhost_config(pconf, server_conf);
445         apr_sort_hooks();
446         apr_pool_clear(plog);
447         if ( ap_run_open_logs(pconf, plog, ptemp, server_conf) != OK) {
448             ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR| APLOG_NOERRNO, 0, NULL, "Unable to open logs\n");
449             destroy_and_exit_process(process, 1);
450         }
451
452         if  (ap_run_post_config(pconf, plog, ptemp, server_conf) != OK) {
453             ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR | APLOG_NOERRNO, 0, NULL, "Configuration Failed\n");
454             destroy_and_exit_process(process, 1);
455         }
456         apr_pool_destroy(ptemp);
457         apr_pool_lock(pconf, 1);
458
459         ap_run_optional_fn_retrieve();
460
461         if (ap_mpm_run(pconf, plog, server_conf)) break;
462         apr_pool_lock(pconf, 0);
463     }
464     apr_pool_lock(pconf, 0);
465     destroy_and_exit_process(process, 0);
466     return 0; /* Supress compiler warning. */
467 }
468
469 /* force Expat to be linked into the server executable */
470 #if defined(USE_EXPAT) && !defined(SHARED_CORE_BOOTSTRAP)
471 #include "xmlparse.h"
472 const XML_LChar *suck_in_expat(void);
473 const XML_LChar *suck_in_expat(void)
474 {
475     return XML_ErrorString(XML_ERROR_NONE);
476 }
477 #endif /* USE_EXPAT */
478
479 #ifndef SHARED_CORE_BOOTSTRAP
480 /*
481  * Force apr_password_validate() into the image so that modules like
482  * mod_auth can use it even if they're dynamically loaded.
483  */
484 void suck_in_apr_password_validate(void);
485 void suck_in_apr_password_validate(void)
486 {
487     apr_password_validate("a", "b");
488 }
489 #endif
490
491 #ifdef AP_USING_AUTOCONF
492 /* This ugly little hack pulls any function referenced in exports.c into
493  * the web server.  exports.c is generated during the build, and it
494  * has all of the APR functions specified by the apr/apr.exports and
495  * apr-util/aprutil.exports files.
496  */
497 const void *suck_in_APR(void);
498 const void *suck_in_APR(void)
499 {
500     extern const void *ap_ugly_hack;
501
502     return ap_ugly_hack;
503 }
504 #endif