Home | History | Annotate | Line # | Download | only in procfs
procfs_vnops.c revision 1.78.2.9
      1 /*	$NetBSD: procfs_vnops.c,v 1.78.2.9 2002/06/24 22:11:16 nathanw Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Jan-Simon Pendry
      5  * Copyright (c) 1993, 1995
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Jan-Simon Pendry.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     40  */
     41 
     42 /*
     43  * procfs vnode interface
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.78.2.9 2002/06/24 22:11:16 nathanw Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/time.h>
     52 #include <sys/kernel.h>
     53 #include <sys/file.h>
     54 #include <sys/lwp.h>
     55 #include <sys/proc.h>
     56 #include <sys/vnode.h>
     57 #include <sys/namei.h>
     58 #include <sys/malloc.h>
     59 #include <sys/mount.h>
     60 #include <sys/dirent.h>
     61 #include <sys/resourcevar.h>
     62 #include <sys/stat.h>
     63 #include <sys/ptrace.h>
     64 
     65 #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
     66 
     67 #include <machine/reg.h>
     68 
     69 #include <miscfs/genfs/genfs.h>
     70 #include <miscfs/procfs/procfs.h>
     71 
     72 /*
     73  * Vnode Operations.
     74  *
     75  */
     76 
     77 static int procfs_validfile_linux __P((struct lwp *, struct mount *));
     78 
     79 /*
     80  * This is a list of the valid names in the
     81  * process-specific sub-directories.  It is
     82  * used in procfs_lookup and procfs_readdir
     83  */
     84 const struct proc_target {
     85 	u_char	pt_type;
     86 	u_char	pt_namlen;
     87 	char	*pt_name;
     88 	pfstype	pt_pfstype;
     89 	int	(*pt_valid) __P((struct lwp *, struct mount *));
     90 } proc_targets[] = {
     91 #define N(s) sizeof(s)-1, s
     92 	/*	  name		type		validp */
     93 	{ DT_DIR, N("."),	Pproc,		NULL },
     94 	{ DT_DIR, N(".."),	Proot,		NULL },
     95 	{ DT_REG, N("file"),	Pfile,		procfs_validfile },
     96 	{ DT_REG, N("mem"),	Pmem,		NULL },
     97 	{ DT_REG, N("regs"),	Pregs,		procfs_validregs },
     98 	{ DT_REG, N("fpregs"),	Pfpregs,	procfs_validfpregs },
     99 	{ DT_REG, N("ctl"),	Pctl,		NULL },
    100 	{ DT_REG, N("status"),	Pstatus,	NULL },
    101 	{ DT_REG, N("note"),	Pnote,		NULL },
    102 	{ DT_REG, N("notepg"),	Pnotepg,	NULL },
    103 	{ DT_REG, N("map"),	Pmap,		procfs_validmap },
    104 	{ DT_REG, N("maps"),	Pmaps,		procfs_validmap },
    105 	{ DT_REG, N("cmdline"), Pcmdline,	NULL },
    106 	{ DT_REG, N("exe"),	Pfile,		procfs_validfile_linux },
    107 #ifdef __HAVE_PROCFS_MACHDEP
    108 	PROCFS_MACHDEP_NODETYPE_DEFNS
    109 #endif
    110 #undef N
    111 };
    112 static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
    113 
    114 /*
    115  * List of files in the root directory. Note: the validate function will
    116  * be called with p == NULL for these ones.
    117  */
    118 struct proc_target proc_root_targets[] = {
    119 #define N(s) sizeof(s)-1, s
    120 	/*	  name		    type	    validp */
    121 	{ DT_REG, N("meminfo"),     Pmeminfo,        procfs_validfile_linux },
    122 	{ DT_REG, N("cpuinfo"),     Pcpuinfo,        procfs_validfile_linux },
    123 #undef N
    124 };
    125 static int nproc_root_targets =
    126     sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
    127 
    128 int	procfs_lookup	__P((void *));
    129 #define	procfs_create	genfs_eopnotsupp_rele
    130 #define	procfs_mknod	genfs_eopnotsupp_rele
    131 int	procfs_open	__P((void *));
    132 int	procfs_close	__P((void *));
    133 int	procfs_access	__P((void *));
    134 int	procfs_getattr	__P((void *));
    135 int	procfs_setattr	__P((void *));
    136 #define	procfs_read	procfs_rw
    137 #define	procfs_write	procfs_rw
    138 #define	procfs_fcntl	genfs_fcntl
    139 #define	procfs_ioctl	genfs_enoioctl
    140 #define	procfs_poll	genfs_poll
    141 #define procfs_revoke	genfs_revoke
    142 #define	procfs_fsync	genfs_nullop
    143 #define	procfs_seek	genfs_nullop
    144 #define	procfs_remove	genfs_eopnotsupp_rele
    145 int	procfs_link	__P((void *));
    146 #define	procfs_rename	genfs_eopnotsupp_rele
    147 #define	procfs_mkdir	genfs_eopnotsupp_rele
    148 #define	procfs_rmdir	genfs_eopnotsupp_rele
    149 int	procfs_symlink	__P((void *));
    150 int	procfs_readdir	__P((void *));
    151 int	procfs_readlink	__P((void *));
    152 #define	procfs_abortop	genfs_abortop
    153 int	procfs_inactive	__P((void *));
    154 int	procfs_reclaim	__P((void *));
    155 #define	procfs_lock	genfs_lock
    156 #define	procfs_unlock	genfs_unlock
    157 #define	procfs_bmap	genfs_badop
    158 #define	procfs_strategy	genfs_badop
    159 int	procfs_print	__P((void *));
    160 int	procfs_pathconf	__P((void *));
    161 #define	procfs_islocked	genfs_islocked
    162 #define	procfs_advlock	genfs_einval
    163 #define	procfs_blkatoff	genfs_eopnotsupp
    164 #define	procfs_valloc	genfs_eopnotsupp
    165 #define	procfs_vfree	genfs_nullop
    166 #define	procfs_truncate	genfs_eopnotsupp
    167 #define	procfs_update	genfs_nullop
    168 #define	procfs_bwrite	genfs_eopnotsupp
    169 #define procfs_putpages	genfs_null_putpages
    170 
    171 static pid_t atopid __P((const char *, u_int));
    172 
    173 /*
    174  * procfs vnode operations.
    175  */
    176 int (**procfs_vnodeop_p) __P((void *));
    177 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
    178 	{ &vop_default_desc, vn_default_error },
    179 	{ &vop_lookup_desc, procfs_lookup },		/* lookup */
    180 	{ &vop_create_desc, procfs_create },		/* create */
    181 	{ &vop_mknod_desc, procfs_mknod },		/* mknod */
    182 	{ &vop_open_desc, procfs_open },		/* open */
    183 	{ &vop_close_desc, procfs_close },		/* close */
    184 	{ &vop_access_desc, procfs_access },		/* access */
    185 	{ &vop_getattr_desc, procfs_getattr },		/* getattr */
    186 	{ &vop_setattr_desc, procfs_setattr },		/* setattr */
    187 	{ &vop_read_desc, procfs_read },		/* read */
    188 	{ &vop_write_desc, procfs_write },		/* write */
    189 	{ &vop_fcntl_desc, procfs_fcntl },		/* fcntl */
    190 	{ &vop_ioctl_desc, procfs_ioctl },		/* ioctl */
    191 	{ &vop_poll_desc, procfs_poll },		/* poll */
    192 	{ &vop_revoke_desc, procfs_revoke },		/* revoke */
    193 	{ &vop_fsync_desc, procfs_fsync },		/* fsync */
    194 	{ &vop_seek_desc, procfs_seek },		/* seek */
    195 	{ &vop_remove_desc, procfs_remove },		/* remove */
    196 	{ &vop_link_desc, procfs_link },		/* link */
    197 	{ &vop_rename_desc, procfs_rename },		/* rename */
    198 	{ &vop_mkdir_desc, procfs_mkdir },		/* mkdir */
    199 	{ &vop_rmdir_desc, procfs_rmdir },		/* rmdir */
    200 	{ &vop_symlink_desc, procfs_symlink },		/* symlink */
    201 	{ &vop_readdir_desc, procfs_readdir },		/* readdir */
    202 	{ &vop_readlink_desc, procfs_readlink },	/* readlink */
    203 	{ &vop_abortop_desc, procfs_abortop },		/* abortop */
    204 	{ &vop_inactive_desc, procfs_inactive },	/* inactive */
    205 	{ &vop_reclaim_desc, procfs_reclaim },		/* reclaim */
    206 	{ &vop_lock_desc, procfs_lock },		/* lock */
    207 	{ &vop_unlock_desc, procfs_unlock },		/* unlock */
    208 	{ &vop_bmap_desc, procfs_bmap },		/* bmap */
    209 	{ &vop_strategy_desc, procfs_strategy },	/* strategy */
    210 	{ &vop_print_desc, procfs_print },		/* print */
    211 	{ &vop_islocked_desc, procfs_islocked },	/* islocked */
    212 	{ &vop_pathconf_desc, procfs_pathconf },	/* pathconf */
    213 	{ &vop_advlock_desc, procfs_advlock },		/* advlock */
    214 	{ &vop_blkatoff_desc, procfs_blkatoff },	/* blkatoff */
    215 	{ &vop_valloc_desc, procfs_valloc },		/* valloc */
    216 	{ &vop_vfree_desc, procfs_vfree },		/* vfree */
    217 	{ &vop_truncate_desc, procfs_truncate },	/* truncate */
    218 	{ &vop_update_desc, procfs_update },		/* update */
    219 	{ &vop_putpages_desc, procfs_putpages },	/* putpages */
    220 	{ NULL, NULL }
    221 };
    222 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
    223 	{ &procfs_vnodeop_p, procfs_vnodeop_entries };
    224 /*
    225  * set things up for doing i/o on
    226  * the pfsnode (vp).  (vp) is locked
    227  * on entry, and should be left locked
    228  * on exit.
    229  *
    230  * for procfs we don't need to do anything
    231  * in particular for i/o.  all that is done
    232  * is to support exclusive open on process
    233  * memory images.
    234  */
    235 int
    236 procfs_open(v)
    237 	void *v;
    238 {
    239 	struct vop_open_args /* {
    240 		struct vnode *a_vp;
    241 		int  a_mode;
    242 		struct ucred *a_cred;
    243 		struct proc *a_p;
    244 	} */ *ap = v;
    245 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    246 	struct proc *p1, *p2;
    247 	int error;
    248 
    249 	p1 = ap->a_p;				/* tracer */
    250 	p2 = PFIND(pfs->pfs_pid);		/* traced */
    251 
    252 	if (p2 == NULL)
    253 		return (ENOENT);		/* was ESRCH, jsp */
    254 
    255 	switch (pfs->pfs_type) {
    256 	case Pmem:
    257 		if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
    258 		    ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
    259 			return (EBUSY);
    260 
    261 		if ((error = process_checkioperm(p1, p2)) != 0)
    262 			return (error);
    263 
    264 		if (ap->a_mode & FWRITE)
    265 			pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
    266 
    267 		return (0);
    268 
    269 	default:
    270 		break;
    271 	}
    272 
    273 	return (0);
    274 }
    275 
    276 /*
    277  * close the pfsnode (vp) after doing i/o.
    278  * (vp) is not locked on entry or exit.
    279  *
    280  * nothing to do for procfs other than undo
    281  * any exclusive open flag (see _open above).
    282  */
    283 int
    284 procfs_close(v)
    285 	void *v;
    286 {
    287 	struct vop_close_args /* {
    288 		struct vnode *a_vp;
    289 		int  a_fflag;
    290 		struct ucred *a_cred;
    291 		struct proc *a_p;
    292 	} */ *ap = v;
    293 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    294 
    295 	switch (pfs->pfs_type) {
    296 	case Pmem:
    297 		if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
    298 			pfs->pfs_flags &= ~(FWRITE|O_EXCL);
    299 		break;
    300 
    301 	default:
    302 		break;
    303 	}
    304 
    305 	return (0);
    306 }
    307 
    308 /*
    309  * _inactive is called when the pfsnode
    310  * is vrele'd and the reference count goes
    311  * to zero.  (vp) will be on the vnode free
    312  * list, so to get it back vget() must be
    313  * used.
    314  *
    315  * for procfs, check if the process is still
    316  * alive and if it isn't then just throw away
    317  * the vnode by calling vgone().  this may
    318  * be overkill and a waste of time since the
    319  * chances are that the process will still be
    320  * there and PFIND is not free.
    321  *
    322  * (vp) is locked on entry, but must be unlocked on exit.
    323  */
    324 int
    325 procfs_inactive(v)
    326 	void *v;
    327 {
    328 	struct vop_inactive_args /* {
    329 		struct vnode *a_vp;
    330 		struct proc *a_p;
    331 	} */ *ap = v;
    332 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    333 
    334 	VOP_UNLOCK(ap->a_vp, 0);
    335 	if (PFIND(pfs->pfs_pid) == 0)
    336 		vgone(ap->a_vp);
    337 
    338 	return (0);
    339 }
    340 
    341 /*
    342  * _reclaim is called when getnewvnode()
    343  * wants to make use of an entry on the vnode
    344  * free list.  at this time the filesystem needs
    345  * to free any private data and remove the node
    346  * from any private lists.
    347  */
    348 int
    349 procfs_reclaim(v)
    350 	void *v;
    351 {
    352 	struct vop_reclaim_args /* {
    353 		struct vnode *a_vp;
    354 	} */ *ap = v;
    355 
    356 	return (procfs_freevp(ap->a_vp));
    357 }
    358 
    359 /*
    360  * Return POSIX pathconf information applicable to special devices.
    361  */
    362 int
    363 procfs_pathconf(v)
    364 	void *v;
    365 {
    366 	struct vop_pathconf_args /* {
    367 		struct vnode *a_vp;
    368 		int a_name;
    369 		register_t *a_retval;
    370 	} */ *ap = v;
    371 
    372 	switch (ap->a_name) {
    373 	case _PC_LINK_MAX:
    374 		*ap->a_retval = LINK_MAX;
    375 		return (0);
    376 	case _PC_MAX_CANON:
    377 		*ap->a_retval = MAX_CANON;
    378 		return (0);
    379 	case _PC_MAX_INPUT:
    380 		*ap->a_retval = MAX_INPUT;
    381 		return (0);
    382 	case _PC_PIPE_BUF:
    383 		*ap->a_retval = PIPE_BUF;
    384 		return (0);
    385 	case _PC_CHOWN_RESTRICTED:
    386 		*ap->a_retval = 1;
    387 		return (0);
    388 	case _PC_VDISABLE:
    389 		*ap->a_retval = _POSIX_VDISABLE;
    390 		return (0);
    391 	case _PC_SYNC_IO:
    392 		*ap->a_retval = 1;
    393 		return (0);
    394 	default:
    395 		return (EINVAL);
    396 	}
    397 	/* NOTREACHED */
    398 }
    399 
    400 /*
    401  * _print is used for debugging.
    402  * just print a readable description
    403  * of (vp).
    404  */
    405 int
    406 procfs_print(v)
    407 	void *v;
    408 {
    409 	struct vop_print_args /* {
    410 		struct vnode *a_vp;
    411 	} */ *ap = v;
    412 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    413 
    414 	printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
    415 	    pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
    416 	return 0;
    417 }
    418 
    419 int
    420 procfs_link(v)
    421 	void *v;
    422 {
    423 	struct vop_link_args /* {
    424 		struct vnode *a_dvp;
    425 		struct vnode *a_vp;
    426 		struct componentname *a_cnp;
    427 	} */ *ap = v;
    428 
    429 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    430 	vput(ap->a_dvp);
    431 	return (EROFS);
    432 }
    433 
    434 int
    435 procfs_symlink(v)
    436 	void *v;
    437 {
    438 	struct vop_symlink_args /* {
    439 		struct vnode *a_dvp;
    440 		struct vnode **a_vpp;
    441 		struct componentname *a_cnp;
    442 		struct vattr *a_vap;
    443 		char *a_target;
    444 	} */ *ap = v;
    445 
    446 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    447 	vput(ap->a_dvp);
    448 	return (EROFS);
    449 }
    450 
    451 /*
    452  * Invent attributes for pfsnode (vp) and store
    453  * them in (vap).
    454  * Directories lengths are returned as zero since
    455  * any real length would require the genuine size
    456  * to be computed, and nothing cares anyway.
    457  *
    458  * this is relatively minimal for procfs.
    459  */
    460 int
    461 procfs_getattr(v)
    462 	void *v;
    463 {
    464 	struct vop_getattr_args /* {
    465 		struct vnode *a_vp;
    466 		struct vattr *a_vap;
    467 		struct ucred *a_cred;
    468 		struct proc *a_p;
    469 	} */ *ap = v;
    470 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    471 	struct vattr *vap = ap->a_vap;
    472 	struct proc *procp;
    473 	struct timeval tv;
    474 	int error;
    475 
    476 	/* first check the process still exists */
    477 	switch (pfs->pfs_type) {
    478 	case Proot:
    479 	case Pcurproc:
    480 	case Pself:
    481 		procp = 0;
    482 		break;
    483 
    484 	default:
    485 		procp = PFIND(pfs->pfs_pid);
    486 		if (procp == 0)
    487 			return (ENOENT);
    488 		break;
    489 	}
    490 
    491 	error = 0;
    492 
    493 	/* start by zeroing out the attributes */
    494 	VATTR_NULL(vap);
    495 
    496 	/* next do all the common fields */
    497 	vap->va_type = ap->a_vp->v_type;
    498 	vap->va_mode = pfs->pfs_mode;
    499 	vap->va_fileid = pfs->pfs_fileno;
    500 	vap->va_flags = 0;
    501 	vap->va_blocksize = PAGE_SIZE;
    502 
    503 	/*
    504 	 * Make all times be current TOD.
    505 	 * It would be possible to get the process start
    506 	 * time from the p_stat structure, but there's
    507 	 * no "file creation" time stamp anyway, and the
    508 	 * p_stat structure is not addressible if u. gets
    509 	 * swapped out for that process.
    510 	 */
    511 	microtime(&tv);
    512 	TIMEVAL_TO_TIMESPEC(&tv, &vap->va_ctime);
    513 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    514 
    515 	switch (pfs->pfs_type) {
    516 	case Pmem:
    517 	case Pregs:
    518 	case Pfpregs:
    519 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    520 	PROCFS_MACHDEP_PROTECT_CASES
    521 #endif
    522 		/*
    523 		 * If the process has exercised some setuid or setgid
    524 		 * privilege, then rip away read/write permission so
    525 		 * that only root can gain access.
    526 		 */
    527 		if (procp->p_flag & P_SUGID)
    528 			vap->va_mode &= ~(S_IRUSR|S_IWUSR);
    529 		/* FALLTHROUGH */
    530 	case Pctl:
    531 	case Pstatus:
    532 	case Pnote:
    533 	case Pnotepg:
    534 	case Pmap:
    535 	case Pmaps:
    536 	case Pcmdline:
    537 		vap->va_nlink = 1;
    538 		vap->va_uid = procp->p_ucred->cr_uid;
    539 		vap->va_gid = procp->p_ucred->cr_gid;
    540 		break;
    541 	case Pmeminfo:
    542 	case Pcpuinfo:
    543 		vap->va_nlink = 1;
    544 		vap->va_uid = vap->va_gid = 0;
    545 		break;
    546 
    547 	default:
    548 		break;
    549 	}
    550 
    551 	/*
    552 	 * now do the object specific fields
    553 	 *
    554 	 * The size could be set from struct reg, but it's hardly
    555 	 * worth the trouble, and it puts some (potentially) machine
    556 	 * dependent data into this machine-independent code.  If it
    557 	 * becomes important then this function should break out into
    558 	 * a per-file stat function in the corresponding .c file.
    559 	 */
    560 
    561 	switch (pfs->pfs_type) {
    562 	case Proot:
    563 		/*
    564 		 * Set nlink to 1 to tell fts(3) we don't actually know.
    565 		 */
    566 		vap->va_nlink = 1;
    567 		vap->va_uid = 0;
    568 		vap->va_gid = 0;
    569 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    570 		break;
    571 
    572 	case Pcurproc: {
    573 		char buf[16];		/* should be enough */
    574 		vap->va_nlink = 1;
    575 		vap->va_uid = 0;
    576 		vap->va_gid = 0;
    577 		vap->va_bytes = vap->va_size =
    578 		    sprintf(buf, "%ld", (long)curproc->p_pid);
    579 		break;
    580 	}
    581 
    582 	case Pself:
    583 		vap->va_nlink = 1;
    584 		vap->va_uid = 0;
    585 		vap->va_gid = 0;
    586 		vap->va_bytes = vap->va_size = sizeof("curlwp");
    587 		break;
    588 
    589 	case Pproc:
    590 		vap->va_nlink = 2;
    591 		vap->va_uid = procp->p_ucred->cr_uid;
    592 		vap->va_gid = procp->p_ucred->cr_gid;
    593 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    594 		break;
    595 
    596 	case Pfile:
    597 		error = EOPNOTSUPP;
    598 		break;
    599 
    600 	case Pmem:
    601 		vap->va_bytes = vap->va_size =
    602 			ctob(procp->p_vmspace->vm_tsize +
    603 				    procp->p_vmspace->vm_dsize +
    604 				    procp->p_vmspace->vm_ssize);
    605 		break;
    606 
    607 #if defined(PT_GETREGS) || defined(PT_SETREGS)
    608 	case Pregs:
    609 		vap->va_bytes = vap->va_size = sizeof(struct reg);
    610 		break;
    611 #endif
    612 
    613 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
    614 	case Pfpregs:
    615 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
    616 		break;
    617 #endif
    618 
    619 	case Pctl:
    620 	case Pstatus:
    621 	case Pnote:
    622 	case Pnotepg:
    623 	case Pcmdline:
    624 	case Pmeminfo:
    625 	case Pcpuinfo:
    626 		vap->va_bytes = vap->va_size = 0;
    627 		break;
    628 	case Pmap:
    629 	case Pmaps:
    630 		/*
    631 		 * Advise a larger blocksize for the map files, so that
    632 		 * they may be read in one pass.
    633 		 */
    634 		vap->va_blocksize = 4 * PAGE_SIZE;
    635 		vap->va_bytes = vap->va_size = 0;
    636 		break;
    637 
    638 #ifdef __HAVE_PROCFS_MACHDEP
    639 	PROCFS_MACHDEP_NODETYPE_CASES
    640 		error = procfs_machdep_getattr(ap->a_vp, vap, procp);
    641 		break;
    642 #endif
    643 
    644 	default:
    645 		panic("procfs_getattr");
    646 	}
    647 
    648 	return (error);
    649 }
    650 
    651 /*ARGSUSED*/
    652 int
    653 procfs_setattr(v)
    654 	void *v;
    655 {
    656 	/*
    657 	 * just fake out attribute setting
    658 	 * it's not good to generate an error
    659 	 * return, otherwise things like creat()
    660 	 * will fail when they try to set the
    661 	 * file length to 0.  worse, this means
    662 	 * that echo $note > /proc/$pid/note will fail.
    663 	 */
    664 
    665 	return (0);
    666 }
    667 
    668 /*
    669  * implement access checking.
    670  *
    671  * actually, the check for super-user is slightly
    672  * broken since it will allow read access to write-only
    673  * objects.  this doesn't cause any particular trouble
    674  * but does mean that the i/o entry points need to check
    675  * that the operation really does make sense.
    676  */
    677 int
    678 procfs_access(v)
    679 	void *v;
    680 {
    681 	struct vop_access_args /* {
    682 		struct vnode *a_vp;
    683 		int a_mode;
    684 		struct ucred *a_cred;
    685 		struct proc *a_p;
    686 	} */ *ap = v;
    687 	struct vattr va;
    688 	int error;
    689 
    690 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
    691 		return (error);
    692 
    693 	return (vaccess(va.va_type, va.va_mode,
    694 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
    695 }
    696 
    697 /*
    698  * lookup.  this is incredibly complicated in the
    699  * general case, however for most pseudo-filesystems
    700  * very little needs to be done.
    701  *
    702  * Locking isn't hard here, just poorly documented.
    703  *
    704  * If we're looking up ".", just vref the parent & return it.
    705  *
    706  * If we're looking up "..", unlock the parent, and lock "..". If everything
    707  * went ok, and we're on the last component and the caller requested the
    708  * parent locked, try to re-lock the parent. We do this to prevent lock
    709  * races.
    710  *
    711  * For anything else, get the needed node. Then unlock the parent if not
    712  * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
    713  * parent in the .. case).
    714  *
    715  * We try to exit with the parent locked in error cases.
    716  */
    717 int
    718 procfs_lookup(v)
    719 	void *v;
    720 {
    721 	struct vop_lookup_args /* {
    722 		struct vnode * a_dvp;
    723 		struct vnode ** a_vpp;
    724 		struct componentname * a_cnp;
    725 	} */ *ap = v;
    726 	struct componentname *cnp = ap->a_cnp;
    727 	struct vnode **vpp = ap->a_vpp;
    728 	struct vnode *dvp = ap->a_dvp;
    729 	const char *pname = cnp->cn_nameptr;
    730 	const struct proc_target *pt = NULL;
    731 	struct vnode *fvp;
    732 	pid_t pid;
    733 	struct pfsnode *pfs;
    734 	struct proc *p = NULL;
    735 	int i, error, wantpunlock, iscurlwp = 0, isself = 0;
    736 
    737 	*vpp = NULL;
    738 	cnp->cn_flags &= ~PDIRUNLOCK;
    739 
    740 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    741 		return (EROFS);
    742 
    743 	if (cnp->cn_namelen == 1 && *pname == '.') {
    744 		*vpp = dvp;
    745 		VREF(dvp);
    746 		return (0);
    747 	}
    748 
    749 	wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
    750 	pfs = VTOPFS(dvp);
    751 	switch (pfs->pfs_type) {
    752 	case Proot:
    753 		/*
    754 		 * Shouldn't get here with .. in the root node.
    755 		 */
    756 		if (cnp->cn_flags & ISDOTDOT)
    757 			return (EIO);
    758 
    759 		iscurlwp = CNEQ(cnp, "curlwp", 7);
    760 		isself = CNEQ(cnp, "self", 4);
    761 
    762 		if (iscurlwp || isself) {
    763 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
    764 			    iscurlwp ? Pcurproc : Pself);
    765 			if ((error == 0) && (wantpunlock)) {
    766 				VOP_UNLOCK(dvp, 0);
    767 				cnp->cn_flags |= PDIRUNLOCK;
    768 			}
    769 			return (error);
    770 		}
    771 
    772 		for (i = 0; i < nproc_root_targets; i++) {
    773 			pt = &proc_root_targets[i];
    774 			if (cnp->cn_namelen == pt->pt_namlen &&
    775 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
    776 			    (pt->pt_valid == NULL ||
    777 			     (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
    778 				 dvp->v_mount)))
    779 				break;
    780 		}
    781 
    782 		if (i != nproc_root_targets) {
    783 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
    784 			    pt->pt_pfstype);
    785 			if ((error == 0) && (wantpunlock)) {
    786 				VOP_UNLOCK(dvp, 0);
    787 				cnp->cn_flags |= PDIRUNLOCK;
    788 			}
    789 			return (error);
    790 		}
    791 
    792 		pid = atopid(pname, cnp->cn_namelen);
    793 		if (pid == NO_PID)
    794 			break;
    795 
    796 		p = PFIND(pid);
    797 		if (p == 0)
    798 			break;
    799 
    800 		error = procfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
    801 		if ((error == 0) && (wantpunlock)) {
    802 			VOP_UNLOCK(dvp, 0);
    803 			cnp->cn_flags |= PDIRUNLOCK;
    804 		}
    805 		return (error);
    806 
    807 	case Pproc:
    808 		/*
    809 		 * do the .. dance. We unlock the directory, and then
    810 		 * get the root dir. That will automatically return ..
    811 		 * locked. Then if the caller wanted dvp locked, we
    812 		 * re-lock.
    813 		 */
    814 		if (cnp->cn_flags & ISDOTDOT) {
    815 			VOP_UNLOCK(dvp, 0);
    816 			cnp->cn_flags |= PDIRUNLOCK;
    817 			error = procfs_root(dvp->v_mount, vpp);
    818 			if ((error == 0) && (wantpunlock == 0) &&
    819 				    ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
    820 				cnp->cn_flags &= ~PDIRUNLOCK;
    821 			return (error);
    822 		}
    823 
    824 		p = PFIND(pfs->pfs_pid);
    825 		if (p == 0)
    826 			break;
    827 
    828 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
    829 			if (cnp->cn_namelen == pt->pt_namlen &&
    830 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
    831 			    (pt->pt_valid == NULL ||
    832 			     (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
    833 				 dvp->v_mount)))
    834 				goto found;
    835 		}
    836 		break;
    837 
    838 	found:
    839 		if (pt->pt_pfstype == Pfile) {
    840 			fvp = p->p_textvp;
    841 			/* We already checked that it exists. */
    842 			VREF(fvp);
    843 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
    844 			if (wantpunlock) {
    845 				VOP_UNLOCK(dvp, 0);
    846 				cnp->cn_flags |= PDIRUNLOCK;
    847 			}
    848 			*vpp = fvp;
    849 			return (0);
    850 		}
    851 
    852 		error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
    853 					pt->pt_pfstype);
    854 		if ((error == 0) && (wantpunlock)) {
    855 			VOP_UNLOCK(dvp, 0);
    856 			cnp->cn_flags |= PDIRUNLOCK;
    857 		}
    858 		return (error);
    859 
    860 	default:
    861 		return (ENOTDIR);
    862 	}
    863 
    864 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    865 }
    866 
    867 int
    868 procfs_validfile(l, mp)
    869 	struct lwp *l;
    870 	struct mount *mp;
    871 {
    872 	return (l->l_proc->p_textvp != NULL);
    873 }
    874 
    875 static int
    876 procfs_validfile_linux(l, mp)
    877 	struct lwp *l;
    878 	struct mount *mp;
    879 {
    880 	int flags;
    881 
    882 	flags = VFSTOPROC(mp)->pmnt_flags;
    883 	return ((flags & PROCFSMNT_LINUXCOMPAT) &&
    884 	    (l == NULL || procfs_validfile(l, mp)));
    885 }
    886 
    887 /*
    888  * readdir returns directory entries from pfsnode (vp).
    889  *
    890  * the strategy here with procfs is to generate a single
    891  * directory entry at a time (struct dirent) and then
    892  * copy that out to userland using uiomove.  a more efficent
    893  * though more complex implementation, would try to minimize
    894  * the number of calls to uiomove().  for procfs, this is
    895  * hardly worth the added code complexity.
    896  *
    897  * this should just be done through read()
    898  */
    899 int
    900 procfs_readdir(v)
    901 	void *v;
    902 {
    903 	struct vop_readdir_args /* {
    904 		struct vnode *a_vp;
    905 		struct uio *a_uio;
    906 		struct ucred *a_cred;
    907 		int *a_eofflag;
    908 		off_t **a_cookies;
    909 		int *a_ncookies;
    910 	} */ *ap = v;
    911 	struct uio *uio = ap->a_uio;
    912 	struct dirent d;
    913 	struct pfsnode *pfs;
    914 	off_t i;
    915 	int error;
    916 	off_t *cookies = NULL;
    917 	int ncookies, left, skip, j;
    918 	struct vnode *vp;
    919 	const struct proc_target *pt;
    920 
    921 	vp = ap->a_vp;
    922 	pfs = VTOPFS(vp);
    923 
    924 	if (uio->uio_resid < UIO_MX)
    925 		return (EINVAL);
    926 	if (uio->uio_offset < 0)
    927 		return (EINVAL);
    928 
    929 	error = 0;
    930 	i = uio->uio_offset;
    931 	memset((caddr_t)&d, 0, UIO_MX);
    932 	d.d_reclen = UIO_MX;
    933 	ncookies = uio->uio_resid / UIO_MX;
    934 
    935 	switch (pfs->pfs_type) {
    936 	/*
    937 	 * this is for the process-specific sub-directories.
    938 	 * all that is needed to is copy out all the entries
    939 	 * from the procent[] table (top of this file).
    940 	 */
    941 	case Pproc: {
    942 		struct proc *p;
    943 
    944 		if (i >= nproc_targets)
    945 			return 0;
    946 
    947 		p = PFIND(pfs->pfs_pid);
    948 		if (p == NULL)
    949 			break;
    950 
    951 		if (ap->a_ncookies) {
    952 			ncookies = min(ncookies, (nproc_targets - i));
    953 			cookies = malloc(ncookies * sizeof (off_t),
    954 			    M_TEMP, M_WAITOK);
    955 			*ap->a_cookies = cookies;
    956 		}
    957 
    958 		for (pt = &proc_targets[i];
    959 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
    960 			if (pt->pt_valid &&
    961 			    (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
    962 				vp->v_mount) == 0)
    963 				continue;
    964 
    965 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
    966 			d.d_namlen = pt->pt_namlen;
    967 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
    968 			d.d_type = pt->pt_type;
    969 
    970 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    971 				break;
    972 			if (cookies)
    973 				*cookies++ = i + 1;
    974 		}
    975 
    976 	    	break;
    977 	}
    978 
    979 	/*
    980 	 * this is for the root of the procfs filesystem
    981 	 * what is needed are special entries for "curlwp"
    982 	 * and "self" followed by an entry for each process
    983 	 * on allproc
    984 #ifdef PROCFS_ZOMBIE
    985 	 * and deadproc and zombproc.
    986 #endif
    987 	 */
    988 
    989 	case Proot: {
    990 		int pcnt = i, nc = 0;
    991 		const struct proclist_desc *pd;
    992 		volatile struct proc *p;
    993 
    994 		if (pcnt > 3)
    995 			pcnt = 3;
    996 		if (ap->a_ncookies) {
    997 			/*
    998 			 * XXX Potentially allocating too much space here,
    999 			 * but I'm lazy. This loop needs some work.
   1000 			 */
   1001 			cookies = malloc(ncookies * sizeof (off_t),
   1002 			    M_TEMP, M_WAITOK);
   1003 			*ap->a_cookies = cookies;
   1004 		}
   1005 		/*
   1006 		 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
   1007 		 * PROCLIST IN THE proclists!
   1008 		 */
   1009 		proclist_lock_read();
   1010 		pd = proclists;
   1011 #ifdef PROCFS_ZOMBIE
   1012 	again:
   1013 #endif
   1014 		for (p = LIST_FIRST(pd->pd_list);
   1015 		     p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
   1016 			switch (i) {
   1017 			case 0:		/* `.' */
   1018 			case 1:		/* `..' */
   1019 				d.d_fileno = PROCFS_FILENO(0, Proot);
   1020 				d.d_namlen = i + 1;
   1021 				memcpy(d.d_name, "..", d.d_namlen);
   1022 				d.d_name[i + 1] = '\0';
   1023 				d.d_type = DT_DIR;
   1024 				break;
   1025 
   1026 			case 2:
   1027 				d.d_fileno = PROCFS_FILENO(0, Pcurproc);
   1028 				d.d_namlen = sizeof("curlwp") - 1;
   1029 				memcpy(d.d_name, "curlwp", sizeof("curlwp"));
   1030 				d.d_type = DT_LNK;
   1031 				break;
   1032 
   1033 			case 3:
   1034 				d.d_fileno = PROCFS_FILENO(0, Pself);
   1035 				d.d_namlen = sizeof("self") - 1;
   1036 				memcpy(d.d_name, "self", sizeof("self"));
   1037 				d.d_type = DT_LNK;
   1038 				break;
   1039 
   1040 			default:
   1041 				while (pcnt < i) {
   1042 					pcnt++;
   1043 					p = LIST_NEXT(p, p_list);
   1044 					if (!p)
   1045 						goto done;
   1046 				}
   1047 				d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
   1048 				d.d_namlen = sprintf(d.d_name, "%ld",
   1049 				    (long)p->p_pid);
   1050 				d.d_type = DT_DIR;
   1051 				p = p->p_list.le_next;
   1052 				break;
   1053 			}
   1054 
   1055 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1056 				break;
   1057 			nc++;
   1058 			if (cookies)
   1059 				*cookies++ = i + 1;
   1060 		}
   1061 	done:
   1062 
   1063 #ifdef PROCFS_ZOMBIE
   1064 		pd++;
   1065 		if (p == NULL && pd->pd_list != NULL)
   1066 			goto again;
   1067 #endif
   1068 		proclist_unlock_read();
   1069 
   1070 		skip = i - pcnt;
   1071 		if (skip >= nproc_root_targets)
   1072 			break;
   1073 		left = nproc_root_targets - skip;
   1074 		for (j = 0, pt = &proc_root_targets[0];
   1075 		     uio->uio_resid >= UIO_MX && j < left;
   1076 		     pt++, j++, i++) {
   1077 			if (pt->pt_valid &&
   1078 			    (*pt->pt_valid)(NULL, vp->v_mount) == 0)
   1079 				continue;
   1080 			d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype);
   1081 			d.d_namlen = pt->pt_namlen;
   1082 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1083 			d.d_type = pt->pt_type;
   1084 
   1085 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1086 				break;
   1087 			nc++;
   1088 			if (cookies)
   1089 				*cookies++ = i + 1;
   1090 		}
   1091 
   1092 		ncookies = nc;
   1093 		break;
   1094 	}
   1095 
   1096 	default:
   1097 		error = ENOTDIR;
   1098 		break;
   1099 	}
   1100 
   1101 	if (ap->a_ncookies) {
   1102 		if (error) {
   1103 			if (cookies)
   1104 				free(*ap->a_cookies, M_TEMP);
   1105 			*ap->a_ncookies = 0;
   1106 			*ap->a_cookies = NULL;
   1107 		} else
   1108 			*ap->a_ncookies = ncookies;
   1109 	}
   1110 	uio->uio_offset = i;
   1111 	return (error);
   1112 }
   1113 
   1114 /*
   1115  * readlink reads the link of `curlwp'
   1116  */
   1117 int
   1118 procfs_readlink(v)
   1119 	void *v;
   1120 {
   1121 	struct vop_readlink_args *ap = v;
   1122 	char buf[16];		/* should be enough */
   1123 	int len;
   1124 
   1125 	if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pcurproc))
   1126 		len = sprintf(buf, "%ld", (long)curproc->p_pid);
   1127 	else if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pself))
   1128 		len = sprintf(buf, "%s", "curlwp");
   1129 	else
   1130 		return (EINVAL);
   1131 
   1132 	return (uiomove((caddr_t)buf, len, ap->a_uio));
   1133 }
   1134 
   1135 /*
   1136  * convert decimal ascii to pid_t
   1137  */
   1138 static pid_t
   1139 atopid(b, len)
   1140 	const char *b;
   1141 	u_int len;
   1142 {
   1143 	pid_t p = 0;
   1144 
   1145 	while (len--) {
   1146 		char c = *b++;
   1147 		if (c < '0' || c > '9')
   1148 			return (NO_PID);
   1149 		p = 10 * p + (c - '0');
   1150 		if (p > PID_MAX)
   1151 			return (NO_PID);
   1152 	}
   1153 
   1154 	return (p);
   1155 }
   1156