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