Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 20 Apr 2013 07:55:32 +0000 (UTC)
From:      Mikolaj Golub <trociny@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r249671 - head/usr.bin/procstat
Message-ID:  <201304200755.r3K7tW6J094952@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trociny
Date: Sat Apr 20 07:55:31 2013
New Revision: 249671
URL: http://svnweb.freebsd.org/changeset/base/249671

Log:
  Use procstat_getgroups(3) for retrieving groups information instead of
  direct sysctl.
  
  MFC after:	1 month

Modified:
  head/usr.bin/procstat/procstat.c
  head/usr.bin/procstat/procstat.h
  head/usr.bin/procstat/procstat_cred.c

Modified: head/usr.bin/procstat/procstat.c
==============================================================================
--- head/usr.bin/procstat/procstat.c	Sat Apr 20 07:54:07 2013	(r249670)
+++ head/usr.bin/procstat/procstat.c	Sat Apr 20 07:55:31 2013	(r249671)
@@ -75,7 +75,7 @@ procstat(struct procstat *prstat, struct
 	else if (lflag)
 		procstat_rlimit(kipp);
 	else if (sflag)
-		procstat_cred(kipp);
+		procstat_cred(prstat, kipp);
 	else if (tflag)
 		procstat_threads(prstat, kipp);
 	else if (vflag)

Modified: head/usr.bin/procstat/procstat.h
==============================================================================
--- head/usr.bin/procstat/procstat.h	Sat Apr 20 07:54:07 2013	(r249670)
+++ head/usr.bin/procstat/procstat.h	Sat Apr 20 07:55:31 2013	(r249671)
@@ -38,7 +38,7 @@ void	procstat_args(struct kinfo_proc *ki
 void	procstat_auxv(struct kinfo_proc *kipp);
 void	procstat_basic(struct kinfo_proc *kipp);
 void	procstat_bin(struct kinfo_proc *kipp);
-void	procstat_cred(struct kinfo_proc *kipp);
+void	procstat_cred(struct procstat *prstat, struct kinfo_proc *kipp);
 void	procstat_env(struct kinfo_proc *kipp);
 void	procstat_files(struct procstat *prstat, struct kinfo_proc *kipp);
 void	procstat_kstack(struct kinfo_proc *kipp, int kflag);

Modified: head/usr.bin/procstat/procstat_cred.c
==============================================================================
--- head/usr.bin/procstat/procstat_cred.c	Sat Apr 20 07:54:07 2013	(r249670)
+++ head/usr.bin/procstat/procstat_cred.c	Sat Apr 20 07:55:31 2013	(r249671)
@@ -41,13 +41,10 @@
 static const char *get_umask(struct kinfo_proc *kipp);
 
 void
-procstat_cred(struct kinfo_proc *kipp)
+procstat_cred(struct procstat *procstat, struct kinfo_proc *kipp)
 {
-	int i;
-	int mib[4];
-	int ngroups;
-	size_t len;
-	gid_t *groups = NULL;
+	unsigned int i, ngroups;
+	gid_t *groups;
 
 	if (!hflag)
 		printf("%5s %-16s %5s %5s %5s %5s %5s %5s %5s %5s %-15s\n",
@@ -66,30 +63,14 @@ procstat_cred(struct kinfo_proc *kipp)
 	printf("%s", kipp->ki_cr_flags & CRED_FLAG_CAPMODE ? "C" : "-");
 	printf("     ");
 
+	groups = NULL;
 	/*
 	 * We may have too many groups to fit in kinfo_proc's statically
-	 * sized storage.  If that occurs, attempt to retrieve them via
-	 * sysctl.
+	 * sized storage.  If that occurs, attempt to retrieve them using
+	 * libprocstat.
 	 */
-	if (kipp->ki_cr_flags & KI_CRF_GRP_OVERFLOW) {
-		mib[0] = CTL_KERN;
-		mib[1] = KERN_PROC;
-		mib[2] = KERN_PROC_GROUPS;
-		mib[3] = kipp->ki_pid;
-
-		ngroups = sysconf(_SC_NGROUPS_MAX) + 1;
-		len = ngroups * sizeof(gid_t);
-		if((groups = malloc(len)) == NULL)
-			err(-1, "malloc");
-
-		if (sysctl(mib, 4, groups, &len, NULL, 0) == -1) {
-			warn("sysctl: kern.proc.groups: %d "
-			    "group list truncated", kipp->ki_pid);
-			free(groups);
-			groups = NULL;
-		}
-		ngroups = len / sizeof(gid_t);
-	}
+	if (kipp->ki_cr_flags & KI_CRF_GRP_OVERFLOW)
+		groups = procstat_getgroups(procstat, kipp, &ngroups);
 	if (groups == NULL) {
 		ngroups = kipp->ki_ngroups;
 		groups = kipp->ki_groups;
@@ -97,7 +78,7 @@ procstat_cred(struct kinfo_proc *kipp)
 	for (i = 0; i < ngroups; i++)
 		printf("%s%d", (i > 0) ? "," : "", groups[i]);
 	if (groups != kipp->ki_groups)
-		free(groups);
+		procstat_freegroups(procstat, groups);
 
 	printf("\n");
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201304200755.r3K7tW6J094952>