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"
24 #include "apr_version.h"
25 #include "apu_version.h"
27 #define APR_WANT_STDIO
28 #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_request.h"
38 #include "http_vhost.h"
40 #include "util_ebcdic.h"
48 /* WARNING: Win32 binds http_main.c dynamically to the server. Please place
49 * extern functions and global data in another appropriate module.
51 * Most significant main() global data can be found in http_config.c
54 static void show_mpm_settings(void)
59 printf("Server MPM: %s\n", ap_show_mpm());
61 retval = ap_mpm_query(AP_MPMQ_IS_THREADED, &mpm_query_info);
63 if (retval == APR_SUCCESS) {
64 printf(" threaded: ");
66 if (mpm_query_info == AP_MPMQ_DYNAMIC) {
67 printf("yes (variable thread count)\n");
69 else if (mpm_query_info == AP_MPMQ_STATIC) {
70 printf("yes (fixed thread count)\n");
77 retval = ap_mpm_query(AP_MPMQ_IS_FORKED, &mpm_query_info);
79 if (retval == APR_SUCCESS) {
82 if (mpm_query_info == AP_MPMQ_DYNAMIC) {
83 printf("yes (variable process count)\n");
85 else if (mpm_query_info == AP_MPMQ_STATIC) {
86 printf("yes (fixed process count)\n");
94 static void show_compile_settings(void)
96 printf("Server version: %s\n", ap_get_server_description());
97 printf("Server built: %s\n", ap_get_server_built());
98 printf("Server's Module Magic Number: %u:%u\n",
99 MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
100 #if APR_MAJOR_VERSION >= 2
101 printf("Server loaded: APR %s\n", apr_version_string());
102 printf("Compiled using: APR %s\n", APR_VERSION_STRING);
104 printf("Server loaded: APR %s, APR-UTIL %s\n",
105 apr_version_string(), apu_version_string());
106 printf("Compiled using: APR %s, APR-UTIL %s\n",
107 APR_VERSION_STRING, APU_VERSION_STRING);
109 /* sizeof(foo) is long on some platforms so we might as well
110 * make it long everywhere to keep the printf format
113 printf("Architecture: %ld-bit\n", 8 * (long)sizeof(void *));
117 printf("Server compiled with....\n");
118 #ifdef BIG_SECURITY_HOLE
119 printf(" -D BIG_SECURITY_HOLE\n");
122 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
123 printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
127 printf(" -D OS=\"" OS "\"\n");
131 printf(" -D HAVE_SHMGET\n");
134 #if APR_FILE_BASED_SHM
135 printf(" -D APR_FILE_BASED_SHM\n");
139 printf(" -D APR_HAS_SENDFILE\n");
143 printf(" -D APR_HAS_MMAP\n");
147 printf(" -D NO_WRITEV\n");
151 printf(" -D NO_LINGCLOSE\n");
155 printf(" -D APR_HAVE_IPV6 (IPv4-mapped addresses ");
156 #ifdef AP_ENABLE_V4_MAPPED
157 printf("enabled)\n");
159 printf("disabled)\n");
163 #if APR_USE_FLOCK_SERIALIZE
164 printf(" -D APR_USE_FLOCK_SERIALIZE\n");
167 #if APR_USE_SYSVSEM_SERIALIZE
168 printf(" -D APR_USE_SYSVSEM_SERIALIZE\n");
171 #if APR_USE_POSIXSEM_SERIALIZE
172 printf(" -D APR_USE_POSIXSEM_SERIALIZE\n");
175 #if APR_USE_FCNTL_SERIALIZE
176 printf(" -D APR_USE_FCNTL_SERIALIZE\n");
179 #if APR_USE_PROC_PTHREAD_SERIALIZE
180 printf(" -D APR_USE_PROC_PTHREAD_SERIALIZE\n");
183 #if APR_USE_PTHREAD_SERIALIZE
184 printf(" -D APR_USE_PTHREAD_SERIALIZE\n");
187 #if APR_PROCESS_LOCK_IS_GLOBAL
188 printf(" -D APR_PROCESS_LOCK_IS_GLOBAL\n");
191 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
192 printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
195 #if APR_HAS_OTHER_CHILD
196 printf(" -D APR_HAS_OTHER_CHILD\n");
199 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
200 printf(" -D AP_HAVE_RELIABLE_PIPED_LOGS\n");
204 printf(" -D BUFFERED_LOGS\n");
206 printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
210 printf(" -D DYNAMIC_MODULE_LIMIT=%ld\n",(long)DYNAMIC_MODULE_LIMIT);
212 #if APR_CHARSET_EBCDIC
213 printf(" -D APR_CHARSET_EBCDIC\n");
216 #ifdef NEED_HASHBANG_EMUL
217 printf(" -D NEED_HASHBANG_EMUL\n");
220 /* This list displays the compiled in default paths: */
222 printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
226 printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
229 #ifdef DEFAULT_PIDLOG
230 printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
233 #ifdef DEFAULT_SCOREBOARD
234 printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
237 #ifdef DEFAULT_ERRORLOG
238 printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
241 #ifdef AP_TYPES_CONFIG_FILE
242 printf(" -D AP_TYPES_CONFIG_FILE=\"" AP_TYPES_CONFIG_FILE "\"\n");
245 #ifdef SERVER_CONFIG_FILE
246 printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
250 #define TASK_SWITCH_SLEEP 10000
252 static void destroy_and_exit_process(process_rec *process,
253 int process_exit_value)
256 * Sleep for TASK_SWITCH_SLEEP micro seconds to cause a task switch on
257 * OS layer and thus give possibly started piped loggers a chance to
258 * process their input. Otherwise it is possible that they get killed
259 * by us before they can do so. In this case maybe valueable log messages
262 apr_sleep(TASK_SWITCH_SLEEP);
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 *init_process(int *argc, const char * const * *argv)
281 process_rec *process;
284 const char *failed = "apr_app_initialize()";
286 stat = apr_app_initialize(argc, argv, NULL);
287 if (stat == APR_SUCCESS) {
288 failed = "apr_pool_create()";
289 stat = apr_pool_create(&cntx, NULL);
292 if (stat != APR_SUCCESS) {
293 /* For all intents and purposes, this is impossibly unlikely,
294 * but APR doesn't exist yet, we can't use it for reporting
295 * these earliest two failures;
297 * XXX: Note the apr_ctime() and apr_time_now() calls. These
298 * work, today, against an uninitialized APR, but in the future
299 * (if they relied on global pools or mutexes, for example) then
300 * the datestamp logic will need to be replaced.
302 char ctimebuff[APR_CTIME_LEN];
303 apr_ctime(ctimebuff, apr_time_now());
304 fprintf(stderr, "[%s] [crit] (%d) %s: %s failed "
305 "to initial context, exiting\n",
306 ctimebuff, stat, (*argv)[0], failed);
311 apr_pool_abort_set(abort_on_oom, cntx);
312 apr_pool_tag(cntx, "process");
313 ap_open_stderr_log(cntx);
315 /* Now we have initialized apr and our logger, no more
316 * exceptional error reporting required for the lifetime
317 * of this server process.
320 process = apr_palloc(cntx, sizeof(process_rec));
321 process->pool = cntx;
323 apr_pool_create(&process->pconf, process->pool);
324 apr_pool_tag(process->pconf, "pconf");
325 process->argc = *argc;
326 process->argv = *argv;
327 process->short_name = apr_filepath_name_get((*argv)[0]);
331 static void usage(process_rec *process)
333 const char *bin = process->argv[0];
334 char pad[MAX_STRING_LEN];
337 for (i = 0; i < strlen(bin); i++) {
343 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
344 "Usage: %s [-D name] [-d directory] [-f file]", bin);
346 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
347 " %s [-C \"directive\"] [-c \"directive\"]", pad);
350 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
351 " %s [-w] [-k start|restart|stop|shutdown] [-n service_name]", pad);
352 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
353 " %s [-k install|config|uninstall] [-n service_name]",
356 /* XXX not all MPMs support signalling the server in general or graceful-stop
359 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
360 " %s [-k start|restart|graceful|graceful-stop|stop]",
363 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
364 " %s [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S]", pad);
365 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
368 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
369 " -D name : define a name for use in "
370 "<IfDefine name> directives");
371 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
372 " -d directory : specify an alternate initial "
374 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
375 " -f file : specify an alternate ServerConfigFile");
376 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
377 " -C \"directive\" : process directive before reading "
379 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
380 " -c \"directive\" : process directive after reading "
384 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
385 " -n name : set screen name");
388 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
389 " -n name : set service name and use its "
390 "ServerConfigFile and ServerRoot");
391 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
392 " -k start : tell Apache to start");
393 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
394 " -k restart : tell running Apache to do a graceful "
396 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
397 " -k stop|shutdown : tell running Apache to shutdown");
398 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
399 " -k install : install an Apache service");
400 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
401 " -k config : change startup Options of an Apache "
403 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
404 " -k uninstall : uninstall an Apache service");
405 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
406 " -w : hold open the console window on error");
409 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
410 " -e level : show startup errors of level "
412 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
413 " -E file : log startup errors to file");
414 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
415 " -v : show version number");
416 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
417 " -V : show compile settings");
418 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
419 " -h : list available command line options "
421 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
422 " -l : list compiled in modules");
423 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
424 " -L : list available configuration "
426 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
427 " -t -D DUMP_VHOSTS : show parsed settings (currently only "
429 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
430 " -S : a synonym for -t -D DUMP_VHOSTS");
431 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
432 " -t -D DUMP_MODULES : show all loaded modules ");
433 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
434 " -M : a synonym for -t -D DUMP_MODULES");
435 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
436 " -t : run syntax check for config files");
437 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
438 " -T : start without DocumentRoot(s) check");
440 destroy_and_exit_process(process, 1);
443 int main(int argc, const char * const argv[])
446 int configtestonly = 0, showcompile = 0;
447 const char *confname = SERVER_CONFIG_FILE;
448 const char *def_server_root = HTTPD_ROOT;
449 const char *temp_error_log = NULL;
451 process_rec *process;
453 apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
454 apr_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
455 apr_pool_t *pcommands; /* Pool for -D, -C and -c switches */
460 APR_OPTIONAL_FN_TYPE(ap_signal_server) *signal_server;
462 AP_MONCONTROL(0); /* turn off profiling of startup */
464 process = init_process(&argc, &argv);
465 ap_pglobal = process->pool;
466 pconf = process->pconf;
467 ap_server_argv0 = process->short_name;
469 /* Set up the OOM callback in the global pool, so all pools should
470 * by default inherit it. */
471 apr_pool_abort_set(abort_on_oom, apr_pool_parent_get(process->pool));
473 #if APR_CHARSET_EBCDIC
474 if (ap_init_ebcdic(ap_pglobal) != APR_SUCCESS) {
475 destroy_and_exit_process(process, 1);
478 if (ap_expr_init(ap_pglobal) != APR_SUCCESS) {
479 destroy_and_exit_process(process, 1);
482 apr_pool_create(&pcommands, ap_pglobal);
483 apr_pool_tag(pcommands, "pcommands");
484 ap_server_pre_read_config = apr_array_make(pcommands, 1, sizeof(char *));
485 ap_server_post_read_config = apr_array_make(pcommands, 1, sizeof(char *));
486 ap_server_config_defines = apr_array_make(pcommands, 1, sizeof(char *));
488 error = ap_setup_prelinked_modules(process);
490 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_EMERG, 0, NULL, "%s: %s",
491 ap_server_argv0, error);
492 destroy_and_exit_process(process, 1);
495 ap_run_rewrite_args(process);
497 /* Maintain AP_SERVER_BASEARGS list in http_main.h to allow the MPM
498 * to safely pass on our args from its rewrite_args() handler.
500 apr_getopt_init(&opt, pcommands, process->argc, process->argv);
502 while ((rv = apr_getopt(opt, AP_SERVER_BASEARGS, &c, &optarg))
508 new = (char **)apr_array_push(ap_server_post_read_config);
509 *new = apr_pstrdup(pcommands, optarg);
513 new = (char **)apr_array_push(ap_server_pre_read_config);
514 *new = apr_pstrdup(pcommands, optarg);
518 def_server_root = optarg;
522 new = (char **)apr_array_push(ap_server_config_defines);
523 *new = apr_pstrdup(pcommands, optarg);
524 /* Setting -D DUMP_VHOSTS is equivalent to setting -S */
525 if (strcmp(optarg, "DUMP_VHOSTS") == 0)
527 /* Setting -D DUMP_MODULES is equivalent to setting -M */
528 if (strcmp(optarg, "DUMP_MODULES") == 0)
533 if (ap_parse_log_level(optarg, &ap_default_loglevel) != NULL)
538 temp_error_log = apr_pstrdup(process->pool, optarg);
542 new = (char **)apr_array_push(ap_server_config_defines);
551 printf("Server version: %s\n", ap_get_server_description());
552 printf("Server built: %s\n", ap_get_server_built());
553 destroy_and_exit_process(process, 0);
556 if (strcmp(ap_show_mpm(), "")) { /* MPM built-in? */
557 show_compile_settings();
558 destroy_and_exit_process(process, 0);
567 destroy_and_exit_process(process, 0);
570 ap_show_directives();
571 destroy_and_exit_process(process, 0);
578 ap_document_root_check = 0;
583 new = (char **)apr_array_push(ap_server_config_defines);
584 *new = "DUMP_VHOSTS";
589 new = (char **)apr_array_push(ap_server_config_defines);
590 *new = "DUMP_MODULES";
599 /* bad cmdline option? then we die */
600 if (rv != APR_EOF || opt->ind < opt->argc) {
604 apr_pool_create(&plog, ap_pglobal);
605 apr_pool_tag(plog, "plog");
606 apr_pool_create(&ptemp, pconf);
607 apr_pool_tag(ptemp, "ptemp");
609 /* Note that we preflight the config file once
610 * before reading it _again_ in the main loop.
611 * This allows things, log files configuration
612 * for example, to settle down.
615 ap_server_root = def_server_root;
616 if (temp_error_log) {
617 ap_replace_stderr_log(process->pool, temp_error_log);
619 ap_server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
620 if (!ap_server_conf) {
621 destroy_and_exit_process(process, 1);
624 if (ap_run_pre_config(pconf, plog, ptemp) != OK) {
625 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
626 NULL, "Pre-configuration failed");
627 destroy_and_exit_process(process, 1);
630 rv = ap_process_config_tree(ap_server_conf, ap_conftree,
631 process->pconf, ptemp);
633 ap_fixup_virtual_hosts(pconf, ap_server_conf);
634 ap_fini_vhost_config(pconf, ap_server_conf);
637 if (ap_run_check_config(pconf, plog, ptemp, ap_server_conf) != OK) {
638 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
639 NULL, "Configuration check failed");
640 destroy_and_exit_process(process, 1);
643 if (configtestonly) {
644 ap_run_test_config(pconf, ap_server_conf);
645 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "Syntax OK");
646 destroy_and_exit_process(process, 0);
648 else if (showcompile) { /* deferred due to dynamically loaded MPM */
649 show_compile_settings();
650 destroy_and_exit_process(process, 0);
654 signal_server = APR_RETRIEVE_OPTIONAL_FN(ap_signal_server);
658 if (signal_server(&exit_status, pconf) != 0) {
659 destroy_and_exit_process(process, exit_status);
663 /* If our config failed, deal with that here. */
665 destroy_and_exit_process(process, 1);
668 apr_pool_clear(plog);
670 if ( ap_run_open_logs(pconf, plog, ptemp, ap_server_conf) != OK) {
671 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
672 0, NULL, "Unable to open logs");
673 destroy_and_exit_process(process, 1);
676 if ( ap_run_post_config(pconf, plog, ptemp, ap_server_conf) != OK) {
677 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
678 NULL, "Configuration Failed");
679 destroy_and_exit_process(process, 1);
682 apr_pool_destroy(ptemp);
685 apr_hook_deregister_all();
686 apr_pool_clear(pconf);
687 ap_clear_auth_internal();
689 for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
690 ap_register_hooks(*mod, pconf);
693 /* This is a hack until we finish the code so that it only reads
694 * the config file once and just operates on the tree already in
698 apr_pool_create(&ptemp, pconf);
699 apr_pool_tag(ptemp, "ptemp");
700 ap_server_root = def_server_root;
701 ap_server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
702 if (!ap_server_conf) {
703 destroy_and_exit_process(process, 1);
706 if (ap_run_pre_config(pconf, plog, ptemp) != OK) {
707 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
708 0, NULL, "Pre-configuration failed");
709 destroy_and_exit_process(process, 1);
712 if (ap_process_config_tree(ap_server_conf, ap_conftree, process->pconf,
714 destroy_and_exit_process(process, 1);
716 ap_fixup_virtual_hosts(pconf, ap_server_conf);
717 ap_fini_vhost_config(pconf, ap_server_conf);
720 if (ap_run_check_config(pconf, plog, ptemp, ap_server_conf) != OK) {
721 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
722 NULL, "Configuration check failed");
723 destroy_and_exit_process(process, 1);
726 apr_pool_clear(plog);
727 if (ap_run_open_logs(pconf, plog, ptemp, ap_server_conf) != OK) {
728 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
729 0, NULL, "Unable to open logs");
730 destroy_and_exit_process(process, 1);
733 if (ap_run_post_config(pconf, plog, ptemp, ap_server_conf) != OK) {
734 ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
735 0, NULL, "Configuration Failed");
736 destroy_and_exit_process(process, 1);
739 apr_pool_destroy(ptemp);
740 apr_pool_lock(pconf, 1);
742 ap_run_optional_fn_retrieve();
744 if (ap_run_mpm(pconf, plog, ap_server_conf) != OK)
747 apr_pool_lock(pconf, 0);
750 apr_pool_lock(pconf, 0);
751 destroy_and_exit_process(process, 0);
753 return 0; /* Termination 'ok' */
756 #ifdef AP_USING_AUTOCONF
757 /* This ugly little hack pulls any function referenced in exports.c into
758 * the web server. exports.c is generated during the build, and it
759 * has all of the APR functions specified by the apr/apr.exports and
760 * apr-util/aprutil.exports files.
762 const void *ap_suck_in_APR(void);
763 const void *ap_suck_in_APR(void)
765 extern const void *ap_ugly_hack;