kvm_proc.c revision 130996
1157184Sache/*- 2157184Sache * Copyright (c) 1989, 1992, 1993 3157184Sache * The Regents of the University of California. All rights reserved. 4157184Sache * 5157184Sache * This code is derived from software developed by the Computer Systems 6157184Sache * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7157184Sache * BG 91-66 and contributed to Berkeley. 8157184Sache * 9157184Sache * Redistribution and use in source and binary forms, with or without 10157184Sache * modification, are permitted provided that the following conditions 11157184Sache * are met: 12157184Sache * 1. Redistributions of source code must retain the above copyright 13157184Sache * notice, this list of conditions and the following disclaimer. 14157184Sache * 2. Redistributions in binary form must reproduce the above copyright 15157184Sache * notice, this list of conditions and the following disclaimer in the 16157184Sache * documentation and/or other materials provided with the distribution. 17157184Sache * 3. All advertising materials mentioning features or use of this software 18157184Sache * must display the following acknowledgement: 19157184Sache * This product includes software developed by the University of 20157184Sache * California, Berkeley and its contributors. 21157184Sache * 4. Neither the name of the University nor the names of its contributors 22157184Sache * may be used to endorse or promote products derived from this software 23157184Sache * without specific prior written permission. 24157184Sache * 25157184Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26157184Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27157184Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28157184Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29157184Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30157184Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31157184Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32157184Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33157184Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34157184Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35157184Sache * SUCH DAMAGE. 36157184Sache */ 37157184Sache 38157184Sache#if 0 39157184Sache#if defined(LIBC_SCCS) && !defined(lint) 40157184Sachestatic char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; 41157184Sache#endif /* LIBC_SCCS and not lint */ 42157184Sache#endif 43157184Sache 44157184Sache#include <sys/cdefs.h> 45157184Sache__FBSDID("$FreeBSD: head/lib/libkvm/kvm_proc.c 130996 2004-06-23 21:59:56Z gad $"); 46157184Sache 47157184Sache/* 48157184Sache * Proc traversal interface for kvm. ps and w are (probably) the exclusive 49157184Sache * users of this code, so we've factored it out into a separate module. 50157184Sache * Thus, we keep this grunge out of the other kvm applications (i.e., 51157184Sache * most other applications are interested only in open/close/read/nlist). 52157184Sache */ 53157184Sache 54157184Sache#include <sys/param.h> 55157184Sache#define _WANT_UCRED /* make ucred.h give us 'struct ucred' */ 56157184Sache#include <sys/ucred.h> 57157184Sache#include <sys/user.h> 58157184Sache#include <sys/proc.h> 59157184Sache#include <sys/exec.h> 60157184Sache#include <sys/stat.h> 61157184Sache#include <sys/sysent.h> 62157184Sache#include <sys/ioctl.h> 63157184Sache#include <sys/tty.h> 64157184Sache#include <sys/file.h> 65157184Sache#include <stdio.h> 66157184Sache#include <stdlib.h> 67157184Sache#include <unistd.h> 68157184Sache#include <nlist.h> 69157184Sache#include <kvm.h> 70157184Sache 71157184Sache#include <vm/vm.h> 72157184Sache#include <vm/vm_param.h> 73157184Sache 74157184Sache#include <sys/sysctl.h> 75157184Sache 76157184Sache#include <limits.h> 77157184Sache#include <memory.h> 78157184Sache#include <paths.h> 79157184Sache 80157184Sache#include "kvm_private.h" 81157184Sache 82157184Sache#define KREAD(kd, addr, obj) \ 83157184Sache (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) 84157184Sache 85157184Sache/* 86157184Sache * Read proc's from memory file into buffer bp, which has space to hold 87157184Sache * at most maxcnt procs. 88157184Sache */ 89157184Sachestatic int 90157184Sachekvm_proclist(kd, what, arg, p, bp, maxcnt) 91157184Sache kvm_t *kd; 92157184Sache int what, arg; 93157184Sache struct proc *p; 94157184Sache struct kinfo_proc *bp; 95157184Sache int maxcnt; 96157184Sache{ 97157184Sache int cnt = 0; 98157184Sache struct kinfo_proc kinfo_proc, *kp; 99157184Sache struct pgrp pgrp; 100157184Sache struct session sess; 101157184Sache struct tty tty; 102157184Sache struct vmspace vmspace; 103157184Sache struct sigacts sigacts; 104157184Sache struct pstats pstats; 105157184Sache struct ucred ucred; 106157184Sache struct thread mtd; 107157184Sache struct kse mke; 108157184Sache struct ksegrp mkg; 109157184Sache struct proc proc; 110157184Sache struct proc pproc; 111157184Sache struct timeval tv; 112157184Sache struct sysentvec sysent; 113157184Sache char svname[KI_EMULNAMELEN]; 114157184Sache 115157184Sache kp = &kinfo_proc; 116157184Sache kp->ki_structsize = sizeof(kinfo_proc); 117157184Sache for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 118157184Sache memset(kp, 0, sizeof *kp); 119157184Sache if (KREAD(kd, (u_long)p, &proc)) { 120157184Sache _kvm_err(kd, kd->program, "can't read proc at %x", p); 121157184Sache return (-1); 122157184Sache } 123157184Sache if (proc.p_state != PRS_ZOMBIE) { 124157184Sache if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads), 125157184Sache &mtd)) { 126157184Sache _kvm_err(kd, kd->program, 127157184Sache "can't read thread at %x", 128157184Sache TAILQ_FIRST(&proc.p_threads)); 129157184Sache return (-1); 130157184Sache } 131157184Sache if ((proc.p_flag & P_SA) == 0) { 132157184Sache if (KREAD(kd, 133157184Sache (u_long)TAILQ_FIRST(&proc.p_ksegrps), 134157184Sache &mkg)) { 135157184Sache _kvm_err(kd, kd->program, 136157184Sache "can't read ksegrp at %x", 137157184Sache TAILQ_FIRST(&proc.p_ksegrps)); 138157184Sache return (-1); 139157184Sache } 140157184Sache if (KREAD(kd, 141157184Sache (u_long)TAILQ_FIRST(&mkg.kg_kseq), &mke)) { 142157184Sache _kvm_err(kd, kd->program, 143157184Sache "can't read kse at %x", 144157184Sache TAILQ_FIRST(&mkg.kg_kseq)); 145157184Sache return (-1); 146157184Sache } 147157184Sache } 148157184Sache } 149157184Sache if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) { 150157184Sache kp->ki_ruid = ucred.cr_ruid; 151157184Sache kp->ki_svuid = ucred.cr_svuid; 152157184Sache kp->ki_rgid = ucred.cr_rgid; 153157184Sache kp->ki_svgid = ucred.cr_svgid; 154157184Sache kp->ki_ngroups = ucred.cr_ngroups; 155157184Sache bcopy(ucred.cr_groups, kp->ki_groups, 156157184Sache NGROUPS * sizeof(gid_t)); 157157184Sache kp->ki_uid = ucred.cr_uid; 158157184Sache } 159157184Sache 160157184Sache switch(what & ~KERN_PROC_INC_THREAD) { 161157184Sache 162157184Sache case KERN_PROC_GID: 163157184Sache if (kp->ki_groups[0] != (gid_t)arg) 164157184Sache continue; 165157184Sache break; 166157184Sache 167157184Sache case KERN_PROC_PID: 168157184Sache if (proc.p_pid != (pid_t)arg) 169157184Sache continue; 170157184Sache break; 171157184Sache 172157184Sache case KERN_PROC_RGID: 173157184Sache if (kp->ki_rgid != (gid_t)arg) 174157184Sache continue; 175157184Sache break; 176157184Sache 177157184Sache case KERN_PROC_UID: 178157184Sache if (kp->ki_uid != (uid_t)arg) 179157184Sache continue; 180157184Sache break; 181157184Sache 182157184Sache case KERN_PROC_RUID: 183157184Sache if (kp->ki_ruid != (uid_t)arg) 184157184Sache continue; 185157184Sache break; 186157184Sache } 187157184Sache /* 188157184Sache * We're going to add another proc to the set. If this 189157184Sache * will overflow the buffer, assume the reason is because 190157184Sache * nprocs (or the proc list) is corrupt and declare an error. 191157184Sache */ 192157184Sache if (cnt >= maxcnt) { 193157184Sache _kvm_err(kd, kd->program, "nprocs corrupt"); 194157184Sache return (-1); 195157184Sache } 196157184Sache /* 197157184Sache * gather kinfo_proc 198157184Sache */ 199157184Sache kp->ki_paddr = p; 200157184Sache kp->ki_addr = proc.p_uarea; 201157184Sache /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ 202157184Sache kp->ki_args = proc.p_args; 203157184Sache kp->ki_tracep = proc.p_tracevp; 204157184Sache kp->ki_textvp = proc.p_textvp; 205157184Sache kp->ki_fd = proc.p_fd; 206157184Sache kp->ki_vmspace = proc.p_vmspace; 207157184Sache if (proc.p_sigacts != NULL) { 208157184Sache if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) { 209157184Sache _kvm_err(kd, kd->program, 210157184Sache "can't read sigacts at %x", proc.p_sigacts); 211157184Sache return (-1); 212157184Sache } 213157184Sache kp->ki_sigignore = sigacts.ps_sigignore; 214157184Sache kp->ki_sigcatch = sigacts.ps_sigcatch; 215157184Sache } 216157184Sache if ((proc.p_sflag & PS_INMEM) && proc.p_stats != NULL) { 217157184Sache if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { 218157184Sache _kvm_err(kd, kd->program, 219157184Sache "can't read stats at %x", proc.p_stats); 220157184Sache return (-1); 221157184Sache } 222157184Sache kp->ki_start = pstats.p_start; 223157184Sache kp->ki_rusage = pstats.p_ru; 224157184Sache kp->ki_childstime = pstats.p_cru.ru_stime; 225157184Sache kp->ki_childutime = pstats.p_cru.ru_utime; 226157184Sache /* Some callers want child-times in a single value */ 227157184Sache timeradd(&kp->ki_childstime, &kp->ki_childutime, 228157184Sache &kp->ki_childtime); 229157184Sache } 230157184Sache if (proc.p_oppid) 231157184Sache kp->ki_ppid = proc.p_oppid; 232157184Sache else if (proc.p_pptr) { 233157184Sache if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { 234157184Sache _kvm_err(kd, kd->program, 235157184Sache "can't read pproc at %x", proc.p_pptr); 236157184Sache return (-1); 237157184Sache } 238157184Sache kp->ki_ppid = pproc.p_pid; 239157184Sache } else 240157184Sache kp->ki_ppid = 0; 241157184Sache if (proc.p_pgrp == NULL) 242157184Sache goto nopgrp; 243157184Sache if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 244157184Sache _kvm_err(kd, kd->program, "can't read pgrp at %x", 245157184Sache proc.p_pgrp); 246157184Sache return (-1); 247157184Sache } 248157184Sache kp->ki_pgid = pgrp.pg_id; 249157184Sache kp->ki_jobc = pgrp.pg_jobc; 250157184Sache if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 251157184Sache _kvm_err(kd, kd->program, "can't read session at %x", 252157184Sache pgrp.pg_session); 253157184Sache return (-1); 254157184Sache } 255157184Sache kp->ki_sid = sess.s_sid; 256157184Sache (void)memcpy(kp->ki_login, sess.s_login, 257157184Sache sizeof(kp->ki_login)); 258157184Sache kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; 259157184Sache if (sess.s_leader == p) 260157184Sache kp->ki_kiflag |= KI_SLEADER; 261157184Sache if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 262157184Sache if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 263157184Sache _kvm_err(kd, kd->program, 264157184Sache "can't read tty at %x", sess.s_ttyp); 265157184Sache return (-1); 266157184Sache } 267157184Sache kp->ki_tdev = tty.t_dev; /* XXX: wrong */ 268157184Sache if (tty.t_pgrp != NULL) { 269157184Sache if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 270157184Sache _kvm_err(kd, kd->program, 271157184Sache "can't read tpgrp at %x", 272157184Sache tty.t_pgrp); 273157184Sache return (-1); 274157184Sache } 275157184Sache kp->ki_tpgid = pgrp.pg_id; 276157184Sache } else 277157184Sache kp->ki_tpgid = -1; 278157184Sache if (tty.t_session != NULL) { 279157184Sache if (KREAD(kd, (u_long)tty.t_session, &sess)) { 280157184Sache _kvm_err(kd, kd->program, 281157184Sache "can't read session at %x", 282157184Sache tty.t_session); 283157184Sache return (-1); 284157184Sache } 285157184Sache kp->ki_tsid = sess.s_sid; 286157184Sache } 287157184Sache } else { 288157184Sachenopgrp: 289157184Sache kp->ki_tdev = NODEV; 290157184Sache } 291157184Sache if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) 292157184Sache (void)kvm_read(kd, (u_long)mtd.td_wmesg, 293157184Sache kp->ki_wmesg, WMESGLEN); 294157184Sache 295157184Sache#ifdef sparc 296157184Sache (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize, 297157184Sache (char *)&kp->ki_rssize, 298157184Sache sizeof(kp->ki_rssize)); 299157184Sache (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize, 300157184Sache (char *)&kp->ki_tsize, 301157184Sache 3 * sizeof(kp->ki_rssize)); /* XXX */ 302157184Sache#else 303157184Sache (void)kvm_read(kd, (u_long)proc.p_vmspace, 304157184Sache (char *)&vmspace, sizeof(vmspace)); 305157184Sache kp->ki_size = vmspace.vm_map.size; 306157184Sache kp->ki_rssize = vmspace.vm_swrss; /* XXX */ 307157184Sache kp->ki_swrss = vmspace.vm_swrss; 308157184Sache kp->ki_tsize = vmspace.vm_tsize; 309157184Sache kp->ki_dsize = vmspace.vm_dsize; 310157184Sache kp->ki_ssize = vmspace.vm_ssize; 311157184Sache#endif 312157184Sache 313157184Sache switch (what & ~KERN_PROC_INC_THREAD) { 314157184Sache 315157184Sache case KERN_PROC_PGRP: 316157184Sache if (kp->ki_pgid != (pid_t)arg) 317157184Sache continue; 318157184Sache break; 319157184Sache 320157184Sache case KERN_PROC_SESSION: 321157184Sache if (kp->ki_sid != (pid_t)arg) 322157184Sache continue; 323157184Sache break; 324157184Sache 325157184Sache case KERN_PROC_TTY: 326157184Sache if ((proc.p_flag & P_CONTROLT) == 0 || 327157184Sache kp->ki_tdev != (dev_t)arg) 328157184Sache continue; 329157184Sache break; 330157184Sache } 331157184Sache if (proc.p_comm[0] != 0) 332157184Sache strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); 333157184Sache (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent, 334157184Sache sizeof(sysent)); 335157184Sache (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname, 336157184Sache sizeof(svname)); 337157184Sache if (svname[0] != 0) 338157184Sache strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN); 339157184Sache if ((proc.p_state != PRS_ZOMBIE) && 340157184Sache (mtd.td_blocked != 0)) { 341157184Sache kp->ki_kiflag |= KI_LOCKBLOCK; 342157184Sache if (mtd.td_lockname) 343157184Sache (void)kvm_read(kd, 344157184Sache (u_long)mtd.td_lockname, 345157184Sache kp->ki_lockname, LOCKNAMELEN); 346157184Sache kp->ki_lockname[LOCKNAMELEN] = 0; 347157184Sache } 348157184Sache bintime2timeval(&proc.p_runtime, &tv); 349157184Sache kp->ki_runtime = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; 350157184Sache kp->ki_pid = proc.p_pid; 351157184Sache kp->ki_siglist = proc.p_siglist; 352157184Sache SIGSETOR(kp->ki_siglist, mtd.td_siglist); 353157184Sache kp->ki_sigmask = mtd.td_sigmask; 354157184Sache kp->ki_xstat = proc.p_xstat; 355157184Sache kp->ki_acflag = proc.p_acflag; 356157184Sache kp->ki_lock = proc.p_lock; 357157184Sache if (proc.p_state != PRS_ZOMBIE) { 358157184Sache kp->ki_swtime = proc.p_swtime; 359157184Sache kp->ki_flag = proc.p_flag; 360157184Sache kp->ki_sflag = proc.p_sflag; 361157184Sache kp->ki_nice = proc.p_nice; 362157184Sache kp->ki_traceflag = proc.p_traceflag; 363157184Sache if (proc.p_state == PRS_NORMAL) { 364157184Sache if (TD_ON_RUNQ(&mtd) || 365157184Sache TD_CAN_RUN(&mtd) || 366157184Sache TD_IS_RUNNING(&mtd)) { 367157184Sache kp->ki_stat = SRUN; 368157184Sache } else if (mtd.td_state == 369157184Sache TDS_INHIBITED) { 370157184Sache if (P_SHOULDSTOP(&proc)) { 371157184Sache kp->ki_stat = SSTOP; 372157184Sache } else if ( 373157184Sache TD_IS_SLEEPING(&mtd)) { 374157184Sache kp->ki_stat = SSLEEP; 375157184Sache } else if (TD_ON_LOCK(&mtd)) { 376157184Sache kp->ki_stat = SLOCK; 377157184Sache } else { 378157184Sache kp->ki_stat = SWAIT; 379157184Sache } 380157184Sache } 381157184Sache } else { 382157184Sache kp->ki_stat = SIDL; 383157184Sache } 384157184Sache /* Stuff from the thread */ 385157184Sache kp->ki_pri.pri_level = mtd.td_priority; 386157184Sache kp->ki_pri.pri_native = mtd.td_base_pri; 387157184Sache kp->ki_lastcpu = mtd.td_lastcpu; 388157184Sache kp->ki_wchan = mtd.td_wchan; 389157184Sache kp->ki_oncpu = mtd.td_oncpu; 390157184Sache 391157184Sache if (!(proc.p_flag & P_SA)) { 392157184Sache /* stuff from the ksegrp */ 393157184Sache kp->ki_slptime = mkg.kg_slptime; 394157184Sache kp->ki_pri.pri_class = mkg.kg_pri_class; 395157184Sache kp->ki_pri.pri_user = mkg.kg_user_pri; 396157184Sache kp->ki_estcpu = mkg.kg_estcpu; 397157184Sache 398157184Sache /* Stuff from the kse */ 399157184Sache kp->ki_pctcpu = mke.ke_pctcpu; 400157184Sache kp->ki_rqindex = mke.ke_rqindex; 401157184Sache } else { 402157184Sache kp->ki_tdflags = -1; 403157184Sache /* All the rest are 0 for now */ 404157184Sache } 405157184Sache } else { 406157184Sache kp->ki_stat = SZOMB; 407157184Sache } 408157184Sache bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); 409157184Sache ++bp; 410157184Sache ++cnt; 411157184Sache } 412157184Sache return (cnt); 413157184Sache} 414157184Sache 415157184Sache/* 416157184Sache * Build proc info array by reading in proc list from a crash dump. 417157184Sache * Return number of procs read. maxcnt is the max we will read. 418157184Sache */ 419157184Sachestatic int 420157184Sachekvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) 421157184Sache kvm_t *kd; 422157184Sache int what, arg; 423157184Sache u_long a_allproc; 424157184Sache u_long a_zombproc; 425157184Sache int maxcnt; 426157184Sache{ 427157184Sache struct kinfo_proc *bp = kd->procbase; 428157184Sache int acnt, zcnt; 429157184Sache struct proc *p; 430157184Sache 431157184Sache if (KREAD(kd, a_allproc, &p)) { 432157184Sache _kvm_err(kd, kd->program, "cannot read allproc"); 433157184Sache return (-1); 434157184Sache } 435157184Sache acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 436157184Sache if (acnt < 0) 437157184Sache return (acnt); 438157184Sache 439157184Sache if (KREAD(kd, a_zombproc, &p)) { 440157184Sache _kvm_err(kd, kd->program, "cannot read zombproc"); 441157184Sache return (-1); 442157184Sache } 443157184Sache zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 444157184Sache if (zcnt < 0) 445157184Sache zcnt = 0; 446157184Sache 447157184Sache return (acnt + zcnt); 448157184Sache} 449157184Sache 450157184Sachestruct kinfo_proc * 451157184Sachekvm_getprocs(kd, op, arg, cnt) 452157184Sache kvm_t *kd; 453157184Sache int op, arg; 454157184Sache int *cnt; 455157184Sache{ 456157184Sache int mib[4], st, nprocs; 457157184Sache size_t size; 458157184Sache int temp_op; 459157184Sache 460157184Sache if (kd->procbase != 0) { 461157184Sache free((void *)kd->procbase); 462157184Sache /* 463157184Sache * Clear this pointer in case this call fails. Otherwise, 464157184Sache * kvm_close() will free it again. 465157184Sache */ 466157184Sache kd->procbase = 0; 467157184Sache } 468157184Sache if (ISALIVE(kd)) { 469157184Sache size = 0; 470157184Sache mib[0] = CTL_KERN; 471157184Sache mib[1] = KERN_PROC; 472157184Sache mib[2] = op; 473157184Sache mib[3] = arg; 474157184Sache temp_op = op & ~KERN_PROC_INC_THREAD; 475157184Sache st = sysctl(mib, 476157184Sache temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ? 477157184Sache 3 : 4, NULL, &size, NULL, 0); 478157184Sache if (st == -1) { 479157184Sache _kvm_syserr(kd, kd->program, "kvm_getprocs"); 480157184Sache return (0); 481157184Sache } 482157184Sache /* 483157184Sache * We can't continue with a size of 0 because we pass 484157184Sache * it to realloc() (via _kvm_realloc()), and passing 0 485157184Sache * to realloc() results in undefined behavior. 486157184Sache */ 487157184Sache if (size == 0) { 488157184Sache /* 489157184Sache * XXX: We should probably return an invalid, 490157184Sache * but non-NULL, pointer here so any client 491157184Sache * program trying to dereference it will 492157184Sache * crash. However, _kvm_freeprocs() calls 493157184Sache * free() on kd->procbase if it isn't NULL, 494157184Sache * and free()'ing a junk pointer isn't good. 495157184Sache * Then again, _kvm_freeprocs() isn't used 496157184Sache * anywhere . . . 497157184Sache */ 498157184Sache kd->procbase = _kvm_malloc(kd, 1); 499157184Sache goto liveout; 500157184Sache } 501157184Sache do { 502157184Sache size += size / 10; 503157184Sache kd->procbase = (struct kinfo_proc *) 504157184Sache _kvm_realloc(kd, kd->procbase, size); 505157184Sache if (kd->procbase == 0) 506157184Sache return (0); 507157184Sache st = sysctl(mib, temp_op == KERN_PROC_ALL || 508157184Sache temp_op == KERN_PROC_PROC ? 3 : 4, 509157184Sache kd->procbase, &size, NULL, 0); 510157184Sache } while (st == -1 && errno == ENOMEM); 511157184Sache if (st == -1) { 512157184Sache _kvm_syserr(kd, kd->program, "kvm_getprocs"); 513157184Sache return (0); 514157184Sache } 515157184Sache /* 516157184Sache * We have to check the size again because sysctl() 517157184Sache * may "round up" oldlenp if oldp is NULL; hence it 518157184Sache * might've told us that there was data to get when 519157184Sache * there really isn't any. 520157184Sache */ 521157184Sache if (size > 0 && 522157184Sache kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { 523157184Sache _kvm_err(kd, kd->program, 524157184Sache "kinfo_proc size mismatch (expected %d, got %d)", 525157184Sache sizeof(struct kinfo_proc), 526157184Sache kd->procbase->ki_structsize); 527157184Sache return (0); 528157184Sache } 529157184Sacheliveout: 530157184Sache nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; 531 } else { 532 struct nlist nl[4], *p; 533 534 nl[0].n_name = "_nprocs"; 535 nl[1].n_name = "_allproc"; 536 nl[2].n_name = "_zombproc"; 537 nl[3].n_name = 0; 538 539 if (kvm_nlist(kd, nl) != 0) { 540 for (p = nl; p->n_type != 0; ++p) 541 ; 542 _kvm_err(kd, kd->program, 543 "%s: no such symbol", p->n_name); 544 return (0); 545 } 546 if (KREAD(kd, nl[0].n_value, &nprocs)) { 547 _kvm_err(kd, kd->program, "can't read nprocs"); 548 return (0); 549 } 550 size = nprocs * sizeof(struct kinfo_proc); 551 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 552 if (kd->procbase == 0) 553 return (0); 554 555 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 556 nl[2].n_value, nprocs); 557#ifdef notdef 558 size = nprocs * sizeof(struct kinfo_proc); 559 (void)realloc(kd->procbase, size); 560#endif 561 } 562 *cnt = nprocs; 563 return (kd->procbase); 564} 565 566void 567_kvm_freeprocs(kd) 568 kvm_t *kd; 569{ 570 if (kd->procbase) { 571 free(kd->procbase); 572 kd->procbase = 0; 573 } 574} 575 576void * 577_kvm_realloc(kd, p, n) 578 kvm_t *kd; 579 void *p; 580 size_t n; 581{ 582 void *np = (void *)realloc(p, n); 583 584 if (np == 0) { 585 free(p); 586 _kvm_err(kd, kd->program, "out of memory"); 587 } 588 return (np); 589} 590 591#ifndef MAX 592#define MAX(a, b) ((a) > (b) ? (a) : (b)) 593#endif 594 595/* 596 * Read in an argument vector from the user address space of process kp. 597 * addr if the user-space base address of narg null-terminated contiguous 598 * strings. This is used to read in both the command arguments and 599 * environment strings. Read at most maxcnt characters of strings. 600 */ 601static char ** 602kvm_argv(kd, kp, addr, narg, maxcnt) 603 kvm_t *kd; 604 struct kinfo_proc *kp; 605 u_long addr; 606 int narg; 607 int maxcnt; 608{ 609 char *np, *cp, *ep, *ap; 610 u_long oaddr = -1; 611 int len, cc; 612 char **argv; 613 614 /* 615 * Check that there aren't an unreasonable number of agruments, 616 * and that the address is in user space. 617 */ 618 if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) 619 return (0); 620 621 /* 622 * kd->argv : work space for fetching the strings from the target 623 * process's space, and is converted for returning to caller 624 */ 625 if (kd->argv == 0) { 626 /* 627 * Try to avoid reallocs. 628 */ 629 kd->argc = MAX(narg + 1, 32); 630 kd->argv = (char **)_kvm_malloc(kd, kd->argc * 631 sizeof(*kd->argv)); 632 if (kd->argv == 0) 633 return (0); 634 } else if (narg + 1 > kd->argc) { 635 kd->argc = MAX(2 * kd->argc, narg + 1); 636 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 637 sizeof(*kd->argv)); 638 if (kd->argv == 0) 639 return (0); 640 } 641 /* 642 * kd->argspc : returned to user, this is where the kd->argv 643 * arrays are left pointing to the collected strings. 644 */ 645 if (kd->argspc == 0) { 646 kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE); 647 if (kd->argspc == 0) 648 return (0); 649 kd->arglen = PAGE_SIZE; 650 } 651 /* 652 * kd->argbuf : used to pull in pages from the target process. 653 * the strings are copied out of here. 654 */ 655 if (kd->argbuf == 0) { 656 kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE); 657 if (kd->argbuf == 0) 658 return (0); 659 } 660 661 /* Pull in the target process'es argv vector */ 662 cc = sizeof(char *) * narg; 663 if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc) 664 return (0); 665 /* 666 * ap : saved start address of string we're working on in kd->argspc 667 * np : pointer to next place to write in kd->argspc 668 * len: length of data in kd->argspc 669 * argv: pointer to the argv vector that we are hunting around the 670 * target process space for, and converting to addresses in 671 * our address space (kd->argspc). 672 */ 673 ap = np = kd->argspc; 674 argv = kd->argv; 675 len = 0; 676 /* 677 * Loop over pages, filling in the argument vector. 678 * Note that the argv strings could be pointing *anywhere* in 679 * the user address space and are no longer contiguous. 680 * Note that *argv is modified when we are going to fetch a string 681 * that crosses a page boundary. We copy the next part of the string 682 * into to "np" and eventually convert the pointer. 683 */ 684 while (argv < kd->argv + narg && *argv != 0) { 685 686 /* get the address that the current argv string is on */ 687 addr = (u_long)*argv & ~(PAGE_SIZE - 1); 688 689 /* is it the same page as the last one? */ 690 if (addr != oaddr) { 691 if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) != 692 PAGE_SIZE) 693 return (0); 694 oaddr = addr; 695 } 696 697 /* offset within the page... kd->argbuf */ 698 addr = (u_long)*argv & (PAGE_SIZE - 1); 699 700 /* cp = start of string, cc = count of chars in this chunk */ 701 cp = kd->argbuf + addr; 702 cc = PAGE_SIZE - addr; 703 704 /* dont get more than asked for by user process */ 705 if (maxcnt > 0 && cc > maxcnt - len) 706 cc = maxcnt - len; 707 708 /* pointer to end of string if we found it in this page */ 709 ep = memchr(cp, '\0', cc); 710 if (ep != 0) 711 cc = ep - cp + 1; 712 /* 713 * at this point, cc is the count of the chars that we are 714 * going to retrieve this time. we may or may not have found 715 * the end of it. (ep points to the null if the end is known) 716 */ 717 718 /* will we exceed the malloc/realloced buffer? */ 719 if (len + cc > kd->arglen) { 720 int off; 721 char **pp; 722 char *op = kd->argspc; 723 724 kd->arglen *= 2; 725 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 726 kd->arglen); 727 if (kd->argspc == 0) 728 return (0); 729 /* 730 * Adjust argv pointers in case realloc moved 731 * the string space. 732 */ 733 off = kd->argspc - op; 734 for (pp = kd->argv; pp < argv; pp++) 735 *pp += off; 736 ap += off; 737 np += off; 738 } 739 /* np = where to put the next part of the string in kd->argspc*/ 740 /* np is kinda redundant.. could use "kd->argspc + len" */ 741 memcpy(np, cp, cc); 742 np += cc; /* inc counters */ 743 len += cc; 744 745 /* 746 * if end of string found, set the *argv pointer to the 747 * saved beginning of string, and advance. argv points to 748 * somewhere in kd->argv.. This is initially relative 749 * to the target process, but when we close it off, we set 750 * it to point in our address space. 751 */ 752 if (ep != 0) { 753 *argv++ = ap; 754 ap = np; 755 } else { 756 /* update the address relative to the target process */ 757 *argv += cc; 758 } 759 760 if (maxcnt > 0 && len >= maxcnt) { 761 /* 762 * We're stopping prematurely. Terminate the 763 * current string. 764 */ 765 if (ep == 0) { 766 *np = '\0'; 767 *argv++ = ap; 768 } 769 break; 770 } 771 } 772 /* Make sure argv is terminated. */ 773 *argv = 0; 774 return (kd->argv); 775} 776 777static void 778ps_str_a(p, addr, n) 779 struct ps_strings *p; 780 u_long *addr; 781 int *n; 782{ 783 *addr = (u_long)p->ps_argvstr; 784 *n = p->ps_nargvstr; 785} 786 787static void 788ps_str_e(p, addr, n) 789 struct ps_strings *p; 790 u_long *addr; 791 int *n; 792{ 793 *addr = (u_long)p->ps_envstr; 794 *n = p->ps_nenvstr; 795} 796 797/* 798 * Determine if the proc indicated by p is still active. 799 * This test is not 100% foolproof in theory, but chances of 800 * being wrong are very low. 801 */ 802static int 803proc_verify(curkp) 804 struct kinfo_proc *curkp; 805{ 806 struct kinfo_proc newkp; 807 int mib[4]; 808 size_t len; 809 810 mib[0] = CTL_KERN; 811 mib[1] = KERN_PROC; 812 mib[2] = KERN_PROC_PID; 813 mib[3] = curkp->ki_pid; 814 len = sizeof(newkp); 815 if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1) 816 return (0); 817 return (curkp->ki_pid == newkp.ki_pid && 818 (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB)); 819} 820 821static char ** 822kvm_doargv(kd, kp, nchr, info) 823 kvm_t *kd; 824 struct kinfo_proc *kp; 825 int nchr; 826 void (*info)(struct ps_strings *, u_long *, int *); 827{ 828 char **ap; 829 u_long addr; 830 int cnt; 831 static struct ps_strings arginfo; 832 static u_long ps_strings; 833 size_t len; 834 835 if (ps_strings == 0) { 836 len = sizeof(ps_strings); 837 if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL, 838 0) == -1) 839 ps_strings = PS_STRINGS; 840 } 841 842 /* 843 * Pointers are stored at the top of the user stack. 844 */ 845 if (kp->ki_stat == SZOMB || 846 kvm_uread(kd, kp, ps_strings, (char *)&arginfo, 847 sizeof(arginfo)) != sizeof(arginfo)) 848 return (0); 849 850 (*info)(&arginfo, &addr, &cnt); 851 if (cnt == 0) 852 return (0); 853 ap = kvm_argv(kd, kp, addr, cnt, nchr); 854 /* 855 * For live kernels, make sure this process didn't go away. 856 */ 857 if (ap != 0 && ISALIVE(kd) && !proc_verify(kp)) 858 ap = 0; 859 return (ap); 860} 861 862/* 863 * Get the command args. This code is now machine independent. 864 */ 865char ** 866kvm_getargv(kd, kp, nchr) 867 kvm_t *kd; 868 const struct kinfo_proc *kp; 869 int nchr; 870{ 871 int oid[4]; 872 int i; 873 size_t bufsz; 874 static unsigned long buflen; 875 static char *buf, *p; 876 static char **bufp; 877 static int argc; 878 879 if (!ISALIVE(kd)) { 880 _kvm_err(kd, kd->program, 881 "cannot read user space from dead kernel"); 882 return (0); 883 } 884 885 if (!buflen) { 886 bufsz = sizeof(buflen); 887 i = sysctlbyname("kern.ps_arg_cache_limit", 888 &buflen, &bufsz, NULL, 0); 889 if (i == -1) { 890 buflen = 0; 891 } else { 892 buf = malloc(buflen); 893 if (buf == NULL) 894 buflen = 0; 895 argc = 32; 896 bufp = malloc(sizeof(char *) * argc); 897 } 898 } 899 if (buf != NULL) { 900 oid[0] = CTL_KERN; 901 oid[1] = KERN_PROC; 902 oid[2] = KERN_PROC_ARGS; 903 oid[3] = kp->ki_pid; 904 bufsz = buflen; 905 i = sysctl(oid, 4, buf, &bufsz, 0, 0); 906 if (i == 0 && bufsz > 0) { 907 i = 0; 908 p = buf; 909 do { 910 bufp[i++] = p; 911 p += strlen(p) + 1; 912 if (i >= argc) { 913 argc += argc; 914 bufp = realloc(bufp, 915 sizeof(char *) * argc); 916 } 917 } while (p < buf + bufsz); 918 bufp[i++] = 0; 919 return (bufp); 920 } 921 } 922 if (kp->ki_flag & P_SYSTEM) 923 return (NULL); 924 return (kvm_doargv(kd, kp, nchr, ps_str_a)); 925} 926 927char ** 928kvm_getenvv(kd, kp, nchr) 929 kvm_t *kd; 930 const struct kinfo_proc *kp; 931 int nchr; 932{ 933 return (kvm_doargv(kd, kp, nchr, ps_str_e)); 934} 935 936/* 937 * Read from user space. The user context is given by p. 938 */ 939ssize_t 940kvm_uread(kd, kp, uva, buf, len) 941 kvm_t *kd; 942 struct kinfo_proc *kp; 943 u_long uva; 944 char *buf; 945 size_t len; 946{ 947 char *cp; 948 char procfile[MAXPATHLEN]; 949 ssize_t amount; 950 int fd; 951 952 if (!ISALIVE(kd)) { 953 _kvm_err(kd, kd->program, 954 "cannot read user space from dead kernel"); 955 return (0); 956 } 957 958 sprintf(procfile, "/proc/%d/mem", kp->ki_pid); 959 fd = open(procfile, O_RDONLY, 0); 960 if (fd < 0) { 961 _kvm_err(kd, kd->program, "cannot open %s", procfile); 962 close(fd); 963 return (0); 964 } 965 966 cp = buf; 967 while (len > 0) { 968 errno = 0; 969 if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) { 970 _kvm_err(kd, kd->program, "invalid address (%x) in %s", 971 uva, procfile); 972 break; 973 } 974 amount = read(fd, cp, len); 975 if (amount < 0) { 976 _kvm_syserr(kd, kd->program, "error reading %s", 977 procfile); 978 break; 979 } 980 if (amount == 0) { 981 _kvm_err(kd, kd->program, "EOF reading %s", procfile); 982 break; 983 } 984 cp += amount; 985 uva += amount; 986 len -= amount; 987 } 988 989 close(fd); 990 return ((ssize_t)(cp - buf)); 991} 992