]> granicus.if.org Git - apache/blob - server/main.c
26669d81c0f36b7219e2aeebbec6d6cc1d58fdd8
[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 "util_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 #if 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 #ifdef AP_USE_FILE_BASED_SCOREBOARD
106     printf(" -D AP_USE_FILE_BASED_SCOREBOARD\n");
107 #endif
108 #ifdef AP_USE_MEM_BASED_SCOREBOARD
109     printf(" -D AP_USE_MEM_BASED_SCOREBOARD\n");
110 #endif
111 #ifdef APR_HAS_MMAP
112     printf(" -D APR_HAS_MMAP\n");
113 #endif /*APR_HAS_MMAP*/
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 #if APR_HAVE_IPV6
121     printf(" -D APR_HAVE_IPV6\n");
122 #endif
123 #if APR_USE_FLOCK_SERIALIZE
124     printf(" -D APR_USE_FLOCK_SERIALIZE\n");
125 #endif
126 #if APR_USE_SYSVSEM_SERIALIZE
127     printf(" -D APR_USE_SYSVSEM_SERIALIZE\n");
128 #endif
129 #if APR_USE_FCNTL_SERIALIZE
130     printf(" -D APR_USE_FCNTL_SERIALIZE\n");
131 #endif
132 #if APR_USE_PROC_PTHREAD_SERIALIZE
133     printf(" -D APR_USE_PROC_PTHREAD_SERIALIZE\n");
134 #endif
135 #if APR_USE_PTHREAD_SERIALIZE
136     printf(" -D APR_USE_PTHREAD_SERIALIZE\n");
137 #endif
138 #if APR_PROCESS_LOCK_IS_GLOBAL
139     printf(" -D APR_PROCESS_LOCK_IS_GLOBAL\n");
140 #endif
141 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
142     printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
143 #endif
144 #if APR_HAS_OTHER_CHILD
145     printf(" -D APR_HAS_OTHER_CHILD\n");
146 #endif
147 #ifdef AP_HAVE_RELIABLE_PIPED_LOGS
148     printf(" -D AP_HAVE_RELIABLE_PIPED_LOGS\n");
149 #endif
150 #ifdef BUFFERED_LOGS
151     printf(" -D BUFFERED_LOGS\n");
152 #ifdef PIPE_BUF
153         printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
154 #endif
155 #endif
156 #if APR_CHARSET_EBCDIC
157     printf(" -D APR_CHARSET_EBCDIC\n");
158 #endif
159 #ifdef APACHE_XLATE
160     printf(" -D APACHE_XLATE\n");
161 #endif
162 #ifdef NEED_HASHBANG_EMUL
163     printf(" -D NEED_HASHBANG_EMUL\n");
164 #endif
165 #ifdef SHARED_CORE
166     printf(" -D SHARED_CORE\n");
167 #endif
168
169 /* This list displays the compiled in default paths: */
170 #ifdef HTTPD_ROOT
171     printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
172 #endif
173 #ifdef SUEXEC_BIN
174     printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
175 #endif
176 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
177     printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
178 #endif
179 #ifdef DEFAULT_PIDLOG
180     printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
181 #endif
182 #ifdef DEFAULT_SCOREBOARD
183     printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
184 #endif
185 #ifdef DEFAULT_LOCKFILE
186     printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
187 #endif
188 #ifdef DEFAULT_ERRORLOG
189     printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
190 #endif
191 #ifdef TYPES_CONFIG_FILE
192     printf(" -D TYPES_CONFIG_FILE=\"" TYPES_CONFIG_FILE "\"\n");
193 #endif
194 #ifdef SERVER_CONFIG_FILE
195     printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
196 #endif
197 }
198
199 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
200 {
201     apr_pool_destroy(process->pool); /* and destroy all descendent pools */
202     apr_terminate();
203     exit(process_exit_value);
204 }
205
206 static process_rec *create_process(int argc, const char * const *argv)
207 {
208     process_rec *process;
209     apr_pool_t *cntx;
210     apr_status_t stat;
211
212     stat = apr_pool_create(&cntx, NULL);
213     if (stat != APR_SUCCESS) {
214         /* XXX From the time that we took away the NULL pool->malloc mapping
215          *     we have been unable to log here without segfaulting.
216          */
217         ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
218                      "apr_pool_create() failed to create "
219                      "initial context");
220         apr_terminate();
221         exit(1);
222     }
223
224     ap_open_stderr_log(cntx);
225
226     process = apr_palloc(cntx, sizeof(process_rec));
227     process->pool = cntx;
228
229     apr_pool_create(&process->pconf, process->pool);
230     process->argc = argc;
231     process->argv = argv;
232     process->short_name = apr_filename_of_pathname(argv[0]);
233     return process;
234 }
235
236 static void usage(process_rec *process)
237 {
238     const char *bin = process->argv[0];
239     char pad[MAX_STRING_LEN];
240     unsigned i;
241
242     for (i = 0; i < strlen(bin); i++)
243         pad[i] = ' ';
244     pad[i] = '\0';
245 #ifdef SHARED_CORE
246     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
247 #else
248     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
249 #endif
250     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-C \"directive\"] [-c \"directive\"]", pad);
251 #ifdef WIN32
252     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-k restart|shutdown|start]", pad);
253     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-n service_name]", pad);
254     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-i] [-u]", pad);
255 #endif
256     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-v] [-V] [-h] [-l] [-L] [-t] [-T]", pad);
257     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:");
258 #ifdef SHARED_CORE
259     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -R directory      : specify an alternate location for shared object files");
260 #endif
261     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -D name           : define a name for use in <IfDefine name> directives");
262     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -d directory      : specify an alternate initial ServerRoot");
263     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -f file           : specify an alternate ServerConfigFile");
264     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -C \"directive\"    : process directive before reading config files");
265     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -c \"directive\"    : process directive after  reading config files");
266 #ifdef WIN32
267     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -n name           : set service name and use its ServerConfigFile");
268     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k shutdown       : tell running Apache to shutdown");
269     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k restart        : tell running Apache to do a graceful restart");
270     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k start          : tell Apache to start");
271     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -i                : install an Apache service");
272     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -u                : uninstall an Apache service");
273 #endif
274     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -v                : show version number");
275     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -V                : show compile settings");
276     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -h                : list available command line options (this page)");
277     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -l                : list compiled in modules");
278     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -L                : list available configuration directives");
279     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings)"); 
280     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t                : run syntax check for config files (with docroot check)");
281     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -T                : run syntax check for config files (without docroot check)");
282     /* TODOC: -X goes away, expect MPMs to use -D options */
283     destroy_and_exit_process(process, 1);
284 }
285
286 int main(int argc, const char * const argv[])
287 {
288     char 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     apr_pool_t *pglobal;
295     apr_pool_t *pconf;
296     apr_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
297     apr_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
298     apr_pool_t *pcommands; /* Pool for -D, -C and -c switches */
299     apr_getopt_t *opt;
300     module **mod;
301     const char *optarg;
302
303     apr_initialize();
304
305     process = create_process(argc, argv);
306     pglobal = process->pool;
307     pconf = process->pconf;
308     ap_server_argv0 = process->short_name;
309
310 #if APR_CHARSET_EBCDIC
311     if (ap_init_ebcdic(pglobal) != APR_SUCCESS) {
312         destroy_and_exit_process(process, 1);
313     }
314 #endif
315
316     ap_util_uri_init();
317
318     ap_setup_prelinked_modules(process);
319
320     apr_pool_create(&pcommands, pglobal);
321     ap_server_pre_read_config  = apr_array_make(pcommands, 1, sizeof(char *));
322     ap_server_post_read_config = apr_array_make(pcommands, 1, sizeof(char *));
323     ap_server_config_defines   = apr_array_make(pcommands, 1, sizeof(char *));
324
325     ap_run_rewrite_args(process);
326
327     /* Maintain AP_SERVER_BASEARGS list in http_main.h to allow the MPM 
328      * to safely pass on our args from its rewrite_args() handler.
329      */
330     apr_getopt_init(&opt, pcommands, process->argc, process->argv);
331
332     while (apr_getopt(opt, AP_SERVER_BASEARGS, &c, &optarg) 
333             == APR_SUCCESS) {
334         char **new;
335         switch (c) {
336         case 'c':
337             new = (char **)apr_array_push(ap_server_post_read_config);
338             *new = apr_pstrdup(pcommands, optarg);
339             break;
340         case 'C':
341             new = (char **)apr_array_push(ap_server_pre_read_config);
342             *new = apr_pstrdup(pcommands, optarg);
343             break;
344         case 'd':
345             def_server_root = optarg;
346             break;
347         case 'D':
348             new = (char **)apr_array_push(ap_server_config_defines);
349             *new = apr_pstrdup(pcommands, optarg);
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 '?':
371         case 'h':
372             usage(process);
373         }
374     }
375
376     apr_pool_create(&plog, pglobal);
377     apr_pool_create(&ptemp, pconf);
378
379     /* Note that we preflight the config file once
380        before reading it _again_ in the main loop.
381        This allows things, log files configuration 
382        for example, to settle down. */
383
384     ap_server_root = def_server_root;
385     server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
386     ap_run_pre_config(pconf, plog, ptemp);
387     ap_process_config_tree(server_conf, ap_conftree, process->pconf, ptemp); 
388     ap_fixup_virtual_hosts(pconf, server_conf);
389     ap_fini_vhost_config(pconf, server_conf);
390     apr_sort_hooks();
391     if (configtestonly) {
392         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n");
393         destroy_and_exit_process(process, 0);
394     }
395     apr_pool_clear(plog);
396     ap_run_open_logs(pconf, plog, ptemp, server_conf);
397     ap_post_config_hook(pconf, plog, ptemp, server_conf);
398     apr_pool_destroy(ptemp);
399
400     for (;;) {
401         apr_hook_deregister_all();
402         apr_pool_clear(pconf);
403         for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
404             ap_register_hooks(*mod, pconf);
405         }
406
407
408         /* This is a hack until we finish the code so that it only reads
409          * the config file once and just operates on the tree already in
410          * memory.  rbb
411          */
412         ap_conftree = NULL;
413         apr_pool_create(&ptemp, pconf);
414         ap_server_root = def_server_root;
415         server_conf = ap_read_config(process, ptemp, confname, &ap_conftree);
416         ap_run_pre_config(pconf, plog, ptemp);
417         ap_process_config_tree(server_conf, ap_conftree, process->pconf, ptemp);
418         ap_fixup_virtual_hosts(pconf, server_conf);
419         ap_fini_vhost_config(pconf, server_conf);
420         apr_sort_hooks();
421         apr_pool_clear(plog);
422         ap_run_open_logs(pconf, plog, ptemp, server_conf);
423         ap_post_config_hook(pconf, plog, ptemp, server_conf);
424         apr_pool_destroy(ptemp);
425
426         ap_run_optional_fn_retrieve();
427
428         if (ap_mpm_run(pconf, plog, server_conf)) break;
429     }
430     destroy_and_exit_process(process, 0);
431     return 0; /* Supress compiler warning. */
432 }
433
434 /* force Expat to be linked into the server executable */
435 #if defined(USE_EXPAT) && !defined(SHARED_CORE_BOOTSTRAP)
436 #include "xmlparse.h"
437 const XML_LChar *suck_in_expat(void);
438 const XML_LChar *suck_in_expat(void)
439 {
440     return XML_ErrorString(XML_ERROR_NONE);
441 }
442 #endif /* USE_EXPAT */
443
444 #ifndef SHARED_CORE_BOOTSTRAP
445 /*
446  * Force apr_password_validate() into the image so that modules like
447  * mod_auth can use it even if they're dynamically loaded.
448  */
449 void suck_in_apr_password_validate(void);
450 void suck_in_apr_password_validate(void)
451 {
452     apr_password_validate("a", "b");
453 }
454 #endif
455
456 #ifdef AP_USING_AUTOCONF
457 /* This ugly little hack pulls any function referenced in exports.c into
458  * the web server.  exports.c is generated during the build, and it
459  * has all of the APR functions specified by the apr/apr.exports and
460  * apr-util/aprutil.exports files.
461  */
462 const void *suck_in_APR(void);
463 const void *suck_in_APR(void)
464 {
465     extern const void *ap_ugly_hack;
466
467     return ap_ugly_hack;
468 }
469 #endif