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