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