Commit 8181e2ed authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

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: http://www.varnish-cache.org/svn/trunk/varnish-cache@2659 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 1476982d
......@@ -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;
......
......@@ -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);
......
......@@ -38,8 +38,7 @@
struct listen_sock {
VTAILQ_ENTRY(listen_sock) list;
int sock;
char *hname;
char *pname;
char *name;
struct vss_addr *addr;
};
......
......@@ -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;
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment