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