1 //! Dynamic library facilities.
3 //! A simple wrapper over the platform's dynamic library facilities
8 pub struct DynamicLibrary {
12 impl Drop for DynamicLibrary {
14 unsafe { dl::close(self.handle) }
19 /// Lazily open a dynamic library.
20 pub fn open(filename: &Path) -> Result<DynamicLibrary, String> {
21 let maybe_library = dl::open(filename.as_os_str());
23 // The dynamic library must not be constructed if there is
24 // an error opening the library so the destructor does not
28 Ok(handle) => Ok(DynamicLibrary { handle }),
32 /// Accesses the value at the symbol of the dynamic library.
33 pub unsafe fn symbol<T>(&self, symbol: &str) -> Result<*mut T, String> {
34 // This function should have a lifetime constraint of 'a on
35 // T but that feature is still unimplemented
37 let raw_string = CString::new(symbol).unwrap();
38 let maybe_symbol_value = dl::symbol(self.handle, raw_string.as_ptr());
40 // The value must not be constructed if there is an error so
41 // the destructor does not run.
42 match maybe_symbol_value {
44 Ok(symbol_value) => Ok(symbol_value as *mut T),
54 use std::ffi::{CStr, CString, OsStr};
55 use std::os::unix::prelude::*;
59 pub(super) fn open(filename: &OsStr) -> Result<*mut u8, String> {
60 check_for_errors_in(|| unsafe {
61 let s = CString::new(filename.as_bytes()).unwrap();
62 libc::dlopen(s.as_ptr(), libc::RTLD_LAZY) as *mut u8
66 fn check_for_errors_in<T, F>(f: F) -> Result<T, String>
70 use std::sync::{Mutex, Once};
71 static INIT: Once = Once::new();
72 static mut LOCK: *mut Mutex<()> = ptr::null_mut();
75 LOCK = Box::into_raw(Box::new(Mutex::new(())));
77 // dlerror isn't thread safe, so we need to lock around this entire
79 let _guard = (*LOCK).lock();
80 let _old_error = libc::dlerror();
84 let last_error = libc::dlerror() as *const _;
85 if ptr::null() == last_error {
88 let s = CStr::from_ptr(last_error).to_bytes();
89 Err(str::from_utf8(s).unwrap().to_owned())
94 pub(super) unsafe fn symbol(
96 symbol: *const libc::c_char,
97 ) -> Result<*mut u8, String> {
98 check_for_errors_in(|| libc::dlsym(handle as *mut libc::c_void, symbol) as *mut u8)
101 pub(super) unsafe fn close(handle: *mut u8) {
102 libc::dlclose(handle as *mut libc::c_void);
110 use std::os::windows::prelude::*;
113 use winapi::shared::minwindef::HMODULE;
114 use winapi::um::errhandlingapi::SetThreadErrorMode;
115 use winapi::um::libloaderapi::{FreeLibrary, GetProcAddress, LoadLibraryW};
116 use winapi::um::winbase::SEM_FAILCRITICALERRORS;
118 pub(super) fn open(filename: &OsStr) -> Result<*mut u8, String> {
119 // disable "dll load failed" error dialog.
120 let prev_error_mode = unsafe {
121 let new_error_mode = SEM_FAILCRITICALERRORS;
122 let mut prev_error_mode = 0;
123 let result = SetThreadErrorMode(new_error_mode, &mut prev_error_mode);
125 return Err(io::Error::last_os_error().to_string());
130 let filename_str: Vec<_> = filename.encode_wide().chain(Some(0)).collect();
131 let result = unsafe { LoadLibraryW(filename_str.as_ptr()) } as *mut u8;
132 let result = ptr_result(result);
135 SetThreadErrorMode(prev_error_mode, ptr::null_mut());
141 pub(super) unsafe fn symbol(
143 symbol: *const libc::c_char,
144 ) -> Result<*mut u8, String> {
145 let ptr = GetProcAddress(handle as HMODULE, symbol) as *mut u8;
149 pub(super) unsafe fn close(handle: *mut u8) {
150 FreeLibrary(handle as HMODULE);
153 fn ptr_result<T>(ptr: *mut T) -> Result<*mut T, String> {
154 if ptr.is_null() { Err(io::Error::last_os_error().to_string()) } else { Ok(ptr) }