Date: Fri, 21 Jun 2013 19:28:58 +0000 (UTC) From: John Baldwin <jhb@FreeBSD.org> 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 Message-ID: <201306211928.r5LJSwn7006611@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
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;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201306211928.r5LJSwn7006611>