QHostInfo: use dlsym() with RTLD_DEFAULT in case libs cannot be loaded

The current code only tries to load the required functions from
LIBRESOLV_SO (if defined) and resolv, but on FreeBSD they are in libc:
https://www.freebsd.org/cgi/man.cgi?query=res_query&sektion=3&apropos=0&manpath=freebsd

This commit changes the code so that, after failing to load the
non-existent libraries, it attempts to load the functions with dlsym()
using the special handle RTLD_DEFAULT, which searches for the specified
symbol in the loaded libraries.

This is a follow-up to 8eeb5150ed.

Change-Id: I19d90b0ca8703398bf4f5f4edd5ae31e346ef251
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Davide Beatrici 2019-04-06 08:01:11 +02:00
parent 8eeb5150ed
commit e199710d0e
2 changed files with 27 additions and 13 deletions

View File

@ -38,13 +38,12 @@ qtConfig(dnslookup) {
}
unix {
!integrity:qtConfig(dnslookup) {
SOURCES += kernel/qdnslookup_unix.cpp
qtConfig(dlopen): QMAKE_USE_PRIVATE += libdl
}
!integrity:qtConfig(dnslookup): SOURCES += kernel/qdnslookup_unix.cpp
SOURCES += kernel/qhostinfo_unix.cpp
qtConfig(dlopen): QMAKE_USE_PRIVATE += libdl
qtConfig(linux-netlink): SOURCES += kernel/qnetworkinterface_linux.cpp
else: SOURCES += kernel/qnetworkinterface_unix.cpp
}

View File

@ -66,6 +66,10 @@
# include <gnu/lib-names.h>
#endif
#if defined(Q_OS_FREEBSD) || QT_CONFIG(dlopen)
# include <dlfcn.h>
#endif
QT_BEGIN_NAMESPACE
// Almost always the same. If not, specify in qplatformdefs.h.
@ -115,6 +119,18 @@ struct LibResolv
};
}
static QFunctionPointer resolveSymbol(QLibrary &lib, const char *sym)
{
if (lib.isLoaded())
return lib.resolve(sym);
#if defined(RTLD_DEFAULT) && (defined(Q_OS_FREEBSD) || QT_CONFIG(dlopen))
return reinterpret_cast<QFunctionPointer>(dlsym(RTLD_DEFAULT, sym));
#else
return nullptr;
#endif
}
LibResolv::LibResolv()
{
QLibrary lib;
@ -124,31 +140,30 @@ LibResolv::LibResolv()
#endif
{
lib.setFileName(QLatin1String("resolv"));
if (!lib.load())
return;
lib.load();
}
// res_ninit is required for localDomainName()
local_res_ninit = res_ninit_proto(lib.resolve("__res_ninit"));
local_res_ninit = res_ninit_proto(resolveSymbol(lib, "__res_ninit"));
if (!local_res_ninit)
local_res_ninit = res_ninit_proto(lib.resolve("res_ninit"));
local_res_ninit = res_ninit_proto(resolveSymbol(lib, "res_ninit"));
if (local_res_ninit) {
// we must now find res_nclose
local_res_nclose = res_nclose_proto(lib.resolve("res_nclose"));
local_res_nclose = res_nclose_proto(resolveSymbol(lib, "res_nclose"));
if (!local_res_nclose)
local_res_nclose = res_nclose_proto(lib.resolve("__res_nclose"));
local_res_nclose = res_nclose_proto(resolveSymbol(lib, "__res_nclose"));
if (!local_res_nclose)
local_res_ninit = nullptr;
}
if (ReinitNecessary || !local_res_ninit) {
local_res_init = res_init_proto(lib.resolve("__res_init"));
local_res_init = res_init_proto(resolveSymbol(lib, "__res_init"));
if (!local_res_init)
local_res_init = res_init_proto(lib.resolve("res_init"));
local_res_init = res_init_proto(resolveSymbol(lib, "res_init"));
if (local_res_init && !local_res_ninit) {
// if we can't get a thread-safe context, we have to use the global _res state
local_res = res_state_ptr(lib.resolve("_res"));
local_res = res_state_ptr(resolveSymbol(lib, "_res"));
}
}
}