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