From owner-freebsd-current@FreeBSD.ORG Mon Feb 24 04:10:24 2014 Return-Path: Delivered-To: current@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 2CC0027C for ; Mon, 24 Feb 2014 04:10:24 +0000 (UTC) Received: from mail.allbsd.org (gatekeeper.allbsd.org [IPv6:2001:2f0:104:e001::32]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 710951BE2 for ; Mon, 24 Feb 2014 04:10:23 +0000 (UTC) Received: from alph.d.allbsd.org (p2106-ipbf2009funabasi.chiba.ocn.ne.jp [114.146.169.106]) (authenticated bits=128) by mail.allbsd.org (8.14.5/8.14.5) with ESMTP id s1O4A2ZO031390 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 24 Feb 2014 13:10:12 +0900 (JST) (envelope-from hrs@FreeBSD.org) Received: from localhost (localhost [IPv6:::1]) (authenticated bits=0) by alph.d.allbsd.org (8.14.7/8.14.7) with ESMTP id s1O4A1L0028230; Mon, 24 Feb 2014 13:10:02 +0900 (JST) (envelope-from hrs@FreeBSD.org) Date: Mon, 24 Feb 2014 13:09:24 +0900 (JST) Message-Id: <20140224.130924.1512809959333112659.hrs@allbsd.org> To: ianf@clue.co.za Subject: Re: netstat: sysctl: net.route.0.0.dump.0: Cannot allocate memory From: Hiroki Sato In-Reply-To: References: X-PGPkey-fingerprint: BDB3 443F A5DD B3D0 A530 FFD7 4F2C D3D8 2793 CF2D X-Mailer: Mew version 6.5 on Emacs 24.3 / Mule 6.0 (HANACHIRUSATO) Mime-Version: 1.0 Content-Type: Multipart/Signed; protocol="application/pgp-signature"; micalg=pgp-sha1; boundary="--Security_Multipart0(Mon_Feb_24_13_09_24_2014_476)--" Content-Transfer-Encoding: 7bit X-Virus-Scanned: clamav-milter 0.97.4 at gatekeeper.allbsd.org X-Virus-Status: Clean X-Greylist: Sender DNS name whitelisted, not delayed by milter-greylist-4.2.7 (mail.allbsd.org [133.31.130.32]); Mon, 24 Feb 2014 13:10:13 +0900 (JST) X-Spam-Status: No, score=-94.3 required=13.0 tests=CONTENT_TYPE_PRESENT, RCVD_IN_PBL,RCVD_IN_RP_RNBL,SPF_SOFTFAIL,USER_IN_WHITELIST autolearn=no version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on gatekeeper.allbsd.org Cc: current@FreeBSD.org X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Feb 2014 04:10:24 -0000 ----Security_Multipart0(Mon_Feb_24_13_09_24_2014_476)-- Content-Type: Multipart/Mixed; boundary="--Next_Part(Mon_Feb_24_13_09_24_2014_200)--" Content-Transfer-Encoding: 7bit ----Next_Part(Mon_Feb_24_13_09_24_2014_200)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Ian FREISLICH wrote in : ia> Hiroki Sato wrote: ia> > Hm, how about the attached one? ia> > ia> > I think the cause is just a race when length of the sysctl's output ia> > is changed in kernel after the buffer allocation in userspace, not ia> > memory shortage. Size of the routing table can quickly change. ia> ia> You are correct. It's growing at about 9000 entries per second (I ia> wish it were faster). ia> ia> This is what the output looks like now. I guess I'm not the average ia> case. Can you try the attached patch? It will attempt to enlarge the buffer every retry. -- Hiroki ----Next_Part(Mon_Feb_24_13_09_24_2014_200)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="netstat-2.diff" Index: route.c =================================================================== --- route.c (revision 262313) +++ route.c (working copy) @@ -69,6 +69,7 @@ #include #include #include +#include #include "netstat.h" #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d))) @@ -560,7 +561,7 @@ char *buf, *next, *lim; struct rt_msghdr *rtm; struct sockaddr *sa; - int fam = 0, ifindex = 0, size; + int fam = 0, ifindex = 0, size, count = 0; struct ifaddrs *ifap, *ifa; struct sockaddr_dl *sdl; @@ -600,6 +601,7 @@ freeifaddrs(ifap); +retry: mib[0] = CTL_NET; mib[1] = PF_ROUTE; mib[2] = 0; @@ -607,19 +609,27 @@ mib[4] = NET_RT_DUMP; mib[5] = 0; mib[6] = fibnum; - if (sysctl(mib, 7, NULL, &needed, NULL, 0) < 0) { - err(1, "sysctl: net.route.0.%d.dump.%d estimate", af, fibnum); + if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) + err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d estimate", af, + fibnum); + needed += PAGE_SIZE * (count + 1); + if ((buf = malloc(needed)) == NULL) + errx(EX_OSERR, "malloc(%zd)", needed); + if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) { + if (errno == ENOMEM && count++ < 20) { + warnx("Routing table grew, retrying"); + sleep(1); + free(buf); + goto retry; + } else + err(EX_OSERR, "sysctl: net.route.0.%d.dump.%d", af, + fibnum); } - - if ((buf = malloc(needed)) == 0) { - errx(2, "malloc(%lu)", (unsigned long)needed); - } - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { - err(1, "sysctl: net.route.0.%d.dump.%d", af, fibnum); - } lim = buf + needed; for (next = buf; next < lim; next += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)next; + if (rtm->rtm_version != RTM_VERSION) + continue; /* * Peek inside header to determine AF */ @@ -632,6 +642,7 @@ } p_rtentry_sysctl(rtm); } + free(buf); } static void ----Next_Part(Mon_Feb_24_13_09_24_2014_200)---- ----Security_Multipart0(Mon_Feb_24_13_09_24_2014_476)-- Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iEYEABECAAYFAlMKxfQACgkQTyzT2CeTzy311gCcCe7gnRiQIX0bqSKt4ykC6hX/ EiIAoK2IhblmxU68fA+6Xbw/YiJ5w8U9 =+fo0 -----END PGP SIGNATURE----- ----Security_Multipart0(Mon_Feb_24_13_09_24_2014_476)----