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