From owner-svn-src-all@FreeBSD.ORG Fri Jun 21 19:28:58 2013 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id B6FAAF2C; Fri, 21 Jun 2013 19:28:58 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id A86121125; Fri, 21 Jun 2013 19:28:58 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r5LJSwUT006612; Fri, 21 Jun 2013 19:28:58 GMT (envelope-from jhb@svn.freebsd.org) Received: (from jhb@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r5LJSwn7006611; Fri, 21 Jun 2013 19:28:58 GMT (envelope-from jhb@svn.freebsd.org) Message-Id: <201306211928.r5LJSwn7006611@svn.freebsd.org> From: John Baldwin Date: Fri, 21 Jun 2013 19:28:58 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org Subject: svn commit: r252065 - stable/9/lib/libprocstat X-SVN-Group: stable-9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Jun 2013 19:28:58 -0000 Author: jhb Date: Fri Jun 21 19:28:58 2013 New Revision: 252065 URL: http://svnweb.freebsd.org/changeset/base/252065 Log: MFC 251637: Borrow the algorithm from kvm_getprocs() to fix procstat_getprocs() to handle the case where the process tables grows in between the calls to fetch the size and fetch the table. Modified: stable/9/lib/libprocstat/libprocstat.c Directory Properties: stable/9/lib/libprocstat/ (props changed) Modified: stable/9/lib/libprocstat/libprocstat.c ============================================================================== --- stable/9/lib/libprocstat/libprocstat.c Fri Jun 21 18:16:54 2013 (r252064) +++ stable/9/lib/libprocstat/libprocstat.c Fri Jun 21 19:28:58 2013 (r252065) @@ -248,7 +248,7 @@ procstat_getprocs(struct procstat *procs unsigned int *count) { struct kinfo_proc *p0, *p; - size_t len; + size_t len, olen; int name[4]; int cnt; int error; @@ -285,12 +285,16 @@ procstat_getprocs(struct procstat *procs warnx("no processes?"); goto fail; } - p = malloc(len); - if (p == NULL) { - warnx("malloc(%zu)", len); - goto fail; - } - error = sysctl(name, 4, p, &len, NULL, 0); + do { + len += len / 10; + p = reallocf(p, len); + if (p == NULL) { + warnx("reallocf(%zu)", len); + goto fail; + } + olen = len; + error = sysctl(name, 4, p, &len, NULL, 0); + } while (error < 0 && errno == ENOMEM && olen == len); if (error < 0 && errno != EPERM) { warn("sysctl(kern.proc)"); goto fail;