Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.27
      1 /*	$NetBSD: procfs_subr.c,v 1.27 1999/07/08 01:26:28 wrstuden 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, and 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, LK_EXCLUSIVE))
    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 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
    170 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    171 		vp->v_type = VREG;
    172 		break;
    173 
    174 	default:
    175 		panic("procfs_allocvp");
    176 	}
    177 
    178 	VOP_LOCK(vp, LK_EXCLUSIVE);
    179 
    180 	/* add to procfs vnode list */
    181 	for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
    182 		continue;
    183 	*pp = pfs;
    184 
    185 out:
    186 	pfsvplock &= ~PROCFS_LOCKED;
    187 
    188 	if (pfsvplock & PROCFS_WANT) {
    189 		pfsvplock &= ~PROCFS_WANT;
    190 		wakeup((caddr_t) &pfsvplock);
    191 	}
    192 
    193 	return (error);
    194 }
    195 
    196 int
    197 procfs_freevp(vp)
    198 	struct vnode *vp;
    199 {
    200 	struct pfsnode **pfspp;
    201 	struct pfsnode *pfs = VTOPFS(vp);
    202 
    203 	for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
    204 		if (*pfspp == pfs) {
    205 			*pfspp = pfs->pfs_next;
    206 			break;
    207 		}
    208 	}
    209 
    210 	FREE(vp->v_data, M_TEMP);
    211 	vp->v_data = 0;
    212 	return (0);
    213 }
    214 
    215 int
    216 procfs_rw(v)
    217 	void *v;
    218 {
    219 	struct vop_read_args *ap = v;
    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 Pregs:
    232 	case Pfpregs:
    233 	case Pmem:
    234 		/*
    235 		 * Do not allow init to be modified while in secure mode; it
    236 		 * could be duped into changing the security level.
    237 		 */
    238 		if (uio->uio_rw == UIO_WRITE &&
    239 		    p == initproc && securelevel > -1)
    240 			return (EPERM);
    241 		break;
    242 
    243 	default:
    244 		break;
    245 	}
    246 
    247 	switch (pfs->pfs_type) {
    248 	case Pnote:
    249 	case Pnotepg:
    250 		return (procfs_donote(curp, p, pfs, uio));
    251 
    252 	case Pregs:
    253 		return (procfs_doregs(curp, p, pfs, uio));
    254 
    255 	case Pfpregs:
    256 		return (procfs_dofpregs(curp, p, pfs, uio));
    257 
    258 	case Pctl:
    259 		return (procfs_doctl(curp, p, pfs, uio));
    260 
    261 	case Pstatus:
    262 		return (procfs_dostatus(curp, p, pfs, uio));
    263 
    264 	case Pmap:
    265 		return (procfs_domap(curp, p, pfs, uio));
    266 
    267 	case Pmem:
    268 		return (procfs_domem(curp, p, pfs, uio));
    269 
    270 	case Pcmdline:
    271 		return (procfs_docmdline(curp, p, pfs, uio));
    272 
    273 	default:
    274 		return (EOPNOTSUPP);
    275 	}
    276 }
    277 
    278 /*
    279  * Get a string from userland into (buf).  Strip a trailing
    280  * nl character (to allow easy access from the shell).
    281  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    282  * will automatically add a nul char at the end.
    283  *
    284  * Returns 0 on success or the following errors
    285  *
    286  * EINVAL:    file offset is non-zero.
    287  * EMSGSIZE:  message is longer than kernel buffer
    288  * EFAULT:    user i/o buffer is not addressable
    289  */
    290 int
    291 vfs_getuserstr(uio, buf, buflenp)
    292 	struct uio *uio;
    293 	char *buf;
    294 	int *buflenp;
    295 {
    296 	int xlen;
    297 	int error;
    298 
    299 	if (uio->uio_offset != 0)
    300 		return (EINVAL);
    301 
    302 	xlen = *buflenp;
    303 
    304 	/* must be able to read the whole string in one go */
    305 	if (xlen < uio->uio_resid)
    306 		return (EMSGSIZE);
    307 	xlen = uio->uio_resid;
    308 
    309 	if ((error = uiomove(buf, xlen, uio)) != 0)
    310 		return (error);
    311 
    312 	/* allow multiple writes without seeks */
    313 	uio->uio_offset = 0;
    314 
    315 	/* cleanup string and remove trailing newline */
    316 	buf[xlen] = '\0';
    317 	xlen = strlen(buf);
    318 	if (xlen > 0 && buf[xlen-1] == '\n')
    319 		buf[--xlen] = '\0';
    320 	*buflenp = xlen;
    321 
    322 	return (0);
    323 }
    324 
    325 vfs_namemap_t *
    326 vfs_findname(nm, buf, buflen)
    327 	vfs_namemap_t *nm;
    328 	char *buf;
    329 	int buflen;
    330 {
    331 
    332 	for (; nm->nm_name; nm++)
    333 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    334 			return (nm);
    335 
    336 	return (0);
    337 }
    338