svn commit: r252065 - stable/9/lib/libprocstat

John Baldwin jhb at FreeBSD.org
Fri Jun 21 19:28:58 UTC 2013


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;


More information about the svn-src-all mailing list