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