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