Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Implement uv_dlopen and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Oct 28, 2011
1 parent c985ea4 commit 90e15f1
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/uv-private/uv-unix.h
Expand Up @@ -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; \
/* \
Expand Down
4 changes: 4 additions & 0 deletions include/uv-private/uv-win.h
Expand Up @@ -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 \
Expand Down
13 changes: 13 additions & 0 deletions include/uv.h
Expand Up @@ -1189,6 +1189,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;
Expand Down
59 changes: 59 additions & 0 deletions 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 <dlfcn.h>
#include <errno.h>


static 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_;
}
63 changes: 63 additions & 0 deletions 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_;
}
2 changes: 2 additions & 0 deletions uv.gyp
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 90e15f1

Please sign in to comment.