Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.64.2.2
      1  1.64.2.2      yamt /*	$NetBSD: procfs_subr.c,v 1.64.2.2 2006/12/30 20:50:18 yamt Exp $	*/
      2      1.57       agc 
      3      1.57       agc /*
      4      1.57       agc  * Copyright (c) 1993
      5      1.57       agc  *	The Regents of the University of California.  All rights reserved.
      6      1.57       agc  *
      7      1.57       agc  * This code is derived from software contributed to Berkeley by
      8      1.57       agc  * Jan-Simon Pendry.
      9      1.57       agc  *
     10      1.57       agc  * Redistribution and use in source and binary forms, with or without
     11      1.57       agc  * modification, are permitted provided that the following conditions
     12      1.57       agc  * are met:
     13      1.57       agc  * 1. Redistributions of source code must retain the above copyright
     14      1.57       agc  *    notice, this list of conditions and the following disclaimer.
     15      1.57       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.57       agc  *    notice, this list of conditions and the following disclaimer in the
     17      1.57       agc  *    documentation and/or other materials provided with the distribution.
     18      1.57       agc  * 3. Neither the name of the University nor the names of its contributors
     19      1.57       agc  *    may be used to endorse or promote products derived from this software
     20      1.57       agc  *    without specific prior written permission.
     21      1.57       agc  *
     22      1.57       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23      1.57       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24      1.57       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25      1.57       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26      1.57       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27      1.57       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28      1.57       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29      1.57       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30      1.57       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31      1.57       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32      1.57       agc  * SUCH DAMAGE.
     33      1.57       agc  *
     34      1.57       agc  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
     35      1.57       agc  */
     36      1.13       cgd 
     37       1.1        pk /*
     38      1.20   thorpej  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
     39       1.5       cgd  * Copyright (c) 1993 Jan-Simon Pendry
     40       1.2        pk  *
     41       1.5       cgd  * This code is derived from software contributed to Berkeley by
     42       1.5       cgd  * Jan-Simon Pendry.
     43       1.5       cgd  *
     44       1.2        pk  * Redistribution and use in source and binary forms, with or without
     45       1.2        pk  * modification, are permitted provided that the following conditions
     46       1.2        pk  * are met:
     47       1.2        pk  * 1. Redistributions of source code must retain the above copyright
     48       1.2        pk  *    notice, this list of conditions and the following disclaimer.
     49       1.2        pk  * 2. Redistributions in binary form must reproduce the above copyright
     50       1.2        pk  *    notice, this list of conditions and the following disclaimer in the
     51       1.2        pk  *    documentation and/or other materials provided with the distribution.
     52       1.2        pk  * 3. All advertising materials mentioning features or use of this software
     53       1.2        pk  *    must display the following acknowledgement:
     54       1.5       cgd  *	This product includes software developed by the University of
     55       1.5       cgd  *	California, Berkeley and its contributors.
     56       1.5       cgd  * 4. Neither the name of the University nor the names of its contributors
     57       1.5       cgd  *    may be used to endorse or promote products derived from this software
     58       1.5       cgd  *    without specific prior written permission.
     59       1.5       cgd  *
     60       1.5       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61       1.5       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62       1.5       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63       1.5       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64       1.5       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65       1.5       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66       1.5       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67       1.5       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68       1.5       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69       1.5       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70       1.5       cgd  * SUCH DAMAGE.
     71       1.2        pk  *
     72      1.23      fvdl  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
     73       1.1        pk  */
     74      1.39     lukem 
     75      1.39     lukem #include <sys/cdefs.h>
     76  1.64.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.64.2.2 2006/12/30 20:50:18 yamt Exp $");
     77       1.5       cgd 
     78       1.4   mycroft #include <sys/param.h>
     79       1.4   mycroft #include <sys/systm.h>
     80       1.4   mycroft #include <sys/time.h>
     81       1.4   mycroft #include <sys/kernel.h>
     82       1.4   mycroft #include <sys/proc.h>
     83       1.4   mycroft #include <sys/vnode.h>
     84      1.11   mycroft #include <sys/malloc.h>
     85      1.18   mycroft #include <sys/stat.h>
     86      1.42  christos #include <sys/file.h>
     87      1.42  christos #include <sys/filedesc.h>
     88  1.64.2.2      yamt #include <sys/kauth.h>
     89      1.18   mycroft 
     90       1.5       cgd #include <miscfs/procfs/procfs.h>
     91       1.1        pk 
     92  1.64.2.1      yamt void procfs_hashins(struct pfsnode *);
     93  1.64.2.1      yamt void procfs_hashrem(struct pfsnode *);
     94  1.64.2.1      yamt struct vnode *procfs_hashget(pid_t, pfstype, int, struct mount *);
     95      1.29      fvdl 
     96      1.29      fvdl LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
     97      1.30      fvdl u_long	pfs_ihash;	/* size of hash table - 1 */
     98      1.38       chs #define PFSPIDHASH(pid)	((pid) & pfs_ihash)
     99      1.29      fvdl 
    100      1.29      fvdl struct lock pfs_hashlock;
    101      1.29      fvdl struct simplelock pfs_hash_slock;
    102       1.1        pk 
    103      1.20   thorpej #define	ISSET(t, f)	((t) & (f))
    104      1.20   thorpej 
    105       1.1        pk /*
    106       1.5       cgd  * allocate a pfsnode/vnode pair.  the vnode is
    107      1.27  wrstuden  * referenced, and locked.
    108       1.5       cgd  *
    109       1.5       cgd  * the pid, pfs_type, and mount point uniquely
    110       1.5       cgd  * identify a pfsnode.  the mount point is needed
    111       1.5       cgd  * because someone might mount this filesystem
    112       1.5       cgd  * twice.
    113       1.5       cgd  *
    114       1.5       cgd  * all pfsnodes are maintained on a singly-linked
    115       1.5       cgd  * list.  new nodes are only allocated when they cannot
    116       1.5       cgd  * be found on this list.  entries on the list are
    117       1.5       cgd  * removed when the vfs reclaim entry is called.
    118       1.5       cgd  *
    119       1.5       cgd  * a single lock is kept for the entire list.  this is
    120       1.5       cgd  * needed because the getnewvnode() function can block
    121       1.5       cgd  * waiting for a vnode to become free, in which case there
    122       1.5       cgd  * may be more than one process trying to get the same
    123       1.5       cgd  * vnode.  this lock is only taken if we are going to
    124       1.5       cgd  * call getnewvnode, since the kernel itself is single-threaded.
    125       1.5       cgd  *
    126       1.5       cgd  * if an entry is found on the list, then call vget() to
    127       1.5       cgd  * take a reference.  this is done because there may be
    128       1.5       cgd  * zero references to it and so it needs to removed from
    129       1.5       cgd  * the vnode free list.
    130       1.1        pk  */
    131      1.11   mycroft int
    132      1.42  christos procfs_allocvp(mp, vpp, pid, pfs_type, fd)
    133       1.5       cgd 	struct mount *mp;
    134       1.5       cgd 	struct vnode **vpp;
    135      1.42  christos 	pid_t pid;
    136       1.5       cgd 	pfstype pfs_type;
    137      1.42  christos 	int fd;
    138       1.1        pk {
    139      1.12   mycroft 	struct pfsnode *pfs;
    140      1.12   mycroft 	struct vnode *vp;
    141       1.5       cgd 	int error;
    142       1.5       cgd 
    143      1.29      fvdl 	do {
    144      1.56      fvdl 		if ((*vpp = procfs_hashget(pid, pfs_type, fd, mp)) != NULL)
    145       1.5       cgd 			return (0);
    146      1.29      fvdl 	} while (lockmgr(&pfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    147       1.1        pk 
    148      1.44  jdolecek 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, &vp)) != 0) {
    149      1.29      fvdl 		*vpp = NULL;
    150      1.33       chs 		lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    151      1.29      fvdl 		return (error);
    152       1.5       cgd 	}
    153       1.5       cgd 
    154      1.11   mycroft 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    155      1.11   mycroft 	vp->v_data = pfs;
    156       1.5       cgd 
    157      1.42  christos 	pfs->pfs_pid = pid;
    158       1.5       cgd 	pfs->pfs_type = pfs_type;
    159      1.11   mycroft 	pfs->pfs_vnode = vp;
    160       1.5       cgd 	pfs->pfs_flags = 0;
    161      1.42  christos 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
    162      1.42  christos 	pfs->pfs_fd = fd;
    163       1.5       cgd 
    164       1.5       cgd 	switch (pfs_type) {
    165      1.58     darcy 	case PFSroot:	/* /proc = dr-xr-xr-x */
    166  1.64.2.2      yamt 		vp->v_flag = VROOT;
    167  1.64.2.2      yamt 		/*FALLTHROUGH*/
    168  1.64.2.2      yamt 	case PFSproc:	/* /proc/N = dr-xr-xr-x */
    169      1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    170      1.11   mycroft 		vp->v_type = VDIR;
    171      1.11   mycroft 		break;
    172      1.11   mycroft 
    173      1.58     darcy 	case PFScurproc:	/* /proc/curproc = lr-xr-xr-x */
    174      1.58     darcy 	case PFSself:	/* /proc/self    = lr-xr-xr-x */
    175  1.64.2.1      yamt 	case PFScwd:	/* /proc/N/cwd = lr-xr-xr-x */
    176  1.64.2.1      yamt 	case PFSchroot:	/* /proc/N/chroot = lr-xr-xr-x */
    177  1.64.2.2      yamt 	case PFSexe:	/* /proc/N/exe = lr-xr-xr-x */
    178      1.22   mycroft 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    179      1.11   mycroft 		vp->v_type = VLNK;
    180       1.5       cgd 		break;
    181       1.5       cgd 
    182      1.58     darcy 	case PFSfd:
    183      1.42  christos 		if (fd == -1) {	/* /proc/N/fd = dr-xr-xr-x */
    184      1.52  christos 			pfs->pfs_mode = S_IRUSR|S_IXUSR;
    185      1.42  christos 			vp->v_type = VDIR;
    186      1.42  christos 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    187      1.42  christos 			struct file *fp;
    188      1.42  christos 			struct vnode *vxp;
    189      1.49  jdolecek 			struct proc *pown;
    190      1.44  jdolecek 
    191      1.44  jdolecek 			/* XXX can procfs_getfp() ever fail here? */
    192      1.49  jdolecek 			if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
    193      1.44  jdolecek 				goto bad;
    194      1.49  jdolecek 			FILE_USE(fp);
    195      1.44  jdolecek 
    196      1.42  christos 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    197      1.42  christos 			switch (fp->f_type) {
    198      1.42  christos 			case DTYPE_VNODE:
    199      1.42  christos 				vxp = (struct vnode *)fp->f_data;
    200      1.50  jdolecek 
    201      1.51  christos 				/*
    202      1.63     perry 				 * We make symlinks for directories
    203      1.51  christos 				 * to avoid cycles.
    204      1.51  christos 				 */
    205      1.51  christos 				if (vxp->v_type == VDIR)
    206      1.51  christos 					goto symlink;
    207      1.42  christos 				vp->v_type = vxp->v_type;
    208      1.42  christos 				break;
    209      1.42  christos 			case DTYPE_PIPE:
    210      1.42  christos 				vp->v_type = VFIFO;
    211      1.42  christos 				break;
    212      1.42  christos 			case DTYPE_SOCKET:
    213      1.42  christos 				vp->v_type = VSOCK;
    214      1.51  christos 				break;
    215      1.51  christos 			case DTYPE_KQUEUE:
    216      1.51  christos 			case DTYPE_MISC:
    217      1.51  christos 			symlink:
    218      1.51  christos 				pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
    219      1.51  christos 				    S_IXGRP|S_IROTH|S_IXOTH;
    220      1.51  christos 				vp->v_type = VLNK;
    221      1.42  christos 				break;
    222      1.45  jdolecek 			default:
    223      1.44  jdolecek 				error = EOPNOTSUPP;
    224  1.64.2.1      yamt 				FILE_UNUSE(fp, proc_representative_lwp(pown));
    225      1.44  jdolecek 				goto bad;
    226      1.42  christos 			}
    227  1.64.2.1      yamt 			FILE_UNUSE(fp, proc_representative_lwp(pown));
    228      1.42  christos 		}
    229       1.5       cgd 		break;
    230       1.5       cgd 
    231      1.58     darcy 	case PFSfile:	/* /proc/N/file = -rw------- */
    232      1.58     darcy 	case PFSmem:	/* /proc/N/mem = -rw------- */
    233      1.58     darcy 	case PFSregs:	/* /proc/N/regs = -rw------- */
    234      1.58     darcy 	case PFSfpregs:	/* /proc/N/fpregs = -rw------- */
    235      1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    236       1.9       cgd 		vp->v_type = VREG;
    237       1.9       cgd 		break;
    238       1.9       cgd 
    239      1.58     darcy 	case PFSctl:	/* /proc/N/ctl = --w------ */
    240      1.58     darcy 	case PFSnote:	/* /proc/N/note = --w------ */
    241      1.58     darcy 	case PFSnotepg:	/* /proc/N/notepg = --w------ */
    242      1.17   mycroft 		pfs->pfs_mode = S_IWUSR;
    243       1.6        ws 		vp->v_type = VREG;
    244       1.5       cgd 		break;
    245       1.5       cgd 
    246      1.58     darcy 	case PFSmap:	/* /proc/N/map = -r--r--r-- */
    247      1.58     darcy 	case PFSmaps:	/* /proc/N/maps = -r--r--r-- */
    248      1.58     darcy 	case PFSstatus:	/* /proc/N/status = -r--r--r-- */
    249      1.58     darcy 	case PFSstat:	/* /proc/N/stat = -r--r--r-- */
    250      1.58     darcy 	case PFScmdline:	/* /proc/N/cmdline = -r--r--r-- */
    251  1.64.2.2      yamt 	case PFSemul:	/* /proc/N/emul = -r--r--r-- */
    252      1.58     darcy 	case PFSmeminfo:	/* /proc/meminfo = -r--r--r-- */
    253  1.64.2.2      yamt 	case PFSdevices:	/* /proc/devices = -r--r--r-- */
    254      1.58     darcy 	case PFScpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    255      1.58     darcy 	case PFSuptime:	/* /proc/uptime = -r--r--r-- */
    256      1.62  jdolecek 	case PFSmounts:	/* /proc/mounts = -r--r--r-- */
    257      1.17   mycroft 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    258       1.6        ws 		vp->v_type = VREG;
    259       1.5       cgd 		break;
    260       1.5       cgd 
    261      1.40   thorpej #ifdef __HAVE_PROCFS_MACHDEP
    262      1.40   thorpej 	PROCFS_MACHDEP_NODETYPE_CASES
    263      1.40   thorpej 		procfs_machdep_allocvp(vp);
    264      1.40   thorpej 		break;
    265      1.40   thorpej #endif
    266      1.40   thorpej 
    267       1.5       cgd 	default:
    268      1.11   mycroft 		panic("procfs_allocvp");
    269       1.5       cgd 	}
    270      1.27  wrstuden 
    271      1.29      fvdl 	procfs_hashins(pfs);
    272      1.34       chs 	uvm_vnp_setsize(vp, 0);
    273      1.33       chs 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    274       1.1        pk 
    275      1.44  jdolecek 	*vpp = vp;
    276      1.44  jdolecek 	return (0);
    277      1.44  jdolecek 
    278      1.44  jdolecek  bad:
    279      1.48     enami 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    280      1.44  jdolecek 	FREE(pfs, M_TEMP);
    281      1.44  jdolecek 	ungetnewvnode(vp);
    282       1.5       cgd 	return (error);
    283       1.1        pk }
    284       1.1        pk 
    285      1.11   mycroft int
    286       1.5       cgd procfs_freevp(vp)
    287       1.5       cgd 	struct vnode *vp;
    288       1.1        pk {
    289       1.5       cgd 	struct pfsnode *pfs = VTOPFS(vp);
    290       1.5       cgd 
    291      1.29      fvdl 	procfs_hashrem(pfs);
    292       1.1        pk 
    293      1.11   mycroft 	FREE(vp->v_data, M_TEMP);
    294      1.11   mycroft 	vp->v_data = 0;
    295       1.5       cgd 	return (0);
    296       1.1        pk }
    297       1.1        pk 
    298      1.11   mycroft int
    299      1.15  christos procfs_rw(v)
    300      1.15  christos 	void *v;
    301       1.1        pk {
    302      1.15  christos 	struct vop_read_args *ap = v;
    303      1.11   mycroft 	struct vnode *vp = ap->a_vp;
    304      1.11   mycroft 	struct uio *uio = ap->a_uio;
    305  1.64.2.1      yamt 	struct lwp *curl;
    306      1.54   darrenr 	struct lwp *l;
    307  1.64.2.1      yamt 	struct pfsnode *pfs = VTOPFS(vp);
    308       1.5       cgd 	struct proc *p;
    309  1.64.2.2      yamt 	int error;
    310       1.5       cgd 
    311      1.59  christos 	if (uio->uio_offset < 0)
    312      1.59  christos 		return EINVAL;
    313       1.5       cgd 	p = PFIND(pfs->pfs_pid);
    314       1.5       cgd 	if (p == 0)
    315      1.59  christos 		return ESRCH;
    316  1.64.2.2      yamt 
    317  1.64.2.2      yamt 	if (ISSET(p->p_flag, P_INEXEC))
    318  1.64.2.2      yamt 		return (EAGAIN);
    319  1.64.2.2      yamt 
    320  1.64.2.2      yamt 	curl = curlwp;
    321  1.64.2.2      yamt 
    322      1.59  christos 	/*
    323      1.59  christos 	 * Do not allow init to be modified while in secure mode; it
    324      1.59  christos 	 * could be duped into changing the security level.
    325      1.59  christos 	 */
    326  1.64.2.2      yamt #define	M2K(m)	((m) == UIO_READ ? KAUTH_REQ_PROCESS_CANPROCFS_READ : \
    327  1.64.2.2      yamt 		 KAUTH_REQ_PROCESS_CANPROCFS_WRITE)
    328  1.64.2.2      yamt 	error = kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_CANPROCFS,
    329  1.64.2.2      yamt 	    p, pfs, KAUTH_ARG(M2K(uio->uio_rw)), NULL);
    330  1.64.2.2      yamt 	if (error)
    331  1.64.2.2      yamt 		return (error);
    332  1.64.2.2      yamt #undef	M2K
    333  1.64.2.1      yamt 
    334      1.43   thorpej 	/* XXX NJWLWP
    335      1.43   thorpej 	 * The entire procfs interface needs work to be useful to
    336      1.63     perry 	 * a process with multiple LWPs. For the moment, we'll
    337      1.43   thorpej 	 * just kluge this and fail on others.
    338      1.43   thorpej 	 */
    339      1.43   thorpej 	l = proc_representative_lwp(p);
    340      1.63     perry 
    341      1.19   mycroft 	switch (pfs->pfs_type) {
    342      1.58     darcy 	case PFSnote:
    343      1.58     darcy 	case PFSnotepg:
    344  1.64.2.1      yamt 		return (procfs_donote(curl, p, pfs, uio));
    345       1.5       cgd 
    346      1.58     darcy 	case PFSregs:
    347  1.64.2.1      yamt 		return (procfs_doregs(curl, l, pfs, uio));
    348       1.9       cgd 
    349      1.58     darcy 	case PFSfpregs:
    350  1.64.2.1      yamt 		return (procfs_dofpregs(curl, l, pfs, uio));
    351       1.5       cgd 
    352      1.58     darcy 	case PFSctl:
    353  1.64.2.1      yamt 		return (procfs_doctl(curl, l, pfs, uio));
    354       1.5       cgd 
    355      1.58     darcy 	case PFSstatus:
    356  1.64.2.1      yamt 		return (procfs_dostatus(curl, l, pfs, uio));
    357      1.53  christos 
    358      1.58     darcy 	case PFSstat:
    359  1.64.2.1      yamt 		return (procfs_do_pid_stat(curl, l, pfs, uio));
    360      1.25   msaitoh 
    361      1.58     darcy 	case PFSmap:
    362  1.64.2.1      yamt 		return (procfs_domap(curl, p, pfs, uio, 0));
    363      1.37      fvdl 
    364      1.58     darcy 	case PFSmaps:
    365  1.64.2.1      yamt 		return (procfs_domap(curl, p, pfs, uio, 1));
    366       1.1        pk 
    367      1.58     darcy 	case PFSmem:
    368  1.64.2.1      yamt 		return (procfs_domem(curl, l, pfs, uio));
    369      1.26  christos 
    370      1.58     darcy 	case PFScmdline:
    371  1.64.2.1      yamt 		return (procfs_docmdline(curl, p, pfs, uio));
    372      1.35      fvdl 
    373      1.58     darcy 	case PFSmeminfo:
    374  1.64.2.1      yamt 		return (procfs_domeminfo(curl, p, pfs, uio));
    375      1.40   thorpej 
    376  1.64.2.2      yamt 	case PFSdevices:
    377  1.64.2.2      yamt 		return (procfs_dodevices(curl, p, pfs, uio));
    378  1.64.2.2      yamt 
    379      1.58     darcy 	case PFScpuinfo:
    380  1.64.2.1      yamt 		return (procfs_docpuinfo(curl, p, pfs, uio));
    381      1.40   thorpej 
    382      1.58     darcy 	case PFSfd:
    383  1.64.2.1      yamt 		return (procfs_dofd(curl, p, pfs, uio));
    384      1.46       jrf 
    385      1.58     darcy 	case PFSuptime:
    386  1.64.2.1      yamt 		return (procfs_douptime(curl, p, pfs, uio));
    387      1.42  christos 
    388      1.62  jdolecek 	case PFSmounts:
    389  1.64.2.1      yamt 		return (procfs_domounts(curl, p, pfs, uio));
    390      1.62  jdolecek 
    391  1.64.2.2      yamt 	case PFSemul:
    392  1.64.2.2      yamt 		return procfs_doemul(curl, p, pfs, uio);
    393  1.64.2.2      yamt 
    394      1.40   thorpej #ifdef __HAVE_PROCFS_MACHDEP
    395      1.40   thorpej 	PROCFS_MACHDEP_NODETYPE_CASES
    396  1.64.2.1      yamt 		return (procfs_machdep_rw(curl, l, pfs, uio));
    397      1.40   thorpej #endif
    398       1.1        pk 
    399       1.5       cgd 	default:
    400       1.5       cgd 		return (EOPNOTSUPP);
    401       1.5       cgd 	}
    402       1.1        pk }
    403       1.1        pk 
    404       1.5       cgd /*
    405      1.64  christos  * Get a string from userland into (bf).  Strip a trailing
    406       1.5       cgd  * nl character (to allow easy access from the shell).
    407      1.11   mycroft  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    408       1.5       cgd  * will automatically add a nul char at the end.
    409       1.5       cgd  *
    410       1.5       cgd  * Returns 0 on success or the following errors
    411       1.5       cgd  *
    412       1.5       cgd  * EINVAL:    file offset is non-zero.
    413       1.5       cgd  * EMSGSIZE:  message is longer than kernel buffer
    414       1.5       cgd  * EFAULT:    user i/o buffer is not addressable
    415       1.5       cgd  */
    416      1.11   mycroft int
    417      1.64  christos vfs_getuserstr(uio, bf, buflenp)
    418       1.5       cgd 	struct uio *uio;
    419      1.64  christos 	char *bf;
    420       1.5       cgd 	int *buflenp;
    421       1.1        pk {
    422       1.5       cgd 	int xlen;
    423       1.5       cgd 	int error;
    424       1.5       cgd 
    425      1.11   mycroft 	if (uio->uio_offset != 0)
    426      1.11   mycroft 		return (EINVAL);
    427      1.11   mycroft 
    428       1.5       cgd 	xlen = *buflenp;
    429       1.1        pk 
    430       1.5       cgd 	/* must be able to read the whole string in one go */
    431       1.5       cgd 	if (xlen < uio->uio_resid)
    432       1.5       cgd 		return (EMSGSIZE);
    433       1.5       cgd 	xlen = uio->uio_resid;
    434       1.5       cgd 
    435      1.64  christos 	if ((error = uiomove(bf, xlen, uio)) != 0)
    436       1.5       cgd 		return (error);
    437       1.5       cgd 
    438      1.11   mycroft 	/* allow multiple writes without seeks */
    439      1.11   mycroft 	uio->uio_offset = 0;
    440      1.11   mycroft 
    441       1.5       cgd 	/* cleanup string and remove trailing newline */
    442      1.64  christos 	bf[xlen] = '\0';
    443      1.64  christos 	xlen = strlen(bf);
    444      1.64  christos 	if (xlen > 0 && bf[xlen-1] == '\n')
    445      1.64  christos 		bf[--xlen] = '\0';
    446       1.5       cgd 	*buflenp = xlen;
    447       1.1        pk 
    448       1.5       cgd 	return (0);
    449       1.1        pk }
    450       1.1        pk 
    451      1.36  jdolecek const vfs_namemap_t *
    452      1.64  christos vfs_findname(nm, bf, buflen)
    453      1.36  jdolecek 	const vfs_namemap_t *nm;
    454      1.64  christos 	const char *bf;
    455       1.5       cgd 	int buflen;
    456       1.1        pk {
    457      1.11   mycroft 
    458       1.5       cgd 	for (; nm->nm_name; nm++)
    459      1.64  christos 		if (memcmp(bf, nm->nm_name, buflen+1) == 0)
    460       1.5       cgd 			return (nm);
    461       1.5       cgd 
    462       1.5       cgd 	return (0);
    463      1.29      fvdl }
    464      1.29      fvdl 
    465      1.29      fvdl /*
    466      1.29      fvdl  * Initialize pfsnode hash table.
    467      1.29      fvdl  */
    468      1.29      fvdl void
    469      1.29      fvdl procfs_hashinit()
    470      1.29      fvdl {
    471      1.29      fvdl 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    472      1.32        ad 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    473      1.32        ad 	    M_WAITOK, &pfs_ihash);
    474      1.29      fvdl 	simple_lock_init(&pfs_hash_slock);
    475      1.31  jdolecek }
    476      1.31  jdolecek 
    477      1.38       chs void
    478      1.38       chs procfs_hashreinit()
    479      1.38       chs {
    480      1.38       chs 	struct pfsnode *pp;
    481      1.38       chs 	struct pfs_hashhead *oldhash, *hash;
    482      1.41   thorpej 	u_long i, oldmask, mask, val;
    483      1.38       chs 
    484      1.38       chs 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    485      1.38       chs 	    &mask);
    486      1.38       chs 
    487      1.38       chs 	simple_lock(&pfs_hash_slock);
    488      1.38       chs 	oldhash = pfs_hashtbl;
    489      1.38       chs 	oldmask = pfs_ihash;
    490      1.38       chs 	pfs_hashtbl = hash;
    491      1.38       chs 	pfs_ihash = mask;
    492      1.38       chs 	for (i = 0; i <= oldmask; i++) {
    493      1.38       chs 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    494      1.38       chs 			LIST_REMOVE(pp, pfs_hash);
    495      1.38       chs 			val = PFSPIDHASH(pp->pfs_pid);
    496      1.38       chs 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    497      1.38       chs 		}
    498      1.38       chs 	}
    499      1.38       chs 	simple_unlock(&pfs_hash_slock);
    500      1.38       chs 	hashdone(oldhash, M_UFSMNT);
    501      1.38       chs }
    502      1.38       chs 
    503      1.31  jdolecek /*
    504      1.31  jdolecek  * Free pfsnode hash table.
    505      1.31  jdolecek  */
    506      1.31  jdolecek void
    507      1.31  jdolecek procfs_hashdone()
    508      1.31  jdolecek {
    509      1.31  jdolecek 	hashdone(pfs_hashtbl, M_UFSMNT);
    510      1.29      fvdl }
    511      1.29      fvdl 
    512      1.29      fvdl struct vnode *
    513      1.56      fvdl procfs_hashget(pid, type, fd, mp)
    514      1.29      fvdl 	pid_t pid;
    515      1.29      fvdl 	pfstype type;
    516      1.42  christos 	int fd;
    517      1.29      fvdl 	struct mount *mp;
    518      1.29      fvdl {
    519      1.38       chs 	struct pfs_hashhead *ppp;
    520      1.29      fvdl 	struct pfsnode *pp;
    521      1.29      fvdl 	struct vnode *vp;
    522      1.29      fvdl 
    523      1.29      fvdl loop:
    524      1.29      fvdl 	simple_lock(&pfs_hash_slock);
    525      1.38       chs 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    526      1.38       chs 	LIST_FOREACH(pp, ppp, pfs_hash) {
    527      1.29      fvdl 		vp = PFSTOV(pp);
    528      1.29      fvdl 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    529      1.42  christos 		    pp->pfs_fd == fd && vp->v_mount == mp) {
    530      1.29      fvdl 			simple_lock(&vp->v_interlock);
    531      1.29      fvdl 			simple_unlock(&pfs_hash_slock);
    532      1.55   thorpej 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    533      1.29      fvdl 				goto loop;
    534      1.29      fvdl 			return (vp);
    535      1.29      fvdl 		}
    536      1.29      fvdl 	}
    537      1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    538      1.29      fvdl 	return (NULL);
    539      1.29      fvdl }
    540      1.29      fvdl 
    541      1.29      fvdl /*
    542      1.29      fvdl  * Insert the pfsnode into the hash table and lock it.
    543      1.29      fvdl  */
    544      1.29      fvdl void
    545      1.29      fvdl procfs_hashins(pp)
    546      1.29      fvdl 	struct pfsnode *pp;
    547      1.29      fvdl {
    548      1.29      fvdl 	struct pfs_hashhead *ppp;
    549      1.29      fvdl 
    550      1.29      fvdl 	/* lock the pfsnode, then put it on the appropriate hash list */
    551      1.29      fvdl 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    552      1.29      fvdl 
    553      1.29      fvdl 	simple_lock(&pfs_hash_slock);
    554      1.38       chs 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    555      1.29      fvdl 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    556      1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    557      1.29      fvdl }
    558      1.29      fvdl 
    559      1.29      fvdl /*
    560      1.29      fvdl  * Remove the pfsnode from the hash table.
    561      1.29      fvdl  */
    562      1.29      fvdl void
    563      1.29      fvdl procfs_hashrem(pp)
    564      1.29      fvdl 	struct pfsnode *pp;
    565      1.29      fvdl {
    566      1.29      fvdl 	simple_lock(&pfs_hash_slock);
    567      1.29      fvdl 	LIST_REMOVE(pp, pfs_hash);
    568      1.29      fvdl 	simple_unlock(&pfs_hash_slock);
    569      1.29      fvdl }
    570      1.29      fvdl 
    571      1.29      fvdl void
    572      1.56      fvdl procfs_revoke_vnodes(p, arg)
    573      1.56      fvdl 	struct proc *p;
    574      1.29      fvdl 	void *arg;
    575      1.29      fvdl {
    576      1.29      fvdl 	struct pfsnode *pfs, *pnext;
    577      1.29      fvdl 	struct vnode *vp;
    578      1.29      fvdl 	struct mount *mp = (struct mount *)arg;
    579      1.38       chs 	struct pfs_hashhead *ppp;
    580      1.29      fvdl 
    581      1.29      fvdl 	if (!(p->p_flag & P_SUGID))
    582      1.29      fvdl 		return;
    583      1.29      fvdl 
    584      1.38       chs 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    585      1.38       chs 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    586      1.29      fvdl 		vp = PFSTOV(pfs);
    587      1.38       chs 		pnext = LIST_NEXT(pfs, pfs_hash);
    588      1.29      fvdl 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    589      1.29      fvdl 		    vp->v_mount == mp)
    590      1.29      fvdl 			VOP_REVOKE(vp, REVOKEALL);
    591      1.29      fvdl 	}
    592      1.42  christos }
    593      1.42  christos 
    594      1.42  christos int
    595      1.49  jdolecek procfs_getfp(pfs, pown, fp)
    596      1.42  christos 	struct pfsnode *pfs;
    597      1.49  jdolecek 	struct proc **pown;
    598      1.42  christos 	struct file **fp;
    599      1.42  christos {
    600      1.42  christos 	struct proc *p = PFIND(pfs->pfs_pid);
    601      1.42  christos 
    602      1.42  christos 	if (p == NULL)
    603      1.42  christos 		return ESRCH;
    604      1.42  christos 
    605      1.42  christos 	if (pfs->pfs_fd == -1)
    606      1.42  christos 		return EINVAL;
    607      1.42  christos 
    608      1.49  jdolecek 	if ((*fp = fd_getfile(p->p_fd, pfs->pfs_fd)) == NULL)
    609      1.42  christos 		return EBADF;
    610      1.49  jdolecek 
    611      1.49  jdolecek 	*pown = p;
    612      1.42  christos 	return 0;
    613       1.1        pk }
    614  1.64.2.2      yamt 
    615  1.64.2.2      yamt int
    616  1.64.2.2      yamt procfs_doemul(struct lwp *curl, struct proc *p,
    617  1.64.2.2      yamt     struct pfsnode *pfs, struct uio *uio)
    618  1.64.2.2      yamt {
    619  1.64.2.2      yamt 	const char *ename = p->p_emul->e_name;
    620  1.64.2.2      yamt 	return uiomove_frombuf(__UNCONST(ename), strlen(ename), uio);
    621  1.64.2.2      yamt }
    622