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*41181Smarc * @(#)kern_sysctl.c 7.8 (Berkeley) 05/01/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;} 32*41181Smarc 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 61*41181Smarc case KINFO_VNODE: 62*41181Smarc server = kinfo_vnode; 63*41181Smarc break; 64*41181Smarc 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); 88*41181Smarc if (server != kinfo_vnode) /* XXX */ 89*41181Smarc vslock(uap->where, bufsize); 9040813Smarc locked = bufsize; 9140813Smarc error = (*server)(uap->op, uap->where, &bufsize, uap->arg, &needed); 92*41181Smarc if (server != kinfo_vnode) /* XXX */ 93*41181Smarc 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; 11839963Smarc register caddr_t dp = (caddr_t)where; 11939963Smarc register needed = 0; 12039963Smarc int buflen; 12139963Smarc int doingzomb; 12240067Smarc struct eproc eproc; 12339963Smarc struct tty *tp; 12439963Smarc int error = 0; 12539963Smarc 12639963Smarc if (where != NULL) 12739963Smarc buflen = *acopysize; 12839963Smarc 12939963Smarc p = allproc; 13039963Smarc doingzomb = 0; 13139963Smarc again: 13239963Smarc for (; p != NULL; p = p->p_nxt) { 13339963Smarc /* 13439963Smarc * TODO - make more efficient (see notes below). 13539963Smarc * do by session. 13639963Smarc */ 13739963Smarc switch (ki_op(op)) { 13839963Smarc 13939963Smarc case KINFO_PROC_PID: 14039963Smarc /* could do this with just a lookup */ 14139963Smarc if (p->p_pid != (pid_t)arg) 14239963Smarc continue; 14339963Smarc break; 14439963Smarc 14539963Smarc case KINFO_PROC_PGRP: 14639963Smarc /* could do this by traversing pgrp */ 14739963Smarc if (p->p_pgrp->pg_id != (pid_t)arg) 14839963Smarc continue; 14939963Smarc break; 15039963Smarc 15139963Smarc case KINFO_PROC_TTY: 15239963Smarc if ((p->p_flag&SCTTY) == 0 || 15339963Smarc p->p_session->s_ttyp == NULL || 15439963Smarc p->p_session->s_ttyp->t_dev != (dev_t)arg) 15539963Smarc continue; 15639963Smarc break; 15739963Smarc 15839963Smarc case KINFO_PROC_UID: 15939963Smarc if (p->p_uid != (uid_t)arg) 16039963Smarc continue; 16139963Smarc break; 16239963Smarc 16339963Smarc case KINFO_PROC_RUID: 16439963Smarc if (p->p_ruid != (uid_t)arg) 16539963Smarc continue; 16639963Smarc break; 16739963Smarc } 16839963Smarc if (where != NULL && buflen >= sizeof (struct kinfo_proc)) { 16940206Smarc register struct text *txt; 17040206Smarc 17139963Smarc if (error = copyout((caddr_t)p, dp, 17239963Smarc sizeof (struct proc))) 17339963Smarc return (error); 17439963Smarc dp += sizeof (struct proc); 17540813Smarc /* 17640813Smarc * XXX NEED ALLIGNMENT 17740813Smarc */ 17840206Smarc eproc.e_paddr = p; 17940206Smarc eproc.e_sess = p->p_pgrp->pg_session; 18040206Smarc eproc.e_pgid = p->p_pgrp->pg_id; 18140206Smarc eproc.e_jobc = p->p_pgrp->pg_jobc; 18240874Smarc if (tp = p->p_pgrp->pg_session->s_ttyp) { 18340874Smarc /* up to caller to check for SCTTY */ 18440206Smarc eproc.e_tdev = tp->t_dev; 18540206Smarc eproc.e_tpgid = tp->t_pgrp ? 18639963Smarc tp->t_pgrp->pg_id : -1; 18740206Smarc eproc.e_tsess = tp->t_session; 18839963Smarc } else 18940206Smarc eproc.e_tdev = NODEV; 19040206Smarc if (p->p_wmesg) 19140206Smarc strncpy(eproc.e_wmesg, p->p_wmesg, WMESGLEN); 19240206Smarc if (txt = p->p_textp) { 19340206Smarc eproc.e_xsize = txt->x_size; 19440206Smarc eproc.e_xrssize = txt->x_rssize; 19540206Smarc eproc.e_xccount = txt->x_ccount; 19640206Smarc eproc.e_xswrss = txt->x_swrss; 19740206Smarc } else { 19840206Smarc eproc.e_xsize = eproc.e_xrssize = 19940206Smarc eproc.e_xccount = eproc.e_xswrss = 0; 20040206Smarc } 20140067Smarc if (error = copyout((caddr_t)&eproc, dp, 20240067Smarc sizeof (eproc))) 20339963Smarc return (error); 20440067Smarc dp += sizeof (eproc); 20539963Smarc buflen -= sizeof (struct kinfo_proc); 20639963Smarc } 20739963Smarc needed += sizeof (struct kinfo_proc); 20839963Smarc } 20939963Smarc if (doingzomb == 0) { 21039963Smarc p = zombproc; 21139963Smarc doingzomb++; 21239963Smarc goto again; 21339963Smarc } 21439963Smarc if (where != NULL) 21539963Smarc *acopysize = dp - where; 21640068Smarc else 21740068Smarc needed += KINFO_PROCSLOP; 21839963Smarc *aneeded = needed; 21939963Smarc 22039963Smarc return (0); 22139963Smarc } 222