Home | History | Annotate | Line # | Download | only in procfs
procfs_vfsops.c revision 1.92
      1  1.92   hannken /*	$NetBSD: procfs_vfsops.c,v 1.92 2014/07/27 16:47:26 hannken Exp $	*/
      2  1.18       cgd 
      3   1.1        pk /*
      4  1.16   mycroft  * Copyright (c) 1993
      5  1.16   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.2        pk  *
      7   1.9       cgd  * This code is derived from software contributed to Berkeley by
      8   1.9       cgd  * Jan-Simon Pendry.
      9   1.9       cgd  *
     10   1.2        pk  * Redistribution and use in source and binary forms, with or without
     11   1.2        pk  * modification, are permitted provided that the following conditions
     12   1.2        pk  * are met:
     13   1.2        pk  * 1. Redistributions of source code must retain the above copyright
     14   1.2        pk  *    notice, this list of conditions and the following disclaimer.
     15   1.2        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2        pk  *    notice, this list of conditions and the following disclaimer in the
     17   1.2        pk  *    documentation and/or other materials provided with the distribution.
     18  1.49       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.49       agc  *    may be used to endorse or promote products derived from this software
     20  1.49       agc  *    without specific prior written permission.
     21  1.49       agc  *
     22  1.49       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.49       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.49       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.49       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.49       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.49       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.49       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.49       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.49       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.49       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.49       agc  * SUCH DAMAGE.
     33  1.49       agc  *
     34  1.49       agc  *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
     35  1.49       agc  */
     36  1.49       agc 
     37  1.49       agc /*
     38  1.49       agc  * Copyright (c) 1993 Jan-Simon Pendry
     39  1.49       agc  *
     40  1.49       agc  * This code is derived from software contributed to Berkeley by
     41  1.49       agc  * Jan-Simon Pendry.
     42  1.49       agc  *
     43  1.49       agc  * Redistribution and use in source and binary forms, with or without
     44  1.49       agc  * modification, are permitted provided that the following conditions
     45  1.49       agc  * are met:
     46  1.49       agc  * 1. Redistributions of source code must retain the above copyright
     47  1.49       agc  *    notice, this list of conditions and the following disclaimer.
     48  1.49       agc  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.49       agc  *    notice, this list of conditions and the following disclaimer in the
     50  1.49       agc  *    documentation and/or other materials provided with the distribution.
     51   1.2        pk  * 3. All advertising materials mentioning features or use of this software
     52   1.2        pk  *    must display the following acknowledgement:
     53   1.9       cgd  *	This product includes software developed by the University of
     54   1.9       cgd  *	California, Berkeley and its contributors.
     55   1.9       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.9       cgd  *    may be used to endorse or promote products derived from this software
     57   1.9       cgd  *    without specific prior written permission.
     58   1.9       cgd  *
     59   1.9       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.9       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.9       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.9       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.9       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.9       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.9       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.9       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.9       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.9       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.9       cgd  * SUCH DAMAGE.
     70   1.9       cgd  *
     71  1.28      fvdl  *	@(#)procfs_vfsops.c	8.7 (Berkeley) 5/10/95
     72   1.2        pk  */
     73   1.2        pk 
     74   1.2        pk /*
     75   1.9       cgd  * procfs VFS interface
     76   1.1        pk  */
     77  1.41     lukem 
     78  1.41     lukem #include <sys/cdefs.h>
     79  1.92   hannken __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.92 2014/07/27 16:47:26 hannken Exp $");
     80  1.29  jonathan 
     81  1.39       mrg #if defined(_KERNEL_OPT)
     82  1.29  jonathan #include "opt_compat_netbsd.h"
     83  1.29  jonathan #endif
     84   1.1        pk 
     85   1.8   mycroft #include <sys/param.h>
     86   1.8   mycroft #include <sys/time.h>
     87   1.8   mycroft #include <sys/kernel.h>
     88  1.25  christos #include <sys/systm.h>
     89  1.51    atatat #include <sys/sysctl.h>
     90   1.8   mycroft #include <sys/proc.h>
     91   1.8   mycroft #include <sys/buf.h>
     92   1.9       cgd #include <sys/syslog.h>
     93   1.8   mycroft #include <sys/mount.h>
     94  1.54  christos #include <sys/dirent.h>
     95   1.8   mycroft #include <sys/signalvar.h>
     96   1.8   mycroft #include <sys/vnode.h>
     97  1.32      fvdl #include <sys/malloc.h>
     98  1.92   hannken #include <sys/file.h>
     99  1.92   hannken #include <sys/filedesc.h>
    100  1.64      elad #include <sys/kauth.h>
    101  1.79    rumble #include <sys/module.h>
    102  1.35       mrg 
    103  1.77  dholland #include <miscfs/genfs/genfs.h>
    104  1.77  dholland 
    105   1.9       cgd #include <miscfs/procfs/procfs.h>
    106  1.35       mrg 
    107  1.35       mrg #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
    108   1.1        pk 
    109  1.79    rumble MODULE(MODULE_CLASS_VFS, procfs, NULL);
    110  1.79    rumble 
    111  1.74     pooka VFS_PROTOS(procfs);
    112  1.52    atatat 
    113  1.81    rumble static struct sysctllog *procfs_sysctl_log;
    114  1.81    rumble 
    115  1.84      elad static kauth_listener_t procfs_listener;
    116  1.84      elad 
    117   1.1        pk /*
    118   1.1        pk  * VFS Operations.
    119   1.1        pk  *
    120   1.1        pk  * mount system call
    121   1.1        pk  */
    122   1.1        pk /* ARGSUSED */
    123  1.25  christos int
    124  1.66  christos procfs_mount(
    125  1.66  christos     struct mount *mp,
    126  1.66  christos     const char *path,
    127  1.66  christos     void *data,
    128  1.75     pooka     size_t *data_len)
    129   1.1        pk {
    130  1.75     pooka 	struct lwp *l = curlwp;
    131  1.32      fvdl 	struct procfsmount *pmnt;
    132  1.71       dsl 	struct procfs_args *args = data;
    133  1.36      fvdl 	int error;
    134   1.1        pk 
    135  1.91      maxv 	if (args == NULL)
    136  1.91      maxv 		return EINVAL;
    137  1.91      maxv 
    138   1.9       cgd 	if (UIO_MX & (UIO_MX-1)) {
    139   1.9       cgd 		log(LOG_ERR, "procfs: invalid directory entry size");
    140   1.9       cgd 		return (EINVAL);
    141  1.43  christos 	}
    142  1.43  christos 
    143  1.43  christos 	if (mp->mnt_flag & MNT_GETARGS) {
    144  1.71       dsl 		if (*data_len < sizeof *args)
    145  1.71       dsl 			return EINVAL;
    146  1.71       dsl 
    147  1.43  christos 		pmnt = VFSTOPROC(mp);
    148  1.43  christos 		if (pmnt == NULL)
    149  1.43  christos 			return EIO;
    150  1.71       dsl 		args->version = PROCFS_ARGSVERSION;
    151  1.71       dsl 		args->flags = pmnt->pmnt_flags;
    152  1.71       dsl 		*data_len = sizeof *args;
    153  1.71       dsl 		return 0;
    154   1.1        pk 	}
    155   1.1        pk 
    156   1.9       cgd 	if (mp->mnt_flag & MNT_UPDATE)
    157   1.9       cgd 		return (EOPNOTSUPP);
    158   1.9       cgd 
    159  1.71       dsl 	if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
    160  1.71       dsl 		return EINVAL;
    161  1.36      fvdl 
    162  1.87     rmind 	pmnt = kmem_zalloc(sizeof(struct procfsmount), KM_SLEEP);
    163  1.32      fvdl 
    164  1.86  christos 	mp->mnt_stat.f_namemax = PROCFS_MAXNAMLEN;
    165  1.58  jdolecek 	mp->mnt_flag |= MNT_LOCAL;
    166  1.42     soren 	mp->mnt_data = pmnt;
    167  1.34     assar 	vfs_getnewfsid(mp);
    168   1.9       cgd 
    169  1.53  christos 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
    170  1.72     pooka 	    mp->mnt_op->vfs_name, mp, l);
    171  1.32      fvdl 	pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
    172  1.71       dsl 	if (*data_len >= sizeof *args)
    173  1.71       dsl 		pmnt->pmnt_flags = args->flags;
    174  1.71       dsl 	else
    175  1.71       dsl 		pmnt->pmnt_flags = 0;
    176  1.32      fvdl 
    177  1.76        ad 	mp->mnt_iflag |= IMNT_MPSAFE;
    178  1.45  christos 	return error;
    179   1.1        pk }
    180   1.1        pk 
    181   1.1        pk /*
    182   1.1        pk  * unmount system call
    183   1.1        pk  */
    184  1.25  christos int
    185  1.75     pooka procfs_unmount(struct mount *mp, int mntflags)
    186   1.1        pk {
    187   1.9       cgd 	int error;
    188   1.9       cgd 	int flags = 0;
    189   1.9       cgd 
    190  1.28      fvdl 	if (mntflags & MNT_FORCE)
    191   1.9       cgd 		flags |= FORCECLOSE;
    192   1.9       cgd 
    193  1.25  christos 	if ((error = vflush(mp, 0, flags)) != 0)
    194   1.9       cgd 		return (error);
    195   1.9       cgd 
    196  1.32      fvdl 	exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
    197  1.32      fvdl 
    198  1.87     rmind 	kmem_free(mp->mnt_data, sizeof(struct procfsmount));
    199  1.80    simonb 	mp->mnt_data = NULL;
    200  1.32      fvdl 
    201  1.87     rmind 	return 0;
    202   1.1        pk }
    203   1.1        pk 
    204  1.25  christos int
    205  1.82       dsl procfs_root(struct mount *mp, struct vnode **vpp)
    206   1.1        pk {
    207  1.88   hannken 	int error;
    208  1.88   hannken 
    209  1.92   hannken 	error = procfs_allocvp(mp, vpp, 0, PFSroot, -1);
    210  1.88   hannken 	if (error == 0) {
    211  1.88   hannken 		error = vn_lock(*vpp, LK_EXCLUSIVE);
    212  1.88   hannken 		if (error != 0) {
    213  1.88   hannken 			vrele(*vpp);
    214  1.88   hannken 			*vpp = NULL;
    215  1.88   hannken 		}
    216  1.88   hannken 	}
    217   1.1        pk 
    218  1.88   hannken 	return error;
    219   1.1        pk }
    220   1.1        pk 
    221   1.1        pk /* ARGSUSED */
    222  1.25  christos int
    223  1.75     pooka procfs_start(struct mount *mp, int flags)
    224   1.1        pk {
    225   1.9       cgd 
    226   1.9       cgd 	return (0);
    227   1.1        pk }
    228   1.1        pk 
    229   1.1        pk /*
    230   1.1        pk  * Get file system statistics.
    231   1.1        pk  */
    232  1.25  christos int
    233  1.75     pooka procfs_statvfs(struct mount *mp, struct statvfs *sbp)
    234   1.1        pk {
    235  1.16   mycroft 
    236  1.85     pooka 	genfs_statvfs(mp, sbp);
    237  1.85     pooka 
    238  1.16   mycroft 	sbp->f_bsize = PAGE_SIZE;
    239  1.53  christos 	sbp->f_frsize = PAGE_SIZE;
    240  1.14       cgd 	sbp->f_iosize = PAGE_SIZE;
    241  1.85     pooka 	sbp->f_blocks = 1;
    242   1.9       cgd 	sbp->f_files = maxproc;			/* approx */
    243   1.9       cgd 	sbp->f_ffree = maxproc - nprocs;	/* approx */
    244  1.53  christos 	sbp->f_favail = maxproc - nprocs;	/* approx */
    245  1.85     pooka 
    246   1.9       cgd 	return (0);
    247   1.1        pk }
    248   1.1        pk 
    249  1.25  christos /*ARGSUSED*/
    250  1.25  christos int
    251  1.66  christos procfs_sync(
    252  1.67  christos     struct mount *mp,
    253  1.67  christos     int waitfor,
    254  1.75     pooka     kauth_cred_t uc)
    255   1.1        pk {
    256   1.9       cgd 
    257   1.9       cgd 	return (0);
    258   1.1        pk }
    259   1.1        pk 
    260  1.25  christos /*ARGSUSED*/
    261  1.25  christos int
    262  1.67  christos procfs_vget(struct mount *mp, ino_t ino,
    263  1.67  christos     struct vnode **vpp)
    264  1.16   mycroft {
    265  1.16   mycroft 	return (EOPNOTSUPP);
    266  1.16   mycroft }
    267  1.16   mycroft 
    268  1.92   hannken int
    269  1.92   hannken procfs_loadvnode(struct mount *mp, struct vnode *vp,
    270  1.92   hannken     const void *key, size_t key_len, const void **new_key)
    271  1.92   hannken {
    272  1.92   hannken 	int error;
    273  1.92   hannken 	struct pfskey pfskey;
    274  1.92   hannken 	struct pfsnode *pfs;
    275  1.92   hannken 
    276  1.92   hannken 	KASSERT(key_len == sizeof(pfskey));
    277  1.92   hannken 	memcpy(&pfskey, key, key_len);
    278  1.92   hannken 
    279  1.92   hannken 	pfs = kmem_alloc(sizeof(*pfs), KM_SLEEP);
    280  1.92   hannken 	pfs->pfs_pid = pfskey.pk_pid;
    281  1.92   hannken 	pfs->pfs_type = pfskey.pk_type;
    282  1.92   hannken 	pfs->pfs_fd = pfskey.pk_fd;
    283  1.92   hannken 	pfs->pfs_vnode = vp;
    284  1.92   hannken 	pfs->pfs_flags = 0;
    285  1.92   hannken 	pfs->pfs_fileno =
    286  1.92   hannken 	    PROCFS_FILENO(pfs->pfs_pid, pfs->pfs_type, pfs->pfs_fd);
    287  1.92   hannken 	vp->v_tag = VT_PROCFS;
    288  1.92   hannken 	vp->v_op = procfs_vnodeop_p;
    289  1.92   hannken 	vp->v_data = pfs;
    290  1.92   hannken 
    291  1.92   hannken 	switch (pfs->pfs_type) {
    292  1.92   hannken 	case PFSroot:	/* /proc = dr-xr-xr-x */
    293  1.92   hannken 		vp->v_vflag |= VV_ROOT;
    294  1.92   hannken 		/*FALLTHROUGH*/
    295  1.92   hannken 	case PFSproc:	/* /proc/N = dr-xr-xr-x */
    296  1.92   hannken 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    297  1.92   hannken 		vp->v_type = VDIR;
    298  1.92   hannken 		break;
    299  1.92   hannken 
    300  1.92   hannken 	case PFStask:	/* /proc/N/task = dr-xr-xr-x */
    301  1.92   hannken 		if (pfs->pfs_fd == -1) {
    302  1.92   hannken 			pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
    303  1.92   hannken 			    S_IROTH|S_IXOTH;
    304  1.92   hannken 			vp->v_type = VDIR;
    305  1.92   hannken 			break;
    306  1.92   hannken 		}
    307  1.92   hannken 		/*FALLTHROUGH*/
    308  1.92   hannken 	case PFScurproc:	/* /proc/curproc = lr-xr-xr-x */
    309  1.92   hannken 	case PFSself:	/* /proc/self    = lr-xr-xr-x */
    310  1.92   hannken 	case PFScwd:	/* /proc/N/cwd = lr-xr-xr-x */
    311  1.92   hannken 	case PFSchroot:	/* /proc/N/chroot = lr-xr-xr-x */
    312  1.92   hannken 	case PFSexe:	/* /proc/N/exe = lr-xr-xr-x */
    313  1.92   hannken 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    314  1.92   hannken 		vp->v_type = VLNK;
    315  1.92   hannken 		break;
    316  1.92   hannken 
    317  1.92   hannken 	case PFSfd:
    318  1.92   hannken 		if (pfs->pfs_fd == -1) {	/* /proc/N/fd = dr-x------ */
    319  1.92   hannken 			pfs->pfs_mode = S_IRUSR|S_IXUSR;
    320  1.92   hannken 			vp->v_type = VDIR;
    321  1.92   hannken 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    322  1.92   hannken 			file_t *fp;
    323  1.92   hannken 			vnode_t *vxp;
    324  1.92   hannken 			struct proc *p;
    325  1.92   hannken 
    326  1.92   hannken 			mutex_enter(proc_lock);
    327  1.92   hannken 			p = proc_find(pfs->pfs_pid);
    328  1.92   hannken 			mutex_exit(proc_lock);
    329  1.92   hannken 			if (p == NULL) {
    330  1.92   hannken 				error = ENOENT;
    331  1.92   hannken 				goto bad;
    332  1.92   hannken 			}
    333  1.92   hannken 			KASSERT(rw_read_held(&p->p_reflock));
    334  1.92   hannken 			if ((fp = fd_getfile2(p, pfs->pfs_fd)) == NULL) {
    335  1.92   hannken 				error = EBADF;
    336  1.92   hannken 				goto bad;
    337  1.92   hannken 			}
    338  1.92   hannken 
    339  1.92   hannken 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    340  1.92   hannken 			switch (fp->f_type) {
    341  1.92   hannken 			case DTYPE_VNODE:
    342  1.92   hannken 				vxp = fp->f_data;
    343  1.92   hannken 
    344  1.92   hannken 				/*
    345  1.92   hannken 				 * We make symlinks for directories
    346  1.92   hannken 				 * to avoid cycles.
    347  1.92   hannken 				 */
    348  1.92   hannken 				if (vxp->v_type == VDIR)
    349  1.92   hannken 					goto symlink;
    350  1.92   hannken 				vp->v_type = vxp->v_type;
    351  1.92   hannken 				break;
    352  1.92   hannken 			case DTYPE_PIPE:
    353  1.92   hannken 				vp->v_type = VFIFO;
    354  1.92   hannken 				break;
    355  1.92   hannken 			case DTYPE_SOCKET:
    356  1.92   hannken 				vp->v_type = VSOCK;
    357  1.92   hannken 				break;
    358  1.92   hannken 			case DTYPE_KQUEUE:
    359  1.92   hannken 			case DTYPE_MISC:
    360  1.92   hannken 			case DTYPE_SEM:
    361  1.92   hannken 			symlink:
    362  1.92   hannken 				pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
    363  1.92   hannken 				    S_IXGRP|S_IROTH|S_IXOTH;
    364  1.92   hannken 				vp->v_type = VLNK;
    365  1.92   hannken 				break;
    366  1.92   hannken 			default:
    367  1.92   hannken 				error = EOPNOTSUPP;
    368  1.92   hannken 				closef(fp);
    369  1.92   hannken 				goto bad;
    370  1.92   hannken 			}
    371  1.92   hannken 			closef(fp);
    372  1.92   hannken 		}
    373  1.92   hannken 		break;
    374  1.92   hannken 
    375  1.92   hannken 	case PFSfile:	/* /proc/N/file = -rw------- */
    376  1.92   hannken 	case PFSmem:	/* /proc/N/mem = -rw------- */
    377  1.92   hannken 	case PFSregs:	/* /proc/N/regs = -rw------- */
    378  1.92   hannken 	case PFSfpregs:	/* /proc/N/fpregs = -rw------- */
    379  1.92   hannken 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    380  1.92   hannken 		vp->v_type = VREG;
    381  1.92   hannken 		break;
    382  1.92   hannken 
    383  1.92   hannken 	case PFSctl:	/* /proc/N/ctl = --w------ */
    384  1.92   hannken 	case PFSnote:	/* /proc/N/note = --w------ */
    385  1.92   hannken 	case PFSnotepg:	/* /proc/N/notepg = --w------ */
    386  1.92   hannken 		pfs->pfs_mode = S_IWUSR;
    387  1.92   hannken 		vp->v_type = VREG;
    388  1.92   hannken 		break;
    389  1.92   hannken 
    390  1.92   hannken 	case PFSmap:	/* /proc/N/map = -r--r--r-- */
    391  1.92   hannken 	case PFSmaps:	/* /proc/N/maps = -r--r--r-- */
    392  1.92   hannken 	case PFSstatus:	/* /proc/N/status = -r--r--r-- */
    393  1.92   hannken 	case PFSstat:	/* /proc/N/stat = -r--r--r-- */
    394  1.92   hannken 	case PFScmdline:	/* /proc/N/cmdline = -r--r--r-- */
    395  1.92   hannken 	case PFSemul:	/* /proc/N/emul = -r--r--r-- */
    396  1.92   hannken 	case PFSmeminfo:	/* /proc/meminfo = -r--r--r-- */
    397  1.92   hannken 	case PFScpustat:	/* /proc/stat = -r--r--r-- */
    398  1.92   hannken 	case PFSdevices:	/* /proc/devices = -r--r--r-- */
    399  1.92   hannken 	case PFScpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    400  1.92   hannken 	case PFSuptime:	/* /proc/uptime = -r--r--r-- */
    401  1.92   hannken 	case PFSmounts:	/* /proc/mounts = -r--r--r-- */
    402  1.92   hannken 	case PFSloadavg:	/* /proc/loadavg = -r--r--r-- */
    403  1.92   hannken 	case PFSstatm:	/* /proc/N/statm = -r--r--r-- */
    404  1.92   hannken 	case PFSversion:	/* /proc/version = -r--r--r-- */
    405  1.92   hannken 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    406  1.92   hannken 		vp->v_type = VREG;
    407  1.92   hannken 		break;
    408  1.92   hannken 
    409  1.92   hannken #ifdef __HAVE_PROCFS_MACHDEP
    410  1.92   hannken 	PROCFS_MACHDEP_NODETYPE_CASES
    411  1.92   hannken 		procfs_machdep_allocvp(vp);
    412  1.92   hannken 		break;
    413  1.92   hannken #endif
    414  1.92   hannken 
    415  1.92   hannken 	default:
    416  1.92   hannken 		panic("procfs_allocvp");
    417  1.92   hannken 	}
    418  1.92   hannken 
    419  1.92   hannken 	uvm_vnp_setsize(vp, 0);
    420  1.92   hannken 	*new_key = &pfs->pfs_key;
    421  1.92   hannken 
    422  1.92   hannken 	return 0;
    423  1.92   hannken 
    424  1.92   hannken bad:
    425  1.92   hannken 	vp->v_tag =VT_NON;
    426  1.92   hannken 	vp->v_type = VNON;
    427  1.92   hannken 	vp->v_op = NULL;
    428  1.92   hannken 	vp->v_data = NULL;
    429  1.92   hannken 	kmem_free(pfs, sizeof(*pfs));
    430  1.92   hannken 	return error;
    431  1.92   hannken }
    432  1.92   hannken 
    433  1.25  christos void
    434  1.83    cegger procfs_init(void)
    435   1.1        pk {
    436  1.92   hannken 
    437   1.1        pk }
    438   1.9       cgd 
    439  1.33  jdolecek void
    440  1.83    cegger procfs_reinit(void)
    441  1.40       chs {
    442  1.92   hannken 
    443  1.40       chs }
    444  1.40       chs 
    445  1.40       chs void
    446  1.83    cegger procfs_done(void)
    447  1.33  jdolecek {
    448  1.92   hannken 
    449  1.33  jdolecek }
    450  1.33  jdolecek 
    451  1.37  jdolecek extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
    452  1.27   thorpej 
    453  1.37  jdolecek const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
    454  1.27   thorpej 	&procfs_vnodeop_opv_desc,
    455  1.27   thorpej 	NULL,
    456  1.27   thorpej };
    457  1.27   thorpej 
    458   1.9       cgd struct vfsops procfs_vfsops = {
    459  1.90   hannken 	.vfs_name = MOUNT_PROCFS,
    460  1.90   hannken 	.vfs_min_mount_data = sizeof (struct procfs_args),
    461  1.90   hannken 	.vfs_mount = procfs_mount,
    462  1.90   hannken 	.vfs_start = procfs_start,
    463  1.90   hannken 	.vfs_unmount = procfs_unmount,
    464  1.90   hannken 	.vfs_root = procfs_root,
    465  1.90   hannken 	.vfs_quotactl = (void *)eopnotsupp,
    466  1.90   hannken 	.vfs_statvfs = procfs_statvfs,
    467  1.90   hannken 	.vfs_sync = procfs_sync,
    468  1.90   hannken 	.vfs_vget = procfs_vget,
    469  1.92   hannken 	.vfs_loadvnode = procfs_loadvnode,
    470  1.90   hannken 	.vfs_fhtovp = (void *)eopnotsupp,
    471  1.90   hannken 	.vfs_vptofh = (void *)eopnotsupp,
    472  1.90   hannken 	.vfs_init = procfs_init,
    473  1.90   hannken 	.vfs_reinit = procfs_reinit,
    474  1.90   hannken 	.vfs_done = procfs_done,
    475  1.90   hannken 	.vfs_snapshot = (void *)eopnotsupp,
    476  1.90   hannken 	.vfs_extattrctl = vfs_stdextattrctl,
    477  1.90   hannken 	.vfs_suspendctl = (void *)eopnotsupp,
    478  1.90   hannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
    479  1.90   hannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
    480  1.90   hannken 	.vfs_fsync = (void *)eopnotsupp,
    481  1.90   hannken 	.vfs_opv_descs = procfs_vnodeopv_descs
    482   1.9       cgd };
    483  1.79    rumble 
    484  1.79    rumble static int
    485  1.84      elad procfs_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    486  1.84      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    487  1.84      elad {
    488  1.84      elad 	struct proc *p;
    489  1.84      elad 	struct pfsnode *pfs;
    490  1.84      elad 	enum kauth_process_req req;
    491  1.84      elad 	int result;
    492  1.84      elad 
    493  1.84      elad 	result = KAUTH_RESULT_DEFER;
    494  1.84      elad 	p = arg0;
    495  1.84      elad 	pfs = arg1;
    496  1.84      elad 	req = (enum kauth_process_req)(unsigned long)arg2;
    497  1.84      elad 
    498  1.84      elad 	if (action != KAUTH_PROCESS_PROCFS)
    499  1.84      elad 		return result;
    500  1.84      elad 
    501  1.84      elad 	/* Privileged; let secmodel handle that. */
    502  1.84      elad 	if (req == KAUTH_REQ_PROCESS_PROCFS_CTL)
    503  1.84      elad 		return result;
    504  1.84      elad 
    505  1.84      elad 	switch (pfs->pfs_type) {
    506  1.84      elad 	case PFSregs:
    507  1.84      elad 	case PFSfpregs:
    508  1.84      elad 	case PFSmem:
    509  1.84      elad 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    510  1.84      elad 		    ISSET(p->p_flag, PK_SUGID))
    511  1.84      elad 			break;
    512  1.84      elad 
    513  1.84      elad 		/*FALLTHROUGH*/
    514  1.84      elad 	default:
    515  1.84      elad 		result = KAUTH_RESULT_ALLOW;
    516  1.84      elad 		break;
    517  1.84      elad 	}
    518  1.84      elad 
    519  1.84      elad 	return result;
    520  1.84      elad }
    521  1.84      elad 
    522  1.84      elad 
    523  1.84      elad static int
    524  1.79    rumble procfs_modcmd(modcmd_t cmd, void *arg)
    525  1.79    rumble {
    526  1.81    rumble 	int error;
    527  1.79    rumble 
    528  1.79    rumble 	switch (cmd) {
    529  1.79    rumble 	case MODULE_CMD_INIT:
    530  1.81    rumble 		error = vfs_attach(&procfs_vfsops);
    531  1.81    rumble 		if (error != 0)
    532  1.81    rumble 			break;
    533  1.81    rumble 		sysctl_createv(&procfs_sysctl_log, 0, NULL, NULL,
    534  1.81    rumble 			       CTLFLAG_PERMANENT,
    535  1.81    rumble 			       CTLTYPE_NODE, "procfs",
    536  1.81    rumble 			       SYSCTL_DESCR("Process file system"),
    537  1.81    rumble 			       NULL, 0, NULL, 0,
    538  1.81    rumble 			       CTL_VFS, 12, CTL_EOL);
    539  1.81    rumble 		/*
    540  1.81    rumble 		 * XXX the "12" above could be dynamic, thereby eliminating
    541  1.81    rumble 		 * one more instance of the "number to vfs" mapping problem,
    542  1.81    rumble 		 * but "12" is the order as taken from sys/mount.h
    543  1.81    rumble 		 */
    544  1.84      elad 
    545  1.84      elad 		procfs_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    546  1.84      elad 		    procfs_listener_cb, NULL);
    547  1.84      elad 
    548  1.81    rumble 		break;
    549  1.79    rumble 	case MODULE_CMD_FINI:
    550  1.81    rumble 		error = vfs_detach(&procfs_vfsops);
    551  1.81    rumble 		if (error != 0)
    552  1.81    rumble 			break;
    553  1.81    rumble 		sysctl_teardown(&procfs_sysctl_log);
    554  1.84      elad 		kauth_unlisten_scope(procfs_listener);
    555  1.81    rumble 		break;
    556  1.79    rumble 	default:
    557  1.81    rumble 		error = ENOTTY;
    558  1.81    rumble 		break;
    559  1.79    rumble 	}
    560  1.81    rumble 
    561  1.81    rumble 	return (error);
    562  1.79    rumble }
    563