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