165523Spendry /* 265523Spendry * Copyright (c) 1993 Jan-Simon Pendry 3*65808Sbostic * Copyright (c) 1993 4*65808Sbostic * 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*65808Sbostic * @(#)procfs_subr.c 8.3 (Berkeley) 01/21/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 12965523Spendry case Pctl: 13065523Spendry pfs->pfs_mode = (VWRITE); 13165523Spendry break; 13265523Spendry 13365523Spendry case Pstatus: 13465523Spendry pfs->pfs_mode = (VREAD) | 13565523Spendry (VREAD >> 3) | 13665523Spendry (VREAD >> 6); 13765523Spendry break; 13865523Spendry 13965523Spendry case Pnote: 14065523Spendry pfs->pfs_mode = (VWRITE); 14165523Spendry break; 14265523Spendry 14365523Spendry case Pnotepg: 14465523Spendry pfs->pfs_mode = (VWRITE); 14565523Spendry break; 14665523Spendry 14765523Spendry default: 14865523Spendry panic("procfs_allocvp"); 14965523Spendry } 15065523Spendry 15165523Spendry /* add to procfs vnode list */ 15265523Spendry for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next) 15365523Spendry continue; 15465523Spendry *pp = pfs; 15565523Spendry 15665523Spendry out: 15765523Spendry pfsvplock &= ~PROCFS_LOCKED; 15865523Spendry 15965523Spendry if (pfsvplock & PROCFS_WANT) { 16065523Spendry pfsvplock &= ~PROCFS_WANT; 16165523Spendry wakeup((caddr_t) &pfsvplock); 16265523Spendry } 16365523Spendry 16465523Spendry return (error); 16565523Spendry } 16665523Spendry 16765523Spendry int 16865523Spendry procfs_freevp(vp) 16965523Spendry struct vnode *vp; 17065523Spendry { 17165523Spendry struct pfsnode **pfspp; 17265523Spendry struct pfsnode *pfs = VTOPFS(vp); 17365523Spendry 17465523Spendry for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) { 17565523Spendry if (*pfspp == pfs) { 17665523Spendry *pfspp = pfs->pfs_next; 17765523Spendry break; 17865523Spendry } 17965523Spendry } 18065523Spendry 18165523Spendry FREE(vp->v_data, M_TEMP); 18265523Spendry vp->v_data = 0; 18365523Spendry return (0); 18465523Spendry } 18565523Spendry 18665523Spendry int 18765523Spendry procfs_rw(ap) 18865523Spendry struct vop_read_args *ap; 18965523Spendry { 19065523Spendry struct vnode *vp = ap->a_vp; 19165523Spendry struct uio *uio = ap->a_uio; 19265523Spendry struct proc *curp = uio->uio_procp; 19365523Spendry struct pfsnode *pfs = VTOPFS(vp); 19465523Spendry struct proc *p; 19565523Spendry 19665523Spendry p = PFIND(pfs->pfs_pid); 19765523Spendry if (p == 0) 19865523Spendry return (EINVAL); 19965523Spendry 20065523Spendry switch (pfs->pfs_type) { 20165523Spendry case Pnote: 20265523Spendry case Pnotepg: 20365523Spendry return (procfs_donote(curp, p, pfs, uio)); 20465523Spendry 20565523Spendry case Pregs: 20665523Spendry return (procfs_doregs(curp, p, pfs, uio)); 20765523Spendry 20865523Spendry case Pctl: 20965523Spendry return (procfs_doctl(curp, p, pfs, uio)); 21065523Spendry 21165523Spendry case Pstatus: 21265523Spendry return (procfs_dostatus(curp, p, pfs, uio)); 21365523Spendry 21465523Spendry case Pmem: 21565523Spendry return (procfs_domem(curp, p, pfs, uio)); 21665523Spendry 21765523Spendry default: 21865523Spendry return (EOPNOTSUPP); 21965523Spendry } 22065523Spendry } 22165523Spendry 22265523Spendry /* 22365523Spendry * Get a string from userland into (buf). Strip a trailing 22465523Spendry * nl character (to allow easy access from the shell). 22565523Spendry * The buffer should be *buflenp + 1 chars long. vfs_getuserstr 22665523Spendry * will automatically add a nul char at the end. 22765523Spendry * 22865523Spendry * Returns 0 on success or the following errors 22965523Spendry * 23065523Spendry * EINVAL: file offset is non-zero. 23165523Spendry * EMSGSIZE: message is longer than kernel buffer 23265523Spendry * EFAULT: user i/o buffer is not addressable 23365523Spendry */ 23465523Spendry int 23565523Spendry vfs_getuserstr(uio, buf, buflenp) 23665523Spendry struct uio *uio; 23765523Spendry char *buf; 23865523Spendry int *buflenp; 23965523Spendry { 24065523Spendry int xlen; 24165523Spendry int error; 24265523Spendry 24365523Spendry if (uio->uio_offset != 0) 24465523Spendry return (EINVAL); 24565523Spendry 24665523Spendry xlen = *buflenp; 24765523Spendry 24865523Spendry /* must be able to read the whole string in one go */ 24965523Spendry if (xlen < uio->uio_resid) 25065523Spendry return (EMSGSIZE); 25165523Spendry xlen = uio->uio_resid; 25265523Spendry 25365523Spendry error = uiomove(buf, xlen, uio); 25465523Spendry if (error) 25565523Spendry return (error); 25665523Spendry 25765523Spendry /* allow multiple writes without seeks */ 25865523Spendry uio->uio_offset = 0; 25965523Spendry 26065523Spendry /* cleanup string and remove trailing newline */ 26165523Spendry buf[xlen] = '\0'; 26265523Spendry xlen = strlen(buf); 26365523Spendry if (xlen > 0 && buf[xlen-1] == '\n') 26465523Spendry buf[--xlen] = '\0'; 26565523Spendry *buflenp = xlen; 26665523Spendry 26765523Spendry return (0); 26865523Spendry } 26965523Spendry 27065523Spendry vfs_namemap_t * 27165523Spendry vfs_findname(nm, buf, buflen) 27265523Spendry vfs_namemap_t *nm; 27365523Spendry char *buf; 27465523Spendry int buflen; 27565523Spendry { 27665523Spendry for (; nm->nm_name; nm++) 27765523Spendry if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0) 27865523Spendry return (nm); 27965523Spendry 28065523Spendry return (0); 28165523Spendry } 282