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 <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Erik Faye-Lund committed
Jan 9, 2010 at 15:13 UTC
15515b73716a6ead5a5a348a7d04cc7d73c74bff
1 file changed
+20
-8
daemon.c
+20
-8
@@ -562,6 +562,24 @@ static int execute(struct sockaddr *addr)
562
return -1;
563
}
564
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
+
583
static int max_connections = 32;
584
585
static unsigned int live_children;
@@ -576,17 +594,12 @@ static void add_child(pid_t pid, struct sockaddr *addr, int addrlen)
594
{
595
struct child *newborn, **cradle;
596
579
- /*
580
- * This must be xcalloc() -- we'll compare the whole sockaddr_storage
581
- * but individual address may be shorter.
582
- */
597
newborn = xcalloc(1, sizeof(*newborn));
598
live_children++;
599
newborn->pid = pid;
600
memcpy(&newborn->address, addr, addrlen);
601
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))
603
break;
604
newborn->next = *cradle;
605
*cradle = newborn;
@@ -619,8 +632,7 @@ static void kill_some_child(void)
632
return;
633
634
for (; (next = blanket->next); blanket = next)
622
- if (!memcmp(&blanket->address, &next->address,
623
- sizeof(next->address))) {
635
+ if (!addrcmp(&blanket->address, &next->address)) {
636
kill(blanket->pid, SIGTERM);
637
break;
638
}