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

This commit is contained in:
Gunnar Beutner 2013-02-13 07:33:14 +01:00
parent 2893134940
commit a80872b314
2 changed files with 35 additions and 20 deletions

View 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)

View 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]);
delete [] m_Arguments;
// free environment
for (int i = 0; m_Environment[i] != NULL; i++)
free(m_Environment[i]);
delete [] m_Environment;
(void) close(fds[1]);
m_FD = fds[0];
}
// parent process
// free arguments
for (int i = 0; m_Arguments[i] != NULL; i++)
free(m_Arguments[i]);
delete [] m_Arguments;
// free environment
for (int i = 0; m_Environment[i] != NULL; i++)
free(m_Environment[i]);
delete [] m_Environment;
(void) close(fds[1]);
m_FD = fds[0];
#endif /* _MSC_VER */
}