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