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