157843Smckusick /*- 257843Smckusick * Copyright (c) 1982, 1986, 1989, 1993 Regents of the University of California. 339963Smarc * All rights reserved. 439963Smarc * 557843Smckusick * This code is derived from software contributed to Berkeley by 657843Smckusick * Mike Karels at Berkeley Software Design, Inc. 757843Smckusick * 844435Sbostic * %sccs.include.redist.c% 939963Smarc * 10*61302Smckusick * @(#)kern_sysctl.c 7.44 (Berkeley) 06/04/93 1139963Smarc */ 1239963Smarc 1357843Smckusick /* 1457843Smckusick * sysctl system call. 1557843Smckusick */ 1657843Smckusick 1756517Sbostic #include <sys/param.h> 1857843Smckusick #include <sys/systm.h> 1960153Smckusick #include <sys/kernel.h> 2057843Smckusick #include <sys/malloc.h> 2156517Sbostic #include <sys/proc.h> 2257843Smckusick #include <sys/file.h> 2359314Smckusick #include <sys/vnode.h> 2457843Smckusick #include <sys/unistd.h> 2557843Smckusick #include <sys/buf.h> 2656517Sbostic #include <sys/ioctl.h> 2756517Sbostic #include <sys/tty.h> 2856517Sbostic #include <vm/vm.h> 2959354Smckusick #include <sys/sysctl.h> 3048407Skarels 3157843Smckusick sysctlfn kern_sysctl; 3257843Smckusick sysctlfn hw_sysctl; 3359161Smckusick #ifdef DEBUG 3459161Smckusick sysctlfn debug_sysctl; 3559161Smckusick #endif 3657843Smckusick extern sysctlfn vm_sysctl; 3757843Smckusick extern sysctlfn fs_sysctl; 3857843Smckusick extern sysctlfn net_sysctl; 3957843Smckusick extern sysctlfn cpu_sysctl; 4040068Smarc 4157843Smckusick /* 4257843Smckusick * Locking and stats 4357843Smckusick */ 4457843Smckusick static struct sysctl_lock { 4557843Smckusick int sl_lock; 4657843Smckusick int sl_want; 4757843Smckusick int sl_locked; 4857843Smckusick } memlock; 4957843Smckusick 5057843Smckusick struct sysctl_args { 5157843Smckusick int *name; 5257843Smckusick u_int namelen; 5357843Smckusick void *old; 5458466Sbostic size_t *oldlenp; 5557843Smckusick void *new; 5658466Sbostic size_t newlen; 5754923Storek }; 5857843Smckusick 5959718Sbostic int 6059718Sbostic __sysctl(p, uap, retval) 6143444Smckusick struct proc *p; 6257843Smckusick register struct sysctl_args *uap; 6343444Smckusick int *retval; 6443444Smckusick { 6557843Smckusick int error, dolock = 1; 6657843Smckusick u_int savelen, oldlen = 0; 6757843Smckusick sysctlfn *fn; 6857843Smckusick int name[CTL_MAXNAME]; 6939963Smarc 7057843Smckusick if (uap->new != NULL && (error = suser(p->p_ucred, &p->p_acflag))) 7157843Smckusick return (error); 7257843Smckusick /* 7357843Smckusick * all top-level sysctl names are non-terminal 7457843Smckusick */ 7557843Smckusick if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 7657843Smckusick return (EINVAL); 7757843Smckusick if (error = copyin(uap->name, &name, uap->namelen * sizeof(int))) 7857843Smckusick return (error); 7939963Smarc 8057843Smckusick switch (name[0]) { 8157843Smckusick case CTL_KERN: 8257843Smckusick fn = kern_sysctl; 8357843Smckusick if (name[2] != KERN_VNODE) /* XXX */ 8457843Smckusick dolock = 0; 8539963Smarc break; 8657843Smckusick case CTL_HW: 8757843Smckusick fn = hw_sysctl; 8840068Smarc break; 8957843Smckusick case CTL_VM: 9057843Smckusick fn = vm_sysctl; 9141181Smarc break; 9257843Smckusick case CTL_NET: 9357843Smckusick fn = net_sysctl; 9450149Smarc break; 9557843Smckusick #ifdef notyet 9657843Smckusick case CTL_FS: 9757843Smckusick fn = fs_sysctl; 9850909Smckusick break; 9960171Smckusick #endif 10057843Smckusick case CTL_MACHDEP: 10157843Smckusick fn = cpu_sysctl; 10252669Smckusick break; 10359161Smckusick #ifdef DEBUG 10459161Smckusick case CTL_DEBUG: 10559161Smckusick fn = debug_sysctl; 10659161Smckusick break; 10759161Smckusick #endif 10839963Smarc default: 10957843Smckusick return (EOPNOTSUPP); 11039963Smarc } 11157843Smckusick 11257843Smckusick if (uap->oldlenp && 11357843Smckusick (error = copyin(uap->oldlenp, &oldlen, sizeof(oldlen)))) 11457843Smckusick return (error); 11557843Smckusick if (uap->old != NULL) { 11657843Smckusick if (!useracc(uap->old, oldlen, B_WRITE)) 11757843Smckusick return (EFAULT); 11857843Smckusick while (memlock.sl_lock) { 11957843Smckusick memlock.sl_want = 1; 12057843Smckusick sleep((caddr_t)&memlock, PRIBIO+1); 12157843Smckusick memlock.sl_locked++; 12257843Smckusick } 12357843Smckusick memlock.sl_lock = 1; 12457843Smckusick if (dolock) 12557843Smckusick vslock(uap->old, oldlen); 12657843Smckusick savelen = oldlen; 12740813Smarc } 12857843Smckusick error = (*fn)(name + 1, uap->namelen - 1, uap->old, &oldlen, 12958415Smckusick uap->new, uap->newlen, p); 13057843Smckusick if (uap->old != NULL) { 13157843Smckusick if (dolock) 13257843Smckusick vsunlock(uap->old, savelen, B_WRITE); 13357843Smckusick memlock.sl_lock = 0; 13457843Smckusick if (memlock.sl_want) { 13557843Smckusick memlock.sl_want = 0; 13657843Smckusick wakeup((caddr_t)&memlock); 13757843Smckusick } 13840206Smarc } 13957843Smckusick if (error) 14057843Smckusick return (error); 14157843Smckusick if (uap->oldlenp) 14257843Smckusick error = copyout(&oldlen, uap->oldlenp, sizeof(oldlen)); 14357843Smckusick *retval = oldlen; 14457843Smckusick return (0); 14557843Smckusick } 14640206Smarc 14757843Smckusick /* 14857843Smckusick * Attributes stored in the kernel. 14957843Smckusick */ 15057843Smckusick char hostname[MAXHOSTNAMELEN]; 15157843Smckusick int hostnamelen; 15257843Smckusick long hostid; 15358415Smckusick int securelevel; 15457843Smckusick 15558415Smckusick /* 15658415Smckusick * kernel related system variables. 15758415Smckusick */ 15858415Smckusick kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 15957843Smckusick int *name; 16057843Smckusick u_int namelen; 16157843Smckusick void *oldp; 16258466Sbostic size_t *oldlenp; 16357843Smckusick void *newp; 16458466Sbostic size_t newlen; 16558415Smckusick struct proc *p; 16657843Smckusick { 16758415Smckusick int error, level; 16857843Smckusick extern char ostype[], osrelease[], version[]; 16957843Smckusick 17057843Smckusick /* all sysctl names at this level are terminal */ 17159312Smckusick if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF)) 17257843Smckusick return (ENOTDIR); /* overloaded */ 17357843Smckusick 17457843Smckusick switch (name[0]) { 17557843Smckusick case KERN_OSTYPE: 17657843Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, ostype)); 17757843Smckusick case KERN_OSRELEASE: 17857843Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, osrelease)); 17957843Smckusick case KERN_OSREV: 18057843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, BSD)); 18157843Smckusick case KERN_VERSION: 18257843Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, version)); 18359718Sbostic case KERN_MAXVNODES: 18459718Sbostic return(sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes)); 18557843Smckusick case KERN_MAXPROC: 18657843Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc)); 18757843Smckusick case KERN_MAXFILES: 18857843Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles)); 18957843Smckusick case KERN_ARGMAX: 19057843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX)); 19158415Smckusick case KERN_SECURELVL: 19258415Smckusick level = securelevel; 19358415Smckusick if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) || 19458415Smckusick newp == NULL) 19558415Smckusick return (error); 19658415Smckusick if (level < securelevel && p->p_pid != 1) 19758415Smckusick return (EPERM); 19858415Smckusick securelevel = level; 19958415Smckusick return (0); 20059718Sbostic case KERN_HOSTNAME: 20159718Sbostic error = sysctl_string(oldp, oldlenp, newp, newlen, 20259718Sbostic hostname, sizeof(hostname)); 203*61302Smckusick if (newp && !error) 20459718Sbostic hostnamelen = newlen; 20559718Sbostic return (error); 20659718Sbostic case KERN_HOSTID: 20759718Sbostic return (sysctl_int(oldp, oldlenp, newp, newlen, &hostid)); 20857843Smckusick case KERN_CLOCKRATE: 20957843Smckusick return (sysctl_clockrate(oldp, oldlenp)); 21060153Smckusick case KERN_BOOTTIME: 21160153Smckusick return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime, 21260153Smckusick sizeof(struct timeval))); 21357843Smckusick case KERN_VNODE: 21457843Smckusick return (sysctl_vnode(oldp, oldlenp)); 21557843Smckusick case KERN_PROC: 21657843Smckusick return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp)); 21759718Sbostic case KERN_FILE: 21859718Sbostic return (sysctl_file(oldp, oldlenp)); 21959312Smckusick #ifdef GPROF 22059312Smckusick case KERN_PROF: 22159312Smckusick return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp, 22259312Smckusick newp, newlen)); 22359312Smckusick #endif 22459718Sbostic case KERN_POSIX1: 22559718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION)); 22659718Sbostic case KERN_NGROUPS: 22759718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX)); 22859718Sbostic case KERN_JOB_CONTROL: 22959718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, 1)); 23059718Sbostic case KERN_SAVED_IDS: 23159718Sbostic #ifdef _POSIX_SAVED_IDS 23259718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, 1)); 23359718Sbostic #else 23459718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, 0)); 23559718Sbostic #endif 23657843Smckusick default: 23757843Smckusick return (EOPNOTSUPP); 23852405Storek } 23957843Smckusick /* NOTREACHED */ 24057843Smckusick } 24157843Smckusick 24258415Smckusick /* 24358415Smckusick * hardware related system variables. 24458415Smckusick */ 24558415Smckusick hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 24657843Smckusick int *name; 24757843Smckusick u_int namelen; 24857843Smckusick void *oldp; 24958466Sbostic size_t *oldlenp; 25057843Smckusick void *newp; 25158466Sbostic size_t newlen; 25258415Smckusick struct proc *p; 25357843Smckusick { 25457843Smckusick extern char machine[], cpu_model[]; 25557843Smckusick 25657843Smckusick /* all sysctl names at this level are terminal */ 25757843Smckusick if (namelen != 1) 25857843Smckusick return (ENOTDIR); /* overloaded */ 25957843Smckusick 26057843Smckusick switch (name[0]) { 26157843Smckusick case HW_MACHINE: 26257843Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, machine)); 26357843Smckusick case HW_MODEL: 26457843Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model)); 26557843Smckusick case HW_NCPU: 26657843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, 1)); /* XXX */ 26759618Smckusick case HW_BYTEORDER: 26859618Smckusick return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER)); 26957843Smckusick case HW_PHYSMEM: 27057843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem))); 27157843Smckusick case HW_USERMEM: 27257843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, 27357843Smckusick ctob(physmem - cnt.v_wire_count))); 27457843Smckusick case HW_PAGESIZE: 27557843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE)); 27657843Smckusick default: 27757843Smckusick return (EOPNOTSUPP); 27857843Smckusick } 27957843Smckusick /* NOTREACHED */ 28057843Smckusick } 28157843Smckusick 28259161Smckusick #ifdef DEBUG 28357843Smckusick /* 28459161Smckusick * Debugging related system variables. 28559161Smckusick */ 28659161Smckusick struct ctldebug debug0, debug1, debug2, debug3, debug4; 28759161Smckusick struct ctldebug debug5, debug6, debug7, debug8, debug9; 28859161Smckusick struct ctldebug debug10, debug11, debug12, debug13, debug14; 28959161Smckusick struct ctldebug debug15, debug16, debug17, debug18, debug19; 29059161Smckusick static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = { 29159161Smckusick &debug0, &debug1, &debug2, &debug3, &debug4, 29259161Smckusick &debug5, &debug6, &debug7, &debug8, &debug9, 29359161Smckusick &debug10, &debug11, &debug12, &debug13, &debug14, 29459161Smckusick &debug15, &debug16, &debug17, &debug18, &debug19, 29559161Smckusick }; 29659161Smckusick int 29759161Smckusick debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 29859161Smckusick int *name; 29959161Smckusick u_int namelen; 30059161Smckusick void *oldp; 30159161Smckusick size_t *oldlenp; 30259161Smckusick void *newp; 30359161Smckusick size_t newlen; 30459161Smckusick struct proc *p; 30559161Smckusick { 30659161Smckusick struct ctldebug *cdp; 30759161Smckusick 30859161Smckusick /* all sysctl names at this level are name and field */ 30959161Smckusick if (namelen != 2) 31059161Smckusick return (ENOTDIR); /* overloaded */ 31159161Smckusick cdp = debugvars[name[0]]; 31259161Smckusick if (cdp->debugname == 0) 31359161Smckusick return (EOPNOTSUPP); 31459161Smckusick switch (name[1]) { 31559161Smckusick case CTL_DEBUG_NAME: 31659161Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname)); 31759161Smckusick case CTL_DEBUG_VALUE: 31859161Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar)); 31959161Smckusick default: 32059161Smckusick return (EOPNOTSUPP); 32159161Smckusick } 32259161Smckusick /* NOTREACHED */ 32359161Smckusick } 32459161Smckusick #endif /* DEBUG */ 32559161Smckusick 32659161Smckusick /* 32757843Smckusick * Validate parameters and get old / set new parameters 32857843Smckusick * for an integer-valued sysctl function. 32957843Smckusick */ 33057843Smckusick sysctl_int(oldp, oldlenp, newp, newlen, valp) 33157843Smckusick void *oldp; 33258466Sbostic size_t *oldlenp; 33357843Smckusick void *newp; 33458466Sbostic size_t newlen; 33557843Smckusick int *valp; 33657843Smckusick { 33757843Smckusick int error = 0; 33857843Smckusick 33957843Smckusick if (oldp && *oldlenp < sizeof(int)) 34057843Smckusick return (ENOMEM); 34157843Smckusick if (newp && newlen != sizeof(int)) 34257843Smckusick return (EINVAL); 34357843Smckusick *oldlenp = sizeof(int); 34457843Smckusick if (oldp) 34557843Smckusick error = copyout(valp, oldp, sizeof(int)); 34657843Smckusick if (error == 0 && newp) 34757843Smckusick error = copyin(newp, valp, sizeof(int)); 34843444Smckusick return (error); 34939963Smarc } 35039963Smarc 35157843Smckusick /* 35257843Smckusick * As above, but read-only. 35357843Smckusick */ 35457843Smckusick sysctl_rdint(oldp, oldlenp, newp, val) 35557843Smckusick void *oldp; 35658466Sbostic size_t *oldlenp; 35757843Smckusick void *newp; 35857843Smckusick int val; 35957843Smckusick { 36057843Smckusick int error = 0; 36157843Smckusick 36257843Smckusick if (oldp && *oldlenp < sizeof(int)) 36357843Smckusick return (ENOMEM); 36457843Smckusick if (newp) 36557843Smckusick return (EPERM); 36657843Smckusick *oldlenp = sizeof(int); 36757843Smckusick if (oldp) 36857843Smckusick error = copyout((caddr_t)&val, oldp, sizeof(int)); 36957843Smckusick return (error); 37057843Smckusick } 37157843Smckusick 37257843Smckusick /* 37357843Smckusick * Validate parameters and get old / set new parameters 37457843Smckusick * for a string-valued sysctl function. 37557843Smckusick */ 37657843Smckusick sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen) 37757843Smckusick void *oldp; 37858466Sbostic size_t *oldlenp; 37957843Smckusick void *newp; 38058466Sbostic size_t newlen; 38157843Smckusick char *str; 38257843Smckusick int maxlen; 38357843Smckusick { 38457843Smckusick int len, error = 0; 38557843Smckusick 38657843Smckusick len = strlen(str) + 1; 38757843Smckusick if (oldp && *oldlenp < len) 38857843Smckusick return (ENOMEM); 38957843Smckusick if (newp && newlen >= maxlen) 39057843Smckusick return (EINVAL); 39158128Sralph if (oldp) { 39258128Sralph *oldlenp = len; 39357843Smckusick error = copyout(str, oldp, len); 39458128Sralph } 39557843Smckusick if (error == 0 && newp) { 39657843Smckusick error = copyin(newp, str, newlen); 39757843Smckusick str[newlen] = 0; 39857843Smckusick } 39957843Smckusick return (error); 40057843Smckusick } 40157843Smckusick 40257843Smckusick /* 40357843Smckusick * As above, but read-only. 40457843Smckusick */ 40557843Smckusick sysctl_rdstring(oldp, oldlenp, newp, str) 40657843Smckusick void *oldp; 40758466Sbostic size_t *oldlenp; 40857843Smckusick void *newp; 40957843Smckusick char *str; 41057843Smckusick { 41157843Smckusick int len, error = 0; 41257843Smckusick 41357843Smckusick len = strlen(str) + 1; 41457843Smckusick if (oldp && *oldlenp < len) 41557843Smckusick return (ENOMEM); 41657843Smckusick if (newp) 41757843Smckusick return (EPERM); 41857843Smckusick *oldlenp = len; 41957843Smckusick if (oldp) 42057843Smckusick error = copyout(str, oldp, len); 42157843Smckusick return (error); 42257843Smckusick } 42357843Smckusick 42457843Smckusick /* 42559312Smckusick * Validate parameters and get old / set new parameters 42659312Smckusick * for a structure oriented sysctl function. 42759312Smckusick */ 42859312Smckusick sysctl_struct(oldp, oldlenp, newp, newlen, sp, len) 42959312Smckusick void *oldp; 43059312Smckusick size_t *oldlenp; 43159312Smckusick void *newp; 43259312Smckusick size_t newlen; 43359312Smckusick void *sp; 43459312Smckusick int len; 43559312Smckusick { 43659312Smckusick int error = 0; 43759312Smckusick 43859312Smckusick if (oldp && *oldlenp < len) 43959312Smckusick return (ENOMEM); 44059312Smckusick if (newp && newlen > len) 44159312Smckusick return (EINVAL); 44259312Smckusick if (oldp) { 44359312Smckusick *oldlenp = len; 44459312Smckusick error = copyout(sp, oldp, len); 44559312Smckusick } 44659312Smckusick if (error == 0 && newp) 44759312Smckusick error = copyin(newp, sp, len); 44859312Smckusick return (error); 44959312Smckusick } 45059312Smckusick 45159312Smckusick /* 45257843Smckusick * Validate parameters and get old parameters 45357843Smckusick * for a structure oriented sysctl function. 45457843Smckusick */ 45557843Smckusick sysctl_rdstruct(oldp, oldlenp, newp, sp, len) 45657843Smckusick void *oldp; 45758466Sbostic size_t *oldlenp; 45858466Sbostic void *newp, *sp; 45957843Smckusick int len; 46057843Smckusick { 46157843Smckusick int error = 0; 46257843Smckusick 46357843Smckusick if (oldp && *oldlenp < len) 46457843Smckusick return (ENOMEM); 46557843Smckusick if (newp) 46657843Smckusick return (EPERM); 46757843Smckusick *oldlenp = len; 46857843Smckusick if (oldp) 46957843Smckusick error = copyout(sp, oldp, len); 47057843Smckusick return (error); 47157843Smckusick } 47257843Smckusick 47357843Smckusick /* 47457843Smckusick * Get file structures. 47557843Smckusick */ 47657843Smckusick sysctl_file(where, sizep) 47757843Smckusick char *where; 47858466Sbostic size_t *sizep; 47957843Smckusick { 48057843Smckusick int buflen, error; 48157843Smckusick struct file *fp; 48257843Smckusick char *start = where; 48357843Smckusick 48457843Smckusick buflen = *sizep; 48557843Smckusick if (where == NULL) { 48657843Smckusick /* 48757843Smckusick * overestimate by 10 files 48857843Smckusick */ 48957843Smckusick *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file); 49057843Smckusick return (0); 49157843Smckusick } 49257843Smckusick 49357843Smckusick /* 49457843Smckusick * first copyout filehead 49557843Smckusick */ 49657843Smckusick if (buflen < sizeof(filehead)) { 49757843Smckusick *sizep = 0; 49857843Smckusick return (0); 49957843Smckusick } 50057843Smckusick if (error = copyout((caddr_t)&filehead, where, sizeof(filehead))) 50157843Smckusick return (error); 50257843Smckusick buflen += sizeof(filehead); 50357843Smckusick where += sizeof(filehead); 50457843Smckusick 50557843Smckusick /* 50657843Smckusick * followed by an array of file structures 50757843Smckusick */ 50857843Smckusick for (fp = filehead; fp != NULL; fp = fp->f_filef) { 50957843Smckusick if (buflen < sizeof(struct file)) { 51057843Smckusick *sizep = where - start; 51157843Smckusick return (ENOMEM); 51257843Smckusick } 51357843Smckusick if (error = copyout((caddr_t)fp, where, sizeof (struct file))) 51457843Smckusick return (error); 51557843Smckusick buflen -= sizeof(struct file); 51657843Smckusick where += sizeof(struct file); 51757843Smckusick } 51857843Smckusick *sizep = where - start; 51957843Smckusick return (0); 52057843Smckusick } 52157843Smckusick 52259312Smckusick /* 52359312Smckusick * try over estimating by 5 procs 52439963Smarc */ 52557843Smckusick #define KERN_PROCSLOP (5 * sizeof (struct kinfo_proc)) 52639963Smarc 52757843Smckusick sysctl_doproc(name, namelen, where, sizep) 52857843Smckusick int *name; 52958466Sbostic u_int namelen; 53039963Smarc char *where; 53158466Sbostic size_t *sizep; 53239963Smarc { 53339963Smarc register struct proc *p; 53443419Smarc register struct kinfo_proc *dp = (struct kinfo_proc *)where; 53557843Smckusick register int needed = 0; 53657843Smckusick int buflen = where != NULL ? *sizep : 0; 53739963Smarc int doingzomb; 53840067Smarc struct eproc eproc; 53939963Smarc int error = 0; 54039963Smarc 54159977Smckusick if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL)) 54257843Smckusick return (EINVAL); 54354755Storek p = (struct proc *)allproc; 54439963Smarc doingzomb = 0; 54539963Smarc again: 54639963Smarc for (; p != NULL; p = p->p_nxt) { 54753819Smckusick /* 54853819Smckusick * Skip embryonic processes. 54953819Smckusick */ 55053819Smckusick if (p->p_stat == SIDL) 55153819Smckusick continue; 55259312Smckusick /* 55339963Smarc * TODO - make more efficient (see notes below). 55459312Smckusick * do by session. 55539963Smarc */ 55657843Smckusick switch (name[0]) { 55739963Smarc 55857843Smckusick case KERN_PROC_PID: 55939963Smarc /* could do this with just a lookup */ 56057843Smckusick if (p->p_pid != (pid_t)name[1]) 56139963Smarc continue; 56239963Smarc break; 56339963Smarc 56457843Smckusick case KERN_PROC_PGRP: 56539963Smarc /* could do this by traversing pgrp */ 56657843Smckusick if (p->p_pgrp->pg_id != (pid_t)name[1]) 56739963Smarc continue; 56839963Smarc break; 56939963Smarc 57057843Smckusick case KERN_PROC_TTY: 57159312Smckusick if ((p->p_flag&SCTTY) == 0 || 57239963Smarc p->p_session->s_ttyp == NULL || 57357843Smckusick p->p_session->s_ttyp->t_dev != (dev_t)name[1]) 57439963Smarc continue; 57539963Smarc break; 57639963Smarc 57757843Smckusick case KERN_PROC_UID: 57857843Smckusick if (p->p_ucred->cr_uid != (uid_t)name[1]) 57939963Smarc continue; 58039963Smarc break; 58139963Smarc 58257843Smckusick case KERN_PROC_RUID: 58357843Smckusick if (p->p_cred->p_ruid != (uid_t)name[1]) 58439963Smarc continue; 58539963Smarc break; 58639963Smarc } 58757843Smckusick if (buflen >= sizeof(struct kinfo_proc)) { 58848407Skarels fill_eproc(p, &eproc); 58959312Smckusick if (error = copyout((caddr_t)p, &dp->kp_proc, 59057843Smckusick sizeof(struct proc))) 59139963Smarc return (error); 59259312Smckusick if (error = copyout((caddr_t)&eproc, &dp->kp_eproc, 59357843Smckusick sizeof(eproc))) 59439963Smarc return (error); 59543419Smarc dp++; 59657843Smckusick buflen -= sizeof(struct kinfo_proc); 59739963Smarc } 59857843Smckusick needed += sizeof(struct kinfo_proc); 59939963Smarc } 60039963Smarc if (doingzomb == 0) { 60139963Smarc p = zombproc; 60239963Smarc doingzomb++; 60339963Smarc goto again; 60439963Smarc } 60557843Smckusick if (where != NULL) { 60657843Smckusick *sizep = (caddr_t)dp - where; 60757843Smckusick if (needed > *sizep) 60857843Smckusick return (ENOMEM); 60957843Smckusick } else { 61057843Smckusick needed += KERN_PROCSLOP; 61157843Smckusick *sizep = needed; 61257843Smckusick } 61339963Smarc return (0); 61439963Smarc } 61548407Skarels 61648407Skarels /* 61748407Skarels * Fill in an eproc structure for the specified process. 61848407Skarels */ 61948407Skarels void 62048407Skarels fill_eproc(p, ep) 62148407Skarels register struct proc *p; 62248407Skarels register struct eproc *ep; 62348407Skarels { 62448407Skarels register struct tty *tp; 62548407Skarels 62648407Skarels ep->e_paddr = p; 62748407Skarels ep->e_sess = p->p_pgrp->pg_session; 62848407Skarels ep->e_pcred = *p->p_cred; 62948407Skarels ep->e_ucred = *p->p_ucred; 63052405Storek if (p->p_stat == SIDL || p->p_stat == SZOMB) { 63152405Storek ep->e_vm.vm_rssize = 0; 63252405Storek ep->e_vm.vm_tsize = 0; 63352405Storek ep->e_vm.vm_dsize = 0; 63452405Storek ep->e_vm.vm_ssize = 0; 63552405Storek #ifndef sparc 63652405Storek /* ep->e_vm.vm_pmap = XXX; */ 63752405Storek #endif 63852405Storek } else { 63952405Storek register struct vmspace *vm = p->p_vmspace; 64052405Storek 64152405Storek ep->e_vm.vm_rssize = vm->vm_rssize; 64252405Storek ep->e_vm.vm_tsize = vm->vm_tsize; 64352405Storek ep->e_vm.vm_dsize = vm->vm_dsize; 64452405Storek ep->e_vm.vm_ssize = vm->vm_ssize; 64552405Storek #ifndef sparc 64652405Storek ep->e_vm.vm_pmap = vm->vm_pmap; 64752405Storek #endif 64852405Storek } 64949141Skarels if (p->p_pptr) 65049141Skarels ep->e_ppid = p->p_pptr->p_pid; 65149141Skarels else 65249141Skarels ep->e_ppid = 0; 65348407Skarels ep->e_pgid = p->p_pgrp->pg_id; 65448407Skarels ep->e_jobc = p->p_pgrp->pg_jobc; 65559312Smckusick if ((p->p_flag&SCTTY) && 65648407Skarels (tp = ep->e_sess->s_ttyp)) { 65748407Skarels ep->e_tdev = tp->t_dev; 65850022Skarels ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 65948407Skarels ep->e_tsess = tp->t_session; 66048407Skarels } else 66148407Skarels ep->e_tdev = NODEV; 66248407Skarels ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0; 66348407Skarels if (SESS_LEADER(p)) 66448407Skarels ep->e_flag |= EPROC_SLEADER; 66548407Skarels if (p->p_wmesg) 66648407Skarels strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 66748407Skarels ep->e_xsize = ep->e_xrssize = 0; 66848407Skarels ep->e_xccount = ep->e_xswrss = 0; 66948407Skarels } 67050149Smarc 67157843Smckusick #ifdef COMPAT_43 67257843Smckusick #include <sys/socket.h> 67357906Smckusick #define KINFO_PROC (0<<8) 67457906Smckusick #define KINFO_RT (1<<8) 67557906Smckusick #define KINFO_VNODE (2<<8) 67657906Smckusick #define KINFO_FILE (3<<8) 67757906Smckusick #define KINFO_METER (4<<8) 67857906Smckusick #define KINFO_LOADAVG (5<<8) 67957906Smckusick #define KINFO_CLOCKRATE (6<<8) 68057843Smckusick 68157843Smckusick struct getkerninfo_args { 68257843Smckusick int op; 68357843Smckusick char *where; 68457843Smckusick int *size; 68557843Smckusick int arg; 68657843Smckusick }; 68757843Smckusick 68858943Smckusick ogetkerninfo(p, uap, retval) 68957843Smckusick struct proc *p; 69057843Smckusick register struct getkerninfo_args *uap; 69157843Smckusick int *retval; 69250149Smarc { 69357843Smckusick int error, name[5]; 69457843Smckusick u_int size; 69550149Smarc 69658461Smckusick if (uap->size && 69758466Sbostic (error = copyin((caddr_t)uap->size, (caddr_t)&size, sizeof(size)))) 69857843Smckusick return (error); 69950149Smarc 70057906Smckusick switch (uap->op & 0xff00) { 70150149Smarc 70257843Smckusick case KINFO_RT: 70357843Smckusick name[0] = PF_ROUTE; 70457843Smckusick name[1] = 0; 70557843Smckusick name[2] = (uap->op & 0xff0000) >> 16; 70657843Smckusick name[3] = uap->op & 0xff; 70757843Smckusick name[4] = uap->arg; 70858415Smckusick error = net_sysctl(name, 5, uap->where, &size, NULL, 0, p); 70957843Smckusick break; 71057843Smckusick 71157843Smckusick case KINFO_VNODE: 71257843Smckusick name[0] = KERN_VNODE; 71358415Smckusick error = kern_sysctl(name, 1, uap->where, &size, NULL, 0, p); 71457843Smckusick break; 71557843Smckusick 71657843Smckusick case KINFO_PROC: 71757843Smckusick name[0] = KERN_PROC; 71857843Smckusick name[1] = uap->op & 0xff; 71957843Smckusick name[2] = uap->arg; 72058415Smckusick error = kern_sysctl(name, 3, uap->where, &size, NULL, 0, p); 72157843Smckusick break; 72257843Smckusick 72357843Smckusick case KINFO_FILE: 72457843Smckusick name[0] = KERN_FILE; 72558415Smckusick error = kern_sysctl(name, 1, uap->where, &size, NULL, 0, p); 72657843Smckusick break; 72757843Smckusick 72857843Smckusick case KINFO_METER: 72957843Smckusick name[0] = VM_METER; 73058415Smckusick error = vm_sysctl(name, 1, uap->where, &size, NULL, 0, p); 73157843Smckusick break; 73257843Smckusick 73357843Smckusick case KINFO_LOADAVG: 73457843Smckusick name[0] = VM_LOADAVG; 73558415Smckusick error = vm_sysctl(name, 1, uap->where, &size, NULL, 0, p); 73657843Smckusick break; 73757843Smckusick 73857843Smckusick case KINFO_CLOCKRATE: 73957843Smckusick name[0] = KERN_CLOCKRATE; 74058415Smckusick error = kern_sysctl(name, 1, uap->where, &size, NULL, 0, p); 74157843Smckusick break; 74257843Smckusick 74357843Smckusick default: 74458415Smckusick return (EOPNOTSUPP); 74550149Smarc } 74657843Smckusick if (error) 74757843Smckusick return (error); 74857843Smckusick *retval = size; 74958461Smckusick if (uap->size) 75058461Smckusick error = copyout((caddr_t)&size, (caddr_t)uap->size, 75158461Smckusick sizeof(size)); 75258461Smckusick return (error); 75350149Smarc } 75457843Smckusick #endif /* COMPAT_43 */ 755