Skip to content

Commit 15515b7

Browse files
kusmagitster
authored andcommitted
daemon: consider only address in kill_some_child()
kill_some_child() compares the entire sockaddr_storage structure (with the pad-bits zeroed out) when trying to find out if connections originate from the same host. However, sockaddr_storage contains the port-number for the connection (which varies between connections), so the comparison always fails. Change the code so we only consider the host-address, by introducing the addrcmp()-function that inspects the address family and compare as appropriate. Signed-off-by: Erik Faye-Lund <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3caa823 commit 15515b7

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

daemon.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,24 @@ static int execute(struct sockaddr *addr)
562562
return -1;
563563
}
564564

565+
static int addrcmp(const struct sockaddr_storage *s1,
566+
const struct sockaddr_storage *s2)
567+
{
568+
if (s1->ss_family != s2->ss_family)
569+
return s1->ss_family - s2->ss_family;
570+
if (s1->ss_family == AF_INET)
571+
return memcmp(&((struct sockaddr_in *)s1)->sin_addr,
572+
&((struct sockaddr_in *)s2)->sin_addr,
573+
sizeof(struct in_addr));
574+
#ifndef NO_IPV6
575+
if (s1->ss_family == AF_INET6)
576+
return memcmp(&((struct sockaddr_in6 *)s1)->sin6_addr,
577+
&((struct sockaddr_in6 *)s2)->sin6_addr,
578+
sizeof(struct in6_addr));
579+
#endif
580+
return 0;
581+
}
582+
565583
static int max_connections = 32;
566584

567585
static unsigned int live_children;
@@ -576,17 +594,12 @@ static void add_child(pid_t pid, struct sockaddr *addr, int addrlen)
576594
{
577595
struct child *newborn, **cradle;
578596

579-
/*
580-
* This must be xcalloc() -- we'll compare the whole sockaddr_storage
581-
* but individual address may be shorter.
582-
*/
583597
newborn = xcalloc(1, sizeof(*newborn));
584598
live_children++;
585599
newborn->pid = pid;
586600
memcpy(&newborn->address, addr, addrlen);
587601
for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
588-
if (!memcmp(&(*cradle)->address, &newborn->address,
589-
sizeof(newborn->address)))
602+
if (!addrcmp(&(*cradle)->address, &newborn->address))
590603
break;
591604
newborn->next = *cradle;
592605
*cradle = newborn;
@@ -619,8 +632,7 @@ static void kill_some_child(void)
619632
return;
620633

621634
for (; (next = blanket->next); blanket = next)
622-
if (!memcmp(&blanket->address, &next->address,
623-
sizeof(next->address))) {
635+
if (!addrcmp(&blanket->address, &next->address)) {
624636
kill(blanket->pid, SIGTERM);
625637
break;
626638
}

0 commit comments

Comments
 (0)