xref: /openbsd-src/lib/libkvm/kvm_proc2.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: kvm_proc2.c,v 1.13 2012/04/17 23:17:53 pirofti 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/user.h>
75 #include <sys/proc.h>
76 #include <sys/exec.h>
77 #include <sys/stat.h>
78 #include <sys/ioctl.h>
79 #include <sys/tty.h>
80 #include <sys/signalvar.h>
81 #include <stddef.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <unistd.h>
85 #include <nlist.h>
86 #include <kvm.h>
87 
88 #include <uvm/uvm_extern.h>
89 #include <uvm/uvm_amap.h>
90 #include <machine/vmparam.h>
91 #include <machine/pmap.h>
92 
93 #include <sys/sysctl.h>
94 
95 #include <limits.h>
96 #include <db.h>
97 #include <paths.h>
98 
99 #include "kvm_private.h"
100 
101 /*
102  * Read proc's from memory file into buffer bp, which has space to hold
103  * at most maxcnt procs.
104  */
105 static int
106 kvm_proclist(kvm_t *kd, int op, int arg, struct proc *p,
107     char *bp, int maxcnt, size_t esize)
108 {
109 	struct kinfo_proc kp;
110 	struct session sess;
111 	struct pcred pcred;
112 	struct ucred ucred;
113 	struct proc proc, proc2;
114 	struct process process, process2;
115 	struct pgrp pgrp;
116 	struct tty tty;
117 	struct sigacts sa, *sap;
118 	struct vmspace vm, *vmp;
119 	struct plimit limits, *limp;
120 	pid_t process_pid, parent_pid, leader_pid;
121 	int cnt = 0;
122 	int dothreads = 0;
123 
124 	dothreads = op & KERN_PROC_SHOW_THREADS;
125 	op &= ~KERN_PROC_SHOW_THREADS;
126 
127 	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
128 		if (KREAD(kd, (u_long)p, &proc)) {
129 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
130 			return (-1);
131 		}
132 		if (KREAD(kd, (u_long)proc.p_p, &process)) {
133 			_kvm_err(kd, kd->program, "can't read process at %x",
134 			    proc.p_p);
135 			return (-1);
136 		}
137 		if (KREAD(kd, (u_long)process.ps_cred, &pcred)) {
138 			_kvm_err(kd, kd->program, "can't read pcred at %x",
139 			    process.ps_cred);
140 			return (-1);
141 		}
142 		if ((proc.p_flag & P_THREAD) == 0)
143 			process_pid = proc.p_pid;
144 		else {
145 			if (KREAD(kd, (u_long)process.ps_mainproc, &proc2)) {
146 				_kvm_err(kd, kd->program,
147 				    "can't read proc at %x",
148 				    process.ps_mainproc);
149 				return (-1);
150 			}
151 			process_pid = proc2.p_pid;
152 		}
153 		if (KREAD(kd, (u_long)pcred.pc_ucred, &ucred)) {
154 			_kvm_err(kd, kd->program, "can't read ucred at %x",
155 			    pcred.pc_ucred);
156 			return (-1);
157 		}
158 		if (KREAD(kd, (u_long)process.ps_pgrp, &pgrp)) {
159 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
160 			    process.ps_pgrp);
161 			return (-1);
162 		}
163 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
164 			_kvm_err(kd, kd->program, "can't read session at %x",
165 			    pgrp.pg_session);
166 			return (-1);
167 		}
168 		if ((process.ps_flags & PS_CONTROLT) && sess.s_ttyp != NULL &&
169 		    KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
170 			_kvm_err(kd, kd->program,
171 			    "can't read tty at %x", sess.s_ttyp);
172 			return (-1);
173 		}
174 		if (process.ps_pptr) {
175 			if (KREAD(kd, (u_long)process.ps_pptr, &process2)) {
176 				_kvm_err(kd, kd->program,
177 				    "can't read process at %x",
178 				    process.ps_pptr);
179 				return (-1);
180 			}
181 			if (KREAD(kd, (u_long)process2.ps_mainproc, &proc2)) {
182 				_kvm_err(kd, kd->program,
183 				    "can't read proc at %x",
184 				    process2.ps_mainproc);
185 				return (-1);
186 			}
187 			parent_pid = proc2.p_pid;
188 		}
189 		else
190 			parent_pid = 0;
191 		if (sess.s_leader) {
192 			if (KREAD(kd, (u_long)sess.s_leader, &process2)) {
193 				_kvm_err(kd, kd->program,
194 				    "can't read proc at %x", sess.s_leader);
195 				return (-1);
196 			}
197 			if (KREAD(kd, (u_long)process2.ps_mainproc, &proc2)) {
198 				_kvm_err(kd, kd->program,
199 				    "can't read proc at %x",
200 				    process2.ps_mainproc);
201 				return (-1);
202 			}
203 			leader_pid = proc2.p_pid;
204 		}
205 		else
206 			leader_pid = 0;
207 		if (proc.p_sigacts) {
208 			if (KREAD(kd, (u_long)proc.p_sigacts, &sa)) {
209 				_kvm_err(kd, kd->program, "can't read sigacts at %x",
210 				    proc.p_sigacts);
211 				return (-1);
212 			}
213 			sap = &sa;
214 		}
215 		else
216 			sap = NULL;
217 
218 		switch (op) {
219 		case KERN_PROC_PID:
220 			if (proc.p_pid != (pid_t)arg)
221 				continue;
222 			break;
223 
224 		case KERN_PROC_PGRP:
225 			if (pgrp.pg_id != (pid_t)arg)
226 				continue;
227 			break;
228 
229 		case KERN_PROC_SESSION:
230 			if (sess.s_leader == NULL ||
231 			    leader_pid != (pid_t)arg)
232 				continue;
233 			break;
234 
235 		case KERN_PROC_TTY:
236 			if ((process.ps_flags & PS_CONTROLT) == 0 ||
237 			    sess.s_ttyp == NULL ||
238 			    tty.t_dev != (dev_t)arg)
239 				continue;
240 			break;
241 
242 		case KERN_PROC_UID:
243 			if (ucred.cr_uid != (uid_t)arg)
244 				continue;
245 			break;
246 
247 		case KERN_PROC_RUID:
248 			if (pcred.p_ruid != (uid_t)arg)
249 				continue;
250 			break;
251 
252 		case KERN_PROC_ALL:
253 			if (proc.p_flag & P_SYSTEM)
254 				continue;
255 			break;
256 
257 		case KERN_PROC_KTHREAD:
258 			/* no filtering */
259 			break;
260 
261 		default:
262 			_kvm_err(kd, kd->program, "invalid filter");
263 			return (-1);
264 		}
265 
266 		/*
267 		 * We're going to add another proc to the set.  If this
268 		 * will overflow the buffer, assume the reason is because
269 		 * nthreads (or the proc list) is corrupt and declare an error.
270 		 */
271 		if (cnt >= maxcnt) {
272 			_kvm_err(kd, kd->program, "nthreads corrupt");
273 			return (-1);
274 		}
275 
276 		/* set up stuff that might not always be there */
277 		vmp = &vm;
278 		if (proc.p_stat == SIDL ||
279 		    P_ZOMBIE(&proc) ||
280 		    KREAD(kd, (u_long)proc.p_vmspace, &vm))
281 			vmp = NULL;
282 
283 		limp = &limits;
284 		if (!process.ps_limit ||
285 		    KREAD(kd, (u_long)process.ps_limit, &limits))
286 			limp = NULL;
287 
288 #define do_copy_str(_d, _s, _l)	kvm_read(kd, (u_long)(_s), (_d), (_l)-1)
289 		if ((proc.p_flag & P_THREAD) == 0) {
290 			FILL_KPROC(&kp, do_copy_str, &proc, &process, &pcred,
291 			    &ucred, &pgrp, p, proc.p_p, &sess, vmp, limp, sap,
292 			    0);
293 
294 			/* stuff that's too painful to generalize */
295 			kp.p_pid = process_pid;
296 			kp.p_ppid = parent_pid;
297 			kp.p_sid = leader_pid;
298 			if ((process.ps_flags & PS_CONTROLT) &&
299 			    sess.s_ttyp != NULL) {
300 				kp.p_tdev = tty.t_dev;
301 				if (tty.t_pgrp != NULL &&
302 				    tty.t_pgrp != process.ps_pgrp &&
303 				    KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
304 					_kvm_err(kd, kd->program,
305 					    "can't read tpgrp at &x",
306 					    tty.t_pgrp);
307 					return (-1);
308 				}
309 				kp.p_tpgid = tty.t_pgrp ? pgrp.pg_id : -1;
310 				kp.p_tsess = PTRTOINT64(tty.t_session);
311 			} else {
312 				kp.p_tpgid = -1;
313 				kp.p_tdev = NODEV;
314 			}
315 
316 			memcpy(bp, &kp, esize);
317 			bp += esize;
318 			++cnt;
319 		}
320 
321 		if (!dothreads)
322 			continue;
323 
324 		FILL_KPROC(&kp, do_copy_str, &proc, &process, &pcred, &ucred,
325 		    &pgrp, p, proc.p_p, &sess, vmp, limp, sap, 1);
326 
327 		/* stuff that's too painful to generalize into the macros */
328 		kp.p_pid = process_pid;
329 		kp.p_ppid = parent_pid;
330 		kp.p_sid = leader_pid;
331 		if ((process.ps_flags & PS_CONTROLT) && sess.s_ttyp != NULL) {
332 			kp.p_tdev = tty.t_dev;
333 			if (tty.t_pgrp != NULL &&
334 			    tty.t_pgrp != process.ps_pgrp &&
335 			    KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
336 				_kvm_err(kd, kd->program,
337 				    "can't read tpgrp at &x", tty.t_pgrp);
338 				return (-1);
339 			}
340 			kp.p_tpgid = tty.t_pgrp ? pgrp.pg_id : -1;
341 			kp.p_tsess = PTRTOINT64(tty.t_session);
342 		} else {
343 			kp.p_tpgid = -1;
344 			kp.p_tdev = NODEV;
345 		}
346 
347 		memcpy(bp, &kp, esize);
348 		bp += esize;
349 		++cnt;
350 #undef do_copy_str
351 	}
352 	return (cnt);
353 }
354 
355 struct kinfo_proc *
356 kvm_getprocs(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
357 {
358 	int mib[6], st, nthreads;
359 	size_t size;
360 
361 	if ((ssize_t)esize < 0)
362 		return (NULL);
363 
364 	if (kd->procbase != NULL) {
365 		free(kd->procbase);
366 		/*
367 		 * Clear this pointer in case this call fails.  Otherwise,
368 		 * kvm_close() will free it again.
369 		 */
370 		kd->procbase = 0;
371 	}
372 
373 	if (ISALIVE(kd)) {
374 		size = 0;
375 		mib[0] = CTL_KERN;
376 		mib[1] = KERN_PROC;
377 		mib[2] = op;
378 		mib[3] = arg;
379 		mib[4] = esize;
380 		mib[5] = 0;
381 		st = sysctl(mib, 6, NULL, &size, NULL, 0);
382 		if (st == -1) {
383 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
384 			return (NULL);
385 		}
386 
387 		mib[5] = size / esize;
388 		kd->procbase = _kvm_malloc(kd, size);
389 		if (kd->procbase == 0)
390 			return (NULL);
391 		st = sysctl(mib, 6, kd->procbase, &size, NULL, 0);
392 		if (st == -1) {
393 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
394 			return (NULL);
395 		}
396 		nthreads = size / esize;
397 	} else {
398 		struct nlist nl[4];
399 		int i, maxthread;
400 		struct proc *p;
401 		char *bp;
402 
403 		if (esize > sizeof(struct kinfo_proc)) {
404 			_kvm_syserr(kd, kd->program,
405 			    "kvm_getprocs: unknown fields requested: libkvm out of date?");
406 			return (NULL);
407 		}
408 
409 		memset(nl, 0, sizeof(nl));
410 		nl[0].n_name = "_nthreads";
411 		nl[1].n_name = "_allproc";
412 		nl[2].n_name = "_zombproc";
413 		nl[3].n_name = NULL;
414 
415 		if (kvm_nlist(kd, nl) != 0) {
416 			for (i = 0; nl[i].n_type != 0; ++i)
417 				;
418 			_kvm_err(kd, kd->program,
419 			    "%s: no such symbol", nl[i].n_name);
420 			return (NULL);
421 		}
422 		if (KREAD(kd, nl[0].n_value, &maxthread)) {
423 			_kvm_err(kd, kd->program, "can't read nthreads");
424 			return (NULL);
425 		}
426 
427 		kd->procbase = _kvm_malloc(kd, maxthread * esize);
428 		if (kd->procbase == 0)
429 			return (NULL);
430 		bp = (char *)kd->procbase;
431 
432 		/* allproc */
433 		if (KREAD(kd, nl[1].n_value, &p)) {
434 			_kvm_err(kd, kd->program, "cannot read allproc");
435 			return (NULL);
436 		}
437 		nthreads = kvm_proclist(kd, op, arg, p, bp, maxthread, esize);
438 		if (nthreads < 0)
439 			return (NULL);
440 
441 		/* zombproc */
442 		if (KREAD(kd, nl[2].n_value, &p)) {
443 			_kvm_err(kd, kd->program, "cannot read zombproc");
444 			return (NULL);
445 		}
446 		i = kvm_proclist(kd, op, arg, p, bp + (esize * nthreads),
447 		    maxthread - nthreads, esize);
448 		if (i > 0)
449 			nthreads += i;
450 	}
451 	if (kd->procbase != NULL)
452 		*cnt = nthreads;
453 	return (kd->procbase);
454 }
455