139963Smarc /* 239963Smarc * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 339963Smarc * All rights reserved. 439963Smarc * 539963Smarc * Redistribution and use in source and binary forms are permitted 639963Smarc * provided that the above copyright notice and this paragraph are 739963Smarc * duplicated in all such forms and that any documentation, 839963Smarc * advertising materials, and other materials related to such 939963Smarc * distribution and use acknowledge that the software was developed 1039963Smarc * by the University of California, Berkeley. The name of the 1139963Smarc * University may not be used to endorse or promote products derived 1239963Smarc * from this software without specific prior written permission. 1339963Smarc * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1439963Smarc * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1539963Smarc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1639963Smarc * 17*43419Smarc * @(#)kern_sysctl.c 7.9 (Berkeley) 06/22/90 1839963Smarc */ 1939963Smarc 2039965Smckusick #include "param.h" 2139965Smckusick #include "user.h" 2239965Smckusick #include "proc.h" 2340206Smarc #include "text.h" 2439963Smarc #include "kinfo.h" 2539963Smarc #include "vm.h" 2639963Smarc #include "ioctl.h" 2739963Smarc #include "tty.h" 2839963Smarc #include "buf.h" 2939963Smarc 3040068Smarc 3140068Smarc #define snderr(e) { error = (e); goto release;} 3241181Smarc extern int kinfo_doproc(), kinfo_rtable(), kinfo_vnode(); 3340068Smarc struct kinfo_lock kinfo_lock; 3440068Smarc 3540206Smarc getkerninfo() 3639963Smarc { 3739963Smarc register struct a { 3839963Smarc int op; 3939963Smarc char *where; 4039963Smarc int *size; 4139963Smarc int arg; 4239963Smarc } *uap = (struct a *)u.u_ap; 4340206Smarc 4440068Smarc int bufsize, /* max size of users buffer */ 4540206Smarc needed, locked, (*server)(), error = 0; 4639963Smarc 4740206Smarc if (error = copyin((caddr_t)uap->size, 4840206Smarc (caddr_t)&bufsize, sizeof (bufsize))) 4940813Smarc goto done; 5040068Smarc 5139963Smarc switch (ki_type(uap->op)) { 5239963Smarc 5339963Smarc case KINFO_PROC: 5440068Smarc server = kinfo_doproc; 5539963Smarc break; 5639963Smarc 5740068Smarc case KINFO_RT: 5840068Smarc server = kinfo_rtable; 5940068Smarc break; 6040068Smarc 6141181Smarc case KINFO_VNODE: 6241181Smarc server = kinfo_vnode; 6341181Smarc break; 6441181Smarc 6539963Smarc default: 6640206Smarc error = EINVAL; 6740813Smarc goto done; 6839963Smarc } 6940813Smarc if (uap->where == NULL || uap->size == NULL) { 7040813Smarc error = (*server)(uap->op, NULL, NULL, uap->arg, &needed); 7140813Smarc goto done; 7240813Smarc } 7340206Smarc while (kinfo_lock.kl_lock) { 7440206Smarc kinfo_lock.kl_want++; 7540206Smarc sleep(&kinfo_lock, PRIBIO+1); 7640206Smarc kinfo_lock.kl_want--; 7740206Smarc kinfo_lock.kl_locked++; 7840206Smarc } 7940206Smarc kinfo_lock.kl_lock++; 8040206Smarc 8140813Smarc if (!useracc(uap->where, bufsize, B_WRITE)) 8240068Smarc snderr(EFAULT); 8340068Smarc /* 8440068Smarc * lock down target pages - NEED DEADLOCK AVOIDANCE 8540068Smarc */ 8640813Smarc if (bufsize > ((int)ptob(freemem) - (20 * 1024))) /* XXX */ 8740068Smarc snderr(ENOMEM); 8841181Smarc if (server != kinfo_vnode) /* XXX */ 8941181Smarc vslock(uap->where, bufsize); 9040813Smarc locked = bufsize; 9140813Smarc error = (*server)(uap->op, uap->where, &bufsize, uap->arg, &needed); 9241181Smarc if (server != kinfo_vnode) /* XXX */ 9341181Smarc vsunlock(uap->where, locked, B_WRITE); 9440813Smarc if (error == 0) 9540813Smarc error = copyout((caddr_t)&bufsize, 9640813Smarc (caddr_t)uap->size, sizeof (bufsize)); 9740068Smarc release: 9840068Smarc kinfo_lock.kl_lock--; 9940068Smarc if (kinfo_lock.kl_want) 10040068Smarc wakeup(&kinfo_lock); 10140813Smarc done: 10240068Smarc if (error) 10340068Smarc u.u_error = error; 10440068Smarc else 10540068Smarc u.u_r.r_val1 = needed; 10639963Smarc } 10739963Smarc 10839963Smarc /* 10939963Smarc * try over estimating by 5 procs 11039963Smarc */ 11139963Smarc #define KINFO_PROCSLOP (5 * sizeof (struct kinfo_proc)) 11239963Smarc 11340206Smarc kinfo_doproc(op, where, acopysize, arg, aneeded) 11439963Smarc char *where; 11539963Smarc int *acopysize, *aneeded; 11639963Smarc { 11739963Smarc register struct proc *p; 118*43419Smarc register struct kinfo_proc *dp = (struct kinfo_proc *)where; 11939963Smarc register needed = 0; 12039963Smarc int buflen; 12139963Smarc int doingzomb; 12240067Smarc struct eproc eproc; 12339963Smarc int error = 0; 12439963Smarc 12539963Smarc if (where != NULL) 12639963Smarc buflen = *acopysize; 12739963Smarc 12839963Smarc p = allproc; 12939963Smarc doingzomb = 0; 13039963Smarc again: 13139963Smarc for (; p != NULL; p = p->p_nxt) { 13239963Smarc /* 13339963Smarc * TODO - make more efficient (see notes below). 13439963Smarc * do by session. 13539963Smarc */ 13639963Smarc switch (ki_op(op)) { 13739963Smarc 13839963Smarc case KINFO_PROC_PID: 13939963Smarc /* could do this with just a lookup */ 14039963Smarc if (p->p_pid != (pid_t)arg) 14139963Smarc continue; 14239963Smarc break; 14339963Smarc 14439963Smarc case KINFO_PROC_PGRP: 14539963Smarc /* could do this by traversing pgrp */ 14639963Smarc if (p->p_pgrp->pg_id != (pid_t)arg) 14739963Smarc continue; 14839963Smarc break; 14939963Smarc 15039963Smarc case KINFO_PROC_TTY: 15139963Smarc if ((p->p_flag&SCTTY) == 0 || 15239963Smarc p->p_session->s_ttyp == NULL || 15339963Smarc p->p_session->s_ttyp->t_dev != (dev_t)arg) 15439963Smarc continue; 15539963Smarc break; 15639963Smarc 15739963Smarc case KINFO_PROC_UID: 15839963Smarc if (p->p_uid != (uid_t)arg) 15939963Smarc continue; 16039963Smarc break; 16139963Smarc 16239963Smarc case KINFO_PROC_RUID: 16339963Smarc if (p->p_ruid != (uid_t)arg) 16439963Smarc continue; 16539963Smarc break; 16639963Smarc } 16739963Smarc if (where != NULL && buflen >= sizeof (struct kinfo_proc)) { 16840206Smarc register struct text *txt; 169*43419Smarc register struct tty *tp; 17040206Smarc 171*43419Smarc if (error = copyout((caddr_t)p, &dp->kp_proc, 17239963Smarc sizeof (struct proc))) 17339963Smarc return (error); 17440206Smarc eproc.e_paddr = p; 17540206Smarc eproc.e_sess = p->p_pgrp->pg_session; 17640206Smarc eproc.e_pgid = p->p_pgrp->pg_id; 17740206Smarc eproc.e_jobc = p->p_pgrp->pg_jobc; 178*43419Smarc if ((p->p_flag&SCTTY) && 179*43419Smarc (tp = eproc.e_sess->s_ttyp)) { 18040206Smarc eproc.e_tdev = tp->t_dev; 18140206Smarc eproc.e_tpgid = tp->t_pgrp ? 18239963Smarc tp->t_pgrp->pg_id : -1; 18340206Smarc eproc.e_tsess = tp->t_session; 18439963Smarc } else 18540206Smarc eproc.e_tdev = NODEV; 186*43419Smarc eproc.e_flag = eproc.e_sess->s_ttyvp ? EPROC_CTTY : 0; 187*43419Smarc if (SESS_LEADER(p)) 188*43419Smarc eproc.e_flag |= EPROC_SLEADER; 18940206Smarc if (p->p_wmesg) 19040206Smarc strncpy(eproc.e_wmesg, p->p_wmesg, WMESGLEN); 19140206Smarc if (txt = p->p_textp) { 19240206Smarc eproc.e_xsize = txt->x_size; 19340206Smarc eproc.e_xrssize = txt->x_rssize; 19440206Smarc eproc.e_xccount = txt->x_ccount; 19540206Smarc eproc.e_xswrss = txt->x_swrss; 19640206Smarc } else { 19740206Smarc eproc.e_xsize = eproc.e_xrssize = 19840206Smarc eproc.e_xccount = eproc.e_xswrss = 0; 19940206Smarc } 200*43419Smarc if (error = copyout((caddr_t)&eproc, &dp->kp_eproc, 20140067Smarc sizeof (eproc))) 20239963Smarc return (error); 203*43419Smarc dp++; 20439963Smarc buflen -= sizeof (struct kinfo_proc); 20539963Smarc } 20639963Smarc needed += sizeof (struct kinfo_proc); 20739963Smarc } 20839963Smarc if (doingzomb == 0) { 20939963Smarc p = zombproc; 21039963Smarc doingzomb++; 21139963Smarc goto again; 21239963Smarc } 21339963Smarc if (where != NULL) 214*43419Smarc *acopysize = (caddr_t)dp - where; 21540068Smarc else 21640068Smarc needed += KINFO_PROCSLOP; 21739963Smarc *aneeded = needed; 21839963Smarc 21939963Smarc return (0); 22039963Smarc } 221