From f3747fd83ae115c6298a3d285ad68c791caa3da1 Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 9 Jun 2008 13:35:35 +0000 Subject: [PATCH] Slight backtracking: Store the specified socket name in the listen socket structure, instead of whatever we actually bound to. It is more informative and intuitive. Report the listen socket spec in the SessionOpen shm record. Close listen sockets as soon as we have forked the child, this eliminated a fd-leak when a socket was eliminated from listen_address while the child ran. Fix various potential problems related to not being able to bind to one or more listen addresses. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2659 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 1 + varnish-cache/bin/varnishd/cache_acceptor.c | 15 ++++++++++----- varnish-cache/bin/varnishd/heritage.h | 3 +-- varnish-cache/bin/varnishd/mgt_child.c | 12 +++--------- varnish-cache/bin/varnishd/mgt_param.c | 3 +++ 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 82ee2645..6dc8aa39 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -319,6 +319,7 @@ struct sess { socklen_t mysockaddrlen; struct sockaddr *sockaddr; struct sockaddr *mysockaddr; + struct listen_sock *mylsock; /* formatted ascii client address */ char *addr; diff --git a/varnish-cache/bin/varnishd/cache_acceptor.c b/varnish-cache/bin/varnishd/cache_acceptor.c index f17d4996..27770ea8 100644 --- a/varnish-cache/bin/varnishd/cache_acceptor.c +++ b/varnish-cache/bin/varnishd/cache_acceptor.c @@ -119,7 +119,8 @@ VCA_Prep(struct sess *sp) addr, sizeof addr, port, sizeof port); sp->addr = WS_Dup(sp->ws, addr); sp->port = WS_Dup(sp->ws, port); - VSL(SLT_SessionOpen, sp->fd, "%s %s", sp->addr, sp->port); + VSL(SLT_SessionOpen, sp->fd, "%s %s %s", + sp->addr, sp->port, sp->mylsock->name); sp->acct.first = sp->t_open; if (need_test) sock_test(sp->fd); @@ -194,16 +195,19 @@ vca_acct(void *arg) #endif i = poll(pfd, heritage.nsocks, 1000); now = TIM_real(); - for (u = 0; u < heritage.nsocks; u++) { - if (pfd[u].revents == 0) + u = 0; + VTAILQ_FOREACH(ls, &heritage.socks, list) { + if (ls->sock < 0) + continue; + if (pfd[u++].revents == 0) continue; VSL_stats->client_conn++; l = sizeof addr_s; addr = (void*)&addr_s; - i = accept(pfd[u].fd, addr, &l); + i = accept(ls->sock, addr, &l); if (i < 0) { if (errno != EAGAIN && errno != ECONNABORTED) { - VSL(SLT_Debug, pfd[u].fd, + VSL(SLT_Debug, ls->sock, "Accept failed errno=%d", errno); /* XXX: stats ? */ } @@ -215,6 +219,7 @@ vca_acct(void *arg) sp->fd = i; sp->id = i; sp->t_open = now; + sp->mylsock = ls; sp->step = STP_FIRST; WRK_QueueSession(sp); diff --git a/varnish-cache/bin/varnishd/heritage.h b/varnish-cache/bin/varnishd/heritage.h index 74542a1e..bf9832a2 100644 --- a/varnish-cache/bin/varnishd/heritage.h +++ b/varnish-cache/bin/varnishd/heritage.h @@ -38,8 +38,7 @@ struct listen_sock { VTAILQ_ENTRY(listen_sock) list; int sock; - char *hname; - char *pname; + char *name; struct vss_addr *addr; }; diff --git a/varnish-cache/bin/varnishd/mgt_child.c b/varnish-cache/bin/varnishd/mgt_child.c index f4fa67ce..005a8826 100644 --- a/varnish-cache/bin/varnishd/mgt_child.c +++ b/varnish-cache/bin/varnishd/mgt_child.c @@ -138,8 +138,6 @@ 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) { @@ -150,9 +148,6 @@ open_sockets(void) 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 @@ -179,8 +174,6 @@ close_sockets(void) continue; AZ(close(ls->sock)); ls->sock = -1; - REPLACE(ls->hname, NULL); - REPLACE(ls->pname, NULL); } } @@ -213,6 +206,7 @@ start_child(void) exit(1); } if (pid == 0) { + /* XXX: We should close things like syslog & telnet sockets */ if (geteuid() == 0) { XXXAZ(setgid(params->gid)); XXXAZ(setuid(params->uid)); @@ -240,6 +234,8 @@ start_child(void) fprintf(stderr, "start child pid %jd\n", (intmax_t)pid); + close_sockets(); + AZ(close(child_fds[1])); child_fds[1] = -1; @@ -292,7 +288,6 @@ stop_child(void) if (child_state != CH_RUNNING) return; - close_sockets(); child_state = CH_STOPPING; if (ev_poker != NULL) { @@ -365,7 +360,6 @@ mgt_sigchld(const struct ev *e, int what) if (child_state == CH_DIED && params->auto_restart) start_child(); else if (child_state == CH_DIED) { - close_sockets(); child_state = CH_STOPPED; } else if (child_state == CH_STOPPING) child_state = CH_STOPPED; diff --git a/varnish-cache/bin/varnishd/mgt_param.c b/varnish-cache/bin/varnishd/mgt_param.c index a0b3613e..9e2d6464 100644 --- a/varnish-cache/bin/varnishd/mgt_param.c +++ b/varnish-cache/bin/varnishd/mgt_param.c @@ -299,6 +299,7 @@ clean_listen_sock_head(struct listen_sock_head *lsh) VTAILQ_FOREACH_SAFE(ls, lsh, list, ls2) { VTAILQ_REMOVE(lsh, ls, list); + free(ls->name); free(ls->addr); free(ls); } @@ -359,6 +360,8 @@ tweak_listen_address(struct cli *cli, const struct parspec *par, const char *arg AN(ls); ls->sock = -1; ls->addr = ta[j]; + ls->name = strdup(av[i]); + AN(ls->name); VTAILQ_INSERT_TAIL(&lsh, ls, list); } free(ta); -- 2.39.5