Author: trociny
Date: Sat Apr 20 07:54:07 2013
New Revision: 249670
URL: http://svnweb.freebsd.org/changeset/base/249670

Log:
  Add procstat_getgroups function to retrieve process groups.
  
  MFC after:    1 month

Modified:
  head/lib/libprocstat/Symbol.map
  head/lib/libprocstat/core.c
  head/lib/libprocstat/core.h
  head/lib/libprocstat/libprocstat.3
  head/lib/libprocstat/libprocstat.c
  head/lib/libprocstat/libprocstat.h

Modified: head/lib/libprocstat/Symbol.map
==============================================================================
--- head/lib/libprocstat/Symbol.map     Sat Apr 20 07:52:23 2013        
(r249669)
+++ head/lib/libprocstat/Symbol.map     Sat Apr 20 07:54:07 2013        
(r249670)
@@ -16,8 +16,10 @@ FBSD_1.2 {
 };
 
 FBSD_1.3 {
+       procstat_freegroups;
        procstat_freevmmap;
        procstat_get_shm_info;
+       procstat_getgroups;
        procstat_getvmmap;
        procstat_open_core;
 };

Modified: head/lib/libprocstat/core.c
==============================================================================
--- head/lib/libprocstat/core.c Sat Apr 20 07:52:23 2013        (r249669)
+++ head/lib/libprocstat/core.c Sat Apr 20 07:54:07 2013        (r249670)
@@ -167,6 +167,10 @@ procstat_core_get(struct procstat_core *
                n_type = NT_PROCSTAT_VMMAP;
                structsize = sizeof(struct kinfo_vmentry);
                break;
+       case PSC_TYPE_GROUPS:
+               n_type = NT_PROCSTAT_GROUPS;
+               structsize = sizeof(gid_t);
+               break;
        default:
                warnx("unknown core stat type: %d", type);
                return (NULL);

Modified: head/lib/libprocstat/core.h
==============================================================================
--- head/lib/libprocstat/core.h Sat Apr 20 07:52:23 2013        (r249669)
+++ head/lib/libprocstat/core.h Sat Apr 20 07:54:07 2013        (r249670)
@@ -33,6 +33,7 @@ enum psc_type {
        PSC_TYPE_PROC,
        PSC_TYPE_FILES,
        PSC_TYPE_VMMAP,
+       PSC_TYPE_GROUPS,
 };
 
 struct procstat_core;

Modified: head/lib/libprocstat/libprocstat.3
==============================================================================
--- head/lib/libprocstat/libprocstat.3  Sat Apr 20 07:52:23 2013        
(r249669)
+++ head/lib/libprocstat/libprocstat.3  Sat Apr 20 07:54:07 2013        
(r249670)
@@ -33,9 +33,11 @@
 .Nm procstat_open_sysctl ,
 .Nm procstat_close ,
 .Nm procstat_getfiles ,
+.Nm procstat_getgroups ,
 .Nm procstat_getprocs ,
 .Nm procstat_getvmmap ,
 .Nm procstat_freefiles ,
+.Nm procstat_freegroups ,
 .Nm procstat_freeprocs ,
 .Nm procstat_freevmmap ,
 .Nm procstat_get_pipe_info ,
@@ -52,12 +54,18 @@
 .In libprocstat.h
 .Ft void
 .Fn procstat_close "struct procstat *procstat"
+.Fc
 .Ft void
 .Fo procstat_freefiles
 .Fa "struct procstat *procstat"
 .Fa "struct filestat_list *head"
 .Fc
 .Ft void
+.Fo procstat_freegroups
+.Fa "struct procstat *procstat"
+.Fa "gid_t *groups"
+.Fc
+.Ft void
 .Fn procstat_freeprocs "struct procstat *procstat" "struct kinfo_proc *p"
 .Ft void
 .Fo procstat_freevmmap
@@ -105,6 +113,12 @@
 .Fa "struct kinfo_proc *kp"
 .Fa "int mmapped"
 .Fc
+.Ft "gid_t *"
+.Fo procstat_getgroups
+.Fa "struct procstat *procstat"
+.Fa "struct kinfo_proc *kp"
+.Fa "unsigned int *count"
+.Fc
 .Ft "struct kinfo_proc *"
 .Fo procstat_getprocs
 .Fa "struct procstat *procstat"
@@ -227,6 +241,19 @@ The caller is responsible to free the al
 function call.
 .Pp
 The
+.Fn procstat_getgroups
+function gets a pointer to the
+.Vt procstat
+structure, a pointer to
+.Vt kinfo_proc
+structure, and returns the process groups as a dynamically allocated array of
+.Vt gid_t
+elements.
+The caller is responsible to free the allocated memory with a subsequent
+.Fn procstat_freegroups
+function call.
+.Pp
+The
 .Fn procstat_getvmmap
 function gets a pointer to the
 .Vt procstat

Modified: head/lib/libprocstat/libprocstat.c
==============================================================================
--- head/lib/libprocstat/libprocstat.c  Sat Apr 20 07:52:23 2013        
(r249669)
+++ head/lib/libprocstat/libprocstat.c  Sat Apr 20 07:54:07 2013        
(r249670)
@@ -132,6 +132,9 @@ static int  procstat_get_vnode_info_kvm(k
     struct vnstat *vn, char *errbuf);
 static int     procstat_get_vnode_info_sysctl(struct filestat *fst,
     struct vnstat *vn, char *errbuf);
+static gid_t   *procstat_getgroups_core(struct procstat_core *core,
+    unsigned int *count);
+static gid_t   *procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
 static int     vntype2psfsttype(int type);
 
 void
@@ -1588,3 +1591,67 @@ procstat_freevmmap(struct procstat *proc
 
        free(vmmap);
 }
+
+static gid_t *
+procstat_getgroups_sysctl(pid_t pid, unsigned int *cntp)
+{
+       int mib[4];
+       size_t len;
+       gid_t *groups;
+
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_PROC;
+       mib[2] = KERN_PROC_GROUPS;
+       mib[3] = pid;
+       len = (sysconf(_SC_NGROUPS_MAX) + 1) * sizeof(gid_t);
+       groups = malloc(len);
+       if (groups == NULL) {
+               warn("malloc(%zu)", len);
+               return (NULL);
+       }
+       if (sysctl(mib, 4, groups, &len, NULL, 0) == -1) {
+               warn("sysctl: kern.proc.groups: %d", pid);
+               free(groups);
+               return (NULL);
+       }
+       *cntp = len / sizeof(gid_t);
+       return (groups);
+}
+
+static gid_t *
+procstat_getgroups_core(struct procstat_core *core, unsigned int *cntp)
+{
+       size_t len;
+       gid_t *groups;
+
+       groups = procstat_core_get(core, PSC_TYPE_GROUPS, NULL, &len);
+       if (groups == NULL)
+               return (NULL);
+       *cntp = len / sizeof(gid_t);
+       return (groups);
+}
+
+gid_t *
+procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
+    unsigned int *cntp)
+{
+       switch(procstat->type) {
+       case PROCSTAT_KVM:
+               warnx("kvm method is not supported");
+               return (NULL);
+       case PROCSTAT_SYSCTL:
+               return (procstat_getgroups_sysctl(kp->ki_pid, cntp));
+       case PROCSTAT_CORE:
+               return (procstat_getgroups_core(procstat->core, cntp));
+       default:
+               warnx("unknown access method: %d", procstat->type);
+               return (NULL);
+       }
+}
+
+void
+procstat_freegroups(struct procstat *procstat __unused, gid_t *groups)
+{
+
+       free(groups);
+}

Modified: head/lib/libprocstat/libprocstat.h
==============================================================================
--- head/lib/libprocstat/libprocstat.h  Sat Apr 20 07:52:23 2013        
(r249669)
+++ head/lib/libprocstat/libprocstat.h  Sat Apr 20 07:54:07 2013        
(r249670)
@@ -146,6 +146,7 @@ STAILQ_HEAD(filestat_list, filestat);
 
 __BEGIN_DECLS
 void   procstat_close(struct procstat *procstat);
+void   procstat_freegroups(struct procstat *procstat, gid_t *groups);
 void   procstat_freeprocs(struct procstat *procstat, struct kinfo_proc *p);
 void   procstat_freefiles(struct procstat *procstat,
     struct filestat_list *head);
@@ -165,6 +166,8 @@ int procstat_get_socket_info(struct proc
     struct sockstat *sock, char *errbuf);
 int    procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
     struct vnstat *vn, char *errbuf);
+gid_t  *procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
+    unsigned int *count);
 struct kinfo_vmentry   *procstat_getvmmap(struct procstat *procstat,
     struct kinfo_proc *kp, unsigned int *count);
 struct procstat        *procstat_open_core(const char *filename);
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to