139963Smarc /* 239963Smarc * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 339963Smarc * All rights reserved. 439963Smarc * 544435Sbostic * %sccs.include.redist.c% 639963Smarc * 7*51727Sralph * @(#)kern_sysctl.c 7.19 (Berkeley) 11/16/91 839963Smarc */ 939963Smarc 1039965Smckusick #include "param.h" 1139965Smckusick #include "proc.h" 1239963Smarc #include "kinfo.h" 1339963Smarc #include "ioctl.h" 1439963Smarc #include "tty.h" 1539963Smarc #include "buf.h" 1650149Smarc #include "file.h" 1739963Smarc 1848407Skarels #include "vm/vm.h" 1948407Skarels 2048407Skarels #include "kinfo_proc.h" 2148407Skarels 2240068Smarc #define snderr(e) { error = (e); goto release;} 2350149Smarc extern int kinfo_doproc(), kinfo_rtable(), kinfo_vnode(), kinfo_file(); 2450909Smckusick extern int kinfo_meter(); 2540068Smarc struct kinfo_lock kinfo_lock; 2640068Smarc 2743444Smckusick /* ARGSUSED */ 2843444Smckusick getkerninfo(p, uap, retval) 2943444Smckusick struct proc *p; 3043444Smckusick register struct args { 3139963Smarc int op; 3239963Smarc char *where; 3339963Smarc int *size; 3439963Smarc int arg; 3543444Smckusick } *uap; 3643444Smckusick int *retval; 3743444Smckusick { 3847545Skarels int bufsize; /* max size of users buffer */ 3947545Skarels int needed, locked, (*server)(), error = 0; 4039963Smarc 4139963Smarc switch (ki_type(uap->op)) { 4239963Smarc 4339963Smarc case KINFO_PROC: 4440068Smarc server = kinfo_doproc; 4539963Smarc break; 4639963Smarc 4740068Smarc case KINFO_RT: 4840068Smarc server = kinfo_rtable; 4940068Smarc break; 5040068Smarc 5141181Smarc case KINFO_VNODE: 5241181Smarc server = kinfo_vnode; 5341181Smarc break; 5441181Smarc 5550149Smarc case KINFO_FILE: 5650149Smarc server = kinfo_file; 5750149Smarc break; 5850149Smarc 5950909Smckusick case KINFO_METER: 6050909Smckusick server = kinfo_meter; 6150909Smckusick break; 6250909Smckusick 6339963Smarc default: 6440206Smarc error = EINVAL; 6540813Smarc goto done; 6639963Smarc } 6740813Smarc if (uap->where == NULL || uap->size == NULL) { 6840813Smarc error = (*server)(uap->op, NULL, NULL, uap->arg, &needed); 6940813Smarc goto done; 7040813Smarc } 71*51727Sralph if (error = copyin((caddr_t)uap->size, (caddr_t)&bufsize, 72*51727Sralph sizeof (bufsize))) 73*51727Sralph goto done; 7440206Smarc while (kinfo_lock.kl_lock) { 7540206Smarc kinfo_lock.kl_want++; 7640206Smarc sleep(&kinfo_lock, PRIBIO+1); 7740206Smarc kinfo_lock.kl_want--; 7840206Smarc kinfo_lock.kl_locked++; 7940206Smarc } 8040206Smarc kinfo_lock.kl_lock++; 8140206Smarc 8240813Smarc if (!useracc(uap->where, bufsize, B_WRITE)) 8340068Smarc snderr(EFAULT); 8441181Smarc if (server != kinfo_vnode) /* XXX */ 8541181Smarc vslock(uap->where, bufsize); 8640813Smarc locked = bufsize; 8740813Smarc error = (*server)(uap->op, uap->where, &bufsize, uap->arg, &needed); 8841181Smarc if (server != kinfo_vnode) /* XXX */ 8941181Smarc vsunlock(uap->where, locked, B_WRITE); 9040813Smarc if (error == 0) 9140813Smarc error = copyout((caddr_t)&bufsize, 9240813Smarc (caddr_t)uap->size, sizeof (bufsize)); 9340068Smarc release: 9440068Smarc kinfo_lock.kl_lock--; 9540068Smarc if (kinfo_lock.kl_want) 9640068Smarc wakeup(&kinfo_lock); 9740813Smarc done: 9843444Smckusick if (!error) 9943444Smckusick *retval = needed; 10043444Smckusick return (error); 10139963Smarc } 10239963Smarc 10339963Smarc /* 10439963Smarc * try over estimating by 5 procs 10539963Smarc */ 10639963Smarc #define KINFO_PROCSLOP (5 * sizeof (struct kinfo_proc)) 10739963Smarc 10840206Smarc kinfo_doproc(op, where, acopysize, arg, aneeded) 10939963Smarc char *where; 11039963Smarc int *acopysize, *aneeded; 11139963Smarc { 11239963Smarc register struct proc *p; 11343419Smarc register struct kinfo_proc *dp = (struct kinfo_proc *)where; 11439963Smarc register needed = 0; 11539963Smarc int buflen; 11639963Smarc int doingzomb; 11740067Smarc struct eproc eproc; 11839963Smarc int error = 0; 11939963Smarc 12039963Smarc if (where != NULL) 12139963Smarc buflen = *acopysize; 12239963Smarc 12339963Smarc p = allproc; 12439963Smarc doingzomb = 0; 12539963Smarc again: 12639963Smarc for (; p != NULL; p = p->p_nxt) { 12739963Smarc /* 12839963Smarc * TODO - make more efficient (see notes below). 12939963Smarc * do by session. 13039963Smarc */ 13139963Smarc switch (ki_op(op)) { 13239963Smarc 13339963Smarc case KINFO_PROC_PID: 13439963Smarc /* could do this with just a lookup */ 13539963Smarc if (p->p_pid != (pid_t)arg) 13639963Smarc continue; 13739963Smarc break; 13839963Smarc 13939963Smarc case KINFO_PROC_PGRP: 14039963Smarc /* could do this by traversing pgrp */ 14139963Smarc if (p->p_pgrp->pg_id != (pid_t)arg) 14239963Smarc continue; 14339963Smarc break; 14439963Smarc 14539963Smarc case KINFO_PROC_TTY: 14639963Smarc if ((p->p_flag&SCTTY) == 0 || 14739963Smarc p->p_session->s_ttyp == NULL || 14839963Smarc p->p_session->s_ttyp->t_dev != (dev_t)arg) 14939963Smarc continue; 15039963Smarc break; 15139963Smarc 15239963Smarc case KINFO_PROC_UID: 15347545Skarels if (p->p_ucred->cr_uid != (uid_t)arg) 15439963Smarc continue; 15539963Smarc break; 15639963Smarc 15739963Smarc case KINFO_PROC_RUID: 15847545Skarels if (p->p_cred->p_ruid != (uid_t)arg) 15939963Smarc continue; 16039963Smarc break; 16139963Smarc } 16239963Smarc if (where != NULL && buflen >= sizeof (struct kinfo_proc)) { 16348407Skarels fill_eproc(p, &eproc); 16443419Smarc if (error = copyout((caddr_t)p, &dp->kp_proc, 16539963Smarc sizeof (struct proc))) 16639963Smarc return (error); 16743419Smarc if (error = copyout((caddr_t)&eproc, &dp->kp_eproc, 16840067Smarc sizeof (eproc))) 16939963Smarc return (error); 17043419Smarc dp++; 17139963Smarc buflen -= sizeof (struct kinfo_proc); 17239963Smarc } 17339963Smarc needed += sizeof (struct kinfo_proc); 17439963Smarc } 17539963Smarc if (doingzomb == 0) { 17639963Smarc p = zombproc; 17739963Smarc doingzomb++; 17839963Smarc goto again; 17939963Smarc } 18039963Smarc if (where != NULL) 18143419Smarc *acopysize = (caddr_t)dp - where; 18240068Smarc else 18340068Smarc needed += KINFO_PROCSLOP; 18439963Smarc *aneeded = needed; 18539963Smarc 18639963Smarc return (0); 18739963Smarc } 18848407Skarels 18948407Skarels /* 19048407Skarels * Fill in an eproc structure for the specified process. 19148407Skarels */ 19248407Skarels void 19348407Skarels fill_eproc(p, ep) 19448407Skarels register struct proc *p; 19548407Skarels register struct eproc *ep; 19648407Skarels { 19748407Skarels register struct tty *tp; 19848407Skarels 19948407Skarels ep->e_paddr = p; 20048407Skarels ep->e_sess = p->p_pgrp->pg_session; 20148407Skarels ep->e_pcred = *p->p_cred; 20248407Skarels ep->e_ucred = *p->p_ucred; 20348407Skarels ep->e_vm = *p->p_vmspace; 20449141Skarels if (p->p_pptr) 20549141Skarels ep->e_ppid = p->p_pptr->p_pid; 20649141Skarels else 20749141Skarels ep->e_ppid = 0; 20848407Skarels ep->e_pgid = p->p_pgrp->pg_id; 20948407Skarels ep->e_jobc = p->p_pgrp->pg_jobc; 21048407Skarels if ((p->p_flag&SCTTY) && 21148407Skarels (tp = ep->e_sess->s_ttyp)) { 21248407Skarels ep->e_tdev = tp->t_dev; 21350022Skarels ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 21448407Skarels ep->e_tsess = tp->t_session; 21548407Skarels } else 21648407Skarels ep->e_tdev = NODEV; 21748407Skarels ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0; 21848407Skarels if (SESS_LEADER(p)) 21948407Skarels ep->e_flag |= EPROC_SLEADER; 22048407Skarels if (p->p_wmesg) 22148407Skarels strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 22248407Skarels ep->e_xsize = ep->e_xrssize = 0; 22348407Skarels ep->e_xccount = ep->e_xswrss = 0; 22448407Skarels } 22550149Smarc 22650149Smarc /* 22750149Smarc * Get file structures. 22850149Smarc */ 22950149Smarc kinfo_file(op, where, acopysize, arg, aneeded) 23050149Smarc register char *where; 23150149Smarc int *acopysize, *aneeded; 23250149Smarc { 23350149Smarc int buflen, needed, error; 23450149Smarc struct file *fp; 23550149Smarc char *start = where; 23650149Smarc 23750149Smarc if (where == NULL) { 23850149Smarc /* 23950149Smarc * overestimate by 10 files 24050149Smarc */ 24150149Smarc *aneeded = sizeof (filehead) + 24250149Smarc (nfiles + 10) * sizeof (struct file); 24350149Smarc return (0); 24450149Smarc } 24550149Smarc buflen = *acopysize; 24650149Smarc needed = 0; 24750149Smarc 24850149Smarc /* 24950149Smarc * first copyout filehead 25050149Smarc */ 25150149Smarc if (buflen > sizeof (filehead)) { 25250149Smarc if (error = copyout((caddr_t)&filehead, where, 25350149Smarc sizeof (filehead))) 25450149Smarc return (error); 25550149Smarc buflen -= sizeof (filehead); 25650149Smarc where += sizeof (filehead); 25750149Smarc } 25850149Smarc needed += sizeof (filehead); 25950149Smarc 26050149Smarc /* 26150149Smarc * followed by an array of file structures 26250149Smarc */ 26350149Smarc for (fp = filehead; fp != NULL; fp = fp->f_filef) { 26450149Smarc if (buflen > sizeof (struct file)) { 26550149Smarc if (error = copyout((caddr_t)fp, where, 26650149Smarc sizeof (struct file))) 26750149Smarc return (error); 26850149Smarc buflen -= sizeof (struct file); 26950149Smarc where += sizeof (struct file); 27050149Smarc } 27150149Smarc needed += sizeof (struct file); 27250149Smarc } 27350149Smarc *acopysize = where - start; 27450149Smarc *aneeded = needed; 27550149Smarc 27650149Smarc return (0); 27750149Smarc } 278