NetBSD/sys/miscfs/procfs/procfs_subr.c

316 lines
7.6 KiB
C

/*
* Copyright (c) 1993 The Regents of the University of California.
* Copyright (c) 1993 Jan-Simon Pendry
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* From:
* Id: procfs_subr.c,v 4.1 1993/12/17 10:47:45 jsp Rel
*
* $Id: procfs_subr.c,v 1.8 1994/01/20 21:23:07 ws Exp $
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <miscfs/procfs/procfs.h>
static struct pfsnode *pfshead;
static int pfsvplock;
/*
* allocate a pfsnode/vnode pair. the vnode is
* referenced, but not locked.
*
* the pid, pfs_type, and mount point uniquely
* identify a pfsnode. the mount point is needed
* because someone might mount this filesystem
* twice.
*
* all pfsnodes are maintained on a singly-linked
* list. new nodes are only allocated when they cannot
* be found on this list. entries on the list are
* removed when the vfs reclaim entry is called.
*
* a single lock is kept for the entire list. this is
* needed because the getnewvnode() function can block
* waiting for a vnode to become free, in which case there
* may be more than one process trying to get the same
* vnode. this lock is only taken if we are going to
* call getnewvnode, since the kernel itself is single-threaded.
*
* if an entry is found on the list, then call vget() to
* take a reference. this is done because there may be
* zero references to it and so it needs to removed from
* the vnode free list.
*/
procfs_allocvp(mp, vpp, pid, pfs_type)
struct mount *mp;
struct vnode **vpp;
long pid;
pfstype pfs_type;
{
int error;
struct pfsnode *pfs;
struct pfsnode **pp;
struct vnode *vp;
loop:
for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
if (pfs->pfs_pid == pid &&
pfs->pfs_type == pfs_type &&
PFSTOV(pfs)->v_mount == mp) {
if (vget(pfs->pfs_vnode))
goto loop;
VOP_UNLOCK(pfs->pfs_vnode);
*vpp = pfs->pfs_vnode;
return (0);
}
}
/*
* otherwise lock the vp list while we call getnewvnode
* since that can block.
*/
if (pfsvplock & PROCFS_LOCKED) {
pfsvplock |= PROCFS_WANT;
sleep((caddr_t) &pfsvplock, PINOD);
goto loop;
}
pfsvplock |= PROCFS_LOCKED;
error = getnewvnode(VT_PROCFS, mp, &procfs_vnodeops, vpp);
if (error)
goto out;
/* 4.4: at this point, need to allocate a pfsnode */
pfs = VTOPFS(*vpp);
pfs->pfs_next = 0;
pfs->pfs_pid = (pid_t) pid;
pfs->pfs_type = pfs_type;
pfs->pfs_vnode = vp = *vpp;
pfs->pfs_flags = 0;
pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
switch (pfs_type) {
case Proot:
switch ((int)pid) {
case 0: /* /proc = dr-xr-xr-x */
pfs->pfs_mode = (VREAD|VEXEC) |
(VREAD|VEXEC) >> 3 |
(VREAD|VEXEC) >> 6;
vp->v_type = VDIR;
vp->v_flag = VROOT;
break;
case 1: /* /proc/curproc = lr--r--r-- */
pfs->pfs_mode = VREAD |
VREAD >> 3 |
VREAD >> 6;
vp->v_type = VLNK;
break;
default:
panic("procfs_allocvp root");
}
break;
case Pproc:
pfs->pfs_mode = (VREAD|VEXEC) |
(VREAD|VEXEC) >> 3 |
(VREAD|VEXEC) >> 6;
vp->v_type = VDIR;
break;
case Pmem:
pfs->pfs_mode = (VREAD|VWRITE);
vp->v_type = VREG;
break;
case Pregs:
pfs->pfs_mode = (VREAD|VWRITE);
vp->v_type = VREG;
break;
case Pctl:
pfs->pfs_mode = (VREAD|VWRITE);
vp->v_type = VREG;
break;
case Pstatus:
pfs->pfs_mode = (VREAD) |
(VREAD >> 3) |
(VREAD >> 6);
vp->v_type = VREG;
break;
case Pnote:
pfs->pfs_mode = (VREAD|VWRITE);
vp->v_type = VREG;
break;
case Pnotepg:
pfs->pfs_mode = (VWRITE);
vp->v_type = VREG;
break;
default:
panic("procfs_allocvp type");
}
/* add to procfs vnode list */
for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
continue;
*pp = pfs;
out:
pfsvplock &= ~PROCFS_LOCKED;
if (pfsvplock & PROCFS_WANT) {
pfsvplock &= ~PROCFS_WANT;
wakeup((caddr_t) &pfsvplock);
}
return (error);
}
procfs_freevp(vp)
struct vnode *vp;
{
struct pfsnode **pfspp;
struct pfsnode *pfs = VTOPFS(vp);
/* 4.4: at this point, need to deallocate the pfsnode */
for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
if (*pfspp == pfs) {
*pfspp = pfs->pfs_next;
break;
}
}
return (0);
}
procfs_rw(vp, uio, ioflag, cred)
struct vnode *vp;
struct uio *uio;
int ioflag;
struct ucred *cred;
{
struct proc *curp = uio->uio_procp;
struct pfsnode *pfs = VTOPFS(vp);
struct proc *p;
p = PFIND(pfs->pfs_pid);
if (p == 0)
return (EINVAL);
switch (pfs->pfs_type) {
case Pnote:
case Pnotepg:
return (pfs_donote(curp, p, pfs, uio));
case Pregs:
return (pfs_doregs(curp, p, pfs, uio));
case Pctl:
return (pfs_doctl(curp, p, pfs, uio));
case Pstatus:
return (pfs_dostatus(curp, p, pfs, uio));
case Pmem:
return (pfs_domem(curp, p, pfs, uio));
default:
return (EOPNOTSUPP);
}
}
/*
* Get a string from userland into (buf). Strip a trailing
* nl character (to allow easy access from the shell).
* The buffer should be *buflenp + 1 chars long. procfs_getuserstr
* will automatically add a nul char at the end.
*
* Returns 0 on success or the following errors
*
* EINVAL: file offset is non-zero.
* EMSGSIZE: message is longer than kernel buffer
* EFAULT: user i/o buffer is not addressable
*/
procfs_getuserstr(uio, buf, buflenp)
struct uio *uio;
char *buf;
int *buflenp;
{
int xlen;
int error;
xlen = *buflenp;
/* must be able to read the whole string in one go */
if (xlen < uio->uio_resid)
return (EMSGSIZE);
xlen = uio->uio_resid;
error = uiomove(buf, xlen, uio);
if (error)
return (error);
/* cleanup string and remove trailing newline */
buf[xlen] = '\0';
xlen = strlen(buf);
if (xlen > 0 && buf[xlen-1] == '\n')
buf[--xlen] = '\0';
*buflenp = xlen;
return (0);
}
procfs_namemap_t *
procfs_findname(nm, buf, buflen)
procfs_namemap_t *nm;
char *buf;
int buflen;
{
for (; nm->nm_name; nm++)
if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0)
return (nm);
return (0);
}