From aabb99048290c0662629fe88588a80a0544fd8c5 Mon Sep 17 00:00:00 2001
From: Tino Mettler <tino+debian@tikei.de>
Date: Sun, 26 Oct 2014 12:54:31 +0100
Subject: [PATCH] Fix FTBFS on kfreebsd due to missing SOCK_CLOEXEC

Work around missing SOCK_CLOEXEC on kfreebsd by setting FD_CLOEXEC
afterwards.
---
 src/gdbusxx/gdbus-cxx-bridge.cpp | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/gdbusxx/gdbus-cxx-bridge.cpp b/src/gdbusxx/gdbus-cxx-bridge.cpp
index 238b52f0..4f259576 100644
--- a/src/gdbusxx/gdbus-cxx-bridge.cpp
+++ b/src/gdbusxx/gdbus-cxx-bridge.cpp
@@ -276,6 +276,10 @@ void DBusConnectionPtr::setDisconnect(const Disconnect_t &func)
                              true);
 }
 
+#ifndef SOCK_CLOEXEC
+#define SOCK_CLOEXEC 0
+#endif
+
 boost::shared_ptr<DBusServerCXX> DBusServerCXX::listen(const NewConnection_t &newConnection, DBusErrorCXX *)
 {
     // Create two fds connected via a two-way stream. The parent
@@ -286,6 +290,21 @@ boost::shared_ptr<DBusServerCXX> DBusServerCXX::listen(const NewConnection_t &ne
     if (retval) {
         SE_THROW(StringPrintf("socketpair: %s", strerror(errno)));
     }
+
+    if(SOCK_CLOEXEC == 0) {
+	int flags;
+	int i;
+	for(i = 0; i < 2; i++) {
+	    flags = fcntl(fds[i], F_GETFD);
+	    if (flags == -1){
+		SE_THROW(StringPrintf("fcntl: %s", strerror(errno)));
+	    }
+	    flags |= FD_CLOEXEC;
+	    if (fcntl(fds[i], F_SETFD, flags) == -1){
+		SE_THROW(StringPrintf("fcntl: %s", strerror(errno)));
+	    }
+	}
+    }
     GuardFD parentfd(fds[0]);
     GuardFD childfd(fds[1]);
 
