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. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if 0 35 #if defined(LIBC_SCCS) && !defined(lint) 36 static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; 37 #endif /* LIBC_SCCS and not lint */ 38 #endif 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 /* 44 * Proc traversal interface for kvm. ps and w are (probably) the exclusive 45 * users of this code, so we've factored it out into a separate module. 46 * Thus, we keep this grunge out of the other kvm applications (i.e., 47 * most other applications are interested only in open/close/read/nlist). 48 */ 49 50 #include <sys/param.h> 51 #define _WANT_UCRED /* make ucred.h give us 'struct ucred' */ 52 #include <sys/ucred.h> 53 #include <sys/queue.h> 54 #include <sys/_lock.h> 55 #include <sys/_mutex.h> 56 #include <sys/_task.h> 57 #include <sys/cpuset.h> 58 #include <sys/user.h> 59 #include <sys/proc.h> 60 #define _WANT_PRISON /* make jail.h give us 'struct prison' */ 61 #include <sys/jail.h> 62 #include <sys/exec.h> 63 #include <sys/stat.h> 64 #include <sys/sysent.h> 65 #include <sys/ioctl.h> 66 #include <sys/tty.h> 67 #include <sys/file.h> 68 #include <sys/conf.h> 69 #define _WANT_KW_EXITCODE 70 #include <sys/wait.h> 71 #include <stdio.h> 72 #include <stdlib.h> 73 #include <unistd.h> 74 #include <nlist.h> 75 #include <kvm.h> 76 77 #include <sys/sysctl.h> 78 79 #include <limits.h> 80 #include <memory.h> 81 #include <paths.h> 82 83 #include "kvm_private.h" 84 85 #define KREAD(kd, addr, obj) \ 86 (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) 87 88 static int ticks; 89 static int hz; 90 static uint64_t cpu_tick_frequency; 91 92 /* 93 * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is 94 * read/initialized before this function is ever called. 95 */ 96 static uint64_t 97 cputick2usec(uint64_t tick) 98 { 99 100 if (cpu_tick_frequency == 0) 101 return (0); 102 if (tick > 18446744073709551) /* floor(2^64 / 1000) */ 103 return (tick / (cpu_tick_frequency / 1000000)); 104 else if (tick > 18446744073709) /* floor(2^64 / 1000000) */ 105 return ((tick * 1000) / (cpu_tick_frequency / 1000)); 106 else 107 return ((tick * 1000000) / cpu_tick_frequency); 108 } 109 110 /* 111 * Read proc's from memory file into buffer bp, which has space to hold 112 * at most maxcnt procs. 113 */ 114 static int 115 kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, 116 struct kinfo_proc *bp, int maxcnt) 117 { 118 int cnt = 0; 119 struct kinfo_proc kinfo_proc, *kp; 120 struct pgrp pgrp; 121 struct session sess; 122 struct cdev t_cdev; 123 struct tty tty; 124 struct vmspace vmspace; 125 struct sigacts sigacts; 126 #if 0 127 struct pstats pstats; 128 #endif 129 struct ucred ucred; 130 struct prison pr; 131 struct thread mtd; 132 struct proc proc; 133 struct proc pproc; 134 struct sysentvec sysent; 135 char svname[KI_EMULNAMELEN]; 136 137 kp = &kinfo_proc; 138 kp->ki_structsize = sizeof(kinfo_proc); 139 /* 140 * Loop on the processes. this is completely broken because we need to be 141 * able to loop on the threads and merge the ones that are the same process some how. 142 */ 143 for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 144 memset(kp, 0, sizeof *kp); 145 if (KREAD(kd, (u_long)p, &proc)) { 146 _kvm_err(kd, kd->program, "can't read proc at %p", p); 147 return (-1); 148 } 149 if (proc.p_state == PRS_NEW) 150 continue; 151 if (proc.p_state != PRS_ZOMBIE) { 152 if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads), 153 &mtd)) { 154 _kvm_err(kd, kd->program, 155 "can't read thread at %p", 156 TAILQ_FIRST(&proc.p_threads)); 157 return (-1); 158 } 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_cr_flags = ucred.cr_flags; 166 if (ucred.cr_ngroups > KI_NGROUPS) { 167 kp->ki_ngroups = KI_NGROUPS; 168 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 169 } else 170 kp->ki_ngroups = ucred.cr_ngroups; 171 kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups, 172 kp->ki_ngroups * sizeof(gid_t)); 173 kp->ki_uid = ucred.cr_uid; 174 if (ucred.cr_prison != NULL) { 175 if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) { 176 _kvm_err(kd, kd->program, 177 "can't read prison at %p", 178 ucred.cr_prison); 179 return (-1); 180 } 181 kp->ki_jid = pr.pr_id; 182 } 183 } 184 185 switch(what & ~KERN_PROC_INC_THREAD) { 186 187 case KERN_PROC_GID: 188 if (kp->ki_groups[0] != (gid_t)arg) 189 continue; 190 break; 191 192 case KERN_PROC_PID: 193 if (proc.p_pid != (pid_t)arg) 194 continue; 195 break; 196 197 case KERN_PROC_RGID: 198 if (kp->ki_rgid != (gid_t)arg) 199 continue; 200 break; 201 202 case KERN_PROC_UID: 203 if (kp->ki_uid != (uid_t)arg) 204 continue; 205 break; 206 207 case KERN_PROC_RUID: 208 if (kp->ki_ruid != (uid_t)arg) 209 continue; 210 break; 211 } 212 /* 213 * We're going to add another proc to the set. If this 214 * will overflow the buffer, assume the reason is because 215 * nprocs (or the proc list) is corrupt and declare an error. 216 */ 217 if (cnt >= maxcnt) { 218 _kvm_err(kd, kd->program, "nprocs corrupt"); 219 return (-1); 220 } 221 /* 222 * gather kinfo_proc 223 */ 224 kp->ki_paddr = p; 225 kp->ki_addr = 0; /* XXX uarea */ 226 /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ 227 kp->ki_args = proc.p_args; 228 kp->ki_tracep = proc.p_tracevp; 229 kp->ki_textvp = proc.p_textvp; 230 kp->ki_fd = proc.p_fd; 231 kp->ki_vmspace = proc.p_vmspace; 232 if (proc.p_sigacts != NULL) { 233 if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) { 234 _kvm_err(kd, kd->program, 235 "can't read sigacts at %p", proc.p_sigacts); 236 return (-1); 237 } 238 kp->ki_sigignore = sigacts.ps_sigignore; 239 kp->ki_sigcatch = sigacts.ps_sigcatch; 240 } 241 #if 0 242 if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) { 243 if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { 244 _kvm_err(kd, kd->program, 245 "can't read stats at %x", proc.p_stats); 246 return (-1); 247 } 248 kp->ki_start = pstats.p_start; 249 250 /* 251 * XXX: The times here are probably zero and need 252 * to be calculated from the raw data in p_rux and 253 * p_crux. 254 */ 255 kp->ki_rusage = pstats.p_ru; 256 kp->ki_childstime = pstats.p_cru.ru_stime; 257 kp->ki_childutime = pstats.p_cru.ru_utime; 258 /* Some callers want child-times in a single value */ 259 timeradd(&kp->ki_childstime, &kp->ki_childutime, 260 &kp->ki_childtime); 261 } 262 #endif 263 if (proc.p_oppid) 264 kp->ki_ppid = proc.p_oppid; 265 else if (proc.p_pptr) { 266 if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { 267 _kvm_err(kd, kd->program, 268 "can't read pproc at %p", proc.p_pptr); 269 return (-1); 270 } 271 kp->ki_ppid = pproc.p_pid; 272 } else 273 kp->ki_ppid = 0; 274 if (proc.p_pgrp == NULL) 275 goto nopgrp; 276 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 277 _kvm_err(kd, kd->program, "can't read pgrp at %p", 278 proc.p_pgrp); 279 return (-1); 280 } 281 kp->ki_pgid = pgrp.pg_id; 282 kp->ki_jobc = pgrp.pg_jobc; 283 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 284 _kvm_err(kd, kd->program, "can't read session at %p", 285 pgrp.pg_session); 286 return (-1); 287 } 288 kp->ki_sid = sess.s_sid; 289 (void)memcpy(kp->ki_login, sess.s_login, 290 sizeof(kp->ki_login)); 291 kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; 292 if (sess.s_leader == p) 293 kp->ki_kiflag |= KI_SLEADER; 294 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 295 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 296 _kvm_err(kd, kd->program, 297 "can't read tty at %p", sess.s_ttyp); 298 return (-1); 299 } 300 if (tty.t_dev != NULL) { 301 if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) { 302 _kvm_err(kd, kd->program, 303 "can't read cdev at %p", 304 tty.t_dev); 305 return (-1); 306 } 307 #if 0 308 kp->ki_tdev = t_cdev.si_udev; 309 #else 310 kp->ki_tdev = NODEV; 311 #endif 312 } 313 if (tty.t_pgrp != NULL) { 314 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 315 _kvm_err(kd, kd->program, 316 "can't read tpgrp at %p", 317 tty.t_pgrp); 318 return (-1); 319 } 320 kp->ki_tpgid = pgrp.pg_id; 321 } else 322 kp->ki_tpgid = -1; 323 if (tty.t_session != NULL) { 324 if (KREAD(kd, (u_long)tty.t_session, &sess)) { 325 _kvm_err(kd, kd->program, 326 "can't read session at %p", 327 tty.t_session); 328 return (-1); 329 } 330 kp->ki_tsid = sess.s_sid; 331 } 332 } else { 333 nopgrp: 334 kp->ki_tdev = NODEV; 335 } 336 if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) 337 (void)kvm_read(kd, (u_long)mtd.td_wmesg, 338 kp->ki_wmesg, WMESGLEN); 339 340 (void)kvm_read(kd, (u_long)proc.p_vmspace, 341 (char *)&vmspace, sizeof(vmspace)); 342 kp->ki_size = vmspace.vm_map.size; 343 /* 344 * Approximate the kernel's method of calculating 345 * this field. 346 */ 347 #define pmap_resident_count(pm) ((pm)->pm_stats.resident_count) 348 kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap); 349 kp->ki_swrss = vmspace.vm_swrss; 350 kp->ki_tsize = vmspace.vm_tsize; 351 kp->ki_dsize = vmspace.vm_dsize; 352 kp->ki_ssize = vmspace.vm_ssize; 353 354 switch (what & ~KERN_PROC_INC_THREAD) { 355 356 case KERN_PROC_PGRP: 357 if (kp->ki_pgid != (pid_t)arg) 358 continue; 359 break; 360 361 case KERN_PROC_SESSION: 362 if (kp->ki_sid != (pid_t)arg) 363 continue; 364 break; 365 366 case KERN_PROC_TTY: 367 if ((proc.p_flag & P_CONTROLT) == 0 || 368 kp->ki_tdev != (dev_t)arg) 369 continue; 370 break; 371 } 372 if (proc.p_comm[0] != 0) 373 strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); 374 (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent, 375 sizeof(sysent)); 376 (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname, 377 sizeof(svname)); 378 if (svname[0] != 0) 379 strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN); 380 if ((proc.p_state != PRS_ZOMBIE) && 381 (mtd.td_blocked != 0)) { 382 kp->ki_kiflag |= KI_LOCKBLOCK; 383 if (mtd.td_lockname) 384 (void)kvm_read(kd, 385 (u_long)mtd.td_lockname, 386 kp->ki_lockname, LOCKNAMELEN); 387 kp->ki_lockname[LOCKNAMELEN] = 0; 388 } 389 kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime); 390 kp->ki_pid = proc.p_pid; 391 kp->ki_siglist = proc.p_siglist; 392 SIGSETOR(kp->ki_siglist, mtd.td_siglist); 393 kp->ki_sigmask = mtd.td_sigmask; 394 kp->ki_xstat = KW_EXITCODE(proc.p_xexit, proc.p_xsig); 395 kp->ki_acflag = proc.p_acflag; 396 kp->ki_lock = proc.p_lock; 397 if (proc.p_state != PRS_ZOMBIE) { 398 kp->ki_swtime = (ticks - proc.p_swtick) / hz; 399 kp->ki_flag = proc.p_flag; 400 kp->ki_sflag = 0; 401 kp->ki_nice = proc.p_nice; 402 kp->ki_traceflag = proc.p_traceflag; 403 if (proc.p_state == PRS_NORMAL) { 404 if (TD_ON_RUNQ(&mtd) || 405 TD_CAN_RUN(&mtd) || 406 TD_IS_RUNNING(&mtd)) { 407 kp->ki_stat = SRUN; 408 } else if (mtd.td_state == 409 TDS_INHIBITED) { 410 if (P_SHOULDSTOP(&proc)) { 411 kp->ki_stat = SSTOP; 412 } else if ( 413 TD_IS_SLEEPING(&mtd)) { 414 kp->ki_stat = SSLEEP; 415 } else if (TD_ON_LOCK(&mtd)) { 416 kp->ki_stat = SLOCK; 417 } else { 418 kp->ki_stat = SWAIT; 419 } 420 } 421 } else { 422 kp->ki_stat = SIDL; 423 } 424 /* Stuff from the thread */ 425 kp->ki_pri.pri_level = mtd.td_priority; 426 kp->ki_pri.pri_native = mtd.td_base_pri; 427 kp->ki_lastcpu = mtd.td_lastcpu; 428 kp->ki_wchan = mtd.td_wchan; 429 kp->ki_oncpu = mtd.td_oncpu; 430 if (mtd.td_name[0] != '\0') 431 strlcpy(kp->ki_tdname, mtd.td_name, sizeof(kp->ki_tdname)); 432 kp->ki_pctcpu = 0; 433 kp->ki_rqindex = 0; 434 435 /* 436 * Note: legacy fields; wraps at NO_CPU_OLD or the 437 * old max CPU value as appropriate 438 */ 439 if (mtd.td_lastcpu == NOCPU) 440 kp->ki_lastcpu_old = NOCPU_OLD; 441 else if (mtd.td_lastcpu > MAXCPU_OLD) 442 kp->ki_lastcpu_old = MAXCPU_OLD; 443 else 444 kp->ki_lastcpu_old = mtd.td_lastcpu; 445 446 if (mtd.td_oncpu == NOCPU) 447 kp->ki_oncpu_old = NOCPU_OLD; 448 else if (mtd.td_oncpu > MAXCPU_OLD) 449 kp->ki_oncpu_old = MAXCPU_OLD; 450 else 451 kp->ki_oncpu_old = mtd.td_oncpu; 452 } else { 453 kp->ki_stat = SZOMB; 454 } 455 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */ 456 bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); 457 ++bp; 458 ++cnt; 459 } 460 return (cnt); 461 } 462 463 /* 464 * Build proc info array by reading in proc list from a crash dump. 465 * Return number of procs read. maxcnt is the max we will read. 466 */ 467 static int 468 kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, 469 u_long a_zombproc, int maxcnt) 470 { 471 struct kinfo_proc *bp = kd->procbase; 472 int acnt, zcnt; 473 struct proc *p; 474 475 if (KREAD(kd, a_allproc, &p)) { 476 _kvm_err(kd, kd->program, "cannot read allproc"); 477 return (-1); 478 } 479 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 480 if (acnt < 0) 481 return (acnt); 482 483 if (KREAD(kd, a_zombproc, &p)) { 484 _kvm_err(kd, kd->program, "cannot read zombproc"); 485 return (-1); 486 } 487 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 488 if (zcnt < 0) 489 zcnt = 0; 490 491 return (acnt + zcnt); 492 } 493 494 struct kinfo_proc * 495 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt) 496 { 497 int mib[4], st, nprocs; 498 size_t size, osize; 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 == NULL) 547 return (0); 548 osize = size; 549 st = sysctl(mib, temp_op == KERN_PROC_ALL || 550 temp_op == KERN_PROC_PROC ? 3 : 4, 551 kd->procbase, &size, NULL, 0); 552 } while (st == -1 && errno == ENOMEM && size == osize); 553 if (st == -1) { 554 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 555 return (0); 556 } 557 /* 558 * We have to check the size again because sysctl() 559 * may "round up" oldlenp if oldp is NULL; hence it 560 * might've told us that there was data to get when 561 * there really isn't any. 562 */ 563 if (size > 0 && 564 kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { 565 _kvm_err(kd, kd->program, 566 "kinfo_proc size mismatch (expected %zu, got %d)", 567 sizeof(struct kinfo_proc), 568 kd->procbase->ki_structsize); 569 return (0); 570 } 571 liveout: 572 nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; 573 } else { 574 struct nlist nl[7], *p; 575 576 nl[0].n_name = "_nprocs"; 577 nl[1].n_name = "_allproc"; 578 nl[2].n_name = "_zombproc"; 579 nl[3].n_name = "_ticks"; 580 nl[4].n_name = "_hz"; 581 nl[5].n_name = "_cpu_tick_frequency"; 582 nl[6].n_name = 0; 583 584 if (!kd->arch->ka_native(kd)) { 585 _kvm_err(kd, kd->program, 586 "cannot read procs from non-native core"); 587 return (0); 588 } 589 590 if (kvm_nlist(kd, nl) != 0) { 591 for (p = nl; p->n_type != 0; ++p) 592 ; 593 _kvm_err(kd, kd->program, 594 "%s: no such symbol", p->n_name); 595 return (0); 596 } 597 if (KREAD(kd, nl[0].n_value, &nprocs)) { 598 _kvm_err(kd, kd->program, "can't read nprocs"); 599 return (0); 600 } 601 if (KREAD(kd, nl[3].n_value, &ticks)) { 602 _kvm_err(kd, kd->program, "can't read ticks"); 603 return (0); 604 } 605 if (KREAD(kd, nl[4].n_value, &hz)) { 606 _kvm_err(kd, kd->program, "can't read hz"); 607 return (0); 608 } 609 if (KREAD(kd, nl[5].n_value, &cpu_tick_frequency)) { 610 _kvm_err(kd, kd->program, 611 "can't read cpu_tick_frequency"); 612 return (0); 613 } 614 size = nprocs * sizeof(struct kinfo_proc); 615 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 616 if (kd->procbase == NULL) 617 return (0); 618 619 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 620 nl[2].n_value, nprocs); 621 if (nprocs <= 0) { 622 _kvm_freeprocs(kd); 623 nprocs = 0; 624 } 625 #ifdef notdef 626 else { 627 size = nprocs * sizeof(struct kinfo_proc); 628 kd->procbase = realloc(kd->procbase, size); 629 } 630 #endif 631 } 632 *cnt = nprocs; 633 return (kd->procbase); 634 } 635 636 void 637 _kvm_freeprocs(kvm_t *kd) 638 { 639 640 free(kd->procbase); 641 kd->procbase = NULL; 642 } 643 644 void * 645 _kvm_realloc(kvm_t *kd, void *p, size_t n) 646 { 647 void *np; 648 649 np = reallocf(p, n); 650 if (np == NULL) 651 _kvm_err(kd, kd->program, "out of memory"); 652 return (np); 653 } 654 655 /* 656 * Get the command args or environment. 657 */ 658 static char ** 659 kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr) 660 { 661 int oid[4]; 662 int i; 663 size_t bufsz; 664 static int buflen; 665 static char *buf, *p; 666 static char **bufp; 667 static int argc; 668 char **nbufp; 669 670 if (!ISALIVE(kd)) { 671 _kvm_err(kd, kd->program, 672 "cannot read user space from dead kernel"); 673 return (NULL); 674 } 675 676 if (nchr == 0 || nchr > ARG_MAX) 677 nchr = ARG_MAX; 678 if (buflen == 0) { 679 buf = malloc(nchr); 680 if (buf == NULL) { 681 _kvm_err(kd, kd->program, "cannot allocate memory"); 682 return (NULL); 683 } 684 argc = 32; 685 bufp = malloc(sizeof(char *) * argc); 686 if (bufp == NULL) { 687 free(buf); 688 buf = NULL; 689 _kvm_err(kd, kd->program, "cannot allocate memory"); 690 return (NULL); 691 } 692 buflen = nchr; 693 } else if (nchr > buflen) { 694 p = realloc(buf, nchr); 695 if (p != NULL) { 696 buf = p; 697 buflen = nchr; 698 } 699 } 700 oid[0] = CTL_KERN; 701 oid[1] = KERN_PROC; 702 oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS; 703 oid[3] = kp->ki_pid; 704 bufsz = buflen; 705 if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) { 706 /* 707 * If the supplied buf is too short to hold the requested 708 * value the sysctl returns with ENOMEM. The buf is filled 709 * with the truncated value and the returned bufsz is equal 710 * to the requested len. 711 */ 712 if (errno != ENOMEM || bufsz != (size_t)buflen) 713 return (NULL); 714 buf[bufsz - 1] = '\0'; 715 errno = 0; 716 } else if (bufsz == 0) 717 return (NULL); 718 i = 0; 719 p = buf; 720 do { 721 bufp[i++] = p; 722 p += strlen(p) + 1; 723 if (i >= argc) { 724 argc += argc; 725 nbufp = realloc(bufp, sizeof(char *) * argc); 726 if (nbufp == NULL) 727 return (NULL); 728 bufp = nbufp; 729 } 730 } while (p < buf + bufsz); 731 bufp[i++] = 0; 732 return (bufp); 733 } 734 735 char ** 736 kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 737 { 738 return (kvm_argv(kd, kp, 0, nchr)); 739 } 740 741 char ** 742 kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 743 { 744 return (kvm_argv(kd, kp, 1, nchr)); 745 } 746