Home | History | Annotate | Line # | Download | only in procfs
procfs_vnops.c revision 1.150
      1 /*	$NetBSD: procfs_vnops.c,v 1.150 2007/02/18 01:55:26 pooka 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.150 2007/02/18 01:55:26 pooka 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 		rvp = rootvnode;
    579 		vp = target->p_textvp;
    580 		break;
    581 	default:
    582 		return (NULL);
    583 	}
    584 
    585 	if (rvp == NULL)
    586 		rvp = rootvnode;
    587 	mutex_exit(&target->p_mutex);	/* XXXSMP */
    588 	if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
    589 	    len / 2, 0, caller) != 0) {
    590 		vp = NULL;
    591 		if (bpp) {
    592 			bp = *bpp;
    593 			*--bp = '/';
    594 		}
    595 	}
    596 	mutex_enter(&target->p_mutex);	/* XXXSMP */
    597 
    598 	if (bpp)
    599 		*bpp = bp;
    600 
    601 	return (vp);
    602 }
    603 
    604 /*
    605  * Invent attributes for pfsnode (vp) and store
    606  * them in (vap).
    607  * Directories lengths are returned as zero since
    608  * any real length would require the genuine size
    609  * to be computed, and nothing cares anyway.
    610  *
    611  * this is relatively minimal for procfs.
    612  */
    613 int
    614 procfs_getattr(v)
    615 	void *v;
    616 {
    617 	struct vop_getattr_args /* {
    618 		struct vnode *a_vp;
    619 		struct vattr *a_vap;
    620 		kauth_cred_t a_cred;
    621 		struct lwp *a_l;
    622 	} */ *ap = v;
    623 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    624 	struct vattr *vap = ap->a_vap;
    625 	struct proc *procp;
    626 	char *path;
    627 	int error;
    628 
    629 	/* first check the process still exists */
    630 	switch (pfs->pfs_type) {
    631 	case PFSroot:
    632 	case PFScurproc:
    633 	case PFSself:
    634 		procp = NULL;
    635 		break;
    636 
    637 	default:
    638 		error = procfs_proc_lock(pfs->pfs_pid, &procp, ENOENT);
    639 		if (error != 0)
    640 			return (error);
    641 		break;
    642 	}
    643 
    644 	switch (pfs->pfs_type) {
    645 	case PFScwd:
    646 	case PFSchroot:
    647 	case PFSexe:
    648 		MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
    649 		    M_WAITOK|M_CANFAIL);
    650 		if (path == NULL && procp != NULL) {
    651 			procfs_proc_unlock(procp);
    652 			return (ENOMEM);
    653 		}
    654 		break;
    655 
    656 	default:
    657 		path = NULL;
    658 		break;
    659 	}
    660 
    661 	if (procp != NULL) {
    662 		mutex_enter(&procp->p_mutex);
    663 		error = kauth_authorize_process(kauth_cred_get(),
    664 		    KAUTH_PROCESS_CANSEE, procp, NULL, NULL, NULL);
    665 		mutex_exit(&procp->p_mutex);
    666 		if (error != 0) {
    667 		    	procfs_proc_unlock(procp);
    668 		    	if (path != NULL)
    669 		    		free(path, M_TEMP);
    670 			return (ENOENT);
    671 		}
    672 	}
    673 
    674 	error = 0;
    675 
    676 	/* start by zeroing out the attributes */
    677 	VATTR_NULL(vap);
    678 
    679 	/* next do all the common fields */
    680 	vap->va_type = ap->a_vp->v_type;
    681 	vap->va_mode = pfs->pfs_mode;
    682 	vap->va_fileid = pfs->pfs_fileno;
    683 	vap->va_flags = 0;
    684 	vap->va_blocksize = PAGE_SIZE;
    685 
    686 	/*
    687 	 * Make all times be current TOD.
    688 	 *
    689 	 * It would be possible to get the process start
    690 	 * time from the p_stats structure, but there's
    691 	 * no "file creation" time stamp anyway, and the
    692 	 * p_stats structure is not addressable if u. gets
    693 	 * swapped out for that process.
    694 	 */
    695 	getnanotime(&vap->va_ctime);
    696 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    697 	if (procp)
    698 		TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
    699 		    &vap->va_birthtime);
    700 	else
    701 		getnanotime(&vap->va_birthtime);
    702 
    703 	switch (pfs->pfs_type) {
    704 	case PFSmem:
    705 	case PFSregs:
    706 	case PFSfpregs:
    707 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    708 	PROCFS_MACHDEP_PROTECT_CASES
    709 #endif
    710 		/*
    711 		 * If the process has exercised some setuid or setgid
    712 		 * privilege, then rip away read/write permission so
    713 		 * that only root can gain access.
    714 		 */
    715 		if (procp->p_flag & PK_SUGID)
    716 			vap->va_mode &= ~(S_IRUSR|S_IWUSR);
    717 		/* FALLTHROUGH */
    718 	case PFSctl:
    719 	case PFSstatus:
    720 	case PFSstat:
    721 	case PFSnote:
    722 	case PFSnotepg:
    723 	case PFSmap:
    724 	case PFSmaps:
    725 	case PFScmdline:
    726 	case PFSemul:
    727 		vap->va_nlink = 1;
    728 		vap->va_uid = kauth_cred_geteuid(procp->p_cred);
    729 		vap->va_gid = kauth_cred_getegid(procp->p_cred);
    730 		break;
    731 	case PFSmeminfo:
    732 	case PFSdevices:
    733 	case PFScpuinfo:
    734 	case PFSuptime:
    735 	case PFSmounts:
    736 		vap->va_nlink = 1;
    737 		vap->va_uid = vap->va_gid = 0;
    738 		break;
    739 
    740 	default:
    741 		break;
    742 	}
    743 
    744 	/*
    745 	 * now do the object specific fields
    746 	 *
    747 	 * The size could be set from struct reg, but it's hardly
    748 	 * worth the trouble, and it puts some (potentially) machine
    749 	 * dependent data into this machine-independent code.  If it
    750 	 * becomes important then this function should break out into
    751 	 * a per-file stat function in the corresponding .c file.
    752 	 */
    753 
    754 	switch (pfs->pfs_type) {
    755 	case PFSroot:
    756 		/*
    757 		 * Set nlink to 1 to tell fts(3) we don't actually know.
    758 		 */
    759 		vap->va_nlink = 1;
    760 		vap->va_uid = 0;
    761 		vap->va_gid = 0;
    762 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    763 		break;
    764 
    765 	case PFSself:
    766 	case PFScurproc: {
    767 		char bf[16];		/* should be enough */
    768 		vap->va_nlink = 1;
    769 		vap->va_uid = 0;
    770 		vap->va_gid = 0;
    771 		vap->va_bytes = vap->va_size =
    772 		    snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
    773 		break;
    774 	}
    775 
    776 	case PFSfd:
    777 		if (pfs->pfs_fd != -1) {
    778 			struct file *fp;
    779 
    780 			fp = fd_getfile(procp->p_fd, pfs->pfs_fd);
    781 			if (fp == NULL) {
    782 				error = EBADF;
    783 				break;
    784 			}
    785 			FILE_USE(fp);
    786 			vap->va_nlink = 1;
    787 			vap->va_uid = kauth_cred_geteuid(fp->f_cred);
    788 			vap->va_gid = kauth_cred_getegid(fp->f_cred);
    789 			switch (fp->f_type) {
    790 			case DTYPE_VNODE:
    791 				vap->va_bytes = vap->va_size =
    792 				    ((struct vnode *)fp->f_data)->v_size;
    793 				break;
    794 			default:
    795 				vap->va_bytes = vap->va_size = 0;
    796 				break;
    797 			}
    798 			FILE_UNUSE(fp, curlwp);
    799 			break;
    800 		}
    801 		/*FALLTHROUGH*/
    802 	case PFSproc:
    803 		vap->va_nlink = 2;
    804 		vap->va_uid = kauth_cred_geteuid(procp->p_cred);
    805 		vap->va_gid = kauth_cred_getegid(procp->p_cred);
    806 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    807 		break;
    808 
    809 	case PFSfile:
    810 		error = EOPNOTSUPP;
    811 		break;
    812 
    813 	case PFSmem:
    814 		vap->va_bytes = vap->va_size =
    815 			ctob(procp->p_vmspace->vm_tsize +
    816 				    procp->p_vmspace->vm_dsize +
    817 				    procp->p_vmspace->vm_ssize);
    818 		break;
    819 
    820 #if defined(PT_GETREGS) || defined(PT_SETREGS)
    821 	case PFSregs:
    822 		vap->va_bytes = vap->va_size = sizeof(struct reg);
    823 		break;
    824 #endif
    825 
    826 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
    827 	case PFSfpregs:
    828 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
    829 		break;
    830 #endif
    831 
    832 	case PFSctl:
    833 	case PFSstatus:
    834 	case PFSstat:
    835 	case PFSnote:
    836 	case PFSnotepg:
    837 	case PFScmdline:
    838 	case PFSmeminfo:
    839 	case PFSdevices:
    840 	case PFScpuinfo:
    841 	case PFSuptime:
    842 	case PFSmounts:
    843 		vap->va_bytes = vap->va_size = 0;
    844 		break;
    845 	case PFSmap:
    846 	case PFSmaps:
    847 		/*
    848 		 * Advise a larger blocksize for the map files, so that
    849 		 * they may be read in one pass.
    850 		 */
    851 		vap->va_blocksize = 4 * PAGE_SIZE;
    852 		vap->va_bytes = vap->va_size = 0;
    853 		break;
    854 
    855 	case PFScwd:
    856 	case PFSchroot:
    857 	case PFSexe: {
    858 		char *bp;
    859 
    860 		vap->va_nlink = 1;
    861 		vap->va_uid = 0;
    862 		vap->va_gid = 0;
    863 		bp = path + MAXPATHLEN;
    864 		*--bp = '\0';
    865 		mutex_enter(&procp->p_mutex);
    866 		(void)procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
    867 		     MAXPATHLEN);
    868 		mutex_exit(&procp->p_mutex);
    869 		vap->va_bytes = vap->va_size = strlen(bp);
    870 		break;
    871 	}
    872 
    873 	case PFSemul:
    874 		vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
    875 		break;
    876 
    877 #ifdef __HAVE_PROCFS_MACHDEP
    878 	PROCFS_MACHDEP_NODETYPE_CASES
    879 		error = procfs_machdep_getattr(ap->a_vp, vap, procp);
    880 		break;
    881 #endif
    882 
    883 	default:
    884 		panic("procfs_getattr");
    885 	}
    886 
    887 	if (procp != NULL)
    888 		procfs_proc_unlock(procp);
    889 	if (path != NULL)
    890 		free(path, M_TEMP);
    891 
    892 	return (error);
    893 }
    894 
    895 /*ARGSUSED*/
    896 int
    897 procfs_setattr(void *v)
    898 {
    899 	/*
    900 	 * just fake out attribute setting
    901 	 * it's not good to generate an error
    902 	 * return, otherwise things like creat()
    903 	 * will fail when they try to set the
    904 	 * file length to 0.  worse, this means
    905 	 * that echo $note > /proc/$pid/note will fail.
    906 	 */
    907 
    908 	return (0);
    909 }
    910 
    911 /*
    912  * implement access checking.
    913  *
    914  * actually, the check for super-user is slightly
    915  * broken since it will allow read access to write-only
    916  * objects.  this doesn't cause any particular trouble
    917  * but does mean that the i/o entry points need to check
    918  * that the operation really does make sense.
    919  */
    920 int
    921 procfs_access(v)
    922 	void *v;
    923 {
    924 	struct vop_access_args /* {
    925 		struct vnode *a_vp;
    926 		int a_mode;
    927 		kauth_cred_t a_cred;
    928 		struct lwp *a_l;
    929 	} */ *ap = v;
    930 	struct vattr va;
    931 	int error;
    932 
    933 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_l)) != 0)
    934 		return (error);
    935 
    936 	return (vaccess(va.va_type, va.va_mode,
    937 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
    938 }
    939 
    940 /*
    941  * lookup.  this is incredibly complicated in the
    942  * general case, however for most pseudo-filesystems
    943  * very little needs to be done.
    944  *
    945  * Locking isn't hard here, just poorly documented.
    946  *
    947  * If we're looking up ".", just vref the parent & return it.
    948  *
    949  * If we're looking up "..", unlock the parent, and lock "..". If everything
    950  * went ok, and we're on the last component and the caller requested the
    951  * parent locked, try to re-lock the parent. We do this to prevent lock
    952  * races.
    953  *
    954  * For anything else, get the needed node. Then unlock the parent if not
    955  * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
    956  * parent in the .. case).
    957  *
    958  * We try to exit with the parent locked in error cases.
    959  */
    960 int
    961 procfs_lookup(v)
    962 	void *v;
    963 {
    964 	struct vop_lookup_args /* {
    965 		struct vnode * a_dvp;
    966 		struct vnode ** a_vpp;
    967 		struct componentname * a_cnp;
    968 	} */ *ap = v;
    969 	struct componentname *cnp = ap->a_cnp;
    970 	struct vnode **vpp = ap->a_vpp;
    971 	struct vnode *dvp = ap->a_dvp;
    972 	const char *pname = cnp->cn_nameptr;
    973 	const struct proc_target *pt = NULL;
    974 	struct vnode *fvp;
    975 	pid_t pid, vnpid;
    976 	struct pfsnode *pfs;
    977 	struct proc *p = NULL;
    978 	struct lwp *l = NULL;
    979 	int i, error;
    980 	pfstype type;
    981 
    982 	*vpp = NULL;
    983 
    984 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    985 		return (EROFS);
    986 
    987 	if (cnp->cn_namelen == 1 && *pname == '.') {
    988 		*vpp = dvp;
    989 		VREF(dvp);
    990 		return (0);
    991 	}
    992 
    993 	pfs = VTOPFS(dvp);
    994 	switch (pfs->pfs_type) {
    995 	case PFSroot:
    996 		/*
    997 		 * Shouldn't get here with .. in the root node.
    998 		 */
    999 		if (cnp->cn_flags & ISDOTDOT)
   1000 			return (EIO);
   1001 
   1002 		for (i = 0; i < nproc_root_targets; i++) {
   1003 			pt = &proc_root_targets[i];
   1004 			/*
   1005 			 * check for node match.  proc is always NULL here,
   1006 			 * so call pt_valid with constant NULL lwp.
   1007 			 */
   1008 			if (cnp->cn_namelen == pt->pt_namlen &&
   1009 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
   1010 			    (pt->pt_valid == NULL ||
   1011 			     (*pt->pt_valid)(NULL, dvp->v_mount)))
   1012 				break;
   1013 		}
   1014 
   1015 		if (i != nproc_root_targets) {
   1016 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
   1017 			    pt->pt_pfstype, -1, NULL);
   1018 			return (error);
   1019 		}
   1020 
   1021 		if (CNEQ(cnp, "curproc", 7)) {
   1022 			pid = curproc->p_pid;
   1023 			vnpid = 0;
   1024 			type = PFScurproc;
   1025 		} else if (CNEQ(cnp, "self", 4)) {
   1026 			pid = curproc->p_pid;
   1027 			vnpid = 0;
   1028 			type = PFSself;
   1029 		} else {
   1030 			pid = (pid_t)atoi(pname, cnp->cn_namelen);
   1031 			vnpid = pid;
   1032 			type = PFSproc;
   1033 		}
   1034 
   1035 		if (procfs_proc_lock(pid, &p, ESRCH) != 0)
   1036 			break;
   1037 		error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1, p);
   1038 		procfs_proc_unlock(p);
   1039 		return (error);
   1040 
   1041 	case PFSproc:
   1042 		/*
   1043 		 * do the .. dance. We unlock the directory, and then
   1044 		 * get the root dir. That will automatically return ..
   1045 		 * locked. Then if the caller wanted dvp locked, we
   1046 		 * re-lock.
   1047 		 */
   1048 		if (cnp->cn_flags & ISDOTDOT) {
   1049 			VOP_UNLOCK(dvp, 0);
   1050 			error = procfs_root(dvp->v_mount, vpp);
   1051 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
   1052 			return (error);
   1053 		}
   1054 
   1055 		if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
   1056 			break;
   1057 
   1058 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
   1059 			if (cnp->cn_namelen == pt->pt_namlen &&
   1060 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
   1061 			    (pt->pt_valid == NULL || (p != NULL &&
   1062 			      (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
   1063 					      dvp->v_mount))))
   1064 				break;
   1065 		}
   1066 		if (i == nproc_targets) {
   1067 			procfs_proc_unlock(p);
   1068 			break;
   1069 		}
   1070 		if (pt->pt_pfstype == PFSfile) {
   1071 			fvp = p->p_textvp;
   1072 			/* We already checked that it exists. */
   1073 			VREF(fvp);
   1074 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
   1075 			*vpp = fvp;
   1076 			procfs_proc_unlock(p);
   1077 			return (0);
   1078 		}
   1079 
   1080 		error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
   1081 		    pt->pt_pfstype, -1, p);
   1082 		procfs_proc_unlock(p);
   1083 		return (error);
   1084 
   1085 	case PFSfd: {
   1086 		int fd;
   1087 		struct file *fp;
   1088 
   1089 		if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0)
   1090 			return error;
   1091 
   1092 		/*
   1093 		 * do the .. dance. We unlock the directory, and then
   1094 		 * get the proc dir. That will automatically return ..
   1095 		 * locked. Then re-lock the directory.
   1096 		 */
   1097 		if (cnp->cn_flags & ISDOTDOT) {
   1098 			VOP_UNLOCK(dvp, 0);
   1099 			error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
   1100 			    PFSproc, -1, p);
   1101 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
   1102 			procfs_proc_unlock(p);
   1103 			return (error);
   1104 		}
   1105 		fd = atoi(pname, cnp->cn_namelen);
   1106 
   1107 		mutex_enter(&p->p_mutex);
   1108 		fp = fd_getfile(p->p_fd, fd);
   1109 		mutex_exit(&p->p_mutex);
   1110 		if (fp == NULL) {
   1111 			procfs_proc_unlock(p);
   1112 			return ENOENT;
   1113 		}
   1114 
   1115 		FILE_USE(fp);
   1116 
   1117 		switch (fp->f_type) {
   1118 		case DTYPE_VNODE:
   1119 			fvp = (struct vnode *)fp->f_data;
   1120 
   1121 			/* Don't show directories */
   1122 			if (fvp->v_type == VDIR)
   1123 				goto symlink;
   1124 
   1125 			VREF(fvp);
   1126 			FILE_UNUSE(fp, l);
   1127 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
   1128 			    (p == curproc ? LK_CANRECURSE : 0));
   1129 			*vpp = fvp;
   1130 			error = 0;
   1131 			break;
   1132 		default:
   1133 		symlink:
   1134 			FILE_UNUSE(fp, l);
   1135 			error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
   1136 			    PFSfd, fd, p);
   1137 			break;
   1138 		}
   1139 		procfs_proc_unlock(p);
   1140 		return error;
   1141 	}
   1142 	default:
   1143 		return (ENOTDIR);
   1144 	}
   1145 
   1146 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
   1147 }
   1148 
   1149 int
   1150 procfs_validfile(struct lwp *l, struct mount *mp)
   1151 {
   1152 	return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL;
   1153 }
   1154 
   1155 static int
   1156 procfs_validfile_linux(l, mp)
   1157 	struct lwp *l;
   1158 	struct mount *mp;
   1159 {
   1160 	int flags;
   1161 
   1162 	flags = VFSTOPROC(mp)->pmnt_flags;
   1163 	return (flags & PROCFSMNT_LINUXCOMPAT) &&
   1164 	    (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
   1165 }
   1166 
   1167 struct procfs_root_readdir_ctx {
   1168 	struct uio *uiop;
   1169 	off_t *cookies;
   1170 	int ncookies;
   1171 	off_t off;
   1172 	off_t startoff;
   1173 	int error;
   1174 };
   1175 
   1176 static int
   1177 procfs_root_readdir_callback(struct proc *p, void *arg)
   1178 {
   1179 	struct procfs_root_readdir_ctx *ctxp = arg;
   1180 	struct dirent d;
   1181 	struct uio *uiop;
   1182 	int error;
   1183 
   1184 	uiop = ctxp->uiop;
   1185 	if (uiop->uio_resid < UIO_MX)
   1186 		return -1; /* no space */
   1187 
   1188 	if (ctxp->off < ctxp->startoff) {
   1189 		ctxp->off++;
   1190 		return 0;
   1191 	}
   1192 
   1193 	if (kauth_authorize_process(kauth_cred_get(),
   1194 	    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
   1195 		return 0;
   1196 
   1197 	memset(&d, 0, UIO_MX);
   1198 	d.d_reclen = UIO_MX;
   1199 	d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
   1200 	d.d_namlen = snprintf(d.d_name,
   1201 	    UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
   1202 	d.d_type = DT_DIR;
   1203 
   1204 	rw_exit(&proclist_lock);
   1205 	error = uiomove(&d, UIO_MX, uiop);
   1206 	rw_enter(&proclist_lock, RW_READER);
   1207 	if (error) {
   1208 		ctxp->error = error;
   1209 		return -1;
   1210 	}
   1211 
   1212 	ctxp->ncookies++;
   1213 	if (ctxp->cookies)
   1214 		*(ctxp->cookies)++ = ctxp->off + 1;
   1215 	ctxp->off++;
   1216 
   1217 	return 0;
   1218 }
   1219 
   1220 /*
   1221  * readdir returns directory entries from pfsnode (vp).
   1222  *
   1223  * the strategy here with procfs is to generate a single
   1224  * directory entry at a time (struct dirent) and then
   1225  * copy that out to userland using uiomove.  a more efficent
   1226  * though more complex implementation, would try to minimize
   1227  * the number of calls to uiomove().  for procfs, this is
   1228  * hardly worth the added code complexity.
   1229  *
   1230  * this should just be done through read()
   1231  */
   1232 int
   1233 procfs_readdir(v)
   1234 	void *v;
   1235 {
   1236 	struct vop_readdir_args /* {
   1237 		struct vnode *a_vp;
   1238 		struct uio *a_uio;
   1239 		kauth_cred_t a_cred;
   1240 		int *a_eofflag;
   1241 		off_t **a_cookies;
   1242 		int *a_ncookies;
   1243 	} */ *ap = v;
   1244 	struct uio *uio = ap->a_uio;
   1245 	struct dirent d;
   1246 	struct pfsnode *pfs;
   1247 	off_t i;
   1248 	int error;
   1249 	off_t *cookies = NULL;
   1250 	int ncookies;
   1251 	struct vnode *vp;
   1252 	const struct proc_target *pt;
   1253 	struct procfs_root_readdir_ctx ctx;
   1254 	struct lwp *l;
   1255 
   1256 	vp = ap->a_vp;
   1257 	pfs = VTOPFS(vp);
   1258 
   1259 	if (uio->uio_resid < UIO_MX)
   1260 		return (EINVAL);
   1261 	if (uio->uio_offset < 0)
   1262 		return (EINVAL);
   1263 
   1264 	error = 0;
   1265 	i = uio->uio_offset;
   1266 	memset(&d, 0, UIO_MX);
   1267 	d.d_reclen = UIO_MX;
   1268 	ncookies = uio->uio_resid / UIO_MX;
   1269 
   1270 	switch (pfs->pfs_type) {
   1271 	/*
   1272 	 * this is for the process-specific sub-directories.
   1273 	 * all that is needed to is copy out all the entries
   1274 	 * from the procent[] table (top of this file).
   1275 	 */
   1276 	case PFSproc: {
   1277 		struct proc *p;
   1278 
   1279 		if (i >= nproc_targets)
   1280 			return 0;
   1281 
   1282 		if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
   1283 			break;
   1284 
   1285 		if (ap->a_ncookies) {
   1286 			ncookies = min(ncookies, (nproc_targets - i));
   1287 			cookies = malloc(ncookies * sizeof (off_t),
   1288 			    M_TEMP, M_WAITOK);
   1289 			*ap->a_cookies = cookies;
   1290 		}
   1291 
   1292 		for (pt = &proc_targets[i];
   1293 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
   1294 			if (pt->pt_valid) {
   1295 				/* XXXSMP locking */
   1296 				mutex_enter(&p->p_smutex);
   1297 				l = proc_representative_lwp(p, NULL, 1);
   1298 				mutex_exit(&p->p_smutex);
   1299 				if ((*pt->pt_valid)(l, vp->v_mount) == 0)
   1300 					continue;
   1301 			}
   1302 
   1303 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
   1304 			    pt->pt_pfstype, -1);
   1305 			d.d_namlen = pt->pt_namlen;
   1306 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1307 			d.d_type = pt->pt_type;
   1308 
   1309 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1310 				break;
   1311 			if (cookies)
   1312 				*cookies++ = i + 1;
   1313 		}
   1314 
   1315 		procfs_proc_unlock(p);
   1316 	    	break;
   1317 	}
   1318 	case PFSfd: {
   1319 		struct proc *p;
   1320 		struct filedesc	*fdp;
   1321 		struct file *fp;
   1322 		int lim, nc = 0;
   1323 
   1324 		if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
   1325 			return error;
   1326 
   1327 		if (kauth_authorize_process(kauth_cred_get(),
   1328 		    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0) {
   1329 		    	procfs_proc_unlock(p);
   1330 			return ESRCH;
   1331 		}
   1332 
   1333 		fdp = p->p_fd;	/* XXXSMP */
   1334 
   1335 		lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
   1336 		if (i >= lim) {
   1337 		    	procfs_proc_unlock(p);
   1338 			return 0;
   1339 		}
   1340 
   1341 		if (ap->a_ncookies) {
   1342 			ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
   1343 			cookies = malloc(ncookies * sizeof (off_t),
   1344 			    M_TEMP, M_WAITOK);
   1345 			*ap->a_cookies = cookies;
   1346 		}
   1347 
   1348 		for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
   1349 			pt = &proc_targets[i];
   1350 			d.d_namlen = pt->pt_namlen;
   1351 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
   1352 			    pt->pt_pfstype, -1);
   1353 			(void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1354 			d.d_type = pt->pt_type;
   1355 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1356 				break;
   1357 			if (cookies)
   1358 				*cookies++ = i + 1;
   1359 			nc++;
   1360 		}
   1361 		if (error) {
   1362 			ncookies = nc;
   1363 			break;
   1364 		}
   1365 		for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
   1366 			/* check the descriptor exists */
   1367 			if ((fp = fd_getfile(fdp, i - 2)) == NULL)
   1368 				continue;
   1369 			simple_unlock(&fp->f_slock);
   1370 
   1371 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
   1372 			d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
   1373 			    "%lld", (long long)(i - 2));
   1374 			d.d_type = VREG;
   1375 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1376 				break;
   1377 			if (cookies)
   1378 				*cookies++ = i + 1;
   1379 			nc++;
   1380 		}
   1381 		ncookies = nc;
   1382 		procfs_proc_unlock(p);
   1383 		break;
   1384 	}
   1385 
   1386 	/*
   1387 	 * this is for the root of the procfs filesystem
   1388 	 * what is needed are special entries for "curproc"
   1389 	 * and "self" followed by an entry for each process
   1390 	 * on allproc.
   1391 	 */
   1392 
   1393 	case PFSroot: {
   1394 		int nc = 0;
   1395 
   1396 		if (ap->a_ncookies) {
   1397 			/*
   1398 			 * XXX Potentially allocating too much space here,
   1399 			 * but I'm lazy. This loop needs some work.
   1400 			 */
   1401 			cookies = malloc(ncookies * sizeof (off_t),
   1402 			    M_TEMP, M_WAITOK);
   1403 			*ap->a_cookies = cookies;
   1404 		}
   1405 		error = 0;
   1406 		/* 0 ... 3 are static entries. */
   1407 		for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
   1408 			switch (i) {
   1409 			case 0:		/* `.' */
   1410 			case 1:		/* `..' */
   1411 				d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
   1412 				d.d_namlen = i + 1;
   1413 				memcpy(d.d_name, "..", d.d_namlen);
   1414 				d.d_name[i + 1] = '\0';
   1415 				d.d_type = DT_DIR;
   1416 				break;
   1417 
   1418 			case 2:
   1419 				d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
   1420 				d.d_namlen = sizeof("curproc") - 1;
   1421 				memcpy(d.d_name, "curproc", sizeof("curproc"));
   1422 				d.d_type = DT_LNK;
   1423 				break;
   1424 
   1425 			case 3:
   1426 				d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
   1427 				d.d_namlen = sizeof("self") - 1;
   1428 				memcpy(d.d_name, "self", sizeof("self"));
   1429 				d.d_type = DT_LNK;
   1430 				break;
   1431 			}
   1432 
   1433 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1434 				break;
   1435 			nc++;
   1436 			if (cookies)
   1437 				*cookies++ = i + 1;
   1438 		}
   1439 		/* 4 ... are process entries. */
   1440 		ctx.uiop = uio;
   1441 		ctx.error = 0;
   1442 		ctx.off = 4;
   1443 		ctx.startoff = i;
   1444 		ctx.cookies = cookies;
   1445 		ctx.ncookies = nc;
   1446 		proclist_foreach_call(&allproc,
   1447 		    procfs_root_readdir_callback, &ctx);
   1448 		cookies = ctx.cookies;
   1449 		nc = ctx.ncookies;
   1450 		error = ctx.error;
   1451 		if (error)
   1452 			break;
   1453 
   1454 		/* misc entries. */
   1455 		if (i < ctx.off)
   1456 			i = ctx.off;
   1457 		if (i >= ctx.off + nproc_root_targets)
   1458 			break;
   1459 		for (pt = &proc_root_targets[i - ctx.off];
   1460 		    uio->uio_resid >= UIO_MX &&
   1461 		    pt < &proc_root_targets[nproc_root_targets];
   1462 		    pt++, i++) {
   1463 			if (pt->pt_valid &&
   1464 			    (*pt->pt_valid)(NULL, vp->v_mount) == 0)
   1465 				continue;
   1466 			d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
   1467 			d.d_namlen = pt->pt_namlen;
   1468 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1469 			d.d_type = pt->pt_type;
   1470 
   1471 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1472 				break;
   1473 			nc++;
   1474 			if (cookies)
   1475 				*cookies++ = i + 1;
   1476 		}
   1477 
   1478 		ncookies = nc;
   1479 		break;
   1480 	}
   1481 
   1482 	default:
   1483 		error = ENOTDIR;
   1484 		break;
   1485 	}
   1486 
   1487 	if (ap->a_ncookies) {
   1488 		if (error) {
   1489 			if (cookies)
   1490 				free(*ap->a_cookies, M_TEMP);
   1491 			*ap->a_ncookies = 0;
   1492 			*ap->a_cookies = NULL;
   1493 		} else
   1494 			*ap->a_ncookies = ncookies;
   1495 	}
   1496 	uio->uio_offset = i;
   1497 	return (error);
   1498 }
   1499 
   1500 /*
   1501  * readlink reads the link of `curproc' and others
   1502  */
   1503 int
   1504 procfs_readlink(v)
   1505 	void *v;
   1506 {
   1507 	struct vop_readlink_args *ap = v;
   1508 	char bf[16];		/* should be enough */
   1509 	char *bp = bf;
   1510 	char *path = NULL;
   1511 	int len;
   1512 	int error = 0;
   1513 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
   1514 	struct proc *pown;
   1515 
   1516 	if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
   1517 		len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
   1518 	else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
   1519 		len = snprintf(bf, sizeof(bf), "%s", "curproc");
   1520 	else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
   1521 	    pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1) ||
   1522 	    pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) {
   1523 		if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
   1524 			return error;
   1525 		MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
   1526 		    M_WAITOK|M_CANFAIL);
   1527 		if (path == NULL) {
   1528 			procfs_proc_unlock(pown);
   1529 			return (ENOMEM);
   1530 		}
   1531 		bp = path + MAXPATHLEN;
   1532 		*--bp = '\0';
   1533 		mutex_enter(&pown->p_mutex);
   1534 		(void)procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
   1535 		    &bp, path, MAXPATHLEN);
   1536 		mutex_exit(&pown->p_mutex);
   1537 		procfs_proc_unlock(pown);
   1538 		len = strlen(bp);
   1539 	} else {
   1540 		struct file *fp;
   1541 		struct vnode *vxp, *vp;
   1542 
   1543 		if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
   1544 			return error;
   1545 		mutex_enter(&pown->p_mutex);
   1546 		fp = fd_getfile(pown->p_fd, pfs->pfs_fd);
   1547 		mutex_exit(&pown->p_mutex);
   1548 		if (error != 0) {
   1549 			procfs_proc_unlock(pown);
   1550 			return (EBADF);
   1551 		}
   1552 		FILE_USE(fp);
   1553 		switch (fp->f_type) {
   1554 		case DTYPE_VNODE:
   1555 			vxp = (struct vnode *)fp->f_data;
   1556 			if (vxp->v_type != VDIR) {
   1557 				FILE_UNUSE(fp, curlwp);
   1558 				error = EINVAL;
   1559 				break;
   1560 			}
   1561 			if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
   1562 			    == NULL) {
   1563 				FILE_UNUSE(fp, curlwp);
   1564 				error = ENOMEM;
   1565 				break;
   1566 			}
   1567 			bp = path + MAXPATHLEN;
   1568 			*--bp = '\0';
   1569 			vp = curproc->p_cwdi->cwdi_rdir;	/* XXXSMP */
   1570 			if (vp == NULL)
   1571 				vp = rootvnode;
   1572 			error = getcwd_common(vxp, vp, &bp, path,
   1573 			    MAXPATHLEN / 2, 0, curlwp);
   1574 			FILE_UNUSE(fp, curlwp);
   1575 			if (error)
   1576 				break;
   1577 			len = strlen(bp);
   1578 			break;
   1579 
   1580 		case DTYPE_MISC:
   1581 			len = snprintf(bf, sizeof(bf), "%s", "[misc]");
   1582 			break;
   1583 
   1584 		case DTYPE_KQUEUE:
   1585 			len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
   1586 			break;
   1587 
   1588 		default:
   1589 			error = EINVAL;
   1590 			break;
   1591 		}
   1592 		procfs_proc_unlock(pown);
   1593 	}
   1594 
   1595 	if (error == 0)
   1596 		error = uiomove(bp, len, ap->a_uio);
   1597 	if (path)
   1598 		free(path, M_TEMP);
   1599 	return error;
   1600 }
   1601 
   1602 /*
   1603  * convert decimal ascii to int
   1604  */
   1605 static int
   1606 atoi(b, len)
   1607 	const char *b;
   1608 	size_t len;
   1609 {
   1610 	int p = 0;
   1611 
   1612 	while (len--) {
   1613 		char c = *b++;
   1614 		if (c < '0' || c > '9')
   1615 			return -1;
   1616 		p = 10 * p + (c - '0');
   1617 	}
   1618 
   1619 	return p;
   1620 }
   1621