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