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