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