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