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