]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
msg/async: set close on exec on server sockets
authorKefu Chai <kchai@redhat.com>
Thu, 16 Jun 2016 17:17:05 +0000 (01:17 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 2 Jul 2016 04:50:56 +0000 (12:50 +0800)
mds execv() when handling the "respawn" command, to avoid fd leakage,
and enormous CLOSE_WAIT connections after respawning, we need to set
FD_CLOEXEC flag for the socket fds.

Fixes: http://tracker.ceph.com/issues/16390
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/msg/async/AsyncMessenger.cc
src/msg/async/net_handler.cc
src/msg/async/net_handler.h

index 90bb30dac6487ae8388fa8f529a8750d94795157..df0f4a6c43a895b807eba3ce06be463ea9841c65 100644 (file)
@@ -104,7 +104,7 @@ int Processor::bind(const entity_addr_t &bind_addr, const set<int>& avoid_ports)
     listen_sd = -1;
     return r;
   }
-
+  net.set_close_on_exec(listen_sd);
   net.set_socket_options(listen_sd);
 
   // use whatever user specified (if anything)
@@ -258,6 +258,7 @@ void Processor::accept()
     socklen_t slen = sizeof(ss);
     int sd = ::accept(listen_sd, (sockaddr*)&ss, &slen);
     if (sd >= 0) {
+      net.set_close_on_exec(sd);
       ldout(msgr->cct, 10) << __func__ << " accepted incoming on sd " << sd << dendl;
 
       msgr->add_accept(sd);
index 497c1cb77c605c119c580b226b5a9e8eaba4c599..dae74e4f5eb9f747e60c91fdc5a62818ac90d1f8 100644 (file)
@@ -73,6 +73,22 @@ int NetHandler::set_nonblock(int sd)
   return 0;
 }
 
+void NetHandler::set_close_on_exec(int sd)
+{
+  int flags = fcntl(sd, F_GETFD, 0);
+  if (flags < 0) {
+    int r = errno;
+    lderr(cct) << __func__ << " fcntl(F_GETFD): "
+              << cpp_strerror(r) << dendl;
+    return;
+  }
+  if (fcntl(sd, F_SETFD, flags | FD_CLOEXEC)) {
+    int r = errno;
+    lderr(cct) << __func__ << " fcntl(F_SETFD): "
+              << cpp_strerror(r) << dendl;
+  }
+}
+
 void NetHandler::set_socket_options(int sd)
 {
   // disable Nagle algorithm?
index 499d8fb9e6cbc0358f5185d60556a8650b7531a2..02f2e96c5fb6c9e5bb1e8f083ffe92fab5854e28 100644 (file)
@@ -28,6 +28,7 @@ namespace ceph {
    public:
     explicit NetHandler(CephContext *c): cct(c) {}
     int set_nonblock(int sd);
+    void set_close_on_exec(int sd);
     void set_socket_options(int sd);
     int connect(const entity_addr_t &addr);