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