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