/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.1*2* Permission is hereby granted, free of charge, to any person obtaining a copy3* of this software and associated documentation files (the "Software"), to4* deal in the Software without restriction, including without limitation the5* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or6* sell copies of the Software, and to permit persons to whom the Software is7* furnished to do so, subject to the following conditions:8*9* The above copyright notice and this permission notice shall be included in10* all copies or substantial portions of the Software.11*12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING17* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS18* IN THE SOFTWARE.19*/2021#include "uv.h"22#include "internal.h"2324#include <dlfcn.h>25#include <errno.h>26#include <string.h>27#include <locale.h>2829static int uv__dlerror(uv_lib_t* lib);303132int uv_dlopen(const char* filename, uv_lib_t* lib) {33dlerror(); /* Reset error status. */34lib->errmsg = NULL;35lib->handle = dlopen(filename, RTLD_LAZY);36return lib->handle ? 0 : uv__dlerror(lib);37}383940void uv_dlclose(uv_lib_t* lib) {41uv__free(lib->errmsg);42lib->errmsg = NULL;4344if (lib->handle) {45/* Ignore errors. No good way to signal them without leaking memory. */46dlclose(lib->handle);47lib->handle = NULL;48}49}505152int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) {53dlerror(); /* Reset error status. */54*ptr = dlsym(lib->handle, name);55return *ptr ? 0 : uv__dlerror(lib);56}575859const char* uv_dlerror(const uv_lib_t* lib) {60return lib->errmsg ? lib->errmsg : "no error";61}626364static int uv__dlerror(uv_lib_t* lib) {65const char* errmsg;6667uv__free(lib->errmsg);6869errmsg = dlerror();7071if (errmsg) {72lib->errmsg = uv__strdup(errmsg);73return -1;74}75else {76lib->errmsg = NULL;77return 0;78}79}808182