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