Skip to content

Commit

Permalink
[common] Conditionalize RTLD_DI_ORIGIN
Browse files Browse the repository at this point in the history
RTLD_DI_ORIGIN is used to get absolute path of plugin. It is used only
for logging useful info and not strictly needed, so use it only when it
is defined (only musl is known to author of the patch)

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
  • Loading branch information
jfriesse authored and fabbione committed Sep 3, 2019
1 parent c798671 commit f907ee4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions configure.ac
Expand Up @@ -205,6 +205,10 @@ AC_SEARCH_LIBS([dlopen], [dl dld], , [AC_MSG_ERROR([dlopen not found])])
AC_SUBST([dl_LIBS], [$LIBS])
LIBS="$saved_LIBS"

# Check RTLD_DI_ORIGIN (not decalred by musl. glibc has it as an enum so cannot use ifdef)
AC_CHECK_DECL([RTLD_DI_ORIGIN], [AC_DEFINE([HAVE_RTLD_DI_ORIGIN], 1,
[define when RTLD_DI_ORIGIN is declared])], ,[[#include <dlfcn.h>]])

# OS detection

AC_MSG_CHECKING([for os in ${host_os}])
Expand Down
28 changes: 27 additions & 1 deletion libknet/common.c
Expand Up @@ -12,6 +12,8 @@
#include <fcntl.h>
#include <dlfcn.h>
#include <errno.h>
#include <libgen.h>
#include <link.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
Expand Down Expand Up @@ -54,6 +56,30 @@ int _fdset_nonblock(int fd)
return 0;
}

static int get_lib_dir(void *lib_handle, char dir[MAXPATHLEN])
{
int res;
#ifndef HAVE_RTLD_DI_ORIGIN
struct link_map *lm;
char l_name[MAXPATHLEN];
#endif

#ifdef HAVE_RTLD_DI_ORIGIN
res = dlinfo(lib_handle, RTLD_DI_ORIGIN, dir);
#else
/*
* musl libc doesn't support RTLD_DI_ORIGIN
*/
res = dlinfo(lib_handle, RTLD_DI_LINKMAP, &lm);
if (res == 0) {
snprintf(l_name, sizeof(l_name), "%s", lm->l_name);
snprintf(dir, MAXPATHLEN, "%s", dirname(l_name));
}
#endif

return res;
}

static void *open_lib(knet_handle_t knet_h, const char *libname, int extra_flags)
{
void *ret = NULL;
Expand Down Expand Up @@ -81,7 +107,7 @@ static void *open_lib(knet_handle_t knet_h, const char *libname, int extra_flags
memset(dir, 0, sizeof(dir));
memset(link, 0, sizeof(link));
memset(path, 0, sizeof(path));
if (dlinfo(ret, RTLD_DI_ORIGIN, &dir) < 0) {
if (get_lib_dir(ret, dir) < 0) {
/*
* should we dlclose and return error?
*/
Expand Down

0 comments on commit f907ee4

Please sign in to comment.