[PATCH] bug in libreplace testcase for socketpair

Green, Paul Paul.Green at stratus.com
Tue Nov 28 02:47:19 GMT 2006

The code that tests socketpair() in libreplace doesn't work when it
references the replacement version of socketpair(). I guess I must have
the only system that actually needs this code.  I looked at ldb, samba4,
talloc, and tdb, all of which reference libreplace, and I can't find any
calls to socketpair() in any of their C code.  So it appears that this
is more of a potential problem than an actual problem.

The issue is that the file descriptors returned by pipe() are restricted
as to which one is the read side and which one is the write side.  This
doesn't seem to be the case for socketpair().  The test case tries to
write to fd[0] but that's the read descriptor.  

When I changed the test to write to fd[1] and read from fd[0], it

My reading of the POSIX standard for both pipe() suggests to me that
pipe is required to make fd[0] be the read descriptor and fd[1] be the
write descriptor, but that socketpair() does not have this restriction.

The simple fix would be to patch the test as shown in the attached file.

A more complex fix would be to use real TCP sockets connected via the
loopback port.

We will shortly be implementing Unix domain sockets for VOS, so this
problem will eventually disappear for me.

Unless someone objects, or sees a flaw in my logic (entirely possible),
I propose to patch the socketpair test case to flip the meaning of the
file descriptors.

Paul Green, Senior Technical Consultant,
Stratus Technologies, Maynard, MA USA
Voice: +1 978-461-7557; FAX: +1 978-461-3610; Mobile +1 (978) 235-2451
-------------- next part --------------
diff -urpN old/libreplace/test/testsuite.c new/libreplace/test/testsuite.c
--- old/libreplace/test/testsuite.c	Sat Nov 25 15:33:28 2006
+++ new/libreplace/test/testsuite.c	Sat Nov 25 15:34:06 2006
@@ -390,14 +390,14 @@ static int test_socketpair(void)
 		return false;
-	if (write(sock[0], "automatisch", 12) == -1) {
+	if (write(sock[1], "automatisch", 12) == -1) {
 		printf("failure: socketpair [\n"
 			   "write() failed: %s\n"
 			   "]\n", strerror(errno));
 		return false;
-	if (read(sock[1], buf, 12) == -1) {
+	if (read(sock[0], buf, 12) == -1) {
 		printf("failure: socketpair [\n"
 			   "read() failed: %s\n"
 			   "]\n", strerror(errno));

More information about the samba-technical mailing list