This is the mail archive of the cygwin-patches@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

dlfcn.cc: clear previous dl errors before new dlopen, dlsym, dlclosecall?


Hi!
Is't needed to clear previous dl errors before new dlopen, dlsym, dlclose call?
See attach test programs.

$ gcc -shared -o demo.dll demo.c
$ gcc -shared -o demo2.dll demo2.c
$ gcc -o test test.c
$ ./test
Output:
handle = f20000
dlsym init_plugin fail
init_plugin = 0
handle2 = f20000
dlsym init_plugin fail
init_plugin = f21050

I think output maybe like these:
handle = f20000
dlsym init_plugin fail
init_plugin = 0
handle2 = f20000
init_plugin = f21050

Is it so?

Thanks.

2003-01-21  David Huang  <davehzhr@hotmail.com>

	* dlfcn.cc: Add clear_dl_error.
	(dlopen): Clear previous dl error.
	(dlsym): Ditto.
	(dlclose): Ditto.




Index: dlfcn.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/dlfcn.cc,v
retrieving revision 1.17
diff -u -p -r1.17 dlfcn.cc
--- dlfcn.cc	31 Oct 2001 00:55:32 -0000	1.17
+++ dlfcn.cc	21 Jan 2003 09:08:39 -0000
@@ -1,6 +1,6 @@
 /* dlfcn.cc
 
-   Copyright 1998, 2000, 2001 Red Hat, Inc.
+   Copyright 1998, 2000, 2001, 2003 Red Hat, Inc.
 
 This file is part of Cygwin.
 
@@ -32,6 +32,12 @@ set_dl_error (const char *str)
   _dl_error = 1;
 }
 
+static void __stdcall
+clear_dl_error ()
+{
+  _dl_error = 0;
+}
+
 /* Look for an executable file given the name and the environment
    variable to use for searching (eg., PATH); returns the full
    pathname (static buffer) if found or NULL if not. */
@@ -89,6 +95,8 @@ dlopen (const char *name, int)
 
   void *ret;
 
+  clear_dl_error();
+
   if (name == NULL)
     ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
   else
@@ -117,6 +125,8 @@ dlopen (const char *name, int)
 void *
 dlsym (void *handle, const char *name)
 {
+  clear_dl_error();
+
   void *ret = (void *) GetProcAddress ((HMODULE) handle, name);
   if (!ret)
     set_dl_error ("dlsym");
@@ -128,6 +138,8 @@ int
 dlclose (void *handle)
 {
   SetResourceLock (LOCK_DLL_LIST, READ_LOCK | WRITE_LOCK, "dlclose");
+
+  clear_dl_error();
 
   int ret = -1;
   void *temphandle = (void *) GetModuleHandle (NULL);

#include <stdio.h>
#include <dlfcn.h>

void main()
{
	char plugin_name[256] = "demo.dll";
	char plugin_name2[256] = "demo2.dll";
	void *handle, *handle2;
	void *(*init_plugin)();
	char *error;

	handle = dlopen(plugin_name, RTLD_NOW);
	if (!handle) {
		printf("dlopen fail\n");
//		return;
	} 
	printf("handle = %x\n", handle);
	init_plugin = dlsym(handle, "init_plugin");
	if ((error = dlerror()) != NULL) {
		printf("dlsym init_plugin fail\n");
//		dlclose(handle);
//		return;
	}
	printf("init_plugin = %x\n", init_plugin);
	dlclose(handle);

	handle2 = dlopen(plugin_name2, RTLD_NOW);
	if (!handle2) {
		printf("dlopen fail\n");
//		return;
	}
	printf("handle2 = %x\n", handle2);
	init_plugin = dlsym(handle2, "init_plugin");
	if ((error = dlerror()) != NULL) {
		printf("dlsym init_plugin fail\n");
//		dlclose(handle);
//		return;
	}
	printf("init_plugin = %x\n", init_plugin);

	dlclose(handle2);
}


void no_init_plugin()
{}

void init_plugin()
{}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]