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