Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.17
      1 /*	$NetBSD: procfs_subr.c,v 1.17 1997/05/05 07:14:00 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Jan-Simon Pendry
      5  * Copyright (c) 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Jan-Simon Pendry.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)procfs_subr.c	8.5 (Berkeley) 6/15/94
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/time.h>
     45 #include <sys/kernel.h>
     46 #include <sys/proc.h>
     47 #include <sys/vnode.h>
     48 #include <sys/malloc.h>
     49 #include <miscfs/procfs/procfs.h>
     50 
     51 static struct pfsnode *pfshead;
     52 static int pfsvplock;
     53 
     54 /*
     55  * allocate a pfsnode/vnode pair.  the vnode is
     56  * referenced, but not locked.
     57  *
     58  * the pid, pfs_type, and mount point uniquely
     59  * identify a pfsnode.  the mount point is needed
     60  * because someone might mount this filesystem
     61  * twice.
     62  *
     63  * all pfsnodes are maintained on a singly-linked
     64  * list.  new nodes are only allocated when they cannot
     65  * be found on this list.  entries on the list are
     66  * removed when the vfs reclaim entry is called.
     67  *
     68  * a single lock is kept for the entire list.  this is
     69  * needed because the getnewvnode() function can block
     70  * waiting for a vnode to become free, in which case there
     71  * may be more than one process trying to get the same
     72  * vnode.  this lock is only taken if we are going to
     73  * call getnewvnode, since the kernel itself is single-threaded.
     74  *
     75  * if an entry is found on the list, then call vget() to
     76  * take a reference.  this is done because there may be
     77  * zero references to it and so it needs to removed from
     78  * the vnode free list.
     79  */
     80 int
     81 procfs_allocvp(mp, vpp, pid, pfs_type)
     82 	struct mount *mp;
     83 	struct vnode **vpp;
     84 	long pid;
     85 	pfstype pfs_type;
     86 {
     87 	struct pfsnode *pfs;
     88 	struct vnode *vp;
     89 	struct pfsnode **pp;
     90 	int error;
     91 
     92 loop:
     93 	for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
     94 		vp = PFSTOV(pfs);
     95 		if (pfs->pfs_pid == pid &&
     96 		    pfs->pfs_type == pfs_type &&
     97 		    vp->v_mount == mp) {
     98 			if (vget(vp, 0))
     99 				goto loop;
    100 			*vpp = vp;
    101 			return (0);
    102 		}
    103 	}
    104 
    105 	/*
    106 	 * otherwise lock the vp list while we call getnewvnode
    107 	 * since that can block.
    108 	 */
    109 	if (pfsvplock & PROCFS_LOCKED) {
    110 		pfsvplock |= PROCFS_WANT;
    111 		sleep((caddr_t) &pfsvplock, PINOD);
    112 		goto loop;
    113 	}
    114 	pfsvplock |= PROCFS_LOCKED;
    115 
    116 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) != 0)
    117 		goto out;
    118 	vp = *vpp;
    119 
    120 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    121 	vp->v_data = pfs;
    122 
    123 	pfs->pfs_next = 0;
    124 	pfs->pfs_pid = (pid_t) pid;
    125 	pfs->pfs_type = pfs_type;
    126 	pfs->pfs_vnode = vp;
    127 	pfs->pfs_flags = 0;
    128 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
    129 
    130 	switch (pfs_type) {
    131 	case Proot:	/* /proc = dr-xr-xr-x */
    132 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    133 		vp->v_type = VDIR;
    134 		vp->v_flag = VROOT;
    135 		break;
    136 
    137 	case Pcurproc:	/* /proc/curproc = lr--r--r-- */
    138 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    139 		vp->v_type = VLNK;
    140 		break;
    141 
    142 	case Pproc:	/* /proc/N = dr-xr-xr-x */
    143 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    144 		vp->v_type = VDIR;
    145 		break;
    146 
    147 	case Pfile:	/* /proc/N/file = -rw------- */
    148 	case Pmem:	/* /proc/N/mem = -rw------- */
    149 	case Pregs:	/* /proc/N/regs = -rw------- */
    150 	case Pfpregs:	/* /proc/N/fpregs = -rw------- */
    151 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    152 		vp->v_type = VREG;
    153 		break;
    154 
    155 	case Pctl:	/* /proc/N/ctl = --w------ */
    156 	case Pnote:	/* /proc/N/note = --w------ */
    157 	case Pnotepg:	/* /proc/N/notepg = --w------ */
    158 		pfs->pfs_mode = S_IWUSR;
    159 		vp->v_type = VREG;
    160 		break;
    161 
    162 	case Pstatus:	/* /proc/N/status = -r--r--r-- */
    163 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    164 		vp->v_type = VREG;
    165 		break;
    166 
    167 	default:
    168 		panic("procfs_allocvp");
    169 	}
    170 
    171 	/* add to procfs vnode list */
    172 	for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
    173 		continue;
    174 	*pp = pfs;
    175 
    176 out:
    177 	pfsvplock &= ~PROCFS_LOCKED;
    178 
    179 	if (pfsvplock & PROCFS_WANT) {
    180 		pfsvplock &= ~PROCFS_WANT;
    181 		wakeup((caddr_t) &pfsvplock);
    182 	}
    183 
    184 	return (error);
    185 }
    186 
    187 int
    188 procfs_freevp(vp)
    189 	struct vnode *vp;
    190 {
    191 	struct pfsnode **pfspp;
    192 	struct pfsnode *pfs = VTOPFS(vp);
    193 
    194 	for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
    195 		if (*pfspp == pfs) {
    196 			*pfspp = pfs->pfs_next;
    197 			break;
    198 		}
    199 	}
    200 
    201 	FREE(vp->v_data, M_TEMP);
    202 	vp->v_data = 0;
    203 	return (0);
    204 }
    205 
    206 int
    207 procfs_rw(v)
    208 	void *v;
    209 {
    210 	struct vop_read_args *ap = v;
    211 	struct vnode *vp = ap->a_vp;
    212 	struct uio *uio = ap->a_uio;
    213 	struct proc *curp = uio->uio_procp;
    214 	struct pfsnode *pfs = VTOPFS(vp);
    215 	struct proc *p;
    216 
    217 	p = PFIND(pfs->pfs_pid);
    218 	if (p == 0)
    219 		return (EINVAL);
    220 
    221 	switch (pfs->pfs_type) {
    222 	case Pnote:
    223 	case Pnotepg:
    224 		return (procfs_donote(curp, p, pfs, uio));
    225 
    226 	case Pregs:
    227 		return (procfs_doregs(curp, p, pfs, uio));
    228 
    229 	case Pfpregs:
    230 		return (procfs_dofpregs(curp, p, pfs, uio));
    231 
    232 	case Pctl:
    233 		return (procfs_doctl(curp, p, pfs, uio));
    234 
    235 	case Pstatus:
    236 		return (procfs_dostatus(curp, p, pfs, uio));
    237 
    238 	case Pmem:
    239 		return (procfs_domem(curp, p, pfs, uio));
    240 
    241 	default:
    242 		return (EOPNOTSUPP);
    243 	}
    244 }
    245 
    246 /*
    247  * Get a string from userland into (buf).  Strip a trailing
    248  * nl character (to allow easy access from the shell).
    249  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    250  * will automatically add a nul char at the end.
    251  *
    252  * Returns 0 on success or the following errors
    253  *
    254  * EINVAL:    file offset is non-zero.
    255  * EMSGSIZE:  message is longer than kernel buffer
    256  * EFAULT:    user i/o buffer is not addressable
    257  */
    258 int
    259 vfs_getuserstr(uio, buf, buflenp)
    260 	struct uio *uio;
    261 	char *buf;
    262 	int *buflenp;
    263 {
    264 	int xlen;
    265 	int error;
    266 
    267 	if (uio->uio_offset != 0)
    268 		return (EINVAL);
    269 
    270 	xlen = *buflenp;
    271 
    272 	/* must be able to read the whole string in one go */
    273 	if (xlen < uio->uio_resid)
    274 		return (EMSGSIZE);
    275 	xlen = uio->uio_resid;
    276 
    277 	if ((error = uiomove(buf, xlen, uio)) != 0)
    278 		return (error);
    279 
    280 	/* allow multiple writes without seeks */
    281 	uio->uio_offset = 0;
    282 
    283 	/* cleanup string and remove trailing newline */
    284 	buf[xlen] = '\0';
    285 	xlen = strlen(buf);
    286 	if (xlen > 0 && buf[xlen-1] == '\n')
    287 		buf[--xlen] = '\0';
    288 	*buflenp = xlen;
    289 
    290 	return (0);
    291 }
    292 
    293 vfs_namemap_t *
    294 vfs_findname(nm, buf, buflen)
    295 	vfs_namemap_t *nm;
    296 	char *buf;
    297 	int buflen;
    298 {
    299 
    300 	for (; nm->nm_name; nm++)
    301 		if (bcmp(buf, nm->nm_name, buflen+1) == 0)
    302 			return (nm);
    303 
    304 	return (0);
    305 }
    306