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