From: phk Date: Mon, 9 Jun 2008 13:11:45 +0000 (+0000) Subject: Don't leak listen address structures if we fail to bind to them. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc8283be9791ccd5723b58aa42c9735bc60f662e;p=varnish Don't leak listen address structures if we fail to bind to them. Associate ascii representation addr/port strings with each listen socket, already in the manager process. Postpone listen(2) call until client is ready, to limit initial connection spike. XXX: I still miss an unlisten(2). git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2658 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache_acceptor.c b/varnish-cache/bin/varnishd/cache_acceptor.c index 5dfd39b3..f17d4996 100644 --- a/varnish-cache/bin/varnishd/cache_acceptor.c +++ b/varnish-cache/bin/varnishd/cache_acceptor.c @@ -161,6 +161,9 @@ vca_acct(void *arg) AN(pfd); i = 0; VTAILQ_FOREACH(ls, &heritage.socks, list) { + if (ls->sock < 0) + continue; + AZ(listen(ls->sock, params->listen_depth)); AZ(setsockopt(ls->sock, SOL_SOCKET, SO_LINGER, &linger, sizeof linger)); pfd[i].events = POLLIN; diff --git a/varnish-cache/bin/varnishd/heritage.h b/varnish-cache/bin/varnishd/heritage.h index 374e2f88..74542a1e 100644 --- a/varnish-cache/bin/varnishd/heritage.h +++ b/varnish-cache/bin/varnishd/heritage.h @@ -38,6 +38,8 @@ struct listen_sock { VTAILQ_ENTRY(listen_sock) list; int sock; + char *hname; + char *pname; struct vss_addr *addr; }; diff --git a/varnish-cache/bin/varnishd/mgt_child.c b/varnish-cache/bin/varnishd/mgt_child.c index 7b88d304..f4fa67ce 100644 --- a/varnish-cache/bin/varnishd/mgt_child.c +++ b/varnish-cache/bin/varnishd/mgt_child.c @@ -138,18 +138,21 @@ open_sockets(void) { struct listen_sock *ls, *ls2; int good = 0; + char hbuf[TCP_ADDRBUFSIZE]; + char pbuf[TCP_PORTBUFSIZE]; VTAILQ_FOREACH_SAFE(ls, &heritage.socks, list, ls2) { if (ls->sock >= 0) { good++; continue; } - ls->sock = VSS_listen(ls->addr, params->listen_depth); - if (ls->sock < 0) { - VTAILQ_REMOVE(&heritage.socks, ls, list); - free(ls); + ls->sock = VSS_bind(ls->addr); + if (ls->sock < 0) continue; - } + + TCP_myname(ls->sock, hbuf, sizeof hbuf, pbuf, sizeof pbuf); + REPLACE(ls->hname, hbuf); + REPLACE(ls->pname, pbuf); /* * Set nonblocking mode to avoid a race where a client * closes before we call accept(2) and nobody else are in @@ -174,8 +177,10 @@ close_sockets(void) VTAILQ_FOREACH(ls, &heritage.socks, list) { if (ls->sock < 0) continue; - (void)close(ls->sock); + AZ(close(ls->sock)); ls->sock = -1; + REPLACE(ls->hname, NULL); + REPLACE(ls->pname, NULL); } }