xref: /openbsd-src/lib/libkvm/kvm_proc2.c (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /*	$OpenBSD: kvm_proc2.c,v 1.29 2019/06/23 16:57:02 deraadt Exp $	*/
2 /*	$NetBSD: kvm_proc.c,v 1.30 1999/03/24 05:50:50 mrg Exp $	*/
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*-
32  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
33  * Copyright (c) 1989, 1992, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This code is derived from software developed by the Computer Systems
37  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
38  * BG 91-66 and contributed to Berkeley.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  */
64 
65 /*
66  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
67  * users of this code, so we've factored it out into a separate module.
68  * Thus, we keep this grunge out of the other kvm applications (i.e.,
69  * most other applications are interested only in open/close/read/nlist).
70  */
71 
72 #define __need_process
73 #include <sys/param.h>
74 #include <sys/proc.h>
75 #include <sys/exec.h>
76 #include <sys/stat.h>
77 #include <sys/ucred.h>
78 #include <sys/ioctl.h>
79 #include <sys/tty.h>
80 #include <sys/resource.h>
81 #include <sys/resourcevar.h>
82 #include <sys/signalvar.h>
83 #include <sys/pledge.h>
84 #include <stddef.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
88 #include <nlist.h>
89 #include <kvm.h>
90 
91 #include <uvm/uvm_extern.h>
92 #include <uvm/uvm_amap.h>
93 #include <machine/vmparam.h>
94 #include <machine/pmap.h>
95 
96 #include <sys/sysctl.h>
97 
98 #include <limits.h>
99 #include <errno.h>
100 #include <db.h>
101 #include <paths.h>
102 
103 #include "kvm_private.h"
104 
105 /*
106  * Read proc's from memory file into buffer bp, which has space to hold
107  * at most maxcnt procs.
108  */
109 static int
110 kvm_proclist(kvm_t *kd, int op, int arg, struct process *pr,
111     char *bp, int maxcnt, size_t esize)
112 {
113 	struct kinfo_proc kp;
114 	struct session sess;
115 	struct ucred ucred;
116 	struct proc proc, *p;
117 	struct process process, process2;
118 	struct pgrp pgrp;
119 	struct tty tty;
120 	struct sigacts sa, *sap;
121 	struct vmspace vm, *vmp;
122 	struct plimit limits, *limp;
123 	pid_t parent_pid, leader_pid;
124 	int cnt = 0;
125 	int dothreads = 0;
126 
127 	dothreads = op & KERN_PROC_SHOW_THREADS;
128 	op &= ~KERN_PROC_SHOW_THREADS;
129 
130 	/*
131 	 * Modelled on sysctl_doproc() in sys/kern/kern_sysctl.c
132 	 */
133 	for (; cnt < maxcnt && pr != NULL; pr = LIST_NEXT(&process, ps_list)) {
134 		if (KREAD(kd, (u_long)pr, &process)) {
135 			_kvm_err(kd, kd->program, "can't read process at %lx",
136 			    (u_long)pr);
137 			return (-1);
138 		}
139 		if (process.ps_pgrp == NULL)
140 			continue;
141 		if (process.ps_flags & PS_EMBRYO)
142 			continue;
143 		if (KREAD(kd, (u_long)process.ps_ucred, &ucred)) {
144 			_kvm_err(kd, kd->program, "can't read ucred at %lx",
145 			    (u_long)process.ps_ucred);
146 			return (-1);
147 		}
148 		if (KREAD(kd, (u_long)process.ps_pgrp, &pgrp)) {
149 			_kvm_err(kd, kd->program, "can't read pgrp at %lx",
150 			    (u_long)process.ps_pgrp);
151 			return (-1);
152 		}
153 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
154 			_kvm_err(kd, kd->program, "can't read session at %lx",
155 			    (u_long)pgrp.pg_session);
156 			return (-1);
157 		}
158 		if ((process.ps_flags & PS_CONTROLT) && sess.s_ttyp != NULL &&
159 		    KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
160 			_kvm_err(kd, kd->program, "can't read tty at %lx",
161 			    (u_long)sess.s_ttyp);
162 			return (-1);
163 		}
164 		if (process.ps_pptr) {
165 			if (KREAD(kd, (u_long)process.ps_pptr, &process2)) {
166 				_kvm_err(kd, kd->program,
167 				    "can't read process at %lx",
168 				    (u_long)process.ps_pptr);
169 				return (-1);
170 			}
171 			parent_pid = process2.ps_pid;
172 		}
173 		else
174 			parent_pid = 0;
175 		if (sess.s_leader) {
176 			if (KREAD(kd, (u_long)sess.s_leader, &process2)) {
177 				_kvm_err(kd, kd->program,
178 				    "can't read proc at %lx",
179 				    (u_long)sess.s_leader);
180 				return (-1);
181 			}
182 			leader_pid = process2.ps_pid;
183 		}
184 		else
185 			leader_pid = 0;
186 		if (process.ps_sigacts) {
187 			if (KREAD(kd, (u_long)process.ps_sigacts, &sa)) {
188 				_kvm_err(kd, kd->program,
189 				    "can't read sigacts at %lx",
190 				    (u_long)process.ps_sigacts);
191 				return (-1);
192 			}
193 			sap = &sa;
194 		}
195 		else
196 			sap = NULL;
197 
198 		switch (op) {
199 		case KERN_PROC_PID:
200 			if (process.ps_pid != (pid_t)arg)
201 				continue;
202 			break;
203 
204 		case KERN_PROC_PGRP:
205 			if (pgrp.pg_id != (pid_t)arg)
206 				continue;
207 			break;
208 
209 		case KERN_PROC_SESSION:
210 			if (sess.s_leader == NULL ||
211 			    leader_pid != (pid_t)arg)
212 				continue;
213 			break;
214 
215 		case KERN_PROC_TTY:
216 			if ((process.ps_flags & PS_CONTROLT) == 0 ||
217 			    sess.s_ttyp == NULL ||
218 			    tty.t_dev != (dev_t)arg)
219 				continue;
220 			break;
221 
222 		case KERN_PROC_UID:
223 			if (ucred.cr_uid != (uid_t)arg)
224 				continue;
225 			break;
226 
227 		case KERN_PROC_RUID:
228 			if (ucred.cr_ruid != (uid_t)arg)
229 				continue;
230 			break;
231 
232 		case KERN_PROC_ALL:
233 			if (process.ps_flags & PS_SYSTEM)
234 				continue;
235 			break;
236 
237 		case KERN_PROC_KTHREAD:
238 			/* no filtering */
239 			break;
240 
241 		default:
242 			_kvm_err(kd, kd->program, "invalid filter");
243 			return (-1);
244 		}
245 
246 		/*
247 		 * We're going to add another proc to the set.  If this
248 		 * will overflow the buffer, assume the reason is because
249 		 * nthreads (or the proc list) is corrupt and declare an error.
250 		 */
251 		if (cnt >= maxcnt) {
252 			_kvm_err(kd, kd->program, "nthreads corrupt");
253 			return (-1);
254 		}
255 
256 		/* set up stuff that might not always be there */
257 		limp = &limits;
258 		if (!process.ps_limit ||
259 		    KREAD(kd, (u_long)process.ps_limit, &limits))
260 			limp = NULL;
261 
262 		vmp = NULL;
263 
264 		if ((process.ps_flags & PS_ZOMBIE) == 0 &&
265 		    !KREAD(kd, (u_long)process.ps_vmspace, &vm))
266 			vmp = &vm;
267 
268 #define do_copy_str(_d, _s, _l)	kvm_read(kd, (u_long)(_s), (_d), (_l)-1)
269 		FILL_KPROC(&kp, do_copy_str, &proc, &process,
270 		    &ucred, &pgrp, process.ps_mainproc, pr, &sess,
271 		    vmp, limp, sap, 0, 1);
272 
273 		/* stuff that's too painful to generalize */
274 		kp.p_ppid = parent_pid;
275 		kp.p_sid = leader_pid;
276 		if ((process.ps_flags & PS_CONTROLT) && sess.s_ttyp != NULL) {
277 			kp.p_tdev = tty.t_dev;
278 			if (tty.t_pgrp != NULL &&
279 			    tty.t_pgrp != process.ps_pgrp &&
280 			    KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
281 				_kvm_err(kd, kd->program,
282 				    "can't read tpgrp at %lx",
283 				    (u_long)tty.t_pgrp);
284 				return (-1);
285 			}
286 			kp.p_tpgid = tty.t_pgrp ? pgrp.pg_id : -1;
287 			kp.p_tsess = PTRTOINT64(tty.t_session);
288 		} else {
289 			kp.p_tpgid = -1;
290 			kp.p_tdev = NODEV;
291 		}
292 
293 		/* update %cpu for all threads */
294 		if (dothreads) {
295 			if (KREAD(kd, (u_long)process.ps_mainproc, &proc)) {
296 				_kvm_err(kd, kd->program,
297 				    "can't read proc at %lx",
298 				    (u_long)process.ps_mainproc);
299 				return (-1);
300 			}
301 			kp.p_pctcpu = proc.p_pctcpu;
302 			kp.p_stat   = proc.p_stat;
303 		} else {
304 			kp.p_pctcpu = 0;
305 			kp.p_stat = (process.ps_flags & PS_ZOMBIE) ? SDEAD :
306 			    SIDL;
307 			for (p = TAILQ_FIRST(&process.ps_threads); p != NULL;
308 			    p = TAILQ_NEXT(&proc, p_thr_link)) {
309 				if (KREAD(kd, (u_long)p, &proc)) {
310 					_kvm_err(kd, kd->program,
311 					    "can't read proc at %lx",
312 					    (u_long)p);
313 					return (-1);
314 				}
315 				kp.p_pctcpu += proc.p_pctcpu;
316 				/*
317 				 * find best state:
318 				 * ONPROC > RUN > STOP > SLEEP > ...
319 				 */
320 				if (proc.p_stat == SONPROC ||
321 				    kp.p_stat == SONPROC)
322 					kp.p_stat = SONPROC;
323 				else if (proc.p_stat == SRUN ||
324 				    kp.p_stat == SRUN)
325 					kp.p_stat = SRUN;
326 				else if (proc.p_stat == SSTOP ||
327 				    kp.p_stat == SSTOP)
328 					kp.p_stat = SSTOP;
329 				else if (proc.p_stat == SSLEEP)
330 					kp.p_stat = SSLEEP;
331 			}
332                 }
333 
334 		memcpy(bp, &kp, esize);
335 		bp += esize;
336 		++cnt;
337 
338 		/* Skip per-thread entries if not required by op */
339 		if (!dothreads)
340 			continue;
341 
342 		for (p = TAILQ_FIRST(&process.ps_threads); p != NULL;
343 		    p = TAILQ_NEXT(&proc, p_thr_link)) {
344 			if (KREAD(kd, (u_long)p, &proc)) {
345 				_kvm_err(kd, kd->program,
346 				    "can't read proc at %lx",
347 				    (u_long)p);
348 				return (-1);
349 			}
350 			FILL_KPROC(&kp, do_copy_str, &proc, &process,
351 			    &ucred, &pgrp, p, pr, &sess, vmp, limp, sap,
352 			    1, 1);
353 
354 			/* see above */
355 			kp.p_ppid = parent_pid;
356 			kp.p_sid = leader_pid;
357 			if ((process.ps_flags & PS_CONTROLT) &&
358 			    sess.s_ttyp != NULL) {
359 				kp.p_tdev = tty.t_dev;
360 				if (tty.t_pgrp != NULL &&
361 				    tty.t_pgrp != process.ps_pgrp &&
362 				    KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
363 					_kvm_err(kd, kd->program,
364 					    "can't read tpgrp at %lx",
365 					    (u_long)tty.t_pgrp);
366 					return (-1);
367 				}
368 				kp.p_tpgid = tty.t_pgrp ? pgrp.pg_id : -1;
369 				kp.p_tsess = PTRTOINT64(tty.t_session);
370 			} else {
371 				kp.p_tpgid = -1;
372 				kp.p_tdev = NODEV;
373 			}
374 		}
375 
376 		memcpy(bp, &kp, esize);
377 		bp += esize;
378 		++cnt;
379 #undef do_copy_str
380 	}
381 	return (cnt);
382 }
383 
384 struct kinfo_proc *
385 kvm_getprocs(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
386 {
387 	int mib[6], st, nthreads;
388 	void *procbase;
389 	size_t size;
390 
391 	if ((ssize_t)esize < 0)
392 		return (NULL);
393 
394 	if (ISALIVE(kd)) {
395 		size = 0;
396 		mib[0] = CTL_KERN;
397 		mib[1] = KERN_PROC;
398 		mib[2] = op;
399 		mib[3] = arg;
400 		mib[4] = esize;
401 
402 		do {
403 			mib[5] = 0;
404 			st = sysctl(mib, 6, NULL, &size, NULL, 0);
405 			if (st == -1) {
406 				_kvm_syserr(kd, kd->program, "kvm_getprocs");
407 				return (NULL);
408 			}
409 
410 			size += size / 8; /* add ~10% */
411 
412 			procbase = _kvm_realloc(kd, kd->procbase, size);
413 			if (procbase == NULL)
414 				return (NULL);
415 
416 			kd->procbase = procbase;
417 
418 			mib[5] = size / esize;
419 			st = sysctl(mib, 6, kd->procbase, &size, NULL, 0);
420 			if (st == -1 && errno != ENOMEM) {
421 				_kvm_syserr(kd, kd->program, "kvm_getprocs");
422 				return (NULL);
423 			}
424 		} while (st == -1);
425 
426 		nthreads = size / esize;
427 	} else {
428 		struct nlist nl[5];
429 		int i, maxthread, maxprocess;
430 		struct process *pr;
431 		char *bp;
432 
433 		if (esize > sizeof(struct kinfo_proc)) {
434 			_kvm_syserr(kd, kd->program,
435 			    "kvm_getprocs: unknown fields requested: libkvm out of date?");
436 			return (NULL);
437 		}
438 
439 		memset(nl, 0, sizeof(nl));
440 		nl[0].n_name = "_nthreads";
441 		nl[1].n_name = "_nprocesses";
442 		nl[2].n_name = "_allprocess";
443 		nl[3].n_name = "_zombprocess";
444 		nl[4].n_name = NULL;
445 
446 		if (kvm_nlist(kd, nl) != 0) {
447 			for (i = 0; nl[i].n_type != 0; ++i)
448 				;
449 			_kvm_err(kd, kd->program,
450 			    "%s: no such symbol", nl[i].n_name);
451 			return (NULL);
452 		}
453 		if (KREAD(kd, nl[0].n_value, &maxthread)) {
454 			_kvm_err(kd, kd->program, "can't read nthreads");
455 			return (NULL);
456 		}
457 		if (KREAD(kd, nl[1].n_value, &maxprocess)) {
458 			_kvm_err(kd, kd->program, "can't read nprocesses");
459 			return (NULL);
460 		}
461 		maxthread += maxprocess;
462 
463 		kd->procbase = _kvm_reallocarray(kd, NULL, maxthread, esize);
464 		if (kd->procbase == 0)
465 			return (NULL);
466 		bp = (char *)kd->procbase;
467 
468 		/* allprocess */
469 		if (KREAD(kd, nl[2].n_value, &pr)) {
470 			_kvm_err(kd, kd->program, "cannot read allprocess");
471 			return (NULL);
472 		}
473 		nthreads = kvm_proclist(kd, op, arg, pr, bp, maxthread, esize);
474 		if (nthreads < 0)
475 			return (NULL);
476 
477 		/* zombprocess */
478 		if (KREAD(kd, nl[3].n_value, &pr)) {
479 			_kvm_err(kd, kd->program, "cannot read zombprocess");
480 			return (NULL);
481 		}
482 		i = kvm_proclist(kd, op, arg, pr, bp + (esize * nthreads),
483 		    maxthread - nthreads, esize);
484 		if (i > 0)
485 			nthreads += i;
486 	}
487 	if (kd->procbase != NULL)
488 		*cnt = nthreads;
489 	return (kd->procbase);
490 }
491