165523Spendry /* 265523Spendry * Copyright (c) 1993 Jan-Simon Pendry 365808Sbostic * Copyright (c) 1993 465808Sbostic * The Regents of the University of California. All rights reserved. 565523Spendry * 665523Spendry * This code is derived from software contributed to Berkeley by 765523Spendry * Jan-Simon Pendry. 865523Spendry * 965523Spendry * %sccs.include.redist.c% 1065523Spendry * 11*65924Spendry * @(#)procfs_subr.c 8.4 (Berkeley) 01/27/94 1265523Spendry * 1365523Spendry * From: 1465523Spendry * $Id: procfs_subr.c,v 3.2 1993/12/15 09:40:17 jsp Exp $ 1565523Spendry */ 1665523Spendry 1765523Spendry #include <sys/param.h> 1865523Spendry #include <sys/systm.h> 1965523Spendry #include <sys/time.h> 2065523Spendry #include <sys/kernel.h> 2165523Spendry #include <sys/proc.h> 2265523Spendry #include <sys/vnode.h> 2365523Spendry #include <sys/malloc.h> 2465523Spendry #include <miscfs/procfs/procfs.h> 2565523Spendry 2665523Spendry static struct pfsnode *pfshead; 2765523Spendry static int pfsvplock; 2865523Spendry 2965523Spendry /* 3065523Spendry * allocate a pfsnode/vnode pair. the vnode is 3165523Spendry * referenced, but not locked. 3265523Spendry * 3365523Spendry * the pid, pfs_type, and mount point uniquely 3465523Spendry * identify a pfsnode. the mount point is needed 3565523Spendry * because someone might mount this filesystem 3665523Spendry * twice. 3765523Spendry * 3865523Spendry * all pfsnodes are maintained on a singly-linked 3965523Spendry * list. new nodes are only allocated when they cannot 4065523Spendry * be found on this list. entries on the list are 4165523Spendry * removed when the vfs reclaim entry is called. 4265523Spendry * 4365523Spendry * a single lock is kept for the entire list. this is 4465523Spendry * needed because the getnewvnode() function can block 4565523Spendry * waiting for a vnode to become free, in which case there 4665523Spendry * may be more than one process trying to get the same 4765523Spendry * vnode. this lock is only taken if we are going to 4865523Spendry * call getnewvnode, since the kernel itself is single-threaded. 4965523Spendry * 5065523Spendry * if an entry is found on the list, then call vget() to 5165523Spendry * take a reference. this is done because there may be 5265523Spendry * zero references to it and so it needs to removed from 5365523Spendry * the vnode free list. 5465523Spendry */ 5565523Spendry int 5665523Spendry procfs_allocvp(mp, vpp, pid, pfs_type) 5765523Spendry struct mount *mp; 5865523Spendry struct vnode **vpp; 5965523Spendry long pid; 6065523Spendry pfstype pfs_type; 6165523Spendry { 6265523Spendry int error; 6365523Spendry struct pfsnode *pfs; 6465523Spendry struct pfsnode **pp; 6565523Spendry 6665523Spendry loop: 6765523Spendry for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) { 6865523Spendry if (pfs->pfs_pid == pid && 6965523Spendry pfs->pfs_type == pfs_type && 7065523Spendry PFSTOV(pfs)->v_mount == mp) { 7165535Spendry if (vget(pfs->pfs_vnode, 0)) 7265523Spendry goto loop; 7365523Spendry *vpp = pfs->pfs_vnode; 7465523Spendry return (0); 7565523Spendry } 7665523Spendry } 7765523Spendry 7865523Spendry /* 7965523Spendry * otherwise lock the vp list while we call getnewvnode 8065523Spendry * since that can block. 8165523Spendry */ 8265523Spendry if (pfsvplock & PROCFS_LOCKED) { 8365523Spendry pfsvplock |= PROCFS_WANT; 8465523Spendry sleep((caddr_t) &pfsvplock, PINOD); 8565523Spendry goto loop; 8665523Spendry } 8765523Spendry pfsvplock |= PROCFS_LOCKED; 8865523Spendry 8965523Spendry error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp); 9065523Spendry if (error) 9165523Spendry goto out; 9265523Spendry 9365523Spendry MALLOC((*vpp)->v_data, void *, sizeof(struct pfsnode), 9465523Spendry M_TEMP, M_WAITOK); 9565523Spendry 9665523Spendry pfs = VTOPFS(*vpp); 9765523Spendry pfs->pfs_next = 0; 9865523Spendry pfs->pfs_pid = (pid_t) pid; 9965523Spendry pfs->pfs_type = pfs_type; 10065523Spendry pfs->pfs_vnode = *vpp; 10165523Spendry pfs->pfs_flags = 0; 10265523Spendry pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type); 10365523Spendry 10465523Spendry switch (pfs_type) { 10565523Spendry case Proot: /* /proc = dr-xr-xr-x */ 10665523Spendry pfs->pfs_mode = (VREAD|VEXEC) | 10765523Spendry (VREAD|VEXEC) >> 3 | 10865523Spendry (VREAD|VEXEC) >> 6; 10965523Spendry break; 11065523Spendry 11165523Spendry case Pproc: 11265523Spendry pfs->pfs_mode = (VREAD|VEXEC) | 11365523Spendry (VREAD|VEXEC) >> 3 | 11465523Spendry (VREAD|VEXEC) >> 6; 11565523Spendry break; 11665523Spendry 11765523Spendry case Pfile: 11865523Spendry pfs->pfs_mode = (VREAD|VWRITE); 11965523Spendry break; 12065523Spendry 12165523Spendry case Pmem: 12265523Spendry pfs->pfs_mode = (VREAD|VWRITE); 12365523Spendry break; 12465523Spendry 12565523Spendry case Pregs: 12665523Spendry pfs->pfs_mode = (VREAD|VWRITE); 12765523Spendry break; 12865523Spendry 129*65924Spendry case Pfpregs: 130*65924Spendry pfs->pfs_mode = (VREAD|VWRITE); 131*65924Spendry break; 132*65924Spendry 13365523Spendry case Pctl: 13465523Spendry pfs->pfs_mode = (VWRITE); 13565523Spendry break; 13665523Spendry 13765523Spendry case Pstatus: 13865523Spendry pfs->pfs_mode = (VREAD) | 13965523Spendry (VREAD >> 3) | 14065523Spendry (VREAD >> 6); 14165523Spendry break; 14265523Spendry 14365523Spendry case Pnote: 14465523Spendry pfs->pfs_mode = (VWRITE); 14565523Spendry break; 14665523Spendry 14765523Spendry case Pnotepg: 14865523Spendry pfs->pfs_mode = (VWRITE); 14965523Spendry break; 15065523Spendry 15165523Spendry default: 15265523Spendry panic("procfs_allocvp"); 15365523Spendry } 15465523Spendry 15565523Spendry /* add to procfs vnode list */ 15665523Spendry for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next) 15765523Spendry continue; 15865523Spendry *pp = pfs; 15965523Spendry 16065523Spendry out: 16165523Spendry pfsvplock &= ~PROCFS_LOCKED; 16265523Spendry 16365523Spendry if (pfsvplock & PROCFS_WANT) { 16465523Spendry pfsvplock &= ~PROCFS_WANT; 16565523Spendry wakeup((caddr_t) &pfsvplock); 16665523Spendry } 16765523Spendry 16865523Spendry return (error); 16965523Spendry } 17065523Spendry 17165523Spendry int 17265523Spendry procfs_freevp(vp) 17365523Spendry struct vnode *vp; 17465523Spendry { 17565523Spendry struct pfsnode **pfspp; 17665523Spendry struct pfsnode *pfs = VTOPFS(vp); 17765523Spendry 17865523Spendry for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) { 17965523Spendry if (*pfspp == pfs) { 18065523Spendry *pfspp = pfs->pfs_next; 18165523Spendry break; 18265523Spendry } 18365523Spendry } 18465523Spendry 18565523Spendry FREE(vp->v_data, M_TEMP); 18665523Spendry vp->v_data = 0; 18765523Spendry return (0); 18865523Spendry } 18965523Spendry 19065523Spendry int 19165523Spendry procfs_rw(ap) 19265523Spendry struct vop_read_args *ap; 19365523Spendry { 19465523Spendry struct vnode *vp = ap->a_vp; 19565523Spendry struct uio *uio = ap->a_uio; 19665523Spendry struct proc *curp = uio->uio_procp; 19765523Spendry struct pfsnode *pfs = VTOPFS(vp); 19865523Spendry struct proc *p; 19965523Spendry 20065523Spendry p = PFIND(pfs->pfs_pid); 20165523Spendry if (p == 0) 20265523Spendry return (EINVAL); 20365523Spendry 20465523Spendry switch (pfs->pfs_type) { 20565523Spendry case Pnote: 20665523Spendry case Pnotepg: 20765523Spendry return (procfs_donote(curp, p, pfs, uio)); 20865523Spendry 20965523Spendry case Pregs: 21065523Spendry return (procfs_doregs(curp, p, pfs, uio)); 21165523Spendry 212*65924Spendry case Pfpregs: 213*65924Spendry return (procfs_dofpregs(curp, p, pfs, uio)); 214*65924Spendry 21565523Spendry case Pctl: 21665523Spendry return (procfs_doctl(curp, p, pfs, uio)); 21765523Spendry 21865523Spendry case Pstatus: 21965523Spendry return (procfs_dostatus(curp, p, pfs, uio)); 22065523Spendry 22165523Spendry case Pmem: 22265523Spendry return (procfs_domem(curp, p, pfs, uio)); 22365523Spendry 22465523Spendry default: 22565523Spendry return (EOPNOTSUPP); 22665523Spendry } 22765523Spendry } 22865523Spendry 22965523Spendry /* 23065523Spendry * Get a string from userland into (buf). Strip a trailing 23165523Spendry * nl character (to allow easy access from the shell). 23265523Spendry * The buffer should be *buflenp + 1 chars long. vfs_getuserstr 23365523Spendry * will automatically add a nul char at the end. 23465523Spendry * 23565523Spendry * Returns 0 on success or the following errors 23665523Spendry * 23765523Spendry * EINVAL: file offset is non-zero. 23865523Spendry * EMSGSIZE: message is longer than kernel buffer 23965523Spendry * EFAULT: user i/o buffer is not addressable 24065523Spendry */ 24165523Spendry int 24265523Spendry vfs_getuserstr(uio, buf, buflenp) 24365523Spendry struct uio *uio; 24465523Spendry char *buf; 24565523Spendry int *buflenp; 24665523Spendry { 24765523Spendry int xlen; 24865523Spendry int error; 24965523Spendry 25065523Spendry if (uio->uio_offset != 0) 25165523Spendry return (EINVAL); 25265523Spendry 25365523Spendry xlen = *buflenp; 25465523Spendry 25565523Spendry /* must be able to read the whole string in one go */ 25665523Spendry if (xlen < uio->uio_resid) 25765523Spendry return (EMSGSIZE); 25865523Spendry xlen = uio->uio_resid; 25965523Spendry 26065523Spendry error = uiomove(buf, xlen, uio); 26165523Spendry if (error) 26265523Spendry return (error); 26365523Spendry 26465523Spendry /* allow multiple writes without seeks */ 26565523Spendry uio->uio_offset = 0; 26665523Spendry 26765523Spendry /* cleanup string and remove trailing newline */ 26865523Spendry buf[xlen] = '\0'; 26965523Spendry xlen = strlen(buf); 27065523Spendry if (xlen > 0 && buf[xlen-1] == '\n') 27165523Spendry buf[--xlen] = '\0'; 27265523Spendry *buflenp = xlen; 27365523Spendry 27465523Spendry return (0); 27565523Spendry } 27665523Spendry 27765523Spendry vfs_namemap_t * 27865523Spendry vfs_findname(nm, buf, buflen) 27965523Spendry vfs_namemap_t *nm; 28065523Spendry char *buf; 28165523Spendry int buflen; 28265523Spendry { 28365523Spendry for (; nm->nm_name; nm++) 28465523Spendry if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0) 28565523Spendry return (nm); 28665523Spendry 28765523Spendry return (0); 28865523Spendry } 289