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