xref: /openbsd-src/lib/libkvm/kvm_proc2.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: kvm_proc2.c,v 1.26 2015/09/08 15:40:32 dlg 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 <stddef.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 #include <nlist.h>
88 #include <kvm.h>
89 
90 #include <uvm/uvm_extern.h>
91 #include <uvm/uvm_amap.h>
92 #include <machine/vmparam.h>
93 #include <machine/pmap.h>
94 
95 #include <sys/sysctl.h>
96 
97 #include <limits.h>
98 #include <errno.h>
99 #include <db.h>
100 #include <paths.h>
101 
102 #include "kvm_private.h"
103 
104 /*
105  * Read proc's from memory file into buffer bp, which has space to hold
106  * at most maxcnt procs.
107  */
108 static int
109 kvm_proclist(kvm_t *kd, int op, int arg, struct process *pr,
110     char *bp, int maxcnt, size_t esize)
111 {
112 	struct kinfo_proc kp;
113 	struct session sess;
114 	struct ucred ucred;
115 	struct proc proc, proc2, *p;
116 	struct process process, process2;
117 	struct pgrp pgrp;
118 	struct tty tty;
119 	struct sigacts sa, *sap;
120 	struct vmspace vm, *vmp;
121 	struct plimit limits, *limp;
122 	pid_t process_pid, parent_pid, leader_pid;
123 	int cnt = 0;
124 	int dothreads = 0;
125 
126 	dothreads = op & KERN_PROC_SHOW_THREADS;
127 	op &= ~KERN_PROC_SHOW_THREADS;
128 
129 	/*
130 	 * Modelled on sysctl_doproc() in sys/kern/kern_sysctl.c
131 	 */
132 	for (; cnt < maxcnt && pr != NULL; pr = LIST_NEXT(&process, ps_list)) {
133 		if (KREAD(kd, (u_long)pr, &process)) {
134 			_kvm_err(kd, kd->program, "can't read process at %lx",
135 			    (u_long)pr);
136 			return (-1);
137 		}
138 		if (process.ps_pgrp == NULL)
139 			continue;
140 		if (process.ps_flags & PS_EMBRYO)
141 			continue;
142 		if (KREAD(kd, (u_long)process.ps_mainproc, &proc)) {
143 			_kvm_err(kd, kd->program, "can't read proc at %lx",
144 			    (u_long)process.ps_mainproc);
145 			return (-1);
146 		}
147 		process_pid = proc.p_pid;
148 		if (KREAD(kd, (u_long)process.ps_ucred, &ucred)) {
149 			_kvm_err(kd, kd->program, "can't read ucred at %lx",
150 			    (u_long)process.ps_ucred);
151 			return (-1);
152 		}
153 		if (KREAD(kd, (u_long)process.ps_pgrp, &pgrp)) {
154 			_kvm_err(kd, kd->program, "can't read pgrp at %lx",
155 			    (u_long)process.ps_pgrp);
156 			return (-1);
157 		}
158 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
159 			_kvm_err(kd, kd->program, "can't read session at %lx",
160 			    (u_long)pgrp.pg_session);
161 			return (-1);
162 		}
163 		if ((process.ps_flags & PS_CONTROLT) && sess.s_ttyp != NULL &&
164 		    KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
165 			_kvm_err(kd, kd->program, "can't read tty at %lx",
166 			    (u_long)sess.s_ttyp);
167 			return (-1);
168 		}
169 		if (process.ps_pptr) {
170 			if (KREAD(kd, (u_long)process.ps_pptr, &process2)) {
171 				_kvm_err(kd, kd->program,
172 				    "can't read process at %lx",
173 				    (u_long)process.ps_pptr);
174 				return (-1);
175 			}
176 			if (KREAD(kd, (u_long)process2.ps_mainproc, &proc2)) {
177 				_kvm_err(kd, kd->program,
178 				    "can't read proc at %lx",
179 				    (u_long)process2.ps_mainproc);
180 				return (-1);
181 			}
182 			parent_pid = proc2.p_pid;
183 		}
184 		else
185 			parent_pid = 0;
186 		if (sess.s_leader) {
187 			if (KREAD(kd, (u_long)sess.s_leader, &process2)) {
188 				_kvm_err(kd, kd->program,
189 				    "can't read proc at %lx",
190 				    (u_long)sess.s_leader);
191 				return (-1);
192 			}
193 			if (KREAD(kd, (u_long)process2.ps_mainproc, &proc2)) {
194 				_kvm_err(kd, kd->program,
195 				    "can't read proc at %lx",
196 				    (u_long)process2.ps_mainproc);
197 				return (-1);
198 			}
199 			leader_pid = proc2.p_pid;
200 		}
201 		else
202 			leader_pid = 0;
203 		if (process.ps_sigacts) {
204 			if (KREAD(kd, (u_long)process.ps_sigacts, &sa)) {
205 				_kvm_err(kd, kd->program,
206 				    "can't read sigacts at %lx",
207 				    (u_long)process.ps_sigacts);
208 				return (-1);
209 			}
210 			sap = &sa;
211 		}
212 		else
213 			sap = NULL;
214 
215 		switch (op) {
216 		case KERN_PROC_PID:
217 			if (parent_pid != (pid_t)arg)
218 				continue;
219 			break;
220 
221 		case KERN_PROC_PGRP:
222 			if (pgrp.pg_id != (pid_t)arg)
223 				continue;
224 			break;
225 
226 		case KERN_PROC_SESSION:
227 			if (sess.s_leader == NULL ||
228 			    leader_pid != (pid_t)arg)
229 				continue;
230 			break;
231 
232 		case KERN_PROC_TTY:
233 			if ((process.ps_flags & PS_CONTROLT) == 0 ||
234 			    sess.s_ttyp == NULL ||
235 			    tty.t_dev != (dev_t)arg)
236 				continue;
237 			break;
238 
239 		case KERN_PROC_UID:
240 			if (ucred.cr_uid != (uid_t)arg)
241 				continue;
242 			break;
243 
244 		case KERN_PROC_RUID:
245 			if (ucred.cr_ruid != (uid_t)arg)
246 				continue;
247 			break;
248 
249 		case KERN_PROC_ALL:
250 			if (proc.p_flag & P_SYSTEM)
251 				continue;
252 			break;
253 
254 		case KERN_PROC_KTHREAD:
255 			/* no filtering */
256 			break;
257 
258 		default:
259 			_kvm_err(kd, kd->program, "invalid filter");
260 			return (-1);
261 		}
262 
263 		/*
264 		 * We're going to add another proc to the set.  If this
265 		 * will overflow the buffer, assume the reason is because
266 		 * nthreads (or the proc list) is corrupt and declare an error.
267 		 */
268 		if (cnt >= maxcnt) {
269 			_kvm_err(kd, kd->program, "nthreads corrupt");
270 			return (-1);
271 		}
272 
273 		/* set up stuff that might not always be there */
274 		limp = &limits;
275 		if (!process.ps_limit ||
276 		    KREAD(kd, (u_long)process.ps_limit, &limits))
277 			limp = NULL;
278 
279 		vmp = NULL;
280 
281 		if ((process.ps_flags & PS_ZOMBIE) == 0 &&
282 		    !KREAD(kd, (u_long)process.ps_vmspace, &vm))
283 			vmp = &vm;
284 
285 #define do_copy_str(_d, _s, _l)	kvm_read(kd, (u_long)(_s), (_d), (_l)-1)
286 		FILL_KPROC(&kp, do_copy_str, &proc, &process,
287 		    &ucred, &pgrp, process.ps_mainproc, proc.p_p, &sess,
288 		    vmp, limp, sap, 0, 1);
289 
290 		/* stuff that's too painful to generalize */
291 		kp.p_pid = process_pid;
292 		kp.p_ppid = parent_pid;
293 		kp.p_sid = leader_pid;
294 		if ((process.ps_flags & PS_CONTROLT) && sess.s_ttyp != NULL) {
295 			kp.p_tdev = tty.t_dev;
296 			if (tty.t_pgrp != NULL &&
297 			    tty.t_pgrp != process.ps_pgrp &&
298 			    KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
299 				_kvm_err(kd, kd->program,
300 				    "can't read tpgrp at %lx",
301 				    (u_long)tty.t_pgrp);
302 				return (-1);
303 			}
304 			kp.p_tpgid = tty.t_pgrp ? pgrp.pg_id : -1;
305 			kp.p_tsess = PTRTOINT64(tty.t_session);
306 		} else {
307 			kp.p_tpgid = -1;
308 			kp.p_tdev = NODEV;
309 		}
310 
311 		/* update %cpu for all threads */
312 		if (dothreads) {
313 			kp.p_pctcpu = proc.p_pctcpu;
314 			kp.p_stat   = proc.p_stat;
315 		} else {
316 			kp.p_pctcpu = 0;
317 			kp.p_stat = (process.ps_flags & PS_ZOMBIE) ? SDEAD :
318 			    SIDL;
319 			for (p = TAILQ_FIRST(&process.ps_threads); p != NULL;
320 			    p = TAILQ_NEXT(&proc, p_thr_link)) {
321 				if (KREAD(kd, (u_long)p, &proc)) {
322 					_kvm_err(kd, kd->program,
323 					    "can't read proc at %lx",
324 					    (u_long)p);
325 					return (-1);
326 				}
327 				kp.p_pctcpu += proc.p_pctcpu;
328 				/*
329 				 * find best state:
330 				 * ONPROC > RUN > STOP > SLEEP > ...
331 				 */
332 				if (proc.p_stat == SONPROC ||
333 				    kp.p_stat == SONPROC)
334 					kp.p_stat = SONPROC;
335 				else if (proc.p_stat == SRUN ||
336 				    kp.p_stat == SRUN)
337 					kp.p_stat = SRUN;
338 				else if (proc.p_stat == SSTOP ||
339 				    kp.p_stat == SSTOP)
340 					kp.p_stat = SSTOP;
341 				else if (proc.p_stat == SSLEEP)
342 					kp.p_stat = SSLEEP;
343 			}
344                 }
345 
346 		memcpy(bp, &kp, esize);
347 		bp += esize;
348 		++cnt;
349 
350 		/* Skip per-thread entries if not required by op */
351 		if (!dothreads)
352 			continue;
353 
354 		for (p = TAILQ_FIRST(&process.ps_threads); p != NULL;
355 		    p = TAILQ_NEXT(&proc, p_thr_link)) {
356 			if (KREAD(kd, (u_long)p, &proc)) {
357 				_kvm_err(kd, kd->program,
358 				    "can't read proc at %lx",
359 				    (u_long)p);
360 				return (-1);
361 			}
362 			FILL_KPROC(&kp, do_copy_str, &proc, &process,
363 			    &ucred, &pgrp, p, proc.p_p, &sess, vmp, limp, sap,
364 			    1, 1);
365 
366 			/* see above */
367 			kp.p_pid = process_pid;
368 			kp.p_ppid = parent_pid;
369 			kp.p_sid = leader_pid;
370 			if ((process.ps_flags & PS_CONTROLT) &&
371 			    sess.s_ttyp != NULL) {
372 				kp.p_tdev = tty.t_dev;
373 				if (tty.t_pgrp != NULL &&
374 				    tty.t_pgrp != process.ps_pgrp &&
375 				    KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
376 					_kvm_err(kd, kd->program,
377 					    "can't read tpgrp at %lx",
378 					    (u_long)tty.t_pgrp);
379 					return (-1);
380 				}
381 				kp.p_tpgid = tty.t_pgrp ? pgrp.pg_id : -1;
382 				kp.p_tsess = PTRTOINT64(tty.t_session);
383 			} else {
384 				kp.p_tpgid = -1;
385 				kp.p_tdev = NODEV;
386 			}
387 		}
388 
389 		memcpy(bp, &kp, esize);
390 		bp += esize;
391 		++cnt;
392 #undef do_copy_str
393 	}
394 	return (cnt);
395 }
396 
397 struct kinfo_proc *
398 kvm_getprocs(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
399 {
400 	int mib[6], st, nthreads;
401 	void *procbase;
402 	size_t size;
403 
404 	if ((ssize_t)esize < 0)
405 		return (NULL);
406 
407 	if (ISALIVE(kd)) {
408 		size = 0;
409 		mib[0] = CTL_KERN;
410 		mib[1] = KERN_PROC;
411 		mib[2] = op;
412 		mib[3] = arg;
413 		mib[4] = esize;
414 
415 		do {
416 			mib[5] = 0;
417 			st = sysctl(mib, 6, NULL, &size, NULL, 0);
418 			if (st == -1) {
419 				_kvm_syserr(kd, kd->program, "kvm_getprocs");
420 				return (NULL);
421 			}
422 
423 			size += size / 8; /* add ~10% */
424 
425 			procbase = _kvm_realloc(kd, kd->procbase, size);
426 			if (procbase == NULL)
427 				return (NULL);
428 
429 			kd->procbase = procbase;
430 
431 			mib[5] = size / esize;
432 			st = sysctl(mib, 6, kd->procbase, &size, NULL, 0);
433 			if (st == -1 && errno != ENOMEM) {
434 				_kvm_syserr(kd, kd->program, "kvm_getprocs");
435 				return (NULL);
436 			}
437 		} while (st == -1);
438 
439 		nthreads = size / esize;
440 	} else {
441 		struct nlist nl[5];
442 		int i, maxthread, maxprocess;
443 		struct process *pr;
444 		char *bp;
445 
446 		if (esize > sizeof(struct kinfo_proc)) {
447 			_kvm_syserr(kd, kd->program,
448 			    "kvm_getprocs: unknown fields requested: libkvm out of date?");
449 			return (NULL);
450 		}
451 
452 		memset(nl, 0, sizeof(nl));
453 		nl[0].n_name = "_nthreads";
454 		nl[1].n_name = "_nprocesses";
455 		nl[2].n_name = "_allprocess";
456 		nl[3].n_name = "_zombprocess";
457 		nl[4].n_name = NULL;
458 
459 		if (kvm_nlist(kd, nl) != 0) {
460 			for (i = 0; nl[i].n_type != 0; ++i)
461 				;
462 			_kvm_err(kd, kd->program,
463 			    "%s: no such symbol", nl[i].n_name);
464 			return (NULL);
465 		}
466 		if (KREAD(kd, nl[0].n_value, &maxthread)) {
467 			_kvm_err(kd, kd->program, "can't read nthreads");
468 			return (NULL);
469 		}
470 		if (KREAD(kd, nl[1].n_value, &maxprocess)) {
471 			_kvm_err(kd, kd->program, "can't read nprocesses");
472 			return (NULL);
473 		}
474 		maxthread += maxprocess;
475 
476 		kd->procbase = _kvm_reallocarray(kd, NULL, maxthread, esize);
477 		if (kd->procbase == 0)
478 			return (NULL);
479 		bp = (char *)kd->procbase;
480 
481 		/* allprocess */
482 		if (KREAD(kd, nl[2].n_value, &pr)) {
483 			_kvm_err(kd, kd->program, "cannot read allprocess");
484 			return (NULL);
485 		}
486 		nthreads = kvm_proclist(kd, op, arg, pr, bp, maxthread, esize);
487 		if (nthreads < 0)
488 			return (NULL);
489 
490 		/* zombprocess */
491 		if (KREAD(kd, nl[3].n_value, &pr)) {
492 			_kvm_err(kd, kd->program, "cannot read zombprocess");
493 			return (NULL);
494 		}
495 		i = kvm_proclist(kd, op, arg, pr, bp + (esize * nthreads),
496 		    maxthread - nthreads, esize);
497 		if (i > 0)
498 			nthreads += i;
499 	}
500 	if (kd->procbase != NULL)
501 		*cnt = nthreads;
502 	return (kd->procbase);
503 }
504