13d91be41SRobert Watson /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4d57486e2SRobert Watson * Copyright (c) 2007-2008 Robert N. M. Watson
5474b62b8SAllan Jude * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
63d91be41SRobert Watson * All rights reserved.
73d91be41SRobert Watson *
83d91be41SRobert Watson * Redistribution and use in source and binary forms, with or without
93d91be41SRobert Watson * modification, are permitted provided that the following conditions
103d91be41SRobert Watson * are met:
113d91be41SRobert Watson * 1. Redistributions of source code must retain the above copyright
123d91be41SRobert Watson * notice, this list of conditions and the following disclaimer.
133d91be41SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
143d91be41SRobert Watson * notice, this list of conditions and the following disclaimer in the
153d91be41SRobert Watson * documentation and/or other materials provided with the distribution.
163d91be41SRobert Watson *
173d91be41SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183d91be41SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193d91be41SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203d91be41SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213d91be41SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223d91be41SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233d91be41SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243d91be41SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253d91be41SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263d91be41SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273d91be41SRobert Watson * SUCH DAMAGE.
283d91be41SRobert Watson */
293d91be41SRobert Watson
30e1f323f3SRobert Watson #include <sys/param.h>
313d91be41SRobert Watson #include <sys/sysctl.h>
323d91be41SRobert Watson #include <sys/user.h>
333d91be41SRobert Watson
343d91be41SRobert Watson #include <err.h>
350daf62d9SStanislav Sedov #include <libprocstat.h>
36254d03c5SBrooks Davis #include <stdlib.h>
373d91be41SRobert Watson #include <stdio.h>
38254d03c5SBrooks Davis #include <unistd.h>
393d91be41SRobert Watson
403d91be41SRobert Watson #include "procstat.h"
413d91be41SRobert Watson
4290a15eb9SMikolaj Golub static const char *get_umask(struct procstat *procstat,
4390a15eb9SMikolaj Golub struct kinfo_proc *kipp);
44c077ef00SMikolaj Golub
453d91be41SRobert Watson void
procstat_cred(struct procstat * procstat,struct kinfo_proc * kipp)46e40d6078SMikolaj Golub procstat_cred(struct procstat *procstat, struct kinfo_proc *kipp)
473d91be41SRobert Watson {
48e40d6078SMikolaj Golub unsigned int i, ngroups;
49e40d6078SMikolaj Golub gid_t *groups;
503d91be41SRobert Watson
512a243b95SBrooks Davis if ((procstat_opts & PS_OPT_NOHEADER) == 0)
52474b62b8SAllan Jude xo_emit("{T:/%5s %-16s %5s %5s %5s %5s %5s %5s %5s %5s %-15s}\n",
53c077ef00SMikolaj Golub "PID", "COMM", "EUID", "RUID", "SVUID", "EGID", "RGID",
54c077ef00SMikolaj Golub "SVGID", "UMASK", "FLAGS", "GROUPS");
553d91be41SRobert Watson
56474b62b8SAllan Jude xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid);
57474b62b8SAllan Jude xo_emit("{:command/%-16s/%s} ", kipp->ki_comm);
58474b62b8SAllan Jude xo_emit("{:uid/%5d} ", kipp->ki_uid);
59474b62b8SAllan Jude xo_emit("{:ruid/%5d} ", kipp->ki_ruid);
60474b62b8SAllan Jude xo_emit("{:svuid/%5d} ", kipp->ki_svuid);
61474b62b8SAllan Jude xo_emit("{:group/%5d} ", kipp->ki_groups[0]);
62474b62b8SAllan Jude xo_emit("{:rgid/%5d} ", kipp->ki_rgid);
63474b62b8SAllan Jude xo_emit("{:svgid/%5d} ", kipp->ki_svgid);
64474b62b8SAllan Jude xo_emit("{:umask/%5s} ", get_umask(procstat, kipp));
65*09290c3aSOlivier Certner xo_emit("{:cr_flags/%s}", kipp->ki_cr_flags & KI_CRF_CAPABILITY_MODE ?
66474b62b8SAllan Jude "C" : "-");
67474b62b8SAllan Jude xo_emit("{P: }");
68254d03c5SBrooks Davis
69e40d6078SMikolaj Golub groups = NULL;
70254d03c5SBrooks Davis /*
71254d03c5SBrooks Davis * We may have too many groups to fit in kinfo_proc's statically
72e40d6078SMikolaj Golub * sized storage. If that occurs, attempt to retrieve them using
73e40d6078SMikolaj Golub * libprocstat.
74254d03c5SBrooks Davis */
75e40d6078SMikolaj Golub if (kipp->ki_cr_flags & KI_CRF_GRP_OVERFLOW)
76e40d6078SMikolaj Golub groups = procstat_getgroups(procstat, kipp, &ngroups);
77254d03c5SBrooks Davis if (groups == NULL) {
78254d03c5SBrooks Davis ngroups = kipp->ki_ngroups;
79254d03c5SBrooks Davis groups = kipp->ki_groups;
80254d03c5SBrooks Davis }
81474b62b8SAllan Jude xo_open_list("groups");
82254d03c5SBrooks Davis for (i = 0; i < ngroups; i++)
83474b62b8SAllan Jude xo_emit("{D:/%s}{l:groups/%d}", (i > 0) ? "," : "", groups[i]);
84254d03c5SBrooks Davis if (groups != kipp->ki_groups)
85e40d6078SMikolaj Golub procstat_freegroups(procstat, groups);
86254d03c5SBrooks Davis
87474b62b8SAllan Jude xo_close_list("groups");
88474b62b8SAllan Jude xo_emit("\n");
893d91be41SRobert Watson }
90c077ef00SMikolaj Golub
91c077ef00SMikolaj Golub static const char *
get_umask(struct procstat * procstat,struct kinfo_proc * kipp)9290a15eb9SMikolaj Golub get_umask(struct procstat *procstat, struct kinfo_proc *kipp)
93c077ef00SMikolaj Golub {
94c077ef00SMikolaj Golub u_short fd_cmask;
95c077ef00SMikolaj Golub static char umask[4];
96c077ef00SMikolaj Golub
9790a15eb9SMikolaj Golub if (procstat_getumask(procstat, kipp, &fd_cmask) == 0) {
98c077ef00SMikolaj Golub snprintf(umask, 4, "%03o", fd_cmask);
99c077ef00SMikolaj Golub return (umask);
100c077ef00SMikolaj Golub } else {
101c077ef00SMikolaj Golub return ("-");
102c077ef00SMikolaj Golub }
103c077ef00SMikolaj Golub }
104