From f7d78ff578a2ba3e56216a5182ff121cbae93606 Mon Sep 17 00:00:00 2001 From: Alexander Chemeris Date: Tue, 23 Nov 2010 10:48:38 +0300 Subject: [PATCH] Set FD_CLOEXEC flag on all sockets. We don't want them to be inherited on exec(). We use exec() to run Transceiver and it OpenBTS process crashes, transceiver becomes owner of all LISTEN sockets. --- public-trunk/CommonLibs/Sockets.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/public-trunk/CommonLibs/Sockets.cpp b/public-trunk/CommonLibs/Sockets.cpp index f311100..00cb9f1 100644 --- a/public-trunk/CommonLibs/Sockets.cpp +++ b/public-trunk/CommonLibs/Sockets.cpp @@ -234,6 +234,11 @@ void UDPSocket::open(unsigned short localPort) perror("socket() failed"); throw SocketError(); } + // Set "close on exec" flag to avoid open sockets inheritance by + // child processes, like 'transceiver'. + int flags = fcntl(mSocketFD, F_GETFD); + if (flags >= 0) fcntl(mSocketFD, F_SETFD, flags | FD_CLOEXEC); + // bind struct sockaddr_in address; @@ -280,6 +285,10 @@ void UDDSocket::open(const char* localPath) perror("socket() failed"); throw SocketError(); } + // Set "close on exec" flag to avoid open sockets inheritance by + // child processes, like 'transceiver'. + int flags = fcntl(mSocketFD, F_GETFD); + if (flags >= 0) fcntl(mSocketFD, F_SETFD, flags | FD_CLOEXEC); // bind struct sockaddr_un address; @@ -313,6 +322,10 @@ ConnectionSocket::ConnectionSocket(int af, int type, int protocol) // LOG(ERROR) << "socket() failed with errno=" << errsv; mSocketFD = -1; } + // Set "close on exec" flag to avoid open sockets inheritance by + // child processes, like 'transceiver'. + int flags = fcntl(mSocketFD, F_GETFD); + if (flags >= 0) fcntl(mSocketFD, F_SETFD, flags | FD_CLOEXEC); } ConnectionSocket::~ConnectionSocket() @@ -393,6 +406,10 @@ ConnectionServerSocket::ConnectionServerSocket(int af, int type, int protocol) // LOG(ERROR) << "socket() failed with errno=" << errsv; mSocketFD = -1; } + // Set "close on exec" flag to avoid open sockets inheritance by + // child processes, like 'transceiver'. + int flags = fcntl(mSocketFD, F_GETFD); + if (flags >= 0) fcntl(mSocketFD, F_SETFD, flags | FD_CLOEXEC); } bool ConnectionServerSocket::bindInternal(const sockaddr *addr, int addrlen,