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