]> granicus.if.org Git - apache/blob - server/main.c
Add compiled and loaded PCRE version numbers
[apache] / server / main.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "apr.h"
18 #include "apr_strings.h"
19 #include "apr_getopt.h"
20 #include "apr_general.h"
21 #include "apr_lib.h"
22 #include "apr_md5.h"
23 #include "apr_time.h"
24 #include "apr_version.h"
25 #include "apu_version.h"
26
27 #define APR_WANT_STDIO
28 #define APR_WANT_STRFUNC
29 #include "apr_want.h"
30
31 #include "ap_config.h"
32 #include "httpd.h"
33 #include "http_main.h"
34 #include "http_log.h"
35 #include "http_config.h"
36 #include "http_core.h"
37 #include "mod_core.h"
38 #include "http_request.h"
39 #include "http_vhost.h"
40 #include "apr_uri.h"
41 #include "util_ebcdic.h"
42 #include "ap_mpm.h"
43
44 #if APR_HAVE_UNISTD_H
45 #include <unistd.h>
46 #else
47 /* Not sure what absence of unistd would signify for tty.  Treating it as a
48  * big NO is safe, as we then won't try to write to stderr that's not a tty.
49  */
50 #define isatty(n) (0)
51 #endif
52
53 /* we know core's module_index is 0 */
54 #undef APLOG_MODULE_INDEX
55 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
56
57 /* WARNING: Win32 binds http_main.c dynamically to the server. Please place
58  *          extern functions and global data in another appropriate module.
59  *
60  * Most significant main() global data can be found in http_config.c
61  */
62
63 static void show_mpm_settings(void)
64 {
65     int mpm_query_info;
66     apr_status_t retval;
67
68     printf("Server MPM:     %s\n", ap_show_mpm());
69
70     retval = ap_mpm_query(AP_MPMQ_IS_THREADED, &mpm_query_info);
71
72     if (retval == APR_SUCCESS) {
73         printf("  threaded:     ");
74
75         if (mpm_query_info == AP_MPMQ_DYNAMIC) {
76             printf("yes (variable thread count)\n");
77         }
78         else if (mpm_query_info == AP_MPMQ_STATIC) {
79             printf("yes (fixed thread count)\n");
80         }
81         else {
82             printf("no\n");
83         }
84     }
85
86     retval = ap_mpm_query(AP_MPMQ_IS_FORKED, &mpm_query_info);
87
88     if (retval == APR_SUCCESS) {
89         printf("    forked:     ");
90
91         if (mpm_query_info == AP_MPMQ_DYNAMIC) {
92             printf("yes (variable process count)\n");
93         }
94         else if (mpm_query_info == AP_MPMQ_STATIC) {
95             printf("yes (fixed process count)\n");
96         }
97         else {
98             printf("no\n");
99         }
100     }
101 }
102
103 static void show_compile_settings(void)
104 {
105     printf("Server version: %s\n", ap_get_server_description());
106     printf("Server built:   %s\n", ap_get_server_built());
107     printf("Server's Module Magic Number: %u:%u\n",
108            MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
109 #if APR_MAJOR_VERSION >= 2
110     printf("Server loaded:  APR %s, PCRE %s\n",
111            apr_version_string(), ap_pcre_version_string(AP_REG_PCRE_LOADED));
112     printf("Compiled using: APR %s, PCRE %s\n",
113            APR_VERSION_STRING, ap_pcre_version_string(AP_REG_PCRE_COMPILED));
114 #else
115     printf("Server loaded:  APR %s, APR-UTIL %s, PCRE %s\n",
116            apr_version_string(), apu_version_string(),
117            ap_pcre_version_string(AP_REG_PCRE_LOADED));
118     printf("Compiled using: APR %s, APR-UTIL %s, PCRE %s\n",
119            APR_VERSION_STRING, APU_VERSION_STRING,
120            ap_pcre_version_string(AP_REG_PCRE_COMPILED));
121 #endif
122     /* sizeof(foo) is long on some platforms so we might as well
123      * make it long everywhere to keep the printf format
124      * consistent
125      */
126     printf("Architecture:   %ld-bit\n", 8 * (long)sizeof(void *));
127
128     show_mpm_settings();
129
130     printf("Server compiled with....\n");
131 #ifdef BIG_SECURITY_HOLE
132     printf(" -D BIG_SECURITY_HOLE\n");
133 #endif
134
135 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
136     printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
137 #endif
138
139 #ifdef OS
140     printf(" -D OS=\"" OS "\"\n");
141 #endif
142
143 #ifdef HAVE_SHMGET
144     printf(" -D HAVE_SHMGET\n");
145 #endif
146
147 #if APR_FILE_BASED_SHM
148     printf(" -D APR_FILE_BASED_SHM\n");
149 #endif
150
151 #if APR_HAS_SENDFILE
152     printf(" -D APR_HAS_SENDFILE\n");
153 #endif
154
155 #if APR_HAS_MMAP
156     printf(" -D APR_HAS_MMAP\n");
157 #endif
158
159 #ifdef NO_WRITEV
160     printf(" -D NO_WRITEV\n");
161 #endif
162
163 #ifdef NO_LINGCLOSE
164     printf(" -D NO_LINGCLOSE\n");
165 #endif
166
167 #if APR_HAVE_IPV6
168     printf(" -D APR_HAVE_IPV6 (IPv4-mapped addresses ");
169 #ifdef AP_ENABLE_V4_MAPPED
170     printf("enabled)\n");
171 #else
172     printf("disabled)\n");
173 #endif
174 #endif
175
176 #if APR_USE_FLOCK_SERIALIZE
177     printf(" -D APR_USE_FLOCK_SERIALIZE\n");
178 #endif
179
180 #if APR_USE_SYSVSEM_SERIALIZE
181     printf(" -D APR_USE_SYSVSEM_SERIALIZE\n");
182 #endif
183
184 #if APR_USE_POSIXSEM_SERIALIZE
185     printf(" -D APR_USE_POSIXSEM_SERIALIZE\n");
186 #endif
187
188 #if APR_USE_FCNTL_SERIALIZE
189     printf(" -D APR_USE_FCNTL_SERIALIZE\n");
190 #endif
191
192 #if APR_USE_PROC_PTHREAD_SERIALIZE
193     printf(" -D APR_USE_PROC_PTHREAD_SERIALIZE\n");
194 #endif
195
196 #if APR_USE_PTHREAD_SERIALIZE
197     printf(" -D APR_USE_PTHREAD_SERIALIZE\n");
198 #endif
199
200 #if APR_PROCESS_LOCK_IS_GLOBAL
201     printf(" -D APR_PROCESS_LOCK_IS_GLOBAL\n");
202 #endif
203
204 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
205     printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
206 #endif
207
208 #if APR_HAS_OTHER_CHILD
209     printf(" -D APR_HAS_OTHER_CHILD\n");
210 #endif
211
212 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
213     printf(" -D AP_HAVE_RELIABLE_PIPED_LOGS\n");
214 #endif
215
216 #ifdef BUFFERED_LOGS
217     printf(" -D BUFFERED_LOGS\n");
218 #ifdef PIPE_BUF
219     printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
220 #endif
221 #endif
222
223     printf(" -D DYNAMIC_MODULE_LIMIT=%ld\n",(long)DYNAMIC_MODULE_LIMIT);
224
225 #if APR_CHARSET_EBCDIC
226     printf(" -D APR_CHARSET_EBCDIC\n");
227 #endif
228
229 #ifdef NEED_HASHBANG_EMUL
230     printf(" -D NEED_HASHBANG_EMUL\n");
231 #endif
232
233 /* This list displays the compiled in default paths: */
234 #ifdef HTTPD_ROOT
235     printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
236 #endif
237
238 #ifdef SUEXEC_BIN
239     printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
240 #endif
241
242 #ifdef DEFAULT_PIDLOG
243     printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
244 #endif
245
246 #ifdef DEFAULT_SCOREBOARD
247     printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
248 #endif
249
250 #ifdef DEFAULT_ERRORLOG
251     printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
252 #endif
253
254 #ifdef AP_TYPES_CONFIG_FILE
255     printf(" -D AP_TYPES_CONFIG_FILE=\"" AP_TYPES_CONFIG_FILE "\"\n");
256 #endif
257
258 #ifdef SERVER_CONFIG_FILE
259     printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
260 #endif
261 }
262
263 #define TASK_SWITCH_SLEEP 10000
264
265 static void destroy_and_exit_process(process_rec *process,
266                                      int process_exit_value)
267 {
268     /*
269      * Sleep for TASK_SWITCH_SLEEP micro seconds to cause a task switch on
270      * OS layer and thus give possibly started piped loggers a chance to
271      * process their input. Otherwise it is possible that they get killed
272      * by us before they can do so. In this case maybe valueable log messages
273      * might get lost.
274      */
275
276     /* If we are to print an error, we need the name before we destroy pool.
277      * short_name is a pointer into argv, so remains valid.
278      */
279     const char *name = process->short_name ? process->short_name : "httpd";
280
281     apr_sleep(TASK_SWITCH_SLEEP);
282     ap_main_state = AP_SQ_MS_EXITING;
283     apr_pool_destroy(process->pool); /* and destroy all descendent pools */
284     apr_terminate();
285     if ((process_exit_value != 0) && isatty(fileno(stderr))) {
286         fprintf(stderr, "%s: abnormal exit %d\n", name, process_exit_value);
287     }
288     exit(process_exit_value);
289 }
290
291 /* APR callback invoked if allocation fails. */
292 static int abort_on_oom(int retcode)
293 {
294     ap_abort_on_oom();
295     return retcode; /* unreachable, hopefully. */
296 }
297
298 static process_rec *init_process(int *argc, const char * const * *argv)
299 {
300     process_rec *process;
301     apr_pool_t *cntx;
302     apr_status_t stat;
303     const char *failed = "apr_app_initialize()";
304
305     stat = apr_app_initialize(argc, argv, NULL);
306     if (stat == APR_SUCCESS) {
307         failed = "apr_pool_create()";
308         stat = apr_pool_create(&cntx, NULL);
309     }
310
311     if (stat != APR_SUCCESS) {
312         /* For all intents and purposes, this is impossibly unlikely,
313          * but APR doesn't exist yet, we can't use it for reporting
314          * these earliest two failures;
315          *
316          * XXX: Note the apr_ctime() and apr_time_now() calls.  These
317          * work, today, against an uninitialized APR, but in the future
318          * (if they relied on global pools or mutexes, for example) then
319          * the datestamp logic will need to be replaced.
320          */
321         char ctimebuff[APR_CTIME_LEN];
322         apr_ctime(ctimebuff, apr_time_now());
323         fprintf(stderr, "[%s] [crit] (%d) %s: %s failed "
324                         "to initial context, exiting\n",
325                         ctimebuff, stat, (*argv)[0], failed);
326         apr_terminate();
327         exit(1);
328     }
329
330     apr_pool_abort_set(abort_on_oom, cntx);
331     apr_pool_tag(cntx, "process");
332     ap_open_stderr_log(cntx);
333
334     /* Now we have initialized apr and our logger, no more
335      * exceptional error reporting required for the lifetime
336      * of this server process.
337      */
338
339     process = apr_palloc(cntx, sizeof(process_rec));
340     process->pool = cntx;
341
342     apr_pool_create(&process->pconf, process->pool);
343     apr_pool_tag(process->pconf, "pconf");
344     process->argc = *argc;
345     process->argv = *argv;
346     process->short_name = apr_filepath_name_get((*argv)[0]);
347     return process;
348 }
349
350 static void usage(process_rec *process)
351 {
352     const char *bin = process->argv[0];
353     int pad_len = strlen(bin);
354
355     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
356                  "Usage: %s [-D name] [-d directory] [-f file]", bin);
357
358     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
359                  "       %*s [-C \"directive\"] [-c \"directive\"]", pad_len, " ");
360
361 #ifdef WIN32
362     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
363                  "       %*s [-w] [-k start|restart|stop|shutdown] [-n service_name]",
364                  pad_len, " ");
365     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
366                  "       %*s [-k install|config|uninstall] [-n service_name]",
367                  pad_len, " ");
368 #else
369 /* XXX not all MPMs support signalling the server in general or graceful-stop
370  * in particular
371  */
372     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
373                  "       %*s [-k start|restart|graceful|graceful-stop|stop]",
374                  pad_len, " ");
375 #endif
376     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
377                  "       %*s [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]",
378                  pad_len, " ");
379     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
380                  "Options:");
381
382     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
383                  "  -D name            : define a name for use in "
384                  "<IfDefine name> directives");
385     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
386                  "  -d directory       : specify an alternate initial "
387                  "ServerRoot");
388     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
389                  "  -f file            : specify an alternate ServerConfigFile");
390     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
391                  "  -C \"directive\"     : process directive before reading "
392                  "config files");
393     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
394                  "  -c \"directive\"     : process directive after reading "
395                  "config files");
396
397 #ifdef NETWARE
398     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
399                  "  -n name            : set screen name");
400 #endif
401 #ifdef WIN32
402     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
403                  "  -n name            : set service name and use its "
404                  "ServerConfigFile and ServerRoot");
405     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
406                  "  -k start           : tell Apache to start");
407     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
408                  "  -k restart         : tell running Apache to do a graceful "
409                  "restart");
410     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
411                  "  -k stop|shutdown   : tell running Apache to shutdown");
412     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
413                  "  -k install         : install an Apache service");
414     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
415                  "  -k config          : change startup Options of an Apache "
416                  "service");
417     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
418                  "  -k uninstall       : uninstall an Apache service");
419     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
420                  "  -w                 : hold open the console window on error");
421 #endif
422
423     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
424                  "  -e level           : show startup errors of level "
425                  "(see LogLevel)");
426     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
427                  "  -E file            : log startup errors to file");
428     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
429                  "  -v                 : show version number");
430     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
431                  "  -V                 : show compile settings");
432     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
433                  "  -h                 : list available command line options "
434                  "(this page)");
435     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
436                  "  -l                 : list compiled in modules");
437     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
438                  "  -L                 : list available configuration "
439                  "directives");
440     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
441                  "  -t -D DUMP_VHOSTS  : show parsed vhost settings");
442     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
443                  "  -t -D DUMP_RUN_CFG : show parsed run settings");
444     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
445                  "  -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG");
446     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
447                  "  -t -D DUMP_MODULES : show all loaded modules ");
448     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
449                  "  -M                 : a synonym for -t -D DUMP_MODULES");
450     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
451                  "  -t                 : run syntax check for config files");
452     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
453                  "  -T                 : start without DocumentRoot(s) check");
454     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
455                  "  -X                 : debug mode (only one worker, do not detach)");
456
457     destroy_and_exit_process(process, 1);
458 }
459
460 int main(int argc, const char * const argv[])
461 {
462     char c;
463     int showcompile = 0, showdirectives = 0;
464     const char *confname = SERVER_CONFIG_FILE;
465     const char *def_server_root = HTTPD_ROOT;
466     const char *temp_error_log = NULL;
467     const char *error;
468     process_rec *process;
469     apr_pool_t *pconf;
470     apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
471     apr_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
472     apr_pool_t *pcommands; /* Pool for -D, -C and -c switches */
473     apr_getopt_t *opt;
474     apr_status_t rv;
475     module **mod;
476     const char *opt_arg;
477     APR_OPTIONAL_FN_TYPE(ap_signal_server) *signal_server;
478
479     AP_MONCONTROL(0); /* turn off profiling of startup */
480
481     process = init_process(&argc, &argv);
482     ap_pglobal = process->pool;
483     pconf = process->pconf;
484     ap_server_argv0 = process->short_name;
485     ap_init_rng(ap_pglobal);
486
487     /* Set up the OOM callback in the global pool, so all pools should
488      * by default inherit it. */
489     apr_pool_abort_set(abort_on_oom, apr_pool_parent_get(process->pool));
490
491 #if APR_CHARSET_EBCDIC
492     if (ap_init_ebcdic(ap_pglobal) != APR_SUCCESS) {
493         destroy_and_exit_process(process, 1);
494     }
495 #endif
496
497     apr_pool_create(&pcommands, ap_pglobal);
498     apr_pool_tag(pcommands, "pcommands");
499     ap_server_pre_read_config  = apr_array_make(pcommands, 1, sizeof(char *));
500     ap_server_post_read_config = apr_array_make(pcommands, 1, sizeof(char *));
501     ap_server_config_defines   = apr_array_make(pcommands, 1, sizeof(char *));
502
503     error = ap_setup_prelinked_modules(process);
504     if (error) {
505         ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_EMERG, 0, NULL, APLOGNO(00012)
506                      "%s: %s", ap_server_argv0, error);
507         destroy_and_exit_process(process, 1);
508     }
509
510     ap_run_rewrite_args(process);
511
512     /* Maintain AP_SERVER_BASEARGS list in http_main.h to allow the MPM
513      * to safely pass on our args from its rewrite_args() handler.
514      */
515     apr_getopt_init(&opt, pcommands, process->argc, process->argv);
516
517     while ((rv = apr_getopt(opt, AP_SERVER_BASEARGS, &c, &opt_arg))
518             == APR_SUCCESS) {
519         char **new;
520
521         switch (c) {
522         case 'c':
523             new = (char **)apr_array_push(ap_server_post_read_config);
524             *new = apr_pstrdup(pcommands, opt_arg);
525             break;
526
527         case 'C':
528             new = (char **)apr_array_push(ap_server_pre_read_config);
529             *new = apr_pstrdup(pcommands, opt_arg);
530             break;
531
532         case 'd':
533             def_server_root = opt_arg;
534             break;
535
536         case 'D':
537             new = (char **)apr_array_push(ap_server_config_defines);
538             *new = apr_pstrdup(pcommands, opt_arg);
539             /* Setting -D DUMP_VHOSTS should work like setting -S */
540             if (strcmp(opt_arg, "DUMP_VHOSTS") == 0)
541                 ap_run_mode = AP_SQ_RM_CONFIG_DUMP;
542             /* Setting -D DUMP_RUN_CFG should work like setting -S */
543             else if (strcmp(opt_arg, "DUMP_RUN_CFG") == 0)
544                 ap_run_mode = AP_SQ_RM_CONFIG_DUMP;
545             /* Setting -D DUMP_MODULES is equivalent to setting -M */
546             else if (strcmp(opt_arg, "DUMP_MODULES") == 0)
547                 ap_run_mode = AP_SQ_RM_CONFIG_DUMP;
548             break;
549
550         case 'e':
551             if (ap_parse_log_level(opt_arg, &ap_default_loglevel) != NULL)
552                 usage(process);
553             break;
554
555         case 'E':
556             temp_error_log = apr_pstrdup(process->pool, opt_arg);
557             break;
558
559         case 'X':
560             new = (char **)apr_array_push(ap_server_config_defines);
561             *new = "DEBUG";
562             break;
563
564         case 'f':
565             confname = opt_arg;
566             break;
567
568         case 'v':
569             printf("Server version: %s\n", ap_get_server_description());
570             printf("Server built:   %s\n", ap_get_server_built());
571             destroy_and_exit_process(process, 0);
572
573         case 'l':
574             ap_show_modules();
575             destroy_and_exit_process(process, 0);
576
577         case 'L':
578             ap_run_mode = AP_SQ_RM_CONFIG_DUMP;
579             showdirectives = 1;
580             break;
581
582         case 't':
583             if (ap_run_mode == AP_SQ_RM_UNKNOWN)
584                 ap_run_mode = AP_SQ_RM_CONFIG_TEST;
585             break;
586
587        case 'T':
588            ap_document_root_check = 0;
589            break;
590
591         case 'S':
592             ap_run_mode = AP_SQ_RM_CONFIG_DUMP;
593             new = (char **)apr_array_push(ap_server_config_defines);
594             *new = "DUMP_VHOSTS";
595             new = (char **)apr_array_push(ap_server_config_defines);
596             *new = "DUMP_RUN_CFG";
597             break;
598
599         case 'M':
600             ap_run_mode = AP_SQ_RM_CONFIG_DUMP;
601             new = (char **)apr_array_push(ap_server_config_defines);
602             *new = "DUMP_MODULES";
603             break;
604
605         case 'V':
606             if (strcmp(ap_show_mpm(), "")) { /* MPM built-in? */
607                 show_compile_settings();
608                 destroy_and_exit_process(process, 0);
609             }
610             else {
611                 showcompile = 1;
612                 ap_run_mode = AP_SQ_RM_CONFIG_DUMP;
613             }
614             break;
615
616         case 'h':
617         case '?':
618             usage(process);
619         }
620     }
621
622     if (ap_run_mode == AP_SQ_RM_UNKNOWN)
623         ap_run_mode = AP_SQ_RM_NORMAL;
624
625     /* bad cmdline option?  then we die */
626     if (rv != APR_EOF || opt->ind < opt->argc) {
627         usage(process);
628     }
629
630     ap_main_state = AP_SQ_MS_CREATE_PRE_CONFIG;
631     apr_pool_create(&plog, ap_pglobal);
632     apr_pool_tag(plog, "plog");
633     apr_pool_create(&ptemp, pconf);
634     apr_pool_tag(ptemp, "ptemp");
635
636     /* Note that we preflight the config file once
637      * before reading it _again_ in the main loop.
638      * This allows things, log files configuration
639      * for example, to settle down.
640      */
641
642     ap_server_root = def_server_root;
643     if (temp_error_log) {
644         ap_replace_stderr_log(process->pool, temp_error_log);
645     }
646     ap_server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
647     if (!ap_server_conf) {
648         destroy_and_exit_process(process, 1);
649     }
650     apr_pool_cleanup_register(pconf, &ap_server_conf, ap_pool_cleanup_set_null,
651                               apr_pool_cleanup_null);
652     /* sort hooks here to make sure pre_config hooks are sorted properly */
653     apr_hook_sort_all();
654
655     if (ap_run_pre_config(pconf, plog, ptemp) != OK) {
656         ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
657                      NULL, APLOGNO(00013) "Pre-configuration failed");
658         destroy_and_exit_process(process, 1);
659     }
660
661     rv = ap_process_config_tree(ap_server_conf, ap_conftree,
662                                 process->pconf, ptemp);
663     if (rv == OK) {
664         ap_fixup_virtual_hosts(pconf, ap_server_conf);
665         ap_fini_vhost_config(pconf, ap_server_conf);
666         /*
667          * Sort hooks again because ap_process_config_tree may have add modules
668          * and hence hooks. This happens with mod_perl and modules written in
669          * perl.
670          */
671         apr_hook_sort_all();
672
673         if (ap_run_check_config(pconf, plog, ptemp, ap_server_conf) != OK) {
674             ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
675                          NULL, APLOGNO(00014) "Configuration check failed");
676             destroy_and_exit_process(process, 1);
677         }
678
679         if (ap_run_mode != AP_SQ_RM_NORMAL) {
680             if (showcompile) { /* deferred due to dynamically loaded MPM */
681                 show_compile_settings();
682             }
683             else if (showdirectives) { /* deferred in case of DSOs */
684                 ap_show_directives();
685                 destroy_and_exit_process(process, 0);
686             }
687             else {
688                 ap_run_test_config(pconf, ap_server_conf);
689                 if (ap_run_mode == AP_SQ_RM_CONFIG_TEST)
690                     ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "Syntax OK");
691             }
692             destroy_and_exit_process(process, 0);
693         }
694     }
695
696     /* If our config failed, deal with that here. */
697     if (rv != OK) {
698         destroy_and_exit_process(process, 1);
699     }
700
701     signal_server = APR_RETRIEVE_OPTIONAL_FN(ap_signal_server);
702     if (signal_server) {
703         int exit_status;
704
705         if (signal_server(&exit_status, pconf) != 0) {
706             destroy_and_exit_process(process, exit_status);
707         }
708     }
709
710     apr_pool_clear(plog);
711
712     if ( ap_run_open_logs(pconf, plog, ptemp, ap_server_conf) != OK) {
713         ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR,
714                      0, NULL, APLOGNO(00015) "Unable to open logs");
715         destroy_and_exit_process(process, 1);
716     }
717
718     if ( ap_run_post_config(pconf, plog, ptemp, ap_server_conf) != OK) {
719         ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0,
720                      NULL, APLOGNO(00016) "Configuration Failed");
721         destroy_and_exit_process(process, 1);
722     }
723
724     apr_pool_destroy(ptemp);
725
726     for (;;) {
727         ap_main_state = AP_SQ_MS_DESTROY_CONFIG;
728         apr_hook_deregister_all();
729         apr_pool_clear(pconf);
730         ap_clear_auth_internal();
731
732         ap_main_state = AP_SQ_MS_CREATE_CONFIG;
733         ap_config_generation++;
734         for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
735             ap_register_hooks(*mod, pconf);
736         }
737
738         /* This is a hack until we finish the code so that it only reads
739          * the config file once and just operates on the tree already in
740          * memory.  rbb
741          */
742         ap_conftree = NULL;
743         apr_pool_create(&ptemp, pconf);
744         apr_pool_tag(ptemp, "ptemp");
745         ap_server_root = def_server_root;
746         ap_server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
747         if (!ap_server_conf) {
748             destroy_and_exit_process(process, 1);
749         }
750         apr_pool_cleanup_register(pconf, &ap_server_conf,
751                                   ap_pool_cleanup_set_null, apr_pool_cleanup_null);
752         /* sort hooks here to make sure pre_config hooks are sorted properly */
753         apr_hook_sort_all();
754
755         if (ap_run_pre_config(pconf, plog, ptemp) != OK) {
756             ap_log_error(APLOG_MARK, APLOG_EMERG, 0, NULL,
757                          APLOGNO(00017) "Pre-configuration failed, exiting");
758             destroy_and_exit_process(process, 1);
759         }
760
761         if (ap_process_config_tree(ap_server_conf, ap_conftree, process->pconf,
762                                    ptemp) != OK) {
763             destroy_and_exit_process(process, 1);
764         }
765         ap_fixup_virtual_hosts(pconf, ap_server_conf);
766         ap_fini_vhost_config(pconf, ap_server_conf);
767         /*
768          * Sort hooks again because ap_process_config_tree may have add modules
769          * and hence hooks. This happens with mod_perl and modules written in
770          * perl.
771          */
772         apr_hook_sort_all();
773
774         if (ap_run_check_config(pconf, plog, ptemp, ap_server_conf) != OK) {
775             ap_log_error(APLOG_MARK, APLOG_EMERG, 0, NULL,
776                          APLOGNO(00018) "Configuration check failed, exiting");
777             destroy_and_exit_process(process, 1);
778         }
779
780         apr_pool_clear(plog);
781         if (ap_run_open_logs(pconf, plog, ptemp, ap_server_conf) != OK) {
782             ap_log_error(APLOG_MARK, APLOG_EMERG, 0, NULL,
783                          APLOGNO(00019) "Unable to open logs, exiting");
784             destroy_and_exit_process(process, 1);
785         }
786
787         if (ap_run_post_config(pconf, plog, ptemp, ap_server_conf) != OK) {
788             ap_log_error(APLOG_MARK, APLOG_EMERG, 0, NULL,
789                          APLOGNO(00020) "Configuration Failed, exiting");
790             destroy_and_exit_process(process, 1);
791         }
792
793         apr_pool_destroy(ptemp);
794         apr_pool_lock(pconf, 1);
795
796         ap_run_optional_fn_retrieve();
797
798         ap_main_state = AP_SQ_MS_RUN_MPM;
799         if (ap_run_mpm(pconf, plog, ap_server_conf) != OK)
800             break;
801
802         apr_pool_lock(pconf, 0);
803     }
804
805     apr_pool_lock(pconf, 0);
806     destroy_and_exit_process(process, 0);
807
808     return 0; /* Termination 'ok' */
809 }
810
811 #ifdef AP_USING_AUTOCONF
812 /* This ugly little hack pulls any function referenced in exports.c into
813  * the web server.  exports.c is generated during the build, and it
814  * has all of the APR functions specified by the apr/apr.exports and
815  * apr-util/aprutil.exports files.
816  */
817 const void *ap_suck_in_APR(void);
818 const void *ap_suck_in_APR(void)
819 {
820     extern const void *ap_ugly_hack;
821
822     return ap_ugly_hack;
823 }
824 #endif