]> granicus.if.org Git - icinga2/commitdiff
Use pipe2() instead of pipe() when possible.
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 13 Feb 2013 06:33:14 +0000 (07:33 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 13 Feb 2013 06:33:14 +0000 (07:33 +0100)
configure.ac
lib/base/process.cpp

index 43782869795abf10a1267642789a31edf1a71f76..a27d039adddf25a4b32730bcd1f4a1ec4ac4142a 100644 (file)
@@ -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)
index 9315c017ea3a0690b7e8d3d99a7e4818d01a6a5e..3d7d38fdef06308837f723186d68bba872c5aea3 100644 (file)
@@ -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 */
 }