1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2016 IBM Corporation. All rights reserved.
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <dirent.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "opal/constants.h"
#include "opal/mca/dl/dl.h"
#include "opal/util/argv.h"
#include "opal/util/printf.h"
#include "dl_dlopen.h"
/*
* Trivial helper function to avoid replicating code
*/
static void do_dlopen(const char *fname, int flags, void **handle, char **err_msg)
{
assert(handle);
*handle = dlopen(fname, flags);
if (NULL != err_msg) {
if (NULL != *handle) {
*err_msg = NULL;
} else {
*err_msg = dlerror();
}
}
}
static int dlopen_open(const char *fname, bool use_ext, bool private_namespace,
opal_dl_handle_t **handle, char **err_msg)
{
assert(handle);
*handle = NULL;
/* Setup the dlopen flags */
int flags = RTLD_LAZY;
if (private_namespace) {
flags |= RTLD_LOCAL;
} else {
flags |= RTLD_GLOBAL;
}
/* If the caller wants to use filename extensions, loop through
them */
void *local_handle = NULL;
if (use_ext && NULL != fname) {
int i;
char *ext;
for (i = 0, ext = mca_dl_dlopen_component.filename_suffixes[i]; NULL != ext;
ext = mca_dl_dlopen_component.filename_suffixes[++i]) {
char *name;
opal_asprintf(&name, "%s%s", fname, ext);
if (NULL == name) {
return OPAL_ERR_IN_ERRNO;
}
/* Does the file exist? */
struct stat buf;
if (stat(name, &buf) < 0) {
free(name);
if (NULL != err_msg) {
*err_msg = "File not found";
}
continue;
}
/* Yes, the file exists -- try to dlopen it. If we can't
dlopen it, bail. */
do_dlopen(name, flags, &local_handle, err_msg);
free(name);
break;
}
}
/* Otherwise, the caller does not want to use filename extensions,
so just use the single filename that the caller provided */
else {
do_dlopen(fname, flags, &local_handle, err_msg);
}
if (NULL != local_handle) {
*handle = calloc(1, sizeof(opal_dl_handle_t));
(*handle)->dlopen_handle = local_handle;
#if OPAL_ENABLE_DEBUG
if (NULL != fname) {
(*handle)->filename = strdup(fname);
} else {
(*handle)->filename = strdup("(null)");
}
#endif
}
return (NULL != local_handle) ? OPAL_SUCCESS : OPAL_ERROR;
}
static int dlopen_lookup(opal_dl_handle_t *handle, const char *symbol, void **ptr, char **err_msg)
{
assert(handle);
assert(handle->dlopen_handle);
assert(symbol);
assert(ptr);
*ptr = dlsym(handle->dlopen_handle, symbol);
if (NULL != *ptr) {
return OPAL_SUCCESS;
}
if (NULL != err_msg) {
*err_msg = dlerror();
}
return OPAL_ERROR;
}
static int dlopen_close(opal_dl_handle_t *handle)
{
assert(handle);
int ret;
ret = dlclose(handle->dlopen_handle);
#if OPAL_ENABLE_DEBUG
free(handle->filename);
#endif
free(handle);
return ret;
}
/*
* Scan all the files in a directory (or path) and invoke a callback
* on each one.
*/
static int dlopen_foreachfile(const char *search_path,
int (*func)(const char *filename, void *data), void *data)
{
int ret;
DIR *dp = NULL;
char **dirs = NULL;
char **good_files = NULL;
dirs = opal_argv_split(search_path, OPAL_ENV_SEP);
for (int i = 0; NULL != dirs && NULL != dirs[i]; ++i) {
dp = opendir(dirs[i]);
if (NULL == dp) {
ret = OPAL_ERR_IN_ERRNO;
goto error;
}
struct dirent *de;
while (NULL != (de = readdir(dp))) {
/* Make the absolute path name */
char *abs_name = NULL;
opal_asprintf(&abs_name, "%s/%s", dirs[i], de->d_name);
if (NULL == abs_name) {
ret = OPAL_ERR_IN_ERRNO;
goto error;
}
/* Stat the file */
struct stat buf;
if (stat(abs_name, &buf) < 0) {
free(abs_name);
ret = OPAL_ERR_IN_ERRNO;
goto error;
}
/* Skip if not a file */
if (!S_ISREG(buf.st_mode)) {
free(abs_name);
continue;
}
/* Find the suffix */
char *ptr = strrchr(abs_name, '.');
if (NULL != ptr) {
/* Skip libtool files */
if (strcmp(ptr, ".la") == 0 || strcmp(ptr, ".lo") == 0) {
free(abs_name);
continue;
}
*ptr = '\0';
}
/* Have we already found this file? Or already found a
file with the same basename (but different suffix)? */
bool found = false;
for (int j = 0; NULL != good_files && NULL != good_files[j]; ++j) {
if (strcmp(good_files[j], abs_name) == 0) {
found = true;
break;
}
}
if (!found) {
opal_argv_append_nosize(&good_files, abs_name);
}
free(abs_name);
}
closedir(dp);
}
dp = NULL;
/* Invoke the callback on all the found files */
if (NULL != good_files) {
for (int i = 0; NULL != good_files[i]; ++i) {
ret = func(good_files[i], data);
if (OPAL_SUCCESS != ret) {
goto error;
}
}
}
ret = OPAL_SUCCESS;
error:
if (NULL != dp) {
closedir(dp);
}
if (NULL != dirs) {
opal_argv_free(dirs);
}
if (NULL != good_files) {
opal_argv_free(good_files);
}
return ret;
}
/*
* Module definition
*/
opal_dl_base_module_t opal_dl_dlopen_module = {.open = dlopen_open,
.lookup = dlopen_lookup,
.close = dlopen_close,
.foreachfile = dlopen_foreachfile};
|