Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.1.1.1
      1      1.1    pk /*
      2  1.1.1.1  fvdl  * Copyright (c) 1993 Jan-Simon Pendry
      3  1.1.1.1  fvdl  * Copyright (c) 1993
      4  1.1.1.1  fvdl  *	The Regents of the University of California.  All rights reserved.
      5  1.1.1.1  fvdl  *
      6  1.1.1.1  fvdl  * This code is derived from software contributed to Berkeley by
      7  1.1.1.1  fvdl  * Jan-Simon Pendry.
      8  1.1.1.1  fvdl  *
      9  1.1.1.1  fvdl  * Redistribution and use in source and binary forms, with or without
     10  1.1.1.1  fvdl  * modification, are permitted provided that the following conditions
     11  1.1.1.1  fvdl  * are met:
     12  1.1.1.1  fvdl  * 1. Redistributions of source code must retain the above copyright
     13  1.1.1.1  fvdl  *    notice, this list of conditions and the following disclaimer.
     14  1.1.1.1  fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1.1.1  fvdl  *    notice, this list of conditions and the following disclaimer in the
     16  1.1.1.1  fvdl  *    documentation and/or other materials provided with the distribution.
     17  1.1.1.1  fvdl  * 3. All advertising materials mentioning features or use of this software
     18  1.1.1.1  fvdl  *    must display the following acknowledgement:
     19  1.1.1.1  fvdl  *	This product includes software developed by the University of
     20  1.1.1.1  fvdl  *	California, Berkeley and its contributors.
     21  1.1.1.1  fvdl  * 4. Neither the name of the University nor the names of its contributors
     22  1.1.1.1  fvdl  *    may be used to endorse or promote products derived from this software
     23  1.1.1.1  fvdl  *    without specific prior written permission.
     24  1.1.1.1  fvdl  *
     25  1.1.1.1  fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1.1.1  fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1.1.1  fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1.1.1  fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1.1.1  fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1.1.1  fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1.1.1  fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1.1.1  fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1.1.1  fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1.1.1  fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1.1.1  fvdl  * SUCH DAMAGE.
     36  1.1.1.1  fvdl  *
     37  1.1.1.1  fvdl  *	@(#)procfs_subr.c	8.4 (Berkeley) 1/27/94
     38  1.1.1.1  fvdl  *
     39  1.1.1.1  fvdl  * From:
     40  1.1.1.1  fvdl  *	$Id: procfs_subr.c,v 1.1.1.1 1998/03/01 02:10:01 fvdl Exp $
     41      1.1    pk  */
     42      1.1    pk 
     43  1.1.1.1  fvdl #include <sys/param.h>
     44  1.1.1.1  fvdl #include <sys/systm.h>
     45  1.1.1.1  fvdl #include <sys/time.h>
     46  1.1.1.1  fvdl #include <sys/kernel.h>
     47  1.1.1.1  fvdl #include <sys/proc.h>
     48  1.1.1.1  fvdl #include <sys/vnode.h>
     49  1.1.1.1  fvdl #include <sys/malloc.h>
     50  1.1.1.1  fvdl #include <miscfs/procfs/procfs.h>
     51      1.1    pk 
     52  1.1.1.1  fvdl static struct pfsnode *pfshead;
     53  1.1.1.1  fvdl static int pfsvplock;
     54      1.1    pk 
     55      1.1    pk /*
     56  1.1.1.1  fvdl  * allocate a pfsnode/vnode pair.  the vnode is
     57  1.1.1.1  fvdl  * referenced, but not locked.
     58  1.1.1.1  fvdl  *
     59  1.1.1.1  fvdl  * the pid, pfs_type, and mount point uniquely
     60  1.1.1.1  fvdl  * identify a pfsnode.  the mount point is needed
     61  1.1.1.1  fvdl  * because someone might mount this filesystem
     62  1.1.1.1  fvdl  * twice.
     63  1.1.1.1  fvdl  *
     64  1.1.1.1  fvdl  * all pfsnodes are maintained on a singly-linked
     65  1.1.1.1  fvdl  * list.  new nodes are only allocated when they cannot
     66  1.1.1.1  fvdl  * be found on this list.  entries on the list are
     67  1.1.1.1  fvdl  * removed when the vfs reclaim entry is called.
     68  1.1.1.1  fvdl  *
     69  1.1.1.1  fvdl  * a single lock is kept for the entire list.  this is
     70  1.1.1.1  fvdl  * needed because the getnewvnode() function can block
     71  1.1.1.1  fvdl  * waiting for a vnode to become free, in which case there
     72  1.1.1.1  fvdl  * may be more than one process trying to get the same
     73  1.1.1.1  fvdl  * vnode.  this lock is only taken if we are going to
     74  1.1.1.1  fvdl  * call getnewvnode, since the kernel itself is single-threaded.
     75  1.1.1.1  fvdl  *
     76  1.1.1.1  fvdl  * if an entry is found on the list, then call vget() to
     77  1.1.1.1  fvdl  * take a reference.  this is done because there may be
     78  1.1.1.1  fvdl  * zero references to it and so it needs to removed from
     79  1.1.1.1  fvdl  * the vnode free list.
     80      1.1    pk  */
     81      1.1    pk int
     82  1.1.1.1  fvdl procfs_allocvp(mp, vpp, pid, pfs_type)
     83  1.1.1.1  fvdl 	struct mount *mp;
     84  1.1.1.1  fvdl 	struct vnode **vpp;
     85  1.1.1.1  fvdl 	long pid;
     86  1.1.1.1  fvdl 	pfstype pfs_type;
     87      1.1    pk {
     88  1.1.1.1  fvdl 	int error;
     89  1.1.1.1  fvdl 	struct pfsnode *pfs;
     90  1.1.1.1  fvdl 	struct pfsnode **pp;
     91  1.1.1.1  fvdl 
     92  1.1.1.1  fvdl loop:
     93  1.1.1.1  fvdl 	for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
     94  1.1.1.1  fvdl 		if (pfs->pfs_pid == pid &&
     95  1.1.1.1  fvdl 		    pfs->pfs_type == pfs_type &&
     96  1.1.1.1  fvdl 		    PFSTOV(pfs)->v_mount == mp) {
     97  1.1.1.1  fvdl 			if (vget(pfs->pfs_vnode, 0))
     98  1.1.1.1  fvdl 				goto loop;
     99  1.1.1.1  fvdl 			*vpp = pfs->pfs_vnode;
    100  1.1.1.1  fvdl 			return (0);
    101      1.1    pk 		}
    102      1.1    pk 	}
    103      1.1    pk 
    104  1.1.1.1  fvdl 	/*
    105  1.1.1.1  fvdl 	 * otherwise lock the vp list while we call getnewvnode
    106  1.1.1.1  fvdl 	 * since that can block.
    107  1.1.1.1  fvdl 	 */
    108  1.1.1.1  fvdl 	if (pfsvplock & PROCFS_LOCKED) {
    109  1.1.1.1  fvdl 		pfsvplock |= PROCFS_WANT;
    110  1.1.1.1  fvdl 		sleep((caddr_t) &pfsvplock, PINOD);
    111  1.1.1.1  fvdl 		goto loop;
    112  1.1.1.1  fvdl 	}
    113  1.1.1.1  fvdl 	pfsvplock |= PROCFS_LOCKED;
    114      1.1    pk 
    115  1.1.1.1  fvdl 	error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp);
    116  1.1.1.1  fvdl 	if (error)
    117  1.1.1.1  fvdl 		goto out;
    118  1.1.1.1  fvdl 
    119  1.1.1.1  fvdl 	MALLOC((*vpp)->v_data, void *, sizeof(struct pfsnode),
    120  1.1.1.1  fvdl 		M_TEMP, M_WAITOK);
    121  1.1.1.1  fvdl 
    122  1.1.1.1  fvdl 	pfs = VTOPFS(*vpp);
    123  1.1.1.1  fvdl 	pfs->pfs_next = 0;
    124  1.1.1.1  fvdl 	pfs->pfs_pid = (pid_t) pid;
    125  1.1.1.1  fvdl 	pfs->pfs_type = pfs_type;
    126  1.1.1.1  fvdl 	pfs->pfs_vnode = *vpp;
    127  1.1.1.1  fvdl 	pfs->pfs_flags = 0;
    128  1.1.1.1  fvdl 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
    129  1.1.1.1  fvdl 
    130  1.1.1.1  fvdl 	switch (pfs_type) {
    131  1.1.1.1  fvdl 	case Proot:	/* /proc = dr-xr-xr-x */
    132  1.1.1.1  fvdl 		pfs->pfs_mode = (VREAD|VEXEC) |
    133  1.1.1.1  fvdl 				(VREAD|VEXEC) >> 3 |
    134  1.1.1.1  fvdl 				(VREAD|VEXEC) >> 6;
    135  1.1.1.1  fvdl 		break;
    136  1.1.1.1  fvdl 
    137  1.1.1.1  fvdl 	case Pproc:
    138  1.1.1.1  fvdl 		pfs->pfs_mode = (VREAD|VEXEC) |
    139  1.1.1.1  fvdl 				(VREAD|VEXEC) >> 3 |
    140  1.1.1.1  fvdl 				(VREAD|VEXEC) >> 6;
    141  1.1.1.1  fvdl 		break;
    142  1.1.1.1  fvdl 
    143  1.1.1.1  fvdl 	case Pfile:
    144  1.1.1.1  fvdl 		pfs->pfs_mode = (VREAD|VWRITE);
    145  1.1.1.1  fvdl 		break;
    146  1.1.1.1  fvdl 
    147  1.1.1.1  fvdl 	case Pmem:
    148  1.1.1.1  fvdl 		pfs->pfs_mode = (VREAD|VWRITE);
    149  1.1.1.1  fvdl 		break;
    150  1.1.1.1  fvdl 
    151  1.1.1.1  fvdl 	case Pregs:
    152  1.1.1.1  fvdl 		pfs->pfs_mode = (VREAD|VWRITE);
    153  1.1.1.1  fvdl 		break;
    154  1.1.1.1  fvdl 
    155  1.1.1.1  fvdl 	case Pfpregs:
    156  1.1.1.1  fvdl 		pfs->pfs_mode = (VREAD|VWRITE);
    157  1.1.1.1  fvdl 		break;
    158  1.1.1.1  fvdl 
    159  1.1.1.1  fvdl 	case Pctl:
    160  1.1.1.1  fvdl 		pfs->pfs_mode = (VWRITE);
    161  1.1.1.1  fvdl 		break;
    162  1.1.1.1  fvdl 
    163  1.1.1.1  fvdl 	case Pstatus:
    164  1.1.1.1  fvdl 		pfs->pfs_mode = (VREAD) |
    165  1.1.1.1  fvdl 				(VREAD >> 3) |
    166  1.1.1.1  fvdl 				(VREAD >> 6);
    167  1.1.1.1  fvdl 		break;
    168  1.1.1.1  fvdl 
    169  1.1.1.1  fvdl 	case Pnote:
    170  1.1.1.1  fvdl 		pfs->pfs_mode = (VWRITE);
    171  1.1.1.1  fvdl 		break;
    172  1.1.1.1  fvdl 
    173  1.1.1.1  fvdl 	case Pnotepg:
    174  1.1.1.1  fvdl 		pfs->pfs_mode = (VWRITE);
    175  1.1.1.1  fvdl 		break;
    176  1.1.1.1  fvdl 
    177  1.1.1.1  fvdl 	default:
    178  1.1.1.1  fvdl 		panic("procfs_allocvp");
    179      1.1    pk 	}
    180      1.1    pk 
    181  1.1.1.1  fvdl 	/* add to procfs vnode list */
    182  1.1.1.1  fvdl 	for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
    183  1.1.1.1  fvdl 		continue;
    184  1.1.1.1  fvdl 	*pp = pfs;
    185  1.1.1.1  fvdl 
    186  1.1.1.1  fvdl out:
    187  1.1.1.1  fvdl 	pfsvplock &= ~PROCFS_LOCKED;
    188  1.1.1.1  fvdl 
    189  1.1.1.1  fvdl 	if (pfsvplock & PROCFS_WANT) {
    190  1.1.1.1  fvdl 		pfsvplock &= ~PROCFS_WANT;
    191  1.1.1.1  fvdl 		wakeup((caddr_t) &pfsvplock);
    192  1.1.1.1  fvdl 	}
    193  1.1.1.1  fvdl 
    194  1.1.1.1  fvdl 	return (error);
    195      1.1    pk }
    196      1.1    pk 
    197      1.1    pk int
    198  1.1.1.1  fvdl procfs_freevp(vp)
    199  1.1.1.1  fvdl 	struct vnode *vp;
    200      1.1    pk {
    201  1.1.1.1  fvdl 	struct pfsnode **pfspp;
    202  1.1.1.1  fvdl 	struct pfsnode *pfs = VTOPFS(vp);
    203      1.1    pk 
    204  1.1.1.1  fvdl 	for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
    205  1.1.1.1  fvdl 		if (*pfspp == pfs) {
    206  1.1.1.1  fvdl 			*pfspp = pfs->pfs_next;
    207  1.1.1.1  fvdl 			break;
    208  1.1.1.1  fvdl 		}
    209  1.1.1.1  fvdl 	}
    210      1.1    pk 
    211  1.1.1.1  fvdl 	FREE(vp->v_data, M_TEMP);
    212  1.1.1.1  fvdl 	vp->v_data = 0;
    213  1.1.1.1  fvdl 	return (0);
    214      1.1    pk }
    215      1.1    pk 
    216  1.1.1.1  fvdl int
    217  1.1.1.1  fvdl procfs_rw(ap)
    218  1.1.1.1  fvdl 	struct vop_read_args *ap;
    219      1.1    pk {
    220  1.1.1.1  fvdl 	struct vnode *vp = ap->a_vp;
    221  1.1.1.1  fvdl 	struct uio *uio = ap->a_uio;
    222  1.1.1.1  fvdl 	struct proc *curp = uio->uio_procp;
    223  1.1.1.1  fvdl 	struct pfsnode *pfs = VTOPFS(vp);
    224  1.1.1.1  fvdl 	struct proc *p;
    225  1.1.1.1  fvdl 
    226  1.1.1.1  fvdl 	p = PFIND(pfs->pfs_pid);
    227  1.1.1.1  fvdl 	if (p == 0)
    228      1.1    pk 		return (EINVAL);
    229      1.1    pk 
    230  1.1.1.1  fvdl 	switch (pfs->pfs_type) {
    231  1.1.1.1  fvdl 	case Pnote:
    232  1.1.1.1  fvdl 	case Pnotepg:
    233  1.1.1.1  fvdl 		return (procfs_donote(curp, p, pfs, uio));
    234      1.1    pk 
    235  1.1.1.1  fvdl 	case Pregs:
    236  1.1.1.1  fvdl 		return (procfs_doregs(curp, p, pfs, uio));
    237      1.1    pk 
    238  1.1.1.1  fvdl 	case Pfpregs:
    239  1.1.1.1  fvdl 		return (procfs_dofpregs(curp, p, pfs, uio));
    240  1.1.1.1  fvdl 
    241  1.1.1.1  fvdl 	case Pctl:
    242  1.1.1.1  fvdl 		return (procfs_doctl(curp, p, pfs, uio));
    243  1.1.1.1  fvdl 
    244  1.1.1.1  fvdl 	case Pstatus:
    245  1.1.1.1  fvdl 		return (procfs_dostatus(curp, p, pfs, uio));
    246  1.1.1.1  fvdl 
    247  1.1.1.1  fvdl 	case Pmem:
    248  1.1.1.1  fvdl 		return (procfs_domem(curp, p, pfs, uio));
    249  1.1.1.1  fvdl 
    250  1.1.1.1  fvdl 	default:
    251  1.1.1.1  fvdl 		return (EOPNOTSUPP);
    252  1.1.1.1  fvdl 	}
    253      1.1    pk }
    254      1.1    pk 
    255  1.1.1.1  fvdl /*
    256  1.1.1.1  fvdl  * Get a string from userland into (buf).  Strip a trailing
    257  1.1.1.1  fvdl  * nl character (to allow easy access from the shell).
    258  1.1.1.1  fvdl  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    259  1.1.1.1  fvdl  * will automatically add a nul char at the end.
    260  1.1.1.1  fvdl  *
    261  1.1.1.1  fvdl  * Returns 0 on success or the following errors
    262  1.1.1.1  fvdl  *
    263  1.1.1.1  fvdl  * EINVAL:    file offset is non-zero.
    264  1.1.1.1  fvdl  * EMSGSIZE:  message is longer than kernel buffer
    265  1.1.1.1  fvdl  * EFAULT:    user i/o buffer is not addressable
    266  1.1.1.1  fvdl  */
    267      1.1    pk int
    268  1.1.1.1  fvdl vfs_getuserstr(uio, buf, buflenp)
    269  1.1.1.1  fvdl 	struct uio *uio;
    270  1.1.1.1  fvdl 	char *buf;
    271  1.1.1.1  fvdl 	int *buflenp;
    272      1.1    pk {
    273  1.1.1.1  fvdl 	int xlen;
    274  1.1.1.1  fvdl 	int error;
    275  1.1.1.1  fvdl 
    276  1.1.1.1  fvdl 	if (uio->uio_offset != 0)
    277  1.1.1.1  fvdl 		return (EINVAL);
    278      1.1    pk 
    279  1.1.1.1  fvdl 	xlen = *buflenp;
    280      1.1    pk 
    281  1.1.1.1  fvdl 	/* must be able to read the whole string in one go */
    282  1.1.1.1  fvdl 	if (xlen < uio->uio_resid)
    283  1.1.1.1  fvdl 		return (EMSGSIZE);
    284  1.1.1.1  fvdl 	xlen = uio->uio_resid;
    285  1.1.1.1  fvdl 
    286  1.1.1.1  fvdl 	error = uiomove(buf, xlen, uio);
    287  1.1.1.1  fvdl 	if (error)
    288  1.1.1.1  fvdl 		return (error);
    289  1.1.1.1  fvdl 
    290  1.1.1.1  fvdl 	/* allow multiple writes without seeks */
    291  1.1.1.1  fvdl 	uio->uio_offset = 0;
    292  1.1.1.1  fvdl 
    293  1.1.1.1  fvdl 	/* cleanup string and remove trailing newline */
    294  1.1.1.1  fvdl 	buf[xlen] = '\0';
    295  1.1.1.1  fvdl 	xlen = strlen(buf);
    296  1.1.1.1  fvdl 	if (xlen > 0 && buf[xlen-1] == '\n')
    297  1.1.1.1  fvdl 		buf[--xlen] = '\0';
    298  1.1.1.1  fvdl 	*buflenp = xlen;
    299      1.1    pk 
    300  1.1.1.1  fvdl 	return (0);
    301      1.1    pk }
    302      1.1    pk 
    303  1.1.1.1  fvdl vfs_namemap_t *
    304  1.1.1.1  fvdl vfs_findname(nm, buf, buflen)
    305  1.1.1.1  fvdl 	vfs_namemap_t *nm;
    306  1.1.1.1  fvdl 	char *buf;
    307  1.1.1.1  fvdl 	int buflen;
    308      1.1    pk {
    309  1.1.1.1  fvdl 	for (; nm->nm_name; nm++)
    310  1.1.1.1  fvdl 		if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0)
    311  1.1.1.1  fvdl 			return (nm);
    312  1.1.1.1  fvdl 
    313  1.1.1.1  fvdl 	return (0);
    314      1.1    pk }
    315