Skip to content

Commit

Permalink
msg/simple: set close on exec on server sockets
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
tchaikov committed Jun 20, 2016
1 parent 9f96da9 commit 90069ce
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/msg/simple/Accepter.cc
Expand Up @@ -37,6 +37,18 @@
* Accepter
*/

static int set_close_on_exec(int fd)
{
int flags = fcntl(fd, F_GETFD, 0);
if (flags < 0) {
return errno;
}
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC)) {
return errno;
}
return 0;
}

int Accepter::bind(const entity_addr_t &bind_addr, const set<int>& avoid_ports)
{
const md_config_t *conf = msgr->cct->_conf;
Expand All @@ -63,6 +75,11 @@ int Accepter::bind(const entity_addr_t &bind_addr, const set<int>& avoid_ports)
return -errno;
}

if (set_close_on_exec(listen_sd)) {
lderr(msgr->cct) << "accepter.bind unable to set_close_exec(): "
<< cpp_strerror(errno) << dendl;
}

// use whatever user specified (if anything)
entity_addr_t listen_addr = bind_addr;
listen_addr.set_family(family);
Expand Down Expand Up @@ -244,6 +261,11 @@ void *Accepter::entry()
socklen_t slen = sizeof(ss);
int sd = ::accept(listen_sd, (sockaddr*)&ss, &slen);
if (sd >= 0) {
int r = set_close_on_exec(sd);
if (r) {
ldout(msgr->cct,0) << "accepter set_close_on_exec() failed "
<< cpp_strerror(r) << dendl;
}
errors = 0;
ldout(msgr->cct,10) << "accepted incoming on sd " << sd << dendl;

Expand Down

0 comments on commit 90069ce

Please sign in to comment.