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