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