Skip to content

Commit 5384920

Browse files
committed
Better prefix extraction
For the libdir it now matched the libdir prefix from the installdirs.h in order to find the real prefix. It does the same for executable allowing us to execute our compiler wrappers and other tools from a relocated directory. The problem here is that all our internal executbales are statically linked against libopal_core, we we will find the basic symbols not in a shared library but directly in the executable. This works fine, but we need a way to prevent this mechanism from triggering on a statically build user application. Signed-off-by: George Bosilca <[email protected]>
1 parent 76f3d0e commit 5384920

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

opal/mca/installdirs/runtime/opal_installdirs_runtime.c

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ opal_installdirs_base_component_t mca_installdirs_runtime_component = {
4141

4242
#include <dlfcn.h>
4343
#include "opal/util/basename.h"
44+
#include "opal/mca/installdirs/install_dirs.h"
4445

4546
/**
4647
* We are trying to solve a particular use case here, when the entire install tree
@@ -51,42 +52,60 @@ static int installdirs_runtime_open(void)
5152
{
5253
Dl_info info;
5354
void* opal_fct;
55+
char* libname = NULL;
56+
const char* base_prefix_path = OPAL_LIBDIR;
5457

5558
/* Casting from void* to fct pointer according to POSIX.1-2001 and POSIX.1-2008 */
5659
*(void **)&opal_fct = dlsym(RTLD_DEFAULT, "opal_init_util");
5760

5861
if( 0 == dladdr(opal_fct, &info) ) {
59-
/* Can't find the symbol */
60-
return OPAL_ERROR;
62+
goto bail_out_with_no_data;
6163
}
6264

6365
/* If this build was both static and shared then this compoenent will be build and will exists
6466
* even in the static library. We need to prevent setting a prefix for the OMPI library that
6567
* is actually the application path. Check, if the name points to a library.
6668
*/
67-
char* libname = opal_basename(info.dli_fname);
69+
libname = opal_basename(info.dli_fname);
6870
if( strncmp(libname, "lib", 3)) { /* not a shared library */
69-
free(libname);
70-
return OPAL_ERROR;
71-
}
71+
base_prefix_path = OPAL_BINDIR;
72+
} else {
7273
#if defined(OPAL_LIB_NAME)
73-
/* Extra check using the installed name of the OPAL library */
74-
if( strncmp(libname+3, OPAL_LIB_NAME, strlen(OPAL_LIB_NAME)) ) { /* not a shared library */
75-
free(libname);
76-
return OPAL_ERROR;
77-
}
74+
/* Extra check using the installed name of the OPAL library */
75+
if( strncmp(libname+3, OPAL_LIB_NAME, strlen(OPAL_LIB_NAME)) ) { /* not a shared library */
76+
goto bail_out_with_no_data;
77+
}
7878
#endif /* defined(OPAL_LIB_NAME) */
79+
}
7980
/* Remove the shared library name and it's first dirname to obtain a prefix. This
8081
* is true in most cases, especially when the install directory was just moved
8182
* moved around, but it is not necessarily always true.
8283
*/
83-
char* dname = opal_dirname(info.dli_fname);
84-
char* prefix = opal_dirname(dname);
84+
char *prefix = NULL, *dname = opal_dirname(info.dli_fname);
85+
int dname_idx = strlen(dname), dname_token = dname_idx;
86+
for( int i = strlen(base_prefix_path); (i > 0) && (dname_idx > 0); i-- ) {
87+
if( dname[dname_idx] != base_prefix_path[i] ) {
88+
dname[dname_token] = '\0';
89+
prefix = dname;
90+
dname = NULL; /* the string is now attached to the component, prevent it from being freed */
91+
break;
92+
}
93+
if( dname[dname_idx] == OPAL_PATH_SEP[0] )
94+
dname_token = dname_idx;
95+
dname_idx--;
96+
}
97+
98+
mca_installdirs_runtime_component.install_dirs_data.prefix = prefix;
99+
100+
/* If we goto here, there is some error. Unfortunately, we can't return an error from
101+
* this function, the MCA infrastructure is not yet completely setup, and a call to
102+
* mca_base_component_close will break.
103+
* So, return success but provide no meaningfull data in the component.
104+
*/
105+
bail_out_with_no_data:
85106

86107
free(libname);
87108
free(dname);
88109

89-
mca_installdirs_runtime_component.install_dirs_data.prefix = prefix;
90-
91110
return OPAL_SUCCESS;
92111
}

0 commit comments

Comments
 (0)