]> granicus.if.org Git - apache/blob - server/main.c
Add -X functionality back to httpd.
[apache] / server / main.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * 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 NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 #include "apr.h"
60 #include "apr_strings.h"
61 #include "apr_getopt.h"
62 #include "apr_general.h"
63 #include "apr_lib.h"
64
65 #define APR_WANT_STDIO
66 #include "apr_want.h"
67
68 #define CORE_PRIVATE
69 #include "ap_config.h"
70 #include "httpd.h" 
71 #include "http_main.h" 
72 #include "http_log.h" 
73 #include "http_config.h"
74 #include "http_vhost.h"
75 #include "apr_uri.h" 
76 #include "util_ebcdic.h"
77 #include "ap_mpm.h"
78
79 /* WARNING: Win32 binds http_main.c dynamically to the server. Please place 
80  *          extern functions and global data in another appropriate module.
81  *
82  * Most significant main() global data can be found in http_config.c
83  */
84
85 /* XXX - We should be able to grab the per-MPM settings here too */
86 static void show_compile_settings(void)
87 {
88     printf("Server version: %s\n", ap_get_server_version());
89     printf("Server built:   %s\n", ap_get_server_built());
90     printf("Server's Module Magic Number: %u:%u\n",
91            MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
92     printf("Server compiled with....\n");
93 #ifdef BIG_SECURITY_HOLE
94     printf(" -D BIG_SECURITY_HOLE\n");
95 #endif
96 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
97     printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
98 #endif
99 #ifdef APACHE_MPM_DIR
100     printf(" -D APACHE_MPM_DIR=\"%s\"\n", APACHE_MPM_DIR);
101 #endif
102 #ifdef HAVE_SHMGET
103     printf(" -D HAVE_SHMGET\n");
104 #endif
105 #if APR_FILE_BASED_SHM
106     printf(" -D APR_FILE_BASED_SHM\n");
107 #endif
108 #if APR_HAS_MMAP
109     printf(" -D APR_HAS_MMAP\n");
110 #endif
111 #ifdef NO_WRITEV
112     printf(" -D NO_WRITEV\n");
113 #endif
114 #ifdef NO_LINGCLOSE
115     printf(" -D NO_LINGCLOSE\n");
116 #endif
117 #if APR_HAVE_IPV6
118     printf(" -D APR_HAVE_IPV6\n");
119 #endif
120 #if APR_USE_FLOCK_SERIALIZE
121     printf(" -D APR_USE_FLOCK_SERIALIZE\n");
122 #endif
123 #if APR_USE_SYSVSEM_SERIALIZE
124     printf(" -D APR_USE_SYSVSEM_SERIALIZE\n");
125 #endif
126 #if APR_USE_FCNTL_SERIALIZE
127     printf(" -D APR_USE_FCNTL_SERIALIZE\n");
128 #endif
129 #if APR_USE_PROC_PTHREAD_SERIALIZE
130     printf(" -D APR_USE_PROC_PTHREAD_SERIALIZE\n");
131 #endif
132 #if APR_USE_PTHREAD_SERIALIZE
133     printf(" -D APR_USE_PTHREAD_SERIALIZE\n");
134 #endif
135 #if APR_PROCESS_LOCK_IS_GLOBAL
136     printf(" -D APR_PROCESS_LOCK_IS_GLOBAL\n");
137 #endif
138 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
139     printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
140 #endif
141 #if APR_HAS_OTHER_CHILD
142     printf(" -D APR_HAS_OTHER_CHILD\n");
143 #endif
144 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
145     printf(" -D AP_HAVE_RELIABLE_PIPED_LOGS\n");
146 #endif
147 #ifdef BUFFERED_LOGS
148     printf(" -D BUFFERED_LOGS\n");
149 #ifdef PIPE_BUF
150         printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
151 #endif
152 #endif
153 #if APR_CHARSET_EBCDIC
154     printf(" -D APR_CHARSET_EBCDIC\n");
155 #endif
156 #ifdef APACHE_XLATE
157     printf(" -D APACHE_XLATE\n");
158 #endif
159 #ifdef NEED_HASHBANG_EMUL
160     printf(" -D NEED_HASHBANG_EMUL\n");
161 #endif
162 #ifdef SHARED_CORE
163     printf(" -D SHARED_CORE\n");
164 #endif
165
166 /* This list displays the compiled in default paths: */
167 #ifdef HTTPD_ROOT
168     printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
169 #endif
170 #ifdef SUEXEC_BIN
171     printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
172 #endif
173 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
174     printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
175 #endif
176 #ifdef DEFAULT_PIDLOG
177     printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
178 #endif
179 #ifdef DEFAULT_SCOREBOARD
180     printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
181 #endif
182 #ifdef DEFAULT_LOCKFILE
183     printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
184 #endif
185 #ifdef DEFAULT_ERRORLOG
186     printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
187 #endif
188 #ifdef TYPES_CONFIG_FILE
189     printf(" -D TYPES_CONFIG_FILE=\"" TYPES_CONFIG_FILE "\"\n");
190 #endif
191 #ifdef SERVER_CONFIG_FILE
192     printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
193 #endif
194 }
195
196 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
197 {
198     apr_pool_destroy(process->pool); /* and destroy all descendent pools */
199     apr_terminate();
200     exit(process_exit_value);
201 }
202
203 static process_rec *create_process(int argc, const char * const *argv)
204 {
205     process_rec *process;
206     apr_pool_t *cntx;
207     apr_status_t stat;
208
209     stat = apr_pool_create(&cntx, NULL);
210     if (stat != APR_SUCCESS) {
211         /* XXX From the time that we took away the NULL pool->malloc mapping
212          *     we have been unable to log here without segfaulting.
213          */
214         ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
215                      "apr_pool_create() failed to create "
216                      "initial context");
217         apr_terminate();
218         exit(1);
219     }
220
221     ap_open_stderr_log(cntx);
222
223     process = apr_palloc(cntx, sizeof(process_rec));
224     process->pool = cntx;
225
226     apr_pool_create(&process->pconf, process->pool);
227     process->argc = argc;
228     process->argv = argv;
229     process->short_name = apr_filename_of_pathname(argv[0]);
230     return process;
231 }
232
233 static void usage(process_rec *process)
234 {
235     const char *bin = process->argv[0];
236     char pad[MAX_STRING_LEN];
237     unsigned i;
238
239     for (i = 0; i < strlen(bin); i++)
240         pad[i] = ' ';
241     pad[i] = '\0';
242 #ifdef SHARED_CORE
243     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
244 #else
245     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
246 #endif
247     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-C \"directive\"] [-c \"directive\"]", pad);
248 #ifdef WIN32
249     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-k restart|shutdown|start]", pad);
250     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-n service_name]", pad);
251     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-i] [-u]", pad);
252 #endif
253     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-v] [-V] [-h] [-l] [-L] [-t] [-T]", pad);
254     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:");
255 #ifdef SHARED_CORE
256     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -R directory      : specify an alternate location for shared object files");
257 #endif
258     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -D name           : define a name for use in <IfDefine name> directives");
259     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -d directory      : specify an alternate initial ServerRoot");
260     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -f file           : specify an alternate ServerConfigFile");
261     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -C \"directive\"    : process directive before reading config files");
262     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -c \"directive\"    : process directive after  reading config files");
263 #ifdef WIN32
264     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -n name           : set service name and use its ServerConfigFile");
265     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k shutdown       : tell running Apache to shutdown");
266     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k restart        : tell running Apache to do a graceful restart");
267     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k start          : tell Apache to start");
268     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -i                : install an Apache service");
269     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -u                : uninstall an Apache service");
270 #endif
271     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -v                : show version number");
272     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -V                : show compile settings");
273     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -h                : list available command line options (this page)");
274     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -l                : list compiled in modules");
275     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -L                : list available configuration directives");
276     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings)"); 
277     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t                : run syntax check for config files (with docroot check)");
278     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -T                : run syntax check for config files (without docroot check)");
279     /* TODOC: -X goes away, expect MPMs to use -D options */
280     destroy_and_exit_process(process, 1);
281 }
282
283 int main(int argc, const char * const argv[])
284 {
285     char c;
286     int configtestonly = 0;
287     const char *confname = SERVER_CONFIG_FILE;
288     const char *def_server_root = HTTPD_ROOT;
289     process_rec *process;
290     server_rec *server_conf;
291     apr_pool_t *pglobal;
292     apr_pool_t *pconf;
293     apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
294     apr_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
295     apr_pool_t *pcommands; /* Pool for -D, -C and -c switches */
296     apr_getopt_t *opt;
297     apr_status_t rv;
298     module **mod;
299     const char *optarg;
300
301     apr_initialize();
302
303     process = create_process(argc, argv);
304     pglobal = process->pool;
305     pconf = process->pconf;
306     ap_server_argv0 = process->short_name;
307
308 #if APR_CHARSET_EBCDIC
309     if (ap_init_ebcdic(pglobal) != APR_SUCCESS) {
310         destroy_and_exit_process(process, 1);
311     }
312 #endif
313
314     ap_setup_prelinked_modules(process);
315
316     apr_pool_create(&pcommands, pglobal);
317     ap_server_pre_read_config  = apr_array_make(pcommands, 1, sizeof(char *));
318     ap_server_post_read_config = apr_array_make(pcommands, 1, sizeof(char *));
319     ap_server_config_defines   = apr_array_make(pcommands, 1, sizeof(char *));
320
321     ap_run_rewrite_args(process);
322
323     /* Maintain AP_SERVER_BASEARGS list in http_main.h to allow the MPM 
324      * to safely pass on our args from its rewrite_args() handler.
325      */
326     apr_getopt_init(&opt, pcommands, process->argc, process->argv);
327
328     while ((rv = apr_getopt(opt, AP_SERVER_BASEARGS, &c, &optarg))
329             == APR_SUCCESS) {
330         char **new;
331         switch (c) {
332         case 'c':
333             new = (char **)apr_array_push(ap_server_post_read_config);
334             *new = apr_pstrdup(pcommands, optarg);
335             break;
336         case 'C':
337             new = (char **)apr_array_push(ap_server_pre_read_config);
338             *new = apr_pstrdup(pcommands, optarg);
339             break;
340         case 'd':
341             def_server_root = optarg;
342             break;
343         case 'D':
344             new = (char **)apr_array_push(ap_server_config_defines);
345             *new = apr_pstrdup(pcommands, optarg);
346             break;
347         case 'X':
348             new = (char **)apr_array_push(ap_server_config_defines);
349             *new = "DEBUG";
350             break;
351         case 'f':
352             confname = optarg;
353             break;
354         case 'v':
355             printf("Server version: %s\n", ap_get_server_version());
356             printf("Server built:   %s\n", ap_get_server_built());
357             destroy_and_exit_process(process, 0);
358         case 'V':
359             show_compile_settings();
360             destroy_and_exit_process(process, 0);
361         case 'l':
362             ap_show_modules();
363             destroy_and_exit_process(process, 0);
364         case 'L':
365             ap_show_directives();
366             destroy_and_exit_process(process, 0);
367         case 't':
368             configtestonly = 1;
369             break;
370         case 'h':
371             usage(process);
372         }
373     }
374
375     if (rv != APR_EOF) { /* bad cmdline option */
376         usage(process);
377     }
378
379     apr_pool_create(&plog, pglobal);
380     apr_pool_create(&ptemp, pconf);
381
382     /* Note that we preflight the config file once
383        before reading it _again_ in the main loop.
384        This allows things, log files configuration 
385        for example, to settle down. */
386
387     ap_server_root = def_server_root;
388     server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
389     ap_run_pre_config(pconf, plog, ptemp);
390     ap_process_config_tree(server_conf, ap_conftree, process->pconf, ptemp); 
391     ap_fixup_virtual_hosts(pconf, server_conf);
392     ap_fini_vhost_config(pconf, server_conf);
393     apr_sort_hooks();
394     if (configtestonly) {
395         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n");
396         destroy_and_exit_process(process, 0);
397     }
398     apr_pool_clear(plog);
399     ap_run_open_logs(pconf, plog, ptemp, server_conf);
400     ap_run_post_config(pconf, plog, ptemp, server_conf);
401     apr_pool_destroy(ptemp);
402
403     for (;;) {
404         apr_hook_deregister_all();
405         apr_pool_clear(pconf);
406         for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
407             ap_register_hooks(*mod, pconf);
408         }
409
410
411         /* This is a hack until we finish the code so that it only reads
412          * the config file once and just operates on the tree already in
413          * memory.  rbb
414          */
415         ap_conftree = NULL;
416         apr_pool_create(&ptemp, pconf);
417         ap_server_root = def_server_root;
418         server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
419         ap_run_pre_config(pconf, plog, ptemp);
420         ap_process_config_tree(server_conf, ap_conftree, process->pconf, ptemp);
421         ap_fixup_virtual_hosts(pconf, server_conf);
422         ap_fini_vhost_config(pconf, server_conf);
423         apr_sort_hooks();
424         apr_pool_clear(plog);
425         ap_run_open_logs(pconf, plog, ptemp, server_conf);
426         ap_run_post_config(pconf, plog, ptemp, server_conf);
427         apr_pool_destroy(ptemp);
428
429         ap_run_optional_fn_retrieve();
430
431         if (ap_mpm_run(pconf, plog, server_conf)) break;
432     }
433     destroy_and_exit_process(process, 0);
434     return 0; /* Supress compiler warning. */
435 }
436
437 /* force Expat to be linked into the server executable */
438 #if defined(USE_EXPAT) && !defined(SHARED_CORE_BOOTSTRAP)
439 #include "xmlparse.h"
440 const XML_LChar *suck_in_expat(void);
441 const XML_LChar *suck_in_expat(void)
442 {
443     return XML_ErrorString(XML_ERROR_NONE);
444 }
445 #endif /* USE_EXPAT */
446
447 #ifndef SHARED_CORE_BOOTSTRAP
448 /*
449  * Force apr_password_validate() into the image so that modules like
450  * mod_auth can use it even if they're dynamically loaded.
451  */
452 void suck_in_apr_password_validate(void);
453 void suck_in_apr_password_validate(void)
454 {
455     apr_password_validate("a", "b");
456 }
457 #endif
458
459 #ifdef AP_USING_AUTOCONF
460 /* This ugly little hack pulls any function referenced in exports.c into
461  * the web server.  exports.c is generated during the build, and it
462  * has all of the APR functions specified by the apr/apr.exports and
463  * apr-util/aprutil.exports files.
464  */
465 const void *suck_in_APR(void);
466 const void *suck_in_APR(void)
467 {
468     extern const void *ap_ugly_hack;
469
470     return ap_ugly_hack;
471 }
472 #endif