1 /* $OpenBSD: kvm_proc.c,v 1.40 2010/01/10 03:37:50 guenther 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 <stdlib.h> 81 #include <string.h> 82 #include <unistd.h> 83 #include <nlist.h> 84 #include <kvm.h> 85 86 #include <uvm/uvm_extern.h> 87 #include <uvm/uvm_amap.h> 88 #include <machine/vmparam.h> 89 #include <machine/pmap.h> 90 91 #include <sys/sysctl.h> 92 93 #include <limits.h> 94 #include <db.h> 95 #include <paths.h> 96 97 #include "kvm_private.h" 98 99 /* 100 * Common info from kinfo_proc and kinfo_proc2 used by helper routines. 101 */ 102 struct miniproc { 103 struct vmspace *p_vmspace; 104 char p_stat; 105 struct proc *p_paddr; 106 pid_t p_pid; 107 }; 108 109 /* 110 * Convert from struct proc and kinfo_proc{,2} to miniproc. 111 */ 112 #define PTOMINI(kp, p) \ 113 do { \ 114 (p)->p_stat = (kp)->p_stat; \ 115 (p)->p_pid = (kp)->p_pid; \ 116 (p)->p_paddr = NULL; \ 117 (p)->p_vmspace = (kp)->p_vmspace; \ 118 } while (/*CONSTCOND*/0); 119 120 #define KPTOMINI(kp, p) \ 121 do { \ 122 (p)->p_stat = (kp)->kp_proc.p_stat; \ 123 (p)->p_pid = (kp)->kp_proc.p_pid; \ 124 (p)->p_paddr = (kp)->kp_eproc.e_paddr; \ 125 (p)->p_vmspace = (kp)->kp_proc.p_vmspace; \ 126 } while (/*CONSTCOND*/0); 127 128 #define KP2TOMINI(kp, p) \ 129 do { \ 130 (p)->p_stat = (kp)->p_stat; \ 131 (p)->p_pid = (kp)->p_pid; \ 132 (p)->p_paddr = (void *)(long)(kp)->p_paddr; \ 133 (p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \ 134 } while (/*CONSTCOND*/0); 135 136 137 ssize_t kvm_uread(kvm_t *, const struct proc *, u_long, char *, size_t); 138 139 static char *_kvm_ureadm(kvm_t *, const struct miniproc *, u_long, u_long *); 140 static ssize_t kvm_ureadm(kvm_t *, const struct miniproc *, u_long, char *, size_t); 141 142 static char **kvm_argv(kvm_t *, const struct miniproc *, u_long, int, int); 143 144 static int kvm_deadprocs(kvm_t *, int, int, u_long, u_long, int); 145 static char **kvm_doargv(kvm_t *, const struct miniproc *, int, 146 void (*)(struct ps_strings *, u_long *, int *)); 147 static int kvm_proclist(kvm_t *, int, int, struct proc *, 148 struct kinfo_proc *, int); 149 static int proc_verify(kvm_t *, const struct miniproc *); 150 static void ps_str_a(struct ps_strings *, u_long *, int *); 151 static void ps_str_e(struct ps_strings *, u_long *, int *); 152 153 static char * 154 _kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long va, u_long *cnt) 155 { 156 u_long addr, head, offset, slot; 157 struct vm_anon *anonp, anon; 158 struct vm_map_entry vme; 159 struct vm_amap amap; 160 struct vm_page pg; 161 162 if (kd->swapspc == 0) { 163 kd->swapspc = _kvm_malloc(kd, kd->nbpg); 164 if (kd->swapspc == 0) 165 return (0); 166 } 167 168 /* 169 * Look through the address map for the memory object 170 * that corresponds to the given virtual address. 171 * The header just has the entire valid range. 172 */ 173 head = (u_long)&p->p_vmspace->vm_map.header; 174 addr = head; 175 while (1) { 176 if (KREAD(kd, addr, &vme)) 177 return (0); 178 179 if (va >= vme.start && va < vme.end && 180 vme.aref.ar_amap != NULL) 181 break; 182 183 addr = (u_long)vme.next; 184 if (addr == head) 185 return (0); 186 } 187 188 /* 189 * we found the map entry, now to find the object... 190 */ 191 if (vme.aref.ar_amap == NULL) 192 return (NULL); 193 194 addr = (u_long)vme.aref.ar_amap; 195 if (KREAD(kd, addr, &amap)) 196 return (NULL); 197 198 offset = va - vme.start; 199 slot = offset / kd->nbpg + vme.aref.ar_pageoff; 200 /* sanity-check slot number */ 201 if (slot > amap.am_nslot) 202 return (NULL); 203 204 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp); 205 if (KREAD(kd, addr, &anonp)) 206 return (NULL); 207 208 addr = (u_long)anonp; 209 if (KREAD(kd, addr, &anon)) 210 return (NULL); 211 212 addr = (u_long)anon.an_page; 213 if (addr) { 214 if (KREAD(kd, addr, &pg)) 215 return (NULL); 216 217 if (_kvm_pread(kd, kd->pmfd, (void *)kd->swapspc, 218 (size_t)kd->nbpg, (off_t)pg.phys_addr) != kd->nbpg) 219 return (NULL); 220 } else { 221 if (kd->swfd == -1 || 222 _kvm_pread(kd, kd->swfd, (void *)kd->swapspc, 223 (size_t)kd->nbpg, 224 (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg) 225 return (NULL); 226 } 227 228 /* Found the page. */ 229 offset %= kd->nbpg; 230 *cnt = kd->nbpg - offset; 231 return (&kd->swapspc[offset]); 232 } 233 234 char * 235 _kvm_uread(kvm_t *kd, const struct proc *p, u_long va, u_long *cnt) 236 { 237 struct miniproc mp; 238 239 PTOMINI(p, &mp); 240 return (_kvm_ureadm(kd, &mp, va, cnt)); 241 } 242 243 /* 244 * Read proc's from memory file into buffer bp, which has space to hold 245 * at most maxcnt procs. 246 */ 247 static int 248 kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, 249 struct kinfo_proc *bp, int maxcnt) 250 { 251 struct session sess; 252 struct eproc eproc; 253 struct proc proc; 254 struct process process; 255 struct pgrp pgrp; 256 struct tty tty; 257 int cnt = 0; 258 259 for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 260 if (KREAD(kd, (u_long)p, &proc)) { 261 _kvm_err(kd, kd->program, "can't read proc at %x", p); 262 return (-1); 263 } 264 if (KREAD(kd, (u_long)proc.p_p, &process)) { 265 _kvm_err(kd, kd->program, "can't read process at %x", proc.p_p); 266 return (-1); 267 } 268 if (KREAD(kd, (u_long)process.ps_cred, &eproc.e_pcred) == 0) 269 KREAD(kd, (u_long)eproc.e_pcred.pc_ucred, 270 &eproc.e_ucred); 271 272 switch (what) { 273 case KERN_PROC_PID: 274 if (proc.p_pid != (pid_t)arg) 275 continue; 276 break; 277 278 case KERN_PROC_UID: 279 if (eproc.e_ucred.cr_uid != (uid_t)arg) 280 continue; 281 break; 282 283 case KERN_PROC_RUID: 284 if (eproc.e_pcred.p_ruid != (uid_t)arg) 285 continue; 286 break; 287 288 case KERN_PROC_ALL: 289 if (proc.p_flag & P_SYSTEM) 290 continue; 291 break; 292 } 293 /* 294 * We're going to add another proc to the set. If this 295 * will overflow the buffer, assume the reason is because 296 * nprocs (or the proc list) is corrupt and declare an error. 297 */ 298 if (cnt >= maxcnt) { 299 _kvm_err(kd, kd->program, "nprocs corrupt"); 300 return (-1); 301 } 302 /* 303 * gather eproc 304 */ 305 eproc.e_paddr = p; 306 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 307 _kvm_err(kd, kd->program, "can't read pgrp at %x", 308 proc.p_pgrp); 309 return (-1); 310 } 311 eproc.e_sess = pgrp.pg_session; 312 eproc.e_pgid = pgrp.pg_id; 313 eproc.e_jobc = pgrp.pg_jobc; 314 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 315 _kvm_err(kd, kd->program, "can't read session at %x", 316 pgrp.pg_session); 317 return (-1); 318 } 319 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 320 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 321 _kvm_err(kd, kd->program, 322 "can't read tty at %x", sess.s_ttyp); 323 return (-1); 324 } 325 eproc.e_tdev = tty.t_dev; 326 eproc.e_tsess = tty.t_session; 327 if (tty.t_pgrp != NULL) { 328 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 329 _kvm_err(kd, kd->program, 330 "can't read tpgrp at &x", 331 tty.t_pgrp); 332 return (-1); 333 } 334 eproc.e_tpgid = pgrp.pg_id; 335 } else 336 eproc.e_tpgid = -1; 337 } else 338 eproc.e_tdev = NODEV; 339 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; 340 if (sess.s_leader == p) 341 eproc.e_flag |= EPROC_SLEADER; 342 if (proc.p_wmesg) 343 (void)kvm_read(kd, (u_long)proc.p_wmesg, 344 eproc.e_wmesg, WMESGLEN); 345 346 (void)kvm_read(kd, (u_long)proc.p_vmspace, 347 &eproc.e_vm, sizeof(eproc.e_vm)); 348 349 eproc.e_xsize = eproc.e_xrssize = 0; 350 eproc.e_xccount = eproc.e_xswrss = 0; 351 352 switch (what) { 353 case KERN_PROC_PGRP: 354 if (eproc.e_pgid != (pid_t)arg) 355 continue; 356 break; 357 358 case KERN_PROC_TTY: 359 if ((proc.p_flag & P_CONTROLT) == 0 || 360 eproc.e_tdev != (dev_t)arg) 361 continue; 362 break; 363 } 364 bcopy(&proc, &bp->kp_proc, sizeof(proc)); 365 bcopy(&eproc, &bp->kp_eproc, sizeof(eproc)); 366 ++bp; 367 ++cnt; 368 } 369 return (cnt); 370 } 371 372 /* 373 * Build proc info array by reading in proc list from a crash dump. 374 * Return number of procs read. maxcnt is the max we will read. 375 */ 376 static int 377 kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, 378 u_long a_zombproc, int maxcnt) 379 { 380 struct kinfo_proc *bp = kd->procbase; 381 struct proc *p; 382 int acnt, zcnt; 383 384 if (KREAD(kd, a_allproc, &p)) { 385 _kvm_err(kd, kd->program, "cannot read allproc"); 386 return (-1); 387 } 388 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 389 if (acnt < 0) 390 return (acnt); 391 392 if (KREAD(kd, a_zombproc, &p)) { 393 _kvm_err(kd, kd->program, "cannot read zombproc"); 394 return (-1); 395 } 396 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 397 if (zcnt < 0) 398 zcnt = 0; 399 400 return (acnt + zcnt); 401 } 402 403 struct kinfo_proc * 404 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt) 405 { 406 int mib[4], st, nprocs; 407 size_t size; 408 409 if (kd->procbase != 0) { 410 free((void *)kd->procbase); 411 /* 412 * Clear this pointer in case this call fails. Otherwise, 413 * kvm_close() will free it again. 414 */ 415 kd->procbase = 0; 416 } 417 if (ISALIVE(kd)) { 418 size = 0; 419 mib[0] = CTL_KERN; 420 mib[1] = KERN_PROC; 421 mib[2] = op; 422 mib[3] = arg; 423 st = sysctl(mib, 4, NULL, &size, NULL, 0); 424 if (st == -1) { 425 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 426 return (0); 427 } 428 kd->procbase = _kvm_malloc(kd, size); 429 if (kd->procbase == 0) 430 return (0); 431 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0); 432 if (st == -1) { 433 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 434 return (0); 435 } 436 if (size % sizeof(struct kinfo_proc) != 0) { 437 _kvm_err(kd, kd->program, 438 "proc size mismatch (%d total, %d chunks)", 439 size, sizeof(struct kinfo_proc)); 440 return (0); 441 } 442 nprocs = size / sizeof(struct kinfo_proc); 443 } else { 444 struct nlist nl[4], *p; 445 446 memset(nl, 0, sizeof(nl)); 447 nl[0].n_name = "_nprocs"; 448 nl[1].n_name = "_allproc"; 449 nl[2].n_name = "_zombproc"; 450 nl[3].n_name = NULL; 451 452 if (kvm_nlist(kd, nl) != 0) { 453 for (p = nl; p->n_type != 0; ++p) 454 ; 455 _kvm_err(kd, kd->program, 456 "%s: no such symbol", p->n_name); 457 return (0); 458 } 459 if (KREAD(kd, nl[0].n_value, &nprocs)) { 460 _kvm_err(kd, kd->program, "can't read nprocs"); 461 return (0); 462 } 463 size = nprocs * sizeof(struct kinfo_proc); 464 kd->procbase = _kvm_malloc(kd, size); 465 if (kd->procbase == 0) 466 return (0); 467 468 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 469 nl[2].n_value, nprocs); 470 #ifdef notdef 471 size = nprocs * sizeof(struct kinfo_proc); 472 (void)realloc(kd->procbase, size); 473 #endif 474 } 475 *cnt = nprocs; 476 return (kd->procbase); 477 } 478 479 void 480 _kvm_freeprocs(kvm_t *kd) 481 { 482 if (kd->procbase) { 483 free(kd->procbase); 484 kd->procbase = 0; 485 } 486 } 487 488 void * 489 _kvm_realloc(kvm_t *kd, void *p, size_t n) 490 { 491 void *np = (void *)realloc(p, n); 492 493 if (np == 0) 494 _kvm_err(kd, kd->program, "out of memory"); 495 return (np); 496 } 497 498 /* 499 * Read in an argument vector from the user address space of process p. 500 * addr if the user-space base address of narg null-terminated contiguous 501 * strings. This is used to read in both the command arguments and 502 * environment strings. Read at most maxcnt characters of strings. 503 */ 504 static char ** 505 kvm_argv(kvm_t *kd, const struct miniproc *p, u_long addr, int narg, 506 int maxcnt) 507 { 508 char *np, *cp, *ep, *ap, **argv; 509 u_long oaddr = -1; 510 int len, cc; 511 512 /* 513 * Check that there aren't an unreasonable number of agruments, 514 * and that the address is in user space. 515 */ 516 if (narg > ARG_MAX || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) 517 return (0); 518 519 if (kd->argv == 0) { 520 /* 521 * Try to avoid reallocs. 522 */ 523 kd->argc = MAX(narg + 1, 32); 524 kd->argv = _kvm_malloc(kd, kd->argc * 525 sizeof(*kd->argv)); 526 if (kd->argv == 0) 527 return (0); 528 } else if (narg + 1 > kd->argc) { 529 kd->argc = MAX(2 * kd->argc, narg + 1); 530 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 531 sizeof(*kd->argv)); 532 if (kd->argv == 0) 533 return (0); 534 } 535 if (kd->argspc == 0) { 536 kd->argspc = _kvm_malloc(kd, kd->nbpg); 537 if (kd->argspc == 0) 538 return (0); 539 kd->arglen = kd->nbpg; 540 } 541 if (kd->argbuf == 0) { 542 kd->argbuf = _kvm_malloc(kd, kd->nbpg); 543 if (kd->argbuf == 0) 544 return (0); 545 } 546 cc = sizeof(char *) * narg; 547 if (kvm_ureadm(kd, p, addr, (char *)kd->argv, cc) != cc) 548 return (0); 549 ap = np = kd->argspc; 550 argv = kd->argv; 551 len = 0; 552 553 /* 554 * Loop over pages, filling in the argument vector. 555 */ 556 while (argv < kd->argv + narg && *argv != 0) { 557 addr = (u_long)*argv & ~(kd->nbpg - 1); 558 if (addr != oaddr) { 559 if (kvm_ureadm(kd, p, addr, kd->argbuf, kd->nbpg) != 560 kd->nbpg) 561 return (0); 562 oaddr = addr; 563 } 564 addr = (u_long)*argv & (kd->nbpg - 1); 565 cp = kd->argbuf + addr; 566 cc = kd->nbpg - addr; 567 if (maxcnt > 0 && cc > maxcnt - len) 568 cc = maxcnt - len; 569 ep = memchr(cp, '\0', cc); 570 if (ep != 0) 571 cc = ep - cp + 1; 572 if (len + cc > kd->arglen) { 573 int off; 574 char **pp; 575 char *op = kd->argspc; 576 577 kd->arglen *= 2; 578 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 579 kd->arglen); 580 if (kd->argspc == 0) 581 return (0); 582 /* 583 * Adjust argv pointers in case realloc moved 584 * the string space. 585 */ 586 off = kd->argspc - op; 587 for (pp = kd->argv; pp < argv; pp++) 588 *pp += off; 589 ap += off; 590 np += off; 591 } 592 memcpy(np, cp, cc); 593 np += cc; 594 len += cc; 595 if (ep != 0) { 596 *argv++ = ap; 597 ap = np; 598 } else 599 *argv += cc; 600 if (maxcnt > 0 && len >= maxcnt) { 601 /* 602 * We're stopping prematurely. Terminate the 603 * current string. 604 */ 605 if (ep == 0) { 606 *np = '\0'; 607 *argv++ = ap; 608 } 609 break; 610 } 611 } 612 /* Make sure argv is terminated. */ 613 *argv = 0; 614 return (kd->argv); 615 } 616 617 static void 618 ps_str_a(struct ps_strings *p, u_long *addr, int *n) 619 { 620 *addr = (u_long)p->ps_argvstr; 621 *n = p->ps_nargvstr; 622 } 623 624 static void 625 ps_str_e(struct ps_strings *p, u_long *addr, int *n) 626 { 627 *addr = (u_long)p->ps_envstr; 628 *n = p->ps_nenvstr; 629 } 630 631 /* 632 * Determine if the proc indicated by p is still active. 633 * This test is not 100% foolproof in theory, but chances of 634 * being wrong are very low. 635 */ 636 static int 637 proc_verify(kvm_t *kd, const struct miniproc *p) 638 { 639 struct proc kernproc; 640 641 /* 642 * Just read in the whole proc. It's not that big relative 643 * to the cost of the read system call. 644 */ 645 if (kvm_read(kd, (u_long)p->p_paddr, &kernproc, sizeof(kernproc)) != 646 sizeof(kernproc)) 647 return (0); 648 return (p->p_pid == kernproc.p_pid && 649 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB)); 650 } 651 652 static char ** 653 kvm_doargv(kvm_t *kd, const struct miniproc *p, int nchr, 654 void (*info)(struct ps_strings *, u_long *, int *)) 655 { 656 static struct ps_strings *ps; 657 struct ps_strings arginfo; 658 u_long addr; 659 char **ap; 660 int cnt; 661 662 if (ps == NULL) { 663 struct _ps_strings _ps; 664 int mib[2]; 665 size_t len; 666 667 mib[0] = CTL_VM; 668 mib[1] = VM_PSSTRINGS; 669 len = sizeof(_ps); 670 sysctl(mib, 2, &_ps, &len, NULL, 0); 671 ps = (struct ps_strings *)_ps.val; 672 } 673 674 /* 675 * Pointers are stored at the top of the user stack. 676 */ 677 if (p->p_stat == SZOMB || 678 kvm_ureadm(kd, p, (u_long)ps, (char *)&arginfo, 679 sizeof(arginfo)) != sizeof(arginfo)) 680 return (0); 681 682 (*info)(&arginfo, &addr, &cnt); 683 if (cnt == 0) 684 return (0); 685 ap = kvm_argv(kd, p, addr, cnt, nchr); 686 /* 687 * For live kernels, make sure this process didn't go away. 688 */ 689 if (ap != 0 && ISALIVE(kd) && !proc_verify(kd, p)) 690 ap = 0; 691 return (ap); 692 } 693 694 static char ** 695 kvm_arg_sysctl(kvm_t *kd, pid_t pid, int nchr, int env) 696 { 697 size_t len, orglen; 698 int mib[4], ret; 699 char *buf; 700 701 orglen = env ? kd->nbpg : 8 * kd->nbpg; /* XXX - should be ARG_MAX */ 702 if (kd->argbuf == NULL && 703 (kd->argbuf = _kvm_malloc(kd, orglen)) == NULL) 704 return (NULL); 705 706 again: 707 mib[0] = CTL_KERN; 708 mib[1] = KERN_PROC_ARGS; 709 mib[2] = (int)pid; 710 mib[3] = env ? KERN_PROC_ENV : KERN_PROC_ARGV; 711 712 len = orglen; 713 ret = (sysctl(mib, 4, kd->argbuf, &len, NULL, 0) < 0); 714 if (ret && errno == ENOMEM) { 715 orglen *= 2; 716 buf = _kvm_realloc(kd, kd->argbuf, orglen); 717 if (buf == NULL) 718 return (NULL); 719 kd->argbuf = buf; 720 goto again; 721 } 722 723 if (ret) { 724 free(kd->argbuf); 725 kd->argbuf = NULL; 726 _kvm_syserr(kd, kd->program, "kvm_arg_sysctl"); 727 return (NULL); 728 } 729 #if 0 730 for (argv = (char **)kd->argbuf; *argv != NULL; argv++) 731 if (strlen(*argv) > nchr) 732 *argv[nchr] = '\0'; 733 #endif 734 735 return (char **)(kd->argbuf); 736 } 737 738 /* 739 * Get the command args. This code is now machine independent. 740 */ 741 char ** 742 kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 743 { 744 struct miniproc p; 745 746 if (ISALIVE(kd)) 747 return (kvm_arg_sysctl(kd, kp->kp_proc.p_pid, nchr, 0)); 748 KPTOMINI(kp, &p); 749 return (kvm_doargv(kd, &p, nchr, ps_str_a)); 750 } 751 752 char ** 753 kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 754 { 755 struct miniproc p; 756 757 if (ISALIVE(kd)) 758 return (kvm_arg_sysctl(kd, kp->kp_proc.p_pid, nchr, 1)); 759 KPTOMINI(kp, &p); 760 return (kvm_doargv(kd, &p, nchr, ps_str_e)); 761 } 762 763 char ** 764 kvm_getargv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr) 765 { 766 struct miniproc p; 767 768 if (ISALIVE(kd)) 769 return (kvm_arg_sysctl(kd, kp->p_pid, nchr, 0)); 770 KP2TOMINI(kp, &p); 771 return (kvm_doargv(kd, &p, nchr, ps_str_a)); 772 } 773 774 char ** 775 kvm_getenvv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr) 776 { 777 struct miniproc p; 778 779 if (ISALIVE(kd)) 780 return (kvm_arg_sysctl(kd, kp->p_pid, nchr, 1)); 781 KP2TOMINI(kp, &p); 782 return (kvm_doargv(kd, &p, nchr, ps_str_e)); 783 } 784 785 /* 786 * Read from user space. The user context is given by p. 787 */ 788 static ssize_t 789 kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long uva, char *buf, 790 size_t len) 791 { 792 char *cp = buf; 793 794 while (len > 0) { 795 u_long cnt; 796 size_t cc; 797 char *dp; 798 799 dp = _kvm_ureadm(kd, p, uva, &cnt); 800 if (dp == 0) { 801 _kvm_err(kd, 0, "invalid address (%lx)", uva); 802 return (0); 803 } 804 cc = (size_t)MIN(cnt, len); 805 bcopy(dp, cp, cc); 806 cp += cc; 807 uva += cc; 808 len -= cc; 809 } 810 return (ssize_t)(cp - buf); 811 } 812 813 ssize_t 814 kvm_uread(kvm_t *kd, const struct proc *p, u_long uva, char *buf, 815 size_t len) 816 { 817 struct miniproc mp; 818 819 PTOMINI(p, &mp); 820 return (kvm_ureadm(kd, &mp, uva, buf, len)); 821 } 822