]> granicus.if.org Git - apache/blob - server/main.c
PR:
[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 /* WARNING: Win32 binds http_main.c dynamically to the server. Please place 
71  *          extern functions and global data in another appropriate module.
72  *
73  * Most significant main() global data can be found in http_config.c
74  */
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 APR_HAS_OTHER_CHILD
139     printf(" -D APR_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 CHARSET_EBCDIC
151     printf(" -D CHARSET_EBCDIC\n");
152 #endif
153 #ifdef APACHE_XLATE
154     printf(" -D APACHE_XLATE\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 }
195
196 static void destroy_and_exit_process(process_rec *process, int process_exit_value)
197 {
198     ap_destroy_pool(process->pool); /* and destroy all descendent pools */
199     ap_terminate();
200     exit(process_exit_value);
201 }
202
203 static process_rec *create_process(int argc, const char **argv)
204 {
205     process_rec *process;
206     
207     {
208         ap_pool_t *cntx;
209         ap_status_t stat;
210
211         stat = ap_create_pool(&cntx, NULL);
212         if (stat != APR_SUCCESS) {
213             ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
214                          "ap_create_pool() failed to create "
215                          "initial context");
216             ap_terminate();
217             exit(1);
218         }
219
220         process = ap_palloc(cntx, sizeof(process_rec));
221         process->pool = cntx;
222     }
223     ap_create_pool(&process->pconf, process->pool);
224     process->argc = argc;
225     process->argv = argv;
226     process->short_name = ap_filename_of_pathname(argv[0]);
227     return process;
228 }
229
230 static void usage(process_rec *process)
231 {
232     const char *bin = process->argv[0];
233     char pad[MAX_STRING_LEN];
234     unsigned i;
235
236     for (i = 0; i < strlen(bin); i++)
237         pad[i] = ' ';
238     pad[i] = '\0';
239 #ifdef SHARED_CORE
240     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0,NULL , "Usage: %s [-R directory] [-D name] [-d directory] [-f file]", bin);
241 #else
242     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin);
243 #endif
244     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-C \"directive\"] [-c \"directive\"]", pad);
245 #ifdef WIN32
246     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-k restart|shutdown|start]", pad);
247     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-n service_name]", pad);
248     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-i] [-u]", pad);
249 #endif
250     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "       %s [-v] [-V] [-h] [-l] [-L] [-S] [-t] [-T]", pad);
251     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:");
252 #ifdef SHARED_CORE
253     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -R directory     : specify an alternate location for shared object files");
254 #endif
255     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -D name          : define a name for use in <IfDefine name> directives");
256     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -d directory     : specify an alternate initial ServerRoot");
257     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -f file          : specify an alternate ServerConfigFile");
258     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -C \"directive\"   : process directive before reading config files");
259     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -c \"directive\"   : process directive after  reading config files");
260 #ifdef WIN32
261     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -n name          : set service name and use its ServerConfigFile");
262     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k shutdown      : tell running Apache to shutdown");
263     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k restart       : tell running Apache to do a graceful restart");
264     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -k start         : tell Apache to start");
265     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -i               : install an Apache service");
266     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -u               : uninstall an Apache service");
267 #endif
268     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -v               : show version number");
269     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -V               : show compile settings");
270     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -h               : list available command line options (this page)");
271     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -l               : list compiled in modules");
272     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -L               : list available configuration directives");
273     /* TODOC: -S has been replaced by '-t -D DUMP_VHOSTS' */
274     /* ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -S               : show parsed settings (currently only vhost settings)"); */
275     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -t               : run syntax check for config files (with docroot check)");
276     ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "  -T               : run syntax check for config files (without docroot check)");
277     /* TODOC: -X goes away, expect MPMs to use -D options */
278     destroy_and_exit_process(process, 1);
279 }
280
281 #ifdef WIN32
282 int apache_main(int argc, char *argv[])
283 #else
284 API_EXPORT(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_pool_t *pglobal;
294     ap_pool_t *pconf;
295     ap_pool_t *plog; /* Pool of log streams, reset _after_ each read of conf */
296     ap_pool_t *ptemp; /* Pool for temporary config stuff, reset often */
297     ap_pool_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 #ifdef CHARSET_EBCDIC
309     if (ap_init_ebcdic(pglobal) != APR_SUCCESS) {
310         destroy_and_exit_process(process, 1);
311     }
312 #endif
313
314     ap_util_uri_init();
315
316     ap_setup_prelinked_modules(process);
317
318     ap_create_pool(&pcommands, pglobal);
319     ap_server_pre_read_config  = ap_make_array(pcommands, 1, sizeof(char *));
320     ap_server_post_read_config = ap_make_array(pcommands, 1, sizeof(char *));
321     ap_server_config_defines   = ap_make_array(pcommands, 1, sizeof(char *));
322
323     ap_run_rewrite_args(process);
324
325     while (ap_getopt(argc, argv, "C:c:D:d:f:vVlLth?", &c, pcommands) == APR_SUCCESS) {
326         char **new;
327         switch (c) {
328         case 'c':
329             new = (char **)ap_push_array(ap_server_post_read_config);
330             *new = ap_pstrdup(pcommands, ap_optarg);
331             break;
332         case 'C':
333             new = (char **)ap_push_array(ap_server_pre_read_config);
334             *new = ap_pstrdup(pcommands, ap_optarg);
335             break;
336         case 'd':
337             def_server_root = ap_optarg;
338             break;
339         case 'D':
340             new = (char **)ap_push_array(ap_server_config_defines);
341             *new = ap_pstrdup(pcommands, ap_optarg);
342             break;
343         case 'f':
344             confname = ap_optarg;
345             break;
346         case 'v':
347             printf("Server version: %s\n", ap_get_server_version());
348             printf("Server built:   %s\n", ap_get_server_built());
349             destroy_and_exit_process(process, 0);
350         case 'V':
351             show_compile_settings();
352             destroy_and_exit_process(process, 0);
353         case 'l':
354             ap_show_modules();
355             destroy_and_exit_process(process, 0);
356         case 'L':
357             ap_show_directives();
358             destroy_and_exit_process(process, 0);
359         case 't':
360             configtestonly = 1;
361             break;
362         case '?':
363         case 'h':
364             usage(process);
365         }
366     }
367
368     ap_create_pool(&plog, pglobal);
369     ap_create_pool(&ptemp, pconf);
370
371     /* Note that we preflight the config file once
372        before reading it _again_ in the main loop.
373        This allows things, log files configuration 
374        for example, to settle down. */
375
376     ap_server_root = def_server_root;
377     ap_run_pre_config(pconf, plog, ptemp);
378     server_conf = ap_read_config(process, ptemp, confname);
379     if (configtestonly) {
380         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n");
381         destroy_and_exit_process(process, 0);
382     }
383     ap_clear_pool(plog);
384     ap_run_open_logs(pconf, plog, ptemp, server_conf);
385     ap_post_config_hook(pconf, plog, ptemp, server_conf);
386     ap_destroy_pool(ptemp);
387
388     for (;;) {
389         ap_hook_deregister_all();
390         ap_clear_pool(pconf);
391         for (mod = ap_prelinked_modules; *mod != NULL; mod++) {
392                 ap_register_hooks(*mod);
393         }
394         ap_create_pool(&ptemp, pconf);
395         ap_server_root = def_server_root;
396         ap_run_pre_config(pconf, plog, ptemp);
397         server_conf = ap_read_config(process, ptemp, confname);
398         ap_clear_pool(plog);
399         ap_run_open_logs(pconf, plog, ptemp, server_conf);
400         ap_post_config_hook(pconf, plog, ptemp, server_conf);
401         ap_destroy_pool(ptemp);
402
403         if (ap_mpm_run(pconf, plog, server_conf)) break;
404     }
405     destroy_and_exit_process(process, 0);
406     return 0; /* Supress compiler warning. */
407 }
408
409 /* force Expat to be linked into the server executable */
410 #if defined(USE_EXPAT) && !defined(SHARED_CORE_BOOTSTRAP)
411 #include "xmlparse.h"
412 const XML_LChar *suck_in_expat(void);
413 const XML_LChar *suck_in_expat(void)
414 {
415     return XML_ErrorString(XML_ERROR_NONE);
416 }
417 #endif /* USE_EXPAT */
418
419 #ifndef SHARED_CORE_BOOTSTRAP
420 /*
421  * Force ap_validate_password() into the image so that modules like
422  * mod_auth can use it even if they're dynamically loaded.
423  */
424 void suck_in_ap_validate_password(void);
425 void suck_in_ap_validate_password(void)
426 {
427     ap_validate_password("a", "b");
428 }
429 #endif
430