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