]> granicus.if.org Git - apache/blob - server/main.c
APR requires that ap_initialize is called as one of the first functions.
[apache] / server / main.c
1 /* ====================================================================
2  * Copyright (c) 1995-1999 The Apache Group.  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 Group 
19  *    for use in the Apache HTTP server project (http://www.apache.org/)." 
20  * 
21  * 4. The names "Apache Server" and "Apache Group" 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 Group. 
29  * 
30  * 6. Redistributions of any form whatsoever must retain the following 
31  *    acknowledgment: 
32  *    "This product includes software developed by the Apache Group 
33  *    for use in the Apache HTTP server project (http://www.apache.org/)." 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``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 GROUP 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 Group 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 Group 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 MULTITHREAD
151     printf(" -D MULTITHREAD\n");
152 #endif
153 #ifdef CHARSET_EBCDIC
154     printf(" -D CHARSET_EBCDIC\n");
155 #endif
156 #ifdef NEED_HASHBANG_EMUL
157     printf(" -D NEED_HASHBANG_EMUL\n");
158 #endif
159 #ifdef SHARED_CORE
160     printf(" -D SHARED_CORE\n");
161 #endif
162
163 /* This list displays the compiled in default paths: */
164 #ifdef HTTPD_ROOT
165     printf(" -D HTTPD_ROOT=\"" HTTPD_ROOT "\"\n");
166 #endif
167 #ifdef SUEXEC_BIN
168     printf(" -D SUEXEC_BIN=\"" SUEXEC_BIN "\"\n");
169 #endif
170 #if defined(SHARED_CORE) && defined(SHARED_CORE_DIR)
171     printf(" -D SHARED_CORE_DIR=\"" SHARED_CORE_DIR "\"\n");
172 #endif
173 #ifdef DEFAULT_PIDLOG
174     printf(" -D DEFAULT_PIDLOG=\"" DEFAULT_PIDLOG "\"\n");
175 #endif
176 #ifdef DEFAULT_SCOREBOARD
177     printf(" -D DEFAULT_SCOREBOARD=\"" DEFAULT_SCOREBOARD "\"\n");
178 #endif
179 #ifdef DEFAULT_LOCKFILE
180     printf(" -D DEFAULT_LOCKFILE=\"" DEFAULT_LOCKFILE "\"\n");
181 #endif
182 #ifdef DEFAULT_XFERLOG
183     printf(" -D DEFAULT_XFERLOG=\"" DEFAULT_XFERLOG "\"\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 #ifdef ACCESS_CONFIG_FILE
195     printf(" -D ACCESS_CONFIG_FILE=\"" ACCESS_CONFIG_FILE "\"\n");
196 #endif
197 #ifdef RESOURCE_CONFIG_FILE
198     printf(" -D RESOURCE_CONFIG_FILE=\"" RESOURCE_CONFIG_FILE "\"\n");
199 #endif
200 }
201
202 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
203 {
204     ap_destroy_pool(process->pool); /* and destroy all descendent pools */
205     exit(process_exit_value);
206 }
207
208 static process_rec *create_process(int argc, const char **argv)
209 {
210     process_rec *process;
211     
212     {
213         ap_context_t *cntx;
214
215         ap_create_context(&cntx, NULL);
216         process = ap_palloc(cntx, sizeof(process_rec));
217         process->pool = cntx;
218     }
219     ap_create_context(&process->pconf, process->pool);
220     process->argc = argc;
221     process->argv = argv;
222     process->short_name = ap_filename_of_pathname(argv[0]);
223     return process;
224 }
225
226 static void usage(process_rec *process)
227 {
228     const char *bin = process->argv[0];
229     char pad[MAX_STRING_LEN];
230     unsigned i;
231
232     for (i = 0; i < strlen(bin); i++)
233         pad[i] = ' ';
234     pad[i] = '\0';
235 #ifdef SHARED_CORE
236     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
237 #else
238     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
239 #endif
240     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-C \"directive\"] [-c \"directive\"]", pad);
241 #ifdef WIN32
242     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-k restart|shutdown|start]", pad);
243     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-n service_name]", pad);
244     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-i] [-u]", pad);
245 #endif
246     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-v] [-V] [-h] [-l] [-L] [-S] [-t] [-T]", pad);
247     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:");
248 #ifdef SHARED_CORE
249     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -R directory     : specify an alternate location for shared object files");
250 #endif
251     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -D name          : define a name for use in <IfDefine name> directives");
252     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -d directory     : specify an alternate initial ServerRoot");
253     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -f file          : specify an alternate ServerConfigFile");
254     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -C \"directive\"   : process directive before reading config files");
255     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -c \"directive\"   : process directive after  reading config files");
256 #ifdef WIN32
257     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -n name          : set service name and use its ServerConfigFile");
258     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k shutdown      : tell running Apache to shutdown");
259     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k restart       : tell running Apache to do a graceful restart");
260     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k start         : tell Apache to start");
261     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -i               : install an Apache service");
262     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -u               : uninstall an Apache service");
263 #endif
264     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -v               : show version number");
265     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -V               : show compile settings");
266     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -h               : list available command line options (this page)");
267     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -l               : list compiled in modules");
268     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -L               : list available configuration directives");
269     /* TODOC: -S has been replaced by '-t -D DUMP_VHOSTS' */
270     /* ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -S               : show parsed settings (currently only vhost settings)"); */
271     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t               : run syntax check for config files (with docroot check)");
272     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -T               : run syntax check for config files (without docroot check)");
273     /* TODOC: -X goes away, expect MPMs to use -D options */
274     destroy_and_exit_process(process, 1);
275 }
276
277
278
279
280
281 ap_context_t *g_pHookPool;
282
283 #ifdef WIN32
284 API_EXPORT_NONSTD(int) apache_main(int argc, char *argv[])
285 #else
286 API_EXPORT_NONSTD(int)        main(int argc, char *argv[])
287 #endif
288 {
289     int c;
290     int configtestonly = 0;
291     const char *confname = SERVER_CONFIG_FILE;
292     const char *def_server_root = HTTPD_ROOT;
293     process_rec *process = create_process(argc, (const char **)argv);
294     server_rec *server_conf;
295     ap_context_t *pglobal = process->pool;
296     ap_context_t *pconf = process->pconf;
297     ap_context_t *plog; /* Pool of log streams, reset _after_ each read of conf */
298     ap_context_t *ptemp; /* Pool for temporary config stuff, reset often */
299     ap_context_t *pcommands; /* Pool for -C and -c switches */
300
301 #ifdef WIN32
302     char *signal = NULL;
303 #endif
304
305     ap_server_argv0 = process->short_name;
306     
307     ap_util_uri_init();
308
309     g_pHookPool=pglobal;
310
311     ap_initialize();
312
313     ap_setup_prelinked_modules(process);
314
315     ap_create_context(&pcommands, pglobal);
316     ap_server_pre_read_config  = ap_make_array(pcommands, 1, sizeof(char *));
317     ap_server_post_read_config = ap_make_array(pcommands, 1, sizeof(char *));
318     ap_server_config_defines   = ap_make_array(pcommands, 1, sizeof(char *));
319
320     while (ap_getopt(pcommands, argc, argv, "C:c:d:f:k:vVlLth", &c) == APR_SUCCESS) {
321         char **new;
322         switch (c) {
323         case 'c':
324             new = (char **)ap_push_array(ap_server_post_read_config);
325             *new = ap_pstrdup(pcommands, ap_optarg);
326             break;
327         case 'C':
328             new = (char **)ap_push_array(ap_server_pre_read_config);
329             *new = ap_pstrdup(pcommands, ap_optarg);
330             break;
331         case 'd':
332             def_server_root = ap_optarg;
333             break;
334         case 'f':
335             confname = ap_optarg;
336             break;
337 #ifdef WIN32
338         case 'k':
339             signal = ap_optarg;
340             break;
341 #endif
342         case 'v':
343             printf("Server version: %s\n", ap_get_server_version());
344             printf("Server built:   %s\n", ap_get_server_built());
345             destroy_and_exit_process(process, 0);
346         case 'V':
347             show_compile_settings();
348             destroy_and_exit_process(process, 0);
349         case 'l':
350             ap_show_modules();
351             destroy_and_exit_process(process, 0);
352         case 'L':
353             ap_show_directives();
354             destroy_and_exit_process(process, 0);
355         case 't':
356             configtestonly = 1;
357             break;
358         case 'h':
359             usage(process);
360         case '?':
361             usage(process);
362         }
363     }
364
365     ap_create_context(&plog, pglobal);
366     ap_create_context(&ptemp, pconf);
367
368     /* Note that we preflight the config file once
369        before reading it _again_ in the main loop.
370        This allows things, log files configuration 
371        for example, to settle down. */
372
373     ap_server_root = def_server_root;
374     ap_run_pre_config(pconf, plog, ptemp);
375     server_conf = ap_read_config(process, ptemp, confname);
376     if (configtestonly) {
377         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n");
378         destroy_and_exit_process(process, 0);
379     }
380 #ifdef WIN32
381     if (signal) {
382         ap_signal_parent(pconf, signal, ap_server_root);
383         destroy_and_exit_process(process, 0);
384     }
385 #endif
386     ap_clear_pool(plog);
387     ap_run_open_logs(pconf, plog, ptemp, server_conf);
388     ap_post_config_hook(pconf, plog, ptemp, server_conf);
389     ap_destroy_pool(ptemp);
390
391     for (;;) {
392         ap_clear_pool(pconf);
393         ap_create_context(&ptemp, pconf);
394         ap_server_root = def_server_root;
395         ap_run_pre_config(pconf, plog, ptemp);
396         server_conf = ap_read_config(process, ptemp, confname);
397         ap_clear_pool(plog);
398         ap_run_open_logs(pconf, plog, ptemp, server_conf);
399         ap_post_config_hook(pconf, plog, ptemp, server_conf);
400         ap_destroy_pool(ptemp);
401
402         if (ap_mpm_run(pconf, plog, server_conf)) break;
403     }
404     destroy_and_exit_process(process, 0);
405     return 0; /* Supress compiler warning. */
406 }
407
408 /* force Expat to be linked into the server executable */
409 #if defined(USE_EXPAT) && !defined(SHARED_CORE_BOOTSTRAP)
410 #include "xmlparse.h"
411 const XML_LChar *suck_in_expat(void);
412 const XML_LChar *suck_in_expat(void)
413 {
414     return XML_ErrorString(XML_ERROR_NONE);
415 }
416 #endif /* USE_EXPAT */
417
418 #ifndef SHARED_CORE_BOOTSTRAP
419 /*
420  * Force ap_validate_password() into the image so that modules like
421  * mod_auth can use it even if they're dynamically loaded.
422  */
423 void suck_in_ap_validate_password(void);
424 void suck_in_ap_validate_password(void)
425 {
426     ap_validate_password("a", "b");
427 }
428 #endif
429