157843Smckusick /*- 2*63457Sbostic * Copyright (c) 1982, 1986, 1989, 1993 3*63457Sbostic * The Regents of the University of California. 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*63457Sbostic * @(#)kern_sysctl.c 8.1 (Berkeley) 06/16/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 { 16763456Smckusick int error, level, inthostid; 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)); 20361302Smckusick if (newp && !error) 20459718Sbostic hostnamelen = newlen; 20559718Sbostic return (error); 20659718Sbostic case KERN_HOSTID: 20763456Smckusick inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */ 20863456Smckusick error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid); 20963456Smckusick hostid = inthostid; 21063456Smckusick return (error); 21157843Smckusick case KERN_CLOCKRATE: 21257843Smckusick return (sysctl_clockrate(oldp, oldlenp)); 21360153Smckusick case KERN_BOOTTIME: 21460153Smckusick return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime, 21560153Smckusick sizeof(struct timeval))); 21657843Smckusick case KERN_VNODE: 21757843Smckusick return (sysctl_vnode(oldp, oldlenp)); 21857843Smckusick case KERN_PROC: 21957843Smckusick return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp)); 22059718Sbostic case KERN_FILE: 22159718Sbostic return (sysctl_file(oldp, oldlenp)); 22259312Smckusick #ifdef GPROF 22359312Smckusick case KERN_PROF: 22459312Smckusick return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp, 22559312Smckusick newp, newlen)); 22659312Smckusick #endif 22759718Sbostic case KERN_POSIX1: 22859718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION)); 22959718Sbostic case KERN_NGROUPS: 23059718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX)); 23159718Sbostic case KERN_JOB_CONTROL: 23259718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, 1)); 23359718Sbostic case KERN_SAVED_IDS: 23459718Sbostic #ifdef _POSIX_SAVED_IDS 23559718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, 1)); 23659718Sbostic #else 23759718Sbostic return (sysctl_rdint(oldp, oldlenp, newp, 0)); 23859718Sbostic #endif 23957843Smckusick default: 24057843Smckusick return (EOPNOTSUPP); 24152405Storek } 24257843Smckusick /* NOTREACHED */ 24357843Smckusick } 24457843Smckusick 24558415Smckusick /* 24658415Smckusick * hardware related system variables. 24758415Smckusick */ 24858415Smckusick hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 24957843Smckusick int *name; 25057843Smckusick u_int namelen; 25157843Smckusick void *oldp; 25258466Sbostic size_t *oldlenp; 25357843Smckusick void *newp; 25458466Sbostic size_t newlen; 25558415Smckusick struct proc *p; 25657843Smckusick { 25757843Smckusick extern char machine[], cpu_model[]; 25857843Smckusick 25957843Smckusick /* all sysctl names at this level are terminal */ 26057843Smckusick if (namelen != 1) 26157843Smckusick return (ENOTDIR); /* overloaded */ 26257843Smckusick 26357843Smckusick switch (name[0]) { 26457843Smckusick case HW_MACHINE: 26557843Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, machine)); 26657843Smckusick case HW_MODEL: 26757843Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model)); 26857843Smckusick case HW_NCPU: 26957843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, 1)); /* XXX */ 27059618Smckusick case HW_BYTEORDER: 27159618Smckusick return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER)); 27257843Smckusick case HW_PHYSMEM: 27357843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem))); 27457843Smckusick case HW_USERMEM: 27557843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, 27657843Smckusick ctob(physmem - cnt.v_wire_count))); 27757843Smckusick case HW_PAGESIZE: 27857843Smckusick return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE)); 27957843Smckusick default: 28057843Smckusick return (EOPNOTSUPP); 28157843Smckusick } 28257843Smckusick /* NOTREACHED */ 28357843Smckusick } 28457843Smckusick 28559161Smckusick #ifdef DEBUG 28657843Smckusick /* 28759161Smckusick * Debugging related system variables. 28859161Smckusick */ 28959161Smckusick struct ctldebug debug0, debug1, debug2, debug3, debug4; 29059161Smckusick struct ctldebug debug5, debug6, debug7, debug8, debug9; 29159161Smckusick struct ctldebug debug10, debug11, debug12, debug13, debug14; 29259161Smckusick struct ctldebug debug15, debug16, debug17, debug18, debug19; 29359161Smckusick static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = { 29459161Smckusick &debug0, &debug1, &debug2, &debug3, &debug4, 29559161Smckusick &debug5, &debug6, &debug7, &debug8, &debug9, 29659161Smckusick &debug10, &debug11, &debug12, &debug13, &debug14, 29759161Smckusick &debug15, &debug16, &debug17, &debug18, &debug19, 29859161Smckusick }; 29959161Smckusick int 30059161Smckusick debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 30159161Smckusick int *name; 30259161Smckusick u_int namelen; 30359161Smckusick void *oldp; 30459161Smckusick size_t *oldlenp; 30559161Smckusick void *newp; 30659161Smckusick size_t newlen; 30759161Smckusick struct proc *p; 30859161Smckusick { 30959161Smckusick struct ctldebug *cdp; 31059161Smckusick 31159161Smckusick /* all sysctl names at this level are name and field */ 31259161Smckusick if (namelen != 2) 31359161Smckusick return (ENOTDIR); /* overloaded */ 31459161Smckusick cdp = debugvars[name[0]]; 31559161Smckusick if (cdp->debugname == 0) 31659161Smckusick return (EOPNOTSUPP); 31759161Smckusick switch (name[1]) { 31859161Smckusick case CTL_DEBUG_NAME: 31959161Smckusick return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname)); 32059161Smckusick case CTL_DEBUG_VALUE: 32159161Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar)); 32259161Smckusick default: 32359161Smckusick return (EOPNOTSUPP); 32459161Smckusick } 32559161Smckusick /* NOTREACHED */ 32659161Smckusick } 32759161Smckusick #endif /* DEBUG */ 32859161Smckusick 32959161Smckusick /* 33057843Smckusick * Validate parameters and get old / set new parameters 33157843Smckusick * for an integer-valued sysctl function. 33257843Smckusick */ 33357843Smckusick sysctl_int(oldp, oldlenp, newp, newlen, valp) 33457843Smckusick void *oldp; 33558466Sbostic size_t *oldlenp; 33657843Smckusick void *newp; 33758466Sbostic size_t newlen; 33857843Smckusick int *valp; 33957843Smckusick { 34057843Smckusick int error = 0; 34157843Smckusick 34257843Smckusick if (oldp && *oldlenp < sizeof(int)) 34357843Smckusick return (ENOMEM); 34457843Smckusick if (newp && newlen != sizeof(int)) 34557843Smckusick return (EINVAL); 34657843Smckusick *oldlenp = sizeof(int); 34757843Smckusick if (oldp) 34857843Smckusick error = copyout(valp, oldp, sizeof(int)); 34957843Smckusick if (error == 0 && newp) 35057843Smckusick error = copyin(newp, valp, sizeof(int)); 35143444Smckusick return (error); 35239963Smarc } 35339963Smarc 35457843Smckusick /* 35557843Smckusick * As above, but read-only. 35657843Smckusick */ 35757843Smckusick sysctl_rdint(oldp, oldlenp, newp, val) 35857843Smckusick void *oldp; 35958466Sbostic size_t *oldlenp; 36057843Smckusick void *newp; 36157843Smckusick int val; 36257843Smckusick { 36357843Smckusick int error = 0; 36457843Smckusick 36557843Smckusick if (oldp && *oldlenp < sizeof(int)) 36657843Smckusick return (ENOMEM); 36757843Smckusick if (newp) 36857843Smckusick return (EPERM); 36957843Smckusick *oldlenp = sizeof(int); 37057843Smckusick if (oldp) 37157843Smckusick error = copyout((caddr_t)&val, oldp, sizeof(int)); 37257843Smckusick return (error); 37357843Smckusick } 37457843Smckusick 37557843Smckusick /* 37657843Smckusick * Validate parameters and get old / set new parameters 37757843Smckusick * for a string-valued sysctl function. 37857843Smckusick */ 37957843Smckusick sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen) 38057843Smckusick void *oldp; 38158466Sbostic size_t *oldlenp; 38257843Smckusick void *newp; 38358466Sbostic size_t newlen; 38457843Smckusick char *str; 38557843Smckusick int maxlen; 38657843Smckusick { 38757843Smckusick int len, error = 0; 38857843Smckusick 38957843Smckusick len = strlen(str) + 1; 39057843Smckusick if (oldp && *oldlenp < len) 39157843Smckusick return (ENOMEM); 39257843Smckusick if (newp && newlen >= maxlen) 39357843Smckusick return (EINVAL); 39458128Sralph if (oldp) { 39558128Sralph *oldlenp = len; 39657843Smckusick error = copyout(str, oldp, len); 39758128Sralph } 39857843Smckusick if (error == 0 && newp) { 39957843Smckusick error = copyin(newp, str, newlen); 40057843Smckusick str[newlen] = 0; 40157843Smckusick } 40257843Smckusick return (error); 40357843Smckusick } 40457843Smckusick 40557843Smckusick /* 40657843Smckusick * As above, but read-only. 40757843Smckusick */ 40857843Smckusick sysctl_rdstring(oldp, oldlenp, newp, str) 40957843Smckusick void *oldp; 41058466Sbostic size_t *oldlenp; 41157843Smckusick void *newp; 41257843Smckusick char *str; 41357843Smckusick { 41457843Smckusick int len, error = 0; 41557843Smckusick 41657843Smckusick len = strlen(str) + 1; 41757843Smckusick if (oldp && *oldlenp < len) 41857843Smckusick return (ENOMEM); 41957843Smckusick if (newp) 42057843Smckusick return (EPERM); 42157843Smckusick *oldlenp = len; 42257843Smckusick if (oldp) 42357843Smckusick error = copyout(str, oldp, len); 42457843Smckusick return (error); 42557843Smckusick } 42657843Smckusick 42757843Smckusick /* 42859312Smckusick * Validate parameters and get old / set new parameters 42959312Smckusick * for a structure oriented sysctl function. 43059312Smckusick */ 43159312Smckusick sysctl_struct(oldp, oldlenp, newp, newlen, sp, len) 43259312Smckusick void *oldp; 43359312Smckusick size_t *oldlenp; 43459312Smckusick void *newp; 43559312Smckusick size_t newlen; 43659312Smckusick void *sp; 43759312Smckusick int len; 43859312Smckusick { 43959312Smckusick int error = 0; 44059312Smckusick 44159312Smckusick if (oldp && *oldlenp < len) 44259312Smckusick return (ENOMEM); 44359312Smckusick if (newp && newlen > len) 44459312Smckusick return (EINVAL); 44559312Smckusick if (oldp) { 44659312Smckusick *oldlenp = len; 44759312Smckusick error = copyout(sp, oldp, len); 44859312Smckusick } 44959312Smckusick if (error == 0 && newp) 45059312Smckusick error = copyin(newp, sp, len); 45159312Smckusick return (error); 45259312Smckusick } 45359312Smckusick 45459312Smckusick /* 45557843Smckusick * Validate parameters and get old parameters 45657843Smckusick * for a structure oriented sysctl function. 45757843Smckusick */ 45857843Smckusick sysctl_rdstruct(oldp, oldlenp, newp, sp, len) 45957843Smckusick void *oldp; 46058466Sbostic size_t *oldlenp; 46158466Sbostic void *newp, *sp; 46257843Smckusick int len; 46357843Smckusick { 46457843Smckusick int error = 0; 46557843Smckusick 46657843Smckusick if (oldp && *oldlenp < len) 46757843Smckusick return (ENOMEM); 46857843Smckusick if (newp) 46957843Smckusick return (EPERM); 47057843Smckusick *oldlenp = len; 47157843Smckusick if (oldp) 47257843Smckusick error = copyout(sp, oldp, len); 47357843Smckusick return (error); 47457843Smckusick } 47557843Smckusick 47657843Smckusick /* 47757843Smckusick * Get file structures. 47857843Smckusick */ 47957843Smckusick sysctl_file(where, sizep) 48057843Smckusick char *where; 48158466Sbostic size_t *sizep; 48257843Smckusick { 48357843Smckusick int buflen, error; 48457843Smckusick struct file *fp; 48557843Smckusick char *start = where; 48657843Smckusick 48757843Smckusick buflen = *sizep; 48857843Smckusick if (where == NULL) { 48957843Smckusick /* 49057843Smckusick * overestimate by 10 files 49157843Smckusick */ 49257843Smckusick *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file); 49357843Smckusick return (0); 49457843Smckusick } 49557843Smckusick 49657843Smckusick /* 49757843Smckusick * first copyout filehead 49857843Smckusick */ 49957843Smckusick if (buflen < sizeof(filehead)) { 50057843Smckusick *sizep = 0; 50157843Smckusick return (0); 50257843Smckusick } 50357843Smckusick if (error = copyout((caddr_t)&filehead, where, sizeof(filehead))) 50457843Smckusick return (error); 50557843Smckusick buflen += sizeof(filehead); 50657843Smckusick where += sizeof(filehead); 50757843Smckusick 50857843Smckusick /* 50957843Smckusick * followed by an array of file structures 51057843Smckusick */ 51157843Smckusick for (fp = filehead; fp != NULL; fp = fp->f_filef) { 51257843Smckusick if (buflen < sizeof(struct file)) { 51357843Smckusick *sizep = where - start; 51457843Smckusick return (ENOMEM); 51557843Smckusick } 51657843Smckusick if (error = copyout((caddr_t)fp, where, sizeof (struct file))) 51757843Smckusick return (error); 51857843Smckusick buflen -= sizeof(struct file); 51957843Smckusick where += sizeof(struct file); 52057843Smckusick } 52157843Smckusick *sizep = where - start; 52257843Smckusick return (0); 52357843Smckusick } 52457843Smckusick 52559312Smckusick /* 52659312Smckusick * try over estimating by 5 procs 52739963Smarc */ 52857843Smckusick #define KERN_PROCSLOP (5 * sizeof (struct kinfo_proc)) 52939963Smarc 53057843Smckusick sysctl_doproc(name, namelen, where, sizep) 53157843Smckusick int *name; 53258466Sbostic u_int namelen; 53339963Smarc char *where; 53458466Sbostic size_t *sizep; 53539963Smarc { 53639963Smarc register struct proc *p; 53743419Smarc register struct kinfo_proc *dp = (struct kinfo_proc *)where; 53857843Smckusick register int needed = 0; 53957843Smckusick int buflen = where != NULL ? *sizep : 0; 54039963Smarc int doingzomb; 54140067Smarc struct eproc eproc; 54239963Smarc int error = 0; 54339963Smarc 54459977Smckusick if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL)) 54557843Smckusick return (EINVAL); 54654755Storek p = (struct proc *)allproc; 54739963Smarc doingzomb = 0; 54839963Smarc again: 54939963Smarc for (; p != NULL; p = p->p_nxt) { 55053819Smckusick /* 55153819Smckusick * Skip embryonic processes. 55253819Smckusick */ 55353819Smckusick if (p->p_stat == SIDL) 55453819Smckusick continue; 55559312Smckusick /* 55639963Smarc * TODO - make more efficient (see notes below). 55759312Smckusick * do by session. 55839963Smarc */ 55957843Smckusick switch (name[0]) { 56039963Smarc 56157843Smckusick case KERN_PROC_PID: 56239963Smarc /* could do this with just a lookup */ 56357843Smckusick if (p->p_pid != (pid_t)name[1]) 56439963Smarc continue; 56539963Smarc break; 56639963Smarc 56757843Smckusick case KERN_PROC_PGRP: 56839963Smarc /* could do this by traversing pgrp */ 56957843Smckusick if (p->p_pgrp->pg_id != (pid_t)name[1]) 57039963Smarc continue; 57139963Smarc break; 57239963Smarc 57357843Smckusick case KERN_PROC_TTY: 57459312Smckusick if ((p->p_flag&SCTTY) == 0 || 57539963Smarc p->p_session->s_ttyp == NULL || 57657843Smckusick p->p_session->s_ttyp->t_dev != (dev_t)name[1]) 57739963Smarc continue; 57839963Smarc break; 57939963Smarc 58057843Smckusick case KERN_PROC_UID: 58157843Smckusick if (p->p_ucred->cr_uid != (uid_t)name[1]) 58239963Smarc continue; 58339963Smarc break; 58439963Smarc 58557843Smckusick case KERN_PROC_RUID: 58657843Smckusick if (p->p_cred->p_ruid != (uid_t)name[1]) 58739963Smarc continue; 58839963Smarc break; 58939963Smarc } 59057843Smckusick if (buflen >= sizeof(struct kinfo_proc)) { 59148407Skarels fill_eproc(p, &eproc); 59259312Smckusick if (error = copyout((caddr_t)p, &dp->kp_proc, 59357843Smckusick sizeof(struct proc))) 59439963Smarc return (error); 59559312Smckusick if (error = copyout((caddr_t)&eproc, &dp->kp_eproc, 59657843Smckusick sizeof(eproc))) 59739963Smarc return (error); 59843419Smarc dp++; 59957843Smckusick buflen -= sizeof(struct kinfo_proc); 60039963Smarc } 60157843Smckusick needed += sizeof(struct kinfo_proc); 60239963Smarc } 60339963Smarc if (doingzomb == 0) { 60439963Smarc p = zombproc; 60539963Smarc doingzomb++; 60639963Smarc goto again; 60739963Smarc } 60857843Smckusick if (where != NULL) { 60957843Smckusick *sizep = (caddr_t)dp - where; 61057843Smckusick if (needed > *sizep) 61157843Smckusick return (ENOMEM); 61257843Smckusick } else { 61357843Smckusick needed += KERN_PROCSLOP; 61457843Smckusick *sizep = needed; 61557843Smckusick } 61639963Smarc return (0); 61739963Smarc } 61848407Skarels 61948407Skarels /* 62048407Skarels * Fill in an eproc structure for the specified process. 62148407Skarels */ 62248407Skarels void 62348407Skarels fill_eproc(p, ep) 62448407Skarels register struct proc *p; 62548407Skarels register struct eproc *ep; 62648407Skarels { 62748407Skarels register struct tty *tp; 62848407Skarels 62948407Skarels ep->e_paddr = p; 63048407Skarels ep->e_sess = p->p_pgrp->pg_session; 63148407Skarels ep->e_pcred = *p->p_cred; 63248407Skarels ep->e_ucred = *p->p_ucred; 63352405Storek if (p->p_stat == SIDL || p->p_stat == SZOMB) { 63452405Storek ep->e_vm.vm_rssize = 0; 63552405Storek ep->e_vm.vm_tsize = 0; 63652405Storek ep->e_vm.vm_dsize = 0; 63752405Storek ep->e_vm.vm_ssize = 0; 63852405Storek #ifndef sparc 63952405Storek /* ep->e_vm.vm_pmap = XXX; */ 64052405Storek #endif 64152405Storek } else { 64252405Storek register struct vmspace *vm = p->p_vmspace; 64352405Storek 64452405Storek ep->e_vm.vm_rssize = vm->vm_rssize; 64552405Storek ep->e_vm.vm_tsize = vm->vm_tsize; 64652405Storek ep->e_vm.vm_dsize = vm->vm_dsize; 64752405Storek ep->e_vm.vm_ssize = vm->vm_ssize; 64852405Storek #ifndef sparc 64952405Storek ep->e_vm.vm_pmap = vm->vm_pmap; 65052405Storek #endif 65152405Storek } 65249141Skarels if (p->p_pptr) 65349141Skarels ep->e_ppid = p->p_pptr->p_pid; 65449141Skarels else 65549141Skarels ep->e_ppid = 0; 65648407Skarels ep->e_pgid = p->p_pgrp->pg_id; 65748407Skarels ep->e_jobc = p->p_pgrp->pg_jobc; 65859312Smckusick if ((p->p_flag&SCTTY) && 65948407Skarels (tp = ep->e_sess->s_ttyp)) { 66048407Skarels ep->e_tdev = tp->t_dev; 66150022Skarels ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 66248407Skarels ep->e_tsess = tp->t_session; 66348407Skarels } else 66448407Skarels ep->e_tdev = NODEV; 66548407Skarels ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0; 66648407Skarels if (SESS_LEADER(p)) 66748407Skarels ep->e_flag |= EPROC_SLEADER; 66848407Skarels if (p->p_wmesg) 66948407Skarels strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 67048407Skarels ep->e_xsize = ep->e_xrssize = 0; 67148407Skarels ep->e_xccount = ep->e_xswrss = 0; 67248407Skarels } 67350149Smarc 67457843Smckusick #ifdef COMPAT_43 67557843Smckusick #include <sys/socket.h> 67657906Smckusick #define KINFO_PROC (0<<8) 67757906Smckusick #define KINFO_RT (1<<8) 67857906Smckusick #define KINFO_VNODE (2<<8) 67957906Smckusick #define KINFO_FILE (3<<8) 68057906Smckusick #define KINFO_METER (4<<8) 68157906Smckusick #define KINFO_LOADAVG (5<<8) 68257906Smckusick #define KINFO_CLOCKRATE (6<<8) 68357843Smckusick 68457843Smckusick struct getkerninfo_args { 68557843Smckusick int op; 68657843Smckusick char *where; 68757843Smckusick int *size; 68857843Smckusick int arg; 68957843Smckusick }; 69057843Smckusick 69158943Smckusick ogetkerninfo(p, uap, retval) 69257843Smckusick struct proc *p; 69357843Smckusick register struct getkerninfo_args *uap; 69457843Smckusick int *retval; 69550149Smarc { 69657843Smckusick int error, name[5]; 69757843Smckusick u_int size; 69850149Smarc 69958461Smckusick if (uap->size && 70058466Sbostic (error = copyin((caddr_t)uap->size, (caddr_t)&size, sizeof(size)))) 70157843Smckusick return (error); 70250149Smarc 70357906Smckusick switch (uap->op & 0xff00) { 70450149Smarc 70557843Smckusick case KINFO_RT: 70657843Smckusick name[0] = PF_ROUTE; 70757843Smckusick name[1] = 0; 70857843Smckusick name[2] = (uap->op & 0xff0000) >> 16; 70957843Smckusick name[3] = uap->op & 0xff; 71057843Smckusick name[4] = uap->arg; 71158415Smckusick error = net_sysctl(name, 5, uap->where, &size, NULL, 0, p); 71257843Smckusick break; 71357843Smckusick 71457843Smckusick case KINFO_VNODE: 71557843Smckusick name[0] = KERN_VNODE; 71658415Smckusick error = kern_sysctl(name, 1, uap->where, &size, NULL, 0, p); 71757843Smckusick break; 71857843Smckusick 71957843Smckusick case KINFO_PROC: 72057843Smckusick name[0] = KERN_PROC; 72157843Smckusick name[1] = uap->op & 0xff; 72257843Smckusick name[2] = uap->arg; 72358415Smckusick error = kern_sysctl(name, 3, uap->where, &size, NULL, 0, p); 72457843Smckusick break; 72557843Smckusick 72657843Smckusick case KINFO_FILE: 72757843Smckusick name[0] = KERN_FILE; 72858415Smckusick error = kern_sysctl(name, 1, uap->where, &size, NULL, 0, p); 72957843Smckusick break; 73057843Smckusick 73157843Smckusick case KINFO_METER: 73257843Smckusick name[0] = VM_METER; 73358415Smckusick error = vm_sysctl(name, 1, uap->where, &size, NULL, 0, p); 73457843Smckusick break; 73557843Smckusick 73657843Smckusick case KINFO_LOADAVG: 73757843Smckusick name[0] = VM_LOADAVG; 73858415Smckusick error = vm_sysctl(name, 1, uap->where, &size, NULL, 0, p); 73957843Smckusick break; 74057843Smckusick 74157843Smckusick case KINFO_CLOCKRATE: 74257843Smckusick name[0] = KERN_CLOCKRATE; 74358415Smckusick error = kern_sysctl(name, 1, uap->where, &size, NULL, 0, p); 74457843Smckusick break; 74557843Smckusick 74657843Smckusick default: 74758415Smckusick return (EOPNOTSUPP); 74850149Smarc } 74957843Smckusick if (error) 75057843Smckusick return (error); 75157843Smckusick *retval = size; 75258461Smckusick if (uap->size) 75358461Smckusick error = copyout((caddr_t)&size, (caddr_t)uap->size, 75458461Smckusick sizeof(size)); 75558461Smckusick return (error); 75650149Smarc } 75757843Smckusick #endif /* COMPAT_43 */ 758