From a86de775b9c298dfafb0c2879e089ca30d8fd2dc Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Thu, 29 May 2008 01:23:14 +0000
Subject: [PATCH] put bandaid on xmlrpc-c to not destroy the box for no reason

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8715 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 libs/xmlrpc-c/lib/abyss/src/socket_unix.c | 37 ++++++++++++-----------
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/libs/xmlrpc-c/lib/abyss/src/socket_unix.c b/libs/xmlrpc-c/lib/abyss/src/socket_unix.c
index 26fecf7f89..c3d2c81c63 100644
--- a/libs/xmlrpc-c/lib/abyss/src/socket_unix.c
+++ b/libs/xmlrpc-c/lib/abyss/src/socket_unix.c
@@ -40,7 +40,7 @@
 
 #include "socket_unix.h"
 
-
+#define sane_close(_it) if (_it > 0) {close(_it) ; _it = -1; }
 
 typedef struct {
     int interruptorFd;
@@ -50,19 +50,19 @@ typedef struct {
 
 
 static void
-initInterruptPipe(interruptPipe * const pipeP,
+initInterruptPipe(interruptPipe * pipeP,
                   const char **   const errorP) {
 
-    int pipeFd[2];
+    int pipeFd[2] = {-1, -1};
     int rc;
 
     rc = pipe(pipeFd);
 
-    if (rc != 0)
+    if (rc != 0) {
         xmlrpc_asprintf(errorP, "Unable to create a pipe to use to interrupt "
                         "waits.  pipe() failed with errno %d (%s)",
                         errno, strerror(errno));
-    else {
+	} else {
         *errorP = NULL;
         pipeP->interruptorFd = pipeFd[1];
         pipeP->interrupteeFd = pipeFd[0];
@@ -72,10 +72,14 @@ initInterruptPipe(interruptPipe * const pipeP,
 
 
 static void
-termInterruptPipe(interruptPipe const pipe) {
+termInterruptPipe(interruptPipe pipe) {
 
-    close(pipe.interruptorFd);
-    close(pipe.interrupteeFd);
+	if (pipe.interruptorFd) {
+		sane_close(pipe.interruptorFd);
+	}
+	if (pipe.interrupteeFd) {
+		sane_close(pipe.interrupteeFd);
+	}
 }
 
 
@@ -153,7 +157,7 @@ channelDestroy(TChannel * const channelP) {
     termInterruptPipe(socketUnixP->interruptPipe);
 
     if (!socketUnixP->userSuppliedFd)
-        close(socketUnixP->fd);
+        sane_close(socketUnixP->fd);
 
     free(socketUnixP);
 }
@@ -484,13 +488,13 @@ makeChannelFromFd(int           const fd,
     struct socketUnix * socketUnixP;
 
     MALLOCVAR(socketUnixP);
-    
+
     if (socketUnixP == NULL)
         xmlrpc_asprintf(errorP, "Unable to allocate memory for Unix "
                         "channel descriptor");
     else {
         TChannel * channelP;
-        
+
         socketUnixP->fd = fd;
         socketUnixP->userSuppliedFd = TRUE;
 
@@ -498,7 +502,6 @@ makeChannelFromFd(int           const fd,
 
         if (!*errorP) {
             ChannelCreate(&channelVtbl, socketUnixP, &channelP);
-        
             if (channelP == NULL)
                 xmlrpc_asprintf(errorP, "Unable to allocate memory for "
                                 "channel descriptor.");
@@ -564,7 +567,7 @@ chanSwitchDestroy(TChanSwitch * const chanSwitchP) {
     termInterruptPipe(socketUnixP->interruptPipe);
 
     if (!socketUnixP->userSuppliedFd)
-        close(socketUnixP->fd);
+        sane_close(socketUnixP->fd);
 
     free(socketUnixP);
 }
@@ -728,13 +731,13 @@ chanSwitchAccept(TChanSwitch * const chanSwitchP,
             rc = accept(listenSocketP->fd, &peerAddr, &size);
 
             if (rc >= 0) {
-                int const acceptedFd = rc;
+                int acceptedFd = rc;
 
                 createChannelForAccept(acceptedFd, peerAddr,
                                        &channelP, channelInfoPP, errorP);
 
                 if (*errorP)
-                    close(acceptedFd);
+                    sane_close(acceptedFd);
             } else if (errno == EINTR)
                 interrupted = TRUE;
             else
@@ -884,7 +887,7 @@ ChanSwitchUnixCreate(unsigned short const portNumber,
         xmlrpc_asprintf(errorP, "socket() failed with errno %d (%s)",
                         errno, strerror(errno));
     else {
-        int const socketFd = rc;
+        int socketFd = rc;
 
         setSocketOptions(socketFd, errorP);
         if (!*errorP) {
@@ -896,7 +899,7 @@ ChanSwitchUnixCreate(unsigned short const portNumber,
             }
         }
         if (*errorP)
-            close(socketFd);
+            sane_close(socketFd);
     }
 }