Home | History | Annotate | Line # | Download | only in procfs
procfs_vnops.c revision 1.37
      1 /*	$NetBSD: procfs_vnops.c,v 1.37 1996/02/09 22:40:54 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Jan-Simon Pendry
      5  * Copyright (c) 1993
      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.8 (Berkeley) 6/15/94
     40  */
     41 
     42 /*
     43  * procfs vnode interface
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/time.h>
     49 #include <sys/kernel.h>
     50 #include <sys/file.h>
     51 #include <sys/proc.h>
     52 #include <sys/vnode.h>
     53 #include <sys/namei.h>
     54 #include <sys/malloc.h>
     55 #include <sys/dirent.h>
     56 #include <sys/resourcevar.h>
     57 #include <sys/ptrace.h>
     58 #include <vm/vm.h>	/* for PAGE_SIZE */
     59 #include <machine/reg.h>
     60 #include <miscfs/procfs/procfs.h>
     61 
     62 /*
     63  * Vnode Operations.
     64  *
     65  */
     66 
     67 /*
     68  * This is a list of the valid names in the
     69  * process-specific sub-directories.  It is
     70  * used in procfs_lookup and procfs_readdir
     71  */
     72 struct proc_target {
     73 	u_char	pt_type;
     74 	u_char	pt_namlen;
     75 	char	*pt_name;
     76 	pfstype	pt_pfstype;
     77 	int	(*pt_valid) __P((struct proc *p));
     78 } proc_targets[] = {
     79 #define N(s) sizeof(s)-1, s
     80 	/*	  name		type		validp */
     81 	{ DT_DIR, N("."),	Pproc,		NULL },
     82 	{ DT_DIR, N(".."),	Proot,		NULL },
     83 	{ DT_REG, N("file"),	Pfile,		procfs_validfile },
     84 	{ DT_REG, N("mem"),	Pmem,		NULL },
     85 	{ DT_REG, N("regs"),	Pregs,		procfs_validregs },
     86 	{ DT_REG, N("fpregs"),	Pfpregs,	procfs_validfpregs },
     87 	{ DT_REG, N("ctl"),	Pctl,		NULL },
     88 	{ DT_REG, N("status"),	Pstatus,	NULL },
     89 	{ DT_REG, N("note"),	Pnote,		NULL },
     90 	{ DT_REG, N("notepg"),	Pnotepg,	NULL },
     91 #undef N
     92 };
     93 static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
     94 
     95 static pid_t atopid __P((const char *, u_int));
     96 
     97 /*
     98  * Prototypes for procfs vnode ops
     99  */
    100 int	procfs_badop	__P((void *));
    101 int	procfs_rw	__P((void *));
    102 int	procfs_lookup	__P((void *));
    103 #define	procfs_create	procfs_badop
    104 #define	procfs_mknod	procfs_badop
    105 int	procfs_open	__P((void *));
    106 int	procfs_close	__P((void *));
    107 int	procfs_access	__P((void *));
    108 int	procfs_getattr	__P((void *));
    109 int	procfs_setattr	__P((void *));
    110 #define	procfs_read	procfs_rw
    111 #define	procfs_write	procfs_rw
    112 int	procfs_ioctl	__P((void *));
    113 #define	procfs_select	procfs_badop
    114 #define	procfs_mmap	procfs_badop
    115 #define	procfs_fsync	procfs_badop
    116 #define	procfs_seek	procfs_badop
    117 #define	procfs_remove	procfs_badop
    118 int	procfs_link	__P((void *));
    119 #define	procfs_rename	procfs_badop
    120 #define	procfs_mkdir	procfs_badop
    121 #define	procfs_rmdir	procfs_badop
    122 int	procfs_symlink	__P((void *));
    123 int	procfs_readdir	__P((void *));
    124 int	procfs_readlink	__P((void *));
    125 int	procfs_abortop	__P((void *));
    126 int	procfs_inactive	__P((void *));
    127 int	procfs_reclaim	__P((void *));
    128 #define	procfs_lock	nullop
    129 #define	procfs_unlock	nullop
    130 int	procfs_bmap	__P((void *));
    131 #define	procfs_strategy	procfs_badop
    132 int	procfs_print	__P((void *));
    133 int	procfs_pathconf	__P((void *));
    134 #define	procfs_islocked	nullop
    135 #define	procfs_advlock	procfs_badop
    136 #define	procfs_blkatoff	procfs_badop
    137 #define	procfs_valloc	procfs_badop
    138 #define	procfs_vfree	nullop
    139 #define	procfs_truncate	procfs_badop
    140 #define	procfs_update	nullop
    141 
    142 static pid_t atopid __P((const char *, u_int));
    143 
    144 /*
    145  * procfs vnode operations.
    146  */
    147 int (**procfs_vnodeop_p) __P((void *));
    148 struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
    149 	{ &vop_default_desc, vn_default_error },
    150 	{ &vop_lookup_desc, procfs_lookup },		/* lookup */
    151 	{ &vop_create_desc, procfs_create },		/* create */
    152 	{ &vop_mknod_desc, procfs_mknod },		/* mknod */
    153 	{ &vop_open_desc, procfs_open },		/* open */
    154 	{ &vop_close_desc, procfs_close },		/* close */
    155 	{ &vop_access_desc, procfs_access },		/* access */
    156 	{ &vop_getattr_desc, procfs_getattr },		/* getattr */
    157 	{ &vop_setattr_desc, procfs_setattr },		/* setattr */
    158 	{ &vop_read_desc, procfs_read },		/* read */
    159 	{ &vop_write_desc, procfs_write },		/* write */
    160 	{ &vop_ioctl_desc, procfs_ioctl },		/* ioctl */
    161 	{ &vop_select_desc, procfs_select },		/* select */
    162 	{ &vop_mmap_desc, procfs_mmap },		/* mmap */
    163 	{ &vop_fsync_desc, procfs_fsync },		/* fsync */
    164 	{ &vop_seek_desc, procfs_seek },		/* seek */
    165 	{ &vop_remove_desc, procfs_remove },		/* remove */
    166 	{ &vop_link_desc, procfs_link },		/* link */
    167 	{ &vop_rename_desc, procfs_rename },		/* rename */
    168 	{ &vop_mkdir_desc, procfs_mkdir },		/* mkdir */
    169 	{ &vop_rmdir_desc, procfs_rmdir },		/* rmdir */
    170 	{ &vop_symlink_desc, procfs_symlink },		/* symlink */
    171 	{ &vop_readdir_desc, procfs_readdir },		/* readdir */
    172 	{ &vop_readlink_desc, procfs_readlink },	/* readlink */
    173 	{ &vop_abortop_desc, procfs_abortop },		/* abortop */
    174 	{ &vop_inactive_desc, procfs_inactive },	/* inactive */
    175 	{ &vop_reclaim_desc, procfs_reclaim },		/* reclaim */
    176 	{ &vop_lock_desc, procfs_lock },		/* lock */
    177 	{ &vop_unlock_desc, procfs_unlock },		/* unlock */
    178 	{ &vop_bmap_desc, procfs_bmap },		/* bmap */
    179 	{ &vop_strategy_desc, procfs_strategy },	/* strategy */
    180 	{ &vop_print_desc, procfs_print },		/* print */
    181 	{ &vop_islocked_desc, procfs_islocked },	/* islocked */
    182 	{ &vop_pathconf_desc, procfs_pathconf },	/* pathconf */
    183 	{ &vop_advlock_desc, procfs_advlock },		/* advlock */
    184 	{ &vop_blkatoff_desc, procfs_blkatoff },	/* blkatoff */
    185 	{ &vop_valloc_desc, procfs_valloc },		/* valloc */
    186 	{ &vop_vfree_desc, procfs_vfree },		/* vfree */
    187 	{ &vop_truncate_desc, procfs_truncate },	/* truncate */
    188 	{ &vop_update_desc, procfs_update },		/* update */
    189 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    190 };
    191 struct vnodeopv_desc procfs_vnodeop_opv_desc =
    192 	{ &procfs_vnodeop_p, procfs_vnodeop_entries };
    193 /*
    194  * set things up for doing i/o on
    195  * the pfsnode (vp).  (vp) is locked
    196  * on entry, and should be left locked
    197  * on exit.
    198  *
    199  * for procfs we don't need to do anything
    200  * in particular for i/o.  all that is done
    201  * is to support exclusive open on process
    202  * memory images.
    203  */
    204 int
    205 procfs_open(v)
    206 	void *v;
    207 {
    208 	struct vop_open_args /* {
    209 		struct vnode *a_vp;
    210 		int  a_mode;
    211 		struct ucred *a_cred;
    212 		struct proc *a_p;
    213 	} */ *ap = v;
    214 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    215 
    216 	switch (pfs->pfs_type) {
    217 	case Pmem:
    218 		if (PFIND(pfs->pfs_pid) == 0)
    219 			return (ENOENT);	/* was ESRCH, jsp */
    220 
    221 		if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
    222 		    ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
    223 			return (EBUSY);
    224 
    225 		if (ap->a_mode & FWRITE)
    226 			pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
    227 
    228 		return (0);
    229 
    230 	default:
    231 		break;
    232 	}
    233 
    234 	return (0);
    235 }
    236 
    237 /*
    238  * close the pfsnode (vp) after doing i/o.
    239  * (vp) is not locked on entry or exit.
    240  *
    241  * nothing to do for procfs other than undo
    242  * any exclusive open flag (see _open above).
    243  */
    244 int
    245 procfs_close(v)
    246 	void *v;
    247 {
    248 	struct vop_close_args /* {
    249 		struct vnode *a_vp;
    250 		int  a_fflag;
    251 		struct ucred *a_cred;
    252 		struct proc *a_p;
    253 	} */ *ap = v;
    254 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    255 
    256 	switch (pfs->pfs_type) {
    257 	case Pmem:
    258 		if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
    259 			pfs->pfs_flags &= ~(FWRITE|O_EXCL);
    260 		break;
    261 	case Pctl:
    262 	case Pstatus:
    263 	case Pnotepg:
    264 	case Pnote:
    265 	case Proot:
    266 	case Pcurproc:
    267 	case Pproc:
    268 	case Pfile:
    269 	case Pregs:
    270 	case Pfpregs:
    271 		break;
    272 	}
    273 
    274 	return (0);
    275 }
    276 
    277 /*
    278  * do an ioctl operation on pfsnode (vp).
    279  * (vp) is not locked on entry or exit.
    280  */
    281 /*ARGSUSED*/
    282 int
    283 procfs_ioctl(v)
    284 	void *v;
    285 {
    286 
    287 	return (ENOTTY);
    288 }
    289 
    290 /*
    291  * do block mapping for pfsnode (vp).
    292  * since we don't use the buffer cache
    293  * for procfs this function should never
    294  * be called.  in any case, it's not clear
    295  * what part of the kernel ever makes use
    296  * of this function.  for sanity, this is the
    297  * usual no-op bmap, although returning
    298  * (EIO) would be a reasonable alternative.
    299  */
    300 int
    301 procfs_bmap(v)
    302 	void *v;
    303 {
    304 	struct vop_bmap_args /* {
    305 		struct vnode *a_vp;
    306 		daddr_t  a_bn;
    307 		struct vnode **a_vpp;
    308 		daddr_t *a_bnp;
    309 	} */ *ap = v;
    310 
    311 	if (ap->a_vpp != NULL)
    312 		*ap->a_vpp = ap->a_vp;
    313 	if (ap->a_bnp != NULL)
    314 		*ap->a_bnp = ap->a_bn;
    315 	return (0);
    316 }
    317 
    318 /*
    319  * _inactive is called when the pfsnode
    320  * is vrele'd and the reference count goes
    321  * to zero.  (vp) will be on the vnode free
    322  * list, so to get it back vget() must be
    323  * used.
    324  *
    325  * for procfs, check if the process is still
    326  * alive and if it isn't then just throw away
    327  * the vnode by calling vgone().  this may
    328  * be overkill and a waste of time since the
    329  * chances are that the process will still be
    330  * there and PFIND is not free.
    331  *
    332  * (vp) is not locked on entry or exit.
    333  */
    334 int
    335 procfs_inactive(v)
    336 	void *v;
    337 {
    338 	struct vop_inactive_args /* {
    339 		struct vnode *a_vp;
    340 	} */ *ap = v;
    341 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    342 
    343 	if (PFIND(pfs->pfs_pid) == 0)
    344 		vgone(ap->a_vp);
    345 
    346 	return (0);
    347 }
    348 
    349 /*
    350  * _reclaim is called when getnewvnode()
    351  * wants to make use of an entry on the vnode
    352  * free list.  at this time the filesystem needs
    353  * to free any private data and remove the node
    354  * from any private lists.
    355  */
    356 int
    357 procfs_reclaim(v)
    358 	void *v;
    359 {
    360 	struct vop_reclaim_args /* {
    361 		struct vnode *a_vp;
    362 	} */ *ap = v;
    363 
    364 	return (procfs_freevp(ap->a_vp));
    365 }
    366 
    367 /*
    368  * Return POSIX pathconf information applicable to special devices.
    369  */
    370 int
    371 procfs_pathconf(v)
    372 	void *v;
    373 {
    374 	struct vop_pathconf_args /* {
    375 		struct vnode *a_vp;
    376 		int a_name;
    377 		register_t *a_retval;
    378 	} */ *ap = v;
    379 
    380 	switch (ap->a_name) {
    381 	case _PC_LINK_MAX:
    382 		*ap->a_retval = LINK_MAX;
    383 		return (0);
    384 	case _PC_MAX_CANON:
    385 		*ap->a_retval = MAX_CANON;
    386 		return (0);
    387 	case _PC_MAX_INPUT:
    388 		*ap->a_retval = MAX_INPUT;
    389 		return (0);
    390 	case _PC_PIPE_BUF:
    391 		*ap->a_retval = PIPE_BUF;
    392 		return (0);
    393 	case _PC_CHOWN_RESTRICTED:
    394 		*ap->a_retval = 1;
    395 		return (0);
    396 	case _PC_VDISABLE:
    397 		*ap->a_retval = _POSIX_VDISABLE;
    398 		return (0);
    399 	default:
    400 		return (EINVAL);
    401 	}
    402 	/* NOTREACHED */
    403 }
    404 
    405 /*
    406  * _print is used for debugging.
    407  * just print a readable description
    408  * of (vp).
    409  */
    410 int
    411 procfs_print(v)
    412 	void *v;
    413 {
    414 	struct vop_print_args /* {
    415 		struct vnode *a_vp;
    416 	} */ *ap = v;
    417 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    418 
    419 	printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %x\n",
    420 	    pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
    421 	return 0;
    422 }
    423 
    424 int
    425 procfs_link(v)
    426 	void *v;
    427 {
    428 	struct vop_link_args /* {
    429 		struct vnode *a_dvp;
    430 		struct vnode *a_vp;
    431 		struct componentname *a_cnp;
    432 	} */ *ap = v;
    433 
    434 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    435 	vput(ap->a_dvp);
    436 	return (EROFS);
    437 }
    438 
    439 int
    440 procfs_symlink(v)
    441 	void *v;
    442 {
    443 	struct vop_symlink_args /* {
    444 		struct vnode *a_dvp;
    445 		struct vnode **a_vpp;
    446 		struct componentname *a_cnp;
    447 		struct vattr *a_vap;
    448 		char *a_target;
    449 	} */ *ap = v;
    450 
    451 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    452 	vput(ap->a_dvp);
    453 	return (EROFS);
    454 }
    455 
    456 /*
    457  * _abortop is called when operations such as
    458  * rename and create fail.  this entry is responsible
    459  * for undoing any side-effects caused by the lookup.
    460  * this will always include freeing the pathname buffer.
    461  */
    462 int
    463 procfs_abortop(v)
    464 	void *v;
    465 {
    466 	struct vop_abortop_args /* {
    467 		struct vnode *a_dvp;
    468 		struct componentname *a_cnp;
    469 	} */ *ap = v;
    470 
    471 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    472 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
    473 	return (0);
    474 }
    475 
    476 /*
    477  * generic entry point for unsupported operations
    478  */
    479 /*ARGSUSED*/
    480 int
    481 procfs_badop(v)
    482 	void *v;
    483 {
    484 
    485 	return (EIO);
    486 }
    487 
    488 /*
    489  * Invent attributes for pfsnode (vp) and store
    490  * them in (vap).
    491  * Directories lengths are returned as zero since
    492  * any real length would require the genuine size
    493  * to be computed, and nothing cares anyway.
    494  *
    495  * this is relatively minimal for procfs.
    496  */
    497 int
    498 procfs_getattr(v)
    499 	void *v;
    500 {
    501 	struct vop_getattr_args /* {
    502 		struct vnode *a_vp;
    503 		struct vattr *a_vap;
    504 		struct ucred *a_cred;
    505 		struct proc *a_p;
    506 	} */ *ap = v;
    507 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    508 	struct vattr *vap = ap->a_vap;
    509 	struct proc *procp;
    510 	struct timeval tv;
    511 	int error;
    512 
    513 	/* first check the process still exists */
    514 	switch (pfs->pfs_type) {
    515 	case Proot:
    516 	case Pcurproc:
    517 		procp = 0;
    518 		break;
    519 
    520 	default:
    521 		procp = PFIND(pfs->pfs_pid);
    522 		if (procp == 0)
    523 			return (ENOENT);
    524 	}
    525 
    526 	error = 0;
    527 
    528 	/* start by zeroing out the attributes */
    529 	VATTR_NULL(vap);
    530 
    531 	/* next do all the common fields */
    532 	vap->va_type = ap->a_vp->v_type;
    533 	vap->va_mode = pfs->pfs_mode;
    534 	vap->va_fileid = pfs->pfs_fileno;
    535 	vap->va_flags = 0;
    536 	vap->va_blocksize = PAGE_SIZE;
    537 	vap->va_bytes = vap->va_size = 0;
    538 
    539 	/*
    540 	 * Make all times be current TOD.
    541 	 * It would be possible to get the process start
    542 	 * time from the p_stat structure, but there's
    543 	 * no "file creation" time stamp anyway, and the
    544 	 * p_stat structure is not addressible if u. gets
    545 	 * swapped out for that process.
    546 	 */
    547 	microtime(&tv);
    548 	TIMEVAL_TO_TIMESPEC(&tv, &vap->va_ctime);
    549 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    550 
    551 	/*
    552 	 * If the process has exercised some setuid or setgid
    553 	 * privilege, then rip away read/write permission so
    554 	 * that only root can gain access.
    555 	 */
    556 	switch (pfs->pfs_type) {
    557 	case Pmem:
    558 	case Pregs:
    559 	case Pfpregs:
    560 		if (procp->p_flag & P_SUGID)
    561 			vap->va_mode &= ~((VREAD|VWRITE)|
    562 					  ((VREAD|VWRITE)>>3)|
    563 					  ((VREAD|VWRITE)>>6));
    564 	case Pctl:
    565 	case Pstatus:
    566 	case Pnote:
    567 	case Pnotepg:
    568 		vap->va_nlink = 1;
    569 		vap->va_uid = procp->p_ucred->cr_uid;
    570 		vap->va_gid = procp->p_ucred->cr_gid;
    571 		break;
    572 	case Pproc:
    573 	case Pfile:
    574 	case Proot:
    575 	case Pcurproc:
    576 		break;
    577 	}
    578 
    579 	/*
    580 	 * now do the object specific fields
    581 	 *
    582 	 * The size could be set from struct reg, but it's hardly
    583 	 * worth the trouble, and it puts some (potentially) machine
    584 	 * dependent data into this machine-independent code.  If it
    585 	 * becomes important then this function should break out into
    586 	 * a per-file stat function in the corresponding .c file.
    587 	 */
    588 
    589 	switch (pfs->pfs_type) {
    590 	case Proot:
    591 		/*
    592 		 * Set nlink to 1 to tell fts(3) we don't actually know.
    593 		 */
    594 		vap->va_nlink = 1;
    595 		vap->va_uid = 0;
    596 		vap->va_gid = 0;
    597 		vap->va_size = vap->va_bytes = DEV_BSIZE;
    598 		break;
    599 
    600 	case Pcurproc: {
    601 		char buf[16];		/* should be enough */
    602 		vap->va_nlink = 1;
    603 		vap->va_uid = 0;
    604 		vap->va_gid = 0;
    605 		vap->va_size = vap->va_bytes =
    606 		    sprintf(buf, "%ld", (long)curproc->p_pid);
    607 		break;
    608 	}
    609 
    610 	case Pproc:
    611 		vap->va_nlink = 2;
    612 		vap->va_uid = procp->p_ucred->cr_uid;
    613 		vap->va_gid = procp->p_ucred->cr_gid;
    614 		vap->va_size = vap->va_bytes = DEV_BSIZE;
    615 		break;
    616 
    617 	case Pfile:
    618 		error = EOPNOTSUPP;
    619 		break;
    620 
    621 	case Pmem:
    622 		vap->va_bytes = vap->va_size =
    623 			ctob(procp->p_vmspace->vm_tsize +
    624 				    procp->p_vmspace->vm_dsize +
    625 				    procp->p_vmspace->vm_ssize);
    626 		break;
    627 
    628 #if defined(PT_GETREGS) || defined(PT_SETREGS)
    629 	case Pregs:
    630 		vap->va_bytes = vap->va_size = sizeof(struct reg);
    631 		break;
    632 #endif
    633 
    634 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
    635 	case Pfpregs:
    636 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
    637 		break;
    638 #endif
    639 
    640 	case Pctl:
    641 	case Pstatus:
    642 	case Pnote:
    643 	case Pnotepg:
    644 		break;
    645 
    646 	default:
    647 		panic("procfs_getattr");
    648 	}
    649 
    650 	return (error);
    651 }
    652 
    653 /*ARGSUSED*/
    654 int
    655 procfs_setattr(v)
    656 	void *v;
    657 {
    658 	/*
    659 	 * just fake out attribute setting
    660 	 * it's not good to generate an error
    661 	 * return, otherwise things like creat()
    662 	 * will fail when they try to set the
    663 	 * file length to 0.  worse, this means
    664 	 * that echo $note > /proc/$pid/note will fail.
    665 	 */
    666 
    667 	return (0);
    668 }
    669 
    670 /*
    671  * implement access checking.
    672  *
    673  * actually, the check for super-user is slightly
    674  * broken since it will allow read access to write-only
    675  * objects.  this doesn't cause any particular trouble
    676  * but does mean that the i/o entry points need to check
    677  * that the operation really does make sense.
    678  */
    679 int
    680 procfs_access(v)
    681 	void *v;
    682 {
    683 	struct vop_access_args /* {
    684 		struct vnode *a_vp;
    685 		int a_mode;
    686 		struct ucred *a_cred;
    687 		struct proc *a_p;
    688 	} */ *ap = v;
    689 	struct vattr va;
    690 	int error;
    691 
    692 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
    693 		return (error);
    694 
    695 	return (vaccess(va.va_mode, va.va_uid, va.va_gid, ap->a_mode,
    696 			ap->a_cred));
    697 }
    698 
    699 /*
    700  * lookup.  this is incredibly complicated in the
    701  * general case, however for most pseudo-filesystems
    702  * very little needs to be done.
    703  *
    704  * unless you want to get a migraine, just make sure your
    705  * filesystem doesn't do any locking of its own.  otherwise
    706  * read and inwardly digest ufs_lookup().
    707  */
    708 int
    709 procfs_lookup(v)
    710 	void *v;
    711 {
    712 	struct vop_lookup_args /* {
    713 		struct vnode * a_dvp;
    714 		struct vnode ** a_vpp;
    715 		struct componentname * a_cnp;
    716 	} */ *ap = v;
    717 	struct componentname *cnp = ap->a_cnp;
    718 	struct vnode **vpp = ap->a_vpp;
    719 	struct vnode *dvp = ap->a_dvp;
    720 	char *pname = cnp->cn_nameptr;
    721 	struct proc_target *pt;
    722 	struct vnode *fvp;
    723 	pid_t pid;
    724 	struct pfsnode *pfs;
    725 	struct proc *p;
    726 	int i;
    727 
    728 	*vpp = NULL;
    729 
    730 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    731 		return (EROFS);
    732 
    733 	if (cnp->cn_namelen == 1 && *pname == '.') {
    734 		*vpp = dvp;
    735 		VREF(dvp);
    736 		/*VOP_LOCK(dvp);*/
    737 		return (0);
    738 	}
    739 
    740 	pfs = VTOPFS(dvp);
    741 	switch (pfs->pfs_type) {
    742 	case Proot:
    743 		if (cnp->cn_flags & ISDOTDOT)
    744 			return (EIO);
    745 
    746 		if (CNEQ(cnp, "curproc", 7))
    747 			return (procfs_allocvp(dvp->v_mount, vpp, 0, Pcurproc));
    748 
    749 		pid = atopid(pname, cnp->cn_namelen);
    750 		if (pid == NO_PID)
    751 			break;
    752 
    753 		p = PFIND(pid);
    754 		if (p == 0)
    755 			break;
    756 
    757 		return (procfs_allocvp(dvp->v_mount, vpp, pid, Pproc));
    758 
    759 	case Pproc:
    760 		if (cnp->cn_flags & ISDOTDOT)
    761 			return (procfs_root(dvp->v_mount, vpp));
    762 
    763 		p = PFIND(pfs->pfs_pid);
    764 		if (p == 0)
    765 			break;
    766 
    767 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
    768 			if (cnp->cn_namelen == pt->pt_namlen &&
    769 			    bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
    770 			    (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
    771 				goto found;
    772 		}
    773 		break;
    774 
    775 	found:
    776 		if (pt->pt_pfstype == Pfile) {
    777 			fvp = procfs_findtextvp(p);
    778 			/* We already checked that it exists. */
    779 			VREF(fvp);
    780 			VOP_LOCK(fvp);
    781 			*vpp = fvp;
    782 			return (0);
    783 		}
    784 
    785 		return (procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
    786 		    pt->pt_pfstype));
    787 
    788 	default:
    789 		return (ENOTDIR);
    790 	}
    791 
    792 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    793 }
    794 
    795 int
    796 procfs_validfile(p)
    797 	struct proc *p;
    798 {
    799 
    800 	return (procfs_findtextvp(p) != NULLVP);
    801 }
    802 
    803 /*
    804  * readdir returns directory entries from pfsnode (vp).
    805  *
    806  * the strategy here with procfs is to generate a single
    807  * directory entry at a time (struct dirent) and then
    808  * copy that out to userland using uiomove.  a more efficent
    809  * though more complex implementation, would try to minimize
    810  * the number of calls to uiomove().  for procfs, this is
    811  * hardly worth the added code complexity.
    812  *
    813  * this should just be done through read()
    814  */
    815 int
    816 procfs_readdir(v)
    817 	void *v;
    818 {
    819 	struct vop_readdir_args /* {
    820 		struct vnode *a_vp;
    821 		struct uio *a_uio;
    822 		struct ucred *a_cred;
    823 		int *a_eofflag;
    824 		u_long *a_cookies;
    825 		int a_ncookies;
    826 	} */ *ap = v;
    827 	struct uio *uio = ap->a_uio;
    828 	struct dirent d;
    829 	struct pfsnode *pfs;
    830 	int i;
    831 	int error;
    832 	u_long *cookies = ap->a_cookies;
    833 	int ncookies = ap->a_ncookies;
    834 
    835 	pfs = VTOPFS(ap->a_vp);
    836 
    837 	if (uio->uio_resid < UIO_MX)
    838 		return (EINVAL);
    839 	if (uio->uio_offset < 0)
    840 		return (EINVAL);
    841 
    842 	error = 0;
    843 	i = uio->uio_offset;
    844 	bzero((caddr_t)&d, UIO_MX);
    845 	d.d_reclen = UIO_MX;
    846 
    847 	switch (pfs->pfs_type) {
    848 	/*
    849 	 * this is for the process-specific sub-directories.
    850 	 * all that is needed to is copy out all the entries
    851 	 * from the procent[] table (top of this file).
    852 	 */
    853 	case Pproc: {
    854 		struct proc *p;
    855 		struct proc_target *pt;
    856 
    857 		p = PFIND(pfs->pfs_pid);
    858 		if (p == NULL)
    859 			break;
    860 
    861 		for (pt = &proc_targets[i];
    862 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
    863 			if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
    864 				continue;
    865 
    866 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
    867 			d.d_namlen = pt->pt_namlen;
    868 			bcopy(pt->pt_name, d.d_name, pt->pt_namlen + 1);
    869 			d.d_type = pt->pt_type;
    870 
    871 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    872 				break;
    873 			if (ncookies-- > 0)
    874 				*cookies++ = i + 1;
    875 		}
    876 
    877 	    	break;
    878 	}
    879 
    880 	/*
    881 	 * this is for the root of the procfs filesystem
    882 	 * what is needed is a special entry for "curproc"
    883 	 * followed by an entry for each process on allproc
    884 #ifdef PROCFS_ZOMBIE
    885 	 * and zombproc.
    886 #endif
    887 	 */
    888 
    889 	case Proot: {
    890 #ifdef PROCFS_ZOMBIE
    891 		int doingzomb = 0;
    892 #endif
    893 		int pcnt = i;
    894 		volatile struct proc *p = allproc.lh_first;
    895 
    896 		if (pcnt > 3)
    897 			pcnt = 3;
    898 #ifdef PROCFS_ZOMBIE
    899 	again:
    900 #endif
    901 		for (; p && uio->uio_resid >= UIO_MX; i++, pcnt++) {
    902 			switch (i) {
    903 			case 0:		/* `.' */
    904 			case 1:		/* `..' */
    905 				d.d_fileno = PROCFS_FILENO(0, Proot);
    906 				d.d_namlen = i + 1;
    907 				bcopy("..", d.d_name, d.d_namlen);
    908 				d.d_name[i + 1] = '\0';
    909 				d.d_type = DT_DIR;
    910 				break;
    911 
    912 			case 2:
    913 				d.d_fileno = PROCFS_FILENO(0, Pcurproc);
    914 				d.d_namlen = 7;
    915 				bcopy("curproc", d.d_name, 8);
    916 				d.d_type = DT_LNK;
    917 				break;
    918 
    919 			default:
    920 				while (pcnt < i) {
    921 					pcnt++;
    922 					p = p->p_list.le_next;
    923 					if (!p)
    924 						goto done;
    925 				}
    926 				d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
    927 				d.d_namlen = sprintf(d.d_name, "%ld",
    928 				    (long)p->p_pid);
    929 				d.d_type = DT_REG;
    930 				p = p->p_list.le_next;
    931 				break;
    932 			}
    933 
    934 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    935 				break;
    936 			if (ncookies-- > 0)
    937 				*cookies++ = i + 1;
    938 		}
    939 	done:
    940 
    941 #ifdef PROCFS_ZOMBIE
    942 		if (p == 0 && doingzomb == 0) {
    943 			doingzomb = 1;
    944 			p = zombproc.lh_first;
    945 			goto again;
    946 		}
    947 #endif
    948 
    949 		break;
    950 
    951 	}
    952 
    953 	default:
    954 		error = ENOTDIR;
    955 		break;
    956 	}
    957 
    958 	uio->uio_offset = i;
    959 	return (error);
    960 }
    961 
    962 /*
    963  * readlink reads the link of `curproc'
    964  */
    965 int
    966 procfs_readlink(v)
    967 	void *v;
    968 {
    969 	struct vop_readlink_args *ap = v;
    970 	char buf[16];		/* should be enough */
    971 	int len;
    972 
    973 	if (VTOPFS(ap->a_vp)->pfs_fileno != PROCFS_FILENO(0, Pcurproc))
    974 		return (EINVAL);
    975 
    976 	len = sprintf(buf, "%ld", (long)curproc->p_pid);
    977 
    978 	return (uiomove((caddr_t)buf, len, ap->a_uio));
    979 }
    980 
    981 /*
    982  * convert decimal ascii to pid_t
    983  */
    984 static pid_t
    985 atopid(b, len)
    986 	const char *b;
    987 	u_int len;
    988 {
    989 	pid_t p = 0;
    990 
    991 	while (len--) {
    992 		char c = *b++;
    993 		if (c < '0' || c > '9')
    994 			return (NO_PID);
    995 		p = 10 * p + (c - '0');
    996 		if (p > PID_MAX)
    997 			return (NO_PID);
    998 	}
    999 
   1000 	return (p);
   1001 }
   1002