Permalink
Browse files

Implement uv_dlopen and friends

  • Loading branch information...
1 parent 721bc72 commit 16e0314c01d7d75396645bd8a0ab751e218da7a8 @piscisaureus piscisaureus committed Oct 28, 2011
Showing with 145 additions and 0 deletions.
  1. +4 −0 include/uv-private/uv-unix.h
  2. +4 −0 include/uv-private/uv-win.h
  3. +13 −0 include/uv.h
  4. +59 −0 src/unix/dl.c
  5. +63 −0 src/win/dl.c
  6. +2 −0 uv.gyp
@@ -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; \
/* \
@@ -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 \
View
@@ -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;
View
@@ -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 <dlfcn.h>
+#include <errno.h>
+
+
+const uv_err_t uv_ok_ = { UV_OK, 0 };
@bnoordhuis
bnoordhuis Oct 28, 2011

Make this static.

+
+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_;
+}
View
@@ -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_;
@bnoordhuis
bnoordhuis Oct 28, 2011

Where does uv_ok_ live? Not in src/unix/dl.c right?

@bnoordhuis
bnoordhuis Oct 28, 2011

How does that even work? src/unix/dl.c is not compiled in on Windows, right?

@piscisaureus
piscisaureus Oct 28, 2011 Joyent member

Somewhere in error.c I think. It's defined anyway - uv-win uses it a lot.
Also it's just a shortcut for returning an uv_err_t with code == UV_OK.

@bnoordhuis
bnoordhuis Oct 28, 2011

Yeah, it's in src/win/error.c. Somehow my grep fu missed that one.

+}
+
+
+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_;
+}
View
@@ -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',

1 comment on commit 16e0314

@bnoordhuis
diff --git a/config-unix.mk b/config-unix.mk
index 53bcbd4..9524061 100644
--- a/config-unix.mk
+++ b/config-unix.mk
@@ -28,6 +28,7 @@ CPPFLAGS += -D_LARGEFILE_SOURCE
 CPPFLAGS += -D_FILE_OFFSET_BITS=64

 OBJS += src/unix/core.o
+OBJS += src/unix/dl.o
 OBJS += src/unix/fs.o
 OBJS += src/unix/cares.o
 OBJS += src/unix/udp.o
Please sign in to comment.