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