xref: /illumos-gate/usr/src/lib/libproc/common/proc_get_info.c (revision f1cd2b6cd01a74ce00b051c8a3028a8d47c8e5ac)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2015, Joyent, Inc.
27  * Copyright 2024 Oxide Computer Company
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <sys/secflags.h>
37 #include <errno.h>
38 
39 #include "Pcontrol.h"
40 
41 /*
42  * These several routines simply get the indicated /proc structures
43  * for a process identified by process ID.  They are convenience
44  * functions for one-time operations.  They do the mechanics of
45  * open() / read() / close() of the necessary /proc files so the
46  * caller's code can look relatively less cluttered.
47  */
48 
49 /*
50  * 'ngroups' is the number of supplementary group entries allocated in
51  * the caller's cred structure.  It should equal zero or one unless extra
52  * space has been allocated for the group list by the caller, like this:
53  *    credp = malloc(sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
54  */
55 int
proc_get_cred(pid_t pid,prcred_t * credp,int ngroups)56 proc_get_cred(pid_t pid, prcred_t *credp, int ngroups)
57 {
58 	char fname[PATH_MAX];
59 	int fd;
60 	int rv = -1;
61 	ssize_t minsize = sizeof (*credp) - sizeof (gid_t);
62 	size_t size = minsize + ngroups * sizeof (gid_t);
63 
64 	(void) snprintf(fname, sizeof (fname), "%s/%d/cred",
65 	    procfs_path, (int)pid);
66 	if ((fd = open(fname, O_RDONLY)) >= 0) {
67 		if (read(fd, credp, size) >= minsize)
68 			rv = 0;
69 		(void) close(fd);
70 	}
71 	return (rv);
72 }
73 
74 int
proc_get_secflags(pid_t pid,prsecflags_t ** psf)75 proc_get_secflags(pid_t pid, prsecflags_t **psf)
76 {
77 	char fname[PATH_MAX];
78 	int fd;
79 	int rv = -1;
80 
81 	if ((*psf = calloc(1, sizeof (prsecflags_t))) == NULL)
82 		return (-1);
83 
84 	(void) snprintf(fname, sizeof (fname), "%s/%d/secflags",
85 	    procfs_path, (int)pid);
86 	if ((fd = open(fname, O_RDONLY)) >= 0) {
87 		if (read(fd, *psf, sizeof (prsecflags_t)) ==
88 		    sizeof (prsecflags_t))
89 			rv = 0;
90 		(void) close(fd);
91 	}
92 	return (rv);
93 }
94 
95 void
proc_free_priv(prpriv_t * prv)96 proc_free_priv(prpriv_t *prv)
97 {
98 	free(prv);
99 }
100 
101 /*
102  * Malloc and return a properly sized structure.
103  */
104 prpriv_t *
proc_get_priv(pid_t pid)105 proc_get_priv(pid_t pid)
106 {
107 	char fname[PATH_MAX];
108 	int fd;
109 	struct stat statb;
110 	prpriv_t *rv = NULL;
111 
112 	(void) snprintf(fname, sizeof (fname), "%s/%d/priv",
113 	    procfs_path, (int)pid);
114 	if ((fd = open(fname, O_RDONLY)) >= 0) {
115 		if (fstat(fd, &statb) != 0 ||
116 		    (rv = malloc(statb.st_size)) == NULL ||
117 		    read(fd, rv, statb.st_size) != statb.st_size) {
118 			free(rv);
119 			rv = NULL;
120 		}
121 		(void) close(fd);
122 	}
123 	return (rv);
124 }
125 
126 #if defined(__i386) || defined(__amd64)
127 /*
128  * Fill in a pointer to a process LDT structure.
129  * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
130  * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
131  * Otherwise we return the actual number of LDT entries fetched (<= nldt).
132  */
133 int
proc_get_ldt(pid_t pid,struct ssd * pldt,int nldt)134 proc_get_ldt(pid_t pid, struct ssd *pldt, int nldt)
135 {
136 	char fname[PATH_MAX];
137 	int fd;
138 	struct stat statb;
139 	size_t size;
140 	ssize_t ssize;
141 
142 	(void) snprintf(fname, sizeof (fname), "%s/%d/ldt",
143 	    procfs_path, (int)pid);
144 	if ((fd = open(fname, O_RDONLY)) < 0)
145 		return (-1);
146 
147 	if (pldt == NULL || nldt == 0) {
148 		nldt = 0;
149 		if (fstat(fd, &statb) == 0)
150 			nldt = statb.st_size / sizeof (struct ssd);
151 		(void) close(fd);
152 		return (nldt);
153 	}
154 
155 	size = nldt * sizeof (struct ssd);
156 	if ((ssize = read(fd, pldt, size)) < 0)
157 		nldt = -1;
158 	else
159 		nldt = ssize / sizeof (struct ssd);
160 
161 	(void) close(fd);
162 	return (nldt);
163 }
164 #endif	/* __i386 || __amd64 */
165 
166 int
proc_get_psinfo(pid_t pid,psinfo_t * psp)167 proc_get_psinfo(pid_t pid, psinfo_t *psp)
168 {
169 	char fname[PATH_MAX];
170 	int fd;
171 	int rv = -1;
172 
173 	(void) snprintf(fname, sizeof (fname), "%s/%d/psinfo",
174 	    procfs_path, (int)pid);
175 	if ((fd = open(fname, O_RDONLY)) >= 0) {
176 		if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
177 			rv = 0;
178 		(void) close(fd);
179 	}
180 	return (rv);
181 }
182 
183 int
proc_get_status(pid_t pid,pstatus_t * psp)184 proc_get_status(pid_t pid, pstatus_t *psp)
185 {
186 	char fname[PATH_MAX];
187 	int fd;
188 	int rv = -1;
189 
190 	(void) snprintf(fname, sizeof (fname), "%s/%d/status",
191 	    procfs_path, (int)pid);
192 	if ((fd = open(fname, O_RDONLY)) >= 0) {
193 		if (read(fd, psp, sizeof (*psp)) == sizeof (*psp))
194 			rv = 0;
195 		(void) close(fd);
196 	}
197 	return (rv);
198 }
199 
200 /*
201  * Get the process's aux vector.
202  * 'naux' is the number of aux entries in the caller's buffer.
203  * We return the number of aux entries actually fetched from
204  * the process (less than or equal to 'naux') or -1 on failure.
205  */
206 int
proc_get_auxv(pid_t pid,auxv_t * pauxv,int naux)207 proc_get_auxv(pid_t pid, auxv_t *pauxv, int naux)
208 {
209 	char fname[PATH_MAX];
210 	int fd;
211 	int rv = -1;
212 
213 	(void) snprintf(fname, sizeof (fname), "%s/%d/auxv",
214 	    procfs_path, (int)pid);
215 	if ((fd = open(fname, O_RDONLY)) >= 0) {
216 		if ((rv = read(fd, pauxv, naux * sizeof (auxv_t))) >= 0)
217 			rv /= sizeof (auxv_t);
218 		(void) close(fd);
219 	}
220 	return (rv);
221 }
222 
223 int
proc_get_lwpsinfo(pid_t pid,uint_t thr,lwpsinfo_t * lwpip)224 proc_get_lwpsinfo(pid_t pid, uint_t thr, lwpsinfo_t *lwpip)
225 {
226 	char fname[PATH_MAX];
227 	int fd;
228 	int rv = -1;
229 
230 	(void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%u/lwpsinfo",
231 	    procfs_path, (int)pid, thr);
232 	if ((fd = open(fname, O_RDONLY)) >= 0) {
233 		if (read(fd, lwpip, sizeof (*lwpip)) == sizeof (*lwpip))
234 			rv = 0;
235 		(void) close(fd);
236 	}
237 	return (rv);
238 }
239 
240 int
proc_get_lwpstatus(pid_t pid,uint_t thr,lwpstatus_t * lwp)241 proc_get_lwpstatus(pid_t pid, uint_t thr, lwpstatus_t *lwp)
242 {
243 	char fname[PATH_MAX];
244 	int fd;
245 	int rv = -1;
246 
247 	(void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%u/lwpstatus",
248 	    procfs_path, (int)pid, thr);
249 	if ((fd = open(fname, O_RDONLY)) >= 0) {
250 		if (read(fd, lwp, sizeof (*lwp)) == sizeof (*lwp))
251 			rv = 0;
252 		(void) close(fd);
253 	}
254 	return (rv);
255 }
256 
257 ssize_t
proc_get_cwd(pid_t pid,char * cwd,size_t len)258 proc_get_cwd(pid_t pid, char *cwd, size_t len)
259 {
260 	ssize_t ret;
261 	char fname[PATH_MAX];
262 
263 	if (len == 0) {
264 		errno = EINVAL;
265 		return (-1);
266 	}
267 
268 	(void) snprintf(fname, sizeof (fname), "%s/%d/path/cwd",
269 	    procfs_path, (int)pid);
270 
271 	if ((ret = readlink(fname, cwd, len - 1)) > 0)
272 		cwd[ret] = '\0';
273 
274 	return (ret);
275 }
276