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