]> granicus.if.org Git - apache/blob - server/main.c
Update to Apache Software License version 1.1
[apache] / server / main.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 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 #define CORE_PRIVATE
60 #include "ap_config.h"
61 #include "httpd.h" 
62 #include "http_main.h" 
63 #include "http_log.h" 
64 #include "http_config.h"
65 #include "util_uri.h" 
66 #include "apr_getopt.h"
67 #include "ap_mpm.h"
68
69 const char *ap_server_argv0;
70
71 const char *ap_server_root;
72
73 ap_array_header_t *ap_server_pre_read_config;
74 ap_array_header_t *ap_server_post_read_config;
75 ap_array_header_t *ap_server_config_defines;
76
77 /* XXX - We should be able to grab the per-MPM settings here too */
78 static void show_compile_settings(void)
79 {
80     printf("Server version: %s\n", ap_get_server_version());
81     printf("Server built:   %s\n", ap_get_server_built());
82     printf("Server's Module Magic Number: %u:%u\n",
83            MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
84     printf("Server compiled with....\n");
85 #ifdef BIG_SECURITY_HOLE
86     printf(" -D BIG_SECURITY_HOLE\n");
87 #endif
88 #ifdef SECURITY_HOLE_PASS_AUTHORIZATION
89     printf(" -D SECURITY_HOLE_PASS_AUTHORIZATION\n");
90 #endif
91 #ifdef HAVE_MMAP
92     printf(" -D HAVE_MMAP\n");
93 #endif
94 #ifdef HAVE_SHMGET
95     printf(" -D HAVE_SHMGET\n");
96 #endif
97 #ifdef USE_MMAP_SCOREBOARD
98     printf(" -D USE_MMAP_SCOREBOARD\n");
99 #endif
100 #ifdef USE_SHMGET_SCOREBOARD
101     printf(" -D USE_SHMGET_SCOREBOARD\n");
102 #endif
103 #ifdef USE_OS2_SCOREBOARD
104     printf(" -D USE_OS2_SCOREBOARD\n");
105 #endif
106 #ifdef USE_POSIX_SCOREBOARD
107     printf(" -D USE_POSIX_SCOREBOARD\n");
108 #endif
109 #ifdef USE_MMAP_FILES
110     printf(" -D USE_MMAP_FILES\n");
111 #ifdef MMAP_SEGMENT_SIZE
112         printf(" -D MMAP_SEGMENT_SIZE=%ld\n",(long)MMAP_SEGMENT_SIZE);
113 #endif
114 #endif /*USE_MMAP_FILES*/
115 #ifdef NO_WRITEV
116     printf(" -D NO_WRITEV\n");
117 #endif
118 #ifdef NO_LINGCLOSE
119     printf(" -D NO_LINGCLOSE\n");
120 #endif
121 #ifdef USE_FCNTL_SERIALIZED_ACCEPT
122     printf(" -D USE_FCNTL_SERIALIZED_ACCEPT\n");
123 #endif
124 #ifdef USE_FLOCK_SERIALIZED_ACCEPT
125     printf(" -D USE_FLOCK_SERIALIZED_ACCEPT\n");
126 #endif
127 #ifdef USE_USLOCK_SERIALIZED_ACCEPT
128     printf(" -D USE_USLOCK_SERIALIZED_ACCEPT\n");
129 #endif
130 #ifdef USE_SYSVSEM_SERIALIZED_ACCEPT
131     printf(" -D USE_SYSVSEM_SERIALIZED_ACCEPT\n");
132 #endif
133 #ifdef USE_PTHREAD_SERIALIZED_ACCEPT
134     printf(" -D USE_PTHREAD_SERIALIZED_ACCEPT\n");
135 #endif
136 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
137     printf(" -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT\n");
138 #endif
139 #ifdef HAS_OTHER_CHILD
140     printf(" -D HAS_OTHER_CHILD\n");
141 #endif
142 #ifdef HAVE_RELIABLE_PIPED_LOGS
143     printf(" -D HAVE_RELIABLE_PIPED_LOGS\n");
144 #endif
145 #ifdef BUFFERED_LOGS
146     printf(" -D BUFFERED_LOGS\n");
147 #ifdef PIPE_BUF
148         printf(" -D PIPE_BUF=%ld\n",(long)PIPE_BUF);
149 #endif
150 #endif
151 #ifdef CHARSET_EBCDIC
152     printf(" -D CHARSET_EBCDIC\n");
153 #endif
154 #ifdef NEED_HASHBANG_EMUL
155     printf(" -D NEED_HASHBANG_EMUL\n");
156 #endif
157 #ifdef SHARED_CORE
158     printf(" -D SHARED_CORE\n");
159 #endif
160
161 /* This list displays the compiled in default paths: */
162 #ifdef HTTPD_ROOT
163     printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
164 #endif
165 #ifdef SUEXEC_BIN
166     printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
167 #endif
168 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
169     printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
170 #endif
171 #ifdef DEFAULT_PIDLOG
172     printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
173 #endif
174 #ifdef DEFAULT_SCOREBOARD
175     printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
176 #endif
177 #ifdef DEFAULT_LOCKFILE
178     printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
179 #endif
180 #ifdef DEFAULT_XFERLOG
181     printf(" -D DEFAULT_XFERLOG=\"" DEFAULT_XFERLOG "\"\n");
182 #endif
183 #ifdef DEFAULT_ERRORLOG
184     printf(" -D DEFAULT_ERRORLOG=\"" DEFAULT_ERRORLOG "\"\n");
185 #endif
186 #ifdef TYPES_CONFIG_FILE
187     printf(" -D TYPES_CONFIG_FILE=\"" TYPES_CONFIG_FILE "\"\n");
188 #endif
189 #ifdef SERVER_CONFIG_FILE
190     printf(" -D SERVER_CONFIG_FILE=\"" SERVER_CONFIG_FILE "\"\n");
191 #endif
192 }
193
194 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
195 {
196     ap_destroy_pool(process->pool); /* and destroy all descendent pools */
197     ap_terminate();
198     exit(process_exit_value);
199 }
200
201 static process_rec *create_process(int argc, const char **argv)
202 {
203     process_rec *process;
204     
205     {
206         ap_context_t *cntx;
207         ap_status_t stat;
208
209         stat = ap_create_context(&cntx, NULL);
210         if (stat != APR_SUCCESS) {
211             ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
212                          "ap_create_context() failed to create "
213                          "initial context");
214             ap_terminate();
215             exit(1);
216         }
217
218         process = ap_palloc(cntx, sizeof(process_rec));
219         process->pool = cntx;
220     }
221     ap_create_context(&process->pconf, process->pool);
222     process->argc = argc;
223     process->argv = argv;
224     process->short_name = ap_filename_of_pathname(argv[0]);
225     return process;
226 }
227
228 static void usage(process_rec *process)
229 {
230     const char *bin = process->argv[0];
231     char pad[MAX_STRING_LEN];
232     unsigned i;
233
234     for (i = 0; i < strlen(bin); i++)
235         pad[i] = ' ';
236     pad[i] = '\0';
237 #ifdef SHARED_CORE
238     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
239 #else
240     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
241 #endif
242     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-C \"directive\"] [-c \"directive\"]", pad);
243 #ifdef WIN32
244     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-k restart|shutdown|start]", pad);
245     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-n service_name]", pad);
246     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-i] [-u]", pad);
247 #endif
248     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-v] [-V] [-h] [-l] [-L] [-S] [-t] [-T]", pad);
249     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:");
250 #ifdef SHARED_CORE
251     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -R directory     : specify an alternate location for shared object files");
252 #endif
253     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -D name          : define a name for use in <IfDefine name> directives");
254     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -d directory     : specify an alternate initial ServerRoot");
255     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -f file          : specify an alternate ServerConfigFile");
256     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -C \"directive\"   : process directive before reading config files");
257     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -c \"directive\"   : process directive after  reading config files");
258 #ifdef WIN32
259     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -n name          : set service name and use its ServerConfigFile");
260     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k shutdown      : tell running Apache to shutdown");
261     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k restart       : tell running Apache to do a graceful restart");
262     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k start         : tell Apache to start");
263     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -i               : install an Apache service");
264     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -u               : uninstall an Apache service");
265 #endif
266     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -v               : show version number");
267     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -V               : show compile settings");
268     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -h               : list available command line options (this page)");
269     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -l               : list compiled in modules");
270     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -L               : list available configuration directives");
271     /* TODOC: -S has been replaced by '-t -D DUMP_VHOSTS' */
272     /* ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -S               : show parsed settings (currently only vhost settings)"); */
273     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t               : run syntax check for config files (with docroot check)");
274     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -T               : run syntax check for config files (without docroot check)");
275     /* TODOC: -X goes away, expect MPMs to use -D options */
276     destroy_and_exit_process(process, 1);
277 }
278
279 ap_context_t *g_pHookPool;
280
281 #ifdef WIN32
282 API_EXPORT_NONSTD(int) apache_main(int argc, char *argv[])
283 #else
284 API_EXPORT_NONSTD(int)        main(int argc, char *argv[])
285 #endif
286 {
287     int c;
288     int configtestonly = 0;
289     const char *confname = SERVER_CONFIG_FILE;
290     const char *def_server_root = HTTPD_ROOT;
291     process_rec *process;
292     server_rec *server_conf;
293     ap_context_t *pglobal;
294     ap_context_t *pconf;
295     ap_context_t *plog; /* Pool of log streams, reset _after_ each read of conf */
296     ap_context_t *ptemp; /* Pool for temporary config stuff, reset often */
297     ap_context_t *pcommands; /* Pool for -C and -c switches */
298     module **mod;
299
300 #ifndef WIN32 /* done in main_win32.c */
301     ap_initialize();
302 #endif
303     process = create_process(argc, (const char **)argv);
304     pglobal = process->pool;
305     pconf = process->pconf;
306     ap_server_argv0 = process->short_name;
307     
308     ap_util_uri_init();
309
310     g_pHookPool=pconf;
311
312     ap_setup_prelinked_modules(process);
313
314     ap_create_context(&pcommands, pglobal);
315     ap_server_pre_read_config  = ap_make_array(pcommands, 1, sizeof(char *));
316     ap_server_post_read_config = ap_make_array(pcommands, 1, sizeof(char *));
317     ap_server_config_defines   = ap_make_array(pcommands, 1, sizeof(char *));
318
319     while (ap_getopt(argc, argv, "C:c:d:f:k:vVlLth", &c, pcommands) == APR_SUCCESS) {
320         char **new;
321         switch (c) {
322         case 'c':
323             new = (char **)ap_push_array(ap_server_post_read_config);
324             *new = ap_pstrdup(pcommands, ap_optarg);
325             break;
326         case 'C':
327             new = (char **)ap_push_array(ap_server_pre_read_config);
328             *new = ap_pstrdup(pcommands, ap_optarg);
329             break;
330         case 'd':
331             def_server_root = ap_optarg;
332             break;
333         case 'f':
334             confname = ap_optarg;
335             break;
336         case 'v':
337             printf("Server version: %s\n", ap_get_server_version());
338             printf("Server built:   %s\n", ap_get_server_built());
339             destroy_and_exit_process(process, 0);
340         case 'V':
341             show_compile_settings();
342             destroy_and_exit_process(process, 0);
343         case 'l':
344             ap_show_modules();
345             destroy_and_exit_process(process, 0);
346         case 'L':
347             ap_show_directives();
348             destroy_and_exit_process(process, 0);
349         case 't':
350             configtestonly = 1;
351             break;
352         case 'h':
353             usage(process);
354         case '?':
355             usage(process);
356         }
357     }
358
359     ap_create_context(&plog, pglobal);
360     ap_create_context(&ptemp, pconf);
361
362     /* Note that we preflight the config file once
363        before reading it _again_ in the main loop.
364        This allows things, log files configuration 
365        for example, to settle down. */
366
367     ap_server_root = def_server_root;
368     run_pre_config(pconf, plog, ptemp);
369     server_conf = ap_read_config(process, ptemp, confname);
370     if (configtestonly) {
371         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n");
372         destroy_and_exit_process(process, 0);
373     }
374     ap_clear_pool(plog);
375     ap_run_open_logs(pconf, plog, ptemp, server_conf);
376     ap_post_config_hook(pconf, plog, ptemp, server_conf);
377     ap_destroy_pool(ptemp);
378
379     for (;;) {
380         ap_hook_deregister_all();
381         ap_clear_pool(pconf);
382         for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
383                 ap_register_hooks(*mod);
384         }
385         ap_create_context(&ptemp, pconf);
386         ap_server_root = def_server_root;
387         run_pre_config(pconf, plog, ptemp);
388         server_conf = ap_read_config(process, ptemp, confname);
389         ap_clear_pool(plog);
390         ap_run_open_logs(pconf, plog, ptemp, server_conf);
391         ap_post_config_hook(pconf, plog, ptemp, server_conf);
392         ap_destroy_pool(ptemp);
393
394         if (ap_mpm_run(pconf, plog, server_conf)) break;
395     }
396     destroy_and_exit_process(process, 0);
397     return 0; /* Supress compiler warning. */
398 }
399
400 /* force Expat to be linked into the server executable */
401 #if defined(USE_EXPAT) && !defined(SHARED_CORE_BOOTSTRAP)
402 #include "xmlparse.h"
403 const XML_LChar *suck_in_expat(void);
404 const XML_LChar *suck_in_expat(void)
405 {
406     return XML_ErrorString(XML_ERROR_NONE);
407 }
408 #endif /* USE_EXPAT */
409
410 #ifndef SHARED_CORE_BOOTSTRAP
411 /*
412  * Force ap_validate_password() into the image so that modules like
413  * mod_auth can use it even if they're dynamically loaded.
414  */
415 void suck_in_ap_validate_password(void);
416 void suck_in_ap_validate_password(void)
417 {
418     ap_validate_password("a", "b");
419 }
420 #endif
421