From: André Malo Date: Sun, 25 Apr 2004 17:23:31 +0000 (+0000) Subject: changed the following APIs to return an error instead of hard exiting: X-Git-Tag: pre_ajp_proxy~316 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=49f48fb4b47480a03bfc3c8b23bac6dd62a54838;p=apache changed the following APIs to return an error instead of hard exiting: ap_add_module, ap_add_loaded_module, ap_setup_prelinked_modules, and ap_process_resource_config git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@103517 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/CHANGES b/CHANGES index c855e73247..581b718500 100644 --- a/CHANGES +++ b/CHANGES @@ -2,7 +2,10 @@ Changes with Apache 2.1.0-dev [Remove entries to the current 2.0 section below, when backported] - *) Removed old and unmaintained ap_add_named_module API. [André Malo] + *) Removed old and unmaintained ap_add_named_module API and changed + the following APIs to return an error instead of hard exiting: + ap_add_module, ap_add_loaded_module, ap_setup_prelinked_modules, + and ap_process_resource_config. [André Malo] *) htpasswd no longer refuses to process files that contain empty lines. [André Malo] diff --git a/include/ap_mmn.h b/include/ap_mmn.h index de36df3571..3c94f4e411 100644 --- a/include/ap_mmn.h +++ b/include/ap_mmn.h @@ -81,6 +81,8 @@ * 20030821.2 (2.1.0-dev) added ap_escape_errorlog_item * 20030821.3 (2.1.0-dev) added ap_get_server_revision / ap_version_t * 20040425 (2.1.0-dev) removed ap_add_named_module API + * changed ap_add_module, ap_add_loaded_module, + * ap_setup_prelinked_modules, ap_process_resource_config */ #define MODULE_MAGIC_COOKIE 0x41503230UL /* "AP20" */ diff --git a/include/http_config.h b/include/http_config.h index 1dcadc831e..b0cda63042 100644 --- a/include/http_config.h +++ b/include/http_config.h @@ -543,7 +543,7 @@ AP_DECLARE(char *) ap_server_root_relative(apr_pool_t *p, const char *fname); * @param m The module structure of the module to add * @param p The pool of the same lifetime as the module */ -AP_DECLARE(void) ap_add_module(module *m, apr_pool_t *p); +AP_DECLARE(const char *) ap_add_module(module *m, apr_pool_t *p); /** * Remove a module from the server. There are some caveats: @@ -559,7 +559,7 @@ AP_DECLARE(void) ap_remove_module(module *m); * @param m The module structure of the module to add * @param p The pool with the same lifetime as the module */ -AP_DECLARE(void) ap_add_loaded_module(module *mod, apr_pool_t *p); +AP_DECLARE(const char *) ap_add_loaded_module(module *mod, apr_pool_t *p); /** * Remove a module fromthe chained modules list and the list of loaded modules * @param m the module structure of the module to remove @@ -740,7 +740,7 @@ AP_DECLARE(void) ap_single_module_configure(apr_pool_t *p, server_rec *s, * Add all of the prelinked modules into the loaded module list * @param process The process that is currently running the server */ -AP_DECLARE(void) ap_setup_prelinked_modules(process_rec *process); +AP_DECLARE(const char *) ap_setup_prelinked_modules(process_rec *process); /** * Show the preloaded configuration directives, the help string explaining @@ -865,9 +865,11 @@ AP_CORE_DECLARE(const char *) ap_init_virtual_host(apr_pool_t *p, * @param p Pool for general allocation * @param ptem Pool for temporary allocation */ -AP_DECLARE(void) ap_process_resource_config(server_rec *s, const char *fname, - ap_directive_t **conftree, - apr_pool_t *p, apr_pool_t *ptemp); +AP_DECLARE(const char *) ap_process_resource_config(server_rec *s, + const char *fname, + ap_directive_t **conftree, + apr_pool_t *p, + apr_pool_t *ptemp); /** * Process all directives in the config tree diff --git a/modules/mappers/mod_so.c b/modules/mappers/mod_so.c index 32dfe16c59..578b170a05 100644 --- a/modules/mappers/mod_so.c +++ b/modules/mappers/mod_so.c @@ -162,6 +162,7 @@ static const char *load_module(cmd_parms *cmd, void *dummy, moduleinfo *modi; moduleinfo *modie; int i; + const char *error; /* we need to setup this value for dummy to make sure that we don't try * to add a non-existant tree into the build when we return to @@ -277,7 +278,10 @@ static const char *load_module(cmd_parms *cmd, void *dummy, /* * Add this module to the Apache core structures */ - ap_add_loaded_module(modp, cmd->pool); + error = ap_add_loaded_module(modp, cmd->pool); + if (error) { + return error; + } /* * Register a cleanup in the config apr_pool_t (normally pconf). When diff --git a/server/config.c b/server/config.c index 7bd60d077a..79e5f7205d 100644 --- a/server/config.c +++ b/server/config.c @@ -404,7 +404,7 @@ AP_DECLARE(void) ap_register_hooks(module *m, apr_pool_t *p) /* One-time setup for precompiled modules --- NOT to be done on restart */ -AP_DECLARE(void) ap_add_module(module *m, apr_pool_t *p) +AP_DECLARE(const char *) ap_add_module(module *m, apr_pool_t *p) { /* This could be called from a LoadModule httpd.conf command, * after the file has been linked and the module structure within it @@ -412,14 +412,10 @@ AP_DECLARE(void) ap_add_module(module *m, apr_pool_t *p) */ if (m->version != MODULE_MAGIC_NUMBER_MAJOR) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "%s: module \"%s\" is not compatible with this " - "version of Apache (found %d, need %d).", - ap_server_argv0, m->name, m->version, - MODULE_MAGIC_NUMBER_MAJOR); - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "Please contact the vendor for the correct version."); - exit(1); + return apr_psprintf(p, "Module \"%s\" is not compatible with this " + "version of Apache (found %d, need %d). Please " + "contact the vendor for the correct version.", + m->name, m->version, MODULE_MAGIC_NUMBER_MAJOR); } if (m->next == NULL) { @@ -432,13 +428,10 @@ AP_DECLARE(void) ap_add_module(module *m, apr_pool_t *p) dynamic_modules++; if (dynamic_modules > DYNAMIC_MODULE_LIMIT) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "%s: module \"%s\" could not be loaded, because" - " the dynamic", ap_server_argv0, m->name); - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "module limit was reached. Please increase " - "DYNAMIC_MODULE_LIMIT and recompile."); - exit(1); + return apr_psprintf(p, "Module \"%s\" could not be loaded, " + "because the dynamic module limit was " + "reached. Please increase " + "DYNAMIC_MODULE_LIMIT and recompile.", m->name); } } @@ -470,6 +463,8 @@ AP_DECLARE(void) ap_add_module(module *m, apr_pool_t *p) * It doesn't appear to be */ ap_register_hooks(m, p); + + return NULL; } /* @@ -517,14 +512,18 @@ AP_DECLARE(void) ap_remove_module(module *m) total_modules--; } -AP_DECLARE(void) ap_add_loaded_module(module *mod, apr_pool_t *p) +AP_DECLARE(const char *) ap_add_loaded_module(module *mod, apr_pool_t *p) { module **m; + const char *error; /* * Add module pointer to top of chained module list */ - ap_add_module(mod, p); + error = ap_add_module(mod, p); + if (error) { + return error; + } /* * And module pointer to list of loaded modules @@ -538,6 +537,8 @@ AP_DECLARE(void) ap_add_loaded_module(module *mod, apr_pool_t *p) ; *m++ = mod; *m = NULL; + + return NULL; } AP_DECLARE(void) ap_remove_loaded_module(module *mod) @@ -570,10 +571,11 @@ AP_DECLARE(void) ap_remove_loaded_module(module *mod) *m = NULL; } -AP_DECLARE(void) ap_setup_prelinked_modules(process_rec *process) +AP_DECLARE(const char *) ap_setup_prelinked_modules(process_rec *process) { module **m; module **m2; + const char *error; apr_hook_global_pool=process->pconf; @@ -591,8 +593,7 @@ AP_DECLARE(void) ap_setup_prelinked_modules(process_rec *process) sizeof(module *) * (total_modules + DYNAMIC_MODULE_LIMIT + 1)); if (ap_loaded_modules == NULL) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "Ouch! Out of memory in ap_setup_prelinked_modules()!"); + return "Ouch! Out of memory in ap_setup_prelinked_modules()!"; } for (m = ap_preloaded_modules, m2 = ap_loaded_modules; *m != NULL; ) @@ -603,10 +604,16 @@ AP_DECLARE(void) ap_setup_prelinked_modules(process_rec *process) /* * Initialize chain of linked (=activate) modules */ - for (m = ap_prelinked_modules; *m != NULL; m++) - ap_add_module(*m, process->pconf); + for (m = ap_prelinked_modules; *m != NULL; m++) { + error = ap_add_module(*m, process->pconf); + if (error) { + return error; + } + } apr_hook_sort_all(); + + return NULL; } AP_DECLARE(const char *) ap_find_module_name(module *m) @@ -1359,9 +1366,11 @@ static int arr_elts_close(void *param) return 0; } -static void process_command_config(server_rec *s, apr_array_header_t *arr, - ap_directive_t **conftree, apr_pool_t *p, - apr_pool_t *ptemp) +static const char *process_command_config(server_rec *s, + apr_array_header_t *arr, + ap_directive_t **conftree, + apr_pool_t *p, + apr_pool_t *ptemp) { const char *errmsg; cmd_parms parms; @@ -1381,14 +1390,14 @@ static void process_command_config(server_rec *s, apr_array_header_t *arr, arr_elts_getstr, arr_elts_close); errmsg = ap_build_config(&parms, p, ptemp, conftree); + ap_cfg_closefile(parms.config_file); + if (errmsg) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "Syntax error in -C/-c directive:" APR_EOL_STR "%s", - errmsg); - exit(1); + return apr_pstrcat(p, "Syntax error in -C/-c directive: ", errmsg, + NULL); } - ap_cfg_closefile(parms.config_file); + return NULL; } typedef struct { @@ -1403,15 +1412,16 @@ static int fname_alphasort(const void *fn1, const void *fn2) return strcmp(f1->fname,f2->fname); } -static void process_resource_config_nofnmatch(server_rec *s, const char *fname, - ap_directive_t **conftree, - apr_pool_t *p, - apr_pool_t *ptemp, - unsigned depth) +static const char *process_resource_config_nofnmatch(server_rec *s, + const char *fname, + ap_directive_t **conftree, + apr_pool_t *p, + apr_pool_t *ptemp, + unsigned depth) { cmd_parms parms; ap_configfile_t *cfp; - const char *errmsg; + const char *error; if (ap_is_directory(p, fname)) { apr_dir_t *dirp; @@ -1420,14 +1430,13 @@ static void process_resource_config_nofnmatch(server_rec *s, const char *fname, apr_array_header_t *candidates = NULL; fnames *fnew; apr_status_t rv; - char errmsg[120], *path = apr_pstrdup(p, fname); + char *path = apr_pstrdup(p, fname); if (++depth > AP_MAX_INCLUDE_DIR_DEPTH) { - fprintf(stderr, "%s: Directory %s exceeds the maximum include " - "directory nesting level of %u. You have probably a " - "recursion somewhere.\n", ap_server_argv0, path, - AP_MAX_INCLUDE_DIR_DEPTH); - exit(1); + return apr_psprintf(p, "Directory %s exceeds the maximum include " + "directory nesting level of %u. You have " + "probably a recursion somewhere.", path, + AP_MAX_INCLUDE_DIR_DEPTH); } /* @@ -1437,10 +1446,9 @@ static void process_resource_config_nofnmatch(server_rec *s, const char *fname, */ rv = apr_dir_open(&dirp, path, p); if (rv != APR_SUCCESS) { - fprintf(stderr, "%s: could not open config directory %s: %s\n", - ap_server_argv0, path, - apr_strerror(rv, errmsg, sizeof errmsg)); - exit(1); + char errmsg[120]; + return apr_psprintf(p, "Could not open config directory %s: %s", + path, apr_strerror(rv, errmsg, sizeof errmsg)); } candidates = apr_array_make(p, 1, sizeof(fnames)); @@ -1464,12 +1472,16 @@ static void process_resource_config_nofnmatch(server_rec *s, const char *fname, */ for (current = 0; current < candidates->nelts; ++current) { fnew = &((fnames *) candidates->elts)[current]; - process_resource_config_nofnmatch(s, fnew->fname, conftree, p, - ptemp, depth); + error = process_resource_config_nofnmatch(s, fnew->fname, + conftree, p, ptemp, + depth); + if (error) { + return error; + } } } - return; + return NULL; } /* GCC's initialization extensions are soooo nice here... */ @@ -1480,35 +1492,28 @@ static void process_resource_config_nofnmatch(server_rec *s, const char *fname, parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT); if (ap_pcfg_openfile(&cfp, p, fname) != APR_SUCCESS) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "%s: could not open document config file %s", - ap_server_argv0, fname); - exit(1); + return apr_pstrcat(p, "Could not open document config file ", + fname, NULL); } parms.config_file = cfp; + error = ap_build_config(&parms, p, ptemp, conftree); + ap_cfg_closefile(cfp); - errmsg = ap_build_config(&parms, p, ptemp, conftree); - - if (errmsg != NULL) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "Syntax error on line %d of %s:", - parms.err_directive->line_num, - parms.err_directive->filename); - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, - "%s", errmsg); - exit(1); + if (error) { + return apr_psprintf(p, "Syntax error on line %d of %s: %s", + parms.err_directive->line_num, + parms.err_directive->filename, error); } - ap_cfg_closefile(cfp); - - return; + return NULL; } -AP_DECLARE(void) ap_process_resource_config(server_rec *s, const char *fname, - ap_directive_t **conftree, - apr_pool_t *p, - apr_pool_t *ptemp) +AP_DECLARE(const char *) ap_process_resource_config(server_rec *s, + const char *fname, + ap_directive_t **conftree, + apr_pool_t *p, + apr_pool_t *ptemp) { /* XXX: lstat() won't work on the wildcard pattern... */ @@ -1520,11 +1525,12 @@ AP_DECLARE(void) ap_process_resource_config(server_rec *s, const char *fname, apr_finfo_t finfo; if (apr_stat(&finfo, fname, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS) - return; + return NULL; } if (!apr_fnmatch_test(fname)) { - process_resource_config_nofnmatch(s, fname, conftree, p, ptemp, 0); + return process_resource_config_nofnmatch(s, fname, conftree, p, ptemp, + 0); } else { apr_dir_t *dirp; @@ -1533,7 +1539,7 @@ AP_DECLARE(void) ap_process_resource_config(server_rec *s, const char *fname, apr_array_header_t *candidates = NULL; fnames *fnew; apr_status_t rv; - char errmsg[120], *path = apr_pstrdup(p, fname), *pattern = NULL; + char *path = apr_pstrdup(p, fname), *pattern = NULL; pattern = ap_strrchr(path, '/'); @@ -1542,21 +1548,18 @@ AP_DECLARE(void) ap_process_resource_config(server_rec *s, const char *fname, *pattern++ = '\0'; if (apr_fnmatch_test(path)) { - fprintf(stderr, "%s: wildcard patterns not allowed in Include " - "%s\n", ap_server_argv0, fname); - exit(1); + return apr_pstrcat(p, "Wildcard patterns not allowed in Include ", + fname, NULL); } if (!ap_is_directory(p, path)){ - fprintf(stderr, "%s: Include directory '%s' not found", - ap_server_argv0, path); - exit(1); + return apr_pstrcat(p, "Include directory '", path, "' not found", + NULL); } if (!apr_fnmatch_test(pattern)) { - fprintf(stderr, "%s: must include a wildcard pattern " - "for Include %s\n", ap_server_argv0, fname); - exit(1); + return apr_pstrcat(p, "Must include a wildcard pattern for " + "Include ", fname, NULL); } /* @@ -1566,10 +1569,9 @@ AP_DECLARE(void) ap_process_resource_config(server_rec *s, const char *fname, */ rv = apr_dir_open(&dirp, path, p); if (rv != APR_SUCCESS) { - fprintf(stderr, "%s: could not open config directory %s: %s\n", - ap_server_argv0, path, - apr_strerror(rv, errmsg, sizeof errmsg)); - exit(1); + char errmsg[120]; + return apr_psprintf(p, "Could not open config directory %s: %s", + path, apr_strerror(rv, errmsg, sizeof errmsg)); } candidates = apr_array_make(p, 1, sizeof(fnames)); @@ -1586,6 +1588,8 @@ AP_DECLARE(void) ap_process_resource_config(server_rec *s, const char *fname, apr_dir_close(dirp); if (candidates->nelts != 0) { + const char *error; + qsort((void *) candidates->elts, candidates->nelts, sizeof(fnames), fname_alphasort); @@ -1595,13 +1599,17 @@ AP_DECLARE(void) ap_process_resource_config(server_rec *s, const char *fname, */ for (current = 0; current < candidates->nelts; ++current) { fnew = &((fnames *) candidates->elts)[current]; - process_resource_config_nofnmatch(s, fnew->fname, conftree, p, - ptemp, 0); + error = process_resource_config_nofnmatch(s, fnew->fname, + conftree, p, + ptemp, 0); + if (error) { + return error; + } } } } - return; + return NULL; } AP_DECLARE(int) ap_process_config_tree(server_rec *s, @@ -1848,15 +1856,20 @@ AP_DECLARE(server_rec*) ap_read_config(process_rec *process, apr_pool_t *ptemp, const char *filename, ap_directive_t **conftree) { - const char *confname; + const char *confname, *error; apr_pool_t *p = process->pconf; server_rec *s = init_server_config(process, p); init_config_globals(p); /* All server-wide config files now have the SAME syntax... */ - process_command_config(s, ap_server_pre_read_config, conftree, - p, ptemp); + error = process_command_config(s, ap_server_pre_read_config, conftree, + p, ptemp); + if (error) { + ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s", + ap_server_argv0, error); + return NULL; + } /* process_command_config may change the ServerRoot so * compute this config file name afterwards. @@ -1867,13 +1880,24 @@ AP_DECLARE(server_rec*) ap_read_config(process_rec *process, apr_pool_t *ptemp, ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH, NULL, "Invalid config file path %s", filename); - exit(1); + return NULL; } - ap_process_resource_config(s, confname, conftree, p, ptemp); + error = ap_process_resource_config(s, confname, conftree, p, ptemp); + if (error) { + ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, + "%s: %s", ap_server_argv0, error); + return NULL; + } + + error = process_command_config(s, ap_server_post_read_config, conftree, + p, ptemp); - process_command_config(s, ap_server_post_read_config, conftree, - p, ptemp); + if (error) { + ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s", + ap_server_argv0, error); + return NULL; + } return s; } diff --git a/server/core.c b/server/core.c index c6182b7c5a..6277477d88 100644 --- a/server/core.c +++ b/server/core.c @@ -2250,7 +2250,7 @@ static const char *include_config (cmd_parms *cmd, void *dummy, const char *name) { ap_directive_t *conftree = NULL; - const char* conffile; + const char* conffile, *error; unsigned *recursion; void *data; @@ -2278,8 +2278,13 @@ static const char *include_config (cmd_parms *cmd, void *dummy, name, NULL); } - ap_process_resource_config(cmd->server, conffile, - &conftree, cmd->pool, cmd->temp_pool); + error = ap_process_resource_config(cmd->server, conffile, + &conftree, cmd->pool, cmd->temp_pool); + if (error) { + *recursion = 0; + return error; + } + *(ap_directive_t **)dummy = conftree; /* recursion level done */ diff --git a/server/main.c b/server/main.c index dce6d23284..69e8bde00b 100644 --- a/server/main.c +++ b/server/main.c @@ -399,6 +399,7 @@ int main(int argc, const char * const argv[]) const char *confname = SERVER_CONFIG_FILE; const char *def_server_root = HTTPD_ROOT; const char *temp_error_log = NULL; + const char *error; process_rec *process; server_rec *server_conf; apr_pool_t *pglobal; @@ -427,7 +428,12 @@ int main(int argc, const char * const argv[]) } #endif - ap_setup_prelinked_modules(process); + error = ap_setup_prelinked_modules(process); + if (error) { + ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_EMERG, 0, NULL, "%s: %s", + ap_server_argv0, error); + destroy_and_exit_process(process, 1); + } apr_pool_create(&pcommands, pglobal); apr_pool_tag(pcommands, "pcommands"); @@ -566,6 +572,10 @@ int main(int argc, const char * const argv[]) ap_replace_stderr_log(process->pool, temp_error_log); } server_conf = ap_read_config(process, ptemp, confname, &ap_conftree); + if (!server_conf) { + destroy_and_exit_process(process, 1); + } + if (ap_run_pre_config(pconf, plog, ptemp) != OK) { ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0, NULL, "Pre-configuration failed"); @@ -631,6 +641,10 @@ int main(int argc, const char * const argv[]) apr_pool_tag(ptemp, "ptemp"); ap_server_root = def_server_root; server_conf = ap_read_config(process, ptemp, confname, &ap_conftree); + if (!server_conf) { + destroy_and_exit_process(process, 1); + } + if (ap_run_pre_config(pconf, plog, ptemp) != OK) { ap_log_error(APLOG_MARK, APLOG_STARTUP |APLOG_ERR, 0, NULL, "Pre-configuration failed");