1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_strings.h"
19 #include "apr_getopt.h"
20 #include "apr_general.h"
23 #include "apr_version.h"
24 #include "apu_version.h"
26 #define APR_WANT_STDIO
27 #define APR_WANT_STRFUNC
31 #include "ap_config.h"
33 #include "http_main.h"
35 #include "http_config.h"
36 #include "http_core.h"
37 #include "http_vhost.h"
39 #include "util_ebcdic.h"
41 #include "mpm_common.h"
47 /* WARNING: Win32 binds http_main.c dynamically to the server. Please place
48 * extern functions and global data in another appropriate module.
50 * Most significant main() global data can be found in http_config.c
53 static void show_mpm_settings(void)
58 printf("Server MPM: %s\n", ap_show_mpm());
60 retval = ap_mpm_query(AP_MPMQ_IS_THREADED, &mpm_query_info);
62 if (retval == APR_SUCCESS) {
63 printf(" threaded: ");
65 if (mpm_query_info == AP_MPMQ_DYNAMIC) {
66 printf("yes (variable thread count)\n");
68 else if (mpm_query_info == AP_MPMQ_STATIC) {
69 printf("yes (fixed thread count)\n");
76 retval = ap_mpm_query(AP_MPMQ_IS_FORKED, &mpm_query_info);
78 if (retval == APR_SUCCESS) {
81 if (mpm_query_info == AP_MPMQ_DYNAMIC) {
82 printf("yes (variable process count)\n");
84 else if (mpm_query_info == AP_MPMQ_STATIC) {
85 printf("yes (fixed process count)\n");
93 static void show_compile_settings(void)
95 printf("Server version: %s\n", ap_get_server_description());
96 printf("Server built: %s\n", ap_get_server_built());
97 printf("Server's Module Magic Number: %u:%u\n",
98 MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
99 printf("Server loaded: APR %s, APR-UTIL %s\n",
100 apr_version_string(), apu_version_string());
101 printf("Compiled using: APR %s, APR-UTIL %s\n",
102 APR_VERSION_STRING, APU_VERSION_STRING);
103 /* sizeof(foo) is long on some platforms so we might as well
104 * make it long everywhere to keep the printf format
107 printf("Architecture: %ld-bit\n", 8 * (long)sizeof(void *));
111 printf("Server compiled with....\n");
112 #ifdef BIG_SECURITY_HOLE
113 printf(" -D BIG_SECURITY_HOLE\n");
116 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
117 printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
121 printf(" -D OS=\"" OS "\"\n");
124 #ifdef APACHE_MPM_DIR
125 printf(" -D APACHE_MPM_DIR=\"" APACHE_MPM_DIR "\"\n");
129 printf(" -D HAVE_SHMGET\n");
132 #if APR_FILE_BASED_SHM
133 printf(" -D APR_FILE_BASED_SHM\n");
137 printf(" -D APR_HAS_SENDFILE\n");
141 printf(" -D APR_HAS_MMAP\n");
145 printf(" -D NO_WRITEV\n");
149 printf(" -D NO_LINGCLOSE\n");
153 printf(" -D APR_HAVE_IPV6 (IPv4-mapped addresses ");
154 #ifdef AP_ENABLE_V4_MAPPED
155 printf("enabled)\n");
157 printf("disabled)\n");
161 #if APR_USE_FLOCK_SERIALIZE
162 printf(" -D APR_USE_FLOCK_SERIALIZE\n");
165 #if APR_USE_SYSVSEM_SERIALIZE
166 printf(" -D APR_USE_SYSVSEM_SERIALIZE\n");
169 #if APR_USE_POSIXSEM_SERIALIZE
170 printf(" -D APR_USE_POSIXSEM_SERIALIZE\n");
173 #if APR_USE_FCNTL_SERIALIZE
174 printf(" -D APR_USE_FCNTL_SERIALIZE\n");
177 #if APR_USE_PROC_PTHREAD_SERIALIZE
178 printf(" -D APR_USE_PROC_PTHREAD_SERIALIZE\n");
181 #if APR_USE_PTHREAD_SERIALIZE
182 printf(" -D APR_USE_PTHREAD_SERIALIZE\n");
185 #if APR_PROCESS_LOCK_IS_GLOBAL
186 printf(" -D APR_PROCESS_LOCK_IS_GLOBAL\n");
189 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
190 printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
193 #if APR_HAS_OTHER_CHILD
194 printf(" -D APR_HAS_OTHER_CHILD\n");
197 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
198 printf(" -D AP_HAVE_RELIABLE_PIPED_LOGS\n");
202 printf(" -D BUFFERED_LOGS\n");
204 printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
208 printf(" -D DYNAMIC_MODULE_LIMIT=%ld\n",(long)DYNAMIC_MODULE_LIMIT);
210 #if APR_CHARSET_EBCDIC
211 printf(" -D APR_CHARSET_EBCDIC\n");
214 #ifdef NEED_HASHBANG_EMUL
215 printf(" -D NEED_HASHBANG_EMUL\n");
219 printf(" -D SHARED_CORE\n");
222 /* This list displays the compiled in default paths: */
224 printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
228 printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
231 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
232 printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
235 #ifdef DEFAULT_PIDLOG
236 printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
239 #ifdef DEFAULT_SCOREBOARD
240 printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
243 #ifdef DEFAULT_LOCKFILE
244 printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
247 #ifdef DEFAULT_ERRORLOG
248 printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
251 #ifdef AP_TYPES_CONFIG_FILE
252 printf(" -D AP_TYPES_CONFIG_FILE=\"" AP_TYPES_CONFIG_FILE "\"\n");
255 #ifdef SERVER_CONFIG_FILE
256 printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
260 static void destroy_and_exit_process(process_rec *process,
261 int process_exit_value)
263 apr_pool_destroy(process->pool); /* and destroy all descendent pools */
265 exit(process_exit_value);
268 #define OOM_MESSAGE "[crit] Memory allocation failed, " \
269 "aborting process." APR_EOL_STR
271 /* APR callback invoked if allocation fails. */
272 static int abort_on_oom(int retcode)
274 write(STDERR_FILENO, OOM_MESSAGE, strlen(OOM_MESSAGE));
276 return retcode; /* unreachable, hopefully. */
279 static process_rec *create_process(int argc, const char * const *argv)
281 process_rec *process;
285 stat = apr_pool_create(&cntx, NULL);
286 if (stat != APR_SUCCESS) {
287 /* XXX From the time that we took away the NULL pool->malloc mapping
288 * we have been unable to log here without segfaulting.
290 ap_log_error(APLOG_MARK, APLOG_ERR, stat, NULL,
291 "apr_pool_create() failed to create "
297 apr_pool_abort_set(abort_on_oom, cntx);
298 apr_pool_tag(cntx, "process");
299 ap_open_stderr_log(cntx);
301 process = apr_palloc(cntx, sizeof(process_rec));
302 process->pool = cntx;
304 apr_pool_create(&process->pconf, process->pool);
305 apr_pool_tag(process->pconf, "pconf");
306 process->argc = argc;
307 process->argv = argv;
308 process->short_name = apr_filepath_name_get(argv[0]);
312 static void usage(process_rec *process)
314 const char *bin = process->argv[0];
315 char pad[MAX_STRING_LEN];
318 for (i = 0; i < strlen(bin); i++) {
325 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL ,
326 "Usage: %s [-R directory] [-D name] [-d directory] [-f file]",
329 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
330 "Usage: %s [-D name] [-d directory] [-f file]", bin);
333 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
334 " %s [-C \"directive\"] [-c \"directive\"]", pad);
337 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
338 " %s [-w] [-k start|restart|stop|shutdown]", pad);
339 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
340 " %s [-k install|config|uninstall] [-n service_name]",
343 #ifdef AP_MPM_WANT_SIGNAL_SERVER
344 #ifdef AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
345 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
346 " %s [-k start|restart|graceful|graceful-stop|stop]",
349 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
350 " %s [-k start|restart|graceful|stop]",
352 #endif /* AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN */
354 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
355 " %s [-v] [-V] [-h] [-l] [-L] [-t] [-S]", pad);
356 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
360 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
361 " -R directory : specify an alternate location for "
362 "shared object files");
365 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
366 " -D name : define a name for use in "
367 "<IfDefine name> directives");
368 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
369 " -d directory : specify an alternate initial "
371 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
372 " -f file : specify an alternate ServerConfigFile");
373 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
374 " -C \"directive\" : process directive before reading "
376 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
377 " -c \"directive\" : process directive after reading "
381 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
382 " -n name : set screen name");
385 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
386 " -n name : set service name and use its "
388 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
389 " -k start : tell Apache to start");
390 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
391 " -k restart : tell running Apache to do a graceful "
393 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
394 " -k stop|shutdown : tell running Apache to shutdown");
395 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
396 " -k install : install an Apache service");
397 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
398 " -k config : change startup Options of an Apache "
400 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
401 " -k uninstall : uninstall an Apache service");
402 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
403 " -w : hold open the console window on error");
406 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
407 " -e level : show startup errors of level "
409 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
410 " -E file : log startup errors to file");
411 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
412 " -v : show version number");
413 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
414 " -V : show compile settings");
415 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
416 " -h : list available command line options "
418 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
419 " -l : list compiled in modules");
420 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
421 " -L : list available configuration "
423 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
424 " -t -D DUMP_VHOSTS : show parsed settings (currently only "
426 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
427 " -S : a synonym for -t -D DUMP_VHOSTS");
428 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
429 " -t -D DUMP_MODULES : show all loaded modules ");
430 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
431 " -M : a synonym for -t -D DUMP_MODULES");
432 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
433 " -t : run syntax check for config files");
435 destroy_and_exit_process(process, 1);
438 int main(int argc, const char * const argv[])
441 int configtestonly = 0;
442 const char *confname = SERVER_CONFIG_FILE;
443 const char *def_server_root = HTTPD_ROOT;
444 const char *temp_error_log = NULL;
446 process_rec *process;
447 server_rec *server_conf;
450 apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
451 apr_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
452 apr_pool_t *pcommands; /* Pool for -D, -C and -c switches */
457 APR_OPTIONAL_FN_TYPE(ap_signal_server) *signal_server;
459 AP_MONCONTROL(0); /* turn off profiling of startup */
461 apr_app_initialize(&argc, &argv, NULL);
463 process = create_process(argc, argv);
464 pglobal = process->pool;
465 pconf = process->pconf;
466 ap_server_argv0 = process->short_name;
468 /* Set up the OOM callback in the global pool, so all pools should
469 * by default inherit it. */
470 apr_pool_abort_set(abort_on_oom, apr_pool_parent_get(process->pool));
472 #if APR_CHARSET_EBCDIC
473 if (ap_init_ebcdic(pglobal) != APR_SUCCESS) {
474 destroy_and_exit_process(process, 1);
478 apr_pool_create(&pcommands, pglobal);
479 apr_pool_tag(pcommands, "pcommands");
480 ap_server_pre_read_config = apr_array_make(pcommands, 1, sizeof(char *));
481 ap_server_post_read_config = apr_array_make(pcommands, 1, sizeof(char *));
482 ap_server_config_defines = apr_array_make(pcommands, 1, sizeof(char *));
483 ap_pid_table = apr_table_make(pglobal, 1024);
485 error = ap_setup_prelinked_modules(process);
487 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_EMERG, 0, NULL, "%s: %s",
488 ap_server_argv0, error);
489 destroy_and_exit_process(process, 1);
492 ap_run_rewrite_args(process);
494 /* Maintain AP_SERVER_BASEARGS list in http_main.h to allow the MPM
495 * to safely pass on our args from its rewrite_args() handler.
497 apr_getopt_init(&opt, pcommands, process->argc, process->argv);
499 while ((rv = apr_getopt(opt, AP_SERVER_BASEARGS, &c, &optarg))
505 new = (char **)apr_array_push(ap_server_post_read_config);
506 *new = apr_pstrdup(pcommands, optarg);
510 new = (char **)apr_array_push(ap_server_pre_read_config);
511 *new = apr_pstrdup(pcommands, optarg);
515 def_server_root = optarg;
519 new = (char **)apr_array_push(ap_server_config_defines);
520 *new = apr_pstrdup(pcommands, optarg);
521 /* Setting -D DUMP_VHOSTS is equivalent to setting -S */
522 if (strcmp(optarg, "DUMP_VHOSTS") == 0)
524 /* Setting -D DUMP_MODULES is equivalent to setting -M */
525 if (strcmp(optarg, "DUMP_MODULES") == 0)
530 if (strcasecmp(optarg, "emerg") == 0) {
531 ap_default_loglevel = APLOG_EMERG;
533 else if (strcasecmp(optarg, "alert") == 0) {
534 ap_default_loglevel = APLOG_ALERT;
536 else if (strcasecmp(optarg, "crit") == 0) {
537 ap_default_loglevel = APLOG_CRIT;
539 else if (strncasecmp(optarg, "err", 3) == 0) {
540 ap_default_loglevel = APLOG_ERR;
542 else if (strncasecmp(optarg, "warn", 4) == 0) {
543 ap_default_loglevel = APLOG_WARNING;
545 else if (strcasecmp(optarg, "notice") == 0) {
546 ap_default_loglevel = APLOG_NOTICE;
548 else if (strcasecmp(optarg, "info") == 0) {
549 ap_default_loglevel = APLOG_INFO;
551 else if (strcasecmp(optarg, "debug") == 0) {
552 ap_default_loglevel = APLOG_DEBUG;
560 temp_error_log = apr_pstrdup(process->pool, optarg);
564 new = (char **)apr_array_push(ap_server_config_defines);
573 printf("Server version: %s\n", ap_get_server_description());
574 printf("Server built: %s\n", ap_get_server_built());
575 destroy_and_exit_process(process, 0);
578 show_compile_settings();
579 destroy_and_exit_process(process, 0);
583 destroy_and_exit_process(process, 0);
586 ap_show_directives();
587 destroy_and_exit_process(process, 0);
595 new = (char **)apr_array_push(ap_server_config_defines);
596 *new = "DUMP_VHOSTS";
601 new = (char **)apr_array_push(ap_server_config_defines);
602 *new = "DUMP_MODULES";
611 /* bad cmdline option? then we die */
612 if (rv != APR_EOF || opt->ind < opt->argc) {
616 apr_pool_create(&plog, pglobal);
617 apr_pool_tag(plog, "plog");
618 apr_pool_create(&ptemp, pconf);
619 apr_pool_tag(ptemp, "ptemp");
621 /* Note that we preflight the config file once
622 * before reading it _again_ in the main loop.
623 * This allows things, log files configuration
624 * for example, to settle down.
627 ap_server_root = def_server_root;
628 if (temp_error_log) {
629 ap_replace_stderr_log(process->pool, temp_error_log);
631 server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
633 destroy_and_exit_process(process, 1);
636 if (ap_run_pre_config(pconf, plog, ptemp) != OK) {
637 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
638 NULL, "Pre-configuration failed");
639 destroy_and_exit_process(process, 1);
642 rv = ap_process_config_tree(server_conf, ap_conftree,
643 process->pconf, ptemp);
645 ap_fixup_virtual_hosts(pconf, server_conf);
646 ap_fini_vhost_config(pconf, server_conf);
649 if (ap_run_check_config(pconf, plog, ptemp, server_conf) != OK) {
650 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
651 NULL, "Configuration check failed");
652 destroy_and_exit_process(process, 1);
655 if (configtestonly) {
656 ap_run_test_config(pconf, server_conf);
657 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "Syntax OK");
658 destroy_and_exit_process(process, 0);
662 signal_server = APR_RETRIEVE_OPTIONAL_FN(ap_signal_server);
666 if (signal_server(&exit_status, pconf) != 0) {
667 destroy_and_exit_process(process, exit_status);
671 /* If our config failed, deal with that here. */
673 destroy_and_exit_process(process, 1);
676 apr_pool_clear(plog);
678 if ( ap_run_open_logs(pconf, plog, ptemp, server_conf) != OK) {
679 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
680 0, NULL, "Unable to open logs");
681 destroy_and_exit_process(process, 1);
684 if ( ap_run_post_config(pconf, plog, ptemp, server_conf) != OK) {
685 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
686 NULL, "Configuration Failed");
687 destroy_and_exit_process(process, 1);
690 apr_pool_destroy(ptemp);
693 apr_hook_deregister_all();
694 apr_pool_clear(pconf);
696 for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
697 ap_register_hooks(*mod, pconf);
700 /* This is a hack until we finish the code so that it only reads
701 * the config file once and just operates on the tree already in
705 apr_pool_create(&ptemp, pconf);
706 apr_pool_tag(ptemp, "ptemp");
707 ap_server_root = def_server_root;
708 server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
710 destroy_and_exit_process(process, 1);
713 if (ap_run_pre_config(pconf, plog, ptemp) != OK) {
714 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
715 0, NULL, "Pre-configuration failed");
716 destroy_and_exit_process(process, 1);
719 if (ap_process_config_tree(server_conf, ap_conftree, process->pconf,
721 destroy_and_exit_process(process, 1);
723 ap_fixup_virtual_hosts(pconf, server_conf);
724 ap_fini_vhost_config(pconf, server_conf);
727 if (ap_run_check_config(pconf, plog, ptemp, server_conf) != OK) {
728 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
729 NULL, "Configuration check failed");
730 destroy_and_exit_process(process, 1);
733 apr_pool_clear(plog);
734 if (ap_run_open_logs(pconf, plog, ptemp, server_conf) != OK) {
735 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
736 0, NULL, "Unable to open logs");
737 destroy_and_exit_process(process, 1);
740 if (ap_run_post_config(pconf, plog, ptemp, server_conf) != OK) {
741 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
742 0, NULL, "Configuration Failed");
743 destroy_and_exit_process(process, 1);
746 apr_pool_destroy(ptemp);
747 apr_pool_lock(pconf, 1);
749 ap_run_optional_fn_retrieve();
751 if (ap_mpm_run(pconf, plog, server_conf))
754 apr_pool_lock(pconf, 0);
757 apr_pool_lock(pconf, 0);
758 destroy_and_exit_process(process, 0);
760 return 0; /* Termination 'ok' */
763 #ifdef AP_USING_AUTOCONF
764 /* This ugly little hack pulls any function referenced in exports.c into
765 * the web server. exports.c is generated during the build, and it
766 * has all of the APR functions specified by the apr/apr.exports and
767 * apr-util/aprutil.exports files.
769 const void *suck_in_APR(void);
770 const void *suck_in_APR(void)
772 extern const void *ap_ugly_hack;