From: Arran Cudbard-Bell Date: Tue, 30 Jun 2015 16:26:49 +0000 (-0400) Subject: Fix dlopen wrapper so it produces proper error messages X-Git-Tag: release_3_0_9~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a210c31dac60e9f58c6ce83cd203741479d71b7;p=thirdparty%2Ffreeradius-server.git Fix dlopen wrapper so it produces proper error messages --- diff --git a/src/main/modules.c b/src/main/modules.c index 355adf6b72f..dae684cf3bc 100644 --- a/src/main/modules.c +++ b/src/main/modules.c @@ -138,10 +138,10 @@ static int check_module_magic(CONF_SECTION *cs, module_t const *module) lt_dlhandle lt_dlopenext(char const *name) { - int flags = RTLD_NOW; - void *handle; - char buffer[2048]; - char *env; + int flags = RTLD_NOW; + void *handle; + char buffer[2048]; + char *env; #ifdef RTLD_GLOBAL if (strcmp(name, "rlm_perl") == 0) { @@ -159,31 +159,42 @@ lt_dlhandle lt_dlopenext(char const *name) /* * Prefer loading our libraries by absolute path. */ - snprintf(buffer, sizeof(buffer), "%s/%s%s", radlib_dir, name, LT_SHREXT); + if (radlib_dir) { + char *error; - DEBUG4("Loading library using absolute path \"%s\"", name); + snprintf(buffer, sizeof(buffer), "%s/%s%s", radlib_dir, name, LT_SHREXT); - handle = dlopen(buffer, flags); - if (handle) return handle; + DEBUG4("Loading library using absolute path \"%s\"", buffer); - /* - * Because dlopen produces really shitty and inaccurate error messages - */ - if (access(name, R_OK) < 0) switch (errno) { - case EACCES: - WARN("Library file found, but we don't have permission to read it"); - break; - - case ENOENT: - DEBUG4("Library file not found"); - break; - - default: - DEBUG4("Issue accessing library file: %s", fr_syserror(errno)); - break; + handle = dlopen(buffer, flags); + if (handle) return handle; + error = dlerror(); + + fr_strerror_printf("%s", error); + DEBUG4("Failed with error: %s", error); + + /* + * Because dlopen (on OSX at least) produces really + * shitty and inaccurate error messages + */ + if (access(buffer, R_OK) < 0) { + switch (errno) { + case EACCES: + WARN("Library file found, but we don't have permission to read it"); + break; + + case ENOENT: + DEBUG4("Library file not found"); + break; + + default: + DEBUG4("Issue accessing library file: %s", fr_syserror(errno)); + break; + } + } } - DEBUG4("Falling back to linker search path(s)"); + DEBUG4("Loading library using linker search path(s)"); if (DEBUG_ENABLED4) { #ifdef __APPLE__ env = getenv("LD_LIBRARY_PATH"); @@ -217,7 +228,21 @@ lt_dlhandle lt_dlopenext(char const *name) */ strlcat(buffer, LT_SHREXT, sizeof(buffer)); - return dlopen(buffer, flags); + handle = dlopen(buffer, flags); + if (!handle) { + char *error = dlerror(); + + DEBUG4("Failed with error: %s", error); + /* + * Don't overwrite the previous message + * It's likely to contain a better error. + */ + if (!radlib_dir) { + fr_strerror_printf("%s", dlerror()); + } + return NULL; + } + return handle; } void *lt_dlsym(lt_dlhandle handle, char const *symbol) @@ -462,7 +487,7 @@ static module_entry_t *module_dlopen(CONF_SECTION *cs, char const *module_name) */ handle = lt_dlopenext(module_name); if (!handle) { - cf_log_err_cs(cs, "Failed to link to module '%s': %s", module_name, dlerror()); + cf_log_err_cs(cs, "Failed to link to module '%s': %s", module_name, fr_strerror()); return NULL; } diff --git a/src/modules/rlm_eap/eap.c b/src/modules/rlm_eap/eap.c index ff46b7c04cb..1d6ee2e4e8e 100644 --- a/src/modules/rlm_eap/eap.c +++ b/src/modules/rlm_eap/eap.c @@ -132,7 +132,7 @@ int eap_module_instantiate(rlm_eap_t *inst, eap_module_t **m_inst, eap_type_t nu */ method->handle = lt_dlopenext(mod_name); if (!method->handle) { - ERROR("rlm_eap (%s): Failed to link %s: %s", inst->xlat_name, mod_name, lt_dlerror()); + ERROR("rlm_eap (%s): Failed to link %s: %s", inst->xlat_name, mod_name, fr_strerror()); return -1; } diff --git a/src/modules/rlm_sql/rlm_sql.c b/src/modules/rlm_sql/rlm_sql.c index 8beedd79470..56df54be656 100644 --- a/src/modules/rlm_sql/rlm_sql.c +++ b/src/modules/rlm_sql/rlm_sql.c @@ -827,7 +827,7 @@ static int mod_bootstrap(CONF_SECTION *conf, void *instance) */ inst->handle = lt_dlopenext(inst->config->sql_driver_name); if (!inst->handle) { - ERROR("Could not link driver %s: %s", inst->config->sql_driver_name, dlerror()); + ERROR("Could not link driver %s: %s", inst->config->sql_driver_name, fr_strerror()); ERROR("Make sure it (and all its dependent libraries!) are in the search path of your system's ld"); return -1; }