svn commit: r251637 - head/lib/libprocstat

John Baldwin jhb at FreeBSD.org
Tue Jun 11 20:00:50 UTC 2013


Author: jhb
Date: Tue Jun 11 20:00:49 2013
New Revision: 251637
URL: http://svnweb.freebsd.org/changeset/base/251637

Log:
  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.
  
  MFC after:	1 week

Modified:
  head/lib/libprocstat/libprocstat.c

Modified: head/lib/libprocstat/libprocstat.c
==============================================================================
--- head/lib/libprocstat/libprocstat.c	Tue Jun 11 19:35:44 2013	(r251636)
+++ head/lib/libprocstat/libprocstat.c	Tue Jun 11 20:00:49 2013	(r251637)
@@ -253,7 +253,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;
@@ -290,12 +290,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