xref: /netbsd-src/sys/miscfs/procfs/procfs_vnops.c (revision 4b71a66d0f279143147d63ebfcfd8a59499a3684)
1 /*	$NetBSD: procfs_vnops.c,v 1.169 2008/04/28 20:24:08 martin 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, 1995
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_vnops.c	8.18 (Berkeley) 5/21/95
64  */
65 
66 /*
67  * Copyright (c) 1993 Jan-Simon Pendry
68  *
69  * This code is derived from software contributed to Berkeley by
70  * Jan-Simon Pendry.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  *    notice, this list of conditions and the following disclaimer in the
79  *    documentation and/or other materials provided with the distribution.
80  * 3. All advertising materials mentioning features or use of this software
81  *    must display the following acknowledgement:
82  *	This product includes software developed by the University of
83  *	California, Berkeley and its contributors.
84  * 4. Neither the name of the University nor the names of its contributors
85  *    may be used to endorse or promote products derived from this software
86  *    without specific prior written permission.
87  *
88  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98  * SUCH DAMAGE.
99  *
100  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
101  */
102 
103 /*
104  * procfs vnode interface
105  */
106 
107 #include <sys/cdefs.h>
108 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.169 2008/04/28 20:24:08 martin Exp $");
109 
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/time.h>
113 #include <sys/kernel.h>
114 #include <sys/file.h>
115 #include <sys/filedesc.h>
116 #include <sys/proc.h>
117 #include <sys/vnode.h>
118 #include <sys/namei.h>
119 #include <sys/malloc.h>
120 #include <sys/mount.h>
121 #include <sys/dirent.h>
122 #include <sys/resourcevar.h>
123 #include <sys/stat.h>
124 #include <sys/ptrace.h>
125 #include <sys/kauth.h>
126 
127 #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
128 
129 #include <machine/reg.h>
130 
131 #include <miscfs/genfs/genfs.h>
132 #include <miscfs/procfs/procfs.h>
133 
134 /*
135  * Vnode Operations.
136  *
137  */
138 
139 static int procfs_validfile_linux(struct lwp *, struct mount *);
140 static int procfs_root_readdir_callback(struct proc *, void *);
141 static void procfs_dir(pfstype, struct lwp *, struct proc *, char **, char *,
142     size_t);
143 
144 /*
145  * This is a list of the valid names in the
146  * process-specific sub-directories.  It is
147  * used in procfs_lookup and procfs_readdir
148  */
149 static const struct proc_target {
150 	u_char	pt_type;
151 	u_char	pt_namlen;
152 	const char	*pt_name;
153 	pfstype	pt_pfstype;
154 	int	(*pt_valid)(struct lwp *, struct mount *);
155 } proc_targets[] = {
156 #define N(s) sizeof(s)-1, s
157 	/*	  name		type		validp */
158 	{ DT_DIR, N("."),	PFSproc,	NULL },
159 	{ DT_DIR, N(".."),	PFSroot,	NULL },
160 	{ DT_DIR, N("fd"),	PFSfd,		NULL },
161 	{ DT_REG, N("file"),	PFSfile,	procfs_validfile },
162 	{ DT_REG, N("mem"),	PFSmem,		NULL },
163 	{ DT_REG, N("regs"),	PFSregs,	procfs_validregs },
164 	{ DT_REG, N("fpregs"),	PFSfpregs,	procfs_validfpregs },
165 	{ DT_REG, N("ctl"),	PFSctl,		NULL },
166 	{ DT_REG, N("stat"),	PFSstat,	procfs_validfile_linux },
167 	{ DT_REG, N("status"),	PFSstatus,	NULL },
168 	{ DT_REG, N("note"),	PFSnote,	NULL },
169 	{ DT_REG, N("notepg"),	PFSnotepg,	NULL },
170 	{ DT_REG, N("map"),	PFSmap,		procfs_validmap },
171 	{ DT_REG, N("maps"),	PFSmaps,	procfs_validmap },
172 	{ DT_REG, N("cmdline"), PFScmdline,	NULL },
173 	{ DT_REG, N("exe"),	PFSexe,		procfs_validfile },
174 	{ DT_LNK, N("cwd"),	PFScwd,		NULL },
175 	{ DT_LNK, N("root"),	PFSchroot,	NULL },
176 	{ DT_LNK, N("emul"),	PFSemul,	NULL },
177 	{ DT_REG, N("statm"),	PFSstatm,	procfs_validfile_linux },
178 #ifdef __HAVE_PROCFS_MACHDEP
179 	PROCFS_MACHDEP_NODETYPE_DEFNS
180 #endif
181 #undef N
182 };
183 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
184 
185 /*
186  * List of files in the root directory. Note: the validate function will
187  * be called with p == NULL for these ones.
188  */
189 static const struct proc_target proc_root_targets[] = {
190 #define N(s) sizeof(s)-1, s
191 	/*	  name		    type	    validp */
192 	{ DT_REG, N("meminfo"),     PFSmeminfo,        procfs_validfile_linux },
193 	{ DT_REG, N("cpuinfo"),     PFScpuinfo,        procfs_validfile_linux },
194 	{ DT_REG, N("uptime"),      PFSuptime,         procfs_validfile_linux },
195 	{ DT_REG, N("mounts"),	    PFSmounts,	       procfs_validfile_linux },
196 	{ DT_REG, N("devices"),     PFSdevices,        procfs_validfile_linux },
197 	{ DT_REG, N("stat"),	    PFScpustat,        procfs_validfile_linux },
198 	{ DT_REG, N("loadavg"),	    PFSloadavg,        procfs_validfile_linux },
199 #undef N
200 };
201 static const int nproc_root_targets =
202     sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
203 
204 int	procfs_lookup(void *);
205 #define	procfs_create	genfs_eopnotsupp
206 #define	procfs_mknod	genfs_eopnotsupp
207 int	procfs_open(void *);
208 int	procfs_close(void *);
209 int	procfs_access(void *);
210 int	procfs_getattr(void *);
211 int	procfs_setattr(void *);
212 #define	procfs_read	procfs_rw
213 #define	procfs_write	procfs_rw
214 #define	procfs_fcntl	genfs_fcntl
215 #define	procfs_ioctl	genfs_enoioctl
216 #define	procfs_poll	genfs_poll
217 #define procfs_revoke	genfs_revoke
218 #define	procfs_fsync	genfs_nullop
219 #define	procfs_seek	genfs_nullop
220 #define	procfs_remove	genfs_eopnotsupp
221 int	procfs_link(void *);
222 #define	procfs_rename	genfs_eopnotsupp
223 #define	procfs_mkdir	genfs_eopnotsupp
224 #define	procfs_rmdir	genfs_eopnotsupp
225 int	procfs_symlink(void *);
226 int	procfs_readdir(void *);
227 int	procfs_readlink(void *);
228 #define	procfs_abortop	genfs_abortop
229 int	procfs_inactive(void *);
230 int	procfs_reclaim(void *);
231 #define	procfs_lock	genfs_lock
232 #define	procfs_unlock	genfs_unlock
233 #define	procfs_bmap	genfs_badop
234 #define	procfs_strategy	genfs_badop
235 int	procfs_print(void *);
236 int	procfs_pathconf(void *);
237 #define	procfs_islocked	genfs_islocked
238 #define	procfs_advlock	genfs_einval
239 #define	procfs_bwrite	genfs_eopnotsupp
240 #define procfs_putpages	genfs_null_putpages
241 
242 static int atoi(const char *, size_t);
243 
244 /*
245  * procfs vnode operations.
246  */
247 int (**procfs_vnodeop_p)(void *);
248 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
249 	{ &vop_default_desc, vn_default_error },
250 	{ &vop_lookup_desc, procfs_lookup },		/* lookup */
251 	{ &vop_create_desc, procfs_create },		/* create */
252 	{ &vop_mknod_desc, procfs_mknod },		/* mknod */
253 	{ &vop_open_desc, procfs_open },		/* open */
254 	{ &vop_close_desc, procfs_close },		/* close */
255 	{ &vop_access_desc, procfs_access },		/* access */
256 	{ &vop_getattr_desc, procfs_getattr },		/* getattr */
257 	{ &vop_setattr_desc, procfs_setattr },		/* setattr */
258 	{ &vop_read_desc, procfs_read },		/* read */
259 	{ &vop_write_desc, procfs_write },		/* write */
260 	{ &vop_fcntl_desc, procfs_fcntl },		/* fcntl */
261 	{ &vop_ioctl_desc, procfs_ioctl },		/* ioctl */
262 	{ &vop_poll_desc, procfs_poll },		/* poll */
263 	{ &vop_revoke_desc, procfs_revoke },		/* revoke */
264 	{ &vop_fsync_desc, procfs_fsync },		/* fsync */
265 	{ &vop_seek_desc, procfs_seek },		/* seek */
266 	{ &vop_remove_desc, procfs_remove },		/* remove */
267 	{ &vop_link_desc, procfs_link },		/* link */
268 	{ &vop_rename_desc, procfs_rename },		/* rename */
269 	{ &vop_mkdir_desc, procfs_mkdir },		/* mkdir */
270 	{ &vop_rmdir_desc, procfs_rmdir },		/* rmdir */
271 	{ &vop_symlink_desc, procfs_symlink },		/* symlink */
272 	{ &vop_readdir_desc, procfs_readdir },		/* readdir */
273 	{ &vop_readlink_desc, procfs_readlink },	/* readlink */
274 	{ &vop_abortop_desc, procfs_abortop },		/* abortop */
275 	{ &vop_inactive_desc, procfs_inactive },	/* inactive */
276 	{ &vop_reclaim_desc, procfs_reclaim },		/* reclaim */
277 	{ &vop_lock_desc, procfs_lock },		/* lock */
278 	{ &vop_unlock_desc, procfs_unlock },		/* unlock */
279 	{ &vop_bmap_desc, procfs_bmap },		/* bmap */
280 	{ &vop_strategy_desc, procfs_strategy },	/* strategy */
281 	{ &vop_print_desc, procfs_print },		/* print */
282 	{ &vop_islocked_desc, procfs_islocked },	/* islocked */
283 	{ &vop_pathconf_desc, procfs_pathconf },	/* pathconf */
284 	{ &vop_advlock_desc, procfs_advlock },		/* advlock */
285 	{ &vop_putpages_desc, procfs_putpages },	/* putpages */
286 	{ NULL, NULL }
287 };
288 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
289 	{ &procfs_vnodeop_p, procfs_vnodeop_entries };
290 /*
291  * set things up for doing i/o on
292  * the pfsnode (vp).  (vp) is locked
293  * on entry, and should be left locked
294  * on exit.
295  *
296  * for procfs we don't need to do anything
297  * in particular for i/o.  all that is done
298  * is to support exclusive open on process
299  * memory images.
300  */
301 int
302 procfs_open(v)
303 	void *v;
304 {
305 	struct vop_open_args /* {
306 		struct vnode *a_vp;
307 		int  a_mode;
308 		kauth_cred_t a_cred;
309 	} */ *ap = v;
310 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
311 	struct lwp *l1;
312 	struct proc *p2;
313 	int error;
314 
315 	if ((error = procfs_proc_lock(pfs->pfs_pid, &p2, ENOENT)) != 0)
316 		return error;
317 
318 	l1 = curlwp;				/* tracer */
319 
320 #define	M2K(m)	(((m) & FREAD) && ((m) & FWRITE) ? \
321 		 KAUTH_REQ_PROCESS_PROCFS_RW : \
322 		 (m) & FWRITE ? KAUTH_REQ_PROCESS_PROCFS_WRITE : \
323 		 KAUTH_REQ_PROCESS_PROCFS_READ)
324 
325 	mutex_enter(p2->p_lock);
326 	error = kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_PROCFS,
327 	    p2, pfs, KAUTH_ARG(M2K(ap->a_mode)), NULL);
328 	mutex_exit(p2->p_lock);
329 	if (error) {
330 		procfs_proc_unlock(p2);
331 		return (error);
332 	}
333 
334 #undef M2K
335 
336 	switch (pfs->pfs_type) {
337 	case PFSmem:
338 		if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
339 		    ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) {
340 			error = EBUSY;
341 			break;
342 		}
343 
344 		if (!proc_isunder(p2, l1)) {
345 			error = EPERM;
346 			break;
347 		}
348 
349 		if (ap->a_mode & FWRITE)
350 			pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
351 
352 		break;
353 
354 	case PFSregs:
355 	case PFSfpregs:
356 		if (!proc_isunder(p2, l1)) {
357 			error = EPERM;
358 			break;
359 		}
360 		break;
361 
362 	default:
363 		break;
364 	}
365 
366 	procfs_proc_unlock(p2);
367 	return (error);
368 }
369 
370 /*
371  * close the pfsnode (vp) after doing i/o.
372  * (vp) is not locked on entry or exit.
373  *
374  * nothing to do for procfs other than undo
375  * any exclusive open flag (see _open above).
376  */
377 int
378 procfs_close(v)
379 	void *v;
380 {
381 	struct vop_close_args /* {
382 		struct vnode *a_vp;
383 		int  a_fflag;
384 		kauth_cred_t a_cred;
385 	} */ *ap = v;
386 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
387 
388 	switch (pfs->pfs_type) {
389 	case PFSmem:
390 		if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
391 			pfs->pfs_flags &= ~(FWRITE|O_EXCL);
392 		break;
393 
394 	default:
395 		break;
396 	}
397 
398 	return (0);
399 }
400 
401 /*
402  * _inactive is called when the pfsnode
403  * is vrele'd and the reference count goes
404  * to zero.  (vp) will be on the vnode free
405  * list, so to get it back vget() must be
406  * used.
407  *
408  * (vp) is locked on entry, but must be unlocked on exit.
409  */
410 int
411 procfs_inactive(v)
412 	void *v;
413 {
414 	struct vop_inactive_args /* {
415 		struct vnode *a_vp;
416 		bool *a_recycle;
417 	} */ *ap = v;
418 	struct vnode *vp = ap->a_vp;
419 	struct pfsnode *pfs = VTOPFS(vp);
420 
421 	mutex_enter(proc_lock);
422 	*ap->a_recycle = (p_find(pfs->pfs_pid, PFIND_LOCKED) == NULL);
423 	mutex_exit(proc_lock);
424 
425 	VOP_UNLOCK(vp, 0);
426 
427 	return (0);
428 }
429 
430 /*
431  * _reclaim is called when getnewvnode()
432  * wants to make use of an entry on the vnode
433  * free list.  at this time the filesystem needs
434  * to free any private data and remove the node
435  * from any private lists.
436  */
437 int
438 procfs_reclaim(v)
439 	void *v;
440 {
441 	struct vop_reclaim_args /* {
442 		struct vnode *a_vp;
443 	} */ *ap = v;
444 
445 	return (procfs_freevp(ap->a_vp));
446 }
447 
448 /*
449  * Return POSIX pathconf information applicable to special devices.
450  */
451 int
452 procfs_pathconf(v)
453 	void *v;
454 {
455 	struct vop_pathconf_args /* {
456 		struct vnode *a_vp;
457 		int a_name;
458 		register_t *a_retval;
459 	} */ *ap = v;
460 
461 	switch (ap->a_name) {
462 	case _PC_LINK_MAX:
463 		*ap->a_retval = LINK_MAX;
464 		return (0);
465 	case _PC_MAX_CANON:
466 		*ap->a_retval = MAX_CANON;
467 		return (0);
468 	case _PC_MAX_INPUT:
469 		*ap->a_retval = MAX_INPUT;
470 		return (0);
471 	case _PC_PIPE_BUF:
472 		*ap->a_retval = PIPE_BUF;
473 		return (0);
474 	case _PC_CHOWN_RESTRICTED:
475 		*ap->a_retval = 1;
476 		return (0);
477 	case _PC_VDISABLE:
478 		*ap->a_retval = _POSIX_VDISABLE;
479 		return (0);
480 	case _PC_SYNC_IO:
481 		*ap->a_retval = 1;
482 		return (0);
483 	default:
484 		return (EINVAL);
485 	}
486 	/* NOTREACHED */
487 }
488 
489 /*
490  * _print is used for debugging.
491  * just print a readable description
492  * of (vp).
493  */
494 int
495 procfs_print(v)
496 	void *v;
497 {
498 	struct vop_print_args /* {
499 		struct vnode *a_vp;
500 	} */ *ap = v;
501 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
502 
503 	printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
504 	    pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
505 	return 0;
506 }
507 
508 int
509 procfs_link(v)
510 	void *v;
511 {
512 	struct vop_link_args /* {
513 		struct vnode *a_dvp;
514 		struct vnode *a_vp;
515 		struct componentname *a_cnp;
516 	} */ *ap = v;
517 
518 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
519 	vput(ap->a_dvp);
520 	return (EROFS);
521 }
522 
523 int
524 procfs_symlink(v)
525 	void *v;
526 {
527 	struct vop_symlink_args /* {
528 		struct vnode *a_dvp;
529 		struct vnode **a_vpp;
530 		struct componentname *a_cnp;
531 		struct vattr *a_vap;
532 		char *a_target;
533 	} */ *ap = v;
534 
535 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
536 	vput(ap->a_dvp);
537 	return (EROFS);
538 }
539 
540 /*
541  * Works out the path to (and vnode of) the target process's current
542  * working directory or chroot.  If the caller is in a chroot and
543  * can't "reach" the target's cwd or root (or some other error
544  * occurs), a "/" is returned for the path and a NULL pointer is
545  * returned for the vnode.
546  */
547 static void
548 procfs_dir(pfstype t, struct lwp *caller, struct proc *target, char **bpp,
549     char *path, size_t len)
550 {
551 	struct cwdinfo *cwdi;
552 	struct vnode *vp, *rvp;
553 	char *bp;
554 
555 	cwdi = caller->l_proc->p_cwdi;
556 	rw_enter(&cwdi->cwdi_lock, RW_READER);
557 
558 	rvp = cwdi->cwdi_rdir;
559 	bp = bpp ? *bpp : NULL;
560 
561 	switch (t) {
562 	case PFScwd:
563 		vp = target->p_cwdi->cwdi_cdir;
564 		break;
565 	case PFSchroot:
566 		vp = target->p_cwdi->cwdi_rdir;
567 		break;
568 	case PFSexe:
569 		vp = target->p_textvp;
570 		break;
571 	default:
572 		rw_exit(&cwdi->cwdi_lock);
573 		return;
574 	}
575 
576 	/*
577 	 * XXX: this horrible kludge avoids locking panics when
578 	 * attempting to lookup links that point to within procfs
579 	 */
580 	if (vp != NULL && vp->v_tag == VT_PROCFS) {
581 		if (bpp) {
582 			*--bp = '/';
583 			*bpp = bp;
584 		}
585 		rw_exit(&cwdi->cwdi_lock);
586 		return;
587 	}
588 
589 	if (rvp == NULL)
590 		rvp = rootvnode;
591 	if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
592 	    len / 2, 0, caller) != 0) {
593 		vp = NULL;
594 		if (bpp) {
595 /*
596 			if (t == PFSexe) {
597 				snprintf(path, len, "%s/%d/file"
598 				    mp->mnt_stat.f_mntonname, pfs->pfs_pid);
599 			} else */ {
600 				bp = *bpp;
601 				*--bp = '/';
602 			}
603 		}
604 	}
605 
606 	if (bpp)
607 		*bpp = bp;
608 
609 	rw_exit(&cwdi->cwdi_lock);
610 }
611 
612 /*
613  * Invent attributes for pfsnode (vp) and store
614  * them in (vap).
615  * Directories lengths are returned as zero since
616  * any real length would require the genuine size
617  * to be computed, and nothing cares anyway.
618  *
619  * this is relatively minimal for procfs.
620  */
621 int
622 procfs_getattr(v)
623 	void *v;
624 {
625 	struct vop_getattr_args /* {
626 		struct vnode *a_vp;
627 		struct vattr *a_vap;
628 		kauth_cred_t a_cred;
629 	} */ *ap = v;
630 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
631 	struct vattr *vap = ap->a_vap;
632 	struct proc *procp;
633 	char *path;
634 	int error;
635 
636 	/* first check the process still exists */
637 	switch (pfs->pfs_type) {
638 	case PFSroot:
639 	case PFScurproc:
640 	case PFSself:
641 		procp = NULL;
642 		break;
643 
644 	default:
645 		error = procfs_proc_lock(pfs->pfs_pid, &procp, ENOENT);
646 		if (error != 0)
647 			return (error);
648 		break;
649 	}
650 
651 	switch (pfs->pfs_type) {
652 	case PFScwd:
653 	case PFSchroot:
654 	case PFSexe:
655 		MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
656 		    M_WAITOK|M_CANFAIL);
657 		if (path == NULL && procp != NULL) {
658 			procfs_proc_unlock(procp);
659 			return (ENOMEM);
660 		}
661 		break;
662 
663 	default:
664 		path = NULL;
665 		break;
666 	}
667 
668 	if (procp != NULL) {
669 		mutex_enter(procp->p_lock);
670 		error = kauth_authorize_process(kauth_cred_get(),
671 		    KAUTH_PROCESS_CANSEE, procp,
672 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
673 		mutex_exit(procp->p_lock);
674 		if (error != 0) {
675 		    	procfs_proc_unlock(procp);
676 		    	if (path != NULL)
677 		    		free(path, M_TEMP);
678 			return (ENOENT);
679 		}
680 	}
681 
682 	error = 0;
683 
684 	/* start by zeroing out the attributes */
685 	VATTR_NULL(vap);
686 
687 	/* next do all the common fields */
688 	vap->va_type = ap->a_vp->v_type;
689 	vap->va_mode = pfs->pfs_mode;
690 	vap->va_fileid = pfs->pfs_fileno;
691 	vap->va_flags = 0;
692 	vap->va_blocksize = PAGE_SIZE;
693 
694 	/*
695 	 * Make all times be current TOD.
696 	 *
697 	 * It would be possible to get the process start
698 	 * time from the p_stats structure, but there's
699 	 * no "file creation" time stamp anyway, and the
700 	 * p_stats structure is not addressable if u. gets
701 	 * swapped out for that process.
702 	 */
703 	getnanotime(&vap->va_ctime);
704 	vap->va_atime = vap->va_mtime = vap->va_ctime;
705 	if (procp)
706 		TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
707 		    &vap->va_birthtime);
708 	else
709 		getnanotime(&vap->va_birthtime);
710 
711 	switch (pfs->pfs_type) {
712 	case PFSmem:
713 	case PFSregs:
714 	case PFSfpregs:
715 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
716 	PROCFS_MACHDEP_PROTECT_CASES
717 #endif
718 		/*
719 		 * If the process has exercised some setuid or setgid
720 		 * privilege, then rip away read/write permission so
721 		 * that only root can gain access.
722 		 */
723 		if (procp->p_flag & PK_SUGID)
724 			vap->va_mode &= ~(S_IRUSR|S_IWUSR);
725 		/* FALLTHROUGH */
726 	case PFSctl:
727 	case PFSstatus:
728 	case PFSstat:
729 	case PFSnote:
730 	case PFSnotepg:
731 	case PFSmap:
732 	case PFSmaps:
733 	case PFScmdline:
734 	case PFSemul:
735 	case PFSstatm:
736 		vap->va_nlink = 1;
737 		vap->va_uid = kauth_cred_geteuid(procp->p_cred);
738 		vap->va_gid = kauth_cred_getegid(procp->p_cred);
739 		break;
740 	case PFSmeminfo:
741 	case PFSdevices:
742 	case PFScpuinfo:
743 	case PFSuptime:
744 	case PFSmounts:
745 	case PFScpustat:
746 	case PFSloadavg:
747 		vap->va_nlink = 1;
748 		vap->va_uid = vap->va_gid = 0;
749 		break;
750 
751 	default:
752 		break;
753 	}
754 
755 	/*
756 	 * now do the object specific fields
757 	 *
758 	 * The size could be set from struct reg, but it's hardly
759 	 * worth the trouble, and it puts some (potentially) machine
760 	 * dependent data into this machine-independent code.  If it
761 	 * becomes important then this function should break out into
762 	 * a per-file stat function in the corresponding .c file.
763 	 */
764 
765 	switch (pfs->pfs_type) {
766 	case PFSroot:
767 		/*
768 		 * Set nlink to 1 to tell fts(3) we don't actually know.
769 		 */
770 		vap->va_nlink = 1;
771 		vap->va_uid = 0;
772 		vap->va_gid = 0;
773 		vap->va_bytes = vap->va_size = DEV_BSIZE;
774 		break;
775 
776 	case PFSself:
777 	case PFScurproc: {
778 		char bf[16];		/* should be enough */
779 		vap->va_nlink = 1;
780 		vap->va_uid = 0;
781 		vap->va_gid = 0;
782 		vap->va_bytes = vap->va_size =
783 		    snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
784 		break;
785 	}
786 
787 	case PFSfd:
788 		if (pfs->pfs_fd != -1) {
789 			file_t *fp;
790 
791 			fp = fd_getfile2(procp, pfs->pfs_fd);
792 			if (fp == NULL) {
793 				error = EBADF;
794 				break;
795 			}
796 			vap->va_nlink = 1;
797 			vap->va_uid = kauth_cred_geteuid(fp->f_cred);
798 			vap->va_gid = kauth_cred_getegid(fp->f_cred);
799 			switch (fp->f_type) {
800 			case DTYPE_VNODE:
801 				vap->va_bytes = vap->va_size =
802 				    ((struct vnode *)fp->f_data)->v_size;
803 				break;
804 			default:
805 				vap->va_bytes = vap->va_size = 0;
806 				break;
807 			}
808 			closef(fp);
809 			break;
810 		}
811 		/*FALLTHROUGH*/
812 	case PFSproc:
813 		vap->va_nlink = 2;
814 		vap->va_uid = kauth_cred_geteuid(procp->p_cred);
815 		vap->va_gid = kauth_cred_getegid(procp->p_cred);
816 		vap->va_bytes = vap->va_size = DEV_BSIZE;
817 		break;
818 
819 	case PFSfile:
820 		error = EOPNOTSUPP;
821 		break;
822 
823 	case PFSmem:
824 		vap->va_bytes = vap->va_size =
825 			ctob(procp->p_vmspace->vm_tsize +
826 				    procp->p_vmspace->vm_dsize +
827 				    procp->p_vmspace->vm_ssize);
828 		break;
829 
830 #if defined(PT_GETREGS) || defined(PT_SETREGS)
831 	case PFSregs:
832 		vap->va_bytes = vap->va_size = sizeof(struct reg);
833 		break;
834 #endif
835 
836 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
837 	case PFSfpregs:
838 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
839 		break;
840 #endif
841 
842 	case PFSctl:
843 	case PFSstatus:
844 	case PFSstat:
845 	case PFSnote:
846 	case PFSnotepg:
847 	case PFScmdline:
848 	case PFSmeminfo:
849 	case PFSdevices:
850 	case PFScpuinfo:
851 	case PFSuptime:
852 	case PFSmounts:
853 	case PFScpustat:
854 	case PFSloadavg:
855 	case PFSstatm:
856 		vap->va_bytes = vap->va_size = 0;
857 		break;
858 	case PFSmap:
859 	case PFSmaps:
860 		/*
861 		 * Advise a larger blocksize for the map files, so that
862 		 * they may be read in one pass.
863 		 */
864 		vap->va_blocksize = 4 * PAGE_SIZE;
865 		vap->va_bytes = vap->va_size = 0;
866 		break;
867 
868 	case PFScwd:
869 	case PFSchroot:
870 	case PFSexe: {
871 		char *bp;
872 
873 		vap->va_nlink = 1;
874 		vap->va_uid = 0;
875 		vap->va_gid = 0;
876 		bp = path + MAXPATHLEN;
877 		*--bp = '\0';
878 		procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
879 		     MAXPATHLEN);
880 		vap->va_bytes = vap->va_size = strlen(bp);
881 		break;
882 	}
883 
884 	case PFSemul:
885 		vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
886 		break;
887 
888 #ifdef __HAVE_PROCFS_MACHDEP
889 	PROCFS_MACHDEP_NODETYPE_CASES
890 		error = procfs_machdep_getattr(ap->a_vp, vap, procp);
891 		break;
892 #endif
893 
894 	default:
895 		panic("procfs_getattr");
896 	}
897 
898 	if (procp != NULL)
899 		procfs_proc_unlock(procp);
900 	if (path != NULL)
901 		free(path, M_TEMP);
902 
903 	return (error);
904 }
905 
906 /*ARGSUSED*/
907 int
908 procfs_setattr(void *v)
909 {
910 	/*
911 	 * just fake out attribute setting
912 	 * it's not good to generate an error
913 	 * return, otherwise things like creat()
914 	 * will fail when they try to set the
915 	 * file length to 0.  worse, this means
916 	 * that echo $note > /proc/$pid/note will fail.
917 	 */
918 
919 	return (0);
920 }
921 
922 /*
923  * implement access checking.
924  *
925  * actually, the check for super-user is slightly
926  * broken since it will allow read access to write-only
927  * objects.  this doesn't cause any particular trouble
928  * but does mean that the i/o entry points need to check
929  * that the operation really does make sense.
930  */
931 int
932 procfs_access(v)
933 	void *v;
934 {
935 	struct vop_access_args /* {
936 		struct vnode *a_vp;
937 		int a_mode;
938 		kauth_cred_t a_cred;
939 	} */ *ap = v;
940 	struct vattr va;
941 	int error;
942 
943 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
944 		return (error);
945 
946 	return (vaccess(va.va_type, va.va_mode,
947 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
948 }
949 
950 /*
951  * lookup.  this is incredibly complicated in the
952  * general case, however for most pseudo-filesystems
953  * very little needs to be done.
954  *
955  * Locking isn't hard here, just poorly documented.
956  *
957  * If we're looking up ".", just vref the parent & return it.
958  *
959  * If we're looking up "..", unlock the parent, and lock "..". If everything
960  * went ok, and we're on the last component and the caller requested the
961  * parent locked, try to re-lock the parent. We do this to prevent lock
962  * races.
963  *
964  * For anything else, get the needed node. Then unlock the parent if not
965  * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
966  * parent in the .. case).
967  *
968  * We try to exit with the parent locked in error cases.
969  */
970 int
971 procfs_lookup(v)
972 	void *v;
973 {
974 	struct vop_lookup_args /* {
975 		struct vnode * a_dvp;
976 		struct vnode ** a_vpp;
977 		struct componentname * a_cnp;
978 	} */ *ap = v;
979 	struct componentname *cnp = ap->a_cnp;
980 	struct vnode **vpp = ap->a_vpp;
981 	struct vnode *dvp = ap->a_dvp;
982 	const char *pname = cnp->cn_nameptr;
983 	const struct proc_target *pt = NULL;
984 	struct vnode *fvp;
985 	pid_t pid, vnpid;
986 	struct pfsnode *pfs;
987 	struct proc *p = NULL;
988 	int i, error;
989 	pfstype type;
990 
991 	*vpp = NULL;
992 
993 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
994 		return (EROFS);
995 
996 	if (cnp->cn_namelen == 1 && *pname == '.') {
997 		*vpp = dvp;
998 		VREF(dvp);
999 		return (0);
1000 	}
1001 
1002 	pfs = VTOPFS(dvp);
1003 	switch (pfs->pfs_type) {
1004 	case PFSroot:
1005 		/*
1006 		 * Shouldn't get here with .. in the root node.
1007 		 */
1008 		if (cnp->cn_flags & ISDOTDOT)
1009 			return (EIO);
1010 
1011 		for (i = 0; i < nproc_root_targets; i++) {
1012 			pt = &proc_root_targets[i];
1013 			/*
1014 			 * check for node match.  proc is always NULL here,
1015 			 * so call pt_valid with constant NULL lwp.
1016 			 */
1017 			if (cnp->cn_namelen == pt->pt_namlen &&
1018 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1019 			    (pt->pt_valid == NULL ||
1020 			     (*pt->pt_valid)(NULL, dvp->v_mount)))
1021 				break;
1022 		}
1023 
1024 		if (i != nproc_root_targets) {
1025 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
1026 			    pt->pt_pfstype, -1, NULL);
1027 			return (error);
1028 		}
1029 
1030 		if (CNEQ(cnp, "curproc", 7)) {
1031 			pid = curproc->p_pid;
1032 			vnpid = 0;
1033 			type = PFScurproc;
1034 		} else if (CNEQ(cnp, "self", 4)) {
1035 			pid = curproc->p_pid;
1036 			vnpid = 0;
1037 			type = PFSself;
1038 		} else {
1039 			pid = (pid_t)atoi(pname, cnp->cn_namelen);
1040 			vnpid = pid;
1041 			type = PFSproc;
1042 		}
1043 
1044 		if (procfs_proc_lock(pid, &p, ESRCH) != 0)
1045 			break;
1046 		error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1, p);
1047 		procfs_proc_unlock(p);
1048 		return (error);
1049 
1050 	case PFSproc:
1051 		/*
1052 		 * do the .. dance. We unlock the directory, and then
1053 		 * get the root dir. That will automatically return ..
1054 		 * locked. Then if the caller wanted dvp locked, we
1055 		 * re-lock.
1056 		 */
1057 		if (cnp->cn_flags & ISDOTDOT) {
1058 			VOP_UNLOCK(dvp, 0);
1059 			error = procfs_root(dvp->v_mount, vpp);
1060 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1061 			return (error);
1062 		}
1063 
1064 		if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
1065 			break;
1066 
1067 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
1068 			struct lwp *plwp;
1069 			int found;
1070 
1071 			mutex_enter(p->p_lock);
1072 			plwp = proc_representative_lwp(p, NULL, 1);
1073 			lwp_addref(plwp);
1074 			mutex_exit(p->p_lock);
1075 			found = cnp->cn_namelen == pt->pt_namlen &&
1076 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1077 			    (pt->pt_valid == NULL
1078 			      || (*pt->pt_valid)(plwp, dvp->v_mount));
1079 			lwp_delref(plwp);
1080 			if (found)
1081 				break;
1082 		}
1083 		if (i == nproc_targets) {
1084 			procfs_proc_unlock(p);
1085 			break;
1086 		}
1087 		if (pt->pt_pfstype == PFSfile) {
1088 			fvp = p->p_textvp;
1089 			/* We already checked that it exists. */
1090 			VREF(fvp);
1091 			procfs_proc_unlock(p);
1092 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
1093 			*vpp = fvp;
1094 			return (0);
1095 		}
1096 
1097 		error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1098 		    pt->pt_pfstype, -1, p);
1099 		procfs_proc_unlock(p);
1100 		return (error);
1101 
1102 	case PFSfd: {
1103 		int fd;
1104 		file_t *fp;
1105 
1106 		if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0)
1107 			return error;
1108 
1109 		/*
1110 		 * do the .. dance. We unlock the directory, and then
1111 		 * get the proc dir. That will automatically return ..
1112 		 * locked. Then re-lock the directory.
1113 		 */
1114 		if (cnp->cn_flags & ISDOTDOT) {
1115 			VOP_UNLOCK(dvp, 0);
1116 			error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1117 			    PFSproc, -1, p);
1118 			procfs_proc_unlock(p);
1119 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1120 			return (error);
1121 		}
1122 		fd = atoi(pname, cnp->cn_namelen);
1123 
1124 		fp = fd_getfile2(p, fd);
1125 		if (fp == NULL) {
1126 			procfs_proc_unlock(p);
1127 			return ENOENT;
1128 		}
1129 		fvp = fp->f_data;
1130 
1131 		/* Don't show directories */
1132 		if (fp->f_type == DTYPE_VNODE && fvp->v_type != VDIR) {
1133 			VREF(fvp);
1134 			closef(fp);
1135 			procfs_proc_unlock(p);
1136 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
1137 			    (p == curproc ? LK_CANRECURSE : 0));
1138 			*vpp = fvp;
1139 			return 0;
1140 		}
1141 
1142 		closef(fp);
1143 		error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1144 		    PFSfd, fd, p);
1145 		procfs_proc_unlock(p);
1146 		return error;
1147 	}
1148 	default:
1149 		return (ENOTDIR);
1150 	}
1151 
1152 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
1153 }
1154 
1155 int
1156 procfs_validfile(struct lwp *l, struct mount *mp)
1157 {
1158 	return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL;
1159 }
1160 
1161 static int
1162 procfs_validfile_linux(l, mp)
1163 	struct lwp *l;
1164 	struct mount *mp;
1165 {
1166 	int flags;
1167 
1168 	flags = VFSTOPROC(mp)->pmnt_flags;
1169 	return (flags & PROCFSMNT_LINUXCOMPAT) &&
1170 	    (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
1171 }
1172 
1173 struct procfs_root_readdir_ctx {
1174 	struct uio *uiop;
1175 	off_t *cookies;
1176 	int ncookies;
1177 	off_t off;
1178 	off_t startoff;
1179 	int error;
1180 };
1181 
1182 static int
1183 procfs_root_readdir_callback(struct proc *p, void *arg)
1184 {
1185 	struct procfs_root_readdir_ctx *ctxp = arg;
1186 	struct dirent d;
1187 	struct uio *uiop;
1188 	int error;
1189 
1190 	uiop = ctxp->uiop;
1191 	if (uiop->uio_resid < UIO_MX)
1192 		return -1; /* no space */
1193 
1194 	if (ctxp->off < ctxp->startoff) {
1195 		ctxp->off++;
1196 		return 0;
1197 	}
1198 
1199 	if (kauth_authorize_process(kauth_cred_get(),
1200 	    KAUTH_PROCESS_CANSEE, p,
1201 	    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL) != 0)
1202 		return 0;
1203 
1204 	memset(&d, 0, UIO_MX);
1205 	d.d_reclen = UIO_MX;
1206 	d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
1207 	d.d_namlen = snprintf(d.d_name,
1208 	    UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
1209 	d.d_type = DT_DIR;
1210 
1211 	mutex_exit(proc_lock);
1212 	error = uiomove(&d, UIO_MX, uiop);
1213 	mutex_enter(proc_lock);
1214 	if (error) {
1215 		ctxp->error = error;
1216 		return -1;
1217 	}
1218 
1219 	ctxp->ncookies++;
1220 	if (ctxp->cookies)
1221 		*(ctxp->cookies)++ = ctxp->off + 1;
1222 	ctxp->off++;
1223 
1224 	return 0;
1225 }
1226 
1227 /*
1228  * readdir returns directory entries from pfsnode (vp).
1229  *
1230  * the strategy here with procfs is to generate a single
1231  * directory entry at a time (struct dirent) and then
1232  * copy that out to userland using uiomove.  a more efficent
1233  * though more complex implementation, would try to minimize
1234  * the number of calls to uiomove().  for procfs, this is
1235  * hardly worth the added code complexity.
1236  *
1237  * this should just be done through read()
1238  */
1239 int
1240 procfs_readdir(v)
1241 	void *v;
1242 {
1243 	struct vop_readdir_args /* {
1244 		struct vnode *a_vp;
1245 		struct uio *a_uio;
1246 		kauth_cred_t a_cred;
1247 		int *a_eofflag;
1248 		off_t **a_cookies;
1249 		int *a_ncookies;
1250 	} */ *ap = v;
1251 	struct uio *uio = ap->a_uio;
1252 	struct dirent d;
1253 	struct pfsnode *pfs;
1254 	off_t i;
1255 	int error;
1256 	off_t *cookies = NULL;
1257 	int ncookies;
1258 	struct vnode *vp;
1259 	const struct proc_target *pt;
1260 	struct procfs_root_readdir_ctx ctx;
1261 	struct lwp *l;
1262 	int nfd;
1263 
1264 	vp = ap->a_vp;
1265 	pfs = VTOPFS(vp);
1266 
1267 	if (uio->uio_resid < UIO_MX)
1268 		return (EINVAL);
1269 	if (uio->uio_offset < 0)
1270 		return (EINVAL);
1271 
1272 	error = 0;
1273 	i = uio->uio_offset;
1274 	memset(&d, 0, UIO_MX);
1275 	d.d_reclen = UIO_MX;
1276 	ncookies = uio->uio_resid / UIO_MX;
1277 
1278 	switch (pfs->pfs_type) {
1279 	/*
1280 	 * this is for the process-specific sub-directories.
1281 	 * all that is needed to is copy out all the entries
1282 	 * from the procent[] table (top of this file).
1283 	 */
1284 	case PFSproc: {
1285 		struct proc *p;
1286 
1287 		if (i >= nproc_targets)
1288 			return 0;
1289 
1290 		if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
1291 			break;
1292 
1293 		if (ap->a_ncookies) {
1294 			ncookies = min(ncookies, (nproc_targets - i));
1295 			cookies = malloc(ncookies * sizeof (off_t),
1296 			    M_TEMP, M_WAITOK);
1297 			*ap->a_cookies = cookies;
1298 		}
1299 
1300 		for (pt = &proc_targets[i];
1301 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1302 			if (pt->pt_valid) {
1303 				/* XXXSMP LWP can disappear */
1304 				mutex_enter(p->p_lock);
1305 				l = proc_representative_lwp(p, NULL, 1);
1306 				mutex_exit(p->p_lock);
1307 				if ((*pt->pt_valid)(l, vp->v_mount) == 0)
1308 					continue;
1309 			}
1310 
1311 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1312 			    pt->pt_pfstype, -1);
1313 			d.d_namlen = pt->pt_namlen;
1314 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1315 			d.d_type = pt->pt_type;
1316 
1317 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1318 				break;
1319 			if (cookies)
1320 				*cookies++ = i + 1;
1321 		}
1322 
1323 		procfs_proc_unlock(p);
1324 	    	break;
1325 	}
1326 	case PFSfd: {
1327 		struct proc *p;
1328 		file_t *fp;
1329 		int lim, nc = 0;
1330 
1331 		if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
1332 			return error;
1333 
1334 		/* XXX Should this be by file as well? */
1335 		if (kauth_authorize_process(kauth_cred_get(),
1336 		    KAUTH_PROCESS_CANSEE, p,
1337 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES), NULL,
1338 		    NULL) != 0) {
1339 		    	procfs_proc_unlock(p);
1340 			return ESRCH;
1341 		}
1342 
1343 		nfd = p->p_fd->fd_nfiles;
1344 
1345 		lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1346 		if (i >= lim) {
1347 		    	procfs_proc_unlock(p);
1348 			return 0;
1349 		}
1350 
1351 		if (ap->a_ncookies) {
1352 			ncookies = min(ncookies, (nfd + 2 - i));
1353 			cookies = malloc(ncookies * sizeof (off_t),
1354 			    M_TEMP, M_WAITOK);
1355 			*ap->a_cookies = cookies;
1356 		}
1357 
1358 		for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1359 			pt = &proc_targets[i];
1360 			d.d_namlen = pt->pt_namlen;
1361 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1362 			    pt->pt_pfstype, -1);
1363 			(void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1364 			d.d_type = pt->pt_type;
1365 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1366 				break;
1367 			if (cookies)
1368 				*cookies++ = i + 1;
1369 			nc++;
1370 		}
1371 		if (error) {
1372 			ncookies = nc;
1373 			break;
1374 		}
1375 		for (; uio->uio_resid >= UIO_MX && i < nfd; i++) {
1376 			/* check the descriptor exists */
1377 			if ((fp = fd_getfile2(p, i - 2)) == NULL)
1378 				continue;
1379 			closef(fp);
1380 
1381 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
1382 			d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1383 			    "%lld", (long long)(i - 2));
1384 			d.d_type = VREG;
1385 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1386 				break;
1387 			if (cookies)
1388 				*cookies++ = i + 1;
1389 			nc++;
1390 		}
1391 		ncookies = nc;
1392 		procfs_proc_unlock(p);
1393 		break;
1394 	}
1395 
1396 	/*
1397 	 * this is for the root of the procfs filesystem
1398 	 * what is needed are special entries for "curproc"
1399 	 * and "self" followed by an entry for each process
1400 	 * on allproc.
1401 	 */
1402 
1403 	case PFSroot: {
1404 		int nc = 0;
1405 
1406 		if (ap->a_ncookies) {
1407 			/*
1408 			 * XXX Potentially allocating too much space here,
1409 			 * but I'm lazy. This loop needs some work.
1410 			 */
1411 			cookies = malloc(ncookies * sizeof (off_t),
1412 			    M_TEMP, M_WAITOK);
1413 			*ap->a_cookies = cookies;
1414 		}
1415 		error = 0;
1416 		/* 0 ... 3 are static entries. */
1417 		for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
1418 			switch (i) {
1419 			case 0:		/* `.' */
1420 			case 1:		/* `..' */
1421 				d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
1422 				d.d_namlen = i + 1;
1423 				memcpy(d.d_name, "..", d.d_namlen);
1424 				d.d_name[i + 1] = '\0';
1425 				d.d_type = DT_DIR;
1426 				break;
1427 
1428 			case 2:
1429 				d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
1430 				d.d_namlen = sizeof("curproc") - 1;
1431 				memcpy(d.d_name, "curproc", sizeof("curproc"));
1432 				d.d_type = DT_LNK;
1433 				break;
1434 
1435 			case 3:
1436 				d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
1437 				d.d_namlen = sizeof("self") - 1;
1438 				memcpy(d.d_name, "self", sizeof("self"));
1439 				d.d_type = DT_LNK;
1440 				break;
1441 			}
1442 
1443 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1444 				break;
1445 			nc++;
1446 			if (cookies)
1447 				*cookies++ = i + 1;
1448 		}
1449 		/* 4 ... are process entries. */
1450 		ctx.uiop = uio;
1451 		ctx.error = 0;
1452 		ctx.off = 4;
1453 		ctx.startoff = i;
1454 		ctx.cookies = cookies;
1455 		ctx.ncookies = nc;
1456 		proclist_foreach_call(&allproc,
1457 		    procfs_root_readdir_callback, &ctx);
1458 		cookies = ctx.cookies;
1459 		nc = ctx.ncookies;
1460 		error = ctx.error;
1461 		if (error)
1462 			break;
1463 
1464 		/* misc entries. */
1465 		if (i < ctx.off)
1466 			i = ctx.off;
1467 		if (i >= ctx.off + nproc_root_targets)
1468 			break;
1469 		for (pt = &proc_root_targets[i - ctx.off];
1470 		    uio->uio_resid >= UIO_MX &&
1471 		    pt < &proc_root_targets[nproc_root_targets];
1472 		    pt++, i++) {
1473 			if (pt->pt_valid &&
1474 			    (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1475 				continue;
1476 			d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1477 			d.d_namlen = pt->pt_namlen;
1478 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1479 			d.d_type = pt->pt_type;
1480 
1481 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1482 				break;
1483 			nc++;
1484 			if (cookies)
1485 				*cookies++ = i + 1;
1486 		}
1487 
1488 		ncookies = nc;
1489 		break;
1490 	}
1491 
1492 	default:
1493 		error = ENOTDIR;
1494 		break;
1495 	}
1496 
1497 	if (ap->a_ncookies) {
1498 		if (error) {
1499 			if (cookies)
1500 				free(*ap->a_cookies, M_TEMP);
1501 			*ap->a_ncookies = 0;
1502 			*ap->a_cookies = NULL;
1503 		} else
1504 			*ap->a_ncookies = ncookies;
1505 	}
1506 	uio->uio_offset = i;
1507 	return (error);
1508 }
1509 
1510 /*
1511  * readlink reads the link of `curproc' and others
1512  */
1513 int
1514 procfs_readlink(v)
1515 	void *v;
1516 {
1517 	struct vop_readlink_args *ap = v;
1518 	char bf[16];		/* should be enough */
1519 	char *bp = bf;
1520 	char *path = NULL;
1521 	int len = 0;
1522 	int error = 0;
1523 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
1524 	struct proc *pown;
1525 
1526 	if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
1527 		len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
1528 	else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
1529 		len = snprintf(bf, sizeof(bf), "%s", "curproc");
1530 	else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
1531 	    pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1) ||
1532 	    pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) {
1533 		if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
1534 			return error;
1535 		MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
1536 		    M_WAITOK|M_CANFAIL);
1537 		if (path == NULL) {
1538 			procfs_proc_unlock(pown);
1539 			return (ENOMEM);
1540 		}
1541 		bp = path + MAXPATHLEN;
1542 		*--bp = '\0';
1543 		procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
1544 		    &bp, path, MAXPATHLEN);
1545 		procfs_proc_unlock(pown);
1546 		len = strlen(bp);
1547 	} else {
1548 		file_t *fp;
1549 		struct vnode *vxp, *vp;
1550 
1551 		if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
1552 			return error;
1553 
1554 		fp = fd_getfile2(pown, pfs->pfs_fd);
1555 		if (fp == NULL) {
1556 			procfs_proc_unlock(pown);
1557 			return EBADF;
1558 		}
1559 
1560 		switch (fp->f_type) {
1561 		case DTYPE_VNODE:
1562 			vxp = (struct vnode *)fp->f_data;
1563 			if (vxp->v_type != VDIR) {
1564 				error = EINVAL;
1565 				break;
1566 			}
1567 			if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
1568 			    == NULL) {
1569 				error = ENOMEM;
1570 				break;
1571 			}
1572 			bp = path + MAXPATHLEN;
1573 			*--bp = '\0';
1574 
1575 			/*
1576 			 * XXX: kludge to avoid locking against ourselves
1577 			 * in getcwd()
1578 			 */
1579 			if (vxp->v_tag == VT_PROCFS) {
1580 				*--bp = '/';
1581 			} else {
1582 				rw_enter(&curproc->p_cwdi->cwdi_lock, RW_READER);
1583 				vp = curproc->p_cwdi->cwdi_rdir;
1584 				if (vp == NULL)
1585 					vp = rootvnode;
1586 				error = getcwd_common(vxp, vp, &bp, path,
1587 				    MAXPATHLEN / 2, 0, curlwp);
1588 				rw_exit(&curproc->p_cwdi->cwdi_lock);
1589 			}
1590 			if (error)
1591 				break;
1592 			len = strlen(bp);
1593 			break;
1594 
1595 		case DTYPE_MISC:
1596 			len = snprintf(bf, sizeof(bf), "%s", "[misc]");
1597 			break;
1598 
1599 		case DTYPE_KQUEUE:
1600 			len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
1601 			break;
1602 
1603 		default:
1604 			error = EINVAL;
1605 			break;
1606 		}
1607 		closef(fp);
1608 		procfs_proc_unlock(pown);
1609 	}
1610 
1611 	if (error == 0)
1612 		error = uiomove(bp, len, ap->a_uio);
1613 	if (path)
1614 		free(path, M_TEMP);
1615 	return error;
1616 }
1617 
1618 /*
1619  * convert decimal ascii to int
1620  */
1621 static int
1622 atoi(b, len)
1623 	const char *b;
1624 	size_t len;
1625 {
1626 	int p = 0;
1627 
1628 	while (len--) {
1629 		char c = *b++;
1630 		if (c < '0' || c > '9')
1631 			return -1;
1632 		p = 10 * p + (c - '0');
1633 	}
1634 
1635 	return p;
1636 }
1637