[Box Backup-commit] COMMIT r2006 - box/trunk/lib/intercept

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sun, 16 Dec 2007 00:44:12 +0000 (UTC)


Author: chris
Date: 2007-12-16 00:44:11 +0000 (Sun, 16 Dec 2007)
New Revision: 2006

Modified:
   box/trunk/lib/intercept/intercept.cpp
Log:
Fix readdir() intercept on FreeBSD by fixing the check for redefined
readdir (from Linux).


Modified: box/trunk/lib/intercept/intercept.cpp
===================================================================
--- box/trunk/lib/intercept/intercept.cpp	2007-12-15 22:20:37 UTC (rev 2005)
+++ box/trunk/lib/intercept/intercept.cpp	2007-12-16 00:44:11 UTC (rev 2006)
@@ -402,12 +402,29 @@
 	lstat_hook = hookfn;
 }
 
+static void * find_function(const char *pName)
+{
+	dlerror();
+
+	void *result = dlsym(RTLD_NEXT, pName);
+	const char *errmsg = (const char *)dlerror();
+
+	if (errmsg != NULL)
+	{
+		BOX_ERROR("Failed to find real " << pName << " function: " <<
+			errmsg);
+		return NULL;
+	}
+
+	return result;
+}
+
 extern "C" 
 DIR *opendir(const char *dirname)
 {
 	if (opendir_real == NULL)
 	{
-		opendir_real = (opendir_t*)(dlsym(RTLD_NEXT, "opendir"));
+		opendir_real = (opendir_t*)find_function("opendir");
 	}
 
 	if (opendir_real == NULL)
@@ -440,10 +457,10 @@
 
 	if (readdir_real == NULL)
 	{
-		#if readdir == readdir64
-		readdir_real = (readdir_t*)(dlsym(RTLD_NEXT, "readdir64"));
+		#if defined readdir && readdir == readdir64
+		readdir_real = (readdir_t*)find_function("readdir64");
 		#else
-		readdir_real = (readdir_t*)(dlsym(RTLD_NEXT, "readdir"));
+		readdir_real = (readdir_t*)find_function("readdir");
 		#endif
 	}
 
@@ -466,7 +483,7 @@
 
 	if (closedir_real == NULL)
 	{
-		closedir_real = (closedir_t*)(dlsym(RTLD_NEXT, "closedir"));
+		closedir_real = (closedir_t*)find_function("closedir");
 	}
 
 	if (closedir_real == NULL)
@@ -489,16 +506,16 @@
 	if (lstat_real == NULL)
 	{
 	#ifdef LINUX_WEIRD_LSTAT
-		#if __lxstat == __lxstat64
-		lstat_real = (lstat_t*)(dlsym(RTLD_NEXT, "__lxstat64"));
+		#if defined __lxstat && __lxstat == __lxstat64
+		lstat_real = (lstat_t*)find_function("__lxstat64");
 		#else
-		lstat_real = (lstat_t*)(dlsym(RTLD_NEXT, "__lxstat"));
+		lstat_real = (lstat_t*)find_function("__lxstat");
 		#endif
 	#else
-		#if lstat == lstat64
-		lstat_real = (lstat_t*)(dlsym(RTLD_NEXT, "lstat64"));
+		#if defined lstat && lstat == lstat64
+		lstat_real = (lstat_t*)find_function("lstat64");
 		#else
-		lstat_real = (lstat_t*)(dlsym(RTLD_NEXT, "lstat"));
+		lstat_real = (lstat_t*)find_function("lstat");
 		#endif
 	#endif
 	}