165524Spendry /*
268232Spendry  * Copyright (c) 1993, 1995 Jan-Simon Pendry
368232Spendry  * Copyright (c) 1993, 1995
465808Sbostic  *	The Regents of the University of California.  All rights reserved.
565524Spendry  *
665524Spendry  * This code is derived from software contributed to Berkeley by
765524Spendry  * Jan-Simon Pendry.
865524Spendry  *
965524Spendry  * %sccs.include.redist.c%
1065524Spendry  *
11*69595Smckusick  *	@(#)procfs_vnops.c	8.18 (Berkeley) 05/21/95
1265524Spendry  *
1365524Spendry  * From:
1465524Spendry  *	$Id: procfs_vnops.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
1565524Spendry  */
1665524Spendry 
1765524Spendry /*
1865524Spendry  * procfs vnode interface
1965524Spendry  */
2065524Spendry 
2165524Spendry #include <sys/param.h>
2265524Spendry #include <sys/systm.h>
2365524Spendry #include <sys/time.h>
2465524Spendry #include <sys/kernel.h>
2565524Spendry #include <sys/file.h>
2665524Spendry #include <sys/proc.h>
2765524Spendry #include <sys/vnode.h>
2865524Spendry #include <sys/namei.h>
2965524Spendry #include <sys/malloc.h>
3065524Spendry #include <sys/dirent.h>
3165524Spendry #include <sys/resourcevar.h>
3267389Spendry #include <vm/vm.h>	/* for PAGE_SIZE */
3367389Spendry #include <machine/reg.h>
3465524Spendry #include <miscfs/procfs/procfs.h>
3565524Spendry 
3665524Spendry /*
3765524Spendry  * Vnode Operations.
3865524Spendry  *
3965524Spendry  */
4065524Spendry 
4165524Spendry /*
4265524Spendry  * This is a list of the valid names in the
4365524Spendry  * process-specific sub-directories.  It is
4465524Spendry  * used in procfs_lookup and procfs_readdir
4565524Spendry  */
4669351Smckusick struct proc_target {
4769351Smckusick 	u_char	pt_type;
4869351Smckusick 	u_char	pt_namlen;
4969351Smckusick 	char	*pt_name;
5069351Smckusick 	pfstype	pt_pfstype;
5169351Smckusick 	int	(*pt_valid) __P((struct proc *p));
5269351Smckusick } proc_targets[] = {
5365524Spendry #define N(s) sizeof(s)-1, s
5469351Smckusick 	/*	  name		type		validp */
5567389Spendry 	{ DT_DIR, N("."),	Pproc,		NULL },
5667389Spendry 	{ DT_DIR, N(".."),	Proot,		NULL },
5767389Spendry 	{ DT_REG, N("file"),	Pfile,		procfs_validfile },
5867389Spendry 	{ DT_REG, N("mem"),	Pmem,		NULL },
5967389Spendry 	{ DT_REG, N("regs"),	Pregs,		procfs_validregs },
6067389Spendry 	{ DT_REG, N("fpregs"),	Pfpregs,	procfs_validfpregs },
6167389Spendry 	{ DT_REG, N("ctl"),	Pctl,		NULL },
6267389Spendry 	{ DT_REG, N("status"),	Pstatus,	NULL },
6367389Spendry 	{ DT_REG, N("note"),	Pnote,		NULL },
6467389Spendry 	{ DT_REG, N("notepg"),	Pnotepg,	NULL },
6565524Spendry #undef N
6665524Spendry };
6769351Smckusick static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
6865524Spendry 
6965524Spendry static pid_t atopid __P((const char *, u_int));
7065524Spendry 
7165524Spendry /*
7265524Spendry  * set things up for doing i/o on
7365524Spendry  * the pfsnode (vp).  (vp) is locked
7465524Spendry  * on entry, and should be left locked
7565524Spendry  * on exit.
7665524Spendry  *
7765524Spendry  * for procfs we don't need to do anything
7865524Spendry  * in particular for i/o.  all that is done
7965524Spendry  * is to support exclusive open on process
8065524Spendry  * memory images.
8165524Spendry  */
8265524Spendry procfs_open(ap)
8367367Smckusick 	struct vop_open_args /* {
8467367Smckusick 		struct vnode *a_vp;
8567367Smckusick 		int  a_mode;
8667367Smckusick 		struct ucred *a_cred;
8767367Smckusick 		struct proc *a_p;
8867367Smckusick 	} */ *ap;
8965524Spendry {
9065524Spendry 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
9165524Spendry 
9265524Spendry 	switch (pfs->pfs_type) {
9365524Spendry 	case Pmem:
9465524Spendry 		if (PFIND(pfs->pfs_pid) == 0)
9565524Spendry 			return (ENOENT);	/* was ESRCH, jsp */
9665524Spendry 
9765524Spendry 		if ((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL) ||
9867389Spendry 		    (pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))
9965524Spendry 			return (EBUSY);
10065524Spendry 
10165524Spendry 		if (ap->a_mode & FWRITE)
10265524Spendry 			pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
10365524Spendry 
10465524Spendry 		return (0);
10565524Spendry 
10665524Spendry 	default:
10765524Spendry 		break;
10865524Spendry 	}
10965524Spendry 
11065524Spendry 	return (0);
11165524Spendry }
11265524Spendry 
11365524Spendry /*
11465524Spendry  * close the pfsnode (vp) after doing i/o.
11565524Spendry  * (vp) is not locked on entry or exit.
11665524Spendry  *
11765524Spendry  * nothing to do for procfs other than undo
11865524Spendry  * any exclusive open flag (see _open above).
11965524Spendry  */
12065524Spendry procfs_close(ap)
12167367Smckusick 	struct vop_close_args /* {
12267367Smckusick 		struct vnode *a_vp;
12367367Smckusick 		int  a_fflag;
12467367Smckusick 		struct ucred *a_cred;
12567367Smckusick 		struct proc *a_p;
12667367Smckusick 	} */ *ap;
12765524Spendry {
12865524Spendry 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
12965524Spendry 
13065524Spendry 	switch (pfs->pfs_type) {
13165524Spendry 	case Pmem:
13265524Spendry 		if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
13365524Spendry 			pfs->pfs_flags &= ~(FWRITE|O_EXCL);
13465524Spendry 		break;
13565524Spendry 	}
13665524Spendry 
13765524Spendry 	return (0);
13865524Spendry }
13965524Spendry 
14065524Spendry /*
14165524Spendry  * do an ioctl operation on pfsnode (vp).
14265524Spendry  * (vp) is not locked on entry or exit.
14365524Spendry  */
14465524Spendry procfs_ioctl(ap)
14567367Smckusick 	struct vop_ioctl_args /* {
14667367Smckusick 		struct vnode *a_vp;
14767367Smckusick 		int a_command;
14867367Smckusick 		caddr_t a_data;
14967367Smckusick 		int a_fflag;
15067367Smckusick 		struct ucred *a_cred;
15167367Smckusick 		struct proc *a_p;
15267367Smckusick 	} */ *ap;
15365524Spendry {
15465524Spendry 
15565524Spendry 	return (ENOTTY);
15665524Spendry }
15765524Spendry 
15865524Spendry /*
15965524Spendry  * do block mapping for pfsnode (vp).
16065524Spendry  * since we don't use the buffer cache
16165524Spendry  * for procfs this function should never
16265524Spendry  * be called.  in any case, it's not clear
16365524Spendry  * what part of the kernel ever makes use
16465524Spendry  * of this function.  for sanity, this is the
16565524Spendry  * usual no-op bmap, although returning
16665524Spendry  * (EIO) would be a reasonable alternative.
16765524Spendry  */
16865524Spendry procfs_bmap(ap)
16967367Smckusick 	struct vop_bmap_args /* {
17067367Smckusick 		struct vnode *a_vp;
17167367Smckusick 		daddr_t  a_bn;
17267367Smckusick 		struct vnode **a_vpp;
17367367Smckusick 		daddr_t *a_bnp;
17468232Spendry 		int *a_runp;
17567367Smckusick 	} */ *ap;
17665524Spendry {
17765524Spendry 
17865524Spendry 	if (ap->a_vpp != NULL)
17965524Spendry 		*ap->a_vpp = ap->a_vp;
18065524Spendry 	if (ap->a_bnp != NULL)
18165524Spendry 		*ap->a_bnp = ap->a_bn;
18268230Spendry 	if (ap->a_runp != NULL)
18368230Spendry 		*ap->a_runp = 0;
18465524Spendry 	return (0);
18565524Spendry }
18665524Spendry 
18765524Spendry /*
188*69595Smckusick  * procfs_inactive is called when the pfsnode
18965524Spendry  * is vrele'd and the reference count goes
19065524Spendry  * to zero.  (vp) will be on the vnode free
19165524Spendry  * list, so to get it back vget() must be
19265524Spendry  * used.
19365524Spendry  *
19465524Spendry  * for procfs, check if the process is still
19565524Spendry  * alive and if it isn't then just throw away
19669362Spendry  * the vnode by calling vgone().  this may
19765524Spendry  * be overkill and a waste of time since the
19865524Spendry  * chances are that the process will still be
19965524Spendry  * there and PFIND is not free.
20065524Spendry  *
201*69595Smckusick  * (vp) is locked on entry, but must be unlocked on exit.
20265524Spendry  */
20365524Spendry procfs_inactive(ap)
20467367Smckusick 	struct vop_inactive_args /* {
20567367Smckusick 		struct vnode *a_vp;
20667367Smckusick 	} */ *ap;
20765524Spendry {
208*69595Smckusick 	struct vnode *vp = ap->a_vp;
209*69595Smckusick 	struct pfsnode *pfs = VTOPFS(vp);
21065524Spendry 
211*69595Smckusick 	VOP_UNLOCK(vp, 0, ap->a_p);
21265524Spendry 	if (PFIND(pfs->pfs_pid) == 0)
213*69595Smckusick 		vgone(vp);
21465524Spendry 
21565524Spendry 	return (0);
21665524Spendry }
21765524Spendry 
21865524Spendry /*
21965524Spendry  * _reclaim is called when getnewvnode()
22065524Spendry  * wants to make use of an entry on the vnode
22165524Spendry  * free list.  at this time the filesystem needs
22265524Spendry  * to free any private data and remove the node
22365524Spendry  * from any private lists.
22465524Spendry  */
22565524Spendry procfs_reclaim(ap)
22667367Smckusick 	struct vop_reclaim_args /* {
22767367Smckusick 		struct vnode *a_vp;
22867367Smckusick 	} */ *ap;
22965524Spendry {
23065524Spendry 
23167389Spendry 	return (procfs_freevp(ap->a_vp));
23265524Spendry }
23365524Spendry 
23465524Spendry /*
23565743Spendry  * Return POSIX pathconf information applicable to special devices.
23665743Spendry  */
23765743Spendry procfs_pathconf(ap)
23865743Spendry 	struct vop_pathconf_args /* {
23965743Spendry 		struct vnode *a_vp;
24065743Spendry 		int a_name;
24165743Spendry 		int *a_retval;
24265743Spendry 	} */ *ap;
24365743Spendry {
24465743Spendry 
24565743Spendry 	switch (ap->a_name) {
24665743Spendry 	case _PC_LINK_MAX:
24765743Spendry 		*ap->a_retval = LINK_MAX;
24865743Spendry 		return (0);
24965743Spendry 	case _PC_MAX_CANON:
25065743Spendry 		*ap->a_retval = MAX_CANON;
25165743Spendry 		return (0);
25265743Spendry 	case _PC_MAX_INPUT:
25365743Spendry 		*ap->a_retval = MAX_INPUT;
25465743Spendry 		return (0);
25565743Spendry 	case _PC_PIPE_BUF:
25665743Spendry 		*ap->a_retval = PIPE_BUF;
25765743Spendry 		return (0);
25865743Spendry 	case _PC_CHOWN_RESTRICTED:
25965743Spendry 		*ap->a_retval = 1;
26065743Spendry 		return (0);
26165743Spendry 	case _PC_VDISABLE:
26265743Spendry 		*ap->a_retval = _POSIX_VDISABLE;
26365743Spendry 		return (0);
26465743Spendry 	default:
26565743Spendry 		return (EINVAL);
26665743Spendry 	}
26765743Spendry 	/* NOTREACHED */
26865743Spendry }
26965743Spendry 
27065743Spendry /*
27165524Spendry  * _print is used for debugging.
27265524Spendry  * just print a readable description
27365524Spendry  * of (vp).
27465524Spendry  */
27565524Spendry procfs_print(ap)
27667367Smckusick 	struct vop_print_args /* {
27767367Smckusick 		struct vnode *a_vp;
27867367Smckusick 	} */ *ap;
27965524Spendry {
28065524Spendry 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
28165524Spendry 
28267389Spendry 	printf("tag VT_PROCFS, type %s, pid %d, mode %x, flags %x\n",
28367389Spendry 	    pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
28465524Spendry }
28565524Spendry 
28665524Spendry /*
28765524Spendry  * _abortop is called when operations such as
28865524Spendry  * rename and create fail.  this entry is responsible
28965524Spendry  * for undoing any side-effects caused by the lookup.
29065524Spendry  * this will always include freeing the pathname buffer.
29165524Spendry  */
29265524Spendry procfs_abortop(ap)
29367367Smckusick 	struct vop_abortop_args /* {
29467367Smckusick 		struct vnode *a_dvp;
29567367Smckusick 		struct componentname *a_cnp;
29667367Smckusick 	} */ *ap;
29765524Spendry {
29865524Spendry 
29965524Spendry 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
30065524Spendry 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
30165524Spendry 	return (0);
30265524Spendry }
30365524Spendry 
30465524Spendry /*
30565524Spendry  * generic entry point for unsupported operations
30665524Spendry  */
procfs_badop()30765524Spendry procfs_badop()
30865524Spendry {
30965524Spendry 
31065524Spendry 	return (EIO);
31165524Spendry }
31265524Spendry 
31365524Spendry /*
31465524Spendry  * Invent attributes for pfsnode (vp) and store
31565524Spendry  * them in (vap).
31665524Spendry  * Directories lengths are returned as zero since
31765524Spendry  * any real length would require the genuine size
31865524Spendry  * to be computed, and nothing cares anyway.
31965524Spendry  *
32065524Spendry  * this is relatively minimal for procfs.
32165524Spendry  */
32265524Spendry procfs_getattr(ap)
32367367Smckusick 	struct vop_getattr_args /* {
32467367Smckusick 		struct vnode *a_vp;
32567367Smckusick 		struct vattr *a_vap;
32667367Smckusick 		struct ucred *a_cred;
32767367Smckusick 		struct proc *a_p;
32867367Smckusick 	} */ *ap;
32965524Spendry {
33065524Spendry 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
33165536Spendry 	struct vattr *vap = ap->a_vap;
33265524Spendry 	struct proc *procp;
33368174Scgd 	struct timeval tv;
33465524Spendry 	int error;
33565524Spendry 
33665524Spendry 	/* first check the process still exists */
33766025Spendry 	switch (pfs->pfs_type) {
33866025Spendry 	case Proot:
33967389Spendry 	case Pcurproc:
34066025Spendry 		procp = 0;
34166025Spendry 		break;
34265524Spendry 
34366025Spendry 	default:
34466025Spendry 		procp = PFIND(pfs->pfs_pid);
34566025Spendry 		if (procp == 0)
34666025Spendry 			return (ENOENT);
34766025Spendry 	}
34866025Spendry 
34965524Spendry 	error = 0;
35065524Spendry 
35165524Spendry 	/* start by zeroing out the attributes */
35265536Spendry 	VATTR_NULL(vap);
35365524Spendry 
35465524Spendry 	/* next do all the common fields */
35565536Spendry 	vap->va_type = ap->a_vp->v_type;
35665536Spendry 	vap->va_mode = pfs->pfs_mode;
35765536Spendry 	vap->va_fileid = pfs->pfs_fileno;
35865536Spendry 	vap->va_flags = 0;
35965536Spendry 	vap->va_blocksize = PAGE_SIZE;
36065536Spendry 	vap->va_bytes = vap->va_size = 0;
36165524Spendry 
36265524Spendry 	/*
36367389Spendry 	 * Make all times be current TOD.
36467389Spendry 	 * It would be possible to get the process start
36567389Spendry 	 * time from the p_stat structure, but there's
36667389Spendry 	 * no "file creation" time stamp anyway, and the
36767389Spendry 	 * p_stat structure is not addressible if u. gets
36867389Spendry 	 * swapped out for that process.
36967389Spendry 	 */
37068174Scgd 	microtime(&tv);
37168174Scgd 	TIMEVAL_TO_TIMESPEC(&tv, &vap->va_ctime);
37267389Spendry 	vap->va_atime = vap->va_mtime = vap->va_ctime;
37367389Spendry 
37467389Spendry 	/*
37566025Spendry 	 * If the process has exercised some setuid or setgid
37666025Spendry 	 * privilege, then rip away read/write permission so
37766025Spendry 	 * that only root can gain access.
37866025Spendry 	 */
37966025Spendry 	switch (pfs->pfs_type) {
38067389Spendry 	case Pmem:
38166025Spendry 	case Pregs:
38266025Spendry 	case Pfpregs:
38366025Spendry 		if (procp->p_flag & P_SUGID)
38466025Spendry 			vap->va_mode &= ~((VREAD|VWRITE)|
38566025Spendry 					  ((VREAD|VWRITE)>>3)|
38666025Spendry 					  ((VREAD|VWRITE)>>6));
38767389Spendry 	case Pctl:
38867389Spendry 	case Pstatus:
38967389Spendry 	case Pnote:
39067389Spendry 	case Pnotepg:
39167389Spendry 		vap->va_nlink = 1;
39267389Spendry 		vap->va_uid = procp->p_ucred->cr_uid;
39367389Spendry 		vap->va_gid = procp->p_ucred->cr_gid;
39466025Spendry 		break;
39566025Spendry 	}
39666025Spendry 
39766025Spendry 	/*
39865524Spendry 	 * now do the object specific fields
39965524Spendry 	 *
40065524Spendry 	 * The size could be set from struct reg, but it's hardly
40165524Spendry 	 * worth the trouble, and it puts some (potentially) machine
40265524Spendry 	 * dependent data into this machine-independent code.  If it
40365524Spendry 	 * becomes important then this function should break out into
40465524Spendry 	 * a per-file stat function in the corresponding .c file.
40565524Spendry 	 */
40665524Spendry 
40765524Spendry 	switch (pfs->pfs_type) {
40865524Spendry 	case Proot:
40967389Spendry 		/*
41067389Spendry 		 * Set nlink to 1 to tell fts(3) we don't actually know.
41167389Spendry 		 */
41267389Spendry 		vap->va_nlink = 1;
41365536Spendry 		vap->va_uid = 0;
41465536Spendry 		vap->va_gid = 0;
41567389Spendry 		vap->va_size = vap->va_bytes = DEV_BSIZE;
41665524Spendry 		break;
41765524Spendry 
41867389Spendry 	case Pcurproc: {
41967389Spendry 		char buf[16];		/* should be enough */
42067389Spendry 		vap->va_nlink = 1;
42167389Spendry 		vap->va_uid = 0;
42267389Spendry 		vap->va_gid = 0;
42367389Spendry 		vap->va_size = vap->va_bytes =
42467389Spendry 		    sprintf(buf, "%ld", (long)curproc->p_pid);
42567389Spendry 		break;
42667389Spendry 	}
42767389Spendry 
42865524Spendry 	case Pproc:
42965536Spendry 		vap->va_nlink = 2;
43065536Spendry 		vap->va_uid = procp->p_ucred->cr_uid;
43165536Spendry 		vap->va_gid = procp->p_ucred->cr_gid;
43267389Spendry 		vap->va_size = vap->va_bytes = DEV_BSIZE;
43365524Spendry 		break;
43465524Spendry 
43565524Spendry 	case Pfile:
43665524Spendry 		error = EOPNOTSUPP;
43765524Spendry 		break;
43865524Spendry 
43965524Spendry 	case Pmem:
44065536Spendry 		vap->va_bytes = vap->va_size =
44165524Spendry 			ctob(procp->p_vmspace->vm_tsize +
44265524Spendry 				    procp->p_vmspace->vm_dsize +
44365524Spendry 				    procp->p_vmspace->vm_ssize);
44465524Spendry 		break;
44565524Spendry 
44665524Spendry 	case Pregs:
44767389Spendry 		vap->va_bytes = vap->va_size = sizeof(struct reg);
44867389Spendry 		break;
44967389Spendry 
45065924Spendry 	case Pfpregs:
45167389Spendry 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
45267389Spendry 		break;
45367389Spendry 
45465524Spendry 	case Pctl:
45565524Spendry 	case Pstatus:
45665524Spendry 	case Pnote:
45765524Spendry 	case Pnotepg:
45865524Spendry 		break;
45965524Spendry 
46065524Spendry 	default:
46165524Spendry 		panic("procfs_getattr");
46265524Spendry 	}
46365524Spendry 
46465524Spendry 	return (error);
46565524Spendry }
46665524Spendry 
46765524Spendry procfs_setattr(ap)
46867367Smckusick 	struct vop_setattr_args /* {
46967367Smckusick 		struct vnode *a_vp;
47067367Smckusick 		struct vattr *a_vap;
47167367Smckusick 		struct ucred *a_cred;
47267367Smckusick 		struct proc *a_p;
47367367Smckusick 	} */ *ap;
47465524Spendry {
47565524Spendry 	/*
47665524Spendry 	 * just fake out attribute setting
47765524Spendry 	 * it's not good to generate an error
47865524Spendry 	 * return, otherwise things like creat()
47965524Spendry 	 * will fail when they try to set the
48065524Spendry 	 * file length to 0.  worse, this means
48165524Spendry 	 * that echo $note > /proc/$pid/note will fail.
48265524Spendry 	 */
48365524Spendry 
48465524Spendry 	return (0);
48565524Spendry }
48665524Spendry 
48765524Spendry /*
48865524Spendry  * implement access checking.
48965524Spendry  *
49065524Spendry  * something very similar to this code is duplicated
49165524Spendry  * throughout the 4bsd kernel and should be moved
49265524Spendry  * into kern/vfs_subr.c sometime.
49365524Spendry  *
49465524Spendry  * actually, the check for super-user is slightly
49565524Spendry  * broken since it will allow read access to write-only
49665524Spendry  * objects.  this doesn't cause any particular trouble
49765524Spendry  * but does mean that the i/o entry points need to check
49865524Spendry  * that the operation really does make sense.
49965524Spendry  */
50065524Spendry procfs_access(ap)
50167367Smckusick 	struct vop_access_args /* {
50267367Smckusick 		struct vnode *a_vp;
50367367Smckusick 		int a_mode;
50467367Smckusick 		struct ucred *a_cred;
50567367Smckusick 		struct proc *a_p;
50667367Smckusick 	} */ *ap;
50765524Spendry {
50865524Spendry 	struct vattr *vap;
50965524Spendry 	struct vattr vattr;
51065524Spendry 	int error;
51165524Spendry 
51265524Spendry 	/*
51365524Spendry 	 * If you're the super-user,
51465524Spendry 	 * you always get access.
51565524Spendry 	 */
51667389Spendry 	if (ap->a_cred->cr_uid == 0)
51765524Spendry 		return (0);
51867389Spendry 
51965524Spendry 	vap = &vattr;
52065524Spendry 	if (error = VOP_GETATTR(ap->a_vp, vap, ap->a_cred, ap->a_p))
52165524Spendry 		return (error);
52265524Spendry 
52365524Spendry 	/*
52465524Spendry 	 * Access check is based on only one of owner, group, public.
52565524Spendry 	 * If not owner, then check group. If not a member of the
52665524Spendry 	 * group, then check public access.
52765524Spendry 	 */
52865524Spendry 	if (ap->a_cred->cr_uid != vap->va_uid) {
52965524Spendry 		gid_t *gp;
53065524Spendry 		int i;
53165524Spendry 
53267389Spendry 		ap->a_mode >>= 3;
53365524Spendry 		gp = ap->a_cred->cr_groups;
53465524Spendry 		for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++)
53565524Spendry 			if (vap->va_gid == *gp)
53665524Spendry 				goto found;
53765524Spendry 		ap->a_mode >>= 3;
53865524Spendry found:
53965524Spendry 		;
54065524Spendry 	}
54165524Spendry 
54265524Spendry 	if ((vap->va_mode & ap->a_mode) == ap->a_mode)
54365524Spendry 		return (0);
54465524Spendry 
54565524Spendry 	return (EACCES);
54665524Spendry }
54765524Spendry 
54865524Spendry /*
54965524Spendry  * lookup.  this is incredibly complicated in the
55065524Spendry  * general case, however for most pseudo-filesystems
55165524Spendry  * very little needs to be done.
55265524Spendry  *
55365524Spendry  * unless you want to get a migraine, just make sure your
55465524Spendry  * filesystem doesn't do any locking of its own.  otherwise
55565524Spendry  * read and inwardly digest ufs_lookup().
55665524Spendry  */
55765524Spendry procfs_lookup(ap)
55867367Smckusick 	struct vop_lookup_args /* {
55967367Smckusick 		struct vnode * a_dvp;
56067367Smckusick 		struct vnode ** a_vpp;
56167367Smckusick 		struct componentname * a_cnp;
56267367Smckusick 	} */ *ap;
56365524Spendry {
56465524Spendry 	struct componentname *cnp = ap->a_cnp;
56565524Spendry 	struct vnode **vpp = ap->a_vpp;
56665524Spendry 	struct vnode *dvp = ap->a_dvp;
56765524Spendry 	char *pname = cnp->cn_nameptr;
56869442Smckusick 	struct proc *curp = cnp->cn_proc;
56965524Spendry 	int error = 0;
57069351Smckusick 	struct proc_target *pt;
57169351Smckusick 	struct vnode *fvp;
57265524Spendry 	pid_t pid;
57365524Spendry 	struct pfsnode *pfs;
57469351Smckusick 	struct proc *p;
57565524Spendry 	int i;
57665524Spendry 
57769351Smckusick 	*vpp = NULL;
57869351Smckusick 
57969351Smckusick 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
58069351Smckusick 		return (EROFS);
58169351Smckusick 
58265524Spendry 	if (cnp->cn_namelen == 1 && *pname == '.') {
58365524Spendry 		*vpp = dvp;
58465524Spendry 		VREF(dvp);
58569442Smckusick 		/* vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, curp); */
58665524Spendry 		return (0);
58765524Spendry 	}
58865524Spendry 
58965524Spendry 	pfs = VTOPFS(dvp);
59065524Spendry 	switch (pfs->pfs_type) {
59165524Spendry 	case Proot:
59265524Spendry 		if (cnp->cn_flags & ISDOTDOT)
59365524Spendry 			return (EIO);
59465524Spendry 
59565524Spendry 		if (CNEQ(cnp, "curproc", 7))
59667389Spendry 			return (procfs_allocvp(dvp->v_mount, vpp, 0, Pcurproc));
59767389Spendry 
59867389Spendry 		pid = atopid(pname, cnp->cn_namelen);
59965524Spendry 		if (pid == NO_PID)
60069351Smckusick 			break;
60165524Spendry 
60269351Smckusick 		p = PFIND(pid);
60369351Smckusick 		if (p == 0)
60469351Smckusick 			break;
60565524Spendry 
60667389Spendry 		return (procfs_allocvp(dvp->v_mount, vpp, pid, Pproc));
60765524Spendry 
60865524Spendry 	case Pproc:
60969351Smckusick 		if (cnp->cn_flags & ISDOTDOT)
61069351Smckusick 			return (procfs_root(dvp->v_mount, vpp));
61165524Spendry 
61269351Smckusick 		p = PFIND(pfs->pfs_pid);
61369351Smckusick 		if (p == 0)
61469351Smckusick 			break;
61565524Spendry 
61669351Smckusick 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
61769351Smckusick 			if (cnp->cn_namelen == pt->pt_namlen &&
61869351Smckusick 			    bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
61969351Smckusick 			    (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
62065524Spendry 				goto found;
62165524Spendry 		}
62269351Smckusick 		break;
62365524Spendry 
62465524Spendry 	found:
62569351Smckusick 		if (pt->pt_pfstype == Pfile) {
62669351Smckusick 			fvp = procfs_findtextvp(p);
62769351Smckusick 			/* We already checked that it exists. */
62869351Smckusick 			VREF(fvp);
62969442Smckusick 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, curp);
63069351Smckusick 			*vpp = fvp;
63167389Spendry 			return (0);
63265524Spendry 		}
63365524Spendry 
63467389Spendry 		return (procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
63569351Smckusick 		    pt->pt_pfstype));
63667389Spendry 
63765524Spendry 	default:
63865524Spendry 		return (ENOTDIR);
63965524Spendry 	}
64069351Smckusick 
64169351Smckusick 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
64265524Spendry }
64365524Spendry 
64467389Spendry int
procfs_validfile(p)64567389Spendry procfs_validfile(p)
64667389Spendry 	struct proc *p;
64767389Spendry {
64867389Spendry 
64967389Spendry 	return (procfs_findtextvp(p) != NULLVP);
65067389Spendry }
65167389Spendry 
65265524Spendry /*
65365524Spendry  * readdir returns directory entries from pfsnode (vp).
65465524Spendry  *
65565524Spendry  * the strategy here with procfs is to generate a single
65665524Spendry  * directory entry at a time (struct pfsdent) and then
65765524Spendry  * copy that out to userland using uiomove.  a more efficent
65865524Spendry  * though more complex implementation, would try to minimize
65965524Spendry  * the number of calls to uiomove().  for procfs, this is
66065524Spendry  * hardly worth the added code complexity.
66165524Spendry  *
66265524Spendry  * this should just be done through read()
66365524Spendry  */
66465524Spendry procfs_readdir(ap)
66567367Smckusick 	struct vop_readdir_args /* {
66667367Smckusick 		struct vnode *a_vp;
66767367Smckusick 		struct uio *a_uio;
66867367Smckusick 		struct ucred *a_cred;
66967367Smckusick 		int *a_eofflag;
67067367Smckusick 		u_long *a_cookies;
67167367Smckusick 		int a_ncookies;
67267367Smckusick 	} */ *ap;
67365524Spendry {
67465524Spendry 	struct uio *uio = ap->a_uio;
67565524Spendry 	struct pfsdent d;
67665524Spendry 	struct pfsdent *dp = &d;
67765524Spendry 	struct pfsnode *pfs;
67865524Spendry 	int error;
67965524Spendry 	int count;
68065524Spendry 	int i;
68165524Spendry 
68267367Smckusick 	/*
68367367Smckusick 	 * We don't allow exporting procfs mounts, and currently local
68467367Smckusick 	 * requests do not need cookies.
68567367Smckusick 	 */
68667367Smckusick 	if (ap->a_ncookies)
68767367Smckusick 		panic("procfs_readdir: not hungry");
68867367Smckusick 
68965524Spendry 	pfs = VTOPFS(ap->a_vp);
69065524Spendry 
69165524Spendry 	if (uio->uio_resid < UIO_MX)
69265524Spendry 		return (EINVAL);
69365524Spendry 	if (uio->uio_offset & (UIO_MX-1))
69465524Spendry 		return (EINVAL);
69565524Spendry 	if (uio->uio_offset < 0)
69665524Spendry 		return (EINVAL);
69765524Spendry 
69865524Spendry 	error = 0;
69965524Spendry 	count = 0;
70065524Spendry 	i = uio->uio_offset / UIO_MX;
70165524Spendry 
70265524Spendry 	switch (pfs->pfs_type) {
70365524Spendry 	/*
70465524Spendry 	 * this is for the process-specific sub-directories.
70565524Spendry 	 * all that is needed to is copy out all the entries
70665524Spendry 	 * from the procent[] table (top of this file).
70765524Spendry 	 */
70865524Spendry 	case Pproc: {
70969351Smckusick 		struct proc *p;
71069351Smckusick 		struct proc_target *pt;
71165524Spendry 
71269351Smckusick 		p = PFIND(pfs->pfs_pid);
71369351Smckusick 		if (p == NULL)
71469351Smckusick 			break;
71567389Spendry 
71669351Smckusick 		for (pt = &proc_targets[i];
71769351Smckusick 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
71869351Smckusick 			if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
71967389Spendry 				continue;
72065524Spendry 
72165524Spendry 			dp->d_reclen = UIO_MX;
72269351Smckusick 			dp->d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
72369351Smckusick 			dp->d_namlen = pt->pt_namlen;
72469351Smckusick 			bcopy(pt->pt_name, dp->d_name, pt->pt_namlen + 1);
72569351Smckusick 			dp->d_type = pt->pt_type;
72667389Spendry 
72767389Spendry 			if (error = uiomove((caddr_t)dp, UIO_MX, uio))
72865524Spendry 				break;
72965524Spendry 		}
73065524Spendry 
73165524Spendry 	    	break;
73265524Spendry 	    }
73365524Spendry 
73465524Spendry 	/*
73565524Spendry 	 * this is for the root of the procfs filesystem
73665524Spendry 	 * what is needed is a special entry for "curproc"
73765524Spendry 	 * followed by an entry for each process on allproc
73865524Spendry #ifdef PROCFS_ZOMBIE
73965524Spendry 	 * and zombproc.
74065524Spendry #endif
74165524Spendry 	 */
74265524Spendry 
74365524Spendry 	case Proot: {
74465524Spendry #ifdef PROCFS_ZOMBIE
74565524Spendry 		int doingzomb = 0;
74665524Spendry #endif
74767389Spendry 		int pcnt = 0;
74867721Smckusick 		volatile struct proc *p = allproc.lh_first;
74965524Spendry 
75067389Spendry 	again:
75167389Spendry 		for (; p && uio->uio_resid >= UIO_MX; i++, pcnt++) {
75265524Spendry 			bzero((char *) dp, UIO_MX);
75365524Spendry 			dp->d_reclen = UIO_MX;
75465524Spendry 
75565524Spendry 			switch (i) {
75667389Spendry 			case 0:		/* `.' */
75767389Spendry 			case 1:		/* `..' */
75867389Spendry 				dp->d_fileno = PROCFS_FILENO(0, Proot);
75967389Spendry 				dp->d_namlen = i + 1;
76067389Spendry 				bcopy("..", dp->d_name, dp->d_namlen);
76167389Spendry 				dp->d_name[i + 1] = '\0';
76267389Spendry 				dp->d_type = DT_DIR;
76365524Spendry 				break;
76465524Spendry 
76567389Spendry 			case 2:
76667389Spendry 				dp->d_fileno = PROCFS_FILENO(0, Pcurproc);
76767389Spendry 				dp->d_namlen = 7;
76867389Spendry 				bcopy("curproc", dp->d_name, 8);
76967389Spendry 				dp->d_type = DT_LNK;
77067389Spendry 				break;
77167389Spendry 
77265524Spendry 			default:
77367389Spendry 				while (pcnt < i) {
77467389Spendry 					pcnt++;
77567721Smckusick 					p = p->p_list.le_next;
77667389Spendry 					if (!p)
77767389Spendry 						goto done;
77865524Spendry 				}
77967389Spendry 				dp->d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
78067389Spendry 				dp->d_namlen = sprintf(dp->d_name, "%ld",
78167389Spendry 				    (long)p->p_pid);
78267389Spendry 				dp->d_type = DT_REG;
78367721Smckusick 				p = p->p_list.le_next;
78465524Spendry 				break;
78565524Spendry 			}
78667389Spendry 
78767389Spendry 			if (error = uiomove((caddr_t)dp, UIO_MX, uio))
78865524Spendry 				break;
78965524Spendry 		}
79067389Spendry 	done:
79165524Spendry 
79267389Spendry #ifdef PROCFS_ZOMBIE
79367389Spendry 		if (p == 0 && doingzomb == 0) {
79467389Spendry 			doingzomb = 1;
79567721Smckusick 			p = zombproc.lh_first;
79667389Spendry 			goto again;
79767389Spendry 		}
79867389Spendry #endif
79967389Spendry 
80065524Spendry 		break;
80165524Spendry 
80265524Spendry 	    }
80365524Spendry 
80465524Spendry 	default:
80565524Spendry 		error = ENOTDIR;
80665524Spendry 		break;
80765524Spendry 	}
80865524Spendry 
80965524Spendry 	uio->uio_offset = i * UIO_MX;
81065524Spendry 
81165524Spendry 	return (error);
81265524Spendry }
81365524Spendry 
81465524Spendry /*
81567389Spendry  * readlink reads the link of `curproc'
81667389Spendry  */
81767389Spendry procfs_readlink(ap)
81867389Spendry 	struct vop_readlink_args *ap;
81967389Spendry {
82067389Spendry 	struct uio *uio = ap->a_uio;
82167389Spendry 	char buf[16];		/* should be enough */
82267389Spendry 	int len;
82367389Spendry 
82467389Spendry 	if (VTOPFS(ap->a_vp)->pfs_fileno != PROCFS_FILENO(0, Pcurproc))
82567389Spendry 		return (EINVAL);
82667389Spendry 
82767389Spendry 	len = sprintf(buf, "%ld", (long)curproc->p_pid);
82867389Spendry 
82967389Spendry 	return (uiomove((caddr_t)buf, len, ap->a_uio));
83067389Spendry }
83167389Spendry 
83267389Spendry /*
83365524Spendry  * convert decimal ascii to pid_t
83465524Spendry  */
83565524Spendry static pid_t
atopid(b,len)83665524Spendry atopid(b, len)
83765524Spendry 	const char *b;
83865524Spendry 	u_int len;
83965524Spendry {
84065524Spendry 	pid_t p = 0;
84165524Spendry 
84265524Spendry 	while (len--) {
84365524Spendry 		char c = *b++;
84465524Spendry 		if (c < '0' || c > '9')
84565524Spendry 			return (NO_PID);
84665524Spendry 		p = 10 * p + (c - '0');
84765524Spendry 		if (p > PID_MAX)
84865524Spendry 			return (NO_PID);
84965524Spendry 	}
85065524Spendry 
85165524Spendry 	return (p);
85265524Spendry }
85365524Spendry 
85465524Spendry /*
85565524Spendry  * procfs vnode operations.
85665524Spendry  */
85765524Spendry int (**procfs_vnodeop_p)();
85865524Spendry struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
85965524Spendry 	{ &vop_default_desc, vn_default_error },
86065524Spendry 	{ &vop_lookup_desc, procfs_lookup },		/* lookup */
86165524Spendry 	{ &vop_create_desc, procfs_create },		/* create */
86265524Spendry 	{ &vop_mknod_desc, procfs_mknod },		/* mknod */
86365524Spendry 	{ &vop_open_desc, procfs_open },		/* open */
86465524Spendry 	{ &vop_close_desc, procfs_close },		/* close */
86565524Spendry 	{ &vop_access_desc, procfs_access },		/* access */
86665524Spendry 	{ &vop_getattr_desc, procfs_getattr },		/* getattr */
86765524Spendry 	{ &vop_setattr_desc, procfs_setattr },		/* setattr */
86865524Spendry 	{ &vop_read_desc, procfs_read },		/* read */
86965524Spendry 	{ &vop_write_desc, procfs_write },		/* write */
87065524Spendry 	{ &vop_ioctl_desc, procfs_ioctl },		/* ioctl */
87165524Spendry 	{ &vop_select_desc, procfs_select },		/* select */
87265524Spendry 	{ &vop_mmap_desc, procfs_mmap },		/* mmap */
87368732Smckusick 	{ &vop_revoke_desc, procfs_revoke },		/* revoke */
87465524Spendry 	{ &vop_fsync_desc, procfs_fsync },		/* fsync */
87565524Spendry 	{ &vop_seek_desc, procfs_seek },		/* seek */
87665524Spendry 	{ &vop_remove_desc, procfs_remove },		/* remove */
87765524Spendry 	{ &vop_link_desc, procfs_link },		/* link */
87865524Spendry 	{ &vop_rename_desc, procfs_rename },		/* rename */
87965524Spendry 	{ &vop_mkdir_desc, procfs_mkdir },		/* mkdir */
88065524Spendry 	{ &vop_rmdir_desc, procfs_rmdir },		/* rmdir */
88165524Spendry 	{ &vop_symlink_desc, procfs_symlink },		/* symlink */
88265524Spendry 	{ &vop_readdir_desc, procfs_readdir },		/* readdir */
88365524Spendry 	{ &vop_readlink_desc, procfs_readlink },	/* readlink */
88465524Spendry 	{ &vop_abortop_desc, procfs_abortop },		/* abortop */
88565524Spendry 	{ &vop_inactive_desc, procfs_inactive },	/* inactive */
88665524Spendry 	{ &vop_reclaim_desc, procfs_reclaim },		/* reclaim */
88765524Spendry 	{ &vop_lock_desc, procfs_lock },		/* lock */
88865524Spendry 	{ &vop_unlock_desc, procfs_unlock },		/* unlock */
88965524Spendry 	{ &vop_bmap_desc, procfs_bmap },		/* bmap */
89065524Spendry 	{ &vop_strategy_desc, procfs_strategy },	/* strategy */
89165524Spendry 	{ &vop_print_desc, procfs_print },		/* print */
89265524Spendry 	{ &vop_islocked_desc, procfs_islocked },	/* islocked */
89365524Spendry 	{ &vop_pathconf_desc, procfs_pathconf },	/* pathconf */
89465524Spendry 	{ &vop_advlock_desc, procfs_advlock },		/* advlock */
89565524Spendry 	{ &vop_blkatoff_desc, procfs_blkatoff },	/* blkatoff */
89665524Spendry 	{ &vop_valloc_desc, procfs_valloc },		/* valloc */
89765524Spendry 	{ &vop_vfree_desc, procfs_vfree },		/* vfree */
89865524Spendry 	{ &vop_truncate_desc, procfs_truncate },	/* truncate */
89965524Spendry 	{ &vop_update_desc, procfs_update },		/* update */
90065524Spendry 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
90165524Spendry };
90265524Spendry struct vnodeopv_desc procfs_vnodeop_opv_desc =
90365524Spendry 	{ &procfs_vnodeop_p, procfs_vnodeop_entries };
904