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