Commit 4bffc758 authored by Haijun Zhu's avatar Haijun Zhu Committed by Andrew Cox

Set CLOEXEC for connected fd

Summary:
AsyncSocket sets CLOEXEC if it connects an address, but won't
if it uses a connected fd. This sets CLOEXEC for such fd.

Test Plan: fbconfig folly/io/async/test && fbmake runtests

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, folly-diffs@, zacm

FB internal diff: D1809300

Signature: t1:1809300:1422900997:40fcc84506582ac67076fd975a36d094522c35c2
parent 1219e494
......@@ -222,6 +222,7 @@ AsyncSocket::AsyncSocket(EventBase* evb, int fd)
<< fd << ")";
init();
fd_ = fd;
setCloseOnExec();
state_ = StateEnum::ESTABLISHED;
}
......@@ -293,6 +294,15 @@ void AsyncSocket::setShutdownSocketSet(ShutdownSocketSet* newSS) {
}
}
void AsyncSocket::setCloseOnExec() {
int rv = fcntl(fd_, F_SETFD, FD_CLOEXEC);
if (rv != 0) {
throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
withAddr("failed to set close-on-exec flag"),
errno);
}
}
void AsyncSocket::connect(ConnectCallback* callback,
const folly::SocketAddress& address,
int timeout,
......@@ -331,14 +341,7 @@ void AsyncSocket::connect(ConnectCallback* callback,
}
ioHandler_.changeHandlerFD(fd_);
// Set the FD_CLOEXEC flag so that the socket will be closed if the program
// later forks and execs.
int rv = fcntl(fd_, F_SETFD, FD_CLOEXEC);
if (rv != 0) {
throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
withAddr("failed to set close-on-exec flag"),
errno);
}
setCloseOnExec();
// Put the socket in non-blocking mode
int flags = fcntl(fd_, F_GETFL, 0);
......@@ -346,7 +349,7 @@ void AsyncSocket::connect(ConnectCallback* callback,
throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
withAddr("failed to get socket flags"), errno);
}
rv = fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
int rv = fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
if (rv == -1) {
throw AsyncSocketException(
AsyncSocketException::INTERNAL_ERROR,
......
......@@ -398,6 +398,13 @@ class AsyncSocket : virtual public AsyncTransportWrapper {
*/
int setNoDelay(bool noDelay);
/**
* Set the FD_CLOEXEC flag so that the socket will be closed if the program
* later forks and execs.
*/
void setCloseOnExec();
/*
* Set the Flavor of Congestion Control to be used for this Socket
* Please check '/lib/modules/<kernel>/kernel/net/ipv4' for tcp_*.ko
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment