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