Set CLOEXEC for connected fd
authorHaijun Zhu <haijunz@fb.com>
Mon, 2 Feb 2015 19:41:38 +0000 (11:41 -0800)
committerAndrew Cox <andrewcox@fb.com>
Wed, 4 Feb 2015 20:58:38 +0000 (12:58 -0800)
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

folly/io/async/AsyncSocket.cpp
folly/io/async/AsyncSocket.h

index 167a49bfdd3b105ec2804128ae9151de4fff02c5..c8754d2c73437a96b147c0bfc535dd70faf501b2 100644 (file)
@@ -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,
index 7819647bfee30ca85ba74d93ff4d8c010f0c8010..10614abf04cbab8d1a25cb83b02ea7c3afdd7eec 100644 (file)
@@ -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