You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2012/05/01 00:20:22 UTC

svn commit: r1332448 - in /incubator/mesos/trunk/third_party/libprocess/src: net.hpp process.cpp

Author: benh
Date: Mon Apr 30 22:20:21 2012
New Revision: 1332448

URL: http://svn.apache.org/viewvc?rev=1332448&view=rev
Log:
Added FD_CLOEXEC to sockets opened by libprocess (contributed by Vinod Kone).

Modified:
    incubator/mesos/trunk/third_party/libprocess/src/net.hpp
    incubator/mesos/trunk/third_party/libprocess/src/process.cpp

Modified: incubator/mesos/trunk/third_party/libprocess/src/net.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/net.hpp?rev=1332448&r1=1332447&r2=1332448&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/net.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/net.hpp Mon Apr 30 22:20:21 2012
@@ -42,23 +42,23 @@ protected:
   {
     if ((s = ::socket(AF_INET, protocol, IPPROTO_IP)) < 0)
       throw runtime_error(string("socket: ") += strerror(errno));
-    
-    int flags = 1;
-    if (ioctl(s, FIONBIO, &flags) &&
-	((flags = fcntl(s, F_GETFL, 0)) < 0 ||
-	 fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0))
-      throw runtime_error(string("ioctl/fcntl: ") += strerror(errno));
+
+    socket(s);
   }
 
-  virtual void socket(int _s)
+  virtual void socket(int sd)
   {
-    s = _s;
-    
+    s = sd;
+
     int flags = 1;
     if (ioctl(s, FIONBIO, &flags) &&
-	((flags = fcntl(s, F_GETFL, 0)) < 0 ||
-	 fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0))
+        ((flags = fcntl(s, F_GETFL, 0)) < 0 ||
+          fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0))
       throw runtime_error(string("ioctl/fcntl: ") += strerror(errno));
+
+    if (fcntl(s, F_SETFD, FD_CLOEXEC) < 0) {
+      throw runtime_error(string("fcntl: ") += strerror(errno));
+    }
   }
 
   virtual void bind(in_addr_t ip, in_port_t port)
@@ -108,7 +108,7 @@ protected:
     } while (offset != bytes);
 
     return offset;
-  }  
+  }
 
   virtual void send(const void *buf, size_t bytes)
   {
@@ -116,7 +116,7 @@ protected:
     do {
       size_t len =
 	::send(s, (char *) buf + offset, bytes - offset, MSG_NOSIGNAL);
-      
+
       if (len > 0)
 	offset += len;
       else if (len < 0 && errno == EWOULDBLOCK)
@@ -175,13 +175,13 @@ protected:
       c = ::accept(SocketProcess<protocol>::s,
 		       (struct sockaddr *) &addr,
 		       (socklen_t *) &size);
-    
+
       if (c == 0)
 	throw runtime_error(string("accept: ") += strerror(errno));
       else if (c < 0 && (errno != EWOULDBLOCK))
 	throw runtime_error(string("accept: ") += strerror(errno));
     } while (!(c > 0));
-    
+
     return c;
   }
 

Modified: incubator/mesos/trunk/third_party/libprocess/src/process.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/process.cpp?rev=1332448&r1=1332447&r2=1332448&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/process.cpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/process.cpp Mon Apr 30 22:20:21 2012
@@ -1058,6 +1058,13 @@ void accept(struct ev_loop* loop, ev_io*
   }
 
   if (set_nbio(s) < 0) {
+    PLOG_IF(INFO, VLOG_IS_ON(1)) << "Failed to accept, set_nbio";
+    close(s);
+    return;
+  }
+
+  if (fcntl(s, F_SETFD, FD_CLOEXEC)) {
+    PLOG_IF(INFO, VLOG_IS_ON(1)) << "Failed to accept, FD_CLOEXEC";
     close(s);
     return;
   }
@@ -1244,6 +1251,11 @@ void initialize(const string& delegate, 
     PLOG(FATAL) << "Failed to initialize, set_nbio";
   }
 
+  // Set FD_CLOEXEC flag.
+  if (fcntl(__s__, F_SETFD, FD_CLOEXEC)) {
+    PLOG(FATAL) << "Failed to initialize, FD_CLOEXEC";
+  }
+
   // Allow address reuse.
   int on = 1;
   if (setsockopt(__s__, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
@@ -1550,6 +1562,10 @@ void SocketManager::link(ProcessBase* pr
         PLOG(FATAL) << "Failed to link, set_nbio";
       }
 
+      if (fcntl(s, F_SETFD, FD_CLOEXEC)) {
+        PLOG(FATAL) << "Failed to link, FD_CLOEXEC";
+      }
+
       Socket socket = Socket(s);
 
       sockets[s] = socket;
@@ -1691,6 +1707,10 @@ void SocketManager::send(Message* messag
         PLOG(FATAL) << "Failed to send, set_nbio";
       }
 
+      if (fcntl(s, F_SETFD, FD_CLOEXEC)) {
+        PLOG(FATAL) << "Failed to send, FD_CLOEXEC";
+      }
+
       sockets[s] = Socket(s);
       nodes[s] = node;
       temps[node] = s;