Blame src/unix/dl.c

Packit b5b901
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "internal.h"
Packit b5b901
Packit b5b901
#include <dlfcn.h>
Packit b5b901
#include <errno.h>
Packit b5b901
#include <string.h>
Packit b5b901
#include <locale.h>
Packit b5b901
Packit b5b901
static int uv__dlerror(uv_lib_t* lib);
Packit b5b901
Packit b5b901
Packit b5b901
int uv_dlopen(const char* filename, uv_lib_t* lib) {
Packit b5b901
  dlerror(); /* Reset error status. */
Packit b5b901
  lib->errmsg = NULL;
Packit b5b901
  lib->handle = dlopen(filename, RTLD_LAZY);
Packit b5b901
  return lib->handle ? 0 : uv__dlerror(lib);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
void uv_dlclose(uv_lib_t* lib) {
Packit b5b901
  uv__free(lib->errmsg);
Packit b5b901
  lib->errmsg = NULL;
Packit b5b901
Packit b5b901
  if (lib->handle) {
Packit b5b901
    /* Ignore errors. No good way to signal them without leaking memory. */
Packit b5b901
    dlclose(lib->handle);
Packit b5b901
    lib->handle = NULL;
Packit b5b901
  }
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) {
Packit b5b901
  dlerror(); /* Reset error status. */
Packit b5b901
  *ptr = dlsym(lib->handle, name);
Packit b5b901
  return uv__dlerror(lib);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
const char* uv_dlerror(const uv_lib_t* lib) {
Packit b5b901
  return lib->errmsg ? lib->errmsg : "no error";
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static int uv__dlerror(uv_lib_t* lib) {
Packit b5b901
  const char* errmsg;
Packit b5b901
Packit b5b901
  uv__free(lib->errmsg);
Packit b5b901
Packit b5b901
  errmsg = dlerror();
Packit b5b901
Packit b5b901
  if (errmsg) {
Packit b5b901
    lib->errmsg = uv__strdup(errmsg);
Packit b5b901
    return -1;
Packit b5b901
  }
Packit b5b901
  else {
Packit b5b901
    lib->errmsg = NULL;
Packit b5b901
    return 0;
Packit b5b901
  }
Packit b5b901
}