Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.100.2.2
      1 /*	$NetBSD: procfs_subr.c,v 1.100.2.2 2013/01/16 05:33:46 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * This code is derived from software contributed to Berkeley by
     37  * Jan-Simon Pendry.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  *
     63  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
     64  */
     65 
     66 /*
     67  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     68  * Copyright (c) 1993 Jan-Simon Pendry
     69  *
     70  * This code is derived from software contributed to Berkeley by
     71  * Jan-Simon Pendry.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed by the University of
     84  *	California, Berkeley and its contributors.
     85  * 4. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
    102  */
    103 
    104 #include <sys/cdefs.h>
    105 __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.100.2.2 2013/01/16 05:33:46 yamt Exp $");
    106 
    107 #include <sys/param.h>
    108 #include <sys/systm.h>
    109 #include <sys/time.h>
    110 #include <sys/kernel.h>
    111 #include <sys/proc.h>
    112 #include <sys/vnode.h>
    113 #include <sys/malloc.h>
    114 #include <sys/stat.h>
    115 #include <sys/file.h>
    116 #include <sys/filedesc.h>
    117 #include <sys/kauth.h>
    118 
    119 #include <miscfs/procfs/procfs.h>
    120 
    121 void procfs_hashins(struct pfsnode *);
    122 void procfs_hashrem(struct pfsnode *);
    123 struct vnode *procfs_hashget(pid_t, pfstype, int, struct mount *, int);
    124 
    125 LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
    126 u_long	pfs_ihash;	/* size of hash table - 1 */
    127 #define PFSPIDHASH(pid)	((pid) & pfs_ihash)
    128 
    129 kmutex_t pfs_hashlock;
    130 kmutex_t pfs_ihash_lock;
    131 
    132 #define	ISSET(t, f)	((t) & (f))
    133 
    134 /*
    135  * allocate a pfsnode/vnode pair.  the vnode is
    136  * referenced, and locked.
    137  *
    138  * the pid, pfs_type, and mount point uniquely
    139  * identify a pfsnode.  the mount point is needed
    140  * because someone might mount this filesystem
    141  * twice.
    142  *
    143  * all pfsnodes are maintained on a singly-linked
    144  * list.  new nodes are only allocated when they cannot
    145  * be found on this list.  entries on the list are
    146  * removed when the vfs reclaim entry is called.
    147  *
    148  * a single lock is kept for the entire list.  this is
    149  * needed because the getnewvnode() function can block
    150  * waiting for a vnode to become free, in which case there
    151  * may be more than one process trying to get the same
    152  * vnode.  this lock is only taken if we are going to
    153  * call getnewvnode, since the kernel itself is single-threaded.
    154  *
    155  * if an entry is found on the list, then call vget() to
    156  * take a reference.  this is done because there may be
    157  * zero references to it and so it needs to removed from
    158  * the vnode free list.
    159  */
    160 int
    161 procfs_allocvp(struct mount *mp, struct vnode **vpp, pid_t pid,
    162     pfstype pfs_type, int fd, struct proc *p)
    163 {
    164 	struct pfsnode *pfs;
    165 	struct vnode *vp;
    166 	int error;
    167 
    168  retry:
    169 	*vpp = procfs_hashget(pid, pfs_type, fd, mp, LK_EXCLUSIVE);
    170 	if (*vpp != NULL)
    171 		return (0);
    172 
    173 	error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, NULL, &vp);
    174 	if (error) {
    175 		*vpp = NULL;
    176 		return (error);
    177 	}
    178 	pfs = malloc(sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    179 
    180 	mutex_enter(&pfs_hashlock);
    181 	if ((*vpp = procfs_hashget(pid, pfs_type, fd, mp, 0)) != NULL) {
    182 		mutex_exit(&pfs_hashlock);
    183 		ungetnewvnode(vp);
    184 		free(pfs, M_TEMP);
    185 		goto retry;
    186 	}
    187 
    188 	vp->v_data = pfs;
    189 	pfs->pfs_pid = pid;
    190 	pfs->pfs_type = pfs_type;
    191 	pfs->pfs_vnode = vp;
    192 	pfs->pfs_flags = 0;
    193 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
    194 	pfs->pfs_fd = fd;
    195 
    196 	switch (pfs_type) {
    197 	case PFSroot:	/* /proc = dr-xr-xr-x */
    198 		vp->v_vflag |= VV_ROOT;
    199 		/*FALLTHROUGH*/
    200 	case PFSproc:	/* /proc/N = dr-xr-xr-x */
    201 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    202 		vp->v_type = VDIR;
    203 		break;
    204 
    205 	case PFStask:	/* /proc/N/task = dr-xr-xr-x */
    206 		if (fd == -1) {
    207 			pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
    208 			    S_IROTH|S_IXOTH;
    209 			vp->v_type = VDIR;
    210 			break;
    211 		}
    212 		/*FALLTHROUGH*/
    213 	case PFScurproc:	/* /proc/curproc = lr-xr-xr-x */
    214 	case PFSself:	/* /proc/self    = lr-xr-xr-x */
    215 	case PFScwd:	/* /proc/N/cwd = lr-xr-xr-x */
    216 	case PFSchroot:	/* /proc/N/chroot = lr-xr-xr-x */
    217 	case PFSexe:	/* /proc/N/exe = lr-xr-xr-x */
    218 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    219 		vp->v_type = VLNK;
    220 		break;
    221 
    222 	case PFSfd:
    223 		if (fd == -1) {	/* /proc/N/fd = dr-x------ */
    224 			pfs->pfs_mode = S_IRUSR|S_IXUSR;
    225 			vp->v_type = VDIR;
    226 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    227 			file_t *fp;
    228 			vnode_t *vxp;
    229 
    230 			if ((fp = fd_getfile2(p, pfs->pfs_fd)) == NULL) {
    231 				error = EBADF;
    232 				goto bad;
    233 			}
    234 
    235 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    236 			switch (fp->f_type) {
    237 			case DTYPE_VNODE:
    238 				vxp = fp->f_data;
    239 
    240 				/*
    241 				 * We make symlinks for directories
    242 				 * to avoid cycles.
    243 				 */
    244 				if (vxp->v_type == VDIR)
    245 					goto symlink;
    246 				vp->v_type = vxp->v_type;
    247 				break;
    248 			case DTYPE_PIPE:
    249 				vp->v_type = VFIFO;
    250 				break;
    251 			case DTYPE_SOCKET:
    252 				vp->v_type = VSOCK;
    253 				break;
    254 			case DTYPE_KQUEUE:
    255 			case DTYPE_MISC:
    256 			case DTYPE_SEM:
    257 			symlink:
    258 				pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
    259 				    S_IXGRP|S_IROTH|S_IXOTH;
    260 				vp->v_type = VLNK;
    261 				break;
    262 			default:
    263 				error = EOPNOTSUPP;
    264 				closef(fp);
    265 				goto bad;
    266 			}
    267 			closef(fp);
    268 		}
    269 		break;
    270 
    271 	case PFSfile:	/* /proc/N/file = -rw------- */
    272 	case PFSmem:	/* /proc/N/mem = -rw------- */
    273 	case PFSregs:	/* /proc/N/regs = -rw------- */
    274 	case PFSfpregs:	/* /proc/N/fpregs = -rw------- */
    275 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    276 		vp->v_type = VREG;
    277 		break;
    278 
    279 	case PFSctl:	/* /proc/N/ctl = --w------ */
    280 	case PFSnote:	/* /proc/N/note = --w------ */
    281 	case PFSnotepg:	/* /proc/N/notepg = --w------ */
    282 		pfs->pfs_mode = S_IWUSR;
    283 		vp->v_type = VREG;
    284 		break;
    285 
    286 	case PFSmap:	/* /proc/N/map = -r--r--r-- */
    287 	case PFSmaps:	/* /proc/N/maps = -r--r--r-- */
    288 	case PFSstatus:	/* /proc/N/status = -r--r--r-- */
    289 	case PFSstat:	/* /proc/N/stat = -r--r--r-- */
    290 	case PFScmdline:	/* /proc/N/cmdline = -r--r--r-- */
    291 	case PFSemul:	/* /proc/N/emul = -r--r--r-- */
    292 	case PFSmeminfo:	/* /proc/meminfo = -r--r--r-- */
    293 	case PFScpustat:	/* /proc/stat = -r--r--r-- */
    294 	case PFSdevices:	/* /proc/devices = -r--r--r-- */
    295 	case PFScpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    296 	case PFSuptime:	/* /proc/uptime = -r--r--r-- */
    297 	case PFSmounts:	/* /proc/mounts = -r--r--r-- */
    298 	case PFSloadavg:	/* /proc/loadavg = -r--r--r-- */
    299 	case PFSstatm:	/* /proc/N/statm = -r--r--r-- */
    300 	case PFSversion:	/* /proc/version = -r--r--r-- */
    301 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    302 		vp->v_type = VREG;
    303 		break;
    304 
    305 #ifdef __HAVE_PROCFS_MACHDEP
    306 	PROCFS_MACHDEP_NODETYPE_CASES
    307 		procfs_machdep_allocvp(vp);
    308 		break;
    309 #endif
    310 
    311 	default:
    312 		panic("procfs_allocvp");
    313 	}
    314 
    315 	procfs_hashins(pfs);
    316 	uvm_vnp_setsize(vp, 0);
    317 	mutex_exit(&pfs_hashlock);
    318 
    319 	*vpp = vp;
    320 	return (0);
    321 
    322  bad:
    323 	mutex_exit(&pfs_hashlock);
    324 	free(pfs, M_TEMP);
    325 	vp->v_data = NULL;
    326 	ungetnewvnode(vp);
    327 	return (error);
    328 }
    329 
    330 int
    331 procfs_freevp(struct vnode *vp)
    332 {
    333 	struct pfsnode *pfs = VTOPFS(vp);
    334 
    335 	procfs_hashrem(pfs);
    336 
    337 	free(vp->v_data, M_TEMP);
    338 	vp->v_data = NULL;
    339 	return (0);
    340 }
    341 
    342 int
    343 procfs_rw(void *v)
    344 {
    345 	struct vop_read_args *ap = v;
    346 	struct vnode *vp = ap->a_vp;
    347 	struct uio *uio = ap->a_uio;
    348 	struct lwp *curl;
    349 	struct lwp *l;
    350 	struct pfsnode *pfs = VTOPFS(vp);
    351 	struct proc *p;
    352 	int error;
    353 
    354 	if (uio->uio_offset < 0)
    355 		return EINVAL;
    356 
    357 	if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
    358 		return error;
    359 
    360 	curl = curlwp;
    361 
    362 	/*
    363 	 * Do not allow init to be modified while in secure mode; it
    364 	 * could be duped into changing the security level.
    365 	 */
    366 #define	M2K(m)	((m) == UIO_READ ? KAUTH_REQ_PROCESS_PROCFS_READ : \
    367 		 KAUTH_REQ_PROCESS_PROCFS_WRITE)
    368 	mutex_enter(p->p_lock);
    369 	error = kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_PROCFS,
    370 	    p, pfs, KAUTH_ARG(M2K(uio->uio_rw)), NULL);
    371 	mutex_exit(p->p_lock);
    372 	if (error) {
    373 		procfs_proc_unlock(p);
    374 		return (error);
    375 	}
    376 #undef	M2K
    377 
    378 	mutex_enter(p->p_lock);
    379 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    380 		if (l->l_stat != LSZOMB)
    381 			break;
    382 	}
    383 	/* Process is exiting if no-LWPS or all LWPs are LSZOMB */
    384 	if (l == NULL) {
    385 		mutex_exit(p->p_lock);
    386 		procfs_proc_unlock(p);
    387 		return ESRCH;
    388 	}
    389 
    390 	lwp_addref(l);
    391 	mutex_exit(p->p_lock);
    392 
    393 	switch (pfs->pfs_type) {
    394 	case PFSnote:
    395 	case PFSnotepg:
    396 		error = procfs_donote(curl, p, pfs, uio);
    397 		break;
    398 
    399 	case PFSregs:
    400 		error = procfs_doregs(curl, l, pfs, uio);
    401 		break;
    402 
    403 	case PFSfpregs:
    404 		error = procfs_dofpregs(curl, l, pfs, uio);
    405 		break;
    406 
    407 	case PFSctl:
    408 		error = procfs_doctl(curl, l, pfs, uio);
    409 		break;
    410 
    411 	case PFSstatus:
    412 		error = procfs_dostatus(curl, l, pfs, uio);
    413 		break;
    414 
    415 	case PFSstat:
    416 		error = procfs_do_pid_stat(curl, l, pfs, uio);
    417 		break;
    418 
    419 	case PFSmap:
    420 		error = procfs_domap(curl, p, pfs, uio, 0);
    421 		break;
    422 
    423 	case PFSmaps:
    424 		error = procfs_domap(curl, p, pfs, uio, 1);
    425 		break;
    426 
    427 	case PFSmem:
    428 		error = procfs_domem(curl, l, pfs, uio);
    429 		break;
    430 
    431 	case PFScmdline:
    432 		error = procfs_docmdline(curl, p, pfs, uio);
    433 		break;
    434 
    435 	case PFSmeminfo:
    436 		error = procfs_domeminfo(curl, p, pfs, uio);
    437 		break;
    438 
    439 	case PFSdevices:
    440 		error = procfs_dodevices(curl, p, pfs, uio);
    441 		break;
    442 
    443 	case PFScpuinfo:
    444 		error = procfs_docpuinfo(curl, p, pfs, uio);
    445 		break;
    446 
    447 	case PFScpustat:
    448 		error = procfs_docpustat(curl, p, pfs, uio);
    449 		break;
    450 
    451 	case PFSloadavg:
    452 		error = procfs_doloadavg(curl, p, pfs, uio);
    453 		break;
    454 
    455 	case PFSstatm:
    456 		error = procfs_do_pid_statm(curl, l, pfs, uio);
    457 		break;
    458 
    459 	case PFSfd:
    460 		error = procfs_dofd(curl, p, pfs, uio);
    461 		break;
    462 
    463 	case PFSuptime:
    464 		error = procfs_douptime(curl, p, pfs, uio);
    465 		break;
    466 
    467 	case PFSmounts:
    468 		error = procfs_domounts(curl, p, pfs, uio);
    469 		break;
    470 
    471 	case PFSemul:
    472 		error = procfs_doemul(curl, p, pfs, uio);
    473 		break;
    474 
    475 	case PFSversion:
    476 		error = procfs_doversion(curl, p, pfs, uio);
    477 		break;
    478 
    479 #ifdef __HAVE_PROCFS_MACHDEP
    480 	PROCFS_MACHDEP_NODETYPE_CASES
    481 		error = procfs_machdep_rw(curl, l, pfs, uio);
    482 		break;
    483 #endif
    484 
    485 	default:
    486 		error = EOPNOTSUPP;
    487 		break;
    488 	}
    489 
    490 	/*
    491 	 * Release the references that we acquired earlier.
    492 	 */
    493 	lwp_delref(l);
    494 	procfs_proc_unlock(p);
    495 
    496 	return (error);
    497 }
    498 
    499 /*
    500  * Get a string from userland into (bf).  Strip a trailing
    501  * nl character (to allow easy access from the shell).
    502  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    503  * will automatically add a nul char at the end.
    504  *
    505  * Returns 0 on success or the following errors
    506  *
    507  * EINVAL:    file offset is non-zero.
    508  * EMSGSIZE:  message is longer than kernel buffer
    509  * EFAULT:    user i/o buffer is not addressable
    510  */
    511 int
    512 vfs_getuserstr(struct uio *uio, char *bf, int *buflenp)
    513 {
    514 	int xlen;
    515 	int error;
    516 
    517 	if (uio->uio_offset != 0)
    518 		return (EINVAL);
    519 
    520 	xlen = *buflenp;
    521 
    522 	/* must be able to read the whole string in one go */
    523 	if (xlen < uio->uio_resid)
    524 		return (EMSGSIZE);
    525 	xlen = uio->uio_resid;
    526 
    527 	if ((error = uiomove(bf, xlen, uio)) != 0)
    528 		return (error);
    529 
    530 	/* allow multiple writes without seeks */
    531 	uio->uio_offset = 0;
    532 
    533 	/* cleanup string and remove trailing newline */
    534 	bf[xlen] = '\0';
    535 	xlen = strlen(bf);
    536 	if (xlen > 0 && bf[xlen-1] == '\n')
    537 		bf[--xlen] = '\0';
    538 	*buflenp = xlen;
    539 
    540 	return (0);
    541 }
    542 
    543 const vfs_namemap_t *
    544 vfs_findname(const vfs_namemap_t *nm, const char *bf, int buflen)
    545 {
    546 
    547 	for (; nm->nm_name; nm++)
    548 		if (memcmp(bf, nm->nm_name, buflen+1) == 0)
    549 			return (nm);
    550 
    551 	return (0);
    552 }
    553 
    554 /*
    555  * Initialize pfsnode hash table.
    556  */
    557 void
    558 procfs_hashinit(void)
    559 {
    560 	mutex_init(&pfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
    561 	mutex_init(&pfs_ihash_lock, MUTEX_DEFAULT, IPL_NONE);
    562 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, true, &pfs_ihash);
    563 }
    564 
    565 void
    566 procfs_hashreinit(void)
    567 {
    568 	struct pfsnode *pp;
    569 	struct pfs_hashhead *oldhash, *hash;
    570 	u_long i, oldmask, mask, val;
    571 
    572 	hash = hashinit(desiredvnodes / 4, HASH_LIST, true, &mask);
    573 
    574 	mutex_enter(&pfs_ihash_lock);
    575 	oldhash = pfs_hashtbl;
    576 	oldmask = pfs_ihash;
    577 	pfs_hashtbl = hash;
    578 	pfs_ihash = mask;
    579 	for (i = 0; i <= oldmask; i++) {
    580 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    581 			LIST_REMOVE(pp, pfs_hash);
    582 			val = PFSPIDHASH(pp->pfs_pid);
    583 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    584 		}
    585 	}
    586 	mutex_exit(&pfs_ihash_lock);
    587 	hashdone(oldhash, HASH_LIST, oldmask);
    588 }
    589 
    590 /*
    591  * Free pfsnode hash table.
    592  */
    593 void
    594 procfs_hashdone(void)
    595 {
    596 	hashdone(pfs_hashtbl, HASH_LIST, pfs_ihash);
    597 	mutex_destroy(&pfs_hashlock);
    598 	mutex_destroy(&pfs_ihash_lock);
    599 }
    600 
    601 struct vnode *
    602 procfs_hashget(pid_t pid, pfstype type, int fd, struct mount *mp, int flags)
    603 {
    604 	struct pfs_hashhead *ppp;
    605 	struct pfsnode *pp;
    606 	struct vnode *vp;
    607 
    608 loop:
    609 	mutex_enter(&pfs_ihash_lock);
    610 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    611 	LIST_FOREACH(pp, ppp, pfs_hash) {
    612 		vp = PFSTOV(pp);
    613 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    614 		    pp->pfs_fd == fd && vp->v_mount == mp) {
    615 		    	if (flags == 0) {
    616 				mutex_exit(&pfs_ihash_lock);
    617 			} else {
    618 				mutex_enter(vp->v_interlock);
    619 				mutex_exit(&pfs_ihash_lock);
    620 				if (vget(vp, flags))
    621 					goto loop;
    622 			}
    623 			return (vp);
    624 		}
    625 	}
    626 	mutex_exit(&pfs_ihash_lock);
    627 	return (NULL);
    628 }
    629 
    630 /*
    631  * Insert the pfsnode into the hash table and lock it.
    632  */
    633 void
    634 procfs_hashins(struct pfsnode *pp)
    635 {
    636 	struct pfs_hashhead *ppp;
    637 
    638 	/* lock the pfsnode, then put it on the appropriate hash list */
    639 	VOP_LOCK(PFSTOV(pp), LK_EXCLUSIVE);
    640 
    641 	mutex_enter(&pfs_ihash_lock);
    642 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    643 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    644 	mutex_exit(&pfs_ihash_lock);
    645 }
    646 
    647 /*
    648  * Remove the pfsnode from the hash table.
    649  */
    650 void
    651 procfs_hashrem(struct pfsnode *pp)
    652 {
    653 	mutex_enter(&pfs_ihash_lock);
    654 	LIST_REMOVE(pp, pfs_hash);
    655 	mutex_exit(&pfs_ihash_lock);
    656 }
    657 
    658 void
    659 procfs_revoke_vnodes(struct proc *p, void *arg)
    660 {
    661 	struct pfsnode *pfs, *pnext;
    662 	struct vnode *vp;
    663 	struct mount *mp = (struct mount *)arg;
    664 	struct pfs_hashhead *ppp;
    665 
    666 	if (!(p->p_flag & PK_SUGID))
    667 		return;
    668 
    669 	mutex_enter(&pfs_ihash_lock);
    670 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    671 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    672 		vp = PFSTOV(pfs);
    673 		pnext = LIST_NEXT(pfs, pfs_hash);
    674 		mutex_enter(vp->v_interlock);
    675 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    676 		    vp->v_mount == mp) {
    677 		    	vp->v_usecount++;
    678 		    	mutex_exit(vp->v_interlock);
    679 			mutex_exit(&pfs_ihash_lock);
    680 			VOP_REVOKE(vp, REVOKEALL);
    681 			vrele(vp);
    682 			mutex_enter(&pfs_ihash_lock);
    683 		} else {
    684 			mutex_exit(vp->v_interlock);
    685 		}
    686 	}
    687 	mutex_exit(&pfs_ihash_lock);
    688 }
    689 
    690 int
    691 procfs_proc_lock(int pid, struct proc **bunghole, int notfound)
    692 {
    693 	struct proc *tp;
    694 	int error = 0;
    695 
    696 	mutex_enter(proc_lock);
    697 
    698 	if (pid == 0)
    699 		tp = &proc0;
    700 	else if ((tp = proc_find(pid)) == NULL)
    701 		error = notfound;
    702 	if (tp != NULL && !rw_tryenter(&tp->p_reflock, RW_READER))
    703 		error = EBUSY;
    704 
    705 	mutex_exit(proc_lock);
    706 
    707 	*bunghole = tp;
    708 	return error;
    709 }
    710 
    711 void
    712 procfs_proc_unlock(struct proc *p)
    713 {
    714 
    715 	rw_exit(&p->p_reflock);
    716 }
    717 
    718 int
    719 procfs_doemul(struct lwp *curl, struct proc *p,
    720     struct pfsnode *pfs, struct uio *uio)
    721 {
    722 	const char *ename = p->p_emul->e_name;
    723 	return uiomove_frombuf(__UNCONST(ename), strlen(ename), uio);
    724 }
    725