| 1 | /* $NetBSD: kvm_proc.c,v 1.92 2016/04/04 22:14:38 christos Exp $ */ |
| 2 | |
| 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 | /*- |
| 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 | #include <sys/cdefs.h> |
| 66 | #if defined(LIBC_SCCS) && !defined(lint) |
| 67 | #if 0 |
| 68 | static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93" ; |
| 69 | #else |
| 70 | __RCSID("$NetBSD: kvm_proc.c,v 1.92 2016/04/04 22:14:38 christos Exp $" ); |
| 71 | #endif |
| 72 | #endif /* LIBC_SCCS and not lint */ |
| 73 | |
| 74 | /* |
| 75 | * Proc traversal interface for kvm. ps and w are (probably) the exclusive |
| 76 | * users of this code, so we've factored it out into a separate module. |
| 77 | * Thus, we keep this grunge out of the other kvm applications (i.e., |
| 78 | * most other applications are interested only in open/close/read/nlist). |
| 79 | */ |
| 80 | |
| 81 | #include <sys/param.h> |
| 82 | #include <sys/lwp.h> |
| 83 | #include <sys/wait.h> |
| 84 | #include <sys/proc.h> |
| 85 | #include <sys/exec.h> |
| 86 | #include <sys/stat.h> |
| 87 | #include <sys/ioctl.h> |
| 88 | #include <sys/tty.h> |
| 89 | #include <sys/resourcevar.h> |
| 90 | #include <sys/mutex.h> |
| 91 | #include <sys/specificdata.h> |
| 92 | #include <sys/types.h> |
| 93 | |
| 94 | #include <errno.h> |
| 95 | #include <stdlib.h> |
| 96 | #include <stddef.h> |
| 97 | #include <string.h> |
| 98 | #include <unistd.h> |
| 99 | #include <nlist.h> |
| 100 | #include <kvm.h> |
| 101 | |
| 102 | #include <uvm/uvm_extern.h> |
| 103 | #include <uvm/uvm_param.h> |
| 104 | #include <uvm/uvm_amap.h> |
| 105 | #include <uvm/uvm_page.h> |
| 106 | |
| 107 | #include <sys/sysctl.h> |
| 108 | |
| 109 | #include <limits.h> |
| 110 | #include <db.h> |
| 111 | #include <paths.h> |
| 112 | |
| 113 | #include "kvm_private.h" |
| 114 | |
| 115 | /* |
| 116 | * Common info from kinfo_proc and kinfo_proc2 used by helper routines. |
| 117 | */ |
| 118 | struct miniproc { |
| 119 | struct vmspace *p_vmspace; |
| 120 | char p_stat; |
| 121 | struct proc *p_paddr; |
| 122 | pid_t p_pid; |
| 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * Convert from struct proc and kinfo_proc{,2} to miniproc. |
| 127 | */ |
| 128 | #define PTOMINI(kp, p) \ |
| 129 | do { \ |
| 130 | (p)->p_stat = (kp)->p_stat; \ |
| 131 | (p)->p_pid = (kp)->p_pid; \ |
| 132 | (p)->p_paddr = NULL; \ |
| 133 | (p)->p_vmspace = (kp)->p_vmspace; \ |
| 134 | } while (/*CONSTCOND*/0); |
| 135 | |
| 136 | #define KPTOMINI(kp, p) \ |
| 137 | do { \ |
| 138 | (p)->p_stat = (kp)->kp_proc.p_stat; \ |
| 139 | (p)->p_pid = (kp)->kp_proc.p_pid; \ |
| 140 | (p)->p_paddr = (kp)->kp_eproc.e_paddr; \ |
| 141 | (p)->p_vmspace = (kp)->kp_proc.p_vmspace; \ |
| 142 | } while (/*CONSTCOND*/0); |
| 143 | |
| 144 | #define KP2TOMINI(kp, p) \ |
| 145 | do { \ |
| 146 | (p)->p_stat = (kp)->p_stat; \ |
| 147 | (p)->p_pid = (kp)->p_pid; \ |
| 148 | (p)->p_paddr = (void *)(long)(kp)->p_paddr; \ |
| 149 | (p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \ |
| 150 | } while (/*CONSTCOND*/0); |
| 151 | |
| 152 | /* |
| 153 | * NetBSD uses kauth(9) to manage credentials, which are stored in kauth_cred_t, |
| 154 | * a kernel-only opaque type. This is an embedded version which is *INTERNAL* to |
| 155 | * kvm(3) so dumps can be read properly. |
| 156 | * |
| 157 | * Whenever NetBSD starts exporting credentials to userland consistently (using |
| 158 | * 'struct uucred', or something) this will have to be updated again. |
| 159 | */ |
| 160 | struct kvm_kauth_cred { |
| 161 | u_int cr_refcnt; /* reference count */ |
| 162 | uint8_t cr_pad[CACHE_LINE_SIZE - sizeof(u_int)]; |
| 163 | uid_t cr_uid; /* user id */ |
| 164 | uid_t cr_euid; /* effective user id */ |
| 165 | uid_t cr_svuid; /* saved effective user id */ |
| 166 | gid_t cr_gid; /* group id */ |
| 167 | gid_t cr_egid; /* effective group id */ |
| 168 | gid_t cr_svgid; /* saved effective group id */ |
| 169 | u_int cr_ngroups; /* number of groups */ |
| 170 | gid_t cr_groups[NGROUPS]; /* group memberships */ |
| 171 | specificdata_reference cr_sd; /* specific data */ |
| 172 | }; |
| 173 | |
| 174 | /* XXX: What uses these two functions? */ |
| 175 | char *_kvm_uread(kvm_t *, const struct proc *, u_long, u_long *); |
| 176 | ssize_t kvm_uread(kvm_t *, const struct proc *, u_long, char *, |
| 177 | size_t); |
| 178 | |
| 179 | static char *_kvm_ureadm(kvm_t *, const struct miniproc *, u_long, |
| 180 | u_long *); |
| 181 | static ssize_t kvm_ureadm(kvm_t *, const struct miniproc *, u_long, |
| 182 | char *, size_t); |
| 183 | |
| 184 | static char **kvm_argv(kvm_t *, const struct miniproc *, u_long, int, int); |
| 185 | static int kvm_deadprocs(kvm_t *, int, int, u_long, u_long, int); |
| 186 | static char **kvm_doargv(kvm_t *, const struct miniproc *, int, |
| 187 | void (*)(struct ps_strings *, u_long *, int *)); |
| 188 | static char **kvm_doargv2(kvm_t *, pid_t, int, int); |
| 189 | static int kvm_proclist(kvm_t *, int, int, struct proc *, |
| 190 | struct kinfo_proc *, int); |
| 191 | static int proc_verify(kvm_t *, u_long, const struct miniproc *); |
| 192 | static void ps_str_a(struct ps_strings *, u_long *, int *); |
| 193 | static void ps_str_e(struct ps_strings *, u_long *, int *); |
| 194 | |
| 195 | |
| 196 | static char * |
| 197 | _kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long va, u_long *cnt) |
| 198 | { |
| 199 | u_long addr, head; |
| 200 | u_long offset; |
| 201 | struct vm_map_entry vme; |
| 202 | struct vm_amap amap; |
| 203 | struct vm_anon *anonp, anon; |
| 204 | struct vm_page pg; |
| 205 | u_long slot; |
| 206 | |
| 207 | if (kd->swapspc == NULL) { |
| 208 | kd->swapspc = _kvm_malloc(kd, (size_t)kd->nbpg); |
| 209 | if (kd->swapspc == NULL) |
| 210 | return (NULL); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Look through the address map for the memory object |
| 215 | * that corresponds to the given virtual address. |
| 216 | * The header just has the entire valid range. |
| 217 | */ |
| 218 | head = (u_long)&p->p_vmspace->vm_map.header; |
| 219 | addr = head; |
| 220 | for (;;) { |
| 221 | if (KREAD(kd, addr, &vme)) |
| 222 | return (NULL); |
| 223 | |
| 224 | if (va >= vme.start && va < vme.end && |
| 225 | vme.aref.ar_amap != NULL) |
| 226 | break; |
| 227 | |
| 228 | addr = (u_long)vme.next; |
| 229 | if (addr == head) |
| 230 | return (NULL); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * we found the map entry, now to find the object... |
| 235 | */ |
| 236 | if (vme.aref.ar_amap == NULL) |
| 237 | return (NULL); |
| 238 | |
| 239 | addr = (u_long)vme.aref.ar_amap; |
| 240 | if (KREAD(kd, addr, &amap)) |
| 241 | return (NULL); |
| 242 | |
| 243 | offset = va - vme.start; |
| 244 | slot = offset / kd->nbpg + vme.aref.ar_pageoff; |
| 245 | /* sanity-check slot number */ |
| 246 | if (slot > amap.am_nslot) |
| 247 | return (NULL); |
| 248 | |
| 249 | addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp); |
| 250 | if (KREAD(kd, addr, &anonp)) |
| 251 | return (NULL); |
| 252 | |
| 253 | addr = (u_long)anonp; |
| 254 | if (KREAD(kd, addr, &anon)) |
| 255 | return (NULL); |
| 256 | |
| 257 | addr = (u_long)anon.an_page; |
| 258 | if (addr) { |
| 259 | if (KREAD(kd, addr, &pg)) |
| 260 | return (NULL); |
| 261 | |
| 262 | if (_kvm_pread(kd, kd->pmfd, kd->swapspc, (size_t)kd->nbpg, |
| 263 | (off_t)pg.phys_addr) != kd->nbpg) |
| 264 | return (NULL); |
| 265 | } else { |
| 266 | if (kd->swfd < 0 || |
| 267 | _kvm_pread(kd, kd->swfd, kd->swapspc, (size_t)kd->nbpg, |
| 268 | (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg) |
| 269 | return (NULL); |
| 270 | } |
| 271 | |
| 272 | /* Found the page. */ |
| 273 | offset %= kd->nbpg; |
| 274 | *cnt = kd->nbpg - offset; |
| 275 | return (&kd->swapspc[(size_t)offset]); |
| 276 | } |
| 277 | |
| 278 | char * |
| 279 | _kvm_uread(kvm_t *kd, const struct proc *p, u_long va, u_long *cnt) |
| 280 | { |
| 281 | struct miniproc mp; |
| 282 | |
| 283 | PTOMINI(p, &mp); |
| 284 | return (_kvm_ureadm(kd, &mp, va, cnt)); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Convert credentials located in kernel space address 'cred' and store |
| 289 | * them in the appropriate members of 'eproc'. |
| 290 | */ |
| 291 | static int |
| 292 | _kvm_convertcred(kvm_t *kd, u_long cred, struct eproc *eproc) |
| 293 | { |
| 294 | struct kvm_kauth_cred kauthcred; |
| 295 | struct ki_pcred *pc = &eproc->e_pcred; |
| 296 | struct ki_ucred *uc = &eproc->e_ucred; |
| 297 | |
| 298 | if (KREAD(kd, cred, &kauthcred) != 0) |
| 299 | return (-1); |
| 300 | |
| 301 | /* inlined version of kauth_cred_to_pcred, see kauth(9). */ |
| 302 | pc->p_ruid = kauthcred.cr_uid; |
| 303 | pc->p_svuid = kauthcred.cr_svuid; |
| 304 | pc->p_rgid = kauthcred.cr_gid; |
| 305 | pc->p_svgid = kauthcred.cr_svgid; |
| 306 | pc->p_refcnt = kauthcred.cr_refcnt; |
| 307 | pc->p_pad = NULL; |
| 308 | |
| 309 | /* inlined version of kauth_cred_to_ucred(), see kauth(9). */ |
| 310 | uc->cr_ref = kauthcred.cr_refcnt; |
| 311 | uc->cr_uid = kauthcred.cr_euid; |
| 312 | uc->cr_gid = kauthcred.cr_egid; |
| 313 | uc->cr_ngroups = (uint32_t)MIN(kauthcred.cr_ngroups, |
| 314 | sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0])); |
| 315 | memcpy(uc->cr_groups, kauthcred.cr_groups, |
| 316 | uc->cr_ngroups * sizeof(uc->cr_groups[0])); |
| 317 | |
| 318 | return (0); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Read proc's from memory file into buffer bp, which has space to hold |
| 323 | * at most maxcnt procs. |
| 324 | */ |
| 325 | static int |
| 326 | kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, |
| 327 | struct kinfo_proc *bp, int maxcnt) |
| 328 | { |
| 329 | int cnt = 0; |
| 330 | int nlwps; |
| 331 | struct kinfo_lwp *kl; |
| 332 | struct eproc eproc; |
| 333 | struct pgrp pgrp; |
| 334 | struct session sess; |
| 335 | struct tty tty; |
| 336 | struct proc proc; |
| 337 | |
| 338 | for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) { |
| 339 | if (KREAD(kd, (u_long)p, &proc)) { |
| 340 | _kvm_err(kd, kd->program, "can't read proc at %p" , p); |
| 341 | return (-1); |
| 342 | } |
| 343 | if (_kvm_convertcred(kd, (u_long)proc.p_cred, &eproc) != 0) { |
| 344 | _kvm_err(kd, kd->program, |
| 345 | "can't read proc credentials at %p" , p); |
| 346 | return (-1); |
| 347 | } |
| 348 | |
| 349 | switch (what) { |
| 350 | |
| 351 | case KERN_PROC_PID: |
| 352 | if (proc.p_pid != (pid_t)arg) |
| 353 | continue; |
| 354 | break; |
| 355 | |
| 356 | case KERN_PROC_UID: |
| 357 | if (eproc.e_ucred.cr_uid != (uid_t)arg) |
| 358 | continue; |
| 359 | break; |
| 360 | |
| 361 | case KERN_PROC_RUID: |
| 362 | if (eproc.e_pcred.p_ruid != (uid_t)arg) |
| 363 | continue; |
| 364 | break; |
| 365 | } |
| 366 | /* |
| 367 | * We're going to add another proc to the set. If this |
| 368 | * will overflow the buffer, assume the reason is because |
| 369 | * nprocs (or the proc list) is corrupt and declare an error. |
| 370 | */ |
| 371 | if (cnt >= maxcnt) { |
| 372 | _kvm_err(kd, kd->program, "nprocs corrupt" ); |
| 373 | return (-1); |
| 374 | } |
| 375 | /* |
| 376 | * gather eproc |
| 377 | */ |
| 378 | eproc.e_paddr = p; |
| 379 | if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { |
| 380 | _kvm_err(kd, kd->program, "can't read pgrp at %p" , |
| 381 | proc.p_pgrp); |
| 382 | return (-1); |
| 383 | } |
| 384 | eproc.e_sess = pgrp.pg_session; |
| 385 | eproc.e_pgid = pgrp.pg_id; |
| 386 | eproc.e_jobc = pgrp.pg_jobc; |
| 387 | if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { |
| 388 | _kvm_err(kd, kd->program, "can't read session at %p" , |
| 389 | pgrp.pg_session); |
| 390 | return (-1); |
| 391 | } |
| 392 | if ((proc.p_lflag & PL_CONTROLT) && sess.s_ttyp != NULL) { |
| 393 | if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { |
| 394 | _kvm_err(kd, kd->program, |
| 395 | "can't read tty at %p" , sess.s_ttyp); |
| 396 | return (-1); |
| 397 | } |
| 398 | eproc.e_tdev = (uint32_t)tty.t_dev; |
| 399 | eproc.e_tsess = tty.t_session; |
| 400 | if (tty.t_pgrp != NULL) { |
| 401 | if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { |
| 402 | _kvm_err(kd, kd->program, |
| 403 | "can't read tpgrp at %p" , |
| 404 | tty.t_pgrp); |
| 405 | return (-1); |
| 406 | } |
| 407 | eproc.e_tpgid = pgrp.pg_id; |
| 408 | } else |
| 409 | eproc.e_tpgid = -1; |
| 410 | } else |
| 411 | eproc.e_tdev = (uint32_t)NODEV; |
| 412 | eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; |
| 413 | eproc.e_sid = sess.s_sid; |
| 414 | if (sess.s_leader == p) |
| 415 | eproc.e_flag |= EPROC_SLEADER; |
| 416 | /* |
| 417 | * Fill in the old-style proc.p_wmesg by copying the wmesg |
| 418 | * from the first available LWP. |
| 419 | */ |
| 420 | kl = kvm_getlwps(kd, proc.p_pid, |
| 421 | (u_long)PTRTOUINT64(eproc.e_paddr), |
| 422 | sizeof(struct kinfo_lwp), &nlwps); |
| 423 | if (kl) { |
| 424 | if (nlwps > 0) { |
| 425 | strcpy(eproc.e_wmesg, kl[0].l_wmesg); |
| 426 | } |
| 427 | } |
| 428 | (void)kvm_read(kd, (u_long)proc.p_vmspace, &eproc.e_vm, |
| 429 | sizeof(eproc.e_vm)); |
| 430 | |
| 431 | eproc.e_xsize = eproc.e_xrssize = 0; |
| 432 | eproc.e_xccount = eproc.e_xswrss = 0; |
| 433 | |
| 434 | switch (what) { |
| 435 | |
| 436 | case KERN_PROC_PGRP: |
| 437 | if (eproc.e_pgid != (pid_t)arg) |
| 438 | continue; |
| 439 | break; |
| 440 | |
| 441 | case KERN_PROC_TTY: |
| 442 | if ((proc.p_lflag & PL_CONTROLT) == 0 || |
| 443 | eproc.e_tdev != (dev_t)arg) |
| 444 | continue; |
| 445 | break; |
| 446 | } |
| 447 | memcpy(&bp->kp_proc, &proc, sizeof(proc)); |
| 448 | memcpy(&bp->kp_eproc, &eproc, sizeof(eproc)); |
| 449 | ++bp; |
| 450 | ++cnt; |
| 451 | } |
| 452 | return (cnt); |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Build proc info array by reading in proc list from a crash dump. |
| 457 | * Return number of procs read. maxcnt is the max we will read. |
| 458 | */ |
| 459 | static int |
| 460 | kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, |
| 461 | u_long a_zombproc, int maxcnt) |
| 462 | { |
| 463 | struct kinfo_proc *bp = kd->procbase; |
| 464 | int acnt, zcnt; |
| 465 | struct proc *p; |
| 466 | |
| 467 | if (KREAD(kd, a_allproc, &p)) { |
| 468 | _kvm_err(kd, kd->program, "cannot read allproc" ); |
| 469 | return (-1); |
| 470 | } |
| 471 | acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); |
| 472 | if (acnt < 0) |
| 473 | return (acnt); |
| 474 | |
| 475 | if (KREAD(kd, a_zombproc, &p)) { |
| 476 | _kvm_err(kd, kd->program, "cannot read zombproc" ); |
| 477 | return (-1); |
| 478 | } |
| 479 | zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, |
| 480 | maxcnt - acnt); |
| 481 | if (zcnt < 0) |
| 482 | zcnt = 0; |
| 483 | |
| 484 | return (acnt + zcnt); |
| 485 | } |
| 486 | |
| 487 | struct kinfo_proc2 * |
| 488 | kvm_getproc2(kvm_t *kd, int op, int arg, size_t esize, int *cnt) |
| 489 | { |
| 490 | size_t size; |
| 491 | int mib[6], st, nprocs; |
| 492 | struct pstats pstats; |
| 493 | |
| 494 | if (ISSYSCTL(kd)) { |
| 495 | size = 0; |
| 496 | mib[0] = CTL_KERN; |
| 497 | mib[1] = KERN_PROC2; |
| 498 | mib[2] = op; |
| 499 | mib[3] = arg; |
| 500 | mib[4] = (int)esize; |
| 501 | again: |
| 502 | mib[5] = 0; |
| 503 | st = sysctl(mib, 6, NULL, &size, NULL, (size_t)0); |
| 504 | if (st == -1) { |
| 505 | _kvm_syserr(kd, kd->program, "kvm_getproc2" ); |
| 506 | return (NULL); |
| 507 | } |
| 508 | |
| 509 | mib[5] = (int) (size / esize); |
| 510 | KVM_ALLOC(kd, procbase2, size); |
| 511 | st = sysctl(mib, 6, kd->procbase2, &size, NULL, (size_t)0); |
| 512 | if (st == -1) { |
| 513 | if (errno == ENOMEM) { |
| 514 | goto again; |
| 515 | } |
| 516 | _kvm_syserr(kd, kd->program, "kvm_getproc2" ); |
| 517 | return (NULL); |
| 518 | } |
| 519 | nprocs = (int) (size / esize); |
| 520 | } else { |
| 521 | char *kp2c; |
| 522 | struct kinfo_proc *kp; |
| 523 | struct kinfo_proc2 kp2, *kp2p; |
| 524 | struct kinfo_lwp *kl; |
| 525 | int i, nlwps; |
| 526 | |
| 527 | kp = kvm_getprocs(kd, op, arg, &nprocs); |
| 528 | if (kp == NULL) |
| 529 | return (NULL); |
| 530 | |
| 531 | size = nprocs * esize; |
| 532 | KVM_ALLOC(kd, procbase2, size); |
| 533 | kp2c = (char *)(void *)kd->procbase2; |
| 534 | kp2p = &kp2; |
| 535 | for (i = 0; i < nprocs; i++, kp++) { |
| 536 | struct timeval tv; |
| 537 | |
| 538 | kl = kvm_getlwps(kd, kp->kp_proc.p_pid, |
| 539 | (u_long)PTRTOUINT64(kp->kp_eproc.e_paddr), |
| 540 | sizeof(struct kinfo_lwp), &nlwps); |
| 541 | |
| 542 | if (kl == NULL) { |
| 543 | _kvm_syserr(kd, NULL, |
| 544 | "kvm_getlwps() failed on process %u\n" , |
| 545 | kp->kp_proc.p_pid); |
| 546 | if (nlwps == 0) |
| 547 | return NULL; |
| 548 | else |
| 549 | continue; |
| 550 | } |
| 551 | |
| 552 | /* We use kl[0] as the "representative" LWP */ |
| 553 | memset(kp2p, 0, sizeof(kp2)); |
| 554 | kp2p->p_forw = kl[0].l_forw; |
| 555 | kp2p->p_back = kl[0].l_back; |
| 556 | kp2p->p_paddr = PTRTOUINT64(kp->kp_eproc.e_paddr); |
| 557 | kp2p->p_addr = kl[0].l_addr; |
| 558 | kp2p->p_fd = PTRTOUINT64(kp->kp_proc.p_fd); |
| 559 | kp2p->p_cwdi = PTRTOUINT64(kp->kp_proc.p_cwdi); |
| 560 | kp2p->p_stats = PTRTOUINT64(kp->kp_proc.p_stats); |
| 561 | kp2p->p_limit = PTRTOUINT64(kp->kp_proc.p_limit); |
| 562 | kp2p->p_vmspace = PTRTOUINT64(kp->kp_proc.p_vmspace); |
| 563 | kp2p->p_sigacts = PTRTOUINT64(kp->kp_proc.p_sigacts); |
| 564 | kp2p->p_sess = PTRTOUINT64(kp->kp_eproc.e_sess); |
| 565 | kp2p->p_tsess = 0; |
| 566 | #if 1 /* XXX: dsl - p_ru was only ever non-zero for zombies */ |
| 567 | kp2p->p_ru = 0; |
| 568 | #else |
| 569 | kp2p->p_ru = PTRTOUINT64(pstats.p_ru); |
| 570 | #endif |
| 571 | |
| 572 | kp2p->p_eflag = 0; |
| 573 | kp2p->p_exitsig = kp->kp_proc.p_exitsig; |
| 574 | kp2p->p_flag = kp->kp_proc.p_flag; |
| 575 | |
| 576 | kp2p->p_pid = kp->kp_proc.p_pid; |
| 577 | |
| 578 | kp2p->p_ppid = kp->kp_eproc.e_ppid; |
| 579 | kp2p->p_sid = kp->kp_eproc.e_sid; |
| 580 | kp2p->p__pgid = kp->kp_eproc.e_pgid; |
| 581 | |
| 582 | kp2p->p_tpgid = -1 /* XXX NO_PGID! */; |
| 583 | |
| 584 | kp2p->p_uid = kp->kp_eproc.e_ucred.cr_uid; |
| 585 | kp2p->p_ruid = kp->kp_eproc.e_pcred.p_ruid; |
| 586 | kp2p->p_svuid = kp->kp_eproc.e_pcred.p_svuid; |
| 587 | kp2p->p_gid = kp->kp_eproc.e_ucred.cr_gid; |
| 588 | kp2p->p_rgid = kp->kp_eproc.e_pcred.p_rgid; |
| 589 | kp2p->p_svgid = kp->kp_eproc.e_pcred.p_svgid; |
| 590 | |
| 591 | /*CONSTCOND*/ |
| 592 | memcpy(kp2p->p_groups, kp->kp_eproc.e_ucred.cr_groups, |
| 593 | MIN(sizeof(kp2p->p_groups), |
| 594 | sizeof(kp->kp_eproc.e_ucred.cr_groups))); |
| 595 | kp2p->p_ngroups = kp->kp_eproc.e_ucred.cr_ngroups; |
| 596 | |
| 597 | kp2p->p_jobc = kp->kp_eproc.e_jobc; |
| 598 | kp2p->p_tdev = kp->kp_eproc.e_tdev; |
| 599 | kp2p->p_tpgid = kp->kp_eproc.e_tpgid; |
| 600 | kp2p->p_tsess = PTRTOUINT64(kp->kp_eproc.e_tsess); |
| 601 | |
| 602 | kp2p->p_estcpu = 0; |
| 603 | bintime2timeval(&kp->kp_proc.p_rtime, &tv); |
| 604 | kp2p->p_rtime_sec = (uint32_t)tv.tv_sec; |
| 605 | kp2p->p_rtime_usec = (uint32_t)tv.tv_usec; |
| 606 | kp2p->p_cpticks = kl[0].l_cpticks; |
| 607 | kp2p->p_pctcpu = kp->kp_proc.p_pctcpu; |
| 608 | kp2p->p_swtime = kl[0].l_swtime; |
| 609 | kp2p->p_slptime = kl[0].l_slptime; |
| 610 | #if 0 /* XXX thorpej */ |
| 611 | kp2p->p_schedflags = kp->kp_proc.p_schedflags; |
| 612 | #else |
| 613 | kp2p->p_schedflags = 0; |
| 614 | #endif |
| 615 | |
| 616 | kp2p->p_uticks = kp->kp_proc.p_uticks; |
| 617 | kp2p->p_sticks = kp->kp_proc.p_sticks; |
| 618 | kp2p->p_iticks = kp->kp_proc.p_iticks; |
| 619 | |
| 620 | kp2p->p_tracep = PTRTOUINT64(kp->kp_proc.p_tracep); |
| 621 | kp2p->p_traceflag = kp->kp_proc.p_traceflag; |
| 622 | |
| 623 | kp2p->p_holdcnt = kl[0].l_holdcnt; |
| 624 | |
| 625 | memcpy(&kp2p->p_siglist, |
| 626 | &kp->kp_proc.p_sigpend.sp_set, |
| 627 | sizeof(ki_sigset_t)); |
| 628 | memset(&kp2p->p_sigmask, 0, |
| 629 | sizeof(ki_sigset_t)); |
| 630 | memcpy(&kp2p->p_sigignore, |
| 631 | &kp->kp_proc.p_sigctx.ps_sigignore, |
| 632 | sizeof(ki_sigset_t)); |
| 633 | memcpy(&kp2p->p_sigcatch, |
| 634 | &kp->kp_proc.p_sigctx.ps_sigcatch, |
| 635 | sizeof(ki_sigset_t)); |
| 636 | |
| 637 | kp2p->p_stat = kl[0].l_stat; |
| 638 | kp2p->p_priority = kl[0].l_priority; |
| 639 | kp2p->p_usrpri = kl[0].l_priority; |
| 640 | kp2p->p_nice = kp->kp_proc.p_nice; |
| 641 | |
| 642 | kp2p->p_xstat = P_WAITSTATUS(&kp->kp_proc); |
| 643 | kp2p->p_acflag = kp->kp_proc.p_acflag; |
| 644 | |
| 645 | /*CONSTCOND*/ |
| 646 | strncpy(kp2p->p_comm, kp->kp_proc.p_comm, |
| 647 | MIN(sizeof(kp2p->p_comm), |
| 648 | sizeof(kp->kp_proc.p_comm))); |
| 649 | |
| 650 | strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg, |
| 651 | sizeof(kp2p->p_wmesg)); |
| 652 | kp2p->p_wchan = kl[0].l_wchan; |
| 653 | strncpy(kp2p->p_login, kp->kp_eproc.e_login, |
| 654 | sizeof(kp2p->p_login)); |
| 655 | |
| 656 | kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize; |
| 657 | kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize; |
| 658 | kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize; |
| 659 | kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize; |
| 660 | kp2p->p_vm_vsize = kp->kp_eproc.e_vm.vm_map.size |
| 661 | / kd->nbpg; |
| 662 | /* Adjust mapped size */ |
| 663 | kp2p->p_vm_msize = |
| 664 | (kp->kp_eproc.e_vm.vm_map.size / kd->nbpg) - |
| 665 | kp->kp_eproc.e_vm.vm_issize + |
| 666 | kp->kp_eproc.e_vm.vm_ssize; |
| 667 | |
| 668 | kp2p->p_eflag = (int32_t)kp->kp_eproc.e_flag; |
| 669 | |
| 670 | kp2p->p_realflag = kp->kp_proc.p_flag; |
| 671 | kp2p->p_nlwps = kp->kp_proc.p_nlwps; |
| 672 | kp2p->p_nrlwps = kp->kp_proc.p_nrlwps; |
| 673 | kp2p->p_realstat = kp->kp_proc.p_stat; |
| 674 | |
| 675 | if (P_ZOMBIE(&kp->kp_proc) || |
| 676 | kp->kp_proc.p_stats == NULL || |
| 677 | KREAD(kd, (u_long)kp->kp_proc.p_stats, &pstats)) { |
| 678 | kp2p->p_uvalid = 0; |
| 679 | } else { |
| 680 | kp2p->p_uvalid = 1; |
| 681 | |
| 682 | kp2p->p_ustart_sec = (u_int32_t) |
| 683 | pstats.p_start.tv_sec; |
| 684 | kp2p->p_ustart_usec = (u_int32_t) |
| 685 | pstats.p_start.tv_usec; |
| 686 | |
| 687 | kp2p->p_uutime_sec = (u_int32_t) |
| 688 | pstats.p_ru.ru_utime.tv_sec; |
| 689 | kp2p->p_uutime_usec = (u_int32_t) |
| 690 | pstats.p_ru.ru_utime.tv_usec; |
| 691 | kp2p->p_ustime_sec = (u_int32_t) |
| 692 | pstats.p_ru.ru_stime.tv_sec; |
| 693 | kp2p->p_ustime_usec = (u_int32_t) |
| 694 | pstats.p_ru.ru_stime.tv_usec; |
| 695 | |
| 696 | kp2p->p_uru_maxrss = pstats.p_ru.ru_maxrss; |
| 697 | kp2p->p_uru_ixrss = pstats.p_ru.ru_ixrss; |
| 698 | kp2p->p_uru_idrss = pstats.p_ru.ru_idrss; |
| 699 | kp2p->p_uru_isrss = pstats.p_ru.ru_isrss; |
| 700 | kp2p->p_uru_minflt = pstats.p_ru.ru_minflt; |
| 701 | kp2p->p_uru_majflt = pstats.p_ru.ru_majflt; |
| 702 | kp2p->p_uru_nswap = pstats.p_ru.ru_nswap; |
| 703 | kp2p->p_uru_inblock = pstats.p_ru.ru_inblock; |
| 704 | kp2p->p_uru_oublock = pstats.p_ru.ru_oublock; |
| 705 | kp2p->p_uru_msgsnd = pstats.p_ru.ru_msgsnd; |
| 706 | kp2p->p_uru_msgrcv = pstats.p_ru.ru_msgrcv; |
| 707 | kp2p->p_uru_nsignals = pstats.p_ru.ru_nsignals; |
| 708 | kp2p->p_uru_nvcsw = pstats.p_ru.ru_nvcsw; |
| 709 | kp2p->p_uru_nivcsw = pstats.p_ru.ru_nivcsw; |
| 710 | |
| 711 | kp2p->p_uctime_sec = (u_int32_t) |
| 712 | (pstats.p_cru.ru_utime.tv_sec + |
| 713 | pstats.p_cru.ru_stime.tv_sec); |
| 714 | kp2p->p_uctime_usec = (u_int32_t) |
| 715 | (pstats.p_cru.ru_utime.tv_usec + |
| 716 | pstats.p_cru.ru_stime.tv_usec); |
| 717 | } |
| 718 | |
| 719 | memcpy(kp2c, &kp2, esize); |
| 720 | kp2c += esize; |
| 721 | } |
| 722 | } |
| 723 | *cnt = nprocs; |
| 724 | return (kd->procbase2); |
| 725 | } |
| 726 | |
| 727 | struct kinfo_lwp * |
| 728 | kvm_getlwps(kvm_t *kd, int pid, u_long paddr, size_t esize, int *cnt) |
| 729 | { |
| 730 | size_t size; |
| 731 | int mib[5], nlwps; |
| 732 | ssize_t st; |
| 733 | struct kinfo_lwp *kl; |
| 734 | |
| 735 | if (ISSYSCTL(kd)) { |
| 736 | size = 0; |
| 737 | mib[0] = CTL_KERN; |
| 738 | mib[1] = KERN_LWP; |
| 739 | mib[2] = pid; |
| 740 | mib[3] = (int)esize; |
| 741 | mib[4] = 0; |
| 742 | again: |
| 743 | st = sysctl(mib, 5, NULL, &size, NULL, (size_t)0); |
| 744 | if (st == -1) { |
| 745 | switch (errno) { |
| 746 | case ESRCH: /* Treat this as a soft error; see kvm.c */ |
| 747 | _kvm_syserr(kd, NULL, "kvm_getlwps" ); |
| 748 | return NULL; |
| 749 | default: |
| 750 | _kvm_syserr(kd, kd->program, "kvm_getlwps" ); |
| 751 | return NULL; |
| 752 | } |
| 753 | } |
| 754 | mib[4] = (int) (size / esize); |
| 755 | KVM_ALLOC(kd, lwpbase, size); |
| 756 | st = sysctl(mib, 5, kd->lwpbase, &size, NULL, (size_t)0); |
| 757 | if (st == -1) { |
| 758 | switch (errno) { |
| 759 | case ESRCH: /* Treat this as a soft error; see kvm.c */ |
| 760 | _kvm_syserr(kd, NULL, "kvm_getlwps" ); |
| 761 | return NULL; |
| 762 | case ENOMEM: |
| 763 | goto again; |
| 764 | default: |
| 765 | _kvm_syserr(kd, kd->program, "kvm_getlwps" ); |
| 766 | return NULL; |
| 767 | } |
| 768 | } |
| 769 | nlwps = (int) (size / esize); |
| 770 | } else { |
| 771 | /* grovel through the memory image */ |
| 772 | struct proc p; |
| 773 | struct lwp l; |
| 774 | u_long laddr; |
| 775 | void *back; |
| 776 | int i; |
| 777 | |
| 778 | st = kvm_read(kd, paddr, &p, sizeof(p)); |
| 779 | if (st == -1) { |
| 780 | _kvm_syserr(kd, kd->program, "kvm_getlwps" ); |
| 781 | return (NULL); |
| 782 | } |
| 783 | |
| 784 | nlwps = p.p_nlwps; |
| 785 | size = nlwps * sizeof(*kd->lwpbase); |
| 786 | KVM_ALLOC(kd, lwpbase, size); |
| 787 | laddr = (u_long)PTRTOUINT64(p.p_lwps.lh_first); |
| 788 | for (i = 0; (i < nlwps) && (laddr != 0); i++) { |
| 789 | st = kvm_read(kd, laddr, &l, sizeof(l)); |
| 790 | if (st == -1) { |
| 791 | _kvm_syserr(kd, kd->program, "kvm_getlwps" ); |
| 792 | return (NULL); |
| 793 | } |
| 794 | kl = &kd->lwpbase[i]; |
| 795 | kl->l_laddr = laddr; |
| 796 | kl->l_forw = PTRTOUINT64(l.l_runq.tqe_next); |
| 797 | laddr = (u_long)PTRTOUINT64(l.l_runq.tqe_prev); |
| 798 | st = kvm_read(kd, laddr, &back, sizeof(back)); |
| 799 | if (st == -1) { |
| 800 | _kvm_syserr(kd, kd->program, "kvm_getlwps" ); |
| 801 | return (NULL); |
| 802 | } |
| 803 | kl->l_back = PTRTOUINT64(back); |
| 804 | kl->l_addr = PTRTOUINT64(l.l_addr); |
| 805 | kl->l_lid = l.l_lid; |
| 806 | kl->l_flag = l.l_flag; |
| 807 | kl->l_swtime = l.l_swtime; |
| 808 | kl->l_slptime = l.l_slptime; |
| 809 | kl->l_schedflags = 0; /* XXX */ |
| 810 | kl->l_holdcnt = 0; |
| 811 | kl->l_priority = l.l_priority; |
| 812 | kl->l_usrpri = l.l_priority; |
| 813 | kl->l_stat = l.l_stat; |
| 814 | kl->l_wchan = PTRTOUINT64(l.l_wchan); |
| 815 | if (l.l_wmesg) |
| 816 | (void)kvm_read(kd, (u_long)l.l_wmesg, |
| 817 | kl->l_wmesg, (size_t)WMESGLEN); |
| 818 | kl->l_cpuid = KI_NOCPU; |
| 819 | laddr = (u_long)PTRTOUINT64(l.l_sibling.le_next); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | *cnt = nlwps; |
| 824 | return (kd->lwpbase); |
| 825 | } |
| 826 | |
| 827 | struct kinfo_proc * |
| 828 | kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt) |
| 829 | { |
| 830 | size_t size; |
| 831 | int mib[4], st, nprocs; |
| 832 | |
| 833 | if (ISALIVE(kd)) { |
| 834 | size = 0; |
| 835 | mib[0] = CTL_KERN; |
| 836 | mib[1] = KERN_PROC; |
| 837 | mib[2] = op; |
| 838 | mib[3] = arg; |
| 839 | st = sysctl(mib, 4, NULL, &size, NULL, (size_t)0); |
| 840 | if (st == -1) { |
| 841 | _kvm_syserr(kd, kd->program, "kvm_getprocs" ); |
| 842 | return (NULL); |
| 843 | } |
| 844 | KVM_ALLOC(kd, procbase, size); |
| 845 | st = sysctl(mib, 4, kd->procbase, &size, NULL, (size_t)0); |
| 846 | if (st == -1) { |
| 847 | _kvm_syserr(kd, kd->program, "kvm_getprocs" ); |
| 848 | return (NULL); |
| 849 | } |
| 850 | if (size % sizeof(struct kinfo_proc) != 0) { |
| 851 | _kvm_err(kd, kd->program, |
| 852 | "proc size mismatch (%lu total, %lu chunks)" , |
| 853 | (u_long)size, (u_long)sizeof(struct kinfo_proc)); |
| 854 | return (NULL); |
| 855 | } |
| 856 | nprocs = (int) (size / sizeof(struct kinfo_proc)); |
| 857 | } else { |
| 858 | struct nlist nl[4], *p; |
| 859 | |
| 860 | (void)memset(nl, 0, sizeof(nl)); |
| 861 | nl[0].n_name = "_nprocs" ; |
| 862 | nl[1].n_name = "_allproc" ; |
| 863 | nl[2].n_name = "_zombproc" ; |
| 864 | nl[3].n_name = NULL; |
| 865 | |
| 866 | if (kvm_nlist(kd, nl) != 0) { |
| 867 | for (p = nl; p->n_type != 0; ++p) |
| 868 | continue; |
| 869 | _kvm_err(kd, kd->program, |
| 870 | "%s: no such symbol" , p->n_name); |
| 871 | return (NULL); |
| 872 | } |
| 873 | if (KREAD(kd, nl[0].n_value, &nprocs)) { |
| 874 | _kvm_err(kd, kd->program, "can't read nprocs" ); |
| 875 | return (NULL); |
| 876 | } |
| 877 | size = nprocs * sizeof(*kd->procbase); |
| 878 | KVM_ALLOC(kd, procbase, size); |
| 879 | nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, |
| 880 | nl[2].n_value, nprocs); |
| 881 | if (nprocs < 0) |
| 882 | return (NULL); |
| 883 | #ifdef notdef |
| 884 | size = nprocs * sizeof(struct kinfo_proc); |
| 885 | (void)realloc(kd->procbase, size); |
| 886 | #endif |
| 887 | } |
| 888 | *cnt = nprocs; |
| 889 | return (kd->procbase); |
| 890 | } |
| 891 | |
| 892 | void * |
| 893 | _kvm_realloc(kvm_t *kd, void *p, size_t n) |
| 894 | { |
| 895 | void *np = realloc(p, n); |
| 896 | |
| 897 | if (np == NULL) |
| 898 | _kvm_err(kd, kd->program, "out of memory" ); |
| 899 | return (np); |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * Read in an argument vector from the user address space of process p. |
| 904 | * addr if the user-space base address of narg null-terminated contiguous |
| 905 | * strings. This is used to read in both the command arguments and |
| 906 | * environment strings. Read at most maxcnt characters of strings. |
| 907 | */ |
| 908 | static char ** |
| 909 | kvm_argv(kvm_t *kd, const struct miniproc *p, u_long addr, int narg, |
| 910 | int maxcnt) |
| 911 | { |
| 912 | char *np, *cp, *ep, *ap; |
| 913 | u_long oaddr = (u_long)~0L; |
| 914 | u_long len; |
| 915 | size_t cc; |
| 916 | char **argv; |
| 917 | |
| 918 | /* |
| 919 | * Check that there aren't an unreasonable number of arguments, |
| 920 | * and that the address is in user space. |
| 921 | */ |
| 922 | if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva) |
| 923 | return (NULL); |
| 924 | |
| 925 | if (kd->argv == NULL) { |
| 926 | /* |
| 927 | * Try to avoid reallocs. |
| 928 | */ |
| 929 | kd->argc = MAX(narg + 1, 32); |
| 930 | kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv)); |
| 931 | if (kd->argv == NULL) |
| 932 | return (NULL); |
| 933 | } else if (narg + 1 > kd->argc) { |
| 934 | kd->argc = MAX(2 * kd->argc, narg + 1); |
| 935 | kd->argv = _kvm_realloc(kd, kd->argv, kd->argc * |
| 936 | sizeof(*kd->argv)); |
| 937 | if (kd->argv == NULL) |
| 938 | return (NULL); |
| 939 | } |
| 940 | if (kd->argspc == NULL) { |
| 941 | kd->argspc = _kvm_malloc(kd, (size_t)kd->nbpg); |
| 942 | if (kd->argspc == NULL) |
| 943 | return (NULL); |
| 944 | kd->argspc_len = kd->nbpg; |
| 945 | } |
| 946 | if (kd->argbuf == NULL) { |
| 947 | kd->argbuf = _kvm_malloc(kd, (size_t)kd->nbpg); |
| 948 | if (kd->argbuf == NULL) |
| 949 | return (NULL); |
| 950 | } |
| 951 | cc = sizeof(char *) * narg; |
| 952 | if (kvm_ureadm(kd, p, addr, (void *)kd->argv, cc) != cc) |
| 953 | return (NULL); |
| 954 | ap = np = kd->argspc; |
| 955 | argv = kd->argv; |
| 956 | len = 0; |
| 957 | /* |
| 958 | * Loop over pages, filling in the argument vector. |
| 959 | */ |
| 960 | while (argv < kd->argv + narg && *argv != NULL) { |
| 961 | addr = (u_long)*argv & ~(kd->nbpg - 1); |
| 962 | if (addr != oaddr) { |
| 963 | if (kvm_ureadm(kd, p, addr, kd->argbuf, |
| 964 | (size_t)kd->nbpg) != kd->nbpg) |
| 965 | return (NULL); |
| 966 | oaddr = addr; |
| 967 | } |
| 968 | addr = (u_long)*argv & (kd->nbpg - 1); |
| 969 | cp = kd->argbuf + (size_t)addr; |
| 970 | cc = kd->nbpg - (size_t)addr; |
| 971 | if (maxcnt > 0 && cc > (size_t)(maxcnt - len)) |
| 972 | cc = (size_t)(maxcnt - len); |
| 973 | ep = memchr(cp, '\0', cc); |
| 974 | if (ep != NULL) |
| 975 | cc = ep - cp + 1; |
| 976 | if (len + cc > kd->argspc_len) { |
| 977 | ptrdiff_t off; |
| 978 | char **pp; |
| 979 | char *op = kd->argspc; |
| 980 | |
| 981 | kd->argspc_len *= 2; |
| 982 | kd->argspc = _kvm_realloc(kd, kd->argspc, |
| 983 | kd->argspc_len); |
| 984 | if (kd->argspc == NULL) |
| 985 | return (NULL); |
| 986 | /* |
| 987 | * Adjust argv pointers in case realloc moved |
| 988 | * the string space. |
| 989 | */ |
| 990 | off = kd->argspc - op; |
| 991 | for (pp = kd->argv; pp < argv; pp++) |
| 992 | *pp += off; |
| 993 | ap += off; |
| 994 | np += off; |
| 995 | } |
| 996 | memcpy(np, cp, cc); |
| 997 | np += cc; |
| 998 | len += cc; |
| 999 | if (ep != NULL) { |
| 1000 | *argv++ = ap; |
| 1001 | ap = np; |
| 1002 | } else |
| 1003 | *argv += cc; |
| 1004 | if (maxcnt > 0 && len >= maxcnt) { |
| 1005 | /* |
| 1006 | * We're stopping prematurely. Terminate the |
| 1007 | * current string. |
| 1008 | */ |
| 1009 | if (ep == NULL) { |
| 1010 | *np = '\0'; |
| 1011 | *argv++ = ap; |
| 1012 | } |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | /* Make sure argv is terminated. */ |
| 1017 | *argv = NULL; |
| 1018 | return (kd->argv); |
| 1019 | } |
| 1020 | |
| 1021 | static void |
| 1022 | ps_str_a(struct ps_strings *p, u_long *addr, int *n) |
| 1023 | { |
| 1024 | |
| 1025 | *addr = (u_long)p->ps_argvstr; |
| 1026 | *n = p->ps_nargvstr; |
| 1027 | } |
| 1028 | |
| 1029 | static void |
| 1030 | ps_str_e(struct ps_strings *p, u_long *addr, int *n) |
| 1031 | { |
| 1032 | |
| 1033 | *addr = (u_long)p->ps_envstr; |
| 1034 | *n = p->ps_nenvstr; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Determine if the proc indicated by p is still active. |
| 1039 | * This test is not 100% foolproof in theory, but chances of |
| 1040 | * being wrong are very low. |
| 1041 | */ |
| 1042 | static int |
| 1043 | proc_verify(kvm_t *kd, u_long kernp, const struct miniproc *p) |
| 1044 | { |
| 1045 | struct proc kernproc; |
| 1046 | |
| 1047 | /* |
| 1048 | * Just read in the whole proc. It's not that big relative |
| 1049 | * to the cost of the read system call. |
| 1050 | */ |
| 1051 | if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) != |
| 1052 | sizeof(kernproc)) |
| 1053 | return (0); |
| 1054 | return (p->p_pid == kernproc.p_pid && |
| 1055 | (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); |
| 1056 | } |
| 1057 | |
| 1058 | static char ** |
| 1059 | kvm_doargv(kvm_t *kd, const struct miniproc *p, int nchr, |
| 1060 | void (*info)(struct ps_strings *, u_long *, int *)) |
| 1061 | { |
| 1062 | char **ap; |
| 1063 | u_long addr; |
| 1064 | int cnt; |
| 1065 | struct ps_strings arginfo; |
| 1066 | |
| 1067 | /* |
| 1068 | * Pointers are stored at the top of the user stack. |
| 1069 | */ |
| 1070 | if (p->p_stat == SZOMB) |
| 1071 | return (NULL); |
| 1072 | cnt = (int)kvm_ureadm(kd, p, kd->usrstack - sizeof(arginfo), |
| 1073 | (void *)&arginfo, sizeof(arginfo)); |
| 1074 | if (cnt != sizeof(arginfo)) |
| 1075 | return (NULL); |
| 1076 | |
| 1077 | (*info)(&arginfo, &addr, &cnt); |
| 1078 | if (cnt == 0) |
| 1079 | return (NULL); |
| 1080 | ap = kvm_argv(kd, p, addr, cnt, nchr); |
| 1081 | /* |
| 1082 | * For live kernels, make sure this process didn't go away. |
| 1083 | */ |
| 1084 | if (ap != NULL && ISALIVE(kd) && |
| 1085 | !proc_verify(kd, (u_long)p->p_paddr, p)) |
| 1086 | ap = NULL; |
| 1087 | return (ap); |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * Get the command args. This code is now machine independent. |
| 1092 | */ |
| 1093 | char ** |
| 1094 | kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) |
| 1095 | { |
| 1096 | struct miniproc p; |
| 1097 | |
| 1098 | KPTOMINI(kp, &p); |
| 1099 | return (kvm_doargv(kd, &p, nchr, ps_str_a)); |
| 1100 | } |
| 1101 | |
| 1102 | char ** |
| 1103 | kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) |
| 1104 | { |
| 1105 | struct miniproc p; |
| 1106 | |
| 1107 | KPTOMINI(kp, &p); |
| 1108 | return (kvm_doargv(kd, &p, nchr, ps_str_e)); |
| 1109 | } |
| 1110 | |
| 1111 | static char ** |
| 1112 | kvm_doargv2(kvm_t *kd, pid_t pid, int type, int nchr) |
| 1113 | { |
| 1114 | size_t bufs; |
| 1115 | int narg, mib[4]; |
| 1116 | size_t newargspc_len; |
| 1117 | char **ap, *bp, *endp; |
| 1118 | |
| 1119 | /* |
| 1120 | * Check that there aren't an unreasonable number of arguments. |
| 1121 | */ |
| 1122 | if (nchr > ARG_MAX) |
| 1123 | return (NULL); |
| 1124 | |
| 1125 | if (nchr == 0) |
| 1126 | nchr = ARG_MAX; |
| 1127 | |
| 1128 | /* Get number of strings in argv */ |
| 1129 | mib[0] = CTL_KERN; |
| 1130 | mib[1] = KERN_PROC_ARGS; |
| 1131 | mib[2] = pid; |
| 1132 | mib[3] = type == KERN_PROC_ARGV ? KERN_PROC_NARGV : KERN_PROC_NENV; |
| 1133 | bufs = sizeof(narg); |
| 1134 | if (sysctl(mib, 4, &narg, &bufs, NULL, (size_t)0) == -1) |
| 1135 | return (NULL); |
| 1136 | |
| 1137 | if (kd->argv == NULL) { |
| 1138 | /* |
| 1139 | * Try to avoid reallocs. |
| 1140 | */ |
| 1141 | kd->argc = MAX(narg + 1, 32); |
| 1142 | kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv)); |
| 1143 | if (kd->argv == NULL) |
| 1144 | return (NULL); |
| 1145 | } else if (narg + 1 > kd->argc) { |
| 1146 | kd->argc = MAX(2 * kd->argc, narg + 1); |
| 1147 | kd->argv = _kvm_realloc(kd, kd->argv, kd->argc * |
| 1148 | sizeof(*kd->argv)); |
| 1149 | if (kd->argv == NULL) |
| 1150 | return (NULL); |
| 1151 | } |
| 1152 | |
| 1153 | newargspc_len = MIN(nchr, ARG_MAX); |
| 1154 | KVM_ALLOC(kd, argspc, newargspc_len); |
| 1155 | memset(kd->argspc, 0, (size_t)kd->argspc_len); /* XXX necessary? */ |
| 1156 | |
| 1157 | mib[0] = CTL_KERN; |
| 1158 | mib[1] = KERN_PROC_ARGS; |
| 1159 | mib[2] = pid; |
| 1160 | mib[3] = type; |
| 1161 | bufs = kd->argspc_len; |
| 1162 | if (sysctl(mib, 4, kd->argspc, &bufs, NULL, (size_t)0) == -1) |
| 1163 | return (NULL); |
| 1164 | |
| 1165 | bp = kd->argspc; |
| 1166 | bp[kd->argspc_len-1] = '\0'; /* make sure the string ends with nul */ |
| 1167 | ap = kd->argv; |
| 1168 | endp = bp + MIN(nchr, bufs); |
| 1169 | |
| 1170 | while (bp < endp) { |
| 1171 | *ap++ = bp; |
| 1172 | /* |
| 1173 | * XXX: don't need following anymore, or stick check |
| 1174 | * for max argc in above while loop? |
| 1175 | */ |
| 1176 | if (ap >= kd->argv + kd->argc) { |
| 1177 | kd->argc *= 2; |
| 1178 | kd->argv = _kvm_realloc(kd, kd->argv, |
| 1179 | kd->argc * sizeof(*kd->argv)); |
| 1180 | ap = kd->argv; |
| 1181 | } |
| 1182 | bp += strlen(bp) + 1; |
| 1183 | } |
| 1184 | *ap = NULL; |
| 1185 | |
| 1186 | return (kd->argv); |
| 1187 | } |
| 1188 | |
| 1189 | char ** |
| 1190 | kvm_getargv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr) |
| 1191 | { |
| 1192 | |
| 1193 | return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ARGV, nchr)); |
| 1194 | } |
| 1195 | |
| 1196 | char ** |
| 1197 | kvm_getenvv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr) |
| 1198 | { |
| 1199 | |
| 1200 | return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ENV, nchr)); |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * Read from user space. The user context is given by p. |
| 1205 | */ |
| 1206 | static ssize_t |
| 1207 | kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long uva, |
| 1208 | char *buf, size_t len) |
| 1209 | { |
| 1210 | char *cp; |
| 1211 | |
| 1212 | cp = buf; |
| 1213 | while (len > 0) { |
| 1214 | size_t cc; |
| 1215 | char *dp; |
| 1216 | u_long cnt; |
| 1217 | |
| 1218 | dp = _kvm_ureadm(kd, p, uva, &cnt); |
| 1219 | if (dp == NULL) { |
| 1220 | _kvm_err(kd, 0, "invalid address (%lx)" , uva); |
| 1221 | return (0); |
| 1222 | } |
| 1223 | cc = (size_t)MIN(cnt, len); |
| 1224 | memcpy(cp, dp, cc); |
| 1225 | cp += cc; |
| 1226 | uva += cc; |
| 1227 | len -= cc; |
| 1228 | } |
| 1229 | return (ssize_t)(cp - buf); |
| 1230 | } |
| 1231 | |
| 1232 | ssize_t |
| 1233 | kvm_uread(kvm_t *kd, const struct proc *p, u_long uva, char *buf, size_t len) |
| 1234 | { |
| 1235 | struct miniproc mp; |
| 1236 | |
| 1237 | PTOMINI(p, &mp); |
| 1238 | return (kvm_ureadm(kd, &mp, uva, buf, len)); |
| 1239 | } |
| 1240 | |