[icinga-checkins] icinga.org: icinga2/master: Use pipe2() instead of pipe() when possible.

git at icinga.org git at icinga.org
Wed Feb 13 07:35:26 CET 2013


Module: icinga2
Branch: master
Commit: a80872b314e8c0b190ac3f4d37ae900ce43336d4
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=a80872b314e8c0b190ac3f4d37ae900ce43336d4

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Wed Feb 13 07:33:14 2013 +0100

Use pipe2() instead of pipe() when possible.

---

 configure.ac         |    2 +-
 lib/base/process.cpp |   43 +++++++++++++++++++++++++++++--------------
 2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/configure.ac b/configure.ac
index 4378286..a27d039 100644
--- a/configure.ac
+++ b/configure.ac
@@ -70,7 +70,7 @@ AC_CHECK_LIB(m, floor)
 AC_CHECK_LIB(socket, getsockname)
 AC_CHECK_LIB(ws2_32, getsockname)
 AC_CHECK_LIB(shlwapi, PathRemoveFileSpecA)
-AC_CHECK_FUNCS([backtrace_symbols execvpe])
+AC_CHECK_FUNCS([backtrace_symbols execvpe pipe2])
 
 AC_MSG_CHECKING(whether to enable debugging)
 AC_ARG_ENABLE(debug, [  --enable-debug=[no/yes]   turn on debugging (default=no)],, enable_debug=no)
diff --git a/lib/base/process.cpp b/lib/base/process.cpp
index 9315c01..3d7d38f 100644
--- a/lib/base/process.cpp
+++ b/lib/base/process.cpp
@@ -69,7 +69,7 @@ Process::Process(const vector<String>& arguments, const Dictionary::Ptr& extraEn
 			if (flags < 0)
 				BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno));
 
-			if (fcntl(childTaskFd, F_SETFL, flags | O_NONBLOCK) < 0)
+			if (fcntl(childTaskFd, F_SETFL, flags | O_NONBLOCK | O_CLOEXEC) < 0)
 				BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno));
 #endif /* _MSC_VER */
 
@@ -309,9 +309,23 @@ void Process::InitTask(void)
 #else /* _MSC_VER */
 	int fds[2];
 
+#ifdef HAVE_PIPE2
+	if (pipe2(fds, O_NONBLOCK | O_CLOEXEC) < 0)
+#else /* HAVE_PIPE2 */
 	if (pipe(fds) < 0)
+#endif /* HAVE_PIPE2 */
 		BOOST_THROW_EXCEPTION(PosixException("pipe() failed.", errno));
 
+#ifndef HAVE_PIPE2
+	int flags;
+	flags = fcntl(childTaskFd, F_GETFL, 0);
+	if (flags < 0)
+		BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno));
+
+	if (fcntl(childTaskFd, F_SETFL, flags | O_NONBLOCK | O_CLOEXEC) < 0)
+		BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno));
+#endif /* HAVE_PIPE2 */
+
 #ifdef HAVE_VFORK
 	m_Pid = vfork();
 #else /* HAVE_VFORK */
@@ -329,6 +343,7 @@ void Process::InitTask(void)
 			_exit(128);
 		}
 
+		(void) close(fds[0]);
 		(void) close(fds[1]);
 
 		if (execvpe(m_Arguments[0], m_Arguments, m_Environment) < 0) {
@@ -337,25 +352,25 @@ void Process::InitTask(void)
 		}
 
 		_exit(128);
-	} else {
-		// parent process
+	}
 
-		// free arguments
-		for (int i = 0; m_Arguments[i] != NULL; i++)
-			free(m_Arguments[i]);
+	// parent process
 
-		delete [] m_Arguments;
+	// free arguments
+	for (int i = 0; m_Arguments[i] != NULL; i++)
+		free(m_Arguments[i]);
 
-		// free environment
-		for (int i = 0; m_Environment[i] != NULL; i++)
-			free(m_Environment[i]);
+	delete [] m_Arguments;
 
-		delete [] m_Environment;
+	// free environment
+	for (int i = 0; m_Environment[i] != NULL; i++)
+		free(m_Environment[i]);
 
-		(void) close(fds[1]);
+	delete [] m_Environment;
 
-		m_FD = fds[0];
-	}
+	(void) close(fds[1]);
+
+	m_FD = fds[0];
 #endif /* _MSC_VER */
 }
 





More information about the icinga-checkins mailing list