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