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*67389Spendry * @(#)procfs_subr.c 8.5 (Berkeley) 06/15/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 struct pfsnode *pfs; 63*67389Spendry struct vnode *vp; 6465523Spendry struct pfsnode **pp; 65*67389Spendry int error; 6665523Spendry 6765523Spendry loop: 6865523Spendry for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) { 69*67389Spendry vp = PFSTOV(pfs); 7065523Spendry if (pfs->pfs_pid == pid && 7165523Spendry pfs->pfs_type == pfs_type && 72*67389Spendry vp->v_mount == mp) { 73*67389Spendry if (vget(vp, 0)) 7465523Spendry goto loop; 75*67389Spendry *vpp = vp; 7665523Spendry return (0); 7765523Spendry } 7865523Spendry } 7965523Spendry 8065523Spendry /* 8165523Spendry * otherwise lock the vp list while we call getnewvnode 8265523Spendry * since that can block. 8365523Spendry */ 8465523Spendry if (pfsvplock & PROCFS_LOCKED) { 8565523Spendry pfsvplock |= PROCFS_WANT; 8665523Spendry sleep((caddr_t) &pfsvplock, PINOD); 8765523Spendry goto loop; 8865523Spendry } 8965523Spendry pfsvplock |= PROCFS_LOCKED; 9065523Spendry 91*67389Spendry if (error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) 9265523Spendry goto out; 93*67389Spendry vp = *vpp; 9465523Spendry 95*67389Spendry MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK); 96*67389Spendry vp->v_data = pfs; 9765523Spendry 9865523Spendry pfs->pfs_next = 0; 9965523Spendry pfs->pfs_pid = (pid_t) pid; 10065523Spendry pfs->pfs_type = pfs_type; 101*67389Spendry pfs->pfs_vnode = vp; 10265523Spendry pfs->pfs_flags = 0; 10365523Spendry pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type); 10465523Spendry 10565523Spendry switch (pfs_type) { 10665523Spendry case Proot: /* /proc = dr-xr-xr-x */ 10765523Spendry pfs->pfs_mode = (VREAD|VEXEC) | 10865523Spendry (VREAD|VEXEC) >> 3 | 10965523Spendry (VREAD|VEXEC) >> 6; 110*67389Spendry vp->v_type = VDIR; 111*67389Spendry vp->v_flag = VROOT; 11265523Spendry break; 11365523Spendry 114*67389Spendry case Pcurproc: /* /proc/curproc = lr--r--r-- */ 115*67389Spendry pfs->pfs_mode = (VREAD) | 116*67389Spendry (VREAD >> 3) | 117*67389Spendry (VREAD >> 6); 118*67389Spendry vp->v_type = VLNK; 119*67389Spendry break; 120*67389Spendry 12165523Spendry case Pproc: 12265523Spendry pfs->pfs_mode = (VREAD|VEXEC) | 12365523Spendry (VREAD|VEXEC) >> 3 | 12465523Spendry (VREAD|VEXEC) >> 6; 125*67389Spendry vp->v_type = VDIR; 12665523Spendry break; 12765523Spendry 12865523Spendry case Pfile: 12965523Spendry case Pmem: 13065523Spendry case Pregs: 13165924Spendry case Pfpregs: 13265924Spendry pfs->pfs_mode = (VREAD|VWRITE); 133*67389Spendry vp->v_type = VREG; 13465924Spendry break; 13565924Spendry 13665523Spendry case Pctl: 137*67389Spendry case Pnote: 138*67389Spendry case Pnotepg: 13965523Spendry pfs->pfs_mode = (VWRITE); 140*67389Spendry vp->v_type = VREG; 14165523Spendry break; 14265523Spendry 14365523Spendry case Pstatus: 14465523Spendry pfs->pfs_mode = (VREAD) | 14565523Spendry (VREAD >> 3) | 14665523Spendry (VREAD >> 6); 147*67389Spendry vp->v_type = VREG; 14865523Spendry break; 14965523Spendry 15065523Spendry default: 15165523Spendry panic("procfs_allocvp"); 15265523Spendry } 15365523Spendry 15465523Spendry /* add to procfs vnode list */ 15565523Spendry for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next) 15665523Spendry continue; 15765523Spendry *pp = pfs; 15865523Spendry 15965523Spendry out: 16065523Spendry pfsvplock &= ~PROCFS_LOCKED; 16165523Spendry 16265523Spendry if (pfsvplock & PROCFS_WANT) { 16365523Spendry pfsvplock &= ~PROCFS_WANT; 16465523Spendry wakeup((caddr_t) &pfsvplock); 16565523Spendry } 16665523Spendry 16765523Spendry return (error); 16865523Spendry } 16965523Spendry 17065523Spendry int 17165523Spendry procfs_freevp(vp) 17265523Spendry struct vnode *vp; 17365523Spendry { 17465523Spendry struct pfsnode **pfspp; 17565523Spendry struct pfsnode *pfs = VTOPFS(vp); 17665523Spendry 17765523Spendry for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) { 17865523Spendry if (*pfspp == pfs) { 17965523Spendry *pfspp = pfs->pfs_next; 18065523Spendry break; 18165523Spendry } 18265523Spendry } 18365523Spendry 18465523Spendry FREE(vp->v_data, M_TEMP); 18565523Spendry vp->v_data = 0; 18665523Spendry return (0); 18765523Spendry } 18865523Spendry 18965523Spendry int 19065523Spendry procfs_rw(ap) 19165523Spendry struct vop_read_args *ap; 19265523Spendry { 19365523Spendry struct vnode *vp = ap->a_vp; 19465523Spendry struct uio *uio = ap->a_uio; 19565523Spendry struct proc *curp = uio->uio_procp; 19665523Spendry struct pfsnode *pfs = VTOPFS(vp); 19765523Spendry struct proc *p; 19865523Spendry 19965523Spendry p = PFIND(pfs->pfs_pid); 20065523Spendry if (p == 0) 20165523Spendry return (EINVAL); 20265523Spendry 20365523Spendry switch (pfs->pfs_type) { 20465523Spendry case Pnote: 20565523Spendry case Pnotepg: 20665523Spendry return (procfs_donote(curp, p, pfs, uio)); 20765523Spendry 20865523Spendry case Pregs: 20965523Spendry return (procfs_doregs(curp, p, pfs, uio)); 21065523Spendry 21165924Spendry case Pfpregs: 21265924Spendry return (procfs_dofpregs(curp, p, pfs, uio)); 21365924Spendry 21465523Spendry case Pctl: 21565523Spendry return (procfs_doctl(curp, p, pfs, uio)); 21665523Spendry 21765523Spendry case Pstatus: 21865523Spendry return (procfs_dostatus(curp, p, pfs, uio)); 21965523Spendry 22065523Spendry case Pmem: 22165523Spendry return (procfs_domem(curp, p, pfs, uio)); 22265523Spendry 22365523Spendry default: 22465523Spendry return (EOPNOTSUPP); 22565523Spendry } 22665523Spendry } 22765523Spendry 22865523Spendry /* 22965523Spendry * Get a string from userland into (buf). Strip a trailing 23065523Spendry * nl character (to allow easy access from the shell). 23165523Spendry * The buffer should be *buflenp + 1 chars long. vfs_getuserstr 23265523Spendry * will automatically add a nul char at the end. 23365523Spendry * 23465523Spendry * Returns 0 on success or the following errors 23565523Spendry * 23665523Spendry * EINVAL: file offset is non-zero. 23765523Spendry * EMSGSIZE: message is longer than kernel buffer 23865523Spendry * EFAULT: user i/o buffer is not addressable 23965523Spendry */ 24065523Spendry int 24165523Spendry vfs_getuserstr(uio, buf, buflenp) 24265523Spendry struct uio *uio; 24365523Spendry char *buf; 24465523Spendry int *buflenp; 24565523Spendry { 24665523Spendry int xlen; 24765523Spendry int error; 24865523Spendry 24965523Spendry if (uio->uio_offset != 0) 25065523Spendry return (EINVAL); 25165523Spendry 25265523Spendry xlen = *buflenp; 25365523Spendry 25465523Spendry /* must be able to read the whole string in one go */ 25565523Spendry if (xlen < uio->uio_resid) 25665523Spendry return (EMSGSIZE); 25765523Spendry xlen = uio->uio_resid; 25865523Spendry 259*67389Spendry if (error = uiomove(buf, xlen, uio)) 26065523Spendry return (error); 26165523Spendry 26265523Spendry /* allow multiple writes without seeks */ 26365523Spendry uio->uio_offset = 0; 26465523Spendry 26565523Spendry /* cleanup string and remove trailing newline */ 26665523Spendry buf[xlen] = '\0'; 26765523Spendry xlen = strlen(buf); 26865523Spendry if (xlen > 0 && buf[xlen-1] == '\n') 26965523Spendry buf[--xlen] = '\0'; 27065523Spendry *buflenp = xlen; 27165523Spendry 27265523Spendry return (0); 27365523Spendry } 27465523Spendry 27565523Spendry vfs_namemap_t * 27665523Spendry vfs_findname(nm, buf, buflen) 27765523Spendry vfs_namemap_t *nm; 27865523Spendry char *buf; 27965523Spendry int buflen; 28065523Spendry { 281*67389Spendry 28265523Spendry for (; nm->nm_name; nm++) 28365523Spendry if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0) 28465523Spendry return (nm); 28565523Spendry 28665523Spendry return (0); 28765523Spendry } 288