1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
47 * ====================================================================
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/>.
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.
60 #include "apr_strings.h"
61 #include "apr_getopt.h"
62 #include "apr_general.h"
65 #define APR_WANT_STDIO
69 #include "ap_config.h"
71 #include "http_main.h"
73 #include "http_config.h"
74 #include "http_vhost.h"
76 #include "util_ebcdic.h"
79 /* WARNING: Win32 binds http_main.c dynamically to the server. Please place
80 * extern functions and global data in another appropriate module.
82 * Most significant main() global data can be found in http_config.c
85 /* XXX - We should be able to grab the per-MPM settings here too */
86 static void show_compile_settings(void)
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
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");
101 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
102 printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
104 #ifdef APACHE_MPM_DIR
105 printf(" -D APACHE_MPM_DIR=\"%s\"\n", APACHE_MPM_DIR);
108 printf(" -D HAVE_SHMGET\n");
110 #if APR_FILE_BASED_SHM
111 printf(" -D APR_FILE_BASED_SHM\n");
114 printf(" -D APR_HAS_SENDFILE\n");
117 printf(" -D APR_HAS_MMAP\n");
120 printf(" -D NO_WRITEV\n");
123 printf(" -D NO_LINGCLOSE\n");
126 printf(" -D APR_HAVE_IPV6\n");
128 #if APR_USE_FLOCK_SERIALIZE
129 printf(" -D APR_USE_FLOCK_SERIALIZE\n");
131 #if APR_USE_SYSVSEM_SERIALIZE
132 printf(" -D APR_USE_SYSVSEM_SERIALIZE\n");
134 #if APR_USE_FCNTL_SERIALIZE
135 printf(" -D APR_USE_FCNTL_SERIALIZE\n");
137 #if APR_USE_PROC_PTHREAD_SERIALIZE
138 printf(" -D APR_USE_PROC_PTHREAD_SERIALIZE\n");
140 #if APR_USE_PTHREAD_SERIALIZE
141 printf(" -D APR_USE_PTHREAD_SERIALIZE\n");
143 #if APR_PROCESS_LOCK_IS_GLOBAL
144 printf(" -D APR_PROCESS_LOCK_IS_GLOBAL\n");
146 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
147 printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
149 #if APR_HAS_OTHER_CHILD
150 printf(" -D APR_HAS_OTHER_CHILD\n");
152 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
153 printf(" -D AP_HAVE_RELIABLE_PIPED_LOGS\n");
156 printf(" -D BUFFERED_LOGS\n");
158 printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
161 #if APR_CHARSET_EBCDIC
162 printf(" -D APR_CHARSET_EBCDIC\n");
165 printf(" -D APACHE_XLATE\n");
167 #ifdef NEED_HASHBANG_EMUL
168 printf(" -D NEED_HASHBANG_EMUL\n");
171 printf(" -D SHARED_CORE\n");
174 /* This list displays the compiled in default paths: */
176 printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
179 printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
181 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
182 printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
184 #ifdef DEFAULT_PIDLOG
185 printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
187 #ifdef DEFAULT_SCOREBOARD
188 printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
190 #ifdef DEFAULT_LOCKFILE
191 printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
193 #ifdef DEFAULT_ERRORLOG
194 printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
196 #ifdef TYPES_CONFIG_FILE
197 printf(" -D TYPES_CONFIG_FILE=\"" TYPES_CONFIG_FILE "\"\n");
199 #ifdef SERVER_CONFIG_FILE
200 printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
204 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
206 apr_pool_destroy(process->pool); /* and destroy all descendent pools */
208 exit(process_exit_value);
211 static process_rec *create_process(int argc, const char * const *argv)
213 process_rec *process;
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.
222 ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
223 "apr_pool_create() failed to create "
229 apr_pool_tag(cntx, "process");
230 ap_open_stderr_log(cntx);
232 process = apr_palloc(cntx, sizeof(process_rec));
233 process->pool = cntx;
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]);
243 static void usage(process_rec *process)
245 const char *bin = process->argv[0];
246 char pad[MAX_STRING_LEN];
249 for (i = 0; i < strlen(bin); i++)
253 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
255 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
257 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " %s [-C \"directive\"] [-c \"directive\"]", pad);
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);
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:");
266 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -R directory : specify an alternate location for shared object files");
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");
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");
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);
293 int main(int argc, const char * const argv[])
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;
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 */
311 AP_MONCONTROL(0); /* turn of profiling of startup */
315 process = create_process(argc, argv);
316 pglobal = process->pool;
317 pconf = process->pconf;
318 ap_server_argv0 = process->short_name;
320 #if APR_CHARSET_EBCDIC
321 if (ap_init_ebcdic(pglobal) != APR_SUCCESS) {
322 destroy_and_exit_process(process, 1);
326 ap_setup_prelinked_modules(process);
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 *));
334 ap_run_rewrite_args(process);
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.
339 apr_getopt_init(&opt, pcommands, process->argc, process->argv);
341 while ((rv = apr_getopt(opt, AP_SERVER_BASEARGS, &c, &optarg))
346 new = (char **)apr_array_push(ap_server_post_read_config);
347 *new = apr_pstrdup(pcommands, optarg);
350 new = (char **)apr_array_push(ap_server_pre_read_config);
351 *new = apr_pstrdup(pcommands, optarg);
354 def_server_root = optarg;
357 new = (char **)apr_array_push(ap_server_config_defines);
358 *new = apr_pstrdup(pcommands, optarg);
361 new = (char **)apr_array_push(ap_server_config_defines);
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);
372 show_compile_settings();
373 destroy_and_exit_process(process, 0);
376 destroy_and_exit_process(process, 0);
378 ap_show_directives();
379 destroy_and_exit_process(process, 0);
389 /* bad cmdline option? then we die */
394 apr_pool_create(&plog, pglobal);
395 apr_pool_tag(plog, "plog");
396 apr_pool_create(&ptemp, pconf);
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. */
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);
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);
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);
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);
423 apr_pool_destroy(ptemp);
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);
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
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);
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);
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);
456 apr_pool_destroy(ptemp);
457 apr_pool_lock(pconf, 1);
459 ap_run_optional_fn_retrieve();
461 if (ap_mpm_run(pconf, plog, server_conf)) break;
462 apr_pool_lock(pconf, 0);
464 apr_pool_lock(pconf, 0);
465 destroy_and_exit_process(process, 0);
466 return 0; /* Supress compiler warning. */
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)
475 return XML_ErrorString(XML_ERROR_NONE);
477 #endif /* USE_EXPAT */
479 #ifndef SHARED_CORE_BOOTSTRAP
481 * Force apr_password_validate() into the image so that modules like
482 * mod_auth can use it even if they're dynamically loaded.
484 void suck_in_apr_password_validate(void);
485 void suck_in_apr_password_validate(void)
487 apr_password_validate("a", "b");
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.
497 const void *suck_in_APR(void);
498 const void *suck_in_APR(void)
500 extern const void *ap_ugly_hack;