Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.26.2.2
      1 /*	$NetBSD: procfs_subr.c,v 1.26.2.2 2000/02/28 09:47:49 he 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 void procfs_hashins __P((struct pfsnode *));
     55 void procfs_hashrem __P((struct pfsnode *));
     56 struct vnode *procfs_hashget __P((pid_t, pfstype, struct mount *));
     57 
     58 LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
     59 u_long	pfs_ihash;	/* size of hash table - 1 */
     60 #define PFSPIDHASH(pid)	(&pfs_hashtbl[(pid) & pfs_ihash])
     61 
     62 struct lock pfs_hashlock;
     63 struct simplelock pfs_hash_slock;
     64 
     65 #define	ISSET(t, f)	((t) & (f))
     66 
     67 /*
     68  * allocate a pfsnode/vnode pair.  the vnode is
     69  * referenced, but not locked.
     70  *
     71  * the pid, pfs_type, and mount point uniquely
     72  * identify a pfsnode.  the mount point is needed
     73  * because someone might mount this filesystem
     74  * twice.
     75  *
     76  * all pfsnodes are maintained on a singly-linked
     77  * list.  new nodes are only allocated when they cannot
     78  * be found on this list.  entries on the list are
     79  * removed when the vfs reclaim entry is called.
     80  *
     81  * a single lock is kept for the entire list.  this is
     82  * needed because the getnewvnode() function can block
     83  * waiting for a vnode to become free, in which case there
     84  * may be more than one process trying to get the same
     85  * vnode.  this lock is only taken if we are going to
     86  * call getnewvnode, since the kernel itself is single-threaded.
     87  *
     88  * if an entry is found on the list, then call vget() to
     89  * take a reference.  this is done because there may be
     90  * zero references to it and so it needs to removed from
     91  * the vnode free list.
     92  */
     93 int
     94 procfs_allocvp(mp, vpp, pid, pfs_type)
     95 	struct mount *mp;
     96 	struct vnode **vpp;
     97 	long pid;
     98 	pfstype pfs_type;
     99 {
    100 	struct pfsnode *pfs;
    101 	struct vnode *vp;
    102 	int error;
    103 
    104 	do {
    105 		if ((*vpp = procfs_hashget(pid, pfs_type, mp)) != NULL)
    106 			return (0);
    107 	} while (lockmgr(&pfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    108 
    109 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) != 0) {
    110 		*vpp = NULL;
    111 		lockmgr(&pfs_hashlock, LK_RELEASE, 0);
    112 		return (error);
    113 	}
    114 	vp = *vpp;
    115 
    116 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    117 	vp->v_data = pfs;
    118 
    119 	pfs->pfs_pid = (pid_t) pid;
    120 	pfs->pfs_type = pfs_type;
    121 	pfs->pfs_vnode = vp;
    122 	pfs->pfs_flags = 0;
    123 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
    124 
    125 	switch (pfs_type) {
    126 	case Proot:	/* /proc = dr-xr-xr-x */
    127 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    128 		vp->v_type = VDIR;
    129 		vp->v_flag = VROOT;
    130 		break;
    131 
    132 	case Pcurproc:	/* /proc/curproc = lr-xr-xr-x */
    133 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    134 		vp->v_type = VLNK;
    135 		break;
    136 
    137 	case Pproc:	/* /proc/N = dr-xr-xr-x */
    138 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    139 		vp->v_type = VDIR;
    140 		break;
    141 
    142 	case Pfile:	/* /proc/N/file = -rw------- */
    143 	case Pmem:	/* /proc/N/mem = -rw------- */
    144 	case Pregs:	/* /proc/N/regs = -rw------- */
    145 	case Pfpregs:	/* /proc/N/fpregs = -rw------- */
    146 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    147 		vp->v_type = VREG;
    148 		break;
    149 
    150 	case Pctl:	/* /proc/N/ctl = --w------ */
    151 	case Pnote:	/* /proc/N/note = --w------ */
    152 	case Pnotepg:	/* /proc/N/notepg = --w------ */
    153 		pfs->pfs_mode = S_IWUSR;
    154 		vp->v_type = VREG;
    155 		break;
    156 
    157 	case Pmap:	/* /proc/N/map = -r--r--r-- */
    158 	case Pstatus:	/* /proc/N/status = -r--r--r-- */
    159 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
    160 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    161 		vp->v_type = VREG;
    162 		break;
    163 
    164 	default:
    165 		panic("procfs_allocvp");
    166 	}
    167 
    168 	procfs_hashins(pfs);
    169 	lockmgr(&pfs_hashlock, LK_RELEASE, 0);
    170 
    171 	return (error);
    172 }
    173 
    174 int
    175 procfs_freevp(vp)
    176 	struct vnode *vp;
    177 {
    178 	struct pfsnode *pfs = VTOPFS(vp);
    179 
    180 	procfs_hashrem(pfs);
    181 
    182 	FREE(vp->v_data, M_TEMP);
    183 	vp->v_data = 0;
    184 	return (0);
    185 }
    186 
    187 int
    188 procfs_rw(v)
    189 	void *v;
    190 {
    191 	struct vop_read_args *ap = v;
    192 	struct vnode *vp = ap->a_vp;
    193 	struct uio *uio = ap->a_uio;
    194 	struct proc *curp = uio->uio_procp;
    195 	struct pfsnode *pfs = VTOPFS(vp);
    196 	struct proc *p;
    197 
    198 	p = PFIND(pfs->pfs_pid);
    199 	if (p == 0)
    200 		return (EINVAL);
    201 
    202 	switch (pfs->pfs_type) {
    203 	case Pregs:
    204 	case Pfpregs:
    205 	case Pmem:
    206 		/*
    207 		 * Do not allow init to be modified while in secure mode; it
    208 		 * could be duped into changing the security level.
    209 		 */
    210 		if (uio->uio_rw == UIO_WRITE &&
    211 		    p == initproc && securelevel > -1)
    212 			return (EPERM);
    213 		break;
    214 
    215 	default:
    216 		break;
    217 	}
    218 
    219 	switch (pfs->pfs_type) {
    220 	case Pnote:
    221 	case Pnotepg:
    222 		return (procfs_donote(curp, p, pfs, uio));
    223 
    224 	case Pregs:
    225 		return (procfs_doregs(curp, p, pfs, uio));
    226 
    227 	case Pfpregs:
    228 		return (procfs_dofpregs(curp, p, pfs, uio));
    229 
    230 	case Pctl:
    231 		return (procfs_doctl(curp, p, pfs, uio));
    232 
    233 	case Pstatus:
    234 		return (procfs_dostatus(curp, p, pfs, uio));
    235 
    236 	case Pmap:
    237 		return (procfs_domap(curp, p, pfs, uio));
    238 
    239 	case Pmem:
    240 		return (procfs_domem(curp, p, pfs, uio));
    241 
    242 	case Pcmdline:
    243 		return (procfs_docmdline(curp, p, pfs, uio));
    244 
    245 	default:
    246 		return (EOPNOTSUPP);
    247 	}
    248 }
    249 
    250 /*
    251  * Get a string from userland into (buf).  Strip a trailing
    252  * nl character (to allow easy access from the shell).
    253  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    254  * will automatically add a nul char at the end.
    255  *
    256  * Returns 0 on success or the following errors
    257  *
    258  * EINVAL:    file offset is non-zero.
    259  * EMSGSIZE:  message is longer than kernel buffer
    260  * EFAULT:    user i/o buffer is not addressable
    261  */
    262 int
    263 vfs_getuserstr(uio, buf, buflenp)
    264 	struct uio *uio;
    265 	char *buf;
    266 	int *buflenp;
    267 {
    268 	int xlen;
    269 	int error;
    270 
    271 	if (uio->uio_offset != 0)
    272 		return (EINVAL);
    273 
    274 	xlen = *buflenp;
    275 
    276 	/* must be able to read the whole string in one go */
    277 	if (xlen < uio->uio_resid)
    278 		return (EMSGSIZE);
    279 	xlen = uio->uio_resid;
    280 
    281 	if ((error = uiomove(buf, xlen, uio)) != 0)
    282 		return (error);
    283 
    284 	/* allow multiple writes without seeks */
    285 	uio->uio_offset = 0;
    286 
    287 	/* cleanup string and remove trailing newline */
    288 	buf[xlen] = '\0';
    289 	xlen = strlen(buf);
    290 	if (xlen > 0 && buf[xlen-1] == '\n')
    291 		buf[--xlen] = '\0';
    292 	*buflenp = xlen;
    293 
    294 	return (0);
    295 }
    296 
    297 vfs_namemap_t *
    298 vfs_findname(nm, buf, buflen)
    299 	vfs_namemap_t *nm;
    300 	char *buf;
    301 	int buflen;
    302 {
    303 
    304 	for (; nm->nm_name; nm++)
    305 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    306 			return (nm);
    307 
    308 	return (0);
    309 }
    310 
    311 /*
    312  * Initialize pfsnode hash table.
    313  */
    314 void
    315 procfs_hashinit()
    316 {
    317 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    318 	pfs_hashtbl = hashinit(desiredvnodes / 4, M_UFSMNT, M_WAITOK,
    319 	    &pfs_ihash);
    320 	simple_lock_init(&pfs_hash_slock);
    321 }
    322 
    323 struct vnode *
    324 procfs_hashget(pid, type, mp)
    325 	pid_t pid;
    326 	pfstype type;
    327 	struct mount *mp;
    328 {
    329 	struct pfsnode *pp;
    330 	struct vnode *vp;
    331 
    332 loop:
    333 	simple_lock(&pfs_hash_slock);
    334 	for (pp = PFSPIDHASH(pid)->lh_first; pp; pp = pp->pfs_hash.le_next) {
    335 		vp = PFSTOV(pp);
    336 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    337 		    vp->v_mount == mp) {
    338 			simple_unlock(&pfs_hash_slock);
    339 			if (vget(vp, 0))
    340 				goto loop;
    341 			return (vp);
    342 		}
    343 	}
    344 	simple_unlock(&pfs_hash_slock);
    345 	return (NULL);
    346 }
    347 
    348 /*
    349  * Insert the pfsnode into the hash table and lock it.
    350  */
    351 void
    352 procfs_hashins(pp)
    353 	struct pfsnode *pp;
    354 {
    355 	struct pfs_hashhead *ppp;
    356 
    357 	simple_lock(&pfs_hash_slock);
    358 	ppp = PFSPIDHASH(pp->pfs_pid);
    359 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    360 	simple_unlock(&pfs_hash_slock);
    361 }
    362 
    363 /*
    364  * Remove the pfsnode from the hash table.
    365  */
    366 void
    367 procfs_hashrem(pp)
    368 	struct pfsnode *pp;
    369 {
    370 	simple_lock(&pfs_hash_slock);
    371 	LIST_REMOVE(pp, pfs_hash);
    372 	simple_unlock(&pfs_hash_slock);
    373 }
    374 
    375 void
    376 procfs_revoke_vnodes(p, arg)
    377 	struct proc *p;
    378 	void *arg;
    379 {
    380 	struct pfsnode *pfs, *pnext;
    381 	struct vnode *vp;
    382 	struct mount *mp = (struct mount *)arg;
    383 
    384 	if (!(p->p_flag & P_SUGID))
    385 		return;
    386 
    387 	for (pfs = PFSPIDHASH(p->p_pid)->lh_first; pfs; pfs = pnext) {
    388 		vp = PFSTOV(pfs);
    389 		pnext = pfs->pfs_hash.le_next;
    390 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    391 		    vp->v_mount == mp)
    392 			VOP_REVOKE(vp, REVOKEALL);
    393 	}
    394 }
    395