]> granicus.if.org Git - apache/blob - server/main.c
Clear hook registrations between reads of the config file.
[apache] / server / main.c
1 /* ====================================================================
2  * Copyright (c) 1995-2000 The Apache Software Foundation.  All rights reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without 
5  * modification, are permitted provided that the following conditions 
6  * are met: 
7  * 
8  * 1. Redistributions of source code must retain the above copyright 
9  *    notice, this list of conditions and the following disclaimer.  
10  * 
11  * 2. Redistributions in binary form must reproduce the above copyright 
12  *    notice, this list of conditions and the following disclaimer in 
13  *    the documentation and/or other materials provided with the 
14  *    distribution. 
15  * 
16  * 3. All advertising materials mentioning features or use of this 
17  *    software must display the following acknowledgment: 
18  *    "This product includes software developed by the Apache Software Foundation 
19  *    for use in the Apache HTTP server project (http://www.apache.org/)." 
20  * 
21  * 4. The names "Apache Server" and "Apache Software Foundation" must not be used to 
22  *    endorse or promote products derived from this software without 
23  *    prior written permission. For written permission, please contact 
24  *    apache@apache.org. 
25  * 
26  * 5. Products derived from this software may not be called "Apache" 
27  *    nor may "Apache" appear in their names without prior written 
28  *    permission of the Apache Software Foundation. 
29  * 
30  * 6. Redistributions of any form whatsoever must retain the following 
31  *    acknowledgment: 
32  *    "This product includes software developed by the Apache Software Foundation 
33  *    for use in the Apache HTTP server project (http://www.apache.org/)." 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE Apache Software Foundation ``AS IS'' AND ANY 
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE Apache Software Foundation OR 
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
46  * OF THE POSSIBILITY OF SUCH DAMAGE. 
47  * ==================================================================== 
48  * 
49  * This software consists of voluntary contributions made by many 
50  * individuals on behalf of the Apache Software Foundation and was originally based 
51  * on public domain software written at the National Center for 
52  * Supercomputing Applications, University of Illinois, Urbana-Champaign. 
53  * For more information on the Apache Software Foundation and the Apache HTTP server 
54  * project, please see <http://www.apache.org/>. 
55  * 
56  */ 
57
58 #define CORE_PRIVATE
59 #include "ap_config.h"
60 #include "httpd.h" 
61 #include "http_main.h" 
62 #include "http_log.h" 
63 #include "http_config.h"
64 #include "util_uri.h" 
65 #include "apr_getopt.h"
66 #include "ap_mpm.h"
67
68 const char *ap_server_argv0;
69
70 const char *ap_server_root;
71
72 ap_array_header_t *ap_server_pre_read_config;
73 ap_array_header_t *ap_server_post_read_config;
74 ap_array_header_t *ap_server_config_defines;
75
76 /* XXX - We should be able to grab the per-MPM settings here too */
77 static void show_compile_settings(void)
78 {
79     printf("Server version: %s\n", ap_get_server_version());
80     printf("Server built:   %s\n", ap_get_server_built());
81     printf("Server's Module Magic Number: %u:%u\n",
82            MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
83     printf("Server compiled with....\n");
84 #ifdef BIG_SECURITY_HOLE
85     printf(" -D BIG_SECURITY_HOLE\n");
86 #endif
87 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
88     printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
89 #endif
90 #ifdef HAVE_MMAP
91     printf(" -D HAVE_MMAP\n");
92 #endif
93 #ifdef HAVE_SHMGET
94     printf(" -D HAVE_SHMGET\n");
95 #endif
96 #ifdef USE_MMAP_SCOREBOARD
97     printf(" -D USE_MMAP_SCOREBOARD\n");
98 #endif
99 #ifdef USE_SHMGET_SCOREBOARD
100     printf(" -D USE_SHMGET_SCOREBOARD\n");
101 #endif
102 #ifdef USE_OS2_SCOREBOARD
103     printf(" -D USE_OS2_SCOREBOARD\n");
104 #endif
105 #ifdef USE_POSIX_SCOREBOARD
106     printf(" -D USE_POSIX_SCOREBOARD\n");
107 #endif
108 #ifdef USE_MMAP_FILES
109     printf(" -D USE_MMAP_FILES\n");
110 #ifdef MMAP_SEGMENT_SIZE
111         printf(" -D MMAP_SEGMENT_SIZE=%ld\n",(long)MMAP_SEGMENT_SIZE);
112 #endif
113 #endif /*USE_MMAP_FILES*/
114 #ifdef NO_WRITEV
115     printf(" -D NO_WRITEV\n");
116 #endif
117 #ifdef NO_LINGCLOSE
118     printf(" -D NO_LINGCLOSE\n");
119 #endif
120 #ifdef USE_FCNTL_SERIALIZED_ACCEPT
121     printf(" -D USE_FCNTL_SERIALIZED_ACCEPT\n");
122 #endif
123 #ifdef USE_FLOCK_SERIALIZED_ACCEPT
124     printf(" -D USE_FLOCK_SERIALIZED_ACCEPT\n");
125 #endif
126 #ifdef USE_USLOCK_SERIALIZED_ACCEPT
127     printf(" -D USE_USLOCK_SERIALIZED_ACCEPT\n");
128 #endif
129 #ifdef USE_SYSVSEM_SERIALIZED_ACCEPT
130     printf(" -D USE_SYSVSEM_SERIALIZED_ACCEPT\n");
131 #endif
132 #ifdef USE_PTHREAD_SERIALIZED_ACCEPT
133     printf(" -D USE_PTHREAD_SERIALIZED_ACCEPT\n");
134 #endif
135 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
136     printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
137 #endif
138 #ifdef HAS_OTHER_CHILD
139     printf(" -D HAS_OTHER_CHILD\n");
140 #endif
141 #ifdef HAVE_RELIABLE_PIPED_LOGS
142     printf(" -D HAVE_RELIABLE_PIPED_LOGS\n");
143 #endif
144 #ifdef BUFFERED_LOGS
145     printf(" -D BUFFERED_LOGS\n");
146 #ifdef PIPE_BUF
147         printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
148 #endif
149 #endif
150 #ifdef CHARSET_EBCDIC
151     printf(" -D CHARSET_EBCDIC\n");
152 #endif
153 #ifdef NEED_HASHBANG_EMUL
154     printf(" -D NEED_HASHBANG_EMUL\n");
155 #endif
156 #ifdef SHARED_CORE
157     printf(" -D SHARED_CORE\n");
158 #endif
159
160 /* This list displays the compiled in default paths: */
161 #ifdef HTTPD_ROOT
162     printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
163 #endif
164 #ifdef SUEXEC_BIN
165     printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
166 #endif
167 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
168     printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
169 #endif
170 #ifdef DEFAULT_PIDLOG
171     printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
172 #endif
173 #ifdef DEFAULT_SCOREBOARD
174     printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
175 #endif
176 #ifdef DEFAULT_LOCKFILE
177     printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
178 #endif
179 #ifdef DEFAULT_XFERLOG
180     printf(" -D DEFAULT_XFERLOG=\"" DEFAULT_XFERLOG "\"\n");
181 #endif
182 #ifdef DEFAULT_ERRORLOG
183     printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
184 #endif
185 #ifdef TYPES_CONFIG_FILE
186     printf(" -D TYPES_CONFIG_FILE=\"" TYPES_CONFIG_FILE "\"\n");
187 #endif
188 #ifdef SERVER_CONFIG_FILE
189     printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
190 #endif
191 }
192
193 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
194 {
195     ap_destroy_pool(process->pool); /* and destroy all descendent pools */
196     exit(process_exit_value);
197 }
198
199 static process_rec *create_process(int argc, const char **argv)
200 {
201     process_rec *process;
202     
203     {
204         ap_context_t *cntx;
205         ap_status_t stat;
206
207         stat = ap_create_context(&cntx, NULL);
208         if (stat != APR_SUCCESS) {
209             ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
210                          "ap_create_context() failed to create "
211                          "initial context");
212             exit(1);
213         }
214
215         process = ap_palloc(cntx, sizeof(process_rec));
216         process->pool = cntx;
217     }
218     ap_create_context(&process->pconf, process->pool);
219     process->argc = argc;
220     process->argv = argv;
221     process->short_name = ap_filename_of_pathname(argv[0]);
222     return process;
223 }
224
225 static void usage(process_rec *process)
226 {
227     const char *bin = process->argv[0];
228     char pad[MAX_STRING_LEN];
229     unsigned i;
230
231     for (i = 0; i < strlen(bin); i++)
232         pad[i] = ' ';
233     pad[i] = '\0';
234 #ifdef SHARED_CORE
235     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
236 #else
237     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
238 #endif
239     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-C \"directive\"] [-c \"directive\"]", pad);
240 #ifdef WIN32
241     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-k restart|shutdown|start]", pad);
242     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-n service_name]", pad);
243     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-i] [-u]", pad);
244 #endif
245     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-v] [-V] [-h] [-l] [-L] [-S] [-t] [-T]", pad);
246     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:");
247 #ifdef SHARED_CORE
248     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -R directory     : specify an alternate location for shared object files");
249 #endif
250     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -D name          : define a name for use in <IfDefine name> directives");
251     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -d directory     : specify an alternate initial ServerRoot");
252     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -f file          : specify an alternate ServerConfigFile");
253     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -C \"directive\"   : process directive before reading config files");
254     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -c \"directive\"   : process directive after  reading config files");
255 #ifdef WIN32
256     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -n name          : set service name and use its ServerConfigFile");
257     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k shutdown      : tell running Apache to shutdown");
258     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k restart       : tell running Apache to do a graceful restart");
259     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k start         : tell Apache to start");
260     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -i               : install an Apache service");
261     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -u               : uninstall an Apache service");
262 #endif
263     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -v               : show version number");
264     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -V               : show compile settings");
265     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -h               : list available command line options (this page)");
266     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -l               : list compiled in modules");
267     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -L               : list available configuration directives");
268     /* TODOC: -S has been replaced by '-t -D DUMP_VHOSTS' */
269     /* ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -S               : show parsed settings (currently only vhost settings)"); */
270     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t               : run syntax check for config files (with docroot check)");
271     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -T               : run syntax check for config files (without docroot check)");
272     /* TODOC: -X goes away, expect MPMs to use -D options */
273     destroy_and_exit_process(process, 1);
274 }
275
276
277
278
279
280 ap_context_t *g_pHookPool;
281
282 #ifdef WIN32
283 API_EXPORT_NONSTD(int) apache_main(int argc, char *argv[])
284 #else
285 API_EXPORT_NONSTD(int)        main(int argc, char *argv[])
286 #endif
287 {
288     int c;
289     int configtestonly = 0;
290     const char *confname = SERVER_CONFIG_FILE;
291     const char *def_server_root = HTTPD_ROOT;
292     process_rec *process;
293     server_rec *server_conf;
294     ap_context_t *pglobal;
295     ap_context_t *pconf;
296     ap_context_t *plog; /* Pool of log streams, reset _after_ each read of conf */
297     ap_context_t *ptemp; /* Pool for temporary config stuff, reset often */
298     ap_context_t *pcommands; /* Pool for -C and -c switches */
299     module **mod;
300
301     ap_initialize();
302     process = create_process(argc, (const char **)argv);
303     pglobal = process->pool;
304     pconf = process->pconf;
305     ap_server_argv0 = process->short_name;
306     
307     ap_util_uri_init();
308
309     g_pHookPool=pconf;
310
311     ap_setup_prelinked_modules(process);
312
313     ap_create_context(&pcommands, pglobal);
314     ap_server_pre_read_config  = ap_make_array(pcommands, 1, sizeof(char *));
315     ap_server_post_read_config = ap_make_array(pcommands, 1, sizeof(char *));
316     ap_server_config_defines   = ap_make_array(pcommands, 1, sizeof(char *));
317
318     while (ap_getopt(argc, argv, "C:c:d:f:k:vVlLth", &c, pcommands) == APR_SUCCESS) {
319         char **new;
320         switch (c) {
321         case 'c':
322             new = (char **)ap_push_array(ap_server_post_read_config);
323             *new = ap_pstrdup(pcommands, ap_optarg);
324             break;
325         case 'C':
326             new = (char **)ap_push_array(ap_server_pre_read_config);
327             *new = ap_pstrdup(pcommands, ap_optarg);
328             break;
329         case 'd':
330             def_server_root = ap_optarg;
331             break;
332         case 'f':
333             confname = ap_optarg;
334             break;
335         case 'v':
336             printf("Server version: %s\n", ap_get_server_version());
337             printf("Server built:   %s\n", ap_get_server_built());
338             destroy_and_exit_process(process, 0);
339         case 'V':
340             show_compile_settings();
341             destroy_and_exit_process(process, 0);
342         case 'l':
343             ap_show_modules();
344             destroy_and_exit_process(process, 0);
345         case 'L':
346             ap_show_directives();
347             destroy_and_exit_process(process, 0);
348         case 't':
349             configtestonly = 1;
350             break;
351         case 'h':
352             usage(process);
353         case '?':
354             usage(process);
355         }
356     }
357
358     ap_create_context(&plog, pglobal);
359     ap_create_context(&ptemp, pconf);
360
361     /* Note that we preflight the config file once
362        before reading it _again_ in the main loop.
363        This allows things, log files configuration 
364        for example, to settle down. */
365
366     ap_server_root = def_server_root;
367     ap_run_pre_config(pconf, plog, ptemp);
368     server_conf = ap_read_config(process, ptemp, confname);
369     if (configtestonly) {
370         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n");
371         destroy_and_exit_process(process, 0);
372     }
373     ap_clear_pool(plog);
374     ap_run_open_logs(pconf, plog, ptemp, server_conf);
375     ap_post_config_hook(pconf, plog, ptemp, server_conf);
376     ap_destroy_pool(ptemp);
377
378     for (;;) {
379         ap_hook_deregister_all();
380         ap_clear_pool(pconf);
381         for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
382                 ap_register_hooks(*mod);
383         }
384         ap_create_context(&ptemp, pconf);
385         ap_server_root = def_server_root;
386         ap_run_pre_config(pconf, plog, ptemp);
387         server_conf = ap_read_config(process, ptemp, confname);
388         ap_clear_pool(plog);
389         ap_run_open_logs(pconf, plog, ptemp, server_conf);
390         ap_post_config_hook(pconf, plog, ptemp, server_conf);
391         ap_destroy_pool(ptemp);
392
393         if (ap_mpm_run(pconf, plog, server_conf)) break;
394     }
395     destroy_and_exit_process(process, 0);
396     return 0; /* Supress compiler warning. */
397 }
398
399 /* force Expat to be linked into the server executable */
400 #if defined(USE_EXPAT) && !defined(SHARED_CORE_BOOTSTRAP)
401 #include "xmlparse.h"
402 const XML_LChar *suck_in_expat(void);
403 const XML_LChar *suck_in_expat(void)
404 {
405     return XML_ErrorString(XML_ERROR_NONE);
406 }
407 #endif /* USE_EXPAT */
408
409 #ifndef SHARED_CORE_BOOTSTRAP
410 /*
411  * Force ap_validate_password() into the image so that modules like
412  * mod_auth can use it even if they're dynamically loaded.
413  */
414 void suck_in_ap_validate_password(void);
415 void suck_in_ap_validate_password(void)
416 {
417     ap_validate_password("a", "b");
418 }
419 #endif
420