From fac2304ae555fa6c880da35effdda39b2c16a611 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 13 Feb 2013 12:47:51 +0100 Subject: [PATCH] Fixed incorrect use of F_SETFL. --- lib/base/application.cpp | 18 ++++++++---------- lib/base/process-unix.cpp | 16 ++++++++-------- lib/base/socket.cpp | 12 ++++++------ 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 7fdcfa2af..9632c2a77 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -455,17 +455,15 @@ void Application::UpdatePidFile(const String& filename) if (m_PidFile == NULL) BOOST_THROW_EXCEPTION(runtime_error("Could not open PID file '" + filename + "'")); -#ifdef F_GETFL - int flags; - flags = fcntl(fileno(m_PidFile), F_GETFL, 0); - if (flags < 0) - BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); - - if (fcntl(fileno(m_PidFile), F_SETFL, flags | FD_CLOEXEC) < 0) - BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); -#endif /* FD_CLOEXEC */ - #ifndef _WIN32 + int flags; + flags = fcntl(fileno(m_PidFile), F_GETFD, 0); + if (flags < 0) + BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); + + if (fcntl(fileno(m_PidFile), F_SETFD, flags | FD_CLOEXEC) < 0) + BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); + if (flock(fileno(m_PidFile), LOCK_EX | LOCK_NB) < 0) { ClosePidFile(); diff --git a/lib/base/process-unix.cpp b/lib/base/process-unix.cpp index 75187fde5..2c89c4c43 100644 --- a/lib/base/process-unix.cpp +++ b/lib/base/process-unix.cpp @@ -36,11 +36,11 @@ void Process::CreateWorkers(void) m_TaskFd = fds[1]; int flags; - flags = fcntl(fds[1], F_GETFL, 0); + flags = fcntl(fds[1], F_GETFD, 0); if (flags < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); - if (fcntl(fds[1], F_SETFL, flags | O_NONBLOCK | FD_CLOEXEC) < 0) + if (fcntl(fds[1], F_SETFD, flags | O_NONBLOCK | FD_CLOEXEC) < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); for (int i = 0; i < thread::hardware_concurrency(); i++) { @@ -52,11 +52,11 @@ void Process::CreateWorkers(void) BOOST_THROW_EXCEPTION(PosixException("dup() failed.", errno)); int flags; - flags = fcntl(childTaskFd, F_GETFL, 0); + flags = fcntl(childTaskFd, F_GETFD, 0); if (flags < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); - if (fcntl(childTaskFd, F_SETFL, flags | O_NONBLOCK | FD_CLOEXEC) < 0) + if (fcntl(childTaskFd, F_SETFD, flags | O_NONBLOCK | FD_CLOEXEC) < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); thread t(&Process::WorkerThreadProc, childTaskFd); @@ -186,18 +186,18 @@ void Process::InitTask(void) #ifndef HAVE_PIPE2 int flags; - flags = fcntl(fds[0], F_GETFL, 0); + flags = fcntl(fds[0], F_GETFD, 0); if (flags < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); - if (fcntl(fds[0], F_SETFL, flags | O_NONBLOCK | FD_CLOEXEC) < 0) + if (fcntl(fds[0], F_SETFD, flags | O_NONBLOCK | FD_CLOEXEC) < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); - flags = fcntl(fds[1], F_GETFL, 0); + flags = fcntl(fds[1], F_GETFD, 0); if (flags < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); - if (fcntl(fds[1], F_SETFL, flags | O_NONBLOCK | FD_CLOEXEC) < 0) + if (fcntl(fds[1], F_SETFD, flags | O_NONBLOCK | FD_CLOEXEC) < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); #endif /* HAVE_PIPE2 */ diff --git a/lib/base/socket.cpp b/lib/base/socket.cpp index 8e59b3b80..286548f22 100644 --- a/lib/base/socket.cpp +++ b/lib/base/socket.cpp @@ -68,20 +68,20 @@ void Socket::Start(void) */ void Socket::SetFD(SOCKET fd) { - /* mark the socket as non-blocking */ + /* mark the socket as non-blocking and close-on-exec */ if (fd != INVALID_SOCKET) { -#ifdef F_GETFL +#ifndef _WIN32 int flags; - flags = fcntl(fd, F_GETFL, 0); + flags = fcntl(fd, F_GETFD, 0); if (flags < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); - if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) + if (fcntl(fd, F_SETFD, flags | O_NONBLOCK | FD_CLOEXEC) < 0) BOOST_THROW_EXCEPTION(PosixException("fcntl failed", errno)); -#else /* F_GETFL */ +#else /* _WIN32 */ unsigned long lTrue = 1; ioctlsocket(fd, FIONBIO, &lTrue); -#endif /* F_GETFL */ +#endif /* _WIN32 */ } m_FD = fd;