17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
59acbbeafSnn * Common Development and Distribution License (the "License").
69acbbeafSnn * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
229acbbeafSnn * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
2543051d27SRobert Mustacchi /*
2643051d27SRobert Mustacchi * Copyright 2015, Joyent, Inc.
27*a7d7cafeSRobert Mustacchi * Copyright 2023 Oxide Computer Company
2843051d27SRobert Mustacchi */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
359acbbeafSnn #include <limits.h>
36d2a70789SRichard Lowe #include <sys/secflags.h>
379acbbeafSnn
389acbbeafSnn #include "Pcontrol.h"
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate * These several routines simply get the indicated /proc structures
427c478bd9Sstevel@tonic-gate * for a process identified by process ID. They are convenience
437c478bd9Sstevel@tonic-gate * functions for one-time operations. They do the mechanics of
447c478bd9Sstevel@tonic-gate * open() / read() / close() of the necessary /proc files so the
457c478bd9Sstevel@tonic-gate * caller's code can look relatively less cluttered.
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate * 'ngroups' is the number of supplementary group entries allocated in
507c478bd9Sstevel@tonic-gate * the caller's cred structure. It should equal zero or one unless extra
517c478bd9Sstevel@tonic-gate * space has been allocated for the group list by the caller, like this:
527c478bd9Sstevel@tonic-gate * credp = malloc(sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate int
proc_get_cred(pid_t pid,prcred_t * credp,int ngroups)557c478bd9Sstevel@tonic-gate proc_get_cred(pid_t pid, prcred_t *credp, int ngroups)
567c478bd9Sstevel@tonic-gate {
579acbbeafSnn char fname[PATH_MAX];
587c478bd9Sstevel@tonic-gate int fd;
597c478bd9Sstevel@tonic-gate int rv = -1;
607c478bd9Sstevel@tonic-gate ssize_t minsize = sizeof (*credp) - sizeof (gid_t);
617c478bd9Sstevel@tonic-gate size_t size = minsize + ngroups * sizeof (gid_t);
627c478bd9Sstevel@tonic-gate
639acbbeafSnn (void) snprintf(fname, sizeof (fname), "%s/%d/cred",
649acbbeafSnn procfs_path, (int)pid);
657c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
667c478bd9Sstevel@tonic-gate if (read(fd, credp, size) >= minsize)
677c478bd9Sstevel@tonic-gate rv = 0;
687c478bd9Sstevel@tonic-gate (void) close(fd);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate return (rv);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate
73d2a70789SRichard Lowe int
proc_get_secflags(pid_t pid,prsecflags_t ** psf)74d2a70789SRichard Lowe proc_get_secflags(pid_t pid, prsecflags_t **psf)
75d2a70789SRichard Lowe {
76d2a70789SRichard Lowe char fname[PATH_MAX];
77d2a70789SRichard Lowe int fd;
78d2a70789SRichard Lowe int rv = -1;
79d2a70789SRichard Lowe
80d2a70789SRichard Lowe if ((*psf = calloc(1, sizeof (prsecflags_t))) == NULL)
81d2a70789SRichard Lowe return (-1);
82d2a70789SRichard Lowe
83d2a70789SRichard Lowe (void) snprintf(fname, sizeof (fname), "%s/%d/secflags",
84d2a70789SRichard Lowe procfs_path, (int)pid);
85d2a70789SRichard Lowe if ((fd = open(fname, O_RDONLY)) >= 0) {
86d2a70789SRichard Lowe if (read(fd, *psf, sizeof (prsecflags_t)) ==
87d2a70789SRichard Lowe sizeof (prsecflags_t))
88d2a70789SRichard Lowe rv = 0;
89d2a70789SRichard Lowe (void) close(fd);
90d2a70789SRichard Lowe }
91d2a70789SRichard Lowe return (rv);
92d2a70789SRichard Lowe }
93d2a70789SRichard Lowe
9443051d27SRobert Mustacchi void
proc_free_priv(prpriv_t * prv)9543051d27SRobert Mustacchi proc_free_priv(prpriv_t *prv)
9643051d27SRobert Mustacchi {
9743051d27SRobert Mustacchi free(prv);
9843051d27SRobert Mustacchi }
9943051d27SRobert Mustacchi
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * Malloc and return a properly sized structure.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate prpriv_t *
proc_get_priv(pid_t pid)1047c478bd9Sstevel@tonic-gate proc_get_priv(pid_t pid)
1057c478bd9Sstevel@tonic-gate {
1069acbbeafSnn char fname[PATH_MAX];
1077c478bd9Sstevel@tonic-gate int fd;
1087c478bd9Sstevel@tonic-gate struct stat statb;
1097c478bd9Sstevel@tonic-gate prpriv_t *rv = NULL;
1107c478bd9Sstevel@tonic-gate
1119acbbeafSnn (void) snprintf(fname, sizeof (fname), "%s/%d/priv",
1129acbbeafSnn procfs_path, (int)pid);
1137c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
1147c478bd9Sstevel@tonic-gate if (fstat(fd, &statb) != 0 ||
1157c478bd9Sstevel@tonic-gate (rv = malloc(statb.st_size)) == NULL ||
1167c478bd9Sstevel@tonic-gate read(fd, rv, statb.st_size) != statb.st_size) {
1177c478bd9Sstevel@tonic-gate free(rv);
1187c478bd9Sstevel@tonic-gate rv = NULL;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate (void) close(fd);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate return (rv);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate * Fill in a pointer to a process LDT structure.
1287c478bd9Sstevel@tonic-gate * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
1297c478bd9Sstevel@tonic-gate * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
1307c478bd9Sstevel@tonic-gate * Otherwise we return the actual number of LDT entries fetched (<= nldt).
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate int
proc_get_ldt(pid_t pid,struct ssd * pldt,int nldt)1337c478bd9Sstevel@tonic-gate proc_get_ldt(pid_t pid, struct ssd *pldt, int nldt)
1347c478bd9Sstevel@tonic-gate {
1359acbbeafSnn char fname[PATH_MAX];
1367c478bd9Sstevel@tonic-gate int fd;
1377c478bd9Sstevel@tonic-gate struct stat statb;
1387c478bd9Sstevel@tonic-gate size_t size;
1397c478bd9Sstevel@tonic-gate ssize_t ssize;
1407c478bd9Sstevel@tonic-gate
1419acbbeafSnn (void) snprintf(fname, sizeof (fname), "%s/%d/ldt",
1429acbbeafSnn procfs_path, (int)pid);
1437c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) < 0)
1447c478bd9Sstevel@tonic-gate return (-1);
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if (pldt == NULL || nldt == 0) {
1477c478bd9Sstevel@tonic-gate nldt = 0;
1487c478bd9Sstevel@tonic-gate if (fstat(fd, &statb) == 0)
1497c478bd9Sstevel@tonic-gate nldt = statb.st_size / sizeof (struct ssd);
1507c478bd9Sstevel@tonic-gate (void) close(fd);
1517c478bd9Sstevel@tonic-gate return (nldt);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate size = nldt * sizeof (struct ssd);
1557c478bd9Sstevel@tonic-gate if ((ssize = read(fd, pldt, size)) < 0)
1567c478bd9Sstevel@tonic-gate nldt = -1;
1577c478bd9Sstevel@tonic-gate else
1587c478bd9Sstevel@tonic-gate nldt = ssize / sizeof (struct ssd);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate (void) close(fd);
1617c478bd9Sstevel@tonic-gate return (nldt);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate int
proc_get_psinfo(pid_t pid,psinfo_t * psp)1667c478bd9Sstevel@tonic-gate proc_get_psinfo(pid_t pid, psinfo_t *psp)
1677c478bd9Sstevel@tonic-gate {
1689acbbeafSnn char fname[PATH_MAX];
1697c478bd9Sstevel@tonic-gate int fd;
1707c478bd9Sstevel@tonic-gate int rv = -1;
1717c478bd9Sstevel@tonic-gate
1729acbbeafSnn (void) snprintf(fname, sizeof (fname), "%s/%d/psinfo",
1739acbbeafSnn procfs_path, (int)pid);
1747c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
1757c478bd9Sstevel@tonic-gate if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
1767c478bd9Sstevel@tonic-gate rv = 0;
1777c478bd9Sstevel@tonic-gate (void) close(fd);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate return (rv);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate int
proc_get_status(pid_t pid,pstatus_t * psp)1837c478bd9Sstevel@tonic-gate proc_get_status(pid_t pid, pstatus_t *psp)
1847c478bd9Sstevel@tonic-gate {
1859acbbeafSnn char fname[PATH_MAX];
1867c478bd9Sstevel@tonic-gate int fd;
1877c478bd9Sstevel@tonic-gate int rv = -1;
1887c478bd9Sstevel@tonic-gate
1899acbbeafSnn (void) snprintf(fname, sizeof (fname), "%s/%d/status",
1909acbbeafSnn procfs_path, (int)pid);
1917c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
1927c478bd9Sstevel@tonic-gate if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
1937c478bd9Sstevel@tonic-gate rv = 0;
1947c478bd9Sstevel@tonic-gate (void) close(fd);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate return (rv);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate * Get the process's aux vector.
2017c478bd9Sstevel@tonic-gate * 'naux' is the number of aux entries in the caller's buffer.
2027c478bd9Sstevel@tonic-gate * We return the number of aux entries actually fetched from
2037c478bd9Sstevel@tonic-gate * the process (less than or equal to 'naux') or -1 on failure.
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate int
proc_get_auxv(pid_t pid,auxv_t * pauxv,int naux)2067c478bd9Sstevel@tonic-gate proc_get_auxv(pid_t pid, auxv_t *pauxv, int naux)
2077c478bd9Sstevel@tonic-gate {
2089acbbeafSnn char fname[PATH_MAX];
2097c478bd9Sstevel@tonic-gate int fd;
2107c478bd9Sstevel@tonic-gate int rv = -1;
2117c478bd9Sstevel@tonic-gate
2129acbbeafSnn (void) snprintf(fname, sizeof (fname), "%s/%d/auxv",
2139acbbeafSnn procfs_path, (int)pid);
2147c478bd9Sstevel@tonic-gate if ((fd = open(fname, O_RDONLY)) >= 0) {
2157c478bd9Sstevel@tonic-gate if ((rv = read(fd, pauxv, naux * sizeof (auxv_t))) >= 0)
2167c478bd9Sstevel@tonic-gate rv /= sizeof (auxv_t);
2177c478bd9Sstevel@tonic-gate (void) close(fd);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate return (rv);
2207c478bd9Sstevel@tonic-gate }
221dea9f5e6SRobert Mustacchi
222dea9f5e6SRobert Mustacchi int
proc_get_lwpsinfo(pid_t pid,uint_t thr,lwpsinfo_t * lwpip)223dea9f5e6SRobert Mustacchi proc_get_lwpsinfo(pid_t pid, uint_t thr, lwpsinfo_t *lwpip)
224dea9f5e6SRobert Mustacchi {
225dea9f5e6SRobert Mustacchi char fname[PATH_MAX];
226dea9f5e6SRobert Mustacchi int fd;
227dea9f5e6SRobert Mustacchi int rv = -1;
228dea9f5e6SRobert Mustacchi
229dea9f5e6SRobert Mustacchi (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%u/lwpsinfo",
230dea9f5e6SRobert Mustacchi procfs_path, (int)pid, thr);
231dea9f5e6SRobert Mustacchi if ((fd = open(fname, O_RDONLY)) >= 0) {
232dea9f5e6SRobert Mustacchi if (read(fd, lwpip, sizeof (*lwpip)) == sizeof (*lwpip))
233dea9f5e6SRobert Mustacchi rv = 0;
234dea9f5e6SRobert Mustacchi (void) close(fd);
235dea9f5e6SRobert Mustacchi }
236dea9f5e6SRobert Mustacchi return (rv);
237*a7d7cafeSRobert Mustacchi }
238*a7d7cafeSRobert Mustacchi
239*a7d7cafeSRobert Mustacchi int
proc_get_lwpstatus(pid_t pid,uint_t thr,lwpstatus_t * lwp)240*a7d7cafeSRobert Mustacchi proc_get_lwpstatus(pid_t pid, uint_t thr, lwpstatus_t *lwp)
241*a7d7cafeSRobert Mustacchi {
242*a7d7cafeSRobert Mustacchi char fname[PATH_MAX];
243*a7d7cafeSRobert Mustacchi int fd;
244*a7d7cafeSRobert Mustacchi int rv = -1;
245dea9f5e6SRobert Mustacchi
246*a7d7cafeSRobert Mustacchi (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%u/lwpstatus",
247*a7d7cafeSRobert Mustacchi procfs_path, (int)pid, thr);
248*a7d7cafeSRobert Mustacchi if ((fd = open(fname, O_RDONLY)) >= 0) {
249*a7d7cafeSRobert Mustacchi if (read(fd, lwp, sizeof (*lwp)) == sizeof (*lwp))
250*a7d7cafeSRobert Mustacchi rv = 0;
251*a7d7cafeSRobert Mustacchi (void) close(fd);
252*a7d7cafeSRobert Mustacchi }
253*a7d7cafeSRobert Mustacchi return (rv);
254dea9f5e6SRobert Mustacchi }
255