1174199Srwatson/*-
2224859Srwatson * Copyright (c) 2007-2008 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$
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
41249673Strocinystatic const char *get_umask(struct procstat *procstat,
42249673Strociny    struct kinfo_proc *kipp);
43232182Strociny
44174199Srwatsonvoid
45249671Strocinyprocstat_cred(struct procstat *procstat, struct kinfo_proc *kipp)
46174199Srwatson{
47249671Strociny	unsigned int i, ngroups;
48249671Strociny	gid_t *groups;
49174199Srwatson
50174199Srwatson	if (!hflag)
51232182Strociny		printf("%5s %-16s %5s %5s %5s %5s %5s %5s %5s %5s %-15s\n",
52232182Strociny		    "PID", "COMM", "EUID", "RUID", "SVUID", "EGID", "RGID",
53232182Strociny		    "SVGID", "UMASK", "FLAGS", "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);
63249673Strociny	printf("%5s ", get_umask(procstat, kipp));
64224859Srwatson	printf("%s", kipp->ki_cr_flags & CRED_FLAG_CAPMODE ? "C" : "-");
65224859Srwatson	printf("     ");
66195853Sbrooks
67249671Strociny	groups = NULL;
68195853Sbrooks	/*
69195853Sbrooks	 * We may have too many groups to fit in kinfo_proc's statically
70249671Strociny	 * sized storage.  If that occurs, attempt to retrieve them using
71249671Strociny	 * libprocstat.
72195853Sbrooks	 */
73249671Strociny	if (kipp->ki_cr_flags & KI_CRF_GRP_OVERFLOW)
74249671Strociny		groups = procstat_getgroups(procstat, kipp, &ngroups);
75195853Sbrooks	if (groups == NULL) {
76195853Sbrooks		ngroups = kipp->ki_ngroups;
77195853Sbrooks		groups = kipp->ki_groups;
78195853Sbrooks	}
79195853Sbrooks	for (i = 0; i < ngroups; i++)
80195853Sbrooks		printf("%s%d", (i > 0) ? "," : "", groups[i]);
81195853Sbrooks	if (groups != kipp->ki_groups)
82249671Strociny		procstat_freegroups(procstat, groups);
83195853Sbrooks
84174199Srwatson	printf("\n");
85174199Srwatson}
86232182Strociny
87232182Strocinystatic const char *
88249673Strocinyget_umask(struct procstat *procstat, struct kinfo_proc *kipp)
89232182Strociny{
90232182Strociny	u_short fd_cmask;
91232182Strociny	static char umask[4];
92232182Strociny
93249673Strociny	if (procstat_getumask(procstat, kipp, &fd_cmask) == 0) {
94232182Strociny		snprintf(umask, 4, "%03o", fd_cmask);
95232182Strociny		return (umask);
96232182Strociny	} else {
97232182Strociny		return ("-");
98232182Strociny	}
99232182Strociny}
100