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