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