165523Spendry /*
265523Spendry * Copyright (c) 1993 Jan-Simon Pendry
365808Sbostic * Copyright (c) 1993
465808Sbostic * The Regents of the University of California. All rights reserved.
565523Spendry *
665523Spendry * This code is derived from software contributed to Berkeley by
765523Spendry * Jan-Simon Pendry.
865523Spendry *
965523Spendry * %sccs.include.redist.c%
1065523Spendry *
11*69442Smckusick * @(#)procfs_subr.c 8.6 (Berkeley) 05/14/95
1265523Spendry *
1365523Spendry * From:
1465523Spendry * $Id: procfs_subr.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
1565523Spendry */
1665523Spendry
1765523Spendry #include <sys/param.h>
1865523Spendry #include <sys/systm.h>
1965523Spendry #include <sys/time.h>
2065523Spendry #include <sys/kernel.h>
2165523Spendry #include <sys/proc.h>
2265523Spendry #include <sys/vnode.h>
2365523Spendry #include <sys/malloc.h>
2465523Spendry #include <miscfs/procfs/procfs.h>
2565523Spendry
2665523Spendry static struct pfsnode *pfshead;
2765523Spendry static int pfsvplock;
2865523Spendry
2965523Spendry /*
3065523Spendry * allocate a pfsnode/vnode pair. the vnode is
3165523Spendry * referenced, but not locked.
3265523Spendry *
3365523Spendry * the pid, pfs_type, and mount point uniquely
3465523Spendry * identify a pfsnode. the mount point is needed
3565523Spendry * because someone might mount this filesystem
3665523Spendry * twice.
3765523Spendry *
3865523Spendry * all pfsnodes are maintained on a singly-linked
3965523Spendry * list. new nodes are only allocated when they cannot
4065523Spendry * be found on this list. entries on the list are
4165523Spendry * removed when the vfs reclaim entry is called.
4265523Spendry *
4365523Spendry * a single lock is kept for the entire list. this is
4465523Spendry * needed because the getnewvnode() function can block
4565523Spendry * waiting for a vnode to become free, in which case there
4665523Spendry * may be more than one process trying to get the same
4765523Spendry * vnode. this lock is only taken if we are going to
4865523Spendry * call getnewvnode, since the kernel itself is single-threaded.
4965523Spendry *
5065523Spendry * if an entry is found on the list, then call vget() to
5165523Spendry * take a reference. this is done because there may be
5265523Spendry * zero references to it and so it needs to removed from
5365523Spendry * the vnode free list.
5465523Spendry */
5565523Spendry int
procfs_allocvp(mp,vpp,pid,pfs_type)5665523Spendry procfs_allocvp(mp, vpp, pid, pfs_type)
5765523Spendry struct mount *mp;
5865523Spendry struct vnode **vpp;
5965523Spendry long pid;
6065523Spendry pfstype pfs_type;
6165523Spendry {
62*69442Smckusick struct proc *p = curproc; /* XXX */
6365523Spendry struct pfsnode *pfs;
6467389Spendry struct vnode *vp;
6565523Spendry struct pfsnode **pp;
6667389Spendry int error;
6765523Spendry
6865523Spendry loop:
6965523Spendry for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
7067389Spendry vp = PFSTOV(pfs);
7165523Spendry if (pfs->pfs_pid == pid &&
7265523Spendry pfs->pfs_type == pfs_type &&
7367389Spendry vp->v_mount == mp) {
74*69442Smckusick if (vget(vp, 0, p))
7565523Spendry goto loop;
7667389Spendry *vpp = vp;
7765523Spendry return (0);
7865523Spendry }
7965523Spendry }
8065523Spendry
8165523Spendry /*
8265523Spendry * otherwise lock the vp list while we call getnewvnode
8365523Spendry * since that can block.
8465523Spendry */
8565523Spendry if (pfsvplock & PROCFS_LOCKED) {
8665523Spendry pfsvplock |= PROCFS_WANT;
8765523Spendry sleep((caddr_t) &pfsvplock, PINOD);
8865523Spendry goto loop;
8965523Spendry }
9065523Spendry pfsvplock |= PROCFS_LOCKED;
9165523Spendry
9267389Spendry if (error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp))
9365523Spendry goto out;
9467389Spendry vp = *vpp;
9565523Spendry
9667389Spendry MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
9767389Spendry vp->v_data = pfs;
9865523Spendry
9965523Spendry pfs->pfs_next = 0;
10065523Spendry pfs->pfs_pid = (pid_t) pid;
10165523Spendry pfs->pfs_type = pfs_type;
10267389Spendry pfs->pfs_vnode = vp;
10365523Spendry pfs->pfs_flags = 0;
10465523Spendry pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
10565523Spendry
10665523Spendry switch (pfs_type) {
10765523Spendry case Proot: /* /proc = dr-xr-xr-x */
10865523Spendry pfs->pfs_mode = (VREAD|VEXEC) |
10965523Spendry (VREAD|VEXEC) >> 3 |
11065523Spendry (VREAD|VEXEC) >> 6;
11167389Spendry vp->v_type = VDIR;
11267389Spendry vp->v_flag = VROOT;
11365523Spendry break;
11465523Spendry
11567389Spendry case Pcurproc: /* /proc/curproc = lr--r--r-- */
11667389Spendry pfs->pfs_mode = (VREAD) |
11767389Spendry (VREAD >> 3) |
11867389Spendry (VREAD >> 6);
11967389Spendry vp->v_type = VLNK;
12067389Spendry break;
12167389Spendry
12265523Spendry case Pproc:
12365523Spendry pfs->pfs_mode = (VREAD|VEXEC) |
12465523Spendry (VREAD|VEXEC) >> 3 |
12565523Spendry (VREAD|VEXEC) >> 6;
12667389Spendry vp->v_type = VDIR;
12765523Spendry break;
12865523Spendry
12965523Spendry case Pfile:
13065523Spendry case Pmem:
13165523Spendry case Pregs:
13265924Spendry case Pfpregs:
13365924Spendry pfs->pfs_mode = (VREAD|VWRITE);
13467389Spendry vp->v_type = VREG;
13565924Spendry break;
13665924Spendry
13765523Spendry case Pctl:
13867389Spendry case Pnote:
13967389Spendry case Pnotepg:
14065523Spendry pfs->pfs_mode = (VWRITE);
14167389Spendry vp->v_type = VREG;
14265523Spendry break;
14365523Spendry
14465523Spendry case Pstatus:
14565523Spendry pfs->pfs_mode = (VREAD) |
14665523Spendry (VREAD >> 3) |
14765523Spendry (VREAD >> 6);
14867389Spendry vp->v_type = VREG;
14965523Spendry break;
15065523Spendry
15165523Spendry default:
15265523Spendry panic("procfs_allocvp");
15365523Spendry }
15465523Spendry
15565523Spendry /* add to procfs vnode list */
15665523Spendry for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
15765523Spendry continue;
15865523Spendry *pp = pfs;
15965523Spendry
16065523Spendry out:
16165523Spendry pfsvplock &= ~PROCFS_LOCKED;
16265523Spendry
16365523Spendry if (pfsvplock & PROCFS_WANT) {
16465523Spendry pfsvplock &= ~PROCFS_WANT;
16565523Spendry wakeup((caddr_t) &pfsvplock);
16665523Spendry }
16765523Spendry
16865523Spendry return (error);
16965523Spendry }
17065523Spendry
17165523Spendry int
procfs_freevp(vp)17265523Spendry procfs_freevp(vp)
17365523Spendry struct vnode *vp;
17465523Spendry {
17565523Spendry struct pfsnode **pfspp;
17665523Spendry struct pfsnode *pfs = VTOPFS(vp);
17765523Spendry
17865523Spendry for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
17965523Spendry if (*pfspp == pfs) {
18065523Spendry *pfspp = pfs->pfs_next;
18165523Spendry break;
18265523Spendry }
18365523Spendry }
18465523Spendry
18565523Spendry FREE(vp->v_data, M_TEMP);
18665523Spendry vp->v_data = 0;
18765523Spendry return (0);
18865523Spendry }
18965523Spendry
19065523Spendry int
procfs_rw(ap)19165523Spendry procfs_rw(ap)
19265523Spendry struct vop_read_args *ap;
19365523Spendry {
19465523Spendry struct vnode *vp = ap->a_vp;
19565523Spendry struct uio *uio = ap->a_uio;
19665523Spendry struct proc *curp = uio->uio_procp;
19765523Spendry struct pfsnode *pfs = VTOPFS(vp);
19865523Spendry struct proc *p;
19965523Spendry
20065523Spendry p = PFIND(pfs->pfs_pid);
20165523Spendry if (p == 0)
20265523Spendry return (EINVAL);
20365523Spendry
20465523Spendry switch (pfs->pfs_type) {
20565523Spendry case Pnote:
20665523Spendry case Pnotepg:
20765523Spendry return (procfs_donote(curp, p, pfs, uio));
20865523Spendry
20965523Spendry case Pregs:
21065523Spendry return (procfs_doregs(curp, p, pfs, uio));
21165523Spendry
21265924Spendry case Pfpregs:
21365924Spendry return (procfs_dofpregs(curp, p, pfs, uio));
21465924Spendry
21565523Spendry case Pctl:
21665523Spendry return (procfs_doctl(curp, p, pfs, uio));
21765523Spendry
21865523Spendry case Pstatus:
21965523Spendry return (procfs_dostatus(curp, p, pfs, uio));
22065523Spendry
22165523Spendry case Pmem:
22265523Spendry return (procfs_domem(curp, p, pfs, uio));
22365523Spendry
22465523Spendry default:
22565523Spendry return (EOPNOTSUPP);
22665523Spendry }
22765523Spendry }
22865523Spendry
22965523Spendry /*
23065523Spendry * Get a string from userland into (buf). Strip a trailing
23165523Spendry * nl character (to allow easy access from the shell).
23265523Spendry * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
23365523Spendry * will automatically add a nul char at the end.
23465523Spendry *
23565523Spendry * Returns 0 on success or the following errors
23665523Spendry *
23765523Spendry * EINVAL: file offset is non-zero.
23865523Spendry * EMSGSIZE: message is longer than kernel buffer
23965523Spendry * EFAULT: user i/o buffer is not addressable
24065523Spendry */
24165523Spendry int
vfs_getuserstr(uio,buf,buflenp)24265523Spendry vfs_getuserstr(uio, buf, buflenp)
24365523Spendry struct uio *uio;
24465523Spendry char *buf;
24565523Spendry int *buflenp;
24665523Spendry {
24765523Spendry int xlen;
24865523Spendry int error;
24965523Spendry
25065523Spendry if (uio->uio_offset != 0)
25165523Spendry return (EINVAL);
25265523Spendry
25365523Spendry xlen = *buflenp;
25465523Spendry
25565523Spendry /* must be able to read the whole string in one go */
25665523Spendry if (xlen < uio->uio_resid)
25765523Spendry return (EMSGSIZE);
25865523Spendry xlen = uio->uio_resid;
25965523Spendry
26067389Spendry if (error = uiomove(buf, xlen, uio))
26165523Spendry return (error);
26265523Spendry
26365523Spendry /* allow multiple writes without seeks */
26465523Spendry uio->uio_offset = 0;
26565523Spendry
26665523Spendry /* cleanup string and remove trailing newline */
26765523Spendry buf[xlen] = '\0';
26865523Spendry xlen = strlen(buf);
26965523Spendry if (xlen > 0 && buf[xlen-1] == '\n')
27065523Spendry buf[--xlen] = '\0';
27165523Spendry *buflenp = xlen;
27265523Spendry
27365523Spendry return (0);
27465523Spendry }
27565523Spendry
27665523Spendry vfs_namemap_t *
vfs_findname(nm,buf,buflen)27765523Spendry vfs_findname(nm, buf, buflen)
27865523Spendry vfs_namemap_t *nm;
27965523Spendry char *buf;
28065523Spendry int buflen;
28165523Spendry {
28267389Spendry
28365523Spendry for (; nm->nm_name; nm++)
28465523Spendry if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0)
28565523Spendry return (nm);
28665523Spendry
28765523Spendry return (0);
28865523Spendry }
289