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 * 4. 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 #include <stdio.h> 70 #include <stdlib.h> 71 #include <unistd.h> 72 #include <nlist.h> 73 #include <kvm.h> 74 75 #include <sys/sysctl.h> 76 77 #include <limits.h> 78 #include <memory.h> 79 #include <paths.h> 80 81 #include "kvm_private.h" 82 83 #define KREAD(kd, addr, obj) \ 84 (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) 85 86 static int ticks; 87 static int hz; 88 static uint64_t cpu_tick_frequency; 89 90 /* 91 * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is 92 * read/initialized before this function is ever called. 93 */ 94 static uint64_t 95 cputick2usec(uint64_t tick) 96 { 97 98 if (cpu_tick_frequency == 0) 99 return (0); 100 if (tick > 18446744073709551) /* floor(2^64 / 1000) */ 101 return (tick / (cpu_tick_frequency / 1000000)); 102 else if (tick > 18446744073709) /* floor(2^64 / 1000000) */ 103 return ((tick * 1000) / (cpu_tick_frequency / 1000)); 104 else 105 return ((tick * 1000000) / cpu_tick_frequency); 106 } 107 108 /* 109 * Read proc's from memory file into buffer bp, which has space to hold 110 * at most maxcnt procs. 111 */ 112 static int 113 kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, 114 struct kinfo_proc *bp, int maxcnt) 115 { 116 int cnt = 0; 117 struct kinfo_proc kinfo_proc, *kp; 118 struct pgrp pgrp; 119 struct session sess; 120 struct cdev t_cdev; 121 struct tty tty; 122 struct vmspace vmspace; 123 struct sigacts sigacts; 124 #if 0 125 struct pstats pstats; 126 #endif 127 struct ucred ucred; 128 struct prison pr; 129 struct thread mtd; 130 struct proc proc; 131 struct proc pproc; 132 struct sysentvec sysent; 133 char svname[KI_EMULNAMELEN]; 134 135 kp = &kinfo_proc; 136 kp->ki_structsize = sizeof(kinfo_proc); 137 /* 138 * Loop on the processes. this is completely broken because we need to be 139 * able to loop on the threads and merge the ones that are the same process some how. 140 */ 141 for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 142 memset(kp, 0, sizeof *kp); 143 if (KREAD(kd, (u_long)p, &proc)) { 144 _kvm_err(kd, kd->program, "can't read proc at %p", p); 145 return (-1); 146 } 147 if (proc.p_state == PRS_NEW) 148 continue; 149 if (proc.p_state != PRS_ZOMBIE) { 150 if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads), 151 &mtd)) { 152 _kvm_err(kd, kd->program, 153 "can't read thread at %p", 154 TAILQ_FIRST(&proc.p_threads)); 155 return (-1); 156 } 157 } 158 if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) { 159 kp->ki_ruid = ucred.cr_ruid; 160 kp->ki_svuid = ucred.cr_svuid; 161 kp->ki_rgid = ucred.cr_rgid; 162 kp->ki_svgid = ucred.cr_svgid; 163 kp->ki_cr_flags = ucred.cr_flags; 164 if (ucred.cr_ngroups > KI_NGROUPS) { 165 kp->ki_ngroups = KI_NGROUPS; 166 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 167 } else 168 kp->ki_ngroups = ucred.cr_ngroups; 169 kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups, 170 kp->ki_ngroups * sizeof(gid_t)); 171 kp->ki_uid = ucred.cr_uid; 172 if (ucred.cr_prison != NULL) { 173 if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) { 174 _kvm_err(kd, kd->program, 175 "can't read prison at %p", 176 ucred.cr_prison); 177 return (-1); 178 } 179 kp->ki_jid = pr.pr_id; 180 } 181 } 182 183 switch(what & ~KERN_PROC_INC_THREAD) { 184 185 case KERN_PROC_GID: 186 if (kp->ki_groups[0] != (gid_t)arg) 187 continue; 188 break; 189 190 case KERN_PROC_PID: 191 if (proc.p_pid != (pid_t)arg) 192 continue; 193 break; 194 195 case KERN_PROC_RGID: 196 if (kp->ki_rgid != (gid_t)arg) 197 continue; 198 break; 199 200 case KERN_PROC_UID: 201 if (kp->ki_uid != (uid_t)arg) 202 continue; 203 break; 204 205 case KERN_PROC_RUID: 206 if (kp->ki_ruid != (uid_t)arg) 207 continue; 208 break; 209 } 210 /* 211 * We're going to add another proc to the set. If this 212 * will overflow the buffer, assume the reason is because 213 * nprocs (or the proc list) is corrupt and declare an error. 214 */ 215 if (cnt >= maxcnt) { 216 _kvm_err(kd, kd->program, "nprocs corrupt"); 217 return (-1); 218 } 219 /* 220 * gather kinfo_proc 221 */ 222 kp->ki_paddr = p; 223 kp->ki_addr = 0; /* XXX uarea */ 224 /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ 225 kp->ki_args = proc.p_args; 226 kp->ki_tracep = proc.p_tracevp; 227 kp->ki_textvp = proc.p_textvp; 228 kp->ki_fd = proc.p_fd; 229 kp->ki_vmspace = proc.p_vmspace; 230 if (proc.p_sigacts != NULL) { 231 if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) { 232 _kvm_err(kd, kd->program, 233 "can't read sigacts at %p", proc.p_sigacts); 234 return (-1); 235 } 236 kp->ki_sigignore = sigacts.ps_sigignore; 237 kp->ki_sigcatch = sigacts.ps_sigcatch; 238 } 239 #if 0 240 if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) { 241 if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { 242 _kvm_err(kd, kd->program, 243 "can't read stats at %x", proc.p_stats); 244 return (-1); 245 } 246 kp->ki_start = pstats.p_start; 247 248 /* 249 * XXX: The times here are probably zero and need 250 * to be calculated from the raw data in p_rux and 251 * p_crux. 252 */ 253 kp->ki_rusage = pstats.p_ru; 254 kp->ki_childstime = pstats.p_cru.ru_stime; 255 kp->ki_childutime = pstats.p_cru.ru_utime; 256 /* Some callers want child-times in a single value */ 257 timeradd(&kp->ki_childstime, &kp->ki_childutime, 258 &kp->ki_childtime); 259 } 260 #endif 261 if (proc.p_oppid) 262 kp->ki_ppid = proc.p_oppid; 263 else if (proc.p_pptr) { 264 if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { 265 _kvm_err(kd, kd->program, 266 "can't read pproc at %p", proc.p_pptr); 267 return (-1); 268 } 269 kp->ki_ppid = pproc.p_pid; 270 } else 271 kp->ki_ppid = 0; 272 if (proc.p_pgrp == NULL) 273 goto nopgrp; 274 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 275 _kvm_err(kd, kd->program, "can't read pgrp at %p", 276 proc.p_pgrp); 277 return (-1); 278 } 279 kp->ki_pgid = pgrp.pg_id; 280 kp->ki_jobc = pgrp.pg_jobc; 281 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 282 _kvm_err(kd, kd->program, "can't read session at %p", 283 pgrp.pg_session); 284 return (-1); 285 } 286 kp->ki_sid = sess.s_sid; 287 (void)memcpy(kp->ki_login, sess.s_login, 288 sizeof(kp->ki_login)); 289 kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; 290 if (sess.s_leader == p) 291 kp->ki_kiflag |= KI_SLEADER; 292 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 293 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 294 _kvm_err(kd, kd->program, 295 "can't read tty at %p", sess.s_ttyp); 296 return (-1); 297 } 298 if (tty.t_dev != NULL) { 299 if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) { 300 _kvm_err(kd, kd->program, 301 "can't read cdev at %p", 302 tty.t_dev); 303 return (-1); 304 } 305 #if 0 306 kp->ki_tdev = t_cdev.si_udev; 307 #else 308 kp->ki_tdev = NODEV; 309 #endif 310 } 311 if (tty.t_pgrp != NULL) { 312 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 313 _kvm_err(kd, kd->program, 314 "can't read tpgrp at %p", 315 tty.t_pgrp); 316 return (-1); 317 } 318 kp->ki_tpgid = pgrp.pg_id; 319 } else 320 kp->ki_tpgid = -1; 321 if (tty.t_session != NULL) { 322 if (KREAD(kd, (u_long)tty.t_session, &sess)) { 323 _kvm_err(kd, kd->program, 324 "can't read session at %p", 325 tty.t_session); 326 return (-1); 327 } 328 kp->ki_tsid = sess.s_sid; 329 } 330 } else { 331 nopgrp: 332 kp->ki_tdev = NODEV; 333 } 334 if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) 335 (void)kvm_read(kd, (u_long)mtd.td_wmesg, 336 kp->ki_wmesg, WMESGLEN); 337 338 (void)kvm_read(kd, (u_long)proc.p_vmspace, 339 (char *)&vmspace, sizeof(vmspace)); 340 kp->ki_size = vmspace.vm_map.size; 341 /* 342 * Approximate the kernel's method of calculating 343 * this field. 344 */ 345 #define pmap_resident_count(pm) ((pm)->pm_stats.resident_count) 346 kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap); 347 kp->ki_swrss = vmspace.vm_swrss; 348 kp->ki_tsize = vmspace.vm_tsize; 349 kp->ki_dsize = vmspace.vm_dsize; 350 kp->ki_ssize = vmspace.vm_ssize; 351 352 switch (what & ~KERN_PROC_INC_THREAD) { 353 354 case KERN_PROC_PGRP: 355 if (kp->ki_pgid != (pid_t)arg) 356 continue; 357 break; 358 359 case KERN_PROC_SESSION: 360 if (kp->ki_sid != (pid_t)arg) 361 continue; 362 break; 363 364 case KERN_PROC_TTY: 365 if ((proc.p_flag & P_CONTROLT) == 0 || 366 kp->ki_tdev != (dev_t)arg) 367 continue; 368 break; 369 } 370 if (proc.p_comm[0] != 0) 371 strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); 372 (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent, 373 sizeof(sysent)); 374 (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname, 375 sizeof(svname)); 376 if (svname[0] != 0) 377 strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN); 378 if ((proc.p_state != PRS_ZOMBIE) && 379 (mtd.td_blocked != 0)) { 380 kp->ki_kiflag |= KI_LOCKBLOCK; 381 if (mtd.td_lockname) 382 (void)kvm_read(kd, 383 (u_long)mtd.td_lockname, 384 kp->ki_lockname, LOCKNAMELEN); 385 kp->ki_lockname[LOCKNAMELEN] = 0; 386 } 387 kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime); 388 kp->ki_pid = proc.p_pid; 389 kp->ki_siglist = proc.p_siglist; 390 SIGSETOR(kp->ki_siglist, mtd.td_siglist); 391 kp->ki_sigmask = mtd.td_sigmask; 392 kp->ki_xstat = proc.p_xstat; 393 kp->ki_acflag = proc.p_acflag; 394 kp->ki_lock = proc.p_lock; 395 if (proc.p_state != PRS_ZOMBIE) { 396 kp->ki_swtime = (ticks - proc.p_swtick) / hz; 397 kp->ki_flag = proc.p_flag; 398 kp->ki_sflag = 0; 399 kp->ki_nice = proc.p_nice; 400 kp->ki_traceflag = proc.p_traceflag; 401 if (proc.p_state == PRS_NORMAL) { 402 if (TD_ON_RUNQ(&mtd) || 403 TD_CAN_RUN(&mtd) || 404 TD_IS_RUNNING(&mtd)) { 405 kp->ki_stat = SRUN; 406 } else if (mtd.td_state == 407 TDS_INHIBITED) { 408 if (P_SHOULDSTOP(&proc)) { 409 kp->ki_stat = SSTOP; 410 } else if ( 411 TD_IS_SLEEPING(&mtd)) { 412 kp->ki_stat = SSLEEP; 413 } else if (TD_ON_LOCK(&mtd)) { 414 kp->ki_stat = SLOCK; 415 } else { 416 kp->ki_stat = SWAIT; 417 } 418 } 419 } else { 420 kp->ki_stat = SIDL; 421 } 422 /* Stuff from the thread */ 423 kp->ki_pri.pri_level = mtd.td_priority; 424 kp->ki_pri.pri_native = mtd.td_base_pri; 425 kp->ki_lastcpu = mtd.td_lastcpu; 426 kp->ki_wchan = mtd.td_wchan; 427 if (mtd.td_name[0] != 0) 428 strlcpy(kp->ki_tdname, mtd.td_name, MAXCOMLEN); 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 } else { 435 kp->ki_stat = SZOMB; 436 } 437 bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); 438 ++bp; 439 ++cnt; 440 } 441 return (cnt); 442 } 443 444 /* 445 * Build proc info array by reading in proc list from a crash dump. 446 * Return number of procs read. maxcnt is the max we will read. 447 */ 448 static int 449 kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, 450 u_long a_zombproc, int maxcnt) 451 { 452 struct kinfo_proc *bp = kd->procbase; 453 int acnt, zcnt; 454 struct proc *p; 455 456 if (KREAD(kd, a_allproc, &p)) { 457 _kvm_err(kd, kd->program, "cannot read allproc"); 458 return (-1); 459 } 460 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 461 if (acnt < 0) 462 return (acnt); 463 464 if (KREAD(kd, a_zombproc, &p)) { 465 _kvm_err(kd, kd->program, "cannot read zombproc"); 466 return (-1); 467 } 468 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 469 if (zcnt < 0) 470 zcnt = 0; 471 472 return (acnt + zcnt); 473 } 474 475 struct kinfo_proc * 476 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt) 477 { 478 int mib[4], st, nprocs; 479 size_t size, osize; 480 int temp_op; 481 482 if (kd->procbase != 0) { 483 free((void *)kd->procbase); 484 /* 485 * Clear this pointer in case this call fails. Otherwise, 486 * kvm_close() will free it again. 487 */ 488 kd->procbase = 0; 489 } 490 if (ISALIVE(kd)) { 491 size = 0; 492 mib[0] = CTL_KERN; 493 mib[1] = KERN_PROC; 494 mib[2] = op; 495 mib[3] = arg; 496 temp_op = op & ~KERN_PROC_INC_THREAD; 497 st = sysctl(mib, 498 temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ? 499 3 : 4, NULL, &size, NULL, 0); 500 if (st == -1) { 501 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 502 return (0); 503 } 504 /* 505 * We can't continue with a size of 0 because we pass 506 * it to realloc() (via _kvm_realloc()), and passing 0 507 * to realloc() results in undefined behavior. 508 */ 509 if (size == 0) { 510 /* 511 * XXX: We should probably return an invalid, 512 * but non-NULL, pointer here so any client 513 * program trying to dereference it will 514 * crash. However, _kvm_freeprocs() calls 515 * free() on kd->procbase if it isn't NULL, 516 * and free()'ing a junk pointer isn't good. 517 * Then again, _kvm_freeprocs() isn't used 518 * anywhere . . . 519 */ 520 kd->procbase = _kvm_malloc(kd, 1); 521 goto liveout; 522 } 523 do { 524 size += size / 10; 525 kd->procbase = (struct kinfo_proc *) 526 _kvm_realloc(kd, kd->procbase, size); 527 if (kd->procbase == 0) 528 return (0); 529 osize = size; 530 st = sysctl(mib, temp_op == KERN_PROC_ALL || 531 temp_op == KERN_PROC_PROC ? 3 : 4, 532 kd->procbase, &size, NULL, 0); 533 } while (st == -1 && errno == ENOMEM && size == osize); 534 if (st == -1) { 535 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 536 return (0); 537 } 538 /* 539 * We have to check the size again because sysctl() 540 * may "round up" oldlenp if oldp is NULL; hence it 541 * might've told us that there was data to get when 542 * there really isn't any. 543 */ 544 if (size > 0 && 545 kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { 546 _kvm_err(kd, kd->program, 547 "kinfo_proc size mismatch (expected %zu, got %d)", 548 sizeof(struct kinfo_proc), 549 kd->procbase->ki_structsize); 550 return (0); 551 } 552 liveout: 553 nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; 554 } else { 555 struct nlist nl[7], *p; 556 557 nl[0].n_name = "_nprocs"; 558 nl[1].n_name = "_allproc"; 559 nl[2].n_name = "_zombproc"; 560 nl[3].n_name = "_ticks"; 561 nl[4].n_name = "_hz"; 562 nl[5].n_name = "_cpu_tick_frequency"; 563 nl[6].n_name = 0; 564 565 if (kvm_nlist(kd, nl) != 0) { 566 for (p = nl; p->n_type != 0; ++p) 567 ; 568 _kvm_err(kd, kd->program, 569 "%s: no such symbol", p->n_name); 570 return (0); 571 } 572 if (KREAD(kd, nl[0].n_value, &nprocs)) { 573 _kvm_err(kd, kd->program, "can't read nprocs"); 574 return (0); 575 } 576 if (KREAD(kd, nl[3].n_value, &ticks)) { 577 _kvm_err(kd, kd->program, "can't read ticks"); 578 return (0); 579 } 580 if (KREAD(kd, nl[4].n_value, &hz)) { 581 _kvm_err(kd, kd->program, "can't read hz"); 582 return (0); 583 } 584 if (KREAD(kd, nl[5].n_value, &cpu_tick_frequency)) { 585 _kvm_err(kd, kd->program, 586 "can't read cpu_tick_frequency"); 587 return (0); 588 } 589 size = nprocs * sizeof(struct kinfo_proc); 590 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 591 if (kd->procbase == 0) 592 return (0); 593 594 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 595 nl[2].n_value, nprocs); 596 if (nprocs <= 0) { 597 _kvm_freeprocs(kd); 598 nprocs = 0; 599 } 600 #ifdef notdef 601 else { 602 size = nprocs * sizeof(struct kinfo_proc); 603 kd->procbase = realloc(kd->procbase, size); 604 } 605 #endif 606 } 607 *cnt = nprocs; 608 return (kd->procbase); 609 } 610 611 void 612 _kvm_freeprocs(kvm_t *kd) 613 { 614 if (kd->procbase) { 615 free(kd->procbase); 616 kd->procbase = 0; 617 } 618 } 619 620 void * 621 _kvm_realloc(kvm_t *kd, void *p, 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 /* 633 * Get the command args or environment. 634 */ 635 static char ** 636 kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr) 637 { 638 int oid[4]; 639 int i; 640 size_t bufsz; 641 static int buflen; 642 static char *buf, *p; 643 static char **bufp; 644 static int argc; 645 646 if (!ISALIVE(kd)) { 647 _kvm_err(kd, kd->program, 648 "cannot read user space from dead kernel"); 649 return (0); 650 } 651 652 if (nchr == 0 || nchr > ARG_MAX) 653 nchr = ARG_MAX; 654 if (buflen == 0) { 655 buf = malloc(nchr); 656 if (buf == NULL) { 657 _kvm_err(kd, kd->program, "cannot allocate memory"); 658 return (0); 659 } 660 buflen = nchr; 661 argc = 32; 662 bufp = malloc(sizeof(char *) * argc); 663 } else if (nchr > buflen) { 664 p = realloc(buf, nchr); 665 if (p != NULL) { 666 buf = p; 667 buflen = nchr; 668 } 669 } 670 oid[0] = CTL_KERN; 671 oid[1] = KERN_PROC; 672 oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS; 673 oid[3] = kp->ki_pid; 674 bufsz = buflen; 675 if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) { 676 /* 677 * If the supplied buf is too short to hold the requested 678 * value the sysctl returns with ENOMEM. The buf is filled 679 * with the truncated value and the returned bufsz is equal 680 * to the requested len. 681 */ 682 if (errno != ENOMEM || bufsz != (size_t)buflen) 683 return (0); 684 buf[bufsz - 1] = '\0'; 685 errno = 0; 686 } else if (bufsz == 0) { 687 return (0); 688 } 689 i = 0; 690 p = buf; 691 do { 692 bufp[i++] = p; 693 p += strlen(p) + 1; 694 if (i >= argc) { 695 argc += argc; 696 bufp = realloc(bufp, 697 sizeof(char *) * argc); 698 } 699 } while (p < buf + bufsz); 700 bufp[i++] = 0; 701 return (bufp); 702 } 703 704 char ** 705 kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 706 { 707 return (kvm_argv(kd, kp, 0, nchr)); 708 } 709 710 char ** 711 kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 712 { 713 return (kvm_argv(kd, kp, 1, nchr)); 714 } 715