|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2007 Los Alamos National Security, LLC. All rights |
| 3 | + * reserved. |
| 4 | + * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. |
| 5 | + * $COPYRIGHT$ |
| 6 | + * |
| 7 | + * Additional copyrights may follow |
| 8 | + * |
| 9 | + * $HEADER$ |
| 10 | + */ |
| 11 | + |
| 12 | +#include "opal_config.h" |
| 13 | + |
| 14 | +#include <stdlib.h> |
| 15 | +#include <string.h> |
| 16 | + |
| 17 | +#include "opal/constants.h" |
| 18 | +#include "opal/mca/installdirs/installdirs.h" |
| 19 | +#include "opal/runtime/opal.h" |
| 20 | + |
| 21 | +static int installdirs_runtime_open(void); |
| 22 | + |
| 23 | +opal_installdirs_base_component_t mca_installdirs_runtime_component = { |
| 24 | + /* First, the mca_component_t struct containing meta information |
| 25 | + about the component itself */ |
| 26 | + {OPAL_INSTALLDIRS_BASE_VERSION_2_0_0, |
| 27 | + |
| 28 | + /* Component name and version */ |
| 29 | + "runtime", OPAL_MAJOR_VERSION, OPAL_MINOR_VERSION, OPAL_RELEASE_VERSION, |
| 30 | + |
| 31 | + /* Component open and close functions */ |
| 32 | + installdirs_runtime_open, NULL}, |
| 33 | + {/* This component is checkpointable */ |
| 34 | + MCA_BASE_METADATA_PARAM_CHECKPOINT}, |
| 35 | + |
| 36 | + /* Next the opal_install_dirs_t install_dirs_data information */ |
| 37 | + { |
| 38 | + NULL, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +#include <dlfcn.h> |
| 43 | +#include "opal/util/basename.h" |
| 44 | + |
| 45 | +/** |
| 46 | + * We are trying to solve a particular use case here, when the entire install tree |
| 47 | + * of Open MPI (and its dependencies) has been moved into another location. Nothing |
| 48 | + * fancy, the entire install tree has maintained his shape but changed the prefix. |
| 49 | + */ |
| 50 | +static int installdirs_runtime_open(void) |
| 51 | +{ |
| 52 | + Dl_info info; |
| 53 | + void* opal_fct; |
| 54 | + |
| 55 | + /* Casting from void* to fct pointer according to POSIX.1-2001 and POSIX.1-2008 */ |
| 56 | + *(void **)&opal_fct = dlsym(RTLD_DEFAULT, "opal_init_util"); |
| 57 | + |
| 58 | + if( 0 == dladdr(opal_fct, &info) ) { |
| 59 | + /* Can't find the symbol */ |
| 60 | + return OPAL_ERROR; |
| 61 | + } |
| 62 | + |
| 63 | + /* If this build was both static and shared then this compoenent will be build and will exists |
| 64 | + * even in the static library. We need to prevent setting a prefix for the OMPI library that |
| 65 | + * is actually the application path. Check, if the name points to a library. |
| 66 | + */ |
| 67 | + char* libname = opal_basename(info.dli_fname); |
| 68 | + if( strncmp(libname, "lib", 3)) { /* not a shared library */ |
| 69 | + free(libname); |
| 70 | + return OPAL_ERROR; |
| 71 | + } |
| 72 | +#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 | + } |
| 78 | +#endif /* defined(OPAL_LIB_NAME) */ |
| 79 | + /* Remove the shared library name and it's first dirname to obtain a prefix. This |
| 80 | + * is true in most cases, especially when the install directory was just moved |
| 81 | + * moved around, but it is not necessarily always true. |
| 82 | + */ |
| 83 | + char* dname = opal_dirname(info.dli_fname); |
| 84 | + char* prefix = opal_dirname(dname); |
| 85 | + |
| 86 | + free(libname); |
| 87 | + free(dname); |
| 88 | + |
| 89 | + mca_installdirs_runtime_component.install_dirs_data.prefix = prefix; |
| 90 | + |
| 91 | + return OPAL_SUCCESS; |
| 92 | +} |
0 commit comments