kvm_proc.c revision 134791
175584Sru/*- 2151497Sru * Copyright (c) 1989, 1992, 1993 375584Sru * The Regents of the University of California. All rights reserved. 4104862Sru * 5104862Sru * This code is derived from software developed by the Computer Systems 6104862Sru * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7104862Sru * BG 91-66 and contributed to Berkeley. 8104862Sru * 9104862Sru * Redistribution and use in source and binary forms, with or without 10104862Sru * modification, are permitted provided that the following conditions 11104862Sru * are met: 12104862Sru * 1. Redistributions of source code must retain the above copyright 13104862Sru * notice, this list of conditions and the following disclaimer. 14104862Sru * 2. Redistributions in binary form must reproduce the above copyright 15104862Sru * notice, this list of conditions and the following disclaimer in the 16104862Sru * documentation and/or other materials provided with the distribution. 17104862Sru * 3. All advertising materials mentioning features or use of this software 18104862Sru * must display the following acknowledgement: 19151497Sru * This product includes software developed by the University of 20104862Sru * California, Berkeley and its contributors. 21151497Sru * 4. Neither the name of the University nor the names of its contributors 22151497Sru * may be used to endorse or promote products derived from this software 23104862Sru * without specific prior written permission. 24151497Sru * 25104862Sru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26104862Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27104862Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28151497Sru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29104862Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30151497Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31104862Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32104862Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33104862Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34104862Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35104862Sru * SUCH DAMAGE. 36104862Sru */ 37104862Sru 38151497Sru#if 0 39151497Sru#if defined(LIBC_SCCS) && !defined(lint) 40104862Srustatic char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; 41151497Sru#endif /* LIBC_SCCS and not lint */ 42104862Sru#endif 43104862Sru 44151497Sru#include <sys/cdefs.h> 45104862Sru__FBSDID("$FreeBSD: head/lib/libkvm/kvm_proc.c 134791 2004-09-05 02:09:54Z julian $"); 46104862Sru 47151497Sru/* 48151497Sru * Proc traversal interface for kvm. ps and w are (probably) the exclusive 49151497Sru * users of this code, so we've factored it out into a separate module. 50104862Sru * Thus, we keep this grunge out of the other kvm applications (i.e., 5175584Sru * most other applications are interested only in open/close/read/nlist). 5275584Sru */ 53114402Sru 54114402Sru#include <sys/param.h> 55114402Sru#define _WANT_UCRED /* make ucred.h give us 'struct ucred' */ 56114402Sru#include <sys/ucred.h> 57114402Sru#include <sys/user.h> 58114402Sru#include <sys/proc.h> 59114402Sru#include <sys/exec.h> 60114402Sru#include <sys/stat.h> 61114402Sru#include <sys/sysent.h> 62114402Sru#include <sys/ioctl.h> 63114402Sru#include <sys/tty.h> 64114402Sru#include <sys/file.h> 65114402Sru#include <stdio.h> 66151497Sru#include <stdlib.h> 67114402Sru#include <unistd.h> 68151497Sru#include <nlist.h> 69114402Sru#include <kvm.h> 70114402Sru 71114402Sru#include <vm/vm.h> 72114402Sru#include <vm/vm_param.h> 73114402Sru 74151497Sru#include <sys/sysctl.h> 75114402Sru 76114402Sru#include <limits.h> 77114402Sru#include <memory.h> 78114402Sru#include <paths.h> 79114402Sru 80114402Sru#include "kvm_private.h" 81114402Sru 82151497Sru#define KREAD(kd, addr, obj) \ 83114402Sru (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) 84114402Sru 85114402Sru/* 86151497Sru * Read proc's from memory file into buffer bp, which has space to hold 87114402Sru * at most maxcnt procs. 88114402Sru */ 89151497Srustatic int 90151497Srukvm_proclist(kd, what, arg, p, bp, maxcnt) 91151497Sru kvm_t *kd; 92114402Sru int what, arg; 9375584Sru struct proc *p; 9475584Sru struct kinfo_proc *bp; 9575584Sru int maxcnt; 9675584Sru{ 9775584Sru int cnt = 0; 9875584Sru struct kinfo_proc kinfo_proc, *kp; 9975584Sru struct pgrp pgrp; 10075584Sru struct session sess; 101151497Sru struct tty tty; 10275584Sru struct vmspace vmspace; 103151497Sru struct sigacts sigacts; 104151497Sru struct pstats pstats; 105114402Sru struct ucred ucred; 10675584Sru struct thread mtd; 107114402Sru /*struct kse mke;*/ 108104862Sru struct ksegrp mkg; 109104862Sru struct proc proc; 11075584Sru struct proc pproc; 111114402Sru struct timeval tv; 11275584Sru struct sysentvec sysent; 113104862Sru char svname[KI_EMULNAMELEN]; 114104862Sru 11575584Sru kp = &kinfo_proc; 116114402Sru kp->ki_structsize = sizeof(kinfo_proc); 117114402Sru for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 118104862Sru memset(kp, 0, sizeof *kp); 119104862Sru if (KREAD(kd, (u_long)p, &proc)) { 120104862Sru _kvm_err(kd, kd->program, "can't read proc at %x", p); 12175584Sru return (-1); 122104862Sru } 123104862Sru if (proc.p_state != PRS_ZOMBIE) { 124104862Sru if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads), 125104862Sru &mtd)) { 126104862Sru _kvm_err(kd, kd->program, 127 "can't read thread at %x", 128 TAILQ_FIRST(&proc.p_threads)); 129 return (-1); 130 } 131 if ((proc.p_flag & P_SA) == 0) { 132 if (KREAD(kd, 133 (u_long)TAILQ_FIRST(&proc.p_ksegrps), 134 &mkg)) { 135 _kvm_err(kd, kd->program, 136 "can't read ksegrp at %x", 137 TAILQ_FIRST(&proc.p_ksegrps)); 138 return (-1); 139 } 140#if 0 141 if (KREAD(kd, 142 (u_long)TAILQ_FIRST(&mkg.kg_kseq), &mke)) { 143 _kvm_err(kd, kd->program, 144 "can't read kse at %x", 145 TAILQ_FIRST(&mkg.kg_kseq)); 146 return (-1); 147 } 148#endif 149 } 150 } 151 if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) { 152 kp->ki_ruid = ucred.cr_ruid; 153 kp->ki_svuid = ucred.cr_svuid; 154 kp->ki_rgid = ucred.cr_rgid; 155 kp->ki_svgid = ucred.cr_svgid; 156 kp->ki_ngroups = ucred.cr_ngroups; 157 bcopy(ucred.cr_groups, kp->ki_groups, 158 NGROUPS * sizeof(gid_t)); 159 kp->ki_uid = ucred.cr_uid; 160 } 161 162 switch(what & ~KERN_PROC_INC_THREAD) { 163 164 case KERN_PROC_GID: 165 if (kp->ki_groups[0] != (gid_t)arg) 166 continue; 167 break; 168 169 case KERN_PROC_PID: 170 if (proc.p_pid != (pid_t)arg) 171 continue; 172 break; 173 174 case KERN_PROC_RGID: 175 if (kp->ki_rgid != (gid_t)arg) 176 continue; 177 break; 178 179 case KERN_PROC_UID: 180 if (kp->ki_uid != (uid_t)arg) 181 continue; 182 break; 183 184 case KERN_PROC_RUID: 185 if (kp->ki_ruid != (uid_t)arg) 186 continue; 187 break; 188 } 189 /* 190 * We're going to add another proc to the set. If this 191 * will overflow the buffer, assume the reason is because 192 * nprocs (or the proc list) is corrupt and declare an error. 193 */ 194 if (cnt >= maxcnt) { 195 _kvm_err(kd, kd->program, "nprocs corrupt"); 196 return (-1); 197 } 198 /* 199 * gather kinfo_proc 200 */ 201 kp->ki_paddr = p; 202 kp->ki_addr = proc.p_uarea; 203 /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ 204 kp->ki_args = proc.p_args; 205 kp->ki_tracep = proc.p_tracevp; 206 kp->ki_textvp = proc.p_textvp; 207 kp->ki_fd = proc.p_fd; 208 kp->ki_vmspace = proc.p_vmspace; 209 if (proc.p_sigacts != NULL) { 210 if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) { 211 _kvm_err(kd, kd->program, 212 "can't read sigacts at %x", proc.p_sigacts); 213 return (-1); 214 } 215 kp->ki_sigignore = sigacts.ps_sigignore; 216 kp->ki_sigcatch = sigacts.ps_sigcatch; 217 } 218 if ((proc.p_sflag & PS_INMEM) && proc.p_stats != NULL) { 219 if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { 220 _kvm_err(kd, kd->program, 221 "can't read stats at %x", proc.p_stats); 222 return (-1); 223 } 224 kp->ki_start = pstats.p_start; 225 kp->ki_rusage = pstats.p_ru; 226 kp->ki_childstime = pstats.p_cru.ru_stime; 227 kp->ki_childutime = pstats.p_cru.ru_utime; 228 /* Some callers want child-times in a single value */ 229 timeradd(&kp->ki_childstime, &kp->ki_childutime, 230 &kp->ki_childtime); 231 } 232 if (proc.p_oppid) 233 kp->ki_ppid = proc.p_oppid; 234 else if (proc.p_pptr) { 235 if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { 236 _kvm_err(kd, kd->program, 237 "can't read pproc at %x", proc.p_pptr); 238 return (-1); 239 } 240 kp->ki_ppid = pproc.p_pid; 241 } else 242 kp->ki_ppid = 0; 243 if (proc.p_pgrp == NULL) 244 goto nopgrp; 245 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 246 _kvm_err(kd, kd->program, "can't read pgrp at %x", 247 proc.p_pgrp); 248 return (-1); 249 } 250 kp->ki_pgid = pgrp.pg_id; 251 kp->ki_jobc = pgrp.pg_jobc; 252 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 253 _kvm_err(kd, kd->program, "can't read session at %x", 254 pgrp.pg_session); 255 return (-1); 256 } 257 kp->ki_sid = sess.s_sid; 258 (void)memcpy(kp->ki_login, sess.s_login, 259 sizeof(kp->ki_login)); 260 kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; 261 if (sess.s_leader == p) 262 kp->ki_kiflag |= KI_SLEADER; 263 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 264 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 265 _kvm_err(kd, kd->program, 266 "can't read tty at %x", sess.s_ttyp); 267 return (-1); 268 } 269 kp->ki_tdev = tty.t_dev; /* XXX: wrong */ 270 if (tty.t_pgrp != NULL) { 271 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 272 _kvm_err(kd, kd->program, 273 "can't read tpgrp at %x", 274 tty.t_pgrp); 275 return (-1); 276 } 277 kp->ki_tpgid = pgrp.pg_id; 278 } else 279 kp->ki_tpgid = -1; 280 if (tty.t_session != NULL) { 281 if (KREAD(kd, (u_long)tty.t_session, &sess)) { 282 _kvm_err(kd, kd->program, 283 "can't read session at %x", 284 tty.t_session); 285 return (-1); 286 } 287 kp->ki_tsid = sess.s_sid; 288 } 289 } else { 290nopgrp: 291 kp->ki_tdev = NODEV; 292 } 293 if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) 294 (void)kvm_read(kd, (u_long)mtd.td_wmesg, 295 kp->ki_wmesg, WMESGLEN); 296 297 (void)kvm_read(kd, (u_long)proc.p_vmspace, 298 (char *)&vmspace, sizeof(vmspace)); 299 kp->ki_size = vmspace.vm_map.size; 300 kp->ki_rssize = vmspace.vm_swrss; /* XXX */ 301 kp->ki_swrss = vmspace.vm_swrss; 302 kp->ki_tsize = vmspace.vm_tsize; 303 kp->ki_dsize = vmspace.vm_dsize; 304 kp->ki_ssize = vmspace.vm_ssize; 305 306 switch (what & ~KERN_PROC_INC_THREAD) { 307 308 case KERN_PROC_PGRP: 309 if (kp->ki_pgid != (pid_t)arg) 310 continue; 311 break; 312 313 case KERN_PROC_SESSION: 314 if (kp->ki_sid != (pid_t)arg) 315 continue; 316 break; 317 318 case KERN_PROC_TTY: 319 if ((proc.p_flag & P_CONTROLT) == 0 || 320 kp->ki_tdev != (dev_t)arg) 321 continue; 322 break; 323 } 324 if (proc.p_comm[0] != 0) 325 strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); 326 (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent, 327 sizeof(sysent)); 328 (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname, 329 sizeof(svname)); 330 if (svname[0] != 0) 331 strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN); 332 if ((proc.p_state != PRS_ZOMBIE) && 333 (mtd.td_blocked != 0)) { 334 kp->ki_kiflag |= KI_LOCKBLOCK; 335 if (mtd.td_lockname) 336 (void)kvm_read(kd, 337 (u_long)mtd.td_lockname, 338 kp->ki_lockname, LOCKNAMELEN); 339 kp->ki_lockname[LOCKNAMELEN] = 0; 340 } 341 bintime2timeval(&proc.p_runtime, &tv); 342 kp->ki_runtime = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; 343 kp->ki_pid = proc.p_pid; 344 kp->ki_siglist = proc.p_siglist; 345 SIGSETOR(kp->ki_siglist, mtd.td_siglist); 346 kp->ki_sigmask = mtd.td_sigmask; 347 kp->ki_xstat = proc.p_xstat; 348 kp->ki_acflag = proc.p_acflag; 349 kp->ki_lock = proc.p_lock; 350 if (proc.p_state != PRS_ZOMBIE) { 351 kp->ki_swtime = proc.p_swtime; 352 kp->ki_flag = proc.p_flag; 353 kp->ki_sflag = proc.p_sflag; 354 kp->ki_nice = proc.p_nice; 355 kp->ki_traceflag = proc.p_traceflag; 356 if (proc.p_state == PRS_NORMAL) { 357 if (TD_ON_RUNQ(&mtd) || 358 TD_CAN_RUN(&mtd) || 359 TD_IS_RUNNING(&mtd)) { 360 kp->ki_stat = SRUN; 361 } else if (mtd.td_state == 362 TDS_INHIBITED) { 363 if (P_SHOULDSTOP(&proc)) { 364 kp->ki_stat = SSTOP; 365 } else if ( 366 TD_IS_SLEEPING(&mtd)) { 367 kp->ki_stat = SSLEEP; 368 } else if (TD_ON_LOCK(&mtd)) { 369 kp->ki_stat = SLOCK; 370 } else { 371 kp->ki_stat = SWAIT; 372 } 373 } 374 } else { 375 kp->ki_stat = SIDL; 376 } 377 /* Stuff from the thread */ 378 kp->ki_pri.pri_level = mtd.td_priority; 379 kp->ki_pri.pri_native = mtd.td_base_pri; 380 kp->ki_lastcpu = mtd.td_lastcpu; 381 kp->ki_wchan = mtd.td_wchan; 382 kp->ki_oncpu = mtd.td_oncpu; 383 384 if (!(proc.p_flag & P_SA)) { 385 /* stuff from the ksegrp */ 386 kp->ki_slptime = mkg.kg_slptime; 387 kp->ki_pri.pri_class = mkg.kg_pri_class; 388 kp->ki_pri.pri_user = mkg.kg_user_pri; 389 kp->ki_estcpu = mkg.kg_estcpu; 390 391#if 0 392 /* Stuff from the kse */ 393 kp->ki_pctcpu = mke.ke_pctcpu; 394 kp->ki_rqindex = mke.ke_rqindex; 395#else 396 kp->ki_pctcpu = 0; 397 kp->ki_rqindex = 0; 398#endif 399 } else { 400 kp->ki_tdflags = -1; 401 /* All the rest are 0 for now */ 402 } 403 } else { 404 kp->ki_stat = SZOMB; 405 } 406 bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); 407 ++bp; 408 ++cnt; 409 } 410 return (cnt); 411} 412 413/* 414 * Build proc info array by reading in proc list from a crash dump. 415 * Return number of procs read. maxcnt is the max we will read. 416 */ 417static int 418kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) 419 kvm_t *kd; 420 int what, arg; 421 u_long a_allproc; 422 u_long a_zombproc; 423 int maxcnt; 424{ 425 struct kinfo_proc *bp = kd->procbase; 426 int acnt, zcnt; 427 struct proc *p; 428 429 if (KREAD(kd, a_allproc, &p)) { 430 _kvm_err(kd, kd->program, "cannot read allproc"); 431 return (-1); 432 } 433 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 434 if (acnt < 0) 435 return (acnt); 436 437 if (KREAD(kd, a_zombproc, &p)) { 438 _kvm_err(kd, kd->program, "cannot read zombproc"); 439 return (-1); 440 } 441 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 442 if (zcnt < 0) 443 zcnt = 0; 444 445 return (acnt + zcnt); 446} 447 448struct kinfo_proc * 449kvm_getprocs(kd, op, arg, cnt) 450 kvm_t *kd; 451 int op, arg; 452 int *cnt; 453{ 454 int mib[4], st, nprocs; 455 size_t size; 456 int temp_op; 457 458 if (kd->procbase != 0) { 459 free((void *)kd->procbase); 460 /* 461 * Clear this pointer in case this call fails. Otherwise, 462 * kvm_close() will free it again. 463 */ 464 kd->procbase = 0; 465 } 466 if (ISALIVE(kd)) { 467 size = 0; 468 mib[0] = CTL_KERN; 469 mib[1] = KERN_PROC; 470 mib[2] = op; 471 mib[3] = arg; 472 temp_op = op & ~KERN_PROC_INC_THREAD; 473 st = sysctl(mib, 474 temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ? 475 3 : 4, NULL, &size, NULL, 0); 476 if (st == -1) { 477 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 478 return (0); 479 } 480 /* 481 * We can't continue with a size of 0 because we pass 482 * it to realloc() (via _kvm_realloc()), and passing 0 483 * to realloc() results in undefined behavior. 484 */ 485 if (size == 0) { 486 /* 487 * XXX: We should probably return an invalid, 488 * but non-NULL, pointer here so any client 489 * program trying to dereference it will 490 * crash. However, _kvm_freeprocs() calls 491 * free() on kd->procbase if it isn't NULL, 492 * and free()'ing a junk pointer isn't good. 493 * Then again, _kvm_freeprocs() isn't used 494 * anywhere . . . 495 */ 496 kd->procbase = _kvm_malloc(kd, 1); 497 goto liveout; 498 } 499 do { 500 size += size / 10; 501 kd->procbase = (struct kinfo_proc *) 502 _kvm_realloc(kd, kd->procbase, size); 503 if (kd->procbase == 0) 504 return (0); 505 st = sysctl(mib, temp_op == KERN_PROC_ALL || 506 temp_op == KERN_PROC_PROC ? 3 : 4, 507 kd->procbase, &size, NULL, 0); 508 } while (st == -1 && errno == ENOMEM); 509 if (st == -1) { 510 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 511 return (0); 512 } 513 /* 514 * We have to check the size again because sysctl() 515 * may "round up" oldlenp if oldp is NULL; hence it 516 * might've told us that there was data to get when 517 * there really isn't any. 518 */ 519 if (size > 0 && 520 kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { 521 _kvm_err(kd, kd->program, 522 "kinfo_proc size mismatch (expected %d, got %d)", 523 sizeof(struct kinfo_proc), 524 kd->procbase->ki_structsize); 525 return (0); 526 } 527liveout: 528 nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; 529 } else { 530 struct nlist nl[4], *p; 531 532 nl[0].n_name = "_nprocs"; 533 nl[1].n_name = "_allproc"; 534 nl[2].n_name = "_zombproc"; 535 nl[3].n_name = 0; 536 537 if (kvm_nlist(kd, nl) != 0) { 538 for (p = nl; p->n_type != 0; ++p) 539 ; 540 _kvm_err(kd, kd->program, 541 "%s: no such symbol", p->n_name); 542 return (0); 543 } 544 if (KREAD(kd, nl[0].n_value, &nprocs)) { 545 _kvm_err(kd, kd->program, "can't read nprocs"); 546 return (0); 547 } 548 size = nprocs * sizeof(struct kinfo_proc); 549 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 550 if (kd->procbase == 0) 551 return (0); 552 553 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 554 nl[2].n_value, nprocs); 555#ifdef notdef 556 size = nprocs * sizeof(struct kinfo_proc); 557 (void)realloc(kd->procbase, size); 558#endif 559 } 560 *cnt = nprocs; 561 return (kd->procbase); 562} 563 564void 565_kvm_freeprocs(kd) 566 kvm_t *kd; 567{ 568 if (kd->procbase) { 569 free(kd->procbase); 570 kd->procbase = 0; 571 } 572} 573 574void * 575_kvm_realloc(kd, p, n) 576 kvm_t *kd; 577 void *p; 578 size_t n; 579{ 580 void *np = (void *)realloc(p, n); 581 582 if (np == 0) { 583 free(p); 584 _kvm_err(kd, kd->program, "out of memory"); 585 } 586 return (np); 587} 588 589#ifndef MAX 590#define MAX(a, b) ((a) > (b) ? (a) : (b)) 591#endif 592 593/* 594 * Read in an argument vector from the user address space of process kp. 595 * addr if the user-space base address of narg null-terminated contiguous 596 * strings. This is used to read in both the command arguments and 597 * environment strings. Read at most maxcnt characters of strings. 598 */ 599static char ** 600kvm_argv(kd, kp, addr, narg, maxcnt) 601 kvm_t *kd; 602 struct kinfo_proc *kp; 603 u_long addr; 604 int narg; 605 int maxcnt; 606{ 607 char *np, *cp, *ep, *ap; 608 u_long oaddr = -1; 609 int len, cc; 610 char **argv; 611 612 /* 613 * Check that there aren't an unreasonable number of agruments, 614 * and that the address is in user space. 615 */ 616 if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) 617 return (0); 618 619 /* 620 * kd->argv : work space for fetching the strings from the target 621 * process's space, and is converted for returning to caller 622 */ 623 if (kd->argv == 0) { 624 /* 625 * Try to avoid reallocs. 626 */ 627 kd->argc = MAX(narg + 1, 32); 628 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 629 sizeof(*kd->argv)); 630 if (kd->argv == 0) 631 return (0); 632 } else if (narg + 1 > kd->argc) { 633 kd->argc = MAX(2 * kd->argc, narg + 1); 634 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 635 sizeof(*kd->argv)); 636 if (kd->argv == 0) 637 return (0); 638 } 639 /* 640 * kd->argspc : returned to user, this is where the kd->argv 641 * arrays are left pointing to the collected strings. 642 */ 643 if (kd->argspc == 0) { 644 kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE); 645 if (kd->argspc == 0) 646 return (0); 647 kd->arglen = PAGE_SIZE; 648 } 649 /* 650 * kd->argbuf : used to pull in pages from the target process. 651 * the strings are copied out of here. 652 */ 653 if (kd->argbuf == 0) { 654 kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE); 655 if (kd->argbuf == 0) 656 return (0); 657 } 658 659 /* Pull in the target process'es argv vector */ 660 cc = sizeof(char *) * narg; 661 if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc) 662 return (0); 663 /* 664 * ap : saved start address of string we're working on in kd->argspc 665 * np : pointer to next place to write in kd->argspc 666 * len: length of data in kd->argspc 667 * argv: pointer to the argv vector that we are hunting around the 668 * target process space for, and converting to addresses in 669 * our address space (kd->argspc). 670 */ 671 ap = np = kd->argspc; 672 argv = kd->argv; 673 len = 0; 674 /* 675 * Loop over pages, filling in the argument vector. 676 * Note that the argv strings could be pointing *anywhere* in 677 * the user address space and are no longer contiguous. 678 * Note that *argv is modified when we are going to fetch a string 679 * that crosses a page boundary. We copy the next part of the string 680 * into to "np" and eventually convert the pointer. 681 */ 682 while (argv < kd->argv + narg && *argv != 0) { 683 684 /* get the address that the current argv string is on */ 685 addr = (u_long)*argv & ~(PAGE_SIZE - 1); 686 687 /* is it the same page as the last one? */ 688 if (addr != oaddr) { 689 if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) != 690 PAGE_SIZE) 691 return (0); 692 oaddr = addr; 693 } 694 695 /* offset within the page... kd->argbuf */ 696 addr = (u_long)*argv & (PAGE_SIZE - 1); 697 698 /* cp = start of string, cc = count of chars in this chunk */ 699 cp = kd->argbuf + addr; 700 cc = PAGE_SIZE - addr; 701 702 /* dont get more than asked for by user process */ 703 if (maxcnt > 0 && cc > maxcnt - len) 704 cc = maxcnt - len; 705 706 /* pointer to end of string if we found it in this page */ 707 ep = memchr(cp, '\0', cc); 708 if (ep != 0) 709 cc = ep - cp + 1; 710 /* 711 * at this point, cc is the count of the chars that we are 712 * going to retrieve this time. we may or may not have found 713 * the end of it. (ep points to the null if the end is known) 714 */ 715 716 /* will we exceed the malloc/realloced buffer? */ 717 if (len + cc > kd->arglen) { 718 int off; 719 char **pp; 720 char *op = kd->argspc; 721 722 kd->arglen *= 2; 723 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 724 kd->arglen); 725 if (kd->argspc == 0) 726 return (0); 727 /* 728 * Adjust argv pointers in case realloc moved 729 * the string space. 730 */ 731 off = kd->argspc - op; 732 for (pp = kd->argv; pp < argv; pp++) 733 *pp += off; 734 ap += off; 735 np += off; 736 } 737 /* np = where to put the next part of the string in kd->argspc*/ 738 /* np is kinda redundant.. could use "kd->argspc + len" */ 739 memcpy(np, cp, cc); 740 np += cc; /* inc counters */ 741 len += cc; 742 743 /* 744 * if end of string found, set the *argv pointer to the 745 * saved beginning of string, and advance. argv points to 746 * somewhere in kd->argv.. This is initially relative 747 * to the target process, but when we close it off, we set 748 * it to point in our address space. 749 */ 750 if (ep != 0) { 751 *argv++ = ap; 752 ap = np; 753 } else { 754 /* update the address relative to the target process */ 755 *argv += cc; 756 } 757 758 if (maxcnt > 0 && len >= maxcnt) { 759 /* 760 * We're stopping prematurely. Terminate the 761 * current string. 762 */ 763 if (ep == 0) { 764 *np = '\0'; 765 *argv++ = ap; 766 } 767 break; 768 } 769 } 770 /* Make sure argv is terminated. */ 771 *argv = 0; 772 return (kd->argv); 773} 774 775static void 776ps_str_a(p, addr, n) 777 struct ps_strings *p; 778 u_long *addr; 779 int *n; 780{ 781 *addr = (u_long)p->ps_argvstr; 782 *n = p->ps_nargvstr; 783} 784 785static void 786ps_str_e(p, addr, n) 787 struct ps_strings *p; 788 u_long *addr; 789 int *n; 790{ 791 *addr = (u_long)p->ps_envstr; 792 *n = p->ps_nenvstr; 793} 794 795/* 796 * Determine if the proc indicated by p is still active. 797 * This test is not 100% foolproof in theory, but chances of 798 * being wrong are very low. 799 */ 800static int 801proc_verify(curkp) 802 struct kinfo_proc *curkp; 803{ 804 struct kinfo_proc newkp; 805 int mib[4]; 806 size_t len; 807 808 mib[0] = CTL_KERN; 809 mib[1] = KERN_PROC; 810 mib[2] = KERN_PROC_PID; 811 mib[3] = curkp->ki_pid; 812 len = sizeof(newkp); 813 if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1) 814 return (0); 815 return (curkp->ki_pid == newkp.ki_pid && 816 (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB)); 817} 818 819static char ** 820kvm_doargv(kd, kp, nchr, info) 821 kvm_t *kd; 822 struct kinfo_proc *kp; 823 int nchr; 824 void (*info)(struct ps_strings *, u_long *, int *); 825{ 826 char **ap; 827 u_long addr; 828 int cnt; 829 static struct ps_strings arginfo; 830 static u_long ps_strings; 831 size_t len; 832 833 if (ps_strings == 0) { 834 len = sizeof(ps_strings); 835 if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL, 836 0) == -1) 837 ps_strings = PS_STRINGS; 838 } 839 840 /* 841 * Pointers are stored at the top of the user stack. 842 */ 843 if (kp->ki_stat == SZOMB || 844 kvm_uread(kd, kp, ps_strings, (char *)&arginfo, 845 sizeof(arginfo)) != sizeof(arginfo)) 846 return (0); 847 848 (*info)(&arginfo, &addr, &cnt); 849 if (cnt == 0) 850 return (0); 851 ap = kvm_argv(kd, kp, addr, cnt, nchr); 852 /* 853 * For live kernels, make sure this process didn't go away. 854 */ 855 if (ap != 0 && ISALIVE(kd) && !proc_verify(kp)) 856 ap = 0; 857 return (ap); 858} 859 860/* 861 * Get the command args. This code is now machine independent. 862 */ 863char ** 864kvm_getargv(kd, kp, nchr) 865 kvm_t *kd; 866 const struct kinfo_proc *kp; 867 int nchr; 868{ 869 int oid[4]; 870 int i; 871 size_t bufsz; 872 static unsigned long buflen; 873 static char *buf, *p; 874 static char **bufp; 875 static int argc; 876 877 if (!ISALIVE(kd)) { 878 _kvm_err(kd, kd->program, 879 "cannot read user space from dead kernel"); 880 return (0); 881 } 882 883 if (!buflen) { 884 bufsz = sizeof(buflen); 885 i = sysctlbyname("kern.ps_arg_cache_limit", 886 &buflen, &bufsz, NULL, 0); 887 if (i == -1) { 888 buflen = 0; 889 } else { 890 buf = malloc(buflen); 891 if (buf == NULL) 892 buflen = 0; 893 argc = 32; 894 bufp = malloc(sizeof(char *) * argc); 895 } 896 } 897 if (buf != NULL) { 898 oid[0] = CTL_KERN; 899 oid[1] = KERN_PROC; 900 oid[2] = KERN_PROC_ARGS; 901 oid[3] = kp->ki_pid; 902 bufsz = buflen; 903 i = sysctl(oid, 4, buf, &bufsz, 0, 0); 904 if (i == 0 && bufsz > 0) { 905 i = 0; 906 p = buf; 907 do { 908 bufp[i++] = p; 909 p += strlen(p) + 1; 910 if (i >= argc) { 911 argc += argc; 912 bufp = realloc(bufp, 913 sizeof(char *) * argc); 914 } 915 } while (p < buf + bufsz); 916 bufp[i++] = 0; 917 return (bufp); 918 } 919 } 920 if (kp->ki_flag & P_SYSTEM) 921 return (NULL); 922 return (kvm_doargv(kd, kp, nchr, ps_str_a)); 923} 924 925char ** 926kvm_getenvv(kd, kp, nchr) 927 kvm_t *kd; 928 const struct kinfo_proc *kp; 929 int nchr; 930{ 931 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 932} 933 934/* 935 * Read from user space. The user context is given by p. 936 */ 937ssize_t 938kvm_uread(kd, kp, uva, buf, len) 939 kvm_t *kd; 940 struct kinfo_proc *kp; 941 u_long uva; 942 char *buf; 943 size_t len; 944{ 945 char *cp; 946 char procfile[MAXPATHLEN]; 947 ssize_t amount; 948 int fd; 949 950 if (!ISALIVE(kd)) { 951 _kvm_err(kd, kd->program, 952 "cannot read user space from dead kernel"); 953 return (0); 954 } 955 956 sprintf(procfile, "/proc/%d/mem", kp->ki_pid); 957 fd = open(procfile, O_RDONLY, 0); 958 if (fd < 0) { 959 _kvm_err(kd, kd->program, "cannot open %s", procfile); 960 close(fd); 961 return (0); 962 } 963 964 cp = buf; 965 while (len > 0) { 966 errno = 0; 967 if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) { 968 _kvm_err(kd, kd->program, "invalid address (%x) in %s", 969 uva, procfile); 970 break; 971 } 972 amount = read(fd, cp, len); 973 if (amount < 0) { 974 _kvm_syserr(kd, kd->program, "error reading %s", 975 procfile); 976 break; 977 } 978 if (amount == 0) { 979 _kvm_err(kd, kd->program, "EOF reading %s", procfile); 980 break; 981 } 982 cp += amount; 983 uva += amount; 984 len -= amount; 985 } 986 987 close(fd); 988 return ((ssize_t)(cp - buf)); 989} 990