aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ext/dl/handle.c19
-rw-r--r--test/dl/test_handle.rb13
2 files changed, 30 insertions, 2 deletions
diff --git a/ext/dl/handle.c b/ext/dl/handle.c
index a9df5d6119..eee0cb26b0 100644
--- a/ext/dl/handle.c
+++ b/ext/dl/handle.c
@@ -57,8 +57,21 @@ rb_dlhandle_close(VALUE self)
struct dl_handle *dlhandle;
TypedData_Get_Struct(self, struct dl_handle, &dlhandle_data_type, dlhandle);
- dlhandle->open = 0;
- return INT2NUM(dlclose(dlhandle->ptr));
+ if(dlhandle->open) {
+ dlhandle->open = 0;
+ int ret = dlclose(dlhandle->ptr);
+
+ /* Check dlclose for successful return value */
+ if(ret) {
+#if defined(HAVE_DLERROR)
+ rb_raise(rb_eDLError, dlerror());
+#else
+ rb_raise(rb_eDLError, "could not close handle");
+#endif
+ }
+ return INT2NUM(ret);
+ }
+ rb_raise(rb_eDLError, "dlclose() called too many times");
}
VALUE
@@ -301,3 +314,5 @@ Init_dlhandle(void)
rb_define_method(rb_cDLHandle, "disable_close", rb_dlhandle_disable_close, 0);
rb_define_method(rb_cDLHandle, "enable_close", rb_dlhandle_enable_close, 0);
}
+
+/* mode: c; tab-with=8; sw=8; ts=8; noexpandtab: */
diff --git a/test/dl/test_handle.rb b/test/dl/test_handle.rb
index d46e16c084..fed9da4f85 100644
--- a/test/dl/test_handle.rb
+++ b/test/dl/test_handle.rb
@@ -2,6 +2,19 @@ require 'test_base'
module DL
class TestHandle < TestBase
+ def test_handle_close
+ handle = DL::Handle.new(LIBC_SO)
+ assert_equal 0, handle.close
+ end
+
+ def test_handle_close_twice
+ handle = DL::Handle.new(LIBC_SO)
+ handle.close
+ assert_raises(DL::DLError) do
+ handle.close
+ end
+ end
+
def test_dlopen_returns_handle
assert_instance_of DL::Handle, dlopen(LIBC_SO)
end