procstat_cred.c revision 221807
1174199Srwatson/*- 2174199Srwatson * Copyright (c) 2007 Robert N. M. Watson 3174199Srwatson * All rights reserved. 4174199Srwatson * 5174199Srwatson * Redistribution and use in source and binary forms, with or without 6174199Srwatson * modification, are permitted provided that the following conditions 7174199Srwatson * are met: 8174199Srwatson * 1. Redistributions of source code must retain the above copyright 9174199Srwatson * notice, this list of conditions and the following disclaimer. 10174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11174199Srwatson * notice, this list of conditions and the following disclaimer in the 12174199Srwatson * documentation and/or other materials provided with the distribution. 13174199Srwatson * 14174199Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15174199Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16174199Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17174199Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18174199Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19174199Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20174199Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21174199Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22174199Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23174199Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24174199Srwatson * SUCH DAMAGE. 25174199Srwatson * 26174199Srwatson * $FreeBSD: head/usr.bin/procstat/procstat_cred.c 221807 2011-05-12 10:11:39Z stas $ 27174199Srwatson */ 28174199Srwatson 29186567Srwatson#include <sys/param.h> 30174199Srwatson#include <sys/sysctl.h> 31174199Srwatson#include <sys/user.h> 32174199Srwatson 33174199Srwatson#include <err.h> 34221807Sstas#include <libprocstat.h> 35195853Sbrooks#include <stdlib.h> 36174199Srwatson#include <stdio.h> 37195853Sbrooks#include <unistd.h> 38174199Srwatson 39174199Srwatson#include "procstat.h" 40174199Srwatson 41174199Srwatsonvoid 42221807Sstasprocstat_cred(struct kinfo_proc *kipp) 43174199Srwatson{ 44174199Srwatson int i; 45195853Sbrooks int mib[4]; 46195853Sbrooks int ngroups; 47195853Sbrooks size_t len; 48195853Sbrooks gid_t *groups = NULL; 49174199Srwatson 50174199Srwatson if (!hflag) 51174530Srwatson printf("%5s %-16s %5s %5s %5s %5s %5s %5s %-20s\n", "PID", 52174530Srwatson "COMM", "EUID", "RUID", "SVUID", "EGID", "RGID", "SVGID", 53174530Srwatson "GROUPS"); 54174199Srwatson 55221807Sstas printf("%5d ", kipp->ki_pid); 56174530Srwatson printf("%-16s ", kipp->ki_comm); 57174199Srwatson printf("%5d ", kipp->ki_uid); 58174199Srwatson printf("%5d ", kipp->ki_ruid); 59174199Srwatson printf("%5d ", kipp->ki_svuid); 60174199Srwatson printf("%5d ", kipp->ki_groups[0]); 61174199Srwatson printf("%5d ", kipp->ki_rgid); 62174199Srwatson printf("%5d ", kipp->ki_svgid); 63195853Sbrooks 64195853Sbrooks /* 65195853Sbrooks * We may have too many groups to fit in kinfo_proc's statically 66195853Sbrooks * sized storage. If that occurs, attempt to retrieve them via 67195853Sbrooks * sysctl. 68195853Sbrooks */ 69195853Sbrooks if (kipp->ki_cr_flags & KI_CRF_GRP_OVERFLOW) { 70195853Sbrooks mib[0] = CTL_KERN; 71195853Sbrooks mib[1] = KERN_PROC; 72195853Sbrooks mib[2] = KERN_PROC_GROUPS; 73221807Sstas mib[3] = kipp->ki_pid; 74195853Sbrooks 75195853Sbrooks ngroups = sysconf(_SC_NGROUPS_MAX) + 1; 76195853Sbrooks len = ngroups * sizeof(gid_t); 77195853Sbrooks if((groups = malloc(len)) == NULL) 78195853Sbrooks err(-1, "malloc"); 79195853Sbrooks 80195853Sbrooks if (sysctl(mib, 4, groups, &len, NULL, 0) == -1) { 81195853Sbrooks warn("sysctl: kern.proc.groups: %d " 82221807Sstas "group list truncated", kipp->ki_pid); 83195853Sbrooks free(groups); 84195853Sbrooks groups = NULL; 85195853Sbrooks } 86195853Sbrooks ngroups = len / sizeof(gid_t); 87195853Sbrooks } 88195853Sbrooks if (groups == NULL) { 89195853Sbrooks ngroups = kipp->ki_ngroups; 90195853Sbrooks groups = kipp->ki_groups; 91195853Sbrooks } 92195853Sbrooks for (i = 0; i < ngroups; i++) 93195853Sbrooks printf("%s%d", (i > 0) ? "," : "", groups[i]); 94195853Sbrooks if (groups != kipp->ki_groups) 95195853Sbrooks free(groups); 96195853Sbrooks 97174199Srwatson printf("\n"); 98174199Srwatson} 99