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