diff --git a/include/uv-private/uv-unix.h b/include/uv-private/uv-unix.h index ded3fb8106..b07781f406 100644 --- a/include/uv-private/uv-unix.h +++ b/include/uv-private/uv-unix.h @@ -43,6 +43,10 @@ typedef struct { typedef int uv_file; +/* Platform-specific definitions for uv_dlopen support. */ +typedef void* uv_lib_t; +#define UV_DYNAMIC /* empty */ + #define UV_LOOP_PRIVATE_FIELDS \ ares_channel channel; \ /* \ diff --git a/include/uv-private/uv-win.h b/include/uv-private/uv-win.h index ed132d3ad4..9d18fa16be 100644 --- a/include/uv-private/uv-win.h +++ b/include/uv-private/uv-win.h @@ -137,6 +137,10 @@ typedef struct uv_buf_t { typedef int uv_file; +/* Platform-specific definitions for uv_dlopen support. */ +typedef HMODULE uv_lib_t; +#define UV_DYNAMIC FAR WINAPI + RB_HEAD(uv_timer_tree_s, uv_timer_s); #define UV_LOOP_PRIVATE_FIELDS \ diff --git a/include/uv.h b/include/uv.h index b438b70d00..8b3a2cde28 100644 --- a/include/uv.h +++ b/include/uv.h @@ -1188,6 +1188,19 @@ UV_EXTERN uint64_t uv_get_total_memory(void); UV_EXTERN extern uint64_t uv_hrtime(void); +/* + * Opens a shared library. The filename is in utf-8. On success, -1 is + * and the variable pointed by library receives a handle to the library. + */ +UV_EXTERN uv_err_t uv_dlopen(const char* filename, uv_lib_t* library); +UV_EXTERN uv_err_t uv_dlclose(uv_lib_t library); + +/* + * Retrieves a data pointer from a dynamic library. + */ +UV_EXTERN uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr); + + /* the presence of these unions force similar struct layout */ union uv_any_handle { uv_tcp_t tcp; diff --git a/src/unix/dl.c b/src/unix/dl.c new file mode 100644 index 0000000000..5c96913a03 --- /dev/null +++ b/src/unix/dl.c @@ -0,0 +1,59 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "internal.h" + +#include +#include + + +const uv_err_t uv_ok_ = { UV_OK, 0 }; + +uv_err_t uv_dlopen(const char* filename, uv_lib_t* library) { + void* handle = dlopen(filename, RTLD_LAZY); + if (handle == NULL) { + return uv__new_sys_error(errno); + } + + *library = handle; + return uv_ok_; +} + + +uv_err_t uv_dlclose(uv_lib_t library) { + if (dlclose(library) != 0) { + return uv__new_sys_error(errno); + } + + return uv_ok_; +} + + +uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr) { + void* address = dlsym(library, name); + if (address == NULL) { + return uv__new_sys_error(errno); + } + + *ptr = (void*) address; + return uv_ok_; +} diff --git a/src/win/dl.c b/src/win/dl.c new file mode 100644 index 0000000000..37cdc131a1 --- /dev/null +++ b/src/win/dl.c @@ -0,0 +1,63 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "internal.h" + + +uv_err_t uv_dlopen(const char* filename, uv_lib_t* library) { + wchar_t filename_w[32768]; + HMODULE handle; + + if (!uv_utf8_to_utf16(filename, + filename_w, + sizeof(filename_w) / sizeof(wchar_t))) { + return uv__new_sys_error(GetLastError()); + } + + handle = LoadLibraryW(filename_w); + if (handle == NULL) { + return uv__new_sys_error(GetLastError()); + } + + *library = handle; + return uv_ok_; +} + + +uv_err_t uv_dlclose(uv_lib_t library) { + if (!FreeLibrary(library)) { + return uv__new_sys_error(GetLastError()); + } + + return uv_ok_; +} + + +uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr) { + FARPROC proc = GetProcAddress(library, name); + if (proc == NULL) { + return uv__new_sys_error(GetLastError()); + } + + *ptr = (void*) proc; + return uv_ok_; +} diff --git a/uv.gyp b/uv.gyp index b21d56ec82..b92760a31b 100644 --- a/uv.gyp +++ b/uv.gyp @@ -130,6 +130,7 @@ 'src/win/async.c', 'src/win/cares.c', 'src/win/core.c', + 'src/win/dl.c', 'src/win/error.c', 'src/win/fs.c', 'src/win/fs-event.c', @@ -182,6 +183,7 @@ 'src/unix/tty.c', 'src/unix/stream.c', 'src/unix/cares.c', + 'src/unix/dl.c', 'src/unix/error.c', 'src/unix/process.c', 'src/unix/internal.h',